Code

moving trunk for module inkscape
[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"
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)
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     }
110 void ColorWheelSelector::init()
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);
177 static void
178 sp_color_wheel_selector_destroy (GtkObject *object)
180     if (((GtkObjectClass *) (parent_class))->destroy)
181         (* ((GtkObjectClass *) (parent_class))->destroy) (object);
184 static void
185 sp_color_wheel_selector_show_all (GtkWidget *widget)
187     gtk_widget_show (widget);
190 static void
191 sp_color_wheel_selector_hide_all (GtkWidget *widget)
193     gtk_widget_hide (widget);
196 GtkWidget *
197 sp_color_wheel_selector_new (void)
199     SPColorWheelSelector *csel;
201     csel = (SPColorWheelSelector*)gtk_type_new (SP_TYPE_COLOR_WHEEL_SELECTOR);
203     return GTK_WIDGET (csel);
206 /* Helpers for setting color value */
208 void ColorWheelSelector::_colorChanged( const SPColor& color, gfloat alpha )
210     _updating = TRUE;
211     sp_color_wheel_set_color( SP_COLOR_WHEEL( _wheel ), &color );
213     guint32 start = sp_color_get_rgba32_ualpha( &color, 0x00 );
214     guint32 mid = sp_color_get_rgba32_ualpha( &color, 0x7f );
215     guint32 end = sp_color_get_rgba32_ualpha( &color, 0xff );
217     sp_color_slider_set_colors (SP_COLOR_SLIDER(_slider), start, mid, end);
219     ColorScales::setScaled(_adj, alpha);
221     _updating = FALSE;
224 void ColorWheelSelector::_adjustmentChanged( GtkAdjustment *adjustment, SPColorWheelSelector *cs )
226 // TODO check this. It looks questionable:
227     // if a value is entered between 0 and 1 exclusive, normalize it to (int) 0..255  or 0..100
228     if (adjustment->value > 0.0 && adjustment->value < 1.0) {
229         gtk_adjustment_set_value( adjustment, floor ((adjustment->value) * adjustment->upper + 0.5) );
230     }
232     ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
233     if (wheelSelector->_updating) return;
235     wheelSelector->_updating = TRUE;
237     wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
239     wheelSelector->_updating = FALSE;
242 void ColorWheelSelector::_sliderGrabbed( SPColorSlider *slider, SPColorWheelSelector *cs )
244     ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
245     if (!wheelSelector->_dragging) {
246         wheelSelector->_dragging = TRUE;
247         wheelSelector->_grabbed();
248         wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
249     }
252 void ColorWheelSelector::_sliderReleased( SPColorSlider *slider, SPColorWheelSelector *cs )
254     ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
255     if (wheelSelector->_dragging) {
256         wheelSelector->_dragging = FALSE;
257         wheelSelector->_released();
258         wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
259     }
262 void ColorWheelSelector::_sliderChanged( SPColorSlider *slider, SPColorWheelSelector *cs )
264     ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
266     wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
269 void ColorWheelSelector::_wheelChanged( SPColorWheel *wheel, SPColorWheelSelector *cs )
271     ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
272     SPColor color;
274     sp_color_wheel_get_color( wheel, &color );
276     guint32 start = sp_color_get_rgba32_ualpha( &color, 0x00 );
277     guint32 mid = sp_color_get_rgba32_ualpha( &color, 0x7f );
278     guint32 end = sp_color_get_rgba32_ualpha( &color, 0xff );
280     sp_color_slider_set_colors (SP_COLOR_SLIDER(wheelSelector->_slider), start, mid, end);
282     wheelSelector->_updateInternals( color, wheelSelector->_alpha, sp_color_wheel_is_adjusting( wheel ) );
286 /*
287   Local Variables:
288   mode:c++
289   c-file-style:"stroustrup"
290   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
291   indent-tabs-mode:nil
292   fill-column:99
293   End:
294 */
295 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :