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 /// ColorPickerPalette class implementation.
22 /// @ingroup cppconsui
24 #include "ColorPickerPalette.h"
26 #include "ColorScheme.h"
30 #define GRAYSCALE_START 232
31 #define GRAYSCALE_END 255
35 ColorPickerPalette::ColorPickerPalette(int default_color
, int flags
)
38 if (flags
== (FLAG_HIDE_ANSI
| FLAG_HIDE_GRAYSCALE
| FLAG_HIDE_COLORCUBE
)) {
39 // Show at least ANSI colors.
40 flags
= FLAG_HIDE_GRAYSCALE
| FLAG_HIDE_COLORCUBE
;
43 if (Curses::getColorCount() < 256)
44 flags
|= (FLAG_HIDE_GRAYSCALE
| FLAG_HIDE_COLORCUBE
);
46 if (!(flags
& FLAG_HIDE_ANSI
)) {
48 addAnsi(default_color
);
51 if (!(flags
& FLAG_HIDE_GRAYSCALE
)) {
53 addGrayscale(default_color
);
56 if (!(flags
& FLAG_HIDE_COLORCUBE
)) {
58 addColorCube(default_color
);
62 void ColorPickerPalette::onSelectColor(Button
&activator
)
64 ColorPickerPaletteButton
*button
=
65 dynamic_cast<ColorPickerPaletteButton
*>(&activator
);
66 assert(button
!= nullptr);
68 signal_color_selected(*this, button
->getColor());
71 void ColorPickerPalette::addButton(int x
, int y
, int color
, int default_color
)
73 auto button
= new ColorPickerPaletteButton(color
);
74 button
->signal_activate
.connect(
75 sigc::mem_fun(this, &ColorPickerPalette::onSelectColor
));
76 addWidget(*button
, x
, y
);
78 if (color
== default_color
)
82 void ColorPickerPalette::addAnsi(int default_color
)
86 // Resize the ColorPickerPalette.
90 // There are 8 ANSI colors, or 16 when bright colors are supported.
91 w
= std::max(w
, Curses::NUM_DEFAULT_COLORS
);
96 // Add the color picker buttons, in two lines.
97 // @todo Support terms with only 8 colors.
98 int half
= Curses::NUM_DEFAULT_COLORS
/ 2;
99 for (x
= 0; x
< Curses::NUM_DEFAULT_COLORS
; ++x
)
102 addButton(x
* 2, y
, x
, default_color
);
106 addButton((x
- half
) * 2, y
+ 1, x
, default_color
);
110 void ColorPickerPalette::addGrayscale(int default_color
)
112 int w
, h
, x
, y
, color
;
114 // Resize the ColorPickerPalette.
118 // Add space between this and previous section.
123 w
= std::max(w
, (GRAYSCALE_END
- GRAYSCALE_START
+ 1) * 2);
128 // Add the color picker buttons.
129 for (color
= GRAYSCALE_START
, x
= 0; color
<= GRAYSCALE_END
; ++color
, x
+= 2)
130 addButton(x
, y
, color
, default_color
);
132 addButton(x
, y
, Curses::Color::WHITE
, default_color
);
135 void ColorPickerPalette::addColorCube(int default_color
)
139 // Resize the ColorPickerPalette.
143 // Add space between this and previous section.
149 w
= std::max(w
, (6 * 6 * 2) + 5);
154 // Add the color picker buttons.
156 for (int g
= 0; g
< 6; ++g
) {
157 for (int r
= 0; r
< 6; ++r
) {
158 for (int b
= 0; b
< 6; ++b
) {
159 addButton(x
, y
, 16 + (r
* 36) + (g
* 6) + b
, default_color
);
171 ColorPickerPalette::ColorPickerPaletteButton::ColorPickerPaletteButton(
173 : Button(2, 1, ""), color_(color
)
177 int ColorPickerPalette::ColorPickerPaletteButton::draw(
178 Curses::ViewPort area
, Error
&error
)
180 ColorScheme::Color
c(Curses::Color::BLACK
, color_
);
182 if (COLORSCHEME
->getColorPair(c
, &attrs
, error
) != 0)
183 return error
.getCode();
186 DRAW(area
.attrOn(Curses::Attr::REVERSE
, error
));
187 DRAW(area
.addString(0, 0, "@@", error
));
188 DRAW(area
.attrOff(Curses::Attr::REVERSE
, error
));
191 DRAW(area
.fill(attrs
, 0, 0, 2, 1, error
));
196 } // namespace CppConsUI
198 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: