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