cmCursesStringWidget.cxx 5.8 KB

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