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 "gtkcssenumvalueprivate.h"
25 #include "gtkcssinheritvalueprivate.h"
26 #include "gtkcssinitialvalueprivate.h"
27 #include "gtkcssstylefuncsprivate.h"
28 #include "gtkcsstypesprivate.h"
30 #include "gtkprivatetypebuiltins.h"
31 #include "gtkstylepropertiesprivate.h"
33 /* this is in case round() is not provided by the compiler,
34 * such as in the case of C89 compilers, like MSVC
36 #include "fallback-c89.c"
47 G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)
50 gtk_css_style_property_constructed (GObject *object)
52 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
53 GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);
55 property->id = klass->style_properties->len;
56 g_ptr_array_add (klass->style_properties, property);
58 G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
62 gtk_css_style_property_set_property (GObject *object,
67 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
72 property->animated = g_value_get_boolean (value);
74 case PROP_AFFECTS_SIZE:
75 property->affects_size = g_value_get_boolean (value);
78 property->inherit = g_value_get_boolean (value);
81 property->initial_value = g_value_dup_boxed (value);
82 g_assert (property->initial_value != NULL);
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
91 gtk_css_style_property_get_property (GObject *object,
96 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
101 g_value_set_boolean (value, property->animated);
103 case PROP_AFFECTS_SIZE:
104 g_value_set_boolean (value, property->affects_size);
107 g_value_set_boolean (value, property->id);
110 g_value_set_boolean (value, property->inherit);
113 g_value_set_boxed (value, property->initial_value);
116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
122 _gtk_css_style_property_assign (GtkStyleProperty *property,
123 GtkStyleProperties *props,
127 GtkCssStyleProperty *style;
128 GtkCssValue *css_value;
130 style = GTK_CSS_STYLE_PROPERTY (property);
131 css_value = style->assign_value (style, value);
133 _gtk_style_properties_set_property_by_property (props,
137 _gtk_css_value_unref (css_value);
141 _gtk_css_style_property_query_special_case (GtkCssStyleProperty *property,
143 GtkStyleQueryFunc query_func,
146 GtkBorderStyle border_style;
148 switch (property->id)
150 case GTK_CSS_PROPERTY_BORDER_TOP_WIDTH:
151 border_style = _gtk_css_border_style_value_get (query_func (GTK_CSS_PROPERTY_BORDER_TOP_STYLE, query_data));
152 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
154 g_value_init (value, G_TYPE_INT);
158 case GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH:
159 border_style = _gtk_css_border_style_value_get (query_func (GTK_CSS_PROPERTY_BORDER_RIGHT_STYLE, query_data));
160 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
162 g_value_init (value, G_TYPE_INT);
166 case GTK_CSS_PROPERTY_BORDER_BOTTOM_WIDTH:
167 border_style = _gtk_css_border_style_value_get (query_func (GTK_CSS_PROPERTY_BORDER_BOTTOM_STYLE, query_data));
168 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
170 g_value_init (value, G_TYPE_INT);
174 case GTK_CSS_PROPERTY_BORDER_LEFT_WIDTH:
175 border_style = _gtk_css_border_style_value_get (query_func (GTK_CSS_PROPERTY_BORDER_LEFT_STYLE, query_data));
176 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
178 g_value_init (value, G_TYPE_INT);
182 case GTK_CSS_PROPERTY_OUTLINE_WIDTH:
183 border_style = _gtk_css_border_style_value_get (query_func (GTK_CSS_PROPERTY_OUTLINE_STYLE, query_data));
184 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
186 g_value_init (value, G_TYPE_INT);
198 _gtk_css_style_property_query (GtkStyleProperty *property,
200 GtkStyleQueryFunc query_func,
203 GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
204 GtkCssValue *css_value;
206 /* I don't like this special case being here in this generic code path, but no idea where else to put it. */
207 if (_gtk_css_style_property_query_special_case (style_property, value, query_func, query_data))
210 css_value = (* query_func) (GTK_CSS_STYLE_PROPERTY (property)->id, query_data);
211 if (css_value == NULL)
212 css_value =_gtk_css_style_property_get_initial_value (style_property);
214 style_property->query_value (style_property, css_value, value);
218 gtk_css_style_property_parse_value (GtkStyleProperty *property,
219 GtkCssParser *parser)
221 GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
223 if (_gtk_css_parser_try (parser, "initial", TRUE))
225 /* the initial value can be explicitly specified with the
226 * ‘initial’ keyword which all properties accept.
228 return _gtk_css_initial_value_new ();
230 else if (_gtk_css_parser_try (parser, "inherit", TRUE))
232 /* All properties accept the ‘inherit’ value which
233 * explicitly specifies that the value will be determined
234 * by inheritance. The ‘inherit’ value can be used to
235 * strengthen inherited values in the cascade, and it can
236 * also be used on properties that are not normally inherited.
238 return _gtk_css_inherit_value_new ();
241 return (* style_property->parse_value) (style_property, parser);
245 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
247 GObjectClass *object_class = G_OBJECT_CLASS (klass);
248 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
250 object_class->constructed = gtk_css_style_property_constructed;
251 object_class->set_property = gtk_css_style_property_set_property;
252 object_class->get_property = gtk_css_style_property_get_property;
254 g_object_class_install_property (object_class,
256 g_param_spec_boolean ("animated",
258 P_("Set if the value can be animated"),
260 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
261 g_object_class_install_property (object_class,
263 g_param_spec_boolean ("affects-size",
265 P_("Set if the value affects the sizing of elements"),
267 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
268 g_object_class_install_property (object_class,
270 g_param_spec_uint ("id",
272 P_("The numeric id for quick access"),
275 g_object_class_install_property (object_class,
277 g_param_spec_boolean ("inherit",
279 P_("Set if the value is inherited by default"),
281 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
282 g_object_class_install_property (object_class,
284 g_param_spec_boxed ("initial-value",
286 P_("The initial specified value used for this property"),
288 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
290 property_class->assign = _gtk_css_style_property_assign;
291 property_class->query = _gtk_css_style_property_query;
292 property_class->parse_value = gtk_css_style_property_parse_value;
294 klass->style_properties = g_ptr_array_new ();
298 gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
299 GtkCssParser *parser)
301 g_assert_not_reached ();
306 _gtk_css_style_property_init (GtkCssStyleProperty *property)
308 property->parse_value = gtk_css_style_property_real_parse_value;
312 * _gtk_css_style_property_get_n_properties:
314 * Gets the number of style properties. This number can increase when new
315 * theme engines are loaded. Shorthand properties are not included here.
317 * Returns: The number of style properties.
320 _gtk_css_style_property_get_n_properties (void)
322 GtkCssStylePropertyClass *klass;
324 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
325 if (G_UNLIKELY (klass == NULL))
327 _gtk_style_property_init_properties ();
328 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
332 return klass->style_properties->len;
336 * _gtk_css_style_property_lookup_by_id:
337 * @id: the id of the property
339 * Gets the style property with the given id. All style properties (but not
340 * shorthand properties) are indexable by id so that it's easy to use arrays
341 * when doing style lookups.
343 * Returns: (transfer none): The style property with the given id
345 GtkCssStyleProperty *
346 _gtk_css_style_property_lookup_by_id (guint id)
348 GtkCssStylePropertyClass *klass;
350 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
351 if (G_UNLIKELY (klass == NULL))
353 _gtk_style_property_init_properties ();
354 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
357 g_return_val_if_fail (id < klass->style_properties->len, NULL);
359 return g_ptr_array_index (klass->style_properties, id);
363 * _gtk_css_style_property_is_inherit:
364 * @property: the property
366 * Queries if the given @property is inherited. See
367 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
368 * the CSS documentation</ulink> for an explanation of this concept.
370 * Returns: %TRUE if the property is inherited by default.
373 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
375 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
377 return property->inherit;
381 * _gtk_css_style_property_is_animated:
382 * @property: the property
384 * Queries if the given @property can be is animated. See
385 * <ulink url="http://www.w3.org/TR/css3-transitions/#animatable-css>
386 * the CSS documentation</ulink> for animatable properties.
388 * Returns: %TRUE if the property can be animated.
391 _gtk_css_style_property_is_animated (GtkCssStyleProperty *property)
393 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
395 return property->animated;
399 * _gtk_css_style_property_affects_size:
400 * @property: the property
402 * Queries if the given @property affects the size of elements. This is
403 * used for optimizations inside GTK, where a gtk_widget_queue_resize()
404 * can be avoided if the property does not affect size.
406 * Returns: %TRUE if the property affects sizing of elements.
409 _gtk_css_style_property_affects_size (GtkCssStyleProperty *property)
411 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
413 return property->affects_size;
416 * _gtk_css_style_property_get_id:
417 * @property: the property
419 * Gets the id for the given property. IDs are used to allow using arrays
422 * Returns: The id of the property
425 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
427 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
433 * _gtk_css_style_property_get_initial_value:
434 * @property: the property
436 * Queries the initial value of the given @property. See
437 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
438 * the CSS documentation</ulink> for an explanation of this concept.
440 * Returns: a reference to the initial value. The value will never change.
443 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
445 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
447 return property->initial_value;