]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsscustomproperty.c
symboliccolor: Remove extra includes
[~andy/gtk] / gtk / gtkcsscustomproperty.c
1 /*
2  * Copyright © 2011 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Benjamin Otte <otte@gnome.org>
18  */
19
20 #include "config.h"
21
22 #include "gtkcsscustompropertyprivate.h"
23
24 #include <string.h>
25
26 #include "gtkcssstylefuncsprivate.h"
27 #include "gtkcsstypedvalueprivate.h"
28 #include "gtkstylepropertiesprivate.h"
29 #include "gtksymboliccolor.h"
30 #include "gtkthemingengine.h"
31
32 G_DEFINE_TYPE (GtkCssCustomProperty, _gtk_css_custom_property, GTK_TYPE_CSS_STYLE_PROPERTY)
33
34 static GType
35 gtk_css_custom_property_get_specified_type (GParamSpec *pspec)
36 {
37   if (pspec->value_type == GDK_TYPE_RGBA ||
38       pspec->value_type == GDK_TYPE_COLOR)
39     return GTK_TYPE_SYMBOLIC_COLOR;
40   else
41     return pspec->value_type;
42 }
43
44 static GtkCssValue *
45 gtk_css_custom_property_parse_value (GtkStyleProperty *property,
46                                      GtkCssParser     *parser)
47 {
48   GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (property);
49   GValue value = G_VALUE_INIT;
50   gboolean success;
51
52   if (custom->property_parse_func)
53     {
54       GError *error = NULL;
55       char *value_str;
56       
57       g_value_init (&value, _gtk_style_property_get_value_type (property));
58
59       value_str = _gtk_css_parser_read_value (parser);
60       if (value_str != NULL)
61         {
62           success = (* custom->property_parse_func) (value_str, &value, &error);
63           g_free (value_str);
64         }
65       else
66         success = FALSE;
67     }
68   else
69     {
70       g_value_init (&value, gtk_css_custom_property_get_specified_type (custom->pspec));
71
72       success = _gtk_css_style_parse_value (&value, parser);
73     }
74
75   if (!success)
76     {
77       g_value_unset (&value);
78       return NULL;
79     }
80
81   return _gtk_css_typed_value_new_take (&value);
82 }
83
84 static void
85 gtk_css_custom_property_query (GtkStyleProperty   *property,
86                                GValue             *value,
87                                GtkStyleQueryFunc   query_func,
88                                gpointer            query_data)
89 {
90   GtkCssStyleProperty *style = GTK_CSS_STYLE_PROPERTY (property);
91   GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (property);
92   GtkCssValue *css_value;
93   
94   css_value = (* query_func) (_gtk_css_style_property_get_id (style), query_data);
95   if (css_value == NULL)
96     css_value = _gtk_css_style_property_get_initial_value (style);
97
98   g_value_init (value, custom->pspec->value_type);
99   g_value_copy (_gtk_css_typed_value_get (css_value), value);
100 }
101
102 static void
103 gtk_css_custom_property_assign (GtkStyleProperty   *property,
104                                 GtkStyleProperties *props,
105                                 GtkStateFlags       state,
106                                 const GValue       *value)
107 {
108   GtkCssValue *css_value = _gtk_css_typed_value_new (value);
109   _gtk_style_properties_set_property_by_property (props,
110                                                   GTK_CSS_STYLE_PROPERTY (property),
111                                                   state,
112                                                   css_value);
113   _gtk_css_value_unref (css_value);
114 }
115
116 static void
117 _gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
118 {
119   GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
120
121   property_class->parse_value = gtk_css_custom_property_parse_value;
122   property_class->query = gtk_css_custom_property_query;
123   property_class->assign = gtk_css_custom_property_assign;
124 }
125
126 static void
127 _gtk_css_custom_property_init (GtkCssCustomProperty *custom)
128 {
129 }
130
131 static GtkCssValue *
132 gtk_css_custom_property_create_initial_value (GParamSpec *pspec)
133 {
134   GValue value = G_VALUE_INIT;
135   GtkCssValue *result;
136
137   g_value_init (&value, pspec->value_type);
138
139   if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
140     g_value_set_object (&value, gtk_theming_engine_load (NULL));
141   else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
142     g_value_take_boxed (&value, pango_font_description_from_string ("Sans 10"));
143   else if (pspec->value_type == GDK_TYPE_RGBA)
144     {
145       GdkRGBA color;
146       gdk_rgba_parse (&color, "pink");
147       g_value_set_boxed (&value, &color);
148     }
149   else if (pspec->value_type == GDK_TYPE_COLOR)
150     {
151       GdkColor color;
152       gdk_color_parse ("pink", &color);
153       g_value_set_boxed (&value, &color);
154     }
155   else if (pspec->value_type == GTK_TYPE_BORDER)
156     {
157       g_value_take_boxed (&value, gtk_border_new ());
158     }
159   else
160     g_param_value_set_default (pspec, &value);
161
162   result = _gtk_css_typed_value_new (&value);
163   g_value_unset (&value);
164
165   return result;
166 }
167
168 /* Property registration functions */
169
170 /**
171  * gtk_theming_engine_register_property: (skip)
172  * @name_space: namespace for the property name
173  * @parse_func: parsing function to use, or %NULL
174  * @pspec: the #GParamSpec for the new property
175  *
176  * Registers a property so it can be used in the CSS file format,
177  * on the CSS file the property will look like
178  * "-${@name_space}-${property_name}". being
179  * ${property_name} the given to @pspec. @name_space will usually
180  * be the theme engine name.
181  *
182  * For any type a @parse_func may be provided, being this function
183  * used for turning any property value (between ':' and ';') in
184  * CSS to the #GValue needed. For basic types there is already
185  * builtin parsing support, so %NULL may be provided for these
186  * cases.
187  *
188  * <note>
189  * Engines must ensure property registration happens exactly once,
190  * usually GTK+ deals with theming engines as singletons, so this
191  * should be guaranteed to happen once, but bear this in mind
192  * when creating #GtkThemeEngine<!-- -->s yourself.
193  * </note>
194  *
195  * <note>
196  * In order to make use of the custom registered properties in
197  * the CSS file, make sure the engine is loaded first by specifying
198  * the engine property, either in a previous rule or within the same
199  * one.
200  * <programlisting>
201  * &ast; {
202  *     engine: someengine;
203  *     -SomeEngine-custom-property: 2;
204  * }
205  * </programlisting>
206  * </note>
207  *
208  * Since: 3.0
209  **/
210 void
211 gtk_theming_engine_register_property (const gchar            *name_space,
212                                       GtkStylePropertyParser  parse_func,
213                                       GParamSpec             *pspec)
214 {
215   GtkCssCustomProperty *node;
216   GtkCssValue *initial;
217   gchar *name;
218
219   g_return_if_fail (name_space != NULL);
220   g_return_if_fail (strchr (name_space, ' ') == NULL);
221   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
222
223   name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
224
225   /* This also initializes the default properties */
226   if (_gtk_style_property_lookup (pspec->name))
227     {
228       g_warning ("a property with name '%s' already exists", name);
229       g_free (name);
230       return;
231     }
232   
233   initial = gtk_css_custom_property_create_initial_value (pspec);
234
235   node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
236                        "initial-value", initial,
237                        "name", name,
238                        "value-type", pspec->value_type,
239                        NULL);
240   node->pspec = pspec;
241   node->property_parse_func = parse_func;
242
243   _gtk_css_value_unref (initial);
244   g_free (name);
245 }
246
247 /**
248  * gtk_style_properties_register_property: (skip)
249  * @parse_func: parsing function to use, or %NULL
250  * @pspec: the #GParamSpec for the new property
251  *
252  * Registers a property so it can be used in the CSS file format.
253  * This function is the low-level equivalent of
254  * gtk_theming_engine_register_property(), if you are implementing
255  * a theming engine, you want to use that function instead.
256  *
257  * Since: 3.0
258  **/
259 void
260 gtk_style_properties_register_property (GtkStylePropertyParser  parse_func,
261                                         GParamSpec             *pspec)
262 {
263   GtkCssCustomProperty *node;
264   GtkCssValue *initial;
265
266   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
267
268   /* This also initializes the default properties */
269   if (_gtk_style_property_lookup (pspec->name))
270     {
271       g_warning ("a property with name '%s' already exists", pspec->name);
272       return;
273     }
274   
275   initial = gtk_css_custom_property_create_initial_value (pspec);
276
277   node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
278                        "initial-value", initial,
279                        "name", pspec->name,
280                        "value-type", pspec->value_type,
281                        NULL);
282   node->pspec = pspec;
283   node->property_parse_func = parse_func;
284
285   _gtk_css_value_unref (initial);
286 }
287
288 /**
289  * gtk_style_properties_lookup_property: (skip)
290  * @property_name: property name to look up
291  * @parse_func: (out): return location for the parse function
292  * @pspec: (out) (transfer none): return location for the #GParamSpec
293  *
294  * Returns %TRUE if a property has been registered, if @pspec or
295  * @parse_func are not %NULL, the #GParamSpec and parsing function
296  * will be respectively returned.
297  *
298  * Returns: %TRUE if the property is registered, %FALSE otherwise
299  *
300  * Since: 3.0
301  **/
302 gboolean
303 gtk_style_properties_lookup_property (const gchar             *property_name,
304                                       GtkStylePropertyParser  *parse_func,
305                                       GParamSpec             **pspec)
306 {
307   GtkStyleProperty *node;
308   gboolean found = FALSE;
309
310   g_return_val_if_fail (property_name != NULL, FALSE);
311
312   node = _gtk_style_property_lookup (property_name);
313
314   if (GTK_IS_CSS_CUSTOM_PROPERTY (node))
315     {
316       GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (node);
317
318       if (pspec)
319         *pspec = custom->pspec;
320
321       if (parse_func)
322         *parse_func = custom->property_parse_func;
323
324       found = TRUE;
325     }
326
327   return found;
328 }
329