1 /* GTK - The GIMP Toolkit
3 * Copyright (C) 2012, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gtkcolorchooser.h"
24 #include "gtkcolorchooserprivate.h"
26 #include "gtktypebuiltins.h"
27 #include "gtkprivate.h"
30 * SECTION:gtkcolorchooser
31 * @Short_description: Interface implemented by widgets for choosing colors
32 * @Title: GtkColorChooser
33 * @See_also: #GtkColorChooserDialog, #GtkColorChooserWidget, #GtkColorButton
35 * #GtkColorChooser is an interface that is implemented by widgets
36 * for choosing colors. Depending on the situation, colors may be
37 * allowed to have alpha (translucency).
39 * In GTK+, the main widgets that implement this interface are
40 * #GtkColorChooserWidget, #GtkColorChooserDialog and #GtkColorButton.
51 static guint signals[LAST_SIGNAL];
53 G_DEFINE_INTERFACE (GtkColorChooser, gtk_color_chooser, G_TYPE_OBJECT);
56 gtk_color_chooser_default_init (GtkColorChooserInterface *iface)
59 * GtkColorChooser:rgba:
61 * The ::rgba property contains the currently selected color,
62 * as a #GdkRGBA struct. The property can be set to change
63 * the current selection programmatically.
67 g_object_interface_install_property (iface,
68 g_param_spec_boxed ("rgba",
70 P_("Current color, as a GdkRGBA"),
72 GTK_PARAM_READWRITE));
75 * GtkColorChooser:use-alpha:
77 * When ::use-alpha is %TRUE, colors may have alpha (translucency)
78 * information. When it is %FALSE, the #GdkRGBA struct obtained
79 * via the #GtkColorChooser:rgba property will be forced to have
82 * Implementations are expected to show alpha by rendering the color
83 * over a non-uniform background (like a checkerboard pattern).
87 g_object_interface_install_property (iface,
88 g_param_spec_boolean ("use-alpha",
90 P_("Whether alpha should be shown"),
92 GTK_PARAM_READWRITE));
95 * GtkColorChooser::color-activated:
96 * @chooser: the object which received the signal
99 * Emitted when a color is activated from the color chooser.
100 * This usually happens when the user clicks a color swatch,
101 * or a color is selected and the user presses one of the keys
102 * Space, Shift+Space, Return or Enter.
106 signals[COLOR_ACTIVATED] =
107 g_signal_new ("color-activated",
108 GTK_TYPE_COLOR_CHOOSER,
110 G_STRUCT_OFFSET (GtkColorChooserInterface, color_activated),
118 _gtk_color_chooser_color_activated (GtkColorChooser *chooser,
119 const GdkRGBA *color)
121 g_signal_emit (chooser, signals[COLOR_ACTIVATED], 0, color);
125 * gtk_color_chooser_get_rgba:
126 * @chooser: a #GtkColorChooser
127 * @color: return location for the color
129 * Gets the currently-selected color.
134 gtk_color_chooser_get_rgba (GtkColorChooser *chooser,
137 g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
139 GTK_COLOR_CHOOSER_GET_IFACE (chooser)->get_rgba (chooser, color);
143 * gtk_color_chooser_set_rgba:
144 * @chooser: a #GtkColorChooser
145 * @color: the new color
150 gtk_color_chooser_set_rgba (GtkColorChooser *chooser,
151 const GdkRGBA *color)
153 g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
154 g_return_if_fail (color != NULL);
156 GTK_COLOR_CHOOSER_GET_IFACE (chooser)->set_rgba (chooser, color);
160 * gtk_color_chooser_get_use_alpha:
161 * @chooser: a #GtkColorChooser
163 * Returns whether the color chooser shows the alpha channel.
165 * Returns: %TRUE if the color chooser uses the alpha channel,
171 gtk_color_chooser_get_use_alpha (GtkColorChooser *chooser)
175 g_return_val_if_fail (GTK_IS_COLOR_CHOOSER (chooser), TRUE);
177 g_object_get (chooser, "use-alpha", &use_alpha, NULL);
183 * gtk_color_chooser_set_use_alpha:
184 * @chooser: a #GtkColorChooser
185 * @use_alpha: %TRUE if color chooser should use alpha channel, %FALSE if not
187 * Sets whether or not the color chooser should use the alpha channel.
192 gtk_color_chooser_set_use_alpha (GtkColorChooser *chooser,
196 g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
198 g_object_set (chooser, "use-alpha", use_alpha, NULL);
202 * gtk_color_chooser_add_palette:
203 * @chooser: a #GtkColorChooser
204 * @horizontal: %TRUE if the palette should be displayed in rows,
206 * @colors_per_line: the number of colors to show in each row/column
207 * @n_colors: the total number of elements in @colors
208 * @colors: (array length=n_colors): the colors of the palette
210 * Adds a palette to the color chooser. If @horizontal is %TRUE,
211 * the colors are grouped in rows, with @colors_per_line colors
212 * in each row. If @horizontal is %FALSE, the colors are grouped
213 * in columns instead.
215 * The default color palette of #GtkColorChooserWidget has
216 * 27 colors, organized in columns of 3 colors. The default gray
217 * palette has 9 grays in a single row.
219 * The layout of the color chooser widget works best when the
220 * palettes have 9-10 columns.
222 * Calling this function is called for the first time has the
223 * side effect of removing the default color and gray palettes
224 * from the color chooser.
227 gtk_color_chooser_add_palette (GtkColorChooser *chooser,
229 gint colors_per_line,
233 g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
235 if (GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette)
236 GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette (chooser, horizontal, colors_per_line, n_colors, colors);
240 _gtk_color_chooser_get_checkered_pattern (void)
242 /* need to respect pixman's stride being a multiple of 4 */
243 static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
244 0x00, 0xFF, 0x00, 0x00 };
245 static cairo_surface_t *checkered = NULL;
246 cairo_pattern_t *pattern;
248 if (checkered == NULL)
249 checkered = cairo_image_surface_create_for_data (data,
253 pattern = cairo_pattern_create_for_surface (checkered);
254 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
255 cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);