cmCursesOptionsWidget.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "cmCursesOptionsWidget.h"
  11. #include "cmCursesMainForm.h"
  12. inline int ctrl(int z)
  13. {
  14. return (z&037);
  15. }
  16. cmCursesOptionsWidget::cmCursesOptionsWidget(int width, int height,
  17. int left, int top) :
  18. cmCursesWidget(width, height, left, top)
  19. {
  20. this->Type = cmCacheManager::BOOL; // this is a bit of a hack
  21. // there is no option type, and string type causes ccmake to cast
  22. // the widget into a string widget at some point. BOOL is safe for
  23. // now.
  24. set_field_fore(this->Field, A_NORMAL);
  25. set_field_back(this->Field, A_STANDOUT);
  26. field_opts_off(this->Field, O_STATIC);
  27. }
  28. bool cmCursesOptionsWidget::HandleInput(int& key, cmCursesMainForm*, WINDOW* w)
  29. {
  30. // 10 == enter
  31. if (key == 10 || key == KEY_ENTER)
  32. {
  33. this->NextOption();
  34. touchwin(w);
  35. wrefresh(w);
  36. return true;
  37. }
  38. else if (key == KEY_LEFT || key == ctrl('b'))
  39. {
  40. touchwin(w);
  41. wrefresh(w);
  42. this->PreviousOption();
  43. return true;
  44. }
  45. else if (key == KEY_RIGHT || key == ctrl('f'))
  46. {
  47. this->NextOption();
  48. touchwin(w);
  49. wrefresh(w);
  50. return true;
  51. }
  52. else
  53. {
  54. return false;
  55. }
  56. return false;
  57. }
  58. void cmCursesOptionsWidget::AddOption(std::string const & option )
  59. {
  60. this->Options.push_back(option);
  61. }
  62. void cmCursesOptionsWidget::NextOption()
  63. {
  64. this->CurrentOption++;
  65. if(this->CurrentOption > this->Options.size()-1)
  66. {
  67. this->CurrentOption = 0;
  68. }
  69. this->SetValue(this->Options[this->CurrentOption].c_str());
  70. }
  71. void cmCursesOptionsWidget::PreviousOption()
  72. {
  73. if(this->CurrentOption == 0)
  74. {
  75. this->CurrentOption = this->Options.size()-1;
  76. }
  77. else
  78. {
  79. this->CurrentOption--;
  80. }
  81. this->SetValue(this->Options[this->CurrentOption].c_str());
  82. }
  83. void cmCursesOptionsWidget::SetOption(const char* value)
  84. {
  85. this->CurrentOption = 0; // default to 0 index
  86. this->SetValue(value);
  87. int index = 0;
  88. for(std::vector<std::string>::iterator i = this->Options.begin();
  89. i != this->Options.end(); ++i)
  90. {
  91. if(*i == value)
  92. {
  93. this->CurrentOption = index;
  94. }
  95. index++;
  96. }
  97. }