]> Pileus Git - ~andy/gtk/blob - gtk/gtkcolorchooser.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20
21 #include "gtkcolorchooser.h"
22 #include "gtkcolorchooserprivate.h"
23 #include "gtkintl.h"
24 #include "gtktypebuiltins.h"
25 #include "gtkprivate.h"
26
27 /**
28  * SECTION:gtkcolorchooser
29  * @Short_description: Interface implemented by widgets for choosing colors
30  * @Title: GtkColorChooser
31  * @See_also: #GtkColorChooserDialog, #GtkColorChooserWidget, #GtkColorButton
32  *
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).
36  *
37  * In GTK+, the main widgets that implement this interface are
38  * #GtkColorChooserWidget, #GtkColorChooserDialog and #GtkColorButton.
39  *
40  * Since: 3.4
41  */
42
43 enum
44 {
45   COLOR_ACTIVATED,
46   LAST_SIGNAL
47 };
48
49 static guint signals[LAST_SIGNAL];
50
51 G_DEFINE_INTERFACE (GtkColorChooser, gtk_color_chooser, G_TYPE_OBJECT);
52
53 static void
54 gtk_color_chooser_default_init (GtkColorChooserInterface *iface)
55 {
56   /**
57    * GtkColorChooser:rgba:
58    *
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.
62    *
63    * Since: 3.4
64    */
65   g_object_interface_install_property (iface,
66       g_param_spec_boxed ("rgba",
67                           P_("Color"),
68                           P_("Current color, as a GdkRGBA"),
69                           GDK_TYPE_RGBA,
70                           GTK_PARAM_READWRITE));
71
72   /**
73    * GtkColorChooser:use-alpha:
74    *
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
78    * alpha == 1.
79    *
80    * Implementations are expected to show alpha by rendering the color
81    * over a non-uniform background (like a checkerboard pattern).
82    *
83    * Since: 3.4
84    */
85   g_object_interface_install_property (iface,
86       g_param_spec_boolean ("use-alpha",
87                             P_("Use alpha"),
88                             P_("Whether alpha should be shown"),
89                             TRUE,
90                             GTK_PARAM_READWRITE));
91
92   /**
93    * GtkColorChooser::color-activated:
94    * @chooser: the object which received the signal
95    * @color: the color
96    *
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.
101    *
102    * Since: 3.4
103    */
104   signals[COLOR_ACTIVATED] =
105     g_signal_new ("color-activated",
106                   GTK_TYPE_COLOR_CHOOSER,
107                   G_SIGNAL_RUN_FIRST,
108                   G_STRUCT_OFFSET (GtkColorChooserInterface, color_activated),
109                   NULL, NULL,
110                   NULL,
111                   G_TYPE_NONE,
112                   1, GDK_TYPE_RGBA);
113 }
114
115 void
116 _gtk_color_chooser_color_activated (GtkColorChooser *chooser,
117                                     const GdkRGBA   *color)
118 {
119   g_signal_emit (chooser, signals[COLOR_ACTIVATED], 0, color);
120 }
121
122 /**
123  * gtk_color_chooser_get_rgba:
124  * @chooser: a #GtkColorChooser
125  * @color: (out): a #GdkRGBA to fill in with the current color
126  *
127  * Gets the currently-selected color.
128  *
129  * Since: 3.4
130  */
131 void
132 gtk_color_chooser_get_rgba (GtkColorChooser *chooser,
133                             GdkRGBA         *color)
134 {
135   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
136
137   GTK_COLOR_CHOOSER_GET_IFACE (chooser)->get_rgba (chooser, color);
138 }
139
140 /**
141  * gtk_color_chooser_set_rgba:
142  * @chooser: a #GtkColorChooser
143  * @color: the new color
144  *
145  * Sets the color.
146  *
147  * Since: 3.4
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  * @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
209  *
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.
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 for the first time has the
223  * side effect of removing the default color and gray palettes
224  * from the color chooser.
225  *
226  * If @colors is %NULL, removes all previously added palettes.
227  *
228  * Since: 3.4
229  */
230 void
231 gtk_color_chooser_add_palette (GtkColorChooser *chooser,
232                                GtkOrientation   orientation,
233                                gint             colors_per_line,
234                                gint             n_colors,
235                                GdkRGBA         *colors)
236 {
237   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
238
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);
241 }
242
243 cairo_pattern_t *
244 _gtk_color_chooser_get_checkered_pattern (void)
245 {
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;
251
252   if (checkered == NULL)
253     checkered = cairo_image_surface_create_for_data (data,
254                                                      CAIRO_FORMAT_A8,
255                                                      2, 2, 4);
256
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);
260
261   return pattern;
262 }