gimpcurvestool.c 41.7 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* The GIMP -- an image manipulation program
 * 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 21
#include "config.h"

#include <stdio.h>
22 23
#include <stdlib.h>
#include <string.h>
24

Sven Neumann's avatar
Sven Neumann committed
25
#include <gtk/gtk.h>
26
#include <gdk/gdkkeysyms.h>
Sven Neumann's avatar
Sven Neumann committed
27

28
#include "libgimpcolor/gimpcolor.h"
29
#include "libgimpwidgets/gimpwidgets.h"
30

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

33
#include "config/gimpguiconfig.h"
34

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

39
#include "core/gimp.h"
Michael Natterer's avatar
Michael Natterer committed
40
#include "core/gimpdrawable.h"
41
#include "core/gimpdrawable-histogram.h"
Michael Natterer's avatar
Michael Natterer committed
42
#include "core/gimpimage.h"
43
#include "core/gimpimagemap.h"
44
#include "core/gimpprogress.h"
45
#include "core/gimptoolinfo.h"
Michael Natterer's avatar
Michael Natterer committed
46

47
#include "widgets/gimpcolorbar.h"
Michael Natterer's avatar
Michael Natterer committed
48
#include "widgets/gimpcursor.h"
49
#include "widgets/gimphelp-ids.h"
50
#include "widgets/gimphistogramview.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"
56
#include "gimptoolcontrol.h"
Sven Neumann's avatar
Sven Neumann committed
57

58
#include "gimp-intl.h"
59

60

61 62
#define XRANGE   (1 << 0)
#define YRANGE   (1 << 1)
63 64
#define GRAPH    (1 << 2)
#define ALL      (XRANGE | YRANGE | GRAPH)
Elliot Lee's avatar
Elliot Lee committed
65

66 67
#define GRAPH_SIZE    256
#define BAR_SIZE       12
68
#define RADIUS          4
69
#define MIN_DISTANCE    8
Elliot Lee's avatar
Elliot Lee committed
70 71


Michael Natterer's avatar
Michael Natterer committed
72 73
/*  local function prototypes  */

74 75 76
static void     gimp_curves_tool_finalize       (GObject          *object);

static gboolean gimp_curves_tool_initialize     (GimpTool         *tool,
77
                                                 GimpDisplay      *display);
78
static void     gimp_curves_tool_button_release (GimpTool         *tool,
79
                                                 GimpCoords       *coords,
80
                                                 guint32           time,
81
                                                 GdkModifierType   state,
82
                                                 GimpDisplay      *display);
83 84
static gboolean gimp_curves_tool_key_press      (GimpTool         *tool,
                                                 GdkEventKey      *kevent,
85
                                                 GimpDisplay      *display);
86
static void     gimp_curves_tool_oper_update    (GimpTool         *tool,
87 88
                                                 GimpCoords       *coords,
                                                 GdkModifierType   state,
89
                                                 gboolean          proximity,
90
                                                 GimpDisplay      *display);
91 92

static void     gimp_curves_tool_color_picked   (GimpColorTool    *color_tool,
93
                                                 GimpColorPickState pick_state,
94 95 96 97 98 99
                                                 GimpImageType     sample_type,
                                                 GimpRGB          *color,
                                                 gint              color_index);
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);
100 101 102 103
static gboolean gimp_curves_tool_settings_load  (GimpImageMapTool *image_map_tool,
                                                 gpointer          fp);
static gboolean gimp_curves_tool_settings_save  (GimpImageMapTool *image_map_tool,
                                                 gpointer          fp);
104

105
static void     curves_add_point                (GimpCurvesTool   *tool,
106 107 108
                                                 gint              channel);
static gboolean curves_key_press                (GimpCurvesTool   *tool,
                                                 GdkEventKey      *kevent);
109
static void     curves_update                   (GimpCurvesTool   *tool,
110 111 112
                                                 gint              update);

static void     curves_channel_callback         (GtkWidget        *widget,
113
                                                 GimpCurvesTool   *tool);
114
static void     curves_channel_reset_callback   (GtkWidget        *widget,
115
                                                 GimpCurvesTool   *tool);
116

117
static gboolean curves_menu_sensitivity         (gint              value,
Sven Neumann's avatar
Sven Neumann committed
118
                                                 gpointer          data);
119

120
static void     curves_curve_type_callback      (GtkWidget        *widget,
121
                                                 GimpCurvesTool   *tool);
122 123
static gboolean curves_graph_events             (GtkWidget        *widget,
                                                 GdkEvent         *event,
124
                                                 GimpCurvesTool   *tool);
125 126
static gboolean curves_graph_expose             (GtkWidget        *widget,
                                                 GdkEventExpose   *eevent,
127
                                                 GimpCurvesTool   *tool);
128

Michael Natterer's avatar
Michael Natterer committed
129

130
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
131 132

#define parent_class gimp_curves_tool_parent_class
133

Michael Natterer's avatar
Michael Natterer committed
134

135
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
136 137

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

154 155 156

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
157 158 159
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  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);

  object_class->finalize           = gimp_curves_tool_finalize;

  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;

  color_tool_class->picked         = gimp_curves_tool_color_picked;

  im_tool_class->shell_desc        = _("Adjust Color Curves");
  im_tool_class->settings_name     = "curves";
  im_tool_class->load_dialog_title = _("Load Curves");
  im_tool_class->load_button_tip   = _("Load curves settings from file");
  im_tool_class->save_dialog_title = _("Save Curves");
  im_tool_class->save_button_tip   = _("Save curves settings to file");

  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_load     = gimp_curves_tool_settings_load;
  im_tool_class->settings_save     = gimp_curves_tool_settings_save;
Michael Natterer's avatar
Michael Natterer committed
186 187 188
}

static void
189
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
190
{
191
  gint i;
192

193 194 195
  tool->curves  = g_new0 (Curves, 1);
  tool->lut     = gimp_lut_new ();
  tool->channel = GIMP_HISTOGRAM_VALUE;
196

197
  curves_init (tool->curves);
198

199 200
  for (i = 0; i < G_N_ELEMENTS (tool->col_value); i++)
    tool->col_value[i] = -1;
201

202 203
  tool->cursor_x = -1;
  tool->cursor_y = -1;
Michael Natterer's avatar
Michael Natterer committed
204 205 206
}

static void
207
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
208
{
209
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (object);
210

211
  if (tool->curves)
Michael Natterer's avatar
Michael Natterer committed
212
    {
213 214
      g_free (tool->curves);
      tool->curves = NULL;
Michael Natterer's avatar
Michael Natterer committed
215
    }
216
  if (tool->lut)
Michael Natterer's avatar
Michael Natterer committed
217
    {
218 219
      gimp_lut_free (tool->lut);
      tool->lut = NULL;
Michael Natterer's avatar
Michael Natterer committed
220
    }
221
  if (tool->hist)
222
    {
223 224
      gimp_histogram_free (tool->hist);
      tool->hist = NULL;
225
    }
226
  if (tool->cursor_layout)
Michael Natterer's avatar
Michael Natterer committed
227
    {
228 229
      g_object_unref (tool->cursor_layout);
      tool->cursor_layout = NULL;
Michael Natterer's avatar
Michael Natterer committed
230
    }
231
  if (tool->xpos_layout)
232
    {
233 234
      g_object_unref (tool->xpos_layout);
      tool->xpos_layout = NULL;
235
    }
Michael Natterer's avatar
Michael Natterer committed
236

237
  G_OBJECT_CLASS (parent_class)->finalize (object);
Michael Natterer's avatar
Michael Natterer committed
238 239
}

240
static gboolean
241
gimp_curves_tool_initialize (GimpTool    *tool,
242
                             GimpDisplay *display)
Michael Natterer's avatar
Michael Natterer committed
243
{
244
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
245 246
  GimpDrawable   *drawable;

247
  drawable = gimp_image_active_drawable (display->image);
248 249

  if (! drawable)
250
    return FALSE;
251

252
  if (gimp_drawable_is_indexed (drawable))
Michael Natterer's avatar
Michael Natterer committed
253
    {
254 255
      gimp_message (display->image->gimp, GIMP_PROGRESS (display),
                    _("Curves for indexed layers cannot be adjusted."));
256
      return FALSE;
257
    }
Michael Natterer's avatar
Michael Natterer committed
258

259
  if (! c_tool->hist)
260
    c_tool->hist = gimp_histogram_new ();
261

262
  curves_init (c_tool->curves);
Michael Natterer's avatar
Michael Natterer committed
263

264
  c_tool->channel = GIMP_HISTOGRAM_VALUE;
265 266
  c_tool->color   = gimp_drawable_is_rgb (drawable);
  c_tool->alpha   = gimp_drawable_has_alpha (drawable);
267

268 269 270
  c_tool->selected = 0;
  c_tool->grabbed  = FALSE;
  c_tool->last     = 0;
271

272
  GIMP_TOOL_CLASS (parent_class)->initialize (tool, display);
273

274 275 276 277
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
                          GIMP_COLOR_OPTIONS (tool->tool_info->tool_options));

278 279
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                      curves_menu_sensitivity, c_tool, NULL);
280

281 282
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                 c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
283

284 285 286
  /* FIXME: hack */
  if (! c_tool->color)
    c_tool->channel = (c_tool->channel == GIMP_HISTOGRAM_ALPHA) ? 1 : 0;
287

288 289 290
  gimp_drawable_calculate_histogram (drawable, c_tool->hist);
  gimp_histogram_view_set_histogram (GIMP_HISTOGRAM_VIEW (c_tool->graph),
                                     c_tool->hist);
291

292 293
  curves_update (c_tool, ALL);

294
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
295 296 297
}

static void
298 299 300
gimp_curves_tool_button_release (GimpTool        *tool,
                                 GimpCoords      *coords,
                                 guint32          time,
301
                                 GdkModifierType  state,
302
                                 GimpDisplay     *display)
Michael Natterer's avatar
Michael Natterer committed
303
{
304
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
305
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
306

307
  drawable = gimp_image_active_drawable (display->image);
Michael Natterer's avatar
Michael Natterer committed
308

309
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
310
    {
311
      curves_add_point (c_tool, c_tool->channel);
312
      curves_calculate_curve (c_tool->curves, c_tool->channel);
313
      curves_update (c_tool, GRAPH | XRANGE);
Michael Natterer's avatar
Michael Natterer committed
314
    }
315
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
316
    {
317
      gint i;
318

319 320
      for (i = 0; i < 5; i++)
        {
321
          curves_add_point (c_tool, i);
322 323
          curves_calculate_curve (c_tool->curves, c_tool->channel);
        }
324

325
      curves_update (c_tool, GRAPH | XRANGE);
Michael Natterer's avatar
Michael Natterer committed
326 327
    }

328 329
  /*  chain up to halt the tool */
  GIMP_TOOL_CLASS (parent_class)->button_release (tool,
330
                                                  coords, time, state, display);
Michael Natterer's avatar
Michael Natterer committed
331 332
}

333 334 335
gboolean
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
336
                            GimpDisplay *display)
337 338 339 340 341 342 343 344
{
  return curves_key_press (GIMP_CURVES_TOOL (tool), kevent);
}

static void
gimp_curves_tool_oper_update (GimpTool        *tool,
                              GimpCoords      *coords,
                              GdkModifierType  state,
345
                              gboolean         proximity,
346
                              GimpDisplay     *display)
347 348 349 350
{
  GimpColorPickMode  mode   = GIMP_COLOR_PICK_MODE_NONE;
  const gchar       *status = NULL;

351
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
352
                                               display);
353

354
  gimp_tool_pop_status (tool, display);
355 356 357 358 359 360 361 362 363 364 365 366 367 368

  if (state & GDK_SHIFT_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
      status = _("Click to add a control point.");
    }
  else if (state & GDK_CONTROL_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
      status = _("Click to add control points to all channels.");
    }

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

369
  if (status && proximity)
370
    gimp_tool_push_status (tool, display, status);
371 372
}

Michael Natterer's avatar
Michael Natterer committed
373
static void
374 375
gimp_curves_tool_color_picked (GimpColorTool      *color_tool,
                               GimpColorPickState  pick_state,
376 377 378
                               GimpImageType       sample_type,
                               GimpRGB            *color,
                               gint                color_index)
Michael Natterer's avatar
Michael Natterer committed
379
{
380
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (color_tool);
381
  GimpDrawable   *drawable;
382
  guchar          r, g, b, a;
Michael Natterer's avatar
Michael Natterer committed
383

384
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
385

386
  gimp_rgba_get_uchar (color, &r, &g, &b, &a);
387

388 389 390
  tool->col_value[GIMP_HISTOGRAM_RED]   = r;
  tool->col_value[GIMP_HISTOGRAM_GREEN] = g;
  tool->col_value[GIMP_HISTOGRAM_BLUE]  = b;
391

392
  if (gimp_drawable_has_alpha (drawable))
393
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = a;
394

395
  if (gimp_drawable_is_indexed (drawable))
396
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = color_index;
397

398
  tool->col_value[GIMP_HISTOGRAM_VALUE] = MAX (MAX (r, g), b);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
399

400
  curves_update (tool, GRAPH);
401
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
402

403
static void
404
curves_add_point (GimpCurvesTool *tool,
405
                  gint            channel)
406 407
{
  /* Add point onto the curve */
408 409 410 411
  gint closest_point = 0;
  gint distance;
  gint curvex;
  gint i;
412

413
  switch (tool->curves->curve_type[channel])
414
    {
415
    case GIMP_CURVE_SMOOTH:
416
      curvex   = tool->col_value[channel];
417
      distance = G_MAXINT;
418

419
      for (i = 0; i < CURVES_NUM_POINTS; i++)
420
        {
421 422
          if (tool->curves->points[channel][i][0] != -1)
            if (abs (curvex - tool->curves->points[channel][i][0]) < distance)
423
              {
424
                distance = abs (curvex - tool->curves->points[channel][i][0]);
425 426 427
                closest_point = i;
              }
        }
428

429
      if (distance > MIN_DISTANCE)
430
        closest_point = (curvex + 8) / 16;
431

432 433 434 435
      tool->curves->points[channel][closest_point][0] = curvex;
      tool->curves->points[channel][closest_point][1] = tool->curves->curve[channel][curvex];

      tool->selected = closest_point;
436
      break;
437

438
    case GIMP_CURVE_FREE:
439
      /* do nothing for free form curves */
440
      break;
441
    }
Elliot Lee's avatar
Elliot Lee committed
442 443
}

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
static gboolean
curves_key_press (GimpCurvesTool *tool,
                  GdkEventKey    *kevent)
{
  gint     i      = tool->selected;
  gint     y      = tool->curves->points[tool->channel][i][1];
  gboolean update = FALSE;

  if (tool->grabbed ||
      tool->curves->curve_type[tool->channel] == GIMP_CURVE_FREE)
    return FALSE;

  switch (kevent->keyval)
    {
    case GDK_Left:
      for (i = i - 1; i >= 0 && !update; i--)
        {
          if (tool->curves->points[tool->channel][i][0] != -1)
            {
              tool->selected = i;
              update = TRUE;
            }
        }
      break;

    case GDK_Right:
      for (i = i + 1; i < CURVES_NUM_POINTS && !update; i++)
        {
          if (tool->curves->points[tool->channel][i][0] != -1)
            {
              tool->selected = i;
              update = TRUE;
            }
        }
      break;

    case GDK_Up:
      if (y < 255)
        {
          y = y + (kevent->state & GDK_SHIFT_MASK ? 16 : 1);
          tool->curves->points[tool->channel][i][1] = MIN (y, 255);
          curves_calculate_curve (tool->curves, tool->channel);
          update = TRUE;
        }
      break;

    case GDK_Down:
      if (y > 0)
        {
          y = y - (kevent->state & GDK_SHIFT_MASK ? 16 : 1);
          tool->curves->points[tool->channel][i][1] = MAX (y, 0);
          curves_calculate_curve (tool->curves, tool->channel);
          update = TRUE;
        }
      break;

    default:
      break;
    }

  if (update)
    {
      curves_update (tool, GRAPH);
      gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));
    }

  return update;
}

513 514
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
515
{
516
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
517

518
  gimp_lut_setup (tool->lut,
519
                  (GimpLutFunc) curves_lut_func,
520
                  tool->curves,
521
                  gimp_drawable_bytes (image_map_tool->drawable));
522

523
  gimp_image_map_apply (image_map_tool->image_map,
524
                        (GimpImageMapApplyFunc) gimp_lut_process,
525
                        tool->lut);
Elliot Lee's avatar
Elliot Lee committed
526 527
}

528

529 530 531
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
532

533 534
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
535
{
536
  GimpCurvesTool  *tool = GIMP_CURVES_TOOL (image_map_tool);
537
  GimpToolOptions *tool_options;
538
  GtkListStore    *store;
539
  GtkWidget       *vbox;
540 541 542 543 544
  GtkWidget       *vbox2;
  GtkWidget       *hbox;
  GtkWidget       *hbox2;
  GtkWidget       *label;
  GtkWidget       *bbox;
545 546 547 548
  GtkWidget       *frame;
  GtkWidget       *menu;
  GtkWidget       *table;
  GtkWidget       *button;
549
  GtkWidget       *bar;
550
  gint             padding;
551

552 553
  tool_options = GIMP_TOOL (tool)->tool_info->tool_options;

554
  vbox = image_map_tool->main_vbox;
Elliot Lee's avatar
Elliot Lee committed
555

556
  /*  The option menu for selecting channels  */
557
  hbox = gtk_hbox_new (FALSE, 6);
558 559 560
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

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

565 566 567
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
568
  menu = gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
569 570
  g_object_unref (store);

571 572 573 574 575
  g_signal_connect (menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);
  gimp_enum_combo_box_set_stock_prefix (GIMP_ENUM_COMBO_BOX (menu),
                                        "gimp-channel");
576 577 578
  gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);

579
  tool->channel_menu = menu;
580

581 582
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), menu);

583
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
584
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
585
  gtk_widget_show (button);
586

587
  g_signal_connect (button, "clicked",
588
                    G_CALLBACK (curves_channel_reset_callback),
589
                    tool);
Elliot Lee's avatar
Elliot Lee committed
590

591 592 593 594 595
  menu = gimp_prop_enum_stock_box_new (G_OBJECT (tool_options),
                                       "histogram-scale", "gimp-histogram",
                                       0, 0);
  gtk_box_pack_end (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);
596

597
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
598
  table = gtk_table_new (2, 2, FALSE);
599 600
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
601 602 603 604 605
  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,
606
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
607
  gtk_widget_show (vbox2);
Elliot Lee's avatar
Elliot Lee committed
608 609

  frame = gtk_frame_new (NULL);
610
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
611
  gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
612 613
  gtk_widget_show (frame);

614 615 616 617
  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);
618

Elliot Lee's avatar
Elliot Lee committed
619 620
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
621
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
622
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
623
                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
624
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
625

626 627 628 629
  tool->graph = gimp_histogram_view_new (FALSE);
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
630
  GTK_WIDGET_SET_FLAGS (tool->graph, GTK_CAN_FOCUS);
631 632 633
  gtk_widget_add_events (tool->graph, (GDK_BUTTON_PRESS_MASK   |
                                       GDK_BUTTON_RELEASE_MASK |
                                       GDK_POINTER_MOTION_MASK |
634
                                       GDK_KEY_PRESS_MASK      |
635 636
                                       GDK_LEAVE_NOTIFY_MASK));
  g_object_set (tool->graph,
637 638 639
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
640 641 642
  GIMP_HISTOGRAM_VIEW (tool->graph)->light_histogram = TRUE;
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
643

644
  g_signal_connect (tool->graph, "event",
645 646
                    G_CALLBACK (curves_graph_events),
                    tool);
647
  g_signal_connect_after (tool->graph, "expose-event",
648
                          G_CALLBACK (curves_graph_expose),
649
                          tool);
Elliot Lee's avatar
Elliot Lee committed
650

651 652

  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
653 654 655 656 657
                                       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,
658
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
659
  gtk_widget_show (hbox2);
660

Elliot Lee's avatar
Elliot Lee committed
661
  frame = gtk_frame_new (NULL);
662
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
663
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
664 665
  gtk_widget_show (frame);

666 667 668 669
  vbox2 = gtk_vbox_new (TRUE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox2);
  gtk_widget_show (vbox2);

670 671 672 673
  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);
674

675 676 677 678
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_box_pack_start (GTK_BOX (vbox2), bar, TRUE, TRUE, 0);
  gtk_widget_show (bar);

679
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
680

681

682 683 684
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
685

686
  /*  Horizontal button box for load / save */
687
  frame = gimp_frame_new (_("All Channels"));
688
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
689 690
  gtk_widget_show (frame);

691 692
  bbox = gtk_hbutton_box_new ();
  gtk_box_set_spacing (GTK_BOX (bbox), 4);
693
  gtk_container_add (GTK_CONTAINER (frame), bbox);
694
  gtk_widget_show (bbox);
695

696 697 698
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->load_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->load_button);
699

700 701 702
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->save_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->save_button);
703 704

  /*  The radio box for selecting the curve type  */
705
  frame = gimp_frame_new (_("Curve Type"));
706 707 708 709
  gtk_box_pack_end (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  hbox = gimp_enum_stock_box_new (GIMP_TYPE_CURVE_TYPE,
710 711 712 713
                                  "gimp-curve", GTK_ICON_SIZE_MENU,
                                  G_CALLBACK (curves_curve_type_callback),
                                  tool,
                                  &tool->curve_type);
714

715
  gtk_widget_style_get (bbox, "child-internal-pad-x", &padding, NULL);
716 717

  gimp_enum_stock_box_set_child_padding (hbox, padding, -1);
718 719 720 721 722 723

  gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
  gtk_box_set_spacing (GTK_BOX (hbox), 4);

  gtk_container_add (GTK_CONTAINER (frame), hbox);
  gtk_widget_show (hbox);
Elliot Lee's avatar
Elliot Lee committed
724 725
}

726
static void
727
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
728
{
729
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (image_map_tool);
730
  GimpHistogramChannel  channel;
731

732
  tool->grabbed = FALSE;
733

734 735 736
  for (channel =  GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
737
    curves_channel_reset (tool->curves, channel);
738

739
  curves_update (tool, XRANGE | GRAPH);
740 741
}

742
static gboolean
743 744
gimp_curves_tool_settings_load (GimpImageMapTool *image_map_tool,
                                gpointer          fp)
745
{
746 747 748 749 750
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  FILE           *file = fp;
  gint            i, j;
  gint            fields;
  gchar           buf[50];
751 752
  gint            index[5][CURVES_NUM_POINTS];
  gint            value[5][CURVES_NUM_POINTS];
753 754 755 756 757 758 759 760 761

  if (! fgets (buf, sizeof (buf), file))
    return FALSE;

  if (strcmp (buf, "# GIMP Curves File\n") != 0)
    return FALSE;

  for (i = 0; i < 5; i++)
    {
762
      for (j = 0; j < CURVES_NUM_POINTS; j++)
763 764 765 766
        {
          fields = fscanf (file, "%d %d ", &index[i][j], &value[i][j]);
          if (fields != 2)
            {
767
              /*  FIXME: should have a helpful error message here  */
768 769 770 771
              g_printerr ("fields != 2");
              return FALSE;
            }
        }
772 773 774 775 776 777
    }

  for (i = 0; i < 5; i++)
    {
      tool->curves->curve_type[i] = GIMP_CURVE_SMOOTH;

778
      for (j = 0; j < CURVES_NUM_POINTS; j++)
779 780 781 782
        {
          tool->curves->points[i][j][0] = index[i][j];
          tool->curves->points[i][j][1] = value[i][j];
        }
783 784 785 786 787 788 789 790
    }

  for (i = 0; i < 5; i++)
    curves_calculate_curve (tool->curves, i);

  curves_update (tool, ALL);

  gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (tool->curve_type),
791
                                   GIMP_CURVE_SMOOTH);
792 793

  return TRUE;
794 795 796
}

static gboolean
797 798
gimp_curves_tool_settings_save (GimpImageMapTool *image_map_tool,
                                gpointer          fp)
799
{
800 801 802 803 804 805 806 807
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  FILE           *file = fp;
  gint            i, j;
  gint32          index;

  for (i = 0; i < 5; i++)
    if (tool->curves->curve_type[i] == GIMP_CURVE_FREE)
      {
808
        /*  pick representative points from the curve
809
            and make them control points  */
810 811 812 813 814 815
        for (j = 0; j <= 8; j++)
          {
            index = CLAMP0255 (j * 32);
            tool->curves->points[i][j * 2][0] = index;
            tool->curves->points[i][j * 2][1] = tool->curves->curve[i][index];
          }
816 817 818 819 820 821
      }

  fprintf (file, "# GIMP Curves File\n");

  for