cmCursesOptionsWidget.cxx 2.2 KB

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