gtkcolorsel.c 80.6 KB
Newer Older
1
/* GTK - The GIMP Toolkit
2
 * Copyright (C) 2000 Red Hat, Inc.
Elliot Lee's avatar
Elliot Lee committed
3 4 5
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
7 8 9 10 11 12
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
13
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
19
 */
20 21

/*
22
 * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
23 24 25 26 27
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

28
#include <config.h>
29
#include "gdkconfig.h"
30
#include <math.h>
31

32
#include "gtkalias.h"
33
#include "gdk/gdkkeysyms.h"
Elliot Lee's avatar
Elliot Lee committed
34
#include "gtkcolorsel.h"
35
#include "gtkhsv.h"
Owen Taylor's avatar
Owen Taylor committed
36
#include "gtkwindow.h"
37 38 39 40
#include "gtkselection.h"
#include "gtkdnd.h"
#include "gtkdrawingarea.h"
#include "gtkhbox.h"
41
#include "gtkhbbox.h"
42 43 44 45
#include "gtkrc.h"
#include "gtkframe.h"
#include "gtktable.h"
#include "gtklabel.h"
46
#include "gtkmarshalers.h"
Manish Singh's avatar
Manish Singh committed
47
#include "gtkimage.h"
48 49 50 51 52 53
#include "gtkspinbutton.h"
#include "gtkrange.h"
#include "gtkhscale.h"
#include "gtkentry.h"
#include "gtkbutton.h"
#include "gtkhseparator.h"
54 55 56 57 58
#include "gtktooltips.h"
#include "gtkinvisible.h"
#include "gtkmenuitem.h"
#include "gtkmain.h"
#include "gtksettings.h"
59
#include "gtkintl.h"
60
#include "gtkimage.h"
61
#include "gtkstock.h"
62
#include "gtkaccessible.h"
Elliot Lee's avatar
Elliot Lee committed
63

64 65
#include <string.h>

66 67 68 69 70
#ifdef GDK_WINDOWING_X11
#include <X11/Xlib.h>
#include "x11/gdkx.h"
#endif

71 72 73 74
/* Number of elements in the custom palatte */
#define GTK_CUSTOM_PALETTE_WIDTH 10
#define GTK_CUSTOM_PALETTE_HEIGHT 2

75 76 77 78 79 80 81
/* Conversion between 0->1 double and and guint16. See
 * scale_round() below for more general conversions
 */
#define SCALE(i) (i / 65535.)
#define UNSCALE(d) ((guint16)(d * 65535 + 0.5))


82
enum {
Elliot Lee's avatar
Elliot Lee committed
83 84 85 86
  COLOR_CHANGED,
  LAST_SIGNAL
};

87 88 89 90 91
enum {
  PROP_0,
  PROP_HAS_PALETTE,
  PROP_HAS_OPACITY_CONTROL,
  PROP_CURRENT_COLOR,
92
  PROP_CURRENT_ALPHA
93 94
};

95
enum {
96 97 98 99 100 101 102 103
  COLORSEL_RED = 0,
  COLORSEL_GREEN = 1,
  COLORSEL_BLUE = 2,
  COLORSEL_OPACITY = 3,
  COLORSEL_HUE,
  COLORSEL_SATURATION,
  COLORSEL_VALUE,
  COLORSEL_NUM_CHANNELS
Elliot Lee's avatar
Elliot Lee committed
104 105
};

106
typedef struct _ColorSelectionPrivate ColorSelectionPrivate;
107

108
struct _ColorSelectionPrivate
Elliot Lee's avatar
Elliot Lee committed
109
{
110 111
  guint has_opacity : 1;
  guint has_palette : 1;
112 113
  guint changing : 1;
  guint default_set : 1;
114
  guint default_alpha_set : 1;
115
  
116 117
  gdouble color[COLORSEL_NUM_CHANNELS];
  gdouble old_color[COLORSEL_NUM_CHANNELS];
118
  
119 120 121 122 123 124 125 126 127 128 129 130
  GtkWidget *triangle_colorsel;
  GtkWidget *hue_spinbutton;
  GtkWidget *sat_spinbutton;
  GtkWidget *val_spinbutton;
  GtkWidget *red_spinbutton;
  GtkWidget *green_spinbutton;
  GtkWidget *blue_spinbutton;
  GtkWidget *opacity_slider;
  GtkWidget *opacity_label;
  GtkWidget *opacity_entry;
  GtkWidget *palette_frame;
  GtkWidget *hex_entry;
131
  
132
  /* The Palette code */
133 134
  GtkWidget *custom_palette [GTK_CUSTOM_PALETTE_WIDTH][GTK_CUSTOM_PALETTE_HEIGHT];
  
135 136 137 138 139
  /* The color_sample stuff */
  GtkWidget *sample_area;
  GtkWidget *old_sample;
  GtkWidget *cur_sample;
  GtkWidget *colorsel;
140 141 142 143 144 145

  /* Tooltips group */
  GtkTooltips *tooltips;

  /* Window for grabbing on */
  GtkWidget *dropper_grab_widget;
146 147

  /* Connection to settings */
148
  gulong settings_connection;
Elliot Lee's avatar
Elliot Lee committed
149 150 151
};


152
static void gtk_color_selection_init		(GtkColorSelection	 *colorsel);
153
static void gtk_color_selection_class_init	(GtkColorSelectionClass	 *klass);
154
static void gtk_color_selection_destroy		(GtkObject		 *object);
155
static void gtk_color_selection_finalize        (GObject		 *object);
156
static void update_color			(GtkColorSelection	 *colorsel);
157 158 159 160 161 162 163 164
static void gtk_color_selection_set_property    (GObject                 *object,
					         guint                    prop_id,
					         const GValue            *value,
					         GParamSpec              *pspec);
static void gtk_color_selection_get_property    (GObject                 *object,
					         guint                    prop_id,
					         GValue                  *value,
					         GParamSpec              *pspec);
165

166 167
static void gtk_color_selection_realize         (GtkWidget               *widget);
static void gtk_color_selection_unrealize       (GtkWidget               *widget);
168
static void gtk_color_selection_show_all        (GtkWidget               *widget);
169

170 171 172
static void     gtk_color_selection_set_palette_color   (GtkColorSelection *colorsel,
                                                         gint               index,
                                                         GdkColor          *color);
173 174
static GdkGC   *get_focus_gc                            (GtkWidget         *drawing_area,
							 gint              *focus_width);
175 176 177 178
static void     default_noscreen_change_palette_func    (const GdkColor    *colors,
							 gint               n_colors);
static void     default_change_palette_func             (GdkScreen	   *screen,
							 const GdkColor    *colors,
179
							 gint               n_colors);
180 181 182 183
static void     make_control_relations                  (AtkObject         *atk_obj,
                                                         GtkWidget         *widget);
static void     make_all_relations                      (AtkObject         *atk_obj,
                                                         ColorSelectionPrivate *priv);
184

185
static gpointer parent_class = NULL;
186 187
static guint color_selection_signals[LAST_SIGNAL] = { 0 };

188
static const gchar default_colors[] = "black:white:gray50:red:purple:blue:light blue:green:yellow:orange:lavender:brown:goldenrod4:dodger blue:pink:light green:gray10:gray30:gray75:gray90";
189

190 191
static GtkColorSelectionChangePaletteFunc noscreen_change_palette_hook = default_noscreen_change_palette_func;
static GtkColorSelectionChangePaletteWithScreenFunc change_palette_hook = default_change_palette_func;
192 193 194 195 196 197 198 199

/* The cursor for the dropper */
#define DROPPER_WIDTH 17
#define DROPPER_HEIGHT 17
#define DROPPER_X_HOT 2
#define DROPPER_Y_HOT 16


Matthias Clasen's avatar
Matthias Clasen committed
200
static const guchar dropper_bits[] = {
201 202 203 204 205 206
  0xff, 0x8f, 0x01, 0xff, 0x77, 0x01, 0xff, 0xfb, 0x00, 0xff, 0xf8, 0x00,
  0x7f, 0xff, 0x00, 0xff, 0x7e, 0x01, 0xff, 0x9d, 0x01, 0xff, 0xd8, 0x01,
  0x7f, 0xd4, 0x01, 0x3f, 0xee, 0x01, 0x1f, 0xff, 0x01, 0x8f, 0xff, 0x01,
  0xc7, 0xff, 0x01, 0xe3, 0xff, 0x01, 0xf3, 0xff, 0x01, 0xfd, 0xff, 0x01,
  0xff, 0xff, 0x01, };

Matthias Clasen's avatar
Matthias Clasen committed
207
static const guchar dropper_mask[] = {
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  0x00, 0x70, 0x00, 0x00, 0xf8, 0x00, 0x00, 0xfc, 0x01, 0x00, 0xff, 0x01,
  0x80, 0xff, 0x01, 0x00, 0xff, 0x00, 0x00, 0x7f, 0x00, 0x80, 0x3f, 0x00,
  0xc0, 0x3f, 0x00, 0xe0, 0x13, 0x00, 0xf0, 0x01, 0x00, 0xf8, 0x00, 0x00,
  0x7c, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x0d, 0x00, 0x00,
  0x02, 0x00, 0x00, };


/*
 *
 * The Sample Color
 *
 */
#define SAMPLE_WIDTH  64
#define SAMPLE_HEIGHT 28

static void color_sample_draw_sample (GtkColorSelection *colorsel, int which);
static void color_sample_draw_samples (GtkColorSelection *colorsel);
Elliot Lee's avatar
Elliot Lee committed
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
static void
set_color_internal (GtkColorSelection *colorsel,
		    gdouble           *color)
{
  ColorSelectionPrivate *priv;
  gint i;
  
  priv = colorsel->private_data;
  priv->changing = TRUE;
  priv->color[COLORSEL_RED] = color[0];
  priv->color[COLORSEL_GREEN] = color[1];
  priv->color[COLORSEL_BLUE] = color[2];
  priv->color[COLORSEL_OPACITY] = color[3];
  gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
		  priv->color[COLORSEL_GREEN],
		  priv->color[COLORSEL_BLUE],
		  &priv->color[COLORSEL_HUE],
		  &priv->color[COLORSEL_SATURATION],
		  &priv->color[COLORSEL_VALUE]);
  if (priv->default_set == FALSE)
    {
      for (i = 0; i < COLORSEL_NUM_CHANNELS; i++)
	priv->old_color[i] = priv->color[i];
    }
  priv->default_set = TRUE;
  priv->default_alpha_set = TRUE;
  update_color (colorsel);
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
static void
set_color_icon (GdkDragContext *context,
		gdouble        *colors)
{
  GdkPixbuf *pixbuf;
  guint32 pixel;

  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE,
			   8, 48, 32);

  pixel = (((UNSCALE (colors[COLORSEL_RED])   & 0xff00) << 16) |
	   ((UNSCALE (colors[COLORSEL_GREEN]) & 0xff00) << 8) |
	   ((UNSCALE (colors[COLORSEL_BLUE])  & 0xff00)));

  gdk_pixbuf_fill (pixbuf, pixel);
  
  gtk_drag_set_icon_pixbuf (context, pixbuf, -2, -2);
Manish Singh's avatar
Manish Singh committed
272
  g_object_unref (pixbuf);
273 274
}

275
static void
276 277 278
color_sample_drag_begin (GtkWidget      *widget,
			 GdkDragContext *context,
			 gpointer        data)
Elliot Lee's avatar
Elliot Lee committed
279
{
280 281 282
  GtkColorSelection *colorsel = data;
  ColorSelectionPrivate *priv;
  gdouble *colsrc;
283
  
284
  priv = colorsel->private_data;
285
  
286 287 288 289
  if (widget == priv->old_sample)
    colsrc = priv->old_color;
  else
    colsrc = priv->color;
290 291

  set_color_icon (context, colsrc);
Elliot Lee's avatar
Elliot Lee committed
292 293
}

294
static void
295 296 297
color_sample_drag_end (GtkWidget      *widget,
		       GdkDragContext *context,
		       gpointer        data)
Elliot Lee's avatar
Elliot Lee committed
298
{
Manish Singh's avatar
Manish Singh committed
299
  g_object_set_data (G_OBJECT (widget), "gtk-color-selection-drag-window", NULL);
300
}
Elliot Lee's avatar
Elliot Lee committed
301

302 303 304 305 306 307 308 309 310 311 312 313 314 315
static void
color_sample_drop_handle (GtkWidget        *widget,
			  GdkDragContext   *context,
			  gint              x,
			  gint              y,
			  GtkSelectionData *selection_data,
			  guint             info,
			  guint             time,
			  gpointer          data)
{
  GtkColorSelection *colorsel = data;
  ColorSelectionPrivate *priv;
  guint16 *vals;
  gdouble color[4];
316
  priv = colorsel->private_data;
317
  
318 319 320 321 322 323
  /* This is currently a guint16 array of the format:
   * R
   * G
   * B
   * opacity
   */
324
  
325 326
  if (selection_data->length < 0)
    return;
327
  
328 329 330 331
  /* We accept drops with the wrong format, since the KDE color
   * chooser incorrectly drops application/x-color with format 8.
   */
  if (selection_data->length != 8)
332 333 334
    {
      g_warning ("Received invalid color data\n");
      return;
Elliot Lee's avatar
Elliot Lee committed
335
    }
336
  
337
  vals = (guint16 *)selection_data->data;
338
  
339
  if (widget == priv->cur_sample)
340
    {
341 342 343 344
      color[0] = (gdouble)vals[0] / 0xffff;
      color[1] = (gdouble)vals[1] / 0xffff;
      color[2] = (gdouble)vals[2] / 0xffff;
      color[3] = (gdouble)vals[3] / 0xffff;
345
      
346
      set_color_internal (colorsel, color);
347 348 349 350
    }
}

static void
351 352 353 354 355 356
color_sample_drag_handle (GtkWidget        *widget,
			  GdkDragContext   *context,
			  GtkSelectionData *selection_data,
			  guint             info,
			  guint             time,
			  gpointer          data)
357
{
358 359 360 361
  GtkColorSelection *colorsel = data;
  ColorSelectionPrivate *priv;
  guint16 vals[4];
  gdouble *colsrc;
362
  
363
  priv = colorsel->private_data;
364
  
365 366 367 368
  if (widget == priv->old_sample)
    colsrc = priv->old_color;
  else
    colsrc = priv->color;
369
  
370 371 372
  vals[0] = colsrc[COLORSEL_RED] * 0xffff;
  vals[1] = colsrc[COLORSEL_GREEN] * 0xffff;
  vals[2] = colsrc[COLORSEL_BLUE] * 0xffff;
373
  vals[3] = priv->has_opacity ? colsrc[COLORSEL_OPACITY] * 0xffff : 0xffff;
374
  
375 376 377
  gtk_selection_data_set (selection_data,
			  gdk_atom_intern ("application/x-color", FALSE),
			  16, (guchar *)vals, 8);
Elliot Lee's avatar
Elliot Lee committed
378 379
}

380 381 382
/* which = 0 means draw old sample, which = 1 means draw new */
static void
color_sample_draw_sample (GtkColorSelection *colorsel, int which)
Elliot Lee's avatar
Elliot Lee committed
383
{
384 385 386 387 388 389
  GtkWidget *da;
  gint x, y, i, wid, heig, f, n, goff;
  guchar c[3 * 2], cc[3 * 4], *cp = c;
  gdouble o;
  guchar *buf;
  ColorSelectionPrivate *priv;
390
  
Elliot Lee's avatar
Elliot Lee committed
391
  g_return_if_fail (colorsel != NULL);
392
  priv = colorsel->private_data;
393
  
394 395 396
  g_return_if_fail (priv->sample_area != NULL);
  if (!GTK_WIDGET_DRAWABLE (priv->sample_area))
    return;
397
  
398 399 400 401
  if (which == 0)
    {
      da = priv->old_sample;
      for (n = 0, i = COLORSEL_RED; n < 3; n++, i++)
402
	c[n] = (guchar) (UNSCALE (priv->old_color[i]) >> 8);
403 404 405 406 407 408
      goff = 0;
    }
  else
    {
      da = priv->cur_sample;
      for (n = 0, i = COLORSEL_RED; n < 3; n++, i++)
409
	c[n] = (guchar) (UNSCALE (priv->color[i]) >> 8);
410
      goff =  priv->old_sample->allocation.width % 32;
Elliot Lee's avatar
Elliot Lee committed
411
    }
412
  
413 414
  wid = da->allocation.width;
  heig = da->allocation.height;
415
  
416
  buf = g_new (guchar, 3 * wid * heig);
417
  
418 419 420 421 422 423 424 425
#if 0
  i = COLORSEL_RED;
  for (n = 0; n < 3; n++)
    {
      c[n] = (guchar) (255.0 * priv->old_color[i]);
      c[n + 3] = (guchar) (255.0 * priv->color[i++]);
    }
#endif
426
  
427
  if (priv->has_opacity)
Elliot Lee's avatar
Elliot Lee committed
428
    {
429
      o = (which) ? priv->color[COLORSEL_OPACITY] : priv->old_color[COLORSEL_OPACITY];
430
      
431 432 433 434 435 436
      for (n = 0; n < 3; n++)
	{
	  cc[n] = (guchar) ((1.0 - o) * 192 + (o * (gdouble) c[n]));
	  cc[n + 3] = (guchar) ((1.0 - o) * 128 + (o * (gdouble) c[n]));
	}
      cp = cc;
Elliot Lee's avatar
Elliot Lee committed
437
    }
438
  
439 440
  i = 0;
  for (y = 0; y < heig; y++)
Elliot Lee's avatar
Elliot Lee committed
441
    {
442 443
      for (x = 0; x < wid; x++)
	{
444
	  if (priv->has_opacity)
445 446 447
	    f = 3 * ((((goff + x) % 32) < 16) ^ ((y % 32) < 16));
	  else
	    f = 0;
448
	  
449 450 451
	  for (n = 0; n < 3; n++)
	    buf[i++] = cp[n + f];
	}
Elliot Lee's avatar
Elliot Lee committed
452
    }
453
  
454 455 456 457 458 459 460
  gdk_draw_rgb_image (da->window,
		      da->style->black_gc,
		      0, 0,
		      wid, heig,
		      GDK_RGB_DITHER_NORMAL,
		      buf,
		      3*wid);
461 462
  
  
463
  g_free (buf);
Elliot Lee's avatar
Elliot Lee committed
464 465 466
}


467 468 469 470 471 472
static void
color_sample_draw_samples (GtkColorSelection *colorsel)
{
  color_sample_draw_sample (colorsel, 0);
  color_sample_draw_sample (colorsel, 1);
}
Elliot Lee's avatar
Elliot Lee committed
473

474
static gboolean
475 476 477
color_old_sample_expose (GtkWidget         *da,
			 GdkEventExpose    *event,
			 GtkColorSelection *colorsel)
478 479
{
  color_sample_draw_sample (colorsel, 0);
480
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
481 482
}

483

484
static gboolean
485 486 487
color_cur_sample_expose (GtkWidget         *da,
			 GdkEventExpose    *event,
			 GtkColorSelection *colorsel)
Elliot Lee's avatar
Elliot Lee committed
488
{
489
  color_sample_draw_sample (colorsel, 1);
490
  return FALSE;
491
}
492

493 494 495
static void
color_sample_setup_dnd (GtkColorSelection *colorsel, GtkWidget *sample)
{
496
  static const GtkTargetEntry targets[] = {
497 498
    { "application/x-color", 0 }
  };
499
  ColorSelectionPrivate *priv;
500
  priv = colorsel->private_data;
501
  
502
  gtk_drag_source_set (sample,
503 504 505
		       GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
		       targets, 1,
		       GDK_ACTION_COPY | GDK_ACTION_MOVE);
506
  
Manish Singh's avatar
Manish Singh committed
507 508 509 510
  g_signal_connect (sample,
		    "drag_begin",
		    G_CALLBACK (color_sample_drag_begin),
		    colorsel);
511 512
  if (sample == priv->cur_sample)
    {
513
      
514 515 516 517 518 519
      gtk_drag_dest_set (sample,
			 GTK_DEST_DEFAULT_HIGHLIGHT |
			 GTK_DEST_DEFAULT_MOTION |
			 GTK_DEST_DEFAULT_DROP,
			 targets, 1,
			 GDK_ACTION_COPY);
520
      
Manish Singh's avatar
Manish Singh committed
521 522 523 524
      g_signal_connect (sample,
			"drag_end",
			G_CALLBACK (color_sample_drag_end),
			colorsel);
525
    }
526
  
Manish Singh's avatar
Manish Singh committed
527 528 529 530 531 532 533 534
  g_signal_connect (sample,
		    "drag_data_get",
		    G_CALLBACK (color_sample_drag_handle),
		    colorsel);
  g_signal_connect (sample,
		    "drag_data_received",
		    G_CALLBACK (color_sample_drop_handle),
		    colorsel);
535
  
Elliot Lee's avatar
Elliot Lee committed
536 537
}

538

539
static void
540
color_sample_new (GtkColorSelection *colorsel)
541
{
542
  ColorSelectionPrivate *priv;
543
  
544
  priv = colorsel->private_data;
545
  
546 547 548
  priv->sample_area = gtk_hbox_new (FALSE, 0);
  priv->old_sample = gtk_drawing_area_new ();
  priv->cur_sample = gtk_drawing_area_new ();
549 550 551 552 553 554

  /* We need enter/leave to do tooltips */
  gtk_widget_add_events (priv->old_sample,
                         GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
  gtk_widget_add_events (priv->cur_sample,
                         GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
555
  
556 557 558 559
  gtk_box_pack_start (GTK_BOX (priv->sample_area), priv->old_sample,
		      TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (priv->sample_area), priv->cur_sample,
		      TRUE, TRUE, 0);
560
  
Manish Singh's avatar
Manish Singh committed
561 562 563 564 565 566
  g_signal_connect (priv->old_sample, "expose_event",
		    G_CALLBACK (color_old_sample_expose),
		    colorsel);
  g_signal_connect (priv->cur_sample, "expose_event",
		    G_CALLBACK (color_cur_sample_expose),
		    colorsel);
567
  
568 569
  color_sample_setup_dnd (colorsel, priv->old_sample);
  color_sample_setup_dnd (colorsel, priv->cur_sample);
570 571 572 573 574 575 576 577 578

  gtk_tooltips_set_tip (priv->tooltips,
                        priv->old_sample,
                        _("The previously-selected color, for comparison to the color you're selecting now. You can drag this color to a palette entry, or select this color as current by dragging it to the other color swatch alongside."), NULL);


  gtk_tooltips_set_tip (priv->tooltips,
                        priv->cur_sample,
                        _("The color you've chosen. You can drag this color to a palette entry to save it for use in the future."), NULL);
579
  
580
  gtk_widget_show_all (priv->sample_area);
Elliot Lee's avatar
Elliot Lee committed
581 582
}

583 584 585 586 587 588

/*
 *
 * The palette area code
 *
 */
589 590
#define CUSTOM_PALETTE_ENTRY_WIDTH   20
#define CUSTOM_PALETTE_ENTRY_HEIGHT  20
591

Elliot Lee's avatar
Elliot Lee committed
592
static void
593
palette_get_color (GtkWidget *drawing_area, gdouble *color)
Elliot Lee's avatar
Elliot Lee committed
594
{
595
  gdouble *color_val;
596
  
597
  g_return_if_fail (color != NULL);
598
  
Manish Singh's avatar
Manish Singh committed
599
  color_val = g_object_get_data (G_OBJECT (drawing_area), "color_val");
600
  if (color_val == NULL)
Elliot Lee's avatar
Elliot Lee committed
601
    {
602 603 604 605 606 607
      /* Default to white for no good reason */
      color[0] = 1.0;
      color[1] = 1.0;
      color[2] = 1.0;
      color[3] = 1.0;
      return;
Elliot Lee's avatar
Elliot Lee committed
608
    }
609
  
610 611 612 613
  color[0] = color_val[0];
  color[1] = color_val[1];
  color[2] = color_val[2];
  color[3] = 1.0;
Elliot Lee's avatar
Elliot Lee committed
614 615
}

616
#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
Elliot Lee's avatar
Elliot Lee committed
617
static void
618 619 620
palette_paint (GtkWidget    *drawing_area,
	       GdkRectangle *area,
	       gpointer      data)
Elliot Lee's avatar
Elliot Lee committed
621
{
622 623
  if (drawing_area->window == NULL)
    return;
624 625 626 627 628

  gdk_draw_rectangle (drawing_area->window,
                      drawing_area->style->bg_gc[GTK_STATE_NORMAL],
                      TRUE,
                      area->x, area->y, area->width, area->height);
629
  
630
  if (GTK_WIDGET_HAS_FOCUS (drawing_area))
631
    {
632 633
      gint focus_width;
      GdkGC *gc = get_focus_gc (drawing_area, &focus_width);
634
      gdk_draw_rectangle (drawing_area->window,
635 636 637 638
			  gc, FALSE, focus_width / 2, focus_width / 2,
			  drawing_area->allocation.width - focus_width,
			  drawing_area->allocation.height - focus_width);
      g_object_unref (gc);
Elliot Lee's avatar
Elliot Lee committed
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
static GdkGC *
get_focus_gc (GtkWidget *drawing_area,
	      gint      *focus_width)
{
  GdkGC *gc = gdk_gc_new (drawing_area->window);
  gdouble color[4];
  gint8 *dash_list;
  
  gtk_widget_style_get (drawing_area,
			"focus-line-width", focus_width,
			"focus-line-pattern", (gchar *)&dash_list,
			NULL);
      
  palette_get_color (drawing_area, color);
      
  if (INTENSITY (color[0], color[1], color[2]) > 0.5)
    gdk_gc_copy (gc, drawing_area->style->black_gc);
  else
    gdk_gc_copy (gc, drawing_area->style->white_gc);

  gdk_gc_set_line_attributes (gc, *focus_width,
			      dash_list[0] ? GDK_LINE_ON_OFF_DASH : GDK_LINE_SOLID,
			      GDK_CAP_BUTT, GDK_JOIN_MITER);

  if (dash_list[0])
667
    gdk_gc_set_dashes (gc, 0, dash_list, strlen ((char *)dash_list));
668

669 670
  g_free (dash_list);
  
671 672
  return gc;
}
Elliot Lee's avatar
Elliot Lee committed
673 674

static void
675 676 677
palette_drag_begin (GtkWidget      *widget,
		    GdkDragContext *context,
		    gpointer        data)
678 679
{
  gdouble colors[4];
680
  
681
  palette_get_color (widget, colors);
682
  set_color_icon (context, colors);
683 684
}

685
static void
686 687 688 689 690 691 692 693 694
palette_drag_handle (GtkWidget        *widget,
		     GdkDragContext   *context,
		     GtkSelectionData *selection_data,
		     guint             info,
		     guint             time,
		     gpointer          data)
{
  guint16 vals[4];
  gdouble colsrc[4];
695
  
696
  palette_get_color (widget, colsrc);
697
  
698 699 700 701
  vals[0] = colsrc[COLORSEL_RED] * 0xffff;
  vals[1] = colsrc[COLORSEL_GREEN] * 0xffff;
  vals[2] = colsrc[COLORSEL_BLUE] * 0xffff;
  vals[3] = 0xffff;
702
  
703 704 705 706 707
  gtk_selection_data_set (selection_data,
			  gdk_atom_intern ("application/x-color", FALSE),
			  16, (guchar *)vals, 8);
}

708 709 710 711 712
static void
palette_drag_end (GtkWidget      *widget,
		  GdkDragContext *context,
		  gpointer        data)
{
Manish Singh's avatar
Manish Singh committed
713
  g_object_set_data (G_OBJECT (widget), "gtk-color-selection-drag-window", NULL);
714 715
}

716 717 718 719 720 721 722 723 724
static GdkColor *
get_current_colors (GtkColorSelection *colorsel)
{
  GtkSettings *settings;
  GdkColor *colors = NULL;
  gint n_colors = 0;
  gchar *palette;

  settings = gtk_widget_get_settings (GTK_WIDGET (colorsel));
725
  g_object_get (settings,
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
		"gtk-color-palette", &palette,
		NULL);
  
  if (!gtk_color_selection_palette_from_string (palette, &colors, &n_colors))
    {
      gtk_color_selection_palette_from_string (default_colors, &colors, &n_colors);
    }
  else
    {
      /* If there are less colors provided than the number of slots in the
       * color selection, we fill in the rest from the defaults.
       */
      if (n_colors < (GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT))
	{
	  GdkColor *tmp_colors = colors;
	  gint tmp_n_colors = n_colors;
	  
	  gtk_color_selection_palette_from_string (default_colors, &colors, &n_colors);
	  memcpy (colors, tmp_colors, sizeof (GdkColor) * tmp_n_colors);

	  g_free (tmp_colors);
	}
    }

  g_assert (n_colors >= GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT);
  g_free (palette);
  
  return colors;
}

756 757 758 759 760 761 762 763 764
/* Changes the model color */
static void
palette_change_color (GtkWidget         *drawing_area,
                      GtkColorSelection *colorsel,
                      gdouble           *color)
{
  gint x, y;
  ColorSelectionPrivate *priv;
  GdkColor gdk_color;
765 766
  GdkColor *current_colors;
  GdkScreen *screen;
767 768 769 770 771 772

  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
  g_return_if_fail (GTK_IS_DRAWING_AREA (drawing_area));
  
  priv = colorsel->private_data;
  
773 774 775
  gdk_color.red = UNSCALE (color[0]);
  gdk_color.green = UNSCALE (color[1]);
  gdk_color.blue = UNSCALE (color[2]);
776 777

  x = 0;
778
  y = 0;			/* Quiet GCC */
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
  while (x < GTK_CUSTOM_PALETTE_WIDTH)
    {
      y = 0;
      while (y < GTK_CUSTOM_PALETTE_HEIGHT)
        {
          if (priv->custom_palette[x][y] == drawing_area)
            goto out;
          
          ++y;
        }

      ++x;
    }

 out:
  
  g_assert (x < GTK_CUSTOM_PALETTE_WIDTH || y < GTK_CUSTOM_PALETTE_HEIGHT);

797
  current_colors = get_current_colors (colorsel);
798 799
  current_colors[y * GTK_CUSTOM_PALETTE_WIDTH + x] = gdk_color;

800 801 802 803 804 805
  screen = gtk_widget_get_screen (GTK_WIDGET (colorsel));
  if (change_palette_hook != default_change_palette_func)
    (* change_palette_hook) (screen, current_colors, 
			     GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT);
  else if (noscreen_change_palette_hook != default_noscreen_change_palette_func)
    {
Owen Taylor's avatar
Owen Taylor committed
806
      if (screen != gdk_screen_get_default ())
807 808 809 810 811 812 813 814 815
	g_warning ("gtk_color_selection_set_change_palette_hook used by widget is not on the default screen.");
      (* noscreen_change_palette_hook) (current_colors, 
					GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT);
    }
  else
    (* change_palette_hook) (screen, current_colors, 
			     GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT);

  g_free (current_colors);
816 817 818
}

/* Changes the view color */
819
static void
820 821 822
palette_set_color (GtkWidget         *drawing_area,
		   GtkColorSelection *colorsel,
		   gdouble           *color)
823 824
{
  gdouble *new_color = g_new (double, 4);
825
  GdkColor gdk_color;
826
  
827 828 829
  gdk_color.red = UNSCALE (color[0]);
  gdk_color.green = UNSCALE (color[1]);
  gdk_color.blue = UNSCALE (color[2]);
830 831

  gtk_widget_modify_bg (drawing_area, GTK_STATE_NORMAL, &gdk_color);
832
  
Manish Singh's avatar
Manish Singh committed
833
  if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (drawing_area), "color_set")) == 0)
834 835 836 837 838 839 840 841
    {
      static const GtkTargetEntry targets[] = {
	{ "application/x-color", 0 }
      };
      gtk_drag_source_set (drawing_area,
			   GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
			   targets, 1,
			   GDK_ACTION_COPY | GDK_ACTION_MOVE);
842
      
Manish Singh's avatar
Manish Singh committed
843 844 845 846 847 848 849 850
      g_signal_connect (drawing_area,
			"drag_begin",
			G_CALLBACK (palette_drag_begin),
			colorsel);
      g_signal_connect (drawing_area,
			"drag_data_get",
			G_CALLBACK (palette_drag_handle),
			colorsel);
851
      
Manish Singh's avatar
Manish Singh committed
852 853
      g_object_set_data (G_OBJECT (drawing_area), "color_set",
			 GINT_TO_POINTER (1));
854
    }
855

856 857 858 859
  new_color[0] = color[0];
  new_color[1] = color[1];
  new_color[2] = color[2];
  new_color[3] = 1.0;
860
  
861
  g_object_set_data_full (G_OBJECT (drawing_area), "color_val", new_color, (GDestroyNotify)g_free);
862 863
}

864
static gboolean
865 866 867
palette_expose (GtkWidget      *drawing_area,
		GdkEventExpose *event,
		gpointer        data)
868
{
869
  if (drawing_area->window == NULL)
870
    return FALSE;
871 872
  
  palette_paint (drawing_area, &(event->area), data);
873 874
  
  return FALSE;
875 876 877 878 879 880 881 882 883 884 885 886
}

static void
popup_position_func (GtkMenu   *menu,
                     gint      *x,
                     gint      *y,
                     gboolean  *push_in,
                     gpointer	user_data)
{
  GtkWidget *widget;
  GtkRequisition req;      
  gint root_x, root_y;
887
  GdkScreen *screen;
888 889 890 891 892 893 894 895 896 897 898 899 900 901
  
  widget = GTK_WIDGET (user_data);
  
  g_return_if_fail (GTK_WIDGET_REALIZED (widget));

  gdk_window_get_origin (widget->window, &root_x, &root_y);
  
  gtk_widget_size_request (GTK_WIDGET (menu), &req);

  /* Put corner of menu centered on color cell */
  *x = root_x + widget->allocation.width / 2;
  *y = root_y + widget->allocation.height / 2;

  /* Ensure sanity */
902 903 904
  screen = gtk_widget_get_screen (widget);
  *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
  *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
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
}

static void
save_color_selected (GtkWidget *menuitem,
                     gpointer   data)
{
  GtkColorSelection *colorsel;
  GtkWidget *drawing_area;
  ColorSelectionPrivate *priv;

  drawing_area = GTK_WIDGET (data);
  
  colorsel = GTK_COLOR_SELECTION (g_object_get_data (G_OBJECT (drawing_area),
                                                     "gtk-color-sel"));

  priv = colorsel->private_data;
  
  palette_change_color (drawing_area, colorsel, priv->color);  
}

static void
do_popup (GtkColorSelection *colorsel,
          GtkWidget         *drawing_area,
          guint32            timestamp)
{
  GtkWidget *menu;
  GtkWidget *mi;
  
  g_object_set_data (G_OBJECT (drawing_area),
                     "gtk-color-sel",
                     colorsel);
  
  menu = gtk_menu_new ();

939
  mi = gtk_menu_item_new_with_mnemonic (_("_Save color here"));
940

Manish Singh's avatar
Manish Singh committed
941 942 943
  g_signal_connect (mi, "activate",
                    G_CALLBACK (save_color_selected),
                    drawing_area);
944 945 946 947 948 949 950 951 952 953 954
  
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);

  gtk_widget_show_all (mi);

  gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
                  popup_position_func, drawing_area,
                  3, timestamp);
}


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
static gboolean
palette_enter (GtkWidget        *drawing_area,
	       GdkEventCrossing *event,
	       gpointer        data)
{
  g_object_set_data (G_OBJECT (drawing_area),
		     "gtk-colorsel-have-pointer",
		     GUINT_TO_POINTER (TRUE));

  return FALSE;
}

static gboolean
palette_leave (GtkWidget        *drawing_area,
	       GdkEventCrossing *event,
	       gpointer        data)
{
  g_object_set_data (G_OBJECT (drawing_area),
		     "gtk-colorsel-have-pointer",
		     NULL);

  return FALSE;
}

static gboolean
980 981 982 983 984 985 986 987
palette_press (GtkWidget      *drawing_area,
	       GdkEventButton *event,
	       gpointer        data)
{
  GtkColorSelection *colorsel = GTK_COLOR_SELECTION (data);

  gtk_widget_grab_focus (drawing_area);
  
988
  if (event->button == 3 &&
989
      event->type == GDK_BUTTON_PRESS)
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
    {
      do_popup (colorsel, drawing_area, event->time);
      return TRUE;
    }

  return FALSE;
}

static gboolean
palette_release (GtkWidget      *drawing_area,
		 GdkEventButton *event,
		 gpointer        data)
{
  GtkColorSelection *colorsel = GTK_COLOR_SELECTION (data);

  gtk_widget_grab_focus (drawing_area);

  if (event->button == 1 &&
      g_object_get_data (G_OBJECT (drawing_area),
			 "gtk-colorsel-have-pointer") != NULL)
    {
Manish Singh's avatar
Manish Singh committed
1011
      if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (drawing_area), "color_set")) != 0)
1012 1013 1014
        {
          gdouble color[4];
          palette_get_color (drawing_area, color);
1015
          set_color_internal (colorsel, color);
1016 1017
        }
    }
1018 1019

  return FALSE;
1020 1021
}

1022
static void
1023 1024 1025 1026 1027 1028 1029 1030
palette_drop_handle (GtkWidget        *widget,
		     GdkDragContext   *context,
		     gint              x,
		     gint              y,
		     GtkSelectionData *selection_data,
		     guint             info,
		     guint             time,
		     gpointer          data)
Elliot Lee's avatar
Elliot Lee committed
1031
{
1032
  GtkColorSelection *colorsel = GTK_COLOR_SELECTION (data);
1033
  guint16 *vals;
1034
  gdouble color[4];
1035
  
1036 1037
  if (selection_data->length < 0)
    return;
1038
  
1039 1040 1041 1042
  /* We accept drops with the wrong format, since the KDE color
   * chooser incorrectly drops application/x-color with format 8.
   */
  if (selection_data->length != 8)
1043 1044 1045 1046
    {
      g_warning ("Received invalid color data\n");
      return;
    }
1047
  
1048
  vals = (guint16 *)selection_data->data;
1049
  
1050 1051 1052 1053
  color[0] = (gdouble)vals[0] / 0xffff;
  color[1] = (gdouble)vals[1] / 0xffff;
  color[2] = (gdouble)vals[2] / 0xffff;
  color[3] = (gdouble)vals[3] / 0xffff;
1054 1055
  palette_change_color (widget, colorsel, color);
  set_color_internal (colorsel, color);
1056 1057
}

1058 1059 1060 1061
static gint
palette_activate (GtkWidget   *widget,
		  GdkEventKey *event,
		  gpointer     data)
1062
{
1063
  /* should have a drawing area subclass with an activate signal */
1064 1065 1066 1067
  if ((event->keyval == GDK_space) ||
      (event->keyval == GDK_Return) ||
      (event->keyval == GDK_KP_Enter) ||
      (event->keyval == GDK_KP_Space))
1068
    {
Manish Singh's avatar
Manish Singh committed
1069
      if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "color_set")) != 0)
1070 1071 1072
        {
          gdouble color[4];
          palette_get_color (widget, color);
1073
          set_color_internal (GTK_COLOR_SELECTION (data), color);
1074
        }
1075
      return TRUE;
1076
    }
1077
  
1078
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
1079 1080
}

1081
static gboolean
1082 1083 1084 1085 1086 1087
palette_popup (GtkWidget *widget,
               gpointer   data)
{
  GtkColorSelection *colorsel = GTK_COLOR_SELECTION (data);

  do_popup (colorsel, widget, GDK_CURRENT_TIME);
1088
  return TRUE;
1089 1090 1091
}
               

1092
static GtkWidget*
1093
palette_new (GtkColorSelection *colorsel)
Elliot Lee's avatar
Elliot Lee committed
1094
{
1095
  GtkWidget *retval;
1096
  ColorSelectionPrivate *priv;
1097 1098 1099 1100
  
  static const GtkTargetEntry targets[] = {
    { "application/x-color", 0 }
  };
1101 1102

  priv = colorsel->private_data;
1103
  
1104
  retval = gtk_drawing_area_new ();
1105 1106 1107

  GTK_WIDGET_SET_FLAGS (retval, GTK_CAN_FOCUS);
  
Manish Singh's avatar
Manish Singh committed
1108
  g_object_set_data (G_OBJECT (retval), "color_set", GINT_TO_POINTER (0)); 
1109 1110 1111 1112 1113
  gtk_widget_set_events (retval, GDK_BUTTON_PRESS_MASK
                         | GDK_BUTTON_RELEASE_MASK
                         | GDK_EXPOSURE_MASK
                         | GDK_ENTER_NOTIFY_MASK
                         | GDK_LEAVE_NOTIFY_MASK);
1114
  
Manish Singh's avatar
Manish Singh committed
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
  g_signal_connect (retval, "expose_event",
		    G_CALLBACK (palette_expose), colorsel);
  g_signal_connect (retval, "button_press_event",
		    G_CALLBACK (palette_press), colorsel);
  g_signal_connect (retval, "button_release_event",
		    G_CALLBACK (palette_release), colorsel);
  g_signal_connect (retval, "enter_notify_event",
		    G_CALLBACK (palette_enter), colorsel);
  g_signal_connect (retval, "leave_notify_event",
		    G_CALLBACK (palette_leave), colorsel);
  g_signal_connect (retval, "key_press_event",
		    G_CALLBACK (palette_activate), colorsel);
  g_signal_connect (retval, "popup_menu",
		    G_CALLBACK (palette_popup), colorsel);
1129
  
1130 1131 1132 1133 1134 1135
  gtk_drag_dest_set (retval,
		     GTK_DEST_DEFAULT_HIGHLIGHT |
		     GTK_DEST_DEFAULT_MOTION |
		     GTK_DEST_DEFAULT_DROP,
		     targets, 1,
		     GDK_ACTION_COPY);
1136
  
Manish Singh's avatar
Manish Singh committed
1137 1138 1139 1140
  g_signal_connect (retval, "drag_end",
                    G_CALLBACK (palette_drag_end), NULL);
  g_signal_connect (retval, "drag_data_received",
                    G_CALLBACK (palette_drop_handle), colorsel);
1141 1142 1143

  gtk_tooltips_set_tip (priv->tooltips,
                        retval,
Havoc Pennington's avatar
Havoc Pennington committed
1144
                        _("Click this palette entry to make it the current color. To change this entry, drag a color swatch here or right-click it and select \"Save color here.\""),
1145
                        NULL);
1146 1147
  return retval;
}
1148

Elliot Lee's avatar
Elliot Lee committed
1149

1150 1151 1152 1153 1154
/*
 *
 * The actual GtkColorSelection widget
 *
 */
Elliot Lee's avatar
Elliot Lee committed
1155

1156 1157
static GdkCursor *
make_picker_cursor (GdkScreen *screen)
Elliot Lee's avatar
Elliot Lee committed
1158
{
1159
  GdkCursor *cursor;
Manish Singh's avatar
Manish Singh committed
1160 1161 1162
  GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
  GdkColor fg = { 0, 0x0000, 0x0000, 0x0000 };
  GdkWindow *window = gdk_screen_get_root_window (screen);
1163
  
1164
  GdkPixmap *pixmap =
Manish Singh's avatar
Manish Singh committed
1165 1166
    gdk_bitmap_create_from_data (window, (gchar *) dropper_bits,
				 DROPPER_WIDTH, DROPPER_HEIGHT);
1167

1168
  GdkPixmap *mask =
Manish Singh's avatar
Manish Singh committed
1169 1170
    gdk_bitmap_create_from_data (window, (gchar *) dropper_mask,
				 DROPPER_WIDTH, DROPPER_HEIGHT);
1171

Manish Singh's avatar
Manish Singh committed
1172 1173
  cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg,
				       DROPPER_X_HOT, DROPPER_Y_HOT);
1174
  
Manish Singh's avatar
Manish Singh committed
1175 1176
  g_object_unref (pixmap);
  g_object_unref (mask);
1177 1178

  return cursor;
Elliot Lee's avatar
Elliot Lee committed
1179 1180 1181
}

static void
1182
grab_color_at_mouse (GdkScreen *screen,
1183 1184 1185
		     gint       x_root,
		     gint       y_root,
		     gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
1186
{
1187 1188 1189 1190
  GdkImage *image;
  guint32 pixel;
  GtkColorSelection *colorsel = data;
  ColorSelectionPrivate *priv;
1191
  GdkColor color;
1192 1193
  GdkColormap *colormap = gdk_screen_get_system_colormap (screen);
  GdkWindow *root_window = gdk_screen_get_root_window (screen);
1194
  
1195
  priv = colorsel->private_data;
1196
  
Manish Singh's avatar
Manish Singh committed
1197
  image = gdk_drawable_get_image (root_window, x_root, y_root, 1, 1);
1198
  pixel = gdk_image_get_pixel (image, 0, 0);
Manish Singh's avatar
Manish Singh committed
1199
  g_object_unref (image);
1200 1201 1202

  gdk_colormap_query_color (colormap, pixel, &color);
  
1203 1204 1205
  priv->color[COLORSEL_RED] = SCALE (color.red);
  priv->color[COLORSEL_GREEN] = SCALE (color.green);
  priv->color[COLORSEL_BLUE] = SCALE (color.blue);
1206
  
1207 1208 1209 1210 1211 1212
  gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
		  priv->color[COLORSEL_GREEN],
		  priv->color[COLORSEL_BLUE],
		  &priv->color[COLORSEL_HUE],
		  &priv->color[COLORSEL_SATURATION],
		  &priv->color[COLORSEL_VALUE]);
1213

1214 1215
  update_color (colorsel);
}
Elliot Lee's avatar
Elliot Lee committed
1216

1217
static void
1218 1219 1220 1221
shutdown_eyedropper (GtkWidget *widget)
{
  GtkColorSelection *colorsel;
  ColorSelectionPrivate *priv;
1222 1223
  GdkDisplay *display = gtk_widget_get_display (widget);
  guint32 time = gtk_get_current_event_time ();
1224 1225 1226 1227

  colorsel = GTK_COLOR_SELECTION (widget);
  priv = colorsel->private_data;    

1228 1229
  gdk_display_keyboard_ungrab (display, time);
  gdk_display_pointer_ungrab (display, time);
1230 1231 1232 1233 1234
  gtk_grab_remove (priv->dropper_grab_widget);
}

static void
mouse_motion (GtkWidget      *invisible,
1235 1236
	      GdkEventMotion *event,
	      gpointer        data)
1237
{
1238 1239
  grab_color_at_mouse (gdk_event_get_screen ((GdkEvent *)event),
		       event->x_root, event->y_root, data); 
Elliot Lee's avatar
Elliot Lee committed
1240 1241
}

1242
static gboolean
1243
mouse_release (GtkWidget      *invisible,
1244 1245
	       GdkEventButton *event,
	       gpointer        data)
Elliot Lee's avatar
Elliot Lee committed
1246
{
1247 1248
  GtkColorSelection *colorsel = data;
  ColorSelectionPrivate *priv;
1249 1250
  priv = colorsel->private_data;  

1251 1252 1253
  if (event->button != 1)
    return FALSE;

1254 1255
  grab_color_at_mouse (gdk_event_get_screen ((GdkEvent *)event),
		       event->x_root, event->y_root, data);
1256 1257 1258

  shutdown_eyedropper (GTK_WIDGET (data));
  
Manish Singh's avatar
Manish Singh committed
1259 1260 1261 1262 1263 1264
  g_signal_handlers_disconnect_by_func (invisible,
					mouse_motion,
					data);
  g_signal_handlers_disconnect_by_func (invisible,
					mouse_release,
					data);
1265

1266
  return TRUE;
1267
}
Elliot Lee's avatar
Elliot Lee committed
1268

1269
/* Helper Functions */
1270 1271 1272 1273 1274

static gboolean mouse_press (GtkWidget      *invisible,
                             GdkEventButton *event,
                             gpointer        data);

1275 1276
#define BIG_STEP 20

1277 1278 1279 1280 1281
static gboolean
key_press (GtkWidget   *invisible,
           GdkEventKey *event,
           gpointer     data)
{  
1282 1283 1284 1285 1286 1287 1288 1289 1290
  GdkDisplay *display = gtk_widget_get_display (invisible);
  guint state = event->state & gtk_accelerator_get_default_mod_mask ();
  gint x, y;
  gint dx, dy;

  dx = 0;
  dy = 0;

  switch (event->keyval) 
1291
    {
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
    case GDK_space:
    case GDK_Return:
    case GDK_KP_Enter:
    case GDK_KP_Space:
      gdk_display_get_pointer (display, 
			       NULL, &x, &y, NULL);
      grab_color_at_mouse (gdk_event_get_screen ((GdkEvent *)event),
			   x, y, data);
      /* fall through */

    case GDK_Escape:
1303
      shutdown_eyedropper (data);
1304
      
Manish Singh's avatar
Manish Singh committed
1305 1306 1307 1308 1309 1310
      g_signal_handlers_disconnect_by_func (invisible,
					    mouse_press,
					    data);
      g_signal_handlers_disconnect_by_func (invisible,
					    key_press,
					    data);
1311 1312
      
      return TRUE;
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337

#ifdef GDK_WINDOWING_X11
    case GDK_Up:
    case GDK_KP_Up:
      dy = state == GDK_MOD1_MASK ? -BIG_STEP : -1;
      break;

    case GDK_Down:
    case GDK_KP_Down:
      dy = state == GDK_MOD1_MASK ? BIG_STEP : 1;
      break;

    case GDK_Left:
    case GDK_KP_Left:
      dx = state == GDK_MOD1_MASK ? -BIG_STEP : -1;
      break;

    case GDK_Right:
    case GDK_KP_Right:
      dx = state == GDK_MOD1_MASK ? BIG_STEP : 1;
      break;
#endif

    default:
      return FALSE;
1338 1339
    }

1340 1341 1342 1343 1344 1345 1346
#ifdef GDK_WINDOWING_X11
  XWarpPointer (gdk_x11_display_get_xdisplay (display),
		None, None, 0, 0, 0, 0, dx, dy);
#endif
  
  return TRUE;

1347 1348
}

1349
static gboolean
1350
mouse_press (GtkWidget      *invisible,
1351 1352
	     GdkEventButton *event,
	     gpointer        data)
1353 1354 1355
{
  GtkColorSelection *colorsel = data;
  ColorSelectionPrivate *priv;
1356
  priv = colorsel->private_data;
1357 1358 1359 1360
  
  if (event->type == GDK_BUTTON_PRESS &&
      event->button == 1)
    {
Manish Singh's avatar
Manish Singh committed
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
      g_signal_connect (invisible, "motion_notify_event",
                        G_CALLBACK (mouse_motion),
                        data);
      g_signal_connect (invisible, "button_release_event",
                        G_CALLBACK (mouse_release),
                        data);
      g_signal_handlers_disconnect_by_func (invisible,
					    mouse_press,
					    data);
      g_signal_handlers_disconnect_by_func (invisible,
					    key_press,
					    data);
1373 1374
      return TRUE;
    }
1375

1376
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
1377 1378
}

1379
/* when the button is clicked */
Elliot Lee's avatar
Elliot Lee committed
1380
static void
1381
get_screen_color (GtkWidget *button)
Elliot Lee's avatar
Elliot Lee committed
1382
{
Manish Singh's avatar
Manish Singh committed
1383
  GtkColorSelection *colorsel = g_object_get_data (G_OBJECT (button), "COLORSEL");
1384 1385 1386 1387
  ColorSelectionPrivate *priv = colorsel->private_data;
  GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (button));
  GdkCursor *picker_cursor;
  GdkGrabStatus grab_status;
1388
  
1389
  if (priv->dropper_grab_widget == NULL)
1390
    {
1391
      priv->dropper_grab_widget = gtk_invisible_new_for_screen (screen);
1392

1393 1394
      gtk_widget_add_events (priv->dropper_grab_widget,
                             GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK);
1395
      
1396
      gtk_widget_show (priv->dropper_grab_widget);
1397
    }