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 if (custom->property_parse_func)
46 g_value_init (value, _gtk_style_property_get_value_type (property));
48 value_str = _gtk_css_parser_read_value (parser);
49 if (value_str != NULL)
51 success = (* custom->property_parse_func) (value_str, value, &error);
59 GtkCssStyleProperty *style = GTK_CSS_STYLE_PROPERTY (property);
60 g_value_init (value, _gtk_css_style_property_get_specified_type (style));
62 success = _gtk_css_style_parse_value (value, parser, base);
66 g_value_unset (value);
72 _gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
74 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
76 property_class->parse_value = gtk_css_custom_property_parse_value;
81 _gtk_css_custom_property_init (GtkCssCustomProperty *custom_property)
86 gtk_css_custom_property_get_specified_type (GParamSpec *pspec)
88 if (pspec->value_type == GDK_TYPE_RGBA ||
89 pspec->value_type == GDK_TYPE_COLOR)
90 return GTK_TYPE_SYMBOLIC_COLOR;
92 return pspec->value_type;
96 gtk_css_custom_property_create_initial_value (GParamSpec *pspec,
99 g_value_init (value, gtk_css_custom_property_get_specified_type (pspec));
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)
109 gdk_rgba_parse (&color, "pink");
110 g_value_take_boxed (value, gtk_symbolic_color_new_literal (&color));
112 else if (pspec->value_type == GTK_TYPE_BORDER)
114 g_value_take_boxed (value, gtk_border_new ());
117 g_param_value_set_default (pspec, value);
120 /* Property registration functions */
123 * gtk_theming_engine_register_property: (skip)
124 * @name_space: namespace for the property name
125 * @parse_func: parsing function to use, or %NULL
126 * @pspec: the #GParamSpec for the new property
128 * Registers a property so it can be used in the CSS file format,
129 * on the CSS file the property will look like
130 * "-${@name_space}-${property_name}". being
131 * ${property_name} the given to @pspec. @name_space will usually
132 * be the theme engine name.
134 * For any type a @parse_func may be provided, being this function
135 * used for turning any property value (between ':' and ';') in
136 * CSS to the #GValue needed. For basic types there is already
137 * builtin parsing support, so %NULL may be provided for these
141 * Engines must ensure property registration happens exactly once,
142 * usually GTK+ deals with theming engines as singletons, so this
143 * should be guaranteed to happen once, but bear this in mind
144 * when creating #GtkThemeEngine<!-- -->s yourself.
148 * In order to make use of the custom registered properties in
149 * the CSS file, make sure the engine is loaded first by specifying
150 * the engine property, either in a previous rule or within the same
154 * engine: someengine;
155 * -SomeEngine-custom-property: 2;
163 gtk_theming_engine_register_property (const gchar *name_space,
164 GtkStylePropertyParser parse_func,
167 GtkCssCustomProperty *node;
168 GValue initial = { 0, };
171 g_return_if_fail (name_space != NULL);
172 g_return_if_fail (strchr (name_space, ' ') == NULL);
173 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
175 name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
176 gtk_css_custom_property_create_initial_value (pspec, &initial);
178 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
179 "initial-value", &initial,
181 "computed-type", pspec->value_type,
182 "value-type", pspec->value_type,
185 node->property_parse_func = parse_func;
187 g_value_unset (&initial);
192 * gtk_style_properties_register_property: (skip)
193 * @parse_func: parsing function to use, or %NULL
194 * @pspec: the #GParamSpec for the new property
196 * Registers a property so it can be used in the CSS file format.
197 * This function is the low-level equivalent of
198 * gtk_theming_engine_register_property(), if you are implementing
199 * a theming engine, you want to use that function instead.
204 gtk_style_properties_register_property (GtkStylePropertyParser parse_func,
207 GtkCssCustomProperty *node;
208 GValue initial = { 0, };
210 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
212 gtk_css_custom_property_create_initial_value (pspec, &initial);
214 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
215 "initial-value", &initial,
217 "computed-type", pspec->value_type,
218 "value-type", pspec->value_type,
221 node->property_parse_func = parse_func;
223 g_value_unset (&initial);
227 * gtk_style_properties_lookup_property: (skip)
228 * @property_name: property name to look up
229 * @parse_func: (out): return location for the parse function
230 * @pspec: (out) (transfer none): return location for the #GParamSpec
232 * Returns %TRUE if a property has been registered, if @pspec or
233 * @parse_func are not %NULL, the #GParamSpec and parsing function
234 * will be respectively returned.
236 * Returns: %TRUE if the property is registered, %FALSE otherwise
241 gtk_style_properties_lookup_property (const gchar *property_name,
242 GtkStylePropertyParser *parse_func,
245 GtkStyleProperty *node;
246 gboolean found = FALSE;
248 g_return_val_if_fail (property_name != NULL, FALSE);
250 node = _gtk_style_property_lookup (property_name);
252 if (GTK_IS_CSS_CUSTOM_PROPERTY (node))
254 GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (node);
257 *pspec = custom->pspec;
260 *parse_func = custom->property_parse_func;