1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
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 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
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkstylepropertyprivate.h"
24 #include "gtkcssprovider.h"
25 #include "gtkcssparserprivate.h"
26 #include "gtkcssshorthandpropertyprivate.h"
27 #include "gtkcssstylefuncsprivate.h"
28 #include "gtkcssstylepropertyprivate.h"
29 #include "gtkcsstypesprivate.h"
31 #include "gtkprivatetypebuiltins.h"
32 #include "gtkstylepropertiesprivate.h"
40 G_DEFINE_ABSTRACT_TYPE (GtkStyleProperty, _gtk_style_property, G_TYPE_OBJECT)
43 gtk_style_property_finalize (GObject *object)
45 GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
47 g_warning ("finalizing %s `%s', how could this happen?", G_OBJECT_TYPE_NAME (object), property->name);
49 G_OBJECT_CLASS (_gtk_style_property_parent_class)->finalize (object);
53 gtk_style_property_set_property (GObject *object,
58 GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
59 GtkStylePropertyClass *klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
64 property->name = g_value_dup_string (value);
65 g_assert (property->name);
66 g_assert (g_hash_table_lookup (klass->properties, property->name) == NULL);
67 g_hash_table_insert (klass->properties, property->name, property);
70 property->value_type = g_value_get_gtype (value);
73 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
79 gtk_style_property_get_property (GObject *object,
84 GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
89 g_value_set_string (value, property->name);
92 g_value_set_gtype (value, property->value_type);
95 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
101 _gtk_style_property_class_init (GtkStylePropertyClass *klass)
103 GObjectClass *object_class = G_OBJECT_CLASS (klass);
105 object_class->finalize = gtk_style_property_finalize;
106 object_class->set_property = gtk_style_property_set_property;
107 object_class->get_property = gtk_style_property_get_property;
109 g_object_class_install_property (object_class,
111 g_param_spec_string ("name",
113 P_("The name of the property"),
115 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
116 g_object_class_install_property (object_class,
118 g_param_spec_gtype ("value-type",
120 P_("The value type returned by GtkStyleContext"),
122 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
124 klass->properties = g_hash_table_new (g_str_hash, g_str_equal);
128 _gtk_style_property_init (GtkStyleProperty *property)
130 property->value_type = G_TYPE_NONE;
134 * _gtk_style_property_parse_value:
135 * @property: the property
136 * @value: an uninitialized value
137 * @parser: the parser to parse from
138 * @base: the base file for @aprser
140 * Tries to parse the given @property from the given @parser into
141 * @value. The type that @value will be assigned is dependant on
142 * the parser and no assumptions must be made about it. If the
143 * parsing fails, %FALSE will be returned and @value will be
144 * left uninitialized.
146 * Only if @property is a #GtkCssShorthandProperty, the @value will
147 * always contain a #GValueArray with the values to be used for
150 * Returns: %TRUE on success
153 _gtk_style_property_parse_value (GtkStyleProperty *property,
155 GtkCssParser *parser,
158 GtkStylePropertyClass *klass;
160 g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), FALSE);
161 g_return_val_if_fail (value != NULL, FALSE);
162 g_return_val_if_fail (parser != NULL, FALSE);
164 klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
166 return klass->parse_value (property, value, parser, base);
170 * _gtk_style_property_assign:
171 * @property: the property
172 * @props: The properties to assign to
173 * @state: The state to assign
174 * @value: (out): the #GValue with the value to be
177 * This function is called by gtk_style_properties_set() and in
178 * turn gtk_style_context_set() and similar functions to set the
179 * value from code using old APIs.
182 _gtk_style_property_assign (GtkStyleProperty *property,
183 GtkStyleProperties *props,
187 GtkStylePropertyClass *klass;
189 g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
190 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
191 g_return_if_fail (value != NULL);
193 klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
195 klass->assign (property, props, state, value);
199 * _gtk_style_property_query:
200 * @property: the property
201 * @props: The properties to query
202 * @state: The state to query
203 * @value: (out): an uninitialized #GValue to be filled with the
204 * contents of the lookup
206 * This function is called by gtk_style_properties_get() and in
207 * turn gtk_style_context_get() and similar functions to get the
208 * value to return to code using old APIs.
211 _gtk_style_property_query (GtkStyleProperty *property,
212 GtkStyleProperties *props,
214 GtkStylePropertyContext *context,
217 GtkStylePropertyClass *klass;
219 g_return_if_fail (property != NULL);
220 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
221 g_return_if_fail (context != NULL);
222 g_return_if_fail (value != NULL);
224 klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
226 g_value_init (value, property->value_type);
228 klass->query (property, props, state, context, value);
232 gtk_style_property_init_properties (void)
234 static gboolean initialized = FALSE;
236 if (G_LIKELY (initialized))
241 _gtk_css_style_property_init_properties ();
242 /* initialize shorthands last, they depend on the real properties existing */
243 _gtk_css_shorthand_property_init_properties ();
247 * _gtk_style_property_lookup:
248 * @name: name of the property to lookup
250 * Looks up the CSS property with the given @name. If no such
251 * property exists, %NULL is returned.
253 * Returns: (transfer none): The property or %NULL if no
254 * property with the given name exists.
257 _gtk_style_property_lookup (const char *name)
259 GtkStylePropertyClass *klass;
261 g_return_val_if_fail (name != NULL, NULL);
263 gtk_style_property_init_properties ();
265 klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY);
267 return g_hash_table_lookup (klass->properties, name);
271 * _gtk_style_property_get_name:
272 * @property: the property to query
274 * Gets the name of the given property.
276 * Returns: the name of the property
279 _gtk_style_property_get_name (GtkStyleProperty *property)
281 g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
283 return property->name;
287 * _gtk_style_property_get_value_type:
288 * @property: the property to query
290 * Gets the value type of the @property, if the property is usable
291 * in public API via _gtk_style_property_assign() and
292 * _gtk_style_property_query(). If the @property is not usable in that
293 * way, %G_TYPE_NONE is returned.
295 * Returns: the value type in use or %G_TYPE_NONE if none.
298 _gtk_style_property_get_value_type (GtkStyleProperty *property)
300 g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), G_TYPE_NONE);
302 return property->value_type;