]> Pileus Git - ~andy/gtk/blob - gtk/gtkcolorbutton.c
081e687cbaed1411eed71f1682968d6b3f09fd3f
[~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 #include "gtkalias.h"
48
49 /* Size of checks and gray levels for alpha compositing checkerboard */
50 #define CHECK_SIZE  4
51 #define CHECK_DARK  21845  /* 65535 / 3     */
52 #define CHECK_LIGHT 43690 
53
54 #define GTK_COLOR_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COLOR_BUTTON, GtkColorButtonPrivate))
55
56 struct _GtkColorButtonPrivate 
57 {
58   GdkPixbuf *pixbuf;    /* Pixbuf for rendering sample */
59   GdkGC *gc;            /* GC for drawing */
60   
61   GtkWidget *draw_area; /* Widget where we draw the color sample */
62   GtkWidget *cs_dialog; /* Color selection dialog */
63   
64   gchar *title;         /* Title for the color selection window */
65   
66   GdkColor color;
67   guint16 alpha;
68   
69   guint use_alpha : 1;  /* Use alpha or not */
70 };
71
72 /* Properties */
73 enum 
74 {
75   PROP_0,
76   PROP_USE_ALPHA,
77   PROP_TITLE,
78   PROP_COLOR,
79   PROP_ALPHA
80 };
81
82 /* Signals */
83 enum 
84 {
85   COLOR_SET,
86   LAST_SIGNAL
87 };
88
89 /* gobject signals */
90 static void gtk_color_button_finalize      (GObject             *object);
91 static void gtk_color_button_set_property  (GObject        *object,
92                                             guint           param_id,
93                                             const GValue   *value,
94                                             GParamSpec     *pspec);
95 static void gtk_color_button_get_property  (GObject        *object,
96                                             guint           param_id,
97                                             GValue         *value,
98                                             GParamSpec     *pspec);
99
100 /* gtkwidget signals */
101 static void gtk_color_button_realize       (GtkWidget *widget);
102 static void gtk_color_button_unrealize     (GtkWidget *widget);
103 static void gtk_color_button_state_changed (GtkWidget           *widget, 
104                                             GtkStateType         previous_state);
105 static void gtk_color_button_style_set     (GtkWidget *widget, 
106                                             GtkStyle  *previous_style);
107
108 /* gtkbutton signals */
109 static void gtk_color_button_clicked       (GtkButton           *button);
110
111 /* source side drag signals */
112 static void gtk_color_button_drag_begin (GtkWidget        *widget,
113                                          GdkDragContext   *context,
114                                          gpointer          data);
115 static void gtk_color_button_drag_data_get (GtkWidget        *widget,
116                                             GdkDragContext   *context,
117                                             GtkSelectionData *selection_data,
118                                             guint             info,
119                                             guint             time,
120                                             GtkColorButton   *color_button);
121
122 /* target side drag signals */
123 static void gtk_color_button_drag_data_received (GtkWidget        *widget,
124                                                  GdkDragContext   *context,
125                                                  gint              x,
126                                                  gint              y,
127                                                  GtkSelectionData *selection_data,
128                                                  guint             info,
129                                                  guint32           time,
130                                                  GtkColorButton   *color_button);
131
132
133 static guint color_button_signals[LAST_SIGNAL] = { 0 };
134
135 static const GtkTargetEntry drop_types[] = { { "application/x-color", 0, 0 } };
136
137 G_DEFINE_TYPE (GtkColorButton, gtk_color_button, GTK_TYPE_BUTTON)
138
139 static void
140 gtk_color_button_class_init (GtkColorButtonClass *klass)
141 {
142   GObjectClass *gobject_class;
143   GtkWidgetClass *widget_class;
144   GtkButtonClass *button_class;
145
146   gobject_class = G_OBJECT_CLASS (klass);
147   widget_class = GTK_WIDGET_CLASS (klass);
148   button_class = GTK_BUTTON_CLASS (klass);
149
150   gobject_class->get_property = gtk_color_button_get_property;
151   gobject_class->set_property = gtk_color_button_set_property;
152   gobject_class->finalize = gtk_color_button_finalize;
153   widget_class->state_changed = gtk_color_button_state_changed;
154   widget_class->realize = gtk_color_button_realize;
155   widget_class->unrealize = gtk_color_button_unrealize;
156   widget_class->style_set = gtk_color_button_style_set;
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 rendered against a 
164    * checkerboard background to show its opacity and the opacity slider is displayed in the 
165    * 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 or not 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   g_object_class_install_property (gobject_class,
199                                    PROP_COLOR,
200                                    g_param_spec_boxed ("color",
201                                                        P_("Current Color"),
202                                                        P_("The selected color"),
203                                                        GDK_TYPE_COLOR,
204                                                        GTK_PARAM_READWRITE));
205
206   /**
207    * GtkColorButton:alpha:
208    *
209    * The selected opacity value (0 fully transparent, 65535 fully opaque). 
210    *
211    * Since: 2.4
212    */
213   g_object_class_install_property (gobject_class,
214                                    PROP_ALPHA,
215                                    g_param_spec_uint ("alpha",
216                                                       P_("Current Alpha"),
217                                                       P_("The selected opacity value (0 fully transparent, 65535 fully opaque)"),
218                                                       0, 65535, 65535,
219                                                       GTK_PARAM_READWRITE));
220         
221   /**
222    * GtkColorButton::color-set:
223    * @widget: the object which received the signal.
224    * 
225    * The ::color-set signal is emitted when the user selects a color. 
226    * When handling this signal, use gtk_color_button_get_color() and 
227    * gtk_color_button_get_alpha() to find out which color was just selected.
228    *
229    * Note that this signal is only emitted when the <emphasis>user</emphasis>
230    * changes the color. If you need to react to programmatic color changes
231    * as well, use the notify::color signal.
232    *
233    * Since: 2.4
234    */
235   color_button_signals[COLOR_SET] = g_signal_new (I_("color-set"),
236                                                   G_TYPE_FROM_CLASS (gobject_class),
237                                                   G_SIGNAL_RUN_FIRST,
238                                                   G_STRUCT_OFFSET (GtkColorButtonClass, color_set),
239                                                   NULL, NULL,
240                                                   _gtk_marshal_VOID__VOID,
241                                                   G_TYPE_NONE, 0);
242
243   g_type_class_add_private (gobject_class, sizeof (GtkColorButtonPrivate));
244 }
245
246 static void
247 render (GtkColorButton *color_button)
248 {
249   gint dark_r, dark_g, dark_b;
250   gint light_r, light_g, light_b;
251   gint i, j, rowstride;
252   gint width, height;
253   gint c1[3], c2[3];
254   guchar *pixels;
255   guint8 insensitive_r = 0;
256   guint8 insensitive_g = 0;
257   guint8 insensitive_b = 0;
258
259   width = color_button->priv->draw_area->allocation.width;
260   height = color_button->priv->draw_area->allocation.height;
261   if (color_button->priv->pixbuf == NULL || 
262       gdk_pixbuf_get_width (color_button->priv->pixbuf) != width ||
263       gdk_pixbuf_get_height (color_button->priv->pixbuf) != height) 
264     {
265       if (color_button->priv->pixbuf != NULL)
266         g_object_unref (color_button->priv->pixbuf);
267       color_button->priv->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
268     }
269   
270
271   /* Compute dark and light check colors */
272
273   insensitive_r = GTK_WIDGET(color_button)->style->bg[GTK_STATE_INSENSITIVE].red >> 8;
274   insensitive_g = GTK_WIDGET(color_button)->style->bg[GTK_STATE_INSENSITIVE].green >> 8;
275   insensitive_b = GTK_WIDGET(color_button)->style->bg[GTK_STATE_INSENSITIVE].blue >> 8;
276
277   if (color_button->priv->use_alpha) 
278     {
279       dark_r = ((CHECK_DARK << 16) + (color_button->priv->color.red - CHECK_DARK) * color_button->priv->alpha) >> 24;
280       dark_g = ((CHECK_DARK << 16) + (color_button->priv->color.green - CHECK_DARK) * color_button->priv->alpha) >> 24;
281       dark_b = ((CHECK_DARK << 16) + (color_button->priv->color.blue - CHECK_DARK) * color_button->priv->alpha) >> 24;
282       
283       light_r = ((CHECK_LIGHT << 16) + (color_button->priv->color.red - CHECK_LIGHT) * color_button->priv->alpha) >> 24;
284       light_g = ((CHECK_LIGHT << 16) + (color_button->priv->color.green - CHECK_LIGHT) * color_button->priv->alpha) >> 24;
285       light_b = ((CHECK_LIGHT << 16) + (color_button->priv->color.blue - CHECK_LIGHT) * color_button->priv->alpha) >> 24;
286     } 
287   else 
288     {
289       dark_r = light_r = color_button->priv->color.red >> 8;
290       dark_g = light_g = color_button->priv->color.green >> 8;
291       dark_b = light_b = color_button->priv->color.blue >> 8;
292     }
293
294   /* Fill image buffer */
295   pixels = gdk_pixbuf_get_pixels (color_button->priv->pixbuf);
296   rowstride = gdk_pixbuf_get_rowstride (color_button->priv->pixbuf);
297   for (j = 0; j < height; j++) 
298     {
299       if ((j / CHECK_SIZE) & 1) 
300         {
301           c1[0] = dark_r;
302           c1[1] = dark_g;
303           c1[2] = dark_b;
304           
305           c2[0] = light_r;
306           c2[1] = light_g;
307           c2[2] = light_b;
308         } 
309       else 
310         {
311           c1[0] = light_r;
312           c1[1] = light_g;
313           c1[2] = light_b;
314           
315           c2[0] = dark_r;
316           c2[1] = dark_g;
317           c2[2] = dark_b;
318         }
319       
320     for (i = 0; i < width; i++) 
321       {
322         if (!GTK_WIDGET_IS_SENSITIVE (GTK_WIDGET (color_button)) && (i+j)%2) 
323           {
324             *(pixels + j * rowstride + i * 3) = insensitive_r;
325             *(pixels + j * rowstride + i * 3 + 1) = insensitive_g;
326             *(pixels + j * rowstride + i * 3 + 2) = insensitive_b;
327           } 
328         else if ((i / CHECK_SIZE) & 1) 
329           {
330             *(pixels + j * rowstride + i * 3)     = c1[0];
331             *(pixels + j * rowstride + i * 3 + 1) = c1[1];
332             *(pixels + j * rowstride + i * 3 + 2) = c1[2];
333           }
334         else
335           {
336             *(pixels + j * rowstride + i * 3)     = c2[0];
337             *(pixels + j * rowstride + i * 3 + 1) = c2[1];
338             *(pixels + j * rowstride + i * 3 + 2) = c2[2];
339           }
340       }
341     }
342 }
343
344 /* Handle exposure events for the color picker's drawing area */
345 static gint
346 expose_event (GtkWidget      *widget, 
347               GdkEventExpose *event, 
348               gpointer        data)
349 {
350   GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
351
352   gint width = color_button->priv->draw_area->allocation.width;
353   gint height = color_button->priv->draw_area->allocation.height;
354
355   if (color_button->priv->pixbuf == NULL ||
356       width != gdk_pixbuf_get_width (color_button->priv->pixbuf) ||
357       height != gdk_pixbuf_get_height (color_button->priv->pixbuf)) 
358     render (color_button);
359
360   gdk_draw_pixbuf (widget->window,
361                    color_button->priv->gc,
362                    color_button->priv->pixbuf,
363                    event->area.x - widget->allocation.x,
364                    event->area.y - widget->allocation.y,
365                    event->area.x,
366                    event->area.y,
367                    event->area.width,
368                    event->area.height,
369                    GDK_RGB_DITHER_MAX,
370                    event->area.x - widget->allocation.x,
371                    event->area.y - widget->allocation.y);
372   return FALSE;
373 }
374
375 static void
376 gtk_color_button_realize (GtkWidget *widget)
377 {
378   GtkColorButton *color_button = GTK_COLOR_BUTTON (widget);
379
380   GTK_WIDGET_CLASS (gtk_color_button_parent_class)->realize (widget);
381
382   color_button->priv->gc = gdk_gc_new (widget->window);
383
384   render (color_button);
385 }
386
387 static void
388 gtk_color_button_unrealize (GtkWidget *widget)
389 {
390   GtkColorButton *color_button = GTK_COLOR_BUTTON (widget);
391
392   g_object_unref (color_button->priv->gc);
393   color_button->priv->gc = NULL;
394
395   GTK_WIDGET_CLASS (gtk_color_button_parent_class)->unrealize (widget);
396 }
397
398 static void
399 gtk_color_button_style_set (GtkWidget *widget, 
400                             GtkStyle  *previous_style)
401 {
402   GtkColorButton *color_button = GTK_COLOR_BUTTON (widget);
403
404   GTK_WIDGET_CLASS (gtk_color_button_parent_class)->style_set (widget, previous_style);
405
406   if (GTK_WIDGET_REALIZED (widget)) 
407     {
408       if (color_button->priv->pixbuf != NULL)
409         g_object_unref (color_button->priv->pixbuf);
410       color_button->priv->pixbuf = NULL;
411     }
412 }
413
414 static void
415 gtk_color_button_state_changed (GtkWidget   *widget, 
416                                           GtkStateType previous_state)
417 {
418   GtkColorButton *color_button = GTK_COLOR_BUTTON (widget);
419
420   if (widget->state == GTK_STATE_INSENSITIVE || previous_state == GTK_STATE_INSENSITIVE)
421     {
422       if (color_button->priv->pixbuf != NULL)
423         g_object_unref (color_button->priv->pixbuf);
424       color_button->priv->pixbuf = NULL;
425     }
426 }
427
428 static void
429 gtk_color_button_drag_data_received (GtkWidget        *widget,
430                                      GdkDragContext   *context,
431                                      gint              x,
432                                      gint              y,
433                                      GtkSelectionData *selection_data,
434                                      guint             info,
435                                      guint32           time,
436                                      GtkColorButton   *color_button)
437 {
438   guint16 *dropped;
439
440   if (selection_data->length < 0)
441     return;
442
443   /* We accept drops with the wrong format, since the KDE color
444    * chooser incorrectly drops application/x-color with format 8.
445    */
446   if (selection_data->length != 8) 
447     {
448       g_warning (_("Received invalid color data\n"));
449       return;
450     }
451
452
453   dropped = (guint16 *)selection_data->data;
454
455   color_button->priv->color.red = dropped[0];
456   color_button->priv->color.green = dropped[1];
457   color_button->priv->color.blue = dropped[2];
458   color_button->priv->alpha = dropped[3];
459
460   if (color_button->priv->pixbuf != NULL)
461     g_object_unref (color_button->priv->pixbuf);
462   color_button->priv->pixbuf = NULL;
463
464   gtk_widget_queue_draw (color_button->priv->draw_area);
465
466   g_signal_emit (color_button, color_button_signals[COLOR_SET], 0);
467
468   g_object_freeze_notify (G_OBJECT (color_button));
469   g_object_notify (G_OBJECT (color_button), "color");
470   g_object_notify (G_OBJECT (color_button), "alpha");
471   g_object_thaw_notify (G_OBJECT (color_button));
472 }
473
474 static void
475 set_color_icon (GdkDragContext *context,
476                 GdkColor       *color)
477 {
478   GdkPixbuf *pixbuf;
479   guint32 pixel;
480
481   pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE,
482                            8, 48, 32);
483
484   pixel = ((color->red & 0xff00) << 16) | 
485           ((color->green & 0xff00) << 8) | 
486            (color->blue & 0xff00);
487
488   gdk_pixbuf_fill (pixbuf, pixel);
489   
490   gtk_drag_set_icon_pixbuf (context, pixbuf, -2, -2);
491   g_object_unref (pixbuf);
492 }
493
494 static void
495 gtk_color_button_drag_begin (GtkWidget      *widget,
496                              GdkDragContext *context,
497                              gpointer        data)
498 {
499   GtkColorButton *color_button = data;
500   
501   set_color_icon (context, &color_button->priv->color);
502 }
503
504 static void
505 gtk_color_button_drag_data_get (GtkWidget        *widget,
506                                 GdkDragContext   *context,
507                                 GtkSelectionData *selection_data,
508                                 guint             info,
509                                 guint             time,
510                                 GtkColorButton   *color_button)
511 {
512   guint16 dropped[4];
513
514   dropped[0] = color_button->priv->color.red;
515   dropped[1] = color_button->priv->color.green;
516   dropped[2] = color_button->priv->color.blue;
517   dropped[3] = color_button->priv->alpha;
518
519   gtk_selection_data_set (selection_data, selection_data->target,
520                           16, (guchar *)dropped, 8);
521 }
522
523 static void
524 gtk_color_button_init (GtkColorButton *color_button)
525 {
526   GtkWidget *alignment;
527   GtkWidget *frame;
528   PangoLayout *layout;
529   PangoRectangle rect;
530
531   /* Create the widgets */
532   color_button->priv = GTK_COLOR_BUTTON_GET_PRIVATE (color_button);
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_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 }
995
996 #define __GTK_COLOR_BUTTON_C__
997 #include "gtkaliasdef.c"