gimpcurvestool.c 25.5 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
Sven Neumann's avatar
Sven Neumann committed
18

19 20
#include "config.h"

21
#include <gegl.h>
Sven Neumann's avatar
Sven Neumann committed
22 23
#include <gtk/gtk.h>

24
#include "libgimpcolor/gimpcolor.h"
25
#include "libgimpconfig/gimpconfig.h"
26
#include "libgimpwidgets/gimpwidgets.h"
27

28
#include "tools-types.h"
Michael Natterer's avatar
Michael Natterer committed
29

30
#include "base/curves.h"
Michael Natterer's avatar
Michael Natterer committed
31 32 33
#include "base/gimphistogram.h"
#include "base/gimplut.h"

34 35 36
#include "gegl/gimpcurvesconfig.h"
#include "gegl/gimpoperationcurves.h"

37
#include "core/gimp.h"
38
#include "core/gimpcurve.h"
Michael Natterer's avatar
Michael Natterer committed
39
#include "core/gimpdrawable.h"
40
#include "core/gimpdrawable-histogram.h"
Michael Natterer's avatar
Michael Natterer committed
41 42
#include "core/gimpimage.h"

43
#include "widgets/gimpcolorbar.h"
44
#include "widgets/gimphelp-ids.h"
45
#include "widgets/gimpcurveview.h"
Michael Natterer's avatar
Michael Natterer committed
46

47 48
#include "display/gimpdisplay.h"

Michael Natterer's avatar
Michael Natterer committed
49
#include "gimpcurvestool.h"
50
#include "gimphistogramoptions.h"
Sven Neumann's avatar
Sven Neumann committed
51

52
#include "gimp-intl.h"
53

54

55 56 57
#define GRAPH_SIZE 256
#define BAR_SIZE    12
#define RADIUS       4
Elliot Lee's avatar
Elliot Lee committed
58 59


Michael Natterer's avatar
Michael Natterer committed
60 61
/*  local function prototypes  */

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static void       gimp_curves_tool_finalize       (GObject              *object);

static gboolean   gimp_curves_tool_initialize     (GimpTool             *tool,
                                                   GimpDisplay          *display,
                                                   GError              **error);
static void       gimp_curves_tool_button_release (GimpTool             *tool,
                                                   GimpCoords           *coords,
                                                   guint32               time,
                                                   GdkModifierType       state,
                                                   GimpButtonReleaseType release_type,
                                                   GimpDisplay          *display);
static gboolean   gimp_curves_tool_key_press      (GimpTool             *tool,
                                                   GdkEventKey          *kevent,
                                                   GimpDisplay          *display);
static void       gimp_curves_tool_oper_update    (GimpTool             *tool,
                                                   GimpCoords           *coords,
                                                   GdkModifierType       state,
                                                   gboolean              proximity,
                                                   GimpDisplay          *display);

static void       gimp_curves_tool_color_picked   (GimpColorTool        *color_tool,
                                                   GimpColorPickState    pick_state,
                                                   GimpImageType         sample_type,
                                                   GimpRGB              *color,
                                                   gint                  color_index);
87 88
static GeglNode * gimp_curves_tool_get_operation  (GimpImageMapTool     *image_map_tool,
                                                   GObject             **config);
89 90 91 92 93 94 95 96 97
static void       gimp_curves_tool_map            (GimpImageMapTool     *image_map_tool);
static void       gimp_curves_tool_dialog         (GimpImageMapTool     *image_map_tool);
static void       gimp_curves_tool_reset          (GimpImageMapTool     *image_map_tool);
static gboolean   gimp_curves_tool_settings_load  (GimpImageMapTool     *image_map_tool,
                                                   gpointer              fp,
                                                   GError              **error);
static gboolean   gimp_curves_tool_settings_save  (GimpImageMapTool     *image_map_tool,
                                                   gpointer              fp);

98 99
static void       gimp_curves_tool_config_notify  (GObject              *object,
                                                   GParamSpec           *pspec,
100
                                                   GimpCurvesTool       *tool);
101

102 103 104 105 106 107 108 109 110 111
static void       curves_channel_callback         (GtkWidget            *widget,
                                                   GimpCurvesTool       *tool);
static void       curves_channel_reset_callback   (GtkWidget            *widget,
                                                   GimpCurvesTool       *tool);

static gboolean   curves_menu_sensitivity         (gint                  value,
                                                   gpointer              data);

static void       curves_curve_type_callback      (GtkWidget            *widget,
                                                   GimpCurvesTool       *tool);
112

Michael Natterer's avatar
Michael Natterer committed
113

114
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
115 116

#define parent_class gimp_curves_tool_parent_class
117

Michael Natterer's avatar
Michael Natterer committed
118

119
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
120 121

void
Nate Summers's avatar
Nate Summers committed
122
gimp_curves_tool_register (GimpToolRegisterCallback  callback,
123
                           gpointer                  data)
Michael Natterer's avatar
Michael Natterer committed
124
{
Nate Summers's avatar
Nate Summers committed
125
  (* callback) (GIMP_TYPE_CURVES_TOOL,
126
                GIMP_TYPE_HISTOGRAM_OPTIONS,
127
                gimp_color_options_gui,
128
                0,
129
                "gimp-curves-tool",
130
                _("Curves"),
131
                _("Curves Tool: Adjust color curves"),
132
                N_("_Curves..."), NULL,
133
                NULL, GIMP_HELP_TOOL_CURVES,
Nate Summers's avatar
Nate Summers committed
134
                GIMP_STOCK_TOOL_CURVES,
135
                data);
Michael Natterer's avatar
Michael Natterer committed
136 137
}

138 139 140

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
141 142 143
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  GObjectClass          *object_class     = G_OBJECT_CLASS (klass);
  GimpToolClass         *tool_class       = GIMP_TOOL_CLASS (klass);
  GimpColorToolClass    *color_tool_class = GIMP_COLOR_TOOL_CLASS (klass);
  GimpImageMapToolClass *im_tool_class    = GIMP_IMAGE_MAP_TOOL_CLASS (klass);

  object_class->finalize           = gimp_curves_tool_finalize;

  tool_class->initialize           = gimp_curves_tool_initialize;
  tool_class->button_release       = gimp_curves_tool_button_release;
  tool_class->key_press            = gimp_curves_tool_key_press;
  tool_class->oper_update          = gimp_curves_tool_oper_update;

  color_tool_class->picked         = gimp_curves_tool_color_picked;

  im_tool_class->shell_desc        = _("Adjust Color Curves");
  im_tool_class->settings_name     = "curves";
  im_tool_class->load_dialog_title = _("Load Curves");
  im_tool_class->load_button_tip   = _("Load curves settings from file");
  im_tool_class->save_dialog_title = _("Save Curves");
  im_tool_class->save_button_tip   = _("Save curves settings to file");

165
  im_tool_class->get_operation     = gimp_curves_tool_get_operation;
166 167 168 169 170
  im_tool_class->map               = gimp_curves_tool_map;
  im_tool_class->dialog            = gimp_curves_tool_dialog;
  im_tool_class->reset             = gimp_curves_tool_reset;
  im_tool_class->settings_load     = gimp_curves_tool_settings_load;
  im_tool_class->settings_save     = gimp_curves_tool_settings_save;
Michael Natterer's avatar
Michael Natterer committed
171 172 173
}

static void
174
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
175
{
176 177
  GimpImageMapTool *im_tool = GIMP_IMAGE_MAP_TOOL (tool);
  gint              i;
178

179
  tool->lut = gimp_lut_new ();
180

181 182
  for (i = 0; i < G_N_ELEMENTS (tool->col_value); i++)
    tool->col_value[i] = -1;
183 184 185

  im_tool->apply_func = (GimpImageMapApplyFunc) gimp_lut_process;
  im_tool->apply_data = tool->lut;
Michael Natterer's avatar
Michael Natterer committed
186 187 188
}

static void
189
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
190
{
191
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (object);
192

193 194
  gimp_lut_free (tool->lut);

195
  if (tool->hist)
196
    {
197 198
      gimp_histogram_free (tool->hist);
      tool->hist = NULL;
199
    }
200

201
  G_OBJECT_CLASS (parent_class)->finalize (object);
Michael Natterer's avatar
Michael Natterer committed
202 203
}

204
static gboolean
205 206 207
gimp_curves_tool_initialize (GimpTool     *tool,
                             GimpDisplay  *display,
                             GError      **error)
Michael Natterer's avatar
Michael Natterer committed
208
{
209 210
  GimpCurvesTool *c_tool   = GIMP_CURVES_TOOL (tool);
  GimpDrawable   *drawable = gimp_image_get_active_drawable (display->image);
211 212

  if (! drawable)
213
    return FALSE;
214

215
  if (gimp_drawable_is_indexed (drawable))
Michael Natterer's avatar
Michael Natterer committed
216
    {
217
      g_set_error (error, 0, 0,
218
                   _("Curves does not operate on indexed layers."));
219
      return FALSE;
220
    }
Michael Natterer's avatar
Michael Natterer committed
221

222
  gimp_config_reset (GIMP_CONFIG (c_tool->config));
223

224
  if (! c_tool->hist)
225
    c_tool->hist = gimp_histogram_new ();
226

227 228
  c_tool->color = gimp_drawable_is_rgb (drawable);
  c_tool->alpha = gimp_drawable_has_alpha (drawable);
229

230
  GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error);
231

232 233
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
234
                          GIMP_COLOR_TOOL_GET_OPTIONS (tool));
235

236 237
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                      curves_menu_sensitivity, c_tool, NULL);
238

239
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
240
                                 c_tool->config->channel);
Michael Natterer's avatar
Michael Natterer committed
241

242
  gimp_drawable_calculate_histogram (drawable, c_tool->hist);
243 244
  gimp_histogram_view_set_background (GIMP_HISTOGRAM_VIEW (c_tool->graph),
                                      c_tool->hist);
245
  gimp_curve_view_set_curve (GIMP_CURVE_VIEW (c_tool->graph),
246
                             c_tool->config->curve[c_tool->config->channel]);
247

248
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
249 250 251
}

static void
252 253 254 255 256 257
gimp_curves_tool_button_release (GimpTool              *tool,
                                 GimpCoords            *coords,
                                 guint32                time,
                                 GdkModifierType        state,
                                 GimpButtonReleaseType  release_type,
                                 GimpDisplay           *display)
Michael Natterer's avatar
Michael Natterer committed
258
{
259 260
  GimpCurvesTool   *c_tool = GIMP_CURVES_TOOL (tool);
  GimpCurvesConfig *config = c_tool->config;
Michael Natterer's avatar
Michael Natterer committed
261

262
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
263
    {
264
      GimpCurve *curve = config->curve[config->channel];
265
      gint       closest;
266

267
      closest =
268
        gimp_curve_get_closest_point (curve,
269
                                      c_tool->col_value[config->channel]);
270

271
      gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
272
                                    closest);
273

274
      gimp_curve_set_point (curve,
275
                            closest,
276 277
                            c_tool->col_value[config->channel],
                            curve->curve[c_tool->col_value[config->channel]]);
Michael Natterer's avatar
Michael Natterer committed
278
    }
279
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
280
    {
281
      gint i;
282

283 284
      for (i = 0; i < 5; i++)
        {
285
          GimpCurve *curve = config->curve[i];
286
          gint       closest;
287

288
          closest =
289 290 291
            gimp_curve_get_closest_point (curve,
                                          c_tool->col_value[i]);

292
          gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
293
                                        closest);
294

295
          gimp_curve_set_point (curve,
296
                                closest,
297 298
                                c_tool->col_value[i],
                                curve->curve[c_tool->col_value[i]]);
299
        }
Michael Natterer's avatar
Michael Natterer committed
300 301
    }

302
  /*  chain up to halt the tool */
303 304
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Michael Natterer's avatar
Michael Natterer committed
305 306
}

307 308 309
gboolean
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
310
                            GimpDisplay *display)
311
{
312 313 314
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);

  return gtk_widget_event (c_tool->graph, (GdkEvent *) kevent);
315 316 317 318 319 320
}

static void
gimp_curves_tool_oper_update (GimpTool        *tool,
                              GimpCoords      *coords,
                              GdkModifierType  state,
321
                              gboolean         proximity,
322
                              GimpDisplay     *display)
323 324 325 326
{
  GimpColorPickMode  mode   = GIMP_COLOR_PICK_MODE_NONE;
  const gchar       *status = NULL;

327
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
328
                                               display);
329

330
  gimp_tool_pop_status (tool, display);
331 332 333 334

  if (state & GDK_SHIFT_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
335
      status = _("Click to add a control point");
336 337 338 339
    }
  else if (state & GDK_CONTROL_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
340
      status = _("Click to add control points to all channels");
341 342 343 344
    }

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

345
  if (status && proximity)
346
    gimp_tool_push_status (tool, display, status);
347 348
}

Michael Natterer's avatar
Michael Natterer committed
349
static void
350 351
gimp_curves_tool_color_picked (GimpColorTool      *color_tool,
                               GimpColorPickState  pick_state,
352 353 354
                               GimpImageType       sample_type,
                               GimpRGB            *color,
                               gint                color_index)
Michael Natterer's avatar
Michael Natterer committed
355
{
356 357 358
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (color_tool);
  GimpDrawable   *drawable;
  guchar          r, g, b, a;
Michael Natterer's avatar
Michael Natterer committed
359

360
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
361

362
  gimp_rgba_get_uchar (color, &r, &g, &b, &a);
363

364 365 366
  tool->col_value[GIMP_HISTOGRAM_RED]   = r;
  tool->col_value[GIMP_HISTOGRAM_GREEN] = g;
  tool->col_value[GIMP_HISTOGRAM_BLUE]  = b;
367

368
  if (gimp_drawable_has_alpha (drawable))
369
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = a;
370

371
  if (gimp_drawable_is_indexed (drawable))
372
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = color_index;
373

374
  tool->col_value[GIMP_HISTOGRAM_VALUE] = MAX (MAX (r, g), b);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
375

376
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
377
                            tool->col_value[tool->config->channel]);
378 379 380
}

static GeglNode *
381 382
gimp_curves_tool_get_operation (GimpImageMapTool  *image_map_tool,
                                GObject          **config)
383 384 385 386 387 388 389 390 391 392
{
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  GeglNode       *node;

  node = g_object_new (GEGL_TYPE_NODE,
                       "operation", "gimp-curves",
                       NULL);

  tool->config = g_object_new (GIMP_TYPE_CURVES_CONFIG, NULL);

393 394 395 396 397
  *config = G_OBJECT (tool->config);

  g_signal_connect_object (tool->config, "notify",
                           G_CALLBACK (gimp_curves_tool_config_notify),
                           tool, 0);
398 399 400 401 402 403

  gegl_node_set (node,
                 "config", tool->config,
                 NULL);

  return node;
404
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
405

406 407
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
408
{
409
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
410 411
  Curves          curves;

412
  gimp_curves_config_to_cruft (tool->config, &curves, tool->color);
413

414
  gimp_lut_setup (tool->lut,
415
                  (GimpLutFunc) curves_lut_func,
416
                  &curves,
417
                  gimp_drawable_bytes (image_map_tool->drawable));
Elliot Lee's avatar
Elliot Lee committed
418 419
}

420

421 422 423
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
424

425 426
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
427
{
428 429
  GimpCurvesTool  *tool         = GIMP_CURVES_TOOL (image_map_tool);
  GimpToolOptions *tool_options = GIMP_TOOL_GET_OPTIONS (image_map_tool);
430
  GtkListStore    *store;
431
  GtkWidget       *vbox;
432 433 434 435 436
  GtkWidget       *vbox2;
  GtkWidget       *hbox;
  GtkWidget       *hbox2;
  GtkWidget       *label;
  GtkWidget       *bbox;
437 438 439 440
  GtkWidget       *frame;
  GtkWidget       *menu;
  GtkWidget       *table;
  GtkWidget       *button;
441
  GtkWidget       *bar;
442
  gint             padding;
443

444
  vbox = image_map_tool->main_vbox;
Elliot Lee's avatar
Elliot Lee committed
445

446
  /*  The option menu for selecting channels  */
447
  hbox = gtk_hbox_new (FALSE, 6);
448 449 450
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

451
  label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
452 453 454
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

455 456 457
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
458
  menu = gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
459 460
  g_object_unref (store);

461 462 463 464 465
  g_signal_connect (menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);
  gimp_enum_combo_box_set_stock_prefix (GIMP_ENUM_COMBO_BOX (menu),
                                        "gimp-channel");
466 467 468
  gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);

469
  tool->channel_menu = menu;
470

471 472
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), menu);

473
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
474
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
475
  gtk_widget_show (button);
476

477
  g_signal_connect (button, "clicked",
478
                    G_CALLBACK (curves_channel_reset_callback),
479
                    tool);
Elliot Lee's avatar
Elliot Lee committed
480

481 482 483 484 485
  menu = gimp_prop_enum_stock_box_new (G_OBJECT (tool_options),
                                       "histogram-scale", "gimp-histogram",
                                       0, 0);
  gtk_box_pack_end (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);
486

487
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
488
  table = gtk_table_new (2, 2, FALSE);
489 490
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
491 492 493 494 495
  gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 0);

  /*  The left color bar  */
  vbox2 = gtk_vbox_new (FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), vbox2, 0, 1, 0, 1,
496
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
497
  gtk_widget_show (vbox2);
Elliot Lee's avatar
Elliot Lee committed
498 499

  frame = gtk_frame_new (NULL);
500
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
501
  gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
502 503
  gtk_widget_show (frame);

504 505 506 507
  tool->yrange = gimp_color_bar_new (GTK_ORIENTATION_VERTICAL);
  gtk_widget_set_size_request (tool->yrange, BAR_SIZE, -1);
  gtk_container_add (GTK_CONTAINER (frame), tool->yrange);
  gtk_widget_show (tool->yrange);
508

Elliot Lee's avatar
Elliot Lee committed
509 510
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
511
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
512
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
513
                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
514
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
515

516
  tool->graph = gimp_curve_view_new ();
517 518 519 520
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
521 522 523
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
524 525
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
526

527
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
528 529 530 531 532
                                       GIMP_HISTOGRAM_VIEW (tool->graph));

  /*  The bottom color bar  */
  hbox2 = gtk_hbox_new (FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), hbox2, 1, 2, 1, 2,
533
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
534
  gtk_widget_show (hbox2);
535

Elliot Lee's avatar
Elliot Lee committed
536
  frame = gtk_frame_new (NULL);
537
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
538
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
539 540
  gtk_widget_show (frame);

541 542 543 544
  vbox2 = gtk_vbox_new (TRUE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox2);
  gtk_widget_show (vbox2);

545 546 547 548
  tool->xrange = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_widget_set_size_request (tool->xrange, -1, BAR_SIZE / 2);
  gtk_box_pack_start (GTK_BOX (vbox2), tool->xrange, TRUE, TRUE, 0);
  gtk_widget_show (tool->xrange);
549

550 551 552 553
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_box_pack_start (GTK_BOX (vbox2), bar, TRUE, TRUE, 0);
  gtk_widget_show (bar);

554
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
555

556

557 558 559
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
560

561
  /*  Horizontal button box for load / save */
562
  frame = gimp_frame_new (_("All Channels"));
563
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
564 565
  gtk_widget_show (frame);

566 567
  bbox = gtk_hbutton_box_new ();
  gtk_box_set_spacing (GTK_BOX (bbox), 4);
568
  gtk_container_add (GTK_CONTAINER (frame), bbox);
569
  gtk_widget_show (bbox);
570

571 572 573
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->load_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->load_button);
574

575 576 577
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->save_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->save_button);
578 579

  /*  The radio box for selecting the curve type  */
580
  frame = gimp_frame_new (_("Curve Type"));
581 582 583 584
  gtk_box_pack_end (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  hbox = gimp_enum_stock_box_new (GIMP_TYPE_CURVE_TYPE,
585 586 587 588
                                  "gimp-curve", GTK_ICON_SIZE_MENU,
                                  G_CALLBACK (curves_curve_type_callback),
                                  tool,
                                  &tool->curve_type);
589

590
  gtk_widget_style_get (bbox, "child-internal-pad-x", &padding, NULL);
591 592

  gimp_enum_stock_box_set_child_padding (hbox, padding, -1);
593 594 595 596 597

  gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);

  gtk_container_add (GTK_CONTAINER (frame), hbox);
  gtk_widget_show (hbox);
Elliot Lee's avatar
Elliot Lee committed
598 599
}

600
static void
601
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
602
{
603 604
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (image_map_tool);
  GimpHistogramChannel  channel;
605

606 607 608 609 610 611
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
      gimp_curve_reset (tool->config->curve[channel], FALSE);
    }
612 613
}

614
static gboolean
615 616 617
gimp_curves_tool_settings_load (GimpImageMapTool  *image_map_tool,
                                gpointer           fp,
                                GError           **error)
618
{
619
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
620

621
  return gimp_curves_config_load_cruft (tool->config, fp, error);
622 623 624
}

static gboolean
625 626
gimp_curves_tool_settings_save (GimpImageMapTool *image_map_tool,
                                gpointer          fp)
627
{
628
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
629

630
  return gimp_curves_config_save_cruft (tool->config, fp);
631
}
632

Elliot Lee's avatar
Elliot Lee committed
633
static void
634 635 636
gimp_curves_tool_config_notify (GObject        *object,
                                GParamSpec     *pspec,
                                GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
637
{
638
  GimpCurvesConfig *config = GIMP_CURVES_CONFIG (object);
639

640
  if (! tool->xrange)
641 642
    return;

643
  if (! strcmp (pspec->name, "channel"))
644
    {
645
      switch (config->channel)
646 647 648
        {
        case GIMP_HISTOGRAM_VALUE:
        case GIMP_HISTOGRAM_ALPHA:
649
        case GIMP_HISTOGRAM_RGB:
650
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
651 652 653
                                      config->curve[config->channel]->curve,
                                      config->curve[config->channel]->curve,
                                      config->curve[config->channel]->curve);
654
          break;
655

656 657 658
        case GIMP_HISTOGRAM_RED:
        case GIMP_HISTOGRAM_GREEN:
        case GIMP_HISTOGRAM_BLUE:
659
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
660 661 662
                                      config->curve[GIMP_HISTOGRAM_RED]->curve,
                                      config->curve[GIMP_HISTOGRAM_GREEN]->curve,
                                      config->curve[GIMP_HISTOGRAM_BLUE]->curve);
663 664
          break;
        }
665

666
      gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (tool->graph),
667
                                       config->channel);
668
      gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
669
                                tool->col_value[config->channel]);
670

671 672
      gimp_color_bar_set_channel (GIMP_COLOR_BAR (tool->yrange),
                                  config->channel);
Elliot Lee's avatar
Elliot Lee committed
673

674
      gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph),
675
                                 config->curve[config->channel]);
676 677 678
    }
  else if (! strcmp (pspec->name, "curve"))
    {
679
      gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (tool->curve_type),
680
                                       config->curve[config->channel]->curve_type);
681 682 683 684
    }

  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));
}
685

686 687 688 689 690 691 692 693 694 695 696 697 698
static void
curves_channel_callback (GtkWidget      *widget,
                         GimpCurvesTool *tool)
{
  GimpCurvesConfig *config = tool->config;
  gint              value;

  if (gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &value) &&
      config->channel != value)
    {
      g_object_set (config,
                    "channel", value,
                    NULL);
699
    }
Elliot Lee's avatar
Elliot Lee committed
700 701 702
}

static void
703
curves_channel_reset_callback (GtkWidget      *widget,
704
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
705
{
706
  gimp_curve_reset (tool->config->curve[tool->config->channel], FALSE);
Elliot Lee's avatar
Elliot Lee committed
707 708
}

709
static gboolean
710 711
curves_menu_sensitivity (gint      value,
                         gpointer  data)
712
{
713 714
  GimpCurvesTool       *tool    = GIMP_CURVES_TOOL (data);
  GimpHistogramChannel  channel = value;
Sven Neumann's avatar
Sven Neumann committed
715

716 717 718 719
  switch (channel)
    {
    case GIMP_HISTOGRAM_VALUE:
      return TRUE;
720

721 722 723
    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
724
      return tool->color;
725

726
    case GIMP_HISTOGRAM_ALPHA:
Sven Neumann's avatar
Sven Neumann committed
727
      return tool->alpha;
728 729 730

    case GIMP_HISTOGRAM_RGB:
      return FALSE;
731
    }
732

733 734 735
  return FALSE;
}

Elliot Lee's avatar
Elliot Lee committed
736
static void
737
curves_curve_type_callback (GtkWidget      *widget,
738
                            GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
739
{
740 741
  GimpCurvesConfig *config = tool->config;
  GimpCurveType     curve_type;
Elliot Lee's avatar
Elliot Lee committed
742

743
  gimp_radio_button_update (widget, &curve_type);
Elliot Lee's avatar
Elliot Lee committed
744

745
  if (config->curve[config->channel]->curve_type != curve_type)
Elliot Lee's avatar
Elliot Lee committed
746
    {
747
      gimp_curve_set_curve_type (config->curve[config->channel], curve_type);
748
    }
Elliot Lee's avatar
Elliot Lee committed
749
}