cmCursesOptionsWidget.cxx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, int left,
  17. int top)
  18. : cmCursesWidget(width, height, left, top)
  19. {
  20. this->Type = cmState::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* /*fm*/,
  29. WINDOW* w)
  30. {
  31. // 10 == enter
  32. if (key == 10 || key == KEY_ENTER) {
  33. this->NextOption();
  34. touchwin(w);
  35. wrefresh(w);
  36. return true;
  37. } else if (key == KEY_LEFT || key == ctrl('b')) {
  38. touchwin(w);
  39. wrefresh(w);
  40. this->PreviousOption();
  41. return true;
  42. } else if (key == KEY_RIGHT || key == ctrl('f')) {
  43. this->NextOption();
  44. touchwin(w);
  45. wrefresh(w);
  46. return true;
  47. } else {
  48. return false;
  49. }
  50. }
  51. void cmCursesOptionsWidget::AddOption(std::string const& option)
  52. {
  53. this->Options.push_back(option);
  54. }
  55. void cmCursesOptionsWidget::NextOption()
  56. {
  57. this->CurrentOption++;
  58. if (this->CurrentOption > this->Options.size() - 1) {
  59. this->CurrentOption = 0;
  60. }
  61. this->SetValue(this->Options[this->CurrentOption]);
  62. }
  63. void cmCursesOptionsWidget::PreviousOption()
  64. {
  65. if (this->CurrentOption == 0) {
  66. this->CurrentOption = this->Options.size() - 1;
  67. } else {
  68. this->CurrentOption--;
  69. }
  70. this->SetValue(this->Options[this->CurrentOption]);
  71. }
  72. void cmCursesOptionsWidget::SetOption(const std::string& value)
  73. {
  74. this->CurrentOption = 0; // default to 0 index
  75. this->SetValue(value);
  76. int index = 0;
  77. for (std::vector<std::string>::iterator i = this->Options.begin();
  78. i != this->Options.end(); ++i) {
  79. if (*i == value) {
  80. this->CurrentOption = index;
  81. }
  82. index++;
  83. }
  84. }