cmCursesOptionsWidget.cxx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. switch (key) {
  23. case 10: // 10 == enter
  24. case KEY_ENTER:
  25. this->NextOption();
  26. touchwin(w);
  27. wrefresh(w);
  28. return true;
  29. case KEY_LEFT:
  30. case ctrl('b'):
  31. touchwin(w);
  32. wrefresh(w);
  33. this->PreviousOption();
  34. return true;
  35. case KEY_RIGHT:
  36. case ctrl('f'):
  37. this->NextOption();
  38. touchwin(w);
  39. wrefresh(w);
  40. return true;
  41. default:
  42. return false;
  43. }
  44. }
  45. void cmCursesOptionsWidget::AddOption(std::string const& option)
  46. {
  47. this->Options.push_back(option);
  48. }
  49. void cmCursesOptionsWidget::NextOption()
  50. {
  51. this->CurrentOption++;
  52. if (this->CurrentOption > this->Options.size() - 1) {
  53. this->CurrentOption = 0;
  54. }
  55. this->SetValue(this->Options[this->CurrentOption]);
  56. }
  57. void cmCursesOptionsWidget::PreviousOption()
  58. {
  59. if (this->CurrentOption == 0) {
  60. this->CurrentOption = this->Options.size() - 1;
  61. } else {
  62. this->CurrentOption--;
  63. }
  64. this->SetValue(this->Options[this->CurrentOption]);
  65. }
  66. void cmCursesOptionsWidget::SetOption(const std::string& value)
  67. {
  68. this->CurrentOption = 0; // default to 0 index
  69. this->SetValue(value);
  70. int index = 0;
  71. for (auto const& opt : this->Options) {
  72. if (opt == value) {
  73. this->CurrentOption = index;
  74. }
  75. index++;
  76. }
  77. }