gradient_editor.c 154 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 19 20 21 22
/* 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
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


23 24 25 26 27
/* hof: Hofer Wolfgang, 1998.01.27   avoid resize bug by keeping 
 *                                   preview widgetsize constant.
 * hof: Hofer Wolfgang, 1997.10.17   Added "Load From FG color"
 */

Elliot Lee's avatar
Elliot Lee committed
28 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/* 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.
 *
 * - Add Reload function.
 *
 * - See that the default-gradient really gets selected on startup.
 *
 * - Provide a way of renaming a gradient (instead of having to do
 *   copy/delete).
 *
 * - Fix the flicker in the hint bar.
 *
 * - Maybe add a little preview of each gradient to the listbox?
 *
 * - 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).
 */


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "appenv.h"
#include "colormaps.h"
#include "cursorutil.h"
#include "datafiles.h"
#include "errors.h"
#include "general.h"
#include "gimprc.h"
#include "gradient.h"
#include "interface.h"
#include "palette.h"


/***** Magic numbers *****/

Manish Singh's avatar
Manish Singh committed
170 171 172 173
#ifndef M_PI
#define M_PI    3.14159265358979323846
#endif /* M_PI */

Elliot Lee's avatar
Elliot Lee committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
#define EPSILON 1e-10

#define GRAD_LIST_WIDTH  300
#define GRAD_LIST_HEIGHT 80

#define GRAD_SCROLLBAR_STEP_SIZE 0.05
#define GRAD_SCROLLBAR_PAGE_SIZE 0.75

#define GRAD_CLOSE_BUTTON_WIDTH 45
#define GRAD_PREVIEW_WIDTH      600
#define GRAD_PREVIEW_HEIGHT     64
#define GRAD_CONTROL_HEIGHT     10

#define GRAD_CHECK_SIZE 8

#define GRAD_CHECK_DARK  (1.0 / 3.0)
#define GRAD_CHECK_LIGHT (2.0 / 3.0)

#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 */

#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 |	\
				 GDK_BUTTON1_MOTION_MASK)

#define GRAD_UPDATE_PREVIEW   0x0001
#define GRAD_UPDATE_CONTROL   0x0002
#define GRAD_RESET_CONTROL    0X0004


/***** Types *****/

/* Gradient segment type */

typedef enum {
	GRAD_LINEAR = 0,
	GRAD_CURVED,
	GRAD_SINE,
	GRAD_SPHERE_INCREASING,
	GRAD_SPHERE_DECREASING
} grad_type_t;

typedef enum {
	GRAD_RGB = 0,  /* normal RGB */
	GRAD_HSV_CCW,  /* counterclockwise hue */
	GRAD_HSV_CW    /* clockwise hue */
} grad_color_t;

typedef struct _grad_segment_t {
	double       left, middle, right; /* Left pos, midpoint, right pos */
	double       r0, g0, b0, a0;   	/* Left color */
	double       r1, g1, b1, a1;   	/* Right color */
	grad_type_t  type;             	/* Segment's blending function */
	grad_color_t color;             /* Segment's coloring type */

	struct _grad_segment_t *prev, *next; /* For linked list of segments */
} grad_segment_t;


/* Complete gradient type */

typedef struct _gradient_t {
	char           *name;
	grad_segment_t *segments;
	grad_segment_t *last_visited;
	int             dirty;
	char           *filename;
	GtkWidget      *list_item;
} gradient_t;


/* 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;
	GtkWidget *hint_label;
	GtkWidget *list;
	GtkWidget *scrollbar;
	GtkWidget *preview;
	GtkWidget *control;

	/* Zoom and scrollbar */

	unsigned int  zoom_factor;
	GtkObject    *scroll_data;

	/* Instant update */

	int instant_update;

	/* Gradient preview */

	guchar   *preview_rows[2]; /* For caching redraw info */
	gint      preview_last_x;
	int       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 */
	int                  control_compress;       /* Compressing/expanding handles */
	gint                 control_last_x;         /* Last mouse position when dragging */
	double               control_last_gx;        /* Last position (wrt gradient) when dragging */
	double               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 */

	GtkAcceleratorTable *accelerator_table;

	/* 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 */

	int split_parts;

	/* Replicate dialog */

	int replicate_times;

	/* Saved colors */

	struct {
		double r, g, b, a;
	} saved_colors[GRAD_NUM_COLORS];

344 345
	GtkWidget *left_load_color_boxes[GRAD_NUM_COLORS + 3];
	GtkWidget *left_load_labels[GRAD_NUM_COLORS + 3];
Elliot Lee's avatar
Elliot Lee committed
346 347 348 349

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

350 351
	GtkWidget *right_load_color_boxes[GRAD_NUM_COLORS + 3];
	GtkWidget *right_load_labels[GRAD_NUM_COLORS + 3];
Elliot Lee's avatar
Elliot Lee committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

	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;
	int             left_saved_dirty;

	GtkWidget      *right_color_preview;
	grad_segment_t *right_saved_segments;
	int             right_saved_dirty;
} gradient_editor_t;


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

/* Gradient editor functions */
371
static void       ed_fetch_foreground(double *fg_r, double *fg_g, double *fg_b, double *fg_a);
Elliot Lee's avatar
Elliot Lee committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
static void       ed_update_editor(int flags);

static GtkWidget *ed_create_button(gchar *label, double xalign, double yalign,
				   GtkSignalFunc signal_func, gpointer user_data);

static void       ed_set_hint(char *str);

static void       ed_set_list_of_gradients(void);
static void       ed_insert_in_gradients_listbox(gradient_t *grad, int pos, int select);
static void       ed_list_item_update(GtkWidget *widget, gpointer data);

static void       ed_initialize_saved_colors(void);

static gint       ed_close_callback(GtkWidget *widget, gpointer client_data);
static void       ed_refresh_callback(GtkWidget *widget, gpointer client_data);
static void       ed_new_gradient_callback(GtkWidget *widget, gpointer client_data);
static void       ed_do_new_gradient_callback(GtkWidget *widget, gpointer client_data, gpointer call_data);
static void       ed_copy_gradient_callback(GtkWidget *widget, gpointer client_data);
static void       ed_do_copy_gradient_callback(GtkWidget *widget, gpointer client_data, gpointer call_data);
static void       ed_delete_gradient_callback(GtkWidget *widget, gpointer client_data);
static void       ed_do_delete_gradient_callback(GtkWidget *widget, gpointer client_data);
static void       ed_cancel_delete_gradient_callback(GtkWidget *widget, gpointer client_data);
static void       ed_save_pov_callback(GtkWidget *widget, gpointer client_data);
static void       ed_do_save_pov_callback(GtkWidget *widget, gpointer client_data);
static void       ed_cancel_save_pov_callback(GtkWidget *widget, gpointer client_data);
static void       ed_scrollbar_update(GtkAdjustment *adjustment, gpointer data);
static void       ed_zoom_all_callback(GtkWidget *widget, gpointer client_data);
static void       ed_zoom_out_callback(GtkWidget *widget, gpointer client_data);
static void       ed_zoom_in_callback(GtkWidget *widget, gpointer client_data);
static void       ed_instant_update_update(GtkWidget *widget, gpointer data);

/* Gradient preview functions */

static gint prev_events(GtkWidget *widget, GdkEvent *event);
static void prev_set_hint(gint x);
static void prev_set_foreground(gint x);
static void prev_update(int recalculate);
static void prev_fill_image(int width, int height, double left, double right);

/* Gradient control functions */

static gint   control_events(GtkWidget *widget, GdkEvent *event);
static void   control_do_hint(gint x, gint y);
static void   control_button_press(gint x, gint y, guint button, guint state);
static int    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, double pos);
static void   control_compress_range(grad_segment_t *range_l, grad_segment_t *range_r,
				     double new_l, double new_r);

static double control_move(grad_segment_t *range_l, grad_segment_t *range_r, double delta);

static void   control_update(int 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);

/* Control popup functions */

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 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(int *equal_blending, int *equal_coloring);
static GtkWidget *cpopup_create_menu_item_with_label(char *str, GtkWidget **label);
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,
					  char *label1, char *label2, GtkSignalFunc callback,
					  gchar accel_key_0, guint8 accel_mods_0,
451 452
					  gchar accel_key_1, guint8 accel_mods_1,
					  gchar accel_key_2, guint8 accel_mods_2);
Elliot Lee's avatar
Elliot Lee committed
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 557 558 559 560 561 562 563 564 565 566 567
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 *widget, gpointer data);
static void       cpopup_save_left_callback(GtkWidget *widget, gpointer data);
static void       cpopup_load_right_callback(GtkWidget *widget, gpointer data);
static void       cpopup_save_right_callback(GtkWidget *widget, gpointer data);

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

static GtkWidget *cpopup_create_sel_ops_menu(void);

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

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 void       cpopup_create_color_dialog(char *title, double r, double g, double b, double a,
					     GtkSignalFunc color_changed_callback,
					     GtkSignalFunc ok_callback,
					     GtkSignalFunc cancel_callback);

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_set_left_color_callback(GtkWidget *widget, gpointer data);
static void       cpopup_left_color_changed(GtkWidget *widget, gpointer client_data);
static void       cpopup_left_color_dialog_ok(GtkWidget *widget, gpointer client_data);
static void       cpopup_left_color_dialog_cancel(GtkWidget *widget, gpointer client_data);
static void       cpopup_set_right_color_callback(GtkWidget *widget, gpointer data);
static void       cpopup_right_color_changed(GtkWidget *widget, gpointer client_data);
static void       cpopup_right_color_dialog_ok(GtkWidget *widget, gpointer client_data);
static void       cpopup_right_color_dialog_cancel(GtkWidget *widget, gpointer client_data);

static void       cpopup_split_midpoint_callback(GtkWidget *widget, gpointer data);
static void       cpopup_split_uniform_callback(GtkWidget *widget, gpointer data);
static void       cpopup_delete_callback(GtkWidget *widget, gpointer data);
static void       cpopup_recenter_callback(GtkWidget *widget, gpointer data);
static void       cpopup_redistribute_callback(GtkWidget *widget, gpointer data);
static void       cpopup_flip_callback(GtkWidget *widget, gpointer data);
static void       cpopup_replicate_callback(GtkWidget *widget, gpointer data);

static void       cpopup_split_uniform_scale_update(GtkAdjustment *adjustment, gpointer data);
static void       cpopup_split_uniform_split_callback(GtkWidget *widget, gpointer client_data);
static void       cpopup_split_uniform_cancel_callback(GtkWidget *widget, gpointer client_data);

static void       cpopup_replicate_scale_update(GtkAdjustment *adjustment, gpointer data);
static void       cpopup_do_replicate_callback(GtkWidget *widget, gpointer client_data);
static void       cpopup_replicate_cancel_callback(GtkWidget *widget, gpointer client_data);

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);
static void 	  cpopup_split_midpoint(grad_segment_t *lseg, grad_segment_t **newl, grad_segment_t **newr);
static void 	  cpopup_split_uniform(grad_segment_t *lseg, int parts,
				       grad_segment_t **newl, grad_segment_t **newr);

/* Gradient functions */

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

static gradient_t *grad_create_default_gradient(void);

static int         grad_insert_in_gradients_list(gradient_t *grad);

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

/* Segment functions */

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

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

/* Calculation functions */

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

static void calc_rgb_to_hsv(double *r, double *g, double *b);
static void calc_hsv_to_rgb(double *h, double *s, double *v);

/* Files and paths functions */

static char *build_user_filename(char *name, char *path_str);

/* Procedural database functions */

static Argument *gradients_get_list_invoker(Argument *args);
static Argument *gradients_get_active_invoker(Argument *args);
static Argument *gradients_set_active_invoker(Argument *args);
static Argument *gradients_sample_uniform_invoker(Argument *args);
static Argument *gradients_sample_custom_invoker(Argument *args);


/***** Local variables *****/

static int         num_gradients         = 0;
568
static GSList     *gradients_list        = NULL; /* The list of gradients */
Elliot Lee's avatar
Elliot Lee committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 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 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
static gradient_t *curr_gradient         = NULL; /* The active gradient */
static gradient_t *grad_default_gradient = NULL;

static gradient_editor_t *g_editor       = NULL; /* The gradient editor */

static char *blending_types[] = {
	"Linear",
	"Curved",
	"Sinuosidal",
	"Spherical (increasing)",
	"Spherical (decreasing)"
}; /* blending_types */

static char *coloring_types[] = {
	"Plain RGB",
	"HSV (counter-clockwise hue)",
	"HSV (clockwise hue)"
}; /* coloring_types */


/***** Public functions *****/

/*****/

void
gradients_init(void)
{
	datafiles_read_directories(gradient_path, grad_load_gradient, 0);

	if (grad_default_gradient != NULL)
		curr_gradient = grad_default_gradient;
	else if (gradients_list != NULL)
		curr_gradient = (gradient_t *) gradients_list->data;
	else {
		curr_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;

		grad_insert_in_gradients_list(curr_gradient);
	} /* else */
} /* gradients_init */


/*****/

void
gradients_free(void)
{
	grad_free_gradients();
} /* gradients_free */


/*****/

void
grad_get_color_at(double pos, double *r, double *g, double *b, double *a)
{
	double          factor;
	grad_segment_t *seg;
	double          seg_len, middle;
	double          h0, s0, v0;
	double          h1, s1, v1;

	if (pos < 0.0)
		pos = 0.0;
	else if (pos > 1.0)
		pos = 1.0;

	seg = seg_get_segment_at(curr_gradient, pos);

	seg_len = seg->right - seg->left;

	if (seg_len < EPSILON) {
		middle = 0.5;
		pos    = 0.5;
	} else {
		middle = (seg->middle - seg->left) / seg_len;
		pos    = (pos - seg->left) / seg_len;
	} /* else */

	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:
			grad_dump_gradient(curr_gradient, stderr);
			fatal_error("grad_get_color_at(): aieee, unknown gradient type %d", (int) seg->type);
			factor = 0.0; /* Shut up -Wall */
			break;
	} /* switch */

	/* Calculate color components */

	*a = seg->a0 + (seg->a1 - seg->a0) * factor;

	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;

		h1 = seg->r1;
		s1 = seg->g1;
		v1 = seg->b1;

		calc_rgb_to_hsv(&h0, &s0, &v0);
		calc_rgb_to_hsv(&h1, &s1, &v1);

		s0 = s0 + (s1 - s0) * factor;
		v0 = v0 + (v1 - v0) * factor;

		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;
				} /* else */

				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;
				} /* else */

				break;

			default:
				grad_dump_gradient(curr_gradient, stderr);
				fatal_error("grad_get_color_at(): aieee, unknown coloring mode %d",
					    (int) seg->color);
				break;
		} /* switch */

		*r = h0;
		*g = s0;
		*b = v0;

		calc_hsv_to_rgb(r, g, b);
	} /* else */
} /* grad_get_color_at */


/*****/

void
grad_create_gradient_editor(void)
{
	GtkWidget *topvbox;
	GtkWidget *vbox;
	GtkWidget *table;
	GtkWidget *label;
	GtkWidget *hbox;
	GtkWidget *listbox;
	GtkWidget *gvbox;
	GtkWidget *button;
	GtkWidget *frame;
	GtkWidget *separator;
	int        i;

	/* If the editor already exists, just show it */

	if (g_editor) {
		if (!GTK_WIDGET_VISIBLE(g_editor->shell))
			gtk_widget_show(g_editor->shell);
		else
			gdk_window_raise(g_editor->shell->window);

		return;
	} /* if */

	/* Create editor */

	g_editor = g_malloc(sizeof(gradient_editor_t));

	/* Shell and main vbox */

	g_editor->shell = gtk_window_new(GTK_WINDOW_TOPLEVEL);
775
	gtk_window_set_wmclass (GTK_WINDOW(g_editor->shell), "gradient_editor", "Gimp");
Elliot Lee's avatar
Elliot Lee committed
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 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 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
	gtk_container_border_width(GTK_CONTAINER(g_editor->shell), 0);
	gtk_window_set_title(GTK_WINDOW(g_editor->shell), "Gradient Editor");
	gtk_window_position(GTK_WINDOW(g_editor->shell), GTK_WIN_POS_CENTER);

	/* handle window manager close signals */
	gtk_signal_connect (GTK_OBJECT (g_editor->shell), "delete_event",
			    GTK_SIGNAL_FUNC (ed_close_callback),
			    NULL);

	topvbox = gtk_vbox_new(FALSE, 0);
	gtk_container_border_width(GTK_CONTAINER(topvbox), 0);
	gtk_container_add(GTK_CONTAINER(g_editor->shell), topvbox);
	gtk_widget_show(topvbox);

	/* Hint bar and close button */

	table = gtk_table_new(2, 2, FALSE);
	gtk_container_border_width(GTK_CONTAINER(table), 0);
	gtk_box_pack_end(GTK_BOX(topvbox), table, FALSE, FALSE, 0);
	gtk_widget_show(table);

	separator = gtk_hseparator_new();
	gtk_table_attach(GTK_TABLE(table), separator, 0, 1, 0, 1, GTK_EXPAND | GTK_FILL, 0, 0, 0);
	gtk_widget_show(separator);

	g_editor->hint_label = gtk_label_new("");
	gtk_misc_set_alignment(GTK_MISC(g_editor->hint_label), 0.0, 0.5);
	gtk_table_attach(GTK_TABLE(table), g_editor->hint_label, 0, 1, 1, 2,
			 GTK_EXPAND | GTK_FILL, GTK_FILL, 8, 2);
	gtk_widget_show(g_editor->hint_label);

	button = ed_create_button("Close", 0.5, 0.5, (GtkSignalFunc) ed_close_callback, NULL);
	gtk_widget_set_usize(button, GRAD_CLOSE_BUTTON_WIDTH, 0);
	gtk_table_attach(GTK_TABLE(table), button, 1, 2, 0, 2, GTK_FILL, GTK_EXPAND | GTK_FILL, 8, 0);
	gtk_widget_show(button);

	/* Vbox for everything else */

	vbox = gtk_vbox_new(FALSE, 0);
	gtk_container_border_width(GTK_CONTAINER(vbox), 8);
	gtk_box_pack_start(GTK_BOX(topvbox), vbox, TRUE, TRUE, 0);
	gtk_widget_show(vbox);

	/* Gradients list box */

	label = gtk_label_new("Gradients");
	gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
	gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
	gtk_widget_show(label);

	hbox = gtk_hbox_new(FALSE, 8);
	gtk_container_border_width(GTK_CONTAINER(hbox), 0);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
	gtk_widget_show(hbox);

	listbox = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(listbox),
				       GTK_POLICY_AUTOMATIC,
				       GTK_POLICY_ALWAYS);
	gtk_widget_set_usize(listbox, GRAD_LIST_WIDTH, GRAD_LIST_HEIGHT);
	gtk_box_pack_start(GTK_BOX(hbox), listbox, TRUE, TRUE, 0);
	gtk_widget_show(listbox);

	g_editor->list = gtk_list_new();
	gtk_list_set_selection_mode(GTK_LIST(g_editor->list), GTK_SELECTION_BROWSE);
	gtk_container_add(GTK_CONTAINER(listbox), g_editor->list);
	gtk_widget_show(g_editor->list);

	/* Buttons for gradient functions */

	gvbox = gtk_vbox_new(FALSE, 0);
	gtk_container_border_width(GTK_CONTAINER(gvbox), 0);
	gtk_box_pack_end(GTK_BOX(hbox), gvbox, FALSE, FALSE, 0);
	gtk_widget_show(gvbox);

	/* Buttons for gradient functions */

	button = ed_create_button("New gradient", 0.0, 0.5,
				  (GtkSignalFunc) ed_new_gradient_callback, NULL);
	gtk_box_pack_start(GTK_BOX(gvbox), button, TRUE, TRUE, 0);
	gtk_widget_show(button);

	button = ed_create_button("Copy gradient", 0.0, 0.5,
				  (GtkSignalFunc) ed_copy_gradient_callback, NULL);
	gtk_box_pack_start(GTK_BOX(gvbox), button, TRUE, TRUE, 0);
	gtk_widget_show(button);

	button = ed_create_button("Delete gradient", 0.0, 0.5,
				  (GtkSignalFunc) ed_delete_gradient_callback, NULL);
	gtk_box_pack_start(GTK_BOX(gvbox), button, TRUE, TRUE, 0);
	gtk_widget_show(button);

	button = ed_create_button("Save as POV-Ray", 0.0, 0.5,
				  (GtkSignalFunc) ed_save_pov_callback, NULL);
	gtk_box_pack_start(GTK_BOX(gvbox), button, TRUE, TRUE, 0);
	gtk_widget_show(button);

	button = ed_create_button("Refresh gradients", 0.0, 0.5,
				  (GtkSignalFunc) ed_refresh_callback, NULL);
	gtk_box_pack_start(GTK_BOX(gvbox), button, TRUE, TRUE, 0);
	gtk_widget_show(button);

	/* Horizontal box for zoom controls, scrollbar, and instant update toggle */

	hbox = gtk_hbox_new(FALSE, 0);
	gtk_container_border_width(GTK_CONTAINER(hbox), 0);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 4);
	gtk_widget_show(hbox);

	/* Zoom buttons */

	button = ed_create_button("Zoom all", 0.5, 0.5, (GtkSignalFunc) ed_zoom_all_callback, g_editor);
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
	gtk_widget_show(button);

	button = ed_create_button("Zoom -", 0.5, 0.5, (GtkSignalFunc) ed_zoom_out_callback, g_editor);
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
	gtk_widget_show(button);

	button = ed_create_button("Zoom +", 0.5, 0.5, (GtkSignalFunc) ed_zoom_in_callback, g_editor);
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
	gtk_widget_show(button);

	/* Scrollbar */

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

	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, 4);
	gtk_widget_show(g_editor->scrollbar);

	/* Instant update toggle */

	g_editor->instant_update = 1;

	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_state(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);
	gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
	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;
	g_editor->preview_button_down = 0;

	g_editor->preview = gtk_preview_new(GTK_PREVIEW_COLOR);
	gtk_preview_size(GTK_PREVIEW(g_editor->preview),
			 GRAD_PREVIEW_WIDTH, GRAD_PREVIEW_HEIGHT);
	gtk_widget_set_events(g_editor->preview, GRAD_PREVIEW_EVENT_MASK);
	gtk_signal_connect(GTK_OBJECT(g_editor->preview), "event",
			   (GtkSignalFunc) prev_events,
			   g_editor);
	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;
	g_editor->control_compress                = 0;
	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->accelerator_table = NULL;

	for (i = 0;
	     i < (sizeof(g_editor->control_blending_items) / sizeof(g_editor->control_blending_items[0]));
	     i++)
		g_editor->control_blending_items[i] = NULL;

	for (i = 0;
	     i < (sizeof(g_editor->control_coloring_items) / sizeof(g_editor->control_coloring_items[0]));
	     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",
			   (GtkSignalFunc) control_events,
			   g_editor);
	gtk_box_pack_start(GTK_BOX(gvbox), g_editor->control, FALSE, TRUE, 0);
	gtk_widget_show(g_editor->control);

	/* Initialize other data */

	g_editor->left_color_preview            = NULL;
	g_editor->left_saved_segments           = NULL;
	g_editor->left_saved_dirty              = 0;

	g_editor->right_color_preview           = NULL;
	g_editor->right_saved_segments          = NULL;
	g_editor->right_saved_dirty             = 0;

	ed_initialize_saved_colors();
	cpopup_create_main_menu();

	/* Show everything */

	ed_set_list_of_gradients();

	gtk_widget_show(g_editor->shell);
} /* grad_create_gradient_editor */


/*****/

void
grad_free_gradient_editor(void)
{
	/* FIXME */
} /* grad_free_gradient_editor */


/***** Gradient editor functions *****/

1047 1048
/*****/

1049
static void
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
ed_fetch_foreground(double *fg_r, double *fg_g, double *fg_b, double *fg_a)
{
	unsigned char r, g, b;
	
 	palette_get_foreground (&r, &g, &b);
 	
 	*fg_r = (double) r / 255.0;
 	*fg_g = (double) g / 255.0;
 	*fg_b = (double) b / 255.0;
	*fg_a = 1.0;                 /* opacity 100 % */
} /* ed_fetch_foreground */


Elliot Lee's avatar
Elliot Lee committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
/*****/

static void
ed_update_editor(int flags)
{
	if (flags & GRAD_UPDATE_PREVIEW)
		prev_update(1);

	if (flags & GRAD_UPDATE_CONTROL)
		control_update(0);

	if (flags & GRAD_RESET_CONTROL)
		control_update(1);
} /* ed_update_editor */


/*****/

static GtkWidget *
ed_create_button(gchar *label, double xalign, double yalign, GtkSignalFunc signal_func, gpointer user_data)
{
	GtkWidget *button;
	GtkWidget *text;

	button = gtk_button_new();
	text   = gtk_label_new(label);

	gtk_misc_set_alignment(GTK_MISC(text), xalign, yalign);
	gtk_container_add(GTK_CONTAINER(button), text);
	gtk_widget_show(text);

	if (signal_func != NULL)
		gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC (signal_func), user_data);

	return button;
} /* ed_create_button */


/*****/

static void
ed_set_hint(char *str)
{
	gtk_label_set(GTK_LABEL(g_editor->hint_label), str);
	gdk_flush();
} /* ed_set_hint */


/*****/

static void
ed_set_list_of_gradients(void)
{
1116
	GSList     *list;
Elliot Lee's avatar
Elliot Lee committed
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	gradient_t *grad;
	int         n;

	list = gradients_list;
	n    = 0;

	while (list) {
		grad = list->data;

		if (grad == curr_gradient)
			ed_insert_in_gradients_listbox(grad, n, 1);
		else
			ed_insert_in_gradients_listbox(grad, n, 0);

1131
		list = g_slist_next(list);
Elliot Lee's avatar
Elliot Lee committed
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 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
		n++;
	} /* while */
} /* ed_set_list_of_gradients */


/*****/

static void
ed_insert_in_gradients_listbox(gradient_t *grad, int pos, int select)
{
	GtkWidget *list_item;
	GList     *list;

	list_item       = gtk_list_item_new_with_label(grad->name);
	grad->list_item = list_item;
	gtk_signal_connect(GTK_OBJECT(list_item), "select",
			   (GtkSignalFunc) ed_list_item_update,
			   (gpointer) grad);
	gtk_widget_show(list_item);

	list = g_list_append(NULL, list_item);

	gtk_list_insert_items(GTK_LIST(g_editor->list), list, pos);

	if (select)
		gtk_list_select_item(GTK_LIST(g_editor->list), pos);
} /* ed_insert_in_gradients_listbox */


/*****/

static void
ed_list_item_update(GtkWidget *widget, gpointer data)
{
	/* If this is not the selected item, do nothing */

	if (widget->state != GTK_STATE_SELECTED)
		return;

	/* Update current gradient */

	curr_gradient = (gradient_t *) data;

	ed_update_editor(GRAD_UPDATE_PREVIEW | GRAD_RESET_CONTROL);
} /* ed_list_item_update */


/*****/

static void
ed_initialize_saved_colors(void)
{
	int i;

1186
	for (i = 0; i < (GRAD_NUM_COLORS + 3); i++) {
Elliot Lee's avatar
Elliot Lee committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
		g_editor->left_load_color_boxes[i] = NULL;
		g_editor->left_load_labels[i]      = NULL;

		g_editor->right_load_color_boxes[i] = NULL;
		g_editor->right_load_labels[i]      = NULL;
	} /* for */

	for (i = 0; i < GRAD_NUM_COLORS; i++) {
		g_editor->left_save_color_boxes[i] = NULL;
		g_editor->left_save_labels[i]      = NULL;

		g_editor->right_save_color_boxes[i] = NULL;
		g_editor->right_save_labels[i]      = NULL;
	} /* for */

	g_editor->saved_colors[0].r = 0.0; /* Black */
	g_editor->saved_colors[0].g = 0.0;
	g_editor->saved_colors[0].b = 0.0;
	g_editor->saved_colors[0].a = 1.0;

	g_editor->saved_colors[1].r = 0.5; /* 50% Gray */
	g_editor->saved_colors[1].g = 0.5;
	g_editor->saved_colors[1].b = 0.5;
	g_editor->saved_colors[1].a = 1.0;

	g_editor->saved_colors[2].r = 1.0; /* White */
	g_editor->saved_colors[2].g = 1.0;
	g_editor->saved_colors[2].b = 1.0;
	g_editor->saved_colors[2].a = 1.0;

	g_editor->saved_colors[3].r = 0.0; /* Clear */
	g_editor->saved_colors[3].g = 0.0;
	g_editor->saved_colors[3].b = 0.0;
	g_editor->saved_colors[3].a = 0.0;

	g_editor->saved_colors[4].r = 1.0; /* Red */
	g_editor->saved_colors[4].g = 0.0;
	g_editor->saved_colors[4].b = 0.0;
	g_editor->saved_colors[4].a = 1.0;

	g_editor->saved_colors[5].r = 1.0; /* Yellow */
	g_editor->saved_colors[5].g = 1.0;
	g_editor->saved_colors[5].b = 0.0;
	g_editor->saved_colors[5].a = 1.0;

	g_editor->saved_colors[6].r = 0.0; /* Green */
	g_editor->saved_colors[6].g = 1.0;
	g_editor->saved_colors[6].b = 0.0;
	g_editor->saved_colors[6].a = 1.0;

	g_editor->saved_colors[7].r = 0.0; /* Cyan */
	g_editor->saved_colors[7].g = 1.0;
	g_editor->saved_colors[7].b = 1.0;
	g_editor->saved_colors[7].a = 1.0;

	g_editor->saved_colors[8].r = 0.0; /* Blue */
	g_editor->saved_colors[8].g = 0.0;
	g_editor->saved_colors[8].b = 1.0;
	g_editor->saved_colors[8].a = 1.0;

	g_editor->saved_colors[9].r = 1.0; /* Magenta */
	g_editor->saved_colors[9].g = 0.0;
	g_editor->saved_colors[9].b = 1.0;
	g_editor->saved_colors[9].a = 1.0;
} /* ed_initialize_saved_colors */


/*****/

static gint
ed_close_callback(GtkWidget *widget, gpointer client_data)
{
	if (GTK_WIDGET_VISIBLE(g_editor->shell))
		gtk_widget_hide(g_editor->shell);

1262
	return TRUE;
Elliot Lee's avatar
Elliot Lee committed
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
} /* ed_close_callback */


/*****/

static void
ed_new_gradient_callback(GtkWidget *widget, gpointer client_data)
{
	query_string_box("New gradient",
			 "Enter a name for the new gradient",
			 "untitled",
			 ed_do_new_gradient_callback, NULL);
} /* ed_new_gradient_callback */


/*****/

static void
ed_do_new_gradient_callback(GtkWidget *widget, gpointer client_data, gpointer call_data)
{
	gradient_t *grad;
	char       *gradient_name;
	int         pos;

	gradient_name = (char *) call_data;

	if (!gradient_name) {
		warning("ed_do_new_gradient_callback(): oops, received NULL in call_data");
		return;
	} /* if */

	grad = grad_create_default_gradient();

	grad->name     = gradient_name; /* We don't need to copy since this memory is ours */
	grad->dirty    = 1;
	grad->filename = build_user_filename(grad->name, gradient_path);

	/* Put new gradient in list */

	pos = grad_insert_in_gradients_list(grad);
	ed_insert_in_gradients_listbox(grad, pos, 1);

	curr_gradient = grad;

	ed_update_editor(GRAD_UPDATE_PREVIEW | GRAD_RESET_CONTROL);
} /* ed_do_new_gradient_callback */


/*****/

static void
ed_copy_gradient_callback(GtkWidget *widget, gpointer client_data)
{
	char *name;

1318 1319 1320
	if (curr_gradient == NULL) 
               return;

Elliot Lee's avatar
Elliot Lee committed
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
	name = g_malloc((strlen(curr_gradient->name) + 6) * sizeof(char));

	sprintf(name, "%s copy", curr_gradient->name);

	query_string_box("Copy gradient",
			 "Enter a name for the copied gradient",
			 name,
			 ed_do_copy_gradient_callback, NULL);

	g_free(name);
} /* ed_copy_gradient_callback */


/*****/

static void
ed_do_copy_gradient_callback(GtkWidget *widget, gpointer client_data, gpointer call_data)
{
	gradient_t     *grad;
	char           *gradient_name;
	int             pos;
	grad_segment_t *head, *prev, *cur, *orig;

	gradient_name = (char *) call_data;

	if (!gradient_name) {
		warning("ed_do_copy_gradient_callback(): oops, received NULL in call_data");
		return;
	} /* if */

	/* Copy current gradient */

	grad = grad_new_gradient();

	grad->name     = gradient_name; /* We don't need to copy since this memory is ours */
	grad->dirty    = 1;
	grad->filename = build_user_filename(grad->name, gradient_path);

	prev = NULL;
	orig = curr_gradient->segments;
	head = NULL;

	while (orig) {
		cur = seg_new_segment();

		*cur = *orig; /* Copy everything */

		cur->prev = prev;
		cur->next = NULL;

		if (prev)
			prev->next = cur;
		else
			head = cur; /* Remember head */

		prev = cur;
		orig = orig->next;
	} /* while */

	grad->segments = head;

	/* Put new gradient in list */

	pos = grad_insert_in_gradients_list(grad);
	ed_insert_in_gradients_listbox(grad, pos, 1);

	curr_gradient = grad;

	ed_update_editor(GRAD_UPDATE_PREVIEW | GRAD_RESET_CONTROL);
} /* ed_do_copy_gradient_callback */


/*****/

static void
ed_delete_gradient_callback(GtkWidget *widget, gpointer client_data)
{
	GtkWidget *dialog;
	GtkWidget *vbox;
	GtkWidget *label;
	GtkWidget *button;
	char      *str;

	if (num_gradients <= 1)
		return;

	dialog = gtk_dialog_new();
	gtk_window_set_title(GTK_WINDOW(dialog), "Delete gradient");
	gtk_window_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
	gtk_container_border_width(GTK_CONTAINER(dialog), 0);

	vbox = gtk_vbox_new(FALSE, 0);
	gtk_container_border_width(GTK_CONTAINER(vbox), 8);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
			   FALSE, FALSE, 0);
	gtk_widget_show(vbox);

	/* Question */

	label = gtk_label_new("Are you sure you want to delete");
	gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
	gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
	gtk_widget_show(label);

	str = g_malloc((strlen(curr_gradient->name) + 32 * sizeof(char)));
	sprintf(str, "\"%s\" from the list and from disk?", curr_gradient->name);

	label = gtk_label_new(str);
	gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
	gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
	gtk_widget_show(label);

	g_free(str);

	/* Buttons */

	button = ed_create_button("Delete", 0.5, 0.5,
				  (GtkSignalFunc) ed_do_delete_gradient_callback,
				  (gpointer) dialog);
	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
			   button, TRUE, TRUE, 0);
	gtk_widget_grab_default(button);
	gtk_widget_show(button);

	button = ed_create_button("Cancel", 0.5, 0.5,
				  (GtkSignalFunc) ed_cancel_delete_gradient_callback,
				  (gpointer) dialog);
	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
			   button, TRUE, TRUE, 0);
	gtk_widget_show(button);

	/* Show! */

	gtk_widget_show(dialog);
	gtk_widget_set_sensitive(g_editor->shell, FALSE);
} /* ed_delete_gradient_callback */


/*****/

static void
ed_do_delete_gradient_callback(GtkWidget *widget, gpointer client_data)
{
	GList      *list;
1467
	GSList     *tmp;
Elliot Lee's avatar
Elliot Lee committed
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
	int         n;
	gradient_t *g;
	GtkWidget  *list_item;

	gtk_widget_destroy(GTK_WIDGET(client_data));
	gtk_widget_set_sensitive(g_editor->shell, TRUE);

	/* See which gradient we will have to select once the current one is deleted */

	n   = 0;
	tmp = gradients_list;

	while (tmp) {
		g = tmp->data;

		if (g == curr_gradient) {
			if (tmp->next == NULL)
				n--; /* Will have to select the *previous* one */

			break; /* We found the one we want */
		} /* if */

		n++; /* Next gradient */
1491
		tmp = g_slist_next(tmp);
Elliot Lee's avatar
Elliot Lee committed
1492 1493 1494 1495 1496 1497 1498 1499 1500
	} /* while */

	if (tmp == NULL)
		fatal_error("ed_do_delete_gradient_callback(): aieee, could not find gradient to delete!");

	/* Delete gradient from gradients list */

	list_item = curr_gradient->list_item; /* Remember list item to delete it later */

1501
	gradients_list = g_slist_remove(gradients_list, curr_gradient);
Elliot Lee's avatar
Elliot Lee committed
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514

	/* Delete file and free gradient */

	unlink(curr_gradient->filename);
	grad_free_gradient(curr_gradient);

	/* Delete gradient from listbox */

	list = g_list_append(NULL, list_item);
	gtk_list_remove_items(GTK_LIST(g_editor->list), list);

	/* Select new gradient */

1515
	curr_gradient = g_slist_nth(gradients_list, n)->data;
Elliot Lee's avatar
Elliot Lee committed
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
	gtk_list_select_item(GTK_LIST(g_editor->list), n);

	/* Update! */

	ed_update_editor(GRAD_UPDATE_PREVIEW | GRAD_RESET_CONTROL);
} /* ed_do_delete_gradient_callback */


/*****/

static void
ed_cancel_delete_gradient_callback(GtkWidget *widget, gpointer client_data)
{
	gtk_widget_destroy(GTK_WIDGET(client_data));
	gtk_widget_set_sensitive(g_editor->shell, TRUE);
} /* ed_cancel_delete_gradient_callback */


/*****/

static void
ed_save_pov_callback(GtkWidget *widget, gpointer client_data)
{
	GtkWidget *window;

1541 1542
	if (curr_gradient == NULL) return;

Elliot Lee's avatar
Elliot Lee committed
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
	window = gtk_file_selection_new("Save as POV-Ray");
	gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);

	gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(window)->ok_button),
			   "clicked", (GtkSignalFunc) ed_do_save_pov_callback,
			   window);

	gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(window)->cancel_button),
			   "clicked", (GtkSignalFunc) ed_cancel_save_pov_callback,
			   window);

	gtk_widget_show(window);
	gtk_widget_set_sensitive(g_editor->shell, FALSE);
} /* ed_save_pov_callback */


/*****/

static void
ed_refresh_callback(GtkWidget *widget, gpointer client_data)
{
1564
	GSList     *node;
Elliot Lee's avatar
Elliot Lee committed
1565 1566 1567 1568 1569
	gradient_t *grad;
	GList      *list;

	list = NULL;

1570
	for (node = gradients_list; node; node = g_slist_next(node)) {
Elliot Lee's avatar
Elliot Lee committed
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
		grad = node->data;
		list = g_list_append(list, grad->list_item);
	}

	gtk_list_remove_items(GTK_LIST(g_editor->list),list);

	grad_free_gradients();

	gradients_init();

	ed_set_list_of_gradients();

	ed_update_editor(GRAD_UPDATE_PREVIEW | GRAD_RESET_CONTROL); 
} /* ed_refresh_callback */

/*****/

static void
ed_do_save_pov_callback(GtkWidget *widget, gpointer client_data)
{
	char           *filename;
	FILE           *file;
	grad_segment_t *seg;

	filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(client_data));

	file = fopen(filename, "w");

	if (!file)
		warning("ed_do_save_pov_callback(): oops, could not open \"%s\"", filename);
	else {
		fprintf(file, "/* color_map file created by the GIMP */\n");
		fprintf(file, "/* http://www.xcf.berkeley.edu/~gimp  */\n");

		fprintf(file, "color_map {\n");

		seg = curr_gradient->segments;

		while (seg) {
			/* Left */

			fprintf(file, "\t[%f color rgbt <%f, %f, %f, %f>]\n",
				seg->left,
				seg->r0, seg->g0, seg->b0, 1.0 - seg->a0);

			/* Middle */

			fprintf(file, "\t[%f color rgbt <%f, %f, %f, %f>]\n",
				seg->middle,
				(seg->r0 + seg->r1) / 2.0,
				(seg->g0 + seg->g1) / 2.0,
				(seg->b0 + seg->b1) / 2.0,
				1.0 - (seg->a0 + seg->a1) / 2.0);

			/* Right */

			fprintf(file, "\t[%f color rgbt <%f, %f, %f, %f>]\n",
				seg->right,
				seg->r1, seg->g1, seg->b1, 1.0 - seg->a1);

			/* Next! */

			seg = seg->next;
		} /* while */

		fprintf(file, "} /* color_map */\n");
		fclose(file);
	} /* else */

	gtk_widget_destroy(GTK_WIDGET(client_data));
	gtk_widget_set_sensitive(g_editor->shell, TRUE);
} /* ed_do_save_pov_callback */


/*****/

static void
ed_cancel_save_pov_callback(GtkWidget *widget, gpointer client_data)
{
	gtk_widget_destroy(GTK_WIDGET(client_data));
	gtk_widget_set_sensitive(g_editor->shell, TRUE);
} /* ed_cancel_save_pov_callback */


/*****/

static void
ed_scrollbar_update(GtkAdjustment *adjustment, gpointer data)
{
	char           str[256];

	sprintf(str, "Zoom factor: %d:1    Displaying [%0.6f, %0.6f]",
		g_editor->zoom_factor, adjustment->value, adjustment->value + adjustment->page_size);

	ed_set_hint(str);

	ed_update_editor(GRAD_UPDATE_PREVIEW | GRAD_UPDATE_CONTROL);
} /* ed_scrollbar_update */


/*****/

static void
ed_zoom_all_callback(GtkWidget *widget, gpointer client_data)
{
	GtkAdjustment *adjustment;

	adjustment = GTK_ADJUSTMENT(g_editor->scroll_data);

	g_editor->zoom_factor = 1;

	adjustment->value     	   = 0.0;
	adjustment->page_size 	   = 1.0;
	adjustment->step_increment = 1.0 * GRAD_SCROLLBAR_STEP_SIZE;
	adjustment->page_increment = 1.0 * GRAD_SCROLLBAR_PAGE_SIZE;

	gtk_signal_emit_by_name(g_editor->scroll_data, "changed");
} /* ed_zoom_all_callback */


/*****/

static void
ed_zoom_out_callback(GtkWidget *widget, gpointer client_data)
{
	GtkAdjustment *adjustment;
	double 	       old_value, value;
	double 	       old_page_size, page_size;

	if (g_editor->zoom_factor <= 1)
		return;

	adjustment = GTK_ADJUSTMENT(g_editor->scroll_data);

	old_value     = adjustment->value;
	old_page_size = adjustment->page_size;

	g_editor->zoom_factor--;

	page_size = 1.0 / g_editor->zoom_factor;
	value     = old_value - (page_size - old_page_size) / 2.0;

	if (value < 0.0)
		value = 0.0;
	else if ((value + page_size) > 1.0)
		value = 1.0 - page_size;

	adjustment->value     	   = value;
	adjustment->page_size 	   = page_size;
	adjustment->step_increment = page_size * GRAD_SCROLLBAR_STEP_SIZE;
	adjustment->page_increment = page_size * GRAD_SCROLLBAR_PAGE_SIZE;

	gtk_signal_emit_by_name(g_editor->scroll_data, "changed");
} /* ed_zoom_out_callback */


/*****/

static void
ed_zoom_in_callback(GtkWidget *widget, gpointer client_data)
{
	GtkAdjustment *adjustment;
	double 	       old_value;
	double 	       old_page_size, page_size;

	adjustment = GTK_ADJUSTMENT(g_editor->scroll_data);

	old_value     = adjustment->value;
	old_page_size = adjustment->page_size;

	g_editor->zoom_factor++;

	page_size = 1.0 / g_editor->zoom_factor;

	adjustment->value     	   = old_value + (old_page_size - page_size) / 2.0;
	adjustment->page_size 	   = page_size;
	adjustment->step_increment = page_size * GRAD_SCROLLBAR_STEP_SIZE;
	adjustment->page_increment = page_size * GRAD_SCROLLBAR_PAGE_SIZE;

	gtk_signal_emit_by_name(g_editor->scroll_data, "changed");
} /* ed_zoom_in_callback */


/*****/

static void
ed_instant_update_update(GtkWidget *widget, gpointer data)
{
	if (GTK_TOGGLE_BUTTON(widget)->active) {
		g_editor->instant_update = 1;
		gtk_range_set_update_policy(GTK_RANGE(g_editor->scrollbar), GTK_UPDATE_CONTINUOUS);
	} else {
		g_editor->instant_update = 0;
		gtk_range_set_update_policy(GTK_RANGE(g_editor->scrollbar), GTK_UPDATE_DELAYED);
	} /* else */
} /* ed_instant_update_update */


/***** Gradient preview functions *****/

/*****/

static gint
prev_events(GtkWidget *widget, GdkEvent *event)
{
	gint            x, y;
	GdkEventButton *bevent;

1779 1780 1781 1782
	/* ignore events when no gradient is present */
	if (curr_gradient == NULL) 
	        return FALSE;

Elliot Lee's avatar
Elliot Lee committed
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
	switch (event->type) {
		case GDK_EXPOSE:
			prev_update(0);
			break;

		case GDK_LEAVE_NOTIFY:
			ed_set_hint("");
			break;

		case GDK_MOTION_NOTIFY:
			gtk_widget_get_pointer(g_editor->preview, &x, &y);

			if (x != g_editor->preview_last_x) {
				g_editor->preview_last_x = x;

				if (g_editor->preview_button_down)
					prev_set_foreground(x);
				else
					prev_set_hint(x);
			} /* if */

			break;

		case GDK_BUTTON_PRESS:
			gtk_widget_get_pointer(g_editor->preview, &x, &y);

			bevent = (GdkEventButton *) event;

			switch (bevent->button) {
				case 1:
					g_editor->preview_last_x = x;
					g_editor->preview_button_down = 1;
					prev_set_foreground(x);
					break;

				case 3:
					cpopup_do_popup();
					break;

				default:
					break;
			} /* switch */

			break;

		case GDK_BUTTON_RELEASE:
			if (g_editor->preview_button_down) {
				gtk_widget_get_pointer(g_editor->preview, &x, &y);
				g_editor->preview_last_x = x;
				g_editor->preview_button_down = 0;
				prev_set_foreground(x);
			} /* if */

			break;

		default:
			break;
	} /* switch */

	return FALSE;
} /* prev_events */


/*****/

static void
prev_set_hint(gint x)
{
	double xpos;
	double r, g, b, a;
	double h, s, v;
	char   str[256];

	xpos = control_calc_g_pos(x);

	grad_get_color_at(xpos, &r, &g, &b, &a);

	h = r;
	s = g;
	v = b;

	calc_rgb_to_hsv(&h, &s, &v);

	sprintf(str, "Position: %0.6f    "
		"RGB (%0.3f, %0.3f, %0.3f)    "
		"HSV (%0.3f, %0.3f, %0.3f)    "
		"Opacity: %0.3f",
		xpos, r, g, b, h * 360.0, s, v, a);

	ed_set_hint(str);
} /* prev_set_hint */


/*****/

static void
prev_set_foreground(gint x)
{
	double xpos;
	double r, g, b, a;
	char   str[512];

	xpos = control_calc_g_pos(x);
	grad_get_color_at(xpos, &r, &g, &b, &a);

	palette_set_foreground(r * 255.0, g * 255.0, b * 255.0);

	sprintf(str, "Foreground color set to RGB (%d, %d, %d) <-> (%0.3f, %0.3f, %0.3f)",
		(int) (r * 255.0),
		(int) (g * 255.0),
		(int) (b * 255.0),
		r, g, b);

	ed_set_hint(str);
} /* prev_set_foreground */


/*****/

static void
prev_update(int recalculate)
{
	long           rowsiz;
	GtkAdjustment *adjustment;
	guint16        width, height;
	guint16        pwidth, pheight;

1910
	/* We only update if we can draw to the widget and a gradient is present */
Elliot Lee's avatar
Elliot Lee committed
1911

1912 1913
	if (curr_gradient == NULL) 
	        return;
Elliot Lee's avatar
Elliot Lee committed
1914 1915 1916 1917 1918 1919 1920
	if (!GTK_WIDGET_DRAWABLE(g_editor->preview))
		return;

	/* See whether we have to re-create the preview widget */

	width   = g_editor->preview->allocation.width;
	height  = g_editor->preview->allocation.height;
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931

	/* hof: do not change preview size on Window resize.
	 *      The original code allows expansion of the preview
	 *      on window resize events. But once expanded, there is no way to shrink
	 *      the window back to the original size.
	 *  A full Bugfix should change the preview size according to the users     
	 *  window resize actions.   
	 */
		width   = GRAD_PREVIEW_WIDTH;
		height  = GRAD_PREVIEW_HEIGHT;

Elliot Lee's avatar
Elliot Lee committed
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995