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