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