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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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);
109

Michael Natterer's avatar
Michael Natterer committed
110

111
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
112 113

#define parent_class gimp_curves_tool_parent_class
114

Michael Natterer's avatar
Michael Natterer committed
115

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

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

135 136 137

/*  private functions  */

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

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

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

176
  tool->lut = gimp_lut_new ();
177

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

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

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

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

196 197
  gimp_lut_free (tool->lut);

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

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

207
static gboolean
208 209 210
gimp_curves_tool_initialize (GimpTool     *tool,
                             GimpDisplay  *display,
                             GError      **error)
Michael Natterer's avatar
Michael Natterer committed
211
{
212 213
  GimpCurvesTool *c_tool   = GIMP_CURVES_TOOL (tool);
  GimpDrawable   *drawable = gimp_image_get_active_drawable (display->image);
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
  gimp_config_reset (GIMP_CONFIG (c_tool->config));
226

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

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

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

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

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

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

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

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

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

379
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
380
                            tool->col_value[tool->config->channel]);
381 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
}

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;
408
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
409

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

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

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

424

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

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

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

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

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

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

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

473
  tool->channel_menu = menu;
474

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

560

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

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

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

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

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

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

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

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

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

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

610 611 612 613 614 615
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
      gimp_curve_reset (tool->config->curve[channel], FALSE);
    }
616 617
}

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

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

630
      return TRUE;
631
    }
632

633
  return FALSE;
634 635 636
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

static void
715
curves_channel_reset_callback (GtkWidget      *widget,
716
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
717
{
718
  gimp_curve_reset (tool->config->curve[tool->config->channel], FALSE);
Elliot Lee's avatar
Elliot Lee committed
719 720
}

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

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

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

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

    case GIMP_HISTOGRAM_RGB:
      return FALSE;
743
    }
744

745 746 747
  return FALSE;
}

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

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

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