]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssvalue.c
Merge branch 'wip/cssvalue'
[~andy/gtk] / gtk / gtkcssvalue.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include "config.h"
19
20 #include "gtkcssvalueprivate.h"
21 #include "gtktypebuiltins.h"
22 #include "gtkgradient.h"
23 #include <cairo-gobject.h>
24 #include "gtkprivatetypebuiltins.h"
25
26 #include "fallback-c89.c"
27
28 struct _GtkCssValue
29 {
30   volatile gint ref_count;
31   GType type;
32   union {
33     gpointer ptr;
34     gint gint;
35     guint guint;
36     double dbl;
37     float flt;
38   } u;
39 };
40
41 G_DEFINE_BOXED_TYPE (GtkCssValue, _gtk_css_value, _gtk_css_value_ref, _gtk_css_value_unref)
42
43 static GtkCssValue *
44 _gtk_css_value_new (GType type)
45 {
46   GtkCssValue *value;
47
48   value = g_slice_new0 (GtkCssValue);
49
50   value->ref_count = 1;
51   value->type = type;
52
53   return value;
54 }
55
56 GtkCssValue *
57 _gtk_css_value_new_from_gvalue (const GValue *g_value)
58 {
59   GtkCssValue *value;
60   GType type;
61
62   g_return_val_if_fail (g_value != NULL, NULL);
63
64   type = G_VALUE_TYPE (g_value);
65
66   /* Make sure we reuse the int/number singletons */
67   if (type == G_TYPE_INT)
68     value = _gtk_css_value_new_from_int (g_value_get_int (g_value));
69   else if (type == GTK_TYPE_CSS_NUMBER)
70     value = _gtk_css_value_new_from_number (g_value_get_boxed (g_value));
71   else
72     {
73       value = _gtk_css_value_new (type);
74
75       if (g_type_is_a (type, G_TYPE_OBJECT))
76         value->u.ptr = g_value_dup_object (g_value);
77       else if (g_type_is_a (type, G_TYPE_BOXED))
78         value->u.ptr = g_value_dup_boxed (g_value);
79       else if (g_type_is_a (type, G_TYPE_INT))
80         value->u.gint = g_value_get_int (g_value);
81       else if (g_type_is_a (type, G_TYPE_UINT))
82         value->u.guint = g_value_get_uint (g_value);
83       else if (g_type_is_a (type, G_TYPE_BOOLEAN))
84         value->u.gint = g_value_get_boolean (g_value);
85       else if (g_type_is_a (type, G_TYPE_ENUM))
86         value->u.gint = g_value_get_enum (g_value);
87       else if (g_type_is_a (type, G_TYPE_FLAGS))
88         value->u.guint = g_value_get_flags (g_value);
89       else if (g_type_is_a (type, G_TYPE_STRING))
90         value->u.ptr = g_value_dup_string (g_value);
91       else if (g_type_is_a (type, G_TYPE_DOUBLE))
92         value->u.dbl = g_value_get_double (g_value);
93       else if (g_type_is_a (type, G_TYPE_FLOAT))
94         value->u.flt = g_value_get_float (g_value);
95       else
96         g_assert_not_reached ();
97     }
98
99   return value;
100 }
101
102 GtkCssValue *
103 _gtk_css_value_new_take_gvalue (GValue *g_value)
104 {
105   GtkCssValue *value;
106   GType type;
107
108   g_return_val_if_fail (g_value != NULL, NULL);
109
110   type = G_VALUE_TYPE (g_value);
111
112   /* Make sure we reuse the int/number singletons */
113   if (type == G_TYPE_INT)
114     {
115       value = _gtk_css_value_new_from_int (g_value_get_int (g_value));
116       g_value_unset (g_value);
117     }
118   else if (type == GTK_TYPE_CSS_NUMBER)
119     {
120       value = _gtk_css_value_new_from_number (g_value_get_boxed (g_value));
121       g_value_unset (g_value);
122     }
123   else
124     {
125       value = _gtk_css_value_new (type);
126
127       if (g_type_is_a (type, G_TYPE_OBJECT))
128         value->u.ptr = g_value_get_object (g_value);
129       else if (g_type_is_a (type, G_TYPE_BOXED))
130         value->u.ptr = g_value_get_boxed (g_value);
131       else if (g_type_is_a (type, G_TYPE_INT))
132         value->u.gint = g_value_get_int (g_value);
133       else if (g_type_is_a (type, G_TYPE_UINT))
134         value->u.guint = g_value_get_uint (g_value);
135       else if (g_type_is_a (type, G_TYPE_BOOLEAN))
136         value->u.gint = g_value_get_boolean (g_value);
137       else if (g_type_is_a (type, G_TYPE_ENUM))
138         value->u.gint = g_value_get_enum (g_value);
139       else if (g_type_is_a (type, G_TYPE_FLAGS))
140         value->u.guint = g_value_get_flags (g_value);
141       else if (g_type_is_a (type, G_TYPE_STRING))
142         value->u.ptr = g_value_dup_string (g_value);
143       else if (g_type_is_a (type, G_TYPE_DOUBLE))
144         value->u.dbl = g_value_get_double (g_value);
145       else if (g_type_is_a (type, G_TYPE_FLOAT))
146         value->u.flt = g_value_get_float (g_value);
147       else
148         g_assert_not_reached ();
149     }
150
151   return value;
152 }
153
154 GtkCssValue *
155 _gtk_css_value_new_from_int (gint val)
156 {
157   GtkCssValue *value;
158   static GtkCssValue *singletons[4] = {NULL};
159
160   if (val >= 0 && val < G_N_ELEMENTS (singletons))
161     {
162       if (singletons[val] == NULL)
163         {
164           value = _gtk_css_value_new (G_TYPE_INT);
165           value->u.gint = val;
166           singletons[val] = value;
167         }
168       return _gtk_css_value_ref (singletons[val]);
169     }
170
171   value = _gtk_css_value_new (G_TYPE_INT);
172   value->u.gint = val;
173
174   return value;
175 }
176
177 GtkCssValue *
178 _gtk_css_value_new_take_string (char *string)
179 {
180   GtkCssValue *value;
181
182   value = _gtk_css_value_new (G_TYPE_STRING);
183   value->u.ptr = string;
184
185   return value;
186 }
187
188 GtkCssValue *
189 _gtk_css_value_new_from_string (const char *string)
190 {
191   GtkCssValue *value;
192
193   value = _gtk_css_value_new (G_TYPE_STRING);
194   value->u.ptr = g_strdup (string);
195
196   return value;
197 }
198
199 static gpointer
200 g_boxed_copy0 (GType         boxed_type,
201                gconstpointer src_boxed)
202 {
203   if (src_boxed == NULL)
204     return NULL;
205   return g_boxed_copy (boxed_type, src_boxed);
206 }
207
208 GtkCssValue *
209 _gtk_css_value_new_from_border (const GtkBorder *border)
210 {
211   GtkCssValue *value;
212
213   value = _gtk_css_value_new (GTK_TYPE_BORDER);
214   value->u.ptr = g_boxed_copy0 (GTK_TYPE_BORDER, border);
215
216   return value;
217 }
218
219 GtkCssValue *
220 _gtk_css_value_new_take_pattern (cairo_pattern_t *v)
221 {
222   GtkCssValue *value;
223
224   value = _gtk_css_value_new (CAIRO_GOBJECT_TYPE_PATTERN);
225   value->u.ptr = v;
226
227   return value;
228 }
229
230 GtkCssValue *
231 _gtk_css_value_new_from_pattern (const cairo_pattern_t *v)
232 {
233   GtkCssValue *value;
234
235   value = _gtk_css_value_new (CAIRO_GOBJECT_TYPE_PATTERN);
236   value->u.ptr = g_boxed_copy0 (CAIRO_GOBJECT_TYPE_PATTERN, v);
237
238   return value;
239 }
240
241 GtkCssValue *
242 _gtk_css_value_new_take_shadow (GtkShadow *v)
243 {
244   GtkCssValue *value;
245
246   value = _gtk_css_value_new (GTK_TYPE_SHADOW);
247   value->u.ptr = v;
248
249   return value;
250 }
251
252 GtkCssValue *
253 _gtk_css_value_new_take_font_description (PangoFontDescription *v)
254 {
255   GtkCssValue *value;
256
257   value = _gtk_css_value_new (PANGO_TYPE_FONT_DESCRIPTION);
258   value->u.ptr = v;
259
260   return value;
261 }
262
263 GtkCssValue *
264 _gtk_css_value_new_take_image (GtkCssImage *v)
265 {
266   GtkCssValue *value;
267
268   value = _gtk_css_value_new (GTK_TYPE_CSS_IMAGE);
269   value->u.ptr = v;
270
271   return value;
272 }
273
274 GtkCssValue *
275 _gtk_css_value_new_from_number (const GtkCssNumber *v)
276 {
277   GtkCssValue *value;
278   static GtkCssValue *zero_singleton = NULL;
279   static GtkCssValue *px_singletons[5] = {NULL};
280
281   if (v->unit == GTK_CSS_NUMBER &&
282       v->value == 0)
283     {
284       if (zero_singleton == NULL)
285         {
286           value = _gtk_css_value_new (GTK_TYPE_CSS_NUMBER);
287           value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_NUMBER, v);
288           zero_singleton = value;
289         }
290       return _gtk_css_value_ref (zero_singleton);
291     }
292
293   if (v->unit == GTK_CSS_PX &&
294       (v->value == 0 ||
295        v->value == 1 ||
296        v->value == 2 ||
297        v->value == 3 ||
298        v->value == 4))
299     {
300       int i = round (v->value);
301       if (px_singletons[i] == NULL)
302         {
303           value = _gtk_css_value_new (GTK_TYPE_CSS_NUMBER);
304           value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_NUMBER, v);
305           px_singletons[i] = value;
306         }
307
308       return _gtk_css_value_ref (px_singletons[i]);
309     }
310
311   value = _gtk_css_value_new (GTK_TYPE_CSS_NUMBER);
312   value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_NUMBER, v);
313
314   return value;
315 }
316
317 GtkCssValue *
318 _gtk_css_value_new_from_rgba (const GdkRGBA *v)
319 {
320   GtkCssValue *value;
321
322   value = _gtk_css_value_new (GDK_TYPE_RGBA);
323   value->u.ptr = g_boxed_copy0 (GDK_TYPE_RGBA, v);
324
325   return value;
326 }
327
328 GtkCssValue *
329 _gtk_css_value_new_from_color (const GdkColor *v)
330 {
331   GtkCssValue *value;
332
333   value = _gtk_css_value_new (GDK_TYPE_COLOR);
334   value->u.ptr = g_boxed_copy0 (GDK_TYPE_COLOR, v);
335
336   return value;
337 }
338
339 GtkCssValue *
340 _gtk_css_value_new_from_background_size (const GtkCssBackgroundSize *v)
341 {
342   GtkCssValue *value;
343
344   value = _gtk_css_value_new (GTK_TYPE_CSS_BACKGROUND_SIZE);
345   value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BACKGROUND_SIZE, v);
346
347   return value;
348 }
349
350 GtkCssValue *
351 _gtk_css_value_new_take_symbolic_color (GtkSymbolicColor *v)
352 {
353   GtkCssValue *value;
354
355   value = _gtk_css_value_new (GTK_TYPE_SYMBOLIC_COLOR);
356   value->u.ptr = v;
357
358   return value;
359 }
360
361 GtkCssValue *
362 _gtk_css_value_ref (GtkCssValue *value)
363 {
364   g_return_val_if_fail (value != NULL, NULL);
365
366   g_atomic_int_add (&value->ref_count, 1);
367
368   return value;
369 }
370
371 void
372 _gtk_css_value_unref (GtkCssValue *value)
373 {
374   GType type;
375
376   if (value == NULL)
377     return;
378
379   if (!g_atomic_int_dec_and_test (&value->ref_count))
380     return;
381
382   type = value->type;
383
384   if (g_type_is_a (type, G_TYPE_OBJECT) && value->u.ptr != NULL)
385     g_object_unref (value->u.ptr);
386   else if (g_type_is_a (type, G_TYPE_BOXED) && value->u.ptr != NULL)
387     g_boxed_free (type, value->u.ptr);
388   else if (g_type_is_a (type, G_TYPE_STRING))
389     g_free (value->u.ptr);
390
391   g_slice_free (GtkCssValue, value);
392 }
393
394 GType
395 _gtk_css_value_get_content_type (GtkCssValue *value)
396 {
397   return value->type;
398 }
399
400 gboolean
401 _gtk_css_value_holds (GtkCssValue *value, GType type)
402 {
403   return g_type_is_a (value->type, type);
404 }
405
406 static void
407 fill_gvalue (GtkCssValue *value,
408              GValue      *g_value)
409 {
410   GType type;
411
412   type = value->type;
413
414   if (g_type_is_a (type, G_TYPE_OBJECT))
415     g_value_set_object (g_value, value->u.ptr);
416   else if (g_type_is_a (type, G_TYPE_BOXED))
417     g_value_set_boxed (g_value, value->u.ptr);
418   else if (g_type_is_a (type, G_TYPE_INT))
419     g_value_set_int (g_value, value->u.gint);
420   else if (g_type_is_a (type, G_TYPE_UINT))
421     g_value_set_uint (g_value, value->u.guint);
422   else if (g_type_is_a (type, G_TYPE_BOOLEAN))
423     g_value_set_boolean (g_value, value->u.gint);
424   else if (g_type_is_a (type, G_TYPE_ENUM))
425     g_value_set_enum (g_value, value->u.gint);
426   else if (g_type_is_a (type, G_TYPE_FLAGS))
427     g_value_set_flags (g_value, value->u.guint);
428   else if (g_type_is_a (type, G_TYPE_STRING))
429     g_value_set_string (g_value, value->u.ptr);
430   else if (g_type_is_a (type, G_TYPE_DOUBLE))
431     g_value_set_double (g_value, value->u.dbl);
432   else if (g_type_is_a (type, G_TYPE_FLOAT))
433     g_value_set_float (g_value, value->u.flt);
434   else
435     g_assert_not_reached ();
436 }
437
438 void
439 _gtk_css_value_init_gvalue (GtkCssValue *value,
440                             GValue      *g_value)
441 {
442   if (value != NULL)
443     {
444       g_value_init (g_value, value->type);
445       fill_gvalue (value, g_value);
446     }
447 }
448
449 void
450 _gtk_css_value_to_gvalue (GtkCssValue *value,
451                           GValue      *g_value)
452 {
453   if (value->type == G_VALUE_TYPE (g_value))
454     fill_gvalue (value, g_value);
455   else if (g_value_type_transformable (value->type, G_VALUE_TYPE (g_value)))
456     {
457       GValue v = G_VALUE_INIT;
458       _gtk_css_value_init_gvalue (value, &v);
459       g_value_transform (&v, g_value);
460       g_value_unset (&v);
461     }
462   else
463     g_warning ("can't convert css value of type `%s' as value of type `%s'",
464                g_type_name (value->type),
465                G_VALUE_TYPE_NAME (g_value));
466 }
467
468 gboolean
469 _gtk_css_value_is_special  (GtkCssValue *value)
470 {
471   return _gtk_css_value_holds (value, GTK_TYPE_CSS_SPECIAL_VALUE);
472 }
473
474 GtkCssSpecialValue
475 _gtk_css_value_get_special_kind  (GtkCssValue *value)
476 {
477   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_SPECIAL_VALUE), 0);
478   return value->u.gint;
479 }
480
481 GtkCssNumber *
482 _gtk_css_value_get_number  (GtkCssValue *value)
483 {
484   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_NUMBER), NULL);
485   return value->u.ptr;
486 }
487
488 GtkSymbolicColor *
489 _gtk_css_value_get_symbolic_color  (GtkCssValue *value)
490 {
491   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_SYMBOLIC_COLOR), NULL);
492   return value->u.ptr;
493 }
494
495 int
496 _gtk_css_value_get_int (GtkCssValue *value)
497 {
498   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_INT), 0);
499   return value->u.gint;
500 }
501
502 double
503 _gtk_css_value_get_double (GtkCssValue *value)
504 {
505   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_DOUBLE), 0);
506   return value->u.dbl;
507 }
508
509 const char *
510 _gtk_css_value_get_string (GtkCssValue *value)
511 {
512   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_STRING), 0);
513   return value->u.ptr;
514 }
515
516 gpointer
517 _gtk_css_value_dup_object (GtkCssValue *value)
518 {
519   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_OBJECT), NULL);
520   if (value->u.ptr)
521     return g_object_ref (value->u.ptr);
522   return NULL;
523 }
524
525 gpointer
526 _gtk_css_value_get_object (GtkCssValue *value)
527 {
528   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_OBJECT), NULL);
529   return value->u.ptr;
530 }
531
532 gpointer
533 _gtk_css_value_get_boxed (GtkCssValue *value)
534 {
535   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_BOXED), NULL);
536   return value->u.ptr;
537 }
538
539 const char **
540 _gtk_css_value_get_strv (GtkCssValue *value)
541 {
542   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_STRV), NULL);
543   return value->u.ptr;
544 }
545
546 GtkCssImage *
547 _gtk_css_value_get_image (GtkCssValue *value)
548 {
549   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_IMAGE), NULL);
550   return value->u.ptr;
551 }
552
553 GtkBorderStyle
554 _gtk_css_value_get_border_style (GtkCssValue *value)
555 {
556   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_BORDER_STYLE), 0);
557   return value->u.gint;
558 }
559
560 GtkCssBackgroundSize *
561 _gtk_css_value_get_background_size (GtkCssValue *value)
562 {
563   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BACKGROUND_SIZE), NULL);
564   return value->u.ptr;
565 }
566
567 GtkCssBorderImageRepeat *
568 _gtk_css_value_get_border_image_repeat (GtkCssValue *value)
569 {
570   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT), NULL);
571   return value->u.ptr;
572 }
573
574 GtkCssBorderCornerRadius *
575 _gtk_css_value_get_border_corner_radius (GtkCssValue *value)
576 {
577   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS), NULL);
578   return value->u.ptr;
579 }
580
581 PangoFontDescription *
582 _gtk_css_value_get_font_description (GtkCssValue *value)
583 {
584   g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_FONT_DESCRIPTION), 0);
585   return value->u.ptr;
586 }
587
588 PangoStyle
589 _gtk_css_value_get_pango_style (GtkCssValue *value)
590 {
591   g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_STYLE), 0);
592   return value->u.gint;
593 }
594
595 PangoVariant
596 _gtk_css_value_get_pango_variant (GtkCssValue *value)
597 {
598   g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_VARIANT), 0);
599   return value->u.gint;
600 }
601
602 PangoWeight
603 _gtk_css_value_get_pango_weight (GtkCssValue *value)
604 {
605   g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_WEIGHT), 0);
606   return value->u.gint;
607 }
608
609 GdkRGBA *
610 _gtk_css_value_get_rgba (GtkCssValue *value)
611 {
612   g_return_val_if_fail (_gtk_css_value_holds (value, GDK_TYPE_RGBA), NULL);
613   return value->u.ptr;
614 }
615
616 cairo_pattern_t *
617 _gtk_css_value_get_pattern (GtkCssValue *value)
618 {
619   g_return_val_if_fail (_gtk_css_value_holds (value, CAIRO_GOBJECT_TYPE_PATTERN), NULL);
620   return value->u.ptr;
621 }
622
623 GtkGradient *
624 _gtk_css_value_get_gradient (GtkCssValue *value)
625 {
626   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_GRADIENT), NULL);
627   return value->u.ptr;
628 }
629
630 GtkShadow *
631 _gtk_css_value_get_shadow (GtkCssValue *value)
632 {
633   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_SHADOW), NULL);
634   return value->u.ptr;
635 }