~valavanisalex/ubuntu/precise/inkscape/fix-943984

« back to all changes in this revision

Viewing changes to inkscape-0.47pre1/src/widgets/sp-color-wheel-selector.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2009-07-02 17:09:45 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20090702170945-nn6d6zswovbwju1t
Tags: 0.47~pre1-0ubuntu1
* New upstream release.
  - Don't constrain maximization on small resolution devices (pre0)
    (LP: #348842)
  - Fixes segfault on startup (pre0)
    (LP: #391149)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
14
 
 
15
G_BEGIN_DECLS
 
16
 
 
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);
 
20
 
 
21
static void sp_color_wheel_selector_show_all (GtkWidget *widget);
 
22
static void sp_color_wheel_selector_hide_all (GtkWidget *widget);
 
23
 
 
24
 
 
25
G_END_DECLS
 
26
 
 
27
static SPColorSelectorClass *parent_class;
 
28
 
 
29
#define XPAD 4
 
30
#define YPAD 1
 
31
 
 
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
        };
 
49
 
 
50
        type = g_type_register_static (SP_TYPE_COLOR_SELECTOR,
 
51
                                       "SPColorWheelSelector",
 
52
                                       &info,
 
53
                                       static_cast< GTypeFlags > (0) );
 
54
    }
 
55
    return type;
 
56
}
 
57
 
 
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;
 
65
 
 
66
    object_class = (GtkObjectClass *) klass;
 
67
    widget_class = (GtkWidgetClass *) klass;
 
68
    selector_class = SP_COLOR_SELECTOR_CLASS (klass);
 
69
 
 
70
    parent_class = SP_COLOR_SELECTOR_CLASS (g_type_class_peek_parent (klass));
 
71
 
 
72
    selector_class->name = nameset;
 
73
    selector_class->submode_count = 1;
 
74
 
 
75
    object_class->destroy = sp_color_wheel_selector_destroy;
 
76
 
 
77
    widget_class->show_all = sp_color_wheel_selector_show_all;
 
78
    widget_class->hide_all = sp_color_wheel_selector_hide_all;
 
79
}
 
80
 
 
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
}
 
91
 
 
92
ColorWheelSelector::~ColorWheelSelector()
 
93
{
 
94
    _adj = 0;
 
95
    _wheel = 0;
 
96
    _sbtn = 0;
 
97
    _label = 0;
 
98
}
 
99
 
 
100
void sp_color_wheel_selector_init (SPColorWheelSelector *cs)
 
101
{
 
102
    SP_COLOR_SELECTOR(cs)->base = new ColorWheelSelector( SP_COLOR_SELECTOR(cs) );
 
103
 
 
104
    if ( SP_COLOR_SELECTOR(cs)->base )
 
105
    {
 
106
        SP_COLOR_SELECTOR(cs)->base->init();
 
107
    }
 
108
}
 
109
 
 
110
void ColorWheelSelector::init()
 
111
{
 
112
    GtkWidget *t;
 
113
    gint row = 0;
 
114
 
 
115
    _updating = FALSE;
 
116
    _dragging = FALSE;
 
117
 
 
118
    _tt = gtk_tooltips_new();
 
119
 
 
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);
 
123
 
 
124
    /* Create components */
 
125
    row = 0;
 
126
 
 
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);
 
130
 
 
131
    row++;
 
132
 
 
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);
 
138
 
 
139
    /* Adjustment */
 
140
    _adj = (GtkAdjustment *) gtk_adjustment_new (0.0, 0.0, 255.0, 1.0, 10.0, 10.0);
 
141
 
 
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);
 
147
 
 
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));
 
152
 
 
153
 
 
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);
 
161
 
 
162
    /* Signals */
 
163
    gtk_signal_connect (GTK_OBJECT (_adj), "value_changed",
 
164
                        GTK_SIGNAL_FUNC (_adjustmentChanged), _csel);
 
165
 
 
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);
 
172
 
 
173
    gtk_signal_connect (GTK_OBJECT(_wheel), "changed",
 
174
                        GTK_SIGNAL_FUNC (_wheelChanged), _csel);
 
175
}
 
176
 
 
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
}
 
183
 
 
184
static void
 
185
sp_color_wheel_selector_show_all (GtkWidget *widget)
 
186
{
 
187
    gtk_widget_show (widget);
 
188
}
 
189
 
 
190
static void
 
191
sp_color_wheel_selector_hide_all (GtkWidget *widget)
 
192
{
 
193
    gtk_widget_hide (widget);
 
194
}
 
195
 
 
196
GtkWidget *
 
197
sp_color_wheel_selector_new (void)
 
198
{
 
199
    SPColorWheelSelector *csel;
 
200
 
 
201
    csel = (SPColorWheelSelector*)gtk_type_new (SP_TYPE_COLOR_WHEEL_SELECTOR);
 
202
 
 
203
    return GTK_WIDGET (csel);
 
204
}
 
205
 
 
206
/* Helpers for setting color value */
 
207
 
 
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 );
 
215
 
 
216
    guint32 start = _color.toRGBA32( 0x00 );
 
217
    guint32 mid = _color.toRGBA32( 0x7f );
 
218
    guint32 end = _color.toRGBA32( 0xff );
 
219
 
 
220
    sp_color_slider_set_colors(SP_COLOR_SLIDER(_slider), start, mid, end);
 
221
 
 
222
    ColorScales::setScaled(_adj, _alpha);
 
223
 
 
224
    _updating = FALSE;
 
225
}
 
226
 
 
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
    }
 
234
 
 
235
    ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
 
236
    if (wheelSelector->_updating) return;
 
237
 
 
238
    wheelSelector->_updating = TRUE;
 
239
 
 
240
    wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
 
241
 
 
242
    wheelSelector->_updating = FALSE;
 
243
}
 
244
 
 
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
}
 
255
 
 
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
}
 
266
 
 
267
void ColorWheelSelector::_sliderChanged( SPColorSlider *slider, SPColorWheelSelector *cs )
 
268
{
 
269
    (void)slider;
 
270
    ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
 
271
 
 
272
    wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
 
273
}
 
274
 
 
275
void ColorWheelSelector::_wheelChanged( SPColorWheel *wheel, SPColorWheelSelector *cs )
 
276
{
 
277
    ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
 
278
    SPColor color;
 
279
 
 
280
    sp_color_wheel_get_color( wheel, &color );
 
281
 
 
282
    guint32 start = color.toRGBA32( 0x00 );
 
283
    guint32 mid = color.toRGBA32( 0x7f );
 
284
    guint32 end = color.toRGBA32( 0xff );
 
285
 
 
286
    sp_color_slider_set_colors (SP_COLOR_SLIDER(wheelSelector->_slider), start, mid, end);
 
287
 
 
288
    wheelSelector->_updateInternals( color, wheelSelector->_alpha, sp_color_wheel_is_adjusting( wheel ) );
 
289
}
 
290
 
 
291
 
 
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 :