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