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 "gtkcssstylefuncsprivate.h"
25 #include "gtkcsstypesprivate.h"
27 #include "gtkprivatetypebuiltins.h"
28 #include "gtkstylepropertiesprivate.h"
31 #include <cairo-gobject.h>
32 #include "gtkcssimagegradientprivate.h"
33 #include "gtkcssimageprivate.h"
35 /* this is in case round() is not provided by the compiler,
36 * such as in the case of C89 compilers, like MSVC
38 #include "fallback-c89.c"
49 G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)
52 gtk_css_style_property_constructed (GObject *object)
54 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
55 GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);
57 property->id = klass->style_properties->len;
58 g_ptr_array_add (klass->style_properties, property);
60 G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
64 gtk_css_style_property_set_property (GObject *object,
69 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
74 property->inherit = g_value_get_boolean (value);
77 property->initial_value = g_value_dup_boxed (value);
78 g_assert (property->initial_value != NULL);
80 case PROP_COMPUTED_TYPE:
81 property->computed_type = g_value_get_gtype (value);
82 g_assert (property->computed_type != G_TYPE_NONE);
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);
100 case PROP_SPECIFIED_TYPE:
101 g_value_set_gtype (value, G_VALUE_TYPE (&property->initial_value));
103 case PROP_COMPUTED_TYPE:
104 g_value_set_gtype (value, property->computed_type);
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 GtkCssValue *css_value = _gtk_css_value_new_from_gvalue (value);
128 _gtk_style_properties_set_property_by_property (props,
129 GTK_CSS_STYLE_PROPERTY (property),
132 _gtk_css_value_unref (css_value);
136 _gtk_css_style_property_query (GtkStyleProperty *property,
137 GtkStyleQueryFunc query_func,
140 GtkCssValue *css_value;
142 css_value = (* query_func) (GTK_CSS_STYLE_PROPERTY (property)->id, query_data);
145 /* Somebody make this a vfunc */
146 if (_gtk_css_value_holds (css_value, GTK_TYPE_CSS_IMAGE))
148 GtkCssImage *image = _gtk_css_value_get_image (css_value);
149 cairo_pattern_t *pattern;
150 cairo_surface_t *surface;
151 cairo_matrix_t matrix;
154 return _gtk_css_value_new_from_pattern (NULL);
155 else if (GTK_IS_CSS_IMAGE_GRADIENT (image))
156 return _gtk_css_value_new_from_pattern (GTK_CSS_IMAGE_GRADIENT (image)->pattern);
159 double width, height;
161 /* the 100, 100 is rather random */
162 _gtk_css_image_get_concrete_size (image, 0, 0, 100, 100, &width, &height);
163 surface = _gtk_css_image_get_surface (image, NULL, width, height);
164 pattern = cairo_pattern_create_for_surface (surface);
165 cairo_matrix_init_scale (&matrix, width, height);
166 cairo_pattern_set_matrix (pattern, &matrix);
167 cairo_surface_destroy (surface);
168 return _gtk_css_value_new_take_pattern (pattern);
171 else if (_gtk_css_value_holds (css_value, GTK_TYPE_CSS_NUMBER))
173 int v = round (_gtk_css_number_get (_gtk_css_value_get_number (css_value), 100));
174 return _gtk_css_value_new_from_int (v);
177 return _gtk_css_value_ref (css_value);
180 return _gtk_css_value_ref (_gtk_css_style_property_get_initial_value (GTK_CSS_STYLE_PROPERTY (property)));
184 gtk_css_style_property_parse_value (GtkStyleProperty *property,
186 GtkCssParser *parser,
189 GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
191 if (_gtk_css_parser_try (parser, "initial", TRUE))
193 /* the initial value can be explicitly specified with the
194 * ‘initial’ keyword which all properties accept.
196 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
197 g_value_set_enum (value, GTK_CSS_INITIAL);
200 else if (_gtk_css_parser_try (parser, "inherit", TRUE))
202 /* All properties accept the ‘inherit’ value which
203 * explicitly specifies that the value will be determined
204 * by inheritance. The ‘inherit’ value can be used to
205 * strengthen inherited values in the cascade, and it can
206 * also be used on properties that are not normally inherited.
208 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
209 g_value_set_enum (value, GTK_CSS_INHERIT);
213 g_value_init (value, _gtk_css_style_property_get_specified_type (style_property));
214 if (!(* style_property->parse_value) (style_property, value, parser, base))
216 g_value_unset (value);
224 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
226 GObjectClass *object_class = G_OBJECT_CLASS (klass);
227 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
229 object_class->constructed = gtk_css_style_property_constructed;
230 object_class->set_property = gtk_css_style_property_set_property;
231 object_class->get_property = gtk_css_style_property_get_property;
233 g_object_class_install_property (object_class,
235 g_param_spec_uint ("id",
237 P_("The numeric id for quick access"),
240 g_object_class_install_property (object_class,
242 g_param_spec_gtype ("specified-type",
243 P_("Specified type"),
244 P_("The type of values after parsing"),
247 g_object_class_install_property (object_class,
249 g_param_spec_gtype ("computed-type",
251 P_("The type of values after style lookup"),
253 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
254 g_object_class_install_property (object_class,
256 g_param_spec_boolean ("inherit",
258 P_("Set if the value is inherited by default"),
260 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
261 g_object_class_install_property (object_class,
263 g_param_spec_boxed ("initial-value",
265 P_("The initial specified value used for this property"),
267 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
269 property_class->assign = _gtk_css_style_property_assign;
270 property_class->query = _gtk_css_style_property_query;
271 property_class->parse_value = gtk_css_style_property_parse_value;
273 klass->style_properties = g_ptr_array_new ();
277 gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
279 GtkCssParser *parser,
282 return _gtk_css_style_parse_value (value, parser, base);
286 gtk_css_style_property_real_print_value (GtkCssStyleProperty *property,
290 _gtk_css_style_print_value (value, string);
294 gtk_css_style_property_real_compute_value (GtkCssStyleProperty *property,
295 GtkStyleContext *context,
296 GtkCssValue *specified)
298 return _gtk_css_style_compute_value (context, _gtk_css_style_property_get_computed_type (property), specified);
302 _gtk_css_style_property_init (GtkCssStyleProperty *property)
304 property->parse_value = gtk_css_style_property_real_parse_value;
305 property->print_value = gtk_css_style_property_real_print_value;
306 property->compute_value = gtk_css_style_property_real_compute_value;
310 * _gtk_css_style_property_get_n_properties:
312 * Gets the number of style properties. This number can increase when new
313 * theme engines are loaded. Shorthand properties are not included here.
315 * Returns: The number of style properties.
318 _gtk_css_style_property_get_n_properties (void)
320 GtkCssStylePropertyClass *klass;
322 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
323 if (G_UNLIKELY (klass == NULL))
325 _gtk_style_property_init_properties ();
326 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
330 return klass->style_properties->len;
334 * _gtk_css_style_property_lookup_by_id:
335 * @id: the id of the property
337 * Gets the style property with the given id. All style properties (but not
338 * shorthand properties) are indexable by id so that it's easy to use arrays
339 * when doing style lookups.
341 * Returns: (transfer none): The style property with the given id
343 GtkCssStyleProperty *
344 _gtk_css_style_property_lookup_by_id (guint id)
346 GtkCssStylePropertyClass *klass;
348 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
349 if (G_UNLIKELY (klass == NULL))
351 _gtk_style_property_init_properties ();
352 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
355 g_return_val_if_fail (id < klass->style_properties->len, NULL);
357 return g_ptr_array_index (klass->style_properties, id);
361 * _gtk_css_style_property_is_inherit:
362 * @property: the property
364 * Queries if the given @property is inherited. See
365 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
366 * the CSS documentation</ulink> for an explanation of this concept.
368 * Returns: %TRUE if the property is inherited by default.
371 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
373 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
375 return property->inherit;
379 * _gtk_css_style_property_get_id:
380 * @property: the property
382 * Gets the id for the given property. IDs are used to allow using arrays
385 * Returns: The id of the property
388 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
390 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
396 * _gtk_css_style_property_get_initial_value:
397 * @property: the property
399 * Queries the initial value of the given @property. See
400 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
401 * the CSS documentation</ulink> for an explanation of this concept.
403 * Returns: a reference to the initial value. The value will never change.
406 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
408 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
410 return property->initial_value;
414 * _gtk_css_style_property_get_computed_type:
415 * @property: the property to query
417 * Gets the #GType used for values for this property after a CSS lookup has
418 * happened. _gtk_css_style_property_compute_value() will convert values to
421 * Returns: the #GType used for computed values.
424 _gtk_css_style_property_get_computed_type (GtkCssStyleProperty *property)
426 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
428 return property->computed_type;
432 * _gtk_css_style_property_get_specified_type:
433 * @property: the property to query
435 * Gets the #GType used for values for this property after CSS parsing if
436 * the value is not a special keyword. _gtk_css_style_property_compute_value()
437 * will convert values of this type to the computed type.
439 * The initial value returned by _gtk_css_style_property_get_initial_value()
440 * will be of this type.
442 * Returns: the #GType used for specified values.
445 _gtk_css_style_property_get_specified_type (GtkCssStyleProperty *property)
447 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
449 return _gtk_css_value_get_content_type (property->initial_value);
453 _gtk_css_style_property_is_specified_type (GtkCssStyleProperty *property,
456 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
458 /* If it's our specified type, of course it's valid */
459 if (type == _gtk_css_value_get_content_type (property->initial_value))
462 /* The special values 'inherit' and 'initial' are always valid */
463 if (type == GTK_TYPE_CSS_SPECIAL_VALUE)
466 /* XXX: Someone needs to fix that legacy */
467 if ((_gtk_css_value_holds (property->initial_value, GDK_TYPE_RGBA) ||
468 _gtk_css_value_holds (property->initial_value, GDK_TYPE_COLOR)) &&
469 type == GTK_TYPE_SYMBOLIC_COLOR)
471 if (_gtk_css_value_holds (property->initial_value, CAIRO_GOBJECT_TYPE_PATTERN) &&
472 type == GTK_TYPE_GRADIENT)
479 * _gtk_css_style_property_compute_value:
480 * @property: the property
481 * @computed: (out): an uninitialized value to be filled with the result
482 * @context: the context to use for resolving
483 * @specified: the value to compute from
485 * Converts the @specified value into the @computed value using the
486 * information in @context. This step is explained in detail in
487 * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
488 * the CSS documentation</ulink>.
491 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
492 GtkStyleContext *context,
493 GtkCssValue *specified)
495 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
496 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
498 return property->compute_value (property, context, specified);
502 * _gtk_css_style_property_print_value:
503 * @property: the property
504 * @value: the value to print
505 * @string: the string to print to
507 * Prints @value to the given @string in CSS format. The @value must be a
508 * valid specified value as parsed using the parse functions or as assigned
509 * via _gtk_style_property_assign().
512 _gtk_css_style_property_print_value (GtkCssStyleProperty *property,
513 GtkCssValue *css_value,
516 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
517 g_return_if_fail (css_value != NULL);
518 g_return_if_fail (string != NULL);
520 if (_gtk_css_value_is_special (css_value))
522 GEnumClass *enum_class;
523 GEnumValue *enum_value;
525 enum_class = g_type_class_ref (GTK_TYPE_CSS_SPECIAL_VALUE);
526 enum_value = g_enum_get_value (enum_class, _gtk_css_value_get_special_kind (css_value));
528 g_string_append (string, enum_value->value_nick);
530 g_type_class_unref (enum_class);
534 GValue value = G_VALUE_INIT;
535 _gtk_css_value_init_gvalue (css_value, &value);
536 property->print_value (property, &value, string);
537 g_value_unset (&value);