color_select.c 46.4 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17 18 19
 */
#include <stdio.h>
#include <stdlib.h>
20
#include <string.h>
Elliot Lee's avatar
Elliot Lee committed
21 22 23
#include "appenv.h"
#include "actionarea.h"
#include "color_select.h"
24
#include "libgimp/color_selector.h"
Elliot Lee's avatar
Elliot Lee committed
25 26 27
#include "colormaps.h"
#include "errors.h"
#include "gimprc.h"
28
#include "session.h"
29
#include "color_area.h" /* for color_area_draw_rect */
Elliot Lee's avatar
Elliot Lee committed
30

31 32
#include "libgimp/gimpintl.h"

33 34
#define XY_DEF_WIDTH       240
#define XY_DEF_HEIGHT      240
Elliot Lee's avatar
Elliot Lee committed
35
#define Z_DEF_WIDTH        15
36
#define Z_DEF_HEIGHT       240
Elliot Lee's avatar
Elliot Lee committed
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
#define COLOR_AREA_WIDTH   74
#define COLOR_AREA_HEIGHT  20

#define COLOR_AREA_MASK GDK_EXPOSURE_MASK | \
                        GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | \
			GDK_BUTTON1_MOTION_MASK | GDK_ENTER_NOTIFY_MASK

typedef enum {
  HUE = 0,
  SATURATION,
  VALUE,
  RED,
  GREEN,
  BLUE,
  HUE_SATURATION,
  HUE_VALUE,
  SATURATION_VALUE,
  RED_GREEN,
  RED_BLUE,
  GREEN_BLUE
} ColorSelectFillType;

typedef enum {
  UPDATE_VALUES = 1 << 0,
  UPDATE_POS = 1 << 1,
  UPDATE_XY_COLOR = 1 << 2,
  UPDATE_Z_COLOR = 1 << 3,
  UPDATE_NEW_COLOR = 1 << 4,
  UPDATE_ORIG_COLOR = 1 << 5,
  UPDATE_CALLER = 1 << 6
} ColorSelectUpdateType;

typedef struct _ColorSelectFill ColorSelectFill;
typedef void (*ColorSelectFillUpdateProc) (ColorSelectFill *);

struct _ColorSelectFill {
  unsigned char *buffer;
  int y;
  int width;
  int height;
  int *values;
  ColorSelectFillUpdateProc update;
};

81 82
static GtkWidget * color_select_widget_new (ColorSelectP, int, int, int);

Elliot Lee's avatar
Elliot Lee committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static void color_select_update (ColorSelectP, ColorSelectUpdateType);
static void color_select_update_caller (ColorSelectP);
static void color_select_update_values (ColorSelectP);
static void color_select_update_rgb_values (ColorSelectP);
static void color_select_update_hsv_values (ColorSelectP);
static void color_select_update_pos (ColorSelectP);
static void color_select_update_sliders (ColorSelectP, int);
static void color_select_update_entries (ColorSelectP, int);
static void color_select_update_colors (ColorSelectP, int);

static void color_select_ok_callback (GtkWidget *, gpointer);
static void color_select_cancel_callback (GtkWidget *, gpointer);
static gint color_select_delete_callback (GtkWidget *, GdkEvent *, gpointer);
static gint color_select_xy_expose (GtkWidget *, GdkEventExpose *, ColorSelectP);
static gint color_select_xy_events (GtkWidget *, GdkEvent *, ColorSelectP);
static gint color_select_z_expose (GtkWidget *, GdkEventExpose *, ColorSelectP);
static gint color_select_z_events (GtkWidget *, GdkEvent *, ColorSelectP);
static gint color_select_color_events (GtkWidget *, GdkEvent *);
static void color_select_slider_update (GtkAdjustment *, gpointer);
static void color_select_entry_update (GtkWidget *, gpointer);
static void color_select_toggle_update (GtkWidget *, gpointer);
104
static gint color_select_hex_entry_leave (GtkWidget *, GdkEvent *, gpointer);
Elliot Lee's avatar
Elliot Lee committed
105 106 107

static void color_select_image_fill (GtkWidget *, ColorSelectFillType, int *);

108 109
static void color_select_draw_z_marker (ColorSelectP, GdkRectangle *);
static void color_select_draw_xy_marker (ColorSelectP, GdkRectangle *);
Elliot Lee's avatar
Elliot Lee committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123

static void color_select_update_red (ColorSelectFill *);
static void color_select_update_green (ColorSelectFill *);
static void color_select_update_blue (ColorSelectFill *);
static void color_select_update_hue (ColorSelectFill *);
static void color_select_update_saturation (ColorSelectFill *);
static void color_select_update_value (ColorSelectFill *);
static void color_select_update_red_green (ColorSelectFill *);
static void color_select_update_red_blue (ColorSelectFill *);
static void color_select_update_green_blue (ColorSelectFill *);
static void color_select_update_hue_saturation (ColorSelectFill *);
static void color_select_update_hue_value (ColorSelectFill *);
static void color_select_update_saturation_value (ColorSelectFill *);

124 125 126 127 128 129 130 131 132 133 134 135
static GtkWidget * color_select_notebook_new (int, int, int,
					      GimpColorSelector_Callback,
					      void *, void **);
static void color_select_notebook_free (void *);
static void color_select_notebook_setcolor (void *, int, int, int, int);
static void color_select_notebook_update_callback (int, int, int,
						   ColorSelectState, void *);





Elliot Lee's avatar
Elliot Lee committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
static ColorSelectFillUpdateProc update_procs[] =
{
  color_select_update_hue,
  color_select_update_saturation,
  color_select_update_value,
  color_select_update_red,
  color_select_update_green,
  color_select_update_blue,
  color_select_update_hue_saturation,
  color_select_update_hue_value,
  color_select_update_saturation_value,
  color_select_update_red_green,
  color_select_update_red_blue,
  color_select_update_green_blue,
};

static ActionAreaItem action_items[2] =
{
154
  { N_("OK"), color_select_ok_callback, NULL, NULL },
155
  { N_("Cancel"), color_select_cancel_callback, NULL, NULL }
Elliot Lee's avatar
Elliot Lee committed
156 157 158 159 160 161 162
};

ColorSelectP
color_select_new (int                  r,
		  int                  g,
		  int                  b,
		  ColorSelectCallback  callback,
163 164
		  void                *client_data,
		  int                  wants_updates)
Elliot Lee's avatar
Elliot Lee committed
165 166 167 168 169 170 171 172 173 174 175
{
  ColorSelectP csp;
  GtkWidget *main_vbox;

  csp = g_malloc (sizeof (_ColorSelect));

  csp->callback = callback;
  csp->client_data = client_data;
  csp->z_color_fill = HUE;
  csp->xy_color_fill = SATURATION_VALUE;
  csp->gc = NULL;
176
  csp->wants_updates = wants_updates;
Elliot Lee's avatar
Elliot Lee committed
177 178 179 180 181 182 183 184

  csp->values[RED] = csp->orig_values[0] = r;
  csp->values[GREEN] = csp->orig_values[1] = g;
  csp->values[BLUE] = csp->orig_values[2] = b;
  color_select_update_hsv_values (csp);
  color_select_update_pos (csp);

  csp->shell = gtk_dialog_new ();
185
  gtk_window_set_wmclass (GTK_WINDOW (csp->shell), "color_selection", "Gimp");
186
  gtk_window_set_title (GTK_WINDOW (csp->shell), _("Color Selection"));
Elliot Lee's avatar
Elliot Lee committed
187
  gtk_window_set_policy (GTK_WINDOW (csp->shell), FALSE, FALSE, FALSE);
188

Elliot Lee's avatar
Elliot Lee committed
189 190 191 192 193

  /*  handle the wm close signal */
  gtk_signal_connect (GTK_OBJECT (csp->shell), "delete_event",
		      (GtkSignalFunc) color_select_delete_callback, csp);
  
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
  main_vbox = color_select_widget_new (csp, r, g, b);
  gtk_widget_show (main_vbox);

  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (csp->shell)->vbox), main_vbox, TRUE, TRUE, 0);

  /*  The action area  */
  action_items[0].user_data = csp;
  action_items[1].user_data = csp;
  if (csp->wants_updates)
    {
      action_items[0].label = _("Close");
      action_items[1].label = _("Revert to Old Color");
    }
  else
    {
      action_items[0].label = _("OK");
      action_items[1].label = _("Cancel");
    }
  build_action_area (GTK_DIALOG (csp->shell), action_items, 2, 0);

  color_select_image_fill (csp->z_color, csp->z_color_fill, csp->values);
  color_select_image_fill (csp->xy_color, csp->xy_color_fill, csp->values);

  gtk_widget_show (csp->shell);

  return csp;
}

static GtkWidget *
color_select_widget_new (ColorSelectP csp, int r, int g, int b)
{
  static char *toggle_titles[6] = { "H", "S", "V", "R", "G", "B" };
  static gfloat slider_max_vals[6] = { 360, 100, 100, 255, 255, 255 };
  static gfloat slider_incs[6] = { 0.1, 0.1, 0.1, 1.0, 1.0, 1.0 };

  GtkWidget *main_vbox;
  GtkWidget *main_hbox;
  GtkWidget *xy_frame;
  GtkWidget *z_frame;
  GtkWidget *colors_frame;
  GtkWidget *colors_hbox;
  GtkWidget *right_vbox;
  GtkWidget *table;
  GtkWidget *slider;
  GtkWidget *hex_hbox;
  GtkWidget *label;
  GSList *group;
  char buffer[16];
  int i;

Elliot Lee's avatar
Elliot Lee committed
244
  main_vbox = gtk_vbox_new (FALSE, 2);
245
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 2);
Elliot Lee's avatar
Elliot Lee committed
246 247

  main_hbox = gtk_hbox_new (FALSE, 2);
248
  gtk_container_set_border_width (GTK_CONTAINER (main_hbox), 0);
Elliot Lee's avatar
Elliot Lee committed
249 250 251 252 253 254 255 256 257
  gtk_box_pack_start (GTK_BOX (main_vbox), main_hbox, TRUE, TRUE, 2);
  gtk_widget_show (main_hbox);

  xy_frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (xy_frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (main_hbox), xy_frame, FALSE, FALSE, 2);
  gtk_widget_show (xy_frame);

  csp->xy_color = gtk_preview_new (GTK_PREVIEW_COLOR);
258
  gtk_preview_set_dither (GTK_PREVIEW (csp->xy_color), GDK_RGB_DITHER_MAX);
Elliot Lee's avatar
Elliot Lee committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  gtk_preview_size (GTK_PREVIEW (csp->xy_color), XY_DEF_WIDTH, XY_DEF_HEIGHT);
  gtk_widget_set_events (csp->xy_color, COLOR_AREA_MASK);
  gtk_signal_connect_after (GTK_OBJECT (csp->xy_color), "expose_event",
			    (GtkSignalFunc) color_select_xy_expose,
			    csp);
  gtk_signal_connect (GTK_OBJECT (csp->xy_color), "event",
		      (GtkSignalFunc) color_select_xy_events,
		      csp);
  gtk_container_add (GTK_CONTAINER (xy_frame), csp->xy_color);
  gtk_widget_show (csp->xy_color);

  z_frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (z_frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (main_hbox), z_frame, FALSE, FALSE, 2);
  gtk_widget_show (z_frame);

  csp->z_color = gtk_preview_new (GTK_PREVIEW_COLOR);
276
  gtk_preview_set_dither (GTK_PREVIEW (csp->z_color), GDK_RGB_DITHER_MAX);
Elliot Lee's avatar
Elliot Lee committed
277 278 279 280 281 282 283 284 285 286 287 288 289
  gtk_preview_size (GTK_PREVIEW (csp->z_color), Z_DEF_WIDTH, Z_DEF_HEIGHT);
  gtk_widget_set_events (csp->z_color, COLOR_AREA_MASK);
  gtk_signal_connect_after (GTK_OBJECT (csp->z_color), "expose_event",
			    (GtkSignalFunc) color_select_z_expose,
			    csp);
  gtk_signal_connect (GTK_OBJECT (csp->z_color), "event",
		      (GtkSignalFunc) color_select_z_events,
		      csp);
  gtk_container_add (GTK_CONTAINER (z_frame), csp->z_color);
  gtk_widget_show (csp->z_color);

  /*  The right vertical box with old/new color area and color space sliders  */
  right_vbox = gtk_vbox_new (FALSE, 2);
290
  gtk_container_set_border_width (GTK_CONTAINER (right_vbox), 0);
Elliot Lee's avatar
Elliot Lee committed
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  gtk_box_pack_start (GTK_BOX (main_hbox), right_vbox, TRUE, TRUE, 0);
  gtk_widget_show (right_vbox);

  /*  The old/new color area  */
  colors_frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (colors_frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (right_vbox), colors_frame, FALSE, FALSE, 0);
  gtk_widget_show (colors_frame);

  colors_hbox = gtk_hbox_new (TRUE, 2);
  gtk_container_add (GTK_CONTAINER (colors_frame), colors_hbox);
  gtk_widget_show (colors_hbox);

  csp->new_color = gtk_drawing_area_new ();
  gtk_drawing_area_size (GTK_DRAWING_AREA (csp->new_color), COLOR_AREA_WIDTH, COLOR_AREA_HEIGHT);
  gtk_widget_set_events (csp->new_color, GDK_EXPOSURE_MASK);
  gtk_signal_connect (GTK_OBJECT (csp->new_color), "event",
		      (GtkSignalFunc) color_select_color_events,
		      csp);
  gtk_object_set_user_data (GTK_OBJECT (csp->new_color), csp);
  gtk_box_pack_start (GTK_BOX (colors_hbox), csp->new_color, TRUE, TRUE, 0);
  gtk_widget_show (csp->new_color);

  csp->orig_color = gtk_drawing_area_new ();
  gtk_drawing_area_size (GTK_DRAWING_AREA (csp->orig_color), COLOR_AREA_WIDTH, COLOR_AREA_HEIGHT);
  gtk_widget_set_events (csp->orig_color, GDK_EXPOSURE_MASK);
  gtk_signal_connect (GTK_OBJECT (csp->orig_color), "event",
		      (GtkSignalFunc) color_select_color_events,
		      csp);
  gtk_object_set_user_data (GTK_OBJECT (csp->orig_color), csp);
  gtk_box_pack_start (GTK_BOX (colors_hbox), csp->orig_color, TRUE, TRUE, 0);
  gtk_widget_show (csp->orig_color);

  /*  The color space sliders, toggle buttons and entries  */
  table = gtk_table_new (6, 3, FALSE);
  gtk_table_set_row_spacings (GTK_TABLE (table), 3);
  gtk_table_set_col_spacings (GTK_TABLE (table), 3);
  gtk_container_border_width (GTK_CONTAINER (table), 2);
  gtk_box_pack_start (GTK_BOX (right_vbox), table, TRUE, TRUE, 0);
  gtk_widget_show (table);

  group = NULL;
  for (i = 0; i < 6; i++)
    {
      csp->toggles[i] = gtk_radio_button_new_with_label (group, toggle_titles[i]);
      group = gtk_radio_button_group (GTK_RADIO_BUTTON (csp->toggles[i]));
      gtk_table_attach (GTK_TABLE (table), csp->toggles[i],
			0, 1, i, i+1, GTK_FILL, GTK_EXPAND, 0, 0);
      gtk_signal_connect (GTK_OBJECT (csp->toggles[i]), "toggled",
			  (GtkSignalFunc) color_select_toggle_update,
			  csp);
      gtk_widget_show (csp->toggles[i]);

      csp->slider_data[i] = GTK_ADJUSTMENT (gtk_adjustment_new (csp->values[i], 0.0,
								slider_max_vals[i],
								slider_incs[i],
								1.0, 0.0));

      slider = gtk_hscale_new (csp->slider_data[i]);
      gtk_table_attach (GTK_TABLE (table), slider, 1, 2, i, i+1,
			GTK_EXPAND | GTK_FILL, GTK_EXPAND, 0, 0);
      gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
      gtk_scale_set_draw_value (GTK_SCALE (slider), FALSE);
      gtk_signal_connect (GTK_OBJECT (csp->slider_data[i]), "value_changed",
			  (GtkSignalFunc) color_select_slider_update,
			  csp);
      gtk_widget_show (slider);

      csp->entries[i] = gtk_entry_new ();
      sprintf (buffer, "%d", csp->values[i]);
      gtk_entry_set_text (GTK_ENTRY (csp->entries[i]), buffer);
      gtk_widget_set_usize (GTK_WIDGET (csp->entries[i]), 40, 0);
      gtk_table_attach (GTK_TABLE (table), csp->entries[i],
			2, 3, i, i+1, GTK_FILL, GTK_EXPAND, 0, 0);
      gtk_signal_connect (GTK_OBJECT (csp->entries[i]), "changed",
			  (GtkSignalFunc) color_select_entry_update,
			  csp);
      gtk_widget_show (csp->entries[i]);
    }

371 372 373 374 375 376 377 378
  /* The hex triplet entry */
  hex_hbox = gtk_hbox_new (FALSE, 3);
  gtk_box_pack_start (GTK_BOX (right_vbox), hex_hbox, FALSE, FALSE, 0);
  gtk_widget_show (hex_hbox);

  csp->hex_entry = gtk_entry_new ();
  sprintf(buffer, "#%.2x%.2x%.2x", r, g, b);
  gtk_entry_set_text (GTK_ENTRY (csp->hex_entry), buffer);
Sven Neumann's avatar
Sven Neumann committed
379
  gtk_widget_set_usize (GTK_WIDGET (csp->hex_entry), 75, 0);
380 381 382 383 384 385
  gtk_box_pack_end (GTK_BOX (hex_hbox), csp->hex_entry, FALSE, FALSE, 2);
  gtk_signal_connect (GTK_OBJECT (csp->hex_entry), "focus_out_event",
		      (GtkSignalFunc) color_select_hex_entry_leave,
		      csp);
  gtk_widget_show (csp->hex_entry);

386
  label = gtk_label_new (_("Hex Triplet:"));
387 388 389
  gtk_box_pack_end (GTK_BOX (hex_hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

390 391
  return main_vbox;
}
Elliot Lee's avatar
Elliot Lee committed
392 393


394 395 396 397 398 399 400 401 402 403 404 405
/* Register the GIMP colour selector with the color notebook */
void
color_select_init (void)
{
  GimpColorSelectorMethods methods =
  {
    color_select_notebook_new,
    color_select_notebook_free,
    color_select_notebook_setcolor
  };

  gimp_color_selector_register ("GIMP", &methods);
Elliot Lee's avatar
Elliot Lee committed
406 407
}

408

Elliot Lee's avatar
Elliot Lee committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
void
color_select_show (ColorSelectP csp)
{
  if (csp)
    gtk_widget_show (csp->shell);
}

void
color_select_hide (ColorSelectP csp)
{
  if (csp)
    gtk_widget_hide (csp->shell);
}

void
color_select_free (ColorSelectP csp)
{
  if (csp)
    {
428 429
      if (csp->shell)
	gtk_widget_destroy (csp->shell);
Elliot Lee's avatar
Elliot Lee committed
430 431 432 433 434 435 436 437 438 439 440 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
      gdk_gc_destroy (csp->gc);
      g_free (csp);
    }
}

void
color_select_set_color (ColorSelectP csp,
			int          r,
			int          g,
			int          b,
			int          set_current)
{
  if (csp)
    {
      csp->orig_values[0] = r;
      csp->orig_values[1] = g;
      csp->orig_values[2] = b;

      color_select_update_colors (csp, 1);

      if (set_current)
	{
	  csp->values[RED] = r;
	  csp->values[GREEN] = g;
	  csp->values[BLUE] = b;

	  color_select_update_hsv_values (csp);
	  color_select_update_pos (csp);
	  color_select_update_sliders (csp, -1);
	  color_select_update_entries (csp, -1);
	  color_select_update_colors (csp, 0);

	  color_select_update (csp, UPDATE_Z_COLOR);
	  color_select_update (csp, UPDATE_XY_COLOR);
	}
    }
}

static void
color_select_update (ColorSelectP          csp,
		     ColorSelectUpdateType update)
{
  if (csp)
    {
      if (update & UPDATE_POS)
	color_select_update_pos (csp);

      if (update & UPDATE_VALUES)
	{
	  color_select_update_values (csp);
	  color_select_update_sliders (csp, -1);
	  color_select_update_entries (csp, -1);

	  if (!(update & UPDATE_NEW_COLOR))
	    color_select_update_colors (csp, 0);
	}

      if (update & UPDATE_XY_COLOR)
	{
	  color_select_image_fill (csp->xy_color, csp->xy_color_fill, csp->values);
	  gtk_widget_draw (csp->xy_color, NULL);
	}

      if (update & UPDATE_Z_COLOR)
	{
	  color_select_image_fill (csp->z_color, csp->z_color_fill, csp->values);
	  gtk_widget_draw (csp->z_color, NULL);
	}

      if (update & UPDATE_NEW_COLOR)
	color_select_update_colors (csp, 0);

      if (update & UPDATE_ORIG_COLOR)
	color_select_update_colors (csp, 1);

      /*if (update & UPDATE_CALLER)*/
      color_select_update_caller (csp);
    }
}

static void
color_select_update_caller (ColorSelectP csp)
{
513
  if (csp && csp->wants_updates && csp->callback)
Elliot Lee's avatar
Elliot Lee committed
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 568 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 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
    {
      (* csp->callback) (csp->values[RED],
			 csp->values[GREEN],
			 csp->values[BLUE],
			 COLOR_SELECT_UPDATE,
			 csp->client_data);
    }
}

static void
color_select_update_values (ColorSelectP csp)
{
  if (csp)
    {
      switch (csp->z_color_fill)
	{
	case RED:
	  csp->values[BLUE] = csp->pos[0];
	  csp->values[GREEN] = csp->pos[1];
	  csp->values[RED] = csp->pos[2];
	  break;
	case GREEN:
	  csp->values[BLUE] = csp->pos[0];
	  csp->values[RED] = csp->pos[1];
	  csp->values[GREEN] = csp->pos[2];
	  break;
	case BLUE:
	  csp->values[GREEN] = csp->pos[0];
	  csp->values[RED] = csp->pos[1];
	  csp->values[BLUE] = csp->pos[2];
	  break;
	case HUE:
	  csp->values[VALUE] = csp->pos[0] * 100 / 255;
	  csp->values[SATURATION] = csp->pos[1] * 100 / 255;
	  csp->values[HUE] = csp->pos[2] * 360 / 255;
	  break;
	case SATURATION:
	  csp->values[VALUE] = csp->pos[0] * 100 / 255;
	  csp->values[HUE] = csp->pos[1] * 360 / 255;
	  csp->values[SATURATION] = csp->pos[2] * 100 / 255;
	  break;
	case VALUE:
	  csp->values[SATURATION] = csp->pos[0] * 100 / 255;
	  csp->values[HUE] = csp->pos[1] * 360 / 255;
	  csp->values[VALUE] = csp->pos[2] * 100 / 255;
	  break;
	}

      switch (csp->z_color_fill)
	{
	case RED:
	case GREEN:
	case BLUE:
	  color_select_update_hsv_values (csp);
	  break;
	case HUE:
	case SATURATION:
	case VALUE:
	  color_select_update_rgb_values (csp);
	  break;
	}
    }
}

static void
color_select_update_rgb_values (ColorSelectP csp)
{
  float h, s, v;
  float f, p, q, t;

  if (csp)
    {
      h = csp->values[HUE];
      s = csp->values[SATURATION] / 100.0;
      v = csp->values[VALUE] / 100.0;

      if (s == 0)
	{
	  csp->values[RED] = v * 255;
	  csp->values[GREEN] = v * 255;
	  csp->values[BLUE] = v * 255;
	}
      else
	{
	  if (h == 360)
	    h = 0;

	  h /= 60;
	  f = h - (int) h;
	  p = v * (1 - s);
	  q = v * (1 - (s * f));
	  t = v * (1 - (s * (1 - f)));

	  switch ((int) h)
	    {
	    case 0:
	      csp->values[RED] = v * 255;
	      csp->values[GREEN] = t * 255;
	      csp->values[BLUE] = p * 255;
	      break;
	    case 1:
	      csp->values[RED] = q * 255;
	      csp->values[GREEN] = v * 255;
	      csp->values[BLUE] = p * 255;
	      break;
	    case 2:
	      csp->values[RED] = p * 255;
	      csp->values[GREEN] = v * 255;
	      csp->values[BLUE] = t * 255;
	      break;
	    case 3:
	      csp->values[RED] = p * 255;
	      csp->values[GREEN] = q * 255;
	      csp->values[BLUE] = v * 255;
	      break;
	    case 4:
	      csp->values[RED] = t * 255;
	      csp->values[GREEN] = p * 255;
	      csp->values[BLUE] = v * 255;
	      break;
	    case 5:
	      csp->values[RED] = v * 255;
	      csp->values[GREEN] = p * 255;
	      csp->values[BLUE] = q * 255;
	      break;
	    }
	}
    }
}

static void
color_select_update_hsv_values (ColorSelectP csp)
{
  int r, g, b;
  float h, s, v;
  int min, max;
  int delta;

  if (csp)
    {
      r = csp->values[RED];
      g = csp->values[GREEN];
      b = csp->values[BLUE];

      if (r > g)
	{
	  if (r > b)
	    max = r;
	  else
	    max = b;

	  if (g < b)
	    min = g;
	  else
	    min = b;
	}
      else
	{
	  if (g > b)
	    max = g;
	  else
	    max = b;

	  if (r < b)
	    min = r;
	  else
	    min = b;
	}

      v = max;

      if (max != 0)
	s = (max - min) / (float) max;
      else
	s = 0;

      if (s == 0)
	h = 0;
      else
	{
	  h = 0;
	  delta = max - min;
	  if (r == max)
	    h = (g - b) / (float) delta;
	  else if (g == max)
	    h = 2 + (b - r) / (float) delta;
	  else if (b == max)
	    h = 4 + (r - g) / (float) delta;
	  h *= 60;

	  if (h < 0)
	    h += 360;
	}

      csp->values[HUE] = h;
      csp->values[SATURATION] = s * 100;
      csp->values[VALUE] = v * 100 / 255;
    }
}

static void
color_select_update_pos (ColorSelectP csp)
{
  if (csp)
    {
      switch (csp->z_color_fill)
	{
	case RED:
	  csp->pos[0] = csp->values[BLUE];
	  csp->pos[1] = csp->values[GREEN];
	  csp->pos[2] = csp->values[RED];
	  break;
	case GREEN:
	  csp->pos[0] = csp->values[BLUE];
	  csp->pos[1] = csp->values[RED];
	  csp->pos[2] = csp->values[GREEN];
	  break;
	case BLUE:
	  csp->pos[0] = csp->values[GREEN];
	  csp->pos[1] = csp->values[RED];
	  csp->pos[2] = csp->values[BLUE];
	  break;
	case HUE:
	  csp->pos[0] = csp->values[VALUE] * 255 / 100;
	  csp->pos[1] = csp->values[SATURATION] * 255 / 100;
	  csp->pos[2] = csp->values[HUE] * 255 / 360;
	  break;
	case SATURATION:
	  csp->pos[0] = csp->values[VALUE] * 255 / 100;
	  csp->pos[1] = csp->values[HUE] * 255 / 360;
	  csp->pos[2] = csp->values[SATURATION] * 255 / 100;
	  break;
	case VALUE:
	  csp->pos[0] = csp->values[SATURATION] * 255 / 100;
	  csp->pos[1] = csp->values[HUE] * 255 / 360;
	  csp->pos[2] = csp->values[VALUE] * 255 / 100;
	  break;
	}
    }
}

static void
color_select_update_sliders (ColorSelectP csp,
			     int          skip)
{
  int i;

  if (csp)
    {
      for (i = 0; i < 6; i++)
	if (i != skip)
	  {
	    csp->slider_data[i]->value = (gfloat) csp->values[i];

	    gtk_signal_handler_block_by_data (GTK_OBJECT (csp->slider_data[i]), csp);
	    gtk_signal_emit_by_name (GTK_OBJECT (csp->slider_data[i]), "value_changed");
	    gtk_signal_handler_unblock_by_data (GTK_OBJECT (csp->slider_data[i]), csp);
	  }
    }
}

static void
color_select_update_entries (ColorSelectP csp,
			     int          skip)
{
  char buffer[16];
  int i;

  if (csp)
    {
      for (i = 0; i < 6; i++)
	if (i != skip)
	  {
	    sprintf (buffer, "%d", csp->values[i]);

	    gtk_signal_handler_block_by_data (GTK_OBJECT (csp->entries[i]), csp);
	    gtk_entry_set_text (GTK_ENTRY (csp->entries[i]), buffer);
	    gtk_signal_handler_unblock_by_data (GTK_OBJECT (csp->entries[i]), csp);
	  }
793 794 795 796 797 798

      sprintf(buffer, "#%.2x%.2x%.2x",
	      csp->values[RED],
	      csp->values[GREEN],
	      csp->values[BLUE]);
      gtk_entry_set_text (GTK_ENTRY (csp->hex_entry), buffer);
Elliot Lee's avatar
Elliot Lee committed
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
    }
}

static void
color_select_update_colors (ColorSelectP csp,
			    int          which)
{
  GdkWindow *window;
  GdkColor color;
  int red, green, blue;
  int width, height;

  if (csp)
    {
      if (which)
	{
	  window = csp->orig_color->window;
	  color.pixel = old_color_pixel;
	  red = csp->orig_values[0];
	  green = csp->orig_values[1];
	  blue = csp->orig_values[2];
	}
      else
	{
	  window = csp->new_color->window;
	  color.pixel = new_color_pixel;
	  red = csp->values[RED];
	  green = csp->values[GREEN];
	  blue = csp->values[BLUE];
	}

830 831 832
      /* if we haven't yet been realised, there's no need to redraw
       * anything. */
      if (!window) return;
Elliot Lee's avatar
Elliot Lee committed
833 834 835

      store_color (&color.pixel, red, green, blue);

836 837
      gdk_window_get_size (window, &width, &height);

Elliot Lee's avatar
Elliot Lee committed
838 839
      if (csp->gc)
	{
840
#ifdef OLD_COLOR_AREA
Elliot Lee's avatar
Elliot Lee committed
841 842 843
	  gdk_gc_set_foreground (csp->gc, &color);
	  gdk_draw_rectangle (window, csp->gc, 1,
			      0, 0, width, height);
844 845 846 847 848
#else
	  color_area_draw_rect (window, csp->gc,
				0, 0, width, height,
				red, green, blue);
#endif
Elliot Lee's avatar
Elliot Lee committed
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
	}
    }
}

static void
color_select_ok_callback (GtkWidget *w,
			  gpointer   client_data)
{
  ColorSelectP csp;

  csp = (ColorSelectP) client_data;
  if (csp)
    {
      if (csp->callback)
	(* csp->callback) (csp->values[RED],
			   csp->values[GREEN],
			   csp->values[BLUE],
			   COLOR_SELECT_OK,
			   csp->client_data);
    }
}

static gint
color_select_delete_callback (GtkWidget *w,
			      GdkEvent  *e,
			      gpointer   client_data)
{
  color_select_cancel_callback (w, client_data);

878
  return TRUE;
Elliot Lee's avatar
Elliot Lee committed
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
}
  

static void
color_select_cancel_callback (GtkWidget *w,
			      gpointer   client_data)
{
  ColorSelectP csp;

  csp = (ColorSelectP) client_data;
  if (csp)
    {
      if (csp->callback)
	(* csp->callback) (csp->orig_values[0],
			   csp->orig_values[1],
			   csp->orig_values[2],
			   COLOR_SELECT_CANCEL,
			   csp->client_data);
    }
}

static gint
color_select_xy_expose (GtkWidget      *widget,
			GdkEventExpose *event,
			ColorSelectP    csp)
{
  if (!csp->gc)
    csp->gc = gdk_gc_new (widget->window);

908
  color_select_draw_xy_marker (csp, &event->area);
Elliot Lee's avatar
Elliot Lee committed
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926

  return FALSE;
}

static gint
color_select_xy_events (GtkWidget    *widget,
			GdkEvent     *event,
			ColorSelectP  csp)
{
  GdkEventButton *bevent;
  GdkEventMotion *mevent;
  int tx, ty;

  switch (event->type)
    {
    case GDK_BUTTON_PRESS:
      bevent = (GdkEventButton *) event;

927
      color_select_draw_xy_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

      csp->pos[0] = (bevent->x * 255) / (XY_DEF_WIDTH - 1);
      csp->pos[1] = 255 - (bevent->y * 255) / (XY_DEF_HEIGHT - 1);

      if (csp->pos[0] < 0)
	csp->pos[0] = 0;
      if (csp->pos[0] > 255)
	csp->pos[0] = 255;
      if (csp->pos[1] < 0)
	csp->pos[1] = 0;
      if (csp->pos[1] > 255)
	csp->pos[1] = 255;

      gdk_pointer_grab (csp->xy_color->window, FALSE,
			GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
			NULL, NULL, bevent->time);
944
      color_select_draw_xy_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
945 946 947 948 949 950 951

      color_select_update (csp, UPDATE_VALUES);
      break;

    case GDK_BUTTON_RELEASE:
      bevent = (GdkEventButton *) event;

952
      color_select_draw_xy_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
953 954 955 956 957 958 959 960 961 962 963 964 965 966

      csp->pos[0] = (bevent->x * 255) / (XY_DEF_WIDTH - 1);
      csp->pos[1] = 255 - (bevent->y * 255) / (XY_DEF_HEIGHT - 1);

      if (csp->pos[0] < 0)
	csp->pos[0] = 0;
      if (csp->pos[0] > 255)
	csp->pos[0] = 255;
      if (csp->pos[1] < 0)
	csp->pos[1] = 0;
      if (csp->pos[1] > 255)
	csp->pos[1] = 255;

      gdk_pointer_ungrab (bevent->time);
967
      color_select_draw_xy_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
968 969 970 971 972 973 974 975 976 977 978 979
      color_select_update (csp, UPDATE_VALUES);
      break;

    case GDK_MOTION_NOTIFY:
      mevent = (GdkEventMotion *) event;
      if (mevent->is_hint)
	{
	  gdk_window_get_pointer (widget->window, &tx, &ty, NULL);
	  mevent->x = tx;
	  mevent->y = ty;
	}

980
      color_select_draw_xy_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
981 982 983 984 985 986 987 988 989 990 991 992 993

      csp->pos[0] = (mevent->x * 255) / (XY_DEF_WIDTH - 1);
      csp->pos[1] = 255 - (mevent->y * 255) / (XY_DEF_HEIGHT - 1);

      if (csp->pos[0] < 0)
	csp->pos[0] = 0;
      if (csp->pos[0] > 255)
	csp->pos[0] = 255;
      if (csp->pos[1] < 0)
	csp->pos[1] = 0;
      if (csp->pos[1] > 255)
	csp->pos[1] = 255;

994
      color_select_draw_xy_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
      color_select_update (csp, UPDATE_VALUES);
      break;

    default:
      break;
    }

  return FALSE;
}

static gint
color_select_z_expose (GtkWidget      *widget,
		       GdkEventExpose *event,
		       ColorSelectP    csp)
{
  if (!csp->gc)
    csp->gc = gdk_gc_new (widget->window);

1013
  color_select_draw_z_marker (csp, &event->area);
Elliot Lee's avatar
Elliot Lee committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

  return FALSE;
}

static gint
color_select_z_events (GtkWidget    *widget,
		       GdkEvent     *event,
		       ColorSelectP  csp)
{
  GdkEventButton *bevent;
  GdkEventMotion *mevent;
  int tx, ty;

  switch (event->type)
    {
    case GDK_BUTTON_PRESS:
      bevent = (GdkEventButton *) event;

1032
      color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

      csp->pos[2] = 255 - (bevent->y * 255) / (Z_DEF_HEIGHT - 1);
      if (csp->pos[2] < 0)
	csp->pos[2] = 0;
      if (csp->pos[2] > 255)
	csp->pos[2] = 255;

      gdk_pointer_grab (csp->z_color->window, FALSE,
			GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
			NULL, NULL, bevent->time);
1043
      color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1044 1045 1046 1047 1048 1049
      color_select_update (csp, UPDATE_VALUES);
      break;

    case GDK_BUTTON_RELEASE:
      bevent = (GdkEventButton *) event;

1050
      color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1051 1052 1053 1054 1055 1056 1057 1058

      csp->pos[2] = 255 - (bevent->y * 255) / (Z_DEF_HEIGHT - 1);
      if (csp->pos[2] < 0)
	csp->pos[2] = 0;
      if (csp->pos[2] > 255)
	csp->pos[2] = 255;

      gdk_pointer_ungrab (bevent->time);
1059
      color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
      color_select_update (csp, UPDATE_VALUES | UPDATE_XY_COLOR);
      break;

    case GDK_MOTION_NOTIFY:
      mevent = (GdkEventMotion *) event;
      if (mevent->is_hint)
	{
	  gdk_window_get_pointer (widget->window, &tx, &ty, NULL);
	  mevent->x = tx;
	  mevent->y = ty;
	}

1072
      color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1073 1074 1075 1076 1077 1078 1079

      csp->pos[2] = 255 - (mevent->y * 255) / (Z_DEF_HEIGHT - 1);
      if (csp->pos[2] < 0)
	csp->pos[2] = 0;
      if (csp->pos[2] > 255)
	csp->pos[2] = 255;

1080
      color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
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 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 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
      color_select_update (csp, UPDATE_VALUES);
      break;

    default:
      break;
    }

  return FALSE;
}

static gint
color_select_color_events (GtkWidget *widget,
			   GdkEvent  *event)
{
  ColorSelectP csp;

  csp = (ColorSelectP) gtk_object_get_user_data (GTK_OBJECT (widget));
  if (!csp)
    return FALSE;

  switch (event->type)
    {
    case GDK_EXPOSE:
      if (!csp->gc)
	csp->gc = gdk_gc_new (widget->window);

      if (widget == csp->new_color)
	color_select_update (csp, UPDATE_NEW_COLOR);
      else if (widget == csp->orig_color)
	color_select_update (csp, UPDATE_ORIG_COLOR);
      break;

    default:
      break;
    }

  return FALSE;
}

static void
color_select_slider_update (GtkAdjustment *adjustment,
			    gpointer       data)
{
  ColorSelectP csp;
  int old_values[6];
  int update_z_marker;
  int update_xy_marker;
  int i, j;

  csp = (ColorSelectP) data;

  if (csp)
    {
      for (i = 0; i < 6; i++)
	if (csp->slider_data[i] == adjustment)
	  break;

      for (j = 0; j < 6; j++)
	old_values[j] = csp->values[j];

      csp->values[i] = (int) adjustment->value;

      if ((i >= HUE) && (i <= VALUE))
	color_select_update_rgb_values (csp);
      else if ((i >= RED) && (i <= BLUE))
	color_select_update_hsv_values (csp);
      color_select_update_sliders (csp, i);
      color_select_update_entries (csp, -1);

      update_z_marker = 0;
      update_xy_marker = 0;
      for (j = 0; j < 6; j++)
	{
	  if (j == csp->z_color_fill)
	    {
	      if (old_values[j] != csp->values[j])
		update_z_marker = 1;
	    }
	  else
	    {
	      if (old_values[j] != csp->values[j])
		update_xy_marker = 1;
	    }
	}

      if (update_z_marker)
	{
1168
	  color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1169
	  color_select_update (csp, UPDATE_POS | UPDATE_XY_COLOR);
1170
	  color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1171 1172 1173 1174
	}
      else
	{
	  if (update_z_marker)
1175
	    color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1176
	  if (update_xy_marker)
1177
	    color_select_draw_xy_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1178 1179 1180 1181

	  color_select_update (csp, UPDATE_POS);

	  if (update_z_marker)
1182
	    color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1183
	  if (update_xy_marker)
1184
	    color_select_draw_xy_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1185 1186 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
	}

      color_select_update (csp, UPDATE_NEW_COLOR);
    }
}

static void
color_select_entry_update (GtkWidget *w,
			   gpointer   data)
{
  ColorSelectP csp;
  int old_values[6];
  int update_z_marker;
  int update_xy_marker;
  int i, j;

  csp = (ColorSelectP) data;

  if (csp)
    {
      for (i = 0; i < 6; i++)
	if (csp->entries[i] == w)
	  break;

      for (j = 0; j < 6; j++)
	old_values[j] = csp->values[j];

      csp->values[i] = atoi (gtk_entry_get_text (GTK_ENTRY (csp->entries[i])));
      if (csp->values[i] == old_values[i])
	return;

      if ((i >= HUE) && (i <= VALUE))
	color_select_update_rgb_values (csp);
      else if ((i >= RED) && (i <= BLUE))
	color_select_update_hsv_values (csp);
      color_select_update_entries (csp, i);
      color_select_update_sliders (csp, -1);

      update_z_marker = 0;
      update_xy_marker = 0;
      for (j = 0; j < 6; j++)
	{
	  if (j == csp->z_color_fill)
	    {
	      if (old_values[j] != csp->values[j])
		update_z_marker = 1;
	    }
	  else
	    {
	      if (old_values[j] != csp->values[j])
		update_xy_marker = 1;
	    }
	}

      if (update_z_marker)
	{
1241
	  color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1242
	  color_select_update (csp, UPDATE_POS | UPDATE_XY_COLOR);
1243
	  color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1244 1245 1246 1247
	}
      else
	{
	  if (update_z_marker)
1248
	    color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1249
	  if (update_xy_marker)
1250
	    color_select_draw_xy_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1251 1252 1253 1254

	  color_select_update (csp, UPDATE_POS);

	  if (update_z_marker)
1255
	    color_select_draw_z_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1256
	  if (update_xy_marker)
1257
	    color_select_draw_xy_marker (csp, NULL);
Elliot Lee's avatar
Elliot Lee committed
1258 1259 1260 1261 1262 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
	}

      color_select_update (csp, UPDATE_NEW_COLOR);
    }
}

static void
color_select_toggle_update (GtkWidget *w,
			    gpointer   data)
{
  ColorSelectP csp;
  ColorSelectFillType type = HUE;
  int i;

  if (!GTK_TOGGLE_BUTTON (w)->active)
    return;

  csp = (ColorSelectP) data;

  if (csp)
    {
      for (i = 0; i < 6; i++)
	if (w == csp->toggles[i])
	  type = (ColorSelectFillType) i;

      switch (type)
	{
	case HUE:
	  csp->z_color_fill = HUE;
	  csp->xy_color_fill = SATURATION_VALUE;
	  break;
	case SATURATION:
	  csp->z_color_fill = SATURATION;
	  csp->xy_color_fill = HUE_VALUE;
	  break;
	case VALUE:
	  csp->z_color_fill = VALUE;
	  csp->xy_color_fill = HUE_SATURATION;
	  break;
	case RED:
	  csp->z_color_fill = RED;
	  csp->xy_color_fill = GREEN_BLUE;
	  break;
	case GREEN:
	  csp->z_color_fill = GREEN;
	  csp->xy_color_fill = RED_BLUE;
	  break;
	case BLUE:
	  csp->z_color_fill = BLUE;
	  csp->xy_color_fill = RED_GREEN;
	  break;
	default:
	  break;
	}

      color_select_update (csp, UPDATE_POS);
      color_select_update (csp, UPDATE_Z_COLOR | UPDATE_XY_COLOR);
    }
}

1318 1319 1320 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
static gint
color_select_hex_entry_leave (GtkWidget *w,
			      GdkEvent  *event,
			      gpointer   data)
{
  ColorSelectP csp;
  gchar buffer[8];
  gchar *hex_color;
  guint hex_rgb;

  csp = (ColorSelectP) data;

  if (csp)
    {
      hex_color = g_strdup (gtk_entry_get_text (GTK_ENTRY (csp->hex_entry)));

      sprintf(buffer, "#%.2x%.2x%.2x",
	      csp->values[RED],
	      csp->values[GREEN],
	      csp->values[BLUE]);

      if ((strlen (hex_color) == 7) &&
	  (g_strcasecmp (buffer, hex_color) != 0))
	{
	  if ((sscanf (hex_color, "#%x", &hex_rgb) == 1) &&
	      (hex_rgb < (1 << 24)))
	    color_select_set_color (csp,
				    (hex_rgb & 0xff0000) >> 16,
				    (hex_rgb & 0x00ff00) >> 8,
				    hex_rgb & 0x0000ff,
				    TRUE);
	}

      g_free (hex_color);
    }

  return FALSE;
}

Elliot Lee's avatar
Elliot Lee committed
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
static void
color_select_image_fill (GtkWidget           *preview,
			 ColorSelectFillType  type,
			 int                 *values)
{
  ColorSelectFill csf;
  int height;

  csf.buffer = g_malloc (preview->requisition.width * 3);

  csf.update = update_procs[type];

  csf.y = -1;
  csf.width = preview->requisition.width;
  csf.height = preview->requisition.height;
  csf.values = values;

  height = csf.height;
  if (height > 0)
    while (height--)
      {
	(* csf.update) (&csf);
	gtk_preview_draw_row (GTK_PREVIEW (preview), csf.buffer, 0, csf.y, csf.width);
      }

  g_free (csf.buffer);
}

static void
color_select_draw_z_marker (ColorSelectP csp,
1387
			    GdkRectangle *clip)
Elliot Lee's avatar
Elliot Lee committed
1388 1389
{
  int width;
1390
  int height;
Elliot Lee's avatar
Elliot Lee committed
1391
  int y;
1392 1393
  int minx;
  int miny;
Elliot Lee's avatar
Elliot Lee committed
1394 1395 1396 1397 1398

  if (csp->gc)
    {
      y = (Z_DEF_HEIGHT - 1) - ((Z_DEF_HEIGHT - 1) * csp->pos[2]) / 255;
      width = csp->z_color->requisition.width;
1399 1400 1401
      height = csp->z_color->requisition.height;
      minx = 0;
      miny = 0;
Elliot Lee's avatar
Elliot Lee committed
1402 1403 1404
      if (width <= 0)
	return;

1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
      if (clip)
        {
	  width  = MIN(width,  clip->x + clip->width);
	  height = MIN(height, clip->y + clip->height);
	  minx   = MAX(0, clip->x);
	  miny   = MAX(0, clip->y);
	}

      if (y >= miny && y < height)
        {
	  gdk_gc_set_function (csp->gc, GDK_INVERT);
1416
	  gdk_draw_line (csp->z_color->window, csp->gc, minx, y, width - 1, y);
1417 1418
	  gdk_gc_set_function (csp->gc, GDK_COPY);
	}
Elliot Lee's avatar
Elliot Lee committed
1419 1420 1421 1422 1423
    }
}

static void
color_select_draw_xy_marker (ColorSelectP csp,
1424
			     GdkRectangle *clip)
Elliot Lee's avatar
Elliot Lee committed
1425 1426 1427 1428
{
  int width;
  int height;
  int x, y;
1429
  int minx, miny;
Elliot Lee's avatar
Elliot Lee committed
1430 1431 1432 1433 1434 1435 1436

  if (csp->gc)
    {
      x = ((XY_DEF_WIDTH - 1) * csp->pos[0]) / 255;
      y = (XY_DEF_HEIGHT - 1) - ((XY_DEF_HEIGHT - 1) * csp->pos[1]) / 255;
      width = csp->xy_color->requisition.width;
      height = csp->xy_color->requisition.height;
1437 1438
      minx = 0;
      miny = 0;
Elliot Lee's avatar
Elliot Lee committed
1439 1440 1441 1442
      if ((width <= 0) || (height <= 0))
	return;

      gdk_gc_set_function (csp->gc, GDK_INVERT);
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452

      if (clip)
        {
	  width  = MIN(width,  clip->x + clip->width);
	  height = MIN(height, clip->y + clip->height);
	  minx   = MAX(0, clip->x);
	  miny   = MAX(0, clip->y);
	}

      if (y >= miny && y < height)
1453
	gdk_draw_line (csp->xy_color->window, csp->gc, minx, y, width - 1, y);
1454 1455

      if (x >= minx && x < width)
1456
	gdk_draw_line (csp->xy_color->window, csp->gc, x, miny, x, height - 1);
1457

Elliot Lee's avatar
Elliot Lee committed
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 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 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 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 1779 1780 1781 1782 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 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 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 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
      gdk_gc_set_function (csp->gc, GDK_COPY);
    }
}

static void
color_select_update_red (ColorSelectFill *csf)
{
  unsigned char *p;
  int i, r;

  p = csf->buffer;

  csf->y += 1;
  r = (csf->height - csf->y + 1) * 255 / csf->height;

  if (r < 0)
    r = 0;
  if (r > 255)
    r = 255;

  for (i = 0; i < csf->width; i++)
    {
      *p++ = r;
      *p++ = 0;
      *p++ = 0;
    }
}

static void
color_select_update_green (ColorSelectFill *csf)
{
  unsigned char *p;
  int i, g;

  p = csf->buffer;

  csf->y += 1;
  g = (csf->height - csf->y + 1) * 255 / csf->height;

  if (g < 0)
    g = 0;
  if (g > 255)
    g = 255;

  for (i = 0; i < csf->width; i++)
    {
      *p++ = 0;
      *p++ = g;
      *p++ = 0;
    }
}

static void
color_select_update_blue (ColorSelectFill *csf)
{
  unsigned char *p;
  int i, b;

  p = csf->buffer;

  csf->y += 1;
  b = (csf->height - csf->y + 1) * 255 / csf->height;

  if (b < 0)
    b = 0;
  if (b > 255)
    b = 255;

  for (i = 0; i < csf->width; i++)
    {
      *p++ = 0;
      *p++ = 0;
      *p++ = b;
    }
}

static void
color_select_update_hue (ColorSelectFill *csf)
{
  unsigned char *p;
  float h, f;
  int r, g, b;
  int i;

  p = csf->buffer;

  csf->y += 1;
  h = csf->y * 360 / csf->height;

  h = 360 - h;

  if (h < 0)
    h = 0;
  if (h >= 360)
    h = 0;

  h /= 60;
  f = (h - (int) h) * 255;

  r = g = b = 0;

  switch ((int) h)
    {
    case 0:
      r = 255;
      g = f;
      b = 0;
      break;
    case 1:
      r = 255 - f;
      g = 255;
      b = 0;
      break;
    case 2:
      r = 0;
      g = 255;
      b = f;
      break;
    case 3:
      r = 0;
      g = 255 - f;
      b = 255;
      break;
    case 4:
      r = f;
      g = 0;
      b = 255;
      break;
    case 5:
      r = 255;
      g = 0;
      b = 255 - f;
      break;
    }

  for (i = 0; i < csf->width; i++)
    {
      *p++ = r;
      *p++ = g;
      *p++ = b;
    }
}

static void
color_select_update_saturation (ColorSelectFill *csf)
{
  unsigned char *p;
  int s;
  int i;

  p = csf->buffer;

  csf->y += 1;
  s = csf->y * 255 / csf->height;

  if (s < 0)
    s = 0;
  if (s > 255)
    s = 255;

  s = 255 - s;

  for (i = 0; i < csf->width; i++)
    {
      *p++ = s;
      *p++ = s;
      *p++ = s;
    }
}

static void
color_select_update_value (ColorSelectFill *csf)
{
  unsigned char *p;
  int v;
  int i;

  p = csf->buffer;

  csf->y += 1;
  v = csf->y * 255 / csf->height;

  if (v < 0)
    v = 0;
  if (v > 255)
    v = 255;

  v = 255 - v;

  for (i = 0; i < csf->width; i++)
    {
      *p++ = v;
      *p++ = v;
      *p++ = v;
    }
}

static void
color_select_update_red_green (ColorSelectFill *csf)
{
  unsigned char *p;
  int i, r, b;
  float g, dg;

  p = csf->buffer;

  csf->y += 1;
  b = csf->values[BLUE];
  r = (csf->height - csf->y + 1) * 255 / csf->height;

  if (r < 0)
    r = 0;
  if (r > 255)
    r = 255;

  g = 0;
  dg = 255.0 / csf->width;

  for (i = 0; i < csf->width; i++)
    {
      *p++ = r;
      *p++ = g;
      *p++ = b;

      g += dg;
    }
}

static void
color_select_update_red_blue (ColorSelectFill *csf)
{
  unsigned char *p;
  int i, r, g;
  float b, db;

  p = csf->buffer;

  csf->y += 1;
  g = csf->values[GREEN];
  r = (csf->height - csf->y + 1) * 255 / csf->height;

  if (r < 0)
    r = 0;
  if (r > 255)
    r = 255;

  b = 0;
  db = 255.0 / csf->width;

  for (i = 0; i < csf->width; i++)
    {
      *p++ = r;
      *p++ = g;
      *p++ = b;

      b += db;
    }
}

static void
color_select_update_green_blue (ColorSelectFill *csf)
{
  unsigned char *p;
  int i, g, r;
  float b, db;

  p = csf->buffer;

  csf->y += 1;
  r = csf->values[RED];
  g = (csf->height - csf->y + 1) * 255 / csf->height;

  if (g < 0)
    g = 0;
  if (g > 255)
    g = 255;

  b = 0;
  db = 255.0 / csf->width;

  for (i = 0; i < csf->width; i++)
    {
      *p++ = r;
      *p++ = g;
      *p++ = b;

      b += db;
    }
}

static void
color_select_update_hue_saturation (ColorSelectFill *csf)
{
  unsigned char *p;
  float h, v, s, ds;
  int f;
  int i;

  p = csf->buffer;

  csf->y += 1;
  h = 360 - (csf->y * 360 / csf->height);

  if (h < 0)
    h = 0;
  if (h > 359)
    h = 359;

  h /= 60;
  f = (h - (int) h) * 255;

  s = 0;
  ds = 1.0 / csf->width;

  v = csf->values[VALUE] / 100.0;

  switch ((int) h)
    {
    case 0:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255;
	  *p++ = v * (255 - (s * (255 - f)));
	  *p++ = v * 255 * (1 - s);

	  s += ds;
	}
      break;
    case 1:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * (255 - s * f);
	  *p++ = v * 255;
	  *p++ = v * 255 * (1 - s);

	  s += ds;
	}
      break;
    case 2:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255 * (1 - s);
	  *p++ = v *255;
	  *p++ = v * (255 - (s * (255 - f)));

	  s += ds;
	}
      break;
    case 3:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255 * (1 - s);
	  *p++ = v * (255 - s * f);
	  *p++ = v * 255;

	  s += ds;
	}
      break;
    case 4:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * (255 - (s * (255 - f)));
	  *p++ = v * (255 * (1 - s));
	  *p++ = v * 255;

	  s += ds;
	}
      break;
    case 5:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255;
	  *p++ = v * 255 * (1 - s);
	  *p++ = v * (255 - s * f);

	  s += ds;
	}
      break;
    }
}

static void
color_select_update_hue_value (ColorSelectFill *csf)
{
  unsigned char *p;
  float h, v, dv, s;
  int f;
  int i;

  p = csf->buffer;

  csf->y += 1;
  h = 360 - (csf->y * 360 / csf->height);

  if (h < 0)
    h = 0;
  if (h > 359)
    h = 359;

  h /= 60;
  f = (h - (int) h) * 255;

  v = 0;
  dv = 1.0 / csf->width;

  s = csf->values[SATURATION] / 100.0;

  switch ((int) h)
    {
    case 0:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255;
	  *p++ = v * (255 - (s * (255 - f)));
	  *p++ = v * 255 * (1 - s);

	  v += dv;
	}
      break;
    case 1:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * (255 - s * f);
	  *p++ = v * 255;
	  *p++ = v * 255 * (1 - s);

	  v += dv;
	}
      break;
    case 2:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255 * (1 - s);
	  *p++ = v *255;
	  *p++ = v * (255 - (s * (255 - f)));

	  v += dv;
	}
      break;
    case 3:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255 * (1 - s);
	  *p++ = v * (255 - s * f);
	  *p++ = v * 255;

	  v += dv;
	}
      break;
    case 4:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * (255 - (s * (255 - f)));
	  *p++ = v * (255 * (1 - s));
	  *p++ = v * 255;

	  v += dv;
	}
      break;
    case 5:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255;
	  *p++ = v * 255 * (1 - s);
	  *p++ = v * (255 - s * f);

	  v += dv;
	}
      break;
    }
}

static void
color_select_update_saturation_value (ColorSelectFill *csf)
{
  unsigned char *p;
  float h, v, dv, s;
  int f;
  int i;

  p = csf->buffer;

  csf->y += 1;
  s = (float) csf->y / csf->height;

  if (s < 0)
    s = 0;
  if (s > 1)
    s = 1;

  s = 1 - s;

  h = (float) csf->values[HUE];
  if (h >= 360)
    h -= 360;
  h /= 60;
  f = (h - (int) h) * 255;

  v = 0;
  dv = 1.0 / csf->width;

  switch ((int) h)
    {
    case 0:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255;
	  *p++ = v * (255 - (s * (255 - f)));
	  *p++ = v * 255 * (1 - s);

	  v += dv;
	}
      break;
    case 1:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * (255 - s * f);
	  *p++ = v * 255;
	  *p++ = v * 255 * (1 - s);

	  v += dv;
	}
      break;
    case 2:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255 * (1 - s);
	  *p++ = v *255;
	  *p++ = v * (255 - (s * (255 - f)));

	  v += dv;
	}
      break;
    case 3:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255 * (1 - s);
	  *p++ = v * (255 - s * f);
	  *p++ = v * 255;

	  v += dv;
	}
      break;
    case 4:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * (255 - (s * (255 - f)));
	  *p++ = v * (255 * (1 - s));
	  *p++ = v * 255;

	  v += dv;
	}
      break;
    case 5:
      for (i = 0; i < csf->width; i++)
	{
	  *p++ = v * 255;
	  *p++ = v * 255 * (1 - s);
	  *p++ = v * (255 - s * f);

	  v += dv;
	}
      break;
    }
}
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115


/*****************************/
/* Colour notebook glue      */

typedef struct {
  GimpColorSelector_Callback      callback;
  void                           *client_data;
  ColorSelectP                    csp;
  GtkWidget                      *main_vbox;
} notebook_glue;


static GtkWidget *
color_select_notebook_new (int r, int g, int b,
	      GimpColorSelector_Callback callback,  void *client_data,
	      /* RETURNS: */
	      void **selector_data)
{
  ColorSelectP csp;
  notebook_glue *glue;

  glue = g_malloc (sizeof (notebook_glue));

  csp = g_malloc (sizeof (_ColorSelect));

  glue->csp = csp;
  glue->callback = callback;
  glue->client_data = client_data;

  csp->callback = color_select_notebook_update_callback;
  csp->client_data = glue;
  csp->z_color_fill = HUE;
  csp->xy_color_fill = SATURATION_VALUE;
  csp->gc = NULL;
  csp->wants_updates = TRUE;

  csp->values[RED] = csp->orig_values[0] = r;
  csp->values[GREEN] = csp->orig_values[1] = g;
  csp->values[BLUE] = csp->orig_values[2] = b;
  color_select_update_hsv_values (csp);
  color_select_update_pos (csp);

  glue->main_vbox = color_select_widget_new (csp, r, g, b);

  /* the shell is provided by the notebook */
  csp->shell = NULL;

  color_select_image_fill (csp->z_color, csp->z_color_fill, csp->values);
  color_select_image_fill (csp->xy_color, csp->xy_color_fill, csp->values);

  (*selector_data) = glue;
  return glue->main_vbox;
}


static void
color_select_notebook_free (void *data)
{
  notebook_glue *glue = data;

  color_select_free (glue->csp);
  /* don't need to destroy the widget, since it's done by the caller
   * of this function */
  g_free (glue);
}


static void
color_select_notebook_setcolor (void *data,
				int r, int g, int b, int set_current)
{
  notebook_glue *glue = data;

  color_select_set_color (glue->csp, r, g, b, set_current);
}

static void
color_select_notebook_update_callback (int r, int g, int b,
				       ColorSelectState state, void *data)
{
  notebook_glue *glue = data;

  switch (state) { 
  case COLOR_SELECT_UPDATE:
    glue->callback (glue->client_data, r, g, b);
    break;

  default:
    g_warning ("state %d can't happen!", state);
    break;
  }
}