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