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