Code

784dd23ad4241a1917fcc6e023b051ecf21abdfa
[inkscape.git] / src / widgets / sp-color-wheel-selector.cpp
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"
13 #include "sp-color-icc-selector.h"
14 #include "../svg/svg-icc-color.h"
16 G_BEGIN_DECLS
18 static void sp_color_wheel_selector_class_init (SPColorWheelSelectorClass *klass);
19 static void sp_color_wheel_selector_init (SPColorWheelSelector *cs);
20 static void sp_color_wheel_selector_destroy (GtkObject *object);
22 static void sp_color_wheel_selector_show_all (GtkWidget *widget);
23 static void sp_color_wheel_selector_hide_all (GtkWidget *widget);
26 G_END_DECLS
28 static SPColorSelectorClass *parent_class;
30 #define XPAD 4
31 #define YPAD 1
33 GType
34 sp_color_wheel_selector_get_type (void)
35 {
36     static GType type = 0;
37     if (!type) {
38         static const GTypeInfo info = {
39             sizeof (SPColorWheelSelectorClass),
40             NULL, /* base_init */
41             NULL, /* base_finalize */
42             (GClassInitFunc) sp_color_wheel_selector_class_init,
43             NULL, /* class_finalize */
44             NULL, /* class_data */
45             sizeof (SPColorWheelSelector),
46             0,    /* n_preallocs */
47             (GInstanceInitFunc) sp_color_wheel_selector_init,
48             0,    /* value_table */
49         };
51         type = g_type_register_static (SP_TYPE_COLOR_SELECTOR,
52                                        "SPColorWheelSelector",
53                                        &info,
54                                        static_cast< GTypeFlags > (0) );
55     }
56     return type;
57 }
59 static void
60 sp_color_wheel_selector_class_init (SPColorWheelSelectorClass *klass)
61 {
62     static const gchar* nameset[] = {N_("Wheel"), 0};
63     GtkObjectClass *object_class;
64     GtkWidgetClass *widget_class;
65     SPColorSelectorClass *selector_class;
67     object_class = (GtkObjectClass *) klass;
68     widget_class = (GtkWidgetClass *) klass;
69     selector_class = SP_COLOR_SELECTOR_CLASS (klass);
71     parent_class = SP_COLOR_SELECTOR_CLASS (g_type_class_peek_parent (klass));
73     selector_class->name = nameset;
74     selector_class->submode_count = 1;
76     object_class->destroy = sp_color_wheel_selector_destroy;
78     widget_class->show_all = sp_color_wheel_selector_show_all;
79     widget_class->hide_all = sp_color_wheel_selector_hide_all;
80 }
82 ColorWheelSelector::ColorWheelSelector( SPColorSelector* csel )
83     : ColorSelector( csel ),
84       _updating( FALSE ),
85       _dragging( FALSE ),
86       _adj(0),
87       _wheel(0),
88       _sbtn(0),
89       _label(0)
90 {
91 }
93 ColorWheelSelector::~ColorWheelSelector()
94 {
95     _adj = 0;
96     _wheel = 0;
97     _sbtn = 0;
98     _label = 0;
99 }
101 void sp_color_wheel_selector_init (SPColorWheelSelector *cs)
103     SP_COLOR_SELECTOR(cs)->base = new ColorWheelSelector( SP_COLOR_SELECTOR(cs) );
105     if ( SP_COLOR_SELECTOR(cs)->base )
106     {
107         SP_COLOR_SELECTOR(cs)->base->init();
108     }
111 static void resizeHSVWheel( GtkHSV *hsv, GtkAllocation *allocation )
113     gint diam = std::min(allocation->width, allocation->height);
115     // drop a little for resizing
116     diam -= 4;
118     GtkStyle *style = gtk_widget_get_style( GTK_WIDGET(hsv) );
119     if ( style ) {
120         gint thick = std::max(style->xthickness, style->ythickness);
121         if (thick > 0) {
122             diam -= thick * 2;
123         }
124     }
125     gint padding = -1;
126     gtk_widget_style_get( GTK_WIDGET(hsv), 
127                           "focus-padding", &padding,
128                           NULL );
129     if (padding > 0) {
130         diam -= padding * 2;
131     }
132      
133     diam = std::max(20, diam);
134     gint ring = static_cast<gint>( static_cast<gdouble>(diam) / (4.0 * 1.618) );
135     gtk_hsv_set_metrics( hsv, diam, ring );
138 static void handleWheelStyleSet(GtkHSV *hsv, GtkStyle* /*previous*/, gpointer /*userData*/)
140     GtkAllocation allocation = {0, 0, 0, 0};
141     gtk_widget_get_allocation( GTK_WIDGET(hsv), &allocation );
142     resizeHSVWheel( hsv, &allocation );
145 static void handleWheelAllocation(GtkHSV *hsv, GtkAllocation *allocation, gpointer /*userData*/)
147     resizeHSVWheel( hsv, allocation );
150 void ColorWheelSelector::init()
152     GtkWidget *t;
153     gint row = 0;
155     _updating = FALSE;
156     _dragging = FALSE;
158     _tt = gtk_tooltips_new();
160     t = gtk_table_new (5, 3, FALSE);
161     gtk_widget_show (t);
162     gtk_box_pack_start (GTK_BOX (_csel), t, TRUE, TRUE, 0);
164     /* Create components */
165     row = 0;
167     _wheel = gtk_hsv_new();
168     gtk_hsv_set_metrics( GTK_HSV(_wheel), 48, 8 );
169     gtk_widget_show( _wheel );
170     gtk_table_attach( GTK_TABLE(t), _wheel, 0, 3, row, row + 1,  (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), XPAD, YPAD);
172     row++;
174     /* Label */
175     _label = gtk_label_new_with_mnemonic (_("_A:"));
176     gtk_misc_set_alignment (GTK_MISC (_label), 1.0, 0.5);
177     gtk_widget_show (_label);
178     gtk_table_attach (GTK_TABLE (t), _label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, XPAD, YPAD);
180     /* Adjustment */
181     _adj = (GtkAdjustment *) gtk_adjustment_new (0.0, 0.0, 255.0, 1.0, 10.0, 10.0);
183     /* Slider */
184     _slider = sp_color_slider_new (_adj);
185     gtk_tooltips_set_tip (_tt, _slider, _("Alpha (opacity)"), NULL);
186     gtk_widget_show (_slider);
187     gtk_table_attach (GTK_TABLE (t), _slider, 1, 2, row, row + 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)GTK_FILL, XPAD, YPAD);
189     sp_color_slider_set_colors (SP_COLOR_SLIDER (_slider),
190                                 SP_RGBA32_F_COMPOSE (1.0, 1.0, 1.0, 0.0),
191                                 SP_RGBA32_F_COMPOSE (1.0, 1.0, 1.0, 0.5),
192                                 SP_RGBA32_F_COMPOSE (1.0, 1.0, 1.0, 1.0));
195     /* Spinbutton */
196     _sbtn = gtk_spin_button_new (GTK_ADJUSTMENT (_adj), 1.0, 0);
197     gtk_tooltips_set_tip (_tt, _sbtn, _("Alpha (opacity)"), NULL);
198     sp_dialog_defocus_on_enter (_sbtn);
199     gtk_label_set_mnemonic_widget (GTK_LABEL(_label), _sbtn);
200     gtk_widget_show (_sbtn);
201     gtk_table_attach (GTK_TABLE (t), _sbtn, 2, 3, row, row + 1, (GtkAttachOptions)0, (GtkAttachOptions)0, XPAD, YPAD);
203     /* Signals */
204     gtk_signal_connect (GTK_OBJECT (_adj), "value_changed",
205                         GTK_SIGNAL_FUNC (_adjustmentChanged), _csel);
207     gtk_signal_connect (GTK_OBJECT (_slider), "grabbed",
208                         GTK_SIGNAL_FUNC (_sliderGrabbed), _csel);
209     gtk_signal_connect (GTK_OBJECT (_slider), "released",
210                         GTK_SIGNAL_FUNC (_sliderReleased), _csel);
211     gtk_signal_connect (GTK_OBJECT (_slider), "changed",
212                         GTK_SIGNAL_FUNC (_sliderChanged), _csel);
214     gtk_signal_connect( GTK_OBJECT(_wheel), "changed",
215                         GTK_SIGNAL_FUNC(_wheelChanged), _csel );
218     // GTK does not automatically scale the color wheel, so we have to add that in:
219     gtk_signal_connect( GTK_OBJECT(_wheel), "size-allocate",
220                         GTK_SIGNAL_FUNC(handleWheelAllocation), _csel );
221     gtk_signal_connect( GTK_OBJECT(_wheel), "style-set",
222                         GTK_SIGNAL_FUNC(handleWheelStyleSet), _csel );
225 static void
226 sp_color_wheel_selector_destroy (GtkObject *object)
228     if (((GtkObjectClass *) (parent_class))->destroy)
229         (* ((GtkObjectClass *) (parent_class))->destroy) (object);
232 static void
233 sp_color_wheel_selector_show_all (GtkWidget *widget)
235     gtk_widget_show (widget);
238 static void
239 sp_color_wheel_selector_hide_all (GtkWidget *widget)
241     gtk_widget_hide (widget);
244 GtkWidget *
245 sp_color_wheel_selector_new (void)
247     SPColorWheelSelector *csel;
249     csel = (SPColorWheelSelector*)gtk_type_new (SP_TYPE_COLOR_WHEEL_SELECTOR);
251     return GTK_WIDGET (csel);
254 /* Helpers for setting color value */
256 static void preserve_icc(SPColor *color, SPColorWheelSelector *cs){
257     ColorSelector* selector = (ColorSelector*)(SP_COLOR_SELECTOR(cs)->base);
258     color->icc = selector->getColor().icc ? new SVGICCColor(*selector->getColor().icc) : 0;
261 void ColorWheelSelector::_colorChanged()
263 #ifdef DUMP_CHANGE_INFO
264     g_message("ColorWheelSelector::_colorChanged( this=%p, %f, %f, %f,   %f)", this, color.v.c[0], color.v.c[1], color.v.c[2], alpha );
265 #endif
266     _updating = TRUE;
267     {
268         gdouble h = 0;
269         gdouble s = 0;
270         gdouble v = 0;
271         gtk_rgb_to_hsv( _color.v.c[0], _color.v.c[1], _color.v.c[2], &h, &s, &v  );
272         gtk_hsv_set_color( GTK_HSV(_wheel), h, s, v );
273     }
275     guint32 start = _color.toRGBA32( 0x00 );
276     guint32 mid = _color.toRGBA32( 0x7f );
277     guint32 end = _color.toRGBA32( 0xff );
279     sp_color_slider_set_colors(SP_COLOR_SLIDER(_slider), start, mid, end);
281     ColorScales::setScaled(_adj, _alpha);
283     _updating = FALSE;
286 void ColorWheelSelector::_adjustmentChanged( GtkAdjustment *adjustment, SPColorWheelSelector *cs )
288 // TODO check this. It looks questionable:
289     // if a value is entered between 0 and 1 exclusive, normalize it to (int) 0..255  or 0..100
290     if (adjustment->value > 0.0 && adjustment->value < 1.0) {
291         gtk_adjustment_set_value( adjustment, floor ((adjustment->value) * adjustment->upper + 0.5) );
292     }
294     ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
295     if (wheelSelector->_updating) return;
297     wheelSelector->_updating = TRUE;
299     preserve_icc(&wheelSelector->_color, cs);
300     wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
302     wheelSelector->_updating = FALSE;
305 void ColorWheelSelector::_sliderGrabbed( SPColorSlider *slider, SPColorWheelSelector *cs )
307     (void)slider;
308     ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
309     if (!wheelSelector->_dragging) {
310         wheelSelector->_dragging = TRUE;
311         wheelSelector->_grabbed();
313         preserve_icc(&wheelSelector->_color, cs);
314         wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
315     }
318 void ColorWheelSelector::_sliderReleased( SPColorSlider *slider, SPColorWheelSelector *cs )
320     (void)slider;
321     ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
322     if (wheelSelector->_dragging) {
323         wheelSelector->_dragging = FALSE;
324         wheelSelector->_released();
326         preserve_icc(&wheelSelector->_color, cs);
327         wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
328     }
331 void ColorWheelSelector::_sliderChanged( SPColorSlider *slider, SPColorWheelSelector *cs )
333     (void)slider;
334     ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
336     preserve_icc(&wheelSelector->_color, cs);
337     wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
340 void ColorWheelSelector::_wheelChanged( GtkHSV *hsv, SPColorWheelSelector *cs )
342     ColorWheelSelector* wheelSelector = static_cast<ColorWheelSelector*>(SP_COLOR_SELECTOR(cs)->base);
344     gdouble h = 0;
345     gdouble s = 0;
346     gdouble v = 0;
347     gtk_hsv_get_color( hsv, &h, &s, &v );
348     
349     gdouble r = 0;
350     gdouble g = 0;
351     gdouble b = 0;
352     gtk_hsv_to_rgb(h, s, v, &r, &g, &b);
354     SPColor color(r, g, b);
356     guint32 start = color.toRGBA32( 0x00 );
357     guint32 mid = color.toRGBA32( 0x7f );
358     guint32 end = color.toRGBA32( 0xff );
360     sp_color_slider_set_colors (SP_COLOR_SLIDER(wheelSelector->_slider), start, mid, end);
362     preserve_icc(&color, cs);
363     wheelSelector->_updateInternals( color, wheelSelector->_alpha, gtk_hsv_is_adjusting( hsv ) );
367 /*
368   Local Variables:
369   mode:c++
370   c-file-style:"stroustrup"
371   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
372   indent-tabs-mode:nil
373   fill-column:99
374   End:
375 */
376 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :