gimpcurvestool.c 29.2 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 22 23
#include <errno.h>

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

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

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

34
#include "base/curves.h"
Michael Natterer's avatar
Michael Natterer committed
35 36 37
#include "base/gimphistogram.h"
#include "base/gimplut.h"

38 39 40
#include "gegl/gimpcurvesconfig.h"
#include "gegl/gimpoperationcurves.h"

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

49
#include "widgets/gimpcolorbar.h"
50
#include "widgets/gimphelp-ids.h"
51
#include "widgets/gimpcurveview.h"
Michael Natterer's avatar
Michael Natterer committed
52

53 54
#include "display/gimpdisplay.h"

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

58
#include "gimp-intl.h"
59

60

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


Michael Natterer's avatar
Michael Natterer committed
66 67
/*  local function prototypes  */

68 69 70 71 72 73
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,
74
                                                   const GimpCoords     *coords,
75 76 77 78 79 80 81 82
                                                   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,
83
                                                   const GimpCoords     *coords,
84 85 86 87 88 89 90 91 92
                                                   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);
93 94
static GeglNode * gimp_curves_tool_get_operation  (GimpImageMapTool     *image_map_tool,
                                                   GObject             **config);
95 96 97
static void       gimp_curves_tool_map            (GimpImageMapTool     *image_map_tool);
static void       gimp_curves_tool_dialog         (GimpImageMapTool     *image_map_tool);
static void       gimp_curves_tool_reset          (GimpImageMapTool     *image_map_tool);
98
static gboolean   gimp_curves_tool_settings_import(GimpImageMapTool     *image_map_tool,
99
                                                   const gchar          *filename,
100
                                                   GError              **error);
101
static gboolean   gimp_curves_tool_settings_export(GimpImageMapTool     *image_map_tool,
102 103
                                                   const gchar          *filename,
                                                   GError              **error);
104

105 106 107 108
static void       gimp_curves_tool_export_setup   (GimpSettingsBox      *settings_box,
                                                   GtkFileChooserDialog *dialog,
                                                   gboolean              export,
                                                   GimpCurvesTool       *tool);
109 110
static void       gimp_curves_tool_config_notify  (GObject              *object,
                                                   GParamSpec           *pspec,
111
                                                   GimpCurvesTool       *tool);
112

113 114 115 116 117 118 119 120 121 122
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);
123

Michael Natterer's avatar
Michael Natterer committed
124

125
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
126 127

#define parent_class gimp_curves_tool_parent_class
128

Michael Natterer's avatar
Michael Natterer committed
129

130
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
131 132

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

149 150 151

/*  private functions  */

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

160
  object_class->finalize             = gimp_curves_tool_finalize;
161

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

167
  color_tool_class->picked           = gimp_curves_tool_color_picked;
168

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

174 175 176 177 178 179
  im_tool_class->get_operation       = gimp_curves_tool_get_operation;
  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_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 186
  GimpImageMapTool *im_tool = GIMP_IMAGE_MAP_TOOL (tool);
  gint              i;
187

188
  tool->lut = gimp_lut_new ();
189

190 191
  for (i = 0; i < G_N_ELEMENTS (tool->picked_color); i++)
    tool->picked_color[i] = -1.0;
192 193 194

  im_tool->apply_func = (GimpImageMapApplyFunc) gimp_lut_process;
  im_tool->apply_data = tool->lut;
Michael Natterer's avatar
Michael Natterer committed
195 196 197
}

static void
198
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
199
{
200
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (object);
201

202 203
  gimp_lut_free (tool->lut);

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
  GimpHistogram  *histogram;
215 216

  if (! drawable)
217
    return FALSE;
218

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

226
  gimp_config_reset (GIMP_CONFIG (c_tool->config));
227

228
  GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error);
229

230 231
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
232
                          GIMP_COLOR_TOOL_GET_OPTIONS (tool));
233

234
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
235
                                      curves_menu_sensitivity, drawable, NULL);
236

237 238
  histogram = gimp_histogram_new ();
  gimp_drawable_calculate_histogram (drawable, histogram);
239
  gimp_histogram_view_set_background (GIMP_HISTOGRAM_VIEW (c_tool->graph),
240 241
                                      histogram);
  gimp_histogram_unref (histogram);
242

243
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
244 245 246
}

static void
247
gimp_curves_tool_button_release (GimpTool              *tool,
248
                                 const GimpCoords      *coords,
249 250 251 252
                                 guint32                time,
                                 GdkModifierType        state,
                                 GimpButtonReleaseType  release_type,
                                 GimpDisplay           *display)
Michael Natterer's avatar
Michael Natterer committed
253
{
254 255
  GimpCurvesTool   *c_tool = GIMP_CURVES_TOOL (tool);
  GimpCurvesConfig *config = c_tool->config;
Michael Natterer's avatar
Michael Natterer committed
256

257
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
258
    {
259
      GimpCurve *curve = config->curve[config->channel];
260
      gdouble    value = c_tool->picked_color[config->channel];
261
      gint       closest;
262

263
      closest = gimp_curve_get_closest_point (curve, value);
264

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

268
      gimp_curve_set_point (curve, closest,
269
                            value, gimp_curve_map_value (curve, value));
Michael Natterer's avatar
Michael Natterer committed
270
    }
271
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
272
    {
273
      gint i;
274

275 276
      for (i = 0; i < 5; i++)
        {
277
          GimpCurve *curve = config->curve[i];
278
          gdouble    value = c_tool->picked_color[i];
279
          gint       closest;
280

281
          closest = gimp_curve_get_closest_point (curve, value);
282

283
          gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
284
                                        closest);
285

286
          gimp_curve_set_point (curve, closest,
287
                                value, gimp_curve_map_value (curve, value));
288
        }
Michael Natterer's avatar
Michael Natterer committed
289 290
    }

291
  /*  chain up to halt the tool */
292 293
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Michael Natterer's avatar
Michael Natterer committed
294 295
}

296
static gboolean
297 298
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
299
                            GimpDisplay *display)
300
{
301 302 303
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);

  return gtk_widget_event (c_tool->graph, (GdkEvent *) kevent);
304 305 306
}

static void
307 308 309 310 311
gimp_curves_tool_oper_update (GimpTool         *tool,
                              const GimpCoords *coords,
                              GdkModifierType   state,
                              gboolean          proximity,
                              GimpDisplay      *display)
312 313 314 315
{
  GimpColorPickMode  mode   = GIMP_COLOR_PICK_MODE_NONE;
  const gchar       *status = NULL;

316
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
317
                                               display);
318

319
  gimp_tool_pop_status (tool, display);
320 321 322 323

  if (state & GDK_SHIFT_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
324
      status = _("Click to add a control point");
325 326 327 328
    }
  else if (state & GDK_CONTROL_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
329
      status = _("Click to add control points to all channels");
330 331 332 333
    }

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

334
  if (status && proximity)
335
    gimp_tool_push_status (tool, display, "%s", status);
336 337
}

Michael Natterer's avatar
Michael Natterer committed
338
static void
339 340
gimp_curves_tool_color_picked (GimpColorTool      *color_tool,
                               GimpColorPickState  pick_state,
341 342 343
                               GimpImageType       sample_type,
                               GimpRGB            *color,
                               gint                color_index)
Michael Natterer's avatar
Michael Natterer committed
344
{
345 346
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (color_tool);
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
347

348
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
349

350 351 352
  tool->picked_color[GIMP_HISTOGRAM_RED]   = color->r;
  tool->picked_color[GIMP_HISTOGRAM_GREEN] = color->g;
  tool->picked_color[GIMP_HISTOGRAM_BLUE]  = color->b;
353

354
  if (gimp_drawable_has_alpha (drawable))
355
    tool->picked_color[GIMP_HISTOGRAM_ALPHA] = color->a;
356

357 358
  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
359

360
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
361
                            tool->picked_color[tool->config->channel]);
362 363 364
}

static GeglNode *
365 366
gimp_curves_tool_get_operation (GimpImageMapTool  *image_map_tool,
                                GObject          **config)
367 368 369 370 371
{
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  GeglNode       *node;

  node = g_object_new (GEGL_TYPE_NODE,
372
                       "operation", "gimp:curves",
373 374 375 376
                       NULL);

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

377 378 379 380 381
  *config = G_OBJECT (tool->config);

  g_signal_connect_object (tool->config, "notify",
                           G_CALLBACK (gimp_curves_tool_config_notify),
                           tool, 0);
382 383 384 385 386 387

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

  return node;
388
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
389

390 391
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
392
{
393 394
  GimpCurvesTool *tool     = GIMP_CURVES_TOOL (image_map_tool);
  GimpDrawable   *drawable = image_map_tool->drawable;
395 396
  Curves          curves;

397 398
  gimp_curves_config_to_cruft (tool->config, &curves,
                               gimp_drawable_is_rgb (drawable));
399

400
  gimp_lut_setup (tool->lut,
401
                  (GimpLutFunc) curves_lut_func,
402
                  &curves,
403
                  gimp_drawable_bytes (drawable));
Elliot Lee's avatar
Elliot Lee committed
404 405
}

406

407 408 409
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
410

411 412
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
413
{
414 415 416 417
  GimpCurvesTool   *tool         = GIMP_CURVES_TOOL (image_map_tool);
  GimpToolOptions  *tool_options = GIMP_TOOL_GET_OPTIONS (image_map_tool);
  GimpCurvesConfig *config       = tool->config;
  GtkListStore     *store;
418 419
  GtkSizeGroup     *label_group;
  GtkWidget        *main_vbox;
420 421 422 423 424 425 426 427
  GtkWidget        *vbox;
  GtkWidget        *hbox;
  GtkWidget        *hbox2;
  GtkWidget        *label;
  GtkWidget        *frame;
  GtkWidget        *table;
  GtkWidget        *button;
  GtkWidget        *bar;
428
  GtkWidget        *combo;
429

430 431 432 433
  g_signal_connect (image_map_tool->settings_box, "file-dialog-setup",
                    G_CALLBACK (gimp_curves_tool_export_setup),
                    image_map_tool);

434 435
  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
436

437
  /*  The combo box for selecting channels  */
438
  hbox = gtk_hbox_new (FALSE, 6);
439
  gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
440 441
  gtk_widget_show (hbox);

442
  label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
443 444 445
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

446 447
  gtk_size_group_add_widget (label_group, label);

448 449 450
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
451 452
  tool->channel_menu =
    gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
453 454
  g_object_unref (store);

455 456 457
  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),
458
                                        "gimp-channel");
459 460
  gtk_box_pack_start (GTK_BOX (hbox), tool->channel_menu, FALSE, FALSE, 0);
  gtk_widget_show (tool->channel_menu);
461

462 463 464
  g_signal_connect (tool->channel_menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);
465

466
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->channel_menu);
467

468
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
469
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
470
  gtk_widget_show (button);
471

472
  g_signal_connect (button, "clicked",
473
                    G_CALLBACK (curves_channel_reset_callback),
474
                    tool);
Elliot Lee's avatar
Elliot Lee committed
475

476 477 478 479 480 481
  /*  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);
482

483
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
484
  table = gtk_table_new (2, 2, FALSE);
485 486
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
487
  gtk_box_pack_start (GTK_BOX (main_vbox), table, TRUE, TRUE, 0);
488 489

  /*  The left color bar  */
490 491
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), vbox, 0, 1, 0, 1,
492
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
493
  gtk_widget_show (vbox);
Elliot Lee's avatar
Elliot Lee committed
494 495

  frame = gtk_frame_new (NULL);
496
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
497
  gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
498 499
  gtk_widget_show (frame);

500 501 502 503
  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);
504

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

512
  tool->graph = gimp_curve_view_new ();
513 514 515 516
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
517 518 519
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
520 521
  gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph),
                             config->curve[config->channel]);
522 523
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
524

525
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
526 527 528 529 530
                                       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,
531
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
532
  gtk_widget_show (hbox2);
533

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

539 540 541
  vbox = gtk_vbox_new (TRUE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);
542

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

548
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
549
  gtk_box_pack_start (GTK_BOX (vbox), bar, TRUE, TRUE, 0);
550 551
  gtk_widget_show (bar);

552
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
553

554
  hbox = gtk_hbox_new (FALSE, 6);
555
  gtk_box_pack_end (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
556
  gtk_widget_show (hbox);
557

558 559 560
  label = gtk_label_new_with_mnemonic (_("Curve _type:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
561

562 563 564 565 566 567 568 569
  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
570 571

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
Elliot Lee's avatar
Elliot Lee committed
572 573
}

574
static void
575
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
576
{
577
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (image_map_tool);
578
  GimpCurvesConfig     *default_config;
579
  GimpHistogramChannel  channel;
580

581 582
  default_config = GIMP_CURVES_CONFIG (image_map_tool->default_config);

583 584 585 586
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
      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);
        }
607
    }
608 609
}

610
static gboolean
611 612 613
gimp_curves_tool_settings_import (GimpImageMapTool  *image_map_tool,
                                  const gchar       *filename,
                                  GError           **error)
614
{
615
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
616
  FILE           *file;
617
  gchar           header[64];
618 619 620 621 622 623 624 625 626 627 628 629

  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;
    }

630 631 632 633 634 635 636 637 638 639 640 641
  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"))
    {
642 643
      gboolean success;

644 645 646 647 648 649 650 651
      rewind (file);

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

      fclose (file);

      return success;
    }
652 653

  fclose (file);
654

655 656 657
  return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_import (image_map_tool,
                                                                    filename,
                                                                    error);
658 659 660
}

static gboolean
661 662 663
gimp_curves_tool_settings_export (GimpImageMapTool  *image_map_tool,
                                  const gchar       *filename,
                                  GError           **error)
664
{
665
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
666

667
  if (tool->export_old_format)
668
    {
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
      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;
688 689
    }

690 691 692 693 694 695 696 697 698 699 700 701
  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;
702

703 704 705 706 707 708 709 710
  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);
711

712 713 714
  g_signal_connect (button, "toggled",
                    G_CALLBACK (gimp_toggle_button_update),
                    &tool->export_old_format);
715
}
716

Elliot Lee's avatar
Elliot Lee committed
717
static void
718 719 720
gimp_curves_tool_config_notify (GObject        *object,
                                GParamSpec     *pspec,
                                GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
721
{
722
  GimpCurvesConfig *config = GIMP_CURVES_CONFIG (object);
723
  GimpCurve        *curve  = config->curve[config->channel];
724

725
  if (! tool->xrange)
726 727
    return;

728
  if (! strcmp (pspec->name, "channel"))
729
    {
730 731 732
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                     config->channel);

733
      switch (config->channel)
734
        {
735 736 737 738
          guchar r[256];
          guchar g[256];
          guchar b[256];

739 740
        case GIMP_HISTOGRAM_VALUE:
        case GIMP_HISTOGRAM_ALPHA:
741
        case GIMP_HISTOGRAM_RGB:
742
          gimp_curve_get_uchar (curve, sizeof (r), r);
743

744
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
745
                                      r, r, r);
746
          break;
747

748 749 750
        case GIMP_HISTOGRAM_RED:
        case GIMP_HISTOGRAM_GREEN:
        case GIMP_HISTOGRAM_BLUE:
751 752 753 754 755 756
          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);
757

758
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
759
                                      r, g, b);
760 761
          break;
        }
762

763
      gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (tool->graph),
764
                                       config->channel);
765
      gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
766
                                tool->picked_color[config->channel]);
767

768 769
      gimp_color_bar_set_channel (GIMP_COLOR_BAR (tool->yrange),
                                  config->channel);
Elliot Lee's avatar
Elliot Lee committed
770

771 772
      gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph), curve);

773 774
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                     curve->curve_type);
775 776 777
    }
  else if (! strcmp (pspec->name, "curve"))
    {
778 779
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                     curve->curve_type);
780 781 782 783
    }

  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));
}
784

785 786 787 788 789 790 791 792 793 794 795 796 797
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);
798
    }
Elliot Lee's avatar
Elliot Lee committed
799 800 801
}

static void
802
curves_channel_reset_callback (GtkWidget      *widget,
803
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
804
{
805
  gimp_curve_reset (tool->config->curve[tool->config->channel], FALSE);
Elliot Lee's avatar
Elliot Lee committed
806 807
}

808
static gboolean
809 810
curves_menu_sensitivity (gint      value,
                         gpointer  data)
811
{
812 813
  GimpDrawable         *drawable = GIMP_DRAWABLE (data);
  GimpHistogramChannel  channel  = value;
Sven Neumann's avatar
Sven Neumann committed
814

815 816 817 818
  switch (channel)
    {
    case GIMP_HISTOGRAM_VALUE:
      return TRUE;
819

820 821 822
    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
823
      return gimp_drawable_is_rgb (drawable);
824

825
    case GIMP_HISTOGRAM_ALPHA:
826
      return gimp_drawable_has_alpha (drawable);
827 828 829

    case GIMP_HISTOGRAM_RGB:
      return FALSE;
830
    }
831

832 833 834
  return FALSE;
}

Elliot Lee's avatar
Elliot Lee committed
835
static void