cmCursesStringWidget.cxx 5.6 KB

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