1 // Copyright (C) 2012 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2012-2015 Petr Pavlu <setup@dagobah.cz>
4 // This file is part of CenterIM.
6 // CenterIM is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // CenterIM is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
20 /// ColorPickerComboBox class implementation.
22 /// @ingroup cppconsui
24 #include "ColorPickerComboBox.h"
29 // Invalid color number that is used for the "More..." button in
30 // ColorPickerComboBox.
31 #define COLOR_MORE (-2)
33 // Width of the drop-down menu.
38 ColorPickerComboBox::ColorPickerComboBox(int w
, int color
)
39 : ComboBox(w
, 1), selected_color_(color
)
40 #ifdef COLORPICKER_256COLOR
43 #endif // COLORPICKER_256COLOR
46 int colors
= std::min(Curses::NUM_DEFAULT_COLORS
, Curses::getColorCount());
47 for (int i
= 0; i
< colors
; ++i
)
48 addOption(nullptr, i
);
50 // Add options for default color and to open the 256 color dialog.
51 addOption(nullptr, Curses::Color::DEFAULT
);
52 #ifdef COLORPICKER_256COLOR
53 addOption(_("More..."), COLOR_MORE
);
54 #endif // COLORPICKER_256COLOR
56 // Set initial selection.
57 setSelectedByData(color
);
60 ColorPickerComboBox::~ColorPickerComboBox()
62 #ifdef COLORPICKER_256COLOR
63 if (colorpicker_
!= nullptr)
64 colorpicker_
->close();
65 #endif // COLORPICKER_256COLOR
68 void ColorPickerComboBox::setColor(int new_color
)
70 if (new_color
< Curses::Color::DEFAULT
||
71 new_color
>= Curses::getColorCount()) {
72 // an invalid color was specified, use the default color
73 new_color
= Curses::Color::DEFAULT
;
76 if (new_color
== selected_color_
)
79 selected_color_
= new_color
;
81 #ifdef COLORPICKER_256COLOR
82 if (selected_color_
>= Curses::Color::DEFAULT
&&
83 selected_color_
< Curses::NUM_DEFAULT_COLORS
)
84 setSelectedByData(selected_color_
);
86 setSelectedByData(COLOR_MORE
);
88 setSelectedByData(selected_color_
);
89 #endif // COLORPICKER_256COLOR
92 int ColorPickerComboBox::draw(Curses::ViewPort area
, Error
&error
)
96 DRAW(getAttributes(ColorScheme::PROPERTY_BUTTON_FOCUS
, &attrs
, error
));
97 attrs
|= Curses::Attr::REVERSE
;
100 DRAW(getAttributes(ColorScheme::PROPERTY_BUTTON_NORMAL
, &attrs
, error
));
102 DRAW(area
.attrOn(attrs
, error
));
103 DRAW(area
.fill(attrs
, 0, 0, real_width_
, 1, error
));
104 DRAW(area
.addChar(0, 0, '[', error
));
105 DRAW(area
.addChar(real_width_
- 1, 0, ']', error
));
106 DRAW(area
.attrOff(attrs
, error
));
108 if (selected_color_
== Curses::Color::DEFAULT
)
109 DRAW(area
.addString(1, 0, _("DEFAULT"), error
));
111 ColorScheme::Color
c(Curses::Color::DEFAULT
, selected_color_
);
113 DRAW(COLORSCHEME
->getColorPair(c
, &colorpair
, error
));
114 DRAW(area
.attrOn(colorpair
, error
));
115 DRAW(area
.fill(colorpair
, 1, 0, real_width_
- 2, 1, error
));
116 DRAW(area
.attrOff(colorpair
, error
));
122 void ColorPickerComboBox::onDropDown(Button
& /*activator*/)
124 dropdown_
= new MenuWindow(*this, MENU_WIDTH
, AUTOSIZE
);
125 dropdown_
->signal_close
.connect(
126 sigc::mem_fun(this, &ColorPickerComboBox::dropDownClose
));
129 ComboBoxEntries::iterator j
;
130 for (i
= 0, j
= options_
.begin(); j
!= options_
.end(); ++i
, ++j
) {
132 if (j
->data
== COLOR_MORE
) {
133 // Add the "More..." button.
134 b
= dropdown_
->appendItem(j
->title
,
135 sigc::bind(sigc::mem_fun(this, &ColorPickerComboBox::dropDownOk
), i
));
138 // Normal color button.
139 b
= new ColorButton(j
->data
);
140 dropdown_
->appendWidget(*b
);
141 b
->signal_activate
.connect(
142 sigc::bind(sigc::mem_fun(this, &ColorPickerComboBox::dropDownOk
), i
));
144 if (i
== selected_entry_
)
151 void ColorPickerComboBox::dropDownOk(Button
& /*activator*/, int new_entry
)
155 #ifdef COLORPICKER_256COLOR
156 if (options_
[new_entry
].data
!= COLOR_MORE
) {
157 setColor(options_
[new_entry
].data
);
161 // Rhe "More..." button was selected, display the color picker dialog.
162 // @todo Initialize the color picker dialog with a selected color.
163 colorpicker_
= new ColorPickerDialog("", 0, 0);
164 colorpicker_
->signal_response
.connect(
165 sigc::mem_fun(this, &ColorPickerComboBox::colorPickerOk
));
166 colorpicker_
->signal_close
.connect(
167 sigc::mem_fun(this, &ColorPickerComboBox::colorPickerClose
));
168 colorpicker_
->show();
170 setColor(options_
[new_entry
].data
);
171 #endif // COLORPICKER_256COLOR
174 #ifdef COLORPICKER_256COLOR
175 void ColorPickerComboBox::colorPickerOk(ColorPickerDialog
&activator
,
176 AbstractDialog::ResponseType response
, int new_color
)
178 if (response
!= AbstractDialog::RESPONSE_OK
)
181 // Selected option did not change.
182 if (new_color
== selected_color
)
188 void ColorPickerComboBox::colorPickerClose(Window
& /*window*/)
190 colorpicker_
= nullptr;
192 #endif // COLORPICKER_256COLOR
194 void ColorPickerComboBox::setSelected(int new_entry
)
196 ComboBox::setSelected(new_entry
);
198 selected_color_
= options_
[new_entry
].data
;
199 signal_color_changed(*this, selected_color_
);
202 ColorPickerComboBox::ColorButton::ColorButton(int color
)
203 : Button(MENU_WIDTH
- 2, 1, ""), color_(color
)
207 int ColorPickerComboBox::ColorButton::draw(Curses::ViewPort area
, Error
&error
)
211 DRAW(getAttributes(ColorScheme::PROPERTY_BUTTON_FOCUS
, &attrs
, error
));
212 attrs
|= Curses::Attr::REVERSE
;
215 DRAW(getAttributes(ColorScheme::PROPERTY_BUTTON_NORMAL
, &attrs
, error
));
217 DRAW(area
.attrOn(attrs
, error
));
218 DRAW(area
.fill(attrs
, 0, 0, real_width_
, 1, error
));
219 DRAW(area
.addChar(0, 0, '[', error
));
220 DRAW(area
.addChar(real_width_
- 1, 0, ']', error
));
221 DRAW(area
.attrOff(attrs
, error
));
223 if (color_
== Curses::Color::DEFAULT
)
224 DRAW(area
.addString(1, 0, _("DEFAULT "), error
));
226 ColorScheme::Color
c(Curses::Color::DEFAULT
, color_
);
228 DRAW(COLORSCHEME
->getColorPair(c
, &colorpair
, error
));
229 DRAW(area
.attrOn(colorpair
, error
));
230 DRAW(area
.fill(colorpair
, 1, 0, real_width_
- 2, 1, error
));
231 DRAW(area
.attrOff(colorpair
, error
));
237 } // namespace CppConsUI
239 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: