Update list of wide characters
[centerim5.git] / cppconsui / CheckBox.cpp
blobbc18c76672fa849f4d7f9b8e2874f27eb6668687
1 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
2 //
3 // This file is part of CenterIM.
4 //
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.
9 //
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/>.
18 /// @file
19 /// CheckBox class implementation.
20 ///
21 /// @ingroup cppconsui
23 #include "CheckBox.h"
25 #include "ColorScheme.h"
26 #include "Dialog.h"
28 #include "gettext.h"
29 #include <cassert>
30 #include <cstring>
32 namespace CppConsUI {
34 CheckBox::CheckBox(int w, int h, const char *text, bool checked)
35 : Widget(w, h), text_(nullptr), text_width_(0), text_height_(0),
36 checked_(checked)
38 setText(text);
40 can_focus_ = true;
41 declareBindables();
44 CheckBox::CheckBox(const char *text, bool checked)
45 : Widget(AUTOSIZE, AUTOSIZE), text_(nullptr), text_width_(0), text_height_(0),
46 checked_(checked)
48 setText(text);
50 can_focus_ = true;
51 declareBindables();
54 CheckBox::~CheckBox()
56 delete[] text_;
59 int CheckBox::draw(Curses::ViewPort area, Error &error)
61 assert(text_ != nullptr);
63 int attrs;
64 if (has_focus_) {
65 DRAW(getAttributes(ColorScheme::PROPERTY_CHECKBOX_FOCUS, &attrs, error));
66 attrs |= Curses::Attr::REVERSE;
68 else
69 DRAW(getAttributes(ColorScheme::PROPERTY_CHECKBOX_NORMAL, &attrs, error));
70 DRAW(area.attrOn(attrs, error));
72 // Print text.
73 DRAW(area.fill(attrs, 0, 0, text_width_, real_height_, error));
74 int y = 0;
75 const char *start, *end;
76 start = end = text_;
77 while (*end != '\0') {
78 if (*end == '\n') {
79 DRAW(area.addString(0, y, real_width_, start, end, error));
80 ++y;
81 start = end + 1;
83 ++end;
85 DRAW(area.addString(0, y, real_width_, start, end, error));
87 int l = text_width_;
88 int h = (text_height_ - 1) / 2;
89 int printed;
91 // Print value.
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));
96 l += printed;
97 DRAW(area.addString(l, h, real_width_ - l, value, error));
99 DRAW(area.attrOff(attrs, error));
101 return 0;
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);
112 else
113 new_storage[0] = '\0';
115 delete[] text_;
116 text_ = new_storage;
118 // Update text_width_, text_height_ and wish height.
119 text_width_ = 0;
120 text_height_ = 1;
122 const char *start, *end;
123 start = end = text_;
124 int w;
125 while (*end != '\0') {
126 if (*end == '\n') {
127 w = Curses::onScreenWidth(start, end);
128 if (w > text_width_)
129 text_width_ = w;
130 ++text_height_;
131 start = end + 1;
133 ++end;
135 w = Curses::onScreenWidth(start, end);
136 if (w > text_width_)
137 text_width_ = w;
138 setWishHeight(text_height_);
140 redraw();
143 void CheckBox::setChecked(bool new_checked)
145 if (new_checked == checked_)
146 return;
148 checked_ = new_checked;
149 signal_toggle(*this, checked_);
150 redraw();
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: