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