cmCursesOptionsWidget.cxx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCursesOptionsWidget.h"
  4. #include "cmCursesColor.h"
  5. #include "cmCursesWidget.h"
  6. #include "cmStateTypes.h"
  7. #define ctrl(z) ((z) & 037)
  8. cmCursesOptionsWidget::cmCursesOptionsWidget(int width, int height, int left,
  9. int top)
  10. : cmCursesWidget(width, height, left, top)
  11. {
  12. this->Type = cmStateEnums::BOOL; // this is a bit of a hack
  13. // there is no option type, and string type causes ccmake to cast
  14. // the widget into a string widget at some point. BOOL is safe for
  15. // now.
  16. if (cmCursesColor::HasColors()) {
  17. set_field_fore(this->Field, COLOR_PAIR(cmCursesColor::Choice));
  18. set_field_back(this->Field, COLOR_PAIR(cmCursesColor::Choice));
  19. } else {
  20. set_field_fore(this->Field, A_NORMAL);
  21. set_field_back(this->Field, A_STANDOUT);
  22. }
  23. field_opts_off(this->Field, O_STATIC);
  24. }
  25. bool cmCursesOptionsWidget::HandleInput(int& key, cmCursesMainForm* /*fm*/,
  26. WINDOW* w)
  27. {
  28. if (this->Options.empty()) {
  29. return false;
  30. }
  31. switch (key) {
  32. case 10: // 10 == enter
  33. case KEY_ENTER:
  34. this->NextOption();
  35. touchwin(w);
  36. wrefresh(w);
  37. return true;
  38. case KEY_LEFT:
  39. case ctrl('b'):
  40. touchwin(w);
  41. wrefresh(w);
  42. this->PreviousOption();
  43. return true;
  44. case KEY_RIGHT:
  45. case ctrl('f'):
  46. this->NextOption();
  47. touchwin(w);
  48. wrefresh(w);
  49. return true;
  50. default:
  51. return false;
  52. }
  53. }
  54. void cmCursesOptionsWidget::AddOption(std::string const& option)
  55. {
  56. this->Options.push_back(option);
  57. }
  58. void cmCursesOptionsWidget::NextOption()
  59. {
  60. this->CurrentOption++;
  61. if (this->CurrentOption > this->Options.size() - 1) {
  62. this->CurrentOption = 0;
  63. }
  64. this->SetValue(this->Options[this->CurrentOption]);
  65. }
  66. void cmCursesOptionsWidget::PreviousOption()
  67. {
  68. if (this->CurrentOption == 0) {
  69. this->CurrentOption = this->Options.size() - 1;
  70. } else {
  71. this->CurrentOption--;
  72. }
  73. this->SetValue(this->Options[this->CurrentOption]);
  74. }
  75. void cmCursesOptionsWidget::SetOption(const std::string& value)
  76. {
  77. this->CurrentOption = 0; // default to 0 index
  78. this->SetValue(value);
  79. int index = 0;
  80. for (auto const& opt : this->Options) {
  81. if (opt == value) {
  82. this->CurrentOption = index;
  83. }
  84. index++;
  85. }
  86. }