]> Pileus Git - ~andy/gtk/blob - gtk/gtkcolorbutton.c
Fixing GtkColorButton to redraw itself when gtk_color_button_set_rgba() is called.
[~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   g_object_class_install_property (gobject_class,
198                                    PROP_COLOR,
199                                    g_param_spec_boxed ("color",
200                                                        P_("Current Color"),
201                                                        P_("The selected color"),
202                                                        GDK_TYPE_COLOR,
203                                                        GTK_PARAM_READWRITE));
204
205   /**
206    * GtkColorButton:alpha:
207    *
208    * The selected opacity value (0 fully transparent, 65535 fully opaque).
209    *
210    * Since: 2.4
211    */
212   g_object_class_install_property (gobject_class,
213                                    PROP_ALPHA,
214                                    g_param_spec_uint ("alpha",
215                                                       P_("Current Alpha"),
216                                                       P_("The selected opacity value (0 fully transparent, 65535 fully opaque)"),
217                                                       0, 65535, 65535,
218                                                       GTK_PARAM_READWRITE));
219
220   /**
221    * GtkColorButton:rgba:
222    *
223    * The RGBA color.
224    *
225    * Since: 3.0
226    */
227   g_object_class_install_property (gobject_class,
228                                    PROP_RGBA,
229                                    g_param_spec_boxed ("rgba",
230                                                        P_("Current RGBA Color"),
231                                                        P_("The selected RGBA color"),
232                                                        GDK_TYPE_RGBA,
233                                                        GTK_PARAM_READWRITE));
234
235
236   /**
237    * GtkColorButton::color-set:
238    * @widget: the object which received the signal.
239    *
240    * The ::color-set signal is emitted when the user selects a color.
241    * When handling this signal, use gtk_color_button_get_color() and
242    * gtk_color_button_get_alpha() (or gtk_color_button_get_rgba()) to
243    * find out which color was just selected.
244    *
245    * Note that this signal is only emitted when the <emphasis>user</emphasis>
246    * changes the color. If you need to react to programmatic color changes
247    * as well, use the notify::color signal.
248    *
249    * Since: 2.4
250    */
251   color_button_signals[COLOR_SET] = g_signal_new (I_("color-set"),
252                                                   G_TYPE_FROM_CLASS (gobject_class),
253                                                   G_SIGNAL_RUN_FIRST,
254                                                   G_STRUCT_OFFSET (GtkColorButtonClass, color_set),
255                                                   NULL, NULL,
256                                                   _gtk_marshal_VOID__VOID,
257                                                   G_TYPE_NONE, 0);
258
259   g_type_class_add_private (gobject_class, sizeof (GtkColorButtonPrivate));
260 }
261
262 static gboolean
263 gtk_color_button_has_alpha (GtkColorButton *color_button)
264 {
265   return color_button->priv->use_alpha &&
266       color_button->priv->rgba.alpha < 1;
267 }
268
269 static cairo_pattern_t *
270 gtk_color_button_get_checkered (void)
271 {
272   /* need to respect pixman's stride being a multiple of 4 */
273   static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
274                                    0x00, 0xFF, 0x00, 0x00 };
275   static cairo_surface_t *checkered = NULL;
276   cairo_pattern_t *pattern;
277
278   if (checkered == NULL)
279     {
280       checkered = cairo_image_surface_create_for_data (data,
281                                                        CAIRO_FORMAT_A8,
282                                                        2, 2, 4);
283     }
284
285   pattern = cairo_pattern_create_for_surface (checkered);
286   cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
287   cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
288
289   return pattern;
290 }
291
292 /* Handle exposure events for the color picker's drawing area */
293 static gint
294 gtk_color_button_draw_cb (GtkWidget *widget,
295                           cairo_t   *cr,
296                           gpointer   data)
297 {
298   GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
299   cairo_pattern_t *checkered;
300
301   if (gtk_color_button_has_alpha (color_button))
302     {
303       cairo_set_source_rgb (cr, CHECK_DARK, CHECK_DARK, CHECK_DARK);
304       cairo_paint (cr);
305
306       cairo_set_source_rgb (cr, CHECK_LIGHT, CHECK_LIGHT, CHECK_LIGHT);
307       cairo_scale (cr, CHECK_SIZE, CHECK_SIZE);
308
309       checkered = gtk_color_button_get_checkered ();
310       cairo_mask (cr, checkered);
311       cairo_pattern_destroy (checkered);
312
313       gdk_cairo_set_source_rgba (cr, &color_button->priv->rgba);
314     }
315   else
316     {
317       cairo_set_source_rgb (cr,
318                             color_button->priv->rgba.red,
319                             color_button->priv->rgba.green,
320                             color_button->priv->rgba.blue);
321     }
322
323   cairo_paint (cr);
324
325   if (!gtk_widget_is_sensitive (GTK_WIDGET (color_button)))
326     {
327       GtkStyleContext *context;
328       GdkRGBA color;
329
330       context = gtk_widget_get_style_context (widget);
331       gtk_style_context_get_background_color (context, GTK_STATE_FLAG_INSENSITIVE, &color);
332
333       gdk_cairo_set_source_rgba (cr, &color);
334       checkered = gtk_color_button_get_checkered ();
335       cairo_mask (cr, checkered);
336       cairo_pattern_destroy (checkered);
337     }
338
339   return FALSE;
340 }
341
342 static void
343 gtk_color_button_state_changed (GtkWidget   *widget,
344                                 GtkStateType previous_state)
345 {
346   gtk_widget_queue_draw (widget);
347 }
348
349 static void
350 gtk_color_button_drag_data_received (GtkWidget        *widget,
351                                      GdkDragContext   *context,
352                                      gint              x,
353                                      gint              y,
354                                      GtkSelectionData *selection_data,
355                                      guint             info,
356                                      guint32           time,
357                                      GtkColorButton   *color_button)
358 {
359   gint length;
360   guint16 *dropped;
361
362   length = gtk_selection_data_get_length (selection_data);
363
364   if (length < 0)
365     return;
366
367   /* We accept drops with the wrong format, since the KDE color
368    * chooser incorrectly drops application/x-color with format 8.
369    */
370   if (length != 8)
371     {
372       g_warning (_("Received invalid color data\n"));
373       return;
374     }
375
376
377   dropped = (guint16 *) gtk_selection_data_get_data (selection_data);
378
379   color_button->priv->rgba.red = dropped[0] / 65535.;
380   color_button->priv->rgba.green = dropped[1] / 65535.;
381   color_button->priv->rgba.blue = dropped[2] / 65535.;
382   color_button->priv->rgba.alpha = dropped[3] / 65535.;
383
384   gtk_widget_queue_draw (color_button->priv->draw_area);
385
386   g_signal_emit (color_button, color_button_signals[COLOR_SET], 0);
387
388   g_object_freeze_notify (G_OBJECT (color_button));
389   g_object_notify (G_OBJECT (color_button), "color");
390   g_object_notify (G_OBJECT (color_button), "alpha");
391   g_object_notify (G_OBJECT (color_button), "rgba");
392   g_object_thaw_notify (G_OBJECT (color_button));
393 }
394
395 static void
396 set_color_icon (GdkDragContext *context,
397                 GdkRGBA        *rgba)
398 {
399   cairo_surface_t *surface;
400   cairo_t *cr;
401
402   surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
403                                         48, 32);
404   cr = cairo_create (surface);
405
406   gdk_cairo_set_source_rgba (cr, rgba);
407   cairo_paint (cr);
408
409   gtk_drag_set_icon_surface (context, surface);
410
411   cairo_destroy (cr);
412   cairo_surface_destroy (surface);
413 }
414
415 static void
416 gtk_color_button_drag_begin (GtkWidget      *widget,
417                              GdkDragContext *context,
418                              gpointer        data)
419 {
420   GtkColorButton *color_button = data;
421
422   set_color_icon (context, &color_button->priv->rgba);
423 }
424
425 static void
426 gtk_color_button_drag_data_get (GtkWidget        *widget,
427                                 GdkDragContext   *context,
428                                 GtkSelectionData *selection_data,
429                                 guint             info,
430                                 guint             time,
431                                 GtkColorButton   *color_button)
432 {
433   guint16 dropped[4];
434
435   dropped[0] = (guint16) (color_button->priv->rgba.red * 65535);
436   dropped[1] = (guint16) (color_button->priv->rgba.green * 65535);
437   dropped[2] = (guint16) (color_button->priv->rgba.blue * 65535);
438   dropped[3] = (guint16) (color_button->priv->rgba.alpha * 65535);
439
440   gtk_selection_data_set (selection_data,
441                           gtk_selection_data_get_target (selection_data),
442                           16, (guchar *)dropped, 8);
443 }
444
445 static void
446 gtk_color_button_init (GtkColorButton *color_button)
447 {
448   GtkWidget *alignment;
449   GtkWidget *frame;
450   PangoLayout *layout;
451   PangoRectangle rect;
452
453   /* Create the widgets */
454   color_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (color_button,
455                                                     GTK_TYPE_COLOR_BUTTON,
456                                                     GtkColorButtonPrivate);
457
458   gtk_widget_push_composite_child ();
459
460   alignment = gtk_alignment_new (0.5, 0.5, 0.5, 1.0);
461   gtk_container_set_border_width (GTK_CONTAINER (alignment), 1);
462   gtk_container_add (GTK_CONTAINER (color_button), alignment);
463   gtk_widget_show (alignment);
464
465   frame = gtk_frame_new (NULL);
466   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_OUT);
467   gtk_container_add (GTK_CONTAINER (alignment), frame);
468   gtk_widget_show (frame);
469
470   /* Just some widget we can hook to expose-event on */
471   color_button->priv->draw_area = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
472
473   layout = gtk_widget_create_pango_layout (GTK_WIDGET (color_button), "Black");
474   pango_layout_get_pixel_extents (layout, NULL, &rect);
475   g_object_unref (layout);
476
477   gtk_widget_set_size_request (color_button->priv->draw_area, rect.width - 2, rect.height - 2);
478   g_signal_connect (color_button->priv->draw_area, "draw",
479                     G_CALLBACK (gtk_color_button_draw_cb), color_button);
480   gtk_container_add (GTK_CONTAINER (frame), color_button->priv->draw_area);
481   gtk_widget_show (color_button->priv->draw_area);
482
483   color_button->priv->title = g_strdup (_("Pick a Color")); /* default title */
484
485   /* Start with opaque black, alpha disabled */
486
487   color_button->priv->rgba.red = 0;
488   color_button->priv->rgba.green = 0;
489   color_button->priv->rgba.blue = 0;
490   color_button->priv->rgba.alpha = 1;
491   color_button->priv->use_alpha = FALSE;
492
493   gtk_drag_dest_set (GTK_WIDGET (color_button),
494                      GTK_DEST_DEFAULT_MOTION |
495                      GTK_DEST_DEFAULT_HIGHLIGHT |
496                      GTK_DEST_DEFAULT_DROP,
497                      drop_types, 1, GDK_ACTION_COPY);
498   gtk_drag_source_set (GTK_WIDGET(color_button),
499                        GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
500                        drop_types, 1,
501                        GDK_ACTION_COPY);
502   g_signal_connect (color_button, "drag-begin",
503                     G_CALLBACK (gtk_color_button_drag_begin), color_button);
504   g_signal_connect (color_button, "drag-data-received",
505                     G_CALLBACK (gtk_color_button_drag_data_received), color_button);
506   g_signal_connect (color_button, "drag-data-get",
507                     G_CALLBACK (gtk_color_button_drag_data_get), color_button);
508
509   gtk_widget_pop_composite_child ();
510 }
511
512 static void
513 gtk_color_button_finalize (GObject *object)
514 {
515   GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
516
517   if (color_button->priv->cs_dialog != NULL)
518     gtk_widget_destroy (color_button->priv->cs_dialog);
519   color_button->priv->cs_dialog = NULL;
520
521   g_free (color_button->priv->title);
522   color_button->priv->title = NULL;
523
524   G_OBJECT_CLASS (gtk_color_button_parent_class)->finalize (object);
525 }
526
527
528 /**
529  * gtk_color_button_new:
530  *
531  * Creates a new color button.
532  *
533  * This returns a widget in the form of a small button containing
534  * a swatch representing the current selected color. When the button
535  * is clicked, a color-selection dialog will open, allowing the user
536  * to select a color. The swatch will be updated to reflect the new
537  * color when the user finishes.
538  *
539  * Returns: a new color button
540  *
541  * Since: 2.4
542  */
543 GtkWidget *
544 gtk_color_button_new (void)
545 {
546   return g_object_new (GTK_TYPE_COLOR_BUTTON, NULL);
547 }
548
549 /**
550  * gtk_color_button_new_with_color:
551  * @color: A #GdkColor to set the current color with
552  *
553  * Creates a new color button.
554  *
555  * Returns: a new color button
556  *
557  * Since: 2.4
558  */
559 GtkWidget *
560 gtk_color_button_new_with_color (const GdkColor *color)
561 {
562   return g_object_new (GTK_TYPE_COLOR_BUTTON, "color", color, NULL);
563 }
564
565 /**
566  * gtk_color_button_new_with_rgba:
567  * @rgba: A #GdkRGBA to set the current color with
568  *
569  * Creates a new color button.
570  *
571  * Returns: a new color button
572  *
573  * Since: 3.0
574  */
575 GtkWidget *
576 gtk_color_button_new_with_rgba (const GdkRGBA *rgba)
577 {
578   return g_object_new (GTK_TYPE_COLOR_BUTTON, "rgba", rgba, NULL);
579 }
580
581 static void
582 dialog_ok_clicked (GtkWidget *widget,
583                    gpointer   data)
584 {
585   GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
586   GtkColorSelection *color_selection;
587   GtkColorSelectionDialog *selection_dialog;
588
589   selection_dialog = GTK_COLOR_SELECTION_DIALOG (color_button->priv->cs_dialog);
590   color_selection = GTK_COLOR_SELECTION (gtk_color_selection_dialog_get_color_selection (selection_dialog));
591
592   gtk_color_selection_get_current_rgba (color_selection, &color_button->priv->rgba);
593
594   gtk_widget_hide (color_button->priv->cs_dialog);
595
596   gtk_widget_queue_draw (color_button->priv->draw_area);
597
598   g_signal_emit (color_button, color_button_signals[COLOR_SET], 0);
599
600   g_object_freeze_notify (G_OBJECT (color_button));
601   g_object_notify (G_OBJECT (color_button), "color");
602   g_object_notify (G_OBJECT (color_button), "alpha");
603   g_object_notify (G_OBJECT (color_button), "rgba");
604   g_object_thaw_notify (G_OBJECT (color_button));
605 }
606
607 static gboolean
608 dialog_destroy (GtkWidget *widget,
609                 gpointer   data)
610 {
611   GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
612
613   color_button->priv->cs_dialog = NULL;
614
615   return FALSE;
616 }
617
618 static void
619 dialog_cancel_clicked (GtkWidget *widget,
620                        gpointer   data)
621 {
622   GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
623
624   gtk_widget_hide (color_button->priv->cs_dialog);
625 }
626
627 static void
628 gtk_color_button_clicked (GtkButton *button)
629 {
630   GtkColorButton *color_button = GTK_COLOR_BUTTON (button);
631   GtkColorSelection *color_selection;
632   GtkColorSelectionDialog *color_dialog;
633
634   /* if dialog already exists, make sure it's shown and raised */
635   if (!color_button->priv->cs_dialog)
636     {
637       /* Create the dialog and connects its buttons */
638       GtkWidget *parent;
639       GtkWidget *ok_button, *cancel_button;
640
641       parent = gtk_widget_get_toplevel (GTK_WIDGET (color_button));
642
643       color_button->priv->cs_dialog = gtk_color_selection_dialog_new (color_button->priv->title);
644
645       color_dialog = GTK_COLOR_SELECTION_DIALOG (color_button->priv->cs_dialog);
646
647       if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
648         {
649           if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (color_dialog)))
650             gtk_window_set_transient_for (GTK_WINDOW (color_dialog), GTK_WINDOW (parent));
651
652           gtk_window_set_modal (GTK_WINDOW (color_dialog),
653                                 gtk_window_get_modal (GTK_WINDOW (parent)));
654         }
655
656       g_object_get (color_dialog,
657                     "ok-button", &ok_button,
658                     "cancel-button", &cancel_button,
659                     NULL);
660
661       g_signal_connect (ok_button, "clicked",
662                         G_CALLBACK (dialog_ok_clicked), color_button);
663       g_signal_connect (cancel_button, "clicked",
664                         G_CALLBACK (dialog_cancel_clicked), color_button);
665       g_signal_connect (color_dialog, "destroy",
666                         G_CALLBACK (dialog_destroy), color_button);
667     }
668
669   color_dialog = GTK_COLOR_SELECTION_DIALOG (color_button->priv->cs_dialog);
670   color_selection = GTK_COLOR_SELECTION (gtk_color_selection_dialog_get_color_selection (color_dialog));
671
672   gtk_color_selection_set_has_opacity_control (color_selection,
673                                                color_button->priv->use_alpha);
674
675   gtk_color_selection_set_previous_rgba (color_selection,
676                                          &color_button->priv->rgba);
677   gtk_color_selection_set_current_rgba (color_selection,
678                                         &color_button->priv->rgba);
679
680   gtk_window_present (GTK_WINDOW (color_button->priv->cs_dialog));
681 }
682
683 /**
684  * gtk_color_button_set_color:
685  * @color_button: a #GtkColorButton
686  * @color: A #GdkColor to set the current color with
687  *
688  * Sets the current color to be @color.
689  *
690  * Since: 2.4
691  */
692 void
693 gtk_color_button_set_color (GtkColorButton *color_button,
694                             const GdkColor *color)
695 {
696   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
697   g_return_if_fail (color != NULL);
698
699   color_button->priv->rgba.red = color->red / 65535.;
700   color_button->priv->rgba.green = color->green / 65535.;
701   color_button->priv->rgba.blue = color->blue / 65535.;
702
703   gtk_widget_queue_draw (color_button->priv->draw_area);
704
705   g_object_notify (G_OBJECT (color_button), "color");
706   g_object_notify (G_OBJECT (color_button), "rgba");
707 }
708
709
710 /**
711  * gtk_color_button_set_alpha:
712  * @color_button: a #GtkColorButton
713  * @alpha: an integer between 0 and 65535
714  *
715  * Sets the current opacity to be @alpha.
716  *
717  * Since: 2.4
718  */
719 void
720 gtk_color_button_set_alpha (GtkColorButton *color_button,
721                             guint16         alpha)
722 {
723   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
724
725   color_button->priv->rgba.alpha = alpha / 65535.;
726
727   gtk_widget_queue_draw (color_button->priv->draw_area);
728
729   g_object_notify (G_OBJECT (color_button), "alpha");
730   g_object_notify (G_OBJECT (color_button), "rgba");
731 }
732
733 /**
734  * gtk_color_button_get_color:
735  * @color_button: a #GtkColorButton
736  * @color: (out): a #GdkColor to fill in with the current color
737  *
738  * Sets @color to be the current color in the #GtkColorButton widget.
739  *
740  * Since: 2.4
741  */
742 void
743 gtk_color_button_get_color (GtkColorButton *color_button,
744                             GdkColor       *color)
745 {
746   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
747
748   color->red = (guint16) (color_button->priv->rgba.red * 65535);
749   color->green = (guint16) (color_button->priv->rgba.green * 65535);
750   color->blue = (guint16) (color_button->priv->rgba.blue * 65535);
751 }
752
753 /**
754  * gtk_color_button_get_alpha:
755  * @color_button: a #GtkColorButton
756  *
757  * Returns the current alpha value.
758  *
759  * Return value: an integer between 0 and 65535
760  *
761  * Since: 2.4
762  */
763 guint16
764 gtk_color_button_get_alpha (GtkColorButton *color_button)
765 {
766   g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), 0);
767
768   return (guint16) (color_button->priv->rgba.alpha * 65535);
769 }
770
771 /**
772  * gtk_color_button_set_rgba:
773  * @color_button: a #GtkColorButton
774  * @rgba: a #GdkRGBA to set the current color with
775  *
776  * Sets the current color to be @rgba.
777  *
778  * Since: 3.0
779  */
780 void
781 gtk_color_button_set_rgba (GtkColorButton *color_button,
782                            const GdkRGBA  *rgba)
783 {
784   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
785   g_return_if_fail (rgba != NULL);
786
787   color_button->priv->rgba = *rgba;
788
789   gtk_widget_queue_draw (color_button->priv->draw_area);
790
791   g_object_notify (G_OBJECT (color_button), "rgba");
792 }
793
794 /**
795  * gtk_color_button_get_rgba:
796  * @color_button: a #GtkColorButton
797  * @rgba: (out): a #GdkRGBA to fill in with the current color
798  *
799  * Sets @rgba to be the current color in the #GtkColorButton widget.
800  *
801  * Since: 3.0
802  */
803 void
804 gtk_color_button_get_rgba (GtkColorButton *color_button,
805                            GdkRGBA        *rgba)
806 {
807   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
808   g_return_if_fail (rgba != NULL);
809
810   *rgba = color_button->priv->rgba;
811 }
812
813 /**
814  * gtk_color_button_set_use_alpha:
815  * @color_button: a #GtkColorButton
816  * @use_alpha: %TRUE if color button should use alpha channel, %FALSE if not
817  *
818  * Sets whether or not the color button should use the alpha channel.
819  *
820  * Since: 2.4
821  */
822 void
823 gtk_color_button_set_use_alpha (GtkColorButton *color_button,
824                                 gboolean        use_alpha)
825 {
826   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
827
828   use_alpha = (use_alpha != FALSE);
829
830   if (color_button->priv->use_alpha != use_alpha)
831     {
832       color_button->priv->use_alpha = use_alpha;
833
834       gtk_widget_queue_draw (color_button->priv->draw_area);
835
836       g_object_notify (G_OBJECT (color_button), "use-alpha");
837     }
838 }
839
840 /**
841  * gtk_color_button_get_use_alpha:
842  * @color_button: a #GtkColorButton
843  *
844  * Does the color selection dialog use the alpha channel ?
845  *
846  * Returns: %TRUE if the color sample uses alpha channel, %FALSE if not
847  *
848  * Since: 2.4
849  */
850 gboolean
851 gtk_color_button_get_use_alpha (GtkColorButton *color_button)
852 {
853   g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), FALSE);
854
855   return color_button->priv->use_alpha;
856 }
857
858
859 /**
860  * gtk_color_button_set_title:
861  * @color_button: a #GtkColorButton
862  * @title: String containing new window title
863  *
864  * Sets the title for the color selection dialog.
865  *
866  * Since: 2.4
867  */
868 void
869 gtk_color_button_set_title (GtkColorButton *color_button,
870                             const gchar    *title)
871 {
872   gchar *old_title;
873
874   g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
875
876   old_title = color_button->priv->title;
877   color_button->priv->title = g_strdup (title);
878   g_free (old_title);
879
880   if (color_button->priv->cs_dialog)
881     gtk_window_set_title (GTK_WINDOW (color_button->priv->cs_dialog),
882                           color_button->priv->title);
883
884   g_object_notify (G_OBJECT (color_button), "title");
885 }
886
887 /**
888  * gtk_color_button_get_title:
889  * @color_button: a #GtkColorButton
890  *
891  * Gets the title of the color selection dialog.
892  *
893  * Returns: An internal string, do not free the return value
894  *
895  * Since: 2.4
896  */
897 G_CONST_RETURN gchar *
898 gtk_color_button_get_title (GtkColorButton *color_button)
899 {
900   g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), NULL);
901
902   return color_button->priv->title;
903 }
904
905 static void
906 gtk_color_button_set_property (GObject      *object,
907                                guint         param_id,
908                                const GValue *value,
909                                GParamSpec   *pspec)
910 {
911   GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
912
913   switch (param_id)
914     {
915     case PROP_USE_ALPHA:
916       gtk_color_button_set_use_alpha (color_button, g_value_get_boolean (value));
917       break;
918     case PROP_TITLE:
919       gtk_color_button_set_title (color_button, g_value_get_string (value));
920       break;
921     case PROP_COLOR:
922       gtk_color_button_set_color (color_button, g_value_get_boxed (value));
923       break;
924     case PROP_ALPHA:
925       gtk_color_button_set_alpha (color_button, g_value_get_uint (value));
926       break;
927     case PROP_RGBA:
928       gtk_color_button_set_rgba (color_button, g_value_get_boxed (value));
929       break;
930     default:
931       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
932       break;
933     }
934 }
935
936 static void
937 gtk_color_button_get_property (GObject    *object,
938                                guint       param_id,
939                                GValue     *value,
940                                GParamSpec *pspec)
941 {
942   GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
943   GdkColor color;
944
945   switch (param_id)
946     {
947     case PROP_USE_ALPHA:
948       g_value_set_boolean (value, gtk_color_button_get_use_alpha (color_button));
949       break;
950     case PROP_TITLE:
951       g_value_set_string (value, gtk_color_button_get_title (color_button));
952       break;
953     case PROP_COLOR:
954       gtk_color_button_get_color (color_button, &color);
955       g_value_set_boxed (value, &color);
956       break;
957     case PROP_ALPHA:
958       g_value_set_uint (value, gtk_color_button_get_alpha (color_button));
959       break;
960     case PROP_RGBA:
961       {
962         GdkRGBA rgba;
963
964         gtk_color_button_get_rgba (color_button, &rgba);
965         g_value_set_boxed (value, &rgba);
966       }
967       break;
968     default:
969       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
970       break;
971     }
972 }