1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4 #include <math.h>
5 #include <gtk/gtksignal.h>
6 #include <gtk/gtklabel.h>
7 #include <gtk/gtktable.h>
8 #include <gtk/gtkspinbutton.h>
9 #include <glibmm/i18n.h>
10 #include "../dialogs/dialog-events.h"
11 #include "sp-color-wheel-selector.h"
12 #include "sp-color-scales.h"
15 G_BEGIN_DECLS
17 static void sp_color_wheel_selector_class_init (SPColorWheelSelectorClass *klass);
18 static void sp_color_wheel_selector_init (SPColorWheelSelector *cs);
19 static void sp_color_wheel_selector_destroy (GtkObject *object);
21 static void sp_color_wheel_selector_show_all (GtkWidget *widget);
22 static void sp_color_wheel_selector_hide_all (GtkWidget *widget);
25 G_END_DECLS
27 static SPColorSelectorClass *parent_class;
29 #define XPAD 4
30 #define YPAD 1
32 GType
33 sp_color_wheel_selector_get_type (void)
34 {
35 static GType type = 0;
36 if (!type) {
37 static const GTypeInfo info = {
38 sizeof (SPColorWheelSelectorClass),
39 NULL, /* base_init */
40 NULL, /* base_finalize */
41 (GClassInitFunc) sp_color_wheel_selector_class_init,
42 NULL, /* class_finalize */
43 NULL, /* class_data */
44 sizeof (SPColorWheelSelector),
45 0, /* n_preallocs */
46 (GInstanceInitFunc) sp_color_wheel_selector_init,
47 0, /* value_table */
48 };
50 type = g_type_register_static (SP_TYPE_COLOR_SELECTOR,
51 "SPColorWheelSelector",
52 &info,
53 static_cast< GTypeFlags > (0) );
54 }
55 return type;
56 }
58 static void
59 sp_color_wheel_selector_class_init (SPColorWheelSelectorClass *klass)
60 {
61 static const gchar* nameset[] = {N_("Wheel"), 0};
62 GtkObjectClass *object_class;
63 GtkWidgetClass *widget_class;
64 SPColorSelectorClass *selector_class;
66 object_class = (GtkObjectClass *) klass;
67 widget_class = (GtkWidgetClass *) klass;
68 selector_class = SP_COLOR_SELECTOR_CLASS (klass);
70 parent_class = SP_COLOR_SELECTOR_CLASS (g_type_class_peek_parent (klass));
72 selector_class->name = nameset;
73 selector_class->submode_count = 1;
75 object_class->destroy = sp_color_wheel_selector_destroy;
77 widget_class->show_all = sp_color_wheel_selector_show_all;
78 widget_class->hide_all = sp_color_wheel_selector_hide_all;
79 }
81 ColorWheelSelector::ColorWheelSelector( SPColorSelector* csel )
82 : ColorSelector( csel ),
83 _updating( FALSE ),
84 _dragging( FALSE ),
85 _adj(0),
86 _wheel(0),
87 _sbtn(0),
88 _label(0)
89 {
90 }
92 ColorWheelSelector::~ColorWheelSelector()
93 {
94 _adj = 0;
95 _wheel = 0;
96 _sbtn = 0;
97 _label = 0;
98 }
100 void sp_color_wheel_selector_init (SPColorWheelSelector *cs)
101 {
102 SP_COLOR_SELECTOR(cs)->base = new ColorWheelSelector( SP_COLOR_SELECTOR(cs) );
104 if ( SP_COLOR_SELECTOR(cs)->base )
105 {
106 SP_COLOR_SELECTOR(cs)->base->init();
107 }
108 }
110 void ColorWheelSelector::init()
111 {
112 GtkWidget *t;
113 gint row = 0;
115 _updating = FALSE;
116 _dragging = FALSE;
118 _tt = gtk_tooltips_new();
120 t = gtk_table_new (5, 3, FALSE);
121 gtk_widget_show (t);
122 gtk_box_pack_start (GTK_BOX (_csel), t, TRUE, TRUE, 0);
124 /* Create components */
125 row = 0;
127 _wheel = sp_color_wheel_new ();
128 gtk_widget_show (_wheel);
129 gtk_table_attach (GTK_TABLE (t), _wheel, 0, 3, row, row + 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), XPAD, YPAD);
131 row++;
133 /* Label */
134 _label = gtk_label_new_with_mnemonic (_("_A"));
135 gtk_misc_set_alignment (GTK_MISC (_label), 1.0, 0.5);
136 gtk_widget_show (_label);
137 gtk_table_attach (GTK_TABLE (t), _label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, XPAD, YPAD);
139 /* Adjustment */
140 _adj = (GtkAdjustment *) gtk_adjustment_new (0.0, 0.0, 255.0, 1.0, 10.0, 10.0);
142 /* Slider */
143 _slider = sp_color_slider_new (_adj);
144 gtk_tooltips_set_tip (_tt, _slider, _("Alpha (opacity)"), NULL);
145 gtk_widget_show (_slider);
146 gtk_table_attach (GTK_TABLE (t), _slider, 1, 2, row, row + 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)GTK_FILL, XPAD, YPAD);
148 sp_color_slider_set_colors (SP_COLOR_SLIDER (_slider),
149 SP_RGBA32_F_COMPOSE (1.0, 1.0, 1.0, 0.0),
150 SP_RGBA32_F_COMPOSE (1.0, 1.0, 1.0, 0.5),
151 SP_RGBA32_F_COMPOSE (1.0, 1.0, 1.0, 1.0));
154 /* Spinbutton */
155 _sbtn = gtk_spin_button_new (GTK_ADJUSTMENT (_adj), 1.0, 0);
156 gtk_tooltips_set_tip (_tt, _sbtn, _("Alpha (opacity)"), NULL);
157 sp_dialog_defocus_on_enter (_sbtn);
158 gtk_label_set_mnemonic_widget (GTK_LABEL(_label), _sbtn);
159 gtk_widget_show (_sbtn);
160 gtk_table_attach (GTK_TABLE (t), _sbtn, 2, 3, row, row + 1, (GtkAttachOptions)0, (GtkAttachOptions)0, XPAD, YPAD);
162 /* Signals */
163 gtk_signal_connect (GTK_OBJECT (_adj), "value_changed",
164 GTK_SIGNAL_FUNC (_adjustmentChanged), _csel);
166 gtk_signal_connect (GTK_OBJECT (_slider), "grabbed",
167 GTK_SIGNAL_FUNC (_sliderGrabbed), _csel);
168 gtk_signal_connect (GTK_OBJECT (_slider), "released",
169 GTK_SIGNAL_FUNC (_sliderReleased), _csel);
170 gtk_signal_connect (GTK_OBJECT (_slider), "changed",
171 GTK_SIGNAL_FUNC (_sliderChanged), _csel);
173 gtk_signal_connect (GTK_OBJECT(_wheel), "changed",
174 GTK_SIGNAL_FUNC (_wheelChanged), _csel);
175 }
177 static void
178 sp_color_wheel_selector_destroy (GtkObject *object)
179 {
180 if (((GtkObjectClass *) (parent_class))->destroy)
181 (* ((GtkObjectClass *) (parent_class))->destroy) (object);
182 }
184 static void
185 sp_color_wheel_selector_show_all (GtkWidget *widget)
186 {
187 gtk_widget_show (widget);
188 }
190 static void
191 sp_color_wheel_selector_hide_all (GtkWidget *widget)
192 {
193 gtk_widget_hide (widget);
194 }
196 GtkWidget *
197 sp_color_wheel_selector_new (void)
198 {
199 SPColorWheelSelector *csel;
201 csel = (SPColorWheelSelector*)gtk_type_new (SP_TYPE_COLOR_WHEEL_SELECTOR);
203 return GTK_WIDGET (csel);
204 }
206 /* Helpers for setting color value */
208 void ColorWheelSelector::_colorChanged()
209 {
210 #ifdef DUMP_CHANGE_INFO
211 g_message("ColorWheelSelector::_colorChanged( this=%p, %f, %f, %f, %f)", this, color.v.c[0], color.v.c[1], color.v.c[2], alpha );
212 #endif
213 _updating = TRUE;
214 sp_color_wheel_set_color( SP_COLOR_WHEEL( _wheel ), &_color );
216 guint32 start = _color.toRGBA32( 0x00 );
217 guint32 mid = _color.toRGBA32( 0x7f );
218 guint32 end = _color.toRGBA32( 0xff );
220 sp_color_slider_set_colors(SP_COLOR_SLIDER(_slider), start, mid, end);
222 ColorScales::setScaled(_adj, _alpha);
224 _updating = FALSE;
225 }
227 void ColorWheelSelector::_adjustmentChanged( GtkAdjustment *adjustment, SPColorWheelSelector *cs )
228 {
229 // TODO check this. It looks questionable:
230 // if a value is entered between 0 and 1 exclusive, normalize it to (int) 0..255 or 0..100
231 if (adjustment->value > 0.0 && adjustment->value < 1.0) {
232 gtk_adjustment_set_value( adjustment, floor ((adjustment->value) * adjustment->upper + 0.5) );
233 }
235 ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
236 if (wheelSelector->_updating) return;
238 wheelSelector->_updating = TRUE;
240 wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
242 wheelSelector->_updating = FALSE;
243 }
245 void ColorWheelSelector::_sliderGrabbed( SPColorSlider *slider, SPColorWheelSelector *cs )
246 {
247 (void)slider;
248 ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
249 if (!wheelSelector->_dragging) {
250 wheelSelector->_dragging = TRUE;
251 wheelSelector->_grabbed();
252 wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
253 }
254 }
256 void ColorWheelSelector::_sliderReleased( SPColorSlider *slider, SPColorWheelSelector *cs )
257 {
258 (void)slider;
259 ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
260 if (wheelSelector->_dragging) {
261 wheelSelector->_dragging = FALSE;
262 wheelSelector->_released();
263 wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
264 }
265 }
267 void ColorWheelSelector::_sliderChanged( SPColorSlider *slider, SPColorWheelSelector *cs )
268 {
269 (void)slider;
270 ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
272 wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
273 }
275 void ColorWheelSelector::_wheelChanged( SPColorWheel *wheel, SPColorWheelSelector *cs )
276 {
277 ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
278 SPColor color;
280 sp_color_wheel_get_color( wheel, &color );
282 guint32 start = color.toRGBA32( 0x00 );
283 guint32 mid = color.toRGBA32( 0x7f );
284 guint32 end = color.toRGBA32( 0xff );
286 sp_color_slider_set_colors (SP_COLOR_SLIDER(wheelSelector->_slider), start, mid, end);
288 wheelSelector->_updateInternals( color, wheelSelector->_alpha, sp_color_wheel_is_adjusting( wheel ) );
289 }
292 /*
293 Local Variables:
294 mode:c++
295 c-file-style:"stroustrup"
296 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
297 indent-tabs-mode:nil
298 fill-column:99
299 End:
300 */
301 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :