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