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"
33 #include <cairo-gobject.h>
34 #include "gtkcssimagegradientprivate.h"
35 #include "gtkcssimageprivate.h"
37 /* this is in case round() is not provided by the compiler,
38 * such as in the case of C89 compilers, like MSVC
40 #include "fallback-c89.c"
51 G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)
54 gtk_css_style_property_constructed (GObject *object)
56 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
57 GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);
59 property->id = klass->style_properties->len;
60 g_ptr_array_add (klass->style_properties, property);
62 G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
66 gtk_css_style_property_set_property (GObject *object,
71 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
76 property->inherit = g_value_get_boolean (value);
79 property->initial_value = g_value_dup_boxed (value);
80 g_assert (property->initial_value != NULL);
82 case PROP_COMPUTED_TYPE:
83 property->computed_type = g_value_get_gtype (value);
84 g_assert (property->computed_type != G_TYPE_NONE);
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 gtk_css_style_property_get_property (GObject *object,
98 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
102 case PROP_SPECIFIED_TYPE:
103 g_value_set_gtype (value, G_VALUE_TYPE (&property->initial_value));
105 case PROP_COMPUTED_TYPE:
106 g_value_set_gtype (value, property->computed_type);
109 g_value_set_boolean (value, property->id);
112 g_value_set_boolean (value, property->inherit);
115 g_value_set_boxed (value, property->initial_value);
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124 _gtk_css_style_property_assign (GtkStyleProperty *property,
125 GtkStyleProperties *props,
129 GtkCssValue *css_value = _gtk_css_value_new_from_gvalue (value);
130 _gtk_style_properties_set_property_by_property (props,
131 GTK_CSS_STYLE_PROPERTY (property),
134 _gtk_css_value_unref (css_value);
138 _gtk_css_style_property_query (GtkStyleProperty *property,
140 GtkStyleQueryFunc query_func,
143 GtkCssValue *css_value;
145 css_value = (* query_func) (GTK_CSS_STYLE_PROPERTY (property)->id, query_data);
148 /* Somebody make this a vfunc */
149 if (_gtk_css_value_holds (css_value, GTK_TYPE_CSS_IMAGE))
151 GtkCssImage *image = _gtk_css_value_get_image (css_value);
152 cairo_pattern_t *pattern;
153 cairo_surface_t *surface;
154 cairo_matrix_t matrix;
156 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
158 if (GTK_IS_CSS_IMAGE_GRADIENT (image))
159 g_value_set_boxed (value, GTK_CSS_IMAGE_GRADIENT (image)->pattern);
160 else if (image != NULL)
162 double width, height;
164 /* the 100, 100 is rather random */
165 _gtk_css_image_get_concrete_size (image, 0, 0, 100, 100, &width, &height);
166 surface = _gtk_css_image_get_surface (image, NULL, width, height);
167 pattern = cairo_pattern_create_for_surface (surface);
168 cairo_matrix_init_scale (&matrix, width, height);
169 cairo_pattern_set_matrix (pattern, &matrix);
170 cairo_surface_destroy (surface);
171 g_value_take_boxed (value, pattern);
174 else if (_gtk_css_value_holds (css_value, GTK_TYPE_CSS_NUMBER))
176 g_value_init (value, G_TYPE_INT);
177 g_value_set_int (value, round (_gtk_css_number_get (_gtk_css_value_get_number (css_value), 100)));
181 _gtk_css_value_init_gvalue (css_value, value);
186 _gtk_css_value_init_gvalue (_gtk_css_style_property_get_initial_value (GTK_CSS_STYLE_PROPERTY (property)),
192 gtk_css_style_property_parse_value (GtkStyleProperty *property,
193 GtkCssParser *parser,
196 GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
198 if (_gtk_css_parser_try (parser, "initial", TRUE))
200 /* the initial value can be explicitly specified with the
201 * ‘initial’ keyword which all properties accept.
203 return _gtk_css_initial_value_new ();
205 else if (_gtk_css_parser_try (parser, "inherit", TRUE))
207 /* All properties accept the ‘inherit’ value which
208 * explicitly specifies that the value will be determined
209 * by inheritance. The ‘inherit’ value can be used to
210 * strengthen inherited values in the cascade, and it can
211 * also be used on properties that are not normally inherited.
213 return _gtk_css_inherit_value_new ();
216 return (* style_property->parse_value) (style_property, parser, base);
220 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
222 GObjectClass *object_class = G_OBJECT_CLASS (klass);
223 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
225 object_class->constructed = gtk_css_style_property_constructed;
226 object_class->set_property = gtk_css_style_property_set_property;
227 object_class->get_property = gtk_css_style_property_get_property;
229 g_object_class_install_property (object_class,
231 g_param_spec_uint ("id",
233 P_("The numeric id for quick access"),
236 g_object_class_install_property (object_class,
238 g_param_spec_gtype ("specified-type",
239 P_("Specified type"),
240 P_("The type of values after parsing"),
243 g_object_class_install_property (object_class,
245 g_param_spec_gtype ("computed-type",
247 P_("The type of values after style lookup"),
249 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
250 g_object_class_install_property (object_class,
252 g_param_spec_boolean ("inherit",
254 P_("Set if the value is inherited by default"),
256 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
257 g_object_class_install_property (object_class,
259 g_param_spec_boxed ("initial-value",
261 P_("The initial specified value used for this property"),
263 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
265 property_class->assign = _gtk_css_style_property_assign;
266 property_class->query = _gtk_css_style_property_query;
267 property_class->parse_value = gtk_css_style_property_parse_value;
269 klass->style_properties = g_ptr_array_new ();
273 gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
274 GtkCssParser *parser,
277 GValue value = G_VALUE_INIT;
280 g_value_init (&value, _gtk_css_style_property_get_specified_type (property));
281 if (!_gtk_css_style_parse_value (&value, parser, base))
283 g_value_unset (&value);
287 result = _gtk_css_value_new_from_gvalue (&value);
288 g_value_unset (&value);
294 gtk_css_style_property_real_print_value (GtkCssStyleProperty *property,
295 const GtkCssValue *value,
298 _gtk_css_value_print (value, string);
302 gtk_css_style_property_real_compute_value (GtkCssStyleProperty *property,
303 GtkStyleContext *context,
304 GtkCssValue *specified)
306 return _gtk_css_value_ref (specified);
310 gtk_css_style_property_real_equal (GtkCssStyleProperty *property,
314 return _gtk_css_value_equal (value1, value2);
318 _gtk_css_style_property_init (GtkCssStyleProperty *property)
320 property->parse_value = gtk_css_style_property_real_parse_value;
321 property->print_value = gtk_css_style_property_real_print_value;
322 property->compute_value = gtk_css_style_property_real_compute_value;
323 property->equal_func = gtk_css_style_property_real_equal;
327 * _gtk_css_style_property_get_n_properties:
329 * Gets the number of style properties. This number can increase when new
330 * theme engines are loaded. Shorthand properties are not included here.
332 * Returns: The number of style properties.
335 _gtk_css_style_property_get_n_properties (void)
337 GtkCssStylePropertyClass *klass;
339 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
340 if (G_UNLIKELY (klass == NULL))
342 _gtk_style_property_init_properties ();
343 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
347 return klass->style_properties->len;
351 * _gtk_css_style_property_lookup_by_id:
352 * @id: the id of the property
354 * Gets the style property with the given id. All style properties (but not
355 * shorthand properties) are indexable by id so that it's easy to use arrays
356 * when doing style lookups.
358 * Returns: (transfer none): The style property with the given id
360 GtkCssStyleProperty *
361 _gtk_css_style_property_lookup_by_id (guint id)
363 GtkCssStylePropertyClass *klass;
365 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
366 if (G_UNLIKELY (klass == NULL))
368 _gtk_style_property_init_properties ();
369 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
372 g_return_val_if_fail (id < klass->style_properties->len, NULL);
374 return g_ptr_array_index (klass->style_properties, id);
378 * _gtk_css_style_property_is_inherit:
379 * @property: the property
381 * Queries if the given @property is inherited. See
382 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
383 * the CSS documentation</ulink> for an explanation of this concept.
385 * Returns: %TRUE if the property is inherited by default.
388 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
390 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
392 return property->inherit;
396 * _gtk_css_style_property_get_id:
397 * @property: the property
399 * Gets the id for the given property. IDs are used to allow using arrays
402 * Returns: The id of the property
405 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
407 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
413 * _gtk_css_style_property_get_initial_value:
414 * @property: the property
416 * Queries the initial value of the given @property. See
417 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
418 * the CSS documentation</ulink> for an explanation of this concept.
420 * Returns: a reference to the initial value. The value will never change.
423 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
425 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
427 return property->initial_value;
431 * _gtk_css_style_property_get_computed_type:
432 * @property: the property to query
434 * Gets the #GType used for values for this property after a CSS lookup has
435 * happened. _gtk_css_style_property_compute_value() will convert values to
438 * Returns: the #GType used for computed values.
441 _gtk_css_style_property_get_computed_type (GtkCssStyleProperty *property)
443 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
445 return property->computed_type;
449 * _gtk_css_style_property_get_specified_type:
450 * @property: the property to query
452 * Gets the #GType used for values for this property after CSS parsing if
453 * the value is not a special keyword. _gtk_css_style_property_compute_value()
454 * will convert values of this type to the computed type.
456 * The initial value returned by _gtk_css_style_property_get_initial_value()
457 * will be of this type.
459 * Returns: the #GType used for specified values.
462 _gtk_css_style_property_get_specified_type (GtkCssStyleProperty *property)
464 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
466 return _gtk_css_value_get_content_type (property->initial_value);
470 * _gtk_css_style_property_compute_value:
471 * @property: the property
472 * @computed: (out): an uninitialized value to be filled with the result
473 * @context: the context to use for resolving
474 * @specified: the value to compute from
476 * Converts the @specified value into the @computed value using the
477 * information in @context. This step is explained in detail in
478 * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
479 * the CSS documentation</ulink>.
482 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
483 GtkStyleContext *context,
484 GtkCssValue *specified)
486 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
487 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
489 return property->compute_value (property, context, specified);
493 * _gtk_css_style_property_print_value:
494 * @property: the property
495 * @value: the value to print
496 * @string: the string to print to
498 * Prints @value to the given @string in CSS format. The @value must be a
499 * valid specified value as parsed using the parse functions or as assigned
500 * via _gtk_style_property_assign().
503 _gtk_css_style_property_print_value (GtkCssStyleProperty *property,
507 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
508 g_return_if_fail (value != NULL);
509 g_return_if_fail (string != NULL);
511 if (_gtk_css_value_is_inherit (value) ||
512 _gtk_css_value_is_initial (value))
513 _gtk_css_value_print (value, string);
515 property->print_value (property, value, string);
519 * _gtk_css_style_property_is_equal:
520 * @property: the property
521 * @value1: the first value to compare
522 * @value2: the second value to compare
524 * Compares @value1 and @value2 for equality. Both values must be the
525 * result of a call _gtk_css_style_property_compute_value().
527 * Returns: %TRUE if @value1 and @value2 are equal
530 _gtk_css_style_property_is_equal (GtkCssStyleProperty *property,
534 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
535 g_return_val_if_fail (value1 != NULL, FALSE);
536 g_return_val_if_fail (value2 != NULL, FALSE);
538 return property->equal_func (property, value1, value2);