cmCursesStringWidget.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCursesStringWidget.h"
  14. #include "cmCursesMainForm.h"
  15. inline int ctrl(int z)
  16. {
  17. return (z&037);
  18. }
  19. cmCursesStringWidget::cmCursesStringWidget(int width, int height,
  20. int left, int top) :
  21. cmCursesWidget(width, height, left, top)
  22. {
  23. m_InEdit = false;
  24. m_Type = cmCacheManager::STRING;
  25. set_field_fore(m_Field, A_NORMAL);
  26. set_field_back(m_Field, A_STANDOUT);
  27. field_opts_off(m_Field, O_STATIC);
  28. }
  29. void cmCursesStringWidget::OnTab(cmCursesMainForm*, WINDOW*)
  30. {
  31. //FORM* form = fm->GetForm();
  32. }
  33. void cmCursesStringWidget::OnReturn(cmCursesMainForm* fm, WINDOW*)
  34. {
  35. FORM* form = fm->GetForm();
  36. if (m_InEdit)
  37. {
  38. cmCursesForm::LogMessage("String widget leaving edit.");
  39. m_InEdit = false;
  40. fm->PrintKeys();
  41. delete[] m_OriginalString;
  42. // trick to force forms to update the field buffer
  43. form_driver(form, REQ_NEXT_FIELD);
  44. form_driver(form, REQ_PREV_FIELD);
  45. m_Done = true;
  46. }
  47. else
  48. {
  49. cmCursesForm::LogMessage("String widget entering edit.");
  50. m_InEdit = true;
  51. fm->PrintKeys();
  52. char* buf = field_buffer(m_Field, 0);
  53. m_OriginalString = new char[strlen(buf)+1];
  54. strcpy(m_OriginalString, buf);
  55. }
  56. }
  57. void cmCursesStringWidget::OnType(int& key, cmCursesMainForm* fm, WINDOW*)
  58. {
  59. form_driver(fm->GetForm(), key);
  60. }
  61. bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
  62. WINDOW* w)
  63. {
  64. int x,y;
  65. FORM* form = fm->GetForm();
  66. // 10 == enter
  67. if (!m_InEdit && ( key != 10 && key != KEY_ENTER ) )
  68. {
  69. return false;
  70. }
  71. m_OriginalString=0;
  72. m_Done = false;
  73. char debugMessage[128];
  74. // <Enter> is used to change edit mode (like <Esc> in vi).
  75. while(!m_Done)
  76. {
  77. sprintf(debugMessage, "String widget handling input, key: %d", key);
  78. cmCursesForm::LogMessage(debugMessage);
  79. fm->PrintKeys();
  80. getmaxyx(stdscr, y, x);
  81. // If window too small, handle 'q' only
  82. if ( x < cmCursesMainForm::MIN_WIDTH ||
  83. y < cmCursesMainForm::MIN_HEIGHT )
  84. {
  85. // quit
  86. if ( key == 'q' )
  87. {
  88. return false;
  89. }
  90. else
  91. {
  92. key=getch();
  93. continue;
  94. }
  95. }
  96. // If resize occured during edit, move out of edit mode
  97. if (!m_InEdit && ( key != 10 && key != KEY_ENTER ) )
  98. {
  99. return false;
  100. }
  101. // 10 == enter
  102. if (key == 10 || key == KEY_ENTER)
  103. {
  104. this->OnReturn(fm, w);
  105. }
  106. else if ( key == KEY_DOWN || key == ctrl('n') ||
  107. key == KEY_UP || key == ctrl('p') ||
  108. key == KEY_NPAGE || key == ctrl('d') ||
  109. key == KEY_PPAGE || key == ctrl('u'))
  110. {
  111. m_InEdit = false;
  112. delete[] m_OriginalString;
  113. // trick to force forms to update the field buffer
  114. form_driver(form, REQ_NEXT_FIELD);
  115. form_driver(form, REQ_PREV_FIELD);
  116. return false;
  117. }
  118. // esc
  119. else if (key == 27)
  120. {
  121. if (m_InEdit)
  122. {
  123. m_InEdit = false;
  124. fm->PrintKeys();
  125. this->SetString(m_OriginalString);
  126. delete[] m_OriginalString;
  127. touchwin(w);
  128. wrefresh(w);
  129. return true;
  130. }
  131. }
  132. else if ( key == 9 )
  133. {
  134. this->OnTab(fm, w);
  135. }
  136. else if ( key == KEY_LEFT || key == ctrl('b') )
  137. {
  138. form_driver(form, REQ_PREV_CHAR);
  139. }
  140. else if ( key == KEY_RIGHT || key == ctrl('f') )
  141. {
  142. form_driver(form, REQ_NEXT_CHAR);
  143. }
  144. else if ( key == ctrl('k') )
  145. {
  146. form_driver(form, REQ_CLR_EOL);
  147. }
  148. else if ( key == ctrl('a') || key == KEY_HOME )
  149. {
  150. form_driver(form, REQ_BEG_FIELD);
  151. }
  152. else if ( key == ctrl('e') || key == KEY_END )
  153. {
  154. form_driver(form, REQ_END_FIELD);
  155. }
  156. else if ( key == ctrl('d') || key == 127 ||
  157. key == KEY_BACKSPACE || key == KEY_DC )
  158. {
  159. if ( form->curcol > 0 )
  160. {
  161. form_driver(form, REQ_DEL_PREV);
  162. }
  163. }
  164. else
  165. {
  166. this->OnType(key, fm, w);
  167. }
  168. if ( !m_Done )
  169. {
  170. touchwin(w);
  171. wrefresh(w);
  172. key=getch();
  173. }
  174. }
  175. return true;
  176. }
  177. void cmCursesStringWidget::SetString(const char* value)
  178. {
  179. this->SetValue(value);
  180. }
  181. const char* cmCursesStringWidget::GetString()
  182. {
  183. return this->GetValue();
  184. }
  185. const char* cmCursesStringWidget::GetValue()
  186. {
  187. return field_buffer(m_Field, 0);
  188. }
  189. bool cmCursesStringWidget::PrintKeys()
  190. {
  191. int x,y;
  192. getmaxyx(stdscr, y, x);
  193. if ( x < cmCursesMainForm::MIN_WIDTH ||
  194. y < cmCursesMainForm::MIN_HEIGHT )
  195. {
  196. return false;
  197. }
  198. if (m_InEdit)
  199. {
  200. char firstLine[512];
  201. // Clean the toolbar
  202. for(int i=0; i<512; i++)
  203. {
  204. firstLine[i] = ' ';
  205. }
  206. firstLine[511] = '\0';
  207. curses_move(y-4,0);
  208. printw(firstLine);
  209. curses_move(y-3,0);
  210. printw(firstLine);
  211. curses_move(y-2,0);
  212. printw(firstLine);
  213. curses_move(y-1,0);
  214. printw(firstLine);
  215. sprintf(firstLine, "Editing option, press [enter] to leave edit.");
  216. curses_move(y-3,0);
  217. printw(firstLine);
  218. return true;
  219. }
  220. else
  221. {
  222. return false;
  223. }
  224. }