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 35
#include "gegl/gimpcurvesconfig.h"
#include "gegl/gimpoperationcurves.h"

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

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

50 51
#include "display/gimpdisplay.h"

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

55
#include "gimp-intl.h"
56

57

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


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

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

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

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

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

Michael Natterer's avatar
Michael Natterer committed
118

119
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
120 121

#define parent_class gimp_curves_tool_parent_class
122

123 124 125 126 127 128 129 130 131
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
132

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

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

152 153 154

/*  private functions  */

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

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
  im_tool_class->dialog_desc         = _("Adjust Color Curves");
170 171 172
  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
  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
179 180 181
}

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

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

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

  if (! drawable)
201
    return FALSE;
202

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

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

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

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

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

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

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

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

243
      closest = gimp_curve_get_closest_point (curve, value);
244

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

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

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

261
          closest = gimp_curve_get_closest_point (curve, value);
262

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

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

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

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

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

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

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

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

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

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

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

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

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

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

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

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

345 346
  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
347

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

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

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

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

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

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

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

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

378

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

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

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

406 407
  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
408

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

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

418 419
  gtk_size_group_add_widget (label_group, label);

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

427 428 429
  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),
430
                                        "gimp-channel");
431 432
  gtk_box_pack_start (GTK_BOX (hbox), tool->channel_menu, FALSE, FALSE, 0);
  gtk_widget_show (tool->channel_menu);
433

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

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

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

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

448 449 450 451 452 453
  /*  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);
454

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

538 539 540 541 542 543 544 545
  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
546 547

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

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

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

559 560 561 562
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
      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);
        }
583
    }
584 585
}

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

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

606 607 608 609 610 611 612 613 614 615 616 617
  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"))
    {
618 619
      gboolean success;

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

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

      fclose (file);

      return success;
    }
628 629

  fclose (file);
630

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

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

643
  if (tool->export_old_format)
644
    {
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
      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;
664 665
    }

666 667 668 669 670 671 672 673 674 675 676 677
  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;
678

679 680 681 682 683 684 685 686
  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);
687

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

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

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

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

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

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

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

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

726 727 728
        case GIMP_HISTOGRAM_RED:
        case GIMP_HISTOGRAM_GREEN:
        case GIMP_HISTOGRAM_BLUE:
729 730 731 732 733 734
          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);
735

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

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

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

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

751 752 753
      for (channel = GIMP_HISTOGRAM_VALUE;
           channel <= GIMP_HISTOGRAM_ALPHA;
           channel++)
754
        {
755 756 757 758 759 760 761 762 763 764 765
          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]);
            }
766 767
        }

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

  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));
}
779

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

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

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

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

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

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

    case GIMP_HISTOGRAM_RGB:
      return FALSE;
825
    }
826

827 828 829
  return FALSE;
}