2 * GTK - The GIMP Toolkit
3 * Copyright (C) 1998, 1999 Red Hat, Inc.
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.
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.
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.
21 /* Color picker button for GNOME
23 * Author: Federico Mena <federico@nuclecu.unam.mx>
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/.
33 #include "gtkcolorbutton.h"
34 #include "gdk/gdkkeysyms.h"
35 #include "gdk-pixbuf/gdk-pixbuf.h"
36 #include "gtkbutton.h"
38 #include "gtkalignment.h"
39 #include "gtkcolorsel.h"
40 #include "gtkcolorseldialog.h"
42 #include "gtkdrawingarea.h"
44 #include "gtkmarshalers.h"
45 #include "gtkprivate.h"
48 /* Size of checks and gray levels for alpha compositing checkerboard */
50 #define CHECK_DARK (1.0 / 3.0)
51 #define CHECK_LIGHT (2.0 / 3.0)
54 struct _GtkColorButtonPrivate
56 GtkWidget *draw_area; /* Widget where we draw the color sample */
57 GtkWidget *cs_dialog; /* Color selection dialog */
59 gchar *title; /* Title for the color selection window */
64 guint use_alpha : 1; /* Use alpha or not */
85 static void gtk_color_button_finalize (GObject *object);
86 static void gtk_color_button_set_property (GObject *object,
90 static void gtk_color_button_get_property (GObject *object,
95 /* gtkwidget signals */
96 static void gtk_color_button_state_changed (GtkWidget *widget,
97 GtkStateType previous_state);
99 /* gtkbutton signals */
100 static void gtk_color_button_clicked (GtkButton *button);
102 /* source side drag signals */
103 static void gtk_color_button_drag_begin (GtkWidget *widget,
104 GdkDragContext *context,
106 static void gtk_color_button_drag_data_get (GtkWidget *widget,
107 GdkDragContext *context,
108 GtkSelectionData *selection_data,
111 GtkColorButton *color_button);
113 /* target side drag signals */
114 static void gtk_color_button_drag_data_received (GtkWidget *widget,
115 GdkDragContext *context,
118 GtkSelectionData *selection_data,
121 GtkColorButton *color_button);
124 static guint color_button_signals[LAST_SIGNAL] = { 0 };
126 static const GtkTargetEntry drop_types[] = { { "application/x-color", 0, 0 } };
128 G_DEFINE_TYPE (GtkColorButton, gtk_color_button, GTK_TYPE_BUTTON)
131 gtk_color_button_class_init (GtkColorButtonClass *klass)
133 GObjectClass *gobject_class;
134 GtkWidgetClass *widget_class;
135 GtkButtonClass *button_class;
137 gobject_class = G_OBJECT_CLASS (klass);
138 widget_class = GTK_WIDGET_CLASS (klass);
139 button_class = GTK_BUTTON_CLASS (klass);
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;
149 * GtkColorButton:use-alpha:
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.
157 g_object_class_install_property (gobject_class,
159 g_param_spec_boolean ("use-alpha", P_("Use alpha"),
160 P_("Whether to give the color an alpha value"),
162 GTK_PARAM_READWRITE));
165 * GtkColorButton:title:
167 * The title of the color selection dialog
171 g_object_class_install_property (gobject_class,
173 g_param_spec_string ("title",
175 P_("The title of the color selection dialog"),
177 GTK_PARAM_READWRITE));
180 * GtkColorButton:color:
182 * The selected color.
186 g_object_class_install_property (gobject_class,
188 g_param_spec_boxed ("color",
190 P_("The selected color"),
192 GTK_PARAM_READWRITE));
195 * GtkColorButton:alpha:
197 * The selected opacity value (0 fully transparent, 65535 fully opaque).
201 g_object_class_install_property (gobject_class,
203 g_param_spec_uint ("alpha",
205 P_("The selected opacity value (0 fully transparent, 65535 fully opaque)"),
207 GTK_PARAM_READWRITE));
210 * GtkColorButton::color-set:
211 * @widget: the object which received the signal.
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.
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.
223 color_button_signals[COLOR_SET] = g_signal_new (I_("color-set"),
224 G_TYPE_FROM_CLASS (gobject_class),
226 G_STRUCT_OFFSET (GtkColorButtonClass, color_set),
228 _gtk_marshal_VOID__VOID,
231 g_type_class_add_private (gobject_class, sizeof (GtkColorButtonPrivate));
235 gtk_color_button_has_alpha (GtkColorButton *color_button)
237 return color_button->priv->use_alpha &&
238 color_button->priv->alpha < 65535;
241 static cairo_pattern_t *
242 gtk_color_button_get_checkered (void)
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;
250 if (checkered == NULL)
252 checkered = cairo_image_surface_create_for_data (data,
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);
264 /* Handle exposure events for the color picker's drawing area */
266 expose_event (GtkWidget *widget,
267 GdkEventExpose *event,
270 GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
271 GtkAllocation allocation;
272 cairo_pattern_t *checkered;
275 cr = gdk_cairo_create (event->window);
277 gtk_widget_get_allocation (widget, &allocation);
278 gdk_cairo_rectangle (cr, &allocation);
281 if (gtk_color_button_has_alpha (color_button))
285 cairo_set_source_rgb (cr, CHECK_DARK, CHECK_DARK, CHECK_DARK);
288 cairo_set_source_rgb (cr, CHECK_LIGHT, CHECK_LIGHT, CHECK_LIGHT);
289 cairo_scale (cr, CHECK_SIZE, CHECK_SIZE);
291 checkered = gtk_color_button_get_checkered ();
292 cairo_mask (cr, checkered);
293 cairo_pattern_destroy (checkered);
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.);
305 gdk_cairo_set_source_color (cr, &color_button->priv->color);
310 if (!gtk_widget_is_sensitive (GTK_WIDGET (color_button)))
312 gdk_cairo_set_source_color (cr, >k_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);
324 gtk_color_button_state_changed (GtkWidget *widget,
325 GtkStateType previous_state)
327 gtk_widget_queue_draw (widget);
331 gtk_color_button_drag_data_received (GtkWidget *widget,
332 GdkDragContext *context,
335 GtkSelectionData *selection_data,
338 GtkColorButton *color_button)
342 if (selection_data->length < 0)
345 /* We accept drops with the wrong format, since the KDE color
346 * chooser incorrectly drops application/x-color with format 8.
348 if (selection_data->length != 8)
350 g_warning (_("Received invalid color data\n"));
355 dropped = (guint16 *)selection_data->data;
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];
362 gtk_widget_queue_draw (color_button->priv->draw_area);
364 g_signal_emit (color_button, color_button_signals[COLOR_SET], 0);
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));
373 set_color_icon (GdkDragContext *context,
379 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE,
382 pixel = ((color->red & 0xff00) << 16) |
383 ((color->green & 0xff00) << 8) |
384 (color->blue & 0xff00);
386 gdk_pixbuf_fill (pixbuf, pixel);
388 gtk_drag_set_icon_pixbuf (context, pixbuf, -2, -2);
389 g_object_unref (pixbuf);
393 gtk_color_button_drag_begin (GtkWidget *widget,
394 GdkDragContext *context,
397 GtkColorButton *color_button = data;
399 set_color_icon (context, &color_button->priv->color);
403 gtk_color_button_drag_data_get (GtkWidget *widget,
404 GdkDragContext *context,
405 GtkSelectionData *selection_data,
408 GtkColorButton *color_button)
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;
417 gtk_selection_data_set (selection_data, selection_data->target,
418 16, (guchar *)dropped, 8);
422 gtk_color_button_init (GtkColorButton *color_button)
424 GtkWidget *alignment;
429 /* Create the widgets */
430 color_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (color_button,
431 GTK_TYPE_COLOR_BUTTON,
432 GtkColorButtonPrivate);
434 gtk_widget_push_composite_child ();
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);
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);
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);
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);
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);
459 color_button->priv->title = g_strdup (_("Pick a Color")); /* default title */
461 /* Start with opaque black, alpha disabled */
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;
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,
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);
485 gtk_widget_pop_composite_child ();
489 gtk_color_button_finalize (GObject *object)
491 GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
493 if (color_button->priv->cs_dialog != NULL)
494 gtk_widget_destroy (color_button->priv->cs_dialog);
495 color_button->priv->cs_dialog = NULL;
497 g_free (color_button->priv->title);
498 color_button->priv->title = NULL;
500 G_OBJECT_CLASS (gtk_color_button_parent_class)->finalize (object);
505 * gtk_color_button_new:
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.
513 * Returns: a new color button.
518 gtk_color_button_new (void)
520 return g_object_new (GTK_TYPE_COLOR_BUTTON, NULL);
524 * gtk_color_button_new_with_color:
525 * @color: A #GdkColor to set the current color with.
527 * Creates a new color button.
529 * Returns: a new color button.
534 gtk_color_button_new_with_color (const GdkColor *color)
536 return g_object_new (GTK_TYPE_COLOR_BUTTON, "color", color, NULL);
540 dialog_ok_clicked (GtkWidget *widget,
543 GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
544 GtkColorSelection *color_selection;
545 GtkColorSelectionDialog *selection_dialog;
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));
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);
553 gtk_widget_hide (color_button->priv->cs_dialog);
555 gtk_widget_queue_draw (color_button->priv->draw_area);
557 g_signal_emit (color_button, color_button_signals[COLOR_SET], 0);
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));
566 dialog_destroy (GtkWidget *widget,
569 GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
571 color_button->priv->cs_dialog = NULL;
577 dialog_cancel_clicked (GtkWidget *widget,
580 GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
582 gtk_widget_hide (color_button->priv->cs_dialog);
586 gtk_color_button_clicked (GtkButton *button)
588 GtkColorButton *color_button = GTK_COLOR_BUTTON (button);
589 GtkColorSelection *color_selection;
590 GtkColorSelectionDialog *color_dialog;
592 /* if dialog already exists, make sure it's shown and raised */
593 if (!color_button->priv->cs_dialog)
595 /* Create the dialog and connects its buttons */
597 GtkWidget *ok_button, *cancel_button;
599 parent = gtk_widget_get_toplevel (GTK_WIDGET (color_button));
601 color_button->priv->cs_dialog = gtk_color_selection_dialog_new (color_button->priv->title);
603 color_dialog = GTK_COLOR_SELECTION_DIALOG (color_button->priv->cs_dialog);
605 if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
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));
610 gtk_window_set_modal (GTK_WINDOW (color_dialog),
611 gtk_window_get_modal (GTK_WINDOW (parent)));
614 g_object_get (color_dialog,
615 "ok-button", &ok_button,
616 "cancel-button", &cancel_button,
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);
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));
630 gtk_color_selection_set_has_opacity_control (color_selection,
631 color_button->priv->use_alpha);
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);
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);
643 gtk_window_present (GTK_WINDOW (color_button->priv->cs_dialog));
647 * gtk_color_button_set_color:
648 * @color_button: a #GtkColorButton.
649 * @color: A #GdkColor to set the current color with.
651 * Sets the current color to be @color.
656 gtk_color_button_set_color (GtkColorButton *color_button,
657 const GdkColor *color)
659 g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
660 g_return_if_fail (color != NULL);
662 color_button->priv->color.red = color->red;
663 color_button->priv->color.green = color->green;
664 color_button->priv->color.blue = color->blue;
666 gtk_widget_queue_draw (color_button->priv->draw_area);
668 g_object_notify (G_OBJECT (color_button), "color");
673 * gtk_color_button_set_alpha:
674 * @color_button: a #GtkColorButton.
675 * @alpha: an integer between 0 and 65535.
677 * Sets the current opacity to be @alpha.
682 gtk_color_button_set_alpha (GtkColorButton *color_button,
685 g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
687 color_button->priv->alpha = alpha;
689 gtk_widget_queue_draw (color_button->priv->draw_area);
691 g_object_notify (G_OBJECT (color_button), "alpha");
695 * gtk_color_button_get_color:
696 * @color_button: a #GtkColorButton.
697 * @color: a #GdkColor to fill in with the current color.
699 * Sets @color to be the current color in the #GtkColorButton widget.
704 gtk_color_button_get_color (GtkColorButton *color_button,
707 g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
709 color->red = color_button->priv->color.red;
710 color->green = color_button->priv->color.green;
711 color->blue = color_button->priv->color.blue;
715 * gtk_color_button_get_alpha:
716 * @color_button: a #GtkColorButton.
718 * Returns the current alpha value.
720 * Return value: an integer between 0 and 65535.
725 gtk_color_button_get_alpha (GtkColorButton *color_button)
727 g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), 0);
729 return color_button->priv->alpha;
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.
737 * Sets whether or not the color button should use the alpha channel.
742 gtk_color_button_set_use_alpha (GtkColorButton *color_button,
745 g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
747 use_alpha = (use_alpha != FALSE);
749 if (color_button->priv->use_alpha != use_alpha)
751 color_button->priv->use_alpha = use_alpha;
753 gtk_widget_queue_draw (color_button->priv->draw_area);
755 g_object_notify (G_OBJECT (color_button), "use-alpha");
760 * gtk_color_button_get_use_alpha:
761 * @color_button: a #GtkColorButton.
763 * Does the color selection dialog use the alpha channel?
765 * Returns: %TRUE if the color sample uses alpha channel, %FALSE if not.
770 gtk_color_button_get_use_alpha (GtkColorButton *color_button)
772 g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), FALSE);
774 return color_button->priv->use_alpha;
779 * gtk_color_button_set_title:
780 * @color_button: a #GtkColorButton
781 * @title: String containing new window title.
783 * Sets the title for the color selection dialog.
788 gtk_color_button_set_title (GtkColorButton *color_button,
793 g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
795 old_title = color_button->priv->title;
796 color_button->priv->title = g_strdup (title);
799 if (color_button->priv->cs_dialog)
800 gtk_window_set_title (GTK_WINDOW (color_button->priv->cs_dialog),
801 color_button->priv->title);
803 g_object_notify (G_OBJECT (color_button), "title");
807 * gtk_color_button_get_title:
808 * @color_button: a #GtkColorButton
810 * Gets the title of the color selection dialog.
812 * Returns: An internal string, do not free the return value
816 G_CONST_RETURN gchar *
817 gtk_color_button_get_title (GtkColorButton *color_button)
819 g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), NULL);
821 return color_button->priv->title;
825 gtk_color_button_set_property (GObject *object,
830 GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
835 gtk_color_button_set_use_alpha (color_button, g_value_get_boolean (value));
838 gtk_color_button_set_title (color_button, g_value_get_string (value));
841 gtk_color_button_set_color (color_button, g_value_get_boxed (value));
844 gtk_color_button_set_alpha (color_button, g_value_get_uint (value));
847 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
853 gtk_color_button_get_property (GObject *object,
858 GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
864 g_value_set_boolean (value, gtk_color_button_get_use_alpha (color_button));
867 g_value_set_string (value, gtk_color_button_get_title (color_button));
870 gtk_color_button_get_color (color_button, &color);
871 g_value_set_boxed (value, &color);
874 g_value_set_uint (value, gtk_color_button_get_alpha (color_button));
877 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);