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