Do not hardcode path to TERMINFO in termex
[centerim5.git] / cppconsui / ColorPicker.cpp
blobf122d999116833cd6c60befed0f1de030ba210a7
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 /// ColorPicker class implementation.
21 ///
22 /// @ingroup cppconsui
24 #include "ColorPicker.h"
26 #include "ColorScheme.h"
27 #include "Spacer.h"
29 #include "gettext.h"
31 namespace CppConsUI {
33 ColorPicker::ColorPicker(int fg, int bg, const char *text, bool sample)
34 : HorizontalListBox(AUTOSIZE, 1), fg_combo_(nullptr), bg_combo_(nullptr),
35 label_(nullptr), sample_(nullptr)
37 fg_combo_ = new ColorPickerComboBox(10, fg);
38 bg_combo_ = new ColorPickerComboBox(10, bg);
40 label_ = new Label(1, 1, "");
41 setText(text);
43 fg_combo_->signal_color_changed.connect(
44 sigc::mem_fun(this, &ColorPicker::onColorChanged));
45 bg_combo_->signal_color_changed.connect(
46 sigc::mem_fun(this, &ColorPicker::onColorChanged));
48 appendWidget(*label_);
49 appendWidget(*fg_combo_);
50 appendWidget(*(new Spacer(1, 1)));
51 appendWidget(*bg_combo_);
53 if (sample) {
54 sample_ = new Sample(10, fg, bg);
55 appendWidget(*sample_);
58 setColorPair(fg, bg);
61 void ColorPicker::setColorPair(int fg, int bg)
63 fg_combo_->setColor(fg);
64 bg_combo_->setColor(bg);
66 if (sample_ != nullptr)
67 sample_->setColors(fg, bg);
69 signal_colorpair_selected(*this, fg, bg);
72 void ColorPicker::setText(const char *new_text)
74 label_->setText(new_text);
75 if (new_text != nullptr)
76 label_->setWidth(Curses::onScreenWidth(new_text) + 1);
77 else
78 label_->setWidth(0);
81 void ColorPicker::onColorChanged(ComboBox &activator, int new_color)
83 int new_fg = fg_combo_->getColor();
84 int new_bg = bg_combo_->getColor();
86 if (&activator == fg_combo_)
87 new_fg = new_color;
88 else
89 new_bg = new_color;
91 setColorPair(new_fg, new_bg);
94 ColorPicker::Sample::Sample(int w, int fg, int bg)
95 : Widget(w, 1), color_(fg, bg)
99 int ColorPicker::Sample::draw(Curses::ViewPort area, Error &error)
101 int attrs;
102 if (COLORSCHEME->getColorPair(color_, &attrs, error) != 0)
103 return error.getCode();
105 DRAW(area.attrOn(attrs, error));
106 DRAW(area.addString(1, 0, _(" SAMPLE "), error));
107 DRAW(area.attrOff(attrs, error));
109 return 0;
112 void ColorPicker::Sample::setColors(int fg, int bg)
114 color_.foreground = fg;
115 color_.background = bg;
118 } // namespace CppConsUI
120 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: