2 * Copyright © 2011 Red Hat Inc.
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.
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.
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/>.
17 * Authors: Benjamin Otte <otte@gnome.org>
22 #include "gtkcsscustompropertyprivate.h"
26 #include "gtkcssstylefuncsprivate.h"
27 #include "gtkcsstypedvalueprivate.h"
28 #include "gtkstylepropertiesprivate.h"
29 #include "gtksymboliccolor.h"
30 #include "gtkthemingengine.h"
32 G_DEFINE_TYPE (GtkCssCustomProperty, _gtk_css_custom_property, GTK_TYPE_CSS_STYLE_PROPERTY)
35 gtk_css_custom_property_get_specified_type (GParamSpec *pspec)
37 if (pspec->value_type == GDK_TYPE_RGBA ||
38 pspec->value_type == GDK_TYPE_COLOR)
39 return GTK_TYPE_SYMBOLIC_COLOR;
41 return pspec->value_type;
45 gtk_css_custom_property_parse_value (GtkStyleProperty *property,
48 GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (property);
49 GValue value = G_VALUE_INIT;
52 if (custom->property_parse_func)
57 g_value_init (&value, _gtk_style_property_get_value_type (property));
59 value_str = _gtk_css_parser_read_value (parser);
60 if (value_str != NULL)
62 success = (* custom->property_parse_func) (value_str, &value, &error);
70 g_value_init (&value, gtk_css_custom_property_get_specified_type (custom->pspec));
72 success = _gtk_css_style_parse_value (&value, parser);
77 g_value_unset (&value);
81 return _gtk_css_typed_value_new_take (&value);
85 gtk_css_custom_property_query (GtkStyleProperty *property,
87 GtkStyleQueryFunc query_func,
90 GtkCssStyleProperty *style = GTK_CSS_STYLE_PROPERTY (property);
91 GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (property);
92 GtkCssValue *css_value;
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);
98 g_value_init (value, custom->pspec->value_type);
99 g_value_copy (_gtk_css_typed_value_get (css_value), value);
103 gtk_css_custom_property_assign (GtkStyleProperty *property,
104 GtkStyleProperties *props,
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),
113 _gtk_css_value_unref (css_value);
117 _gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
119 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
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;
127 _gtk_css_custom_property_init (GtkCssCustomProperty *custom)
132 gtk_css_custom_property_create_initial_value (GParamSpec *pspec)
134 GValue value = G_VALUE_INIT;
137 g_value_init (&value, pspec->value_type);
139 if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
140 g_value_set_object (&value, gtk_theming_engine_load (NULL));
141 else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
142 g_value_take_boxed (&value, pango_font_description_from_string ("Sans 10"));
143 else if (pspec->value_type == GDK_TYPE_RGBA)
146 gdk_rgba_parse (&color, "pink");
147 g_value_set_boxed (&value, &color);
149 else if (pspec->value_type == GDK_TYPE_COLOR)
152 gdk_color_parse ("pink", &color);
153 g_value_set_boxed (&value, &color);
155 else if (pspec->value_type == GTK_TYPE_BORDER)
157 g_value_take_boxed (&value, gtk_border_new ());
160 g_param_value_set_default (pspec, &value);
162 result = _gtk_css_typed_value_new (&value);
163 g_value_unset (&value);
168 /* Property registration functions */
171 * gtk_theming_engine_register_property: (skip)
172 * @name_space: namespace for the property name
173 * @parse_func: parsing function to use, or %NULL
174 * @pspec: the #GParamSpec for the new property
176 * Registers a property so it can be used in the CSS file format,
177 * on the CSS file the property will look like
178 * "-${@name_space}-${property_name}". being
179 * ${property_name} the given to @pspec. @name_space will usually
180 * be the theme engine name.
182 * For any type a @parse_func may be provided, being this function
183 * used for turning any property value (between ':' and ';') in
184 * CSS to the #GValue needed. For basic types there is already
185 * builtin parsing support, so %NULL may be provided for these
189 * Engines must ensure property registration happens exactly once,
190 * usually GTK+ deals with theming engines as singletons, so this
191 * should be guaranteed to happen once, but bear this in mind
192 * when creating #GtkThemeEngine<!-- -->s yourself.
196 * In order to make use of the custom registered properties in
197 * the CSS file, make sure the engine is loaded first by specifying
198 * the engine property, either in a previous rule or within the same
202 * engine: someengine;
203 * -SomeEngine-custom-property: 2;
211 gtk_theming_engine_register_property (const gchar *name_space,
212 GtkStylePropertyParser parse_func,
215 GtkCssCustomProperty *node;
216 GtkCssValue *initial;
219 g_return_if_fail (name_space != NULL);
220 g_return_if_fail (strchr (name_space, ' ') == NULL);
221 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
223 name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
225 /* This also initializes the default properties */
226 if (_gtk_style_property_lookup (pspec->name))
228 g_warning ("a property with name '%s' already exists", name);
233 initial = gtk_css_custom_property_create_initial_value (pspec);
235 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
236 "initial-value", initial,
238 "value-type", pspec->value_type,
241 node->property_parse_func = parse_func;
243 _gtk_css_value_unref (initial);
248 * gtk_style_properties_register_property: (skip)
249 * @parse_func: parsing function to use, or %NULL
250 * @pspec: the #GParamSpec for the new property
252 * Registers a property so it can be used in the CSS file format.
253 * This function is the low-level equivalent of
254 * gtk_theming_engine_register_property(), if you are implementing
255 * a theming engine, you want to use that function instead.
260 gtk_style_properties_register_property (GtkStylePropertyParser parse_func,
263 GtkCssCustomProperty *node;
264 GtkCssValue *initial;
266 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
268 /* This also initializes the default properties */
269 if (_gtk_style_property_lookup (pspec->name))
271 g_warning ("a property with name '%s' already exists", pspec->name);
275 initial = gtk_css_custom_property_create_initial_value (pspec);
277 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
278 "initial-value", initial,
280 "value-type", pspec->value_type,
283 node->property_parse_func = parse_func;
285 _gtk_css_value_unref (initial);
289 * gtk_style_properties_lookup_property: (skip)
290 * @property_name: property name to look up
291 * @parse_func: (out): return location for the parse function
292 * @pspec: (out) (transfer none): return location for the #GParamSpec
294 * Returns %TRUE if a property has been registered, if @pspec or
295 * @parse_func are not %NULL, the #GParamSpec and parsing function
296 * will be respectively returned.
298 * Returns: %TRUE if the property is registered, %FALSE otherwise
303 gtk_style_properties_lookup_property (const gchar *property_name,
304 GtkStylePropertyParser *parse_func,
307 GtkStyleProperty *node;
308 gboolean found = FALSE;
310 g_return_val_if_fail (property_name != NULL, FALSE);
312 node = _gtk_style_property_lookup (property_name);
314 if (GTK_IS_CSS_CUSTOM_PROPERTY (node))
316 GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (node);
319 *pspec = custom->pspec;
322 *parse_func = custom->property_parse_func;