]> Pileus Git - ~andy/gtk/blob - gtk/gtkcolorchooser.c
a54127b614ec18e026cb8768678dd19f3ba84326
[~andy/gtk] / gtk / gtkcolorchooser.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2012, Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "config.h"
22
23 #include "gtkcolorchooser.h"
24 #include "gtkcolorchooserprivate.h"
25 #include "gtkintl.h"
26 #include "gtktypebuiltins.h"
27 #include "gtkprivate.h"
28
29 /**
30  * SECTION:gtkcolorchooser
31  * @Short_description: Interface implemented by widgets for choosing colors
32  * @Title: GtkColorChooser
33  * @See_also: #GtkColorChooserDialog, #GtkColorChooserWidget, #GtkColorButton
34  *
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).
38  *
39  * In GTK+, the main widgets that implement this interface are
40  * #GtkColorChooserWidget, #GtkColorChooserDialog and #GtkColorButton.
41  *
42  * Since: 3.4
43  */
44
45 enum
46 {
47   COLOR_ACTIVATED,
48   LAST_SIGNAL
49 };
50
51 static guint signals[LAST_SIGNAL];
52
53 G_DEFINE_INTERFACE (GtkColorChooser, gtk_color_chooser, G_TYPE_OBJECT);
54
55 static void
56 gtk_color_chooser_default_init (GtkColorChooserInterface *iface)
57 {
58   /**
59    * GtkColorChooser:rgba:
60    *
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.
64    *
65    * Since: 3.4
66    */
67   g_object_interface_install_property (iface,
68       g_param_spec_boxed ("rgba",
69                           P_("Color"),
70                           P_("Current color, as a GdkRGBA"),
71                           GDK_TYPE_RGBA,
72                           GTK_PARAM_READWRITE));
73
74   /**
75    * GtkColorChooser:use-alpha:
76    *
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
80    * alpha == 1.
81    *
82    * Implementations are expected to show alpha by rendering the color
83    * over a non-uniform background (like a checkerboard pattern).
84    *
85    * Since: 3.4
86    */
87   g_object_interface_install_property (iface,
88       g_param_spec_boolean ("use-alpha",
89                             P_("Use alpha"),
90                             P_("Whether alpha should be shown"),
91                             TRUE,
92                             GTK_PARAM_READWRITE));
93
94   /**
95    * GtkColorChooser::color-activated:
96    * @chooser: the object which received the signal
97    * @color: the color
98    *
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.
103    *
104    * Since: 3.4
105    */
106   signals[COLOR_ACTIVATED] =
107     g_signal_new ("color-activated",
108                   GTK_TYPE_COLOR_CHOOSER,
109                   G_SIGNAL_RUN_FIRST,
110                   G_STRUCT_OFFSET (GtkColorChooserInterface, color_activated),
111                   NULL, NULL,
112                   NULL,
113                   G_TYPE_NONE,
114                   1, G_TYPE_STRING);
115 }
116
117 void
118 _gtk_color_chooser_color_activated (GtkColorChooser *chooser,
119                                     const GdkRGBA   *color)
120 {
121   g_signal_emit (chooser, signals[COLOR_ACTIVATED], 0, color);
122 }
123
124 /**
125  * gtk_color_chooser_get_rgba:
126  * @chooser: a #GtkColorChooser
127  * @color: return location for the color
128  *
129  * Gets the currently-selected color.
130  *
131  * Since: 3.4
132  */
133 void
134 gtk_color_chooser_get_rgba (GtkColorChooser *chooser,
135                             GdkRGBA         *color)
136 {
137   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
138
139   GTK_COLOR_CHOOSER_GET_IFACE (chooser)->get_rgba (chooser, color);
140 }
141
142 /**
143  * gtk_color_chooser_set_rgba:
144  * @chooser: a #GtkColorChooser
145  * @color: the new color
146  *
147  * Sets the color.
148  */
149 void
150 gtk_color_chooser_set_rgba (GtkColorChooser *chooser,
151                             const GdkRGBA   *color)
152 {
153   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
154   g_return_if_fail (color != NULL);
155
156   GTK_COLOR_CHOOSER_GET_IFACE (chooser)->set_rgba (chooser, color);
157 }
158
159 /**
160  * gtk_color_chooser_get_use_alpha:
161  * @chooser: a #GtkColorChooser
162  *
163  * Returns whether the color chooser shows the alpha channel.
164  *
165  * Returns: %TRUE if the color chooser uses the alpha channel,
166  *     %FALSE if not
167  *
168  * Since: 3.4
169  */
170 gboolean
171 gtk_color_chooser_get_use_alpha (GtkColorChooser *chooser)
172 {
173   gboolean use_alpha;
174
175   g_return_val_if_fail (GTK_IS_COLOR_CHOOSER (chooser), TRUE);
176
177   g_object_get (chooser, "use-alpha", &use_alpha, NULL);
178
179   return use_alpha;
180 }
181
182 /**
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
186  *
187  * Sets whether or not the color chooser should use the alpha channel.
188  *
189  * Since: 3.4
190  */
191 void
192 gtk_color_chooser_set_use_alpha (GtkColorChooser *chooser,
193                                  gboolean         use_alpha)
194 {
195
196   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
197
198   g_object_set (chooser, "use-alpha", use_alpha, NULL);
199 }
200
201 /**
202  * gtk_color_chooser_add_palette:
203  * @chooser: a #GtkColorChooser
204  * @horizontal: %TRUE if the palette should be displayed in rows,
205  *     %FALSE 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: (array length=n_colors): the colors of the palette
209  *
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.
214  *
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.
218  *
219  * The layout of the color chooser widget works best when the
220  * palettes have 9-10 columns.
221  *
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.
225  */
226 void
227 gtk_color_chooser_add_palette (GtkColorChooser *chooser,
228                                gboolean         horizontal,
229                                gint             colors_per_line,
230                                gint             n_colors,
231                                GdkRGBA         *colors)
232 {
233   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
234
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);
237 }
238
239 cairo_pattern_t *
240 _gtk_color_chooser_get_checkered_pattern (void)
241 {
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;
247
248   if (checkered == NULL)
249     checkered = cairo_image_surface_create_for_data (data,
250                                                      CAIRO_FORMAT_A8,
251                                                      2, 2, 4);
252
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);
256
257   return pattern;
258 }