gimpcurvestool.c 25.6 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 "libgimpwidgets/gimpwidgets.h"
26

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

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

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

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

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

46 47
#include "display/gimpdisplay.h"

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

51
#include "gimp-intl.h"
52

53

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


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

61 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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);
static GeglNode * gimp_curves_tool_get_operation  (GimpImageMapTool     *image_map_tool);
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);

static void       curves_curve_callback           (GimpCurve            *curve,
                                                   GimpCurvesTool       *tool);
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);
108

Michael Natterer's avatar
Michael Natterer committed
109

110
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
111 112

#define parent_class gimp_curves_tool_parent_class
113

Michael Natterer's avatar
Michael Natterer committed
114

115
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
116 117

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

134 135 136

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
137 138 139
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  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");

161
  im_tool_class->get_operation     = gimp_curves_tool_get_operation;
162 163 164 165 166
  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
167 168 169
}

static void
170
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
171
{
172 173
  GimpImageMapTool *im_tool = GIMP_IMAGE_MAP_TOOL (tool);
  gint              i;
174

175
  tool->lut = gimp_lut_new ();
176

177 178
  for (i = 0; i < G_N_ELEMENTS (tool->col_value); i++)
    tool->col_value[i] = -1;
179 180 181

  im_tool->apply_func = (GimpImageMapApplyFunc) gimp_lut_process;
  im_tool->apply_data = tool->lut;
Michael Natterer's avatar
Michael Natterer committed
182 183 184
}

static void
185
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
186
{
187
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (object);
188

189 190 191 192 193
  if (tool->config)
    {
      g_object_unref (tool->config);
      tool->config = NULL;
    }
194

195 196
  gimp_lut_free (tool->lut);

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

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

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

  if (! drawable)
216
    return FALSE;
217

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

225 226
  for (i = 0; i < G_N_ELEMENTS (c_tool->config->curve); i++)
    gimp_curve_reset (c_tool->config->curve[i], TRUE);
227

228
  if (! c_tool->hist)
229
    c_tool->hist = gimp_histogram_new ();
230

231 232
  c_tool->color = gimp_drawable_is_rgb (drawable);
  c_tool->alpha = gimp_drawable_has_alpha (drawable);
233

234
  GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error);
235

236 237
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
238
                          GIMP_COLOR_TOOL_GET_OPTIONS (tool));
239

240 241
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                      curves_menu_sensitivity, c_tool, NULL);
242

243
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
244
                                 c_tool->config->channel);
Michael Natterer's avatar
Michael Natterer committed
245

246
  gimp_drawable_calculate_histogram (drawable, c_tool->hist);
247 248
  gimp_histogram_view_set_background (GIMP_HISTOGRAM_VIEW (c_tool->graph),
                                      c_tool->hist);
249
  gimp_curve_view_set_curve (GIMP_CURVE_VIEW (c_tool->graph),
250
                             c_tool->config->curve[c_tool->config->channel]);
251

252
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
253 254 255
}

static void
256 257 258 259 260 261
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
262
{
263 264
  GimpCurvesTool   *c_tool = GIMP_CURVES_TOOL (tool);
  GimpCurvesConfig *config = c_tool->config;
Michael Natterer's avatar
Michael Natterer committed
265

266
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
267
    {
268
      GimpCurve *curve = config->curve[config->channel];
269
      gint       closest;
270

271
      closest =
272
        gimp_curve_get_closest_point (curve,
273
                                      c_tool->col_value[config->channel]);
274

275
      gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
276
                                    closest);
277

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

287 288
      for (i = 0; i < 5; i++)
        {
289
          GimpCurve *curve = config->curve[i];
290
          gint       closest;
291

292
          closest =
293 294 295
            gimp_curve_get_closest_point (curve,
                                          c_tool->col_value[i]);

296
          gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
297
                                        closest);
298

299
          gimp_curve_set_point (curve,
300
                                closest,
301 302
                                c_tool->col_value[i],
                                curve->curve[c_tool->col_value[i]]);
303
        }
Michael Natterer's avatar
Michael Natterer committed
304 305
    }

306
  /*  chain up to halt the tool */
307 308
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Michael Natterer's avatar
Michael Natterer committed
309 310
}

311 312 313
gboolean
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
314
                            GimpDisplay *display)
315
{
316 317 318
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);

  return gtk_widget_event (c_tool->graph, (GdkEvent *) kevent);
319 320 321 322 323 324
}

static void
gimp_curves_tool_oper_update (GimpTool        *tool,
                              GimpCoords      *coords,
                              GdkModifierType  state,
325
                              gboolean         proximity,
326
                              GimpDisplay     *display)
327 328 329 330
{
  GimpColorPickMode  mode   = GIMP_COLOR_PICK_MODE_NONE;
  const gchar       *status = NULL;

331
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
332
                                               display);
333

334
  gimp_tool_pop_status (tool, display);
335 336 337 338

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

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

349
  if (status && proximity)
350
    gimp_tool_push_status (tool, display, status);
351 352
}

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

364
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
365

366
  gimp_rgba_get_uchar (color, &r, &g, &b, &a);
367

368 369 370
  tool->col_value[GIMP_HISTOGRAM_RED]   = r;
  tool->col_value[GIMP_HISTOGRAM_GREEN] = g;
  tool->col_value[GIMP_HISTOGRAM_BLUE]  = b;
371

372
  if (gimp_drawable_has_alpha (drawable))
373
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = a;
374

375
  if (gimp_drawable_is_indexed (drawable))
376
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = color_index;
377

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

380
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
381
                            tool->col_value[tool->config->channel]);
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
}

static GeglNode *
gimp_curves_tool_get_operation (GimpImageMapTool *image_map_tool)
{
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  GeglNode       *node;
  gint            i;

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

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

  for (i = 0; i < G_N_ELEMENTS (tool->config->curve); i++)
    {
      g_signal_connect_object (tool->config->curve[i], "dirty",
                               G_CALLBACK (curves_curve_callback),
                               tool, 0);
    }

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

  return node;
409
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
410

411 412
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
413
{
414
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
415 416
  Curves          curves;

417
  gimp_curves_config_to_cruft (tool->config, &curves, tool->color);
418

419
  gimp_lut_setup (tool->lut,
420
                  (GimpLutFunc) curves_lut_func,
421
                  &curves,
422
                  gimp_drawable_bytes (image_map_tool->drawable));
Elliot Lee's avatar
Elliot Lee committed
423 424
}

425

426 427 428
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
429

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

449
  vbox = image_map_tool->main_vbox;
Elliot Lee's avatar
Elliot Lee committed
450

451
  /*  The option menu for selecting channels  */
452
  hbox = gtk_hbox_new (FALSE, 6);
453 454 455
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

456
  label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
457 458 459
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

460 461 462
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
463
  menu = gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
464 465
  g_object_unref (store);

466 467 468 469 470
  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");
471 472 473
  gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);

474
  tool->channel_menu = menu;
475

476 477
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), menu);

478
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
479
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
480
  gtk_widget_show (button);
481

482
  g_signal_connect (button, "clicked",
483
                    G_CALLBACK (curves_channel_reset_callback),
484
                    tool);
Elliot Lee's avatar
Elliot Lee committed
485

486 487 488 489 490
  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);
491

492
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
493
  table = gtk_table_new (2, 2, FALSE);
494 495
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
496 497 498 499 500
  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,
501
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
502
  gtk_widget_show (vbox2);
Elliot Lee's avatar
Elliot Lee committed
503 504

  frame = gtk_frame_new (NULL);
505
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
506
  gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
507 508
  gtk_widget_show (frame);

509 510 511 512
  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);
513

Elliot Lee's avatar
Elliot Lee committed
514 515
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
516
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
517
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
518
                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
519
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
520

521
  tool->graph = gimp_curve_view_new ();
522 523 524 525
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
526 527 528
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
529 530
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
531

532
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
533 534 535 536 537
                                       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,
538
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
539
  gtk_widget_show (hbox2);
540

Elliot Lee's avatar
Elliot Lee committed
541
  frame = gtk_frame_new (NULL);
542
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
543
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
544 545
  gtk_widget_show (frame);

546 547 548 549
  vbox2 = gtk_vbox_new (TRUE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox2);
  gtk_widget_show (vbox2);

550 551 552 553
  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);
554

555 556 557 558
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_box_pack_start (GTK_BOX (vbox2), bar, TRUE, TRUE, 0);
  gtk_widget_show (bar);

559
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
560

561

562 563 564
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
565

566
  /*  Horizontal button box for load / save */
567
  frame = gimp_frame_new (_("All Channels"));
568
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
569 570
  gtk_widget_show (frame);

571 572
  bbox = gtk_hbutton_box_new ();
  gtk_box_set_spacing (GTK_BOX (bbox), 4);
573
  gtk_container_add (GTK_CONTAINER (frame), bbox);
574
  gtk_widget_show (bbox);
575

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

580 581 582
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->save_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->save_button);
583 584

  /*  The radio box for selecting the curve type  */
585
  frame = gimp_frame_new (_("Curve Type"));
586 587 588 589
  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,
590 591 592 593
                                  "gimp-curve", GTK_ICON_SIZE_MENU,
                                  G_CALLBACK (curves_curve_type_callback),
                                  tool,
                                  &tool->curve_type);
594

595
  gtk_widget_style_get (bbox, "child-internal-pad-x", &padding, NULL);
596 597

  gimp_enum_stock_box_set_child_padding (hbox, padding, -1);
598 599 600 601 602

  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
603 604
}

605
static void
606
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
607
{
608 609
  GimpCurvesTool       *tool    = GIMP_CURVES_TOOL (image_map_tool);
  GimpHistogramChannel  channel = tool->config->channel;
610

611
  gimp_curves_config_reset (tool->config);
612 613 614
  g_object_set (tool->config,
                "channel", channel,
                NULL);
615 616
}

617
static gboolean
618 619 620
gimp_curves_tool_settings_load (GimpImageMapTool  *image_map_tool,
                                gpointer           fp,
                                GError           **error)
621
{
622
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
623

624
  if (gimp_curves_config_load_cruft (tool->config, fp, error))
625
    {
626 627
      gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (tool->curve_type),
                                       GIMP_CURVE_SMOOTH);
628

629
      return TRUE;
630
    }
631

632
  return FALSE;
633 634 635
}

static gboolean
636 637
gimp_curves_tool_settings_save (GimpImageMapTool *image_map_tool,
                                gpointer          fp)
638
{
639
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
640

641
  return gimp_curves_config_save_cruft (tool->config, fp);
642
}
643

Elliot Lee's avatar
Elliot Lee committed
644
static void
645 646
curves_curve_callback (GimpCurve      *curve,
                       GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
647
{
648 649 650
  GimpCurvesConfig *config = tool->config;

  if (curve != config->curve[config->channel])
651 652 653
    return;

  if (tool->xrange)
654
    {
655
      switch (config->channel)
656 657 658
        {
        case GIMP_HISTOGRAM_VALUE:
        case GIMP_HISTOGRAM_ALPHA:
659
        case GIMP_HISTOGRAM_RGB:
660
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
661 662 663
                                      config->curve[config->channel]->curve,
                                      config->curve[config->channel]->curve,
                                      config->curve[config->channel]->curve);
664
          break;
665

666 667 668
        case GIMP_HISTOGRAM_RED:
        case GIMP_HISTOGRAM_GREEN:
        case GIMP_HISTOGRAM_BLUE:
669
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
670 671 672
                                      config->curve[GIMP_HISTOGRAM_RED]->curve,
                                      config->curve[GIMP_HISTOGRAM_GREEN]->curve,
                                      config->curve[GIMP_HISTOGRAM_BLUE]->curve);
673 674
          break;
        }
Elliot Lee's avatar
Elliot Lee committed
675
    }
676 677 678

  if (GIMP_IMAGE_MAP_TOOL (tool)->drawable)
    gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));
Elliot Lee's avatar
Elliot Lee committed
679 680 681
}

static void
682
curves_channel_callback (GtkWidget      *widget,
683
                         GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
684
{
685
  gint value;
686

687 688
  if (gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &value))
    {
689
      GimpCurvesConfig *config = tool->config;
690

691 692
      g_object_set (config,
                    "channel", value,
693 694
                    NULL);

695
      gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (tool->graph),
696
                                       config->channel);
697
      gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
698
                                tool->col_value[config->channel]);
699

700 701
      gimp_color_bar_set_channel (GIMP_COLOR_BAR (tool->yrange),
                                  config->channel);
Elliot Lee's avatar
Elliot Lee committed
702

703
      gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph),
704
                                 config->curve[config->channel]);
705

706
      gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (tool->curve_type),
707
                                       config->curve[config->channel]->curve_type);
708

709
      curves_curve_callback (config->curve[config->channel], tool);
710
    }
Elliot Lee's avatar
Elliot Lee committed
711 712 713
}

static void
714
curves_channel_reset_callback (GtkWidget      *widget,
715
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
716
{
717
  gimp_curves_config_reset_channel (tool->config, tool->config->channel);
Elliot Lee's avatar
Elliot Lee committed
718 719
}

720
static gboolean
721 722
curves_menu_sensitivity (gint      value,
                         gpointer  data)
723
{
724 725
  GimpCurvesTool       *tool    = GIMP_CURVES_TOOL (data);
  GimpHistogramChannel  channel = value;
Sven Neumann's avatar
Sven Neumann committed
726

727 728 729 730
  switch (channel)
    {
    case GIMP_HISTOGRAM_VALUE:
      return TRUE;
731

732 733 734
    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
735
      return tool->color;
736

737
    case GIMP_HISTOGRAM_ALPHA:
Sven Neumann's avatar
Sven Neumann committed
738
      return tool->alpha;
739 740 741

    case GIMP_HISTOGRAM_RGB:
      return FALSE;
742
    }
743

744 745 746
  return FALSE;
}

Elliot Lee's avatar
Elliot Lee committed
747
static void
748
curves_curve_type_callback (GtkWidget      *widget,
749
                            GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
750
{
751 752
  GimpCurvesConfig *config = tool->config;
  GimpCurveType     curve_type;
Elliot Lee's avatar
Elliot Lee committed
753

754
  gimp_radio_button_update (widget, &curve_type);
Elliot Lee's avatar
Elliot Lee committed
755

756
  if (config->curve[config->channel]->curve_type != curve_type)
Elliot Lee's avatar
Elliot Lee committed
757
    {
758
      gimp_curve_set_curve_type (config->curve[config->channel], curve_type);
759
    }
Elliot Lee's avatar
Elliot Lee committed
760
}