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