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

19 20
#include "config.h"

21 22 23
#include <errno.h>

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

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

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

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

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

41
#include "core/gimp.h"
42
#include "core/gimpcurve.h"
Sven Neumann's avatar
Sven Neumann committed
43
#include "core/gimpcurve-map.h"
Michael Natterer's avatar
Michael Natterer committed
44
#include "core/gimpdrawable.h"
45
#include "core/gimpdrawable-histogram.h"
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
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,
                                                   GimpCoords           *coords,
                                                   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,
                                                   GimpCoords           *coords,
                                                   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
static void       gimp_curves_tool_config_notify  (GObject              *object,
                                                   GParamSpec           *pspec,
106
                                                   GimpCurvesTool       *tool);
107

108 109 110 111 112 113 114 115 116 117
static void       curves_channel_callback         (GtkWidget            *widget,
                                                   GimpCurvesTool       *tool);
static void       curves_channel_reset_callback   (GtkWidget            *widget,
                                                   GimpCurvesTool       *tool);

static gboolean   curves_menu_sensitivity         (gint                  value,
                                                   gpointer              data);

static void       curves_curve_type_callback      (GtkWidget            *widget,
                                                   GimpCurvesTool       *tool);
118

Michael Natterer's avatar
Michael Natterer committed
119

120
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
121 122

#define parent_class gimp_curves_tool_parent_class
123

Michael Natterer's avatar
Michael Natterer committed
124

125
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
126 127

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

144 145 146

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
147 148 149
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
150 151 152 153 154
  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);

155
  object_class->finalize             = gimp_curves_tool_finalize;
156

157 158 159 160
  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;
161

162
  color_tool_class->picked           = gimp_curves_tool_color_picked;
163

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

169 170 171 172 173 174
  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
175 176 177
}

static void
178
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
179
{
180 181
  GimpImageMapTool *im_tool = GIMP_IMAGE_MAP_TOOL (tool);
  gint              i;
182

183
  tool->lut = gimp_lut_new ();
184

185 186
  for (i = 0; i < G_N_ELEMENTS (tool->picked_color); i++)
    tool->picked_color[i] = -1.0;
187 188 189

  im_tool->apply_func = (GimpImageMapApplyFunc) gimp_lut_process;
  im_tool->apply_data = tool->lut;
Michael Natterer's avatar
Michael Natterer committed
190 191 192
}

static void
193
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
194
{
195
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (object);
196

197 198
  gimp_lut_free (tool->lut);

199
  G_OBJECT_CLASS (parent_class)->finalize (object);
Michael Natterer's avatar
Michael Natterer committed
200 201
}

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

  if (! drawable)
212
    return FALSE;
213

214
  if (gimp_drawable_is_indexed (drawable))
Michael Natterer's avatar
Michael Natterer committed
215
    {
216
      g_set_error (error, 0, 0,
217
                   _("Curves does not operate on indexed layers."));
218
      return FALSE;
219
    }
Michael Natterer's avatar
Michael Natterer committed
220

221
  gimp_config_reset (GIMP_CONFIG (c_tool->config));
222

223
  GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error);
224

225 226
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
227
                          GIMP_COLOR_TOOL_GET_OPTIONS (tool));
228

229
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
230
                                      curves_menu_sensitivity, drawable, NULL);
231

232 233
  histogram = gimp_histogram_new ();
  gimp_drawable_calculate_histogram (drawable, histogram);
234
  gimp_histogram_view_set_background (GIMP_HISTOGRAM_VIEW (c_tool->graph),
235 236
                                      histogram);
  gimp_histogram_unref (histogram);
237

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

static void
242 243 244 245 246 247
gimp_curves_tool_button_release (GimpTool              *tool,
                                 GimpCoords            *coords,
                                 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 & GDK_SHIFT_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 & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
267
    {
268
      gint i;
269

270 271
      for (i = 0; i < 5; i++)
        {
272
          GimpCurve *curve = config->curve[i];
273
          gdouble    value = c_tool->picked_color[i];
274
          gint       closest;
275

276
          closest = gimp_curve_get_closest_point (curve, value);
277

278
          gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
279
                                        closest);
280

281
          gimp_curve_set_point (curve, closest,
282
                                value, gimp_curve_map_value (curve, value));
283
        }
Michael Natterer's avatar
Michael Natterer committed
284 285
    }

286
  /*  chain up to halt the tool */
287 288
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Michael Natterer's avatar
Michael Natterer committed
289 290
}

291
static gboolean
292 293
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
294
                            GimpDisplay *display)
295
{
296 297 298
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);

  return gtk_widget_event (c_tool->graph, (GdkEvent *) kevent);
299 300 301 302 303 304
}

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

311
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
312
                                               display);
313

314
  gimp_tool_pop_status (tool, display);
315 316 317 318

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

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

329
  if (status && proximity)
330
    gimp_tool_push_status (tool, display, status);
331 332
}

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

343
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
344

345 346 347
  tool->picked_color[GIMP_HISTOGRAM_RED]   = color->r;
  tool->picked_color[GIMP_HISTOGRAM_GREEN] = color->g;
  tool->picked_color[GIMP_HISTOGRAM_BLUE]  = color->b;
348

349
  if (gimp_drawable_has_alpha (drawable))
350
    tool->picked_color[GIMP_HISTOGRAM_ALPHA] = color->a;
351

352 353
  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
354

355
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
356
                            tool->picked_color[tool->config->channel]);
357 358 359
}

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

  node = g_object_new (GEGL_TYPE_NODE,
                       "operation", "gimp-curves",
                       NULL);

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

372 373 374 375 376
  *config = G_OBJECT (tool->config);

  g_signal_connect_object (tool->config, "notify",
                           G_CALLBACK (gimp_curves_tool_config_notify),
                           tool, 0);
377 378 379 380 381 382

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

  return node;
383
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
384

385 386
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
387
{
388 389
  GimpCurvesTool *tool     = GIMP_CURVES_TOOL (image_map_tool);
  GimpDrawable   *drawable = image_map_tool->drawable;
390 391
  Curves          curves;

392 393
  gimp_curves_config_to_cruft (tool->config, &curves,
                               gimp_drawable_is_rgb (drawable));
394

395
  gimp_lut_setup (tool->lut,
396
                  (GimpLutFunc) curves_lut_func,
397
                  &curves,
398
                  gimp_drawable_bytes (drawable));
Elliot Lee's avatar
Elliot Lee committed
399 400
}

401

402 403 404
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
405

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

424
  vbox = image_map_tool->main_vbox;
Elliot Lee's avatar
Elliot Lee committed
425

426
  /*  The combo box for selecting channels  */
427
  hbox = gtk_hbox_new (FALSE, 6);
428 429 430
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

431
  label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
432 433 434
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

435 436 437
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
438 439
  tool->channel_menu =
    gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
440 441
  g_object_unref (store);

442 443 444
  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),
445
                                        "gimp-channel");
446 447
  gtk_box_pack_start (GTK_BOX (hbox), tool->channel_menu, FALSE, FALSE, 0);
  gtk_widget_show (tool->channel_menu);
448

449 450 451
  g_signal_connect (tool->channel_menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);
452

453
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->channel_menu);
454

455
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
456
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
457
  gtk_widget_show (button);
458

459
  g_signal_connect (button, "clicked",
460
                    G_CALLBACK (curves_channel_reset_callback),
461
                    tool);
Elliot Lee's avatar
Elliot Lee committed
462

463 464 465 466 467 468
  /*  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);
469

470
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
471
  table = gtk_table_new (2, 2, FALSE);
472 473
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
474 475 476 477 478
  gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 0);

  /*  The left color bar  */
  vbox2 = gtk_vbox_new (FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), vbox2, 0, 1, 0, 1,
479
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
480
  gtk_widget_show (vbox2);
Elliot Lee's avatar
Elliot Lee committed
481 482

  frame = gtk_frame_new (NULL);
483
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
484
  gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
485 486
  gtk_widget_show (frame);

487 488 489 490
  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);
491

Elliot Lee's avatar
Elliot Lee committed
492 493
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
494
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
495
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
496
                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
497
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
498

499
  tool->graph = gimp_curve_view_new ();
500 501 502 503
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
504 505 506
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
507 508
  gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph),
                             config->curve[config->channel]);
509 510
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
511

512
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
513 514 515 516 517
                                       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,
518
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
519
  gtk_widget_show (hbox2);
520

Elliot Lee's avatar
Elliot Lee committed
521
  frame = gtk_frame_new (NULL);
522
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
523
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
524 525
  gtk_widget_show (frame);

526 527 528 529
  vbox2 = gtk_vbox_new (TRUE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox2);
  gtk_widget_show (vbox2);

530 531 532 533
  tool->xrange = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_widget_set_size_request (tool->xrange, -1, BAR_SIZE / 2);
  gtk_box_pack_start (GTK_BOX (vbox2), tool->xrange, TRUE, TRUE, 0);
  gtk_widget_show (tool->xrange);
534

535 536 537 538
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_box_pack_start (GTK_BOX (vbox2), bar, TRUE, TRUE, 0);
  gtk_widget_show (bar);

539
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
540

541 542 543
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
544

545 546 547
  label = gtk_label_new_with_mnemonic (_("Curve _type:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
548

549 550 551 552 553 554 555 556
  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
557 558

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
Elliot Lee's avatar
Elliot Lee committed
559 560
}

561
static void
562
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
563
{
564 565
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (image_map_tool);
  GimpHistogramChannel  channel;
566

567 568 569 570 571 572
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
      gimp_curve_reset (tool->config->curve[channel], FALSE);
    }
573 574
}

575
static gboolean
576 577 578
gimp_curves_tool_settings_import (GimpImageMapTool  *image_map_tool,
                                  const gchar       *filename,
                                  GError           **error)
579
{
580
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
  FILE           *file;
  gboolean        success;

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

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

  fclose (file);
598

599
  return success;
600 601 602
}

static gboolean
603 604 605
gimp_curves_tool_settings_export (GimpImageMapTool  *image_map_tool,
                                  const gchar       *filename,
                                  GError           **error)
606
{
607
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
  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);
625

626
  return success;
627
}
628

Elliot Lee's avatar
Elliot Lee committed
629
static void
630 631 632
gimp_curves_tool_config_notify (GObject        *object,
                                GParamSpec     *pspec,
                                GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
633
{
634
  GimpCurvesConfig *config = GIMP_CURVES_CONFIG (object);
635
  GimpCurve        *curve  = config->curve[config->channel];
636

637
  if (! tool->xrange)
638 639
    return;

640
  if (! strcmp (pspec->name, "channel"))
641
    {
642 643 644
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                     config->channel);

645
      switch (config->channel)
646
        {
647 648 649 650
          guchar r[256];
          guchar g[256];
          guchar b[256];

651 652
        case GIMP_HISTOGRAM_VALUE:
        case GIMP_HISTOGRAM_ALPHA:
653
        case GIMP_HISTOGRAM_RGB:
654
          gimp_curve_get_uchar (curve, sizeof (r), r);
655

656
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
657
                                      r, r, r);
658
          break;
659

660 661 662
        case GIMP_HISTOGRAM_RED:
        case GIMP_HISTOGRAM_GREEN:
        case GIMP_HISTOGRAM_BLUE:
663 664 665 666 667 668
          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);
669

670
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
671
                                      r, g, b);
672 673
          break;
        }
674

675
      gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (tool->graph),
676
                                       config->channel);
677
      gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
678
                                tool->picked_color[config->channel]);
679

680 681
      gimp_color_bar_set_channel (GIMP_COLOR_BAR (tool->yrange),
                                  config->channel);
Elliot Lee's avatar
Elliot Lee committed
682

683 684
      gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph), curve);

685 686
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                     curve->curve_type);
687 688 689
    }
  else if (! strcmp (pspec->name, "curve"))
    {
690 691
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                     curve->curve_type);
692 693 694 695
    }

  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));
}
696

697 698 699 700 701 702 703 704 705 706 707 708 709
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);
710
    }
Elliot Lee's avatar
Elliot Lee committed
711 712 713
}

static void
714
curves_channel_reset_callback (GtkWidget      *widget,
715
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
716
{
717
  gimp_curve_reset (tool->config->curve[tool->config->channel], FALSE);
Elliot Lee's avatar
Elliot Lee committed
718 719
}

720
static gboolean
721 722
curves_menu_sensitivity (gint      value,
                         gpointer  data)
723
{
724 725
  GimpDrawable         *drawable = GIMP_DRAWABLE (data);
  GimpHistogramChannel  channel  = value;
Sven Neumann's avatar
Sven Neumann committed
726

727 728 729 730
  switch (channel)
    {
    case GIMP_HISTOGRAM_VALUE:
      return TRUE;
731

732 733 734
    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
735
      return gimp_drawable_is_rgb (drawable);
736

737
    case GIMP_HISTOGRAM_ALPHA:
738
      return gimp_drawable_has_alpha (drawable);
739 740 741

    case GIMP_HISTOGRAM_RGB:
      return FALSE;
742
    }
743

744 745 746
  return FALSE;
}

Elliot Lee's avatar
Elliot Lee committed
747
static void
748
curves_curve_type_callback (GtkWidget      *widget,
749
                            GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
750
{
751
  gint value;
Elliot Lee's avatar
Elliot Lee committed
752

753 754 755 756
  if (gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &value))
    {
      GimpCurvesConfig *config     = tool->config;
      GimpCurveType     curve_type = value;
Elliot Lee's avatar
Elliot Lee committed
757

758
      if (config->curve[config->channel]->curve_type != curve_type)
759
        gimp_curve_set_curve_type (config->curve[config->channel], curve_type);
760
    }
Elliot Lee's avatar
Elliot Lee committed
761
}