cmCursesStringWidget.cxx 5.7 KB

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