cmake/Source/CursesDialog/cmCursesOptionsWidget.cxx

94 lines
2.4 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2014-08-03 19:52:23 +02:00
#include "cmCursesOptionsWidget.h"
2016-07-09 11:21:54 +02:00
2020-08-30 11:54:41 +02:00
#include "cmCursesColor.h"
2016-10-30 18:24:19 +01:00
#include "cmCursesWidget.h"
2017-04-14 19:02:05 +02:00
#include "cmStateTypes.h"
2014-08-03 19:52:23 +02:00
2016-10-30 18:24:19 +01:00
#define ctrl(z) ((z)&037)
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
cmCursesOptionsWidget::cmCursesOptionsWidget(int width, int height, int left,
int top)
: cmCursesWidget(width, height, left, top)
2014-08-03 19:52:23 +02:00
{
2017-04-14 19:02:05 +02:00
this->Type = cmStateEnums::BOOL; // this is a bit of a hack
2014-08-03 19:52:23 +02:00
// there is no option type, and string type causes ccmake to cast
// the widget into a string widget at some point. BOOL is safe for
// now.
2020-08-30 11:54:41 +02:00
if (cmCursesColor::HasColors()) {
set_field_fore(this->Field, COLOR_PAIR(cmCursesColor::Choice));
set_field_back(this->Field, COLOR_PAIR(cmCursesColor::Choice));
} else {
set_field_fore(this->Field, A_NORMAL);
set_field_back(this->Field, A_STANDOUT);
}
2016-07-09 11:21:54 +02:00
field_opts_off(this->Field, O_STATIC);
2014-08-03 19:52:23 +02:00
}
2016-10-30 18:24:19 +01:00
bool cmCursesOptionsWidget::HandleInput(int& key, cmCursesMainForm* /*fm*/,
WINDOW* w)
2014-08-03 19:52:23 +02:00
{
2019-11-11 23:01:05 +01:00
if (this->Options.empty()) {
return false;
}
2016-10-30 18:24:19 +01:00
switch (key) {
case 10: // 10 == enter
case KEY_ENTER:
this->NextOption();
touchwin(w);
wrefresh(w);
return true;
case KEY_LEFT:
case ctrl('b'):
touchwin(w);
wrefresh(w);
this->PreviousOption();
return true;
case KEY_RIGHT:
case ctrl('f'):
this->NextOption();
touchwin(w);
wrefresh(w);
return true;
default:
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
void cmCursesOptionsWidget::AddOption(std::string const& option)
2014-08-03 19:52:23 +02:00
{
this->Options.push_back(option);
}
void cmCursesOptionsWidget::NextOption()
{
this->CurrentOption++;
2016-07-09 11:21:54 +02:00
if (this->CurrentOption > this->Options.size() - 1) {
2014-08-03 19:52:23 +02:00
this->CurrentOption = 0;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
this->SetValue(this->Options[this->CurrentOption]);
2014-08-03 19:52:23 +02:00
}
void cmCursesOptionsWidget::PreviousOption()
{
2016-07-09 11:21:54 +02:00
if (this->CurrentOption == 0) {
this->CurrentOption = this->Options.size() - 1;
} else {
2014-08-03 19:52:23 +02:00
this->CurrentOption--;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
this->SetValue(this->Options[this->CurrentOption]);
2014-08-03 19:52:23 +02:00
}
2015-04-27 22:25:09 +02:00
void cmCursesOptionsWidget::SetOption(const std::string& value)
2014-08-03 19:52:23 +02:00
{
this->CurrentOption = 0; // default to 0 index
this->SetValue(value);
int index = 0;
2018-01-26 17:06:56 +01:00
for (auto const& opt : this->Options) {
if (opt == value) {
2014-08-03 19:52:23 +02:00
this->CurrentOption = index;
}
2016-07-09 11:21:54 +02:00
index++;
}
2014-08-03 19:52:23 +02:00
}