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,
138 GtkStyleQueryFunc query_func,
141 GtkCssValue *css_value;
143 css_value = (* query_func) (GTK_CSS_STYLE_PROPERTY (property)->id, query_data);
146 /* Somebody make this a vfunc */
147 if (_gtk_css_value_holds (css_value, GTK_TYPE_CSS_IMAGE))
149 GtkCssImage *image = _gtk_css_value_get_image (css_value);
150 cairo_pattern_t *pattern;
151 cairo_surface_t *surface;
152 cairo_matrix_t matrix;
154 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
156 if (GTK_IS_CSS_IMAGE_GRADIENT (image))
157 g_value_set_boxed (value, GTK_CSS_IMAGE_GRADIENT (image)->pattern);
158 else if (image != NULL)
160 double width, height;
162 /* the 100, 100 is rather random */
163 _gtk_css_image_get_concrete_size (image, 0, 0, 100, 100, &width, &height);
164 surface = _gtk_css_image_get_surface (image, NULL, width, height);
165 pattern = cairo_pattern_create_for_surface (surface);
166 cairo_matrix_init_scale (&matrix, width, height);
167 cairo_pattern_set_matrix (pattern, &matrix);
168 cairo_surface_destroy (surface);
169 g_value_take_boxed (value, pattern);
172 else if (_gtk_css_value_holds (css_value, GTK_TYPE_CSS_NUMBER))
174 g_value_init (value, G_TYPE_INT);
175 g_value_set_int (value, round (_gtk_css_number_get (_gtk_css_value_get_number (css_value), 100)));
179 _gtk_css_value_init_gvalue (css_value, value);
184 _gtk_css_value_init_gvalue (_gtk_css_style_property_get_initial_value (GTK_CSS_STYLE_PROPERTY (property)),
190 gtk_css_style_property_parse_value (GtkStyleProperty *property,
192 GtkCssParser *parser,
195 GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
197 if (_gtk_css_parser_try (parser, "initial", TRUE))
199 /* the initial value can be explicitly specified with the
200 * ‘initial’ keyword which all properties accept.
202 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
203 g_value_set_enum (value, GTK_CSS_INITIAL);
206 else if (_gtk_css_parser_try (parser, "inherit", TRUE))
208 /* All properties accept the ‘inherit’ value which
209 * explicitly specifies that the value will be determined
210 * by inheritance. The ‘inherit’ value can be used to
211 * strengthen inherited values in the cascade, and it can
212 * also be used on properties that are not normally inherited.
214 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
215 g_value_set_enum (value, GTK_CSS_INHERIT);
219 g_value_init (value, _gtk_css_style_property_get_specified_type (style_property));
220 if (!(* style_property->parse_value) (style_property, value, parser, base))
222 g_value_unset (value);
230 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
232 GObjectClass *object_class = G_OBJECT_CLASS (klass);
233 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
235 object_class->constructed = gtk_css_style_property_constructed;
236 object_class->set_property = gtk_css_style_property_set_property;
237 object_class->get_property = gtk_css_style_property_get_property;
239 g_object_class_install_property (object_class,
241 g_param_spec_uint ("id",
243 P_("The numeric id for quick access"),
246 g_object_class_install_property (object_class,
248 g_param_spec_gtype ("specified-type",
249 P_("Specified type"),
250 P_("The type of values after parsing"),
253 g_object_class_install_property (object_class,
255 g_param_spec_gtype ("computed-type",
257 P_("The type of values after style lookup"),
259 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
260 g_object_class_install_property (object_class,
262 g_param_spec_boolean ("inherit",
264 P_("Set if the value is inherited by default"),
266 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
267 g_object_class_install_property (object_class,
269 g_param_spec_boxed ("initial-value",
271 P_("The initial specified value used for this property"),
273 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
275 property_class->assign = _gtk_css_style_property_assign;
276 property_class->query = _gtk_css_style_property_query;
277 property_class->parse_value = gtk_css_style_property_parse_value;
279 klass->style_properties = g_ptr_array_new ();
283 gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
285 GtkCssParser *parser,
288 return _gtk_css_style_parse_value (value, parser, base);
292 gtk_css_style_property_real_print_value (GtkCssStyleProperty *property,
293 const GtkCssValue *value,
296 _gtk_css_value_print (value, string);
300 gtk_css_style_property_real_compute_value (GtkCssStyleProperty *property,
301 GtkStyleContext *context,
302 GtkCssValue *specified)
304 return _gtk_css_style_compute_value (context, _gtk_css_style_property_get_computed_type (property), specified);
308 gtk_css_style_property_real_equal (GtkCssStyleProperty *property,
316 _gtk_css_style_property_init (GtkCssStyleProperty *property)
318 property->parse_value = gtk_css_style_property_real_parse_value;
319 property->print_value = gtk_css_style_property_real_print_value;
320 property->compute_value = gtk_css_style_property_real_compute_value;
321 property->equal_func = gtk_css_style_property_real_equal;
325 * _gtk_css_style_property_get_n_properties:
327 * Gets the number of style properties. This number can increase when new
328 * theme engines are loaded. Shorthand properties are not included here.
330 * Returns: The number of style properties.
333 _gtk_css_style_property_get_n_properties (void)
335 GtkCssStylePropertyClass *klass;
337 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
338 if (G_UNLIKELY (klass == NULL))
340 _gtk_style_property_init_properties ();
341 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
345 return klass->style_properties->len;
349 * _gtk_css_style_property_lookup_by_id:
350 * @id: the id of the property
352 * Gets the style property with the given id. All style properties (but not
353 * shorthand properties) are indexable by id so that it's easy to use arrays
354 * when doing style lookups.
356 * Returns: (transfer none): The style property with the given id
358 GtkCssStyleProperty *
359 _gtk_css_style_property_lookup_by_id (guint id)
361 GtkCssStylePropertyClass *klass;
363 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
364 if (G_UNLIKELY (klass == NULL))
366 _gtk_style_property_init_properties ();
367 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
370 g_return_val_if_fail (id < klass->style_properties->len, NULL);
372 return g_ptr_array_index (klass->style_properties, id);
376 * _gtk_css_style_property_is_inherit:
377 * @property: the property
379 * Queries if the given @property is inherited. See
380 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
381 * the CSS documentation</ulink> for an explanation of this concept.
383 * Returns: %TRUE if the property is inherited by default.
386 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
388 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
390 return property->inherit;
394 * _gtk_css_style_property_get_id:
395 * @property: the property
397 * Gets the id for the given property. IDs are used to allow using arrays
400 * Returns: The id of the property
403 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
405 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
411 * _gtk_css_style_property_get_initial_value:
412 * @property: the property
414 * Queries the initial value of the given @property. See
415 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
416 * the CSS documentation</ulink> for an explanation of this concept.
418 * Returns: a reference to the initial value. The value will never change.
421 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
423 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
425 return property->initial_value;
429 * _gtk_css_style_property_get_computed_type:
430 * @property: the property to query
432 * Gets the #GType used for values for this property after a CSS lookup has
433 * happened. _gtk_css_style_property_compute_value() will convert values to
436 * Returns: the #GType used for computed values.
439 _gtk_css_style_property_get_computed_type (GtkCssStyleProperty *property)
441 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
443 return property->computed_type;
447 * _gtk_css_style_property_get_specified_type:
448 * @property: the property to query
450 * Gets the #GType used for values for this property after CSS parsing if
451 * the value is not a special keyword. _gtk_css_style_property_compute_value()
452 * will convert values of this type to the computed type.
454 * The initial value returned by _gtk_css_style_property_get_initial_value()
455 * will be of this type.
457 * Returns: the #GType used for specified values.
460 _gtk_css_style_property_get_specified_type (GtkCssStyleProperty *property)
462 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
464 return _gtk_css_value_get_content_type (property->initial_value);
468 * _gtk_css_style_property_compute_value:
469 * @property: the property
470 * @computed: (out): an uninitialized value to be filled with the result
471 * @context: the context to use for resolving
472 * @specified: the value to compute from
474 * Converts the @specified value into the @computed value using the
475 * information in @context. This step is explained in detail in
476 * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
477 * the CSS documentation</ulink>.
480 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
481 GtkStyleContext *context,
482 GtkCssValue *specified)
484 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
485 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
487 return property->compute_value (property, context, specified);
491 * _gtk_css_style_property_print_value:
492 * @property: the property
493 * @value: the value to print
494 * @string: the string to print to
496 * Prints @value to the given @string in CSS format. The @value must be a
497 * valid specified value as parsed using the parse functions or as assigned
498 * via _gtk_style_property_assign().
501 _gtk_css_style_property_print_value (GtkCssStyleProperty *property,
502 GtkCssValue *css_value,
505 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
506 g_return_if_fail (css_value != NULL);
507 g_return_if_fail (string != NULL);
509 if (_gtk_css_value_is_special (css_value))
511 GEnumClass *enum_class;
512 GEnumValue *enum_value;
514 enum_class = g_type_class_ref (GTK_TYPE_CSS_SPECIAL_VALUE);
515 enum_value = g_enum_get_value (enum_class, _gtk_css_value_get_special_kind (css_value));
517 g_string_append (string, enum_value->value_nick);
519 g_type_class_unref (enum_class);
523 property->print_value (property, css_value, string);
528 * _gtk_css_style_property_is_equal:
529 * @property: the property
530 * @value1: the first value to compare
531 * @value2: the second value to compare
533 * Compares @value1 and @value2 for equality. Both values must be the
534 * result of a call _gtk_css_style_property_compute_value().
536 * Returns: %TRUE if @value1 and @value2 are equal
539 _gtk_css_style_property_is_equal (GtkCssStyleProperty *property,
543 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
544 g_return_val_if_fail (value1 != NULL, FALSE);
545 g_return_val_if_fail (value2 != NULL, FALSE);
547 return property->equal_func (property, value1, value2);