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,
38 GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (property);
41 g_value_init (value, _gtk_style_property_get_value_type (property));
43 if (custom->property_parse_func)
48 value_str = _gtk_css_parser_read_value (parser);
49 if (value_str != NULL)
51 success = (* custom->property_parse_func) (value_str, value, &error);
58 success = _gtk_css_style_parse_value (value, parser, base);
61 g_value_unset (value);
67 _gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
69 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
71 property_class->parse_value = gtk_css_custom_property_parse_value;
76 _gtk_css_custom_property_init (GtkCssCustomProperty *custom_property)
81 gtk_css_custom_property_create_initial_value (GParamSpec *pspec,
84 g_value_init (value, pspec->value_type);
86 if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
87 g_value_set_object (value, gtk_theming_engine_load (NULL));
88 else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
89 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
90 else if (pspec->value_type == GDK_TYPE_RGBA)
93 gdk_rgba_parse (&color, "pink");
94 g_value_set_boxed (value, &color);
96 else if (pspec->value_type == GTK_TYPE_BORDER)
98 g_value_take_boxed (value, gtk_border_new ());
101 g_param_value_set_default (pspec, value);
104 /* Property registration functions */
107 * gtk_theming_engine_register_property: (skip)
108 * @name_space: namespace for the property name
109 * @parse_func: parsing function to use, or %NULL
110 * @pspec: the #GParamSpec for the new property
112 * Registers a property so it can be used in the CSS file format,
113 * on the CSS file the property will look like
114 * "-${@name_space}-${property_name}". being
115 * ${property_name} the given to @pspec. @name_space will usually
116 * be the theme engine name.
118 * For any type a @parse_func may be provided, being this function
119 * used for turning any property value (between ':' and ';') in
120 * CSS to the #GValue needed. For basic types there is already
121 * builtin parsing support, so %NULL may be provided for these
125 * Engines must ensure property registration happens exactly once,
126 * usually GTK+ deals with theming engines as singletons, so this
127 * should be guaranteed to happen once, but bear this in mind
128 * when creating #GtkThemeEngine<!-- -->s yourself.
132 * In order to make use of the custom registered properties in
133 * the CSS file, make sure the engine is loaded first by specifying
134 * the engine property, either in a previous rule or within the same
138 * engine: someengine;
139 * -SomeEngine-custom-property: 2;
147 gtk_theming_engine_register_property (const gchar *name_space,
148 GtkStylePropertyParser parse_func,
151 GtkCssCustomProperty *node;
152 GValue initial = { 0, };
155 g_return_if_fail (name_space != NULL);
156 g_return_if_fail (strchr (name_space, ' ') == NULL);
157 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
159 name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
160 gtk_css_custom_property_create_initial_value (pspec, &initial);
162 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
163 "initial-value", &initial,
165 "computed-type", pspec->value_type,
166 "value-type", pspec->value_type,
169 node->property_parse_func = parse_func;
171 g_value_unset (&initial);
176 * gtk_style_properties_register_property: (skip)
177 * @parse_func: parsing function to use, or %NULL
178 * @pspec: the #GParamSpec for the new property
180 * Registers a property so it can be used in the CSS file format.
181 * This function is the low-level equivalent of
182 * gtk_theming_engine_register_property(), if you are implementing
183 * a theming engine, you want to use that function instead.
188 gtk_style_properties_register_property (GtkStylePropertyParser parse_func,
191 GtkCssCustomProperty *node;
192 GValue initial = { 0, };
194 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
196 gtk_css_custom_property_create_initial_value (pspec, &initial);
198 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
199 "initial-value", &initial,
201 "computed-type", pspec->value_type,
202 "value-type", pspec->value_type,
205 node->property_parse_func = parse_func;
207 g_value_unset (&initial);
211 * gtk_style_properties_lookup_property: (skip)
212 * @property_name: property name to look up
213 * @parse_func: (out): return location for the parse function
214 * @pspec: (out) (transfer none): return location for the #GParamSpec
216 * Returns %TRUE if a property has been registered, if @pspec or
217 * @parse_func are not %NULL, the #GParamSpec and parsing function
218 * will be respectively returned.
220 * Returns: %TRUE if the property is registered, %FALSE otherwise
225 gtk_style_properties_lookup_property (const gchar *property_name,
226 GtkStylePropertyParser *parse_func,
229 GtkStyleProperty *node;
230 gboolean found = FALSE;
232 g_return_val_if_fail (property_name != NULL, FALSE);
234 node = _gtk_style_property_lookup (property_name);
236 if (GTK_IS_CSS_CUSTOM_PROPERTY (node))
238 GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (node);
241 *pspec = custom->pspec;
244 *parse_func = custom->property_parse_func;