cmCursesStringWidget.cxx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "cmCursesStringWidget.h"
  2. inline int ctrl(int z)
  3. {
  4. return (z&037);
  5. }
  6. cmCursesStringWidget::cmCursesStringWidget(int width, int height,
  7. int left, int top) :
  8. cmCursesWidget(width, height, left, top)
  9. {
  10. m_InEdit = false;
  11. m_Type = cmCacheManager::STRING;
  12. set_field_fore(m_Field, A_NORMAL);
  13. set_field_back(m_Field, A_STANDOUT);
  14. field_opts_off(m_Field, O_STATIC);
  15. }
  16. bool cmCursesStringWidget::HandleInput(int& key, FORM* form, WINDOW* w)
  17. {
  18. // 10 == enter
  19. if (!m_InEdit && ( key != 10 ) )
  20. {
  21. return false;
  22. }
  23. char* originalStr=0;
  24. // <Enter> is used to change edit mode (like <Esc> in vi).
  25. while(1)
  26. {
  27. if (!m_InEdit && ( key != 10 ) )
  28. {
  29. return false;
  30. }
  31. // 10 == enter
  32. if (key == 10)
  33. {
  34. if (m_InEdit)
  35. {
  36. m_InEdit = false;
  37. delete[] originalStr;
  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. return true;
  42. }
  43. else
  44. {
  45. m_InEdit = true;
  46. char* buf = field_buffer(m_Field, 0);
  47. originalStr = new char[strlen(buf)+1];
  48. strcpy(originalStr, buf);
  49. }
  50. }
  51. else if (key == 27)
  52. {
  53. if (m_InEdit)
  54. {
  55. m_InEdit = false;
  56. this->SetString(originalStr);
  57. delete[] originalStr;
  58. touchwin(w);
  59. wrefresh(w);
  60. return true;
  61. }
  62. }
  63. else if ( key == KEY_LEFT || key == ctrl('b') )
  64. {
  65. form_driver(form, REQ_PREV_CHAR);
  66. }
  67. else if ( key == KEY_RIGHT || key == ctrl('f') )
  68. {
  69. form_driver(form, REQ_NEXT_CHAR);
  70. }
  71. else if ( key == ctrl('d') || key == 127 )
  72. {
  73. form_driver(form, REQ_DEL_PREV);
  74. }
  75. else
  76. {
  77. form_driver(form, key);
  78. }
  79. touchwin(w);
  80. wrefresh(w);
  81. key=getch();
  82. }
  83. }
  84. void cmCursesStringWidget::SetString(const char* value)
  85. {
  86. this->SetValue(value);
  87. }
  88. const char* cmCursesStringWidget::GetString()
  89. {
  90. return this->GetValue();
  91. }
  92. const char* cmCursesStringWidget::GetValue()
  93. {
  94. std::cout << field_buffer(m_Field, 0) << std::endl;
  95. return field_buffer(m_Field, 0);
  96. }