gradient-editor.c 159 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
16
17
18
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * Gradient editor module copyight (C) 1996-1997 Federico Mena Quintero
 * federico@nuclecu.unam.mx
 *
 * 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 PURIGHTE.  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
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
20
21
 */

22
23
24
/* alt: Added previews and some other buttons to gradient dialog. 
 *
 * hof: Hofer Wolfgang, 1998.01.27   avoid resize bug by keeping 
25
26
27
28
 *                                   preview widgetsize constant.
 * hof: Hofer Wolfgang, 1997.10.17   Added "Load From FG color"
 */

Elliot Lee's avatar
Elliot Lee committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Release date: 1997/06/02
 *
 * - Added the following procedural_db calls:
 *     gimp_gradients_get_list
 *     gimp_gradients_get_active
 *     gimp_gradients_set_active
 *     gimp_gradients_sample_uniform
 *     gimp_gradients_sample_custom
 *   Many thanks to Eiichi and Marcelo for their suggestions!
 */

/* Release date: 1997/05/07
 *
 * - Added accelerator keys for the popup functions.  This allows for
 * very fast operation of the editor.
 *
 * - Added Replicate Selection function.
 *
 * - Re-arranged the pop-up menu a bit; it was getting too big.  I am
 * still not entirely happy with it.
 *
 * - Added grad_dump_gradient(); it is useful for debugging.
 */

/* Release date: 1997/04/30
 *
 * - All `dangerous' dialogs now de-sensitize the main editor window.
 * This fixes a lot of potential bugs when the dialogs are active.
 *
 * - Fixed two bugs due to uninitialized variables, one in
 * prev_events() (thanks to Marcelo for pointing it out) and another
 * in cpopup_render_color_box() (me), and removed all warnings due to
 * those.
 *
 * - Removed the printf()'s in the pop-up menu (they were only used
 * for debugging).
 */

/* Release date: 1997/04/22
 *
 * - Added GtkRadioMenuItems to the blending and coloring pop-up
 * menus.  You no longer have to remember the dang type for each
 * segment.
 *
 * - Added midpoint capabilities to sinuosidal and spherical sigments.
 * Many thanks to Marcelo for the patches!
 *
 * - Added a *real* Cancel function to the color pickers.  I don't
 * know why nobody killed me for not having done it before.
 */

/* Release date: 1997/04/21
 *
 * - Re-wrote the old pop-up menu code, which was *horrible*.  The
 * memory leaks *should* go away once I write
 * grad_free_gradient_editor().  I'll do it once I'm finished adding
 * crap to gradient_editor_t.
 *
 * - Added "fetch from" color buttons.  Yeah, we all missed them.  You
 * should shed happiness tears when you see it.
 *
 * - Added an eyedropper function to the preview widget.  You can now
 * click on the preview widget and the foreground color will be set to
 * the gradient's color under the cursor.  This is still missing the
 * eyedropper cursor shape.
 *
 * - You can now invoke the pop-up menu from the preview widget.  Even
 * my hand gets unsteady at times.
 *
 * - Cool functions: Flip selection, Blend colors.  Can't live without
 * them.
 */

/* Special thanks to:
 *
 * Luis Albarran (luis4@mindspring.com) - Nice UI suggestions
 *
 * Miguel de Icaza (miguel@nuclecu.unam.mx) - Pop-up menu suggestion
 *
 * Marcelo Malheiros (malheiro@dca.fee.unicamp.br) - many, many
 * suggestions, nice gradient files
 *
 * Adam Moss (adam@uunet.pipex.com) - idea for the hint bar
 *
 * Everyone on #gimp - many suggestions
 */

/* TODO:
 *
 * - Fix memory leaks: grad_free_gradient_editor() and any others
 * which I may have missed.
 *
 * - Add all of Marcelo's neat suggestions:
 *   - Hue rotate, saturation, brightness, contrast.
 *
 * - Add Save Dirty Gradients function.
 *
 * - Provide a way of renaming a gradient (instead of having to do
 *   copy/delete).
 *
 * - Fix the flicker in the hint bar.
 *
 * - Better handling of bogus gradient files and inconsistent
 *   segments.  Do not loop indefinitely in seg_get_segment_at() if
 *   there is a missing segment between two others.
 *
 * - Add a Gradient brush mode (color changes as you move it).
 */

138
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
139
140
141
142

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
143
#ifdef HAVE_UNISTD_H
Elliot Lee's avatar
Elliot Lee committed
144
#include <unistd.h>
145
#endif
Elliot Lee's avatar
Elliot Lee committed
146
147
148
149
150
151
152

#include "appenv.h"
#include "colormaps.h"
#include "cursorutil.h"
#include "datafiles.h"
#include "errors.h"
#include "general.h"
153
#include "gimpcontext.h"
Michael Natterer's avatar
Michael Natterer committed
154
#include "gimpdnd.h"
Elliot Lee's avatar
Elliot Lee committed
155
#include "gimprc.h"
156
#include "gimpui.h"
Elliot Lee's avatar
Elliot Lee committed
157
#include "gradient.h"
Michael Natterer's avatar
Michael Natterer committed
158
#include "gradientP.h"
159
#include "gradient_header.h"
Michael Natterer's avatar
Michael Natterer committed
160
161
#include "gradient_select.h"
#include "session.h"
Elliot Lee's avatar
Elliot Lee committed
162

163
#include "libgimp/gimpintl.h"
Michael Natterer's avatar
Michael Natterer committed
164
#include "libgimp/gimplimits.h"
165
#include "libgimp/gimpmath.h"
166

167
168
169
#include "pixmaps/zoom_in.xpm"
#include "pixmaps/zoom_out.xpm"

Elliot Lee's avatar
Elliot Lee committed
170
171
172
173
/***** Magic numbers *****/

#define EPSILON 1e-10

Michael Natterer's avatar
Michael Natterer committed
174
#define GRAD_LIST_WIDTH  300
175
#define GRAD_LIST_HEIGHT  80
Michael Natterer's avatar
Michael Natterer committed
176
177
178
179

#define GRAD_SCROLLBAR_STEP_SIZE 0.05
#define GRAD_SCROLLBAR_PAGE_SIZE 0.75

180
181
182
#define GRAD_PREVIEW_WIDTH  600
#define GRAD_PREVIEW_HEIGHT  64
#define GRAD_CONTROL_HEIGHT  10
Michael Natterer's avatar
Michael Natterer committed
183
184
185
186
187
188
189
190

#define GRAD_COLOR_BOX_WIDTH  24
#define GRAD_COLOR_BOX_HEIGHT 16

#define GRAD_NUM_COLORS 10

#define GRAD_MOVE_TIME 150 /* ms between mouse click and detection of movement in gradient control */

191
192
193
194
195
196
197
198
199
200
201
202
203
#define GRAD_PREVIEW_EVENT_MASK (GDK_EXPOSURE_MASK | \
                                 GDK_LEAVE_NOTIFY_MASK | \
				 GDK_POINTER_MOTION_MASK | \
                                 GDK_POINTER_MOTION_HINT_MASK | \
				 GDK_BUTTON_PRESS_MASK | \
                                 GDK_BUTTON_RELEASE_MASK)

#define GRAD_CONTROL_EVENT_MASK (GDK_EXPOSURE_MASK | \
				 GDK_LEAVE_NOTIFY_MASK | \
				 GDK_POINTER_MOTION_MASK | \
				 GDK_POINTER_MOTION_HINT_MASK | \
				 GDK_BUTTON_PRESS_MASK | \
				 GDK_BUTTON_RELEASE_MASK | \
Michael Natterer's avatar
Michael Natterer committed
204
205
				 GDK_BUTTON1_MOTION_MASK)

206
207
208
209
#define GRAD_UPDATE_GRADIENT 1
#define GRAD_UPDATE_PREVIEW  2
#define GRAD_UPDATE_CONTROL  4
#define GRAD_RESET_CONTROL   8
Michael Natterer's avatar
Michael Natterer committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

/* Gradient editor type */

typedef enum
{
  GRAD_DRAG_NONE = 0,
  GRAD_DRAG_LEFT,
  GRAD_DRAG_MIDDLE,
  GRAD_DRAG_ALL
} control_drag_mode_t;

typedef struct
{
  GtkWidget *shell;
  GdkGC     *gc;
  GtkWidget *hint_label;
  GtkWidget *clist;
  GtkWidget *scrollbar;
  GtkWidget *preview;
  GtkWidget *control;

  /*  Zoom and scrollbar  */
  guint      zoom_factor;
  GtkObject *scroll_data;

  /*  Instant update  */
  gboolean  instant_update;

  /*  Gradient preview  */
  guchar   *preview_rows[2]; /* For caching redraw info */
  gint      preview_last_x;
  gboolean  preview_button_down;

  /*  Gradient control  */
  GdkPixmap           *control_pixmap;
  grad_segment_t      *control_drag_segment; /* Segment which is being dragged */
  grad_segment_t      *control_sel_l;        /* Left segment of selection */
  grad_segment_t      *control_sel_r;        /* Right segment of selection */
  control_drag_mode_t  control_drag_mode;    /* What is being dragged? */
  guint32              control_click_time;   /* Time when mouse was pressed */
  gboolean             control_compress;     /* Compressing/expanding handles */
  gint                 control_last_x;       /* Last mouse position when dragging */
  gdouble              control_last_gx;      /* Last position (wrt gradient) when dragging */
  gdouble              control_orig_pos;     /* Original click position when dragging */

  GtkWidget *control_main_popup;              /* Popup menu */
  GtkWidget *control_blending_label;          /* Blending function label */
  GtkWidget *control_coloring_label;          /* Coloring type label */
  GtkWidget *control_splitm_label;            /* Split at midpoint label */
  GtkWidget *control_splitu_label;            /* Split uniformly label */
  GtkWidget *control_delete_menu_item;        /* Delete menu item */
  GtkWidget *control_delete_label;            /* Delete label */
  GtkWidget *control_recenter_label;          /* Re-center label */
  GtkWidget *control_redistribute_label;      /* Re-distribute label */
  GtkWidget *control_flip_label;              /* Flip label */
  GtkWidget *control_replicate_label;         /* Replicate label */
  GtkWidget *control_blend_colors_menu_item;  /* Blend colors menu item */
  GtkWidget *control_blend_opacity_menu_item; /* Blend opacity menu item */
  GtkWidget *control_left_load_popup;         /* Left endpoint load menu */
  GtkWidget *control_left_save_popup;         /* Left endpoint save menu */
  GtkWidget *control_right_load_popup;        /* Right endpoint load menu */
  GtkWidget *control_right_save_popup;        /* Right endpoint save menu */
  GtkWidget *control_blending_popup;          /* Blending function menu */
  GtkWidget *control_coloring_popup;          /* Coloring type menu */
  GtkWidget *control_sel_ops_popup;           /* Selection ops menu */

  GtkAccelGroup *accel_group;

  /*  Blending and coloring menus  */
  GtkWidget *control_blending_items[5 + 1]; /* Add 1 for the "Varies" item */
  GtkWidget *control_coloring_items[3 + 1];

  /*  Split uniformly dialog  */
  gint split_parts;

  /*  Replicate dialog  */
  gint replicate_times;

  /*  Saved colors  */
  struct
  {
    gdouble r, g, b, a;
  } saved_colors[GRAD_NUM_COLORS];

  GtkWidget *left_load_color_boxes[GRAD_NUM_COLORS + 3];
  GtkWidget *left_load_labels[GRAD_NUM_COLORS + 3];

  GtkWidget *left_save_color_boxes[GRAD_NUM_COLORS];
  GtkWidget *left_save_labels[GRAD_NUM_COLORS];

  GtkWidget *right_load_color_boxes[GRAD_NUM_COLORS + 3];
  GtkWidget *right_load_labels[GRAD_NUM_COLORS + 3];

  GtkWidget *right_save_color_boxes[GRAD_NUM_COLORS];
  GtkWidget *right_save_labels[GRAD_NUM_COLORS];

  /*  Color dialogs  */
  GtkWidget      *left_color_preview;
  grad_segment_t *left_saved_segments;
  gboolean        left_saved_dirty;

  GtkWidget      *right_color_preview;
  grad_segment_t *right_saved_segments;
  gboolean        right_saved_dirty;
} GradientEditor;
Elliot Lee's avatar
Elliot Lee committed
315
316
317

/***** Local functions *****/

318
319
320
321
322
323
324
static gint gradient_editor_clist_button_press (GtkWidget      *widget,
						GdkEventButton *bevent,
						gpointer        data);

static gradient_t * gradient_editor_drag_gradient (GtkWidget  *widget,
						   gpointer    data);

Michael Natterer's avatar
Michael Natterer committed
325
326
327
328
static void  gradient_editor_drop_gradient (GtkWidget  *widget,
					    gradient_t *gradient,
					    gpointer    data);

Elliot Lee's avatar
Elliot Lee committed
329
/* Gradient editor functions */
Michael Natterer's avatar
Michael Natterer committed
330

331
332
333
334
335
336
337
static GtkWidget *ed_create_button (gchar *label,
				    double xalign, double yalign,
				    GtkSignalFunc signal_func,
				    gpointer user_data);

static void  ed_fetch_foreground (double *fg_r, double *fg_g, double *fg_b,
				  double *fg_a);
338
static void  ed_update_editor    (int flags);
339

340
static void  ed_set_hint         (gchar *str);
341
342
343
344
345
346
347
348
349
350


static void  ed_list_item_update (GtkWidget *widget, 
				  gint row,
				  gint column,
				  GdkEventButton *event,
				  gpointer data);

static void  ed_initialize_saved_colors (void);

351
/* Main dialog button callbacks & functions */
352

353
354
static void  ed_new_gradient_callback       (GtkWidget *, gpointer);
static void  ed_do_new_gradient_callback    (GtkWidget *, gpointer , gpointer);
355

356
357
static void  ed_copy_gradient_callback      (GtkWidget *, gpointer);
static void  ed_do_copy_gradient_callback   (GtkWidget *, gpointer , gpointer);
358
359
360
361
362
363
364
365

static void  ed_delete_gradient_callback        (GtkWidget *, gpointer);
static void  ed_do_delete_gradient_callback     (GtkWidget *, gpointer);
static void  ed_cancel_delete_gradient_callback (GtkWidget *, gpointer);

static void  ed_rename_gradient_callback    (GtkWidget *, gpointer);
static void  ed_do_rename_gradient_callback (GtkWidget *, gpointer, gpointer);

366
367
368
369
static void  ed_save_pov_callback           (GtkWidget *, gpointer);
static void  ed_do_save_pov_callback        (GtkWidget *, gpointer);
static void  ed_cancel_save_pov_callback    (GtkWidget *, gpointer);
static gint  ed_delete_save_pov_callback    (GtkWidget *, GdkEvent *, gpointer);
370

371
372
373
374
375
static void  ed_save_grads_callback         (GtkWidget *, gpointer);
static void  ed_refresh_grads_callback      (GtkWidget *, gpointer);
static void  ed_close_callback              (GtkWidget *, gpointer);

/* Zoom, scrollbar & instant update callbacks */
376
377
378
379
380
381

static void  ed_scrollbar_update      (GtkAdjustment *, gpointer);
static void  ed_zoom_all_callback     (GtkWidget *, gpointer);
static void  ed_zoom_out_callback     (GtkWidget *, gpointer);
static void  ed_zoom_in_callback      (GtkWidget *, gpointer);
static void  ed_instant_update_update (GtkWidget *, gpointer);
Elliot Lee's avatar
Elliot Lee committed
382
383
384

/* Gradient preview functions */

385
386
static gint prev_events         (GtkWidget *, GdkEvent *, gpointer);
static void prev_set_hint       (gint x);
387
388
389
390

static void prev_set_foreground (gint x);
static void prev_set_background (gint x);

391
static void gradient_update     (void);
392
393
394
static void prev_update         (gboolean recalculate);
static void prev_fill_image     (int width, int height,
				 double left, double right);
Elliot Lee's avatar
Elliot Lee committed
395
396
397

/* Gradient control functions */

398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
static gint     control_events                (GtkWidget *, GdkEvent *,
					       gpointer);
static void     control_do_hint               (gint x, gint y);
static void     control_button_press          (gint x, gint y,
					       guint button, guint state);
static gboolean control_point_in_handle       (gint x, gint y,
					       grad_segment_t *seg,
					       control_drag_mode_t handle);
static void     control_select_single_segment (grad_segment_t *seg);
static void     control_extend_selection      (grad_segment_t *seg, double pos);
static void     control_motion                (gint x);

static void     control_compress_left         (grad_segment_t *range_l,
					       grad_segment_t *range_r,
					       grad_segment_t *drag_seg,
413
					       double pos);
414
415
416
static void     control_compress_range        (grad_segment_t *range_l,
					       grad_segment_t *range_r,
					       double new_l, double new_r);
417

418
419
420
static double   control_move                  (grad_segment_t *range_l,
					       grad_segment_t *range_r,
					       double delta);
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437

/* Control update/redraw functions */

static void   control_update             (gboolean recalculate);
static void   control_draw               (GdkPixmap *pixmap,
					  int width, int height,
					  double left, double right);
static void   control_draw_normal_handle (GdkPixmap *pixmap,
					  double pos, int height);
static void   control_draw_middle_handle (GdkPixmap *pixmap,
					  double pos, int height);
static void   control_draw_handle        (GdkPixmap *pixmap,
					  GdkGC *border_gc, GdkGC *fill_gc,
					  int xpos, int height);

static int    control_calc_p_pos         (double pos);
static double control_calc_g_pos         (int pos);
Elliot Lee's avatar
Elliot Lee committed
438
439
440

/* Control popup functions */

441
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
static void       cpopup_create_main_menu  (void);
static void       cpopup_do_popup          (void);

static GtkWidget *cpopup_create_color_item           (GtkWidget **color_box,
						      GtkWidget **label);
static GtkWidget *cpopup_create_menu_item_with_label (gchar      *str,
						      GtkWidget **label);

static void  cpopup_adjust_menus           (void);
static void  cpopup_adjust_blending_menu   (void);
static void  cpopup_adjust_coloring_menu   (void);
static void  cpopup_check_selection_params (gint *equal_blending,
					    gint *equal_coloring);

static void  cpopup_render_color_box (GtkPreview *preview,
				      double r, double g, double b, double a);

static GtkWidget *cpopup_create_load_menu (GtkWidget **color_boxes,
					   GtkWidget **labels,
					   gchar *label1, gchar *label2,
					   GtkSignalFunc callback,
					   gchar accel_key_0,
					   guint8 accel_mods_0,
					   gchar accel_key_1,
					   guint8 accel_mods_1,
					   gchar accel_key_2,
					   guint8 accel_mods_2);
static GtkWidget *cpopup_create_save_menu (GtkWidget **color_boxes,
					   GtkWidget **labels,
					   GtkSignalFunc callback);

static void  cpopup_update_saved_color  (int n,
					 double r, double g, double b, double a);

static void  cpopup_load_left_callback  (GtkWidget *, gpointer);
static void  cpopup_save_left_callback  (GtkWidget *, gpointer);
static void  cpopup_load_right_callback (GtkWidget *, gpointer);
static void  cpopup_save_right_callback (GtkWidget *, gpointer);

static void  cpopup_set_color_selection_color (GtkColorSelection *cs,
					       double r, double g,
					       double b, double a);
static void  cpopup_get_color_selection_color (GtkColorSelection *cs,
					       double *r, double *g,
					       double *b, double *a);

static grad_segment_t *cpopup_save_selection    (void);
static void            cpopup_free_selection    (grad_segment_t *seg);
static void            cpopup_replace_selection (grad_segment_t *replace_seg);

/* ----- */

static void  cpopup_create_color_dialog (gchar *title,
					 double r, double g, double b, double a,
					 GtkSignalFunc color_changed_callback,
					 GtkSignalFunc ok_callback,
					 GtkSignalFunc cancel_callback,
					 GtkSignalFunc delete_callback);

static void  cpopup_set_left_color_callback  (GtkWidget *, gpointer);
static void  cpopup_left_color_changed       (GtkWidget *, gpointer);
static void  cpopup_left_color_dialog_ok     (GtkWidget *, gpointer);
static void  cpopup_left_color_dialog_cancel (GtkWidget *, gpointer);
static int   cpopup_left_color_dialog_delete (GtkWidget *, GdkEvent *,
					      gpointer);

static void  cpopup_set_right_color_callback  (GtkWidget *, gpointer);
static void  cpopup_right_color_changed       (GtkWidget *, gpointer);
static void  cpopup_right_color_dialog_ok     (GtkWidget *, gpointer);
static void  cpopup_right_color_dialog_cancel (GtkWidget *, gpointer);
static int   cpopup_right_color_dialog_delete (GtkWidget *, GdkEvent *,
					       gpointer);

/* ----- */

static GtkWidget *cpopup_create_blending_menu (void);
static void       cpopup_blending_callback    (GtkWidget *, gpointer);
static GtkWidget *cpopup_create_coloring_menu (void);
static void       cpopup_coloring_callback    (GtkWidget *, gpointer);

/* ----- */

static void  cpopup_split_midpoint_callback (GtkWidget *, gpointer);
static void  cpopup_split_midpoint (grad_segment_t *lseg, grad_segment_t **newl,
				    grad_segment_t **newr);

static void  cpopup_split_uniform_callback        (GtkWidget *, gpointer);
static void  cpopup_split_uniform_scale_update    (GtkAdjustment *, gpointer);
static void  cpopup_split_uniform_split_callback  (GtkWidget *, gpointer);
static void  cpopup_split_uniform_cancel_callback (GtkWidget *, gpointer);
static void  cpopup_split_uniform (grad_segment_t *lseg, int parts,
				   grad_segment_t **newl, grad_segment_t **newr);

static void  cpopup_delete_callback       (GtkWidget *, gpointer);
static void  cpopup_recenter_callback     (GtkWidget *, gpointer);
static void  cpopup_redistribute_callback (GtkWidget *, gpointer);

/* Control popup -> Selection operations functions */

static GtkWidget * cpopup_create_sel_ops_menu (void);

static void  cpopup_flip_callback             (GtkWidget *, gpointer);

static void  cpopup_replicate_callback        (GtkWidget *, gpointer);
static void  cpopup_replicate_scale_update    (GtkAdjustment *, gpointer);
static void  cpopup_do_replicate_callback     (GtkWidget *, gpointer);
static void  cpopup_replicate_cancel_callback (GtkWidget *, gpointer);

static void  cpopup_blend_colors              (GtkWidget *, gpointer);
static void  cpopup_blend_opacity             (GtkWidget *, gpointer);

/* Blend function */

static void  cpopup_blend_endpoints (double r0, double g0, double b0, double a0,
				     double r1, double g1, double b1, double a1,
				     int blend_colors, int blend_opacity);
Elliot Lee's avatar
Elliot Lee committed
557
558
559

/* Gradient functions */

560
561
562
563
564
565
static gradient_t *grad_new_gradient             (void);
static void        grad_free_gradient            (gradient_t *grad);
static void        grad_free_gradients           (void);
static void        grad_load_gradient            (char *filename);
static void        grad_save_gradient            (gradient_t *grad,
						  char *filename);
566
static void        grad_save_all                 (gboolean need_free);
Elliot Lee's avatar
Elliot Lee committed
567

568
static gradient_t *grad_create_default_gradient  (void);
Elliot Lee's avatar
Elliot Lee committed
569

570
static int         grad_insert_in_gradients_list (gradient_t *grad);
Elliot Lee's avatar
Elliot Lee committed
571

572
static void        grad_dump_gradient            (gradient_t *grad, FILE *file);
573

Elliot Lee's avatar
Elliot Lee committed
574
575
576

/* Segment functions */

577
578
579
static grad_segment_t *seg_new_segment        (void);
static void            seg_free_segment       (grad_segment_t *seg);
static void            seg_free_segments      (grad_segment_t *seg);
Elliot Lee's avatar
Elliot Lee committed
580

581
582
583
584
585
static grad_segment_t *seg_get_segment_at     (gradient_t *grad, double pos);
static grad_segment_t *seg_get_last_segment   (grad_segment_t *seg);
static void            seg_get_closest_handle (gradient_t *grad, double pos,
					       grad_segment_t **seg,
					       control_drag_mode_t *handle);
Elliot Lee's avatar
Elliot Lee committed
586
587
588

/* Calculation functions */

589
590
591
592
593
static double calc_linear_factor            (double middle, double pos);
static double calc_curved_factor            (double middle, double pos);
static double calc_sine_factor              (double middle, double pos);
static double calc_sphere_increasing_factor (double middle, double pos);
static double calc_sphere_decreasing_factor (double middle, double pos);
Elliot Lee's avatar
Elliot Lee committed
594

595
596
static void   calc_rgb_to_hsv               (double *r, double *g, double *b);
static void   calc_hsv_to_rgb               (double *h, double *s, double *v);
Elliot Lee's avatar
Elliot Lee committed
597
598
599

/* Files and paths functions */

600
static gchar *build_user_filename (char *name, char *path_str);
Elliot Lee's avatar
Elliot Lee committed
601

Michael Natterer's avatar
Michael Natterer committed
602
603
604
605
606
/***** Global variables *****/

GSList * gradients_list = NULL; /* The list of gradients */
gint     num_gradients  = 0;

Elliot Lee's avatar
Elliot Lee committed
607
608
/***** Local variables *****/

Michael Natterer's avatar
Michael Natterer committed
609
610
static GdkColor         black;
static gradient_t     * curr_gradient = NULL;
611
static gradient_t     * dnd_gradient  = NULL;
Michael Natterer's avatar
Michael Natterer committed
612
static GradientEditor * g_editor      = NULL;
613
614

static gradient_t * standard_gradient = NULL;
Elliot Lee's avatar
Elliot Lee committed
615

616
617
618
619
620
621
622
623
static const gchar *blending_types[] =
{
  N_("Linear"),
  N_("Curved"),
  N_("Sinusoidal"),
  N_("Spherical (increasing)"),
  N_("Spherical (decreasing)")
};
Elliot Lee's avatar
Elliot Lee committed
624

625
626
627
628
629
630
static const gchar *coloring_types[] =
{
  N_("Plain RGB"),
  N_("HSV (counter-clockwise hue)"),
  N_("HSV (clockwise hue)")
};
Elliot Lee's avatar
Elliot Lee committed
631

Michael Natterer's avatar
Michael Natterer committed
632
633
634
635
636
637
638
639
/*  dnd stuff  */
static GtkTargetEntry gradient_target_table[] =
{
  GIMP_TARGET_GRADIENT
};
static guint n_gradient_targets = (sizeof (gradient_target_table) /
				   sizeof (gradient_target_table[0]));

Elliot Lee's avatar
Elliot Lee committed
640
641
642
/***** Public functions *****/

void
643
gradients_init (int no_data)
Elliot Lee's avatar
Elliot Lee committed
644
{
645
646
  if (gradients_list)
    gradients_free ();
Elliot Lee's avatar
Elliot Lee committed
647

648
649
  if (gradient_path != NULL && !no_data)
    datafiles_read_directories (gradient_path, grad_load_gradient, 0);
Elliot Lee's avatar
Elliot Lee committed
650

651
652
  gimp_context_refresh_gradients ();
}
Elliot Lee's avatar
Elliot Lee committed
653
654

void
655
gradients_free (void)
Elliot Lee's avatar
Elliot Lee committed
656
{
657
658
  grad_free_gradients ();
}
Elliot Lee's avatar
Elliot Lee committed
659

660
661
662
663
664
665
666
667
668
669
670
671
672
673
gradient_t *
gradients_get_standard_gradient (void)
{
  if (! standard_gradient)
    {
      standard_gradient = grad_create_default_gradient ();
      standard_gradient->name     = g_strdup ("Standard");
      standard_gradient->filename = NULL;
      standard_gradient->dirty    = FALSE;
    }

  return standard_gradient;
}

Michael Natterer's avatar
Michael Natterer committed
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
gradient_t *
gradient_list_get_gradient (GSList *list,
			    gchar  *name)
{
  gradient_t *gradient;

  for (; list; list = g_slist_next (list))
    {
      gradient = (gradient_t *) list->data;

      if (strcmp (gradient->name, name) == 0)
	return gradient;
    }

  return NULL;
}

gint
gradient_list_get_gradient_index (GSList     *list,
				  gradient_t *gradient)
{
  gradient_t *cmp_gradient;
  gint index;

  for (index = 0; list; list = g_slist_next (list), index++)
    {
      cmp_gradient = (gradient_t *) list->data;

      if (cmp_gradient == gradient)
	return index;
    }

  return -1;
}

Elliot Lee's avatar
Elliot Lee committed
709
710
711
/*****/

void
712
713
714
715
716
717
gradient_get_color_at (gradient_t *gradient,
		       gdouble     pos,
		       gdouble    *r,
		       gdouble    *g,
		       gdouble    *b,
		       gdouble    *a)
Elliot Lee's avatar
Elliot Lee committed
718
{
719
720
721
722
723
  double          factor = 0.0;
  grad_segment_t *seg;
  double          seg_len, middle;
  double          h0, s0, v0;
  double          h1, s1, v1;
Elliot Lee's avatar
Elliot Lee committed
724

725
  /* if there is no gradient return a totally transparent black */
726
  if (gradient == NULL) 
727
728
729
730
    {
      r = 0; g = 0; b = 0; a = 0;
      return;
    }
Elliot Lee's avatar
Elliot Lee committed
731

732
733
734
735
  if (pos < 0.0)
    pos = 0.0;
  else if (pos > 1.0)
    pos = 1.0;
Elliot Lee's avatar
Elliot Lee committed
736

737
  seg = seg_get_segment_at (gradient, pos);
Elliot Lee's avatar
Elliot Lee committed
738

739
  seg_len = seg->right - seg->left;
Elliot Lee's avatar
Elliot Lee committed
740

741
742
743
744
745
746
747
748
749
750
  if (seg_len < EPSILON)
    {
      middle = 0.5;
      pos    = 0.5;
    }
  else
    {
      middle = (seg->middle - seg->left) / seg_len;
      pos    = (pos - seg->left) / seg_len;
    }
Elliot Lee's avatar
Elliot Lee committed
751

752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
  switch (seg->type)
    {
    case GRAD_LINEAR:
      factor = calc_linear_factor (middle, pos);
      break;

    case GRAD_CURVED:
      factor = calc_curved_factor (middle, pos);
      break;

    case GRAD_SINE:
      factor = calc_sine_factor (middle, pos);
      break;

    case GRAD_SPHERE_INCREASING:
      factor = calc_sphere_increasing_factor (middle, pos);
      break;

    case GRAD_SPHERE_DECREASING:
      factor = calc_sphere_decreasing_factor (middle, pos);
      break;

    default:
775
      grad_dump_gradient (gradient, stderr);
776
      gimp_fatal_error ("gradient_get_color_at(): Unknown gradient type %d",
777
778
779
			(int) seg->type);
      break;
    }
Elliot Lee's avatar
Elliot Lee committed
780

781
  /* Calculate color components */
Elliot Lee's avatar
Elliot Lee committed
782

783
  *a = seg->a0 + (seg->a1 - seg->a0) * factor;
Elliot Lee's avatar
Elliot Lee committed
784

785
786
787
788
789
790
791
792
793
794
795
  if (seg->color == GRAD_RGB)
    {
      *r = seg->r0 + (seg->r1 - seg->r0) * factor;
      *g = seg->g0 + (seg->g1 - seg->g0) * factor;
      *b = seg->b0 + (seg->b1 - seg->b0) * factor;
    }
  else
    {
      h0 = seg->r0;
      s0 = seg->g0;
      v0 = seg->b0;
Elliot Lee's avatar
Elliot Lee committed
796

797
798
799
      h1 = seg->r1;
      s1 = seg->g1;
      v1 = seg->b1;
Elliot Lee's avatar
Elliot Lee committed
800

801
802
      calc_rgb_to_hsv(&h0, &s0, &v0);
      calc_rgb_to_hsv(&h1, &s1, &v1);
Elliot Lee's avatar
Elliot Lee committed
803

804
805
      s0 = s0 + (s1 - s0) * factor;
      v0 = v0 + (v1 - v0) * factor;
Elliot Lee's avatar
Elliot Lee committed
806

807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
      switch (seg->color)
	{
	case GRAD_HSV_CCW:
	  if (h0 < h1)
	    h0 = h0 + (h1 - h0) * factor;
	  else
	    {
	      h0 = h0 + (1.0 - (h0 - h1)) * factor;
	      if (h0 > 1.0)
		h0 -= 1.0;
	    }
	  break;

	case GRAD_HSV_CW:
	  if (h1 < h0)
	    h0 = h0 - (h0 - h1) * factor;
	  else
	    {
	      h0 = h0 - (1.0 - (h1 - h0)) * factor;
	      if (h0 < 0.0)
		h0 += 1.0;
	    }
	  break;

	default:
832
	  grad_dump_gradient (gradient, stderr);
833
	  gimp_fatal_error ("gradient_get_color_at(): Unknown coloring mode %d",
834
835
836
			    (int) seg->color);
	  break;
	}
Elliot Lee's avatar
Elliot Lee committed
837

838
839
840
      *r = h0;
      *g = s0;
      *b = v0;
Elliot Lee's avatar
Elliot Lee committed
841

842
843
844
      calc_hsv_to_rgb (r, g, b);
    }
}
Elliot Lee's avatar
Elliot Lee committed
845

846
/***** The main gradient editor dialog *****/
Elliot Lee's avatar
Elliot Lee committed
847

848
void 
Michael Natterer's avatar
Michael Natterer committed
849
gradient_editor_create (void)
Elliot Lee's avatar
Elliot Lee committed
850
{
851
852
853
854
855
856
857
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *gvbox;
  GtkWidget *button;
  GtkWidget *frame;
  GtkWidget *scrolled_win;
  GdkColormap *colormap;
858
859
860
861
  GtkWidget* pixmapwid;
  GdkPixmap* pixmap;
  GdkBitmap* mask;
  GtkStyle* style;
862
863
864
  gint i;
  gint select_pos;

865
  /* If the editor already exists, just show it */
866
867
  if (g_editor)
    {
868
      if (!GTK_WIDGET_VISIBLE (g_editor->shell))
Michael Natterer's avatar
Michael Natterer committed
869
	gtk_widget_show (g_editor->shell);
870
871
      else
	gdk_window_raise (g_editor->shell->window);
Elliot Lee's avatar
Elliot Lee committed
872

873
874
      return;
    }
Elliot Lee's avatar
Elliot Lee committed
875

Michael Natterer's avatar
Michael Natterer committed
876
  g_editor = g_new (GradientEditor, 1);
Elliot Lee's avatar
Elliot Lee committed
877

878
  /* Shell and main vbox */
Michael Natterer's avatar
Michael Natterer committed
879
880
881
882
883
884
  g_editor->shell =
    gimp_dialog_new (_("Gradient Editor"), "gradient_editor",
		     gimp_standard_help_func,
		     "dialogs/gradient_editor/gradient_editor.html",
		     GTK_WIN_POS_NONE,
		     FALSE, TRUE, FALSE,
885

Michael Natterer's avatar
Michael Natterer committed
886
887
888
889
890
891
		     _("Save"), ed_save_grads_callback,
		     NULL, NULL, FALSE, FALSE,
		     _("Refresh"), ed_refresh_grads_callback,
		     NULL, NULL, FALSE, FALSE,
		     _("Close"), ed_close_callback,
		     NULL, NULL, TRUE, TRUE,
892

Michael Natterer's avatar
Michael Natterer committed
893
		     NULL);
Elliot Lee's avatar
Elliot Lee committed
894

895
896
897
  vbox = gtk_vbox_new (FALSE, 4);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (g_editor->shell)->vbox), vbox);
898
  gtk_widget_show (vbox);
Elliot Lee's avatar
Elliot Lee committed
899

900
  /* Gradients list box */
901
  hbox = gtk_hbox_new (FALSE, 4);
902
903
  gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
  gtk_widget_show (hbox);
Elliot Lee's avatar
Elliot Lee committed
904

905
906
  /* clist preview of gradients */
  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
907

908
909
910
  g_editor->clist = gtk_clist_new (2);
  gtk_clist_set_shadow_type (GTK_CLIST (g_editor->clist), GTK_SHADOW_IN);
  gtk_clist_set_row_height (GTK_CLIST (g_editor->clist), 18);
911
912
  gtk_clist_set_selection_mode (GTK_CLIST (g_editor->clist),
				GTK_SELECTION_BROWSE);
913

914
915
916
  gtk_clist_set_column_width (GTK_CLIST (g_editor->clist), 0, 52);
  gtk_clist_set_column_title (GTK_CLIST (g_editor->clist), 0, _("Gradient"));
  gtk_clist_set_column_title (GTK_CLIST (g_editor->clist), 1, _("Name"));
917

918
  gtk_clist_column_titles_show (GTK_CLIST (g_editor->clist));
919
920
  gtk_clist_set_use_drag_icons (GTK_CLIST (g_editor->clist), FALSE);
  gtk_clist_column_titles_passive (GTK_CLIST (g_editor->clist));
921

922
923
924
925
926
  gtk_box_pack_start (GTK_BOX (hbox), scrolled_win, TRUE, TRUE, 0); 
  gtk_container_add (GTK_CONTAINER (scrolled_win), g_editor->clist);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
				  GTK_POLICY_AUTOMATIC,
				  GTK_POLICY_ALWAYS);
927

928
929
  gtk_widget_show (scrolled_win);
  gtk_widget_show (g_editor->clist);
930

931
932
933
  colormap = gtk_widget_get_colormap (g_editor->clist);
  gdk_color_parse ("black", &black);
  gdk_color_alloc (colormap, &black);
934
	
935
936
937
938
  gtk_signal_connect (GTK_OBJECT (g_editor->clist), "button_press_event",
                      GTK_SIGNAL_FUNC (gradient_editor_clist_button_press),
                      NULL);

939
940
  gtk_signal_connect (GTK_OBJECT (g_editor->clist), "select_row",
		      GTK_SIGNAL_FUNC (ed_list_item_update),
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
		      NULL);

  /*  dnd stuff  */
  gtk_drag_source_set (g_editor->clist,
		       GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
		       gradient_target_table, n_gradient_targets,
		       GDK_ACTION_COPY);
  gimp_dnd_gradient_source_set (g_editor->clist, gradient_editor_drag_gradient,
				NULL);

  gtk_drag_dest_set (g_editor->clist,
		     GTK_DEST_DEFAULT_ALL,
		     gradient_target_table, n_gradient_targets,
		     GDK_ACTION_COPY);
  gimp_dnd_gradient_dest_set (g_editor->clist, gradient_editor_drop_gradient,
			      NULL);
957

958
959
960
  /* Frame & vbox for gradient functions */
  frame = gtk_frame_new (_("Gradient Ops"));
  gtk_box_pack_end (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
961

962
963
964
  gvbox = gtk_vbox_new (FALSE, 2);
  gtk_container_set_border_width (GTK_CONTAINER (gvbox), 4);
  gtk_container_add (GTK_CONTAINER (frame), gvbox);
965
966

  /* Buttons for gradient functions */
967
  button = ed_create_button (_("New Gradient"), 0.5, 0.5,
968
			     (GtkSignalFunc) ed_new_gradient_callback, NULL);
969
  GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
970
  gtk_box_pack_start (GTK_BOX (gvbox), button, FALSE, FALSE, 0);
971
972
  gtk_widget_show (button);

973
  button = ed_create_button (_("Copy Gradient"), 0.5, 0.5,
974
			     (GtkSignalFunc) ed_copy_gradient_callback, NULL);
975
  GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
976
  gtk_box_pack_start (GTK_BOX (gvbox), button, FALSE, FALSE, 0);
977
978
  gtk_widget_show (button);

979
  button = ed_create_button (_("Delete Gradient"), 0.5, 0.5,
980
			     (GtkSignalFunc) ed_delete_gradient_callback, NULL);
981
  GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
982
  gtk_box_pack_start (GTK_BOX (gvbox), button, FALSE, FALSE, 0);
983
984
  gtk_widget_show (button);

985
986
  button = ed_create_button (_("Rename Gradient"), 0.5, 0.5,
			     (GtkSignalFunc) ed_rename_gradient_callback, NULL);
987
  GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
988
  gtk_box_pack_start (GTK_BOX (gvbox), button, FALSE, FALSE, 0);
989
990
  gtk_widget_show (button);

991
992
  button = ed_create_button (_("Save as POV-Ray"), 0.5, 0.5,
			     (GtkSignalFunc) ed_save_pov_callback, NULL);
993
  GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
994
  gtk_box_pack_start (GTK_BOX (gvbox), button, FALSE, FALSE, 0);
995
996
  gtk_widget_show (button);

997
998
  gtk_widget_show (gvbox);
  gtk_widget_show (frame);
999

1000
  /*  Horizontal box for zoom controls, scrollbar and instant update toggle  */
1001
  hbox = gtk_hbox_new (FALSE, 4);
1002
  gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
1003
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1004
1005
  gtk_widget_show (hbox);

1006
  /*  Zoom all button */
1007
1008
  button = ed_create_button (_("Zoom all"), 0.5, 0.5,
			     (GtkSignalFunc) ed_zoom_all_callback, g_editor);
1009
  GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
1010
1011
1012
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

1013
1014
1015
1016
1017
1018
1019
1020
1021
  /*  + and - buttons  */
  gtk_widget_realize (g_editor->shell);
  style = gtk_widget_get_style (g_editor->shell);

  button = gtk_button_new ();
  GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
		      GTK_SIGNAL_FUNC (ed_zoom_in_callback),
		      (gpointer) g_editor);
1022
1023
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
  pixmap = gdk_pixmap_create_from_xpm_d (g_editor->shell->window, &mask,
					 &style->bg[GTK_STATE_NORMAL], 
					 zoom_in_xpm);
  pixmapwid = gtk_pixmap_new (pixmap, mask);
  gtk_container_add (GTK_CONTAINER (button), pixmapwid);
  gtk_widget_show (pixmapwid);
  gtk_widget_show (button);

  button = gtk_button_new ();
  GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
		      GTK_SIGNAL_FUNC (ed_zoom_out_callback),
		      (gpointer) g_editor);
1037
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
1038
1039
1040
1041
1042
1043
1044

  pixmap = gdk_pixmap_create_from_xpm_d (g_editor->shell->window, &mask,
					 &style->bg[GTK_STATE_NORMAL], 
					 zoom_out_xpm);
  pixmapwid = gtk_pixmap_new (pixmap, mask);
  gtk_container_add (GTK_CONTAINER (button), pixmapwid);
  gtk_widget_show (pixmapwid);
1045
1046
  gtk_widget_show (button);

1047
  /*  Scrollbar  */
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
  g_editor->zoom_factor = 1;

  g_editor->scroll_data = gtk_adjustment_new (0.0, 0.0, 1.0,
					      1.0 * GRAD_SCROLLBAR_STEP_SIZE,
					      1.0 * GRAD_SCROLLBAR_PAGE_SIZE,
					      1.0);

  gtk_signal_connect (g_editor->scroll_data, "value_changed",
		      (GtkSignalFunc) ed_scrollbar_update,
		      g_editor);
  gtk_signal_connect (g_editor->scroll_data, "changed",
		      (GtkSignalFunc) ed_scrollbar_update,
		      g_editor);

1062
1063
1064
1065
1066
  g_editor->scrollbar =
    gtk_hscrollbar_new (GTK_ADJUSTMENT (g_editor->scroll_data));
  gtk_range_set_update_policy (GTK_RANGE (g_editor->scrollbar),
			       GTK_UPDATE_CONTINUOUS);
  gtk_box_pack_start (GTK_BOX (hbox), g_editor->scrollbar, TRUE, TRUE, 0);
1067
1068
1069
  gtk_widget_show (g_editor->scrollbar);

  /* Instant update toggle */
Michael Natterer's avatar
Michael Natterer committed
1070
  g_editor->instant_update = TRUE;
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082

  button = gtk_check_button_new_with_label (_("Instant update"));
  gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  gtk_signal_connect (GTK_OBJECT (button), "toggled",
		      (GtkSignalFunc) ed_instant_update_update,
		      g_editor);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
  gtk_widget_show (button);

  /* Frame for gradient preview and gradient control */
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
1083
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
  gtk_widget_show (frame);

  gvbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (frame), gvbox); 
  gtk_widget_show (gvbox);

  /* Gradient preview */
  g_editor->preview_rows[0]     = NULL;
  g_editor->preview_rows[1]     = NULL;
  g_editor->preview_last_x      = 0;
Michael Natterer's avatar
Michael Natterer committed
1094
  g_editor->preview_button_down = FALSE;
1095
1096

  g_editor->preview = gtk_preview_new (GTK_PREVIEW_COLOR);
1097
  gtk_preview_set_dither (GTK_PREVIEW (g_editor->preview), GDK_RGB_DITHER_MAX);
1098
1099
  gtk_preview_size (GTK_PREVIEW (g_editor->preview),
		    GRAD_PREVIEW_WIDTH, GRAD_PREVIEW_HEIGHT);
1100
1101
1102
1103
1104
1105

  /*  Enable auto-resizing of the preview but ensure a minimal size  */
  gtk_widget_set_usize (g_editor->preview,
			GRAD_PREVIEW_WIDTH, GRAD_PREVIEW_HEIGHT);
  gtk_preview_set_expand (GTK_PREVIEW (g_editor->preview), TRUE);

1106
1107
  gtk_widget_set_events (g_editor->preview, GRAD_PREVIEW_EVENT_MASK);
  gtk_signal_connect (GTK_OBJECT(g_editor->preview), "event",
1108
		      (GdkEventFunc) prev_events,
1109
		      g_editor);
Michael Natterer's avatar
Michael Natterer committed
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120

  gtk_drag_dest_set (g_editor->preview,
		     GTK_DEST_DEFAULT_HIGHLIGHT |
		     GTK_DEST_DEFAULT_MOTION |
		     GTK_DEST_DEFAULT_DROP,
		     gradient_target_table, n_gradient_targets,
		     GDK_ACTION_COPY); 
  gimp_dnd_gradient_dest_set (g_editor->preview,
			      gradient_editor_drop_gradient,
			      NULL);

1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
  gtk_box_pack_start (GTK_BOX (gvbox), g_editor->preview, TRUE, TRUE, 0);
  gtk_widget_show (g_editor->preview);

  /* Gradient control */
  g_editor->control_pixmap                  = NULL;
  g_editor->control_drag_segment            = NULL;
  g_editor->control_sel_l                   = NULL;
  g_editor->control_sel_r                   = NULL;
  g_editor->control_drag_mode               = GRAD_DRAG_NONE;
  g_editor->control_click_time              = 0;
Michael Natterer's avatar
Michael Natterer committed
1131
  g_editor->control_compress                = FALSE;
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
  g_editor->control_last_x                  = 0;
  g_editor->control_last_gx                 = 0.0;
  g_editor->control_orig_pos                = 0.0;
  g_editor->control_main_popup              = NULL;
  g_editor->control_blending_label          = NULL;
  g_editor->control_coloring_label          = NULL;
  g_editor->control_splitm_label            = NULL;
  g_editor->control_splitu_label            = NULL;
  g_editor->control_delete_menu_item        = NULL;
  g_editor->control_delete_label            = NULL;
  g_editor->control_recenter_label          = NULL;
  g_editor->control_redistribute_label      = NULL;
  g_editor->control_flip_label              = NULL;
  g_editor->control_replicate_label         = NULL;
  g_editor->control_blend_colors_menu_item  = NULL;
  g_editor->control_blend_opacity_menu_item = NULL;
  g_editor->control_left_load_popup         = NULL;
  g_editor->control_left_save_popup         = NULL;
  g_editor->control_right_load_popup        = NULL;
  g_editor->control_right_save_popup        = NULL;
  g_editor->control_blending_popup          = NULL;
  g_editor->control_coloring_popup          = NULL;
  g_editor->control_sel_ops_popup           = NULL;

  g_editor->accel_group = NULL;

  for (i = 0;
1159
1160
       i < (sizeof (g_editor->control_blending_items) /
	    sizeof (g_editor->control_blending_items[0]));
1161
1162
1163
1164
       i++)
    g_editor->control_blending_items[i] = NULL;

  for (i = 0;
1165
1166
       i < (sizeof (g_editor->control_coloring_items) /
	    sizeof (g_editor->control_coloring_items[0]));
1167
1168
1169
1170
1171
1172
1173
1174
       i++)
    g_editor->control_coloring_items[i] = NULL;

  g_editor->control = gtk_drawing_area_new ();
  gtk_drawing_area_size (GTK_DRAWING_AREA (g_editor->control),
			 GRAD_PREVIEW_WIDTH, GRAD_CONTROL_HEIGHT);
  gtk_widget_set_events (g_editor->control, GRAD_CONTROL_EVENT_MASK);
  gtk_signal_connect (GTK_OBJECT (g_editor->control), "event",
1175
		      (GdkEventFunc) control_events,
1176
		      g_editor);
1177
  gtk_box_pack_start (GTK_BOX (gvbox), g_editor->control, TRUE, TRUE, 0);
1178
1179
  gtk_widget_show (g_editor->control);

1180
1181
1182
1183
1184
1185
1186
  /* Hint bar and close button */
  g_editor->hint_label = gtk_label_new ("");
  gtk_misc_set_alignment (GTK_MISC (g_editor->hint_label), 0.0, 0.5);
  gtk_box_pack_start (GTK_BOX (vbox), g_editor->hint_label, FALSE, FALSE, 0);
  gtk_widget_show (g_editor->hint_label);

  /* Initialize other data */
Michael Natterer's avatar
Michael Natterer committed
1187
1188
1189
  g_editor->left_color_preview  = NULL;
  g_editor->left_saved_segments = NULL;
  g_editor->left_saved_dirty    = FALSE;
1190

Michael Natterer's avatar
Michael Natterer committed
1191
1192
1193
  g_editor->right_color_preview  = NULL;
  g_editor->right_saved_segments = NULL;
  g_editor->right_saved_dirty    = FALSE;
1194

1195
1196
  ed_initialize_saved_colors ();
  cpopup_create_main_menu ();
1197

1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
  if (gradients_list)
    {
      curr_gradient = dnd_gradient = (gradient_t *) gradients_list->data;
    }
  else
    {
      gint pos;

      curr_gradient = dnd_gradient = grad_create_default_gradient ();
      curr_gradient->name     = g_strdup (_("Default"));
      curr_gradient->filename =
	build_user_filename (curr_gradient->name, gradient_path);
      curr_gradient->dirty    = FALSE;

      pos = grad_insert_in_gradients_list (curr_gradient);
      gradient_select_insert_all (pos, curr_gradient);
      gimp_context_refresh_gradients ();
    }

1217
1218
  /* Show everything */
  g_editor->gc = gdk_gc_new (g_editor->shell->window);
Michael Natterer's avatar
Michael Natterer committed
1219
1220
1221
  select_pos = gradient_clist_init (g_editor->shell, g_editor->gc,
				    g_editor->clist,
				    curr_gradient);
1222

Michael Natterer's avatar
Michael Natterer committed
1223
  gtk_widget_show (g_editor->shell);
1224
1225
1226

  if (select_pos != -1)
    gtk_clist_moveto (GTK_CLIST (g_editor->clist), select_pos, 0, 0.0, 0.0);
1227
}
Elliot Lee's avatar
Elliot Lee committed
1228

Michael Natterer's avatar
Michael Natterer committed
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
void
gradient_editor_free (void)
{
}

gboolean
gradient_editor_set_gradient (gradient_t *gradient)
{
  gint n;

  n = gradient_list_get_gradient_index (gradients_list, gradient);

1241
  if (n < 0)
Michael Natterer's avatar
Michael Natterer committed
1242
1243
1244
1245
1246
1247
1248
1249
1250
    return FALSE;

  if (g_editor)
    {
      gtk_clist_select_row (GTK_CLIST (g_editor->clist), n, -1);
      gtk_clist_moveto (GTK_CLIST (g_editor->clist), n, 0, 0.5, 0.0);
    }
  else
    {
1251
      curr_gradient = dnd_gradient = gradient;
Michael Natterer's avatar
Michael Natterer committed
1252
1253
1254
1255
1256
    }

  return TRUE;
}

Elliot Lee's avatar
Elliot Lee committed
1257
1258
/***** Gradient editor functions *****/

1259
1260
1261
1262
1263
1264
1265
static gint
gradient_editor_clist_button_press (GtkWidget      *widget,
				    GdkEventButton *bevent,
				    gpointer        data)
{
  if (bevent->button == 2)
    {
1266
      GSList *list = NULL;
1267
1268
1269
1270
1271
1272
1273
      gint row;
      gint column;

      gtk_clist_get_selection_info (GTK_CLIST (g_editor->clist),
                                    bevent->x, bevent->y,
                                    &row, &column);

1274
1275
1276
1277
1278
1279
1280
      if (gradients_list)
	list = g_slist_nth (gradients_list, row);

      if (list)
	dnd_gradient = (gradient_t *) list->data;
      else
	dnd_gradient = NULL;
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294

      return TRUE;
    }

  return FALSE;
}

static gradient_t *
gradient_editor_drag_gradient (GtkWidget  *widget,
			       gpointer    data)
{
  return dnd_gradient;
}

Michael Natterer's avatar
Michael Natterer committed
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
static void
gradient_editor_drop_gradient (GtkWidget  *widget,
			       gradient_t *gradient,
			       gpointer    data)
{
  gradient_editor_set_gradient (gradient);
}

/*****/

1305
static void
1306
1307
1308
1309
ed_fetch_foreground (double *fg_r,
		     double *fg_g,
		     double *fg_b,
		     double *fg_a)
1310
{
1311
  guchar r, g, b;
1312
	
1313
  gimp_context_get_foreground (gimp_context_get_user (), &r, &g, &b);
1314
 	
1315
1316
1317
1318
1319
  *fg_r = (double) r / 255.0;
  *fg_g = (double) g / 255.0;
  *fg_b = (double) b / 255.0;
  *fg_a = 1.0;                 /* opacity 100 % */
}
1320

Elliot Lee's avatar
Elliot Lee committed
1321
1322
1323
/*****/

static void
1324
ed_update_editor (int flags)
Elliot Lee's avatar
Elliot Lee committed
1325
{
1326
1327
1328
1329
1330
1331
  if (flags & GRAD_UPDATE_GRADIENT)
    {
      prev_update (TRUE);
      gradient_update ();
    }

1332
1333
  if (flags & GRAD_UPDATE_PREVIEW)
    prev_update (TRUE);
Elliot Lee's avatar
Elliot Lee committed
1334

1335
  if (flags & GRAD_UPDATE_CONTROL)
1336
    control_update (FALSE);
Elliot Lee's avatar
Elliot Lee committed
1337

1338
1339
1340
  if (flags & GRAD_RESET_CONTROL)
    control_update (TRUE);
}
Elliot Lee's avatar
Elliot Lee committed
1341
1342
1343
1344

/*****/

static GtkWidget *
1345
1346
1347
1348
1349
ed_create_button (gchar         *label,
		  double         xalign,
		  double         yalign,
		  GtkSignalFunc  signal_func,
		  gpointer       user_data)
Elliot Lee's avatar
Elliot Lee committed
1350
{
1351
1352
  GtkWidget *button;
  GtkWidget *text;
Elliot Lee's avatar
Elliot Lee committed
1353

1354
1355
  button = gtk_button_new ();
  text   = gtk_label_new (label);
Elliot Lee's avatar
Elliot Lee committed
1356

1357
1358
1359
1360
  gtk_misc_set_alignment (GTK_MISC (text), xalign, yalign);
  gtk_misc_set_padding (GTK_MISC (text), 4, 0);
  gtk_container_add (GTK_CONTAINER (button), text);
  gtk_widget_show (text);
Elliot Lee's avatar
Elliot Lee committed
1361

1362
1363
1364
1365
  if (signal_func != NULL)
    gtk_signal_connect (GTK_OBJECT (button), "clicked",
			(GtkSignalFunc) signal_func,
			user_data);
Elliot Lee's avatar
Elliot Lee committed
1366

1367
1368
  return button;
}
Elliot Lee's avatar
Elliot Lee committed
1369
1370
1371
1372

/*****/

static void
1373
ed_set_hint (gchar *str)
Elliot Lee's avatar
Elliot Lee committed
1374
{
1375
1376
1377
  gtk_label_set_text (GTK_LABEL (g_editor->hint_label), str);
  gdk_flush();
}
Elliot Lee's avatar
Elliot Lee committed
1378
1379
1380

/*****/

1381
static void
Michael Natterer's avatar
Michael Natterer committed
1382
1383
1384
1385
1386
1387
gradient_clist_fill_preview (gradient_t *gradient,
			     guchar     *buf,
			     gint        width,
			     gint        height,
			     gdouble     left,
			     gdouble     right)
1388
1389
{
  guchar *p0, *p1,*even,*odd;
Michael Natterer's avatar
Michael Natterer committed
1390
1391
1392
1393
  gint     x, y;
  gdouble  dx, cur_x;
  gdouble  r, g, b, a;
  gdouble  c0, c1;
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403

  dx    = (right - left) / (width - 1);
  cur_x = left;
  p0    = even = g_malloc (width * 3);
  p1    = odd  = g_malloc (width * 3);

  /* Create lines to fill the image */

  for (x = 0; x < width; x++)
    {
Michael Natterer's avatar
Michael Natterer committed
1404
      gradient_get_color_at (gradient, cur_x, &r, &g, &b, &a);
1405

Michael Natterer's avatar
Michael Natterer committed
1406
      if ((x / GIMP_CHECK_SIZE_SM) & 1)
1407
	{
Michael Natterer's avatar
Michael Natterer committed
1408
1409
	  c0 = GIMP_CHECK_LIGHT;
	  c1 = GIMP_CHECK_DARK;