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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Benjamin Otte <otte@gnome.org>
23 #include "gtkcsscustompropertyprivate.h"
27 #include "gtkcssstylefuncsprivate.h"
28 #include "gtkthemingengine.h"
30 G_DEFINE_TYPE (GtkCssCustomProperty, _gtk_css_custom_property, GTK_TYPE_CSS_STYLE_PROPERTY)
33 gtk_css_custom_property_parse_value (GtkStyleProperty *property,
40 g_value_init (value, _gtk_style_property_get_value_type (property));
42 if (property->property_parse_func)
47 value_str = _gtk_css_parser_read_value (parser);
48 if (value_str != NULL)
50 success = (*property->property_parse_func) (value_str, value, &error);
57 success = _gtk_css_style_parse_value (value, parser, base);
60 g_value_unset (value);
66 _gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
68 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
70 property_class->parse_value = gtk_css_custom_property_parse_value;
75 _gtk_css_custom_property_init (GtkCssCustomProperty *custom_property)
80 gtk_css_custom_property_create_initial_value (GParamSpec *pspec,
83 g_value_init (value, pspec->value_type);
85 if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
86 g_value_set_object (value, gtk_theming_engine_load (NULL));
87 else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
88 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
89 else if (pspec->value_type == GDK_TYPE_RGBA)
92 gdk_rgba_parse (&color, "pink");
93 g_value_set_boxed (value, &color);
95 else if (pspec->value_type == GTK_TYPE_BORDER)
97 g_value_take_boxed (value, gtk_border_new ());
100 g_param_value_set_default (pspec, value);
103 /* Property registration functions */
106 * gtk_theming_engine_register_property: (skip)
107 * @name_space: namespace for the property name
108 * @parse_func: parsing function to use, or %NULL
109 * @pspec: the #GParamSpec for the new property
111 * Registers a property so it can be used in the CSS file format,
112 * on the CSS file the property will look like
113 * "-${@name_space}-${property_name}". being
114 * ${property_name} the given to @pspec. @name_space will usually
115 * be the theme engine name.
117 * For any type a @parse_func may be provided, being this function
118 * used for turning any property value (between ':' and ';') in
119 * CSS to the #GValue needed. For basic types there is already
120 * builtin parsing support, so %NULL may be provided for these
124 * Engines must ensure property registration happens exactly once,
125 * usually GTK+ deals with theming engines as singletons, so this
126 * should be guaranteed to happen once, but bear this in mind
127 * when creating #GtkThemeEngine<!-- -->s yourself.
131 * In order to make use of the custom registered properties in
132 * the CSS file, make sure the engine is loaded first by specifying
133 * the engine property, either in a previous rule or within the same
137 * engine: someengine;
138 * -SomeEngine-custom-property: 2;
146 gtk_theming_engine_register_property (const gchar *name_space,
147 GtkStylePropertyParser parse_func,
150 GtkStyleProperty *node;
151 GValue initial = { 0, };
154 g_return_if_fail (name_space != NULL);
155 g_return_if_fail (strchr (name_space, ' ') == NULL);
156 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
158 name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
159 gtk_css_custom_property_create_initial_value (pspec, &initial);
161 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
162 "initial-value", &initial,
164 "value-type", pspec->value_type,
166 GTK_CSS_STYLE_PROPERTY (node)->pspec = pspec;
167 node->property_parse_func = parse_func;
169 g_value_unset (&initial);
174 * gtk_style_properties_register_property: (skip)
175 * @parse_func: parsing function to use, or %NULL
176 * @pspec: the #GParamSpec for the new property
178 * Registers a property so it can be used in the CSS file format.
179 * This function is the low-level equivalent of
180 * gtk_theming_engine_register_property(), if you are implementing
181 * a theming engine, you want to use that function instead.
186 gtk_style_properties_register_property (GtkStylePropertyParser parse_func,
189 GtkStyleProperty *node;
190 GValue initial = { 0, };
192 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
194 gtk_css_custom_property_create_initial_value (pspec, &initial);
196 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
197 "initial-value", &initial,
199 "value-type", pspec->value_type,
201 GTK_CSS_STYLE_PROPERTY (node)->pspec = pspec;
202 node->property_parse_func = parse_func;
204 g_value_unset (&initial);
208 * gtk_style_properties_lookup_property: (skip)
209 * @property_name: property name to look up
210 * @parse_func: (out): return location for the parse function
211 * @pspec: (out) (transfer none): return location for the #GParamSpec
213 * Returns %TRUE if a property has been registered, if @pspec or
214 * @parse_func are not %NULL, the #GParamSpec and parsing function
215 * will be respectively returned.
217 * Returns: %TRUE if the property is registered, %FALSE otherwise
222 gtk_style_properties_lookup_property (const gchar *property_name,
223 GtkStylePropertyParser *parse_func,
226 GtkStyleProperty *node;
227 gboolean found = FALSE;
229 g_return_val_if_fail (property_name != NULL, FALSE);
231 node = _gtk_style_property_lookup (property_name);
233 if (GTK_IS_CSS_CUSTOM_PROPERTY (node))
236 *pspec = GTK_CSS_STYLE_PROPERTY (node)->pspec;
239 *parse_func = node->property_parse_func;