cmCursesBoolWidget.cxx 1.67 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
Berk Geveci's avatar
Berk Geveci committed
3
#include "cmCursesBoolWidget.h"
Brad King's avatar
Brad King committed
4

5 6
#include <string>

7
#include "cmCursesColor.h"
8
#include "cmCursesWidget.h"
9
#include "cmStateTypes.h"
10

11 12 13
cmCursesBoolWidget::cmCursesBoolWidget(int width, int height, int left,
                                       int top)
  : cmCursesWidget(width, height, left, top)
Berk Geveci's avatar
Berk Geveci committed
14
{
15
  this->Type = cmStateEnums::BOOL;
16 17 18 19
  if (!cmCursesColor::HasColors()) {
    set_field_fore(this->Field, A_NORMAL);
    set_field_back(this->Field, A_STANDOUT);
  }
20
  field_opts_off(this->Field, O_STATIC);
Berk Geveci's avatar
Berk Geveci committed
21 22 23
  this->SetValueAsBool(false);
}

24 25
bool cmCursesBoolWidget::HandleInput(int& key, cmCursesMainForm* /*fm*/,
                                     WINDOW* w)
Berk Geveci's avatar
Berk Geveci committed
26
{
Berk Geveci's avatar
Berk Geveci committed
27

28
  // toggle boolean values with enter or space
Berk Geveci's avatar
Berk Geveci committed
29
  // 10 == enter
30
  if (key == 10 || key == KEY_ENTER || key == ' ') {
31
    if (this->GetValueAsBool()) {
Berk Geveci's avatar
Berk Geveci committed
32
      this->SetValueAsBool(false);
33
    } else {
Berk Geveci's avatar
Berk Geveci committed
34
      this->SetValueAsBool(true);
35
    }
Berk Geveci's avatar
Berk Geveci committed
36

37 38
    touchwin(w);
    wrefresh(w);
Berk Geveci's avatar
Berk Geveci committed
39
    return true;
40
  }
41
  return false;
Berk Geveci's avatar
Berk Geveci committed
42 43 44 45
}

void cmCursesBoolWidget::SetValueAsBool(bool value)
{
46
  if (value) {
Berk Geveci's avatar
Berk Geveci committed
47
    this->SetValue("ON");
48 49 50 51
    if (cmCursesColor::HasColors()) {
      set_field_fore(this->Field, COLOR_PAIR(cmCursesColor::BoolOn));
      set_field_back(this->Field, COLOR_PAIR(cmCursesColor::BoolOn));
    }
52
  } else {
Berk Geveci's avatar
Berk Geveci committed
53
    this->SetValue("OFF");
54 55 56 57
    if (cmCursesColor::HasColors()) {
      set_field_fore(this->Field, COLOR_PAIR(cmCursesColor::BoolOff));
      set_field_back(this->Field, COLOR_PAIR(cmCursesColor::BoolOff));
    }
58
  }
Berk Geveci's avatar
Berk Geveci committed
59 60 61 62
}

bool cmCursesBoolWidget::GetValueAsBool()
{
Daniel Pfeifer's avatar
Daniel Pfeifer committed
63
  return this->Value == "ON";
Berk Geveci's avatar
Berk Geveci committed
64
}