gimpcurvestool.c 30.2 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Elliot Lee's avatar
Elliot Lee committed
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
Elliot Lee's avatar
Elliot Lee committed
7 8 9 10 11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
Sven Neumann's avatar
Sven Neumann committed
17

18 19
#include "config.h"

20 21 22
#include <errno.h>

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

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

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

33 34
#include "operations/gimpcurvesconfig.h"
#include "operations/gimpoperationcurves.h"
35

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
static void       gimp_curves_tool_constructed    (GObject              *object);

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

static void       gimp_curves_tool_color_picked   (GimpColorTool        *color_tool,
                                                   GimpColorPickState    pick_state,
87 88
                                                   gdouble               x,
                                                   gdouble               y,
89
                                                   const Babl           *sample_format,
90 91
                                                   gpointer              pixel,
                                                   const GimpRGB        *color);
92
static GeglNode * gimp_curves_tool_get_operation  (GimpImageMapTool     *image_map_tool,
93 94
                                                   GObject             **config,
                                                   gchar               **undo_desc);
95 96
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
                                                   GInputStream         *input,
99
                                                   GError              **error);
100
static gboolean   gimp_curves_tool_settings_export(GimpImageMapTool     *image_map_tool,
101
                                                   GOutputStream        *output,
102
                                                   GError              **error);
103

104 105 106 107
static void       gimp_curves_tool_export_setup   (GimpSettingsBox      *settings_box,
                                                   GtkFileChooserDialog *dialog,
                                                   gboolean              export,
                                                   GimpCurvesTool       *tool);
108
static void       gimp_curves_tool_update_channel (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

124 125
static const GimpRGB * curves_get_channel_color   (GimpHistogramChannel  channel);

Michael Natterer's avatar
Michael Natterer committed
126

127
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
128 129

#define parent_class gimp_curves_tool_parent_class
130

Michael Natterer's avatar
Michael Natterer committed
131

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

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

151 152 153

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
154 155 156
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
157
  GObjectClass          *object_class     = G_OBJECT_CLASS (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
  object_class->constructed          = gimp_curves_tool_constructed;

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

169
  color_tool_class->picked           = gimp_curves_tool_color_picked;
170

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

176 177 178 179 180
  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
181 182 183
}

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

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

192 193 194 195 196 197 198 199 200 201 202
static void
gimp_curves_tool_constructed (GObject *object)
{
  G_OBJECT_CLASS (parent_class)->constructed (object);

  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (object),
                          GIMP_COLOR_TOOL_GET_OPTIONS (object));

}

203
static gboolean
204 205 206
gimp_curves_tool_initialize (GimpTool     *tool,
                             GimpDisplay  *display,
                             GError      **error)
Michael Natterer's avatar
Michael Natterer committed
207
{
208
  GimpCurvesTool *c_tool   = GIMP_CURVES_TOOL (tool);
209 210
  GimpImage      *image    = gimp_display_get_image (display);
  GimpDrawable   *drawable = gimp_image_get_active_drawable (image);
211
  GimpHistogram  *histogram;
212

213 214 215 216
  if (! GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error))
    {
      return FALSE;
    }
217

218
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
219
                                      curves_menu_sensitivity, drawable, NULL);
220

221
  histogram = gimp_histogram_new (TRUE);
222
  gimp_drawable_calculate_histogram (drawable, histogram);
223
  gimp_histogram_view_set_background (GIMP_HISTOGRAM_VIEW (c_tool->graph),
224
                                      histogram);
225
  g_object_unref (histogram);
226

227
  if (gimp_drawable_get_component_type (drawable) == GIMP_COMPONENT_TYPE_U8)
228 229 230 231 232 233
    {
      gimp_curve_view_set_range_x (GIMP_CURVE_VIEW (c_tool->graph), 0, 255);
      gimp_curve_view_set_range_y (GIMP_CURVE_VIEW (c_tool->graph), 0, 255);
    }
  else
    {
234 235
      gimp_curve_view_set_range_x (GIMP_CURVE_VIEW (c_tool->graph), 0, 100);
      gimp_curve_view_set_range_y (GIMP_CURVE_VIEW (c_tool->graph), 0, 100);
236 237
    }

238
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
239 240 241
}

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

252
  if (state & gimp_get_extend_selection_mask ())
Michael Natterer's avatar
Michael Natterer committed
253
    {
254
      GimpCurve *curve = config->curve[config->channel];
255
      gdouble    value = c_tool->picked_color[config->channel];
256
      gint       closest;
257

258
      closest = gimp_curve_get_closest_point (curve, value);
259

260
      gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
261
                                    closest);
262

263
      gimp_curve_set_point (curve, closest,
264
                            value, gimp_curve_map_value (curve, value));
Michael Natterer's avatar
Michael Natterer committed
265
    }
266
  else if (state & gimp_get_toggle_behavior_mask ())
Michael Natterer's avatar
Michael Natterer committed
267
    {
268
      GimpHistogramChannel channel;
269

270 271 272
      for (channel = GIMP_HISTOGRAM_VALUE;
           channel <= GIMP_HISTOGRAM_ALPHA;
           channel++)
273
        {
274 275
          GimpCurve *curve = config->curve[channel];
          gdouble    value = c_tool->picked_color[channel];
276
          gint       closest;
277

278 279 280
          if (value != -1)
            {
              closest = gimp_curve_get_closest_point (curve, value);
281

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

285 286 287
              gimp_curve_set_point (curve, closest,
                                    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
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);

303 304 305 306 307
  if (tool->display && c_tool->graph)
    {
      if (gtk_widget_event (c_tool->graph, (GdkEvent *) kevent))
        return TRUE;
    }
308 309

  return GIMP_TOOL_CLASS (parent_class)->key_press (tool, kevent, display);
310 311 312
}

static void
313 314 315 316 317
gimp_curves_tool_oper_update (GimpTool         *tool,
                              const GimpCoords *coords,
                              GdkModifierType   state,
                              gboolean          proximity,
                              GimpDisplay      *display)
318
{
319
  GimpColorPickMode  mode;
320 321 322
  gchar             *status      = NULL;
  GdkModifierType    extend_mask = gimp_get_extend_selection_mask ();
  GdkModifierType    toggle_mask = gimp_get_toggle_behavior_mask ();
323

324
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
325
                                               display);
326

327
  gimp_tool_pop_status (tool, display);
328

329
  if (state & extend_mask)
330 331
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
332
      status = g_strdup (_("Click to add a control point"));
333
    }
334
  else if (state & toggle_mask)
335 336
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
337
      status = g_strdup (_("Click to add control points to all channels"));
338
    }
339 340 341
  else
    {
      mode   = GIMP_COLOR_PICK_MODE_NONE;
342 343 344 345 346
      status = gimp_suggest_modifiers (_("Click to locate on curve"),
                                       (extend_mask | toggle_mask) & ~state,
                                       _("%s: add control point"),
                                       _("%s: add control points to all channels"),
                                       NULL);
347
    }
348 349 350

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

351
  if (proximity)
352
    gimp_tool_push_status (tool, display, "%s", status);
353 354

  g_free (status);
355 356
}

Michael Natterer's avatar
Michael Natterer committed
357
static void
358 359
gimp_curves_tool_color_picked (GimpColorTool      *color_tool,
                               GimpColorPickState  pick_state,
360 361
                               gdouble             x,
                               gdouble             y,
362
                               const Babl         *sample_format,
363 364
                               gpointer            pixel,
                               const GimpRGB      *color)
Michael Natterer's avatar
Michael Natterer committed
365
{
366 367
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (color_tool);
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
368

369
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
370

371 372 373
  tool->picked_color[GIMP_HISTOGRAM_RED]   = color->r;
  tool->picked_color[GIMP_HISTOGRAM_GREEN] = color->g;
  tool->picked_color[GIMP_HISTOGRAM_BLUE]  = color->b;
374

375
  if (gimp_drawable_has_alpha (drawable))
376
    tool->picked_color[GIMP_HISTOGRAM_ALPHA] = color->a;
377 378
  else
    tool->picked_color[GIMP_HISTOGRAM_ALPHA] = -1;
379

380 381
  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
382

383
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
384
                            tool->picked_color[tool->config->channel]);
385 386 387
}

static GeglNode *
388
gimp_curves_tool_get_operation (GimpImageMapTool  *image_map_tool,
389 390
                                GObject          **config,
                                gchar            **undo_desc)
391 392 393 394 395
{
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);

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

396 397 398
  g_signal_connect_object (tool->config, "notify",
                           G_CALLBACK (gimp_curves_tool_config_notify),
                           tool, 0);
399

400
  *config = G_OBJECT (tool->config);
401

402 403 404 405
  return gegl_node_new_child (NULL,
                              "operation", "gimp:curves",
                              "config",    tool->config,
                              NULL);
406
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
407

408

409 410 411
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
412

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

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

436 437
  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
438

439
  /*  The combo box for selecting channels  */
Michael Natterer's avatar
Michael Natterer committed
440
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
441
  gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
442 443
  gtk_widget_show (hbox);

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

448 449
  gtk_size_group_add_widget (label_group, label);

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

457 458
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                 config->channel);
459 460
  gimp_enum_combo_box_set_icon_prefix (GIMP_ENUM_COMBO_BOX (tool->channel_menu),
                                       "gimp-channel");
461 462
  gtk_box_pack_start (GTK_BOX (hbox), tool->channel_menu, FALSE, FALSE, 0);
  gtk_widget_show (tool->channel_menu);
463

464 465 466
  g_signal_connect (tool->channel_menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);
467

468
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->channel_menu);
469

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

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

478
  /*  The histogram scale radio buttons  */
479 480 481
  hbox2 = gimp_prop_enum_icon_box_new (G_OBJECT (tool_options),
                                       "histogram-scale", "gimp-histogram",
                                       0, 0);
482 483
  gtk_box_pack_end (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);
  gtk_widget_show (hbox2);
484

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

  /*  The left color bar  */
Michael Natterer's avatar
Michael Natterer committed
492
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
493
  gtk_table_attach (GTK_TABLE (table), vbox, 0, 1, 0, 1,
494
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
495
  gtk_widget_show (vbox);
Elliot Lee's avatar
Elliot Lee committed
496 497

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

502 503 504 505
  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);
506

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

514
  tool->graph = gimp_curve_view_new ();
515 516
  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);
517 518 519 520
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
521 522 523
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
524 525
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
526

527
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
528 529 530
                                       GIMP_HISTOGRAM_VIEW (tool->graph));

  /*  The bottom color bar  */
Michael Natterer's avatar
Michael Natterer committed
531
  hbox2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
532
  gtk_table_attach (GTK_TABLE (table), hbox2, 1, 2, 1, 2,
533
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
534
  gtk_widget_show (hbox2);
535

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

Michael Natterer's avatar
Michael Natterer committed
541 542
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_box_set_homogeneous (GTK_BOX (vbox), TRUE);
543 544
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);
545

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

551
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
552
  gtk_box_pack_start (GTK_BOX (vbox), bar, TRUE, TRUE, 0);
553 554
  gtk_widget_show (bar);

555
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
556

Michael Natterer's avatar
Michael Natterer committed
557
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
558
  gtk_box_pack_end (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
559
  gtk_widget_show (hbox);
560

561 562 563
  label = gtk_label_new_with_mnemonic (_("Curve _type:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
564

565
  tool->curve_type = combo = gimp_enum_combo_box_new (GIMP_TYPE_CURVE_TYPE);
566 567
  gimp_enum_combo_box_set_icon_prefix (GIMP_ENUM_COMBO_BOX (combo),
                                       "gimp-curve");
568 569 570 571 572
  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
573 574

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
575 576

  gimp_curves_tool_update_channel (tool);
Elliot Lee's avatar
Elliot Lee committed
577 578
}

579
static void
580
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
581
{
582
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (image_map_tool);
583
  GimpCurvesConfig     *default_config;
584
  GimpHistogramChannel  channel;
585

586 587
  default_config = GIMP_CURVES_CONFIG (image_map_tool->default_config);

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

615
static gboolean
616
gimp_curves_tool_settings_import (GimpImageMapTool  *image_map_tool,
617
                                  GInputStream      *input,
618
                                  GError           **error)
619
{
620
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
621
  gchar           header[64];
622
  gsize           bytes_read;
623

624 625 626
  if (! g_input_stream_read_all (input, header, sizeof (header),
                                 &bytes_read, NULL, error) ||
      bytes_read != sizeof (header))
627
    {
628
      g_prefix_error (error, _("Could not read header: "));
629 630 631
      return FALSE;
    }

632
  g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL);
633

634 635
  if (g_str_has_prefix (header, "# GIMP Curves File\n"))
    return gimp_curves_config_load_cruft (tool->config, input, error);
636

637
  return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_import (image_map_tool,
638
                                                                    input,
639
                                                                    error);
640 641 642
}

static gboolean
643
gimp_curves_tool_settings_export (GimpImageMapTool  *image_map_tool,
644
                                  GOutputStream     *output,
645
                                  GError           **error)
646
{
647
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
648

649
  if (tool->export_old_format)
650
    return gimp_curves_config_save_cruft (tool->config, output, error);
651

652
  return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_export (image_map_tool,
653
                                                                    output,
654 655 656 657 658 659 660 661 662 663
                                                                    error);
}

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

665 666 667 668 669 670 671 672
  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);
673

674 675 676
  g_signal_connect (button, "toggled",
                    G_CALLBACK (gimp_toggle_button_update),
                    &tool->export_old_format);
677
}
678

Elliot Lee's avatar
Elliot Lee committed
679
static void
680
gimp_curves_tool_update_channel (GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
681
{
682 683 684
  GimpCurvesConfig     *config = GIMP_CURVES_TOOL (tool)->config;
  GimpCurve            *curve  = config->curve[config->channel];
  GimpHistogramChannel  channel;
685

686 687
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                 config->channel);
688

689
  switch (config->channel)
690
    {
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
      guchar r[256];
      guchar g[256];
      guchar b[256];

    case GIMP_HISTOGRAM_VALUE:
    case GIMP_HISTOGRAM_ALPHA:
    case GIMP_HISTOGRAM_RGB:
      gimp_curve_get_uchar (curve, sizeof (r), r);

      gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
                                  r, r, r);
      break;

    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
      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);

      gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
                                  r, g, b);
      break;
    }

  gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (tool->graph),
                                   config->channel);
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
                            tool->picked_color[config->channel]);

  gimp_color_bar_set_channel (GIMP_COLOR_BAR (tool->yrange),
                              config->channel);
726

727
  gimp_curve_view_remove_all_backgrounds (GIMP_CURVE_VIEW (tool->graph));
728

729 730 731 732 733
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
      if (channel == config->channel)
734
        {
735 736
          gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph), curve,
                                     curves_get_channel_color (channel));
737
        }
738 739 740 741 742 743 744
      else
        {
          gimp_curve_view_add_background (GIMP_CURVE_VIEW (tool->graph),
                                          config->curve[channel],
                                          curves_get_channel_color (channel));
        }
    }
745

746 747 748
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                 curve->curve_type);
}
Elliot Lee's avatar
Elliot Lee committed
749

750 751 752 753 754 755 756
static void
gimp_curves_tool_config_notify (GObject        *object,
                                GParamSpec     *pspec,
                                GimpCurvesTool *tool)
{
  GimpCurvesConfig *config = GIMP_CURVES_CONFIG (object);
  GimpCurve        *curve  = config->curve[config->channel];
757

758 759
  if (! tool->xrange)
    return;
760

761 762 763
  if (! strcmp (pspec->name, "channel"))
    {
      gimp_curves_tool_update_channel (GIMP_CURVES_TOOL (tool));
764 765 766
    }
  else if (! strcmp (pspec->name, "curve"))
    {
767 768
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                     curve->curve_type);
769 770
    }
}
771

772 773 774 775 776 777 778 779 780 781 782 783 784
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);
785
    }
Elliot Lee's avatar
Elliot Lee committed
786 787 788
}

static void
789
curves_channel_reset_callback (GtkWidget      *widget,
790
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
791
{
792
  gimp_curve_reset (tool->config->curve[tool->config->channel], FALSE);
Elliot Lee's avatar
Elliot Lee committed
793 794
}

795
static gboolean
796 797
curves_menu_sensitivity (gint      value,
                         gpointer  data)
798
{
799 800
  GimpDrawable         *drawable = GIMP_DRAWABLE (data);
  GimpHistogramChannel  channel  = value;
Sven Neumann's avatar
Sven Neumann committed
801

802 803 804 805
  switch (channel)
    {
    case GIMP_HISTOGRAM_VALUE:
      return TRUE;
806

807 808 809
    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
810
      return gimp_drawable_is_rgb (drawable);
811