cmCursesStringWidget.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "cmCursesStringWidget.h"
  2. #include "cmCursesMainForm.h"
  3. inline int ctrl(int z)
  4. {
  5. return (z&037);
  6. }
  7. cmCursesStringWidget::cmCursesStringWidget(int width, int height,
  8. int left, int top) :
  9. cmCursesWidget(width, height, left, top)
  10. {
  11. m_InEdit = false;
  12. m_Type = cmCacheManager::STRING;
  13. set_field_fore(m_Field, A_NORMAL);
  14. set_field_back(m_Field, A_STANDOUT);
  15. field_opts_off(m_Field, O_STATIC);
  16. }
  17. bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
  18. WINDOW* w)
  19. {
  20. int x,y;
  21. FORM* form = fm->GetForm();
  22. // 10 == enter
  23. if (!m_InEdit && ( key != 10 ) )
  24. {
  25. return false;
  26. }
  27. char* originalStr=0;
  28. char debugMessage[128];
  29. // <Enter> is used to change edit mode (like <Esc> in vi).
  30. while(1)
  31. {
  32. sprintf(debugMessage, "String widget handling input, key: %d", key);
  33. cmCursesForm::LogMessage(debugMessage);
  34. fm->PrintKeys();
  35. getmaxyx(stdscr, y, x);
  36. // If window too small, handle 'q' only
  37. if ( x < cmCursesMainForm::MIN_WIDTH ||
  38. y < cmCursesMainForm::MIN_HEIGHT )
  39. {
  40. // quit
  41. if ( key == 'q' )
  42. {
  43. return false;
  44. }
  45. else
  46. {
  47. key=getch();
  48. continue;
  49. }
  50. }
  51. // If resize occured during edit, move out of edit mode
  52. if (!m_InEdit && ( key != 10 && key != KEY_ENTER ) )
  53. {
  54. return false;
  55. }
  56. // 10 == enter
  57. if (key == 10 || key == KEY_ENTER)
  58. {
  59. if (m_InEdit)
  60. {
  61. cmCursesForm::LogMessage("String widget leaving edit.");
  62. m_InEdit = false;
  63. fm->PrintKeys();
  64. delete[] originalStr;
  65. // trick to force forms to update the field buffer
  66. form_driver(form, REQ_NEXT_FIELD);
  67. form_driver(form, REQ_PREV_FIELD);
  68. return true;
  69. }
  70. else
  71. {
  72. cmCursesForm::LogMessage("String widget entering edit.");
  73. m_InEdit = true;
  74. fm->PrintKeys();
  75. char* buf = field_buffer(m_Field, 0);
  76. originalStr = new char[strlen(buf)+1];
  77. strcpy(originalStr, buf);
  78. }
  79. }
  80. else if ( key == KEY_DOWN || key == ctrl('n') ||
  81. key == KEY_UP || key == ctrl('p') ||
  82. key == KEY_NPAGE || key == ctrl('d') ||
  83. key == KEY_PPAGE || key == ctrl('u'))
  84. {
  85. m_InEdit = false;
  86. delete[] originalStr;
  87. // trick to force forms to update the field buffer
  88. form_driver(form, REQ_NEXT_FIELD);
  89. form_driver(form, REQ_PREV_FIELD);
  90. return false;
  91. }
  92. // esc
  93. else if (key == 27)
  94. {
  95. if (m_InEdit)
  96. {
  97. m_InEdit = false;
  98. fm->PrintKeys();
  99. this->SetString(originalStr);
  100. delete[] originalStr;
  101. touchwin(w);
  102. wrefresh(w);
  103. return true;
  104. }
  105. }
  106. else if ( key == KEY_LEFT || key == ctrl('b') )
  107. {
  108. form_driver(form, REQ_PREV_CHAR);
  109. }
  110. else if ( key == KEY_RIGHT || key == ctrl('f') )
  111. {
  112. form_driver(form, REQ_NEXT_CHAR);
  113. }
  114. else if ( key == ctrl('k') )
  115. {
  116. form_driver(form, REQ_CLR_EOL);
  117. }
  118. else if ( key == ctrl('a') )
  119. {
  120. form_driver(form, REQ_BEG_FIELD);
  121. }
  122. else if ( key == ctrl('e') )
  123. {
  124. form_driver(form, REQ_END_FIELD);
  125. }
  126. else if ( key == ctrl('d') || key == 127 ||
  127. key == KEY_BACKSPACE || key == KEY_DC )
  128. {
  129. form_driver(form, REQ_DEL_PREV);
  130. }
  131. else
  132. {
  133. form_driver(form, key);
  134. }
  135. touchwin(w);
  136. wrefresh(w);
  137. key=getch();
  138. }
  139. }
  140. void cmCursesStringWidget::SetString(const char* value)
  141. {
  142. this->SetValue(value);
  143. }
  144. const char* cmCursesStringWidget::GetString()
  145. {
  146. return this->GetValue();
  147. }
  148. const char* cmCursesStringWidget::GetValue()
  149. {
  150. return field_buffer(m_Field, 0);
  151. }
  152. bool cmCursesStringWidget::PrintKeys()
  153. {
  154. int x,y;
  155. getmaxyx(stdscr, y, x);
  156. if ( x < cmCursesMainForm::MIN_WIDTH ||
  157. y < cmCursesMainForm::MIN_HEIGHT )
  158. {
  159. return false;
  160. }
  161. if (m_InEdit)
  162. {
  163. char firstLine[512];
  164. // Clean the toolbar
  165. for(int i=0; i<512; i++)
  166. {
  167. firstLine[i] = ' ';
  168. }
  169. firstLine[511] = '\0';
  170. curses_move(y-4,0);
  171. printw(firstLine);
  172. curses_move(y-3,0);
  173. printw(firstLine);
  174. curses_move(y-2,0);
  175. printw(firstLine);
  176. curses_move(y-1,0);
  177. printw(firstLine);
  178. sprintf(firstLine, "Editing option, press [enter] to leave edit.");
  179. curses_move(y-3,0);
  180. printw(firstLine);
  181. return true;
  182. }
  183. else
  184. {
  185. return false;
  186. }
  187. }