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_parse_value (GtkStyleProperty *property,
37 GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (property);
40 if (custom->property_parse_func)
45 g_value_init (value, _gtk_style_property_get_value_type (property));
47 value_str = _gtk_css_parser_read_value (parser);
48 if (value_str != NULL)
50 success = (* custom->property_parse_func) (value_str, value, &error);
58 GtkCssStyleProperty *style = GTK_CSS_STYLE_PROPERTY (property);
59 g_value_init (value, _gtk_css_style_property_get_specified_type (style));
61 success = _gtk_css_style_parse_value (value, parser, base);
65 g_value_unset (value);
71 _gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
73 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
75 property_class->parse_value = gtk_css_custom_property_parse_value;
80 _gtk_css_custom_property_init (GtkCssCustomProperty *custom_property)
85 gtk_css_custom_property_get_specified_type (GParamSpec *pspec)
87 if (pspec->value_type == GDK_TYPE_RGBA ||
88 pspec->value_type == GDK_TYPE_COLOR)
89 return GTK_TYPE_SYMBOLIC_COLOR;
91 return pspec->value_type;
95 gtk_css_custom_property_create_initial_value (GParamSpec *pspec,
98 g_value_init (value, gtk_css_custom_property_get_specified_type (pspec));
100 if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
101 g_value_set_object (value, gtk_theming_engine_load (NULL));
102 else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
103 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
104 else if (pspec->value_type == GDK_TYPE_RGBA ||
105 pspec->value_type == GDK_TYPE_COLOR)
108 gdk_rgba_parse (&color, "pink");
109 g_value_take_boxed (value, gtk_symbolic_color_new_literal (&color));
111 else if (pspec->value_type == GTK_TYPE_BORDER)
113 g_value_take_boxed (value, gtk_border_new ());
116 g_param_value_set_default (pspec, value);
119 /* Property registration functions */
122 * gtk_theming_engine_register_property: (skip)
123 * @name_space: namespace for the property name
124 * @parse_func: parsing function to use, or %NULL
125 * @pspec: the #GParamSpec for the new property
127 * Registers a property so it can be used in the CSS file format,
128 * on the CSS file the property will look like
129 * "-${@name_space}-${property_name}". being
130 * ${property_name} the given to @pspec. @name_space will usually
131 * be the theme engine name.
133 * For any type a @parse_func may be provided, being this function
134 * used for turning any property value (between ':' and ';') in
135 * CSS to the #GValue needed. For basic types there is already
136 * builtin parsing support, so %NULL may be provided for these
140 * Engines must ensure property registration happens exactly once,
141 * usually GTK+ deals with theming engines as singletons, so this
142 * should be guaranteed to happen once, but bear this in mind
143 * when creating #GtkThemeEngine<!-- -->s yourself.
147 * In order to make use of the custom registered properties in
148 * the CSS file, make sure the engine is loaded first by specifying
149 * the engine property, either in a previous rule or within the same
153 * engine: someengine;
154 * -SomeEngine-custom-property: 2;
162 gtk_theming_engine_register_property (const gchar *name_space,
163 GtkStylePropertyParser parse_func,
166 GtkCssCustomProperty *node;
167 GValue initial = { 0, };
170 g_return_if_fail (name_space != NULL);
171 g_return_if_fail (strchr (name_space, ' ') == NULL);
172 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
174 name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
175 gtk_css_custom_property_create_initial_value (pspec, &initial);
177 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
178 "initial-value", &initial,
180 "computed-type", pspec->value_type,
181 "value-type", pspec->value_type,
184 node->property_parse_func = parse_func;
186 g_value_unset (&initial);
191 * gtk_style_properties_register_property: (skip)
192 * @parse_func: parsing function to use, or %NULL
193 * @pspec: the #GParamSpec for the new property
195 * Registers a property so it can be used in the CSS file format.
196 * This function is the low-level equivalent of
197 * gtk_theming_engine_register_property(), if you are implementing
198 * a theming engine, you want to use that function instead.
203 gtk_style_properties_register_property (GtkStylePropertyParser parse_func,
206 GtkCssCustomProperty *node;
207 GValue initial = { 0, };
209 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
211 gtk_css_custom_property_create_initial_value (pspec, &initial);
213 node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
214 "initial-value", &initial,
216 "computed-type", pspec->value_type,
217 "value-type", pspec->value_type,
220 node->property_parse_func = parse_func;
222 g_value_unset (&initial);
226 * gtk_style_properties_lookup_property: (skip)
227 * @property_name: property name to look up
228 * @parse_func: (out): return location for the parse function
229 * @pspec: (out) (transfer none): return location for the #GParamSpec
231 * Returns %TRUE if a property has been registered, if @pspec or
232 * @parse_func are not %NULL, the #GParamSpec and parsing function
233 * will be respectively returned.
235 * Returns: %TRUE if the property is registered, %FALSE otherwise
240 gtk_style_properties_lookup_property (const gchar *property_name,
241 GtkStylePropertyParser *parse_func,
244 GtkStyleProperty *node;
245 gboolean found = FALSE;
247 g_return_val_if_fail (property_name != NULL, FALSE);
249 node = _gtk_style_property_lookup (property_name);
251 if (GTK_IS_CSS_CUSTOM_PROPERTY (node))
253 GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (node);
256 *pspec = custom->pspec;
259 *parse_func = custom->property_parse_func;