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