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 "gtkcssstylepropertyprivate.h"
24 #include "gtkcssinheritvalueprivate.h"
25 #include "gtkcssinitialvalueprivate.h"
26 #include "gtkcssstylefuncsprivate.h"
27 #include "gtkcsstypesprivate.h"
29 #include "gtkprivatetypebuiltins.h"
30 #include "gtkstylepropertiesprivate.h"
32 /* this is in case round() is not provided by the compiler,
33 * such as in the case of C89 compilers, like MSVC
35 #include "fallback-c89.c"
45 G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)
48 gtk_css_style_property_constructed (GObject *object)
50 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
51 GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);
53 property->id = klass->style_properties->len;
54 g_ptr_array_add (klass->style_properties, property);
56 G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
60 gtk_css_style_property_set_property (GObject *object,
65 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
70 property->animated = g_value_get_boolean (value);
73 property->inherit = g_value_get_boolean (value);
76 property->initial_value = g_value_dup_boxed (value);
77 g_assert (property->initial_value != NULL);
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
86 gtk_css_style_property_get_property (GObject *object,
91 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
96 g_value_set_boolean (value, property->animated);
99 g_value_set_boolean (value, property->id);
102 g_value_set_boolean (value, property->inherit);
105 g_value_set_boxed (value, property->initial_value);
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114 _gtk_css_style_property_assign (GtkStyleProperty *property,
115 GtkStyleProperties *props,
119 GtkCssStyleProperty *style;
120 GtkCssValue *css_value;
122 style = GTK_CSS_STYLE_PROPERTY (property);
123 css_value = style->assign_value (style, value);
125 _gtk_style_properties_set_property_by_property (props,
129 _gtk_css_value_unref (css_value);
133 _gtk_css_style_property_query (GtkStyleProperty *property,
135 GtkStyleQueryFunc query_func,
138 GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
139 GtkCssValue *css_value;
141 css_value = (* query_func) (GTK_CSS_STYLE_PROPERTY (property)->id, query_data);
142 if (css_value == NULL)
143 css_value =_gtk_css_style_property_get_initial_value (style_property);
145 style_property->query_value (style_property, css_value, value);
149 gtk_css_style_property_parse_value (GtkStyleProperty *property,
150 GtkCssParser *parser,
153 GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
155 if (_gtk_css_parser_try (parser, "initial", TRUE))
157 /* the initial value can be explicitly specified with the
158 * ‘initial’ keyword which all properties accept.
160 return _gtk_css_initial_value_new ();
162 else if (_gtk_css_parser_try (parser, "inherit", TRUE))
164 /* All properties accept the ‘inherit’ value which
165 * explicitly specifies that the value will be determined
166 * by inheritance. The ‘inherit’ value can be used to
167 * strengthen inherited values in the cascade, and it can
168 * also be used on properties that are not normally inherited.
170 return _gtk_css_inherit_value_new ();
173 return (* style_property->parse_value) (style_property, parser, base);
177 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
179 GObjectClass *object_class = G_OBJECT_CLASS (klass);
180 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
182 object_class->constructed = gtk_css_style_property_constructed;
183 object_class->set_property = gtk_css_style_property_set_property;
184 object_class->get_property = gtk_css_style_property_get_property;
186 g_object_class_install_property (object_class,
188 g_param_spec_boolean ("animated",
190 P_("Set if the value can be animated"),
192 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
193 g_object_class_install_property (object_class,
195 g_param_spec_uint ("id",
197 P_("The numeric id for quick access"),
200 g_object_class_install_property (object_class,
202 g_param_spec_boolean ("inherit",
204 P_("Set if the value is inherited by default"),
206 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
207 g_object_class_install_property (object_class,
209 g_param_spec_boxed ("initial-value",
211 P_("The initial specified value used for this property"),
213 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
215 property_class->assign = _gtk_css_style_property_assign;
216 property_class->query = _gtk_css_style_property_query;
217 property_class->parse_value = gtk_css_style_property_parse_value;
219 klass->style_properties = g_ptr_array_new ();
223 gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
224 GtkCssParser *parser,
227 g_assert_not_reached ();
232 gtk_css_style_property_real_print_value (GtkCssStyleProperty *property,
233 const GtkCssValue *value,
236 _gtk_css_value_print (value, string);
240 gtk_css_style_property_real_compute_value (GtkCssStyleProperty *property,
241 GtkStyleContext *context,
242 GtkCssValue *specified)
244 return _gtk_css_value_ref (specified);
248 _gtk_css_style_property_init (GtkCssStyleProperty *property)
250 property->parse_value = gtk_css_style_property_real_parse_value;
251 property->print_value = gtk_css_style_property_real_print_value;
252 property->compute_value = gtk_css_style_property_real_compute_value;
256 * _gtk_css_style_property_get_n_properties:
258 * Gets the number of style properties. This number can increase when new
259 * theme engines are loaded. Shorthand properties are not included here.
261 * Returns: The number of style properties.
264 _gtk_css_style_property_get_n_properties (void)
266 GtkCssStylePropertyClass *klass;
268 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
269 if (G_UNLIKELY (klass == NULL))
271 _gtk_style_property_init_properties ();
272 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
276 return klass->style_properties->len;
280 * _gtk_css_style_property_lookup_by_id:
281 * @id: the id of the property
283 * Gets the style property with the given id. All style properties (but not
284 * shorthand properties) are indexable by id so that it's easy to use arrays
285 * when doing style lookups.
287 * Returns: (transfer none): The style property with the given id
289 GtkCssStyleProperty *
290 _gtk_css_style_property_lookup_by_id (guint id)
292 GtkCssStylePropertyClass *klass;
294 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
295 if (G_UNLIKELY (klass == NULL))
297 _gtk_style_property_init_properties ();
298 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
301 g_return_val_if_fail (id < klass->style_properties->len, NULL);
303 return g_ptr_array_index (klass->style_properties, id);
307 * _gtk_css_style_property_is_inherit:
308 * @property: the property
310 * Queries if the given @property is inherited. See
311 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
312 * the CSS documentation</ulink> for an explanation of this concept.
314 * Returns: %TRUE if the property is inherited by default.
317 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
319 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
321 return property->inherit;
325 * _gtk_css_style_property_is_animated:
326 * @property: the property
328 * Queries if the given @property can be is animated. See
329 * <ulink url="http://www.w3.org/TR/css3-transitions/#animatable-css>
330 * the CSS documentation</ulink> for animatable properties.
332 * Returns: %TRUE if the property can be animated.
335 _gtk_css_style_property_is_animated (GtkCssStyleProperty *property)
337 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
339 return property->animated;
343 * _gtk_css_style_property_get_id:
344 * @property: the property
346 * Gets the id for the given property. IDs are used to allow using arrays
349 * Returns: The id of the property
352 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
354 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
360 * _gtk_css_style_property_get_initial_value:
361 * @property: the property
363 * Queries the initial value of the given @property. See
364 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
365 * the CSS documentation</ulink> for an explanation of this concept.
367 * Returns: a reference to the initial value. The value will never change.
370 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
372 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
374 return property->initial_value;
378 * _gtk_css_style_property_compute_value:
379 * @property: the property
380 * @computed: (out): an uninitialized value to be filled with the result
381 * @context: the context to use for resolving
382 * @specified: the value to compute from
384 * Converts the @specified value into the @computed value using the
385 * information in @context. This step is explained in detail in
386 * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
387 * the CSS documentation</ulink>.
390 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
391 GtkStyleContext *context,
392 GtkCssValue *specified)
394 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
395 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
397 return property->compute_value (property, context, specified);
401 * _gtk_css_style_property_print_value:
402 * @property: the property
403 * @value: the value to print
404 * @string: the string to print to
406 * Prints @value to the given @string in CSS format. The @value must be a
407 * valid specified value as parsed using the parse functions or as assigned
408 * via _gtk_style_property_assign().
411 _gtk_css_style_property_print_value (GtkCssStyleProperty *property,
415 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
416 g_return_if_fail (value != NULL);
417 g_return_if_fail (string != NULL);
419 if (_gtk_css_value_is_inherit (value) ||
420 _gtk_css_value_is_initial (value))
421 _gtk_css_value_print (value, string);
423 property->print_value (property, value, string);