cmCursesStringWidget.cxx 5.6 KB

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