]> Pileus Git - ~andy/gtk/blob - gtk/gtkcolorsel.c
GailEntry: remove idle if cell editing is canceled
[~andy/gtk] / gtk / gtkcolorsel.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc.
3  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26  */
27
28 #include "config.h"
29
30 #include "gtkcolorsel.h"
31
32 #include <math.h>
33 #include <string.h>
34
35 #include "gdk/gdk.h"
36 #include "gtkhsv.h"
37 #include "gtkwindow.h"
38 #include "gtkselection.h"
39 #include "gtkdnd.h"
40 #include "gtkdrawingarea.h"
41 #include "gtkhbox.h"
42 #include "gtkhbbox.h"
43 #include "gtkrc.h"
44 #include "gtkframe.h"
45 #include "gtktable.h"
46 #include "gtklabel.h"
47 #include "gtkmarshalers.h"
48 #include "gtkimage.h"
49 #include "gtkspinbutton.h"
50 #include "gtkrange.h"
51 #include "gtkhscale.h"
52 #include "gtkentry.h"
53 #include "gtkbutton.h"
54 #include "gtkhseparator.h"
55 #include "gtkinvisible.h"
56 #include "gtkmenuitem.h"
57 #include "gtkmain.h"
58 #include "gtksettings.h"
59 #include "gtkstock.h"
60 #include "gtkaccessible.h"
61 #include "gtksizerequest.h"
62 #include "gtkprivate.h"
63 #include "gtkintl.h"
64
65
66 /**
67  * SECTION:gtkcolorsel
68  * @Short_description: A widget used to select a color
69  * @Title: GtkColorSelection
70  *
71  * The #GtkColorSelection is a widget that is used to select
72  * a color.  It consists of a color wheel and number of sliders
73  * and entry boxes for color parameters such as hue, saturation,
74  * value, red, green, blue, and opacity.  It is found on the standard
75  * color selection dialog box #GtkColorSelectionDialog.
76  */
77
78
79 /* Keep it in sync with gtksettings.c:default_color_palette */
80 #define DEFAULT_COLOR_PALETTE   "black:white:gray50:red:purple:blue:light blue:green:yellow:orange:lavender:brown:goldenrod4:dodger blue:pink:light green:gray10:gray30:gray75:gray90"
81
82 /* Number of elements in the custom palatte */
83 #define GTK_CUSTOM_PALETTE_WIDTH 10
84 #define GTK_CUSTOM_PALETTE_HEIGHT 2
85
86 #define CUSTOM_PALETTE_ENTRY_WIDTH   20
87 #define CUSTOM_PALETTE_ENTRY_HEIGHT  20
88
89 /* The cursor for the dropper */
90 #define DROPPER_WIDTH 17
91 #define DROPPER_HEIGHT 17
92 #define DROPPER_STRIDE (DROPPER_WIDTH * 4)
93 #define DROPPER_X_HOT 2
94 #define DROPPER_Y_HOT 16
95
96 #define SAMPLE_WIDTH  64
97 #define SAMPLE_HEIGHT 28
98 #define CHECK_SIZE 16
99 #define BIG_STEP 20
100
101 /* Conversion between 0->1 double and and guint16. See
102  * scale_round() below for more general conversions
103  */
104 #define SCALE(i) (i / 65535.)
105 #define UNSCALE(d) ((guint16)(d * 65535 + 0.5))
106 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
107
108 enum {
109   COLOR_CHANGED,
110   LAST_SIGNAL
111 };
112
113 enum {
114   PROP_0,
115   PROP_HAS_PALETTE,
116   PROP_HAS_OPACITY_CONTROL,
117   PROP_CURRENT_COLOR,
118   PROP_CURRENT_ALPHA,
119   PROP_CURRENT_RGBA
120 };
121
122 enum {
123   COLORSEL_RED = 0,
124   COLORSEL_GREEN = 1,
125   COLORSEL_BLUE = 2,
126   COLORSEL_OPACITY = 3,
127   COLORSEL_HUE,
128   COLORSEL_SATURATION,
129   COLORSEL_VALUE,
130   COLORSEL_NUM_CHANNELS
131 };
132
133
134 struct _GtkColorSelectionPrivate
135 {
136   guint has_opacity       : 1;
137   guint has_palette       : 1;
138   guint changing          : 1;
139   guint default_set       : 1;
140   guint default_alpha_set : 1;
141   guint has_grab          : 1;
142
143   gdouble color[COLORSEL_NUM_CHANNELS];
144   gdouble old_color[COLORSEL_NUM_CHANNELS];
145
146   GtkWidget *triangle_colorsel;
147   GtkWidget *hue_spinbutton;
148   GtkWidget *sat_spinbutton;
149   GtkWidget *val_spinbutton;
150   GtkWidget *red_spinbutton;
151   GtkWidget *green_spinbutton;
152   GtkWidget *blue_spinbutton;
153   GtkWidget *opacity_slider;
154   GtkWidget *opacity_label;
155   GtkWidget *opacity_entry;
156   GtkWidget *palette_frame;
157   GtkWidget *hex_entry;
158
159   /* The Palette code */
160   GtkWidget *custom_palette [GTK_CUSTOM_PALETTE_WIDTH][GTK_CUSTOM_PALETTE_HEIGHT];
161
162   /* The color_sample stuff */
163   GtkWidget *sample_area;
164   GtkWidget *old_sample;
165   GtkWidget *cur_sample;
166   GtkWidget *colorsel;
167
168   /* Window for grabbing on */
169   GtkWidget *dropper_grab_widget;
170   guint32    grab_time;
171   GdkDevice *keyboard_device;
172   GdkDevice *pointer_device;
173
174   /* Connection to settings */
175   gulong settings_connection;
176 };
177
178
179 static void gtk_color_selection_destroy         (GtkWidget               *widget);
180 static void gtk_color_selection_finalize        (GObject                 *object);
181 static void update_color                        (GtkColorSelection       *colorsel);
182 static void gtk_color_selection_set_property    (GObject                 *object,
183                                                  guint                    prop_id,
184                                                  const GValue            *value,
185                                                  GParamSpec              *pspec);
186 static void gtk_color_selection_get_property    (GObject                 *object,
187                                                  guint                    prop_id,
188                                                  GValue                  *value,
189                                                  GParamSpec              *pspec);
190
191 static void gtk_color_selection_realize         (GtkWidget               *widget);
192 static void gtk_color_selection_unrealize       (GtkWidget               *widget);
193 static void gtk_color_selection_show_all        (GtkWidget               *widget);
194 static gboolean gtk_color_selection_grab_broken (GtkWidget               *widget,
195                                                  GdkEventGrabBroken      *event);
196
197 static void     gtk_color_selection_set_palette_color   (GtkColorSelection *colorsel,
198                                                          gint               index,
199                                                          GdkColor          *color);
200 static void     set_focus_line_attributes               (GtkWidget         *drawing_area,
201                                                          cairo_t           *cr,
202                                                          gint              *focus_width);
203 static void     default_noscreen_change_palette_func    (const GdkColor    *colors,
204                                                          gint               n_colors);
205 static void     default_change_palette_func             (GdkScreen         *screen,
206                                                          const GdkColor    *colors,
207                                                          gint               n_colors);
208 static void     make_control_relations                  (AtkObject         *atk_obj,
209                                                          GtkWidget         *widget);
210 static void     make_all_relations                      (AtkObject         *atk_obj,
211                                                          GtkColorSelectionPrivate *priv);
212
213 static void     hsv_changed                             (GtkWidget         *hsv,
214                                                          gpointer           data);
215 static void     get_screen_color                        (GtkWidget         *button);
216 static void     adjustment_changed                      (GtkAdjustment     *adjustment,
217                                                          gpointer           data);
218 static void     opacity_entry_changed                   (GtkWidget         *opacity_entry,
219                                                          gpointer           data);
220 static void     hex_changed                             (GtkWidget         *hex_entry,
221                                                          gpointer           data);
222 static gboolean hex_focus_out                           (GtkWidget         *hex_entry,
223                                                          GdkEventFocus     *event,
224                                                          gpointer           data);
225 static void     color_sample_new                        (GtkColorSelection *colorsel);
226 static void     make_label_spinbutton                   (GtkColorSelection *colorsel,
227                                                          GtkWidget        **spinbutton,
228                                                          gchar             *text,
229                                                          GtkWidget         *table,
230                                                          gint               i,
231                                                          gint               j,
232                                                          gint               channel_type,
233                                                          const gchar       *tooltip);
234 static void     make_palette_frame                      (GtkColorSelection *colorsel,
235                                                          GtkWidget         *table,
236                                                          gint               i,
237                                                          gint               j);
238 static void     set_selected_palette                    (GtkColorSelection *colorsel,
239                                                          int                x,
240                                                          int                y);
241 static void     set_focus_line_attributes               (GtkWidget         *drawing_area,
242                                                          cairo_t           *cr,
243                                                          gint              *focus_width);
244 static gboolean mouse_press                             (GtkWidget         *invisible,
245                                                          GdkEventButton    *event,
246                                                          gpointer           data);
247 static void  palette_change_notify_instance (GObject    *object,
248                                              GParamSpec *pspec,
249                                              gpointer    data);
250 static void update_palette (GtkColorSelection *colorsel);
251 static void shutdown_eyedropper (GtkWidget *widget);
252
253 static guint color_selection_signals[LAST_SIGNAL] = { 0 };
254
255 static GtkColorSelectionChangePaletteFunc noscreen_change_palette_hook = default_noscreen_change_palette_func;
256 static GtkColorSelectionChangePaletteWithScreenFunc change_palette_hook = default_change_palette_func;
257
258 static const guchar dropper_bits[] = {
259   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
260   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\377\377\377\377\377\377\377"
261   "\377\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
262   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\377\0\0\0\377"
263   "\0\0\0\377\0\0\0\377\377\377\377\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
264   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377"
265   "\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\377\377\377\377"
266   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377"
267   "\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377\0\0\0\377\0\0"
268   "\0\377\0\0\0\377\0\0\0\377\377\377\377\377\0\0\0\0\0\0\0\0\0\0\0\0\0"
269   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\377\0\0\0\377\0\0\0\377\0"
270   "\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\377\377\377"
271   "\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
272   "\377\377\377\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0"
273   "\0\0\377\377\377\377\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
274   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\377\377\0\0\0\377\0\0"
275   "\0\377\0\0\0\377\377\377\377\377\377\377\377\377\0\0\0\0\0\0\0\0\0\0"
276   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377"
277   "\377\377\377\377\377\377\377\377\377\377\0\0\0\377\0\0\0\377\377\377"
278   "\377\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
279   "\0\0\0\0\0\0\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377"
280   "\0\0\0\377\377\377\377\377\0\0\0\377\377\377\377\377\0\0\0\0\0\0\0\0"
281   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\377"
282   "\377\377\377\377\377\377\377\377\377\0\0\0\377\0\0\0\0\0\0\0\0\377\377"
283   "\377\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
284   "\0\0\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0"
285   "\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
286   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\377\377\377\377\377\377\377"
287   "\377\377\377\0\0\0\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
288   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\377\377"
289   "\377\377\377\377\377\377\377\377\0\0\0\377\0\0\0\0\0\0\0\0\0\0\0\0\0"
290   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
291   "\377\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377\0\0\0"
292   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
293   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\377\377\377\377\377\377\0\0"
294   "\0\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
295   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0\0\0\0\0\377\0\0\0"
296   "\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
297   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0\0\0"
298   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
299   "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"};
300
301 G_DEFINE_TYPE (GtkColorSelection, gtk_color_selection, GTK_TYPE_VBOX)
302
303 static void
304 gtk_color_selection_class_init (GtkColorSelectionClass *klass)
305 {
306   GObjectClass *gobject_class;
307   GtkWidgetClass *widget_class;
308
309   gobject_class = G_OBJECT_CLASS (klass);
310   gobject_class->finalize = gtk_color_selection_finalize;
311   gobject_class->set_property = gtk_color_selection_set_property;
312   gobject_class->get_property = gtk_color_selection_get_property;
313
314   widget_class = GTK_WIDGET_CLASS (klass);
315   widget_class->destroy = gtk_color_selection_destroy;
316   widget_class->realize = gtk_color_selection_realize;
317   widget_class->unrealize = gtk_color_selection_unrealize;
318   widget_class->show_all = gtk_color_selection_show_all;
319   widget_class->grab_broken_event = gtk_color_selection_grab_broken;
320
321   g_object_class_install_property (gobject_class,
322                                    PROP_HAS_OPACITY_CONTROL,
323                                    g_param_spec_boolean ("has-opacity-control",
324                                                          P_("Has Opacity Control"),
325                                                          P_("Whether the color selector should allow setting opacity"),
326                                                          FALSE,
327                                                          GTK_PARAM_READWRITE));
328   g_object_class_install_property (gobject_class,
329                                    PROP_HAS_PALETTE,
330                                    g_param_spec_boolean ("has-palette",
331                                                          P_("Has palette"),
332                                                          P_("Whether a palette should be used"),
333                                                          FALSE,
334                                                          GTK_PARAM_READWRITE));
335   g_object_class_install_property (gobject_class,
336                                    PROP_CURRENT_COLOR,
337                                    g_param_spec_boxed ("current-color",
338                                                        P_("Current Color"),
339                                                        P_("The current color"),
340                                                        GDK_TYPE_COLOR,
341                                                        GTK_PARAM_READWRITE));
342   g_object_class_install_property (gobject_class,
343                                    PROP_CURRENT_ALPHA,
344                                    g_param_spec_uint ("current-alpha",
345                                                       P_("Current Alpha"),
346                                                       P_("The current opacity value (0 fully transparent, 65535 fully opaque)"),
347                                                       0, 65535, 65535,
348                                                       GTK_PARAM_READWRITE));
349
350   /**
351    * GtkColorSelection:current-rgba
352    *
353    * The current RGBA color.
354    *
355    * Since: 3.0
356    */
357   g_object_class_install_property (gobject_class,
358                                    PROP_CURRENT_RGBA,
359                                    g_param_spec_boxed ("current-rgba",
360                                                        P_("Current RGBA"),
361                                                        P_("The current RGBA color"),
362                                                        GDK_TYPE_RGBA,
363                                                        GTK_PARAM_READWRITE));
364
365   /**
366    * GtkColorSelection::color-changed:
367    * @colorselection: the object which received the signal.
368    *
369    * This signal is emitted when the color changes in the #GtkColorSelection
370    * according to its update policy.
371    */
372   color_selection_signals[COLOR_CHANGED] =
373     g_signal_new (I_("color-changed"),
374                   G_OBJECT_CLASS_TYPE (gobject_class),
375                   G_SIGNAL_RUN_FIRST,
376                   G_STRUCT_OFFSET (GtkColorSelectionClass, color_changed),
377                   NULL, NULL,
378                   _gtk_marshal_VOID__VOID,
379                   G_TYPE_NONE, 0);
380
381   g_type_class_add_private (gobject_class, sizeof (GtkColorSelectionPrivate));
382 }
383
384 static void
385 gtk_color_selection_init (GtkColorSelection *colorsel)
386 {
387   GtkWidget *top_hbox;
388   GtkWidget *top_right_vbox;
389   GtkWidget *table, *label, *hbox, *frame, *vbox, *button;
390   GtkAdjustment *adjust;
391   GtkWidget *picker_image;
392   gint i, j;
393   GtkColorSelectionPrivate *priv;
394   AtkObject *atk_obj;
395   GList *focus_chain = NULL;
396
397   gtk_widget_push_composite_child ();
398
399   priv = colorsel->private_data = G_TYPE_INSTANCE_GET_PRIVATE (colorsel, GTK_TYPE_COLOR_SELECTION, GtkColorSelectionPrivate);
400   priv->changing = FALSE;
401   priv->default_set = FALSE;
402   priv->default_alpha_set = FALSE;
403
404   top_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
405   gtk_box_pack_start (GTK_BOX (colorsel), top_hbox, FALSE, FALSE, 0);
406
407   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
408   priv->triangle_colorsel = gtk_hsv_new ();
409   g_signal_connect (priv->triangle_colorsel, "changed",
410                     G_CALLBACK (hsv_changed), colorsel);
411   gtk_hsv_set_metrics (GTK_HSV (priv->triangle_colorsel), 174, 15);
412   gtk_box_pack_start (GTK_BOX (top_hbox), vbox, FALSE, FALSE, 0);
413   gtk_box_pack_start (GTK_BOX (vbox), priv->triangle_colorsel, FALSE, FALSE, 0);
414   gtk_widget_set_tooltip_text (priv->triangle_colorsel,
415                         _("Select the color you want from the outer ring. "
416                           "Select the darkness or lightness of that color "
417                           "using the inner triangle."));
418
419   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
420   gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
421
422   frame = gtk_frame_new (NULL);
423   gtk_widget_set_size_request (frame, -1, 30);
424   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
425   color_sample_new (colorsel);
426   gtk_container_add (GTK_CONTAINER (frame), priv->sample_area);
427   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
428
429   button = gtk_button_new ();
430
431   gtk_widget_set_events (button, GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
432   g_object_set_data (G_OBJECT (button), I_("COLORSEL"), colorsel);
433   g_signal_connect (button, "clicked",
434                     G_CALLBACK (get_screen_color), NULL);
435   picker_image = gtk_image_new_from_stock (GTK_STOCK_COLOR_PICKER, GTK_ICON_SIZE_BUTTON);
436   gtk_container_add (GTK_CONTAINER (button), picker_image);
437   gtk_widget_show (GTK_WIDGET (picker_image));
438   gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
439
440   gtk_widget_set_tooltip_text (button,
441                         _("Click the eyedropper, then click a color "
442                           "anywhere on your screen to select that color."));
443
444   top_right_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
445   gtk_box_pack_start (GTK_BOX (top_hbox), top_right_vbox, FALSE, FALSE, 0);
446   table = gtk_table_new (8, 6, FALSE);
447   gtk_box_pack_start (GTK_BOX (top_right_vbox), table, FALSE, FALSE, 0);
448   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
449   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
450
451   make_label_spinbutton (colorsel, &priv->hue_spinbutton, _("_Hue:"), table, 0, 0, COLORSEL_HUE,
452                          _("Position on the color wheel."));
453   gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (priv->hue_spinbutton), TRUE);
454   make_label_spinbutton (colorsel, &priv->sat_spinbutton, _("_Saturation:"), table, 0, 1, COLORSEL_SATURATION,
455                          _("Intensity of the color."));
456   make_label_spinbutton (colorsel, &priv->val_spinbutton, _("_Value:"), table, 0, 2, COLORSEL_VALUE,
457                          _("Brightness of the color."));
458   make_label_spinbutton (colorsel, &priv->red_spinbutton, _("_Red:"), table, 6, 0, COLORSEL_RED,
459                          _("Amount of red light in the color."));
460   make_label_spinbutton (colorsel, &priv->green_spinbutton, _("_Green:"), table, 6, 1, COLORSEL_GREEN,
461                          _("Amount of green light in the color."));
462   make_label_spinbutton (colorsel, &priv->blue_spinbutton, _("_Blue:"), table, 6, 2, COLORSEL_BLUE,
463                          _("Amount of blue light in the color."));
464   gtk_table_attach_defaults (GTK_TABLE (table), gtk_separator_new (GTK_ORIENTATION_HORIZONTAL), 0, 8, 3, 4);
465
466   priv->opacity_label = gtk_label_new_with_mnemonic (_("Op_acity:"));
467   gtk_misc_set_alignment (GTK_MISC (priv->opacity_label), 0.0, 0.5);
468   gtk_table_attach_defaults (GTK_TABLE (table), priv->opacity_label, 0, 1, 4, 5);
469   adjust = gtk_adjustment_new (0.0, 0.0, 255.0, 1.0, 1.0, 0.0);
470   g_object_set_data (G_OBJECT (adjust), I_("COLORSEL"), colorsel);
471   priv->opacity_slider = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adjust);
472   gtk_widget_set_tooltip_text (priv->opacity_slider,
473                         _("Transparency of the color."));
474   gtk_label_set_mnemonic_widget (GTK_LABEL (priv->opacity_label),
475                                  priv->opacity_slider);
476   gtk_scale_set_draw_value (GTK_SCALE (priv->opacity_slider), FALSE);
477   g_signal_connect (adjust, "value-changed",
478                     G_CALLBACK (adjustment_changed),
479                     GINT_TO_POINTER (COLORSEL_OPACITY));
480   gtk_table_attach_defaults (GTK_TABLE (table), priv->opacity_slider, 1, 7, 4, 5);
481   priv->opacity_entry = gtk_entry_new ();
482   gtk_widget_set_tooltip_text (priv->opacity_entry,
483                         _("Transparency of the color."));
484   gtk_widget_set_size_request (priv->opacity_entry, 40, -1);
485
486   g_signal_connect (priv->opacity_entry, "activate",
487                     G_CALLBACK (opacity_entry_changed), colorsel);
488   gtk_table_attach_defaults (GTK_TABLE (table), priv->opacity_entry, 7, 8, 4, 5);
489
490   label = gtk_label_new_with_mnemonic (_("Color _name:"));
491   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 5, 6);
492   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
493   priv->hex_entry = gtk_entry_new ();
494
495   gtk_label_set_mnemonic_widget (GTK_LABEL (label), priv->hex_entry);
496
497   g_signal_connect (priv->hex_entry, "activate",
498                     G_CALLBACK (hex_changed), colorsel);
499
500   g_signal_connect (priv->hex_entry, "focus-out-event",
501                     G_CALLBACK (hex_focus_out), colorsel);
502
503   gtk_widget_set_tooltip_text (priv->hex_entry,
504                         _("You can enter an HTML-style hexadecimal color "
505                           "value, or simply a color name such as 'orange' "
506                           "in this entry."));
507
508   gtk_entry_set_width_chars (GTK_ENTRY (priv->hex_entry), 7);
509   gtk_table_attach_defaults (GTK_TABLE (table), priv->hex_entry, 1, 5, 5, 6);
510
511   focus_chain = g_list_append (focus_chain, priv->hue_spinbutton);
512   focus_chain = g_list_append (focus_chain, priv->sat_spinbutton);
513   focus_chain = g_list_append (focus_chain, priv->val_spinbutton);
514   focus_chain = g_list_append (focus_chain, priv->red_spinbutton);
515   focus_chain = g_list_append (focus_chain, priv->green_spinbutton);
516   focus_chain = g_list_append (focus_chain, priv->blue_spinbutton);
517   focus_chain = g_list_append (focus_chain, priv->opacity_slider);
518   focus_chain = g_list_append (focus_chain, priv->opacity_entry);
519   focus_chain = g_list_append (focus_chain, priv->hex_entry);
520   gtk_container_set_focus_chain (GTK_CONTAINER (table), focus_chain);
521   g_list_free (focus_chain);
522
523   /* Set up the palette */
524   table = gtk_table_new (GTK_CUSTOM_PALETTE_HEIGHT, GTK_CUSTOM_PALETTE_WIDTH, TRUE);
525   gtk_table_set_row_spacings (GTK_TABLE (table), 1);
526   gtk_table_set_col_spacings (GTK_TABLE (table), 1);
527   for (i = 0; i < GTK_CUSTOM_PALETTE_WIDTH; i++)
528     {
529       for (j = 0; j < GTK_CUSTOM_PALETTE_HEIGHT; j++)
530         {
531           make_palette_frame (colorsel, table, i, j);
532         }
533     }
534   set_selected_palette (colorsel, 0, 0);
535   priv->palette_frame = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
536   label = gtk_label_new_with_mnemonic (_("_Palette:"));
537   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
538   gtk_box_pack_start (GTK_BOX (priv->palette_frame), label, FALSE, FALSE, 0);
539
540   gtk_label_set_mnemonic_widget (GTK_LABEL (label),
541                                  priv->custom_palette[0][0]);
542
543   gtk_box_pack_end (GTK_BOX (top_right_vbox), priv->palette_frame, FALSE, FALSE, 0);
544   gtk_box_pack_start (GTK_BOX (priv->palette_frame), table, FALSE, FALSE, 0);
545
546   gtk_widget_show_all (top_hbox);
547
548   /* hide unused stuff */
549
550   if (priv->has_opacity == FALSE)
551     {
552       gtk_widget_hide (priv->opacity_label);
553       gtk_widget_hide (priv->opacity_slider);
554       gtk_widget_hide (priv->opacity_entry);
555     }
556
557   if (priv->has_palette == FALSE)
558     {
559       gtk_widget_hide (priv->palette_frame);
560     }
561
562   atk_obj = gtk_widget_get_accessible (priv->triangle_colorsel);
563   if (GTK_IS_ACCESSIBLE (atk_obj))
564     {
565       atk_object_set_name (atk_obj, _("Color Wheel"));
566       atk_object_set_role (gtk_widget_get_accessible (GTK_WIDGET (colorsel)), ATK_ROLE_COLOR_CHOOSER);
567       make_all_relations (atk_obj, priv);
568     }
569
570   gtk_widget_pop_composite_child ();
571 }
572
573 /* GObject methods */
574 static void
575 gtk_color_selection_finalize (GObject *object)
576 {
577   G_OBJECT_CLASS (gtk_color_selection_parent_class)->finalize (object);
578 }
579
580 static void
581 gtk_color_selection_set_property (GObject         *object,
582                                   guint            prop_id,
583                                   const GValue    *value,
584                                   GParamSpec      *pspec)
585 {
586   GtkColorSelection *colorsel = GTK_COLOR_SELECTION (object);
587
588   switch (prop_id)
589     {
590     case PROP_HAS_OPACITY_CONTROL:
591       gtk_color_selection_set_has_opacity_control (colorsel,
592                                                    g_value_get_boolean (value));
593       break;
594     case PROP_HAS_PALETTE:
595       gtk_color_selection_set_has_palette (colorsel,
596                                            g_value_get_boolean (value));
597       break;
598     case PROP_CURRENT_COLOR:
599       gtk_color_selection_set_current_color (colorsel, g_value_get_boxed (value));
600       break;
601     case PROP_CURRENT_ALPHA:
602       gtk_color_selection_set_current_alpha (colorsel, g_value_get_uint (value));
603       break;
604     case PROP_CURRENT_RGBA:
605       gtk_color_selection_set_current_rgba (colorsel, g_value_get_boxed (value));
606       break;
607     default:
608       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
609       break;
610     }
611
612 }
613
614 static void
615 gtk_color_selection_get_property (GObject     *object,
616                                   guint        prop_id,
617                                   GValue      *value,
618                                   GParamSpec  *pspec)
619 {
620   GtkColorSelection *colorsel = GTK_COLOR_SELECTION (object);
621   GdkColor color;
622
623   switch (prop_id)
624     {
625     case PROP_HAS_OPACITY_CONTROL:
626       g_value_set_boolean (value, gtk_color_selection_get_has_opacity_control (colorsel));
627       break;
628     case PROP_HAS_PALETTE:
629       g_value_set_boolean (value, gtk_color_selection_get_has_palette (colorsel));
630       break;
631     case PROP_CURRENT_COLOR:
632       gtk_color_selection_get_current_color (colorsel, &color);
633       g_value_set_boxed (value, &color);
634       break;
635     case PROP_CURRENT_ALPHA:
636       g_value_set_uint (value, gtk_color_selection_get_current_alpha (colorsel));
637       break;
638     case PROP_CURRENT_RGBA:
639       {
640         GdkRGBA rgba;
641
642         gtk_color_selection_get_current_rgba (colorsel, &rgba);
643         g_value_set_boxed (value, &rgba);
644       }
645       break;
646     default:
647       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
648       break;
649     }
650 }
651
652 /* GtkWidget methods */
653
654 static void
655 gtk_color_selection_destroy (GtkWidget *widget)
656 {
657   GtkColorSelection *cselection = GTK_COLOR_SELECTION (widget);
658   GtkColorSelectionPrivate *priv = cselection->private_data;
659
660   if (priv->dropper_grab_widget)
661     {
662       gtk_widget_destroy (priv->dropper_grab_widget);
663       priv->dropper_grab_widget = NULL;
664     }
665
666   GTK_WIDGET_CLASS (gtk_color_selection_parent_class)->destroy (widget);
667 }
668
669 static void
670 gtk_color_selection_realize (GtkWidget *widget)
671 {
672   GtkColorSelection *colorsel = GTK_COLOR_SELECTION (widget);
673   GtkColorSelectionPrivate *priv = colorsel->private_data;
674   GtkSettings *settings = gtk_widget_get_settings (widget);
675
676   priv->settings_connection =  g_signal_connect (settings,
677                                                  "notify::gtk-color-palette",
678                                                  G_CALLBACK (palette_change_notify_instance),
679                                                  widget);
680   update_palette (colorsel);
681
682   GTK_WIDGET_CLASS (gtk_color_selection_parent_class)->realize (widget);
683 }
684
685 static void
686 gtk_color_selection_unrealize (GtkWidget *widget)
687 {
688   GtkColorSelection *colorsel = GTK_COLOR_SELECTION (widget);
689   GtkColorSelectionPrivate *priv = colorsel->private_data;
690   GtkSettings *settings = gtk_widget_get_settings (widget);
691
692   g_signal_handler_disconnect (settings, priv->settings_connection);
693
694   GTK_WIDGET_CLASS (gtk_color_selection_parent_class)->unrealize (widget);
695 }
696
697 /* We override show-all since we have internal widgets that
698  * shouldn't be shown when you call show_all(), like the
699  * palette and opacity sliders.
700  */
701 static void
702 gtk_color_selection_show_all (GtkWidget *widget)
703 {
704   gtk_widget_show (widget);
705 }
706
707 static gboolean
708 gtk_color_selection_grab_broken (GtkWidget          *widget,
709                                  GdkEventGrabBroken *event)
710 {
711   shutdown_eyedropper (widget);
712
713   return TRUE;
714 }
715
716 /*
717  *
718  * The Sample Color
719  *
720  */
721
722 static void color_sample_draw_sample (GtkColorSelection *colorsel,
723                                       int                which,
724                                       cairo_t *          cr);
725 static void color_sample_update_samples (GtkColorSelection *colorsel);
726
727 static void
728 set_color_internal (GtkColorSelection *colorsel,
729                     gdouble           *color)
730 {
731   GtkColorSelectionPrivate *priv;
732   gint i;
733
734   priv = colorsel->private_data;
735   priv->changing = TRUE;
736   priv->color[COLORSEL_RED] = color[0];
737   priv->color[COLORSEL_GREEN] = color[1];
738   priv->color[COLORSEL_BLUE] = color[2];
739   priv->color[COLORSEL_OPACITY] = color[3];
740   gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
741                   priv->color[COLORSEL_GREEN],
742                   priv->color[COLORSEL_BLUE],
743                   &priv->color[COLORSEL_HUE],
744                   &priv->color[COLORSEL_SATURATION],
745                   &priv->color[COLORSEL_VALUE]);
746   if (priv->default_set == FALSE)
747     {
748       for (i = 0; i < COLORSEL_NUM_CHANNELS; i++)
749         priv->old_color[i] = priv->color[i];
750     }
751   priv->default_set = TRUE;
752   priv->default_alpha_set = TRUE;
753   update_color (colorsel);
754 }
755
756 static void
757 set_color_icon (GdkDragContext *context,
758                 gdouble        *colors)
759 {
760   GdkPixbuf *pixbuf;
761   guint32 pixel;
762
763   pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE,
764                            8, 48, 32);
765
766   pixel = (((UNSCALE (colors[COLORSEL_RED])   & 0xff00) << 16) |
767            ((UNSCALE (colors[COLORSEL_GREEN]) & 0xff00) << 8) |
768            ((UNSCALE (colors[COLORSEL_BLUE])  & 0xff00)));
769
770   gdk_pixbuf_fill (pixbuf, pixel);
771
772   gtk_drag_set_icon_pixbuf (context, pixbuf, -2, -2);
773   g_object_unref (pixbuf);
774 }
775
776 static void
777 color_sample_drag_begin (GtkWidget      *widget,
778                          GdkDragContext *context,
779                          gpointer        data)
780 {
781   GtkColorSelection *colorsel = data;
782   GtkColorSelectionPrivate *priv;
783   gdouble *colsrc;
784
785   priv = colorsel->private_data;
786
787   if (widget == priv->old_sample)
788     colsrc = priv->old_color;
789   else
790     colsrc = priv->color;
791
792   set_color_icon (context, colsrc);
793 }
794
795 static void
796 color_sample_drag_end (GtkWidget      *widget,
797                        GdkDragContext *context,
798                        gpointer        data)
799 {
800   g_object_set_data (G_OBJECT (widget), I_("gtk-color-selection-drag-window"), NULL);
801 }
802
803 static void
804 color_sample_drop_handle (GtkWidget        *widget,
805                           GdkDragContext   *context,
806                           gint              x,
807                           gint              y,
808                           GtkSelectionData *selection_data,
809                           guint             info,
810                           guint             time,
811                           gpointer          data)
812 {
813   GtkColorSelection *colorsel = data;
814   GtkColorSelectionPrivate *priv;
815   gint length;
816   guint16 *vals;
817   gdouble color[4];
818   priv = colorsel->private_data;
819
820   /* This is currently a guint16 array of the format:
821    * R
822    * G
823    * B
824    * opacity
825    */
826
827   length = gtk_selection_data_get_length (selection_data);
828
829   if (length < 0)
830     return;
831
832   /* We accept drops with the wrong format, since the KDE color
833    * chooser incorrectly drops application/x-color with format 8.
834    */
835   if (length != 8)
836     {
837       g_warning ("Received invalid color data\n");
838       return;
839     }
840
841   vals = (guint16 *) gtk_selection_data_get_data (selection_data);
842
843   if (widget == priv->cur_sample)
844     {
845       color[0] = (gdouble)vals[0] / 0xffff;
846       color[1] = (gdouble)vals[1] / 0xffff;
847       color[2] = (gdouble)vals[2] / 0xffff;
848       color[3] = (gdouble)vals[3] / 0xffff;
849
850       set_color_internal (colorsel, color);
851     }
852 }
853
854 static void
855 color_sample_drag_handle (GtkWidget        *widget,
856                           GdkDragContext   *context,
857                           GtkSelectionData *selection_data,
858                           guint             info,
859                           guint             time,
860                           gpointer          data)
861 {
862   GtkColorSelection *colorsel = data;
863   GtkColorSelectionPrivate *priv;
864   guint16 vals[4];
865   gdouble *colsrc;
866
867   priv = colorsel->private_data;
868
869   if (widget == priv->old_sample)
870     colsrc = priv->old_color;
871   else
872     colsrc = priv->color;
873
874   vals[0] = colsrc[COLORSEL_RED] * 0xffff;
875   vals[1] = colsrc[COLORSEL_GREEN] * 0xffff;
876   vals[2] = colsrc[COLORSEL_BLUE] * 0xffff;
877   vals[3] = priv->has_opacity ? colsrc[COLORSEL_OPACITY] * 0xffff : 0xffff;
878
879   gtk_selection_data_set (selection_data,
880                           gdk_atom_intern_static_string ("application/x-color"),
881                           16, (guchar *)vals, 8);
882 }
883
884 /* which = 0 means draw old sample, which = 1 means draw new */
885 static void
886 color_sample_draw_sample (GtkColorSelection *colorsel,
887                           int                which,
888                           cairo_t           *cr)
889 {
890   GtkWidget *da;
891   gint x, y, goff;
892   GtkColorSelectionPrivate *priv;
893   int width, height;
894
895   g_return_if_fail (colorsel != NULL);
896   priv = colorsel->private_data;
897
898   g_return_if_fail (priv->sample_area != NULL);
899   if (!gtk_widget_is_drawable (priv->sample_area))
900     return;
901
902   if (which == 0)
903     {
904       da = priv->old_sample;
905       goff = 0;
906     }
907   else
908     {
909       GtkAllocation old_sample_allocation;
910
911       da = priv->cur_sample;
912       gtk_widget_get_allocation (priv->old_sample, &old_sample_allocation);
913       goff =  old_sample_allocation.width % 32;
914     }
915
916   /* Below needs tweaking for non-power-of-two */
917   width = gtk_widget_get_allocated_width (da);
918   height = gtk_widget_get_allocated_height (da);
919
920   if (priv->has_opacity)
921     {
922       /* Draw checks in background */
923
924       cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
925       cairo_rectangle (cr, 0, 0, width, height);
926       cairo_fill (cr);
927
928       cairo_set_source_rgb (cr, 0.75, 0.75, 0.75);
929       for (x = goff & -CHECK_SIZE; x < goff + width; x += CHECK_SIZE)
930         for (y = 0; y < height; y += CHECK_SIZE)
931           if ((x / CHECK_SIZE + y / CHECK_SIZE) % 2 == 0)
932             cairo_rectangle (cr, x - goff, y, CHECK_SIZE, CHECK_SIZE);
933       cairo_fill (cr);
934     }
935
936   if (which == 0)
937     {
938       cairo_set_source_rgba (cr,
939                              priv->old_color[COLORSEL_RED],
940                              priv->old_color[COLORSEL_GREEN],
941                              priv->old_color[COLORSEL_BLUE],
942                              priv->has_opacity ?
943                                 priv->old_color[COLORSEL_OPACITY] : 1.0);
944     }
945   else
946     {
947       cairo_set_source_rgba (cr,
948                              priv->color[COLORSEL_RED],
949                              priv->color[COLORSEL_GREEN],
950                              priv->color[COLORSEL_BLUE],
951                              priv->has_opacity ?
952                                priv->color[COLORSEL_OPACITY] : 1.0);
953     }
954
955   cairo_rectangle (cr, 0, 0, width, height);
956   cairo_fill (cr);
957 }
958
959
960 static void
961 color_sample_update_samples (GtkColorSelection *colorsel)
962 {
963   GtkColorSelectionPrivate *priv = colorsel->private_data;
964   gtk_widget_queue_draw (priv->old_sample);
965   gtk_widget_queue_draw (priv->cur_sample);
966 }
967
968 static gboolean
969 color_old_sample_draw (GtkWidget         *da,
970                        cairo_t           *cr,
971                        GtkColorSelection *colorsel)
972 {
973   color_sample_draw_sample (colorsel, 0, cr);
974   return FALSE;
975 }
976
977
978 static gboolean
979 color_cur_sample_draw (GtkWidget         *da,
980                        cairo_t           *cr,
981                        GtkColorSelection *colorsel)
982 {
983   color_sample_draw_sample (colorsel, 1, cr);
984   return FALSE;
985 }
986
987 static void
988 color_sample_setup_dnd (GtkColorSelection *colorsel, GtkWidget *sample)
989 {
990   static const GtkTargetEntry targets[] = {
991     { "application/x-color", 0 }
992   };
993   GtkColorSelectionPrivate *priv;
994   priv = colorsel->private_data;
995
996   gtk_drag_source_set (sample,
997                        GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
998                        targets, 1,
999                        GDK_ACTION_COPY | GDK_ACTION_MOVE);
1000
1001   g_signal_connect (sample, "drag-begin",
1002                     G_CALLBACK (color_sample_drag_begin),
1003                     colorsel);
1004   if (sample == priv->cur_sample)
1005     {
1006
1007       gtk_drag_dest_set (sample,
1008                          GTK_DEST_DEFAULT_HIGHLIGHT |
1009                          GTK_DEST_DEFAULT_MOTION |
1010                          GTK_DEST_DEFAULT_DROP,
1011                          targets, 1,
1012                          GDK_ACTION_COPY);
1013
1014       g_signal_connect (sample, "drag-end",
1015                         G_CALLBACK (color_sample_drag_end),
1016                         colorsel);
1017     }
1018
1019   g_signal_connect (sample, "drag-data-get",
1020                     G_CALLBACK (color_sample_drag_handle),
1021                     colorsel);
1022   g_signal_connect (sample, "drag-data-received",
1023                     G_CALLBACK (color_sample_drop_handle),
1024                     colorsel);
1025
1026 }
1027
1028 static void
1029 update_tooltips (GtkColorSelection *colorsel)
1030 {
1031   GtkColorSelectionPrivate *priv;
1032
1033   priv = colorsel->private_data;
1034
1035   if (priv->has_palette == TRUE)
1036     {
1037       gtk_widget_set_tooltip_text (priv->old_sample,
1038         _("The previously-selected color, for comparison to the color "
1039           "you're selecting now. You can drag this color to a palette "
1040           "entry, or select this color as current by dragging it to the "
1041           "other color swatch alongside."));
1042
1043       gtk_widget_set_tooltip_text (priv->cur_sample,
1044         _("The color you've chosen. You can drag this color to a palette "
1045           "entry to save it for use in the future."));
1046     }
1047   else
1048     {
1049       gtk_widget_set_tooltip_text (priv->old_sample,
1050         _("The previously-selected color, for comparison to the color "
1051           "you're selecting now."));
1052
1053       gtk_widget_set_tooltip_text (priv->cur_sample,
1054         _("The color you've chosen."));
1055     }
1056 }
1057
1058 static void
1059 color_sample_new (GtkColorSelection *colorsel)
1060 {
1061   GtkColorSelectionPrivate *priv;
1062
1063   priv = colorsel->private_data;
1064
1065   priv->sample_area = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1066   priv->old_sample = gtk_drawing_area_new ();
1067   priv->cur_sample = gtk_drawing_area_new ();
1068
1069   gtk_box_pack_start (GTK_BOX (priv->sample_area), priv->old_sample,
1070                       TRUE, TRUE, 0);
1071   gtk_box_pack_start (GTK_BOX (priv->sample_area), priv->cur_sample,
1072                       TRUE, TRUE, 0);
1073
1074   g_signal_connect (priv->old_sample, "draw",
1075                     G_CALLBACK (color_old_sample_draw),
1076                     colorsel);
1077   g_signal_connect (priv->cur_sample, "draw",
1078                     G_CALLBACK (color_cur_sample_draw),
1079                     colorsel);
1080
1081   color_sample_setup_dnd (colorsel, priv->old_sample);
1082   color_sample_setup_dnd (colorsel, priv->cur_sample);
1083
1084   update_tooltips (colorsel);
1085
1086   gtk_widget_show_all (priv->sample_area);
1087 }
1088
1089
1090 /* The palette area code */
1091
1092 static void
1093 palette_get_color (GtkWidget *drawing_area, gdouble *color)
1094 {
1095   gdouble *color_val;
1096
1097   g_return_if_fail (color != NULL);
1098
1099   color_val = g_object_get_data (G_OBJECT (drawing_area), "color_val");
1100   if (color_val == NULL)
1101     {
1102       /* Default to white for no good reason */
1103       color[0] = 1.0;
1104       color[1] = 1.0;
1105       color[2] = 1.0;
1106       color[3] = 1.0;
1107       return;
1108     }
1109
1110   color[0] = color_val[0];
1111   color[1] = color_val[1];
1112   color[2] = color_val[2];
1113   color[3] = 1.0;
1114 }
1115
1116 static gboolean
1117 palette_draw (GtkWidget *drawing_area,
1118                cairo_t   *cr,
1119                gpointer   data)
1120 {
1121   GtkStyleContext *context;
1122   gint focus_width;
1123   GdkRGBA color;
1124
1125   context = gtk_widget_get_style_context (drawing_area);
1126   gtk_style_context_get_background_color (context, 0, &color);
1127   gdk_cairo_set_source_rgba (cr, &color);
1128   cairo_paint (cr);
1129
1130   if (gtk_widget_has_focus (drawing_area))
1131     {
1132       set_focus_line_attributes (drawing_area, cr, &focus_width);
1133
1134       cairo_rectangle (cr,
1135                        focus_width / 2., focus_width / 2.,
1136                        gtk_widget_get_allocated_width (drawing_area) - focus_width,
1137                        gtk_widget_get_allocated_height (drawing_area) - focus_width);
1138       cairo_stroke (cr);
1139     }
1140
1141   return FALSE;
1142 }
1143
1144 static void
1145 set_focus_line_attributes (GtkWidget *drawing_area,
1146                            cairo_t   *cr,
1147                            gint      *focus_width)
1148 {
1149   gdouble color[4];
1150   gint8 *dash_list;
1151
1152   gtk_widget_style_get (drawing_area,
1153                         "focus-line-width", focus_width,
1154                         "focus-line-pattern", (gchar *)&dash_list,
1155                         NULL);
1156
1157   palette_get_color (drawing_area, color);
1158
1159   if (INTENSITY (color[0], color[1], color[2]) > 0.5)
1160     cairo_set_source_rgb (cr, 0., 0., 0.);
1161   else
1162     cairo_set_source_rgb (cr, 1., 1., 1.);
1163
1164   cairo_set_line_width (cr, *focus_width);
1165
1166   if (dash_list[0])
1167     {
1168       gint n_dashes = strlen ((gchar *)dash_list);
1169       gdouble *dashes = g_new (gdouble, n_dashes);
1170       gdouble total_length = 0;
1171       gdouble dash_offset;
1172       gint i;
1173
1174       for (i = 0; i < n_dashes; i++)
1175         {
1176           dashes[i] = dash_list[i];
1177           total_length += dash_list[i];
1178         }
1179
1180       /* The dash offset here aligns the pattern to integer pixels
1181        * by starting the dash at the right side of the left border
1182        * Negative dash offsets in cairo don't work
1183        * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
1184        */
1185       dash_offset = - *focus_width / 2.;
1186       while (dash_offset < 0)
1187         dash_offset += total_length;
1188
1189       cairo_set_dash (cr, dashes, n_dashes, dash_offset);
1190       g_free (dashes);
1191     }
1192
1193   g_free (dash_list);
1194 }
1195
1196 static void
1197 palette_drag_begin (GtkWidget      *widget,
1198                     GdkDragContext *context,
1199                     gpointer        data)
1200 {
1201   gdouble colors[4];
1202
1203   palette_get_color (widget, colors);
1204   set_color_icon (context, colors);
1205 }
1206
1207 static void
1208 palette_drag_handle (GtkWidget        *widget,
1209                      GdkDragContext   *context,
1210                      GtkSelectionData *selection_data,
1211                      guint             info,
1212                      guint             time,
1213                      gpointer          data)
1214 {
1215   guint16 vals[4];
1216   gdouble colsrc[4];
1217
1218   palette_get_color (widget, colsrc);
1219
1220   vals[0] = colsrc[COLORSEL_RED] * 0xffff;
1221   vals[1] = colsrc[COLORSEL_GREEN] * 0xffff;
1222   vals[2] = colsrc[COLORSEL_BLUE] * 0xffff;
1223   vals[3] = 0xffff;
1224
1225   gtk_selection_data_set (selection_data,
1226                           gdk_atom_intern_static_string ("application/x-color"),
1227                           16, (guchar *)vals, 8);
1228 }
1229
1230 static void
1231 palette_drag_end (GtkWidget      *widget,
1232                   GdkDragContext *context,
1233                   gpointer        data)
1234 {
1235   g_object_set_data (G_OBJECT (widget), I_("gtk-color-selection-drag-window"), NULL);
1236 }
1237
1238 static GdkColor *
1239 get_current_colors (GtkColorSelection *colorsel)
1240 {
1241   GtkSettings *settings;
1242   GdkColor *colors = NULL;
1243   gint n_colors = 0;
1244   gchar *palette;
1245
1246   settings = gtk_widget_get_settings (GTK_WIDGET (colorsel));
1247   g_object_get (settings, "gtk-color-palette", &palette, NULL);
1248
1249   if (!gtk_color_selection_palette_from_string (palette, &colors, &n_colors))
1250     {
1251       gtk_color_selection_palette_from_string (DEFAULT_COLOR_PALETTE,
1252                                                &colors,
1253                                                &n_colors);
1254     }
1255   else
1256     {
1257       /* If there are less colors provided than the number of slots in the
1258        * color selection, we fill in the rest from the defaults.
1259        */
1260       if (n_colors < (GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT))
1261         {
1262           GdkColor *tmp_colors = colors;
1263           gint tmp_n_colors = n_colors;
1264
1265           gtk_color_selection_palette_from_string (DEFAULT_COLOR_PALETTE,
1266                                                    &colors,
1267                                                    &n_colors);
1268           memcpy (colors, tmp_colors, sizeof (GdkColor) * tmp_n_colors);
1269
1270           g_free (tmp_colors);
1271         }
1272     }
1273
1274   /* make sure that we fill every slot */
1275   g_assert (n_colors == GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT);
1276   g_free (palette);
1277
1278   return colors;
1279 }
1280
1281 /* Changes the model color */
1282 static void
1283 palette_change_color (GtkWidget         *drawing_area,
1284                       GtkColorSelection *colorsel,
1285                       gdouble           *color)
1286 {
1287   gint x, y;
1288   GtkColorSelectionPrivate *priv;
1289   GdkColor gdk_color;
1290   GdkColor *current_colors;
1291   GdkScreen *screen;
1292
1293   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
1294   g_return_if_fail (GTK_IS_DRAWING_AREA (drawing_area));
1295
1296   priv = colorsel->private_data;
1297
1298   gdk_color.red = UNSCALE (color[0]);
1299   gdk_color.green = UNSCALE (color[1]);
1300   gdk_color.blue = UNSCALE (color[2]);
1301   gdk_color.pixel = 0;
1302
1303   x = 0;
1304   y = 0;                        /* Quiet GCC */
1305   while (x < GTK_CUSTOM_PALETTE_WIDTH)
1306     {
1307       y = 0;
1308       while (y < GTK_CUSTOM_PALETTE_HEIGHT)
1309         {
1310           if (priv->custom_palette[x][y] == drawing_area)
1311             goto out;
1312
1313           ++y;
1314         }
1315
1316       ++x;
1317     }
1318
1319  out:
1320
1321   g_assert (x < GTK_CUSTOM_PALETTE_WIDTH || y < GTK_CUSTOM_PALETTE_HEIGHT);
1322
1323   current_colors = get_current_colors (colorsel);
1324   current_colors[y * GTK_CUSTOM_PALETTE_WIDTH + x] = gdk_color;
1325
1326   screen = gtk_widget_get_screen (GTK_WIDGET (colorsel));
1327   if (change_palette_hook != default_change_palette_func)
1328     (* change_palette_hook) (screen, current_colors,
1329                              GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT);
1330   else if (noscreen_change_palette_hook != default_noscreen_change_palette_func)
1331     {
1332       if (screen != gdk_screen_get_default ())
1333         g_warning ("gtk_color_selection_set_change_palette_hook used by "
1334                    "widget is not on the default screen.");
1335       (* noscreen_change_palette_hook) (current_colors,
1336                                         GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT);
1337     }
1338   else
1339     (* change_palette_hook) (screen, current_colors,
1340                              GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT);
1341
1342   g_free (current_colors);
1343 }
1344
1345 /* Changes the view color */
1346 static void
1347 palette_set_color (GtkWidget         *drawing_area,
1348                    GtkColorSelection *colorsel,
1349                    gdouble           *color)
1350 {
1351   gdouble *new_color = g_new (double, 4);
1352   GdkColor gdk_color;
1353
1354   gdk_color.red = UNSCALE (color[0]);
1355   gdk_color.green = UNSCALE (color[1]);
1356   gdk_color.blue = UNSCALE (color[2]);
1357
1358   gtk_widget_modify_bg (drawing_area, GTK_STATE_NORMAL, &gdk_color);
1359
1360   if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (drawing_area), "color_set")) == 0)
1361     {
1362       static const GtkTargetEntry targets[] = {
1363         { "application/x-color", 0 }
1364       };
1365       gtk_drag_source_set (drawing_area,
1366                            GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
1367                            targets, 1,
1368                            GDK_ACTION_COPY | GDK_ACTION_MOVE);
1369
1370       g_signal_connect (drawing_area, "drag-begin",
1371                         G_CALLBACK (palette_drag_begin),
1372                         colorsel);
1373       g_signal_connect (drawing_area, "drag-data-get",
1374                         G_CALLBACK (palette_drag_handle),
1375                         colorsel);
1376
1377       g_object_set_data (G_OBJECT (drawing_area), I_("color_set"),
1378                          GINT_TO_POINTER (1));
1379     }
1380
1381   new_color[0] = color[0];
1382   new_color[1] = color[1];
1383   new_color[2] = color[2];
1384   new_color[3] = 1.0;
1385
1386   g_object_set_data_full (G_OBJECT (drawing_area),
1387                           I_("color_val"), new_color, (GDestroyNotify)g_free);
1388 }
1389
1390 static void
1391 popup_position_func (GtkMenu   *menu,
1392                      gint      *x,
1393                      gint      *y,
1394                      gboolean  *push_in,
1395                      gpointer   user_data)
1396 {
1397   GtkAllocation allocation;
1398   GtkWidget *widget;
1399   GtkRequisition req;
1400   gint root_x, root_y;
1401   GdkScreen *screen;
1402
1403   widget = GTK_WIDGET (user_data);
1404
1405   g_return_if_fail (gtk_widget_get_realized (widget));
1406
1407   gdk_window_get_origin (gtk_widget_get_window (widget),
1408                          &root_x, &root_y);
1409
1410   gtk_widget_get_preferred_size (GTK_WIDGET (menu),
1411                                  &req, NULL);
1412   gtk_widget_get_allocation (widget, &allocation);
1413
1414   /* Put corner of menu centered on color cell */
1415   *x = root_x + allocation.width / 2;
1416   *y = root_y + allocation.height / 2;
1417
1418   /* Ensure sanity */
1419   screen = gtk_widget_get_screen (widget);
1420   *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
1421   *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
1422 }
1423
1424 static void
1425 save_color_selected (GtkWidget *menuitem,
1426                      gpointer   data)
1427 {
1428   GtkColorSelection *colorsel;
1429   GtkWidget *drawing_area;
1430   GtkColorSelectionPrivate *priv;
1431
1432   drawing_area = GTK_WIDGET (data);
1433
1434   colorsel = GTK_COLOR_SELECTION (g_object_get_data (G_OBJECT (drawing_area),
1435                                                      "gtk-color-sel"));
1436
1437   priv = colorsel->private_data;
1438
1439   palette_change_color (drawing_area, colorsel, priv->color);
1440 }
1441
1442 static void
1443 do_popup (GtkColorSelection *colorsel,
1444           GtkWidget         *drawing_area,
1445           guint32            timestamp)
1446 {
1447   GtkWidget *menu;
1448   GtkWidget *mi;
1449
1450   g_object_set_data (G_OBJECT (drawing_area),
1451                      I_("gtk-color-sel"),
1452                      colorsel);
1453
1454   menu = gtk_menu_new ();
1455
1456   mi = gtk_menu_item_new_with_mnemonic (_("_Save color here"));
1457
1458   g_signal_connect (mi, "activate",
1459                     G_CALLBACK (save_color_selected),
1460                     drawing_area);
1461
1462   gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
1463
1464   gtk_widget_show_all (mi);
1465
1466   gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
1467                   popup_position_func, drawing_area,
1468                   3, timestamp);
1469 }
1470
1471
1472 static gboolean
1473 palette_enter (GtkWidget        *drawing_area,
1474                GdkEventCrossing *event,
1475                gpointer        data)
1476 {
1477   g_object_set_data (G_OBJECT (drawing_area),
1478                      I_("gtk-colorsel-have-pointer"),
1479                      GUINT_TO_POINTER (TRUE));
1480
1481   return FALSE;
1482 }
1483
1484 static gboolean
1485 palette_leave (GtkWidget        *drawing_area,
1486                GdkEventCrossing *event,
1487                gpointer        data)
1488 {
1489   g_object_set_data (G_OBJECT (drawing_area),
1490                      I_("gtk-colorsel-have-pointer"),
1491                      NULL);
1492
1493   return FALSE;
1494 }
1495
1496 static gboolean
1497 palette_press (GtkWidget      *drawing_area,
1498                GdkEventButton *event,
1499                gpointer        data)
1500 {
1501   GtkColorSelection *colorsel = GTK_COLOR_SELECTION (data);
1502
1503   gtk_widget_grab_focus (drawing_area);
1504
1505   if (event->button == 3 &&
1506       event->type == GDK_BUTTON_PRESS)
1507     {
1508       do_popup (colorsel, drawing_area, event->time);
1509       return TRUE;
1510     }
1511
1512   return FALSE;
1513 }
1514
1515 static gboolean
1516 palette_release (GtkWidget      *drawing_area,
1517                  GdkEventButton *event,
1518                  gpointer        data)
1519 {
1520   GtkColorSelection *colorsel = GTK_COLOR_SELECTION (data);
1521
1522   gtk_widget_grab_focus (drawing_area);
1523
1524   if (event->button == 1 &&
1525       g_object_get_data (G_OBJECT (drawing_area),
1526                          "gtk-colorsel-have-pointer") != NULL)
1527     {
1528       if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (drawing_area), "color_set")) != 0)
1529         {
1530           gdouble color[4];
1531           palette_get_color (drawing_area, color);
1532           set_color_internal (colorsel, color);
1533         }
1534     }
1535
1536   return FALSE;
1537 }
1538
1539 static void
1540 palette_drop_handle (GtkWidget        *widget,
1541                      GdkDragContext   *context,
1542                      gint              x,
1543                      gint              y,
1544                      GtkSelectionData *selection_data,
1545                      guint             info,
1546                      guint             time,
1547                      gpointer          data)
1548 {
1549   GtkColorSelection *colorsel = GTK_COLOR_SELECTION (data);
1550   gint length;
1551   guint16 *vals;
1552   gdouble color[4];
1553
1554   length = gtk_selection_data_get_length (selection_data);
1555
1556   if (length < 0)
1557     return;
1558
1559   /* We accept drops with the wrong format, since the KDE color
1560    * chooser incorrectly drops application/x-color with format 8.
1561    */
1562   if (length != 8)
1563     {
1564       g_warning ("Received invalid color data\n");
1565       return;
1566     }
1567
1568   vals = (guint16 *) gtk_selection_data_get_data (selection_data);
1569
1570   color[0] = (gdouble)vals[0] / 0xffff;
1571   color[1] = (gdouble)vals[1] / 0xffff;
1572   color[2] = (gdouble)vals[2] / 0xffff;
1573   color[3] = (gdouble)vals[3] / 0xffff;
1574   palette_change_color (widget, colorsel, color);
1575   set_color_internal (colorsel, color);
1576 }
1577
1578 static gint
1579 palette_activate (GtkWidget   *widget,
1580                   GdkEventKey *event,
1581                   gpointer     data)
1582 {
1583   /* should have a drawing area subclass with an activate signal */
1584   if ((event->keyval == GDK_KEY_space) ||
1585       (event->keyval == GDK_KEY_Return) ||
1586       (event->keyval == GDK_KEY_ISO_Enter) ||
1587       (event->keyval == GDK_KEY_KP_Enter) ||
1588       (event->keyval == GDK_KEY_KP_Space))
1589     {
1590       if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "color_set")) != 0)
1591         {
1592           gdouble color[4];
1593           palette_get_color (widget, color);
1594           set_color_internal (GTK_COLOR_SELECTION (data), color);
1595         }
1596       return TRUE;
1597     }
1598
1599   return FALSE;
1600 }
1601
1602 static gboolean
1603 palette_popup (GtkWidget *widget,
1604                gpointer   data)
1605 {
1606   GtkColorSelection *colorsel = GTK_COLOR_SELECTION (data);
1607
1608   do_popup (colorsel, widget, GDK_CURRENT_TIME);
1609   return TRUE;
1610 }
1611
1612
1613 static GtkWidget*
1614 palette_new (GtkColorSelection *colorsel)
1615 {
1616   GtkWidget *retval;
1617
1618   static const GtkTargetEntry targets[] = {
1619     { "application/x-color", 0 }
1620   };
1621
1622   retval = gtk_drawing_area_new ();
1623
1624   gtk_widget_set_can_focus (retval, TRUE);
1625
1626   g_object_set_data (G_OBJECT (retval), I_("color_set"), GINT_TO_POINTER (0));
1627   gtk_widget_set_events (retval, GDK_BUTTON_PRESS_MASK
1628                          | GDK_BUTTON_RELEASE_MASK
1629                          | GDK_EXPOSURE_MASK
1630                          | GDK_ENTER_NOTIFY_MASK
1631                          | GDK_LEAVE_NOTIFY_MASK);
1632
1633   g_signal_connect (retval, "draw",
1634                     G_CALLBACK (palette_draw), colorsel);
1635   g_signal_connect (retval, "button-press-event",
1636                     G_CALLBACK (palette_press), colorsel);
1637   g_signal_connect (retval, "button-release-event",
1638                     G_CALLBACK (palette_release), colorsel);
1639   g_signal_connect (retval, "enter-notify-event",
1640                     G_CALLBACK (palette_enter), colorsel);
1641   g_signal_connect (retval, "leave-notify-event",
1642                     G_CALLBACK (palette_leave), colorsel);
1643   g_signal_connect (retval, "key-press-event",
1644                     G_CALLBACK (palette_activate), colorsel);
1645   g_signal_connect (retval, "popup-menu",
1646                     G_CALLBACK (palette_popup), colorsel);
1647
1648   gtk_drag_dest_set (retval,
1649                      GTK_DEST_DEFAULT_HIGHLIGHT |
1650                      GTK_DEST_DEFAULT_MOTION |
1651                      GTK_DEST_DEFAULT_DROP,
1652                      targets, 1,
1653                      GDK_ACTION_COPY);
1654
1655   g_signal_connect (retval, "drag-end",
1656                     G_CALLBACK (palette_drag_end), NULL);
1657   g_signal_connect (retval, "drag-data-received",
1658                     G_CALLBACK (palette_drop_handle), colorsel);
1659
1660   gtk_widget_set_tooltip_text (retval,
1661     _("Click this palette entry to make it the current color. "
1662       "To change this entry, drag a color swatch here or right-click "
1663       "it and select \"Save color here.\""));
1664   return retval;
1665 }
1666
1667
1668 /* The actual GtkColorSelection widget */
1669
1670 static GdkCursor *
1671 make_picker_cursor (GdkScreen *screen)
1672 {
1673   GdkCursor *cursor;
1674
1675   cursor = gdk_cursor_new_from_name (gdk_screen_get_display (screen),
1676                                      "color-picker");
1677
1678   if (!cursor)
1679     {
1680       GdkPixbuf *pixbuf;
1681
1682       pixbuf = gdk_pixbuf_new_from_data (dropper_bits,
1683                                          GDK_COLORSPACE_RGB, TRUE, 8,
1684                                          DROPPER_WIDTH, DROPPER_HEIGHT,
1685                                          DROPPER_STRIDE,
1686                                          NULL, NULL);
1687
1688       cursor = gdk_cursor_new_from_pixbuf (gdk_screen_get_display (screen),
1689                                            pixbuf,
1690                                            DROPPER_X_HOT, DROPPER_Y_HOT);
1691
1692       g_object_unref (pixbuf);
1693     }
1694
1695   return cursor;
1696 }
1697
1698 static void
1699 grab_color_at_pointer (GdkScreen *screen,
1700                        GdkDevice *device,
1701                        gint       x_root,
1702                        gint       y_root,
1703                        gpointer   data)
1704 {
1705   GdkPixbuf *pixbuf;
1706   guchar *pixels;
1707   GtkColorSelection *colorsel = data;
1708   GtkColorSelectionPrivate *priv;
1709   GdkColor color;
1710   GdkWindow *root_window = gdk_screen_get_root_window (screen);
1711
1712   priv = colorsel->private_data;
1713
1714   pixbuf = gdk_pixbuf_get_from_window (root_window,
1715                                        x_root, y_root,
1716                                        1, 1);
1717   if (!pixbuf)
1718     {
1719       gint x, y;
1720       GdkWindow *window = gdk_device_get_window_at_position (device, &x, &y);
1721       if (!window)
1722         return;
1723       pixbuf = gdk_pixbuf_get_from_window (window,
1724                                            x, y,
1725                                            1, 1);
1726       if (!pixbuf)
1727         return;
1728     }
1729   pixels = gdk_pixbuf_get_pixels (pixbuf);
1730   color.red = pixels[0] * 0x101;
1731   color.green = pixels[1] * 0x101;
1732   color.blue = pixels[2] * 0x101;
1733   g_object_unref (pixbuf);
1734
1735   priv->color[COLORSEL_RED] = SCALE (color.red);
1736   priv->color[COLORSEL_GREEN] = SCALE (color.green);
1737   priv->color[COLORSEL_BLUE] = SCALE (color.blue);
1738
1739   gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
1740                   priv->color[COLORSEL_GREEN],
1741                   priv->color[COLORSEL_BLUE],
1742                   &priv->color[COLORSEL_HUE],
1743                   &priv->color[COLORSEL_SATURATION],
1744                   &priv->color[COLORSEL_VALUE]);
1745
1746   update_color (colorsel);
1747 }
1748
1749 static void
1750 shutdown_eyedropper (GtkWidget *widget)
1751 {
1752   GtkColorSelection *colorsel;
1753   GtkColorSelectionPrivate *priv;
1754
1755   colorsel = GTK_COLOR_SELECTION (widget);
1756   priv = colorsel->private_data;
1757
1758   if (priv->has_grab)
1759     {
1760       gdk_device_ungrab (priv->keyboard_device, priv->grab_time);
1761       gdk_device_ungrab (priv->pointer_device, priv->grab_time);
1762       gtk_device_grab_remove (priv->dropper_grab_widget, priv->pointer_device);
1763
1764       priv->has_grab = FALSE;
1765       priv->keyboard_device = NULL;
1766       priv->pointer_device = NULL;
1767     }
1768 }
1769
1770 static void
1771 mouse_motion (GtkWidget      *invisible,
1772               GdkEventMotion *event,
1773               gpointer        data)
1774 {
1775   grab_color_at_pointer (gdk_event_get_screen ((GdkEvent *) event),
1776                          gdk_event_get_device ((GdkEvent *) event),
1777                          event->x_root, event->y_root, data);
1778 }
1779
1780 static gboolean
1781 mouse_release (GtkWidget      *invisible,
1782                GdkEventButton *event,
1783                gpointer        data)
1784 {
1785   /* GtkColorSelection *colorsel = data; */
1786
1787   if (event->button != 1)
1788     return FALSE;
1789
1790   grab_color_at_pointer (gdk_event_get_screen ((GdkEvent *) event),
1791                          gdk_event_get_device ((GdkEvent *) event),
1792                          event->x_root, event->y_root, data);
1793
1794   shutdown_eyedropper (GTK_WIDGET (data));
1795
1796   g_signal_handlers_disconnect_by_func (invisible,
1797                                         mouse_motion,
1798                                         data);
1799   g_signal_handlers_disconnect_by_func (invisible,
1800                                         mouse_release,
1801                                         data);
1802
1803   return TRUE;
1804 }
1805
1806 /* Helper Functions */
1807
1808 static gboolean
1809 key_press (GtkWidget   *invisible,
1810            GdkEventKey *event,
1811            gpointer     data)
1812 {
1813   GdkScreen *screen = gdk_event_get_screen ((GdkEvent *) event);
1814   GdkDevice *device, *pointer_device;
1815   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
1816   gint x, y;
1817   gint dx, dy;
1818
1819   device = gdk_event_get_device ((GdkEvent * ) event);
1820   pointer_device = gdk_device_get_associated_device (device);
1821   gdk_device_get_position (pointer_device, NULL, &x, &y);
1822
1823   dx = 0;
1824   dy = 0;
1825
1826   switch (event->keyval)
1827     {
1828     case GDK_KEY_space:
1829     case GDK_KEY_Return:
1830     case GDK_KEY_ISO_Enter:
1831     case GDK_KEY_KP_Enter:
1832     case GDK_KEY_KP_Space:
1833       grab_color_at_pointer (screen, pointer_device, x, y, data);
1834       /* fall through */
1835
1836     case GDK_KEY_Escape:
1837       shutdown_eyedropper (data);
1838
1839       g_signal_handlers_disconnect_by_func (invisible,
1840                                             mouse_press,
1841                                             data);
1842       g_signal_handlers_disconnect_by_func (invisible,
1843                                             key_press,
1844                                             data);
1845
1846       return TRUE;
1847
1848     case GDK_KEY_Up:
1849     case GDK_KEY_KP_Up:
1850       dy = state == GDK_MOD1_MASK ? -BIG_STEP : -1;
1851       break;
1852
1853     case GDK_KEY_Down:
1854     case GDK_KEY_KP_Down:
1855       dy = state == GDK_MOD1_MASK ? BIG_STEP : 1;
1856       break;
1857
1858     case GDK_KEY_Left:
1859     case GDK_KEY_KP_Left:
1860       dx = state == GDK_MOD1_MASK ? -BIG_STEP : -1;
1861       break;
1862
1863     case GDK_KEY_Right:
1864     case GDK_KEY_KP_Right:
1865       dx = state == GDK_MOD1_MASK ? BIG_STEP : 1;
1866       break;
1867
1868     default:
1869       return FALSE;
1870     }
1871
1872   gdk_device_warp (pointer_device, screen, x + dx, y + dy);
1873
1874   return TRUE;
1875
1876 }
1877
1878 static gboolean
1879 mouse_press (GtkWidget      *invisible,
1880              GdkEventButton *event,
1881              gpointer        data)
1882 {
1883   if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1884     {
1885       g_signal_connect (invisible, "motion-notify-event",
1886                         G_CALLBACK (mouse_motion), data);
1887       g_signal_connect (invisible, "button-release-event",
1888                         G_CALLBACK (mouse_release), data);
1889       g_signal_handlers_disconnect_by_func (invisible,
1890                                             mouse_press,
1891                                             data);
1892       g_signal_handlers_disconnect_by_func (invisible,
1893                                             key_press,
1894                                             data);
1895       return TRUE;
1896     }
1897
1898   return FALSE;
1899 }
1900
1901 /* when the button is clicked */
1902 static void
1903 get_screen_color (GtkWidget *button)
1904 {
1905   GtkColorSelection *colorsel = g_object_get_data (G_OBJECT (button), "COLORSEL");
1906   GtkColorSelectionPrivate *priv = colorsel->private_data;
1907   GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (button));
1908   GdkDevice *device, *keyb_device, *pointer_device;
1909   GdkCursor *picker_cursor;
1910   GdkGrabStatus grab_status;
1911   GdkWindow *window;
1912   GtkWidget *grab_widget, *toplevel;
1913
1914   guint32 time = gtk_get_current_event_time ();
1915
1916   device = gtk_get_current_event_device ();
1917
1918   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1919     {
1920       keyb_device = device;
1921       pointer_device = gdk_device_get_associated_device (device);
1922     }
1923   else
1924     {
1925       pointer_device = device;
1926       keyb_device = gdk_device_get_associated_device (device);
1927     }
1928
1929   if (priv->dropper_grab_widget == NULL)
1930     {
1931       grab_widget = gtk_window_new (GTK_WINDOW_POPUP);
1932       gtk_window_set_screen (GTK_WINDOW (grab_widget), screen);
1933       gtk_window_resize (GTK_WINDOW (grab_widget), 1, 1);
1934       gtk_window_move (GTK_WINDOW (grab_widget), -100, -100);
1935       gtk_widget_show (grab_widget);
1936
1937       gtk_widget_add_events (grab_widget,
1938                              GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK);
1939
1940       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (colorsel));
1941
1942       if (GTK_IS_WINDOW (toplevel))
1943         {
1944           if (gtk_window_has_group (GTK_WINDOW (toplevel)))
1945             gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1946                                          GTK_WINDOW (grab_widget));
1947         }
1948
1949       priv->dropper_grab_widget = grab_widget;
1950     }
1951
1952   window = gtk_widget_get_window (priv->dropper_grab_widget);
1953
1954   if (gdk_device_grab (keyb_device,
1955                        window,
1956                        GDK_OWNERSHIP_APPLICATION, FALSE,
1957                        GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1958                        NULL, time) != GDK_GRAB_SUCCESS)
1959     return;
1960
1961   picker_cursor = make_picker_cursor (screen);
1962   grab_status = gdk_device_grab (pointer_device,
1963                                  window,
1964                                  GDK_OWNERSHIP_APPLICATION,
1965                                  FALSE,
1966                                  GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK,
1967                                  picker_cursor,
1968                                  time);
1969   g_object_unref (picker_cursor);
1970
1971   if (grab_status != GDK_GRAB_SUCCESS)
1972     {
1973       gdk_device_ungrab (keyb_device, time);
1974       return;
1975     }
1976
1977   gtk_device_grab_add (priv->dropper_grab_widget,
1978                        pointer_device,
1979                        TRUE);
1980
1981   priv->grab_time = time;
1982   priv->has_grab = TRUE;
1983   priv->keyboard_device = keyb_device;
1984   priv->pointer_device = pointer_device;
1985
1986   g_signal_connect (priv->dropper_grab_widget, "button-press-event",
1987                     G_CALLBACK (mouse_press), colorsel);
1988   g_signal_connect (priv->dropper_grab_widget, "key-press-event",
1989                     G_CALLBACK (key_press), colorsel);
1990 }
1991
1992 static void
1993 hex_changed (GtkWidget *hex_entry,
1994              gpointer   data)
1995 {
1996   GtkColorSelection *colorsel;
1997   GtkColorSelectionPrivate *priv;
1998   GdkColor color;
1999   gchar *text;
2000
2001   colorsel = GTK_COLOR_SELECTION (data);
2002   priv = colorsel->private_data;
2003
2004   if (priv->changing)
2005     return;
2006
2007   text = gtk_editable_get_chars (GTK_EDITABLE (priv->hex_entry), 0, -1);
2008   if (gdk_color_parse (text, &color))
2009     {
2010       priv->color[COLORSEL_RED]   = CLAMP (color.red   / 65535.0, 0.0, 1.0);
2011       priv->color[COLORSEL_GREEN] = CLAMP (color.green / 65535.0, 0.0, 1.0);
2012       priv->color[COLORSEL_BLUE]  = CLAMP (color.blue  / 65535.0, 0.0, 1.0);
2013       gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
2014                       priv->color[COLORSEL_GREEN],
2015                       priv->color[COLORSEL_BLUE],
2016                       &priv->color[COLORSEL_HUE],
2017                       &priv->color[COLORSEL_SATURATION],
2018                       &priv->color[COLORSEL_VALUE]);
2019       update_color (colorsel);
2020     }
2021   g_free (text);
2022 }
2023
2024 static gboolean
2025 hex_focus_out (GtkWidget     *hex_entry,
2026                GdkEventFocus *event,
2027                gpointer       data)
2028 {
2029   hex_changed (hex_entry, data);
2030
2031   return FALSE;
2032 }
2033
2034 static void
2035 hsv_changed (GtkWidget *hsv,
2036              gpointer   data)
2037 {
2038   GtkColorSelection *colorsel;
2039   GtkColorSelectionPrivate *priv;
2040
2041   colorsel = GTK_COLOR_SELECTION (data);
2042   priv = colorsel->private_data;
2043
2044   if (priv->changing)
2045     return;
2046
2047   gtk_hsv_get_color (GTK_HSV (hsv),
2048                      &priv->color[COLORSEL_HUE],
2049                      &priv->color[COLORSEL_SATURATION],
2050                      &priv->color[COLORSEL_VALUE]);
2051   gtk_hsv_to_rgb (priv->color[COLORSEL_HUE],
2052                   priv->color[COLORSEL_SATURATION],
2053                   priv->color[COLORSEL_VALUE],
2054                   &priv->color[COLORSEL_RED],
2055                   &priv->color[COLORSEL_GREEN],
2056                   &priv->color[COLORSEL_BLUE]);
2057   update_color (colorsel);
2058 }
2059
2060 static void
2061 adjustment_changed (GtkAdjustment *adjustment,
2062                     gpointer       data)
2063 {
2064   GtkColorSelection *colorsel;
2065   GtkColorSelectionPrivate *priv;
2066
2067   colorsel = GTK_COLOR_SELECTION (g_object_get_data (G_OBJECT (adjustment), "COLORSEL"));
2068   priv = colorsel->private_data;
2069
2070   if (priv->changing)
2071     return;
2072
2073   switch (GPOINTER_TO_INT (data))
2074     {
2075     case COLORSEL_SATURATION:
2076     case COLORSEL_VALUE:
2077       priv->color[GPOINTER_TO_INT (data)] = gtk_adjustment_get_value (adjustment) / 100;
2078       gtk_hsv_to_rgb (priv->color[COLORSEL_HUE],
2079                       priv->color[COLORSEL_SATURATION],
2080                       priv->color[COLORSEL_VALUE],
2081                       &priv->color[COLORSEL_RED],
2082                       &priv->color[COLORSEL_GREEN],
2083                       &priv->color[COLORSEL_BLUE]);
2084       break;
2085     case COLORSEL_HUE:
2086       priv->color[GPOINTER_TO_INT (data)] = gtk_adjustment_get_value (adjustment) / 360;
2087       gtk_hsv_to_rgb (priv->color[COLORSEL_HUE],
2088                       priv->color[COLORSEL_SATURATION],
2089                       priv->color[COLORSEL_VALUE],
2090                       &priv->color[COLORSEL_RED],
2091                       &priv->color[COLORSEL_GREEN],
2092                       &priv->color[COLORSEL_BLUE]);
2093       break;
2094     case COLORSEL_RED:
2095     case COLORSEL_GREEN:
2096     case COLORSEL_BLUE:
2097       priv->color[GPOINTER_TO_INT (data)] = gtk_adjustment_get_value (adjustment) / 255;
2098
2099       gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
2100                       priv->color[COLORSEL_GREEN],
2101                       priv->color[COLORSEL_BLUE],
2102                       &priv->color[COLORSEL_HUE],
2103                       &priv->color[COLORSEL_SATURATION],
2104                       &priv->color[COLORSEL_VALUE]);
2105       break;
2106     default:
2107       priv->color[GPOINTER_TO_INT (data)] = gtk_adjustment_get_value (adjustment) / 255;
2108       break;
2109     }
2110   update_color (colorsel);
2111 }
2112
2113 static void
2114 opacity_entry_changed (GtkWidget *opacity_entry,
2115                        gpointer   data)
2116 {
2117   GtkColorSelection *colorsel;
2118   GtkColorSelectionPrivate *priv;
2119   GtkAdjustment *adj;
2120   gchar *text;
2121
2122   colorsel = GTK_COLOR_SELECTION (data);
2123   priv = colorsel->private_data;
2124
2125   if (priv->changing)
2126     return;
2127
2128   text = gtk_editable_get_chars (GTK_EDITABLE (priv->opacity_entry), 0, -1);
2129   adj = gtk_range_get_adjustment (GTK_RANGE (priv->opacity_slider));
2130   gtk_adjustment_set_value (adj, g_strtod (text, NULL));
2131
2132   update_color (colorsel);
2133
2134   g_free (text);
2135 }
2136
2137 static void
2138 make_label_spinbutton (GtkColorSelection *colorsel,
2139                        GtkWidget        **spinbutton,
2140                        gchar             *text,
2141                        GtkWidget         *table,
2142                        gint               i,
2143                        gint               j,
2144                        gint               channel_type,
2145                        const gchar       *tooltip)
2146 {
2147   GtkWidget *label;
2148   GtkAdjustment *adjust;
2149
2150   if (channel_type == COLORSEL_HUE)
2151     {
2152       adjust = gtk_adjustment_new (0.0, 0.0, 360.0, 1.0, 1.0, 0.0);
2153     }
2154   else if (channel_type == COLORSEL_SATURATION ||
2155            channel_type == COLORSEL_VALUE)
2156     {
2157       adjust = gtk_adjustment_new (0.0, 0.0, 100.0, 1.0, 1.0, 0.0);
2158     }
2159   else
2160     {
2161       adjust = gtk_adjustment_new (0.0, 0.0, 255.0, 1.0, 1.0, 0.0);
2162     }
2163   g_object_set_data (G_OBJECT (adjust), I_("COLORSEL"), colorsel);
2164   *spinbutton = gtk_spin_button_new (adjust, 10.0, 0);
2165
2166   gtk_widget_set_tooltip_text (*spinbutton, tooltip);
2167
2168   g_signal_connect (adjust, "value-changed",
2169                     G_CALLBACK (adjustment_changed),
2170                     GINT_TO_POINTER (channel_type));
2171   label = gtk_label_new_with_mnemonic (text);
2172   gtk_label_set_mnemonic_widget (GTK_LABEL (label), *spinbutton);
2173
2174   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
2175   gtk_table_attach_defaults (GTK_TABLE (table), label, i, i+1, j, j+1);
2176   gtk_table_attach_defaults (GTK_TABLE (table), *spinbutton, i+1, i+2, j, j+1);
2177 }
2178
2179 static void
2180 make_palette_frame (GtkColorSelection *colorsel,
2181                     GtkWidget         *table,
2182                     gint               i,
2183                     gint               j)
2184 {
2185   GtkWidget *frame;
2186   GtkColorSelectionPrivate *priv;
2187
2188   priv = colorsel->private_data;
2189   frame = gtk_frame_new (NULL);
2190   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
2191   priv->custom_palette[i][j] = palette_new (colorsel);
2192   gtk_widget_set_size_request (priv->custom_palette[i][j], CUSTOM_PALETTE_ENTRY_WIDTH, CUSTOM_PALETTE_ENTRY_HEIGHT);
2193   gtk_container_add (GTK_CONTAINER (frame), priv->custom_palette[i][j]);
2194   gtk_table_attach_defaults (GTK_TABLE (table), frame, i, i+1, j, j+1);
2195 }
2196
2197 /* Set the palette entry [x][y] to be the currently selected one. */
2198 static void
2199 set_selected_palette (GtkColorSelection *colorsel, int x, int y)
2200 {
2201   GtkColorSelectionPrivate *priv = colorsel->private_data;
2202
2203   gtk_widget_grab_focus (priv->custom_palette[x][y]);
2204 }
2205
2206 static double
2207 scale_round (double val, double factor)
2208 {
2209   val = floor (val * factor + 0.5);
2210   val = MAX (val, 0);
2211   val = MIN (val, factor);
2212   return val;
2213 }
2214
2215 static void
2216 update_color (GtkColorSelection *colorsel)
2217 {
2218   GtkColorSelectionPrivate *priv = colorsel->private_data;
2219   gchar entryval[12];
2220   gchar opacity_text[32];
2221   gchar *ptr;
2222
2223   priv->changing = TRUE;
2224   color_sample_update_samples (colorsel);
2225
2226   gtk_hsv_set_color (GTK_HSV (priv->triangle_colorsel),
2227                      priv->color[COLORSEL_HUE],
2228                      priv->color[COLORSEL_SATURATION],
2229                      priv->color[COLORSEL_VALUE]);
2230   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
2231                             (GTK_SPIN_BUTTON (priv->hue_spinbutton)),
2232                             scale_round (priv->color[COLORSEL_HUE], 360));
2233   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
2234                             (GTK_SPIN_BUTTON (priv->sat_spinbutton)),
2235                             scale_round (priv->color[COLORSEL_SATURATION], 100));
2236   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
2237                             (GTK_SPIN_BUTTON (priv->val_spinbutton)),
2238                             scale_round (priv->color[COLORSEL_VALUE], 100));
2239   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
2240                             (GTK_SPIN_BUTTON (priv->red_spinbutton)),
2241                             scale_round (priv->color[COLORSEL_RED], 255));
2242   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
2243                             (GTK_SPIN_BUTTON (priv->green_spinbutton)),
2244                             scale_round (priv->color[COLORSEL_GREEN], 255));
2245   gtk_adjustment_set_value (gtk_spin_button_get_adjustment
2246                             (GTK_SPIN_BUTTON (priv->blue_spinbutton)),
2247                             scale_round (priv->color[COLORSEL_BLUE], 255));
2248   gtk_adjustment_set_value (gtk_range_get_adjustment
2249                             (GTK_RANGE (priv->opacity_slider)),
2250                             scale_round (priv->color[COLORSEL_OPACITY], 255));
2251
2252   g_snprintf (opacity_text, 32, "%.0f", scale_round (priv->color[COLORSEL_OPACITY], 255));
2253   gtk_entry_set_text (GTK_ENTRY (priv->opacity_entry), opacity_text);
2254
2255   g_snprintf (entryval, 11, "#%2X%2X%2X",
2256               (guint) (scale_round (priv->color[COLORSEL_RED], 255)),
2257               (guint) (scale_round (priv->color[COLORSEL_GREEN], 255)),
2258               (guint) (scale_round (priv->color[COLORSEL_BLUE], 255)));
2259
2260   for (ptr = entryval; *ptr; ptr++)
2261     if (*ptr == ' ')
2262       *ptr = '0';
2263   gtk_entry_set_text (GTK_ENTRY (priv->hex_entry), entryval);
2264   priv->changing = FALSE;
2265
2266   g_object_ref (colorsel);
2267
2268   g_signal_emit (colorsel, color_selection_signals[COLOR_CHANGED], 0);
2269
2270   g_object_freeze_notify (G_OBJECT (colorsel));
2271   g_object_notify (G_OBJECT (colorsel), "current-color");
2272   g_object_notify (G_OBJECT (colorsel), "current-alpha");
2273   g_object_thaw_notify (G_OBJECT (colorsel));
2274
2275   g_object_unref (colorsel);
2276 }
2277
2278 static void
2279 update_palette (GtkColorSelection *colorsel)
2280 {
2281   GdkColor *current_colors;
2282   gint i, j;
2283
2284   current_colors = get_current_colors (colorsel);
2285
2286   for (i = 0; i < GTK_CUSTOM_PALETTE_HEIGHT; i++)
2287     {
2288       for (j = 0; j < GTK_CUSTOM_PALETTE_WIDTH; j++)
2289         {
2290           gint index;
2291
2292           index = i * GTK_CUSTOM_PALETTE_WIDTH + j;
2293
2294           gtk_color_selection_set_palette_color (colorsel,
2295                                                  index,
2296                                                  &current_colors[index]);
2297         }
2298     }
2299
2300   g_free (current_colors);
2301 }
2302
2303 static void
2304 palette_change_notify_instance (GObject    *object,
2305                                 GParamSpec *pspec,
2306                                 gpointer    data)
2307 {
2308   update_palette (GTK_COLOR_SELECTION (data));
2309 }
2310
2311 static void
2312 default_noscreen_change_palette_func (const GdkColor *colors,
2313                                       gint            n_colors)
2314 {
2315   default_change_palette_func (gdk_screen_get_default (), colors, n_colors);
2316 }
2317
2318 static void
2319 default_change_palette_func (GdkScreen      *screen,
2320                              const GdkColor *colors,
2321                              gint            n_colors)
2322 {
2323   gchar *str;
2324
2325   str = gtk_color_selection_palette_to_string (colors, n_colors);
2326
2327   gtk_settings_set_string_property (gtk_settings_get_for_screen (screen),
2328                                     "gtk-color-palette",
2329                                     str,
2330                                     "gtk_color_selection_palette_to_string");
2331
2332   g_free (str);
2333 }
2334
2335 /**
2336  * gtk_color_selection_new:
2337  *
2338  * Creates a new GtkColorSelection.
2339  *
2340  * Return value: a new #GtkColorSelection
2341  */
2342 GtkWidget *
2343 gtk_color_selection_new (void)
2344 {
2345   GtkColorSelection *colorsel;
2346   GtkColorSelectionPrivate *priv;
2347   gdouble color[4];
2348   color[0] = 1.0;
2349   color[1] = 1.0;
2350   color[2] = 1.0;
2351   color[3] = 1.0;
2352
2353   colorsel = g_object_new (GTK_TYPE_COLOR_SELECTION, NULL);
2354   priv = colorsel->private_data;
2355   set_color_internal (colorsel, color);
2356   gtk_color_selection_set_has_opacity_control (colorsel, TRUE);
2357
2358   /* We want to make sure that default_set is FALSE.
2359    * This way the user can still set it.
2360    */
2361   priv->default_set = FALSE;
2362   priv->default_alpha_set = FALSE;
2363
2364   return GTK_WIDGET (colorsel);
2365 }
2366
2367 /**
2368  * gtk_color_selection_get_has_opacity_control:
2369  * @colorsel: a #GtkColorSelection
2370  *
2371  * Determines whether the colorsel has an opacity control.
2372  *
2373  * Return value: %TRUE if the @colorsel has an opacity control,
2374  *     %FALSE if it does't
2375  */
2376 gboolean
2377 gtk_color_selection_get_has_opacity_control (GtkColorSelection *colorsel)
2378 {
2379   GtkColorSelectionPrivate *priv;
2380
2381   g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), FALSE);
2382
2383   priv = colorsel->private_data;
2384
2385   return priv->has_opacity;
2386 }
2387
2388 /**
2389  * gtk_color_selection_set_has_opacity_control:
2390  * @colorsel: a #GtkColorSelection
2391  * @has_opacity: %TRUE if @colorsel can set the opacity, %FALSE otherwise
2392  *
2393  * Sets the @colorsel to use or not use opacity.
2394  */
2395 void
2396 gtk_color_selection_set_has_opacity_control (GtkColorSelection *colorsel,
2397                                              gboolean           has_opacity)
2398 {
2399   GtkColorSelectionPrivate *priv;
2400
2401   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2402
2403   priv = colorsel->private_data;
2404   has_opacity = has_opacity != FALSE;
2405
2406   if (priv->has_opacity != has_opacity)
2407     {
2408       priv->has_opacity = has_opacity;
2409       if (has_opacity)
2410         {
2411           gtk_widget_show (priv->opacity_slider);
2412           gtk_widget_show (priv->opacity_label);
2413           gtk_widget_show (priv->opacity_entry);
2414         }
2415       else
2416         {
2417           gtk_widget_hide (priv->opacity_slider);
2418           gtk_widget_hide (priv->opacity_label);
2419           gtk_widget_hide (priv->opacity_entry);
2420         }
2421       color_sample_update_samples (colorsel);
2422
2423       g_object_notify (G_OBJECT (colorsel), "has-opacity-control");
2424     }
2425 }
2426
2427 /**
2428  * gtk_color_selection_get_has_palette:
2429  * @colorsel: a #GtkColorSelection
2430  *
2431  * Determines whether the color selector has a color palette.
2432  *
2433  * Return value: %TRUE if the selector has a palette, %FALSE if it hasn't
2434  */
2435 gboolean
2436 gtk_color_selection_get_has_palette (GtkColorSelection *colorsel)
2437 {
2438   GtkColorSelectionPrivate *priv;
2439
2440   g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), FALSE);
2441
2442   priv = colorsel->private_data;
2443
2444   return priv->has_palette;
2445 }
2446
2447 /**
2448  * gtk_color_selection_set_has_palette:
2449  * @colorsel: a #GtkColorSelection
2450  * @has_palette: %TRUE if palette is to be visible, %FALSE otherwise
2451  *
2452  * Shows and hides the palette based upon the value of @has_palette.
2453  */
2454 void
2455 gtk_color_selection_set_has_palette (GtkColorSelection *colorsel,
2456                                      gboolean           has_palette)
2457 {
2458   GtkColorSelectionPrivate *priv;
2459   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2460
2461   priv = colorsel->private_data;
2462   has_palette = has_palette != FALSE;
2463
2464   if (priv->has_palette != has_palette)
2465     {
2466       priv->has_palette = has_palette;
2467       if (has_palette)
2468         gtk_widget_show (priv->palette_frame);
2469       else
2470         gtk_widget_hide (priv->palette_frame);
2471
2472       update_tooltips (colorsel);
2473
2474       g_object_notify (G_OBJECT (colorsel), "has-palette");
2475     }
2476 }
2477
2478 /**
2479  * gtk_color_selection_set_current_color:
2480  * @colorsel: a #GtkColorSelection
2481  * @color: a #GdkColor to set the current color with
2482  *
2483  * Sets the current color to be @color.
2484  *
2485  * The first time this is called, it will also set
2486  * the original color to be @color too.
2487  */
2488 void
2489 gtk_color_selection_set_current_color (GtkColorSelection *colorsel,
2490                                        const GdkColor    *color)
2491 {
2492   GtkColorSelectionPrivate *priv;
2493   gint i;
2494
2495   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2496   g_return_if_fail (color != NULL);
2497
2498   priv = colorsel->private_data;
2499   priv->changing = TRUE;
2500   priv->color[COLORSEL_RED] = SCALE (color->red);
2501   priv->color[COLORSEL_GREEN] = SCALE (color->green);
2502   priv->color[COLORSEL_BLUE] = SCALE (color->blue);
2503   gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
2504                   priv->color[COLORSEL_GREEN],
2505                   priv->color[COLORSEL_BLUE],
2506                   &priv->color[COLORSEL_HUE],
2507                   &priv->color[COLORSEL_SATURATION],
2508                   &priv->color[COLORSEL_VALUE]);
2509   if (priv->default_set == FALSE)
2510     {
2511       for (i = 0; i < COLORSEL_NUM_CHANNELS; i++)
2512         priv->old_color[i] = priv->color[i];
2513     }
2514   priv->default_set = TRUE;
2515   update_color (colorsel);
2516 }
2517
2518 /**
2519  * gtk_color_selection_set_current_alpha:
2520  * @colorsel: a #GtkColorSelection
2521  * @alpha: an integer between 0 and 65535
2522  *
2523  * Sets the current opacity to be @alpha.
2524  *
2525  * The first time this is called, it will also set
2526  * the original opacity to be @alpha too.
2527  */
2528 void
2529 gtk_color_selection_set_current_alpha (GtkColorSelection *colorsel,
2530                                        guint16            alpha)
2531 {
2532   GtkColorSelectionPrivate *priv;
2533   gint i;
2534
2535   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2536
2537   priv = colorsel->private_data;
2538   priv->changing = TRUE;
2539   priv->color[COLORSEL_OPACITY] = SCALE (alpha);
2540   if (priv->default_alpha_set == FALSE)
2541     {
2542       for (i = 0; i < COLORSEL_NUM_CHANNELS; i++)
2543         priv->old_color[i] = priv->color[i];
2544     }
2545   priv->default_alpha_set = TRUE;
2546   update_color (colorsel);
2547 }
2548
2549 /**
2550  * gtk_color_selection_get_current_color:
2551  * @colorsel: a #GtkColorSelection
2552  * @color: (out): a #GdkColor to fill in with the current color
2553  *
2554  * Sets @color to be the current color in the GtkColorSelection widget.
2555  */
2556 void
2557 gtk_color_selection_get_current_color (GtkColorSelection *colorsel,
2558                                        GdkColor          *color)
2559 {
2560   GtkColorSelectionPrivate *priv;
2561
2562   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2563   g_return_if_fail (color != NULL);
2564
2565   priv = colorsel->private_data;
2566   color->red = UNSCALE (priv->color[COLORSEL_RED]);
2567   color->green = UNSCALE (priv->color[COLORSEL_GREEN]);
2568   color->blue = UNSCALE (priv->color[COLORSEL_BLUE]);
2569 }
2570
2571 /**
2572  * gtk_color_selection_get_current_alpha:
2573  * @colorsel: a #GtkColorSelection
2574  *
2575  * Returns the current alpha value.
2576  *
2577  * Return value: an integer between 0 and 65535
2578  */
2579 guint16
2580 gtk_color_selection_get_current_alpha (GtkColorSelection *colorsel)
2581 {
2582   GtkColorSelectionPrivate *priv;
2583
2584   g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), 0);
2585
2586   priv = colorsel->private_data;
2587   return priv->has_opacity ? UNSCALE (priv->color[COLORSEL_OPACITY]) : 65535;
2588 }
2589
2590 /**
2591  * gtk_color_selection_set_previous_color:
2592  * @colorsel: a #GtkColorSelection
2593  * @color: a #GdkColor to set the previous color with
2594  *
2595  * Sets the 'previous' color to be @color.
2596  *
2597  * This function should be called with some hesitations,
2598  * as it might seem confusing to have that color change.
2599  * Calling gtk_color_selection_set_current_color() will also
2600  * set this color the first time it is called.
2601  */
2602 void
2603 gtk_color_selection_set_previous_color (GtkColorSelection *colorsel,
2604                                         const GdkColor    *color)
2605 {
2606   GtkColorSelectionPrivate *priv;
2607
2608   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2609   g_return_if_fail (color != NULL);
2610
2611   priv = colorsel->private_data;
2612   priv->changing = TRUE;
2613   priv->old_color[COLORSEL_RED] = SCALE (color->red);
2614   priv->old_color[COLORSEL_GREEN] = SCALE (color->green);
2615   priv->old_color[COLORSEL_BLUE] = SCALE (color->blue);
2616   gtk_rgb_to_hsv (priv->old_color[COLORSEL_RED],
2617                   priv->old_color[COLORSEL_GREEN],
2618                   priv->old_color[COLORSEL_BLUE],
2619                   &priv->old_color[COLORSEL_HUE],
2620                   &priv->old_color[COLORSEL_SATURATION],
2621                   &priv->old_color[COLORSEL_VALUE]);
2622   color_sample_update_samples (colorsel);
2623   priv->default_set = TRUE;
2624   priv->changing = FALSE;
2625 }
2626
2627 /**
2628  * gtk_color_selection_set_previous_alpha:
2629  * @colorsel: a #GtkColorSelection
2630  * @alpha: an integer between 0 and 65535
2631  *
2632  * Sets the 'previous' alpha to be @alpha.
2633  *
2634  * This function should be called with some hesitations,
2635  * as it might seem confusing to have that alpha change.
2636  */
2637 void
2638 gtk_color_selection_set_previous_alpha (GtkColorSelection *colorsel,
2639                                         guint16            alpha)
2640 {
2641   GtkColorSelectionPrivate *priv;
2642
2643   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2644
2645   priv = colorsel->private_data;
2646   priv->changing = TRUE;
2647   priv->old_color[COLORSEL_OPACITY] = SCALE (alpha);
2648   color_sample_update_samples (colorsel);
2649   priv->default_alpha_set = TRUE;
2650   priv->changing = FALSE;
2651 }
2652
2653
2654 /**
2655  * gtk_color_selection_get_previous_color:
2656  * @colorsel: a #GtkColorSelection
2657  * @color: (out): a #GdkColor to fill in with the original color value
2658  *
2659  * Fills @color in with the original color value.
2660  */
2661 void
2662 gtk_color_selection_get_previous_color (GtkColorSelection *colorsel,
2663                                         GdkColor           *color)
2664 {
2665   GtkColorSelectionPrivate *priv;
2666
2667   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2668   g_return_if_fail (color != NULL);
2669
2670   priv = colorsel->private_data;
2671   color->red = UNSCALE (priv->old_color[COLORSEL_RED]);
2672   color->green = UNSCALE (priv->old_color[COLORSEL_GREEN]);
2673   color->blue = UNSCALE (priv->old_color[COLORSEL_BLUE]);
2674 }
2675
2676 /**
2677  * gtk_color_selection_get_previous_alpha:
2678  * @colorsel: a #GtkColorSelection
2679  *
2680  * Returns the previous alpha value.
2681  *
2682  * Return value: an integer between 0 and 65535
2683  */
2684 guint16
2685 gtk_color_selection_get_previous_alpha (GtkColorSelection *colorsel)
2686 {
2687   GtkColorSelectionPrivate *priv;
2688
2689   g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), 0);
2690
2691   priv = colorsel->private_data;
2692   return priv->has_opacity ? UNSCALE (priv->old_color[COLORSEL_OPACITY]) : 65535;
2693 }
2694
2695 /**
2696  * gtk_color_selection_set_current_rgba:
2697  * @colorsel: a #GtkColorSelection
2698  * @rgba: A #GdkRGBA to set the current color with
2699  *
2700  * Sets the current color to be @rgba.
2701  *
2702  * The first time this is called, it will also set
2703  * the original color to be @rgba too.
2704  *
2705  * Since: 3.0
2706  */
2707 void
2708 gtk_color_selection_set_current_rgba (GtkColorSelection *colorsel,
2709                                       const GdkRGBA     *rgba)
2710 {
2711   GtkColorSelectionPrivate *priv;
2712   gint i;
2713
2714   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2715   g_return_if_fail (rgba != NULL);
2716
2717   priv = colorsel->private_data;
2718   priv->changing = TRUE;
2719
2720   priv->color[COLORSEL_RED] = CLAMP (rgba->red, 0, 1);
2721   priv->color[COLORSEL_GREEN] = CLAMP (rgba->green, 0, 1);
2722   priv->color[COLORSEL_BLUE] = CLAMP (rgba->blue, 0, 1);
2723   priv->color[COLORSEL_OPACITY] = CLAMP (rgba->alpha, 0, 1);
2724
2725   gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
2726                   priv->color[COLORSEL_GREEN],
2727                   priv->color[COLORSEL_BLUE],
2728                   &priv->color[COLORSEL_HUE],
2729                   &priv->color[COLORSEL_SATURATION],
2730                   &priv->color[COLORSEL_VALUE]);
2731
2732   if (priv->default_set == FALSE)
2733     {
2734       for (i = 0; i < COLORSEL_NUM_CHANNELS; i++)
2735         priv->old_color[i] = priv->color[i];
2736     }
2737
2738   priv->default_set = TRUE;
2739   update_color (colorsel);
2740 }
2741
2742 /**
2743  * gtk_color_selection_get_current_rgba:
2744  * @colorsel: a #GtkColorSelection
2745  * @rgba: (out): a #GdkRGBA to fill in with the current color
2746  *
2747  * Sets @rgba to be the current color in the GtkColorSelection widget.
2748  *
2749  * Since: 3.0
2750  */
2751 void
2752 gtk_color_selection_get_current_rgba (GtkColorSelection *colorsel,
2753                                       GdkRGBA           *rgba)
2754 {
2755   GtkColorSelectionPrivate *priv;
2756
2757   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2758   g_return_if_fail (rgba != NULL);
2759
2760   priv = colorsel->private_data;
2761   rgba->red = priv->color[COLORSEL_RED];
2762   rgba->green = priv->color[COLORSEL_GREEN];
2763   rgba->blue = priv->color[COLORSEL_BLUE];
2764   rgba->alpha = (priv->has_opacity) ? priv->color[COLORSEL_OPACITY] : 1;
2765 }
2766
2767 /**
2768  * gtk_color_selection_set_previous_rgba:
2769  * @colorsel: a #GtkColorSelection
2770  * @rgba: a #GdkRGBA to set the previous color with
2771  *
2772  * Sets the 'previous' color to be @rgba.
2773  *
2774  * This function should be called with some hesitations,
2775  * as it might seem confusing to have that color change.
2776  * Calling gtk_color_selection_set_current_rgba() will also
2777  * set this color the first time it is called.
2778  *
2779  * Since: 3.0
2780  */
2781 void
2782 gtk_color_selection_set_previous_rgba (GtkColorSelection *colorsel,
2783                                        const GdkRGBA     *rgba)
2784 {
2785   GtkColorSelectionPrivate *priv;
2786
2787   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2788   g_return_if_fail (rgba != NULL);
2789
2790   priv = colorsel->private_data;
2791   priv->changing = TRUE;
2792
2793   priv->old_color[COLORSEL_RED] = CLAMP (rgba->red, 0, 1);
2794   priv->old_color[COLORSEL_GREEN] = CLAMP (rgba->green, 0, 1);
2795   priv->old_color[COLORSEL_BLUE] = CLAMP (rgba->blue, 0, 1);
2796   priv->old_color[COLORSEL_OPACITY] = CLAMP (rgba->alpha, 0, 1);
2797
2798   gtk_rgb_to_hsv (priv->old_color[COLORSEL_RED],
2799                   priv->old_color[COLORSEL_GREEN],
2800                   priv->old_color[COLORSEL_BLUE],
2801                   &priv->old_color[COLORSEL_HUE],
2802                   &priv->old_color[COLORSEL_SATURATION],
2803                   &priv->old_color[COLORSEL_VALUE]);
2804
2805   color_sample_update_samples (colorsel);
2806   priv->default_set = TRUE;
2807   priv->changing = FALSE;
2808 }
2809
2810 /**
2811  * gtk_color_selection_get_previous_rgba:
2812  * @colorsel: a #GtkColorSelection
2813  * @rgba: (out): a #GdkRGBA to fill in with the original color value
2814  *
2815  * Fills @rgba in with the original color value.
2816  *
2817  * Since: 3.0
2818  */
2819 void
2820 gtk_color_selection_get_previous_rgba (GtkColorSelection *colorsel,
2821                                        GdkRGBA           *rgba)
2822 {
2823   GtkColorSelectionPrivate *priv;
2824
2825   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2826   g_return_if_fail (rgba != NULL);
2827
2828   priv = colorsel->private_data;
2829   rgba->red = priv->old_color[COLORSEL_RED];
2830   rgba->green = priv->old_color[COLORSEL_GREEN];
2831   rgba->blue = priv->old_color[COLORSEL_BLUE];
2832   rgba->alpha = (priv->has_opacity) ? priv->old_color[COLORSEL_OPACITY] : 1;
2833 }
2834
2835 /**
2836  * gtk_color_selection_set_palette_color:
2837  * @colorsel: a #GtkColorSelection
2838  * @index: the color index of the palette
2839  * @color: A #GdkColor to set the palette with
2840  *
2841  * Sets the palette located at @index to have @color as its color.
2842  */
2843 static void
2844 gtk_color_selection_set_palette_color (GtkColorSelection *colorsel,
2845                                        gint               index,
2846                                        GdkColor          *color)
2847 {
2848   GtkColorSelectionPrivate *priv;
2849   gint x, y;
2850   gdouble col[3];
2851
2852   g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
2853   g_return_if_fail (index >= 0  && index < GTK_CUSTOM_PALETTE_WIDTH*GTK_CUSTOM_PALETTE_HEIGHT);
2854
2855   x = index % GTK_CUSTOM_PALETTE_WIDTH;
2856   y = index / GTK_CUSTOM_PALETTE_WIDTH;
2857
2858   priv = colorsel->private_data;
2859   col[0] = SCALE (color->red);
2860   col[1] = SCALE (color->green);
2861   col[2] = SCALE (color->blue);
2862
2863   palette_set_color (priv->custom_palette[x][y], colorsel, col);
2864 }
2865
2866 /**
2867  * gtk_color_selection_is_adjusting:
2868  * @colorsel: a #GtkColorSelection
2869  *
2870  * Gets the current state of the @colorsel.
2871  *
2872  * Return value: %TRUE if the user is currently dragging
2873  *     a color around, and %FALSE if the selection has stopped
2874  */
2875 gboolean
2876 gtk_color_selection_is_adjusting (GtkColorSelection *colorsel)
2877 {
2878   GtkColorSelectionPrivate *priv;
2879
2880   g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), FALSE);
2881
2882   priv = colorsel->private_data;
2883
2884   return (gtk_hsv_is_adjusting (GTK_HSV (priv->triangle_colorsel)));
2885 }
2886
2887
2888 /**
2889  * gtk_color_selection_palette_from_string:
2890  * @str: a string encoding a color palette
2891  * @colors: (out) (array length=n_colors): return location for
2892  *     allocated array of #GdkColor
2893  * @n_colors: return location for length of array
2894  *
2895  * Parses a color palette string; the string is a colon-separated
2896  * list of color names readable by gdk_color_parse().
2897  *
2898  * Return value: %TRUE if a palette was successfully parsed
2899  */
2900 gboolean
2901 gtk_color_selection_palette_from_string (const gchar  *str,
2902                                          GdkColor    **colors,
2903                                          gint         *n_colors)
2904 {
2905   GdkColor *retval;
2906   gint count;
2907   gchar *p;
2908   gchar *start;
2909   gchar *copy;
2910
2911   count = 0;
2912   retval = NULL;
2913   copy = g_strdup (str);
2914
2915   start = copy;
2916   p = copy;
2917   while (TRUE)
2918     {
2919       if (*p == ':' || *p == '\0')
2920         {
2921           gboolean done = TRUE;
2922
2923           if (start == p)
2924             {
2925               goto failed; /* empty entry */
2926             }
2927
2928           if (*p)
2929             {
2930               *p = '\0';
2931               done = FALSE;
2932             }
2933
2934           retval = g_renew (GdkColor, retval, count + 1);
2935           if (!gdk_color_parse (start, retval + count))
2936             {
2937               goto failed;
2938             }
2939
2940           ++count;
2941
2942           if (done)
2943             break;
2944           else
2945             start = p + 1;
2946         }
2947
2948       ++p;
2949     }
2950
2951   g_free (copy);
2952
2953   if (colors)
2954     *colors = retval;
2955   else
2956     g_free (retval);
2957
2958   if (n_colors)
2959     *n_colors = count;
2960
2961   return TRUE;
2962
2963  failed:
2964   g_free (copy);
2965   g_free (retval);
2966
2967   if (colors)
2968     *colors = NULL;
2969   if (n_colors)
2970     *n_colors = 0;
2971
2972   return FALSE;
2973 }
2974
2975 /**
2976  * gtk_color_selection_palette_to_string:
2977  * @colors: (array length=n_colors): an array of colors
2978  * @n_colors: length of the array
2979  *
2980  * Encodes a palette as a string, useful for persistent storage.
2981  *
2982  * Return value: allocated string encoding the palette
2983  */
2984 gchar*
2985 gtk_color_selection_palette_to_string (const GdkColor *colors,
2986                                        gint            n_colors)
2987 {
2988   gint i;
2989   gchar **strs = NULL;
2990   gchar *retval;
2991
2992   if (n_colors == 0)
2993     return g_strdup ("");
2994
2995   strs = g_new0 (gchar*, n_colors + 1);
2996
2997   i = 0;
2998   while (i < n_colors)
2999     {
3000       gchar *ptr;
3001
3002       strs[i] =
3003         g_strdup_printf ("#%2X%2X%2X",
3004                          colors[i].red / 256,
3005                          colors[i].green / 256,
3006                          colors[i].blue / 256);
3007
3008       for (ptr = strs[i]; *ptr; ptr++)
3009         if (*ptr == ' ')
3010           *ptr = '0';
3011
3012       ++i;
3013     }
3014
3015   retval = g_strjoinv (":", strs);
3016
3017   g_strfreev (strs);
3018
3019   return retval;
3020 }
3021
3022 /**
3023  * gtk_color_selection_set_change_palette_with_screen_hook: (skip)
3024  * @func: a function to call when the custom palette needs saving
3025  *
3026  * Installs a global function to be called whenever the user
3027  * tries to modify the palette in a color selection.
3028  *
3029  * This function should save the new palette contents, and update
3030  * the #GtkSettings::gtk-color-palette GtkSettings property so all
3031  * GtkColorSelection widgets will be modified.
3032  *
3033  * Return value: the previous change palette hook (that was replaced)
3034  *
3035  * Since: 2.2
3036  */
3037 GtkColorSelectionChangePaletteWithScreenFunc
3038 gtk_color_selection_set_change_palette_with_screen_hook (GtkColorSelectionChangePaletteWithScreenFunc func)
3039 {
3040   GtkColorSelectionChangePaletteWithScreenFunc old;
3041
3042   old = change_palette_hook;
3043
3044   change_palette_hook = func;
3045
3046   return old;
3047 }
3048
3049 static void
3050 make_control_relations (AtkObject *atk_obj,
3051                         GtkWidget *widget)
3052 {
3053   AtkObject *obj;
3054
3055   obj = gtk_widget_get_accessible (widget);
3056   atk_object_add_relationship (atk_obj, ATK_RELATION_CONTROLLED_BY, obj);
3057   atk_object_add_relationship (obj, ATK_RELATION_CONTROLLER_FOR, atk_obj);
3058 }
3059
3060 static void
3061 make_all_relations (AtkObject                *atk_obj,
3062                     GtkColorSelectionPrivate *priv)
3063 {
3064   make_control_relations (atk_obj, priv->hue_spinbutton);
3065   make_control_relations (atk_obj, priv->sat_spinbutton);
3066   make_control_relations (atk_obj, priv->val_spinbutton);
3067   make_control_relations (atk_obj, priv->red_spinbutton);
3068   make_control_relations (atk_obj, priv->green_spinbutton);
3069   make_control_relations (atk_obj, priv->blue_spinbutton);
3070 }