cmCursesBoolWidget.cxx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmCursesBoolWidget.h"
  4. #include <string>
  5. #include "cmCursesColor.h"
  6. #include "cmCursesWidget.h"
  7. #include "cmStateTypes.h"
  8. cmCursesBoolWidget::cmCursesBoolWidget(int width, int height, int left,
  9. int top)
  10. : cmCursesWidget(width, height, left, top)
  11. {
  12. this->Type = cmStateEnums::BOOL;
  13. if (!cmCursesColor::HasColors()) {
  14. set_field_fore(this->Field, A_NORMAL);
  15. set_field_back(this->Field, A_STANDOUT);
  16. }
  17. field_opts_off(this->Field, O_STATIC);
  18. this->SetValueAsBool(false);
  19. }
  20. bool cmCursesBoolWidget::HandleInput(int& key, cmCursesMainForm* /*fm*/,
  21. WINDOW* w)
  22. {
  23. // toggle boolean values with enter or space
  24. // 10 == enter
  25. if (key == 10 || key == KEY_ENTER || key == ' ') {
  26. if (this->GetValueAsBool()) {
  27. this->SetValueAsBool(false);
  28. } else {
  29. this->SetValueAsBool(true);
  30. }
  31. touchwin(w);
  32. wrefresh(w);
  33. return true;
  34. }
  35. return false;
  36. }
  37. void cmCursesBoolWidget::SetValueAsBool(bool value)
  38. {
  39. if (value) {
  40. this->SetValue("ON");
  41. if (cmCursesColor::HasColors()) {
  42. set_field_fore(this->Field, COLOR_PAIR(cmCursesColor::BoolOn));
  43. set_field_back(this->Field, COLOR_PAIR(cmCursesColor::BoolOn));
  44. }
  45. } else {
  46. this->SetValue("OFF");
  47. if (cmCursesColor::HasColors()) {
  48. set_field_fore(this->Field, COLOR_PAIR(cmCursesColor::BoolOff));
  49. set_field_back(this->Field, COLOR_PAIR(cmCursesColor::BoolOff));
  50. }
  51. }
  52. }
  53. bool cmCursesBoolWidget::GetValueAsBool()
  54. {
  55. return this->Value == "ON";
  56. }