gimpcurvestool.c 38.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
#include <errno.h>
25

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

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

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

34 35
#include "config/gimpbaseconfig.h"

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

40
#include "core/gimp.h"
Michael Natterer's avatar
Michael Natterer committed
41
#include "core/gimpdrawable.h"
42
#include "core/gimpdrawable-histogram.h"
Michael Natterer's avatar
Michael Natterer committed
43
#include "core/gimpimage.h"
44
#include "core/gimpimagemap.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/gimpenummenu.h"
50
#include "widgets/gimphelp-ids.h"
51
#include "widgets/gimphistogramview.h"
Michael Natterer's avatar
Michael Natterer committed
52

53 54
#include "display/gimpdisplay.h"

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

59
#include "gimp-intl.h"
60

61

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

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


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

75
static void     gimp_curves_tool_class_init     (GimpCurvesToolClass *klass);
76
static void     gimp_curves_tool_init           (GimpCurvesTool      *tool);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

static void     gimp_curves_tool_finalize       (GObject          *object);

static gboolean gimp_curves_tool_initialize     (GimpTool         *tool,
                                                 GimpDisplay      *gdisp);
static void     gimp_curves_tool_button_release (GimpTool         *tool,
                                                 GimpCoords       *coords,
                                                 guint32           time,
                                                 GdkModifierType   state,
                                                 GimpDisplay      *gdisp);

static void     gimp_curves_tool_color_picked   (GimpColorTool    *color_tool,
                                                 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);

96
static void     curves_add_point                (GimpCurvesTool   *tool,
97 98 99 100
                                                 gint              x,
                                                 gint              y,
                                                 gint              cchan);

101
static void     curves_update                   (GimpCurvesTool   *tool,
102 103 104
                                                 gint              update);

static void     curves_channel_callback         (GtkWidget        *widget,
105
                                                 GimpCurvesTool   *tool);
106
static void     curves_channel_reset_callback   (GtkWidget        *widget,
107
                                                 GimpCurvesTool   *tool);
108 109

static gboolean curves_set_sensitive_callback   (GimpHistogramChannel channel,
110
                                                 GimpCurvesTool   *tool);
111
static void     curves_curve_type_callback      (GtkWidget        *widget,
112
                                                 GimpCurvesTool   *tool);
113
static void     curves_load_callback            (GtkWidget        *widget,
114
                                                 GimpCurvesTool   *tool);
115
static void     curves_save_callback            (GtkWidget        *widget,
116
                                                 GimpCurvesTool   *tool);
117 118
static gboolean curves_graph_events             (GtkWidget        *widget,
                                                 GdkEvent         *event,
119
                                                 GimpCurvesTool   *tool);
120 121
static gboolean curves_graph_expose             (GtkWidget        *widget,
                                                 GdkEventExpose   *eevent,
122
                                                 GimpCurvesTool   *tool);
123

124
static void     file_dialog_create              (GimpCurvesTool   *tool);
125 126
static void     file_dialog_response            (GtkWidget        *dialog,
                                                 gint              response_id,
127
                                                 GimpCurvesTool   *tool);
128

129
static gboolean curves_read_from_file           (GimpCurvesTool   *tool,
130
                                                 FILE             *file);
131
static void     curves_write_to_file            (GimpCurvesTool   *tool,
132
                                                 FILE             *file);
133

Michael Natterer's avatar
Michael Natterer committed
134

135 136
static GimpImageMapToolClass *parent_class = NULL;

Michael Natterer's avatar
Michael Natterer committed
137

138
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
139 140

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

157
GType
Michael Natterer's avatar
Michael Natterer committed
158 159
gimp_curves_tool_get_type (void)
{
160
  static GType tool_type = 0;
Michael Natterer's avatar
Michael Natterer committed
161 162 163

  if (! tool_type)
    {
164
      static const GTypeInfo tool_info =
Michael Natterer's avatar
Michael Natterer committed
165 166
      {
        sizeof (GimpCurvesToolClass),
167 168 169 170 171 172 173 174
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) gimp_curves_tool_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data     */
	sizeof (GimpCurvesTool),
	0,              /* n_preallocs    */
	(GInstanceInitFunc) gimp_curves_tool_init,
Michael Natterer's avatar
Michael Natterer committed
175 176
      };

177
      tool_type = g_type_register_static (GIMP_TYPE_IMAGE_MAP_TOOL,
178
					  "GimpCurvesTool",
179
                                          &tool_info, 0);
Michael Natterer's avatar
Michael Natterer committed
180 181 182 183 184
    }

  return tool_type;
}

185 186 187

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
188 189 190
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
191 192
  GObjectClass          *object_class;
  GimpToolClass         *tool_class;
193
  GimpColorToolClass    *color_tool_class;
194
  GimpImageMapToolClass *image_map_tool_class;
Michael Natterer's avatar
Michael Natterer committed
195

196 197
  object_class         = G_OBJECT_CLASS (klass);
  tool_class           = GIMP_TOOL_CLASS (klass);
198
  color_tool_class     = GIMP_COLOR_TOOL_CLASS (klass);
199
  image_map_tool_class = GIMP_IMAGE_MAP_TOOL_CLASS (klass);
Michael Natterer's avatar
Michael Natterer committed
200

201
  parent_class = g_type_class_peek_parent (klass);
Michael Natterer's avatar
Michael Natterer committed
202

203
  object_class->finalize       = gimp_curves_tool_finalize;
204

205 206 207 208
  tool_class->initialize       = gimp_curves_tool_initialize;
  tool_class->button_release   = gimp_curves_tool_button_release;

  color_tool_class->picked     = gimp_curves_tool_color_picked;
209 210 211 212

  image_map_tool_class->map    = gimp_curves_tool_map;
  image_map_tool_class->dialog = gimp_curves_tool_dialog;
  image_map_tool_class->reset  = gimp_curves_tool_reset;
Michael Natterer's avatar
Michael Natterer committed
213 214 215
}

static void
216
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
217
{
218
  GimpImageMapTool *image_map_tool = GIMP_IMAGE_MAP_TOOL (tool);
219 220
  gint              i;

221
  image_map_tool->shell_desc = _("Adjust Color Curves");
222

223 224 225
  tool->curves  = g_new0 (Curves, 1);
  tool->lut     = gimp_lut_new ();
  tool->channel = GIMP_HISTOGRAM_VALUE;
226

227
  curves_init (tool->curves);
228

229 230
  for (i = 0; i < G_N_ELEMENTS (tool->col_value); i++)
    tool->col_value[i] = -1;
231

232 233
  tool->cursor_x = -1;
  tool->cursor_y = -1;
Michael Natterer's avatar
Michael Natterer committed
234 235 236
}

static void
237
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
238
{
239
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (object);
240

241
  if (tool->curves)
Michael Natterer's avatar
Michael Natterer committed
242
    {
243 244
      g_free (tool->curves);
      tool->curves = NULL;
Michael Natterer's avatar
Michael Natterer committed
245
    }
246
  if (tool->lut)
Michael Natterer's avatar
Michael Natterer committed
247
    {
248 249
      gimp_lut_free (tool->lut);
      tool->lut = NULL;
Michael Natterer's avatar
Michael Natterer committed
250
    }
251
  if (tool->hist)
252
    {
253 254
      gimp_histogram_free (tool->hist);
      tool->hist = NULL;
255
    }
256
  if (tool->cursor_layout)
Michael Natterer's avatar
Michael Natterer committed
257
    {
258 259
      g_object_unref (tool->cursor_layout);
      tool->cursor_layout = NULL;
Michael Natterer's avatar
Michael Natterer committed
260
    }
261
  if (tool->xpos_layout)
262
    {
263 264
      g_object_unref (tool->xpos_layout);
      tool->xpos_layout = NULL;
265
    }
Michael Natterer's avatar
Michael Natterer committed
266

267
  G_OBJECT_CLASS (parent_class)->finalize (object);
Michael Natterer's avatar
Michael Natterer committed
268 269
}

270
static gboolean
271 272
gimp_curves_tool_initialize (GimpTool    *tool,
			     GimpDisplay *gdisp)
Michael Natterer's avatar
Michael Natterer committed
273
{
274
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
275 276
  GimpDrawable   *drawable;

277 278 279
  drawable = gimp_image_active_drawable (gdisp->gimage);

  if (! drawable)
280
    return FALSE;
281

282
  if (gimp_drawable_is_indexed (drawable))
Michael Natterer's avatar
Michael Natterer committed
283
    {
284
      g_message (_("Curves for indexed layers cannot be adjusted."));
285
      return FALSE;
286
    }
Michael Natterer's avatar
Michael Natterer committed
287

288 289 290 291 292 293 294
  if (! c_tool->hist)
    {
      Gimp *gimp = GIMP_TOOL (c_tool)->tool_info->gimp;

      c_tool->hist = gimp_histogram_new (GIMP_BASE_CONFIG (gimp->config));
    }

295
  curves_init (c_tool->curves);
Michael Natterer's avatar
Michael Natterer committed
296

297
  c_tool->channel = GIMP_HISTOGRAM_VALUE;
298 299
  c_tool->color   = gimp_drawable_is_rgb (drawable);
  c_tool->alpha   = gimp_drawable_has_alpha (drawable);
300 301 302 303 304 305

  c_tool->grab_point = -1;
  c_tool->last       = 0;

  GIMP_TOOL_CLASS (parent_class)->initialize (tool, gdisp);

306 307 308 309
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
                          GIMP_COLOR_OPTIONS (tool->tool_info->tool_options));

310
  /* set the sensitivity of the channel menu based on the drawable type */
311 312 313
  gimp_int_option_menu_set_sensitive (GTK_OPTION_MENU (c_tool->channel_menu),
                                      (GimpIntOptionMenuSensitivityCallback) curves_set_sensitive_callback,
                                      c_tool);
314 315

  /* set the current selection */
316 317
  gimp_int_option_menu_set_history (GTK_OPTION_MENU (c_tool->channel_menu),
                                    c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
318

319 320 321
  if (! c_tool->color && c_tool->alpha)
    c_tool->channel = 1;

322 323 324
  gimp_drawable_calculate_histogram (drawable, c_tool->hist);
  gimp_histogram_view_set_histogram (GIMP_HISTOGRAM_VIEW (c_tool->graph),
                                     c_tool->hist);
325

326 327
  curves_update (c_tool, ALL);

328
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
329 330 331
}

static void
332 333 334 335 336
gimp_curves_tool_button_release (GimpTool        *tool,
                                 GimpCoords      *coords,
                                 guint32          time,
				 GdkModifierType  state,
				 GimpDisplay     *gdisp)
Michael Natterer's avatar
Michael Natterer committed
337
{
338
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
339
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
340

341
  drawable = gimp_image_active_drawable (gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
342

343
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
344
    {
345
      curves_add_point (c_tool, coords->x, coords->y, c_tool->channel);
346
      curves_calculate_curve (c_tool->curves, c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
347
    }
348
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
349
    {
350
      gint i;
351

352 353 354 355 356
      for (i = 0; i < 5; i++)
        {
          curves_add_point (c_tool, coords->x, coords->y, i);
          curves_calculate_curve (c_tool->curves, c_tool->channel);
        }
Michael Natterer's avatar
Michael Natterer committed
357 358
    }

359 360 361
  /*  chain up to halt the tool */
  GIMP_TOOL_CLASS (parent_class)->button_release (tool,
                                                  coords, time, state, gdisp);
Michael Natterer's avatar
Michael Natterer committed
362 363 364
}

static void
365 366 367 368
gimp_curves_tool_color_picked (GimpColorTool *color_tool,
			       GimpImageType  sample_type,
			       GimpRGB       *color,
			       gint           color_index)
Michael Natterer's avatar
Michael Natterer committed
369
{
370
  GimpCurvesTool *tool;
371
  GimpDrawable   *drawable;
372
  guchar          r, g, b, a;
Michael Natterer's avatar
Michael Natterer committed
373

374 375
  tool = GIMP_CURVES_TOOL (color_tool);
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
376

377
  gimp_rgba_get_uchar (color, &r, &g, &b, &a);
378

379 380 381
  tool->col_value[GIMP_HISTOGRAM_RED]   = r;
  tool->col_value[GIMP_HISTOGRAM_GREEN] = g;
  tool->col_value[GIMP_HISTOGRAM_BLUE]  = b;
382

383
  if (gimp_drawable_has_alpha (drawable))
384
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = a;
385

386
  if (gimp_drawable_is_indexed (drawable))
387
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = color_index;
388

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

391
  curves_update (tool, GRAPH);
392
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
393

394
static void
395
curves_add_point (GimpCurvesTool *tool,
396 397 398
		  gint            x,
		  gint            y,
		  gint            cchan)
399 400
{
  /* Add point onto the curve */
401 402 403 404
  gint closest_point = 0;
  gint distance;
  gint curvex;
  gint i;
405

406
  switch (tool->curves->curve_type[cchan])
407
    {
408
    case GIMP_CURVE_SMOOTH:
409
      curvex   = tool->col_value[cchan];
410
      distance = G_MAXINT;
411

412 413
      for (i = 0; i < 17; i++)
	{
414 415
	  if (tool->curves->points[cchan][i][0] != -1)
	    if (abs (curvex - tool->curves->points[cchan][i][0]) < distance)
416
	      {
417
		distance = abs (curvex - tool->curves->points[cchan][i][0]);
418 419 420
		closest_point = i;
	      }
	}
421

422 423
      if (distance > MIN_DISTANCE)
	closest_point = (curvex + 8) / 16;
424

425 426
      tool->curves->points[cchan][closest_point][0] = curvex;
      tool->curves->points[cchan][closest_point][1] = tool->curves->curve[cchan][curvex];
427
      break;
428

429
    case GIMP_CURVE_FREE:
430
      tool->curves->curve[cchan][x] = 255 - y;
431
      break;
432
    }
Elliot Lee's avatar
Elliot Lee committed
433 434
}

435 436
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
437
{
438
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
439

440
  gimp_lut_setup (tool->lut,
441
		  (GimpLutFunc) curves_lut_func,
442
                  tool->curves,
443
		  gimp_drawable_bytes (image_map_tool->drawable));
444

445 446
  gimp_image_map_apply (image_map_tool->image_map,
                        (GimpImageMapApplyFunc) gimp_lut_process_2,
447
                        tool->lut);
Elliot Lee's avatar
Elliot Lee committed
448 449
}

450

451 452 453
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
454

455 456
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
457
{
458
  GimpCurvesTool  *tool = GIMP_CURVES_TOOL (image_map_tool);
459 460
  GimpToolOptions *tool_options;
  GtkWidget       *vbox;
461 462 463 464 465
  GtkWidget       *vbox2;
  GtkWidget       *hbox;
  GtkWidget       *hbox2;
  GtkWidget       *label;
  GtkWidget       *bbox;
466 467 468 469
  GtkWidget       *frame;
  GtkWidget       *menu;
  GtkWidget       *table;
  GtkWidget       *button;
470
  GtkWidget       *bar;
471
  gint             padding;
472

473
  vbox = image_map_tool->main_vbox;
Elliot Lee's avatar
Elliot Lee committed
474

475 476
  /*  The option menu for selecting channels  */
  hbox = gtk_hbox_new (FALSE, 4);
477 478 479 480 481 482 483
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  label = gtk_label_new (_("Channel:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

484 485
  menu = gimp_enum_option_menu_new (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                    G_CALLBACK (curves_channel_callback),
486
                                    tool);
487 488 489 490 491
  gimp_enum_option_menu_set_stock_prefix (GTK_OPTION_MENU (menu),
                                          "gimp-channel");
  gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);

492
  tool->channel_menu = menu;
493

494
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
495
  gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
496
  gtk_widget_show (button);
497

498
  g_signal_connect (button, "clicked",
499
                    G_CALLBACK (curves_channel_reset_callback),
500
                    tool);
Elliot Lee's avatar
Elliot Lee committed
501

502

503
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
504
  table = gtk_table_new (2, 2, FALSE);
505 506
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
507 508 509 510 511 512 513
  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,
		    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_widget_show (vbox2);
Elliot Lee's avatar
Elliot Lee committed
514 515

  frame = gtk_frame_new (NULL);
516
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
517
  gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
518 519
  gtk_widget_show (frame);

520 521 522 523
  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);
524

Elliot Lee's avatar
Elliot Lee committed
525 526
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
527
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
528
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
529
		    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
530
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
531

532 533 534 535 536 537 538 539 540
  tool->graph = gimp_histogram_view_new (FALSE);
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  gtk_widget_add_events (tool->graph, (GDK_BUTTON_PRESS_MASK   |
                                       GDK_BUTTON_RELEASE_MASK |
                                       GDK_POINTER_MOTION_MASK |
                                       GDK_LEAVE_NOTIFY_MASK));
  g_object_set (tool->graph,
541 542 543
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
544 545 546
  GIMP_HISTOGRAM_VIEW (tool->graph)->light_histogram = TRUE;
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
547

548
  g_signal_connect (tool->graph, "event",
549
		    G_CALLBACK (curves_graph_events),
550 551
		    tool);
  g_signal_connect_after (tool->graph, "expose_event",
552
                          G_CALLBACK (curves_graph_expose),
553
                          tool);
Elliot Lee's avatar
Elliot Lee committed
554

555

556
  tool_options = GIMP_TOOL (tool)->tool_info->tool_options;
557
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
558 559 560 561 562 563 564
                                       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,
		    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (hbox2);
565

Elliot Lee's avatar
Elliot Lee committed
566
  frame = gtk_frame_new (NULL);
567
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
568
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
569 570
  gtk_widget_show (frame);

571 572 573 574
  vbox2 = gtk_vbox_new (TRUE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox2);
  gtk_widget_show (vbox2);

575 576 577 578
  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);
579

580 581 582 583
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_box_pack_start (GTK_BOX (vbox2), bar, TRUE, TRUE, 0);
  gtk_widget_show (bar);

584
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
585

586

587 588 589
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
590

591
  /*  Horizontal button box for load / save */
592
  frame = gtk_frame_new (_("All Channels"));
593
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
594 595
  gtk_widget_show (frame);

596 597 598 599 600 601
  bbox = gtk_hbutton_box_new ();
  gtk_container_set_border_width (GTK_CONTAINER (bbox), 2);
  gtk_box_set_spacing (GTK_BOX (bbox), 4);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_SPREAD);
  gtk_container_add (GTK_CONTAINER (frame), bbox);
  gtk_widget_show (bbox);
602

603
  button = gtk_button_new_from_stock (GTK_STOCK_OPEN);
604
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
605
  gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
606
  gimp_help_set_help_data (button, _("Read curves settings from file"), NULL);
607 608
  gtk_widget_show (button);

609
  g_signal_connect (button, "clicked",
610
		    G_CALLBACK (curves_load_callback),
611
		    tool);
612

613
  button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
614
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
615
  gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
616
  gimp_help_set_help_data (button, _("Save curves settings to file"), NULL);
617 618
  gtk_widget_show (button);

619
  g_signal_connect (button, "clicked",
620
		    G_CALLBACK (curves_save_callback),
621
		    tool);
622 623 624 625 626 627 628

  /*  The radio box for selecting the curve type  */
  frame = gtk_frame_new (_("Curve Type"));
  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,
629
				  "gimp-curve", GTK_ICON_SIZE_MENU,
630
				  G_CALLBACK (curves_curve_type_callback),
631 632 633 634 635 636
				  tool,
				  &tool->curve_type);

  gtk_widget_style_get (bbox, "child_internal_pad_x", &padding, NULL);

  gimp_enum_stock_box_set_child_padding (hbox, padding, -1);
637 638 639 640 641 642

  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
643 644
}

645
static void
646
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
647
{
648
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (image_map_tool);
649
  GimpHistogramChannel  channel;
650

651
  tool->grab_point = -1;
652

653 654 655
  for (channel =  GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
656
    curves_channel_reset (tool->curves, channel);
657

658
  curves_update (tool, XRANGE | GRAPH);
659 660
}

661

662
/* TODO: preview alpha channel stuff correctly.  -- austin, 20/May/99 */
Elliot Lee's avatar
Elliot Lee committed
663
static void
664
curves_update (GimpCurvesTool *tool,
665
	       gint            update)
Elliot Lee's avatar
Elliot Lee committed
666
{
667
  GimpHistogramChannel channel;
668

669
  if (tool->color)
670
    {
671
      channel = tool->channel;
672
    }
673
  else
674
    {
675
      if (tool->channel == 2)
676
        channel = GIMP_HISTOGRAM_ALPHA;
677
      else
678
        channel = GIMP_HISTOGRAM_VALUE;
679
    }
680

681 682 683
  if (update & GRAPH)
    gtk_widget_queue_draw (tool->graph);

684
  if (update & XRANGE)
Elliot Lee's avatar
Elliot Lee committed
685
    {
686
      switch (channel)
687
	{
Michael Natterer's avatar
Michael Natterer committed
688 689
	case GIMP_HISTOGRAM_VALUE:
	case GIMP_HISTOGRAM_ALPHA:
690 691 692 693
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
                                      tool->curves->curve[channel],
                                      tool->curves->curve[channel],
                                      tool->curves->curve[channel]);
694 695
	  break;

Michael Natterer's avatar
Michael Natterer committed
696 697 698
	case GIMP_HISTOGRAM_RED:
	case GIMP_HISTOGRAM_GREEN:
	case GIMP_HISTOGRAM_BLUE:
699 700 701 702
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
                                      tool->curves->curve[GIMP_HISTOGRAM_RED],
                                      tool->curves->curve[GIMP_HISTOGRAM_GREEN],
                                      tool->curves->curve[GIMP_HISTOGRAM_BLUE]);
703
	  break;
704
	}
Elliot Lee's avatar
Elliot Lee committed
705
    }
706

Elliot Lee's avatar
Elliot Lee committed
707 708
  if (update & YRANGE)
    {
709
      gimp_color_bar_set_channel (GIMP_COLOR_BAR (tool->yrange), channel);
Elliot Lee's avatar
Elliot Lee committed
710 711 712 713
    }
}

static void
714
curves_channel_callback (GtkWidget      *widget,
715
			 GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
716
{
717
  gimp_menu_item_update (widget, &tool->channel);
Elliot Lee's avatar
Elliot Lee committed
718

719 720
  gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (tool->graph),
                                   tool->channel);
721 722

  /* FIXME: hack */
723 724
  if (! tool->color && tool->alpha)
    tool->channel = (tool->channel > 1) ? 2 : 1;
Elliot Lee's avatar
Elliot Lee committed
725

726 727
  gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (tool->curve_type),
			           tool->curves->curve_type[tool->channel]);
728

729
  curves_update (tool, ALL);
Elliot Lee's avatar
Elliot Lee committed
730 731 732
}

static void
733
curves_channel_reset_callback (GtkWidget      *widget,
734
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
735
{
736
  tool->grab_point = -1;
Elliot Lee's avatar
Elliot Lee committed
737

738
  curves_channel_reset (tool->curves, tool->channel);
Elliot Lee's avatar
Elliot Lee committed
739

740
  curves_update (tool, XRANGE | GRAPH);
Elliot Lee's avatar
Elliot Lee committed
741

742
  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));
Elliot Lee's avatar
Elliot Lee committed
743 744
}

745
static gboolean
746
curves_set_sensitive_callback (GimpHistogramChannel  channel,
747
                               GimpCurvesTool       *tool)
748 749 750 751 752
{
  switch (channel)
    {
    case GIMP_HISTOGRAM_VALUE:
      return TRUE;
753

754 755 756
    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
757
      return tool->color;
758

759
    case GIMP_HISTOGRAM_ALPHA:
760
      return gimp_drawable_has_alpha (GIMP_IMAGE_MAP_TOOL (tool)->drawable);
Sven Neumann's avatar