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, see <http://www.gnu.org/licenses/>.
20 #include "gtkstylepropertyprivate.h"
22 #include "gtkcssprovider.h"
23 #include "gtkcssparserprivate.h"
24 #include "gtkcssshorthandpropertyprivate.h"
25 #include "gtkcssstylefuncsprivate.h"
26 #include "gtkcssstylepropertyprivate.h"
27 #include "gtkcsstypesprivate.h"
29 #include "gtkprivatetypebuiltins.h"
30 #include "gtkstylepropertiesprivate.h"
38 G_DEFINE_ABSTRACT_TYPE (GtkStyleProperty, _gtk_style_property, G_TYPE_OBJECT)
41 gtk_style_property_finalize (GObject *object)
43 GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
45 g_warning ("finalizing %s `%s', how could this happen?", G_OBJECT_TYPE_NAME (object), property->name);
47 G_OBJECT_CLASS (_gtk_style_property_parent_class)->finalize (object);
51 gtk_style_property_set_property (GObject *object,
56 GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
57 GtkStylePropertyClass *klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
62 property->name = g_value_dup_string (value);
63 g_assert (property->name);
64 g_assert (g_hash_table_lookup (klass->properties, property->name) == NULL);
65 g_hash_table_insert (klass->properties, property->name, property);
68 property->value_type = g_value_get_gtype (value);
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
77 gtk_style_property_get_property (GObject *object,
82 GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
87 g_value_set_string (value, property->name);
90 g_value_set_gtype (value, property->value_type);
93 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
99 _gtk_style_property_class_init (GtkStylePropertyClass *klass)
101 GObjectClass *object_class = G_OBJECT_CLASS (klass);
103 object_class->finalize = gtk_style_property_finalize;
104 object_class->set_property = gtk_style_property_set_property;
105 object_class->get_property = gtk_style_property_get_property;
107 g_object_class_install_property (object_class,
109 g_param_spec_string ("name",
111 P_("The name of the property"),
113 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
114 g_object_class_install_property (object_class,
116 g_param_spec_gtype ("value-type",
118 P_("The value type returned by GtkStyleContext"),
120 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
122 klass->properties = g_hash_table_new (g_str_hash, g_str_equal);
126 _gtk_style_property_init (GtkStyleProperty *property)
128 property->value_type = G_TYPE_NONE;
132 * _gtk_style_property_parse_value:
133 * @property: the property
134 * @parser: the parser to parse from
135 * @base: the base file for @aprser
137 * Tries to parse the given @property from the given @parser into
138 * @value. The type that @value will be assigned is dependant on
139 * the parser and no assumptions must be made about it. If the
140 * parsing fails, %FALSE will be returned and @value will be
141 * left uninitialized.
143 * Only if @property is a #GtkCssShorthandProperty, the @value will
144 * always be a #GtkCssValue whose values can be queried with
145 * _gtk_css_array_value_get_nth().
147 * Returns: %NULL on failure or the parsed #GtkCssValue
150 _gtk_style_property_parse_value (GtkStyleProperty *property,
151 GtkCssParser *parser,
154 GtkStylePropertyClass *klass;
156 g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
157 g_return_val_if_fail (parser != NULL, NULL);
159 klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
161 return klass->parse_value (property, parser, base);
165 * _gtk_style_property_assign:
166 * @property: the property
167 * @props: The properties to assign to
168 * @state: The state to assign
169 * @value: (out): the #GValue with the value to be
172 * This function is called by gtk_style_properties_set() and in
173 * turn gtk_style_context_set() and similar functions to set the
174 * value from code using old APIs.
177 _gtk_style_property_assign (GtkStyleProperty *property,
178 GtkStyleProperties *props,
182 GtkStylePropertyClass *klass;
184 g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
185 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
186 g_return_if_fail (value != NULL);
188 klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
190 klass->assign (property, props, state, value);
194 * _gtk_style_property_query:
195 * @property: the property
196 * @value: (out): an uninitialized #GValue to be filled with the
197 * contents of the lookup
198 * @query_func: The function to use to query properties
199 * @query_data: The data to pass to @query_func
201 * This function is called by gtk_style_properties_get() and in
202 * turn gtk_style_context_get() and similar functions to get the
203 * value to return to code using old APIs.
206 _gtk_style_property_query (GtkStyleProperty *property,
208 GtkStyleQueryFunc query_func,
211 GtkStylePropertyClass *klass;
213 g_return_if_fail (value != NULL);
214 g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
215 g_return_if_fail (query_func != NULL);
217 klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
219 return klass->query (property, value, query_func, query_data);
223 _gtk_style_property_init_properties (void)
225 static gboolean initialized = FALSE;
227 if (G_LIKELY (initialized))
232 _gtk_css_style_property_init_properties ();
233 /* initialize shorthands last, they depend on the real properties existing */
234 _gtk_css_shorthand_property_init_properties ();
238 * _gtk_style_property_lookup:
239 * @name: name of the property to lookup
241 * Looks up the CSS property with the given @name. If no such
242 * property exists, %NULL is returned.
244 * Returns: (transfer none): The property or %NULL if no
245 * property with the given name exists.
248 _gtk_style_property_lookup (const char *name)
250 GtkStylePropertyClass *klass;
252 g_return_val_if_fail (name != NULL, NULL);
254 _gtk_style_property_init_properties ();
256 klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY);
258 return g_hash_table_lookup (klass->properties, name);
262 * _gtk_style_property_get_name:
263 * @property: the property to query
265 * Gets the name of the given property.
267 * Returns: the name of the property
270 _gtk_style_property_get_name (GtkStyleProperty *property)
272 g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
274 return property->name;
278 * _gtk_style_property_get_value_type:
279 * @property: the property to query
281 * Gets the value type of the @property, if the property is usable
282 * in public API via _gtk_style_property_assign() and
283 * _gtk_style_property_query(). If the @property is not usable in that
284 * way, %G_TYPE_NONE is returned.
286 * Returns: the value type in use or %G_TYPE_NONE if none.
289 _gtk_style_property_get_value_type (GtkStyleProperty *property)
291 g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), G_TYPE_NONE);
293 return property->value_type;