cmCursesStringWidget.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCursesStringWidget.h"
  11. #include "cmCursesMainForm.h"
  12. inline int ctrl(int z)
  13. {
  14. return (z & 037);
  15. }
  16. cmCursesStringWidget::cmCursesStringWidget(int width, int height, int left,
  17. int top)
  18. : cmCursesWidget(width, height, left, top)
  19. {
  20. this->InEdit = false;
  21. this->Type = cmState::STRING;
  22. set_field_fore(this->Field, A_NORMAL);
  23. set_field_back(this->Field, A_STANDOUT);
  24. field_opts_off(this->Field, O_STATIC);
  25. }
  26. void cmCursesStringWidget::OnTab(cmCursesMainForm* /*unused*/,
  27. WINDOW* /*unused*/)
  28. {
  29. // FORM* form = fm->GetForm();
  30. }
  31. void cmCursesStringWidget::OnReturn(cmCursesMainForm* fm, WINDOW* /*unused*/)
  32. {
  33. FORM* form = fm->GetForm();
  34. if (this->InEdit) {
  35. cmCursesForm::LogMessage("String widget leaving edit.");
  36. this->InEdit = false;
  37. fm->PrintKeys();
  38. delete[] this->OriginalString;
  39. // trick to force forms to update the field buffer
  40. form_driver(form, REQ_NEXT_FIELD);
  41. form_driver(form, REQ_PREV_FIELD);
  42. this->Done = true;
  43. } else {
  44. cmCursesForm::LogMessage("String widget entering edit.");
  45. this->InEdit = true;
  46. fm->PrintKeys();
  47. char* buf = field_buffer(this->Field, 0);
  48. this->OriginalString = new char[strlen(buf) + 1];
  49. strcpy(this->OriginalString, buf);
  50. }
  51. }
  52. void cmCursesStringWidget::OnType(int& key, cmCursesMainForm* fm,
  53. WINDOW* /*unused*/)
  54. {
  55. form_driver(fm->GetForm(), key);
  56. }
  57. bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
  58. WINDOW* w)
  59. {
  60. int x, 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 = CM_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. } else {
  83. key = getch();
  84. continue;
  85. }
  86. }
  87. // If resize occurred during edit, move out of edit mode
  88. if (!this->InEdit && (key != 10 && key != KEY_ENTER && key != 'i')) {
  89. return false;
  90. }
  91. // enter edit with return and i (vim binding)
  92. if (!this->InEdit && (key == 10 || key == KEY_ENTER || key == 'i')) {
  93. this->OnReturn(fm, w);
  94. }
  95. // leave edit with return (but not i -- not a toggle)
  96. else if (this->InEdit && (key == 10 || key == KEY_ENTER)) {
  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. delete[] this->OriginalString;
  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. delete[] this->OriginalString;
  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, 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. for (int i = 0; i < 512; i++) {
  174. firstLine[i] = ' ';
  175. }
  176. firstLine[511] = '\0';
  177. curses_move(y - 4, 0);
  178. printw(fmt_s, firstLine);
  179. curses_move(y - 3, 0);
  180. printw(fmt_s, firstLine);
  181. curses_move(y - 2, 0);
  182. printw(fmt_s, firstLine);
  183. curses_move(y - 1, 0);
  184. printw(fmt_s, firstLine);
  185. curses_move(y - 3, 0);
  186. printw(fmt_s, "Editing option, press [enter] to leave edit.");
  187. return true;
  188. } else {
  189. return false;
  190. }
  191. }