gtkcolorsel.c 81.1 KB
Newer Older
1
/* GTK - The GIMP Toolkit
Owen Taylor's avatar
Owen Taylor committed
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

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

63 64
#include <string.h>

65 66 67 68
/* Number of elements in the custom palatte */
#define GTK_CUSTOM_PALETTE_WIDTH 10
#define GTK_CUSTOM_PALETTE_HEIGHT 2

69 70 71 72 73 74 75
/* 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))


76
enum {
Elliot Lee's avatar
Elliot Lee committed
77 78 79 80
  COLOR_CHANGED,
  LAST_SIGNAL
};

81 82 83 84 85
enum {
  PROP_0,
  PROP_HAS_PALETTE,
  PROP_HAS_OPACITY_CONTROL,
  PROP_CURRENT_COLOR,
86
  PROP_CURRENT_ALPHA
87 88
};

89
enum {
Owen Taylor's avatar
Owen Taylor committed
90 91 92 93 94 95 96 97
  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
98 99
};

Owen Taylor's avatar
Owen Taylor committed
100
typedef struct _ColorSelectionPrivate ColorSelectionPrivate;
101

Owen Taylor's avatar
Owen Taylor committed
102
struct _ColorSelectionPrivate
Elliot Lee's avatar
Elliot Lee committed
103
{
104 105
  guint has_opacity : 1;
  guint has_palette : 1;
106 107
  guint changing : 1;
  guint default_set : 1;
108
  guint default_alpha_set : 1;
109
  guint has_grab : 1;
110
  
Owen Taylor's avatar
Owen Taylor committed
111 112
  gdouble color[COLORSEL_NUM_CHANNELS];
  gdouble old_color[COLORSEL_NUM_CHANNELS];
113
  
Owen Taylor's avatar
Owen Taylor committed
114 115 116 117 118 119 120 121 122 123 124 125
  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;
126
  
Owen Taylor's avatar
Owen Taylor committed
127
  /* The Palette code */
128 129
  GtkWidget *custom_palette [GTK_CUSTOM_PALETTE_WIDTH][GTK_CUSTOM_PALETTE_HEIGHT];
  
Owen Taylor's avatar
Owen Taylor committed
130 131 132 133 134
  /* The color_sample stuff */
  GtkWidget *sample_area;
  GtkWidget *old_sample;
  GtkWidget *cur_sample;
  GtkWidget *colorsel;
135 136 137

  /* Window for grabbing on */
  GtkWidget *dropper_grab_widget;
138
  guint32    grab_time;
139 140

  /* Connection to settings */
141
  gulong settings_connection;
Elliot Lee's avatar
Elliot Lee committed
142 143 144
};


145
static void gtk_color_selection_destroy		(GtkObject		 *object);
146
static void gtk_color_selection_finalize        (GObject		 *object);
147
static void update_color			(GtkColorSelection	 *colorsel);
148 149 150 151 152 153 154 155
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);
Owen Taylor's avatar
Owen Taylor committed
156

157 158
static void gtk_color_selection_realize         (GtkWidget               *widget);
static void gtk_color_selection_unrealize       (GtkWidget               *widget);
159
static void gtk_color_selection_show_all        (GtkWidget               *widget);
160 161
static gboolean gtk_color_selection_grab_broken (GtkWidget               *widget,
						 GdkEventGrabBroken      *event);
162

163 164 165
static void     gtk_color_selection_set_palette_color   (GtkColorSelection *colorsel,
                                                         gint               index,
                                                         GdkColor          *color);
166 167
static void    set_focus_line_attributes                (GtkWidget         *drawing_area,
							 cairo_t           *cr,
168
							 gint              *focus_width);
169 170 171 172
static void     default_noscreen_change_palette_func    (const GdkColor    *colors,
							 gint               n_colors);
static void     default_change_palette_func             (GdkScreen	   *screen,
							 const GdkColor    *colors,
173
							 gint               n_colors);
174 175 176 177
static void     make_control_relations                  (AtkObject         *atk_obj,
                                                         GtkWidget         *widget);
static void     make_all_relations                      (AtkObject         *atk_obj,
                                                         ColorSelectionPrivate *priv);
178

Owen Taylor's avatar
Owen Taylor committed
179 180
static guint color_selection_signals[LAST_SIGNAL] = { 0 };

181
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";
182

183 184
static GtkColorSelectionChangePaletteFunc noscreen_change_palette_hook = default_noscreen_change_palette_func;
static GtkColorSelectionChangePaletteWithScreenFunc change_palette_hook = default_change_palette_func;
Owen Taylor's avatar
Owen Taylor committed
185 186 187 188 189 190 191 192

/* 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
193
static const guchar dropper_bits[] = {
Owen Taylor's avatar
Owen Taylor committed
194 195 196 197 198 199
  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
200
static const guchar dropper_mask[] = {
Owen Taylor's avatar
Owen Taylor committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  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);
217
static void color_sample_update_samples (GtkColorSelection *colorsel);
Elliot Lee's avatar
Elliot Lee committed
218

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
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);
}

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
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
265
  g_object_unref (pixbuf);
266 267
}

268
static void
Owen Taylor's avatar
Owen Taylor committed
269 270 271
color_sample_drag_begin (GtkWidget      *widget,
			 GdkDragContext *context,
			 gpointer        data)
Elliot Lee's avatar
Elliot Lee committed
272
{
Owen Taylor's avatar
Owen Taylor committed
273 274 275
  GtkColorSelection *colorsel = data;
  ColorSelectionPrivate *priv;
  gdouble *colsrc;
276
  
277
  priv = colorsel->private_data;
278
  
Owen Taylor's avatar
Owen Taylor committed
279 280 281 282
  if (widget == priv->old_sample)
    colsrc = priv->old_color;
  else
    colsrc = priv->color;
283 284

  set_color_icon (context, colsrc);
Elliot Lee's avatar
Elliot Lee committed
285 286
}

287
static void
Owen Taylor's avatar
Owen Taylor committed
288 289 290
color_sample_drag_end (GtkWidget      *widget,
		       GdkDragContext *context,
		       gpointer        data)
Elliot Lee's avatar
Elliot Lee committed
291
{
Matthias Clasen's avatar
Matthias Clasen committed
292
  g_object_set_data (G_OBJECT (widget), I_("gtk-color-selection-drag-window"), NULL);
Owen Taylor's avatar
Owen Taylor committed
293
}
Elliot Lee's avatar
Elliot Lee committed
294

Owen Taylor's avatar
Owen Taylor committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308
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];
309
  priv = colorsel->private_data;
310
  
Owen Taylor's avatar
Owen Taylor committed
311 312 313 314 315 316
  /* This is currently a guint16 array of the format:
   * R
   * G
   * B
   * opacity
   */
317
  
Owen Taylor's avatar
Owen Taylor committed
318 319
  if (selection_data->length < 0)
    return;
320
  
321 322 323 324
  /* 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)
Owen Taylor's avatar
Owen Taylor committed
325 326 327
    {
      g_warning ("Received invalid color data\n");
      return;
Elliot Lee's avatar
Elliot Lee committed
328
    }
329
  
Owen Taylor's avatar
Owen Taylor committed
330
  vals = (guint16 *)selection_data->data;
331
  
Owen Taylor's avatar
Owen Taylor committed
332
  if (widget == priv->cur_sample)
333
    {
Owen Taylor's avatar
Owen Taylor committed
334 335 336 337
      color[0] = (gdouble)vals[0] / 0xffff;
      color[1] = (gdouble)vals[1] / 0xffff;
      color[2] = (gdouble)vals[2] / 0xffff;
      color[3] = (gdouble)vals[3] / 0xffff;
338
      
339
      set_color_internal (colorsel, color);
340 341 342 343
    }
}

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

Owen Taylor's avatar
Owen Taylor committed
373 374 375
/* 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
376
{
Owen Taylor's avatar
Owen Taylor committed
377
  GtkWidget *da;
378
  gint x, y, wid, heig, goff;
Owen Taylor's avatar
Owen Taylor committed
379
  ColorSelectionPrivate *priv;
380
  cairo_t *cr;
381
  
Elliot Lee's avatar
Elliot Lee committed
382
  g_return_if_fail (colorsel != NULL);
383
  priv = colorsel->private_data;
384
  
Owen Taylor's avatar
Owen Taylor committed
385 386 387
  g_return_if_fail (priv->sample_area != NULL);
  if (!GTK_WIDGET_DRAWABLE (priv->sample_area))
    return;
388

Owen Taylor's avatar
Owen Taylor committed
389 390 391 392 393 394 395 396 397
  if (which == 0)
    {
      da = priv->old_sample;
      goff = 0;
    }
  else
    {
      da = priv->cur_sample;
      goff =  priv->old_sample->allocation.width % 32;
Elliot Lee's avatar
Elliot Lee committed
398
    }
399

400
  cr = gdk_cairo_create (da->window);
401
  
Owen Taylor's avatar
Owen Taylor committed
402 403
  wid = da->allocation.width;
  heig = da->allocation.height;
404 405 406

  /* Below needs tweaking for non-power-of-two */  
#define CHECK_SIZE 16  
407
  
408
  if (priv->has_opacity)
Owen Taylor's avatar
Owen Taylor committed
409
    {
410 411
      /* Draw checks in background */

412
      cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
413 414 415
      cairo_rectangle (cr, 0, 0, wid, heig);
      cairo_fill (cr);

416
      cairo_set_source_rgb (cr, 0.75, 0.75, 0.75);
417 418 419 420 421
      for (x = goff & -CHECK_SIZE; x < goff + wid; x += CHECK_SIZE)
	for (y = 0; y < heig; y += CHECK_SIZE)
	  if ((x / CHECK_SIZE + y / CHECK_SIZE) % 2 == 0)
	    cairo_rectangle (cr, x - goff, y, CHECK_SIZE, CHECK_SIZE);
      cairo_fill (cr);
Owen Taylor's avatar
Owen Taylor committed
422
    }
423 424

  if (which == 0)
Elliot Lee's avatar
Elliot Lee committed
425
    {
426 427 428 429 430 431
      cairo_set_source_rgba (cr,
			     priv->old_color[COLORSEL_RED], 
			     priv->old_color[COLORSEL_GREEN], 
			     priv->old_color[COLORSEL_BLUE],
			     priv->has_opacity ?
			        priv->old_color[COLORSEL_OPACITY] : 1.0);
Elliot Lee's avatar
Elliot Lee committed
432
    }
433
  else
Elliot Lee's avatar
Elliot Lee committed
434
    {
435 436 437 438 439 440
      cairo_set_source_rgba (cr,
			     priv->color[COLORSEL_RED], 
			     priv->color[COLORSEL_GREEN], 
			     priv->color[COLORSEL_BLUE],
			     priv->has_opacity ?
			       priv->color[COLORSEL_OPACITY] : 1.0);
Elliot Lee's avatar
Elliot Lee committed
441
    }
442 443 444 445 446

  cairo_rectangle (cr, 0, 0, wid, heig);
  cairo_fill (cr);

  cairo_destroy (cr);
Elliot Lee's avatar
Elliot Lee committed
447 448 449
}


Owen Taylor's avatar
Owen Taylor committed
450
static void
451
color_sample_update_samples (GtkColorSelection *colorsel)
Owen Taylor's avatar
Owen Taylor committed
452
{
453 454 455
  ColorSelectionPrivate *priv = colorsel->private_data;
  gtk_widget_queue_draw (priv->old_sample);
  gtk_widget_queue_draw (priv->cur_sample);
Owen Taylor's avatar
Owen Taylor committed
456
}
Elliot Lee's avatar
Elliot Lee committed
457

458
static gboolean
459 460 461
color_old_sample_expose (GtkWidget         *da,
			 GdkEventExpose    *event,
			 GtkColorSelection *colorsel)
Owen Taylor's avatar
Owen Taylor committed
462 463
{
  color_sample_draw_sample (colorsel, 0);
464
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
465 466
}

Owen Taylor's avatar
Owen Taylor committed
467

468
static gboolean
469 470 471
color_cur_sample_expose (GtkWidget         *da,
			 GdkEventExpose    *event,
			 GtkColorSelection *colorsel)
Elliot Lee's avatar
Elliot Lee committed
472
{
Owen Taylor's avatar
Owen Taylor committed
473
  color_sample_draw_sample (colorsel, 1);
474
  return FALSE;
Owen Taylor's avatar
Owen Taylor committed
475
}
476

Owen Taylor's avatar
Owen Taylor committed
477 478 479
static void
color_sample_setup_dnd (GtkColorSelection *colorsel, GtkWidget *sample)
{
480
  static const GtkTargetEntry targets[] = {
481 482
    { "application/x-color", 0 }
  };
Owen Taylor's avatar
Owen Taylor committed
483
  ColorSelectionPrivate *priv;
484
  priv = colorsel->private_data;
485
  
Owen Taylor's avatar
Owen Taylor committed
486
  gtk_drag_source_set (sample,
487 488 489
		       GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
		       targets, 1,
		       GDK_ACTION_COPY | GDK_ACTION_MOVE);
490
  
Manish Singh's avatar
Manish Singh committed
491 492 493 494
  g_signal_connect (sample,
		    "drag_begin",
		    G_CALLBACK (color_sample_drag_begin),
		    colorsel);
Owen Taylor's avatar
Owen Taylor committed
495 496
  if (sample == priv->cur_sample)
    {
497
      
Owen Taylor's avatar
Owen Taylor committed
498 499 500 501 502 503
      gtk_drag_dest_set (sample,
			 GTK_DEST_DEFAULT_HIGHLIGHT |
			 GTK_DEST_DEFAULT_MOTION |
			 GTK_DEST_DEFAULT_DROP,
			 targets, 1,
			 GDK_ACTION_COPY);
504
      
Manish Singh's avatar
Manish Singh committed
505 506 507 508
      g_signal_connect (sample,
			"drag_end",
			G_CALLBACK (color_sample_drag_end),
			colorsel);
Owen Taylor's avatar
Owen Taylor committed
509
    }
510
  
Manish Singh's avatar
Manish Singh committed
511 512 513 514 515 516 517 518
  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);
519
  
Elliot Lee's avatar
Elliot Lee committed
520 521
}

Owen Taylor's avatar
Owen Taylor committed
522

523
static void
Owen Taylor's avatar
Owen Taylor committed
524
color_sample_new (GtkColorSelection *colorsel)
525
{
Owen Taylor's avatar
Owen Taylor committed
526
  ColorSelectionPrivate *priv;
527
  
528
  priv = colorsel->private_data;
529
  
Owen Taylor's avatar
Owen Taylor committed
530 531 532
  priv->sample_area = gtk_hbox_new (FALSE, 0);
  priv->old_sample = gtk_drawing_area_new ();
  priv->cur_sample = gtk_drawing_area_new ();
533

Owen Taylor's avatar
Owen Taylor committed
534 535 536 537
  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);
538
  
Manish Singh's avatar
Manish Singh committed
539 540 541 542 543 544
  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);
545
  
Owen Taylor's avatar
Owen Taylor committed
546 547
  color_sample_setup_dnd (colorsel, priv->old_sample);
  color_sample_setup_dnd (colorsel, priv->cur_sample);
548

549 550
  gtk_widget_set_tooltip_text (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."));
551 552


553 554
  gtk_widget_set_tooltip_text (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."));
555
  
Owen Taylor's avatar
Owen Taylor committed
556
  gtk_widget_show_all (priv->sample_area);
Elliot Lee's avatar
Elliot Lee committed
557 558
}

Owen Taylor's avatar
Owen Taylor committed
559 560 561 562 563 564

/*
 *
 * The palette area code
 *
 */
565 566
#define CUSTOM_PALETTE_ENTRY_WIDTH   20
#define CUSTOM_PALETTE_ENTRY_HEIGHT  20
Owen Taylor's avatar
Owen Taylor committed
567

Elliot Lee's avatar
Elliot Lee committed
568
static void
Owen Taylor's avatar
Owen Taylor committed
569
palette_get_color (GtkWidget *drawing_area, gdouble *color)
Elliot Lee's avatar
Elliot Lee committed
570
{
Owen Taylor's avatar
Owen Taylor committed
571
  gdouble *color_val;
572
  
Owen Taylor's avatar
Owen Taylor committed
573
  g_return_if_fail (color != NULL);
574
  
Manish Singh's avatar
Manish Singh committed
575
  color_val = g_object_get_data (G_OBJECT (drawing_area), "color_val");
Owen Taylor's avatar
Owen Taylor committed
576
  if (color_val == NULL)
Elliot Lee's avatar
Elliot Lee committed
577
    {
Owen Taylor's avatar
Owen Taylor committed
578 579 580 581 582 583
      /* 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
584
    }
585
  
Owen Taylor's avatar
Owen Taylor committed
586 587 588 589
  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
590 591
}

Owen Taylor's avatar
Owen Taylor committed
592
#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
Elliot Lee's avatar
Elliot Lee committed
593
static void
594 595 596
palette_paint (GtkWidget    *drawing_area,
	       GdkRectangle *area,
	       gpointer      data)
Elliot Lee's avatar
Elliot Lee committed
597
{
598 599 600
  cairo_t *cr;
  gint focus_width;
    
Owen Taylor's avatar
Owen Taylor committed
601 602
  if (drawing_area->window == NULL)
    return;
603

604
  cr = gdk_cairo_create (drawing_area->window);
605 606

  gdk_cairo_set_source_color (cr, &drawing_area->style->bg[GTK_STATE_NORMAL]);
607
  gdk_cairo_rectangle (cr, area);
608
  cairo_fill (cr);
609
  
610
  if (GTK_WIDGET_HAS_FOCUS (drawing_area))
Owen Taylor's avatar
Owen Taylor committed
611
    {
612 613 614 615 616 617 618
      set_focus_line_attributes (drawing_area, cr, &focus_width);

      cairo_rectangle (cr,
		       focus_width / 2., focus_width / 2.,
		       drawing_area->allocation.width - focus_width,
		       drawing_area->allocation.height - focus_width);
      cairo_stroke (cr);
Elliot Lee's avatar
Elliot Lee committed
619
    }
620 621

  cairo_destroy (cr);
Elliot Lee's avatar
Elliot Lee committed
622 623
}

624 625 626 627
static void
set_focus_line_attributes (GtkWidget *drawing_area,
			   cairo_t   *cr,
			   gint      *focus_width)
628 629 630 631 632 633 634 635 636 637
{
  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);
638

639
  if (INTENSITY (color[0], color[1], color[2]) > 0.5)
640
    cairo_set_source_rgb (cr, 0., 0., 0.);
641
  else
642
    cairo_set_source_rgb (cr, 1., 1., 1.);
643

644
  cairo_set_line_width (cr, *focus_width);
645 646

  if (dash_list[0])
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
    {
      gint n_dashes = strlen (dash_list);
      gdouble *dashes = g_new (gdouble, n_dashes);
      gdouble total_length = 0;
      gdouble dash_offset;
      gint i;

      for (i = 0; i < n_dashes; i++)
	{
	  dashes[i] = dash_list[i];
	  total_length += dash_list[i];
	}

      /* The dash offset here aligns the pattern to integer pixels
       * by starting the dash at the right side of the left border
       * Negative dash offsets in cairo don't work
       * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
       */
      dash_offset = - *focus_width / 2.;
      while (dash_offset < 0)
	dash_offset += total_length;
      
      cairo_set_dash (cr, dashes, n_dashes, dash_offset);
      g_free (dashes);
    }
672

Anders Carlsson's avatar
Anders Carlsson committed
673
  g_free (dash_list);
674
}
Elliot Lee's avatar
Elliot Lee committed
675 676

static void
Owen Taylor's avatar
Owen Taylor committed
677 678 679
palette_drag_begin (GtkWidget      *widget,
		    GdkDragContext *context,
		    gpointer        data)
680 681
{
  gdouble colors[4];
682
  
Owen Taylor's avatar
Owen Taylor committed
683
  palette_get_color (widget, colors);
684
  set_color_icon (context, colors);
685 686
}

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

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

718 719 720 721 722 723 724 725 726
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));
727
  g_object_get (settings,
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
		"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;
}

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

  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
  g_return_if_fail (GTK_IS_DRAWING_AREA (drawing_area));
  
  priv = colorsel->private_data;
  
775 776 777
  gdk_color.red = UNSCALE (color[0]);
  gdk_color.green = UNSCALE (color[1]);
  gdk_color.blue = UNSCALE (color[2]);
Matthias Clasen's avatar
Matthias Clasen committed
778
  gdk_color.pixel = 0;
779 780

  x = 0;
781
  y = 0;			/* Quiet GCC */
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
  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);

800
  current_colors = get_current_colors (colorsel);
801 802
  current_colors[y * GTK_CUSTOM_PALETTE_WIDTH + x] = gdk_color;

803 804 805 806 807 808
  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
809
      if (screen != gdk_screen_get_default ())
810 811 812 813 814 815 816 817 818
	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);
819 820 821
}

/* Changes the view color */
Owen Taylor's avatar
Owen Taylor committed
822
static void
823 824 825
palette_set_color (GtkWidget         *drawing_area,
		   GtkColorSelection *colorsel,
		   gdouble           *color)
Owen Taylor's avatar
Owen Taylor committed
826 827
{
  gdouble *new_color = g_new (double, 4);
828
  GdkColor gdk_color;
829
  
830 831 832
  gdk_color.red = UNSCALE (color[0]);
  gdk_color.green = UNSCALE (color[1]);
  gdk_color.blue = UNSCALE (color[2]);
833 834

  gtk_widget_modify_bg (drawing_area, GTK_STATE_NORMAL, &gdk_color);
835
  
Manish Singh's avatar
Manish Singh committed
836
  if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (drawing_area), "color_set")) == 0)
Owen Taylor's avatar
Owen Taylor committed
837 838 839 840 841 842 843 844
    {
      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);
845
      
Manish Singh's avatar
Manish Singh committed
846 847 848 849 850 851 852 853
      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);
854
      
Matthias Clasen's avatar
Matthias Clasen committed
855
      g_object_set_data (G_OBJECT (drawing_area), I_("color_set"),
Manish Singh's avatar
Manish Singh committed
856
			 GINT_TO_POINTER (1));
Owen Taylor's avatar
Owen Taylor committed
857
    }
858

Owen Taylor's avatar
Owen Taylor committed
859 860 861 862
  new_color[0] = color[0];
  new_color[1] = color[1];
  new_color[2] = color[2];
  new_color[3] = 1.0;
863
  
Matthias Clasen's avatar
Matthias Clasen committed
864
  g_object_set_data_full (G_OBJECT (drawing_area), I_("color_val"), new_color, (GDestroyNotify)g_free);
Owen Taylor's avatar
Owen Taylor committed
865 866
}

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

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;
890
  GdkScreen *screen;
891 892 893 894 895 896 897 898 899 900 901 902 903 904
  
  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 */
905 906 907
  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));
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
}

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),
Matthias Clasen's avatar
Matthias Clasen committed
937
                     I_("gtk-color-sel"),
938 939 940 941
                     colorsel);
  
  menu = gtk_menu_new ();

942
  mi = gtk_menu_item_new_with_mnemonic (_("_Save color here"));
943

Manish Singh's avatar
Manish Singh committed
944 945 946
  g_signal_connect (mi, "activate",
                    G_CALLBACK (save_color_selected),
                    drawing_area);
947 948 949 950 951 952 953 954 955 956 957
  
  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);
}


958 959 960 961 962 963
static gboolean
palette_enter (GtkWidget        *drawing_area,
	       GdkEventCrossing *event,
	       gpointer        data)
{
  g_object_set_data (G_OBJECT (drawing_area),
Matthias Clasen's avatar
Matthias Clasen committed
964
		     I_("gtk-colorsel-have-pointer"),
965 966 967 968 969 970 971 972 973 974 975
		     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),
Matthias Clasen's avatar
Matthias Clasen committed
976
		     I_("gtk-colorsel-have-pointer"),
977 978 979 980 981 982
		     NULL);

  return FALSE;
}

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

  gtk_widget_grab_focus (drawing_area);
  
991
  if (event->button == 3 &&
992
      event->type == GDK_BUTTON_PRESS)
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    {
      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
1014
      if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (drawing_area), "color_set")) != 0)
1015 1016 1017
        {
          gdouble color[4];
          palette_get_color (drawing_area, color);
1018
          set_color_internal (colorsel, color);
1019 1020
        }
    }
1021 1022

  return FALSE;
1023 1024
}

1025
static void
Owen Taylor's avatar
Owen Taylor committed
1026 1027 1028 1029 1030 1031 1032 1033
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
1034
{
1035
  GtkColorSelection *colorsel = GTK_COLOR_SELECTION (data);
1036
  guint16 *vals;
Owen Taylor's avatar
Owen Taylor committed
1037
  gdouble color[4];
1038
  
1039 1040
  if (selection_data->length < 0)
    return;