1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 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 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/>.
20 #include "gtkcssvalueprivate.h"
21 #include "gtkcssstylefuncsprivate.h"
22 #include "gtktypebuiltins.h"
23 #include "gtkgradient.h"
24 #include <cairo-gobject.h>
25 #include "gtkprivatetypebuiltins.h"
27 #include "fallback-c89.c"
43 gtk_css_value_default_free (GtkCssValue *value)
45 GType type = value->type;
47 if (g_type_is_a (type, G_TYPE_OBJECT))
49 if (value->u.ptr != NULL)
50 g_object_unref (value->u.ptr);
52 else if (g_type_is_a (type, G_TYPE_BOXED))
54 if (value->u.ptr != NULL)
55 g_boxed_free (type, value->u.ptr);
57 else if (g_type_is_a (type, G_TYPE_STRING))
58 g_free (value->u.ptr);
59 else if (g_type_is_a (type, G_TYPE_INT))
61 else if (g_type_is_a (type, G_TYPE_UINT))
63 else if (g_type_is_a (type, G_TYPE_BOOLEAN))
65 else if (g_type_is_a (type, G_TYPE_ENUM))
67 else if (g_type_is_a (type, G_TYPE_FLAGS))
69 else if (g_type_is_a (type, G_TYPE_DOUBLE))
71 else if (g_type_is_a (type, G_TYPE_FLOAT))
75 g_value_unset (value->u.ptr);
76 g_slice_free (GValue, value->u.ptr);
79 g_slice_free (GtkCssValue, value);
83 gtk_css_value_default_equal (const GtkCssValue *value1,
84 const GtkCssValue *value2)
90 gtk_css_value_default_print (const GtkCssValue *value,
93 GValue g_value = G_VALUE_INIT;
95 _gtk_css_value_init_gvalue (value, &g_value);
96 _gtk_css_style_print_value (&g_value, string);
97 g_value_unset (&g_value);
100 static const GtkCssValueClass GTK_CSS_VALUE_DEFAULT = {
101 gtk_css_value_default_free,
102 gtk_css_value_default_equal,
103 gtk_css_value_default_print
106 G_DEFINE_BOXED_TYPE (GtkCssValue, _gtk_css_value, _gtk_css_value_ref, _gtk_css_value_unref)
109 gtk_css_value_new (GType type)
113 value = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_DEFAULT);
121 _gtk_css_value_new_from_gvalue (const GValue *g_value)
126 g_return_val_if_fail (g_value != NULL, NULL);
128 type = G_VALUE_TYPE (g_value);
130 /* Make sure we reuse the int/number singletons */
131 if (type == G_TYPE_INT)
132 value = _gtk_css_value_new_from_int (g_value_get_int (g_value));
135 value = gtk_css_value_new (type);
137 if (g_type_is_a (type, G_TYPE_OBJECT))
138 value->u.ptr = g_value_dup_object (g_value);
139 else if (g_type_is_a (type, G_TYPE_BOXED))
140 value->u.ptr = g_value_dup_boxed (g_value);
141 else if (g_type_is_a (type, G_TYPE_INT))
142 value->u.gint = g_value_get_int (g_value);
143 else if (g_type_is_a (type, G_TYPE_UINT))
144 value->u.guint = g_value_get_uint (g_value);
145 else if (g_type_is_a (type, G_TYPE_BOOLEAN))
146 value->u.gint = g_value_get_boolean (g_value);
147 else if (g_type_is_a (type, G_TYPE_ENUM))
148 value->u.gint = g_value_get_enum (g_value);
149 else if (g_type_is_a (type, G_TYPE_FLAGS))
150 value->u.guint = g_value_get_flags (g_value);
151 else if (g_type_is_a (type, G_TYPE_STRING))
152 value->u.ptr = g_value_dup_string (g_value);
153 else if (g_type_is_a (type, G_TYPE_DOUBLE))
154 value->u.dbl = g_value_get_double (g_value);
155 else if (g_type_is_a (type, G_TYPE_FLOAT))
156 value->u.flt = g_value_get_float (g_value);
159 value->u.ptr = g_slice_new0 (GValue);
160 g_value_init (value->u.ptr, G_VALUE_TYPE (g_value));
161 g_value_copy (g_value, value->u.ptr);
169 _gtk_css_value_new_from_int (gint val)
172 static GtkCssValue *singletons[4] = {NULL};
174 if (val >= 0 && val < G_N_ELEMENTS (singletons))
176 if (singletons[val] == NULL)
178 value = gtk_css_value_new (G_TYPE_INT);
180 singletons[val] = value;
182 return _gtk_css_value_ref (singletons[val]);
185 value = gtk_css_value_new (G_TYPE_INT);
192 _gtk_css_value_new_from_enum (GType type,
197 g_return_val_if_fail (g_type_is_a (type, G_TYPE_ENUM), NULL);
199 value = gtk_css_value_new (type);
206 _gtk_css_value_new_from_double (double d)
210 value = gtk_css_value_new (G_TYPE_DOUBLE);
217 _gtk_css_value_new_take_strv (char **strv)
221 value = gtk_css_value_new (G_TYPE_STRV);
228 g_boxed_copy0 (GType boxed_type,
229 gconstpointer src_boxed)
231 if (src_boxed == NULL)
233 return g_boxed_copy (boxed_type, src_boxed);
237 _gtk_css_value_new_from_boxed (GType type,
242 g_return_val_if_fail (g_type_is_a (type, G_TYPE_BOXED), NULL);
244 value = gtk_css_value_new (type);
245 value->u.ptr = g_boxed_copy0 (type, boxed);
251 _gtk_css_value_new_take_pattern (cairo_pattern_t *v)
255 value = gtk_css_value_new (CAIRO_GOBJECT_TYPE_PATTERN);
262 _gtk_css_value_new_from_theming_engine (GtkThemingEngine *v)
266 value = gtk_css_value_new (GTK_TYPE_THEMING_ENGINE);
267 value->u.ptr = g_object_ref (v);
273 _gtk_css_value_new_take_binding_sets (GPtrArray *array)
277 value = gtk_css_value_new (G_TYPE_PTR_ARRAY);
278 value->u.ptr = array;
284 _gtk_css_value_new_from_color (const GdkColor *v)
288 value = gtk_css_value_new (GDK_TYPE_COLOR);
289 value->u.ptr = g_boxed_copy0 (GDK_TYPE_COLOR, v);
295 _gtk_css_value_new_from_background_size (const GtkCssBackgroundSize *v)
299 value = gtk_css_value_new (GTK_TYPE_CSS_BACKGROUND_SIZE);
300 value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BACKGROUND_SIZE, v);
306 _gtk_css_value_new_from_background_position (const GtkCssBackgroundPosition *v)
310 value = gtk_css_value_new (GTK_TYPE_CSS_BACKGROUND_POSITION);
311 value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BACKGROUND_POSITION, v);
317 _gtk_css_value_new_from_border_corner_radius (const GtkCssBorderCornerRadius *v)
321 value = gtk_css_value_new (GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
322 value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BORDER_CORNER_RADIUS, v);
328 _gtk_css_value_new_from_border_image_repeat (const GtkCssBorderImageRepeat *v)
332 value = gtk_css_value_new (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
333 value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, v);
339 _gtk_css_value_new_from_border_style (GtkBorderStyle style)
343 value = gtk_css_value_new (GTK_TYPE_BORDER_STYLE);
344 value->u.gint = style;
350 _gtk_css_value_new_take_symbolic_color (GtkSymbolicColor *v)
354 value = gtk_css_value_new (GTK_TYPE_SYMBOLIC_COLOR);
361 _gtk_css_value_alloc (const GtkCssValueClass *klass,
366 value = g_slice_alloc0 (size);
368 value->class = klass;
369 value->ref_count = 1;
375 _gtk_css_value_ref (GtkCssValue *value)
377 g_return_val_if_fail (value != NULL, NULL);
379 g_atomic_int_add (&value->ref_count, 1);
385 _gtk_css_value_unref (GtkCssValue *value)
390 if (!g_atomic_int_dec_and_test (&value->ref_count))
393 value->class->free (value);
397 _gtk_css_value_equal (const GtkCssValue *value1,
398 const GtkCssValue *value2)
400 g_return_val_if_fail (value1 != NULL, FALSE);
401 g_return_val_if_fail (value2 != NULL, FALSE);
403 if (value1->class != value2->class)
406 return value1->class->equal (value1, value2);
410 _gtk_css_value_print (const GtkCssValue *value,
413 g_return_if_fail (value != NULL);
414 g_return_if_fail (string != NULL);
416 value->class->print (value, string);
420 _gtk_css_value_get_content_type (const GtkCssValue *value)
422 g_return_val_if_fail (value->class == >K_CSS_VALUE_DEFAULT, G_TYPE_NONE);
428 _gtk_css_value_holds (const GtkCssValue *value, GType type)
430 g_return_val_if_fail (value->class == >K_CSS_VALUE_DEFAULT, FALSE);
432 return g_type_is_a (value->type, type);
436 fill_gvalue (const GtkCssValue *value,
443 if (g_type_is_a (type, G_TYPE_OBJECT))
444 g_value_set_object (g_value, value->u.ptr);
445 else if (g_type_is_a (type, G_TYPE_BOXED))
446 g_value_set_boxed (g_value, value->u.ptr);
447 else if (g_type_is_a (type, G_TYPE_INT))
448 g_value_set_int (g_value, value->u.gint);
449 else if (g_type_is_a (type, G_TYPE_UINT))
450 g_value_set_uint (g_value, value->u.guint);
451 else if (g_type_is_a (type, G_TYPE_BOOLEAN))
452 g_value_set_boolean (g_value, value->u.gint);
453 else if (g_type_is_a (type, G_TYPE_ENUM))
454 g_value_set_enum (g_value, value->u.gint);
455 else if (g_type_is_a (type, G_TYPE_FLAGS))
456 g_value_set_flags (g_value, value->u.guint);
457 else if (g_type_is_a (type, G_TYPE_STRING))
458 g_value_set_string (g_value, value->u.ptr);
459 else if (g_type_is_a (type, G_TYPE_DOUBLE))
460 g_value_set_double (g_value, value->u.dbl);
461 else if (g_type_is_a (type, G_TYPE_FLOAT))
462 g_value_set_float (g_value, value->u.flt);
464 g_value_copy (value->u.ptr, g_value);
468 _gtk_css_value_init_gvalue (const GtkCssValue *value,
473 g_return_if_fail (value->class == >K_CSS_VALUE_DEFAULT);
474 g_value_init (g_value, value->type);
475 fill_gvalue (value, g_value);
480 _gtk_css_value_get_symbolic_color (const GtkCssValue *value)
482 g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_SYMBOLIC_COLOR), NULL);
487 _gtk_css_value_get_int (const GtkCssValue *value)
489 g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_INT), 0);
490 return value->u.gint;
494 _gtk_css_value_get_enum (const GtkCssValue *value)
496 g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_ENUM), 0);
497 return value->u.gint;
501 _gtk_css_value_get_double (const GtkCssValue *value)
503 g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_DOUBLE), 0);
508 _gtk_css_value_dup_object (const GtkCssValue *value)
510 g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_OBJECT), NULL);
512 return g_object_ref (value->u.ptr);
517 _gtk_css_value_get_object (const GtkCssValue *value)
519 g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_OBJECT), NULL);
524 _gtk_css_value_get_boxed (const GtkCssValue *value)
526 g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_BOXED), NULL);
531 _gtk_css_value_get_strv (const GtkCssValue *value)
533 g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_STRV), NULL);
538 _gtk_css_value_get_border_style (const GtkCssValue *value)
540 g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_BORDER_STYLE), 0);
541 return value->u.gint;
544 const GtkCssBackgroundSize *
545 _gtk_css_value_get_background_size (const GtkCssValue *value)
547 g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BACKGROUND_SIZE), NULL);
551 const GtkCssBackgroundPosition *
552 _gtk_css_value_get_background_position (const GtkCssValue *value)
554 g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BACKGROUND_POSITION), NULL);
558 const GtkCssBorderImageRepeat *
559 _gtk_css_value_get_border_image_repeat (const GtkCssValue *value)
561 g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT), NULL);
565 const GtkCssBorderCornerRadius *
566 _gtk_css_value_get_border_corner_radius (const GtkCssValue *value)
568 g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS), NULL);
573 _gtk_css_value_get_gradient (const GtkCssValue *value)
575 g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_GRADIENT), NULL);