cmCursesOptionsWidget.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  57. void cmCursesOptionsWidget::AddOption(std::string const & option )
  58. {
  59. this->Options.push_back(option);
  60. }
  61. void cmCursesOptionsWidget::NextOption()
  62. {
  63. this->CurrentOption++;
  64. if(this->CurrentOption > this->Options.size()-1)
  65. {
  66. this->CurrentOption = 0;
  67. }
  68. this->SetValue(this->Options[this->CurrentOption].c_str());
  69. }
  70. void cmCursesOptionsWidget::PreviousOption()
  71. {
  72. if(this->CurrentOption == 0)
  73. {
  74. this->CurrentOption = this->Options.size()-1;
  75. }
  76. else
  77. {
  78. this->CurrentOption--;
  79. }
  80. this->SetValue(this->Options[this->CurrentOption].c_str());
  81. }
  82. void cmCursesOptionsWidget::SetOption(const char* value)
  83. {
  84. this->CurrentOption = 0; // default to 0 index
  85. this->SetValue(value);
  86. int index = 0;
  87. for(std::vector<std::string>::iterator i = this->Options.begin();
  88. i != this->Options.end(); ++i)
  89. {
  90. if(*i == value)
  91. {
  92. this->CurrentOption = index;
  93. }
  94. index++;
  95. }
  96. }