cmCursesStringWidget.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCursesStringWidget.h"
  4. #include <cstdio>
  5. #include "cmCursesColor.h"
  6. #include "cmCursesForm.h"
  7. #include "cmCursesMainForm.h"
  8. #include "cmCursesStandardIncludes.h"
  9. #include "cmCursesWidget.h"
  10. #include "cmStateTypes.h"
  11. inline int ctrl(int z)
  12. {
  13. return (z & 037);
  14. }
  15. cmCursesStringWidget::cmCursesStringWidget(int width, int height, int left,
  16. int top)
  17. : cmCursesWidget(width, height, left, top)
  18. {
  19. this->InEdit = false;
  20. this->Type = cmStateEnums::STRING;
  21. if (cmCursesColor::HasColors()) {
  22. set_field_fore(this->Field, COLOR_PAIR(cmCursesColor::String));
  23. set_field_back(this->Field, COLOR_PAIR(cmCursesColor::String));
  24. } else {
  25. set_field_fore(this->Field, A_NORMAL);
  26. set_field_back(this->Field, A_STANDOUT);
  27. }
  28. field_opts_off(this->Field, O_STATIC);
  29. }
  30. void cmCursesStringWidget::OnTab(cmCursesMainForm* /*unused*/,
  31. WINDOW* /*unused*/)
  32. {
  33. // FORM* form = fm->GetForm();
  34. }
  35. void cmCursesStringWidget::OnReturn(cmCursesMainForm* fm, WINDOW* /*unused*/)
  36. {
  37. if (this->InEdit) {
  38. cmCursesForm::LogMessage("String widget leaving edit.");
  39. this->InEdit = false;
  40. fm->PrintKeys();
  41. this->OriginalString.clear();
  42. // trick to force forms to update the field buffer
  43. FORM* form = fm->GetForm();
  44. form_driver(form, REQ_NEXT_FIELD);
  45. form_driver(form, REQ_PREV_FIELD);
  46. this->Done = true;
  47. } else {
  48. cmCursesForm::LogMessage("String widget entering edit.");
  49. this->InEdit = true;
  50. fm->PrintKeys();
  51. this->OriginalString = field_buffer(this->Field, 0);
  52. }
  53. }
  54. void cmCursesStringWidget::OnType(int& key, cmCursesMainForm* fm,
  55. WINDOW* /*unused*/)
  56. {
  57. form_driver(fm->GetForm(), key);
  58. }
  59. bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
  60. WINDOW* w)
  61. {
  62. int x;
  63. int y;
  64. FORM* form = fm->GetForm();
  65. // when not in edit mode, edit mode is entered by pressing enter or i (vim
  66. // binding)
  67. // 10 == enter
  68. if (!this->InEdit && (key != 10 && key != KEY_ENTER && key != 'i')) {
  69. return false;
  70. }
  71. this->OriginalString.clear();
  72. this->Done = false;
  73. char debugMessage[128];
  74. // <Enter> is used to change edit mode (like <Esc> in vi).
  75. while (!this->Done) {
  76. sprintf(debugMessage, "String widget handling input, key: %d", key);
  77. cmCursesForm::LogMessage(debugMessage);
  78. fm->PrintKeys();
  79. getmaxyx(stdscr, y, x);
  80. // If window too small, handle 'q' only
  81. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  82. // quit
  83. if (key == 'q') {
  84. return false;
  85. }
  86. key = getch();
  87. continue;
  88. }
  89. // If resize occurred during edit, move out of edit mode
  90. if (!this->InEdit && (key != 10 && key != KEY_ENTER && key != 'i')) {
  91. return false;
  92. }
  93. // enter edit with return and i (vim binding)
  94. if (!this->InEdit && (key == 10 || key == KEY_ENTER || key == 'i')) {
  95. this->OnReturn(fm, w);
  96. }
  97. // leave edit with return (but not i -- not a toggle)
  98. else if (this->InEdit && (key == 10 || key == KEY_ENTER)) {
  99. this->OnReturn(fm, w);
  100. } else if (key == KEY_DOWN || key == ctrl('n') || key == KEY_UP ||
  101. key == ctrl('p') || key == KEY_NPAGE || key == ctrl('d') ||
  102. key == KEY_PPAGE || key == ctrl('u')) {
  103. this->InEdit = false;
  104. this->OriginalString.clear();
  105. // trick to force forms to update the field buffer
  106. form_driver(form, REQ_NEXT_FIELD);
  107. form_driver(form, REQ_PREV_FIELD);
  108. return false;
  109. }
  110. // esc
  111. else if (key == 27) {
  112. if (this->InEdit) {
  113. this->InEdit = false;
  114. fm->PrintKeys();
  115. this->SetString(this->OriginalString);
  116. this->OriginalString.clear();
  117. touchwin(w);
  118. wrefresh(w);
  119. return true;
  120. }
  121. } else if (key == 9) {
  122. this->OnTab(fm, w);
  123. } else if (key == KEY_LEFT || key == ctrl('b')) {
  124. form_driver(form, REQ_PREV_CHAR);
  125. } else if (key == KEY_RIGHT || key == ctrl('f')) {
  126. form_driver(form, REQ_NEXT_CHAR);
  127. } else if (key == ctrl('k')) {
  128. form_driver(form, REQ_CLR_EOL);
  129. } else if (key == ctrl('a') || key == KEY_HOME) {
  130. form_driver(form, REQ_BEG_FIELD);
  131. } else if (key == ctrl('e') || key == KEY_END) {
  132. form_driver(form, REQ_END_FIELD);
  133. } else if (key == 127 || key == KEY_BACKSPACE) {
  134. FIELD* cur = current_field(form);
  135. form_driver(form, REQ_DEL_PREV);
  136. if (current_field(form) != cur) {
  137. set_current_field(form, cur);
  138. }
  139. } else if (key == ctrl('d') || key == KEY_DC) {
  140. form_driver(form, REQ_DEL_CHAR);
  141. } else {
  142. this->OnType(key, fm, w);
  143. }
  144. if (!this->Done) {
  145. touchwin(w);
  146. wrefresh(w);
  147. key = getch();
  148. }
  149. }
  150. return true;
  151. }
  152. void cmCursesStringWidget::SetString(const std::string& value)
  153. {
  154. this->SetValue(value);
  155. }
  156. const char* cmCursesStringWidget::GetString()
  157. {
  158. return this->GetValue();
  159. }
  160. const char* cmCursesStringWidget::GetValue()
  161. {
  162. return field_buffer(this->Field, 0);
  163. }
  164. bool cmCursesStringWidget::PrintKeys()
  165. {
  166. int x;
  167. int y;
  168. getmaxyx(stdscr, y, x);
  169. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  170. return false;
  171. }
  172. if (this->InEdit) {
  173. char fmt_s[] = "%s";
  174. // Clean the toolbar
  175. curses_move(y - 4, 0);
  176. clrtoeol();
  177. curses_move(y - 3, 0);
  178. printw(fmt_s, "Editing option, press [enter] to confirm");
  179. clrtoeol();
  180. curses_move(y - 2, 0);
  181. printw(fmt_s, " press [esc] to cancel");
  182. clrtoeol();
  183. curses_move(y - 1, 0);
  184. clrtoeol();
  185. return true;
  186. }
  187. return false;
  188. }