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, see <http://www.gnu.org/licenses/>.
21 #include "gtkcolorchooser.h"
22 #include "gtkcolorchooserprivate.h"
24 #include "gtktypebuiltins.h"
25 #include "gtkprivate.h"
28 * SECTION:gtkcolorchooser
29 * @Short_description: Interface implemented by widgets for choosing colors
30 * @Title: GtkColorChooser
31 * @See_also: #GtkColorChooserDialog, #GtkColorChooserWidget, #GtkColorButton
33 * #GtkColorChooser is an interface that is implemented by widgets
34 * for choosing colors. Depending on the situation, colors may be
35 * allowed to have alpha (translucency).
37 * In GTK+, the main widgets that implement this interface are
38 * #GtkColorChooserWidget, #GtkColorChooserDialog and #GtkColorButton.
49 static guint signals[LAST_SIGNAL];
51 G_DEFINE_INTERFACE (GtkColorChooser, gtk_color_chooser, G_TYPE_OBJECT);
54 gtk_color_chooser_default_init (GtkColorChooserInterface *iface)
57 * GtkColorChooser:rgba:
59 * The ::rgba property contains the currently selected color,
60 * as a #GdkRGBA struct. The property can be set to change
61 * the current selection programmatically.
65 g_object_interface_install_property (iface,
66 g_param_spec_boxed ("rgba",
68 P_("Current color, as a GdkRGBA"),
70 GTK_PARAM_READWRITE));
73 * GtkColorChooser:use-alpha:
75 * When ::use-alpha is %TRUE, colors may have alpha (translucency)
76 * information. When it is %FALSE, the #GdkRGBA struct obtained
77 * via the #GtkColorChooser:rgba property will be forced to have
80 * Implementations are expected to show alpha by rendering the color
81 * over a non-uniform background (like a checkerboard pattern).
85 g_object_interface_install_property (iface,
86 g_param_spec_boolean ("use-alpha",
88 P_("Whether alpha should be shown"),
90 GTK_PARAM_READWRITE));
93 * GtkColorChooser::color-activated:
94 * @chooser: the object which received the signal
97 * Emitted when a color is activated from the color chooser.
98 * This usually happens when the user clicks a color swatch,
99 * or a color is selected and the user presses one of the keys
100 * Space, Shift+Space, Return or Enter.
104 signals[COLOR_ACTIVATED] =
105 g_signal_new ("color-activated",
106 GTK_TYPE_COLOR_CHOOSER,
108 G_STRUCT_OFFSET (GtkColorChooserInterface, color_activated),
116 _gtk_color_chooser_color_activated (GtkColorChooser *chooser,
117 const GdkRGBA *color)
119 g_signal_emit (chooser, signals[COLOR_ACTIVATED], 0, color);
123 * gtk_color_chooser_get_rgba:
124 * @chooser: a #GtkColorChooser
125 * @color: (out): a #GdkRGBA to fill in with the current color
127 * Gets the currently-selected color.
132 gtk_color_chooser_get_rgba (GtkColorChooser *chooser,
135 g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
137 GTK_COLOR_CHOOSER_GET_IFACE (chooser)->get_rgba (chooser, color);
141 * gtk_color_chooser_set_rgba:
142 * @chooser: a #GtkColorChooser
143 * @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 * @orientation: %GTK_ORIENTATION_HORIZONTAL if the palette should
205 * be displayed in rows, %GTK_ORIENTATION_VERTICAL for columns
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: (allow-none) (array length=n_colors): the colors of the palette, or %NULL
210 * Adds a palette to the color chooser. If @orientation is horizontal,
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 for the first time has the
223 * side effect of removing the default color and gray palettes
224 * from the color chooser.
226 * If @colors is %NULL, removes all previously added palettes.
231 gtk_color_chooser_add_palette (GtkColorChooser *chooser,
232 GtkOrientation orientation,
233 gint colors_per_line,
237 g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
239 if (GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette)
240 GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette (chooser, orientation, colors_per_line, n_colors, colors);
244 _gtk_color_chooser_get_checkered_pattern (void)
246 /* need to respect pixman's stride being a multiple of 4 */
247 static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
248 0x00, 0xFF, 0x00, 0x00 };
249 static cairo_surface_t *checkered = NULL;
250 cairo_pattern_t *pattern;
252 if (checkered == NULL)
253 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);