]> Pileus Git - ~andy/gtk/blob - gtk/gtkcolorchooser.c
Change FSF Address
[~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, G_TYPE_STRING);
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: return location for the 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 void
148 gtk_color_chooser_set_rgba (GtkColorChooser *chooser,
149                             const GdkRGBA   *color)
150 {
151   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
152   g_return_if_fail (color != NULL);
153
154   GTK_COLOR_CHOOSER_GET_IFACE (chooser)->set_rgba (chooser, color);
155 }
156
157 /**
158  * gtk_color_chooser_get_use_alpha:
159  * @chooser: a #GtkColorChooser
160  *
161  * Returns whether the color chooser shows the alpha channel.
162  *
163  * Returns: %TRUE if the color chooser uses the alpha channel,
164  *     %FALSE if not
165  *
166  * Since: 3.4
167  */
168 gboolean
169 gtk_color_chooser_get_use_alpha (GtkColorChooser *chooser)
170 {
171   gboolean use_alpha;
172
173   g_return_val_if_fail (GTK_IS_COLOR_CHOOSER (chooser), TRUE);
174
175   g_object_get (chooser, "use-alpha", &use_alpha, NULL);
176
177   return use_alpha;
178 }
179
180 /**
181  * gtk_color_chooser_set_use_alpha:
182  * @chooser: a #GtkColorChooser
183  * @use_alpha: %TRUE if color chooser should use alpha channel, %FALSE if not
184  *
185  * Sets whether or not the color chooser should use the alpha channel.
186  *
187  * Since: 3.4
188  */
189 void
190 gtk_color_chooser_set_use_alpha (GtkColorChooser *chooser,
191                                  gboolean         use_alpha)
192 {
193
194   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
195
196   g_object_set (chooser, "use-alpha", use_alpha, NULL);
197 }
198
199 /**
200  * gtk_color_chooser_add_palette:
201  * @chooser: a #GtkColorChooser
202  * @horizontal: %TRUE if the palette should be displayed in rows,
203  *     %FALSE for columns
204  * @colors_per_line: the number of colors to show in each row/column
205  * @n_colors: the total number of elements in @colors
206  * @colors: (array length=n_colors): the colors of the palette
207  *
208  * Adds a palette to the color chooser. If @horizontal is %TRUE,
209  * the colors are grouped in rows, with @colors_per_line colors
210  * in each row. If @horizontal is %FALSE, the colors are grouped
211  * in columns instead.
212  *
213  * The default color palette of #GtkColorChooserWidget has
214  * 27 colors, organized in columns of 3 colors. The default gray
215  * palette has 9 grays in a single row.
216  *
217  * The layout of the color chooser widget works best when the
218  * palettes have 9-10 columns.
219  *
220  * Calling this function is called for the first time has the
221  * side effect of removing the default color and gray palettes
222  * from the color chooser.
223  */
224 void
225 gtk_color_chooser_add_palette (GtkColorChooser *chooser,
226                                gboolean         horizontal,
227                                gint             colors_per_line,
228                                gint             n_colors,
229                                GdkRGBA         *colors)
230 {
231   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
232
233   if (GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette)
234     GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette (chooser, horizontal, colors_per_line, n_colors, colors);
235 }
236
237 cairo_pattern_t *
238 _gtk_color_chooser_get_checkered_pattern (void)
239 {
240   /* need to respect pixman's stride being a multiple of 4 */
241   static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
242                                    0x00, 0xFF, 0x00, 0x00 };
243   static cairo_surface_t *checkered = NULL;
244   cairo_pattern_t *pattern;
245
246   if (checkered == NULL)
247     checkered = cairo_image_surface_create_for_data (data,
248                                                      CAIRO_FORMAT_A8,
249                                                      2, 2, 4);
250
251   pattern = cairo_pattern_create_for_surface (checkered);
252   cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
253   cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
254
255   return pattern;
256 }