Fix include name for the ncursesw library
[centerim5.git] / cppconsui / ColorPickerPalette.cpp
blob9e2a4f37314904c57907ce27f471b17e8917593d
1 // Copyright (C) 2012 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2012-2015 Petr Pavlu <setup@dagobah.cz>
3 //
4 // This file is part of CenterIM.
5 //
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/>.
19 /// @file
20 /// ColorPickerPalette class implementation.
21 ///
22 /// @ingroup cppconsui
24 #include "ColorPickerPalette.h"
26 #include "ColorScheme.h"
28 #include <algorithm>
30 #define GRAYSCALE_START 232
31 #define GRAYSCALE_END 255
33 namespace CppConsUI {
35 ColorPickerPalette::ColorPickerPalette(int default_color, int flags)
36 : Container(0, 0)
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)) {
47 // Default 16 colors.
48 addAnsi(default_color);
51 if (!(flags & FLAG_HIDE_GRAYSCALE)) {
52 // Grayscale ladder.
53 addGrayscale(default_color);
56 if (!(flags & FLAG_HIDE_COLORCUBE)) {
57 // 6x6x6 color cube.
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)
79 button->grabFocus();
82 void ColorPickerPalette::addAnsi(int default_color)
84 int w, h, x, y;
86 // Resize the ColorPickerPalette.
87 w = getWidth();
88 h = y = getHeight();
90 // There are 8 ANSI colors, or 16 when bright colors are supported.
91 w = std::max(w, Curses::NUM_DEFAULT_COLORS);
92 h += 2;
94 resize(w, h);
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)
100 if (x < half) {
101 // The first line.
102 addButton(x * 2, y, x, default_color);
104 else {
105 // The second line.
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.
115 w = getWidth();
116 h = getHeight();
118 // Add space between this and previous section.
119 if (h != 0)
120 ++h;
122 y = h;
123 w = std::max(w, (GRAYSCALE_END - GRAYSCALE_START + 1) * 2);
124 h = h + 1;
126 resize(w, h);
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)
137 int w, h, x, y;
139 // Resize the ColorPickerPalette.
140 w = getWidth();
141 h = getHeight();
143 // Add space between this and previous section.
144 if (h != 0)
145 ++h;
147 y = h;
149 w = std::max(w, (6 * 6 * 2) + 5);
150 h = h + 6;
152 resize(w, h);
154 // Add the color picker buttons.
155 x = 0;
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);
160 x += 2;
163 ++x;
166 ++y;
167 x = 0;
171 ColorPickerPalette::ColorPickerPaletteButton::ColorPickerPaletteButton(
172 int color)
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_);
181 int attrs;
182 if (COLORSCHEME->getColorPair(c, &attrs, error) != 0)
183 return error.getCode();
185 if (has_focus_) {
186 DRAW(area.attrOn(Curses::Attr::REVERSE, error));
187 DRAW(area.addString(0, 0, "@@", error));
188 DRAW(area.attrOff(Curses::Attr::REVERSE, error));
190 else
191 DRAW(area.fill(attrs, 0, 0, 2, 1, error));
193 return 0;
196 } // namespace CppConsUI
198 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: