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,
287 const GtkCssValue *value,
290 _gtk_css_value_print (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_real_equal (GtkCssStyleProperty *property,
310 _gtk_css_style_property_init (GtkCssStyleProperty *property)
312 property->parse_value = gtk_css_style_property_real_parse_value;
313 property->print_value = gtk_css_style_property_real_print_value;
314 property->compute_value = gtk_css_style_property_real_compute_value;
315 property->equal_func = gtk_css_style_property_real_equal;
319 * _gtk_css_style_property_get_n_properties:
321 * Gets the number of style properties. This number can increase when new
322 * theme engines are loaded. Shorthand properties are not included here.
324 * Returns: The number of style properties.
327 _gtk_css_style_property_get_n_properties (void)
329 GtkCssStylePropertyClass *klass;
331 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
332 if (G_UNLIKELY (klass == NULL))
334 _gtk_style_property_init_properties ();
335 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
339 return klass->style_properties->len;
343 * _gtk_css_style_property_lookup_by_id:
344 * @id: the id of the property
346 * Gets the style property with the given id. All style properties (but not
347 * shorthand properties) are indexable by id so that it's easy to use arrays
348 * when doing style lookups.
350 * Returns: (transfer none): The style property with the given id
352 GtkCssStyleProperty *
353 _gtk_css_style_property_lookup_by_id (guint id)
355 GtkCssStylePropertyClass *klass;
357 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
358 if (G_UNLIKELY (klass == NULL))
360 _gtk_style_property_init_properties ();
361 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
364 g_return_val_if_fail (id < klass->style_properties->len, NULL);
366 return g_ptr_array_index (klass->style_properties, id);
370 * _gtk_css_style_property_is_inherit:
371 * @property: the property
373 * Queries if the given @property is inherited. See
374 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
375 * the CSS documentation</ulink> for an explanation of this concept.
377 * Returns: %TRUE if the property is inherited by default.
380 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
382 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
384 return property->inherit;
388 * _gtk_css_style_property_get_id:
389 * @property: the property
391 * Gets the id for the given property. IDs are used to allow using arrays
394 * Returns: The id of the property
397 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
399 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
405 * _gtk_css_style_property_get_initial_value:
406 * @property: the property
408 * Queries the initial value of the given @property. See
409 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
410 * the CSS documentation</ulink> for an explanation of this concept.
412 * Returns: a reference to the initial value. The value will never change.
415 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
417 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
419 return property->initial_value;
423 * _gtk_css_style_property_get_computed_type:
424 * @property: the property to query
426 * Gets the #GType used for values for this property after a CSS lookup has
427 * happened. _gtk_css_style_property_compute_value() will convert values to
430 * Returns: the #GType used for computed values.
433 _gtk_css_style_property_get_computed_type (GtkCssStyleProperty *property)
435 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
437 return property->computed_type;
441 * _gtk_css_style_property_get_specified_type:
442 * @property: the property to query
444 * Gets the #GType used for values for this property after CSS parsing if
445 * the value is not a special keyword. _gtk_css_style_property_compute_value()
446 * will convert values of this type to the computed type.
448 * The initial value returned by _gtk_css_style_property_get_initial_value()
449 * will be of this type.
451 * Returns: the #GType used for specified values.
454 _gtk_css_style_property_get_specified_type (GtkCssStyleProperty *property)
456 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
458 return _gtk_css_value_get_content_type (property->initial_value);
462 _gtk_css_style_property_is_specified_type (GtkCssStyleProperty *property,
465 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
467 /* If it's our specified type, of course it's valid */
468 if (type == _gtk_css_value_get_content_type (property->initial_value))
471 /* The special values 'inherit' and 'initial' are always valid */
472 if (type == GTK_TYPE_CSS_SPECIAL_VALUE)
475 /* XXX: Someone needs to fix that legacy */
476 if ((_gtk_css_value_holds (property->initial_value, GDK_TYPE_RGBA) ||
477 _gtk_css_value_holds (property->initial_value, GDK_TYPE_COLOR)) &&
478 type == GTK_TYPE_SYMBOLIC_COLOR)
480 if (_gtk_css_value_holds (property->initial_value, CAIRO_GOBJECT_TYPE_PATTERN) &&
481 type == GTK_TYPE_GRADIENT)
488 * _gtk_css_style_property_compute_value:
489 * @property: the property
490 * @computed: (out): an uninitialized value to be filled with the result
491 * @context: the context to use for resolving
492 * @specified: the value to compute from
494 * Converts the @specified value into the @computed value using the
495 * information in @context. This step is explained in detail in
496 * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
497 * the CSS documentation</ulink>.
500 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
501 GtkStyleContext *context,
502 GtkCssValue *specified)
504 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
505 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
507 return property->compute_value (property, context, specified);
511 * _gtk_css_style_property_print_value:
512 * @property: the property
513 * @value: the value to print
514 * @string: the string to print to
516 * Prints @value to the given @string in CSS format. The @value must be a
517 * valid specified value as parsed using the parse functions or as assigned
518 * via _gtk_style_property_assign().
521 _gtk_css_style_property_print_value (GtkCssStyleProperty *property,
522 GtkCssValue *css_value,
525 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
526 g_return_if_fail (css_value != NULL);
527 g_return_if_fail (string != NULL);
529 if (_gtk_css_value_is_special (css_value))
531 GEnumClass *enum_class;
532 GEnumValue *enum_value;
534 enum_class = g_type_class_ref (GTK_TYPE_CSS_SPECIAL_VALUE);
535 enum_value = g_enum_get_value (enum_class, _gtk_css_value_get_special_kind (css_value));
537 g_string_append (string, enum_value->value_nick);
539 g_type_class_unref (enum_class);
543 property->print_value (property, css_value, string);
548 * _gtk_css_style_property_is_equal:
549 * @property: the property
550 * @value1: the first value to compare
551 * @value2: the second value to compare
553 * Compares @value1 and @value2 for equality. Both values must be the
554 * result of a call _gtk_css_style_property_compute_value().
556 * Returns: %TRUE if @value1 and @value2 are equal
559 _gtk_css_style_property_is_equal (GtkCssStyleProperty *property,
563 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
564 g_return_val_if_fail (value1 != NULL, FALSE);
565 g_return_val_if_fail (value2 != NULL, FALSE);
567 return property->equal_func (property, value1, value2);