]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperty.c
css: start background-repeat
[~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 "gtkcsstypesprivate.h"
35
36 /* the actual parsers we have */
37 #include "gtkanimationdescription.h"
38 #include "gtkbindings.h"
39 #include "gtkborderimageprivate.h"
40 #include "gtkgradient.h"
41 #include "gtkshadowprivate.h"
42 #include "gtkthemingengine.h"
43 #include "gtktypebuiltins.h"
44
45 /* this is in case round() is not provided by the compiler, 
46  * such as in the case of C89 compilers, like MSVC
47  */
48 #include "fallback-c89.c"
49
50 static GHashTable *parse_funcs = NULL;
51 static GHashTable *print_funcs = NULL;
52 static GHashTable *properties = NULL;
53
54 static void
55 register_conversion_function (GType             type,
56                               GtkStyleParseFunc parse,
57                               GtkStylePrintFunc print)
58 {
59   if (parse)
60     g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
61   if (print)
62     g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
63 }
64
65 static void
66 string_append_double (GString *string,
67                       double   d)
68 {
69   char buf[G_ASCII_DTOSTR_BUF_SIZE];
70
71   g_ascii_dtostr (buf, sizeof (buf), d);
72   g_string_append (string, buf);
73 }
74
75 static void
76 string_append_string (GString    *str,
77                       const char *string)
78 {
79   gsize len;
80
81   g_string_append_c (str, '"');
82
83   do {
84     len = strcspn (string, "\"\n\r\f");
85     g_string_append (str, string);
86     string += len;
87     switch (*string)
88       {
89       case '\0':
90         break;
91       case '\n':
92         g_string_append (str, "\\A ");
93         break;
94       case '\r':
95         g_string_append (str, "\\D ");
96         break;
97       case '\f':
98         g_string_append (str, "\\C ");
99         break;
100       case '\"':
101         g_string_append (str, "\\\"");
102         break;
103       default:
104         g_assert_not_reached ();
105         break;
106       }
107   } while (*string);
108
109   g_string_append_c (str, '"');
110 }
111
112 /*** IMPLEMENTATIONS ***/
113
114 static gboolean 
115 rgba_value_parse (GtkCssParser *parser,
116                   GFile        *base,
117                   GValue       *value)
118 {
119   GtkSymbolicColor *symbolic;
120   GdkRGBA rgba;
121
122   symbolic = _gtk_css_parser_read_symbolic_color (parser);
123   if (symbolic == NULL)
124     return FALSE;
125
126   if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
127     {
128       g_value_set_boxed (value, &rgba);
129       gtk_symbolic_color_unref (symbolic);
130     }
131   else
132     {
133       g_value_unset (value);
134       g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
135       g_value_take_boxed (value, symbolic);
136     }
137
138   return TRUE;
139 }
140
141 static void
142 rgba_value_print (const GValue *value,
143                   GString      *string)
144 {
145   const GdkRGBA *rgba = g_value_get_boxed (value);
146
147   if (rgba == NULL)
148     g_string_append (string, "none");
149   else
150     {
151       char *s = gdk_rgba_to_string (rgba);
152       g_string_append (string, s);
153       g_free (s);
154     }
155 }
156
157 static gboolean 
158 color_value_parse (GtkCssParser *parser,
159                    GFile        *base,
160                    GValue       *value)
161 {
162   GtkSymbolicColor *symbolic;
163   GdkRGBA rgba;
164
165   symbolic = _gtk_css_parser_read_symbolic_color (parser);
166   if (symbolic == NULL)
167     return FALSE;
168
169   if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
170     {
171       GdkColor color;
172
173       color.red = rgba.red * 65535. + 0.5;
174       color.green = rgba.green * 65535. + 0.5;
175       color.blue = rgba.blue * 65535. + 0.5;
176
177       g_value_set_boxed (value, &color);
178     }
179   else
180     {
181       g_value_unset (value);
182       g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
183       g_value_take_boxed (value, symbolic);
184     }
185
186   return TRUE;
187 }
188
189 static void
190 color_value_print (const GValue *value,
191                    GString      *string)
192 {
193   const GdkColor *color = g_value_get_boxed (value);
194
195   if (color == NULL)
196     g_string_append (string, "none");
197   else
198     {
199       char *s = gdk_color_to_string (color);
200       g_string_append (string, s);
201       g_free (s);
202     }
203 }
204
205 static gboolean
206 symbolic_color_value_parse (GtkCssParser *parser,
207                             GFile        *base,
208                             GValue       *value)
209 {
210   GtkSymbolicColor *symbolic;
211
212   symbolic = _gtk_css_parser_read_symbolic_color (parser);
213   if (symbolic == NULL)
214     return FALSE;
215
216   g_value_take_boxed (value, symbolic);
217   return TRUE;
218 }
219
220 static void
221 symbolic_color_value_print (const GValue *value,
222                             GString      *string)
223 {
224   GtkSymbolicColor *symbolic = g_value_get_boxed (value);
225
226   if (symbolic == NULL)
227     g_string_append (string, "none");
228   else
229     {
230       char *s = gtk_symbolic_color_to_string (symbolic);
231       g_string_append (string, s);
232       g_free (s);
233     }
234 }
235
236 static gboolean
237 font_family_parse (GtkCssParser *parser,
238                    GFile        *base,
239                    GValue       *value)
240 {
241   GPtrArray *names;
242   char *name;
243
244   /* We don't special case generic families. Pango should do
245    * that for us */
246
247   names = g_ptr_array_new ();
248
249   do {
250     name = _gtk_css_parser_try_ident (parser, TRUE);
251     if (name)
252       {
253         GString *string = g_string_new (name);
254         g_free (name);
255         while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
256           {
257             g_string_append_c (string, ' ');
258             g_string_append (string, name);
259             g_free (name);
260           }
261         name = g_string_free (string, FALSE);
262       }
263     else 
264       {
265         name = _gtk_css_parser_read_string (parser);
266         if (name == NULL)
267           {
268             g_ptr_array_free (names, TRUE);
269             return FALSE;
270           }
271       }
272
273     g_ptr_array_add (names, name);
274   } while (_gtk_css_parser_try (parser, ",", TRUE));
275
276   /* NULL-terminate array */
277   g_ptr_array_add (names, NULL);
278   g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
279   return TRUE;
280 }
281
282 static void
283 font_family_value_print (const GValue *value,
284                          GString      *string)
285 {
286   const char **names = g_value_get_boxed (value);
287
288   if (names == NULL || *names == NULL)
289     {
290       g_string_append (string, "none");
291       return;
292     }
293
294   string_append_string (string, *names);
295   names++;
296   while (*names)
297     {
298       g_string_append (string, ", ");
299       string_append_string (string, *names);
300       names++;
301     }
302 }
303
304 static gboolean 
305 font_description_value_parse (GtkCssParser *parser,
306                               GFile        *base,
307                               GValue       *value)
308 {
309   PangoFontDescription *font_desc;
310   guint mask;
311   char *str;
312
313   str = _gtk_css_parser_read_value (parser);
314   if (str == NULL)
315     return FALSE;
316
317   font_desc = pango_font_description_from_string (str);
318   mask = pango_font_description_get_set_fields (font_desc);
319   /* These values are not really correct,
320    * but the fields must be set, so we set them to something */
321   if ((mask & PANGO_FONT_MASK_FAMILY) == 0)
322     pango_font_description_set_family_static (font_desc, "Sans");
323   if ((mask & PANGO_FONT_MASK_SIZE) == 0)
324     pango_font_description_set_size (font_desc, 10 * PANGO_SCALE);
325   g_free (str);
326   g_value_take_boxed (value, font_desc);
327   return TRUE;
328 }
329
330 static void
331 font_description_value_print (const GValue *value,
332                               GString      *string)
333 {
334   const PangoFontDescription *desc = g_value_get_boxed (value);
335
336   if (desc == NULL)
337     g_string_append (string, "none");
338   else
339     {
340       char *s = pango_font_description_to_string (desc);
341       g_string_append (string, s);
342       g_free (s);
343     }
344 }
345
346 static gboolean 
347 boolean_value_parse (GtkCssParser *parser,
348                      GFile        *base,
349                      GValue       *value)
350 {
351   if (_gtk_css_parser_try (parser, "true", TRUE) ||
352       _gtk_css_parser_try (parser, "1", TRUE))
353     {
354       g_value_set_boolean (value, TRUE);
355       return TRUE;
356     }
357   else if (_gtk_css_parser_try (parser, "false", TRUE) ||
358            _gtk_css_parser_try (parser, "0", TRUE))
359     {
360       g_value_set_boolean (value, FALSE);
361       return TRUE;
362     }
363   else
364     {
365       _gtk_css_parser_error (parser, "Expected a boolean value");
366       return FALSE;
367     }
368 }
369
370 static void
371 boolean_value_print (const GValue *value,
372                      GString      *string)
373 {
374   if (g_value_get_boolean (value))
375     g_string_append (string, "true");
376   else
377     g_string_append (string, "false");
378 }
379
380 static gboolean 
381 int_value_parse (GtkCssParser *parser,
382                  GFile        *base,
383                  GValue       *value)
384 {
385   gint i;
386
387   if (!_gtk_css_parser_try_int (parser, &i))
388     {
389       _gtk_css_parser_error (parser, "Expected a valid integer value");
390       return FALSE;
391     }
392
393   g_value_set_int (value, i);
394   return TRUE;
395 }
396
397 static void
398 int_value_print (const GValue *value,
399                  GString      *string)
400 {
401   g_string_append_printf (string, "%d", g_value_get_int (value));
402 }
403
404 static gboolean 
405 uint_value_parse (GtkCssParser *parser,
406                   GFile        *base,
407                   GValue       *value)
408 {
409   guint u;
410
411   if (!_gtk_css_parser_try_uint (parser, &u))
412     {
413       _gtk_css_parser_error (parser, "Expected a valid unsigned value");
414       return FALSE;
415     }
416
417   g_value_set_uint (value, u);
418   return TRUE;
419 }
420
421 static void
422 uint_value_print (const GValue *value,
423                   GString      *string)
424 {
425   g_string_append_printf (string, "%u", g_value_get_uint (value));
426 }
427
428 static gboolean 
429 double_value_parse (GtkCssParser *parser,
430                     GFile        *base,
431                     GValue       *value)
432 {
433   gdouble d;
434
435   if (!_gtk_css_parser_try_double (parser, &d))
436     {
437       _gtk_css_parser_error (parser, "Expected a number");
438       return FALSE;
439     }
440
441   g_value_set_double (value, d);
442   return TRUE;
443 }
444
445 static void
446 double_value_print (const GValue *value,
447                     GString      *string)
448 {
449   string_append_double (string, g_value_get_double (value));
450 }
451
452 static gboolean 
453 float_value_parse (GtkCssParser *parser,
454                    GFile        *base,
455                    GValue       *value)
456 {
457   gdouble d;
458
459   if (!_gtk_css_parser_try_double (parser, &d))
460     {
461       _gtk_css_parser_error (parser, "Expected a number");
462       return FALSE;
463     }
464
465   g_value_set_float (value, d);
466   return TRUE;
467 }
468
469 static void
470 float_value_print (const GValue *value,
471                    GString      *string)
472 {
473   string_append_double (string, g_value_get_float (value));
474 }
475
476 static gboolean 
477 string_value_parse (GtkCssParser *parser,
478                     GFile        *base,
479                     GValue       *value)
480 {
481   char *str = _gtk_css_parser_read_string (parser);
482
483   if (str == NULL)
484     return FALSE;
485
486   g_value_take_string (value, str);
487   return TRUE;
488 }
489
490 static void
491 string_value_print (const GValue *value,
492                     GString      *str)
493 {
494   string_append_string (str, g_value_get_string (value));
495 }
496
497 static gboolean 
498 theming_engine_value_parse (GtkCssParser *parser,
499                             GFile        *base,
500                             GValue       *value)
501 {
502   GtkThemingEngine *engine;
503   char *str;
504
505   str = _gtk_css_parser_try_ident (parser, TRUE);
506   if (str == NULL)
507     {
508       _gtk_css_parser_error (parser, "Expected a valid theme name");
509       return FALSE;
510     }
511
512   engine = gtk_theming_engine_load (str);
513   if (engine == NULL)
514     {
515       _gtk_css_parser_error (parser, "Themeing engine '%s' not found", str);
516       g_free (str);
517       return FALSE;
518     }
519
520   g_value_set_object (value, engine);
521   g_free (str);
522   return TRUE;
523 }
524
525 static void
526 theming_engine_value_print (const GValue *value,
527                             GString      *string)
528 {
529   GtkThemingEngine *engine;
530   char *name;
531
532   engine = g_value_get_object (value);
533   if (engine == NULL)
534     g_string_append (string, "none");
535   else
536     {
537       /* XXX: gtk_theming_engine_get_name()? */
538       g_object_get (engine, "name", &name, NULL);
539       g_string_append (string, name ? name : "none");
540       g_free (name);
541     }
542 }
543
544 static gboolean 
545 animation_description_value_parse (GtkCssParser *parser,
546                                    GFile        *base,
547                                    GValue       *value)
548 {
549   GtkAnimationDescription *desc;
550   char *str;
551
552   str = _gtk_css_parser_read_value (parser);
553   if (str == NULL)
554     return FALSE;
555
556   desc = _gtk_animation_description_from_string (str);
557   g_free (str);
558
559   if (desc == NULL)
560     {
561       _gtk_css_parser_error (parser, "Invalid animation description");
562       return FALSE;
563     }
564   
565   g_value_take_boxed (value, desc);
566   return TRUE;
567 }
568
569 static void
570 animation_description_value_print (const GValue *value,
571                                    GString      *string)
572 {
573   GtkAnimationDescription *desc = g_value_get_boxed (value);
574
575   if (desc == NULL)
576     g_string_append (string, "none");
577   else
578     _gtk_animation_description_print (desc, string);
579 }
580
581 static gboolean 
582 border_value_parse (GtkCssParser *parser,
583                     GFile        *base,
584                     GValue       *value)
585 {
586   GtkBorder border = { 0, };
587   guint i, numbers[4];
588
589   for (i = 0; i < G_N_ELEMENTS (numbers); i++)
590     {
591       if (!_gtk_css_parser_try_uint (parser, &numbers[i]))
592         break;
593
594       /* XXX: shouldn't allow spaces here? */
595       _gtk_css_parser_try (parser, "px", TRUE);
596     }
597
598   if (i == 0)
599     {
600       _gtk_css_parser_error (parser, "Expected valid border");
601       return FALSE;
602     }
603
604   border.top = numbers[0];
605   if (i > 1)
606     border.right = numbers[1];
607   else
608     border.right = border.top;
609   if (i > 2)
610     border.bottom = numbers[2];
611   else
612     border.bottom = border.top;
613   if (i > 3)
614     border.left = numbers[3];
615   else
616     border.left = border.right;
617
618   g_value_set_boxed (value, &border);
619   return TRUE;
620 }
621
622 static void
623 border_value_print (const GValue *value, GString *string)
624 {
625   const GtkBorder *border = g_value_get_boxed (value);
626
627   if (border == NULL)
628     g_string_append (string, "none");
629   else if (border->left != border->right)
630     g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
631   else if (border->top != border->bottom)
632     g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
633   else if (border->top != border->left)
634     g_string_append_printf (string, "%d %d", border->top, border->right);
635   else
636     g_string_append_printf (string, "%d", border->top);
637 }
638
639 static gboolean 
640 gradient_value_parse (GtkCssParser *parser,
641                       GFile        *base,
642                       GValue       *value)
643 {
644   GtkGradient *gradient;
645   cairo_pattern_type_t type;
646   gdouble coords[6];
647   guint i;
648
649   if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
650     {
651       _gtk_css_parser_error (parser,
652                              "Expected '-gtk-gradient'");
653       return FALSE;
654     }
655
656   if (!_gtk_css_parser_try (parser, "(", TRUE))
657     {
658       _gtk_css_parser_error (parser,
659                              "Expected '(' after '-gtk-gradient'");
660       return FALSE;
661     }
662
663   /* Parse gradient type */
664   if (_gtk_css_parser_try (parser, "linear", TRUE))
665     type = CAIRO_PATTERN_TYPE_LINEAR;
666   else if (_gtk_css_parser_try (parser, "radial", TRUE))
667     type = CAIRO_PATTERN_TYPE_RADIAL;
668   else
669     {
670       _gtk_css_parser_error (parser,
671                              "Gradient type must be 'radial' or 'linear'");
672       return FALSE;
673     }
674
675   /* Parse start/stop position parameters */
676   for (i = 0; i < 2; i++)
677     {
678       if (! _gtk_css_parser_try (parser, ",", TRUE))
679         {
680           _gtk_css_parser_error (parser,
681                                  "Expected ','");
682           return FALSE;
683         }
684
685       if (_gtk_css_parser_try (parser, "left", TRUE))
686         coords[i * 3] = 0;
687       else if (_gtk_css_parser_try (parser, "right", TRUE))
688         coords[i * 3] = 1;
689       else if (_gtk_css_parser_try (parser, "center", TRUE))
690         coords[i * 3] = 0.5;
691       else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
692         {
693           _gtk_css_parser_error (parser,
694                                  "Expected a valid X coordinate");
695           return FALSE;
696         }
697
698       if (_gtk_css_parser_try (parser, "top", TRUE))
699         coords[i * 3 + 1] = 0;
700       else if (_gtk_css_parser_try (parser, "bottom", TRUE))
701         coords[i * 3 + 1] = 1;
702       else if (_gtk_css_parser_try (parser, "center", TRUE))
703         coords[i * 3 + 1] = 0.5;
704       else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
705         {
706           _gtk_css_parser_error (parser,
707                                  "Expected a valid Y coordinate");
708           return FALSE;
709         }
710
711       if (type == CAIRO_PATTERN_TYPE_RADIAL)
712         {
713           /* Parse radius */
714           if (! _gtk_css_parser_try (parser, ",", TRUE))
715             {
716               _gtk_css_parser_error (parser,
717                                      "Expected ','");
718               return FALSE;
719             }
720
721           if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
722             {
723               _gtk_css_parser_error (parser,
724                                      "Expected a numer for the radius");
725               return FALSE;
726             }
727         }
728     }
729
730   if (type == CAIRO_PATTERN_TYPE_LINEAR)
731     gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
732   else
733     gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
734                                         coords[3], coords[4], coords[5]);
735
736   while (_gtk_css_parser_try (parser, ",", TRUE))
737     {
738       GtkSymbolicColor *color;
739       gdouble position;
740
741       if (_gtk_css_parser_try (parser, "from", TRUE))
742         {
743           position = 0;
744
745           if (!_gtk_css_parser_try (parser, "(", TRUE))
746             {
747               gtk_gradient_unref (gradient);
748               _gtk_css_parser_error (parser,
749                                      "Expected '('");
750               return FALSE;
751             }
752
753         }
754       else if (_gtk_css_parser_try (parser, "to", TRUE))
755         {
756           position = 1;
757
758           if (!_gtk_css_parser_try (parser, "(", TRUE))
759             {
760               gtk_gradient_unref (gradient);
761               _gtk_css_parser_error (parser,
762                                      "Expected '('");
763               return FALSE;
764             }
765
766         }
767       else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
768         {
769           if (!_gtk_css_parser_try (parser, "(", TRUE))
770             {
771               gtk_gradient_unref (gradient);
772               _gtk_css_parser_error (parser,
773                                      "Expected '('");
774               return FALSE;
775             }
776
777           if (!_gtk_css_parser_try_double (parser, &position))
778             {
779               gtk_gradient_unref (gradient);
780               _gtk_css_parser_error (parser,
781                                      "Expected a valid number");
782               return FALSE;
783             }
784
785           if (!_gtk_css_parser_try (parser, ",", TRUE))
786             {
787               gtk_gradient_unref (gradient);
788               _gtk_css_parser_error (parser,
789                                      "Expected a comma");
790               return FALSE;
791             }
792         }
793       else
794         {
795           gtk_gradient_unref (gradient);
796           _gtk_css_parser_error (parser,
797                                  "Not a valid color-stop definition");
798           return FALSE;
799         }
800
801       color = _gtk_css_parser_read_symbolic_color (parser);
802       if (color == NULL)
803         {
804           gtk_gradient_unref (gradient);
805           return FALSE;
806         }
807
808       gtk_gradient_add_color_stop (gradient, position, color);
809       gtk_symbolic_color_unref (color);
810
811       if (!_gtk_css_parser_try (parser, ")", TRUE))
812         {
813           gtk_gradient_unref (gradient);
814           _gtk_css_parser_error (parser,
815                                  "Expected ')'");
816           return FALSE;
817         }
818     }
819
820   if (!_gtk_css_parser_try (parser, ")", TRUE))
821     {
822       gtk_gradient_unref (gradient);
823       _gtk_css_parser_error (parser,
824                              "Expected ')'");
825       return FALSE;
826     }
827
828   g_value_take_boxed (value, gradient);
829   return TRUE;
830 }
831
832 static void
833 gradient_value_print (const GValue *value,
834                       GString      *string)
835 {
836   GtkGradient *gradient = g_value_get_boxed (value);
837
838   if (gradient == NULL)
839     g_string_append (string, "none");
840   else
841     {
842       char *s = gtk_gradient_to_string (gradient);
843       g_string_append (string, s);
844       g_free (s);
845     }
846 }
847
848 static GFile *
849 gtk_css_parse_url (GtkCssParser *parser,
850                    GFile        *base)
851 {
852   gchar *path;
853   GFile *file;
854
855   if (_gtk_css_parser_try (parser, "url", FALSE))
856     {
857       if (!_gtk_css_parser_try (parser, "(", TRUE))
858         {
859           _gtk_css_parser_skip_whitespace (parser);
860           if (_gtk_css_parser_try (parser, "(", TRUE))
861             {
862               GError *error;
863               
864               error = g_error_new_literal (GTK_CSS_PROVIDER_ERROR,
865                                            GTK_CSS_PROVIDER_ERROR_DEPRECATED,
866                                            "Whitespace between 'url' and '(' is deprecated");
867                              
868               _gtk_css_parser_take_error (parser, error);
869             }
870           else
871             {
872               _gtk_css_parser_error (parser, "Expected '(' after 'url'");
873               return NULL;
874             }
875         }
876
877       path = _gtk_css_parser_read_string (parser);
878       if (path == NULL)
879         return NULL;
880
881       if (!_gtk_css_parser_try (parser, ")", TRUE))
882         {
883           _gtk_css_parser_error (parser, "No closing ')' found for 'url'");
884           g_free (path);
885           return NULL;
886         }
887     }
888   else
889     {
890       path = _gtk_css_parser_try_name (parser, TRUE);
891       if (path == NULL)
892         {
893           _gtk_css_parser_error (parser, "Not a valid url");
894           return NULL;
895         }
896     }
897
898   file = g_file_resolve_relative_path (base, path);
899   g_free (path);
900
901   return file;
902 }
903
904 static gboolean 
905 pattern_value_parse (GtkCssParser *parser,
906                      GFile        *base,
907                      GValue       *value)
908 {
909   if (_gtk_css_parser_begins_with (parser, '-'))
910     {
911       g_value_unset (value);
912       g_value_init (value, GTK_TYPE_GRADIENT);
913       return gradient_value_parse (parser, base, value);
914     }
915   else
916     {
917       GError *error = NULL;
918       gchar *path;
919       GdkPixbuf *pixbuf;
920       GFile *file;
921       cairo_surface_t *surface;
922       cairo_pattern_t *pattern;
923       cairo_t *cr;
924       cairo_matrix_t matrix;
925
926       file = gtk_css_parse_url (parser, base);
927       if (file == NULL)
928         return FALSE;
929
930       path = g_file_get_path (file);
931       g_object_unref (file);
932
933       pixbuf = gdk_pixbuf_new_from_file (path, &error);
934       g_free (path);
935       if (pixbuf == NULL)
936         {
937           _gtk_css_parser_take_error (parser, error);
938           return FALSE;
939         }
940
941       surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
942                                             gdk_pixbuf_get_width (pixbuf),
943                                             gdk_pixbuf_get_height (pixbuf));
944       cr = cairo_create (surface);
945       gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
946       cairo_paint (cr);
947       pattern = cairo_pattern_create_for_surface (surface);
948
949       cairo_matrix_init_scale (&matrix,
950                                gdk_pixbuf_get_width (pixbuf),
951                                gdk_pixbuf_get_height (pixbuf));
952       cairo_pattern_set_matrix (pattern, &matrix);
953
954       cairo_surface_destroy (surface);
955       cairo_destroy (cr);
956       g_object_unref (pixbuf);
957
958       g_value_take_boxed (value, pattern);
959     }
960   
961   return TRUE;
962 }
963
964 static cairo_status_t
965 surface_write (void                *closure,
966                const unsigned char *data,
967                unsigned int         length)
968 {
969   g_byte_array_append (closure, data, length);
970
971   return CAIRO_STATUS_SUCCESS;
972 }
973
974 static void
975 surface_print (cairo_surface_t *surface,
976                GString *        string)
977 {
978 #if CAIRO_HAS_PNG_FUNCTIONS
979   GByteArray *array;
980   char *base64;
981   
982   array = g_byte_array_new ();
983   cairo_surface_write_to_png_stream (surface, surface_write, array);
984   base64 = g_base64_encode (array->data, array->len);
985   g_byte_array_free (array, TRUE);
986
987   g_string_append (string, "url(\"data:image/png;base64,");
988   g_string_append (string, base64);
989   g_string_append (string, "\")");
990
991   g_free (base64);
992 #else
993   g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
994 #endif
995 }
996
997 static void
998 pattern_value_print (const GValue *value,
999                      GString      *string)
1000 {
1001   cairo_pattern_t *pattern;
1002   cairo_surface_t *surface;
1003
1004   pattern = g_value_get_boxed (value);
1005
1006   if (pattern == NULL)
1007     {
1008       g_string_append (string, "none");
1009       return;
1010     }
1011
1012   switch (cairo_pattern_get_type (pattern))
1013     {
1014     case CAIRO_PATTERN_TYPE_SURFACE:
1015       if (cairo_pattern_get_surface (pattern, &surface) != CAIRO_STATUS_SUCCESS)
1016         {
1017           g_assert_not_reached ();
1018         }
1019       surface_print (surface, string);
1020       break;
1021     case CAIRO_PATTERN_TYPE_SOLID:
1022     case CAIRO_PATTERN_TYPE_LINEAR:
1023     case CAIRO_PATTERN_TYPE_RADIAL:
1024     default:
1025       g_assert_not_reached ();
1026       break;
1027     }
1028 }
1029
1030 static gboolean
1031 shadow_value_parse (GtkCssParser *parser,
1032                     GFile *base,
1033                     GValue *value)
1034 {
1035   gboolean have_inset, have_color, have_lengths;
1036   gdouble hoffset, voffset, blur, spread;
1037   GtkSymbolicColor *color;
1038   GtkShadow *shadow;
1039   guint i;
1040
1041   shadow = _gtk_shadow_new ();
1042
1043   do
1044     {
1045       have_inset = have_lengths = have_color = FALSE;
1046
1047       for (i = 0; i < 3; i++)
1048         {
1049           if (!have_inset && 
1050               _gtk_css_parser_try (parser, "inset", TRUE))
1051             {
1052               have_inset = TRUE;
1053               continue;
1054             }
1055             
1056           if (!have_lengths &&
1057               _gtk_css_parser_try_double (parser, &hoffset))
1058             {
1059               have_lengths = TRUE;
1060
1061               if (!_gtk_css_parser_try_double (parser, &voffset))
1062                 {
1063                   _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
1064                   _gtk_shadow_unref (shadow);
1065                   return FALSE;
1066                 }
1067
1068               if (!_gtk_css_parser_try_double (parser, &blur))
1069                 blur = 0;
1070
1071               if (!_gtk_css_parser_try_double (parser, &spread))
1072                 spread = 0;
1073
1074               continue;
1075             }
1076
1077           if (!have_color)
1078             {
1079               have_color = TRUE;
1080
1081               /* XXX: the color is optional and UA-defined if it's missing,
1082                * but it doesn't really make sense for us...
1083                */
1084               color = _gtk_css_parser_read_symbolic_color (parser);
1085
1086               if (color == NULL)
1087                 {
1088                   _gtk_shadow_unref (shadow);
1089                   return FALSE;
1090                 }
1091             }
1092         }
1093
1094       if (!have_color || !have_lengths)
1095         {
1096           _gtk_css_parser_error (parser, "Must specify at least color and offsets");
1097           _gtk_shadow_unref (shadow);
1098           return FALSE;
1099         }
1100
1101       _gtk_shadow_append (shadow,
1102                           hoffset, voffset,
1103                           blur, spread,
1104                           have_inset, color);
1105
1106       gtk_symbolic_color_unref (color);
1107
1108     }
1109   while (_gtk_css_parser_try (parser, ",", TRUE));
1110
1111   g_value_take_boxed (value, shadow);
1112   return TRUE;
1113 }
1114
1115 static void
1116 shadow_value_print (const GValue *value,
1117                     GString      *string)
1118 {
1119   GtkShadow *shadow;
1120
1121   shadow = g_value_get_boxed (value);
1122
1123   if (shadow == NULL)
1124     g_string_append (string, "none");
1125   else
1126     _gtk_shadow_print (shadow, string);
1127 }
1128
1129 static gboolean
1130 background_repeat_value_parse (GtkCssParser *parser,
1131                                GFile *file,
1132                                GValue *value)
1133 {
1134   GtkCssBackgroundRepeat repeat;
1135   GtkCssBackgroundRepeatStyle style;
1136
1137   if (_gtk_css_parser_try (parser, "repeat", TRUE))
1138     style = GTK_CSS_BACKGROUND_REPEAT_STYLE_REPEAT;
1139   else if (_gtk_css_parser_try (parser, "no-repeat", TRUE))
1140     style = GTK_CSS_BACKGROUND_REPEAT_STYLE_NO_REPEAT;
1141   else
1142     style = GTK_CSS_BACKGROUND_REPEAT_STYLE_NONE;
1143
1144   repeat.repeat = style;
1145
1146   g_value_set_boxed (value, &repeat);
1147
1148   return TRUE;
1149 }
1150
1151 static const gchar *
1152 background_repeat_style_to_string (GtkCssBackgroundRepeatStyle repeat)
1153 {
1154   switch (repeat)
1155     {
1156     case GTK_CSS_BACKGROUND_REPEAT_STYLE_REPEAT:
1157       return "repeat";
1158     case GTK_CSS_BACKGROUND_REPEAT_STYLE_NO_REPEAT:
1159       return "no-repeat";
1160     default:
1161       return NULL;
1162     }
1163 }
1164
1165 static void
1166 background_repeat_value_print (const GValue *value,
1167                                GString      *string)
1168 {
1169   GtkCssBackgroundRepeat *repeat;
1170
1171   repeat = g_value_get_boxed (value);
1172
1173   g_string_append (string, background_repeat_style_to_string (repeat->repeat));
1174 }
1175
1176 static gboolean
1177 border_image_repeat_value_parse (GtkCssParser *parser,
1178                                  GFile *file,
1179                                  GValue *value)
1180 {
1181   GtkCssBorderImageRepeat image_repeat;
1182   GtkCssBorderRepeatStyle styles[2];
1183   gint i;
1184
1185   for (i = 0; i < 2; i++)
1186     {
1187       if (_gtk_css_parser_try (parser, "stretch", TRUE))
1188         styles[i] = GTK_CSS_REPEAT_STYLE_NONE;
1189       else if (_gtk_css_parser_try (parser, "repeat", TRUE))
1190         styles[i] = GTK_CSS_REPEAT_STYLE_REPEAT;
1191       else if (_gtk_css_parser_try (parser, "round", TRUE))
1192         styles[i] = GTK_CSS_REPEAT_STYLE_ROUND;
1193       else if (_gtk_css_parser_try (parser, "space", TRUE))
1194         styles[i] = GTK_CSS_REPEAT_STYLE_SPACE;
1195       else if (i == 0)
1196         {
1197           styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_NONE;
1198           break;
1199         }
1200       else
1201         styles[i] = styles[0];
1202     }
1203
1204   image_repeat.hrepeat = styles[0];
1205   image_repeat.vrepeat = styles[1];
1206
1207   g_value_set_boxed (value, &image_repeat);
1208
1209   return TRUE;
1210 }
1211
1212 static const gchar *
1213 border_image_repeat_style_to_string (GtkCssBorderRepeatStyle repeat)
1214 {
1215   switch (repeat)
1216     {
1217     case GTK_CSS_REPEAT_STYLE_NONE:
1218       return "stretch";
1219     case GTK_CSS_REPEAT_STYLE_REPEAT:
1220       return "repeat";
1221     case GTK_CSS_REPEAT_STYLE_ROUND:
1222       return "round";
1223     case GTK_CSS_REPEAT_STYLE_SPACE:
1224       return "space";
1225     default:
1226       return NULL;
1227     }
1228 }
1229
1230 static void
1231 border_image_repeat_value_print (const GValue *value,
1232                                  GString      *string)
1233 {
1234   GtkCssBorderImageRepeat *image_repeat;
1235
1236   image_repeat = g_value_get_boxed (value);
1237
1238   g_string_append (string, border_image_repeat_style_to_string (image_repeat->hrepeat));
1239   if (image_repeat->hrepeat != image_repeat->vrepeat)
1240     {
1241       g_string_append (string, " ");
1242       g_string_append (string, border_image_repeat_style_to_string (image_repeat->vrepeat));
1243     }
1244 }
1245
1246 static gboolean
1247 border_image_value_parse (GtkCssParser *parser,
1248                           GFile *base,
1249                           GValue *value)
1250 {
1251   GValue temp = G_VALUE_INIT;
1252   cairo_pattern_t *pattern = NULL;
1253   GtkGradient *gradient = NULL;
1254   GtkBorder slice, *width = NULL, *parsed_slice;
1255   GtkCssBorderImageRepeat repeat, *parsed_repeat;
1256   gboolean retval = FALSE;
1257   GtkBorderImage *image = NULL;
1258
1259   g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
1260
1261   if (!pattern_value_parse (parser, base, &temp))
1262     return FALSE;
1263
1264   if (G_VALUE_TYPE (&temp) == GTK_TYPE_GRADIENT)
1265     gradient = g_value_dup_boxed (&temp);
1266   else
1267     pattern = g_value_dup_boxed (&temp);
1268
1269   g_value_unset (&temp);
1270   g_value_init (&temp, GTK_TYPE_BORDER);
1271
1272   if (!border_value_parse (parser, base, &temp))
1273     goto out;
1274
1275   parsed_slice = g_value_get_boxed (&temp);
1276   slice = *parsed_slice;
1277
1278   if (_gtk_css_parser_try (parser, "/", TRUE))
1279     {
1280       g_value_unset (&temp);
1281       g_value_init (&temp, GTK_TYPE_BORDER);
1282
1283       if (!border_value_parse (parser, base, &temp))
1284         goto out;
1285
1286       width = g_value_dup_boxed (&temp);
1287     }
1288
1289   g_value_unset (&temp);
1290   g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
1291
1292   if (!border_image_repeat_value_parse (parser, base, &temp))
1293     goto out;
1294
1295   parsed_repeat = g_value_get_boxed (&temp);
1296   repeat = *parsed_repeat;
1297
1298   g_value_unset (&temp);
1299
1300   if (gradient != NULL)
1301     image = _gtk_border_image_new_for_gradient (gradient, &slice, width, &repeat);
1302   else if (pattern != NULL)
1303     image = _gtk_border_image_new (pattern, &slice, width, &repeat);
1304
1305   if (image != NULL)
1306     {
1307       retval = TRUE;
1308       g_value_take_boxed (value, image);
1309     }
1310
1311  out:
1312   if (pattern != NULL)
1313     cairo_pattern_destroy (pattern);
1314
1315   if (gradient != NULL)
1316     gtk_gradient_unref (gradient);
1317
1318   if (width != NULL)
1319     gtk_border_free (width);
1320
1321   return retval;
1322 }
1323
1324 static gboolean 
1325 enum_value_parse (GtkCssParser *parser,
1326                   GFile        *base,
1327                   GValue       *value)
1328 {
1329   GEnumClass *enum_class;
1330   GEnumValue *enum_value;
1331   char *str;
1332
1333   str = _gtk_css_parser_try_ident (parser, TRUE);
1334   if (str == NULL)
1335     {
1336       _gtk_css_parser_error (parser, "Expected an identifier");
1337       return FALSE;
1338     }
1339
1340   enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1341   enum_value = g_enum_get_value_by_nick (enum_class, str);
1342
1343   if (enum_value)
1344     g_value_set_enum (value, enum_value->value);
1345   else
1346     _gtk_css_parser_error (parser,
1347                            "Unknown value '%s' for enum type '%s'",
1348                            str, g_type_name (G_VALUE_TYPE (value)));
1349   
1350   g_type_class_unref (enum_class);
1351   g_free (str);
1352
1353   return enum_value != NULL;
1354 }
1355
1356 static void
1357 enum_value_print (const GValue *value,
1358                   GString      *string)
1359 {
1360   GEnumClass *enum_class;
1361   GEnumValue *enum_value;
1362
1363   enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1364   enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
1365
1366   g_string_append (string, enum_value->value_nick);
1367
1368   g_type_class_unref (enum_class);
1369 }
1370
1371 static gboolean 
1372 flags_value_parse (GtkCssParser *parser,
1373                    GFile        *base,
1374                    GValue       *value)
1375 {
1376   GFlagsClass *flags_class;
1377   GFlagsValue *flag_value;
1378   guint flags = 0;
1379   char *str;
1380
1381   flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1382
1383   do {
1384     str = _gtk_css_parser_try_ident (parser, TRUE);
1385     if (str == NULL)
1386       {
1387         _gtk_css_parser_error (parser, "Expected an identifier");
1388         g_type_class_unref (flags_class);
1389         return FALSE;
1390       }
1391
1392       flag_value = g_flags_get_value_by_nick (flags_class, str);
1393       if (!flag_value)
1394         {
1395           _gtk_css_parser_error (parser,
1396                                  "Unknown flag value '%s' for type '%s'",
1397                                  str, g_type_name (G_VALUE_TYPE (value)));
1398           /* XXX Do we want to return FALSE here? We can get
1399            * forward-compatibility for new values this way
1400            */
1401           g_free (str);
1402           g_type_class_unref (flags_class);
1403           return FALSE;
1404         }
1405
1406       g_free (str);
1407     }
1408   while (_gtk_css_parser_try (parser, ",", FALSE));
1409
1410   g_type_class_unref (flags_class);
1411
1412   g_value_set_enum (value, flags);
1413
1414   return TRUE;
1415 }
1416
1417 static void
1418 flags_value_print (const GValue *value,
1419                    GString      *string)
1420 {
1421   GFlagsClass *flags_class;
1422   guint i, flags;
1423
1424   flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1425   flags = g_value_get_flags (value);
1426
1427   for (i = 0; i < flags_class->n_values; i++)
1428     {
1429       GFlagsValue *flags_value = &flags_class->values[i];
1430
1431       if (flags & flags_value->value)
1432         {
1433           if (string->len != 0)
1434             g_string_append (string, ", ");
1435
1436           g_string_append (string, flags_value->value_nick);
1437         }
1438     }
1439
1440   g_type_class_unref (flags_class);
1441 }
1442
1443 static gboolean 
1444 bindings_value_parse (GtkCssParser *parser,
1445                       GFile        *base,
1446                       GValue       *value)
1447 {
1448   GPtrArray *array;
1449   GtkBindingSet *binding_set;
1450   char *name;
1451
1452   array = g_ptr_array_new ();
1453
1454   do {
1455       name = _gtk_css_parser_try_ident (parser, TRUE);
1456       if (name == NULL)
1457         {
1458           _gtk_css_parser_error (parser, "Not a valid binding name");
1459           g_ptr_array_free (array, TRUE);
1460           return FALSE;
1461         }
1462
1463       binding_set = gtk_binding_set_find (name);
1464
1465       if (!binding_set)
1466         {
1467           _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1468           g_free (name);
1469           continue;
1470         }
1471
1472       g_ptr_array_add (array, binding_set);
1473       g_free (name);
1474     }
1475   while (_gtk_css_parser_try (parser, ",", TRUE));
1476
1477   g_value_take_boxed (value, array);
1478
1479   return TRUE;
1480 }
1481
1482 static void
1483 bindings_value_print (const GValue *value,
1484                       GString      *string)
1485 {
1486   GPtrArray *array;
1487   guint i;
1488
1489   array = g_value_get_boxed (value);
1490
1491   for (i = 0; i < array->len; i++)
1492     {
1493       GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1494
1495       if (i > 0)
1496         g_string_append (string, ", ");
1497       g_string_append (string, binding_set->set_name);
1498     }
1499 }
1500
1501 static gboolean 
1502 border_corner_radius_value_parse (GtkCssParser *parser,
1503                                   GFile        *base,
1504                                   GValue       *value)
1505 {
1506   GtkCssBorderCornerRadius corner;
1507
1508   if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
1509     {
1510       _gtk_css_parser_error (parser, "Expected a number");
1511       return FALSE;
1512     }
1513   else if (corner.horizontal < 0)
1514     goto negative;
1515
1516   if (!_gtk_css_parser_try_double (parser, &corner.vertical))
1517     corner.vertical = corner.horizontal;
1518   else if (corner.vertical < 0)
1519     goto negative;
1520
1521   g_value_set_boxed (value, &corner);
1522   return TRUE;
1523
1524 negative:
1525   _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1526   return FALSE;
1527 }
1528
1529 static void
1530 border_corner_radius_value_print (const GValue *value,
1531                                   GString      *string)
1532 {
1533   GtkCssBorderCornerRadius *corner;
1534
1535   corner = g_value_get_boxed (value);
1536
1537   if (corner == NULL)
1538     {
1539       g_string_append (string, "none");
1540       return;
1541     }
1542
1543   string_append_double (string, corner->horizontal);
1544   if (corner->horizontal != corner->vertical)
1545     {
1546       g_string_append_c (string, ' ');
1547       string_append_double (string, corner->vertical);
1548     }
1549 }
1550
1551 static gboolean 
1552 border_radius_value_parse (GtkCssParser *parser,
1553                            GFile        *base,
1554                            GValue       *value)
1555 {
1556   GtkCssBorderRadius border;
1557
1558   if (!_gtk_css_parser_try_double (parser, &border.top_left.horizontal))
1559     {
1560       _gtk_css_parser_error (parser, "Expected a number");
1561       return FALSE;
1562     }
1563   else if (border.top_left.horizontal < 0)
1564     goto negative;
1565
1566   if (_gtk_css_parser_try_double (parser, &border.top_right.horizontal))
1567     {
1568       if (border.top_right.horizontal < 0)
1569         goto negative;
1570       if (_gtk_css_parser_try_double (parser, &border.bottom_right.horizontal))
1571         {
1572           if (border.bottom_right.horizontal < 0)
1573             goto negative;
1574           if (!_gtk_css_parser_try_double (parser, &border.bottom_left.horizontal))
1575             border.bottom_left.horizontal = border.top_right.horizontal;
1576           else if (border.bottom_left.horizontal < 0)
1577             goto negative;
1578         }
1579       else
1580         {
1581           border.bottom_right.horizontal = border.top_left.horizontal;
1582           border.bottom_left.horizontal = border.top_right.horizontal;
1583         }
1584     }
1585   else
1586     {
1587       border.top_right.horizontal = border.top_left.horizontal;
1588       border.bottom_right.horizontal = border.top_left.horizontal;
1589       border.bottom_left.horizontal = border.top_left.horizontal;
1590     }
1591
1592   if (_gtk_css_parser_try (parser, "/", TRUE))
1593     {
1594       if (!_gtk_css_parser_try_double (parser, &border.top_left.vertical))
1595         {
1596           _gtk_css_parser_error (parser, "Expected a number");
1597           return FALSE;
1598         }
1599       else if (border.top_left.vertical < 0)
1600         goto negative;
1601
1602       if (_gtk_css_parser_try_double (parser, &border.top_right.vertical))
1603         {
1604           if (border.top_right.vertical < 0)
1605             goto negative;
1606           if (_gtk_css_parser_try_double (parser, &border.bottom_right.vertical))
1607             {
1608               if (border.bottom_right.vertical < 0)
1609                 goto negative;
1610               if (!_gtk_css_parser_try_double (parser, &border.bottom_left.vertical))
1611                 border.bottom_left.vertical = border.top_right.vertical;
1612               else if (border.bottom_left.vertical < 0)
1613                 goto negative;
1614             }
1615           else
1616             {
1617               border.bottom_right.vertical = border.top_left.vertical;
1618               border.bottom_left.vertical = border.top_right.vertical;
1619             }
1620         }
1621       else
1622         {
1623           border.top_right.vertical = border.top_left.vertical;
1624           border.bottom_right.vertical = border.top_left.vertical;
1625           border.bottom_left.vertical = border.top_left.vertical;
1626         }
1627     }
1628   else
1629     {
1630       border.top_left.vertical = border.top_left.horizontal;
1631       border.top_right.vertical = border.top_right.horizontal;
1632       border.bottom_right.vertical = border.bottom_right.horizontal;
1633       border.bottom_left.vertical = border.bottom_left.horizontal;
1634     }
1635
1636   /* border-radius is an int property for backwards-compat reasons */
1637   g_value_unset (value);
1638   g_value_init (value, GTK_TYPE_CSS_BORDER_RADIUS);
1639   g_value_set_boxed (value, &border);
1640
1641   return TRUE;
1642
1643 negative:
1644   _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1645   return FALSE;
1646 }
1647
1648 static void
1649 border_radius_value_print (const GValue *value,
1650                            GString      *string)
1651 {
1652   GtkCssBorderRadius *border;
1653
1654   border = g_value_get_boxed (value);
1655
1656   if (border == NULL)
1657     {
1658       g_string_append (string, "none");
1659       return;
1660     }
1661
1662   string_append_double (string, border->top_left.horizontal);
1663   if (border->top_left.horizontal != border->top_right.horizontal ||
1664       border->top_left.horizontal != border->bottom_right.horizontal ||
1665       border->top_left.horizontal != border->bottom_left.horizontal)
1666     {
1667       g_string_append_c (string, ' ');
1668       string_append_double (string, border->top_right.horizontal);
1669       if (border->top_left.horizontal != border->bottom_right.horizontal ||
1670           border->top_right.horizontal != border->bottom_left.horizontal)
1671         {
1672           g_string_append_c (string, ' ');
1673           string_append_double (string, border->bottom_right.horizontal);
1674           if (border->top_right.horizontal != border->bottom_left.horizontal)
1675             {
1676               g_string_append_c (string, ' ');
1677               string_append_double (string, border->bottom_left.horizontal);
1678             }
1679         }
1680     }
1681
1682   if (border->top_left.horizontal != border->top_left.vertical ||
1683       border->top_right.horizontal != border->top_right.vertical ||
1684       border->bottom_right.horizontal != border->bottom_right.vertical ||
1685       border->bottom_left.horizontal != border->bottom_left.vertical)
1686     {
1687       g_string_append (string, " / ");
1688       string_append_double (string, border->top_left.vertical);
1689       if (border->top_left.vertical != border->top_right.vertical ||
1690           border->top_left.vertical != border->bottom_right.vertical ||
1691           border->top_left.vertical != border->bottom_left.vertical)
1692         {
1693           g_string_append_c (string, ' ');
1694           string_append_double (string, border->top_right.vertical);
1695           if (border->top_left.vertical != border->bottom_right.vertical ||
1696               border->top_right.vertical != border->bottom_left.vertical)
1697             {
1698               g_string_append_c (string, ' ');
1699               string_append_double (string, border->bottom_right.vertical);
1700               if (border->top_right.vertical != border->bottom_left.vertical)
1701                 {
1702                   g_string_append_c (string, ' ');
1703                   string_append_double (string, border->bottom_left.vertical);
1704                 }
1705             }
1706         }
1707
1708     }
1709 }
1710
1711 static gboolean 
1712 transparent_color_value_parse (GtkCssParser *parser,
1713                                GFile        *base,
1714                                GValue       *value)
1715 {
1716   if (_gtk_css_parser_try (parser, "transparent", TRUE))
1717     {
1718       GdkRGBA transparent = { 0, 0, 0, 0 };
1719           
1720       g_value_set_boxed (value, &transparent);
1721
1722       return TRUE;
1723     }
1724
1725   return rgba_value_parse (parser, base, value);
1726 }
1727
1728 static gboolean 
1729 border_color_shorthand_value_parse (GtkCssParser *parser,
1730                                     GFile        *base,
1731                                     GValue       *value)
1732 {
1733   GtkSymbolicColor *symbolic;
1734   GPtrArray *array;
1735
1736   array = g_ptr_array_new_with_free_func ((GDestroyNotify) gtk_symbolic_color_unref);
1737
1738   do
1739     {
1740       if (_gtk_css_parser_try (parser, "transparent", TRUE))
1741         {
1742           GdkRGBA transparent = { 0, 0, 0, 0 };
1743           
1744           symbolic = gtk_symbolic_color_new_literal (&transparent);
1745         }
1746       else
1747         {
1748           symbolic = _gtk_css_parser_read_symbolic_color (parser);
1749       
1750           if (symbolic == NULL)
1751             return FALSE;
1752         }
1753       
1754       g_ptr_array_add (array, symbolic);
1755     }
1756   while (array->len < 4 && 
1757          !_gtk_css_parser_is_eof (parser) &&
1758          !_gtk_css_parser_begins_with (parser, ';') &&
1759          !_gtk_css_parser_begins_with (parser, '}'));
1760
1761   switch (array->len)
1762     {
1763       default:
1764         g_assert_not_reached ();
1765         break;
1766       case 1:
1767         g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1768         /* fall through */
1769       case 2:
1770         g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1771         /* fall through */
1772       case 3:
1773         g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 1)));
1774         /* fall through */
1775       case 4:
1776         break;
1777     }
1778
1779   g_value_unset (value);
1780   g_value_init (value, G_TYPE_PTR_ARRAY);
1781   g_value_take_boxed (value, array);
1782
1783   return TRUE;
1784 }
1785
1786 /*** PACKING ***/
1787
1788 static GParameter *
1789 unpack_border (const GValue *value,
1790                guint        *n_params,
1791                const char   *top,
1792                const char   *left,
1793                const char   *bottom,
1794                const char   *right)
1795 {
1796   GParameter *parameter = g_new0 (GParameter, 4);
1797   GtkBorder *border = g_value_get_boxed (value);
1798
1799   parameter[0].name = top;
1800   g_value_init (&parameter[0].value, G_TYPE_INT);
1801   g_value_set_int (&parameter[0].value, border->top);
1802   parameter[1].name = left;
1803   g_value_init (&parameter[1].value, G_TYPE_INT);
1804   g_value_set_int (&parameter[1].value, border->left);
1805   parameter[2].name = bottom;
1806   g_value_init (&parameter[2].value, G_TYPE_INT);
1807   g_value_set_int (&parameter[2].value, border->bottom);
1808   parameter[3].name = right;
1809   g_value_init (&parameter[3].value, G_TYPE_INT);
1810   g_value_set_int (&parameter[3].value, border->right);
1811
1812   *n_params = 4;
1813   return parameter;
1814 }
1815
1816 static void
1817 pack_border (GValue             *value,
1818              GtkStyleProperties *props,
1819              GtkStateFlags       state,
1820              const char         *top,
1821              const char         *left,
1822              const char         *bottom,
1823              const char         *right)
1824 {
1825   GtkBorder border;
1826   int t, l, b, r;
1827
1828   gtk_style_properties_get (props,
1829                             state,
1830                             top, &t,
1831                             left, &l,
1832                             bottom, &b,
1833                             right, &r,
1834                             NULL);
1835
1836   border.top = t;
1837   border.left = l;
1838   border.bottom = b;
1839   border.right = r;
1840
1841   g_value_set_boxed (value, &border);
1842 }
1843
1844 static GParameter *
1845 unpack_border_width (const GValue *value,
1846                      guint        *n_params)
1847 {
1848   return unpack_border (value, n_params,
1849                         "border-top-width", "border-left-width",
1850                         "border-bottom-width", "border-right-width");
1851 }
1852
1853 static void
1854 pack_border_width (GValue             *value,
1855                    GtkStyleProperties *props,
1856                    GtkStateFlags       state)
1857 {
1858   pack_border (value, props, state,
1859                "border-top-width", "border-left-width",
1860                "border-bottom-width", "border-right-width");
1861 }
1862
1863 static GParameter *
1864 unpack_padding (const GValue *value,
1865                 guint        *n_params)
1866 {
1867   return unpack_border (value, n_params,
1868                         "padding-top", "padding-left",
1869                         "padding-bottom", "padding-right");
1870 }
1871
1872 static void
1873 pack_padding (GValue             *value,
1874               GtkStyleProperties *props,
1875               GtkStateFlags       state)
1876 {
1877   pack_border (value, props, state,
1878                "padding-top", "padding-left",
1879                "padding-bottom", "padding-right");
1880 }
1881
1882 static GParameter *
1883 unpack_margin (const GValue *value,
1884                guint        *n_params)
1885 {
1886   return unpack_border (value, n_params,
1887                         "margin-top", "margin-left",
1888                         "margin-bottom", "margin-right");
1889 }
1890
1891 static void
1892 pack_margin (GValue             *value,
1893              GtkStyleProperties *props,
1894              GtkStateFlags       state)
1895 {
1896   pack_border (value, props, state,
1897                "margin-top", "margin-left",
1898                "margin-bottom", "margin-right");
1899 }
1900
1901 static GParameter *
1902 unpack_border_radius (const GValue *value,
1903                       guint        *n_params)
1904 {
1905   GParameter *parameter = g_new0 (GParameter, 4);
1906   GtkCssBorderRadius *border;
1907   
1908   if (G_VALUE_HOLDS_BOXED (value))
1909     border = g_value_get_boxed (value);
1910   else
1911     border = NULL;
1912
1913   parameter[0].name = "border-top-left-radius";
1914   g_value_init (&parameter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1915   parameter[1].name = "border-top-right-radius";
1916   g_value_init (&parameter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1917   parameter[2].name = "border-bottom-right-radius";
1918   g_value_init (&parameter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1919   parameter[3].name = "border-bottom-left-radius";
1920   g_value_init (&parameter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1921   if (border)
1922     {
1923       g_value_set_boxed (&parameter[0].value, &border->top_left);
1924       g_value_set_boxed (&parameter[1].value, &border->top_right);
1925       g_value_set_boxed (&parameter[2].value, &border->bottom_right);
1926       g_value_set_boxed (&parameter[3].value, &border->bottom_left);
1927     }
1928
1929   *n_params = 4;
1930   return parameter;
1931 }
1932
1933 static void
1934 pack_border_radius (GValue             *value,
1935                     GtkStyleProperties *props,
1936                     GtkStateFlags       state)
1937 {
1938   GtkCssBorderCornerRadius *top_left;
1939
1940   /* NB: We are an int property, so we have to resolve to an int here.
1941    * So we just resolve to an int. We pick one and stick to it.
1942    * Lesson learned: Don't query border-radius shorthand, query the 
1943    * real properties instead. */
1944   gtk_style_properties_get (props,
1945                             state,
1946                             "border-top-left-radius", &top_left,
1947                             NULL);
1948
1949   if (top_left)
1950     g_value_set_int (value, top_left->horizontal);
1951
1952   g_free (top_left);
1953 }
1954
1955 static GParameter *
1956 unpack_font_description (const GValue *value,
1957                          guint        *n_params)
1958 {
1959   GParameter *parameter = g_new0 (GParameter, 5);
1960   PangoFontDescription *description;
1961   PangoFontMask mask;
1962   guint n;
1963   
1964   /* For backwards compat, we only unpack values that are indeed set.
1965    * For strict CSS conformance we need to unpack all of them.
1966    * Note that we do set all of them in the parse function, so it
1967    * will not have effects when parsing CSS files. It will though
1968    * for custom style providers.
1969    */
1970
1971   description = g_value_get_boxed (value);
1972   n = 0;
1973
1974   if (description)
1975     mask = pango_font_description_get_set_fields (description);
1976   else
1977     mask = 0;
1978
1979   if (mask & PANGO_FONT_MASK_FAMILY)
1980     {
1981       GPtrArray *strv = g_ptr_array_new ();
1982
1983       g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
1984       g_ptr_array_add (strv, NULL);
1985       parameter[n].name = "font-family";
1986       g_value_init (&parameter[n].value, G_TYPE_STRV);
1987       g_value_take_boxed (&parameter[n].value,
1988                           g_ptr_array_free (strv, FALSE));
1989       n++;
1990     }
1991
1992   if (mask & PANGO_FONT_MASK_STYLE)
1993     {
1994       parameter[n].name = "font-style";
1995       g_value_init (&parameter[n].value, PANGO_TYPE_STYLE);
1996       g_value_set_enum (&parameter[n].value,
1997                         pango_font_description_get_style (description));
1998       n++;
1999     }
2000
2001   if (mask & PANGO_FONT_MASK_VARIANT)
2002     {
2003       parameter[n].name = "font-variant";
2004       g_value_init (&parameter[n].value, PANGO_TYPE_VARIANT);
2005       g_value_set_enum (&parameter[n].value,
2006                         pango_font_description_get_variant (description));
2007       n++;
2008     }
2009
2010   if (mask & PANGO_FONT_MASK_WEIGHT)
2011     {
2012       parameter[n].name = "font-weight";
2013       g_value_init (&parameter[n].value, PANGO_TYPE_WEIGHT);
2014       g_value_set_enum (&parameter[n].value,
2015                         pango_font_description_get_weight (description));
2016       n++;
2017     }
2018
2019   if (mask & PANGO_FONT_MASK_SIZE)
2020     {
2021       parameter[n].name = "font-size";
2022       g_value_init (&parameter[n].value, G_TYPE_DOUBLE);
2023       g_value_set_double (&parameter[n].value,
2024                           (double) pango_font_description_get_size (description) / PANGO_SCALE);
2025       n++;
2026     }
2027
2028   *n_params = n;
2029
2030   return parameter;
2031 }
2032
2033 static void
2034 pack_font_description (GValue             *value,
2035                        GtkStyleProperties *props,
2036                        GtkStateFlags       state)
2037 {
2038   PangoFontDescription *description;
2039   char **families;
2040   PangoStyle style;
2041   PangoVariant variant;
2042   PangoWeight weight;
2043   double size;
2044
2045   gtk_style_properties_get (props,
2046                             state,
2047                             "font-family", &families,
2048                             "font-style", &style,
2049                             "font-variant", &variant,
2050                             "font-weight", &weight,
2051                             "font-size", &size,
2052                             NULL);
2053
2054   description = pango_font_description_new ();
2055   /* xxx: Can we set all the families here somehow? */
2056   if (families)
2057     pango_font_description_set_family (description, families[0]);
2058   pango_font_description_set_size (description, round (size * PANGO_SCALE));
2059   pango_font_description_set_style (description, style);
2060   pango_font_description_set_variant (description, variant);
2061   pango_font_description_set_weight (description, weight);
2062
2063   g_strfreev (families);
2064
2065   g_value_take_boxed (value, description);
2066 }
2067
2068 static GParameter *
2069 unpack_border_color (const GValue *value,
2070                      guint        *n_params)
2071 {
2072   GParameter *parameter = g_new0 (GParameter, 4);
2073   GType type;
2074   
2075   type = G_VALUE_TYPE (value);
2076   if (type == G_TYPE_PTR_ARRAY)
2077     type = GTK_TYPE_SYMBOLIC_COLOR;
2078
2079   parameter[0].name = "border-top-color";
2080   g_value_init (&parameter[0].value, type);
2081   parameter[1].name = "border-right-color";
2082   g_value_init (&parameter[1].value, type);
2083   parameter[2].name = "border-bottom-color";
2084   g_value_init (&parameter[2].value, type);
2085   parameter[3].name = "border-left-color";
2086   g_value_init (&parameter[3].value, type);
2087
2088   if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
2089     {
2090       GPtrArray *array = g_value_get_boxed (value);
2091       guint i;
2092
2093       for (i = 0; i < 4; i++)
2094         g_value_set_boxed (&parameter[i].value, g_ptr_array_index (array, i));
2095     }
2096   else
2097     {
2098       /* can be RGBA or symbolic color */
2099       gpointer p = g_value_get_boxed (value);
2100
2101       g_value_set_boxed (&parameter[0].value, p);
2102       g_value_set_boxed (&parameter[1].value, p);
2103       g_value_set_boxed (&parameter[2].value, p);
2104       g_value_set_boxed (&parameter[3].value, p);
2105     }
2106
2107   *n_params = 4;
2108   return parameter;
2109 }
2110
2111 static void
2112 pack_border_color (GValue             *value,
2113                    GtkStyleProperties *props,
2114                    GtkStateFlags       state)
2115 {
2116   /* NB: We are a color property, so we have to resolve to a color here.
2117    * So we just resolve to a color. We pick one and stick to it.
2118    * Lesson learned: Don't query border-color shorthand, query the 
2119    * real properties instead. */
2120   g_value_unset (value);
2121   gtk_style_properties_get_property (props, "border-top-color", state, value);
2122 }
2123
2124 /*** UNSET FUNCS ***/
2125
2126 static void
2127 unset_font_description (GtkStyleProperties *props,
2128                         GtkStateFlags       state)
2129 {
2130   gtk_style_properties_unset_property (props, "font-family", state);
2131   gtk_style_properties_unset_property (props, "font-style", state);
2132   gtk_style_properties_unset_property (props, "font-variant", state);
2133   gtk_style_properties_unset_property (props, "font-weight", state);
2134   gtk_style_properties_unset_property (props, "font-size", state);
2135 }
2136
2137 static void
2138 unset_margin (GtkStyleProperties *props,
2139               GtkStateFlags       state)
2140 {
2141   gtk_style_properties_unset_property (props, "margin-top", state);
2142   gtk_style_properties_unset_property (props, "margin-right", state);
2143   gtk_style_properties_unset_property (props, "margin-bottom", state);
2144   gtk_style_properties_unset_property (props, "margin-left", state);
2145 }
2146
2147 static void
2148 unset_padding (GtkStyleProperties *props,
2149                GtkStateFlags       state)
2150 {
2151   gtk_style_properties_unset_property (props, "padding-top", state);
2152   gtk_style_properties_unset_property (props, "padding-right", state);
2153   gtk_style_properties_unset_property (props, "padding-bottom", state);
2154   gtk_style_properties_unset_property (props, "padding-left", state);
2155 }
2156
2157 static void
2158 unset_border_width (GtkStyleProperties *props,
2159                     GtkStateFlags       state)
2160 {
2161   gtk_style_properties_unset_property (props, "border-top-width", state);
2162   gtk_style_properties_unset_property (props, "border-right-width", state);
2163   gtk_style_properties_unset_property (props, "border-bottom-width", state);
2164   gtk_style_properties_unset_property (props, "border-left-width", state);
2165 }
2166
2167 static void
2168 unset_border_radius (GtkStyleProperties *props,
2169                      GtkStateFlags       state)
2170 {
2171   gtk_style_properties_unset_property (props, "border-top-right-radius", state);
2172   gtk_style_properties_unset_property (props, "border-bottom-right-radius", state);
2173   gtk_style_properties_unset_property (props, "border-bottom-left-radius", state);
2174   gtk_style_properties_unset_property (props, "border-top-left-radius", state);
2175 }
2176
2177 static void
2178 unset_border_color (GtkStyleProperties *props,
2179                     GtkStateFlags       state)
2180 {
2181   gtk_style_properties_unset_property (props, "border-top-color", state);
2182   gtk_style_properties_unset_property (props, "border-right-color", state);
2183   gtk_style_properties_unset_property (props, "border-bottom-color", state);
2184   gtk_style_properties_unset_property (props, "border-left-color", state);
2185 }
2186
2187 static void
2188 unset_border_image (GtkStyleProperties *props,
2189                     GtkStateFlags       state)
2190 {
2191   gtk_style_properties_unset_property (props, "border-image-source", state);
2192   gtk_style_properties_unset_property (props, "border-image-slice", state);
2193   gtk_style_properties_unset_property (props, "border-image-repeat", state);
2194   gtk_style_properties_unset_property (props, "border-image-width", state);
2195 }
2196
2197 /*** default values ***/
2198
2199 static void
2200 border_image_width_default_value (GtkStyleProperties *props,
2201                                   GtkStateFlags       state,
2202                                   GValue             *value)
2203 {
2204 }
2205
2206 static void
2207 background_color_default_value (GtkStyleProperties *props,
2208                                 GtkStateFlags       state,
2209                                 GValue             *value)
2210 {
2211   GdkRGBA transparent_black = { 0, 0, 0, 0 };
2212
2213   g_value_set_boxed (value, &transparent_black);
2214 }
2215
2216 static void
2217 border_color_default_value (GtkStyleProperties *props,
2218                             GtkStateFlags       state,
2219                             GValue             *value)
2220 {
2221   g_value_unset (value);
2222   gtk_style_properties_get_property (props, "color", state, value);
2223 }
2224
2225 /*** API ***/
2226
2227 static void
2228 css_string_funcs_init (void)
2229 {
2230   if (G_LIKELY (parse_funcs != NULL))
2231     return;
2232
2233   parse_funcs = g_hash_table_new (NULL, NULL);
2234   print_funcs = g_hash_table_new (NULL, NULL);
2235
2236   register_conversion_function (GDK_TYPE_RGBA,
2237                                 rgba_value_parse,
2238                                 rgba_value_print);
2239   register_conversion_function (GDK_TYPE_COLOR,
2240                                 color_value_parse,
2241                                 color_value_print);
2242   register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
2243                                 symbolic_color_value_parse,
2244                                 symbolic_color_value_print);
2245   register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
2246                                 font_description_value_parse,
2247                                 font_description_value_print);
2248   register_conversion_function (G_TYPE_BOOLEAN,
2249                                 boolean_value_parse,
2250                                 boolean_value_print);
2251   register_conversion_function (G_TYPE_INT,
2252                                 int_value_parse,
2253                                 int_value_print);
2254   register_conversion_function (G_TYPE_UINT,
2255                                 uint_value_parse,
2256                                 uint_value_print);
2257   register_conversion_function (G_TYPE_DOUBLE,
2258                                 double_value_parse,
2259                                 double_value_print);
2260   register_conversion_function (G_TYPE_FLOAT,
2261                                 float_value_parse,
2262                                 float_value_print);
2263   register_conversion_function (G_TYPE_STRING,
2264                                 string_value_parse,
2265                                 string_value_print);
2266   register_conversion_function (GTK_TYPE_THEMING_ENGINE,
2267                                 theming_engine_value_parse,
2268                                 theming_engine_value_print);
2269   register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
2270                                 animation_description_value_parse,
2271                                 animation_description_value_print);
2272   register_conversion_function (GTK_TYPE_BORDER,
2273                                 border_value_parse,
2274                                 border_value_print);
2275   register_conversion_function (GTK_TYPE_GRADIENT,
2276                                 gradient_value_parse,
2277                                 gradient_value_print);
2278   register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
2279                                 pattern_value_parse,
2280                                 pattern_value_print);
2281   register_conversion_function (GTK_TYPE_BORDER_IMAGE,
2282                                 border_image_value_parse,
2283                                 NULL);
2284   register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
2285                                 border_image_repeat_value_parse,
2286                                 border_image_repeat_value_print);
2287   register_conversion_function (GTK_TYPE_SHADOW,
2288                                 shadow_value_parse,
2289                                 shadow_value_print);
2290   register_conversion_function (G_TYPE_ENUM,
2291                                 enum_value_parse,
2292                                 enum_value_print);
2293   register_conversion_function (G_TYPE_FLAGS,
2294                                 flags_value_parse,
2295                                 flags_value_print);
2296   register_conversion_function (GTK_TYPE_CSS_BACKGROUND_REPEAT,
2297                                 background_repeat_value_parse,
2298                                 background_repeat_value_print);
2299 }
2300
2301 gboolean
2302 _gtk_style_property_parse_value (const GtkStyleProperty *property,
2303                                  GValue                 *value,
2304                                  GtkCssParser           *parser,
2305                                  GFile                  *base)
2306 {
2307   GtkStyleParseFunc func;
2308
2309   g_return_val_if_fail (value != NULL, FALSE);
2310   g_return_val_if_fail (parser != NULL, FALSE);
2311
2312   css_string_funcs_init ();
2313
2314   if (property)
2315     {
2316       if (_gtk_css_parser_try (parser, "none", TRUE))
2317         {
2318           /* Insert the default value, so it has an opportunity
2319            * to override other style providers when merged
2320            */
2321           g_param_value_set_default (property->pspec, value);
2322           return TRUE;
2323         }
2324       else if (property->property_parse_func)
2325         {
2326           GError *error = NULL;
2327           char *value_str;
2328           gboolean success;
2329           
2330           value_str = _gtk_css_parser_read_value (parser);
2331           if (value_str == NULL)
2332             return FALSE;
2333           
2334           success = (*property->property_parse_func) (value_str, value, &error);
2335
2336           g_free (value_str);
2337
2338           return success;
2339         }
2340
2341       func = property->parse_func;
2342     }
2343   else
2344     func = NULL;
2345
2346   if (func == NULL)
2347     func = g_hash_table_lookup (parse_funcs,
2348                                 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2349   if (func == NULL)
2350     func = g_hash_table_lookup (parse_funcs,
2351                                 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2352
2353   if (func == NULL)
2354     {
2355       _gtk_css_parser_error (parser,
2356                              "Cannot convert to type '%s'",
2357                              g_type_name (G_VALUE_TYPE (value)));
2358       return FALSE;
2359     }
2360
2361   return (*func) (parser, base, value);
2362 }
2363
2364 void
2365 _gtk_style_property_print_value (const GtkStyleProperty *property,
2366                                  const GValue           *value,
2367                                  GString                *string)
2368 {
2369   GtkStylePrintFunc func;
2370
2371   css_string_funcs_init ();
2372
2373   if (property)
2374     func = property->print_func;
2375   else
2376     func = NULL;
2377
2378   if (func == NULL)
2379     func = g_hash_table_lookup (print_funcs,
2380                                 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2381   if (func == NULL)
2382     func = g_hash_table_lookup (print_funcs,
2383                                 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2384
2385   if (func == NULL)
2386     {
2387       char *s = g_strdup_value_contents (value);
2388       g_string_append (string, s);
2389       g_free (s);
2390       return;
2391     }
2392   
2393   func (value, string);
2394 }
2395
2396 void
2397 _gtk_style_property_default_value (const GtkStyleProperty *property,
2398                                    GtkStyleProperties     *properties,
2399                                    GtkStateFlags           state,
2400                                    GValue                 *value)
2401 {
2402   if (property->default_value_func)
2403     property->default_value_func (properties, state, value);
2404   else if (property->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
2405     g_value_set_object (value, gtk_theming_engine_load (NULL));
2406   else if (property->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
2407     g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
2408   else if (property->pspec->value_type == GDK_TYPE_RGBA)
2409     {
2410       GdkRGBA color;
2411       gdk_rgba_parse (&color, "pink");
2412       g_value_set_boxed (value, &color);
2413     }
2414   else if (property->pspec->value_type == GTK_TYPE_BORDER)
2415     {
2416       g_value_take_boxed (value, gtk_border_new ());
2417     }
2418   else
2419     g_param_value_set_default (property->pspec, value);
2420 }
2421
2422 gboolean
2423 _gtk_style_property_is_inherit (const GtkStyleProperty *property)
2424 {
2425   g_return_val_if_fail (property != NULL, FALSE);
2426
2427   return property->flags & GTK_STYLE_PROPERTY_INHERIT ? TRUE : FALSE;
2428 }
2429
2430 static gboolean
2431 resolve_color (GtkStyleProperties *props,
2432                GValue             *value)
2433 {
2434   GdkRGBA color;
2435
2436   /* Resolve symbolic color to GdkRGBA */
2437   if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
2438     return FALSE;
2439
2440   /* Store it back, this is where GdkRGBA caching happens */
2441   g_value_unset (value);
2442   g_value_init (value, GDK_TYPE_RGBA);
2443   g_value_set_boxed (value, &color);
2444
2445   return TRUE;
2446 }
2447
2448 static gboolean
2449 resolve_color_rgb (GtkStyleProperties *props,
2450                    GValue             *value)
2451 {
2452   GdkColor color = { 0 };
2453   GdkRGBA rgba;
2454
2455   if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
2456     return FALSE;
2457
2458   color.red = rgba.red * 65535. + 0.5;
2459   color.green = rgba.green * 65535. + 0.5;
2460   color.blue = rgba.blue * 65535. + 0.5;
2461
2462   g_value_unset (value);
2463   g_value_init (value, GDK_TYPE_COLOR);
2464   g_value_set_boxed (value, &color);
2465
2466   return TRUE;
2467 }
2468
2469 static gboolean
2470 resolve_gradient (GtkStyleProperties *props,
2471                   GValue             *value)
2472 {
2473   cairo_pattern_t *gradient;
2474
2475   if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
2476     return FALSE;
2477
2478   /* Store it back, this is where cairo_pattern_t caching happens */
2479   g_value_unset (value);
2480   g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
2481   g_value_take_boxed (value, gradient);
2482
2483   return TRUE;
2484 }
2485
2486 static gboolean
2487 resolve_shadow (GtkStyleProperties *props,
2488                 GValue *value)
2489 {
2490   GtkShadow *resolved, *base;
2491
2492   base = g_value_get_boxed (value);
2493
2494   if (base == NULL)
2495     return TRUE;
2496   
2497   if (_gtk_shadow_get_resolved (base))
2498     return TRUE;
2499
2500   resolved = _gtk_shadow_resolve (base, props);
2501   if (resolved == NULL)
2502     return FALSE;
2503
2504   g_value_take_boxed (value, resolved);
2505
2506   return TRUE;
2507 }
2508
2509 void
2510 _gtk_style_property_resolve (const GtkStyleProperty *property,
2511                              GtkStyleProperties     *props,
2512                              GtkStateFlags           state,
2513                              GValue                 *val)
2514 {
2515   if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
2516     {
2517       if (property->pspec->value_type == GDK_TYPE_RGBA)
2518         {
2519           if (resolve_color (props, val))
2520             return;
2521         }
2522       else if (property->pspec->value_type == GDK_TYPE_COLOR)
2523         {
2524           if (resolve_color_rgb (props, val))
2525             return;
2526         }
2527       
2528       g_value_unset (val);
2529       g_value_init (val, property->pspec->value_type);
2530       _gtk_style_property_default_value (property, props, state, val);
2531     }
2532   else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
2533     {
2534       if (g_value_get_boxed (val) == NULL)
2535         _gtk_style_property_default_value (property, props, state, val);
2536     }
2537   else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
2538     {
2539       g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
2540
2541       if (!resolve_gradient (props, val))
2542         {
2543           g_value_unset (val);
2544           g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
2545           _gtk_style_property_default_value (property, props, state, val);
2546         }
2547     }
2548   else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
2549     {
2550       if (!resolve_shadow (props, val))
2551         _gtk_style_property_default_value (property, props, state, val);
2552     }
2553 }
2554
2555 gboolean
2556 _gtk_style_property_is_shorthand  (const GtkStyleProperty *property)
2557 {
2558   g_return_val_if_fail (property != NULL, FALSE);
2559
2560   return property->pack_func != NULL;
2561 }
2562
2563 GParameter *
2564 _gtk_style_property_unpack (const GtkStyleProperty *property,
2565                             const GValue           *value,
2566                             guint                  *n_params)
2567 {
2568   g_return_val_if_fail (property != NULL, NULL);
2569   g_return_val_if_fail (property->unpack_func != NULL, NULL);
2570   g_return_val_if_fail (value != NULL, NULL);
2571   g_return_val_if_fail (n_params != NULL, NULL);
2572
2573   return property->unpack_func (value, n_params);
2574 }
2575
2576 void
2577 _gtk_style_property_pack (const GtkStyleProperty *property,
2578                           GtkStyleProperties     *props,
2579                           GtkStateFlags           state,
2580                           GValue                 *value)
2581 {
2582   g_return_if_fail (property != NULL);
2583   g_return_if_fail (property->pack_func != NULL);
2584   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2585   g_return_if_fail (G_IS_VALUE (value));
2586
2587   property->pack_func (value, props, state);
2588 }
2589
2590 static void
2591 gtk_style_property_init (void)
2592 {
2593   if (G_LIKELY (properties))
2594     return;
2595
2596   /* stuff is never freed, so no need for free functions */
2597   properties = g_hash_table_new (g_str_hash, g_str_equal);
2598
2599   /* note that gtk_style_properties_register_property() calls this function,
2600    * so make sure we're sanely inited to avoid infloops */
2601
2602   _gtk_style_property_register           (g_param_spec_boxed ("color",
2603                                           "Foreground color",
2604                                           "Foreground color",
2605                                           GDK_TYPE_RGBA, 0),
2606                                           GTK_STYLE_PROPERTY_INHERIT,
2607                                           NULL,
2608                                           NULL,
2609                                           NULL,
2610                                           NULL,
2611                                           NULL,
2612                                           NULL,
2613                                           NULL);
2614   _gtk_style_property_register           (g_param_spec_boxed ("background-color",
2615                                           "Background color",
2616                                           "Background color",
2617                                           GDK_TYPE_RGBA, 0),
2618                                           0,
2619                                           NULL,
2620                                           NULL,
2621                                           NULL,
2622                                           transparent_color_value_parse,
2623                                           NULL,
2624                                           background_color_default_value,
2625                                           NULL);
2626
2627   _gtk_style_property_register           (g_param_spec_boxed ("font-family",
2628                                                               "Font family",
2629                                                               "Font family",
2630                                                               G_TYPE_STRV, 0),
2631                                           GTK_STYLE_PROPERTY_INHERIT,
2632                                           NULL,
2633                                           NULL,
2634                                           NULL,
2635                                           font_family_parse,
2636                                           font_family_value_print,
2637                                           NULL,
2638                                           NULL);
2639   _gtk_style_property_register           (g_param_spec_enum ("font-style",
2640                                                              "Font style",
2641                                                              "Font style",
2642                                                              PANGO_TYPE_STYLE,
2643                                                              PANGO_STYLE_NORMAL, 0),
2644                                           GTK_STYLE_PROPERTY_INHERIT,
2645                                           NULL,
2646                                           NULL,
2647                                           NULL,
2648                                           NULL,
2649                                           NULL,
2650                                           NULL,
2651                                           NULL);
2652   _gtk_style_property_register           (g_param_spec_enum ("font-variant",
2653                                                              "Font variant",
2654                                                              "Font variant",
2655                                                              PANGO_TYPE_VARIANT,
2656                                                              PANGO_VARIANT_NORMAL, 0),
2657                                           GTK_STYLE_PROPERTY_INHERIT,
2658                                           NULL,
2659                                           NULL,
2660                                           NULL,
2661                                           NULL,
2662                                           NULL,
2663                                           NULL,
2664                                           NULL);
2665   /* xxx: need to parse this properly, ie parse the numbers */
2666   _gtk_style_property_register           (g_param_spec_enum ("font-weight",
2667                                                              "Font weight",
2668                                                              "Font weight",
2669                                                              PANGO_TYPE_WEIGHT,
2670                                                              PANGO_WEIGHT_NORMAL, 0),
2671                                           GTK_STYLE_PROPERTY_INHERIT,
2672                                           NULL,
2673                                           NULL,
2674                                           NULL,
2675                                           NULL,
2676                                           NULL,
2677                                           NULL,
2678                                           NULL);
2679   _gtk_style_property_register           (g_param_spec_double ("font-size",
2680                                                                "Font size",
2681                                                                "Font size",
2682                                                                0, G_MAXDOUBLE, 0, 0),
2683                                           GTK_STYLE_PROPERTY_INHERIT,
2684                                           NULL,
2685                                           NULL,
2686                                           NULL,
2687                                           NULL,
2688                                           NULL,
2689                                           NULL,
2690                                           NULL);
2691   _gtk_style_property_register           (g_param_spec_boxed ("font",
2692                                                               "Font Description",
2693                                                               "Font Description",
2694                                                               PANGO_TYPE_FONT_DESCRIPTION, 0),
2695                                           GTK_STYLE_PROPERTY_INHERIT,
2696                                           NULL,
2697                                           unpack_font_description,
2698                                           pack_font_description,
2699                                           font_description_value_parse,
2700                                           font_description_value_print,
2701                                           NULL,
2702                                           unset_font_description);
2703
2704   _gtk_style_property_register           (g_param_spec_boxed ("text-shadow",
2705                                                               "Text shadow",
2706                                                               "Text shadow",
2707                                                               GTK_TYPE_SHADOW, 0),
2708                                           GTK_STYLE_PROPERTY_INHERIT,
2709                                           NULL,
2710                                           NULL,
2711                                           NULL,
2712                                           NULL,
2713                                           NULL,
2714                                           NULL,
2715                                           NULL);
2716
2717   _gtk_style_property_register           (g_param_spec_boxed ("icon-shadow",
2718                                                               "Icon shadow",
2719                                                               "Icon shadow",
2720                                                               GTK_TYPE_SHADOW, 0),
2721                                           GTK_STYLE_PROPERTY_INHERIT,
2722                                           NULL,
2723                                           NULL,
2724                                           NULL,
2725                                           NULL,
2726                                           NULL,
2727                                           NULL,
2728                                           NULL);
2729
2730   gtk_style_properties_register_property (NULL,
2731                                           g_param_spec_boxed ("box-shadow",
2732                                                               "Box shadow",
2733                                                               "Box shadow",
2734                                                               GTK_TYPE_SHADOW, 0));
2735   gtk_style_properties_register_property (NULL,
2736                                           g_param_spec_int ("margin-top",
2737                                                             "margin top",
2738                                                             "Margin at top",
2739                                                             0, G_MAXINT, 0, 0));
2740   gtk_style_properties_register_property (NULL,
2741                                           g_param_spec_int ("margin-left",
2742                                                             "margin left",
2743                                                             "Margin at left",
2744                                                             0, G_MAXINT, 0, 0));
2745   gtk_style_properties_register_property (NULL,
2746                                           g_param_spec_int ("margin-bottom",
2747                                                             "margin bottom",
2748                                                             "Margin at bottom",
2749                                                             0, G_MAXINT, 0, 0));
2750   gtk_style_properties_register_property (NULL,
2751                                           g_param_spec_int ("margin-right",
2752                                                             "margin right",
2753                                                             "Margin at right",
2754                                                             0, G_MAXINT, 0, 0));
2755   _gtk_style_property_register           (g_param_spec_boxed ("margin",
2756                                                               "Margin",
2757                                                               "Margin",
2758                                                               GTK_TYPE_BORDER, 0),
2759                                           0,
2760                                           NULL,
2761                                           unpack_margin,
2762                                           pack_margin,
2763                                           NULL,
2764                                           NULL,
2765                                           NULL,
2766                                           unset_margin);
2767   gtk_style_properties_register_property (NULL,
2768                                           g_param_spec_int ("padding-top",
2769                                                             "padding top",
2770                                                             "Padding at top",
2771                                                             0, G_MAXINT, 0, 0));
2772   gtk_style_properties_register_property (NULL,
2773                                           g_param_spec_int ("padding-left",
2774                                                             "padding left",
2775                                                             "Padding at left",
2776                                                             0, G_MAXINT, 0, 0));
2777   gtk_style_properties_register_property (NULL,
2778                                           g_param_spec_int ("padding-bottom",
2779                                                             "padding bottom",
2780                                                             "Padding at bottom",
2781                                                             0, G_MAXINT, 0, 0));
2782   gtk_style_properties_register_property (NULL,
2783                                           g_param_spec_int ("padding-right",
2784                                                             "padding right",
2785                                                             "Padding at right",
2786                                                             0, G_MAXINT, 0, 0));
2787   _gtk_style_property_register           (g_param_spec_boxed ("padding",
2788                                                               "Padding",
2789                                                               "Padding",
2790                                                               GTK_TYPE_BORDER, 0),
2791                                           0,
2792                                           NULL,
2793                                           unpack_padding,
2794                                           pack_padding,
2795                                           NULL,
2796                                           NULL,
2797                                           NULL,
2798                                           unset_padding);
2799   gtk_style_properties_register_property (NULL,
2800                                           g_param_spec_int ("border-top-width",
2801                                                             "border top width",
2802                                                             "Border width at top",
2803                                                             0, G_MAXINT, 0, 0));
2804   gtk_style_properties_register_property (NULL,
2805                                           g_param_spec_int ("border-left-width",
2806                                                             "border left width",
2807                                                             "Border width at left",
2808                                                             0, G_MAXINT, 0, 0));
2809   gtk_style_properties_register_property (NULL,
2810                                           g_param_spec_int ("border-bottom-width",
2811                                                             "border bottom width",
2812                                                             "Border width at bottom",
2813                                                             0, G_MAXINT, 0, 0));
2814   gtk_style_properties_register_property (NULL,
2815                                           g_param_spec_int ("border-right-width",
2816                                                             "border right width",
2817                                                             "Border width at right",
2818                                                             0, G_MAXINT, 0, 0));
2819   _gtk_style_property_register           (g_param_spec_boxed ("border-width",
2820                                                               "Border width",
2821                                                               "Border width, in pixels",
2822                                                               GTK_TYPE_BORDER, 0),
2823                                           0,
2824                                           NULL,
2825                                           unpack_border_width,
2826                                           pack_border_width,
2827                                           NULL,
2828                                           NULL,
2829                                           NULL,
2830                                           unset_border_width);
2831
2832   _gtk_style_property_register           (g_param_spec_boxed ("border-top-left-radius",
2833                                                               "Border top left radius",
2834                                                               "Border radius of top left corner, in pixels",
2835                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2836                                           0,
2837                                           NULL,
2838                                           NULL,
2839                                           NULL,
2840                                           border_corner_radius_value_parse,
2841                                           border_corner_radius_value_print,
2842                                           NULL,
2843                                           NULL);
2844   _gtk_style_property_register           (g_param_spec_boxed ("border-top-right-radius",
2845                                                               "Border top right radius",
2846                                                               "Border radius of top right corner, in pixels",
2847                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2848                                           0,
2849                                           NULL,
2850                                           NULL,
2851                                           NULL,
2852                                           border_corner_radius_value_parse,
2853                                           border_corner_radius_value_print,
2854                                           NULL,
2855                                           NULL);
2856   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-right-radius",
2857                                                               "Border bottom right radius",
2858                                                               "Border radius of bottom right corner, in pixels",
2859                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2860                                           0,
2861                                           NULL,
2862                                           NULL,
2863                                           NULL,
2864                                           border_corner_radius_value_parse,
2865                                           border_corner_radius_value_print,
2866                                           NULL,
2867                                           NULL);
2868   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-left-radius",
2869                                                               "Border bottom left radius",
2870                                                               "Border radius of bottom left corner, in pixels",
2871                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2872                                           0,
2873                                           NULL,
2874                                           NULL,
2875                                           NULL,
2876                                           border_corner_radius_value_parse,
2877                                           border_corner_radius_value_print,
2878                                           NULL,
2879                                           NULL);
2880   _gtk_style_property_register           (g_param_spec_int ("border-radius",
2881                                                             "Border radius",
2882                                                             "Border radius, in pixels",
2883                                                             0, G_MAXINT, 0, 0),
2884                                           0,
2885                                           NULL,
2886                                           unpack_border_radius,
2887                                           pack_border_radius,
2888                                           border_radius_value_parse,
2889                                           border_radius_value_print,
2890                                           NULL,
2891                                           unset_border_radius);
2892
2893   gtk_style_properties_register_property (NULL,
2894                                           g_param_spec_enum ("border-style",
2895                                                              "Border style",
2896                                                              "Border style",
2897                                                              GTK_TYPE_BORDER_STYLE,
2898                                                              GTK_BORDER_STYLE_NONE, 0));
2899   _gtk_style_property_register           (g_param_spec_boxed ("border-top-color",
2900                                                               "Border top color",
2901                                                               "Border top color",
2902                                                               GDK_TYPE_RGBA, 0),
2903                                           0,
2904                                           NULL,
2905                                           NULL,
2906                                           NULL,
2907                                           transparent_color_value_parse,
2908                                           NULL,
2909                                           border_color_default_value,
2910                                           NULL);
2911   _gtk_style_property_register           (g_param_spec_boxed ("border-right-color",
2912                                                               "Border right color",
2913                                                               "Border right color",
2914                                                               GDK_TYPE_RGBA, 0),
2915                                           0,
2916                                           NULL,
2917                                           NULL,
2918                                           NULL,
2919                                           transparent_color_value_parse,
2920                                           NULL,
2921                                           border_color_default_value,
2922                                           NULL);
2923   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-color",
2924                                                               "Border bottom color",
2925                                                               "Border bottom color",
2926                                                               GDK_TYPE_RGBA, 0),
2927                                           0,
2928                                           NULL,
2929                                           NULL,
2930                                           NULL,
2931                                           transparent_color_value_parse,
2932                                           NULL,
2933                                           border_color_default_value,
2934                                           NULL);
2935   _gtk_style_property_register           (g_param_spec_boxed ("border-left-color",
2936                                                               "Border left color",
2937                                                               "Border left color",
2938                                                               GDK_TYPE_RGBA, 0),
2939                                           0,
2940                                           NULL,
2941                                           NULL,
2942                                           NULL,
2943                                           transparent_color_value_parse,
2944                                           NULL,
2945                                           border_color_default_value,
2946                                           NULL);
2947   _gtk_style_property_register           (g_param_spec_boxed ("border-color",
2948                                                               "Border color",
2949                                                               "Border color",
2950                                                               GDK_TYPE_RGBA, 0),
2951                                           0,
2952                                           NULL,
2953                                           unpack_border_color,
2954                                           pack_border_color,
2955                                           border_color_shorthand_value_parse,
2956                                           NULL,
2957                                           NULL,
2958                                           unset_border_color);
2959
2960   gtk_style_properties_register_property (NULL,
2961                                           g_param_spec_boxed ("background-image",
2962                                                               "Background Image",
2963                                                               "Background Image",
2964                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
2965   gtk_style_properties_register_property (NULL,
2966                                           g_param_spec_boxed ("background-repeat",
2967                                                               "Background repeat",
2968                                                               "Background repeat",
2969                                                               GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
2970
2971   gtk_style_properties_register_property (NULL,
2972                                           g_param_spec_boxed ("border-image-source",
2973                                                               "Border image source",
2974                                                               "Border image source",
2975                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
2976   gtk_style_properties_register_property (NULL,
2977                                           g_param_spec_boxed ("border-image-repeat",
2978                                                               "Border image repeat",
2979                                                               "Border image repeat",
2980                                                               GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
2981   gtk_style_properties_register_property (NULL,
2982                                           g_param_spec_boxed ("border-image-slice",
2983                                                               "Border image slice",
2984                                                               "Border image slice",
2985                                                               GTK_TYPE_BORDER, 0));
2986   _gtk_style_property_register           (g_param_spec_boxed ("border-image-width",
2987                                                               "Border image width",
2988                                                               "Border image width",
2989                                                               GTK_TYPE_BORDER, 0),
2990                                           0,
2991                                           NULL,
2992                                           NULL,
2993                                           NULL,
2994                                           NULL,
2995                                           NULL,
2996                                           border_image_width_default_value,
2997                                           NULL);
2998   _gtk_style_property_register           (g_param_spec_boxed ("border-image",
2999                                                               "Border Image",
3000                                                               "Border Image",
3001                                                               GTK_TYPE_BORDER_IMAGE, 0),
3002                                           0,
3003                                           NULL,
3004                                           _gtk_border_image_unpack,
3005                                           _gtk_border_image_pack,
3006                                           NULL,
3007                                           NULL,
3008                                           NULL,
3009                                           unset_border_image);
3010   gtk_style_properties_register_property (NULL,
3011                                           g_param_spec_object ("engine",
3012                                                                "Theming Engine",
3013                                                                "Theming Engine",
3014                                                                GTK_TYPE_THEMING_ENGINE, 0));
3015   gtk_style_properties_register_property (NULL,
3016                                           g_param_spec_boxed ("transition",
3017                                                               "Transition animation description",
3018                                                               "Transition animation description",
3019                                                               GTK_TYPE_ANIMATION_DESCRIPTION, 0));
3020
3021   /* Private property holding the binding sets */
3022   _gtk_style_property_register           (g_param_spec_boxed ("gtk-key-bindings",
3023                                                               "Key bindings",
3024                                                               "Key bindings",
3025                                                               G_TYPE_PTR_ARRAY, 0),
3026                                           0,
3027                                           NULL,
3028                                           NULL,
3029                                           NULL,
3030                                           bindings_value_parse,
3031                                           bindings_value_print,
3032                                           NULL,
3033                                           NULL);
3034 }
3035
3036 const GtkStyleProperty *
3037 _gtk_style_property_lookup (const char *name)
3038 {
3039   gtk_style_property_init ();
3040
3041   return g_hash_table_lookup (properties, name);
3042 }
3043
3044 void
3045 _gtk_style_property_register (GParamSpec               *pspec,
3046                               GtkStylePropertyFlags     flags,
3047                               GtkStylePropertyParser    property_parse_func,
3048                               GtkStyleUnpackFunc        unpack_func,
3049                               GtkStylePackFunc          pack_func,
3050                               GtkStyleParseFunc         parse_func,
3051                               GtkStylePrintFunc         print_func,
3052                               GtkStyleDefaultValueFunc  default_value_func,
3053                               GtkStyleUnsetFunc         unset_func)
3054 {
3055   const GtkStyleProperty *existing;
3056   GtkStyleProperty *node;
3057
3058   g_return_if_fail ((pack_func == NULL) == (unpack_func == NULL));
3059
3060   gtk_style_property_init ();
3061
3062   existing = _gtk_style_property_lookup (pspec->name);
3063   if (existing != NULL)
3064     {
3065       g_warning ("Property \"%s\" was already registered with type %s",
3066                  pspec->name, g_type_name (existing->pspec->value_type));
3067       return;
3068     }
3069
3070   node = g_slice_new0 (GtkStyleProperty);
3071   node->flags = flags;
3072   node->pspec = pspec;
3073   node->property_parse_func = property_parse_func;
3074   node->pack_func = pack_func;
3075   node->unpack_func = unpack_func;
3076   node->parse_func = parse_func;
3077   node->print_func = print_func;
3078   node->default_value_func = default_value_func;
3079   node->unset_func = unset_func;
3080
3081   /* pspec owns name */
3082   g_hash_table_insert (properties, (gchar *)pspec->name, node);
3083 }