]> Pileus Git - ~andy/gtk/blob - gtk/gtkcolorbutton.c
bee1bec58a32f255f3070cbde5e0cd81cb9ad2a2
[~andy/gtk] / gtk / gtkcolorbutton.c
1 /*
2  * GTK - The GIMP Toolkit
3  * Copyright (C) 1998, 1999 Red Hat, Inc.
4  * All rights reserved.
5  *
6  * This Library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This Library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with the Gnome Library; see the file COPYING.LIB.  If not,
18  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 /* Color picker button for GNOME
22  *
23  * Author: Federico Mena <federico@nuclecu.unam.mx>
24  *
25  * Modified by the GTK+ Team and others 2003.  See the AUTHORS
26  * file for a list of people on the GTK+ Team.  See the ChangeLog
27  * files for a list of changes.  These files are distributed with
28  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29  */
30
31 #include "config.h"
32
33 #include "gtkcolorbutton.h"
34
35 #include "gtkbutton.h"
36 #include "gtkmain.h"
37 #include "gtkalignment.h"
38 #include "gtkcolorsel.h"
39 #include "gtkcolorseldialog.h"
40 #include "gtkdnd.h"
41 #include "gtkdrawingarea.h"
42 #include "gtkframe.h"
43 #include "gtkmarshalers.h"
44 #include "gtkprivate.h"
45 #include "gtkintl.h"
46
47
48 /**
49  * SECTION:gtkcolorbutton
50  * @Short_description: A button to launch a color selection dialog
51  * @Title: GtkColorButton
52  * @See_also: #GtkColorSelectionDialog, #GtkFontButton
53  *
54  * The #GtkColorButton is a button which displays the currently selected
55  * color an allows to open a color selection dialog to change the color.
56  * It is suitable widget for selecting a color in a preference dialog.
57  */
58
59
60 /* Size of checks and gray levels for alpha compositing checkerboard */
61 #define CHECK_SIZE  4
62 #define CHECK_DARK  (1.0 / 3.0)
63 #define CHECK_LIGHT (2.0 / 3.0)
64
65
66 struct _GtkColorButtonPrivate
67 {
68   GtkWidget *draw_area; /* Widget where we draw the color sample */
69   GtkWidget *cs_dialog; /* Color selection dialog */
70
71   gchar *title;         /* Title for the color selection window */
72   GdkRGBA rgba;
73
74   guint use_alpha : 1;  /* Use alpha or not */
75 };
76
77 /* Properties */
78 enum
79 {
80   PROP_0,
81   PROP_USE_ALPHA,
82   PROP_TITLE,
83   PROP_COLOR,
84   PROP_ALPHA,
85   PROP_RGBA
86 };
87
88 /* Signals */
89 enum
90 {
91   COLOR_SET,
92   LAST_SIGNAL
93 };
94
95 /* gobject signals */
96 static void gtk_color_button_finalize      (GObject             *object);
97 static void gtk_color_button_set_property  (GObject        *object,
98                                             guint           param_id,
99                                             const GValue   *value,
100                                             GParamSpec     *pspec);
101 static void gtk_color_button_get_property  (GObject        *object,
102                                             guint           param_id,
103                                             GValue         *value,
104                                             GParamSpec     *pspec);
105
106 /* gtkwidget signals */
107 static void gtk_color_button_state_changed (GtkWidget           *widget,
108                                             GtkStateType         previous_state);
109
110 /* gtkbutton signals */
111 static void gtk_color_button_clicked       (GtkButton           *button);
112
113 /* source side drag signals */
114 static void gtk_color_button_drag_begin (GtkWidget        *widget,
115                                          GdkDragContext   *context,
116                                          gpointer          data);
117 static void gtk_color_button_drag_data_get (GtkWidget        *widget,
118                                             GdkDragContext   *context,
119                                             GtkSelectionData *selection_data,
120                                             guint             info,
121                                             guint             time,
122                                             GtkColorButton   *color_button);
123
124 /* target side drag signals */
125 static void gtk_color_button_drag_data_received (GtkWidget        *widget,
126                                                  GdkDragContext   *context,
127                                                  gint              x,
128                                                  gint              y,
129                                                  GtkSelectionData *selection_data,
130                                                  guint             info,
131                                                  guint32           time,
132                                                  GtkColorButton   *color_button);
133
134
135 static guint color_button_signals[LAST_SIGNAL] = { 0 };
136
137 static const GtkTargetEntry drop_types[] = { { "application/x-color", 0, 0 } };
138
139 G_DEFINE_TYPE (GtkColorButton, gtk_color_button, GTK_TYPE_BUTTON)
140
141 static void
142 gtk_color_button_class_init (GtkColorButtonClass *klass)
143 {
144   GObjectClass *gobject_class;
145   GtkWidgetClass *widget_class;
146   GtkButtonClass *button_class;
147
148   gobject_class = G_OBJECT_CLASS (klass);
149   widget_class = GTK_WIDGET_CLASS (klass);
150   button_class = GTK_BUTTON_CLASS (klass);
151
152   gobject_class->get_property = gtk_color_button_get_property;
153   gobject_class->set_property = gtk_color_button_set_property;
154   gobject_class->finalize = gtk_color_button_finalize;
155   widget_class->state_changed = gtk_color_button_state_changed;
156   button_class->clicked = gtk_color_button_clicked;
157   klass->color_set = NULL;
158
159   /**
160    * GtkColorButton:use-alpha:
161    *
162    * If this property is set to %TRUE, the color swatch on the button is rendered against a
163    * checkerboard background to show its opacity and the opacity slider is displayed in the
164    * color selection dialog.
165    *
166    * Since: 2.4
167    */
168   g_object_class_install_property (gobject_class,
169                                    PROP_USE_ALPHA,
170                                    g_param_spec_boolean ("use-alpha", P_("Use alpha"),
171                                                          P_("Whether to give the color an alpha value"),
172                                                          FALSE,
173                                                          GTK_PARAM_READWRITE));
174
175   /**
176    * GtkColorButton:title:
177    *
178    * The title of the color selection dialog
179    *
180    * Since: 2.4
181    */
182   g_object_class_install_property (gobject_class,
183                                    PROP_TITLE,
184                                    g_param_spec_string ("title",
185                                                         P_("Title"),
186                                                         P_("The title of the color selection dialog"),
187                                                         _("Pick a Color"),
188                                                         GTK_PARAM_READWRITE));
189
190   /**
191    * GtkColorButton:color:
192    *
193    * The selected color.
194    *
195    * Since: 2.4
196    *
197    * Deprecated: 3.4: Use #GtkColorButton:rgba instead.
198    */
199   g_object_class_install_property (gobject_class,
200                                    PROP_COLOR,
201                                    g_param_spec_boxed ("color",
202                                                        P_("Current Color"),
203                                                        P_("The selected color"),
204                                                        GDK_TYPE_COLOR,
205                                                        GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
206
207   /**
208    * GtkColorButton:alpha:
209    *
210    * The selected opacity value (0 fully transparent, 65535 fully opaque).
211    *
212    * Since: 2.4
213    */
214   g_object_class_install_property (gobject_class,
215                                    PROP_ALPHA,
216                                    g_param_spec_uint ("alpha",
217                                                       P_("Current Alpha"),
218                                                       P_("The selected opacity value (0 fully transparent, 65535 fully opaque)"),
219                                                       0, 65535, 65535,
220                                                       GTK_PARAM_READWRITE));
221
222   /**
223    * GtkColorButton:rgba:
224    *
225    * The RGBA color.
226    *
227    * Since: 3.0
228    */
229   g_object_class_install_property (gobject_class,
230                                    PROP_RGBA,
231                                    g_param_spec_boxed ("rgba",
232                                                        P_("Current RGBA Color"),
233                                                        P_("The selected RGBA color"),
234                                                        GDK_TYPE_RGBA,
235                                                        GTK_PARAM_READWRITE));
236
237
238   /**
239    * GtkColorButton::color-set:
240    * @widget: the object which received the signal.
241    *
242    * The ::color-set signal is emitted when the user selects a color.
243    * When handling this signal, use gtk_color_button_get_color() and
244    * gtk_color_button_get_alpha() (or gtk_color_button_get_rgba()) to
245    * find out which color was just selected.
246    *
247    * Note that this signal is only emitted when the <emphasis>user</emphasis>
248    * changes the color. If you need to react to programmatic color changes
249    * as well, use the notify::color signal.
250    *
251    * Since: 2.4
252    */
253   color_button_signals[COLOR_SET] = g_signal_new (I_("color-set"),
254                                                   G_TYPE_FROM_CLASS (gobject_class),
255                                                   G_SIGNAL_RUN_FIRST,
256                                                   G_STRUCT_OFFSET (GtkColorButtonClass, color_set),
257                                                   NULL, NULL,
258                                                   _gtk_marshal_VOID__VOID,
259                                                   G_TYPE_NONE, 0);
260
261   g_type_class_add_private (gobject_class, sizeof (GtkColorButtonPrivate));
262 }
263
264 static gboolean
265 gtk_color_button_has_alpha (GtkColorButton *color_button)
266 {
267   return color_button->priv->use_alpha &&
268       color_button->priv->rgba.alpha < 1;
269 }
270
271 static cairo_pattern_t *
272 gtk_color_button_get_checkered (void)
273 {
274   /* need to respect pixman's stride being a multiple of 4 */
275   static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
276                                    0x00, 0xFF, 0x00, 0x00 };
277   static cairo_surface_t *checkered = NULL;
278   cairo_pattern_t *pattern;
279
280   if (checkered == NULL)
281     {
282       checkered = cairo_image_surface_create_for_data (data,
283                                                        CAIRO_FORMAT_A8,
284                                                        2, 2, 4);
285     }
286
287   pattern = cairo_pattern_create_for_surface (checkered);
288   cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
289   cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
290
291   return pattern;
292 }
293
294 /* Handle exposure events for the color picker's drawing area */
295 static gint
296 gtk_color_button_draw_cb (GtkWidget *widget,
297                           cairo_t   *cr,
298                           gpointer   data)
299 {
300   GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
301   cairo_pattern_t *checkered;
302
303   if (gtk_color_button_has_alpha (color_button))
304     {
305       cairo_set_source_rgb (cr, CHECK_DARK, CHECK_DARK, CHECK_DARK);
306       cairo_paint (cr);
307
308       cairo_set_source_rgb (cr, CHECK_LIGHT, CHECK_LIGHT, CHECK_LIGHT);
309       cairo_scale (cr, CHECK_SIZE, CHECK_SIZE);
310
311       checkered = gtk_color_button_get_checkered ();
312       cairo_mask (cr, checkered);
313       cairo_pattern_destroy (checkered);
314
315       gdk_cairo_set_source_rgba (cr, &color_button->priv->rgba);
316     }
317   else
318     {
319       cairo_set_source_rgb (cr,
320                             color_button->priv->rgba.red,
321                             color_button->priv->rgba.green,
322                             color_button->priv->rgba.blue);
323     }
324
325   cairo_paint (cr);
326
327   if (!gtk_widget_is_sensitive (GTK_WIDGET (color_button)))
328     {
329       GtkStyleContext *context;
330       GdkRGBA color;
331
332       context = gtk_widget_get_style_context (widget);
333       gtk_style_context_get_background_color (context, GTK_STATE_FLAG_INSENSITIVE, &color);
334
335       gdk_cairo_set_source_rgba (cr, &color);
336       checkered = gtk_color_button_get_checkered ();
337       cairo_mask (cr, checkered);
338       cairo_pattern_destroy (checkered);
339     }
340
341   return FALSE;
342 }
343
344 static void
345 gtk_color_button_state_changed (GtkWidget   *widget,
346                                 GtkStateType previous_state)
347 {
348   gtk_widget_queue_draw (widget);
349 }
350
351 static void
352 gtk_color_button_drag_data_received (GtkWidget        *widget,
353                                      GdkDragContext   *context,
354                                      gint              x,
355                                      gint              y,
356                                      GtkSelectionData *selection_data,
357                                      guint             info,
358                                      guint32           time,
359                                      GtkColorButton   *color_button)
360 {
361   gint length;
362   guint16 *dropped;
363
364   length = gtk_selection_data_get_length (selection_data);
365
366   if (length < 0)
367     return;
368
369   /* We accept drops with the wrong format, since the KDE color
370    * chooser incorrectly drops application/x-color with format 8.
371    */
372   if (length != 8)
373     {
374       g_warning ("%s: Received invalid color data", G_STRFUNC);
375       return;
376     }
377
378
379   dropped = (guint16 *) gtk_selection_data_get_data (selection_data);
380
381   color_button->priv->rgba.red = dropped[0] / 65535.;
382   color_button->priv->rgba.green = dropped[1] / 65535.;
383   color_button->priv->rgba.blue = dropped[2] / 65535.;
384   color_button->priv->rgba.alpha = dropped[3] / 65535.;
385
386   gtk_widget_queue_draw (color_button->priv->draw_area);
387
388   g_signal_emit (color_button, color_button_signals[COLOR_SET], 0);
389
390   g_object_freeze_notify (G_OBJECT (color_button));
391   g_object_notify (G_OBJECT (color_button), "color");
392   g_object_notify (G_OBJECT (color_button), "alpha");
393   g_object_notify (G_OBJECT (color_button), "rgba");
394   g_object_thaw_notify (G_OBJECT (color_button));
395 }
396
397 static void
398 set_color_icon (GdkDragContext *context,
399                 GdkRGBA        *rgba)
400 {
401   cairo_surface_t *surface;
402   cairo_t *cr;
403
404   surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
405                                         48, 32);
406   cr = cairo_create (surface);
407
408   gdk_cairo_set_source_rgba (cr, rgba);
409   cairo_paint (cr);
410
411   gtk_drag_set_icon_surface (context, surface);
412
413   cairo_destroy (cr);
414   cairo_surface_destroy (surface);
415 }
416
417 static void
418 gtk_color_button_drag_begin (GtkWidget      *widget,
419                              GdkDragContext *context,
420                              gpointer        data)
421 {
422   GtkColorButton *color_button = data;
423
424   set_color_icon (context, &color_button->priv->rgba);
425 }
426
427 static void
428 gtk_color_button_drag_data_get (GtkWidget        *widget,
429                                 GdkDragContext   *context,
430                                 GtkSelectionData *selection_data,
431                                 guint             info,
432                                 guint             time,
433                                 GtkColorButton   *color_button)
434 {
435   guint16 dropped[4];
436
437   dropped[0] = (guint16) (color_button->priv->rgba.red * 65535);
438   dropped[1] = (guint16) (color_button->priv->rgba.green * 65535);
439   dropped[2] = (guint16) (color_button->priv->rgba.blue * 65535);
440   dropped[3] = (guint16) (color_button->priv->rgba.alpha * 65535);
441
442   gtk_selection_data_set (selection_data,
443                           gtk_selection_data_get_target (selection_data),
444                           16, (guchar *)dropped, 8);
445 }
446
447 static void
448 gtk_color_button_init (GtkColorButton *color_button)
449 {
450   GtkWidget *alignment;
451   GtkWidget *frame;
452   PangoLayout *layout;
453   PangoRectangle rect;
454
455   /* Create the widgets */
456   color_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (color_button,
457                                                     GTK_TYPE_COLOR_BUTTON,
458                                                     GtkColorButtonPrivate);
459
460   gtk_widget_push_composite_child ();
461
462   alignment = gtk_alignment_new (0.5, 0.5, 0.5, 1.0);
463   gtk_container_set_border_width (GTK_CONTAINER (alignment), 1);
464   gtk_container_add (GTK_CONTAINER (color_button), alignment);
465   gtk_widget_show (alignment);
466
467   frame = gtk_frame_new (NULL);
468   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_OUT);
469   gtk_container_add (GTK_CONTAINER (alignment), frame);
470   gtk_widget_show (frame);
471
472   /* Just some widget we can hook to expose-event on */
473   color_button->priv->draw_area = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
474
475   layout = gtk_widget_create_pango_layout (GTK_WIDGET (color_button), "Black");
476   pango_layout_get_pixel_extents (layout, NULL, &rect);
477   g_object_unref (layout);
478
479   gtk_widget_set_size_request (color_button->priv->draw_area, rect.width - 2, rect.height - 2);
480   g_signal_connect (color_button->priv->draw_area, "draw",
481                     G_CALLBACK (gtk_color_button_draw_cb), color_button);
482   gtk_container_add (GTK_CONTAINER (frame), color_button->priv->draw_area);
483   gtk_widget_show (color_button->priv->draw_area);
484
485   color_button->priv->title = g_strdup (_("Pick a Color")); /* default title */
486
487   /* Start with opaque black, alpha disabled */
488
489   color_button->priv->rgba.red = 0;
490   color_button->priv->rgba.green = 0;
491   color_button->priv->rgba.blue = 0;
492   color_button->priv->rgba.alpha = 1;
493   color_button->priv->use_alpha = FALSE;
494
495   gtk_drag_dest_set (GTK_WIDGET (color_button),
496                      GTK_DEST_DEFAULT_MOTION |
497                      GTK_DEST_DEFAULT_HIGHLIGHT |
498                      GTK_DEST_DEFAULT_DROP,
499                      drop_types, 1, GDK_ACTION_COPY);
500   gtk_drag_source_set (GTK_WIDGET(color_button),
501                        GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
502                        drop_types, 1,
503                        GDK_ACTION_COPY);
504   g_signal_connect (color_button, "drag-begin",
505                     G_CALLBACK (gtk_color_button_drag_begin), color_button);
506   g_signal_connect (color_button, "drag-data-received",
507                     G_CALLBACK (gtk_color_button_drag_data_received), color_button);
508   g_signal_connect (color_button, "drag-data-get",
509                     G_CALLBACK (gtk_color_button_drag_data_get), color_button);
510
511   gtk_widget_pop_composite_child ();
512 }
513
514 static void
515 gtk_color_button_finalize (GObject *object)
516 {
517   GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
518
519   if (color_button->priv->cs_dialog != NULL)
520     gtk_widget_destroy (color_button->priv->cs_dialog);
521   color_button->priv->cs_dialog = NULL;
522
523   g_free (color_button->priv->title);
524   color_button->priv->title = NULL;
525
526   G_OBJECT_CLASS (gtk_color_button_parent_class)->finalize (object);
527 }
528
529
530 /**
531  * gtk_color_button_new:
532  *
533  * Creates a new color button.
534  *
535  * This returns a widget in the form of a small button containing
536  * a swatch representing the current selected color. When the button
537  * is clicked, a color-selection dialog will open, allowing the user
538  * to select a color. The swatch will be updated to reflect the new
539  * color when the user finishes.
540  *
541  * Returns: a new color button
542  *
543  * Since: 2.4
544  */
545 GtkWidget *
546 gtk_color_button_new (void)
547 {
548   return g_object_new (GTK_TYPE_COLOR_BUTTON, NULL);
549 }
550
551 /**
552  * gtk_color_button_new_with_color:
553  * @color: A #GdkColor to set the current color with
554  *
555  * Creates a new color button.
556  *
557  * Returns: a new color button
558  *
559  * Since: 2.4
560  *
561  * Deprecated: 3.4: Use gtk_color_button_new_with_rgba() instead.
562  */
563 GtkWidget *
564 gtk_color_button_new_with_color (const GdkColor *color)
565 {
566   return g_object_new (GTK_TYPE_COLOR_BUTTON, "color", color, NULL);
567 }
568
569 /**
570  * gtk_color_button_new_with_rgba:
571  * @rgba: A #GdkRGBA to set the current color with
572  *
573  * Creates a new color button.
574  *
575  * Returns: a new color button
576  *
577  * Since: 3.0
578  */
579 GtkWidget *
580 gtk_color_button_new_with_rgba (const GdkRGBA *rgba)
581 {
582   return g_object_new (GTK_TYPE_COLOR_BUTTON, "rgba", rgba, NULL);
583 }
584
585 static void
586 dialog_ok_clicked (GtkWidget *widget,
587                    gpointer   data)
588 {
589   GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
590   GtkColorSelection *color_selection;
591   GtkColorSelectionDialog *selection_dialog;
592
593   selection_dialog = GTK_COLOR_SELECTION_DIALOG (color_button->priv->cs_dialog);
594   color_selection = GTK_COLOR_SELECTION (gtk_color_selection_dialog_get_color_selection (selection_dialog));
595
596   gtk_color_selection_get_current_rgba (color_selection, &color_button->priv->rgba);
597
598   gtk_widget_hide (color_button->priv->cs_dialog);
599
600   gtk_widget_queue_draw (color_button->priv->draw_area);
601
602   g_signal_emit (color_button, color_button_signals[COLOR_SET], 0);
603
604   g_object_freeze_notify (G_OBJECT (color_button));
605   g_object_notify (G_OBJECT (color_button), "color");
606   g_object_notify (G_OBJECT (color_button), "alpha");
607   g_object_notify (G_OBJECT (color_button), "rgba");
608   g_object_thaw_notify (G_OBJECT (color_button));
609 }
610
611 static gboolean
612 dialog_destroy (GtkWidget *widget,
613                 gpointer   data)
614 {
615   GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
616
617   color_button->priv->cs_dialog = NULL;
618
619   return FALSE;
620 }
621
622 static void
623 dialog_cancel_clicked (GtkWidget *widget,
624                        gpointer   data)
625 {
626   GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
627
628   gtk_widget_hide (color_button->priv->cs_dialog);
629 }
630
631 static void
632 gtk_color_button_clicked (GtkButton *button)
633 {
634   GtkColorButton *color_button = GTK_COLOR_BUTTON (button);
635   GtkColorSelection *color_selection;
636   GtkColorSelectionDialog *color_dialog;
637
638   /* if dialog already exists, make sure it's shown and raised */
639   if (!color_button->priv->cs_dialog)
640     {
641       /* Create the dialog and connects its buttons */
642       GtkWidget *parent;
643       GtkWidget *ok_button, *cancel_button;
644
645       parent = gtk_widget_get_toplevel (GTK_WIDGET (color_button));
646
647       color_button->priv->cs_dialog = gtk_color_selection_dialog_new (color_button->priv->title);
648
649       color_dialog = GTK_COLOR_SELECTION_DIALOG (color_button->priv->cs_dialog);
650
651       if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
652         {
653           if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (color_dialog)))
654             gtk_window_set_transient_for (GTK_WINDOW (color_dialog), GTK_WINDOW (parent));
655
656           gtk_window_set_modal (GTK_WINDOW (color_dialog),
657                                 gtk_window_get_modal (GTK_WINDOW (parent)));
658         }
659
660       g_object_get (color_dialog,
661                     "ok-button", &ok_button,
662                     "cancel-button", &cancel_button,
663                     NULL);
664
665       g_signal_connect (ok_button, "clicked",
666                         G_CALLBACK (dialog_ok_clicked), color_button);
667       g_signal_connect (cancel_button, "clicked",
668                         G_CALLBACK (dialog_cancel_clicked), color_button);
669       g_signal_connect (color_dialog, "destroy",
670                         G_CALLBACK (dialog_destroy), color_button);
671
672       g_object_unref (ok_button);
673       g_object_unref (cancel_button);
674     }
675
676   color_dialog = GTK_COLOR_SELECTION_DIALOG (color_button->priv->cs_dialog);
677   color_selection = GTK_COLOR_SELECTION (gtk_color_selection_dialog_get_color_selection (color_dialog));
678
679   gtk_color_selection_set_has_opacity_control (color_selection,
680                                                color_button->priv->use_alpha);
681
682   gtk_color_selection_set_previous_rgba (color_selection,
683                                          &color_button->priv->rgba);
684   gtk_color_selection_set_current_rgba (color_selection,
685                                         &color_button->priv->rgba);
686
687   gtk_window_present (GTK_WINDOW (color_button->priv->cs_dialog));
688 }
689
690 /**
691  * gtk_color_button_set_color:
692  * @color_button: a #GtkColorButton
693  * @color: A #GdkColor to set the current color with
694  *
695  * Sets the current color to be @color.
696  *
697  * Since: 2.4
698  *
699  * Deprecated: Use gtk_color_button_set_rgba() instead.
700  */
701 void
702 gtk_color_button_set_color (GtkColorButton *color_button,
703                             const GdkColor *color)
704 {
705   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
706   g_return_if_fail (color != NULL);
707
708   color_button->priv->rgba.red = color->red / 65535.;
709   color_button->priv->rgba.green = color->green / 65535.;
710   color_button->priv->rgba.blue = color->blue / 65535.;
711
712   gtk_widget_queue_draw (color_button->priv->draw_area);
713
714   g_object_notify (G_OBJECT (color_button), "color");
715   g_object_notify (G_OBJECT (color_button), "rgba");
716 }
717
718
719 /**
720  * gtk_color_button_set_alpha:
721  * @color_button: a #GtkColorButton
722  * @alpha: an integer between 0 and 65535
723  *
724  * Sets the current opacity to be @alpha.
725  *
726  * Since: 2.4
727  */
728 void
729 gtk_color_button_set_alpha (GtkColorButton *color_button,
730                             guint16         alpha)
731 {
732   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
733
734   color_button->priv->rgba.alpha = alpha / 65535.;
735
736   gtk_widget_queue_draw (color_button->priv->draw_area);
737
738   g_object_notify (G_OBJECT (color_button), "alpha");
739   g_object_notify (G_OBJECT (color_button), "rgba");
740 }
741
742 /**
743  * gtk_color_button_get_color:
744  * @color_button: a #GtkColorButton
745  * @color: (out): a #GdkColor to fill in with the current color
746  *
747  * Sets @color to be the current color in the #GtkColorButton widget.
748  *
749  * Since: 2.4
750  *
751  * Deprecated: 3.4: Use gtk_color_button_get_rgba() instead.
752  */
753 void
754 gtk_color_button_get_color (GtkColorButton *color_button,
755                             GdkColor       *color)
756 {
757   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
758
759   color->red = (guint16) (color_button->priv->rgba.red * 65535);
760   color->green = (guint16) (color_button->priv->rgba.green * 65535);
761   color->blue = (guint16) (color_button->priv->rgba.blue * 65535);
762 }
763
764 /**
765  * gtk_color_button_get_alpha:
766  * @color_button: a #GtkColorButton
767  *
768  * Returns the current alpha value.
769  *
770  * Return value: an integer between 0 and 65535
771  *
772  * Since: 2.4
773  */
774 guint16
775 gtk_color_button_get_alpha (GtkColorButton *color_button)
776 {
777   g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), 0);
778
779   return (guint16) (color_button->priv->rgba.alpha * 65535);
780 }
781
782 /**
783  * gtk_color_button_set_rgba:
784  * @color_button: a #GtkColorButton
785  * @rgba: a #GdkRGBA to set the current color with
786  *
787  * Sets the current color to be @rgba.
788  *
789  * Since: 3.0
790  */
791 void
792 gtk_color_button_set_rgba (GtkColorButton *color_button,
793                            const GdkRGBA  *rgba)
794 {
795   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
796   g_return_if_fail (rgba != NULL);
797
798   color_button->priv->rgba = *rgba;
799
800   gtk_widget_queue_draw (color_button->priv->draw_area);
801
802   g_object_notify (G_OBJECT (color_button), "color");
803   g_object_notify (G_OBJECT (color_button), "alpha");
804   g_object_notify (G_OBJECT (color_button), "rgba");
805 }
806
807 /**
808  * gtk_color_button_get_rgba:
809  * @color_button: a #GtkColorButton
810  * @rgba: (out): a #GdkRGBA to fill in with the current color
811  *
812  * Sets @rgba to be the current color in the #GtkColorButton widget.
813  *
814  * Since: 3.0
815  */
816 void
817 gtk_color_button_get_rgba (GtkColorButton *color_button,
818                            GdkRGBA        *rgba)
819 {
820   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
821   g_return_if_fail (rgba != NULL);
822
823   *rgba = color_button->priv->rgba;
824 }
825
826 /**
827  * gtk_color_button_set_use_alpha:
828  * @color_button: a #GtkColorButton
829  * @use_alpha: %TRUE if color button should use alpha channel, %FALSE if not
830  *
831  * Sets whether or not the color button should use the alpha channel.
832  *
833  * Since: 2.4
834  */
835 void
836 gtk_color_button_set_use_alpha (GtkColorButton *color_button,
837                                 gboolean        use_alpha)
838 {
839   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
840
841   use_alpha = (use_alpha != FALSE);
842
843   if (color_button->priv->use_alpha != use_alpha)
844     {
845       color_button->priv->use_alpha = use_alpha;
846
847       gtk_widget_queue_draw (color_button->priv->draw_area);
848
849       g_object_notify (G_OBJECT (color_button), "use-alpha");
850     }
851 }
852
853 /**
854  * gtk_color_button_get_use_alpha:
855  * @color_button: a #GtkColorButton
856  *
857  * Does the color selection dialog use the alpha channel ?
858  *
859  * Returns: %TRUE if the color sample uses alpha channel, %FALSE if not
860  *
861  * Since: 2.4
862  */
863 gboolean
864 gtk_color_button_get_use_alpha (GtkColorButton *color_button)
865 {
866   g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), FALSE);
867
868   return color_button->priv->use_alpha;
869 }
870
871
872 /**
873  * gtk_color_button_set_title:
874  * @color_button: a #GtkColorButton
875  * @title: String containing new window title
876  *
877  * Sets the title for the color selection dialog.
878  *
879  * Since: 2.4
880  */
881 void
882 gtk_color_button_set_title (GtkColorButton *color_button,
883                             const gchar    *title)
884 {
885   gchar *old_title;
886
887   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
888
889   old_title = color_button->priv->title;
890   color_button->priv->title = g_strdup (title);
891   g_free (old_title);
892
893   if (color_button->priv->cs_dialog)
894     gtk_window_set_title (GTK_WINDOW (color_button->priv->cs_dialog),
895                           color_button->priv->title);
896
897   g_object_notify (G_OBJECT (color_button), "title");
898 }
899
900 /**
901  * gtk_color_button_get_title:
902  * @color_button: a #GtkColorButton
903  *
904  * Gets the title of the color selection dialog.
905  *
906  * Returns: An internal string, do not free the return value
907  *
908  * Since: 2.4
909  */
910 const gchar *
911 gtk_color_button_get_title (GtkColorButton *color_button)
912 {
913   g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), NULL);
914
915   return color_button->priv->title;
916 }
917
918 static void
919 gtk_color_button_set_property (GObject      *object,
920                                guint         param_id,
921                                const GValue *value,
922                                GParamSpec   *pspec)
923 {
924   GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
925
926   switch (param_id)
927     {
928     case PROP_USE_ALPHA:
929       gtk_color_button_set_use_alpha (color_button, g_value_get_boolean (value));
930       break;
931     case PROP_TITLE:
932       gtk_color_button_set_title (color_button, g_value_get_string (value));
933       break;
934     case PROP_COLOR:
935       {
936         GdkColor *color;
937         GdkRGBA rgba;
938
939         color = g_value_get_boxed (value);
940
941         rgba.red = color->red / 65535.0;
942         rgba.green = color->green / 65535.0;
943         rgba.blue = color->blue / 65535.0;
944         rgba.alpha = 1.0;
945
946         gtk_color_button_set_rgba (color_button, &rgba);
947       }
948       break;
949     case PROP_ALPHA:
950       gtk_color_button_set_alpha (color_button, g_value_get_uint (value));
951       break;
952     case PROP_RGBA:
953       gtk_color_button_set_rgba (color_button, g_value_get_boxed (value));
954       break;
955     default:
956       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
957       break;
958     }
959 }
960
961 static void
962 gtk_color_button_get_property (GObject    *object,
963                                guint       param_id,
964                                GValue     *value,
965                                GParamSpec *pspec)
966 {
967   GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
968
969   switch (param_id)
970     {
971     case PROP_USE_ALPHA:
972       g_value_set_boolean (value, gtk_color_button_get_use_alpha (color_button));
973       break;
974     case PROP_TITLE:
975       g_value_set_string (value, gtk_color_button_get_title (color_button));
976       break;
977     case PROP_COLOR:
978       {
979         GdkColor color;
980         GdkRGBA rgba;
981
982         gtk_color_button_get_rgba (color_button, &rgba);
983
984         color.red = (guint16) (rgba.red * 65535 + 0.5);
985         color.green = (guint16) (rgba.green * 65535 + 0.5);
986         color.blue = (guint16) (rgba.blue * 65535 + 0.5);
987
988         g_value_set_boxed (value, &color);
989       }
990       break;
991     case PROP_ALPHA:
992       g_value_set_uint (value, gtk_color_button_get_alpha (color_button));
993       break;
994     case PROP_RGBA:
995       {
996         GdkRGBA rgba;
997
998         gtk_color_button_get_rgba (color_button, &rgba);
999         g_value_set_boxed (value, &rgba);
1000       }
1001       break;
1002     default:
1003       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1004       break;
1005     }
1006 }