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
#include "base/curves.h"
Michael Natterer's avatar
Michael Natterer committed
34 35 36
#include "base/gimphistogram.h"
#include "base/gimplut.h"

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

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

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

52 53
#include "display/gimpdisplay.h"

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

57
#include "gimp-intl.h"
58

59

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


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

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

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

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

Michael Natterer's avatar
Michael Natterer committed
123

124
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
125 126

#define parent_class gimp_curves_tool_parent_class
127

Michael Natterer's avatar
Michael Natterer committed
128

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

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

148 149 150

/*  private functions  */

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

159
  object_class->finalize             = gimp_curves_tool_finalize;
160

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

166
  color_tool_class->picked           = gimp_curves_tool_color_picked;
167

168 169 170 171
  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");
172

173 174 175 176 177 178
  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
179 180 181
}

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

187
  tool->lut = gimp_lut_new ();
188

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

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

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

201 202
  gimp_lut_free (tool->lut);

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

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

  if (! drawable)
216
    return FALSE;
217

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

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

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

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

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

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

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

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

256
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
257
    {
258
      GimpCurve *curve = config->curve[config->channel];
259
      gdouble    value = c_tool->picked_color[config->channel];
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));
Michael Natterer's avatar
Michael Natterer committed
269
    }
270
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
271
    {
272
      gint i;
273

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

280
          closest = gimp_curve_get_closest_point (curve, value);
281

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

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

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

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

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

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

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

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

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

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

405

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

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

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

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

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

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

445 446
  gtk_size_group_add_widget (label_group, label);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      fclose (file);

      return success;
    }
651 652

  fclose (file);
653

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));
}
783

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

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

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

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

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

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

    case GIMP_HISTOGRAM_RGB:
      return FALSE;
829
    }