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