]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssvalue.c
cssvalue: Remove unused function
[~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
134     {
135       value = gtk_css_value_new (type);
136
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);
157       else
158         {
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);
162         }
163     }
164
165   return value;
166 }
167
168 GtkCssValue *
169 _gtk_css_value_new_from_int (gint val)
170 {
171   GtkCssValue *value;
172   static GtkCssValue *singletons[4] = {NULL};
173
174   if (val >= 0 && val < G_N_ELEMENTS (singletons))
175     {
176       if (singletons[val] == NULL)
177         {
178           value = gtk_css_value_new (G_TYPE_INT);
179           value->u.gint = val;
180           singletons[val] = value;
181         }
182       return _gtk_css_value_ref (singletons[val]);
183     }
184
185   value = gtk_css_value_new (G_TYPE_INT);
186   value->u.gint = val;
187
188   return value;
189 }
190
191 GtkCssValue *
192 _gtk_css_value_new_from_enum (GType type,
193                               gint  val)
194 {
195   GtkCssValue *value;
196
197   g_return_val_if_fail (g_type_is_a (type, G_TYPE_ENUM), NULL);
198
199   value = gtk_css_value_new (type);
200   value->u.gint = val;
201
202   return value;
203 }
204
205 GtkCssValue *
206 _gtk_css_value_new_from_double (double d)
207 {
208   GtkCssValue *value;
209
210   value = gtk_css_value_new (G_TYPE_DOUBLE);
211   value->u.dbl = d;
212
213   return value;
214 }
215
216 GtkCssValue *
217 _gtk_css_value_new_take_string (char *string)
218 {
219   GtkCssValue *value;
220
221   value = gtk_css_value_new (G_TYPE_STRING);
222   value->u.ptr = string;
223
224   return value;
225 }
226
227 GtkCssValue *
228 _gtk_css_value_new_take_strv (char **strv)
229 {
230   GtkCssValue *value;
231
232   value = gtk_css_value_new (G_TYPE_STRV);
233   value->u.ptr = strv;
234
235   return value;
236 }
237
238 static gpointer
239 g_boxed_copy0 (GType         boxed_type,
240                gconstpointer src_boxed)
241 {
242   if (src_boxed == NULL)
243     return NULL;
244   return g_boxed_copy (boxed_type, src_boxed);
245 }
246
247 GtkCssValue *
248 _gtk_css_value_new_from_boxed (GType    type,
249                                gpointer boxed)
250 {
251   GtkCssValue *value;
252
253   g_return_val_if_fail (g_type_is_a (type, G_TYPE_BOXED), NULL);
254
255   value = gtk_css_value_new (type);
256   value->u.ptr = g_boxed_copy0 (type, boxed);
257
258   return value;
259 }
260
261 GtkCssValue *
262 _gtk_css_value_new_take_pattern (cairo_pattern_t *v)
263 {
264   GtkCssValue *value;
265
266   value = gtk_css_value_new (CAIRO_GOBJECT_TYPE_PATTERN);
267   value->u.ptr = v;
268
269   return value;
270 }
271
272 GtkCssValue *
273 _gtk_css_value_new_take_image (GtkCssImage *v)
274 {
275   GtkCssValue *value;
276
277   value = gtk_css_value_new (GTK_TYPE_CSS_IMAGE);
278   value->u.ptr = v;
279
280   return value;
281 }
282
283 GtkCssValue *
284 _gtk_css_value_new_from_theming_engine (GtkThemingEngine *v)
285 {
286   GtkCssValue *value;
287
288   value = gtk_css_value_new (GTK_TYPE_THEMING_ENGINE);
289   value->u.ptr = g_object_ref (v);
290
291   return value;
292 }
293
294 GtkCssValue *
295 _gtk_css_value_new_take_binding_sets (GPtrArray *array)
296 {
297   GtkCssValue *value;
298
299   value = gtk_css_value_new (G_TYPE_PTR_ARRAY);
300   value->u.ptr = array;
301
302   return value;
303 }
304
305 GtkCssValue *
306 _gtk_css_value_new_from_rgba (const GdkRGBA *v)
307 {
308   GtkCssValue *value;
309
310   value = gtk_css_value_new (GDK_TYPE_RGBA);
311   value->u.ptr = g_boxed_copy0 (GDK_TYPE_RGBA, v);
312
313   return value;
314 }
315
316 GtkCssValue *
317 _gtk_css_value_new_from_color (const GdkColor *v)
318 {
319   GtkCssValue *value;
320
321   value = gtk_css_value_new (GDK_TYPE_COLOR);
322   value->u.ptr = g_boxed_copy0 (GDK_TYPE_COLOR, v);
323
324   return value;
325 }
326
327 GtkCssValue *
328 _gtk_css_value_new_from_background_size (const GtkCssBackgroundSize *v)
329 {
330   GtkCssValue *value;
331
332   value = gtk_css_value_new (GTK_TYPE_CSS_BACKGROUND_SIZE);
333   value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BACKGROUND_SIZE, v);
334
335   return value;
336 }
337
338 GtkCssValue *
339 _gtk_css_value_new_from_background_position (const GtkCssBackgroundPosition *v)
340 {
341   GtkCssValue *value;
342
343   value = gtk_css_value_new (GTK_TYPE_CSS_BACKGROUND_POSITION);
344   value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BACKGROUND_POSITION, v);
345
346   return value;
347 }
348
349 GtkCssValue *
350 _gtk_css_value_new_from_border_corner_radius (const GtkCssBorderCornerRadius *v)
351 {
352   GtkCssValue *value;
353
354   value = gtk_css_value_new (GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
355   value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BORDER_CORNER_RADIUS, v);
356
357   return value;
358 }
359
360 GtkCssValue *
361 _gtk_css_value_new_from_border_image_repeat (const GtkCssBorderImageRepeat *v)
362 {
363   GtkCssValue *value;
364
365   value = gtk_css_value_new (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
366   value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, v);
367
368   return value;
369 }
370
371 GtkCssValue *
372 _gtk_css_value_new_from_border_style (GtkBorderStyle style)
373 {
374   GtkCssValue *value;
375
376   value = gtk_css_value_new (GTK_TYPE_BORDER_STYLE);
377   value->u.gint = style;
378
379   return value;
380 }
381
382 GtkCssValue *
383 _gtk_css_value_new_take_symbolic_color (GtkSymbolicColor *v)
384 {
385   GtkCssValue *value;
386
387   value = gtk_css_value_new (GTK_TYPE_SYMBOLIC_COLOR);
388   value->u.ptr = v;
389
390   return value;
391 }
392
393 GtkCssValue *
394 _gtk_css_value_alloc (const GtkCssValueClass *klass,
395                       gsize                   size)
396 {
397   GtkCssValue *value;
398
399   value = g_slice_alloc0 (size);
400
401   value->class = klass;
402   value->ref_count = 1;
403
404   return value;
405 }
406
407 GtkCssValue *
408 _gtk_css_value_ref (GtkCssValue *value)
409 {
410   g_return_val_if_fail (value != NULL, NULL);
411
412   g_atomic_int_add (&value->ref_count, 1);
413
414   return value;
415 }
416
417 void
418 _gtk_css_value_unref (GtkCssValue *value)
419 {
420   if (value == NULL)
421     return;
422
423   if (!g_atomic_int_dec_and_test (&value->ref_count))
424     return;
425
426   value->class->free (value);
427 }
428
429 gboolean
430 _gtk_css_value_equal (const GtkCssValue *value1,
431                       const GtkCssValue *value2)
432 {
433   g_return_val_if_fail (value1 != NULL, FALSE);
434   g_return_val_if_fail (value2 != NULL, FALSE);
435
436   if (value1->class != value2->class)
437     return FALSE;
438
439   return value1->class->equal (value1, value2);
440 }
441
442 void
443 _gtk_css_value_print (const GtkCssValue *value,
444                       GString           *string)
445 {
446   g_return_if_fail (value != NULL);
447   g_return_if_fail (string != NULL);
448
449   value->class->print (value, string);
450 }
451
452 GType
453 _gtk_css_value_get_content_type (const GtkCssValue *value)
454 {
455   return value->type;
456 }
457
458 gboolean
459 _gtk_css_value_holds (const GtkCssValue *value, GType type)
460 {
461   return g_type_is_a (value->type, type);
462 }
463
464 static void
465 fill_gvalue (const GtkCssValue *value,
466              GValue            *g_value)
467 {
468   GType type;
469
470   type = value->type;
471
472   if (g_type_is_a (type, G_TYPE_OBJECT))
473     g_value_set_object (g_value, value->u.ptr);
474   else if (g_type_is_a (type, G_TYPE_BOXED))
475     g_value_set_boxed (g_value, value->u.ptr);
476   else if (g_type_is_a (type, G_TYPE_INT))
477     g_value_set_int (g_value, value->u.gint);
478   else if (g_type_is_a (type, G_TYPE_UINT))
479     g_value_set_uint (g_value, value->u.guint);
480   else if (g_type_is_a (type, G_TYPE_BOOLEAN))
481     g_value_set_boolean (g_value, value->u.gint);
482   else if (g_type_is_a (type, G_TYPE_ENUM))
483     g_value_set_enum (g_value, value->u.gint);
484   else if (g_type_is_a (type, G_TYPE_FLAGS))
485     g_value_set_flags (g_value, value->u.guint);
486   else if (g_type_is_a (type, G_TYPE_STRING))
487     g_value_set_string (g_value, value->u.ptr);
488   else if (g_type_is_a (type, G_TYPE_DOUBLE))
489     g_value_set_double (g_value, value->u.dbl);
490   else if (g_type_is_a (type, G_TYPE_FLOAT))
491     g_value_set_float (g_value, value->u.flt);
492   else
493     g_value_copy (value->u.ptr, g_value);
494 }
495
496 void
497 _gtk_css_value_init_gvalue (const GtkCssValue *value,
498                             GValue            *g_value)
499 {
500   if (value != NULL)
501     {
502       g_value_init (g_value, value->type);
503       fill_gvalue (value, g_value);
504     }
505 }
506
507 GtkSymbolicColor *
508 _gtk_css_value_get_symbolic_color (const GtkCssValue *value)
509 {
510   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_SYMBOLIC_COLOR), NULL);
511   return value->u.ptr;
512 }
513
514 int
515 _gtk_css_value_get_int (const GtkCssValue *value)
516 {
517   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_INT), 0);
518   return value->u.gint;
519 }
520
521 int
522 _gtk_css_value_get_enum (const GtkCssValue *value)
523 {
524   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_ENUM), 0);
525   return value->u.gint;
526 }
527
528 double
529 _gtk_css_value_get_double (const GtkCssValue *value)
530 {
531   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_DOUBLE), 0);
532   return value->u.dbl;
533 }
534
535 const char *
536 _gtk_css_value_get_string (const GtkCssValue *value)
537 {
538   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_STRING), 0);
539   return value->u.ptr;
540 }
541
542 gpointer
543 _gtk_css_value_dup_object (const GtkCssValue *value)
544 {
545   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_OBJECT), NULL);
546   if (value->u.ptr)
547     return g_object_ref (value->u.ptr);
548   return NULL;
549 }
550
551 gpointer
552 _gtk_css_value_get_object (const GtkCssValue *value)
553 {
554   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_OBJECT), NULL);
555   return value->u.ptr;
556 }
557
558 gpointer
559 _gtk_css_value_get_boxed (const GtkCssValue *value)
560 {
561   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_BOXED), NULL);
562   return value->u.ptr;
563 }
564
565 const char **
566 _gtk_css_value_get_strv (const GtkCssValue *value)
567 {
568   g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_STRV), NULL);
569   return value->u.ptr;
570 }
571
572 GtkCssImage *
573 _gtk_css_value_get_image (const GtkCssValue *value)
574 {
575   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_IMAGE), NULL);
576   return value->u.ptr;
577 }
578
579 GtkBorderStyle
580 _gtk_css_value_get_border_style (const GtkCssValue *value)
581 {
582   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_BORDER_STYLE), 0);
583   return value->u.gint;
584 }
585
586 const GtkCssBackgroundSize *
587 _gtk_css_value_get_background_size (const GtkCssValue *value)
588 {
589   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BACKGROUND_SIZE), NULL);
590   return value->u.ptr;
591 }
592
593 const GtkCssBackgroundPosition *
594 _gtk_css_value_get_background_position (const GtkCssValue *value)
595 {
596   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BACKGROUND_POSITION), NULL);
597   return value->u.ptr;
598 }
599
600 const GtkCssBorderImageRepeat *
601 _gtk_css_value_get_border_image_repeat (const GtkCssValue *value)
602 {
603   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT), NULL);
604   return value->u.ptr;
605 }
606
607 const GtkCssBorderCornerRadius *
608 _gtk_css_value_get_border_corner_radius (const GtkCssValue *value)
609 {
610   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS), NULL);
611   return value->u.ptr;
612 }
613
614 PangoStyle
615 _gtk_css_value_get_pango_style (const GtkCssValue *value)
616 {
617   g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_STYLE), 0);
618   return value->u.gint;
619 }
620
621 PangoVariant
622 _gtk_css_value_get_pango_variant (const GtkCssValue *value)
623 {
624   g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_VARIANT), 0);
625   return value->u.gint;
626 }
627
628 PangoWeight
629 _gtk_css_value_get_pango_weight (const GtkCssValue *value)
630 {
631   g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_WEIGHT), 0);
632   return value->u.gint;
633 }
634
635 const GdkRGBA *
636 _gtk_css_value_get_rgba (const GtkCssValue *value)
637 {
638   g_return_val_if_fail (_gtk_css_value_holds (value, GDK_TYPE_RGBA), NULL);
639   return value->u.ptr;
640 }
641
642 GtkGradient *
643 _gtk_css_value_get_gradient (const GtkCssValue *value)
644 {
645   g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_GRADIENT), NULL);
646   return value->u.ptr;
647 }
648