]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsscustomproperty.c
styleproperty: Make _gtk_style_property_parse_value() return a CssValue
[~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 "gtkthemingengine.h"
28
29 G_DEFINE_TYPE (GtkCssCustomProperty, _gtk_css_custom_property, GTK_TYPE_CSS_STYLE_PROPERTY)
30
31 static GtkCssValue *
32 gtk_css_custom_property_parse_value (GtkStyleProperty *property,
33                                      GtkCssParser     *parser,
34                                      GFile            *base)
35 {
36   GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (property);
37   GValue value = G_VALUE_INIT;
38   GtkCssValue *result;
39   gboolean success;
40
41   if (custom->property_parse_func)
42     {
43       GError *error = NULL;
44       char *value_str;
45       
46       g_value_init (&value, _gtk_style_property_get_value_type (property));
47
48       value_str = _gtk_css_parser_read_value (parser);
49       if (value_str != NULL)
50         {
51           success = (* custom->property_parse_func) (value_str, &value, &error);
52           g_free (value_str);
53         }
54       else
55         success = FALSE;
56     }
57   else
58     {
59       GtkCssStyleProperty *style = GTK_CSS_STYLE_PROPERTY (property);
60       g_value_init (&value, _gtk_css_style_property_get_specified_type (style));
61
62       success = _gtk_css_style_parse_value (&value, parser, base);
63     }
64
65   if (!success)
66     {
67       g_value_unset (&value);
68       return NULL;
69     }
70
71   result = _gtk_css_value_new_from_gvalue (&value);
72   g_value_unset (&value);
73
74   return result;
75 }
76
77 static void
78 _gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
79 {
80   GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
81
82   property_class->parse_value = gtk_css_custom_property_parse_value;
83 }
84
85
86 static void
87 _gtk_css_custom_property_init (GtkCssCustomProperty *custom_property)
88 {
89 }
90
91 static GtkCssValue *
92 gtk_css_custom_property_create_initial_value (GParamSpec *pspec)
93 {
94   GValue value = G_VALUE_INIT;
95   GtkCssValue *result;
96
97   g_value_init (&value, pspec->value_type);
98
99   if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
100     g_value_set_object (&value, gtk_theming_engine_load (NULL));
101   else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
102     g_value_take_boxed (&value, pango_font_description_from_string ("Sans 10"));
103   else if (pspec->value_type == GDK_TYPE_RGBA)
104     {
105       GdkRGBA color;
106       gdk_rgba_parse (&color, "pink");
107       g_value_set_boxed (&value, &color);
108     }
109   else if (pspec->value_type == GDK_TYPE_COLOR)
110     {
111       GdkColor color;
112       gdk_color_parse ("pink", &color);
113       g_value_set_boxed (&value, &color);
114     }
115   else if (pspec->value_type == GTK_TYPE_BORDER)
116     {
117       g_value_take_boxed (&value, gtk_border_new ());
118     }
119   else
120     g_param_value_set_default (pspec, &value);
121
122   result = _gtk_css_value_new_from_gvalue (&value);
123   g_value_unset (&value);
124
125   return result;
126 }
127
128 /* Property registration functions */
129
130 /**
131  * gtk_theming_engine_register_property: (skip)
132  * @name_space: namespace for the property name
133  * @parse_func: parsing function to use, or %NULL
134  * @pspec: the #GParamSpec for the new property
135  *
136  * Registers a property so it can be used in the CSS file format,
137  * on the CSS file the property will look like
138  * "-${@name_space}-${property_name}". being
139  * ${property_name} the given to @pspec. @name_space will usually
140  * be the theme engine name.
141  *
142  * For any type a @parse_func may be provided, being this function
143  * used for turning any property value (between ':' and ';') in
144  * CSS to the #GValue needed. For basic types there is already
145  * builtin parsing support, so %NULL may be provided for these
146  * cases.
147  *
148  * <note>
149  * Engines must ensure property registration happens exactly once,
150  * usually GTK+ deals with theming engines as singletons, so this
151  * should be guaranteed to happen once, but bear this in mind
152  * when creating #GtkThemeEngine<!-- -->s yourself.
153  * </note>
154  *
155  * <note>
156  * In order to make use of the custom registered properties in
157  * the CSS file, make sure the engine is loaded first by specifying
158  * the engine property, either in a previous rule or within the same
159  * one.
160  * <programlisting>
161  * &ast; {
162  *     engine: someengine;
163  *     -SomeEngine-custom-property: 2;
164  * }
165  * </programlisting>
166  * </note>
167  *
168  * Since: 3.0
169  **/
170 void
171 gtk_theming_engine_register_property (const gchar            *name_space,
172                                       GtkStylePropertyParser  parse_func,
173                                       GParamSpec             *pspec)
174 {
175   GtkCssCustomProperty *node;
176   GtkCssValue *initial;
177   gchar *name;
178
179   g_return_if_fail (name_space != NULL);
180   g_return_if_fail (strchr (name_space, ' ') == NULL);
181   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
182
183   name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
184
185   /* This also initializes the default properties */
186   if (_gtk_style_property_lookup (pspec->name))
187     {
188       g_warning ("a property with name '%s' already exists", name);
189       g_free (name);
190       return;
191     }
192   
193   initial = gtk_css_custom_property_create_initial_value (pspec);
194
195   node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
196                        "initial-value", initial,
197                        "name", name,
198                        "computed-type", pspec->value_type,
199                        "value-type", pspec->value_type,
200                        NULL);
201   node->pspec = pspec;
202   node->property_parse_func = parse_func;
203
204   _gtk_css_value_unref (initial);
205   g_free (name);
206 }
207
208 /**
209  * gtk_style_properties_register_property: (skip)
210  * @parse_func: parsing function to use, or %NULL
211  * @pspec: the #GParamSpec for the new property
212  *
213  * Registers a property so it can be used in the CSS file format.
214  * This function is the low-level equivalent of
215  * gtk_theming_engine_register_property(), if you are implementing
216  * a theming engine, you want to use that function instead.
217  *
218  * Since: 3.0
219  **/
220 void
221 gtk_style_properties_register_property (GtkStylePropertyParser  parse_func,
222                                         GParamSpec             *pspec)
223 {
224   GtkCssCustomProperty *node;
225   GtkCssValue *initial;
226
227   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
228
229   /* This also initializes the default properties */
230   if (_gtk_style_property_lookup (pspec->name))
231     {
232       g_warning ("a property with name '%s' already exists", pspec->name);
233       return;
234     }
235   
236   initial = gtk_css_custom_property_create_initial_value (pspec);
237
238   node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
239                        "initial-value", initial,
240                        "name", pspec->name,
241                        "computed-type", pspec->value_type,
242                        "value-type", pspec->value_type,
243                        NULL);
244   node->pspec = pspec;
245   node->property_parse_func = parse_func;
246
247   _gtk_css_value_unref (initial);
248 }
249
250 /**
251  * gtk_style_properties_lookup_property: (skip)
252  * @property_name: property name to look up
253  * @parse_func: (out): return location for the parse function
254  * @pspec: (out) (transfer none): return location for the #GParamSpec
255  *
256  * Returns %TRUE if a property has been registered, if @pspec or
257  * @parse_func are not %NULL, the #GParamSpec and parsing function
258  * will be respectively returned.
259  *
260  * Returns: %TRUE if the property is registered, %FALSE otherwise
261  *
262  * Since: 3.0
263  **/
264 gboolean
265 gtk_style_properties_lookup_property (const gchar             *property_name,
266                                       GtkStylePropertyParser  *parse_func,
267                                       GParamSpec             **pspec)
268 {
269   GtkStyleProperty *node;
270   gboolean found = FALSE;
271
272   g_return_val_if_fail (property_name != NULL, FALSE);
273
274   node = _gtk_style_property_lookup (property_name);
275
276   if (GTK_IS_CSS_CUSTOM_PROPERTY (node))
277     {
278       GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (node);
279
280       if (pspec)
281         *pspec = custom->pspec;
282
283       if (parse_func)
284         *parse_func = custom->property_parse_func;
285
286       found = TRUE;
287     }
288
289   return found;
290 }
291