cmCursesStringWidget.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. }
  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, y;
  164. getmaxyx(stdscr, y, x);
  165. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  166. return false;
  167. }
  168. if (this->InEdit) {
  169. char fmt_s[] = "%s";
  170. char firstLine[512];
  171. // Clean the toolbar
  172. for (int i = 0; i < 512; i++) {
  173. firstLine[i] = ' ';
  174. }
  175. firstLine[511] = '\0';
  176. curses_move(y - 4, 0);
  177. printw(fmt_s, firstLine);
  178. curses_move(y - 3, 0);
  179. printw(fmt_s, firstLine);
  180. curses_move(y - 2, 0);
  181. printw(fmt_s, firstLine);
  182. curses_move(y - 1, 0);
  183. printw(fmt_s, firstLine);
  184. curses_move(y - 3, 0);
  185. printw(fmt_s, "Editing option, press [enter] to leave edit.");
  186. return true;
  187. }
  188. return false;
  189. }