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