cmCursesStringWidget.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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. bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
  30. WINDOW* w)
  31. {
  32. int x,y;
  33. FORM* form = fm->GetForm();
  34. // 10 == enter
  35. if (!m_InEdit && ( key != 10 ) )
  36. {
  37. return false;
  38. }
  39. char* originalStr=0;
  40. char debugMessage[128];
  41. // <Enter> is used to change edit mode (like <Esc> in vi).
  42. while(1)
  43. {
  44. sprintf(debugMessage, "String widget handling input, key: %d", key);
  45. cmCursesForm::LogMessage(debugMessage);
  46. fm->PrintKeys();
  47. getmaxyx(stdscr, y, x);
  48. // If window too small, handle 'q' only
  49. if ( x < cmCursesMainForm::MIN_WIDTH ||
  50. y < cmCursesMainForm::MIN_HEIGHT )
  51. {
  52. // quit
  53. if ( key == 'q' )
  54. {
  55. return false;
  56. }
  57. else
  58. {
  59. key=getch();
  60. continue;
  61. }
  62. }
  63. // If resize occured during edit, move out of edit mode
  64. if (!m_InEdit && ( key != 10 && key != KEY_ENTER ) )
  65. {
  66. return false;
  67. }
  68. // 10 == enter
  69. if (key == 10 || key == KEY_ENTER)
  70. {
  71. if (m_InEdit)
  72. {
  73. cmCursesForm::LogMessage("String widget leaving edit.");
  74. m_InEdit = false;
  75. fm->PrintKeys();
  76. delete[] originalStr;
  77. // trick to force forms to update the field buffer
  78. form_driver(form, REQ_NEXT_FIELD);
  79. form_driver(form, REQ_PREV_FIELD);
  80. return true;
  81. }
  82. else
  83. {
  84. cmCursesForm::LogMessage("String widget entering edit.");
  85. m_InEdit = true;
  86. fm->PrintKeys();
  87. char* buf = field_buffer(m_Field, 0);
  88. originalStr = new char[strlen(buf)+1];
  89. strcpy(originalStr, buf);
  90. }
  91. }
  92. else if ( key == KEY_DOWN || key == ctrl('n') ||
  93. key == KEY_UP || key == ctrl('p') ||
  94. key == KEY_NPAGE || key == ctrl('d') ||
  95. key == KEY_PPAGE || key == ctrl('u'))
  96. {
  97. m_InEdit = false;
  98. delete[] originalStr;
  99. // trick to force forms to update the field buffer
  100. form_driver(form, REQ_NEXT_FIELD);
  101. form_driver(form, REQ_PREV_FIELD);
  102. return false;
  103. }
  104. // esc
  105. else if (key == 27)
  106. {
  107. if (m_InEdit)
  108. {
  109. m_InEdit = false;
  110. fm->PrintKeys();
  111. this->SetString(originalStr);
  112. delete[] originalStr;
  113. touchwin(w);
  114. wrefresh(w);
  115. return true;
  116. }
  117. }
  118. else if ( key == KEY_LEFT || key == ctrl('b') )
  119. {
  120. form_driver(form, REQ_PREV_CHAR);
  121. }
  122. else if ( key == KEY_RIGHT || key == ctrl('f') )
  123. {
  124. form_driver(form, REQ_NEXT_CHAR);
  125. }
  126. else if ( key == ctrl('k') )
  127. {
  128. form_driver(form, REQ_CLR_EOL);
  129. }
  130. else if ( key == ctrl('a') )
  131. {
  132. form_driver(form, REQ_BEG_FIELD);
  133. }
  134. else if ( key == ctrl('e') )
  135. {
  136. form_driver(form, REQ_END_FIELD);
  137. }
  138. else if ( key == ctrl('d') || key == 127 ||
  139. key == KEY_BACKSPACE || key == KEY_DC )
  140. {
  141. form_driver(form, REQ_DEL_PREV);
  142. }
  143. else
  144. {
  145. form_driver(form, key);
  146. }
  147. touchwin(w);
  148. wrefresh(w);
  149. key=getch();
  150. }
  151. }
  152. void cmCursesStringWidget::SetString(const char* value)
  153. {
  154. this->SetValue(value);
  155. }
  156. const char* cmCursesStringWidget::GetString()
  157. {
  158. return this->GetValue();
  159. }
  160. const char* cmCursesStringWidget::GetValue()
  161. {
  162. return field_buffer(m_Field, 0);
  163. }
  164. bool cmCursesStringWidget::PrintKeys()
  165. {
  166. int x,y;
  167. getmaxyx(stdscr, y, x);
  168. if ( x < cmCursesMainForm::MIN_WIDTH ||
  169. y < cmCursesMainForm::MIN_HEIGHT )
  170. {
  171. return false;
  172. }
  173. if (m_InEdit)
  174. {
  175. char firstLine[512];
  176. // Clean the toolbar
  177. for(int i=0; i<512; i++)
  178. {
  179. firstLine[i] = ' ';
  180. }
  181. firstLine[511] = '\0';
  182. curses_move(y-4,0);
  183. printw(firstLine);
  184. curses_move(y-3,0);
  185. printw(firstLine);
  186. curses_move(y-2,0);
  187. printw(firstLine);
  188. curses_move(y-1,0);
  189. printw(firstLine);
  190. sprintf(firstLine, "Editing option, press [enter] to leave edit.");
  191. curses_move(y-3,0);
  192. printw(firstLine);
  193. return true;
  194. }
  195. else
  196. {
  197. return false;
  198. }
  199. }