gimpcolorscales.c 13.1 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 4 5 6 7 8 9 10 11 12 13
 *
 * gimpcolorscales.c
 * Copyright (C) 2002 Michael Natterer <mitch@gimp.org>
 *
 * based on color_notebook module
 * Copyright (C) 1998 Austin Donnelly <austin@greenend.org.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
14 15 16 17
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 19 20 21 22 23 24 25 26 27
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

28
#include <string.h>
29 30 31

#include <gtk/gtk.h>

32
#include "libgimpbase/gimpbase.h"
33
#include "libgimpcolor/gimpcolor.h"
34
#include "libgimpmath/gimpmath.h"
35 36 37

#include "gimpwidgetstypes.h"

38
#include "gimpcolorscale.h"
39 40 41
#include "gimpcolorscales.h"
#include "gimpwidgets.h"

42
#include "libgimp/libgimp-intl.h"
43 44 45 46 47 48 49 50 51 52 53 54 55 56


#define GIMP_COLOR_SCALES_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_COLOR_SCALES, GimpColorScalesClass))
#define GIMP_IS_COLOR_SCALES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_COLOR_SCALES))
#define GIMP_COLOR_SCALES_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_COLOR_SCALES, GimpColorScalesClass))


typedef struct _GimpColorScalesClass GimpColorScalesClass;

struct _GimpColorScales
{
  GimpColorSelector  parent_instance;

  GtkWidget         *toggles[7];
57
  GtkWidget         *sliders[7];
58 59 60 61 62 63 64 65 66
  GtkObject         *slider_data[7];
};

struct _GimpColorScalesClass
{
  GimpColorSelectorClass  parent_class;
};


67 68 69 70 71
static void   gimp_color_scales_togg_sensitive (GimpColorSelector *selector,
                                                gboolean           sensitive);
static void   gimp_color_scales_togg_visible   (GimpColorSelector *selector,
                                                gboolean           visible);

72 73 74 75 76 77 78 79 80 81 82 83 84 85
static void   gimp_color_scales_set_show_alpha (GimpColorSelector *selector,
                                                gboolean           show_alpha);
static void   gimp_color_scales_set_color      (GimpColorSelector *selector,
                                                const GimpRGB     *rgb,
                                                const GimpHSV     *hsv);
static void   gimp_color_scales_set_channel    (GimpColorSelector *selector,
                                                GimpColorSelectorChannel  channel);

static void   gimp_color_scales_update_scales  (GimpColorScales   *scales,
                                                gint               skip);
static void   gimp_color_scales_toggle_update  (GtkWidget         *widget,
                                                GimpColorScales   *scales);
static void   gimp_color_scales_scale_update   (GtkAdjustment     *adjustment,
                                                GimpColorScales   *scales);
86

87

88
G_DEFINE_TYPE (GimpColorScales, gimp_color_scales, GIMP_TYPE_COLOR_SELECTOR)
89

90
#define parent_class gimp_color_scales_parent_class
91 92 93 94 95


static void
gimp_color_scales_class_init (GimpColorScalesClass *klass)
{
96
  GimpColorSelectorClass *selector_class = GIMP_COLOR_SELECTOR_CLASS (klass);
97

98
  selector_class->name                  = _("Scales");
99
  selector_class->help_id               = "gimp-colorselector-scales";
100 101 102 103 104 105
  selector_class->stock_id              = GIMP_STOCK_TOOL_OPTIONS;
  selector_class->set_toggles_visible   = gimp_color_scales_togg_visible;
  selector_class->set_toggles_sensitive = gimp_color_scales_togg_sensitive;
  selector_class->set_show_alpha        = gimp_color_scales_set_show_alpha;
  selector_class->set_color             = gimp_color_scales_set_color;
  selector_class->set_channel           = gimp_color_scales_set_channel;
106 107 108 109 110
}

static void
gimp_color_scales_init (GimpColorScales *scales)
{
111
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (scales);
112
  GtkWidget         *table;
113
  GEnumClass        *enum_class;
114 115
  GSList            *group;
  gint               i;
116

117 118 119 120 121 122
  static const gdouble slider_initial_vals[] =
    {   0,   0,   0,   0,   0,   0,   0 };
  static const gdouble slider_max_vals[] =
    { 360, 100, 100, 255, 255, 255, 100 };
  static const gdouble slider_incs[] =
    {  30,  10,  10,  16,  16,  16,  10 };
123

124 125 126
  /*  don't needs the toggles for our own operation  */
  selector->toggles_visible = FALSE;

127
  table = gtk_table_new (7, 4, FALSE);
128
  gtk_table_set_row_spacings (GTK_TABLE (table), 1);
129
  gtk_table_set_row_spacing (GTK_TABLE (table), 2, 3); /* hsv <-> rgb   */
130
  gtk_table_set_row_spacing (GTK_TABLE (table), 5, 3); /* rgb <-> alpha */
131 132 133 134 135
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 0);
  gtk_box_pack_start (GTK_BOX (scales), table, FALSE, FALSE, 0);
  gtk_widget_show (table);

136 137
  enum_class = g_type_class_ref (GIMP_TYPE_COLOR_SELECTOR_CHANNEL);

138
  group = NULL;
139

140
  for (i = GIMP_COLOR_SELECTOR_HUE; i <= GIMP_COLOR_SELECTOR_ALPHA; i++)
141
    {
142 143 144 145 146
      GimpEnumDesc *enum_desc;

      enum_desc = gimp_enum_get_desc (enum_class, i);

      if (i == GIMP_COLOR_SELECTOR_ALPHA)
Sven Neumann's avatar
Sven Neumann committed
147 148 149
        {
          scales->toggles[i] = NULL;
        }
150
      else
Sven Neumann's avatar
Sven Neumann committed
151 152 153 154 155 156
        {
          scales->toggles[i] = gtk_radio_button_new (group);
          group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (scales->toggles[i]));
          gtk_table_attach (GTK_TABLE (table), scales->toggles[i],
                            0, 1, i, i + 1,
                            GTK_SHRINK, GTK_EXPAND, 0, 0);
157 158 159 160

          if (selector->toggles_visible)
            gtk_widget_show (scales->toggles[i]);

Sven Neumann's avatar
Sven Neumann committed
161 162
          gimp_help_set_help_data (scales->toggles[i],
                                   gettext (enum_desc->value_help), NULL);
163

Sven Neumann's avatar
Sven Neumann committed
164 165 166 167
          g_signal_connect (scales->toggles[i], "toggled",
                            G_CALLBACK (gimp_color_scales_toggle_update),
                            scales);
        }
168

169 170
      scales->slider_data[i] =
        gimp_color_scale_entry_new (GTK_TABLE (table), 1, i,
171
                                    gettext (enum_desc->value_desc),
172
                                    -1, -1,
173 174 175 176
                                    slider_initial_vals[i],
                                    0.0, slider_max_vals[i],
                                    1.0, slider_incs[i],
                                    0,
177
                                    gettext (enum_desc->value_help),
178 179 180 181 182
                                    NULL);

      scales->sliders[i] = GIMP_SCALE_ENTRY_SCALE (scales->slider_data[i]);

      gimp_color_scale_set_channel (GIMP_COLOR_SCALE (scales->sliders[i]), i);
183

184
      g_signal_connect (scales->slider_data[i], "value-changed",
Sven Neumann's avatar
Sven Neumann committed
185 186
                        G_CALLBACK (gimp_color_scales_scale_update),
                        scales);
187
    }
188 189

  g_type_class_unref (enum_class);
190 191
}

192 193 194 195
static void
gimp_color_scales_togg_sensitive (GimpColorSelector *selector,
                                  gboolean           sensitive)
{
196
  GimpColorScales *scales = GIMP_COLOR_SCALES (selector);
197 198 199 200 201 202 203 204 205 206
  gint             i;

  for (i = 0; i < 6; i++)
    gtk_widget_set_sensitive (scales->toggles[i], sensitive);
}

static void
gimp_color_scales_togg_visible (GimpColorSelector *selector,
                                gboolean           visible)
{
207
  GimpColorScales *scales = GIMP_COLOR_SCALES (selector);
208 209 210 211 212 213 214 215 216 217 218
  gint             i;

  for (i = 0; i < 6; i++)
    {
      if (visible)
        gtk_widget_show (scales->toggles[i]);
      else
        gtk_widget_hide (scales->toggles[i]);
    }
}

219 220 221 222
static void
gimp_color_scales_set_show_alpha (GimpColorSelector *selector,
                                  gboolean           show_alpha)
{
223
  GimpColorScales *scales = GIMP_COLOR_SCALES (selector);
224 225 226
  GtkWidget       *label;
  GtkWidget       *scale;
  GtkWidget       *spin;
227
  GtkWidget       *table;
228 229 230 231 232

  label = GIMP_SCALE_ENTRY_LABEL (scales->slider_data[6]);
  scale = GIMP_SCALE_ENTRY_SCALE (scales->slider_data[6]);
  spin  = GIMP_SCALE_ENTRY_SPINBUTTON (scales->slider_data[6]);

233 234
  table = gtk_widget_get_parent (scale);
  if (GTK_IS_TABLE (table))
235 236 237 238
    {
      gtk_table_set_row_spacing (GTK_TABLE (table), 5, /* rgb <-> alpha */
                                 show_alpha ? 3 : 0);
    }
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
  if (show_alpha)
    {
      gtk_widget_show (label);
      gtk_widget_show (scale);
      gtk_widget_show (spin);
    }
  else
    {
      gtk_widget_hide (label);
      gtk_widget_hide (scale);
      gtk_widget_hide (spin);
    }
}

static void
gimp_color_scales_set_color (GimpColorSelector *selector,
                             const GimpRGB     *rgb,
                             const GimpHSV     *hsv)
{
259
  GimpColorScales *scales = GIMP_COLOR_SCALES (selector);
260 261 262 263 264 265 266 267

  gimp_color_scales_update_scales (scales, -1);
}

static void
gimp_color_scales_set_channel (GimpColorSelector        *selector,
                               GimpColorSelectorChannel  channel)
{
268
  GimpColorScales *scales = GIMP_COLOR_SCALES (selector);
269

270
  if (channel < 7)
271
    {
272
      g_signal_handlers_block_by_func (scales->toggles[channel],
273 274 275 276 277 278
                                       gimp_color_scales_toggle_update,
                                       scales);

      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scales->toggles[channel]),
                                    TRUE);

279
      g_signal_handlers_unblock_by_func (scales->toggles[channel],
280 281 282 283 284 285 286 287 288
                                         gimp_color_scales_toggle_update,
                                         scales);
    }
}

static void
gimp_color_scales_update_scales (GimpColorScales *scales,
                                 gint             skip)
{
289
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (scales);
290 291 292
  gint               values[7];
  gint               i;

293 294 295 296 297 298 299
  values[GIMP_COLOR_SELECTOR_HUE]        = ROUND (selector->hsv.h * 360.0);
  values[GIMP_COLOR_SELECTOR_SATURATION] = ROUND (selector->hsv.s * 100.0);
  values[GIMP_COLOR_SELECTOR_VALUE]      = ROUND (selector->hsv.v * 100.0);
  values[GIMP_COLOR_SELECTOR_RED]        = ROUND (selector->rgb.r * 255.0);
  values[GIMP_COLOR_SELECTOR_GREEN]      = ROUND (selector->rgb.g * 255.0);
  values[GIMP_COLOR_SELECTOR_BLUE]       = ROUND (selector->rgb.b * 255.0);
  values[GIMP_COLOR_SELECTOR_ALPHA]      = ROUND (selector->rgb.a * 100.0);
300 301 302 303 304

  for (i = 0; i < 7; i++)
    {
      if (i != skip)
        {
305
          g_signal_handlers_block_by_func (scales->slider_data[i],
306 307 308 309 310 311
                                           gimp_color_scales_scale_update,
                                           scales);

          gtk_adjustment_set_value (GTK_ADJUSTMENT (scales->slider_data[i]),
                                    values[i]);

312
          g_signal_handlers_unblock_by_func (scales->slider_data[i],
313 314 315
                                             gimp_color_scales_scale_update,
                                             scales);
        }
316 317 318

      gimp_color_scale_set_color (GIMP_COLOR_SCALE (scales->sliders[i]),
                                  &selector->rgb, &selector->hsv);
319 320 321 322 323 324 325
    }
}

static void
gimp_color_scales_toggle_update (GtkWidget       *widget,
                                 GimpColorScales *scales)
{
326
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (scales);
327 328 329 330 331 332 333

  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      gint i;

      for (i = 0; i < 6; i++)
        if (widget == scales->toggles[i])
334 335 336 337
          {
            selector->channel = (GimpColorSelectorChannel) i;
            break;
          }
338 339 340 341 342 343 344 345 346

      gimp_color_selector_channel_changed (selector);
    }
}

static void
gimp_color_scales_scale_update (GtkAdjustment   *adjustment,
                                GimpColorScales *scales)
{
347
  GimpColorSelector *selector = GIMP_COLOR_SELECTOR (scales);
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  gint               i;

  for (i = 0; i < 7; i++)
    if (scales->slider_data[i] == GTK_OBJECT (adjustment))
      break;

  switch (i)
    {
    case GIMP_COLOR_SELECTOR_HUE:
      selector->hsv.h = GTK_ADJUSTMENT (adjustment)->value / 360.0;
      break;

    case GIMP_COLOR_SELECTOR_SATURATION:
      selector->hsv.s = GTK_ADJUSTMENT (adjustment)->value / 100.0;
      break;

    case GIMP_COLOR_SELECTOR_VALUE:
      selector->hsv.v = GTK_ADJUSTMENT (adjustment)->value / 100.0;
      break;

    case GIMP_COLOR_SELECTOR_RED:
      selector->rgb.r = GTK_ADJUSTMENT (adjustment)->value / 255.0;
      break;

    case GIMP_COLOR_SELECTOR_GREEN:
      selector->rgb.g = GTK_ADJUSTMENT (adjustment)->value / 255.0;
      break;

    case GIMP_COLOR_SELECTOR_BLUE:
      selector->rgb.b = GTK_ADJUSTMENT (adjustment)->value / 255.0;
      break;

    case GIMP_COLOR_SELECTOR_ALPHA:
      selector->hsv.a = selector->rgb.a =
        GTK_ADJUSTMENT (adjustment)->value / 100.0;
      break;
    }

  if ((i >= GIMP_COLOR_SELECTOR_HUE) && (i <= GIMP_COLOR_SELECTOR_VALUE))
    {
      gimp_hsv_to_rgb (&selector->hsv, &selector->rgb);
    }
  else if ((i >= GIMP_COLOR_SELECTOR_RED) && (i <= GIMP_COLOR_SELECTOR_BLUE))
    {
      gimp_rgb_to_hsv (&selector->rgb, &selector->hsv);
    }

  gimp_color_scales_update_scales (scales, i);

  gimp_color_selector_color_changed (selector);
}