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 "gtkthemingengine.h"
29 G_DEFINE_TYPE (GtkCssCustomProperty, _gtk_css_custom_property, GTK_TYPE_CSS_STYLE_PROPERTY)
32 gtk_css_custom_property_get_specified_type (GParamSpec *pspec)
34 if (pspec->value_type == GDK_TYPE_RGBA ||
35 pspec->value_type == GDK_TYPE_COLOR)
36 return GTK_TYPE_SYMBOLIC_COLOR;
38 return pspec->value_type;
42 gtk_css_custom_property_parse_value (GtkStyleProperty *property,
46 GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (property);
47 GValue value = G_VALUE_INIT;
51 if (custom->property_parse_func)
56 g_value_init (&value, _gtk_style_property_get_value_type (property));
58 value_str = _gtk_css_parser_read_value (parser);
59 if (value_str != NULL)
61 success = (* custom->property_parse_func) (value_str, &value, &error);
69 g_value_init (&value, gtk_css_custom_property_get_specified_type (custom->pspec));
71 success = _gtk_css_style_parse_value (&value, parser, base);
76 g_value_unset (&value);
80 result = _gtk_css_value_new_from_gvalue (&value);
81 g_value_unset (&value);
87 _gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
89 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
91 property_class->parse_value = gtk_css_custom_property_parse_value;
96 _gtk_css_custom_property_init (GtkCssCustomProperty *custom_property)
101 gtk_css_custom_property_create_initial_value (GParamSpec *pspec)
103 GValue value = G_VALUE_INIT;
106 g_value_init (&value, pspec->value_type);
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)
115 gdk_rgba_parse (&color, "pink");
116 g_value_set_boxed (&value, &color);
118 else if (pspec->value_type == GDK_TYPE_COLOR)
121 gdk_color_parse ("pink", &color);
122 g_value_set_boxed (&value, &color);
124 else if (pspec->value_type == GTK_TYPE_BORDER)
126 g_value_take_boxed (&value, gtk_border_new ());
129 g_param_value_set_default (pspec, &value);
131 result = _gtk_css_value_new_from_gvalue (&value);
132 g_value_unset (&value);
137 /* Property registration functions */
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
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.
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
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.
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
171 * engine: someengine;
172 * -SomeEngine-custom-property: 2;
180 gtk_theming_engine_register_property (const gchar *name_space,
181 GtkStylePropertyParser parse_func,
184 GtkCssCustomProperty *node;
185 GtkCssValue *initial;
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));
192 name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
194 /* This also initializes the default properties */
195 if (_gtk_style_property_lookup (pspec->name))
197 g_warning ("a property with name '%s' already exists", name);
202 initial = gtk_css_custom_property_create_initial_value (pspec);
204 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
205 "initial-value", initial,
207 "computed-type", pspec->value_type,
208 "value-type", pspec->value_type,
211 node->property_parse_func = parse_func;
213 _gtk_css_value_unref (initial);
218 * gtk_style_properties_register_property: (skip)
219 * @parse_func: parsing function to use, or %NULL
220 * @pspec: the #GParamSpec for the new property
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.
230 gtk_style_properties_register_property (GtkStylePropertyParser parse_func,
233 GtkCssCustomProperty *node;
234 GtkCssValue *initial;
236 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
238 /* This also initializes the default properties */
239 if (_gtk_style_property_lookup (pspec->name))
241 g_warning ("a property with name '%s' already exists", pspec->name);
245 initial = gtk_css_custom_property_create_initial_value (pspec);
247 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
248 "initial-value", initial,
250 "computed-type", pspec->value_type,
251 "value-type", pspec->value_type,
254 node->property_parse_func = parse_func;
256 _gtk_css_value_unref (initial);
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
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.
269 * Returns: %TRUE if the property is registered, %FALSE otherwise
274 gtk_style_properties_lookup_property (const gchar *property_name,
275 GtkStylePropertyParser *parse_func,
278 GtkStyleProperty *node;
279 gboolean found = FALSE;
281 g_return_val_if_fail (property_name != NULL, FALSE);
283 node = _gtk_style_property_lookup (property_name);
285 if (GTK_IS_CSS_CUSTOM_PROPERTY (node))
287 GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (node);
290 *pspec = custom->pspec;
293 *parse_func = custom->property_parse_func;