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 | #include "gtksnapshot.h" |
27 | #include "gdk/gdkrgbaprivate.h" |
28 | |
29 | /** |
30 | * GtkColorChooser: |
31 | * |
32 | * `GtkColorChooser` is an interface that is implemented by widgets |
33 | * for choosing colors. |
34 | * |
35 | * Depending on the situation, colors may be allowed to have alpha (translucency). |
36 | * |
37 | * In GTK, the main widgets that implement this interface are |
38 | * [class@Gtk.ColorChooserWidget], [class@Gtk.ColorChooserDialog] and |
39 | * [class@Gtk.ColorButton]. |
40 | */ |
41 | |
42 | enum |
43 | { |
44 | COLOR_ACTIVATED, |
45 | LAST_SIGNAL |
46 | }; |
47 | |
48 | static guint signals[LAST_SIGNAL]; |
49 | |
50 | G_DEFINE_INTERFACE (GtkColorChooser, gtk_color_chooser, G_TYPE_OBJECT); |
51 | |
52 | static void |
53 | gtk_color_chooser_default_init (GtkColorChooserInterface *iface) |
54 | { |
55 | /** |
56 | * GtkColorChooser:rgba: (attributes org.gtk.Property.get=gtk_color_chooser_get_rgba org.gtk.Property.set=gtk_color_chooser_set_rgba) |
57 | * |
58 | * The currently selected color, as a `GdkRGBA` struct. |
59 | * |
60 | * The property can be set to change the current selection |
61 | * programmatically. |
62 | */ |
63 | g_object_interface_install_property (g_iface: iface, |
64 | pspec: g_param_spec_boxed (name: "rgba" , |
65 | P_("Color" ), |
66 | P_("Current color, as a GdkRGBA" ), |
67 | GDK_TYPE_RGBA, |
68 | GTK_PARAM_READWRITE)); |
69 | |
70 | /** |
71 | * GtkColorChooser:use-alpha: (attributes org.gtk.Property.get=gtk_color_chooser_get_use_alpha org.gtk.Property.set=gtk_color_chooser_set_use_alpha) |
72 | * |
73 | * Whether colors may have alpha (translucency). |
74 | * |
75 | * When ::use-alpha is %FALSE, the `GdkRGBA` struct obtained |
76 | * via the [property@Gtk.ColorChooser:rgba] property will be |
77 | * forced to have alpha == 1. |
78 | * |
79 | * Implementations are expected to show alpha by rendering the color |
80 | * over a non-uniform background (like a checkerboard pattern). |
81 | */ |
82 | g_object_interface_install_property (g_iface: iface, |
83 | pspec: g_param_spec_boolean (name: "use-alpha" , |
84 | P_("Use alpha" ), |
85 | P_("Whether alpha should be shown" ), |
86 | TRUE, |
87 | GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY)); |
88 | |
89 | /** |
90 | * GtkColorChooser::color-activated: |
91 | * @chooser: the object which received the signal |
92 | * @color: the color |
93 | * |
94 | * Emitted when a color is activated from the color chooser. |
95 | * |
96 | * This usually happens when the user clicks a color swatch, |
97 | * or a color is selected and the user presses one of the keys |
98 | * Space, Shift+Space, Return or Enter. |
99 | */ |
100 | signals[COLOR_ACTIVATED] = |
101 | g_signal_new (I_("color-activated" ), |
102 | GTK_TYPE_COLOR_CHOOSER, |
103 | signal_flags: G_SIGNAL_RUN_FIRST, |
104 | G_STRUCT_OFFSET (GtkColorChooserInterface, color_activated), |
105 | NULL, NULL, |
106 | NULL, |
107 | G_TYPE_NONE, |
108 | n_params: 1, GDK_TYPE_RGBA); |
109 | } |
110 | |
111 | void |
112 | _gtk_color_chooser_color_activated (GtkColorChooser *chooser, |
113 | const GdkRGBA *color) |
114 | { |
115 | g_signal_emit (instance: chooser, signal_id: signals[COLOR_ACTIVATED], detail: 0, color); |
116 | } |
117 | |
118 | /** |
119 | * gtk_color_chooser_get_rgba: (attributes org.gtk.Method.get_property=rgba) |
120 | * @chooser: a `GtkColorChooser` |
121 | * @color: (out): a `GdkRGBA` to fill in with the current color |
122 | * |
123 | * Gets the currently-selected color. |
124 | */ |
125 | void |
126 | gtk_color_chooser_get_rgba (GtkColorChooser *chooser, |
127 | GdkRGBA *color) |
128 | { |
129 | g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser)); |
130 | |
131 | GTK_COLOR_CHOOSER_GET_IFACE (chooser)->get_rgba (chooser, color); |
132 | } |
133 | |
134 | /** |
135 | * gtk_color_chooser_set_rgba: (attributes org.gtk.Method.set_property=rgba) |
136 | * @chooser: a `GtkColorChooser` |
137 | * @color: the new color |
138 | * |
139 | * Sets the color. |
140 | */ |
141 | void |
142 | gtk_color_chooser_set_rgba (GtkColorChooser *chooser, |
143 | const GdkRGBA *color) |
144 | { |
145 | g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser)); |
146 | g_return_if_fail (color != NULL); |
147 | |
148 | GTK_COLOR_CHOOSER_GET_IFACE (chooser)->set_rgba (chooser, color); |
149 | } |
150 | |
151 | /** |
152 | * gtk_color_chooser_get_use_alpha: (attributes org.gtk.Method.get_property=use-alpha) |
153 | * @chooser: a `GtkColorChooser` |
154 | * |
155 | * Returns whether the color chooser shows the alpha channel. |
156 | * |
157 | * Returns: %TRUE if the color chooser uses the alpha channel, |
158 | * %FALSE if not |
159 | */ |
160 | gboolean |
161 | gtk_color_chooser_get_use_alpha (GtkColorChooser *chooser) |
162 | { |
163 | gboolean use_alpha; |
164 | |
165 | g_return_val_if_fail (GTK_IS_COLOR_CHOOSER (chooser), TRUE); |
166 | |
167 | g_object_get (object: chooser, first_property_name: "use-alpha" , &use_alpha, NULL); |
168 | |
169 | return use_alpha; |
170 | } |
171 | |
172 | /** |
173 | * gtk_color_chooser_set_use_alpha: (attributes org.gtk.Method.set_property=use-alpha) |
174 | * @chooser: a `GtkColorChooser` |
175 | * @use_alpha: %TRUE if color chooser should use alpha channel, %FALSE if not |
176 | * |
177 | * Sets whether or not the color chooser should use the alpha channel. |
178 | */ |
179 | void |
180 | gtk_color_chooser_set_use_alpha (GtkColorChooser *chooser, |
181 | gboolean use_alpha) |
182 | { |
183 | |
184 | g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser)); |
185 | |
186 | g_object_set (object: chooser, first_property_name: "use-alpha" , use_alpha, NULL); |
187 | } |
188 | |
189 | /** |
190 | * gtk_color_chooser_add_palette: |
191 | * @chooser: a `GtkColorChooser` |
192 | * @orientation: %GTK_ORIENTATION_HORIZONTAL if the palette should |
193 | * be displayed in rows, %GTK_ORIENTATION_VERTICAL for columns |
194 | * @colors_per_line: the number of colors to show in each row/column |
195 | * @n_colors: the total number of elements in @colors |
196 | * @colors: (nullable) (array length=n_colors): the colors of the palette |
197 | * |
198 | * Adds a palette to the color chooser. |
199 | * |
200 | * If @orientation is horizontal, the colors are grouped in rows, |
201 | * with @colors_per_line colors in each row. If @horizontal is %FALSE, |
202 | * the colors are grouped in columns instead. |
203 | * |
204 | * The default color palette of [class@Gtk.ColorChooserWidget] has |
205 | * 45 colors, organized in columns of 5 colors (this includes some |
206 | * grays). |
207 | * |
208 | * The layout of the color chooser widget works best when the |
209 | * palettes have 9-10 columns. |
210 | * |
211 | * Calling this function for the first time has the side effect |
212 | * of removing the default color palette from the color chooser. |
213 | * |
214 | * If @colors is %NULL, removes all previously added palettes. |
215 | */ |
216 | void |
217 | gtk_color_chooser_add_palette (GtkColorChooser *chooser, |
218 | GtkOrientation orientation, |
219 | int colors_per_line, |
220 | int n_colors, |
221 | GdkRGBA *colors) |
222 | { |
223 | g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser)); |
224 | |
225 | if (GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette) |
226 | GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette (chooser, orientation, colors_per_line, n_colors, colors); |
227 | } |
228 | |
229 | void |
230 | _gtk_color_chooser_snapshot_checkered_pattern (GtkSnapshot *snapshot, |
231 | int width, |
232 | int height) |
233 | { |
234 | const GdkRGBA color1 = GDK_RGBA("A8A8A8" ); |
235 | const GdkRGBA color2 = GDK_RGBA("545454" ); |
236 | |
237 | gtk_snapshot_push_repeat (snapshot, bounds: &GRAPHENE_RECT_INIT (0, 0, width, height), NULL); |
238 | gtk_snapshot_append_color (snapshot, color: &color1, bounds: &GRAPHENE_RECT_INIT (0, 0, 10, 10)); |
239 | gtk_snapshot_append_color (snapshot, color: &color2, bounds: &GRAPHENE_RECT_INIT (10, 0, 10, 10)); |
240 | gtk_snapshot_append_color (snapshot, color: &color2, bounds: &GRAPHENE_RECT_INIT (0, 10, 10, 10)); |
241 | gtk_snapshot_append_color (snapshot, color: &color1, bounds: &GRAPHENE_RECT_INIT (10, 10, 10, 10)); |
242 | gtk_snapshot_pop (snapshot); |
243 | } |
244 | |