Turn ColorScheme properties into an enumeration
[centerim5.git] / cppconsui / ColorPickerComboBox.cpp
blob31ba110b295a17808bf270653d54ec68b284bef6
1 /*
2 * Copyright (C) 2012 Mark Pustjens <pustjens@dds.nl>
3 * Copyright (C) 2012-2015 Petr Pavlu <setup@dagobah.cz>
5 * This file is part of CenterIM.
7 * CenterIM is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CenterIM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /**
23 * @file
24 * ColorPickerComboBox class implementation.
26 * @ingroup cppconsui
29 #include "ColorPickerComboBox.h"
31 #include <algorithm>
32 #include "gettext.h"
34 /* This is an invalid color number that is used for the "More..." button in
35 * ColorPickerComboBox. */
36 #define COLOR_MORE (-2)
38 // width of the drop-down menu
39 #define MENU_WIDTH 12
41 namespace CppConsUI {
43 ColorPickerComboBox::ColorPickerComboBox(int w, int color)
44 : ComboBox(w, 1), selected_color(color)
45 #ifdef COLORPICKER_256COLOR
47 colorpicker(NULL)
48 #endif // COLORPICKER_256COLOR
50 // add ANSI colors
51 int colors = std::min(Curses::NUM_DEFAULT_COLORS, Curses::getColorCount());
52 for (int i = 0; i < colors; i++)
53 addOption(NULL, i);
55 // add options for default color and to open the 256 color dialog
56 addOption(NULL, Curses::Color::DEFAULT);
57 #ifdef COLORPICKER_256COLOR
58 addOption(_("More..."), COLOR_MORE);
59 #endif // COLORPICKER_256COLOR
61 // set initial selection
62 setSelectedByData(color);
65 ColorPickerComboBox::~ColorPickerComboBox()
67 #ifdef COLORPICKER_256COLOR
68 if (colorpicker)
69 colorpicker->close();
70 #endif // COLORPICKER_256COLOR
73 void ColorPickerComboBox::setColor(int new_color)
75 if (new_color < Curses::Color::DEFAULT ||
76 new_color >= Curses::getColorCount()) {
77 // an invalid color was specified, use the default color
78 new_color = Curses::Color::DEFAULT;
81 if (new_color == selected_color)
82 return;
84 selected_color = new_color;
86 #ifdef COLORPICKER_256COLOR
87 if (selected_color >= Curses::Color::DEFAULT &&
88 selected_color < Curses::NUM_DEFAULT_COLORS)
89 setSelectedByData(selected_color);
90 else
91 setSelectedByData(COLOR_MORE);
92 #else
93 setSelectedByData(selected_color);
94 #endif // COLORPICKER_256COLOR
97 int ColorPickerComboBox::draw(Curses::ViewPort area, Error &error)
99 int attrs;
100 if (has_focus) {
101 DRAW(getAttributes(ColorScheme::PROPERTY_BUTTON_FOCUS, &attrs, error));
102 attrs |= Curses::Attr::REVERSE;
104 else
105 DRAW(getAttributes(ColorScheme::PROPERTY_BUTTON_NORMAL, &attrs, error));
107 int color = selected_color;
109 DRAW(area.attrOn(attrs, error));
110 DRAW(area.fill(attrs, 0, 0, real_width, 1, error));
111 DRAW(area.addChar(0, 0, '[', error));
112 DRAW(area.addChar(real_width - 1, 0, ']', error));
113 DRAW(area.attrOff(attrs, error));
115 if (selected_color == Curses::Color::DEFAULT)
116 DRAW(area.addString(1, 0, _("DEFAULT"), error));
117 else {
118 ColorScheme::Color c(Curses::Color::DEFAULT, color);
119 int colorpair;
120 DRAW(COLORSCHEME->getColorPair(c, &colorpair, error));
121 DRAW(area.attrOn(colorpair, error));
122 DRAW(area.fill(colorpair, 1, 0, real_width - 2, 1, error));
123 DRAW(area.attrOff(colorpair, error));
126 return 0;
129 void ColorPickerComboBox::onDropDown(Button & /*activator*/)
131 dropdown = new MenuWindow(*this, MENU_WIDTH, AUTOSIZE);
132 dropdown->signal_close.connect(
133 sigc::mem_fun(this, &ColorPickerComboBox::dropDownClose));
135 int i;
136 ComboBoxEntries::iterator j;
137 for (i = 0, j = options.begin(); j != options.end(); i++, j++) {
138 Button *b;
139 if (j->data == COLOR_MORE) {
140 // add the "More..." button
141 b = dropdown->appendItem(j->title,
142 sigc::bind(sigc::mem_fun(this, &ColorPickerComboBox::dropDownOk), i));
144 else {
145 // normal color button
146 b = new ColorButton(j->data);
147 dropdown->appendWidget(*b);
148 b->signal_activate.connect(
149 sigc::bind(sigc::mem_fun(this, &ColorPickerComboBox::dropDownOk), i));
151 if (i == selected_entry)
152 b->grabFocus();
155 dropdown->show();
158 void ColorPickerComboBox::dropDownOk(Button & /*activator*/, int new_entry)
160 dropdown->close();
162 #ifdef COLORPICKER_256COLOR
163 if (options[new_entry].data != COLOR_MORE) {
164 setColor(options[new_entry].data);
165 return;
168 // the "More..." button was selected, display the color picker dialog
169 // @todo Initialize the color picker dialog with a selected color.
170 colorpicker = new ColorPickerDialog("", 0, 0);
171 colorpicker->signal_response.connect(
172 sigc::mem_fun(this, &ColorPickerComboBox::colorPickerOk));
173 colorpicker->signal_close.connect(
174 sigc::mem_fun(this, &ColorPickerComboBox::colorPickerClose));
175 colorpicker->show();
176 #else
177 setColor(options[new_entry].data);
178 #endif // COLORPICKER_256COLOR
181 #ifdef COLORPICKER_256COLOR
182 void ColorPickerComboBox::colorPickerOk(ColorPickerDialog &activator,
183 AbstractDialog::ResponseType response, int new_color)
185 if (response != AbstractDialog::RESPONSE_OK)
186 return;
188 // selected option didn't change
189 if (new_color == selected_color)
190 return;
192 setColor(new_color);
195 void ColorPickerComboBox::colorPickerClose(Window & /*window*/)
197 colorpicker = NULL;
199 #endif // COLORPICKER_256COLOR
201 void ColorPickerComboBox::setSelected(int new_entry)
203 ComboBox::setSelected(new_entry);
205 selected_color = options[new_entry].data;
206 signal_color_changed(*this, selected_color);
209 ColorPickerComboBox::ColorButton::ColorButton(int color_)
210 : Button(MENU_WIDTH - 2, 1, ""), color(color_)
214 int ColorPickerComboBox::ColorButton::draw(Curses::ViewPort area, Error &error)
216 int attrs;
217 if (has_focus) {
218 DRAW(getAttributes(ColorScheme::PROPERTY_BUTTON_FOCUS, &attrs, error));
219 attrs |= Curses::Attr::REVERSE;
221 else
222 DRAW(getAttributes(ColorScheme::PROPERTY_BUTTON_NORMAL, &attrs, error));
224 DRAW(area.attrOn(attrs, error));
225 DRAW(area.fill(attrs, 0, 0, real_width, 1, error));
226 DRAW(area.addChar(0, 0, '[', error));
227 DRAW(area.addChar(real_width - 1, 0, ']', error));
228 DRAW(area.attrOff(attrs, error));
230 if (color == Curses::Color::DEFAULT)
231 DRAW(area.addString(1, 0, _("DEFAULT "), error));
232 else {
233 ColorScheme::Color c(Curses::Color::DEFAULT, color);
234 int colorpair;
235 DRAW(COLORSCHEME->getColorPair(c, &colorpair, error));
236 DRAW(area.attrOn(colorpair, error));
237 DRAW(area.fill(colorpair, 1, 0, real_width - 2, 1, error));
238 DRAW(area.attrOff(colorpair, error));
241 return 0;
244 } // namespace CppConsUI
246 /* vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab : */