cmCursesStringWidget.cxx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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,
  17. int left, int top) :
  18. cmCursesWidget(width, height, left, top)
  19. {
  20. this->InEdit = false;
  21. this->Type = cmCacheManager::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. {
  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. }
  44. else
  45. {
  46. cmCursesForm::LogMessage("String widget entering edit.");
  47. this->InEdit = true;
  48. fm->PrintKeys();
  49. char* buf = field_buffer(this->Field, 0);
  50. this->OriginalString = new char[strlen(buf)+1];
  51. strcpy(this->OriginalString, buf);
  52. }
  53. }
  54. void cmCursesStringWidget::OnType(int& key, cmCursesMainForm* fm, WINDOW*)
  55. {
  56. form_driver(fm->GetForm(), key);
  57. }
  58. bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
  59. WINDOW* w)
  60. {
  61. int x,y;
  62. FORM* form = fm->GetForm();
  63. // 10 == enter
  64. if (!this->InEdit && ( key != 10 && key != KEY_ENTER ) )
  65. {
  66. return false;
  67. }
  68. this->OriginalString=0;
  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. {
  74. sprintf(debugMessage, "String widget handling input, key: %d", key);
  75. cmCursesForm::LogMessage(debugMessage);
  76. fm->PrintKeys();
  77. getmaxyx(stdscr, y, x);
  78. // If window too small, handle 'q' only
  79. if ( x < cmCursesMainForm::MIN_WIDTH ||
  80. y < cmCursesMainForm::MIN_HEIGHT )
  81. {
  82. // quit
  83. if ( key == 'q' )
  84. {
  85. return false;
  86. }
  87. else
  88. {
  89. key=getch();
  90. continue;
  91. }
  92. }
  93. // If resize occured during edit, move out of edit mode
  94. if (!this->InEdit && ( key != 10 && key != KEY_ENTER ) )
  95. {
  96. return false;
  97. }
  98. // 10 == enter
  99. if (key == 10 || key == KEY_ENTER)
  100. {
  101. this->OnReturn(fm, w);
  102. }
  103. else if ( key == KEY_DOWN || key == ctrl('n') ||
  104. key == KEY_UP || key == ctrl('p') ||
  105. key == KEY_NPAGE || key == ctrl('d') ||
  106. key == KEY_PPAGE || key == ctrl('u'))
  107. {
  108. this->InEdit = false;
  109. delete[] this->OriginalString;
  110. // trick to force forms to update the field buffer
  111. form_driver(form, REQ_NEXT_FIELD);
  112. form_driver(form, REQ_PREV_FIELD);
  113. return false;
  114. }
  115. // esc
  116. else if (key == 27)
  117. {
  118. if (this->InEdit)
  119. {
  120. this->InEdit = false;
  121. fm->PrintKeys();
  122. this->SetString(this->OriginalString);
  123. delete[] this->OriginalString;
  124. touchwin(w);
  125. wrefresh(w);
  126. return true;
  127. }
  128. }
  129. else if ( key == 9 )
  130. {
  131. this->OnTab(fm, w);
  132. }
  133. else if ( key == KEY_LEFT || key == ctrl('b') )
  134. {
  135. form_driver(form, REQ_PREV_CHAR);
  136. }
  137. else if ( key == KEY_RIGHT || key == ctrl('f') )
  138. {
  139. form_driver(form, REQ_NEXT_CHAR);
  140. }
  141. else if ( key == ctrl('k') )
  142. {
  143. form_driver(form, REQ_CLR_EOL);
  144. }
  145. else if ( key == ctrl('a') || key == KEY_HOME )
  146. {
  147. form_driver(form, REQ_BEG_FIELD);
  148. }
  149. else if ( key == ctrl('e') || key == KEY_END )
  150. {
  151. form_driver(form, REQ_END_FIELD);
  152. }
  153. else if ( key == 127 ||
  154. key == KEY_BACKSPACE )
  155. {
  156. if ( form->curcol > 0 )
  157. {
  158. form_driver(form, REQ_DEL_PREV);
  159. }
  160. }
  161. else if ( key == ctrl('d') ||key == KEY_DC )
  162. {
  163. if ( form->curcol >= 0 )
  164. {
  165. form_driver(form, REQ_DEL_CHAR);
  166. }
  167. }
  168. else
  169. {
  170. this->OnType(key, fm, w);
  171. }
  172. if ( !this->Done )
  173. {
  174. touchwin(w);
  175. wrefresh(w);
  176. key=getch();
  177. }
  178. }
  179. return true;
  180. }
  181. void cmCursesStringWidget::SetString(const char* value)
  182. {
  183. this->SetValue(value);
  184. }
  185. const char* cmCursesStringWidget::GetString()
  186. {
  187. return this->GetValue();
  188. }
  189. const char* cmCursesStringWidget::GetValue()
  190. {
  191. return field_buffer(this->Field, 0);
  192. }
  193. bool cmCursesStringWidget::PrintKeys()
  194. {
  195. int x,y;
  196. getmaxyx(stdscr, y, x);
  197. if ( x < cmCursesMainForm::MIN_WIDTH ||
  198. y < cmCursesMainForm::MIN_HEIGHT )
  199. {
  200. return false;
  201. }
  202. if (this->InEdit)
  203. {
  204. char firstLine[512];
  205. // Clean the toolbar
  206. for(int i=0; i<512; i++)
  207. {
  208. firstLine[i] = ' ';
  209. }
  210. firstLine[511] = '\0';
  211. curses_move(y-4,0);
  212. printw(firstLine);
  213. curses_move(y-3,0);
  214. printw(firstLine);
  215. curses_move(y-2,0);
  216. printw(firstLine);
  217. curses_move(y-1,0);
  218. printw(firstLine);
  219. sprintf(firstLine, "Editing option, press [enter] to leave edit.");
  220. curses_move(y-3,0);
  221. printw(firstLine);
  222. return true;
  223. }
  224. else
  225. {
  226. return false;
  227. }
  228. }