gimpcurvestool.c 29.1 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Elliot Lee's avatar
Elliot Lee committed
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
Elliot Lee's avatar
Elliot Lee committed
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
Sven Neumann's avatar
Sven Neumann committed
17

18 19
#include "config.h"

20 21 22
#include <errno.h>

#include <glib/gstdio.h>
23
#include <gegl.h>
Sven Neumann's avatar
Sven Neumann committed
24 25
#include <gtk/gtk.h>

26
#include "libgimpbase/gimpbase.h"
27
#include "libgimpcolor/gimpcolor.h"
28
#include "libgimpconfig/gimpconfig.h"
29
#include "libgimpwidgets/gimpwidgets.h"
30

31
#include "tools-types.h"
Michael Natterer's avatar
Michael Natterer committed
32 33 34

#include "base/gimphistogram.h"

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

38
#include "core/gimp.h"
39
#include "core/gimpcurve.h"
Sven Neumann's avatar
Sven Neumann committed
40
#include "core/gimpcurve-map.h"
Michael Natterer's avatar
Michael Natterer committed
41
#include "core/gimpdrawable.h"
42
#include "core/gimpdrawable-histogram.h"
43
#include "core/gimperror.h"
Michael Natterer's avatar
Michael Natterer committed
44 45
#include "core/gimpimage.h"

46
#include "widgets/gimpcolorbar.h"
47
#include "widgets/gimphelp-ids.h"
48
#include "widgets/gimpcurveview.h"
49
#include "widgets/gimpwidgets-utils.h"
Michael Natterer's avatar
Michael Natterer committed
50

51 52
#include "display/gimpdisplay.h"

Michael Natterer's avatar
Michael Natterer committed
53
#include "gimpcurvestool.h"
54
#include "gimphistogramoptions.h"
Sven Neumann's avatar
Sven Neumann committed
55

56
#include "gimp-intl.h"
57

58

59 60 61
#define GRAPH_SIZE 256
#define BAR_SIZE    12
#define RADIUS       4
Elliot Lee's avatar
Elliot Lee committed
62 63


Michael Natterer's avatar
Michael Natterer committed
64 65
/*  local function prototypes  */

66 67 68 69
static gboolean   gimp_curves_tool_initialize     (GimpTool             *tool,
                                                   GimpDisplay          *display,
                                                   GError              **error);
static void       gimp_curves_tool_button_release (GimpTool             *tool,
70
                                                   const GimpCoords     *coords,
71 72 73 74 75 76 77 78
                                                   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,
79
                                                   const GimpCoords     *coords,
80 81 82 83 84 85 86
                                                   GdkModifierType       state,
                                                   gboolean              proximity,
                                                   GimpDisplay          *display);

static void       gimp_curves_tool_color_picked   (GimpColorTool        *color_tool,
                                                   GimpColorPickState    pick_state,
                                                   GimpImageType         sample_type,
87
                                                   const GimpRGB        *color,
88
                                                   gint                  color_index);
89 90
static GeglNode * gimp_curves_tool_get_operation  (GimpImageMapTool     *image_map_tool,
                                                   GObject             **config);
91 92
static void       gimp_curves_tool_dialog         (GimpImageMapTool     *image_map_tool);
static void       gimp_curves_tool_reset          (GimpImageMapTool     *image_map_tool);
93
static gboolean   gimp_curves_tool_settings_import(GimpImageMapTool     *image_map_tool,
94
                                                   const gchar          *filename,
95
                                                   GError              **error);
96
static gboolean   gimp_curves_tool_settings_export(GimpImageMapTool     *image_map_tool,
97 98
                                                   const gchar          *filename,
                                                   GError              **error);
99

100 101 102 103
static void       gimp_curves_tool_export_setup   (GimpSettingsBox      *settings_box,
                                                   GtkFileChooserDialog *dialog,
                                                   gboolean              export,
                                                   GimpCurvesTool       *tool);
104 105
static void       gimp_curves_tool_config_notify  (GObject              *object,
                                                   GParamSpec           *pspec,
106
                                                   GimpCurvesTool       *tool);
107

108 109 110 111 112 113 114 115 116 117
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);
118

Michael Natterer's avatar
Michael Natterer committed
119

120
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
121 122

#define parent_class gimp_curves_tool_parent_class
123

124 125 126 127 128 129 130 131 132
static GimpRGB channel_colors[GIMP_HISTOGRAM_RGB] =
{
  { 0.0, 0.0, 0.0, 1.0 },
  { 1.0, 0.0, 0.0, 1.0 },
  { 0.0, 1.0, 0.0, 1.0 },
  { 0.0, 0.0, 1.0, 1.0 },
  { 0.5, 0.5, 0.5, 1.0 }
};

Michael Natterer's avatar
Michael Natterer committed
133

134
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
135 136

void
Nate Summers's avatar
Nate Summers committed
137
gimp_curves_tool_register (GimpToolRegisterCallback  callback,
138
                           gpointer                  data)
Michael Natterer's avatar
Michael Natterer committed
139
{
Nate Summers's avatar
Nate Summers committed
140
  (* callback) (GIMP_TYPE_CURVES_TOOL,
141
                GIMP_TYPE_HISTOGRAM_OPTIONS,
142
                gimp_color_options_gui,
143
                0,
144
                "gimp-curves-tool",
145
                _("Curves"),
146
                _("Curves Tool: Adjust color curves"),
147
                N_("_Curves..."), NULL,
148
                NULL, GIMP_HELP_TOOL_CURVES,
Nate Summers's avatar
Nate Summers committed
149
                GIMP_STOCK_TOOL_CURVES,
150
                data);
Michael Natterer's avatar
Michael Natterer committed
151 152
}

153 154 155

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
156 157 158
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
159 160 161 162
  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);

163 164 165 166
  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;
167

168
  color_tool_class->picked           = gimp_curves_tool_color_picked;
169

170
  im_tool_class->dialog_desc         = _("Adjust Color Curves");
171 172 173
  im_tool_class->settings_name       = "curves";
  im_tool_class->import_dialog_title = _("Import Curves");
  im_tool_class->export_dialog_title = _("Export Curves");
174

175 176 177 178 179
  im_tool_class->get_operation       = gimp_curves_tool_get_operation;
  im_tool_class->dialog              = gimp_curves_tool_dialog;
  im_tool_class->reset               = gimp_curves_tool_reset;
  im_tool_class->settings_import     = gimp_curves_tool_settings_import;
  im_tool_class->settings_export     = gimp_curves_tool_settings_export;
Michael Natterer's avatar
Michael Natterer committed
180 181 182
}

static void
183
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
184
{
185
  gint i;
186

187 188
  for (i = 0; i < G_N_ELEMENTS (tool->picked_color); i++)
    tool->picked_color[i] = -1.0;
Michael Natterer's avatar
Michael Natterer committed
189 190
}

191
static gboolean
192 193 194
gimp_curves_tool_initialize (GimpTool     *tool,
                             GimpDisplay  *display,
                             GError      **error)
Michael Natterer's avatar
Michael Natterer committed
195
{
196
  GimpCurvesTool *c_tool   = GIMP_CURVES_TOOL (tool);
197 198
  GimpImage      *image    = gimp_display_get_image (display);
  GimpDrawable   *drawable = gimp_image_get_active_drawable (image);
199
  GimpHistogram  *histogram;
200 201

  if (! drawable)
202
    return FALSE;
203

204
  gimp_config_reset (GIMP_CONFIG (c_tool->config));
205

206 207 208 209
  if (! GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error))
    {
      return FALSE;
    }
210

211 212
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
213
                          GIMP_COLOR_TOOL_GET_OPTIONS (tool));
214

215
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
216
                                      curves_menu_sensitivity, drawable, NULL);
217

218 219
  histogram = gimp_histogram_new ();
  gimp_drawable_calculate_histogram (drawable, histogram);
220
  gimp_histogram_view_set_background (GIMP_HISTOGRAM_VIEW (c_tool->graph),
221 222
                                      histogram);
  gimp_histogram_unref (histogram);
223

224
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
225 226 227
}

static void
228
gimp_curves_tool_button_release (GimpTool              *tool,
229
                                 const GimpCoords      *coords,
230 231 232 233
                                 guint32                time,
                                 GdkModifierType        state,
                                 GimpButtonReleaseType  release_type,
                                 GimpDisplay           *display)
Michael Natterer's avatar
Michael Natterer committed
234
{
235 236
  GimpCurvesTool   *c_tool = GIMP_CURVES_TOOL (tool);
  GimpCurvesConfig *config = c_tool->config;
Michael Natterer's avatar
Michael Natterer committed
237

238
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
239
    {
240
      GimpCurve *curve = config->curve[config->channel];
241
      gdouble    value = c_tool->picked_color[config->channel];
242
      gint       closest;
243

244
      closest = gimp_curve_get_closest_point (curve, value);
245

246
      gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
247
                                    closest);
248

249
      gimp_curve_set_point (curve, closest,
250
                            value, gimp_curve_map_value (curve, value));
Michael Natterer's avatar
Michael Natterer committed
251
    }
252
  else if (state & gimp_get_toggle_behavior_mask ())
Michael Natterer's avatar
Michael Natterer committed
253
    {
254
      gint i;
255

256 257
      for (i = 0; i < 5; i++)
        {
258
          GimpCurve *curve = config->curve[i];
259
          gdouble    value = c_tool->picked_color[i];
260
          gint       closest;
261

262
          closest = gimp_curve_get_closest_point (curve, value);
263

264
          gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
265
                                        closest);
266

267
          gimp_curve_set_point (curve, closest,
268
                                value, gimp_curve_map_value (curve, value));
269
        }
Michael Natterer's avatar
Michael Natterer committed
270 271
    }

272
  /*  chain up to halt the tool */
273 274
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Michael Natterer's avatar
Michael Natterer committed
275 276
}

277
static gboolean
278 279
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
280
                            GimpDisplay *display)
281
{
282 283
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);

284 285 286 287
  if (gtk_widget_event (c_tool->graph, (GdkEvent *) kevent))
    return TRUE;

  return GIMP_TOOL_CLASS (parent_class)->key_press (tool, kevent, display);
288 289 290
}

static void
291 292 293 294 295
gimp_curves_tool_oper_update (GimpTool         *tool,
                              const GimpCoords *coords,
                              GdkModifierType   state,
                              gboolean          proximity,
                              GimpDisplay      *display)
296
{
297 298
  GimpColorPickMode  mode;
  const gchar       *status;
299

300
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
301
                                               display);
302

303
  gimp_tool_pop_status (tool, display);
304 305 306 307

  if (state & GDK_SHIFT_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
308
      status = _("Click to add a control point");
309
    }
310
  else if (state & gimp_get_toggle_behavior_mask ())
311 312
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
313
      status = _("Click to add control points to all channels");
314
    }
315 316 317 318 319
  else
    {
      mode   = GIMP_COLOR_PICK_MODE_NONE;
      status = _("Click to locate on curve (try Shift, Ctrl)");
    }
320 321 322

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

323
  if (proximity)
324
    gimp_tool_push_status (tool, display, "%s", status);
325 326
}

Michael Natterer's avatar
Michael Natterer committed
327
static void
328 329
gimp_curves_tool_color_picked (GimpColorTool      *color_tool,
                               GimpColorPickState  pick_state,
330
                               GimpImageType       sample_type,
331
                               const GimpRGB      *color,
332
                               gint                color_index)
Michael Natterer's avatar
Michael Natterer committed
333
{
334 335
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (color_tool);
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
336

337
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
338

339 340 341
  tool->picked_color[GIMP_HISTOGRAM_RED]   = color->r;
  tool->picked_color[GIMP_HISTOGRAM_GREEN] = color->g;
  tool->picked_color[GIMP_HISTOGRAM_BLUE]  = color->b;
342

343
  if (gimp_drawable_has_alpha (drawable))
344
    tool->picked_color[GIMP_HISTOGRAM_ALPHA] = color->a;
345

346 347
  tool->picked_color[GIMP_HISTOGRAM_VALUE] = MAX (MAX (color->r, color->g),
                                                  color->b);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
348

349
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
350
                            tool->picked_color[tool->config->channel]);
351 352 353
}

static GeglNode *
354 355
gimp_curves_tool_get_operation (GimpImageMapTool  *image_map_tool,
                                GObject          **config)
356 357 358 359 360
{
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  GeglNode       *node;

  node = g_object_new (GEGL_TYPE_NODE,
361
                       "operation", "gimp:curves",
362 363 364 365
                       NULL);

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

366 367 368 369 370
  *config = G_OBJECT (tool->config);

  g_signal_connect_object (tool->config, "notify",
                           G_CALLBACK (gimp_curves_tool_config_notify),
                           tool, 0);
371 372 373 374 375 376

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

  return node;
377
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
378

379

380 381 382
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
383

384 385
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
386
{
387 388 389 390
  GimpCurvesTool   *tool         = GIMP_CURVES_TOOL (image_map_tool);
  GimpToolOptions  *tool_options = GIMP_TOOL_GET_OPTIONS (image_map_tool);
  GimpCurvesConfig *config       = tool->config;
  GtkListStore     *store;
391 392
  GtkSizeGroup     *label_group;
  GtkWidget        *main_vbox;
393 394 395 396 397 398 399 400
  GtkWidget        *vbox;
  GtkWidget        *hbox;
  GtkWidget        *hbox2;
  GtkWidget        *label;
  GtkWidget        *frame;
  GtkWidget        *table;
  GtkWidget        *button;
  GtkWidget        *bar;
401
  GtkWidget        *combo;
402

403 404 405 406
  g_signal_connect (image_map_tool->settings_box, "file-dialog-setup",
                    G_CALLBACK (gimp_curves_tool_export_setup),
                    image_map_tool);

407 408
  main_vbox   = gimp_image_map_tool_dialog_get_vbox (image_map_tool);
  label_group = gimp_image_map_tool_dialog_get_label_group (image_map_tool);
Elliot Lee's avatar
Elliot Lee committed
409

410
  /*  The combo box for selecting channels  */
Michael Natterer's avatar
Michael Natterer committed
411
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
412
  gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
413 414
  gtk_widget_show (hbox);

415
  label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
416 417 418
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

419 420
  gtk_size_group_add_widget (label_group, label);

421 422 423
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
424 425
  tool->channel_menu =
    gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
426 427
  g_object_unref (store);

428 429 430
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                 config->channel);
  gimp_enum_combo_box_set_stock_prefix (GIMP_ENUM_COMBO_BOX (tool->channel_menu),
431
                                        "gimp-channel");
432 433
  gtk_box_pack_start (GTK_BOX (hbox), tool->channel_menu, FALSE, FALSE, 0);
  gtk_widget_show (tool->channel_menu);
434

435 436 437
  g_signal_connect (tool->channel_menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);
438

439
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->channel_menu);
440

441
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
442
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
443
  gtk_widget_show (button);
444

445
  g_signal_connect (button, "clicked",
446
                    G_CALLBACK (curves_channel_reset_callback),
447
                    tool);
Elliot Lee's avatar
Elliot Lee committed
448

449 450 451 452 453 454
  /*  The histogram scale radio buttons  */
  hbox2 = gimp_prop_enum_stock_box_new (G_OBJECT (tool_options),
                                        "histogram-scale", "gimp-histogram",
                                        0, 0);
  gtk_box_pack_end (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);
  gtk_widget_show (hbox2);
455

456
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
457
  table = gtk_table_new (2, 2, FALSE);
458 459
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
460
  gtk_box_pack_start (GTK_BOX (main_vbox), table, TRUE, TRUE, 0);
461 462

  /*  The left color bar  */
Michael Natterer's avatar
Michael Natterer committed
463
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
464
  gtk_table_attach (GTK_TABLE (table), vbox, 0, 1, 0, 1,
465
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
466
  gtk_widget_show (vbox);
Elliot Lee's avatar
Elliot Lee committed
467 468

  frame = gtk_frame_new (NULL);
469
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
470
  gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
471 472
  gtk_widget_show (frame);

473 474 475 476
  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);
477

Elliot Lee's avatar
Elliot Lee committed
478 479
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
480
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
481
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
482
                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
483
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
484

485
  tool->graph = gimp_curve_view_new ();
486 487
  gimp_curve_view_set_range_x (GIMP_CURVE_VIEW (tool->graph), 0, 255);
  gimp_curve_view_set_range_y (GIMP_CURVE_VIEW (tool->graph), 0, 255);
488 489 490 491
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
492 493 494
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
495
  gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph),
496 497
                             config->curve[config->channel],
                             &channel_colors[config->channel]);
498 499
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
500

501
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
502 503 504
                                       GIMP_HISTOGRAM_VIEW (tool->graph));

  /*  The bottom color bar  */
Michael Natterer's avatar
Michael Natterer committed
505
  hbox2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
506
  gtk_table_attach (GTK_TABLE (table), hbox2, 1, 2, 1, 2,
507
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
508
  gtk_widget_show (hbox2);
509

Elliot Lee's avatar
Elliot Lee committed
510
  frame = gtk_frame_new (NULL);
511
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
512
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
513 514
  gtk_widget_show (frame);

Michael Natterer's avatar
Michael Natterer committed
515 516
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_box_set_homogeneous (GTK_BOX (vbox), TRUE);
517 518
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);
519

520 521
  tool->xrange = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_widget_set_size_request (tool->xrange, -1, BAR_SIZE / 2);
522
  gtk_box_pack_start (GTK_BOX (vbox), tool->xrange, TRUE, TRUE, 0);
523
  gtk_widget_show (tool->xrange);
524

525
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
526
  gtk_box_pack_start (GTK_BOX (vbox), bar, TRUE, TRUE, 0);
527 528
  gtk_widget_show (bar);

529
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
530

Michael Natterer's avatar
Michael Natterer committed
531
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
532
  gtk_box_pack_end (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
533
  gtk_widget_show (hbox);
534

535 536 537
  label = gtk_label_new_with_mnemonic (_("Curve _type:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
538

539 540 541 542 543 544 545 546
  tool->curve_type = combo = gimp_enum_combo_box_new (GIMP_TYPE_CURVE_TYPE);
  gimp_enum_combo_box_set_stock_prefix (GIMP_ENUM_COMBO_BOX (combo),
                                        "gimp-curve");
  gimp_int_combo_box_connect (GIMP_INT_COMBO_BOX (combo), 0,
                              G_CALLBACK (curves_curve_type_callback),
                              tool);
  gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
  gtk_widget_show (combo);
Sven Neumann's avatar
Sven Neumann committed
547 548

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
Elliot Lee's avatar
Elliot Lee committed
549 550
}

551
static void
552
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
553
{
554
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (image_map_tool);
555
  GimpCurvesConfig     *default_config;
556
  GimpHistogramChannel  channel;
557

558 559
  default_config = GIMP_CURVES_CONFIG (image_map_tool->default_config);

560 561 562 563
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
      if (default_config)
        {
          GimpCurveType curve_type = tool->config->curve[channel]->curve_type;

          g_object_freeze_notify (G_OBJECT (tool->config->curve[channel]));

          gimp_config_copy (GIMP_CONFIG (default_config->curve[channel]),
                            GIMP_CONFIG (tool->config->curve[channel]),
                            0);

          g_object_set (tool->config->curve[channel],
                        "curve-type", curve_type,
                        NULL);

          g_object_thaw_notify (G_OBJECT (tool->config->curve[channel]));
        }
      else
        {
          gimp_curve_reset (tool->config->curve[channel], FALSE);
        }
584
    }
585 586
}

587
static gboolean
588 589 590
gimp_curves_tool_settings_import (GimpImageMapTool  *image_map_tool,
                                  const gchar       *filename,
                                  GError           **error)
591
{
592
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
593
  FILE           *file;
594
  gchar           header[64];
595 596 597 598 599 600 601 602 603 604 605 606

  file = g_fopen (filename, "rt");

  if (! file)
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not open '%s' for reading: %s"),
                   gimp_filename_to_utf8 (filename),
                   g_strerror (errno));
      return FALSE;
    }

607 608 609 610 611 612 613 614 615 616 617 618
  if (! fgets (header, sizeof (header), file))
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not read header from '%s': %s"),
                   gimp_filename_to_utf8 (filename),
                   g_strerror (errno));
      fclose (file);
      return FALSE;
    }

  if (g_str_has_prefix (header, "# GIMP Curves File\n"))
    {
619 620
      gboolean success;

621 622 623 624 625 626 627 628
      rewind (file);

      success = gimp_curves_config_load_cruft (tool->config, file, error);

      fclose (file);

      return success;
    }
629 630

  fclose (file);
631

632 633 634
  return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_import (image_map_tool,
                                                                    filename,
                                                                    error);
635 636 637
}

static gboolean
638 639 640
gimp_curves_tool_settings_export (GimpImageMapTool  *image_map_tool,
                                  const gchar       *filename,
                                  GError           **error)
641
{
642
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
643

644
  if (tool->export_old_format)
645
    {
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
      FILE     *file;
      gboolean  success;

      file = g_fopen (filename, "wt");

      if (! file)
        {
          g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                       _("Could not open '%s' for writing: %s"),
                       gimp_filename_to_utf8 (filename),
                       g_strerror (errno));
          return FALSE;
        }

      success = gimp_curves_config_save_cruft (tool->config, file, error);

      fclose (file);

      return success;
665 666
    }

667 668 669 670 671 672 673 674 675 676 677 678
  return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_export (image_map_tool,
                                                                    filename,
                                                                    error);
}

static void
gimp_curves_tool_export_setup (GimpSettingsBox      *settings_box,
                               GtkFileChooserDialog *dialog,
                               gboolean              export,
                               GimpCurvesTool       *tool)
{
  GtkWidget *button;
679

680 681 682 683 684 685 686 687
  if (! export)
    return;

  button = gtk_check_button_new_with_mnemonic (_("Use _old curves file format"));
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
                                tool->export_old_format);
  gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), button);
  gtk_widget_show (button);
688

689 690 691
  g_signal_connect (button, "toggled",
                    G_CALLBACK (gimp_toggle_button_update),
                    &tool->export_old_format);
692
}
693

Elliot Lee's avatar
Elliot Lee committed
694
static void
695 696 697
gimp_curves_tool_config_notify (GObject        *object,
                                GParamSpec     *pspec,
                                GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
698
{
699
  GimpCurvesConfig *config = GIMP_CURVES_CONFIG (object);
700
  GimpCurve        *curve  = config->curve[config->channel];
701

702
  if (! tool->xrange)
703 704
    return;

705
  if (! strcmp (pspec->name, "channel"))
706
    {
707
      GimpHistogramChannel channel;
708

709 710 711
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                     config->channel);

712
      switch (config->channel)
713
        {
714 715 716 717
          guchar r[256];
          guchar g[256];
          guchar b[256];

718 719
        case GIMP_HISTOGRAM_VALUE:
        case GIMP_HISTOGRAM_ALPHA:
720
        case GIMP_HISTOGRAM_RGB:
721
          gimp_curve_get_uchar (curve, sizeof (r), r);
722

723
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
724
                                      r, r, r);
725
          break;
726

727 728 729
        case GIMP_HISTOGRAM_RED:
        case GIMP_HISTOGRAM_GREEN:
        case GIMP_HISTOGRAM_BLUE:
730 731 732 733 734 735
          gimp_curve_get_uchar (config->curve[GIMP_HISTOGRAM_RED],
                                sizeof (r), r);
          gimp_curve_get_uchar (config->curve[GIMP_HISTOGRAM_GREEN],
                                sizeof (g), g);
          gimp_curve_get_uchar (config->curve[GIMP_HISTOGRAM_BLUE],
                                sizeof (b), b);
736

737
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
738
                                      r, g, b);
739 740
          break;
        }
741

742
      gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (tool->graph),
743
                                       config->channel);
744
      gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
745
                                tool->picked_color[config->channel]);
746

747 748
      gimp_color_bar_set_channel (GIMP_COLOR_BAR (tool->yrange),
                                  config->channel);
Elliot Lee's avatar
Elliot Lee committed
749

750
      gimp_curve_view_remove_all_backgrounds (GIMP_CURVE_VIEW (tool->graph));
751

752 753 754
      for (channel = GIMP_HISTOGRAM_VALUE;
           channel <= GIMP_HISTOGRAM_ALPHA;
           channel++)
755
        {
756 757 758 759 760 761 762 763 764 765 766
          if (channel == config->channel)
            {
              gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph), curve,
                                         &channel_colors[channel]);
            }
          else
            {
              gimp_curve_view_add_background (GIMP_CURVE_VIEW (tool->graph),
                                              config->curve[channel],
                                              &channel_colors[channel]);
            }
767 768
        }

769 770
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                     curve->curve_type);
771 772 773
    }
  else if (! strcmp (pspec->name, "curve"))
    {
774 775
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                     curve->curve_type);
776 777 778 779
    }

  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));
}
780

781 782 783 784 785 786 787 788 789 790 791 792 793
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);
794
    }
Elliot Lee's avatar
Elliot Lee committed
795 796 797
}

static void
798
curves_channel_reset_callback (GtkWidget      *widget,
799
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
800
{
801
  gimp_curve_reset (tool->config->curve[tool->config->channel], FALSE);
Elliot Lee's avatar
Elliot Lee committed
802 803
}

804
static gboolean
805 806
curves_menu_sensitivity (gint      value,
                         gpointer  data)
807
{
808 809
  GimpDrawable         *drawable = GIMP_DRAWABLE (data);
  GimpHistogramChannel  channel  = value;
Sven Neumann's avatar
Sven Neumann committed
810

811 812 813 814
  switch (channel)
    {
    case GIMP_HISTOGRAM_VALUE:
      return TRUE;
815

816 817 818
    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
819
      return gimp_drawable_is_rgb (drawable);
820

821
    case GIMP_HISTOGRAM_ALPHA:
822
      return gimp_drawable_has_alpha (drawable);
823 824 825

    case GIMP_HISTOGRAM_RGB:
      return FALSE;
826
    }
827

828 829 830
  return FALSE;
}

Elliot Lee's avatar
Elliot Lee committed
831
static void