]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssvalue.c
styleproperty: Make gtk_style_property_register() not be valist
[~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 "gtkcssstylefuncsprivate.h"
22 #include "gtktypebuiltins.h"
23 #include "gtkgradient.h"
24 #include <cairo-gobject.h>
25 #include "gtkprivatetypebuiltins.h"
26
27 #include "fallback-c89.c"
28
29 struct _GtkCssValue
30 {
31   GTK_CSS_VALUE_BASE
32   GType type;
33   union {
34     gpointer ptr;
35     gint gint;
36     guint guint;
37     double dbl;
38     float flt;
39   } u;
40 };
41
42 static void
43 gtk_css_value_default_free (GtkCssValue *value)
44 {
45   GType type = value->type;
46
47   if (g_type_is_a (type, G_TYPE_OBJECT))
48     {
49       if (value->u.ptr != NULL)
50         g_object_unref (value->u.ptr);
51     }
52   else if (g_type_is_a (type, G_TYPE_BOXED))
53     {
54       if (value->u.ptr != NULL)
55         g_boxed_free (type, value->u.ptr);
56     }
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))
60     {}
61   else if (g_type_is_a (type, G_TYPE_UINT))
62     {}
63   else if (g_type_is_a (type, G_TYPE_BOOLEAN))
64     {}
65   else if (g_type_is_a (type, G_TYPE_ENUM))
66     {}
67   else if (g_type_is_a (type, G_TYPE_FLAGS))
68     {}
69   else if (g_type_is_a (type, G_TYPE_DOUBLE))
70     {}
71   else if (g_type_is_a (type, G_TYPE_FLOAT))
72     {}
73   else
74     {
75       g_value_unset (value->u.ptr);
76       g_slice_free (GValue, value->u.ptr);
77     }
78
79   g_slice_free (GtkCssValue, value);
80 }
81
82 static gboolean
83 gtk_css_value_default_equal (const GtkCssValue *value1,
84                              const GtkCssValue *value2)
85 {
86   return FALSE;
87 }
88
89 static void
90 gtk_css_value_default_print (const GtkCssValue *value,
91                              GString           *string)
92 {
93   GValue g_value = G_VALUE_INIT;
94
95   _gtk_css_value_init_gvalue (value, &g_value);
96   _gtk_css_style_print_value (&g_value, string);
97   g_value_unset (&g_value);
98 }
99
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
104 };
105
106 G_DEFINE_BOXED_TYPE (GtkCssValue, _gtk_css_value, _gtk_css_value_ref, _gtk_css_value_unref)
107
108 static GtkCssValue *
109 gtk_css_value_new (GType type)
110 {
111   GtkCssValue *value;
112
113   value = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_DEFAULT);
114
115   value->type = type;
116
117   return value;
118 }
119
120 GtkCssValue *
121 _gtk_css_value_new_from_gvalue (const GValue *g_value)
122 {
123   GtkCssValue *value;
124   GType type;
125
126   g_return_val_if_fail (g_value != NULL, NULL);
127
128   type = G_VALUE_TYPE (g_value);
129
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));
133   else if (type == GTK_TYPE_CSS_NUMBER)
134     value = _gtk_css_value_new_from_number (g_value_get_boxed (g_value));
135   else
136     {
137       value = gtk_css_value_new (type);
138
139       if (g_type_is_a (type, G_TYPE_OBJECT))
140         value->u.ptr = g_value_dup_object (g_value);
141       else if (g_type_is_a (type, G_TYPE_BOXED))
142         value->u.ptr = g_value_dup_boxed (g_value);
143       else if (g_type_is_a (type, G_TYPE_INT))
144         value->u.gint = g_value_get_int (g_value);
145       else if (g_type_is_a (type, G_TYPE_UINT))
146         value->u.guint = g_value_get_uint (g_value);
147       else if (g_type_is_a (type, G_TYPE_BOOLEAN))
148         value->u.gint = g_value_get_boolean (g_value);
149       else if (g_type_is_a (type, G_TYPE_ENUM))
150         value->u.gint = g_value_get_enum (g_value);
151       else if (g_type_is_a (type, G_TYPE_FLAGS))
152         value->u.guint = g_value_get_flags (g_value);
153       else if (g_type_is_a (type, G_TYPE_STRING))
154         value->u.ptr = g_value_dup_string (g_value);
155       else if (g_type_is_a (type, G_TYPE_DOUBLE))
156         value->u.dbl = g_value_get_double (g_value);
157       else if (g_type_is_a (type, G_TYPE_FLOAT))
158         value->u.flt = g_value_get_float (g_value);
159       else
160         {
161           value->u.ptr = g_slice_new0 (GValue);
162           g_value_init (value->u.ptr, G_VALUE_TYPE (g_value));
163           g_value_copy (g_value, value->u.ptr);
164         }
165     }
166
167   return value;
168 }
169
170 GtkCssValue *
171 _gtk_css_value_new_from_int (gint val)
172 {
173   GtkCssValue *value;
174   static GtkCssValue *singletons[4] = {NULL};
175
176   if (val >= 0 && val < G_N_ELEMENTS (singletons))
177     {
178       if (singletons[val] == NULL)
179         {
180           value = gtk_css_value_new (G_TYPE_INT);
181           value->u.gint = val;
182           singletons[val] = value;
183         }
184       return _gtk_css_value_ref (singletons[val]);
185     }
186
187   value = gtk_css_value_new (G_TYPE_INT);
188   value->u.gint = val;
189
190   return value;
191 }
192
193 GtkCssValue *
194 _gtk_css_value_new_from_enum (GType type,
195                               gint  val)
196 {
197   GtkCssValue *value;
198
199   g_return_val_if_fail (g_type_is_a (type, G_TYPE_ENUM), NULL);
200
201   value = gtk_css_value_new (type);
202   value->u.gint = val;
203
204   return value;
205 }
206
207 GtkCssValue *
208 _gtk_css_value_new_from_double (double d)
209 {
210   GtkCssValue *value;
211
212   value = gtk_css_value_new (G_TYPE_DOUBLE);
213   value->u.dbl = d;
214
215   return value;
216 }
217
218 GtkCssValue *
219 _gtk_css_value_new_take_string (char *string)
220 {
221   GtkCssValue *value;
222
223   value = gtk_css_value_new (G_TYPE_STRING);
224   value->u.ptr = string;
225
226   return value;
227 }
228
229 GtkCssValue *
230 _gtk_css_value_new_take_strv (char **strv)
231 {
232   GtkCssValue *value;
233
234   value = gtk_css_value_new (G_TYPE_STRV);
235   value->u.ptr = strv;
236
237   return value;
238 }
239
240 static gpointer
241 g_boxed_copy0 (GType         boxed_type,
242                gconstpointer src_boxed)
243 {
244   if (src_boxed == NULL)
245     return NULL;
246   return g_boxed_copy (boxed_type, src_boxed);
247 }
248
249 GtkCssValue *
250 _gtk_css_value_new_from_boxed (GType    type,
251                                gpointer boxed)
252 {
253   GtkCssValue *value;
254
255   g_return_val_if_fail (g_type_is_a (type, G_TYPE_BOXED), NULL);
256
257   value = gtk_css_value_new (type);
258   value->u.ptr = g_boxed_copy0 (type, boxed);
259
260   return value;
261 }
262
263 GtkCssValue *
264 _gtk_css_value_new_take_pattern (cairo_pattern_t *v)
265 {
266   GtkCssValue *value;
267
268   value = gtk_css_value_new (CAIRO_GOBJECT_TYPE_PATTERN);
269   value->u.ptr = v;
270
271   return value;
272 }
273
274 GtkCssValue *
275 _gtk_css_value_new_take_shadow (GtkShadow *v)
276 {
277   GtkCssValue *value;
278
279   value = gtk_css_value_new (GTK_TYPE_SHADOW);
280   value->u.ptr = v;
281
282   return value;
283 }
284
285 GtkCssValue *
286 _gtk_css_value_new_take_image (GtkCssImage *v)
287 {
288   GtkCssValue *value;
289
290   value = gtk_css_value_new (GTK_TYPE_CSS_IMAGE);
291   value->u.ptr = v;
292
293   return value;
294 }
295
296 GtkCssValue *
297 _gtk_css_value_new_from_theming_engine (GtkThemingEngine *v)
298 {
299   GtkCssValue *value;
300
301   value = gtk_css_value_new (GTK_TYPE_THEMING_ENGINE);
302   value->u.ptr = g_object_ref (v);
303
304   return value;
305 }
306
307 GtkCssValue *
308 _gtk_css_value_new_take_binding_sets (GPtrArray *array)
309 {
310   GtkCssValue *value;
311
312   value = gtk_css_value_new (G_TYPE_PTR_ARRAY);
313   value->u.ptr = array;
314
315   return value;
316 }
317
318 GtkCssValue *
319 _gtk_css_value_new_from_number (const GtkCssNumber *v)
320 {
321   GtkCssValue *value;
322   static GtkCssValue *zero_singleton = NULL;
323   static GtkCssValue *px_singletons[5] = {NULL};
324
325   if (v->unit == GTK_CSS_NUMBER &&
326       v->value == 0)
327     {
328       if (zero_singleton == NULL)
329         {
330           value = gtk_css_value_new (GTK_TYPE_CSS_NUMBER);
331           value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_NUMBER, v);
332           zero_singleton = value;
333         }
334       return _gtk_css_value_ref (zero_singleton);
335     }
336
337   if (v->unit == GTK_CSS_PX &&
338       (v->value == 0 ||
339        v->value == 1 ||
340        v->value == 2 ||
341        v->value == 3 ||
342        v->value == 4))
343     {
344       int i = round (v->value);
345       if (px_singletons[i] == NULL)
346         {
347           value = gtk_css_value_new (GTK_TYPE_CSS_NUMBER);
348           value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_NUMBER, v);
349           px_singletons[i] = value;
350         }
351
352       return _gtk_css_value_ref (px_singletons[i]);
353     }
354
355   value = gtk_css_value_new (GTK_TYPE_CSS_NUMBER);
356   value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_NUMBER, v);
357
358   return value;
359 }
360
361 GtkCssValue *
362 _gtk_css_value_new_from_rgba (const GdkRGBA *v)
363 {
364   GtkCssValue *value;
365
366   value = gtk_css_value_new (GDK_TYPE_RGBA);
367   value->u.ptr = g_boxed_copy0 (GDK_TYPE_RGBA, v);
368
369   return value;
370 }
371
372 GtkCssValue *
373 _gtk_css_value_new_from_color (const GdkColor *v)
374 {
375   GtkCssValue *value;
376
377   value = gtk_css_value_new (GDK_TYPE_COLOR);
378   value->u.ptr = g_boxed_copy0 (GDK_TYPE_COLOR, v);
379
380   return value;
381 }
382
383 GtkCssValue *
384 _gtk_css_value_new_from_background_size (const GtkCssBackgroundSize *v)
385 {
386   GtkCssValue *value;
387
388   value = gtk_css_value_new (GTK_TYPE_CSS_BACKGROUND_SIZE);
389   value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BACKGROUND_SIZE, v);
390
391   return value;
392 }
393
394 GtkCssValue *
395 _gtk_css_value_new_from_background_position (const GtkCssBackgroundPosition *v)
396 {
397   GtkCssValue *value;
398
399   value = gtk_css_value_new (GTK_TYPE_CSS_BACKGROUND_POSITION);
400   value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BACKGROUND_POSITION, v);
401
402   return value;
403 }
404
405 GtkCssValue *
406 _gtk_css_value_new_from_border_corner_radius (const GtkCssBorderCornerRadius *v)
407 {
408   GtkCssValue *value;
409
410   value = gtk_css_value_new (GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
411   value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BORDER_CORNER_RADIUS, v);
412
413   return value;
414 }
415
416 GtkCssValue *
417 _gtk_css_value_new_from_border_image_repeat (const GtkCssBorderImageRepeat *v)
418 {
419   GtkCssValue *value;
420
421   value = gtk_css_value_new (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
422   value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, v);
423
424   return value;
425 }
426
427 GtkCssValue *
428 _gtk_css_value_new_from_border_style (GtkBorderStyle style)
429 {
430   GtkCssValue *value;
431
432   value = gtk_css_value_new (GTK_TYPE_BORDER_STYLE);
433   value->u.gint = style;
434
435   return value;
436 }
437
438 GtkCssValue *
439 _gtk_css_value_new_take_symbolic_color (GtkSymbolicColor *v)
440 {
441   GtkCssValue *value;
442
443   value = gtk_css_value_new (GTK_TYPE_SYMBOLIC_COLOR);
444   value->u.ptr = v;
445
446   return value;
447 }
448
449 GtkCssValue *
450 _gtk_css_value_alloc (const GtkCssValueClass *klass,
451                       gsize                   size)
452 {
453   GtkCssValue *value;
454
455   value = g_slice_alloc0 (size);
456
457   value->class = klass;
458   value->ref_count = 1;
459
460   return value;
461 }
462
463 GtkCssValue *
464 _gtk_css_value_ref (GtkCssValue *value)
465 {
466   g_return_val_if_fail (value != NULL, NULL);
467
468   g_atomic_int_add (&value->ref_count, 1);
469
470   return value;
471 }
472
473 void
474 _gtk_css_value_unref (GtkCssValue *value)
475 {
476   if (value == NULL)
477     return;
478
479   if (!g_atomic_int_dec_and_test (&value->ref_count))
480     return;
481
482   value->class->free (value);
483 }
484
485 gboolean
486 _gtk_css_value_equal (const GtkCssValue *value1,
487                       const GtkCssValue *value2)
488 {
489   g_return_val_if_fail (value1 != NULL, FALSE);
490   g_return_val_if_fail (value2 != NULL, FALSE);
491
492   if (value1->class != value2->class)
493     return FALSE;
494
495   return value1->class->equal (value1, value2);
496 }
497
498 void
499 _gtk_css_value_print (const GtkCssValue *value,
500                       GString           *string)
501 {
502   g_return_if_fail (value != NULL);
503   g_return_if_fail (string != NULL);
504
505   value->class->print (value, string);
506 }
507
508 GType
509 _gtk_css_value_get_content_type (const GtkCssValue *value)
510 {
511   return value->type;
512 }
513
514 gboolean
515 _gtk_css_value_holds (const GtkCssValue *value, GType type)
516 {
517   return g_type_is_a (value->type, type);
518 }
519
520 static void
521 fill_gvalue (const GtkCssValue *value,
522              GValue            *g_value)
523 {
524   GType type;
525
526   type = value->type;
527
528   if (g_type_is_a (type, G_TYPE_OBJECT))
529     g_value_set_object (g_value, value->u.ptr);
530   else if (g_type_is_a (type, G_TYPE_BOXED))
531     g_value_set_boxed (g_value, value->u.ptr);
532   else if (g_type_is_a (type, G_TYPE_INT))
533     g_value_set_int (g_value, value->u.gint);
534   else if (g_type_is_a (type, G_TYPE_UINT))
535     g_value_set_uint (g_value, value->u.guint);
536   else if (g_type_is_a (type, G_TYPE_BOOLEAN))
537     g_value_set_boolean (g_value, value->u.gint);
538   else if (g_type_is_a (type, G_TYPE_ENUM))
539     g_value_set_enum (g_value, value->u.gint);
540   else if (g_type_is_a (type, G_TYPE_FLAGS))
541     g_value_set_flags (g_value, value->u.guint);
542   else if (g_type_is_a (type, G_TYPE_STRING))
543     g_value_set_string (g_value, value->u.ptr);
544   else if (g_type_is_a (type, G_TYPE_DOUBLE))
545     g_value_set_double (g_value, value->u.dbl);
546   else if (g_type_is_a (type, G_TYPE_FLOAT))
547     g_value_set_float (g_value, value->u.flt);
548   else
549     g_value_copy (value->u.ptr, g_value);
550 }
551
552 void
553 _gtk_css_value_init_gvalue (const GtkCssValue *value,
554                             GValue            *g_value)
555 {
556   if (value != NULL)
557     {
558       g_value_init (g_value, value->type);
559       fill_gvalue (value, g_value);
560     }
561 }
562
563 const GtkCssNumber *
564 _gtk_css_value_get_number (const GtkCssValue *value)
565 {
566   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_NUMBER), NULL);
567   return value->u.ptr;
568 }
569
570 GtkSymbolicColor *
571 _gtk_css_value_get_symbolic_color (const GtkCssValue *value)
572 {
573   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_SYMBOLIC_COLOR), NULL);
574   return value->u.ptr;
575 }
576
577 int
578 _gtk_css_value_get_int (const GtkCssValue *value)
579 {
580   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_INT), 0);
581   return value->u.gint;
582 }
583
584 int
585 _gtk_css_value_get_enum (const GtkCssValue *value)
586 {
587   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_ENUM), 0);
588   return value->u.gint;
589 }
590
591 double
592 _gtk_css_value_get_double (const GtkCssValue *value)
593 {
594   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_DOUBLE), 0);
595   return value->u.dbl;
596 }
597
598 const char *
599 _gtk_css_value_get_string (const GtkCssValue *value)
600 {
601   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_STRING), 0);
602   return value->u.ptr;
603 }
604
605 gpointer
606 _gtk_css_value_dup_object (const GtkCssValue *value)
607 {
608   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_OBJECT), NULL);
609   if (value->u.ptr)
610     return g_object_ref (value->u.ptr);
611   return NULL;
612 }
613
614 gpointer
615 _gtk_css_value_get_object (const GtkCssValue *value)
616 {
617   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_OBJECT), NULL);
618   return value->u.ptr;
619 }
620
621 gpointer
622 _gtk_css_value_get_boxed (const GtkCssValue *value)
623 {
624   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_BOXED), NULL);
625   return value->u.ptr;
626 }
627
628 const char **
629 _gtk_css_value_get_strv (const GtkCssValue *value)
630 {
631   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_STRV), NULL);
632   return value->u.ptr;
633 }
634
635 GtkCssImage *
636 _gtk_css_value_get_image (const GtkCssValue *value)
637 {
638   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_IMAGE), NULL);
639   return value->u.ptr;
640 }
641
642 GtkBorderStyle
643 _gtk_css_value_get_border_style (const GtkCssValue *value)
644 {
645   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_BORDER_STYLE), 0);
646   return value->u.gint;
647 }
648
649 const GtkCssBackgroundSize *
650 _gtk_css_value_get_background_size (const GtkCssValue *value)
651 {
652   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BACKGROUND_SIZE), NULL);
653   return value->u.ptr;
654 }
655
656 const GtkCssBackgroundPosition *
657 _gtk_css_value_get_background_position (const GtkCssValue *value)
658 {
659   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BACKGROUND_POSITION), NULL);
660   return value->u.ptr;
661 }
662
663 const GtkCssBorderImageRepeat *
664 _gtk_css_value_get_border_image_repeat (const GtkCssValue *value)
665 {
666   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT), NULL);
667   return value->u.ptr;
668 }
669
670 const GtkCssBorderCornerRadius *
671 _gtk_css_value_get_border_corner_radius (const GtkCssValue *value)
672 {
673   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS), NULL);
674   return value->u.ptr;
675 }
676
677 PangoStyle
678 _gtk_css_value_get_pango_style (const GtkCssValue *value)
679 {
680   g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_STYLE), 0);
681   return value->u.gint;
682 }
683
684 PangoVariant
685 _gtk_css_value_get_pango_variant (const GtkCssValue *value)
686 {
687   g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_VARIANT), 0);
688   return value->u.gint;
689 }
690
691 PangoWeight
692 _gtk_css_value_get_pango_weight (const GtkCssValue *value)
693 {
694   g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_WEIGHT), 0);
695   return value->u.gint;
696 }
697
698 const GdkRGBA *
699 _gtk_css_value_get_rgba (const GtkCssValue *value)
700 {
701   g_return_val_if_fail (_gtk_css_value_holds (value, GDK_TYPE_RGBA), NULL);
702   return value->u.ptr;
703 }
704
705 cairo_pattern_t *
706 _gtk_css_value_get_pattern (const GtkCssValue *value)
707 {
708   g_return_val_if_fail (_gtk_css_value_holds (value, CAIRO_GOBJECT_TYPE_PATTERN), NULL);
709   return value->u.ptr;
710 }
711
712 GtkGradient *
713 _gtk_css_value_get_gradient (const GtkCssValue *value)
714 {
715   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_GRADIENT), NULL);
716   return value->u.ptr;
717 }
718
719 GtkShadow *
720 _gtk_css_value_get_shadow (const GtkCssValue *value)
721 {
722   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_SHADOW), NULL);
723   return value->u.ptr;
724 }