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