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 g_assert_not_reached ();
282 gtk_css_style_property_real_print_value (GtkCssStyleProperty *property,
283 const GtkCssValue *value,
286 _gtk_css_value_print (value, string);
290 gtk_css_style_property_real_compute_value (GtkCssStyleProperty *property,
291 GtkStyleContext *context,
292 GtkCssValue *specified)
294 return _gtk_css_value_ref (specified);
298 gtk_css_style_property_real_equal (GtkCssStyleProperty *property,
302 return _gtk_css_value_equal (value1, value2);
306 _gtk_css_style_property_init (GtkCssStyleProperty *property)
308 property->parse_value = gtk_css_style_property_real_parse_value;
309 property->print_value = gtk_css_style_property_real_print_value;
310 property->compute_value = gtk_css_style_property_real_compute_value;
311 property->equal_func = gtk_css_style_property_real_equal;
315 * _gtk_css_style_property_get_n_properties:
317 * Gets the number of style properties. This number can increase when new
318 * theme engines are loaded. Shorthand properties are not included here.
320 * Returns: The number of style properties.
323 _gtk_css_style_property_get_n_properties (void)
325 GtkCssStylePropertyClass *klass;
327 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
328 if (G_UNLIKELY (klass == NULL))
330 _gtk_style_property_init_properties ();
331 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
335 return klass->style_properties->len;
339 * _gtk_css_style_property_lookup_by_id:
340 * @id: the id of the property
342 * Gets the style property with the given id. All style properties (but not
343 * shorthand properties) are indexable by id so that it's easy to use arrays
344 * when doing style lookups.
346 * Returns: (transfer none): The style property with the given id
348 GtkCssStyleProperty *
349 _gtk_css_style_property_lookup_by_id (guint id)
351 GtkCssStylePropertyClass *klass;
353 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
354 if (G_UNLIKELY (klass == NULL))
356 _gtk_style_property_init_properties ();
357 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
360 g_return_val_if_fail (id < klass->style_properties->len, NULL);
362 return g_ptr_array_index (klass->style_properties, id);
366 * _gtk_css_style_property_is_inherit:
367 * @property: the property
369 * Queries if the given @property is inherited. See
370 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
371 * the CSS documentation</ulink> for an explanation of this concept.
373 * Returns: %TRUE if the property is inherited by default.
376 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
378 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
380 return property->inherit;
384 * _gtk_css_style_property_get_id:
385 * @property: the property
387 * Gets the id for the given property. IDs are used to allow using arrays
390 * Returns: The id of the property
393 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
395 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
401 * _gtk_css_style_property_get_initial_value:
402 * @property: the property
404 * Queries the initial value of the given @property. See
405 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
406 * the CSS documentation</ulink> for an explanation of this concept.
408 * Returns: a reference to the initial value. The value will never change.
411 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
413 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
415 return property->initial_value;
419 * _gtk_css_style_property_get_computed_type:
420 * @property: the property to query
422 * Gets the #GType used for values for this property after a CSS lookup has
423 * happened. _gtk_css_style_property_compute_value() will convert values to
426 * Returns: the #GType used for computed values.
429 _gtk_css_style_property_get_computed_type (GtkCssStyleProperty *property)
431 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
433 return property->computed_type;
437 * _gtk_css_style_property_get_specified_type:
438 * @property: the property to query
440 * Gets the #GType used for values for this property after CSS parsing if
441 * the value is not a special keyword. _gtk_css_style_property_compute_value()
442 * will convert values of this type to the computed type.
444 * The initial value returned by _gtk_css_style_property_get_initial_value()
445 * will be of this type.
447 * Returns: the #GType used for specified values.
450 _gtk_css_style_property_get_specified_type (GtkCssStyleProperty *property)
452 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
454 return _gtk_css_value_get_content_type (property->initial_value);
458 * _gtk_css_style_property_compute_value:
459 * @property: the property
460 * @computed: (out): an uninitialized value to be filled with the result
461 * @context: the context to use for resolving
462 * @specified: the value to compute from
464 * Converts the @specified value into the @computed value using the
465 * information in @context. This step is explained in detail in
466 * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
467 * the CSS documentation</ulink>.
470 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
471 GtkStyleContext *context,
472 GtkCssValue *specified)
474 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
475 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
477 return property->compute_value (property, context, specified);
481 * _gtk_css_style_property_print_value:
482 * @property: the property
483 * @value: the value to print
484 * @string: the string to print to
486 * Prints @value to the given @string in CSS format. The @value must be a
487 * valid specified value as parsed using the parse functions or as assigned
488 * via _gtk_style_property_assign().
491 _gtk_css_style_property_print_value (GtkCssStyleProperty *property,
495 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
496 g_return_if_fail (value != NULL);
497 g_return_if_fail (string != NULL);
499 if (_gtk_css_value_is_inherit (value) ||
500 _gtk_css_value_is_initial (value))
501 _gtk_css_value_print (value, string);
503 property->print_value (property, value, string);
507 * _gtk_css_style_property_is_equal:
508 * @property: the property
509 * @value1: the first value to compare
510 * @value2: the second value to compare
512 * Compares @value1 and @value2 for equality. Both values must be the
513 * result of a call _gtk_css_style_property_compute_value().
515 * Returns: %TRUE if @value1 and @value2 are equal
518 _gtk_css_style_property_is_equal (GtkCssStyleProperty *property,
522 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
523 g_return_val_if_fail (value1 != NULL, FALSE);
524 g_return_val_if_fail (value2 != NULL, FALSE);
526 return property->equal_func (property, value1, value2);