1 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
3 // This file is part of CenterIM.
5 // CenterIM is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // CenterIM is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
19 /// CheckBox class implementation.
21 /// @ingroup cppconsui
25 #include "ColorScheme.h"
34 CheckBox::CheckBox(int w
, int h
, const char *text
, bool checked
)
35 : Widget(w
, h
), text_(nullptr), text_width_(0), text_height_(0),
44 CheckBox::CheckBox(const char *text
, bool checked
)
45 : Widget(AUTOSIZE
, AUTOSIZE
), text_(nullptr), text_width_(0), text_height_(0),
59 int CheckBox::draw(Curses::ViewPort area
, Error
&error
)
61 assert(text_
!= nullptr);
65 DRAW(getAttributes(ColorScheme::PROPERTY_CHECKBOX_FOCUS
, &attrs
, error
));
66 attrs
|= Curses::Attr::REVERSE
;
69 DRAW(getAttributes(ColorScheme::PROPERTY_CHECKBOX_NORMAL
, &attrs
, error
));
70 DRAW(area
.attrOn(attrs
, error
));
73 DRAW(area
.fill(attrs
, 0, 0, text_width_
, real_height_
, error
));
75 const char *start
, *end
;
77 while (*end
!= '\0') {
79 DRAW(area
.addString(0, y
, real_width_
, start
, end
, error
));
85 DRAW(area
.addString(0, y
, real_width_
, start
, end
, error
));
88 int h
= (text_height_
- 1) / 2;
92 const char *value
= checked_
? YES_BUTTON_TEXT
: NO_BUTTON_TEXT
;
93 int value_width
= Curses::onScreenWidth(value
);
94 DRAW(area
.fill(attrs
, l
, 0, value_width
+ 2, real_height_
, error
));
95 DRAW(area
.addString(l
, h
, real_width_
- l
, ": ", error
, &printed
));
97 DRAW(area
.addString(l
, h
, real_width_
- l
, value
, error
));
99 DRAW(area
.attrOff(attrs
, error
));
104 void CheckBox::setText(const char *new_text
)
106 std::size_t size
= 1;
107 if (new_text
!= nullptr)
108 size
+= std::strlen(new_text
);
109 auto new_storage
= new char[size
];
110 if (new_text
!= nullptr)
111 std::strcpy(new_storage
, new_text
);
113 new_storage
[0] = '\0';
118 // Update text_width_, text_height_ and wish height.
122 const char *start
, *end
;
125 while (*end
!= '\0') {
127 w
= Curses::onScreenWidth(start
, end
);
135 w
= Curses::onScreenWidth(start
, end
);
138 setWishHeight(text_height_
);
143 void CheckBox::setChecked(bool new_checked
)
145 if (new_checked
== checked_
)
148 checked_
= new_checked
;
149 signal_toggle(*this, checked_
);
153 void CheckBox::actionToggle()
155 setChecked(!checked_
);
158 void CheckBox::declareBindables()
160 declareBindable("checkbox", "toggle",
161 sigc::mem_fun(this, &CheckBox::actionToggle
),
162 InputProcessor::BINDABLE_NORMAL
);
165 } // namespace CppConsUI
167 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: