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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Benjamin Otte <otte@gnome.org>
23 #include "gtkcssstylepropertyprivate.h"
25 #include "gtkcssstylefuncsprivate.h"
26 #include "gtkcsstypesprivate.h"
28 #include "gtkprivatetypebuiltins.h"
29 #include "gtkstylepropertiesprivate.h"
31 #include "gtkcssimagegradientprivate.h"
32 #include "gtkcssimageprivate.h"
43 G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)
46 gtk_css_style_property_constructed (GObject *object)
48 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
49 GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);
51 property->id = klass->style_properties->len;
52 g_ptr_array_add (klass->style_properties, property);
54 G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
58 gtk_css_style_property_set_property (GObject *object,
63 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
64 const GValue *initial;
69 property->inherit = g_value_get_boolean (value);
72 initial = g_value_get_boxed (value);
74 g_value_init (&property->initial_value, G_VALUE_TYPE (initial));
75 g_value_copy (initial, &property->initial_value);
77 case PROP_COMPUTED_TYPE:
78 property->computed_type = g_value_get_gtype (value);
79 g_assert (property->computed_type != G_TYPE_NONE);
82 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
88 gtk_css_style_property_get_property (GObject *object,
93 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
97 case PROP_SPECIFIED_TYPE:
98 g_value_set_gtype (value, G_VALUE_TYPE (&property->initial_value));
100 case PROP_COMPUTED_TYPE:
101 g_value_set_gtype (value, property->computed_type);
104 g_value_set_boolean (value, property->id);
107 g_value_set_boolean (value, property->inherit);
110 g_value_set_boxed (value, &property->initial_value);
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 _gtk_css_style_property_assign (GtkStyleProperty *property,
120 GtkStyleProperties *props,
124 _gtk_style_properties_set_property_by_property (props,
125 GTK_CSS_STYLE_PROPERTY (property),
131 _gtk_css_style_property_query (GtkStyleProperty *property,
133 GtkStyleQueryFunc query_func,
138 val = (* query_func) (GTK_CSS_STYLE_PROPERTY (property)->id, query_data);
141 /* Somebody make this a vfunc */
142 if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_IMAGE)
144 GtkCssImage *image = g_value_get_object (val);
145 cairo_pattern_t *pattern;
146 cairo_surface_t *surface;
147 cairo_matrix_t matrix;
150 g_value_set_boxed (value, NULL);
151 else if (GTK_IS_CSS_IMAGE_GRADIENT (image))
152 g_value_set_boxed (value, GTK_CSS_IMAGE_GRADIENT (image)->pattern);
155 double width, height;
157 /* the 100, 100 is rather random */
158 _gtk_css_image_get_concrete_size (image, 0, 0, 100, 100, &width, &height);
159 surface = _gtk_css_image_get_surface (image, NULL, width, height);
160 pattern = cairo_pattern_create_for_surface (surface);
161 cairo_matrix_init_scale (&matrix, width, height);
162 cairo_pattern_set_matrix (pattern, &matrix);
163 cairo_surface_destroy (surface);
164 g_value_take_boxed (value, pattern);
168 g_value_copy (val, value);
171 g_value_copy (_gtk_css_style_property_get_initial_value (GTK_CSS_STYLE_PROPERTY (property)), value);
175 gtk_css_style_property_parse_value (GtkStyleProperty *property,
177 GtkCssParser *parser,
180 GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
182 if (_gtk_css_parser_try (parser, "initial", TRUE))
184 /* the initial value can be explicitly specified with the
185 * ‘initial’ keyword which all properties accept.
187 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
188 g_value_set_enum (value, GTK_CSS_INITIAL);
191 else if (_gtk_css_parser_try (parser, "inherit", TRUE))
193 /* All properties accept the ‘inherit’ value which
194 * explicitly specifies that the value will be determined
195 * by inheritance. The ‘inherit’ value can be used to
196 * strengthen inherited values in the cascade, and it can
197 * also be used on properties that are not normally inherited.
199 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
200 g_value_set_enum (value, GTK_CSS_INHERIT);
204 g_value_init (value, _gtk_css_style_property_get_specified_type (style_property));
205 if (!(* style_property->parse_value) (style_property, value, parser, base))
207 g_value_unset (value);
215 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
217 GObjectClass *object_class = G_OBJECT_CLASS (klass);
218 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
220 object_class->constructed = gtk_css_style_property_constructed;
221 object_class->set_property = gtk_css_style_property_set_property;
222 object_class->get_property = gtk_css_style_property_get_property;
224 g_object_class_install_property (object_class,
226 g_param_spec_uint ("id",
228 P_("The numeric id for quick access"),
231 g_object_class_install_property (object_class,
233 g_param_spec_gtype ("specified-type",
234 P_("Specified type"),
235 P_("The type of values after parsing"),
238 g_object_class_install_property (object_class,
240 g_param_spec_gtype ("computed-type",
242 P_("The type of values after style lookup"),
244 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
245 g_object_class_install_property (object_class,
247 g_param_spec_boolean ("inherit",
249 P_("Set if the value is inherited by default"),
251 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
252 g_object_class_install_property (object_class,
254 g_param_spec_boxed ("initial-value",
256 P_("The initial specified value used for this property"),
258 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
260 property_class->assign = _gtk_css_style_property_assign;
261 property_class->query = _gtk_css_style_property_query;
262 property_class->parse_value = gtk_css_style_property_parse_value;
264 klass->style_properties = g_ptr_array_new ();
268 gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
270 GtkCssParser *parser,
273 return _gtk_css_style_parse_value (value, parser, base);
277 gtk_css_style_property_real_print_value (GtkCssStyleProperty *property,
281 _gtk_css_style_print_value (value, string);
285 gtk_css_style_property_real_compute_value (GtkCssStyleProperty *property,
287 GtkStyleContext *context,
288 const GValue *specified)
290 g_value_init (computed, _gtk_style_property_get_value_type (GTK_STYLE_PROPERTY (property)));
291 _gtk_css_style_compute_value (computed, context, specified);
295 _gtk_css_style_property_init (GtkCssStyleProperty *property)
297 property->parse_value = gtk_css_style_property_real_parse_value;
298 property->print_value = gtk_css_style_property_real_print_value;
299 property->compute_value = gtk_css_style_property_real_compute_value;
303 * _gtk_css_style_property_get_n_properties:
305 * Gets the number of style properties. This number can increase when new
306 * theme engines are loaded. Shorthand properties are not included here.
308 * Returns: The number of style properties.
311 _gtk_css_style_property_get_n_properties (void)
313 GtkCssStylePropertyClass *klass;
315 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
316 if (G_UNLIKELY (klass == NULL))
318 _gtk_style_property_init_properties ();
319 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
323 return klass->style_properties->len;
327 * _gtk_css_style_property_lookup_by_id:
328 * @id: the id of the property
330 * Gets the style property with the given id. All style properties (but not
331 * shorthand properties) are indexable by id so that it's easy to use arrays
332 * when doing style lookups.
334 * Returns: (transfer none): The style property with the given id
336 GtkCssStyleProperty *
337 _gtk_css_style_property_lookup_by_id (guint id)
339 GtkCssStylePropertyClass *klass;
341 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
342 if (G_UNLIKELY (klass == NULL))
344 _gtk_style_property_init_properties ();
345 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
348 g_return_val_if_fail (id < klass->style_properties->len, NULL);
350 return g_ptr_array_index (klass->style_properties, id);
354 * _gtk_css_style_property_is_inherit:
355 * @property: the property
357 * Queries if the given @property is inherited. See
358 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
359 * the CSS documentation</ulink> for an explanation of this concept.
361 * Returns: %TRUE if the property is inherited by default.
364 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
366 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
368 return property->inherit;
372 * _gtk_css_style_property_get_id:
373 * @property: the property
375 * Gets the id for the given property. IDs are used to allow using arrays
378 * Returns: The id of the property
381 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
383 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
389 * _gtk_css_style_property_get_initial_value:
390 * @property: the property
392 * Queries the initial value of the given @property. See
393 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
394 * the CSS documentation</ulink> for an explanation of this concept.
396 * Returns: a reference to the initial value. The value will never change.
399 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
401 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
403 return &property->initial_value;
407 * _gtk_css_style_property_get_computed_type:
408 * @property: the property to query
410 * Gets the #GType used for values for this property after a CSS lookup has
411 * happened. _gtk_css_style_property_compute_value() will convert values to
414 * Returns: the #GType used for computed values.
417 _gtk_css_style_property_get_computed_type (GtkCssStyleProperty *property)
419 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
421 return property->computed_type;
425 * _gtk_css_style_property_get_specified_type:
426 * @property: the property to query
428 * Gets the #GType used for values for this property after CSS parsing if
429 * the value is not a special keyword. _gtk_css_style_property_compute_value()
430 * will convert values of this type to the computed type.
432 * The initial value returned by _gtk_css_style_property_get_initial_value()
433 * will be of this type.
435 * Returns: the #GType used for specified values.
438 _gtk_css_style_property_get_specified_type (GtkCssStyleProperty *property)
440 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
442 return G_VALUE_TYPE (&property->initial_value);
446 * _gtk_css_style_property_compute_value:
447 * @property: the property
448 * @computed: (out): an uninitialized value to be filled with the result
449 * @context: the context to use for resolving
450 * @specified: the value to compute from
452 * Converts the @specified value into the @computed value using the
453 * information in @context. This step is explained in detail in
454 * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
455 * the CSS documentation</ulink>.
458 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
460 GtkStyleContext *context,
461 const GValue *specified)
463 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
464 g_return_if_fail (computed != NULL && !G_IS_VALUE (computed));
465 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
466 g_return_if_fail (G_IS_VALUE (specified));
468 property->compute_value (property, computed, context, specified);
472 * _gtk_css_style_property_print_value:
473 * @property: the property
474 * @value: the value to print
475 * @string: the string to print to
477 * Prints @value to the given @string in CSS format. The @value must be a
478 * valid specified value as parsed using the parse functions or as assigned
479 * via _gtk_style_property_assign().
482 _gtk_css_style_property_print_value (GtkCssStyleProperty *property,
486 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
487 g_return_if_fail (value != NULL);
488 g_return_if_fail (string != NULL);
490 if (G_VALUE_HOLDS (value, GTK_TYPE_CSS_SPECIAL_VALUE))
492 GEnumClass *enum_class;
493 GEnumValue *enum_value;
495 enum_class = g_type_class_ref (GTK_TYPE_CSS_SPECIAL_VALUE);
496 enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
498 g_string_append (string, enum_value->value_nick);
500 g_type_class_unref (enum_class);
503 property->print_value (property, value, string);