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 /// ColorPicker class implementation.
22 /// @ingroup cppconsui
24 #include "ColorPicker.h"
26 #include "ColorScheme.h"
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, "");
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_
);
54 sample_
= new Sample(10, fg
, bg
);
55 appendWidget(*sample_
);
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);
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_
)
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
)
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
));
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: