cmCursesStringWidget.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. snprintf(debugMessage, sizeof(debugMessage),
  77. "String widget handling input, key: %d", key);
  78. cmCursesForm::LogMessage(debugMessage);
  79. fm->PrintKeys();
  80. getmaxyx(stdscr, y, x);
  81. // If window too small, handle 'q' only
  82. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  83. // quit
  84. if (key == 'q') {
  85. return false;
  86. }
  87. key = getch();
  88. continue;
  89. }
  90. // If resize occurred during edit, move out of edit mode
  91. if (!this->InEdit && (key != 10 && key != KEY_ENTER && key != 'i')) {
  92. return false;
  93. }
  94. // toggle edit with return
  95. if ((key == 10 || key == KEY_ENTER)
  96. // enter edit with i (and not-edit mode)
  97. || (!this->InEdit && key == 'i')) {
  98. this->OnReturn(fm, w);
  99. } else if (key == KEY_DOWN || key == ctrl('n') || key == KEY_UP ||
  100. key == ctrl('p') || key == KEY_NPAGE || key == ctrl('d') ||
  101. key == KEY_PPAGE || key == ctrl('u')) {
  102. this->InEdit = false;
  103. this->OriginalString.clear();
  104. // trick to force forms to update the field buffer
  105. form_driver(form, REQ_NEXT_FIELD);
  106. form_driver(form, REQ_PREV_FIELD);
  107. return false;
  108. }
  109. // esc
  110. else if (key == 27) {
  111. if (this->InEdit) {
  112. this->InEdit = false;
  113. fm->PrintKeys();
  114. this->SetString(this->OriginalString);
  115. this->OriginalString.clear();
  116. touchwin(w);
  117. wrefresh(w);
  118. return true;
  119. }
  120. } else if (key == 9) {
  121. this->OnTab(fm, w);
  122. } else if (key == KEY_LEFT || key == ctrl('b')) {
  123. form_driver(form, REQ_PREV_CHAR);
  124. } else if (key == KEY_RIGHT || key == ctrl('f')) {
  125. form_driver(form, REQ_NEXT_CHAR);
  126. } else if (key == ctrl('k')) {
  127. form_driver(form, REQ_CLR_EOL);
  128. } else if (key == ctrl('a') || key == KEY_HOME) {
  129. form_driver(form, REQ_BEG_FIELD);
  130. } else if (key == ctrl('e') || key == KEY_END) {
  131. form_driver(form, REQ_END_FIELD);
  132. } else if (key == 127 || key == KEY_BACKSPACE) {
  133. FIELD* cur = current_field(form);
  134. form_driver(form, REQ_DEL_PREV);
  135. if (current_field(form) != cur) {
  136. set_current_field(form, cur);
  137. }
  138. } else if (key == ctrl('d') || key == KEY_DC) {
  139. form_driver(form, REQ_DEL_CHAR);
  140. } else {
  141. this->OnType(key, fm, w);
  142. }
  143. if (!this->Done) {
  144. touchwin(w);
  145. wrefresh(w);
  146. key = getch();
  147. }
  148. }
  149. return true;
  150. }
  151. void cmCursesStringWidget::SetString(const std::string& value)
  152. {
  153. this->SetValue(value);
  154. }
  155. const char* cmCursesStringWidget::GetString()
  156. {
  157. return this->GetValue();
  158. }
  159. const char* cmCursesStringWidget::GetValue()
  160. {
  161. return field_buffer(this->Field, 0);
  162. }
  163. bool cmCursesStringWidget::PrintKeys()
  164. {
  165. int x;
  166. int y;
  167. getmaxyx(stdscr, y, x);
  168. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  169. return false;
  170. }
  171. if (this->InEdit) {
  172. char fmt_s[] = "%s";
  173. // Clean the toolbar
  174. curses_move(y - 4, 0);
  175. clrtoeol();
  176. curses_move(y - 3, 0);
  177. printw(fmt_s, "Editing option, press [enter] to confirm");
  178. clrtoeol();
  179. curses_move(y - 2, 0);
  180. printw(fmt_s, " press [esc] to cancel");
  181. clrtoeol();
  182. curses_move(y - 1, 0);
  183. clrtoeol();
  184. return true;
  185. }
  186. return false;
  187. }