]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperty.c
69a16f2e3a0c02457baf471cafd264997162cad5
[~andy/gtk] / gtk / gtkstyleproperty.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include "gtkstylepropertyprivate.h"
23
24 #include <errno.h>
25 #include <math.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <gdk-pixbuf/gdk-pixbuf.h>
30 #include <cairo-gobject.h>
31
32 #include "gtkcssprovider.h"
33 #include "gtkcssparserprivate.h"
34 #include "gtkcssshorthandpropertyprivate.h"
35 #include "gtkcssstylepropertyprivate.h"
36 #include "gtkcsstypesprivate.h"
37 #include "gtkintl.h"
38 #include "gtkprivatetypebuiltins.h"
39 #include "gtkstylepropertiesprivate.h"
40
41 /* the actual parsers we have */
42 #include "gtkanimationdescription.h"
43 #include "gtkbindings.h"
44 #include "gtkgradient.h"
45 #include "gtkshadowprivate.h"
46 #include "gtkthemingengine.h"
47 #include "gtktypebuiltins.h"
48 #include "gtkwin32themeprivate.h"
49
50 /* this is in case round() is not provided by the compiler, 
51  * such as in the case of C89 compilers, like MSVC
52  */
53 #include "fallback-c89.c"
54
55 enum {
56   PROP_0,
57   PROP_NAME,
58   PROP_VALUE_TYPE
59 };
60
61 static GHashTable *parse_funcs = NULL;
62 static GHashTable *print_funcs = NULL;
63 static GPtrArray *__style_property_array = NULL;
64
65 G_DEFINE_ABSTRACT_TYPE (GtkStyleProperty, _gtk_style_property, G_TYPE_OBJECT)
66
67 static void
68 gtk_style_property_finalize (GObject *object)
69 {
70   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
71
72   g_warning ("finalizing %s `%s', how could this happen?", G_OBJECT_TYPE_NAME (object), property->name);
73
74   G_OBJECT_CLASS (_gtk_style_property_parent_class)->finalize (object);
75 }
76
77 static void
78 gtk_style_property_set_property (GObject      *object,
79                                  guint         prop_id,
80                                  const GValue *value,
81                                  GParamSpec   *pspec)
82 {
83   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
84   GtkStylePropertyClass *klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
85
86   switch (prop_id)
87     {
88     case PROP_NAME:
89       property->name = g_value_dup_string (value);
90       g_assert (property->name);
91       g_assert (g_hash_table_lookup (klass->properties, property->name) == NULL);
92       g_hash_table_insert (klass->properties, property->name, property);
93       break;
94     case PROP_VALUE_TYPE:
95       property->value_type = g_value_get_gtype (value);
96       break;
97     default:
98       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
99       break;
100     }
101 }
102
103 static void
104 gtk_style_property_get_property (GObject    *object,
105                                  guint       prop_id,
106                                  GValue     *value,
107                                  GParamSpec *pspec)
108 {
109   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
110
111   switch (prop_id)
112     {
113     case PROP_NAME:
114       g_value_set_string (value, property->name);
115       break;
116     case PROP_VALUE_TYPE:
117       g_value_set_gtype (value, property->value_type);
118       break;
119     default:
120       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121       break;
122     }
123 }
124
125 static void
126 _gtk_style_property_class_init (GtkStylePropertyClass *klass)
127 {
128   GObjectClass *object_class = G_OBJECT_CLASS (klass);
129
130   object_class->finalize = gtk_style_property_finalize;
131   object_class->set_property = gtk_style_property_set_property;
132   object_class->get_property = gtk_style_property_get_property;
133
134   g_object_class_install_property (object_class,
135                                    PROP_NAME,
136                                    g_param_spec_string ("name",
137                                                         P_("Property name"),
138                                                         P_("The name of the property"),
139                                                         NULL,
140                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
141   g_object_class_install_property (object_class,
142                                    PROP_VALUE_TYPE,
143                                    g_param_spec_gtype ("value-type",
144                                                        P_("Value type"),
145                                                        P_("The value type returned by GtkStyleContext"),
146                                                        G_TYPE_NONE,
147                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
148
149   klass->properties = g_hash_table_new (g_str_hash, g_str_equal);
150 }
151
152 static void
153 _gtk_style_property_init (GtkStyleProperty *property)
154 {
155   property->value_type = G_TYPE_NONE;
156 }
157
158 static void
159 register_conversion_function (GType             type,
160                               GtkStyleParseFunc parse,
161                               GtkStylePrintFunc print)
162 {
163   if (parse)
164     g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
165   if (print)
166     g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
167 }
168
169 static void
170 string_append_double (GString *string,
171                       double   d)
172 {
173   char buf[G_ASCII_DTOSTR_BUF_SIZE];
174
175   g_ascii_dtostr (buf, sizeof (buf), d);
176   g_string_append (string, buf);
177 }
178
179 static void
180 string_append_string (GString    *str,
181                       const char *string)
182 {
183   gsize len;
184
185   g_string_append_c (str, '"');
186
187   do {
188     len = strcspn (string, "\"\n\r\f");
189     g_string_append (str, string);
190     string += len;
191     switch (*string)
192       {
193       case '\0':
194         break;
195       case '\n':
196         g_string_append (str, "\\A ");
197         break;
198       case '\r':
199         g_string_append (str, "\\D ");
200         break;
201       case '\f':
202         g_string_append (str, "\\C ");
203         break;
204       case '\"':
205         g_string_append (str, "\\\"");
206         break;
207       default:
208         g_assert_not_reached ();
209         break;
210       }
211   } while (*string);
212
213   g_string_append_c (str, '"');
214 }
215
216 /*** IMPLEMENTATIONS ***/
217
218 static gboolean 
219 enum_parse (GtkCssParser *parser,
220             GType         type,
221             int          *res)
222 {
223   char *str;
224
225   if (_gtk_css_parser_try_enum (parser, type, res))
226     return TRUE;
227
228   str = _gtk_css_parser_try_ident (parser, TRUE);
229   if (str == NULL)
230     {
231       _gtk_css_parser_error (parser, "Expected an identifier");
232       return FALSE;
233     }
234
235   _gtk_css_parser_error (parser,
236                          "Unknown value '%s' for enum type '%s'",
237                          str, g_type_name (type));
238   g_free (str);
239
240   return FALSE;
241 }
242
243 static void
244 enum_print (int         value,
245             GType       type,
246             GString    *string)
247 {
248   GEnumClass *enum_class;
249   GEnumValue *enum_value;
250
251   enum_class = g_type_class_ref (type);
252   enum_value = g_enum_get_value (enum_class, value);
253
254   g_string_append (string, enum_value->value_nick);
255
256   g_type_class_unref (enum_class);
257 }
258
259 static gboolean
260 rgba_value_parse (GtkCssParser *parser,
261                   GFile        *base,
262                   GValue       *value)
263 {
264   GtkSymbolicColor *symbolic;
265   GdkRGBA rgba;
266
267   symbolic = _gtk_css_parser_read_symbolic_color (parser);
268   if (symbolic == NULL)
269     return FALSE;
270
271   if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
272     {
273       g_value_set_boxed (value, &rgba);
274       gtk_symbolic_color_unref (symbolic);
275     }
276   else
277     {
278       g_value_unset (value);
279       g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
280       g_value_take_boxed (value, symbolic);
281     }
282
283   return TRUE;
284 }
285
286 static void
287 rgba_value_print (const GValue *value,
288                   GString      *string)
289 {
290   const GdkRGBA *rgba = g_value_get_boxed (value);
291
292   if (rgba == NULL)
293     g_string_append (string, "none");
294   else
295     {
296       char *s = gdk_rgba_to_string (rgba);
297       g_string_append (string, s);
298       g_free (s);
299     }
300 }
301
302 static gboolean 
303 color_value_parse (GtkCssParser *parser,
304                    GFile        *base,
305                    GValue       *value)
306 {
307   GtkSymbolicColor *symbolic;
308   GdkRGBA rgba;
309
310   symbolic = _gtk_css_parser_read_symbolic_color (parser);
311   if (symbolic == NULL)
312     return FALSE;
313
314   if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
315     {
316       GdkColor color;
317
318       color.red = rgba.red * 65535. + 0.5;
319       color.green = rgba.green * 65535. + 0.5;
320       color.blue = rgba.blue * 65535. + 0.5;
321
322       g_value_set_boxed (value, &color);
323     }
324   else
325     {
326       g_value_unset (value);
327       g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
328       g_value_take_boxed (value, symbolic);
329     }
330
331   return TRUE;
332 }
333
334 static void
335 color_value_print (const GValue *value,
336                    GString      *string)
337 {
338   const GdkColor *color = g_value_get_boxed (value);
339
340   if (color == NULL)
341     g_string_append (string, "none");
342   else
343     {
344       char *s = gdk_color_to_string (color);
345       g_string_append (string, s);
346       g_free (s);
347     }
348 }
349
350 static gboolean
351 symbolic_color_value_parse (GtkCssParser *parser,
352                             GFile        *base,
353                             GValue       *value)
354 {
355   GtkSymbolicColor *symbolic;
356
357   symbolic = _gtk_css_parser_read_symbolic_color (parser);
358   if (symbolic == NULL)
359     return FALSE;
360
361   g_value_take_boxed (value, symbolic);
362   return TRUE;
363 }
364
365 static void
366 symbolic_color_value_print (const GValue *value,
367                             GString      *string)
368 {
369   GtkSymbolicColor *symbolic = g_value_get_boxed (value);
370
371   if (symbolic == NULL)
372     g_string_append (string, "none");
373   else
374     {
375       char *s = gtk_symbolic_color_to_string (symbolic);
376       g_string_append (string, s);
377       g_free (s);
378     }
379 }
380
381 static gboolean
382 font_family_parse (GtkCssParser *parser,
383                    GFile        *base,
384                    GValue       *value)
385 {
386   GPtrArray *names;
387   char *name;
388
389   /* We don't special case generic families. Pango should do
390    * that for us */
391
392   names = g_ptr_array_new ();
393
394   do {
395     name = _gtk_css_parser_try_ident (parser, TRUE);
396     if (name)
397       {
398         GString *string = g_string_new (name);
399         g_free (name);
400         while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
401           {
402             g_string_append_c (string, ' ');
403             g_string_append (string, name);
404             g_free (name);
405           }
406         name = g_string_free (string, FALSE);
407       }
408     else 
409       {
410         name = _gtk_css_parser_read_string (parser);
411         if (name == NULL)
412           {
413             g_ptr_array_free (names, TRUE);
414             return FALSE;
415           }
416       }
417
418     g_ptr_array_add (names, name);
419   } while (_gtk_css_parser_try (parser, ",", TRUE));
420
421   /* NULL-terminate array */
422   g_ptr_array_add (names, NULL);
423   g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
424   return TRUE;
425 }
426
427 static void
428 font_family_value_print (const GValue *value,
429                          GString      *string)
430 {
431   const char **names = g_value_get_boxed (value);
432
433   if (names == NULL || *names == NULL)
434     {
435       g_string_append (string, "none");
436       return;
437     }
438
439   string_append_string (string, *names);
440   names++;
441   while (*names)
442     {
443       g_string_append (string, ", ");
444       string_append_string (string, *names);
445       names++;
446     }
447 }
448
449 static gboolean 
450 font_description_value_parse (GtkCssParser *parser,
451                               GFile        *base,
452                               GValue       *value)
453 {
454   PangoFontDescription *font_desc;
455   guint mask;
456   char *str;
457
458   str = _gtk_css_parser_read_value (parser);
459   if (str == NULL)
460     return FALSE;
461
462   font_desc = pango_font_description_from_string (str);
463   mask = pango_font_description_get_set_fields (font_desc);
464   /* These values are not really correct,
465    * but the fields must be set, so we set them to something */
466   if ((mask & PANGO_FONT_MASK_FAMILY) == 0)
467     pango_font_description_set_family_static (font_desc, "Sans");
468   if ((mask & PANGO_FONT_MASK_SIZE) == 0)
469     pango_font_description_set_size (font_desc, 10 * PANGO_SCALE);
470   g_free (str);
471   g_value_take_boxed (value, font_desc);
472   return TRUE;
473 }
474
475 static void
476 font_description_value_print (const GValue *value,
477                               GString      *string)
478 {
479   const PangoFontDescription *desc = g_value_get_boxed (value);
480
481   if (desc == NULL)
482     g_string_append (string, "none");
483   else
484     {
485       char *s = pango_font_description_to_string (desc);
486       g_string_append (string, s);
487       g_free (s);
488     }
489 }
490
491 static gboolean 
492 boolean_value_parse (GtkCssParser *parser,
493                      GFile        *base,
494                      GValue       *value)
495 {
496   if (_gtk_css_parser_try (parser, "true", TRUE) ||
497       _gtk_css_parser_try (parser, "1", TRUE))
498     {
499       g_value_set_boolean (value, TRUE);
500       return TRUE;
501     }
502   else if (_gtk_css_parser_try (parser, "false", TRUE) ||
503            _gtk_css_parser_try (parser, "0", TRUE))
504     {
505       g_value_set_boolean (value, FALSE);
506       return TRUE;
507     }
508   else
509     {
510       _gtk_css_parser_error (parser, "Expected a boolean value");
511       return FALSE;
512     }
513 }
514
515 static void
516 boolean_value_print (const GValue *value,
517                      GString      *string)
518 {
519   if (g_value_get_boolean (value))
520     g_string_append (string, "true");
521   else
522     g_string_append (string, "false");
523 }
524
525 static gboolean 
526 int_value_parse (GtkCssParser *parser,
527                  GFile        *base,
528                  GValue       *value)
529 {
530   gint i;
531
532   if (_gtk_css_parser_begins_with (parser, '-'))
533     {
534       int res = _gtk_win32_theme_int_parse (parser, base, &i);
535       if (res >= 0)
536         {
537           g_value_set_int (value, i);
538           return res > 0;
539         }
540       /* < 0 => continue */
541     }
542
543   if (!_gtk_css_parser_try_int (parser, &i))
544     {
545       _gtk_css_parser_error (parser, "Expected a valid integer value");
546       return FALSE;
547     }
548
549   g_value_set_int (value, i);
550   return TRUE;
551 }
552
553 static void
554 int_value_print (const GValue *value,
555                  GString      *string)
556 {
557   g_string_append_printf (string, "%d", g_value_get_int (value));
558 }
559
560 static gboolean 
561 uint_value_parse (GtkCssParser *parser,
562                   GFile        *base,
563                   GValue       *value)
564 {
565   guint u;
566
567   if (!_gtk_css_parser_try_uint (parser, &u))
568     {
569       _gtk_css_parser_error (parser, "Expected a valid unsigned value");
570       return FALSE;
571     }
572
573   g_value_set_uint (value, u);
574   return TRUE;
575 }
576
577 static void
578 uint_value_print (const GValue *value,
579                   GString      *string)
580 {
581   g_string_append_printf (string, "%u", g_value_get_uint (value));
582 }
583
584 static gboolean 
585 double_value_parse (GtkCssParser *parser,
586                     GFile        *base,
587                     GValue       *value)
588 {
589   gdouble d;
590
591   if (!_gtk_css_parser_try_double (parser, &d))
592     {
593       _gtk_css_parser_error (parser, "Expected a number");
594       return FALSE;
595     }
596
597   g_value_set_double (value, d);
598   return TRUE;
599 }
600
601 static void
602 double_value_print (const GValue *value,
603                     GString      *string)
604 {
605   string_append_double (string, g_value_get_double (value));
606 }
607
608 static gboolean 
609 float_value_parse (GtkCssParser *parser,
610                    GFile        *base,
611                    GValue       *value)
612 {
613   gdouble d;
614
615   if (!_gtk_css_parser_try_double (parser, &d))
616     {
617       _gtk_css_parser_error (parser, "Expected a number");
618       return FALSE;
619     }
620
621   g_value_set_float (value, d);
622   return TRUE;
623 }
624
625 static void
626 float_value_print (const GValue *value,
627                    GString      *string)
628 {
629   string_append_double (string, g_value_get_float (value));
630 }
631
632 static gboolean 
633 string_value_parse (GtkCssParser *parser,
634                     GFile        *base,
635                     GValue       *value)
636 {
637   char *str = _gtk_css_parser_read_string (parser);
638
639   if (str == NULL)
640     return FALSE;
641
642   g_value_take_string (value, str);
643   return TRUE;
644 }
645
646 static void
647 string_value_print (const GValue *value,
648                     GString      *str)
649 {
650   string_append_string (str, g_value_get_string (value));
651 }
652
653 static gboolean 
654 theming_engine_value_parse (GtkCssParser *parser,
655                             GFile        *base,
656                             GValue       *value)
657 {
658   GtkThemingEngine *engine;
659   char *str;
660
661   if (_gtk_css_parser_try (parser, "none", TRUE))
662     {
663       g_value_set_object (value, gtk_theming_engine_load (NULL));
664       return TRUE;
665     }
666
667   str = _gtk_css_parser_try_ident (parser, TRUE);
668   if (str == NULL)
669     {
670       _gtk_css_parser_error (parser, "Expected a valid theme name");
671       return FALSE;
672     }
673
674   engine = gtk_theming_engine_load (str);
675
676   if (engine == NULL)
677     {
678       _gtk_css_parser_error (parser, "Themeing engine '%s' not found", str);
679       g_free (str);
680       return FALSE;
681     }
682
683   g_value_set_object (value, engine);
684   g_free (str);
685   return TRUE;
686 }
687
688 static void
689 theming_engine_value_print (const GValue *value,
690                             GString      *string)
691 {
692   GtkThemingEngine *engine;
693   char *name;
694
695   engine = g_value_get_object (value);
696   if (engine == NULL)
697     g_string_append (string, "none");
698   else
699     {
700       /* XXX: gtk_theming_engine_get_name()? */
701       g_object_get (engine, "name", &name, NULL);
702       g_string_append (string, name ? name : "none");
703       g_free (name);
704     }
705 }
706
707 static gboolean 
708 animation_description_value_parse (GtkCssParser *parser,
709                                    GFile        *base,
710                                    GValue       *value)
711 {
712   GtkAnimationDescription *desc;
713   char *str;
714
715   str = _gtk_css_parser_read_value (parser);
716   if (str == NULL)
717     return FALSE;
718
719   desc = _gtk_animation_description_from_string (str);
720   g_free (str);
721
722   if (desc == NULL)
723     {
724       _gtk_css_parser_error (parser, "Invalid animation description");
725       return FALSE;
726     }
727   
728   g_value_take_boxed (value, desc);
729   return TRUE;
730 }
731
732 static void
733 animation_description_value_print (const GValue *value,
734                                    GString      *string)
735 {
736   GtkAnimationDescription *desc = g_value_get_boxed (value);
737
738   if (desc == NULL)
739     g_string_append (string, "none");
740   else
741     _gtk_animation_description_print (desc, string);
742 }
743
744 static gboolean 
745 border_value_parse (GtkCssParser *parser,
746                     GFile        *base,
747                     GValue       *value)
748 {
749   GtkBorder border = { 0, };
750   guint i, numbers[4];
751
752   for (i = 0; i < G_N_ELEMENTS (numbers); i++)
753     {
754       if (_gtk_css_parser_begins_with (parser, '-'))
755         {
756           /* These are strictly speaking signed, but we want to be able to use them
757              for unsigned types too, as the actual ranges of values make this safe */
758           int res = _gtk_win32_theme_int_parse (parser, base, (int *)&numbers[i]);
759
760           if (res == 0) /* Parse error, report */
761             return FALSE;
762
763           if (res < 0) /* Nothing known to expand */
764             break;
765         }
766       else
767         {
768           if (!_gtk_css_parser_try_uint (parser, &numbers[i]))
769             break;
770
771           /* XXX: shouldn't allow spaces here? */
772           _gtk_css_parser_try (parser, "px", TRUE);
773         }
774     }
775
776   if (i == 0)
777     {
778       _gtk_css_parser_error (parser, "Expected valid border");
779       return FALSE;
780     }
781
782   border.top = numbers[0];
783   if (i > 1)
784     border.right = numbers[1];
785   else
786     border.right = border.top;
787   if (i > 2)
788     border.bottom = numbers[2];
789   else
790     border.bottom = border.top;
791   if (i > 3)
792     border.left = numbers[3];
793   else
794     border.left = border.right;
795
796   g_value_set_boxed (value, &border);
797   return TRUE;
798 }
799
800 static void
801 border_value_print (const GValue *value, GString *string)
802 {
803   const GtkBorder *border = g_value_get_boxed (value);
804
805   if (border == NULL)
806     g_string_append (string, "none");
807   else if (border->left != border->right)
808     g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
809   else if (border->top != border->bottom)
810     g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
811   else if (border->top != border->left)
812     g_string_append_printf (string, "%d %d", border->top, border->right);
813   else
814     g_string_append_printf (string, "%d", border->top);
815 }
816
817 static gboolean 
818 gradient_value_parse (GtkCssParser *parser,
819                       GFile        *base,
820                       GValue       *value)
821 {
822   GtkGradient *gradient;
823   cairo_pattern_type_t type;
824   gdouble coords[6];
825   guint i;
826
827   if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
828     {
829       _gtk_css_parser_error (parser,
830                              "Expected '-gtk-gradient'");
831       return FALSE;
832     }
833
834   if (!_gtk_css_parser_try (parser, "(", TRUE))
835     {
836       _gtk_css_parser_error (parser,
837                              "Expected '(' after '-gtk-gradient'");
838       return FALSE;
839     }
840
841   /* Parse gradient type */
842   if (_gtk_css_parser_try (parser, "linear", TRUE))
843     type = CAIRO_PATTERN_TYPE_LINEAR;
844   else if (_gtk_css_parser_try (parser, "radial", TRUE))
845     type = CAIRO_PATTERN_TYPE_RADIAL;
846   else
847     {
848       _gtk_css_parser_error (parser,
849                              "Gradient type must be 'radial' or 'linear'");
850       return FALSE;
851     }
852
853   /* Parse start/stop position parameters */
854   for (i = 0; i < 2; i++)
855     {
856       if (! _gtk_css_parser_try (parser, ",", TRUE))
857         {
858           _gtk_css_parser_error (parser,
859                                  "Expected ','");
860           return FALSE;
861         }
862
863       if (_gtk_css_parser_try (parser, "left", TRUE))
864         coords[i * 3] = 0;
865       else if (_gtk_css_parser_try (parser, "right", TRUE))
866         coords[i * 3] = 1;
867       else if (_gtk_css_parser_try (parser, "center", TRUE))
868         coords[i * 3] = 0.5;
869       else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
870         {
871           _gtk_css_parser_error (parser,
872                                  "Expected a valid X coordinate");
873           return FALSE;
874         }
875
876       if (_gtk_css_parser_try (parser, "top", TRUE))
877         coords[i * 3 + 1] = 0;
878       else if (_gtk_css_parser_try (parser, "bottom", TRUE))
879         coords[i * 3 + 1] = 1;
880       else if (_gtk_css_parser_try (parser, "center", TRUE))
881         coords[i * 3 + 1] = 0.5;
882       else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
883         {
884           _gtk_css_parser_error (parser,
885                                  "Expected a valid Y coordinate");
886           return FALSE;
887         }
888
889       if (type == CAIRO_PATTERN_TYPE_RADIAL)
890         {
891           /* Parse radius */
892           if (! _gtk_css_parser_try (parser, ",", TRUE))
893             {
894               _gtk_css_parser_error (parser,
895                                      "Expected ','");
896               return FALSE;
897             }
898
899           if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
900             {
901               _gtk_css_parser_error (parser,
902                                      "Expected a numer for the radius");
903               return FALSE;
904             }
905         }
906     }
907
908   if (type == CAIRO_PATTERN_TYPE_LINEAR)
909     gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
910   else
911     gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
912                                         coords[3], coords[4], coords[5]);
913
914   while (_gtk_css_parser_try (parser, ",", TRUE))
915     {
916       GtkSymbolicColor *color;
917       gdouble position;
918
919       if (_gtk_css_parser_try (parser, "from", TRUE))
920         {
921           position = 0;
922
923           if (!_gtk_css_parser_try (parser, "(", TRUE))
924             {
925               gtk_gradient_unref (gradient);
926               _gtk_css_parser_error (parser,
927                                      "Expected '('");
928               return FALSE;
929             }
930
931         }
932       else if (_gtk_css_parser_try (parser, "to", TRUE))
933         {
934           position = 1;
935
936           if (!_gtk_css_parser_try (parser, "(", TRUE))
937             {
938               gtk_gradient_unref (gradient);
939               _gtk_css_parser_error (parser,
940                                      "Expected '('");
941               return FALSE;
942             }
943
944         }
945       else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
946         {
947           if (!_gtk_css_parser_try (parser, "(", TRUE))
948             {
949               gtk_gradient_unref (gradient);
950               _gtk_css_parser_error (parser,
951                                      "Expected '('");
952               return FALSE;
953             }
954
955           if (!_gtk_css_parser_try_double (parser, &position))
956             {
957               gtk_gradient_unref (gradient);
958               _gtk_css_parser_error (parser,
959                                      "Expected a valid number");
960               return FALSE;
961             }
962
963           if (!_gtk_css_parser_try (parser, ",", TRUE))
964             {
965               gtk_gradient_unref (gradient);
966               _gtk_css_parser_error (parser,
967                                      "Expected a comma");
968               return FALSE;
969             }
970         }
971       else
972         {
973           gtk_gradient_unref (gradient);
974           _gtk_css_parser_error (parser,
975                                  "Not a valid color-stop definition");
976           return FALSE;
977         }
978
979       color = _gtk_css_parser_read_symbolic_color (parser);
980       if (color == NULL)
981         {
982           gtk_gradient_unref (gradient);
983           return FALSE;
984         }
985
986       gtk_gradient_add_color_stop (gradient, position, color);
987       gtk_symbolic_color_unref (color);
988
989       if (!_gtk_css_parser_try (parser, ")", TRUE))
990         {
991           gtk_gradient_unref (gradient);
992           _gtk_css_parser_error (parser,
993                                  "Expected ')'");
994           return FALSE;
995         }
996     }
997
998   if (!_gtk_css_parser_try (parser, ")", TRUE))
999     {
1000       gtk_gradient_unref (gradient);
1001       _gtk_css_parser_error (parser,
1002                              "Expected ')'");
1003       return FALSE;
1004     }
1005
1006   g_value_take_boxed (value, gradient);
1007   return TRUE;
1008 }
1009
1010 static void
1011 gradient_value_print (const GValue *value,
1012                       GString      *string)
1013 {
1014   GtkGradient *gradient = g_value_get_boxed (value);
1015
1016   if (gradient == NULL)
1017     g_string_append (string, "none");
1018   else
1019     {
1020       char *s = gtk_gradient_to_string (gradient);
1021       g_string_append (string, s);
1022       g_free (s);
1023     }
1024 }
1025
1026 static GFile *
1027 gtk_css_parse_url (GtkCssParser *parser,
1028                    GFile        *base)
1029 {
1030   gchar *path;
1031   GFile *file;
1032
1033   if (_gtk_css_parser_try (parser, "url", FALSE))
1034     {
1035       if (!_gtk_css_parser_try (parser, "(", TRUE))
1036         {
1037           _gtk_css_parser_skip_whitespace (parser);
1038           if (_gtk_css_parser_try (parser, "(", TRUE))
1039             {
1040               GError *error;
1041               
1042               error = g_error_new_literal (GTK_CSS_PROVIDER_ERROR,
1043                                            GTK_CSS_PROVIDER_ERROR_DEPRECATED,
1044                                            "Whitespace between 'url' and '(' is deprecated");
1045                              
1046               _gtk_css_parser_take_error (parser, error);
1047             }
1048           else
1049             {
1050               _gtk_css_parser_error (parser, "Expected '(' after 'url'");
1051               return NULL;
1052             }
1053         }
1054
1055       path = _gtk_css_parser_read_string (parser);
1056       if (path == NULL)
1057         return NULL;
1058
1059       if (!_gtk_css_parser_try (parser, ")", TRUE))
1060         {
1061           _gtk_css_parser_error (parser, "No closing ')' found for 'url'");
1062           g_free (path);
1063           return NULL;
1064         }
1065     }
1066   else
1067     {
1068       path = _gtk_css_parser_try_name (parser, TRUE);
1069       if (path == NULL)
1070         {
1071           _gtk_css_parser_error (parser, "Not a valid url");
1072           return NULL;
1073         }
1074     }
1075
1076   file = g_file_resolve_relative_path (base, path);
1077   g_free (path);
1078
1079   return file;
1080 }
1081
1082 static gboolean 
1083 pattern_value_parse (GtkCssParser *parser,
1084                      GFile        *base,
1085                      GValue       *value)
1086 {
1087   if (_gtk_css_parser_try (parser, "none", TRUE))
1088     {
1089       /* nothing to do here */
1090     }
1091   else if (_gtk_css_parser_begins_with (parser, '-'))
1092     {
1093       int res;
1094       res = _gtk_win32_theme_part_parse (parser, base, value);
1095       if (res >= 0)
1096         return res > 0;
1097       /* < 0 => continue */
1098       g_value_unset (value);
1099       g_value_init (value, GTK_TYPE_GRADIENT);
1100       return gradient_value_parse (parser, base, value);
1101     }
1102   else
1103     {
1104       GError *error = NULL;
1105       gchar *path;
1106       GdkPixbuf *pixbuf;
1107       GFile *file;
1108       cairo_surface_t *surface;
1109       cairo_pattern_t *pattern;
1110       cairo_t *cr;
1111       cairo_matrix_t matrix;
1112
1113       file = gtk_css_parse_url (parser, base);
1114       if (file == NULL)
1115         return FALSE;
1116
1117       path = g_file_get_path (file);
1118       g_object_unref (file);
1119
1120       pixbuf = gdk_pixbuf_new_from_file (path, &error);
1121       g_free (path);
1122       if (pixbuf == NULL)
1123         {
1124           _gtk_css_parser_take_error (parser, error);
1125           return FALSE;
1126         }
1127
1128       surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1129                                             gdk_pixbuf_get_width (pixbuf),
1130                                             gdk_pixbuf_get_height (pixbuf));
1131       cr = cairo_create (surface);
1132       gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
1133       cairo_paint (cr);
1134       pattern = cairo_pattern_create_for_surface (surface);
1135
1136       cairo_matrix_init_scale (&matrix,
1137                                gdk_pixbuf_get_width (pixbuf),
1138                                gdk_pixbuf_get_height (pixbuf));
1139       cairo_pattern_set_matrix (pattern, &matrix);
1140
1141       cairo_surface_destroy (surface);
1142       cairo_destroy (cr);
1143       g_object_unref (pixbuf);
1144
1145       g_value_take_boxed (value, pattern);
1146     }
1147   
1148   return TRUE;
1149 }
1150
1151 static cairo_status_t
1152 surface_write (void                *closure,
1153                const unsigned char *data,
1154                unsigned int         length)
1155 {
1156   g_byte_array_append (closure, data, length);
1157
1158   return CAIRO_STATUS_SUCCESS;
1159 }
1160
1161 static void
1162 surface_print (cairo_surface_t *surface,
1163                GString *        string)
1164 {
1165 #if CAIRO_HAS_PNG_FUNCTIONS
1166   GByteArray *array;
1167   char *base64;
1168   
1169   array = g_byte_array_new ();
1170   cairo_surface_write_to_png_stream (surface, surface_write, array);
1171   base64 = g_base64_encode (array->data, array->len);
1172   g_byte_array_free (array, TRUE);
1173
1174   g_string_append (string, "url(\"data:image/png;base64,");
1175   g_string_append (string, base64);
1176   g_string_append (string, "\")");
1177
1178   g_free (base64);
1179 #else
1180   g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
1181 #endif
1182 }
1183
1184 static void
1185 pattern_value_print (const GValue *value,
1186                      GString      *string)
1187 {
1188   cairo_pattern_t *pattern;
1189   cairo_surface_t *surface;
1190
1191   pattern = g_value_get_boxed (value);
1192
1193   if (pattern == NULL)
1194     {
1195       g_string_append (string, "none");
1196       return;
1197     }
1198
1199   switch (cairo_pattern_get_type (pattern))
1200     {
1201     case CAIRO_PATTERN_TYPE_SURFACE:
1202       if (cairo_pattern_get_surface (pattern, &surface) != CAIRO_STATUS_SUCCESS)
1203         {
1204           g_assert_not_reached ();
1205         }
1206       surface_print (surface, string);
1207       break;
1208     case CAIRO_PATTERN_TYPE_SOLID:
1209     case CAIRO_PATTERN_TYPE_LINEAR:
1210     case CAIRO_PATTERN_TYPE_RADIAL:
1211     default:
1212       g_assert_not_reached ();
1213       break;
1214     }
1215 }
1216
1217 static gboolean
1218 shadow_value_parse (GtkCssParser *parser,
1219                     GFile *base,
1220                     GValue *value)
1221 {
1222   gboolean have_inset, have_color, have_lengths;
1223   gdouble hoffset, voffset, blur, spread;
1224   GtkSymbolicColor *color;
1225   GtkShadow *shadow;
1226   guint i;
1227
1228   shadow = _gtk_shadow_new ();
1229
1230   if (_gtk_css_parser_try (parser, "none", TRUE))
1231     return TRUE;
1232
1233   do
1234     {
1235       have_inset = have_lengths = have_color = FALSE;
1236
1237       for (i = 0; i < 3; i++)
1238         {
1239           if (!have_inset && 
1240               _gtk_css_parser_try (parser, "inset", TRUE))
1241             {
1242               have_inset = TRUE;
1243               continue;
1244             }
1245             
1246           if (!have_lengths &&
1247               _gtk_css_parser_try_double (parser, &hoffset))
1248             {
1249               have_lengths = TRUE;
1250
1251               if (!_gtk_css_parser_try_double (parser, &voffset))
1252                 {
1253                   _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
1254                   _gtk_shadow_unref (shadow);
1255                   return FALSE;
1256                 }
1257
1258               if (!_gtk_css_parser_try_double (parser, &blur))
1259                 blur = 0;
1260
1261               if (!_gtk_css_parser_try_double (parser, &spread))
1262                 spread = 0;
1263
1264               continue;
1265             }
1266
1267           if (!have_color)
1268             {
1269               have_color = TRUE;
1270
1271               /* XXX: the color is optional and UA-defined if it's missing,
1272                * but it doesn't really make sense for us...
1273                */
1274               color = _gtk_css_parser_read_symbolic_color (parser);
1275
1276               if (color == NULL)
1277                 {
1278                   _gtk_shadow_unref (shadow);
1279                   return FALSE;
1280                 }
1281             }
1282         }
1283
1284       if (!have_color || !have_lengths)
1285         {
1286           _gtk_css_parser_error (parser, "Must specify at least color and offsets");
1287           _gtk_shadow_unref (shadow);
1288           return FALSE;
1289         }
1290
1291       _gtk_shadow_append (shadow,
1292                           hoffset, voffset,
1293                           blur, spread,
1294                           have_inset, color);
1295
1296       gtk_symbolic_color_unref (color);
1297
1298     }
1299   while (_gtk_css_parser_try (parser, ",", TRUE));
1300
1301   g_value_take_boxed (value, shadow);
1302   return TRUE;
1303 }
1304
1305 static void
1306 shadow_value_print (const GValue *value,
1307                     GString      *string)
1308 {
1309   GtkShadow *shadow;
1310
1311   shadow = g_value_get_boxed (value);
1312
1313   if (shadow == NULL)
1314     g_string_append (string, "none");
1315   else
1316     _gtk_shadow_print (shadow, string);
1317 }
1318
1319 static gboolean
1320 background_repeat_value_parse (GtkCssParser *parser,
1321                                GFile *file,
1322                                GValue *value)
1323 {
1324   GtkCssBackgroundRepeat repeat;
1325   int style;
1326
1327   if (!enum_parse (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, &style))
1328     return FALSE;
1329
1330   repeat.repeat = style;
1331
1332   g_value_set_boxed (value, &repeat);
1333
1334   return TRUE;
1335 }
1336
1337 static void
1338 background_repeat_value_print (const GValue *value,
1339                                GString      *string)
1340 {
1341   GtkCssBackgroundRepeat *repeat;
1342
1343   repeat = g_value_get_boxed (value);
1344
1345   enum_print (repeat->repeat, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, string);
1346 }
1347
1348 static gboolean
1349 border_image_repeat_value_parse (GtkCssParser *parser,
1350                                  GFile *file,
1351                                  GValue *value)
1352 {
1353   GtkCssBorderImageRepeat image_repeat;
1354   GtkCssBorderRepeatStyle styles[2];
1355   gint i, v;
1356
1357   for (i = 0; i < 2; i++)
1358     {
1359       if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, &v))
1360         styles[i] = v;
1361       else if (i == 0)
1362         {
1363           styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_STRETCH;
1364           break;
1365         }
1366       else
1367         styles[i] = styles[0];
1368     }
1369
1370   image_repeat.hrepeat = styles[0];
1371   image_repeat.vrepeat = styles[1];
1372
1373   g_value_set_boxed (value, &image_repeat);
1374
1375   return TRUE;
1376 }
1377
1378 static void
1379 border_image_repeat_value_print (const GValue *value,
1380                                  GString      *string)
1381 {
1382   GtkCssBorderImageRepeat *image_repeat;
1383
1384   image_repeat = g_value_get_boxed (value);
1385
1386   enum_print (image_repeat->hrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1387   if (image_repeat->hrepeat != image_repeat->vrepeat)
1388     {
1389       g_string_append (string, " ");
1390       enum_print (image_repeat->vrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1391     }
1392 }
1393
1394 static gboolean 
1395 enum_value_parse (GtkCssParser *parser,
1396                   GFile        *base,
1397                   GValue       *value)
1398 {
1399   int v;
1400
1401   if (enum_parse (parser, G_VALUE_TYPE (value), &v))
1402     {
1403       g_value_set_enum (value, v);
1404       return TRUE;
1405     }
1406
1407   return FALSE;
1408 }
1409
1410 static void
1411 enum_value_print (const GValue *value,
1412                   GString      *string)
1413 {
1414   enum_print (g_value_get_enum (value), G_VALUE_TYPE (value), string);
1415 }
1416
1417 static gboolean 
1418 flags_value_parse (GtkCssParser *parser,
1419                    GFile        *base,
1420                    GValue       *value)
1421 {
1422   GFlagsClass *flags_class;
1423   GFlagsValue *flag_value;
1424   guint flags = 0;
1425   char *str;
1426
1427   flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1428
1429   do {
1430     str = _gtk_css_parser_try_ident (parser, TRUE);
1431     if (str == NULL)
1432       {
1433         _gtk_css_parser_error (parser, "Expected an identifier");
1434         g_type_class_unref (flags_class);
1435         return FALSE;
1436       }
1437
1438       flag_value = g_flags_get_value_by_nick (flags_class, str);
1439       if (!flag_value)
1440         {
1441           _gtk_css_parser_error (parser,
1442                                  "Unknown flag value '%s' for type '%s'",
1443                                  str, g_type_name (G_VALUE_TYPE (value)));
1444           /* XXX Do we want to return FALSE here? We can get
1445            * forward-compatibility for new values this way
1446            */
1447           g_free (str);
1448           g_type_class_unref (flags_class);
1449           return FALSE;
1450         }
1451
1452       g_free (str);
1453     }
1454   while (_gtk_css_parser_try (parser, ",", FALSE));
1455
1456   g_type_class_unref (flags_class);
1457
1458   g_value_set_enum (value, flags);
1459
1460   return TRUE;
1461 }
1462
1463 static void
1464 flags_value_print (const GValue *value,
1465                    GString      *string)
1466 {
1467   GFlagsClass *flags_class;
1468   guint i, flags;
1469
1470   flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1471   flags = g_value_get_flags (value);
1472
1473   for (i = 0; i < flags_class->n_values; i++)
1474     {
1475       GFlagsValue *flags_value = &flags_class->values[i];
1476
1477       if (flags & flags_value->value)
1478         {
1479           if (string->len != 0)
1480             g_string_append (string, ", ");
1481
1482           g_string_append (string, flags_value->value_nick);
1483         }
1484     }
1485
1486   g_type_class_unref (flags_class);
1487 }
1488
1489 static gboolean 
1490 bindings_value_parse (GtkCssParser *parser,
1491                       GFile        *base,
1492                       GValue       *value)
1493 {
1494   GPtrArray *array;
1495   GtkBindingSet *binding_set;
1496   char *name;
1497
1498   array = g_ptr_array_new ();
1499
1500   do {
1501       name = _gtk_css_parser_try_ident (parser, TRUE);
1502       if (name == NULL)
1503         {
1504           _gtk_css_parser_error (parser, "Not a valid binding name");
1505           g_ptr_array_free (array, TRUE);
1506           return FALSE;
1507         }
1508
1509       binding_set = gtk_binding_set_find (name);
1510
1511       if (!binding_set)
1512         {
1513           _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1514           g_free (name);
1515           continue;
1516         }
1517
1518       g_ptr_array_add (array, binding_set);
1519       g_free (name);
1520     }
1521   while (_gtk_css_parser_try (parser, ",", TRUE));
1522
1523   g_value_take_boxed (value, array);
1524
1525   return TRUE;
1526 }
1527
1528 static void
1529 bindings_value_print (const GValue *value,
1530                       GString      *string)
1531 {
1532   GPtrArray *array;
1533   guint i;
1534
1535   array = g_value_get_boxed (value);
1536
1537   for (i = 0; i < array->len; i++)
1538     {
1539       GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1540
1541       if (i > 0)
1542         g_string_append (string, ", ");
1543       g_string_append (string, binding_set->set_name);
1544     }
1545 }
1546
1547 static gboolean 
1548 border_corner_radius_value_parse (GtkCssParser *parser,
1549                                   GFile        *base,
1550                                   GValue       *value)
1551 {
1552   GtkCssBorderCornerRadius corner;
1553
1554   if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
1555     {
1556       _gtk_css_parser_error (parser, "Expected a number");
1557       return FALSE;
1558     }
1559   else if (corner.horizontal < 0)
1560     goto negative;
1561
1562   if (!_gtk_css_parser_try_double (parser, &corner.vertical))
1563     corner.vertical = corner.horizontal;
1564   else if (corner.vertical < 0)
1565     goto negative;
1566
1567   g_value_set_boxed (value, &corner);
1568   return TRUE;
1569
1570 negative:
1571   _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1572   return FALSE;
1573 }
1574
1575 static void
1576 border_corner_radius_value_print (const GValue *value,
1577                                   GString      *string)
1578 {
1579   GtkCssBorderCornerRadius *corner;
1580
1581   corner = g_value_get_boxed (value);
1582
1583   if (corner == NULL)
1584     {
1585       g_string_append (string, "none");
1586       return;
1587     }
1588
1589   string_append_double (string, corner->horizontal);
1590   if (corner->horizontal != corner->vertical)
1591     {
1592       g_string_append_c (string, ' ');
1593       string_append_double (string, corner->vertical);
1594     }
1595 }
1596
1597 static gboolean 
1598 transparent_color_value_parse (GtkCssParser *parser,
1599                                GFile        *base,
1600                                GValue       *value)
1601 {
1602   if (_gtk_css_parser_try (parser, "transparent", TRUE))
1603     {
1604       GdkRGBA transparent = { 0, 0, 0, 0 };
1605           
1606       g_value_set_boxed (value, &transparent);
1607
1608       return TRUE;
1609     }
1610
1611   return rgba_value_parse (parser, base, value);
1612 }
1613
1614 /*** API ***/
1615
1616 guint
1617 _gtk_style_property_get_count (void)
1618 {
1619   return __style_property_array ? __style_property_array->len : 0;
1620 }
1621
1622 GtkStyleProperty *
1623 _gtk_style_property_get (guint id)
1624 {
1625   g_assert (__style_property_array);
1626   
1627   return g_ptr_array_index (__style_property_array, id);
1628 }
1629
1630 static void
1631 _gtk_style_property_generate_id (GtkStyleProperty *node)
1632 {
1633   if (__style_property_array == NULL)
1634     __style_property_array = g_ptr_array_new ();
1635
1636   node->id = __style_property_array->len;
1637   g_ptr_array_add (__style_property_array, node);
1638 }
1639
1640 static void
1641 css_string_funcs_init (void)
1642 {
1643   if (G_LIKELY (parse_funcs != NULL))
1644     return;
1645
1646   parse_funcs = g_hash_table_new (NULL, NULL);
1647   print_funcs = g_hash_table_new (NULL, NULL);
1648
1649   register_conversion_function (GDK_TYPE_RGBA,
1650                                 rgba_value_parse,
1651                                 rgba_value_print);
1652   register_conversion_function (GDK_TYPE_COLOR,
1653                                 color_value_parse,
1654                                 color_value_print);
1655   register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
1656                                 symbolic_color_value_parse,
1657                                 symbolic_color_value_print);
1658   register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
1659                                 font_description_value_parse,
1660                                 font_description_value_print);
1661   register_conversion_function (G_TYPE_BOOLEAN,
1662                                 boolean_value_parse,
1663                                 boolean_value_print);
1664   register_conversion_function (G_TYPE_INT,
1665                                 int_value_parse,
1666                                 int_value_print);
1667   register_conversion_function (G_TYPE_UINT,
1668                                 uint_value_parse,
1669                                 uint_value_print);
1670   register_conversion_function (G_TYPE_DOUBLE,
1671                                 double_value_parse,
1672                                 double_value_print);
1673   register_conversion_function (G_TYPE_FLOAT,
1674                                 float_value_parse,
1675                                 float_value_print);
1676   register_conversion_function (G_TYPE_STRING,
1677                                 string_value_parse,
1678                                 string_value_print);
1679   register_conversion_function (GTK_TYPE_THEMING_ENGINE,
1680                                 theming_engine_value_parse,
1681                                 theming_engine_value_print);
1682   register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
1683                                 animation_description_value_parse,
1684                                 animation_description_value_print);
1685   register_conversion_function (GTK_TYPE_BORDER,
1686                                 border_value_parse,
1687                                 border_value_print);
1688   register_conversion_function (GTK_TYPE_GRADIENT,
1689                                 gradient_value_parse,
1690                                 gradient_value_print);
1691   register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
1692                                 pattern_value_parse,
1693                                 pattern_value_print);
1694   register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
1695                                 border_image_repeat_value_parse,
1696                                 border_image_repeat_value_print);
1697   register_conversion_function (GTK_TYPE_SHADOW,
1698                                 shadow_value_parse,
1699                                 shadow_value_print);
1700   register_conversion_function (G_TYPE_ENUM,
1701                                 enum_value_parse,
1702                                 enum_value_print);
1703   register_conversion_function (G_TYPE_FLAGS,
1704                                 flags_value_parse,
1705                                 flags_value_print);
1706   register_conversion_function (GTK_TYPE_CSS_BACKGROUND_REPEAT,
1707                                 background_repeat_value_parse,
1708                                 background_repeat_value_print);
1709 }
1710
1711 gboolean
1712 _gtk_style_property_parse_value (GtkStyleProperty *property,
1713                                  GValue           *value,
1714                                  GtkCssParser     *parser,
1715                                  GFile            *base)
1716 {
1717   GtkStyleParseFunc func;
1718
1719   g_return_val_if_fail (value != NULL, FALSE);
1720   g_return_val_if_fail (parser != NULL, FALSE);
1721
1722   css_string_funcs_init ();
1723
1724   if (property)
1725     {
1726       if (_gtk_css_parser_try (parser, "initial", TRUE))
1727         {
1728           /* the initial value can be explicitly specified with the
1729            * â€˜initial’ keyword which all properties accept.
1730            */
1731           g_value_unset (value);
1732           g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
1733           g_value_set_enum (value, GTK_CSS_INITIAL);
1734           return TRUE;
1735         }
1736       else if (_gtk_css_parser_try (parser, "inherit", TRUE))
1737         {
1738           /* All properties accept the â€˜inherit’ value which
1739            * explicitly specifies that the value will be determined
1740            * by inheritance. The â€˜inherit’ value can be used to
1741            * strengthen inherited values in the cascade, and it can
1742            * also be used on properties that are not normally inherited.
1743            */
1744           g_value_unset (value);
1745           g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
1746           g_value_set_enum (value, GTK_CSS_INHERIT);
1747           return TRUE;
1748         }
1749       else if (property->property_parse_func)
1750         {
1751           GError *error = NULL;
1752           char *value_str;
1753           gboolean success;
1754           
1755           value_str = _gtk_css_parser_read_value (parser);
1756           if (value_str == NULL)
1757             return FALSE;
1758           
1759           success = (*property->property_parse_func) (value_str, value, &error);
1760
1761           g_free (value_str);
1762
1763           return success;
1764         }
1765
1766       func = property->parse_func;
1767     }
1768   else
1769     func = NULL;
1770
1771   if (func == NULL)
1772     func = g_hash_table_lookup (parse_funcs,
1773                                 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
1774   if (func == NULL)
1775     func = g_hash_table_lookup (parse_funcs,
1776                                 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
1777
1778   if (func == NULL)
1779     {
1780       _gtk_css_parser_error (parser,
1781                              "Cannot convert to type '%s'",
1782                              g_type_name (G_VALUE_TYPE (value)));
1783       return FALSE;
1784     }
1785
1786   return (*func) (parser, base, value);
1787 }
1788
1789 void
1790 _gtk_style_property_print_value (GtkStyleProperty *property,
1791                                  const GValue     *value,
1792                                  GString          *string)
1793 {
1794   GtkStylePrintFunc func;
1795
1796   css_string_funcs_init ();
1797
1798   if (G_VALUE_HOLDS (value, GTK_TYPE_CSS_SPECIAL_VALUE))
1799     func = enum_value_print;
1800   else if (property)
1801     func = property->print_func;
1802   else
1803     func = NULL;
1804
1805   if (func == NULL)
1806     func = g_hash_table_lookup (print_funcs,
1807                                 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
1808   if (func == NULL)
1809     func = g_hash_table_lookup (print_funcs,
1810                                 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
1811
1812   if (func == NULL)
1813     {
1814       char *s = g_strdup_value_contents (value);
1815       g_string_append (string, s);
1816       g_free (s);
1817       return;
1818     }
1819   
1820   func (value, string);
1821 }
1822
1823 static void
1824 _gtk_style_property_default_value (GtkStyleProperty   *property,
1825                                    GtkStyleProperties *properties,
1826                                    GtkStateFlags       state,
1827                                    GValue             *value)
1828 {
1829   g_value_copy (&property->initial_value, value);
1830 }
1831
1832 gboolean
1833 _gtk_style_property_is_inherit (GtkStyleProperty *property)
1834 {
1835   g_return_val_if_fail (property != NULL, FALSE);
1836
1837   return property->flags & GTK_STYLE_PROPERTY_INHERIT ? TRUE : FALSE;
1838 }
1839
1840 guint
1841 _gtk_style_property_get_id (GtkStyleProperty *property)
1842 {
1843   g_return_val_if_fail (property != NULL, FALSE);
1844
1845   return property->id;
1846 }
1847
1848 static gboolean
1849 resolve_color (GtkStyleProperties *props,
1850                GValue             *value)
1851 {
1852   GdkRGBA color;
1853
1854   /* Resolve symbolic color to GdkRGBA */
1855   if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
1856     return FALSE;
1857
1858   /* Store it back, this is where GdkRGBA caching happens */
1859   g_value_unset (value);
1860   g_value_init (value, GDK_TYPE_RGBA);
1861   g_value_set_boxed (value, &color);
1862
1863   return TRUE;
1864 }
1865
1866 static gboolean
1867 resolve_color_rgb (GtkStyleProperties *props,
1868                    GValue             *value)
1869 {
1870   GdkColor color = { 0 };
1871   GdkRGBA rgba;
1872
1873   if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
1874     return FALSE;
1875
1876   color.red = rgba.red * 65535. + 0.5;
1877   color.green = rgba.green * 65535. + 0.5;
1878   color.blue = rgba.blue * 65535. + 0.5;
1879
1880   g_value_unset (value);
1881   g_value_init (value, GDK_TYPE_COLOR);
1882   g_value_set_boxed (value, &color);
1883
1884   return TRUE;
1885 }
1886
1887 static gboolean
1888 resolve_win32_theme_part (GtkStyleProperties *props,
1889                           GValue             *value,
1890                           GValue             *value_out,
1891                           GtkStylePropertyContext *context)
1892 {
1893   GtkWin32ThemePart  *part;
1894   cairo_pattern_t *pattern;
1895
1896   part = g_value_get_boxed (value);
1897   if (part == NULL)
1898     return FALSE;
1899
1900   pattern = _gtk_win32_theme_part_render (part, context->width, context->height);
1901
1902   g_value_take_boxed (value_out, pattern);
1903
1904   return TRUE;
1905 }
1906
1907
1908 static gboolean
1909 resolve_gradient (GtkStyleProperties *props,
1910                   GValue             *value)
1911 {
1912   cairo_pattern_t *gradient;
1913
1914   if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
1915     return FALSE;
1916
1917   /* Store it back, this is where cairo_pattern_t caching happens */
1918   g_value_unset (value);
1919   g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
1920   g_value_take_boxed (value, gradient);
1921
1922   return TRUE;
1923 }
1924
1925 static gboolean
1926 resolve_shadow (GtkStyleProperties *props,
1927                 GValue *value)
1928 {
1929   GtkShadow *resolved, *base;
1930
1931   base = g_value_get_boxed (value);
1932
1933   if (base == NULL)
1934     return TRUE;
1935   
1936   if (_gtk_shadow_get_resolved (base))
1937     return TRUE;
1938
1939   resolved = _gtk_shadow_resolve (base, props);
1940   if (resolved == NULL)
1941     return FALSE;
1942
1943   g_value_take_boxed (value, resolved);
1944
1945   return TRUE;
1946 }
1947
1948 static void
1949 _gtk_style_property_resolve (GtkStyleProperty       *property,
1950                              GtkStyleProperties     *props,
1951                              GtkStateFlags           state,
1952                              GtkStylePropertyContext *context,
1953                              GValue                 *val,
1954                              GValue                 *val_out)
1955 {
1956   if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_SPECIAL_VALUE)
1957     {
1958       GtkCssSpecialValue special = g_value_get_enum (val);
1959
1960       g_value_unset (val);
1961       switch (special)
1962         {
1963         case GTK_CSS_CURRENT_COLOR:
1964           g_assert (property->pspec->value_type == GDK_TYPE_RGBA);
1965           gtk_style_properties_get_property (props, "color", state, val);
1966           break;
1967         case GTK_CSS_INHERIT:
1968         case GTK_CSS_INITIAL:
1969         default:
1970           g_assert_not_reached ();
1971         }
1972     }
1973   else if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
1974     {
1975       if (property->pspec->value_type == GDK_TYPE_RGBA)
1976         {
1977           if (resolve_color (props, val))
1978             goto out;
1979         }
1980       else if (property->pspec->value_type == GDK_TYPE_COLOR)
1981         {
1982           if (resolve_color_rgb (props, val))
1983             goto out;
1984         }
1985       
1986       g_value_unset (val);
1987       g_value_init (val, property->pspec->value_type);
1988       _gtk_style_property_default_value (property, props, state, val);
1989     }
1990   else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
1991     {
1992       if (g_value_get_boxed (val) == NULL)
1993         _gtk_style_property_default_value (property, props, state, val);
1994     }
1995   else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
1996     {
1997       g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
1998
1999       if (!resolve_gradient (props, val))
2000         {
2001           g_value_unset (val);
2002           g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
2003           _gtk_style_property_default_value (property, props, state, val);
2004         }
2005     }
2006   else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
2007     {
2008       if (!resolve_shadow (props, val))
2009         _gtk_style_property_default_value (property, props, state, val);
2010     }
2011   else if (G_VALUE_TYPE (val) == GTK_TYPE_WIN32_THEME_PART)
2012     {
2013       if (resolve_win32_theme_part (props, val, val_out, context))
2014         return; /* Don't copy val, this sets val_out */
2015       _gtk_style_property_default_value (property, props, state, val);
2016     }
2017
2018  out:
2019   g_value_copy (val, val_out);
2020 }
2021
2022 const GValue *
2023 _gtk_style_property_get_initial_value (GtkStyleProperty *property)
2024 {
2025   g_return_val_if_fail (property != NULL, NULL);
2026
2027   return &property->initial_value;
2028 }
2029
2030 GParameter *
2031 _gtk_style_property_unpack (GtkStyleProperty *property,
2032                             const GValue     *value,
2033                             guint            *n_params)
2034 {
2035   g_return_val_if_fail (property != NULL, NULL);
2036   g_return_val_if_fail (property->unpack_func != NULL, NULL);
2037   g_return_val_if_fail (value != NULL, NULL);
2038   g_return_val_if_fail (n_params != NULL, NULL);
2039
2040   return property->unpack_func (value, n_params);
2041 }
2042
2043 static void
2044 _gtk_style_property_pack (GtkStyleProperty   *property,
2045                           GtkStyleProperties *props,
2046                           GtkStateFlags       state,
2047                           GtkStylePropertyContext *context,
2048                           GValue             *value)
2049 {
2050   g_return_if_fail (property != NULL);
2051   g_return_if_fail (property->pack_func != NULL);
2052   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2053   g_return_if_fail (G_IS_VALUE (value));
2054
2055   property->pack_func (value, props, state, context);
2056 }
2057
2058 void
2059 _gtk_style_property_assign (GtkStyleProperty   *property,
2060                             GtkStyleProperties *props,
2061                             GtkStateFlags       state,
2062                             const GValue       *value)
2063 {
2064   g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
2065   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2066   g_return_if_fail (value != NULL);
2067
2068   if (GTK_IS_CSS_SHORTHAND_PROPERTY (property))
2069     {
2070       GParameter *parameters;
2071       guint i, n_parameters;
2072
2073       parameters = _gtk_style_property_unpack (property, value, &n_parameters);
2074
2075       for (i = 0; i < n_parameters; i++)
2076         {
2077           _gtk_style_property_assign (_gtk_style_property_lookup (parameters[i].name),
2078                                       props,
2079                                       state,
2080                                       &parameters[i].value);
2081           g_value_unset (&parameters[i].value);
2082         }
2083       g_free (parameters);
2084       return;
2085     }
2086   else if (GTK_IS_CSS_STYLE_PROPERTY (property))
2087     {
2088       _gtk_style_properties_set_property_by_property (props,
2089                                                       property,
2090                                                       state,
2091                                                       value);
2092     }
2093   else
2094     {
2095       g_assert_not_reached ();
2096     }
2097 }
2098
2099 void
2100 _gtk_style_property_query (GtkStyleProperty        *property,
2101                            GtkStyleProperties      *props,
2102                            GtkStateFlags            state,
2103                            GtkStylePropertyContext *context,
2104                            GValue                  *value)
2105 {
2106   const GValue *val;
2107
2108   g_return_if_fail (property != NULL);
2109   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2110   g_return_if_fail (context != NULL);
2111   g_return_if_fail (value != NULL);
2112
2113   val = _gtk_style_properties_peek_property (props, property, state);
2114   g_value_init (value, property->pspec->value_type);
2115
2116   if (val)
2117     _gtk_style_property_resolve (property, props, state, context, (GValue *) val, value);
2118   else if (GTK_IS_CSS_SHORTHAND_PROPERTY (property))
2119     _gtk_style_property_pack (property, props, state, context, value);
2120   else
2121     _gtk_style_property_default_value (property, props, state, value);
2122 }
2123
2124 #define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
2125   (rgba)->red = (r); \
2126   (rgba)->green = (g); \
2127   (rgba)->blue = (b); \
2128   (rgba)->alpha = (a); \
2129 }G_STMT_END
2130 static void
2131 gtk_style_property_init_properties (void)
2132 {
2133   static gboolean initialized = FALSE;
2134   GValue value = { 0, };
2135   char *default_font_family[] = { "Sans", NULL };
2136   GdkRGBA rgba;
2137
2138   if (G_LIKELY (initialized))
2139     return;
2140
2141   initialized = TRUE;
2142
2143   g_value_init (&value, GDK_TYPE_RGBA);
2144   rgba_init (&rgba, 1, 1, 1, 1);
2145   g_value_set_boxed (&value, &rgba);
2146   _gtk_style_property_register           (g_param_spec_boxed ("color",
2147                                           "Foreground color",
2148                                           "Foreground color",
2149                                           GDK_TYPE_RGBA, 0),
2150                                           GTK_STYLE_PROPERTY_INHERIT,
2151                                           NULL,
2152                                           NULL,
2153                                           NULL,
2154                                           &value);
2155   rgba_init (&rgba, 0, 0, 0, 0);
2156   g_value_set_boxed (&value, &rgba);
2157   _gtk_style_property_register           (g_param_spec_boxed ("background-color",
2158                                           "Background color",
2159                                           "Background color",
2160                                           GDK_TYPE_RGBA, 0),
2161                                           0,
2162                                           NULL,
2163                                           transparent_color_value_parse,
2164                                           NULL,
2165                                           &value);
2166   g_value_unset (&value);
2167
2168   g_value_init (&value, G_TYPE_STRV);
2169   g_value_set_boxed (&value, default_font_family);
2170   _gtk_style_property_register           (g_param_spec_boxed ("font-family",
2171                                                               "Font family",
2172                                                               "Font family",
2173                                                               G_TYPE_STRV, 0),
2174                                           GTK_STYLE_PROPERTY_INHERIT,
2175                                           NULL,
2176                                           font_family_parse,
2177                                           font_family_value_print,
2178                                           &value);
2179   g_value_unset (&value);
2180   _gtk_style_property_register           (g_param_spec_enum ("font-style",
2181                                                              "Font style",
2182                                                              "Font style",
2183                                                              PANGO_TYPE_STYLE,
2184                                                              PANGO_STYLE_NORMAL, 0),
2185                                           GTK_STYLE_PROPERTY_INHERIT,
2186                                           NULL,
2187                                           NULL,
2188                                           NULL,
2189                                           NULL);
2190   _gtk_style_property_register           (g_param_spec_enum ("font-variant",
2191                                                              "Font variant",
2192                                                              "Font variant",
2193                                                              PANGO_TYPE_VARIANT,
2194                                                              PANGO_VARIANT_NORMAL, 0),
2195                                           GTK_STYLE_PROPERTY_INHERIT,
2196                                           NULL,
2197                                           NULL,
2198                                           NULL,
2199                                           NULL);
2200   /* xxx: need to parse this properly, ie parse the numbers */
2201   _gtk_style_property_register           (g_param_spec_enum ("font-weight",
2202                                                              "Font weight",
2203                                                              "Font weight",
2204                                                              PANGO_TYPE_WEIGHT,
2205                                                              PANGO_WEIGHT_NORMAL, 0),
2206                                           GTK_STYLE_PROPERTY_INHERIT,
2207                                           NULL,
2208                                           NULL,
2209                                           NULL,
2210                                           NULL);
2211   g_value_init (&value, G_TYPE_DOUBLE);
2212   g_value_set_double (&value, 10);
2213   _gtk_style_property_register           (g_param_spec_double ("font-size",
2214                                                                "Font size",
2215                                                                "Font size",
2216                                                                0, G_MAXDOUBLE, 0, 0),
2217                                           GTK_STYLE_PROPERTY_INHERIT,
2218                                           NULL,
2219                                           NULL,
2220                                           NULL,
2221                                           &value);
2222   g_value_unset (&value);
2223
2224   _gtk_style_property_register           (g_param_spec_boxed ("text-shadow",
2225                                                               "Text shadow",
2226                                                               "Text shadow",
2227                                                               GTK_TYPE_SHADOW, 0),
2228                                           GTK_STYLE_PROPERTY_INHERIT,
2229                                           NULL,
2230                                           NULL,
2231                                           NULL,
2232                                           NULL);
2233
2234   _gtk_style_property_register           (g_param_spec_boxed ("icon-shadow",
2235                                                               "Icon shadow",
2236                                                               "Icon shadow",
2237                                                               GTK_TYPE_SHADOW, 0),
2238                                           GTK_STYLE_PROPERTY_INHERIT,
2239                                           NULL,
2240                                           NULL,
2241                                           NULL,
2242                                           NULL);
2243
2244   gtk_style_properties_register_property (NULL,
2245                                           g_param_spec_boxed ("box-shadow",
2246                                                               "Box shadow",
2247                                                               "Box shadow",
2248                                                               GTK_TYPE_SHADOW, 0));
2249   gtk_style_properties_register_property (NULL,
2250                                           g_param_spec_int ("margin-top",
2251                                                             "margin top",
2252                                                             "Margin at top",
2253                                                             0, G_MAXINT, 0, 0));
2254   gtk_style_properties_register_property (NULL,
2255                                           g_param_spec_int ("margin-left",
2256                                                             "margin left",
2257                                                             "Margin at left",
2258                                                             0, G_MAXINT, 0, 0));
2259   gtk_style_properties_register_property (NULL,
2260                                           g_param_spec_int ("margin-bottom",
2261                                                             "margin bottom",
2262                                                             "Margin at bottom",
2263                                                             0, G_MAXINT, 0, 0));
2264   gtk_style_properties_register_property (NULL,
2265                                           g_param_spec_int ("margin-right",
2266                                                             "margin right",
2267                                                             "Margin at right",
2268                                                             0, G_MAXINT, 0, 0));
2269   gtk_style_properties_register_property (NULL,
2270                                           g_param_spec_int ("padding-top",
2271                                                             "padding top",
2272                                                             "Padding at top",
2273                                                             0, G_MAXINT, 0, 0));
2274   gtk_style_properties_register_property (NULL,
2275                                           g_param_spec_int ("padding-left",
2276                                                             "padding left",
2277                                                             "Padding at left",
2278                                                             0, G_MAXINT, 0, 0));
2279   gtk_style_properties_register_property (NULL,
2280                                           g_param_spec_int ("padding-bottom",
2281                                                             "padding bottom",
2282                                                             "Padding at bottom",
2283                                                             0, G_MAXINT, 0, 0));
2284   gtk_style_properties_register_property (NULL,
2285                                           g_param_spec_int ("padding-right",
2286                                                             "padding right",
2287                                                             "Padding at right",
2288                                                             0, G_MAXINT, 0, 0));
2289   gtk_style_properties_register_property (NULL,
2290                                           g_param_spec_int ("border-top-width",
2291                                                             "border top width",
2292                                                             "Border width at top",
2293                                                             0, G_MAXINT, 0, 0));
2294   gtk_style_properties_register_property (NULL,
2295                                           g_param_spec_int ("border-left-width",
2296                                                             "border left width",
2297                                                             "Border width at left",
2298                                                             0, G_MAXINT, 0, 0));
2299   gtk_style_properties_register_property (NULL,
2300                                           g_param_spec_int ("border-bottom-width",
2301                                                             "border bottom width",
2302                                                             "Border width at bottom",
2303                                                             0, G_MAXINT, 0, 0));
2304   gtk_style_properties_register_property (NULL,
2305                                           g_param_spec_int ("border-right-width",
2306                                                             "border right width",
2307                                                             "Border width at right",
2308                                                             0, G_MAXINT, 0, 0));
2309
2310   _gtk_style_property_register           (g_param_spec_boxed ("border-top-left-radius",
2311                                                               "Border top left radius",
2312                                                               "Border radius of top left corner, in pixels",
2313                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2314                                           0,
2315                                           NULL,
2316                                           border_corner_radius_value_parse,
2317                                           border_corner_radius_value_print,
2318                                           NULL);
2319   _gtk_style_property_register           (g_param_spec_boxed ("border-top-right-radius",
2320                                                               "Border top right radius",
2321                                                               "Border radius of top right corner, in pixels",
2322                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2323                                           0,
2324                                           NULL,
2325                                           border_corner_radius_value_parse,
2326                                           border_corner_radius_value_print,
2327                                           NULL);
2328   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-right-radius",
2329                                                               "Border bottom right radius",
2330                                                               "Border radius of bottom right corner, in pixels",
2331                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2332                                           0,
2333                                           NULL,
2334                                           border_corner_radius_value_parse,
2335                                           border_corner_radius_value_print,
2336                                           NULL);
2337   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-left-radius",
2338                                                               "Border bottom left radius",
2339                                                               "Border radius of bottom left corner, in pixels",
2340                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2341                                           0,
2342                                           NULL,
2343                                           border_corner_radius_value_parse,
2344                                           border_corner_radius_value_print,
2345                                           NULL);
2346
2347   gtk_style_properties_register_property (NULL,
2348                                           g_param_spec_enum ("border-style",
2349                                                              "Border style",
2350                                                              "Border style",
2351                                                              GTK_TYPE_BORDER_STYLE,
2352                                                              GTK_BORDER_STYLE_NONE, 0));
2353   gtk_style_properties_register_property (NULL,
2354                                           g_param_spec_enum ("background-clip",
2355                                                              "Background clip",
2356                                                              "Background clip",
2357                                                              GTK_TYPE_CSS_AREA,
2358                                                              GTK_CSS_AREA_BORDER_BOX, 0));
2359   gtk_style_properties_register_property (NULL,
2360                                           g_param_spec_enum ("background-origin",
2361                                                              "Background origin",
2362                                                              "Background origin",
2363                                                              GTK_TYPE_CSS_AREA,
2364                                                              GTK_CSS_AREA_PADDING_BOX, 0));
2365   g_value_init (&value, GTK_TYPE_CSS_SPECIAL_VALUE);
2366   g_value_set_enum (&value, GTK_CSS_CURRENT_COLOR);
2367   _gtk_style_property_register           (g_param_spec_boxed ("border-top-color",
2368                                                               "Border top color",
2369                                                               "Border top color",
2370                                                               GDK_TYPE_RGBA, 0),
2371                                           0,
2372                                           NULL,
2373                                           transparent_color_value_parse,
2374                                           NULL,
2375                                           &value);
2376   _gtk_style_property_register           (g_param_spec_boxed ("border-right-color",
2377                                                               "Border right color",
2378                                                               "Border right color",
2379                                                               GDK_TYPE_RGBA, 0),
2380                                           0,
2381                                           NULL,
2382                                           transparent_color_value_parse,
2383                                           NULL,
2384                                           &value);
2385   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-color",
2386                                                               "Border bottom color",
2387                                                               "Border bottom color",
2388                                                               GDK_TYPE_RGBA, 0),
2389                                           0,
2390                                           NULL,
2391                                           transparent_color_value_parse,
2392                                           NULL,
2393                                           &value);
2394   _gtk_style_property_register           (g_param_spec_boxed ("border-left-color",
2395                                                               "Border left color",
2396                                                               "Border left color",
2397                                                               GDK_TYPE_RGBA, 0),
2398                                           0,
2399                                           NULL,
2400                                           transparent_color_value_parse,
2401                                           NULL,
2402                                           &value);
2403   g_value_unset (&value);
2404
2405   gtk_style_properties_register_property (NULL,
2406                                           g_param_spec_boxed ("background-image",
2407                                                               "Background Image",
2408                                                               "Background Image",
2409                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
2410   gtk_style_properties_register_property (NULL,
2411                                           g_param_spec_boxed ("background-repeat",
2412                                                               "Background repeat",
2413                                                               "Background repeat",
2414                                                               GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
2415
2416   gtk_style_properties_register_property (NULL,
2417                                           g_param_spec_boxed ("border-image-source",
2418                                                               "Border image source",
2419                                                               "Border image source",
2420                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
2421   gtk_style_properties_register_property (NULL,
2422                                           g_param_spec_boxed ("border-image-repeat",
2423                                                               "Border image repeat",
2424                                                               "Border image repeat",
2425                                                               GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
2426   gtk_style_properties_register_property (NULL,
2427                                           g_param_spec_boxed ("border-image-slice",
2428                                                               "Border image slice",
2429                                                               "Border image slice",
2430                                                               GTK_TYPE_BORDER, 0));
2431   g_value_init (&value, GTK_TYPE_BORDER);
2432   _gtk_style_property_register           (g_param_spec_boxed ("border-image-width",
2433                                                               "Border image width",
2434                                                               "Border image width",
2435                                                               GTK_TYPE_BORDER, 0),
2436                                           0,
2437                                           NULL,
2438                                           NULL,
2439                                           NULL,
2440                                           &value);
2441   g_value_unset (&value);
2442   gtk_style_properties_register_property (NULL,
2443                                           g_param_spec_object ("engine",
2444                                                                "Theming Engine",
2445                                                                "Theming Engine",
2446                                                                GTK_TYPE_THEMING_ENGINE, 0));
2447   gtk_style_properties_register_property (NULL,
2448                                           g_param_spec_boxed ("transition",
2449                                                               "Transition animation description",
2450                                                               "Transition animation description",
2451                                                               GTK_TYPE_ANIMATION_DESCRIPTION, 0));
2452
2453   /* Private property holding the binding sets */
2454   _gtk_style_property_register           (g_param_spec_boxed ("gtk-key-bindings",
2455                                                               "Key bindings",
2456                                                               "Key bindings",
2457                                                               G_TYPE_PTR_ARRAY, 0),
2458                                           0,
2459                                           NULL,
2460                                           bindings_value_parse,
2461                                           bindings_value_print,
2462                                           NULL);
2463
2464   /* initialize shorthands last, they depend on the real properties existing */
2465   _gtk_css_shorthand_property_init_properties ();
2466 }
2467
2468 /**
2469  * _gtk_style_property_lookup:
2470  * @name: name of the property to lookup
2471  *
2472  * Looks up the CSS property with the given @name. If no such
2473  * property exists, %NULL is returned.
2474  *
2475  * Returns: (transfer none): The property or %NULL if no
2476  *     property with the given name exists.
2477  **/
2478 GtkStyleProperty *
2479 _gtk_style_property_lookup (const char *name)
2480 {
2481   GtkStylePropertyClass *klass;
2482
2483   g_return_val_if_fail (name != NULL, NULL);
2484
2485   gtk_style_property_init_properties ();
2486
2487   klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY);
2488
2489   return g_hash_table_lookup (klass->properties, name);
2490 }
2491
2492 /**
2493  * _gtk_style_property_get_name:
2494  * @property: the property to query
2495  *
2496  * Gets the name of the given property.
2497  *
2498  * Returns: the name of the property
2499  **/
2500 const char *
2501 _gtk_style_property_get_name (GtkStyleProperty *property)
2502 {
2503   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
2504
2505   return property->name;
2506 }
2507
2508 /**
2509  * _gtk_style_property_get_value_type:
2510  * @property: the property to query
2511  *
2512  * Gets the value type of the @property, if the property is usable
2513  * in public API via _gtk_style_property_assign() and
2514  * _gtk_style_property_query(). If the @property is not usable in that
2515  * way, %G_TYPE_NONE is returned.
2516  *
2517  * Returns: the value type in use or %G_TYPE_NONE if none.
2518  **/
2519 GType
2520 _gtk_style_property_get_value_type (GtkStyleProperty *property)
2521 {
2522   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), G_TYPE_NONE);
2523
2524   return property->value_type;
2525 }
2526
2527
2528 void
2529 _gtk_style_property_register (GParamSpec               *pspec,
2530                               GtkStylePropertyFlags     flags,
2531                               GtkStylePropertyParser    property_parse_func,
2532                               GtkStyleParseFunc         parse_func,
2533                               GtkStylePrintFunc         print_func,
2534                               const GValue *            initial_value)
2535 {
2536   GtkStyleProperty *node;
2537
2538   node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
2539                        "name", pspec->name,
2540                        "value-type", pspec->value_type,
2541                        NULL);
2542   node->flags = flags;
2543   node->pspec = pspec;
2544   node->property_parse_func = property_parse_func;
2545   node->parse_func = parse_func;
2546   node->print_func = print_func;
2547
2548   _gtk_style_property_generate_id (node);
2549
2550   /* initialize the initial value */
2551   if (initial_value)
2552     {
2553       g_value_init (&node->initial_value, G_VALUE_TYPE (initial_value));
2554       g_value_copy (initial_value, &node->initial_value);
2555     }
2556   else
2557     {
2558       g_value_init (&node->initial_value, pspec->value_type);
2559       if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
2560         g_value_set_object (&node->initial_value, gtk_theming_engine_load (NULL));
2561       else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
2562         g_value_take_boxed (&node->initial_value, pango_font_description_from_string ("Sans 10"));
2563       else if (pspec->value_type == GDK_TYPE_RGBA)
2564         {
2565           GdkRGBA color;
2566           gdk_rgba_parse (&color, "pink");
2567           g_value_set_boxed (&node->initial_value, &color);
2568         }
2569       else if (pspec->value_type == GTK_TYPE_BORDER)
2570         {
2571           g_value_take_boxed (&node->initial_value, gtk_border_new ());
2572         }
2573       else
2574         g_param_value_set_default (pspec, &node->initial_value);
2575     }
2576 }