~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to libgimpwidgets/gimpcolorscales.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LIBGIMP - The GIMP Library
 
2
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 
3
 *
 
4
 * gimpcolorscales.c
 
5
 * Copyright (C) 2002 Michael Natterer <mitch@gimp.org>
 
6
 *
 
7
 * based on color_notebook module
 
8
 * Copyright (C) 1998 Austin Donnelly <austin@greenend.org.uk>
 
9
 *
 
10
 * This library is free software; you can redistribute it and/or
 
11
 * modify it under the terms of the GNU Lesser General Public
 
12
 * License as published by the Free Software Foundation; either
 
13
 * version 2 of the License, or (at your option) any later version.
 
14
 *
 
15
 * This library is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
 * Library General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU Lesser General Public
 
21
 * License along with this library; if not, write to the
 
22
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
23
 * Boston, MA 02111-1307, USA.
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
 
 
28
#include <string.h>
 
29
 
 
30
#include <gtk/gtk.h>
 
31
 
 
32
#include "libgimpcolor/gimpcolor.h"
 
33
#include "libgimpmath/gimpmath.h"
 
34
 
 
35
#include "gimpwidgetstypes.h"
 
36
 
 
37
#include "gimpcolorscale.h"
 
38
#include "gimpcolorscales.h"
 
39
#include "gimpcolorhexentry.h"
 
40
#include "gimpwidgets.h"
 
41
 
 
42
#include "libgimp/libgimp-intl.h"
 
43
 
 
44
 
 
45
#define GIMP_COLOR_SCALES_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_COLOR_SCALES, GimpColorScalesClass))
 
46
#define GIMP_IS_COLOR_SCALES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_COLOR_SCALES))
 
47
#define GIMP_COLOR_SCALES_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_COLOR_SCALES, GimpColorScalesClass))
 
48
 
 
49
 
 
50
typedef struct _GimpColorScalesClass GimpColorScalesClass;
 
51
 
 
52
struct _GimpColorScales
 
53
{
 
54
  GimpColorSelector  parent_instance;
 
55
 
 
56
  GtkWidget         *toggles[7];
 
57
  GtkWidget         *sliders[7];
 
58
  GtkObject         *slider_data[7];
 
59
  GtkWidget         *hex_entry;
 
60
};
 
61
 
 
62
struct _GimpColorScalesClass
 
63
{
 
64
  GimpColorSelectorClass  parent_class;
 
65
};
 
66
 
 
67
 
 
68
static void   gimp_color_scales_class_init (GimpColorScalesClass  *klass);
 
69
static void   gimp_color_scales_init       (GimpColorScales       *scales);
 
70
 
 
71
static void   gimp_color_scales_togg_sensitive (GimpColorSelector *selector,
 
72
                                                gboolean           sensitive);
 
73
static void   gimp_color_scales_togg_visible   (GimpColorSelector *selector,
 
74
                                                gboolean           visible);
 
75
 
 
76
static void   gimp_color_scales_set_show_alpha (GimpColorSelector *selector,
 
77
                                                gboolean           show_alpha);
 
78
static void   gimp_color_scales_set_color      (GimpColorSelector *selector,
 
79
                                                const GimpRGB     *rgb,
 
80
                                                const GimpHSV     *hsv);
 
81
static void   gimp_color_scales_set_channel    (GimpColorSelector *selector,
 
82
                                                GimpColorSelectorChannel  channel);
 
83
 
 
84
static void   gimp_color_scales_update_scales  (GimpColorScales   *scales,
 
85
                                                gint               skip);
 
86
static void   gimp_color_scales_toggle_update  (GtkWidget         *widget,
 
87
                                                GimpColorScales   *scales);
 
88
static void   gimp_color_scales_scale_update   (GtkAdjustment     *adjustment,
 
89
                                                GimpColorScales   *scales);
 
90
 
 
91
static void   gimp_color_scales_entry_changed  (GimpColorHexEntry *entry,
 
92
                                                GimpColorScales   *scales);
 
93
 
 
94
 
 
95
static GimpColorSelectorClass *parent_class = NULL;
 
96
 
 
97
 
 
98
GType
 
99
gimp_color_scales_get_type (void)
 
100
{
 
101
  static GType scales_type = 0;
 
102
 
 
103
  if (! scales_type)
 
104
    {
 
105
      static const GTypeInfo scales_info =
 
106
      {
 
107
        sizeof (GimpColorScalesClass),
 
108
        (GBaseInitFunc) NULL,
 
109
        (GBaseFinalizeFunc) NULL,
 
110
        (GClassInitFunc) gimp_color_scales_class_init,
 
111
        NULL,           /* class_finalize */
 
112
        NULL,           /* class_data     */
 
113
        sizeof (GimpColorScales),
 
114
        0,              /* n_preallocs    */
 
115
        (GInstanceInitFunc) gimp_color_scales_init,
 
116
      };
 
117
 
 
118
      scales_type = g_type_register_static (GIMP_TYPE_COLOR_SELECTOR,
 
119
                                            "GimpColorScales",
 
120
                                            &scales_info, 0);
 
121
    }
 
122
 
 
123
  return scales_type;
 
124
}
 
125
 
 
126
static void
 
127
gimp_color_scales_class_init (GimpColorScalesClass *klass)
 
128
{
 
129
  GimpColorSelectorClass *selector_class = GIMP_COLOR_SELECTOR_CLASS (klass);
 
130
 
 
131
  parent_class = g_type_class_peek_parent (klass);
 
132
 
 
133
  selector_class->name                  = _("Scales");
 
134
  selector_class->help_id               = "gimp-colorselector-scales";
 
135
  selector_class->stock_id              = GIMP_STOCK_TOOL_OPTIONS;
 
136
  selector_class->set_toggles_visible   = gimp_color_scales_togg_visible;
 
137
  selector_class->set_toggles_sensitive = gimp_color_scales_togg_sensitive;
 
138
  selector_class->set_show_alpha        = gimp_color_scales_set_show_alpha;
 
139
  selector_class->set_color             = gimp_color_scales_set_color;
 
140
  selector_class->set_channel           = gimp_color_scales_set_channel;
 
141
}
 
142
 
 
143
static void
 
144
gimp_color_scales_init (GimpColorScales *scales)
 
145
{
 
146
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (scales);
 
147
  GtkWidget         *table;
 
148
  GtkWidget         *hbox;
 
149
  GtkWidget         *label;
 
150
  GSList            *group;
 
151
  gint               i;
 
152
 
 
153
  static const gchar *toggle_titles[] =
 
154
  {
 
155
    N_("_H"),
 
156
    N_("_S"),
 
157
    N_("_V"),
 
158
    N_("_R"),
 
159
    N_("_G"),
 
160
    N_("_B"),
 
161
    N_("_A")
 
162
  };
 
163
  static const gchar *slider_tips[7] =
 
164
  {
 
165
    N_("Hue"),
 
166
    N_("Saturation"),
 
167
    N_("Value"),
 
168
    N_("Red"),
 
169
    N_("Green"),
 
170
    N_("Blue"),
 
171
    N_("Alpha")
 
172
  };
 
173
  static gdouble slider_initial_vals[] = {   0,   0,   0,   0,   0,   0,   0 };
 
174
  static gdouble slider_max_vals[]     = { 360, 100, 100, 255, 255, 255, 100 };
 
175
  static gdouble slider_incs[]         = {  30,  10,  10,  16,  16,  16,  10 };
 
176
 
 
177
  /*  don't needs the toggles for our own operation  */
 
178
  selector->toggles_visible = FALSE;
 
179
 
 
180
  table = gtk_table_new (7, 4, FALSE);
 
181
  gtk_table_set_row_spacings (GTK_TABLE (table), 1);
 
182
  gtk_table_set_row_spacing (GTK_TABLE (table), 2, 3); /* hsv <-> rgb   */
 
183
  gtk_table_set_row_spacing (GTK_TABLE (table), 5, 3); /* rgb <-> alpha */
 
184
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
 
185
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 0);
 
186
  gtk_box_pack_start (GTK_BOX (scales), table, FALSE, FALSE, 0);
 
187
  gtk_widget_show (table);
 
188
 
 
189
  group = NULL;
 
190
 
 
191
  for (i = 0; i < 7; i++)
 
192
    {
 
193
      if (i == 6)
 
194
        {
 
195
          scales->toggles[i] = NULL;
 
196
        }
 
197
      else
 
198
        {
 
199
          scales->toggles[i] = gtk_radio_button_new (group);
 
200
          group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (scales->toggles[i]));
 
201
          gtk_table_attach (GTK_TABLE (table), scales->toggles[i],
 
202
                            0, 1, i, i + 1,
 
203
                            GTK_SHRINK, GTK_EXPAND, 0, 0);
 
204
 
 
205
          if (selector->toggles_visible)
 
206
            gtk_widget_show (scales->toggles[i]);
 
207
 
 
208
          gimp_help_set_help_data (scales->toggles[i],
 
209
                                   gettext (slider_tips[i]), NULL);
 
210
 
 
211
          g_signal_connect (scales->toggles[i], "toggled",
 
212
                            G_CALLBACK (gimp_color_scales_toggle_update),
 
213
                            scales);
 
214
        }
 
215
 
 
216
      scales->slider_data[i] =
 
217
        gimp_color_scale_entry_new (GTK_TABLE (table), 1, i,
 
218
                                    gettext (toggle_titles[i]),
 
219
                                    -1, -1,
 
220
                                    slider_initial_vals[i],
 
221
                                    0.0, slider_max_vals[i],
 
222
                                    1.0, slider_incs[i],
 
223
                                    0,
 
224
                                    gettext (slider_tips[i]),
 
225
                                    NULL);
 
226
 
 
227
      scales->sliders[i] = GIMP_SCALE_ENTRY_SCALE (scales->slider_data[i]);
 
228
 
 
229
      gimp_color_scale_set_channel (GIMP_COLOR_SCALE (scales->sliders[i]), i);
 
230
 
 
231
      g_signal_connect (scales->slider_data[i], "value_changed",
 
232
                        G_CALLBACK (gimp_color_scales_scale_update),
 
233
                        scales);
 
234
    }
 
235
 
 
236
  /* The hex triplet entry */
 
237
  hbox = gtk_hbox_new (FALSE, 6);
 
238
  gtk_box_pack_end (GTK_BOX (scales), hbox, FALSE, FALSE, 0);
 
239
  gtk_widget_show (hbox);
 
240
 
 
241
  scales->hex_entry = gimp_color_hex_entry_new ();
 
242
  gimp_help_set_help_data (scales->hex_entry,
 
243
                           _("Hexadecimal color notation "
 
244
                             "as used in HTML and CSS"), NULL);
 
245
  gtk_box_pack_end (GTK_BOX (hbox), scales->hex_entry, TRUE, TRUE, 0);
 
246
  gtk_widget_show (scales->hex_entry);
 
247
 
 
248
  label = gtk_label_new_with_mnemonic (_("HTML _Notation:"));
 
249
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), scales->hex_entry);
 
250
  gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
 
251
  gtk_widget_show (label);
 
252
 
 
253
  g_signal_connect (scales->hex_entry, "color_changed",
 
254
                    G_CALLBACK (gimp_color_scales_entry_changed),
 
255
                    scales);
 
256
}
 
257
 
 
258
static void
 
259
gimp_color_scales_togg_sensitive (GimpColorSelector *selector,
 
260
                                  gboolean           sensitive)
 
261
{
 
262
  GimpColorScales *scales = GIMP_COLOR_SCALES (selector);
 
263
  gint             i;
 
264
 
 
265
  for (i = 0; i < 6; i++)
 
266
    gtk_widget_set_sensitive (scales->toggles[i], sensitive);
 
267
}
 
268
 
 
269
static void
 
270
gimp_color_scales_togg_visible (GimpColorSelector *selector,
 
271
                                gboolean           visible)
 
272
{
 
273
  GimpColorScales *scales = GIMP_COLOR_SCALES (selector);
 
274
  gint             i;
 
275
 
 
276
  for (i = 0; i < 6; i++)
 
277
    {
 
278
      if (visible)
 
279
        gtk_widget_show (scales->toggles[i]);
 
280
      else
 
281
        gtk_widget_hide (scales->toggles[i]);
 
282
    }
 
283
}
 
284
 
 
285
static void
 
286
gimp_color_scales_set_show_alpha (GimpColorSelector *selector,
 
287
                                  gboolean           show_alpha)
 
288
{
 
289
  GimpColorScales *scales = GIMP_COLOR_SCALES (selector);
 
290
  GtkWidget       *label;
 
291
  GtkWidget       *scale;
 
292
  GtkWidget       *spin;
 
293
 
 
294
  label = GIMP_SCALE_ENTRY_LABEL (scales->slider_data[6]);
 
295
  scale = GIMP_SCALE_ENTRY_SCALE (scales->slider_data[6]);
 
296
  spin  = GIMP_SCALE_ENTRY_SPINBUTTON (scales->slider_data[6]);
 
297
 
 
298
  if (show_alpha)
 
299
    {
 
300
      gtk_widget_show (label);
 
301
      gtk_widget_show (scale);
 
302
      gtk_widget_show (spin);
 
303
    }
 
304
  else
 
305
    {
 
306
      gtk_widget_hide (label);
 
307
      gtk_widget_hide (scale);
 
308
      gtk_widget_hide (spin);
 
309
    }
 
310
}
 
311
 
 
312
static void
 
313
gimp_color_scales_set_color (GimpColorSelector *selector,
 
314
                             const GimpRGB     *rgb,
 
315
                             const GimpHSV     *hsv)
 
316
{
 
317
  GimpColorScales *scales = GIMP_COLOR_SCALES (selector);
 
318
 
 
319
  gimp_color_scales_update_scales (scales, -1);
 
320
}
 
321
 
 
322
static void
 
323
gimp_color_scales_set_channel (GimpColorSelector        *selector,
 
324
                               GimpColorSelectorChannel  channel)
 
325
{
 
326
  GimpColorScales *scales = GIMP_COLOR_SCALES (selector);
 
327
 
 
328
  if (channel >= 0 && channel <= 7)
 
329
    {
 
330
      g_signal_handlers_block_by_func (scales->toggles[channel],
 
331
                                       gimp_color_scales_toggle_update,
 
332
                                       scales);
 
333
 
 
334
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scales->toggles[channel]),
 
335
                                    TRUE);
 
336
 
 
337
      g_signal_handlers_unblock_by_func (scales->toggles[channel],
 
338
                                         gimp_color_scales_toggle_update,
 
339
                                         scales);
 
340
    }
 
341
}
 
342
 
 
343
static void
 
344
gimp_color_scales_update_scales (GimpColorScales *scales,
 
345
                                 gint             skip)
 
346
{
 
347
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (scales);
 
348
  gint               values[7];
 
349
  gint               i;
 
350
 
 
351
  values[GIMP_COLOR_SELECTOR_HUE]        = ROUND (selector->hsv.h * 360.0);
 
352
  values[GIMP_COLOR_SELECTOR_SATURATION] = ROUND (selector->hsv.s * 100.0);
 
353
  values[GIMP_COLOR_SELECTOR_VALUE]      = ROUND (selector->hsv.v * 100.0);
 
354
  values[GIMP_COLOR_SELECTOR_RED]        = ROUND (selector->rgb.r * 255.0);
 
355
  values[GIMP_COLOR_SELECTOR_GREEN]      = ROUND (selector->rgb.g * 255.0);
 
356
  values[GIMP_COLOR_SELECTOR_BLUE]       = ROUND (selector->rgb.b * 255.0);
 
357
  values[GIMP_COLOR_SELECTOR_ALPHA]      = ROUND (selector->rgb.a * 100.0);
 
358
 
 
359
  for (i = 0; i < 7; i++)
 
360
    {
 
361
      if (i != skip)
 
362
        {
 
363
          g_signal_handlers_block_by_func (scales->slider_data[i],
 
364
                                           gimp_color_scales_scale_update,
 
365
                                           scales);
 
366
 
 
367
          gtk_adjustment_set_value (GTK_ADJUSTMENT (scales->slider_data[i]),
 
368
                                    values[i]);
 
369
 
 
370
          g_signal_handlers_unblock_by_func (scales->slider_data[i],
 
371
                                             gimp_color_scales_scale_update,
 
372
                                             scales);
 
373
        }
 
374
 
 
375
      gimp_color_scale_set_color (GIMP_COLOR_SCALE (scales->sliders[i]),
 
376
                                  &selector->rgb, &selector->hsv);
 
377
    }
 
378
 
 
379
  g_signal_handlers_block_by_func (scales->hex_entry,
 
380
                                   gimp_color_scales_entry_changed,
 
381
                                   scales);
 
382
 
 
383
  gimp_color_hex_entry_set_color (GIMP_COLOR_HEX_ENTRY (scales->hex_entry),
 
384
                                                        &selector->rgb);
 
385
 
 
386
  g_signal_handlers_unblock_by_func (scales->hex_entry,
 
387
                                     gimp_color_scales_entry_changed,
 
388
                                     scales);
 
389
}
 
390
 
 
391
static void
 
392
gimp_color_scales_toggle_update (GtkWidget       *widget,
 
393
                                 GimpColorScales *scales)
 
394
{
 
395
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (scales);
 
396
 
 
397
  if (GTK_TOGGLE_BUTTON (widget)->active)
 
398
    {
 
399
      gint i;
 
400
 
 
401
      for (i = 0; i < 6; i++)
 
402
        if (widget == scales->toggles[i])
 
403
          {
 
404
            selector->channel = (GimpColorSelectorChannel) i;
 
405
            break;
 
406
          }
 
407
 
 
408
      gimp_color_selector_channel_changed (selector);
 
409
    }
 
410
}
 
411
 
 
412
static void
 
413
gimp_color_scales_scale_update (GtkAdjustment   *adjustment,
 
414
                                GimpColorScales *scales)
 
415
{
 
416
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (scales);
 
417
  gint               i;
 
418
 
 
419
  for (i = 0; i < 7; i++)
 
420
    if (scales->slider_data[i] == GTK_OBJECT (adjustment))
 
421
      break;
 
422
 
 
423
  switch (i)
 
424
    {
 
425
    case GIMP_COLOR_SELECTOR_HUE:
 
426
      selector->hsv.h = GTK_ADJUSTMENT (adjustment)->value / 360.0;
 
427
      break;
 
428
 
 
429
    case GIMP_COLOR_SELECTOR_SATURATION:
 
430
      selector->hsv.s = GTK_ADJUSTMENT (adjustment)->value / 100.0;
 
431
      break;
 
432
 
 
433
    case GIMP_COLOR_SELECTOR_VALUE:
 
434
      selector->hsv.v = GTK_ADJUSTMENT (adjustment)->value / 100.0;
 
435
      break;
 
436
 
 
437
    case GIMP_COLOR_SELECTOR_RED:
 
438
      selector->rgb.r = GTK_ADJUSTMENT (adjustment)->value / 255.0;
 
439
      break;
 
440
 
 
441
    case GIMP_COLOR_SELECTOR_GREEN:
 
442
      selector->rgb.g = GTK_ADJUSTMENT (adjustment)->value / 255.0;
 
443
      break;
 
444
 
 
445
    case GIMP_COLOR_SELECTOR_BLUE:
 
446
      selector->rgb.b = GTK_ADJUSTMENT (adjustment)->value / 255.0;
 
447
      break;
 
448
 
 
449
    case GIMP_COLOR_SELECTOR_ALPHA:
 
450
      selector->hsv.a = selector->rgb.a =
 
451
        GTK_ADJUSTMENT (adjustment)->value / 100.0;
 
452
      break;
 
453
    }
 
454
 
 
455
  if ((i >= GIMP_COLOR_SELECTOR_HUE) && (i <= GIMP_COLOR_SELECTOR_VALUE))
 
456
    {
 
457
      gimp_hsv_to_rgb (&selector->hsv, &selector->rgb);
 
458
    }
 
459
  else if ((i >= GIMP_COLOR_SELECTOR_RED) && (i <= GIMP_COLOR_SELECTOR_BLUE))
 
460
    {
 
461
      gimp_rgb_to_hsv (&selector->rgb, &selector->hsv);
 
462
    }
 
463
 
 
464
  gimp_color_scales_update_scales (scales, i);
 
465
 
 
466
  gimp_color_selector_color_changed (selector);
 
467
}
 
468
 
 
469
static void
 
470
gimp_color_scales_entry_changed (GimpColorHexEntry *entry,
 
471
                                 GimpColorScales   *scales)
 
472
{
 
473
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (scales);
 
474
 
 
475
  gimp_color_hex_entry_get_color (entry, &selector->rgb);
 
476
 
 
477
  gimp_rgb_to_hsv (&selector->rgb, &selector->hsv);
 
478
  gimp_color_scales_update_scales (scales, -1);
 
479
 
 
480
  gimp_color_selector_color_changed (selector);
 
481
}