]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperty.c
Save a generic boxes source in GtkImageBorder
[~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   gconstpointer *boxed = NULL;
1254   GType boxed_type;
1255   GtkBorder slice, *width = NULL, *parsed_slice;
1256   GtkCssBorderImageRepeat repeat, *parsed_repeat;
1257   gboolean retval = FALSE;
1258   GtkBorderImage *image = NULL;
1259
1260   g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
1261
1262   if (!pattern_value_parse (parser, base, &temp))
1263     return FALSE;
1264
1265   boxed_type = G_VALUE_TYPE (&temp);
1266   if (boxed_type != CAIRO_GOBJECT_TYPE_PATTERN)
1267     boxed = g_value_dup_boxed (&temp);
1268   else
1269     pattern = g_value_dup_boxed (&temp);
1270
1271   g_value_unset (&temp);
1272   g_value_init (&temp, GTK_TYPE_BORDER);
1273
1274   if (!border_value_parse (parser, base, &temp))
1275     goto out;
1276
1277   parsed_slice = g_value_get_boxed (&temp);
1278   slice = *parsed_slice;
1279
1280   if (_gtk_css_parser_try (parser, "/", TRUE))
1281     {
1282       g_value_unset (&temp);
1283       g_value_init (&temp, GTK_TYPE_BORDER);
1284
1285       if (!border_value_parse (parser, base, &temp))
1286         goto out;
1287
1288       width = g_value_dup_boxed (&temp);
1289     }
1290
1291   g_value_unset (&temp);
1292   g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
1293
1294   if (!border_image_repeat_value_parse (parser, base, &temp))
1295     goto out;
1296
1297   parsed_repeat = g_value_get_boxed (&temp);
1298   repeat = *parsed_repeat;
1299
1300   g_value_unset (&temp);
1301
1302   if (boxed != NULL)
1303     image = _gtk_border_image_new_for_boxed (boxed_type, boxed, &slice, width, &repeat);
1304   else if (pattern != NULL)
1305     image = _gtk_border_image_new (pattern, &slice, width, &repeat);
1306
1307   if (image != NULL)
1308     {
1309       retval = TRUE;
1310       g_value_take_boxed (value, image);
1311     }
1312
1313  out:
1314   if (pattern != NULL)
1315     cairo_pattern_destroy (pattern);
1316
1317   if (boxed != NULL)
1318     g_boxed_free (boxed_type, boxed);
1319
1320   if (width != NULL)
1321     gtk_border_free (width);
1322
1323   return retval;
1324 }
1325
1326 static gboolean 
1327 enum_value_parse (GtkCssParser *parser,
1328                   GFile        *base,
1329                   GValue       *value)
1330 {
1331   GEnumClass *enum_class;
1332   GEnumValue *enum_value;
1333   char *str;
1334
1335   str = _gtk_css_parser_try_ident (parser, TRUE);
1336   if (str == NULL)
1337     {
1338       _gtk_css_parser_error (parser, "Expected an identifier");
1339       return FALSE;
1340     }
1341
1342   enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1343   enum_value = g_enum_get_value_by_nick (enum_class, str);
1344
1345   if (enum_value)
1346     g_value_set_enum (value, enum_value->value);
1347   else
1348     _gtk_css_parser_error (parser,
1349                            "Unknown value '%s' for enum type '%s'",
1350                            str, g_type_name (G_VALUE_TYPE (value)));
1351   
1352   g_type_class_unref (enum_class);
1353   g_free (str);
1354
1355   return enum_value != NULL;
1356 }
1357
1358 static void
1359 enum_value_print (const GValue *value,
1360                   GString      *string)
1361 {
1362   GEnumClass *enum_class;
1363   GEnumValue *enum_value;
1364
1365   enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1366   enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
1367
1368   g_string_append (string, enum_value->value_nick);
1369
1370   g_type_class_unref (enum_class);
1371 }
1372
1373 static gboolean 
1374 flags_value_parse (GtkCssParser *parser,
1375                    GFile        *base,
1376                    GValue       *value)
1377 {
1378   GFlagsClass *flags_class;
1379   GFlagsValue *flag_value;
1380   guint flags = 0;
1381   char *str;
1382
1383   flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1384
1385   do {
1386     str = _gtk_css_parser_try_ident (parser, TRUE);
1387     if (str == NULL)
1388       {
1389         _gtk_css_parser_error (parser, "Expected an identifier");
1390         g_type_class_unref (flags_class);
1391         return FALSE;
1392       }
1393
1394       flag_value = g_flags_get_value_by_nick (flags_class, str);
1395       if (!flag_value)
1396         {
1397           _gtk_css_parser_error (parser,
1398                                  "Unknown flag value '%s' for type '%s'",
1399                                  str, g_type_name (G_VALUE_TYPE (value)));
1400           /* XXX Do we want to return FALSE here? We can get
1401            * forward-compatibility for new values this way
1402            */
1403           g_free (str);
1404           g_type_class_unref (flags_class);
1405           return FALSE;
1406         }
1407
1408       g_free (str);
1409     }
1410   while (_gtk_css_parser_try (parser, ",", FALSE));
1411
1412   g_type_class_unref (flags_class);
1413
1414   g_value_set_enum (value, flags);
1415
1416   return TRUE;
1417 }
1418
1419 static void
1420 flags_value_print (const GValue *value,
1421                    GString      *string)
1422 {
1423   GFlagsClass *flags_class;
1424   guint i, flags;
1425
1426   flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1427   flags = g_value_get_flags (value);
1428
1429   for (i = 0; i < flags_class->n_values; i++)
1430     {
1431       GFlagsValue *flags_value = &flags_class->values[i];
1432
1433       if (flags & flags_value->value)
1434         {
1435           if (string->len != 0)
1436             g_string_append (string, ", ");
1437
1438           g_string_append (string, flags_value->value_nick);
1439         }
1440     }
1441
1442   g_type_class_unref (flags_class);
1443 }
1444
1445 static gboolean 
1446 bindings_value_parse (GtkCssParser *parser,
1447                       GFile        *base,
1448                       GValue       *value)
1449 {
1450   GPtrArray *array;
1451   GtkBindingSet *binding_set;
1452   char *name;
1453
1454   array = g_ptr_array_new ();
1455
1456   do {
1457       name = _gtk_css_parser_try_ident (parser, TRUE);
1458       if (name == NULL)
1459         {
1460           _gtk_css_parser_error (parser, "Not a valid binding name");
1461           g_ptr_array_free (array, TRUE);
1462           return FALSE;
1463         }
1464
1465       binding_set = gtk_binding_set_find (name);
1466
1467       if (!binding_set)
1468         {
1469           _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1470           g_free (name);
1471           continue;
1472         }
1473
1474       g_ptr_array_add (array, binding_set);
1475       g_free (name);
1476     }
1477   while (_gtk_css_parser_try (parser, ",", TRUE));
1478
1479   g_value_take_boxed (value, array);
1480
1481   return TRUE;
1482 }
1483
1484 static void
1485 bindings_value_print (const GValue *value,
1486                       GString      *string)
1487 {
1488   GPtrArray *array;
1489   guint i;
1490
1491   array = g_value_get_boxed (value);
1492
1493   for (i = 0; i < array->len; i++)
1494     {
1495       GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1496
1497       if (i > 0)
1498         g_string_append (string, ", ");
1499       g_string_append (string, binding_set->set_name);
1500     }
1501 }
1502
1503 static gboolean 
1504 border_corner_radius_value_parse (GtkCssParser *parser,
1505                                   GFile        *base,
1506                                   GValue       *value)
1507 {
1508   GtkCssBorderCornerRadius corner;
1509
1510   if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
1511     {
1512       _gtk_css_parser_error (parser, "Expected a number");
1513       return FALSE;
1514     }
1515   else if (corner.horizontal < 0)
1516     goto negative;
1517
1518   if (!_gtk_css_parser_try_double (parser, &corner.vertical))
1519     corner.vertical = corner.horizontal;
1520   else if (corner.vertical < 0)
1521     goto negative;
1522
1523   g_value_set_boxed (value, &corner);
1524   return TRUE;
1525
1526 negative:
1527   _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1528   return FALSE;
1529 }
1530
1531 static void
1532 border_corner_radius_value_print (const GValue *value,
1533                                   GString      *string)
1534 {
1535   GtkCssBorderCornerRadius *corner;
1536
1537   corner = g_value_get_boxed (value);
1538
1539   if (corner == NULL)
1540     {
1541       g_string_append (string, "none");
1542       return;
1543     }
1544
1545   string_append_double (string, corner->horizontal);
1546   if (corner->horizontal != corner->vertical)
1547     {
1548       g_string_append_c (string, ' ');
1549       string_append_double (string, corner->vertical);
1550     }
1551 }
1552
1553 static gboolean 
1554 border_radius_value_parse (GtkCssParser *parser,
1555                            GFile        *base,
1556                            GValue       *value)
1557 {
1558   GtkCssBorderRadius border;
1559
1560   if (!_gtk_css_parser_try_double (parser, &border.top_left.horizontal))
1561     {
1562       _gtk_css_parser_error (parser, "Expected a number");
1563       return FALSE;
1564     }
1565   else if (border.top_left.horizontal < 0)
1566     goto negative;
1567
1568   if (_gtk_css_parser_try_double (parser, &border.top_right.horizontal))
1569     {
1570       if (border.top_right.horizontal < 0)
1571         goto negative;
1572       if (_gtk_css_parser_try_double (parser, &border.bottom_right.horizontal))
1573         {
1574           if (border.bottom_right.horizontal < 0)
1575             goto negative;
1576           if (!_gtk_css_parser_try_double (parser, &border.bottom_left.horizontal))
1577             border.bottom_left.horizontal = border.top_right.horizontal;
1578           else if (border.bottom_left.horizontal < 0)
1579             goto negative;
1580         }
1581       else
1582         {
1583           border.bottom_right.horizontal = border.top_left.horizontal;
1584           border.bottom_left.horizontal = border.top_right.horizontal;
1585         }
1586     }
1587   else
1588     {
1589       border.top_right.horizontal = border.top_left.horizontal;
1590       border.bottom_right.horizontal = border.top_left.horizontal;
1591       border.bottom_left.horizontal = border.top_left.horizontal;
1592     }
1593
1594   if (_gtk_css_parser_try (parser, "/", TRUE))
1595     {
1596       if (!_gtk_css_parser_try_double (parser, &border.top_left.vertical))
1597         {
1598           _gtk_css_parser_error (parser, "Expected a number");
1599           return FALSE;
1600         }
1601       else if (border.top_left.vertical < 0)
1602         goto negative;
1603
1604       if (_gtk_css_parser_try_double (parser, &border.top_right.vertical))
1605         {
1606           if (border.top_right.vertical < 0)
1607             goto negative;
1608           if (_gtk_css_parser_try_double (parser, &border.bottom_right.vertical))
1609             {
1610               if (border.bottom_right.vertical < 0)
1611                 goto negative;
1612               if (!_gtk_css_parser_try_double (parser, &border.bottom_left.vertical))
1613                 border.bottom_left.vertical = border.top_right.vertical;
1614               else if (border.bottom_left.vertical < 0)
1615                 goto negative;
1616             }
1617           else
1618             {
1619               border.bottom_right.vertical = border.top_left.vertical;
1620               border.bottom_left.vertical = border.top_right.vertical;
1621             }
1622         }
1623       else
1624         {
1625           border.top_right.vertical = border.top_left.vertical;
1626           border.bottom_right.vertical = border.top_left.vertical;
1627           border.bottom_left.vertical = border.top_left.vertical;
1628         }
1629     }
1630   else
1631     {
1632       border.top_left.vertical = border.top_left.horizontal;
1633       border.top_right.vertical = border.top_right.horizontal;
1634       border.bottom_right.vertical = border.bottom_right.horizontal;
1635       border.bottom_left.vertical = border.bottom_left.horizontal;
1636     }
1637
1638   /* border-radius is an int property for backwards-compat reasons */
1639   g_value_unset (value);
1640   g_value_init (value, GTK_TYPE_CSS_BORDER_RADIUS);
1641   g_value_set_boxed (value, &border);
1642
1643   return TRUE;
1644
1645 negative:
1646   _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1647   return FALSE;
1648 }
1649
1650 static void
1651 border_radius_value_print (const GValue *value,
1652                            GString      *string)
1653 {
1654   GtkCssBorderRadius *border;
1655
1656   border = g_value_get_boxed (value);
1657
1658   if (border == NULL)
1659     {
1660       g_string_append (string, "none");
1661       return;
1662     }
1663
1664   string_append_double (string, border->top_left.horizontal);
1665   if (border->top_left.horizontal != border->top_right.horizontal ||
1666       border->top_left.horizontal != border->bottom_right.horizontal ||
1667       border->top_left.horizontal != border->bottom_left.horizontal)
1668     {
1669       g_string_append_c (string, ' ');
1670       string_append_double (string, border->top_right.horizontal);
1671       if (border->top_left.horizontal != border->bottom_right.horizontal ||
1672           border->top_right.horizontal != border->bottom_left.horizontal)
1673         {
1674           g_string_append_c (string, ' ');
1675           string_append_double (string, border->bottom_right.horizontal);
1676           if (border->top_right.horizontal != border->bottom_left.horizontal)
1677             {
1678               g_string_append_c (string, ' ');
1679               string_append_double (string, border->bottom_left.horizontal);
1680             }
1681         }
1682     }
1683
1684   if (border->top_left.horizontal != border->top_left.vertical ||
1685       border->top_right.horizontal != border->top_right.vertical ||
1686       border->bottom_right.horizontal != border->bottom_right.vertical ||
1687       border->bottom_left.horizontal != border->bottom_left.vertical)
1688     {
1689       g_string_append (string, " / ");
1690       string_append_double (string, border->top_left.vertical);
1691       if (border->top_left.vertical != border->top_right.vertical ||
1692           border->top_left.vertical != border->bottom_right.vertical ||
1693           border->top_left.vertical != border->bottom_left.vertical)
1694         {
1695           g_string_append_c (string, ' ');
1696           string_append_double (string, border->top_right.vertical);
1697           if (border->top_left.vertical != border->bottom_right.vertical ||
1698               border->top_right.vertical != border->bottom_left.vertical)
1699             {
1700               g_string_append_c (string, ' ');
1701               string_append_double (string, border->bottom_right.vertical);
1702               if (border->top_right.vertical != border->bottom_left.vertical)
1703                 {
1704                   g_string_append_c (string, ' ');
1705                   string_append_double (string, border->bottom_left.vertical);
1706                 }
1707             }
1708         }
1709
1710     }
1711 }
1712
1713 static gboolean 
1714 transparent_color_value_parse (GtkCssParser *parser,
1715                                GFile        *base,
1716                                GValue       *value)
1717 {
1718   if (_gtk_css_parser_try (parser, "transparent", TRUE))
1719     {
1720       GdkRGBA transparent = { 0, 0, 0, 0 };
1721           
1722       g_value_set_boxed (value, &transparent);
1723
1724       return TRUE;
1725     }
1726
1727   return rgba_value_parse (parser, base, value);
1728 }
1729
1730 static gboolean 
1731 border_color_shorthand_value_parse (GtkCssParser *parser,
1732                                     GFile        *base,
1733                                     GValue       *value)
1734 {
1735   GtkSymbolicColor *symbolic;
1736   GPtrArray *array;
1737
1738   array = g_ptr_array_new_with_free_func ((GDestroyNotify) gtk_symbolic_color_unref);
1739
1740   do
1741     {
1742       if (_gtk_css_parser_try (parser, "transparent", TRUE))
1743         {
1744           GdkRGBA transparent = { 0, 0, 0, 0 };
1745           
1746           symbolic = gtk_symbolic_color_new_literal (&transparent);
1747         }
1748       else
1749         {
1750           symbolic = _gtk_css_parser_read_symbolic_color (parser);
1751       
1752           if (symbolic == NULL)
1753             return FALSE;
1754         }
1755       
1756       g_ptr_array_add (array, symbolic);
1757     }
1758   while (array->len < 4 && 
1759          !_gtk_css_parser_is_eof (parser) &&
1760          !_gtk_css_parser_begins_with (parser, ';') &&
1761          !_gtk_css_parser_begins_with (parser, '}'));
1762
1763   switch (array->len)
1764     {
1765       default:
1766         g_assert_not_reached ();
1767         break;
1768       case 1:
1769         g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1770         /* fall through */
1771       case 2:
1772         g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1773         /* fall through */
1774       case 3:
1775         g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 1)));
1776         /* fall through */
1777       case 4:
1778         break;
1779     }
1780
1781   g_value_unset (value);
1782   g_value_init (value, G_TYPE_PTR_ARRAY);
1783   g_value_take_boxed (value, array);
1784
1785   return TRUE;
1786 }
1787
1788 /*** PACKING ***/
1789
1790 static GParameter *
1791 unpack_border (const GValue *value,
1792                guint        *n_params,
1793                const char   *top,
1794                const char   *left,
1795                const char   *bottom,
1796                const char   *right)
1797 {
1798   GParameter *parameter = g_new0 (GParameter, 4);
1799   GtkBorder *border = g_value_get_boxed (value);
1800
1801   parameter[0].name = top;
1802   g_value_init (&parameter[0].value, G_TYPE_INT);
1803   g_value_set_int (&parameter[0].value, border->top);
1804   parameter[1].name = left;
1805   g_value_init (&parameter[1].value, G_TYPE_INT);
1806   g_value_set_int (&parameter[1].value, border->left);
1807   parameter[2].name = bottom;
1808   g_value_init (&parameter[2].value, G_TYPE_INT);
1809   g_value_set_int (&parameter[2].value, border->bottom);
1810   parameter[3].name = right;
1811   g_value_init (&parameter[3].value, G_TYPE_INT);
1812   g_value_set_int (&parameter[3].value, border->right);
1813
1814   *n_params = 4;
1815   return parameter;
1816 }
1817
1818 static void
1819 pack_border (GValue             *value,
1820              GtkStyleProperties *props,
1821              GtkStateFlags       state,
1822              const char         *top,
1823              const char         *left,
1824              const char         *bottom,
1825              const char         *right)
1826 {
1827   GtkBorder border;
1828   int t, l, b, r;
1829
1830   gtk_style_properties_get (props,
1831                             state,
1832                             top, &t,
1833                             left, &l,
1834                             bottom, &b,
1835                             right, &r,
1836                             NULL);
1837
1838   border.top = t;
1839   border.left = l;
1840   border.bottom = b;
1841   border.right = r;
1842
1843   g_value_set_boxed (value, &border);
1844 }
1845
1846 static GParameter *
1847 unpack_border_width (const GValue *value,
1848                      guint        *n_params)
1849 {
1850   return unpack_border (value, n_params,
1851                         "border-top-width", "border-left-width",
1852                         "border-bottom-width", "border-right-width");
1853 }
1854
1855 static void
1856 pack_border_width (GValue             *value,
1857                    GtkStyleProperties *props,
1858                    GtkStateFlags       state)
1859 {
1860   pack_border (value, props, state,
1861                "border-top-width", "border-left-width",
1862                "border-bottom-width", "border-right-width");
1863 }
1864
1865 static GParameter *
1866 unpack_padding (const GValue *value,
1867                 guint        *n_params)
1868 {
1869   return unpack_border (value, n_params,
1870                         "padding-top", "padding-left",
1871                         "padding-bottom", "padding-right");
1872 }
1873
1874 static void
1875 pack_padding (GValue             *value,
1876               GtkStyleProperties *props,
1877               GtkStateFlags       state)
1878 {
1879   pack_border (value, props, state,
1880                "padding-top", "padding-left",
1881                "padding-bottom", "padding-right");
1882 }
1883
1884 static GParameter *
1885 unpack_margin (const GValue *value,
1886                guint        *n_params)
1887 {
1888   return unpack_border (value, n_params,
1889                         "margin-top", "margin-left",
1890                         "margin-bottom", "margin-right");
1891 }
1892
1893 static void
1894 pack_margin (GValue             *value,
1895              GtkStyleProperties *props,
1896              GtkStateFlags       state)
1897 {
1898   pack_border (value, props, state,
1899                "margin-top", "margin-left",
1900                "margin-bottom", "margin-right");
1901 }
1902
1903 static GParameter *
1904 unpack_border_radius (const GValue *value,
1905                       guint        *n_params)
1906 {
1907   GParameter *parameter = g_new0 (GParameter, 4);
1908   GtkCssBorderRadius *border;
1909   
1910   if (G_VALUE_HOLDS_BOXED (value))
1911     border = g_value_get_boxed (value);
1912   else
1913     border = NULL;
1914
1915   parameter[0].name = "border-top-left-radius";
1916   g_value_init (&parameter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1917   parameter[1].name = "border-top-right-radius";
1918   g_value_init (&parameter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1919   parameter[2].name = "border-bottom-right-radius";
1920   g_value_init (&parameter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1921   parameter[3].name = "border-bottom-left-radius";
1922   g_value_init (&parameter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1923   if (border)
1924     {
1925       g_value_set_boxed (&parameter[0].value, &border->top_left);
1926       g_value_set_boxed (&parameter[1].value, &border->top_right);
1927       g_value_set_boxed (&parameter[2].value, &border->bottom_right);
1928       g_value_set_boxed (&parameter[3].value, &border->bottom_left);
1929     }
1930
1931   *n_params = 4;
1932   return parameter;
1933 }
1934
1935 static void
1936 pack_border_radius (GValue             *value,
1937                     GtkStyleProperties *props,
1938                     GtkStateFlags       state)
1939 {
1940   GtkCssBorderCornerRadius *top_left;
1941
1942   /* NB: We are an int property, so we have to resolve to an int here.
1943    * So we just resolve to an int. We pick one and stick to it.
1944    * Lesson learned: Don't query border-radius shorthand, query the 
1945    * real properties instead. */
1946   gtk_style_properties_get (props,
1947                             state,
1948                             "border-top-left-radius", &top_left,
1949                             NULL);
1950
1951   if (top_left)
1952     g_value_set_int (value, top_left->horizontal);
1953
1954   g_free (top_left);
1955 }
1956
1957 static GParameter *
1958 unpack_font_description (const GValue *value,
1959                          guint        *n_params)
1960 {
1961   GParameter *parameter = g_new0 (GParameter, 5);
1962   PangoFontDescription *description;
1963   PangoFontMask mask;
1964   guint n;
1965   
1966   /* For backwards compat, we only unpack values that are indeed set.
1967    * For strict CSS conformance we need to unpack all of them.
1968    * Note that we do set all of them in the parse function, so it
1969    * will not have effects when parsing CSS files. It will though
1970    * for custom style providers.
1971    */
1972
1973   description = g_value_get_boxed (value);
1974   n = 0;
1975
1976   if (description)
1977     mask = pango_font_description_get_set_fields (description);
1978   else
1979     mask = 0;
1980
1981   if (mask & PANGO_FONT_MASK_FAMILY)
1982     {
1983       GPtrArray *strv = g_ptr_array_new ();
1984
1985       g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
1986       g_ptr_array_add (strv, NULL);
1987       parameter[n].name = "font-family";
1988       g_value_init (&parameter[n].value, G_TYPE_STRV);
1989       g_value_take_boxed (&parameter[n].value,
1990                           g_ptr_array_free (strv, FALSE));
1991       n++;
1992     }
1993
1994   if (mask & PANGO_FONT_MASK_STYLE)
1995     {
1996       parameter[n].name = "font-style";
1997       g_value_init (&parameter[n].value, PANGO_TYPE_STYLE);
1998       g_value_set_enum (&parameter[n].value,
1999                         pango_font_description_get_style (description));
2000       n++;
2001     }
2002
2003   if (mask & PANGO_FONT_MASK_VARIANT)
2004     {
2005       parameter[n].name = "font-variant";
2006       g_value_init (&parameter[n].value, PANGO_TYPE_VARIANT);
2007       g_value_set_enum (&parameter[n].value,
2008                         pango_font_description_get_variant (description));
2009       n++;
2010     }
2011
2012   if (mask & PANGO_FONT_MASK_WEIGHT)
2013     {
2014       parameter[n].name = "font-weight";
2015       g_value_init (&parameter[n].value, PANGO_TYPE_WEIGHT);
2016       g_value_set_enum (&parameter[n].value,
2017                         pango_font_description_get_weight (description));
2018       n++;
2019     }
2020
2021   if (mask & PANGO_FONT_MASK_SIZE)
2022     {
2023       parameter[n].name = "font-size";
2024       g_value_init (&parameter[n].value, G_TYPE_DOUBLE);
2025       g_value_set_double (&parameter[n].value,
2026                           (double) pango_font_description_get_size (description) / PANGO_SCALE);
2027       n++;
2028     }
2029
2030   *n_params = n;
2031
2032   return parameter;
2033 }
2034
2035 static void
2036 pack_font_description (GValue             *value,
2037                        GtkStyleProperties *props,
2038                        GtkStateFlags       state)
2039 {
2040   PangoFontDescription *description;
2041   char **families;
2042   PangoStyle style;
2043   PangoVariant variant;
2044   PangoWeight weight;
2045   double size;
2046
2047   gtk_style_properties_get (props,
2048                             state,
2049                             "font-family", &families,
2050                             "font-style", &style,
2051                             "font-variant", &variant,
2052                             "font-weight", &weight,
2053                             "font-size", &size,
2054                             NULL);
2055
2056   description = pango_font_description_new ();
2057   /* xxx: Can we set all the families here somehow? */
2058   if (families)
2059     pango_font_description_set_family (description, families[0]);
2060   pango_font_description_set_size (description, round (size * PANGO_SCALE));
2061   pango_font_description_set_style (description, style);
2062   pango_font_description_set_variant (description, variant);
2063   pango_font_description_set_weight (description, weight);
2064
2065   g_strfreev (families);
2066
2067   g_value_take_boxed (value, description);
2068 }
2069
2070 static GParameter *
2071 unpack_border_color (const GValue *value,
2072                      guint        *n_params)
2073 {
2074   GParameter *parameter = g_new0 (GParameter, 4);
2075   GType type;
2076   
2077   type = G_VALUE_TYPE (value);
2078   if (type == G_TYPE_PTR_ARRAY)
2079     type = GTK_TYPE_SYMBOLIC_COLOR;
2080
2081   parameter[0].name = "border-top-color";
2082   g_value_init (&parameter[0].value, type);
2083   parameter[1].name = "border-right-color";
2084   g_value_init (&parameter[1].value, type);
2085   parameter[2].name = "border-bottom-color";
2086   g_value_init (&parameter[2].value, type);
2087   parameter[3].name = "border-left-color";
2088   g_value_init (&parameter[3].value, type);
2089
2090   if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
2091     {
2092       GPtrArray *array = g_value_get_boxed (value);
2093       guint i;
2094
2095       for (i = 0; i < 4; i++)
2096         g_value_set_boxed (&parameter[i].value, g_ptr_array_index (array, i));
2097     }
2098   else
2099     {
2100       /* can be RGBA or symbolic color */
2101       gpointer p = g_value_get_boxed (value);
2102
2103       g_value_set_boxed (&parameter[0].value, p);
2104       g_value_set_boxed (&parameter[1].value, p);
2105       g_value_set_boxed (&parameter[2].value, p);
2106       g_value_set_boxed (&parameter[3].value, p);
2107     }
2108
2109   *n_params = 4;
2110   return parameter;
2111 }
2112
2113 static void
2114 pack_border_color (GValue             *value,
2115                    GtkStyleProperties *props,
2116                    GtkStateFlags       state)
2117 {
2118   /* NB: We are a color property, so we have to resolve to a color here.
2119    * So we just resolve to a color. We pick one and stick to it.
2120    * Lesson learned: Don't query border-color shorthand, query the 
2121    * real properties instead. */
2122   g_value_unset (value);
2123   gtk_style_properties_get_property (props, "border-top-color", state, value);
2124 }
2125
2126 /*** UNSET FUNCS ***/
2127
2128 static void
2129 unset_font_description (GtkStyleProperties *props,
2130                         GtkStateFlags       state)
2131 {
2132   gtk_style_properties_unset_property (props, "font-family", state);
2133   gtk_style_properties_unset_property (props, "font-style", state);
2134   gtk_style_properties_unset_property (props, "font-variant", state);
2135   gtk_style_properties_unset_property (props, "font-weight", state);
2136   gtk_style_properties_unset_property (props, "font-size", state);
2137 }
2138
2139 static void
2140 unset_margin (GtkStyleProperties *props,
2141               GtkStateFlags       state)
2142 {
2143   gtk_style_properties_unset_property (props, "margin-top", state);
2144   gtk_style_properties_unset_property (props, "margin-right", state);
2145   gtk_style_properties_unset_property (props, "margin-bottom", state);
2146   gtk_style_properties_unset_property (props, "margin-left", state);
2147 }
2148
2149 static void
2150 unset_padding (GtkStyleProperties *props,
2151                GtkStateFlags       state)
2152 {
2153   gtk_style_properties_unset_property (props, "padding-top", state);
2154   gtk_style_properties_unset_property (props, "padding-right", state);
2155   gtk_style_properties_unset_property (props, "padding-bottom", state);
2156   gtk_style_properties_unset_property (props, "padding-left", state);
2157 }
2158
2159 static void
2160 unset_border_width (GtkStyleProperties *props,
2161                     GtkStateFlags       state)
2162 {
2163   gtk_style_properties_unset_property (props, "border-top-width", state);
2164   gtk_style_properties_unset_property (props, "border-right-width", state);
2165   gtk_style_properties_unset_property (props, "border-bottom-width", state);
2166   gtk_style_properties_unset_property (props, "border-left-width", state);
2167 }
2168
2169 static void
2170 unset_border_radius (GtkStyleProperties *props,
2171                      GtkStateFlags       state)
2172 {
2173   gtk_style_properties_unset_property (props, "border-top-right-radius", state);
2174   gtk_style_properties_unset_property (props, "border-bottom-right-radius", state);
2175   gtk_style_properties_unset_property (props, "border-bottom-left-radius", state);
2176   gtk_style_properties_unset_property (props, "border-top-left-radius", state);
2177 }
2178
2179 static void
2180 unset_border_color (GtkStyleProperties *props,
2181                     GtkStateFlags       state)
2182 {
2183   gtk_style_properties_unset_property (props, "border-top-color", state);
2184   gtk_style_properties_unset_property (props, "border-right-color", state);
2185   gtk_style_properties_unset_property (props, "border-bottom-color", state);
2186   gtk_style_properties_unset_property (props, "border-left-color", state);
2187 }
2188
2189 static void
2190 unset_border_image (GtkStyleProperties *props,
2191                     GtkStateFlags       state)
2192 {
2193   gtk_style_properties_unset_property (props, "border-image-source", state);
2194   gtk_style_properties_unset_property (props, "border-image-slice", state);
2195   gtk_style_properties_unset_property (props, "border-image-repeat", state);
2196   gtk_style_properties_unset_property (props, "border-image-width", state);
2197 }
2198
2199 /*** default values ***/
2200
2201 static void
2202 border_image_width_default_value (GtkStyleProperties *props,
2203                                   GtkStateFlags       state,
2204                                   GValue             *value)
2205 {
2206 }
2207
2208 static void
2209 background_color_default_value (GtkStyleProperties *props,
2210                                 GtkStateFlags       state,
2211                                 GValue             *value)
2212 {
2213   GdkRGBA transparent_black = { 0, 0, 0, 0 };
2214
2215   g_value_set_boxed (value, &transparent_black);
2216 }
2217
2218 static void
2219 border_color_default_value (GtkStyleProperties *props,
2220                             GtkStateFlags       state,
2221                             GValue             *value)
2222 {
2223   g_value_unset (value);
2224   gtk_style_properties_get_property (props, "color", state, value);
2225 }
2226
2227 /*** API ***/
2228
2229 static void
2230 css_string_funcs_init (void)
2231 {
2232   if (G_LIKELY (parse_funcs != NULL))
2233     return;
2234
2235   parse_funcs = g_hash_table_new (NULL, NULL);
2236   print_funcs = g_hash_table_new (NULL, NULL);
2237
2238   register_conversion_function (GDK_TYPE_RGBA,
2239                                 rgba_value_parse,
2240                                 rgba_value_print);
2241   register_conversion_function (GDK_TYPE_COLOR,
2242                                 color_value_parse,
2243                                 color_value_print);
2244   register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
2245                                 symbolic_color_value_parse,
2246                                 symbolic_color_value_print);
2247   register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
2248                                 font_description_value_parse,
2249                                 font_description_value_print);
2250   register_conversion_function (G_TYPE_BOOLEAN,
2251                                 boolean_value_parse,
2252                                 boolean_value_print);
2253   register_conversion_function (G_TYPE_INT,
2254                                 int_value_parse,
2255                                 int_value_print);
2256   register_conversion_function (G_TYPE_UINT,
2257                                 uint_value_parse,
2258                                 uint_value_print);
2259   register_conversion_function (G_TYPE_DOUBLE,
2260                                 double_value_parse,
2261                                 double_value_print);
2262   register_conversion_function (G_TYPE_FLOAT,
2263                                 float_value_parse,
2264                                 float_value_print);
2265   register_conversion_function (G_TYPE_STRING,
2266                                 string_value_parse,
2267                                 string_value_print);
2268   register_conversion_function (GTK_TYPE_THEMING_ENGINE,
2269                                 theming_engine_value_parse,
2270                                 theming_engine_value_print);
2271   register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
2272                                 animation_description_value_parse,
2273                                 animation_description_value_print);
2274   register_conversion_function (GTK_TYPE_BORDER,
2275                                 border_value_parse,
2276                                 border_value_print);
2277   register_conversion_function (GTK_TYPE_GRADIENT,
2278                                 gradient_value_parse,
2279                                 gradient_value_print);
2280   register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
2281                                 pattern_value_parse,
2282                                 pattern_value_print);
2283   register_conversion_function (GTK_TYPE_BORDER_IMAGE,
2284                                 border_image_value_parse,
2285                                 NULL);
2286   register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
2287                                 border_image_repeat_value_parse,
2288                                 border_image_repeat_value_print);
2289   register_conversion_function (GTK_TYPE_SHADOW,
2290                                 shadow_value_parse,
2291                                 shadow_value_print);
2292   register_conversion_function (G_TYPE_ENUM,
2293                                 enum_value_parse,
2294                                 enum_value_print);
2295   register_conversion_function (G_TYPE_FLAGS,
2296                                 flags_value_parse,
2297                                 flags_value_print);
2298   register_conversion_function (GTK_TYPE_CSS_BACKGROUND_REPEAT,
2299                                 background_repeat_value_parse,
2300                                 background_repeat_value_print);
2301 }
2302
2303 gboolean
2304 _gtk_style_property_parse_value (const GtkStyleProperty *property,
2305                                  GValue                 *value,
2306                                  GtkCssParser           *parser,
2307                                  GFile                  *base)
2308 {
2309   GtkStyleParseFunc func;
2310
2311   g_return_val_if_fail (value != NULL, FALSE);
2312   g_return_val_if_fail (parser != NULL, FALSE);
2313
2314   css_string_funcs_init ();
2315
2316   if (property)
2317     {
2318       if (_gtk_css_parser_try (parser, "none", TRUE))
2319         {
2320           /* Insert the default value, so it has an opportunity
2321            * to override other style providers when merged
2322            */
2323           g_param_value_set_default (property->pspec, value);
2324           return TRUE;
2325         }
2326       else if (property->property_parse_func)
2327         {
2328           GError *error = NULL;
2329           char *value_str;
2330           gboolean success;
2331           
2332           value_str = _gtk_css_parser_read_value (parser);
2333           if (value_str == NULL)
2334             return FALSE;
2335           
2336           success = (*property->property_parse_func) (value_str, value, &error);
2337
2338           g_free (value_str);
2339
2340           return success;
2341         }
2342
2343       func = property->parse_func;
2344     }
2345   else
2346     func = NULL;
2347
2348   if (func == NULL)
2349     func = g_hash_table_lookup (parse_funcs,
2350                                 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2351   if (func == NULL)
2352     func = g_hash_table_lookup (parse_funcs,
2353                                 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2354
2355   if (func == NULL)
2356     {
2357       _gtk_css_parser_error (parser,
2358                              "Cannot convert to type '%s'",
2359                              g_type_name (G_VALUE_TYPE (value)));
2360       return FALSE;
2361     }
2362
2363   return (*func) (parser, base, value);
2364 }
2365
2366 void
2367 _gtk_style_property_print_value (const GtkStyleProperty *property,
2368                                  const GValue           *value,
2369                                  GString                *string)
2370 {
2371   GtkStylePrintFunc func;
2372
2373   css_string_funcs_init ();
2374
2375   if (property)
2376     func = property->print_func;
2377   else
2378     func = NULL;
2379
2380   if (func == NULL)
2381     func = g_hash_table_lookup (print_funcs,
2382                                 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2383   if (func == NULL)
2384     func = g_hash_table_lookup (print_funcs,
2385                                 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2386
2387   if (func == NULL)
2388     {
2389       char *s = g_strdup_value_contents (value);
2390       g_string_append (string, s);
2391       g_free (s);
2392       return;
2393     }
2394   
2395   func (value, string);
2396 }
2397
2398 void
2399 _gtk_style_property_default_value (const GtkStyleProperty *property,
2400                                    GtkStyleProperties     *properties,
2401                                    GtkStateFlags           state,
2402                                    GValue                 *value)
2403 {
2404   if (property->default_value_func)
2405     property->default_value_func (properties, state, value);
2406   else if (property->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
2407     g_value_set_object (value, gtk_theming_engine_load (NULL));
2408   else if (property->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
2409     g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
2410   else if (property->pspec->value_type == GDK_TYPE_RGBA)
2411     {
2412       GdkRGBA color;
2413       gdk_rgba_parse (&color, "pink");
2414       g_value_set_boxed (value, &color);
2415     }
2416   else if (property->pspec->value_type == GTK_TYPE_BORDER)
2417     {
2418       g_value_take_boxed (value, gtk_border_new ());
2419     }
2420   else
2421     g_param_value_set_default (property->pspec, value);
2422 }
2423
2424 gboolean
2425 _gtk_style_property_is_inherit (const GtkStyleProperty *property)
2426 {
2427   g_return_val_if_fail (property != NULL, FALSE);
2428
2429   return property->flags & GTK_STYLE_PROPERTY_INHERIT ? TRUE : FALSE;
2430 }
2431
2432 static gboolean
2433 resolve_color (GtkStyleProperties *props,
2434                GValue             *value)
2435 {
2436   GdkRGBA color;
2437
2438   /* Resolve symbolic color to GdkRGBA */
2439   if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
2440     return FALSE;
2441
2442   /* Store it back, this is where GdkRGBA caching happens */
2443   g_value_unset (value);
2444   g_value_init (value, GDK_TYPE_RGBA);
2445   g_value_set_boxed (value, &color);
2446
2447   return TRUE;
2448 }
2449
2450 static gboolean
2451 resolve_color_rgb (GtkStyleProperties *props,
2452                    GValue             *value)
2453 {
2454   GdkColor color = { 0 };
2455   GdkRGBA rgba;
2456
2457   if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
2458     return FALSE;
2459
2460   color.red = rgba.red * 65535. + 0.5;
2461   color.green = rgba.green * 65535. + 0.5;
2462   color.blue = rgba.blue * 65535. + 0.5;
2463
2464   g_value_unset (value);
2465   g_value_init (value, GDK_TYPE_COLOR);
2466   g_value_set_boxed (value, &color);
2467
2468   return TRUE;
2469 }
2470
2471 static gboolean
2472 resolve_gradient (GtkStyleProperties *props,
2473                   GValue             *value)
2474 {
2475   cairo_pattern_t *gradient;
2476
2477   if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
2478     return FALSE;
2479
2480   /* Store it back, this is where cairo_pattern_t caching happens */
2481   g_value_unset (value);
2482   g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
2483   g_value_take_boxed (value, gradient);
2484
2485   return TRUE;
2486 }
2487
2488 static gboolean
2489 resolve_shadow (GtkStyleProperties *props,
2490                 GValue *value)
2491 {
2492   GtkShadow *resolved, *base;
2493
2494   base = g_value_get_boxed (value);
2495
2496   if (base == NULL)
2497     return TRUE;
2498   
2499   if (_gtk_shadow_get_resolved (base))
2500     return TRUE;
2501
2502   resolved = _gtk_shadow_resolve (base, props);
2503   if (resolved == NULL)
2504     return FALSE;
2505
2506   g_value_take_boxed (value, resolved);
2507
2508   return TRUE;
2509 }
2510
2511 void
2512 _gtk_style_property_resolve (const GtkStyleProperty *property,
2513                              GtkStyleProperties     *props,
2514                              GtkStateFlags           state,
2515                              GValue                 *val)
2516 {
2517   if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
2518     {
2519       if (property->pspec->value_type == GDK_TYPE_RGBA)
2520         {
2521           if (resolve_color (props, val))
2522             return;
2523         }
2524       else if (property->pspec->value_type == GDK_TYPE_COLOR)
2525         {
2526           if (resolve_color_rgb (props, val))
2527             return;
2528         }
2529       
2530       g_value_unset (val);
2531       g_value_init (val, property->pspec->value_type);
2532       _gtk_style_property_default_value (property, props, state, val);
2533     }
2534   else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
2535     {
2536       if (g_value_get_boxed (val) == NULL)
2537         _gtk_style_property_default_value (property, props, state, val);
2538     }
2539   else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
2540     {
2541       g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
2542
2543       if (!resolve_gradient (props, val))
2544         {
2545           g_value_unset (val);
2546           g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
2547           _gtk_style_property_default_value (property, props, state, val);
2548         }
2549     }
2550   else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
2551     {
2552       if (!resolve_shadow (props, val))
2553         _gtk_style_property_default_value (property, props, state, val);
2554     }
2555 }
2556
2557 gboolean
2558 _gtk_style_property_is_shorthand  (const GtkStyleProperty *property)
2559 {
2560   g_return_val_if_fail (property != NULL, FALSE);
2561
2562   return property->pack_func != NULL;
2563 }
2564
2565 GParameter *
2566 _gtk_style_property_unpack (const GtkStyleProperty *property,
2567                             const GValue           *value,
2568                             guint                  *n_params)
2569 {
2570   g_return_val_if_fail (property != NULL, NULL);
2571   g_return_val_if_fail (property->unpack_func != NULL, NULL);
2572   g_return_val_if_fail (value != NULL, NULL);
2573   g_return_val_if_fail (n_params != NULL, NULL);
2574
2575   return property->unpack_func (value, n_params);
2576 }
2577
2578 void
2579 _gtk_style_property_pack (const GtkStyleProperty *property,
2580                           GtkStyleProperties     *props,
2581                           GtkStateFlags           state,
2582                           GValue                 *value)
2583 {
2584   g_return_if_fail (property != NULL);
2585   g_return_if_fail (property->pack_func != NULL);
2586   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2587   g_return_if_fail (G_IS_VALUE (value));
2588
2589   property->pack_func (value, props, state);
2590 }
2591
2592 static void
2593 gtk_style_property_init (void)
2594 {
2595   if (G_LIKELY (properties))
2596     return;
2597
2598   /* stuff is never freed, so no need for free functions */
2599   properties = g_hash_table_new (g_str_hash, g_str_equal);
2600
2601   /* note that gtk_style_properties_register_property() calls this function,
2602    * so make sure we're sanely inited to avoid infloops */
2603
2604   _gtk_style_property_register           (g_param_spec_boxed ("color",
2605                                           "Foreground color",
2606                                           "Foreground color",
2607                                           GDK_TYPE_RGBA, 0),
2608                                           GTK_STYLE_PROPERTY_INHERIT,
2609                                           NULL,
2610                                           NULL,
2611                                           NULL,
2612                                           NULL,
2613                                           NULL,
2614                                           NULL,
2615                                           NULL);
2616   _gtk_style_property_register           (g_param_spec_boxed ("background-color",
2617                                           "Background color",
2618                                           "Background color",
2619                                           GDK_TYPE_RGBA, 0),
2620                                           0,
2621                                           NULL,
2622                                           NULL,
2623                                           NULL,
2624                                           transparent_color_value_parse,
2625                                           NULL,
2626                                           background_color_default_value,
2627                                           NULL);
2628
2629   _gtk_style_property_register           (g_param_spec_boxed ("font-family",
2630                                                               "Font family",
2631                                                               "Font family",
2632                                                               G_TYPE_STRV, 0),
2633                                           GTK_STYLE_PROPERTY_INHERIT,
2634                                           NULL,
2635                                           NULL,
2636                                           NULL,
2637                                           font_family_parse,
2638                                           font_family_value_print,
2639                                           NULL,
2640                                           NULL);
2641   _gtk_style_property_register           (g_param_spec_enum ("font-style",
2642                                                              "Font style",
2643                                                              "Font style",
2644                                                              PANGO_TYPE_STYLE,
2645                                                              PANGO_STYLE_NORMAL, 0),
2646                                           GTK_STYLE_PROPERTY_INHERIT,
2647                                           NULL,
2648                                           NULL,
2649                                           NULL,
2650                                           NULL,
2651                                           NULL,
2652                                           NULL,
2653                                           NULL);
2654   _gtk_style_property_register           (g_param_spec_enum ("font-variant",
2655                                                              "Font variant",
2656                                                              "Font variant",
2657                                                              PANGO_TYPE_VARIANT,
2658                                                              PANGO_VARIANT_NORMAL, 0),
2659                                           GTK_STYLE_PROPERTY_INHERIT,
2660                                           NULL,
2661                                           NULL,
2662                                           NULL,
2663                                           NULL,
2664                                           NULL,
2665                                           NULL,
2666                                           NULL);
2667   /* xxx: need to parse this properly, ie parse the numbers */
2668   _gtk_style_property_register           (g_param_spec_enum ("font-weight",
2669                                                              "Font weight",
2670                                                              "Font weight",
2671                                                              PANGO_TYPE_WEIGHT,
2672                                                              PANGO_WEIGHT_NORMAL, 0),
2673                                           GTK_STYLE_PROPERTY_INHERIT,
2674                                           NULL,
2675                                           NULL,
2676                                           NULL,
2677                                           NULL,
2678                                           NULL,
2679                                           NULL,
2680                                           NULL);
2681   _gtk_style_property_register           (g_param_spec_double ("font-size",
2682                                                                "Font size",
2683                                                                "Font size",
2684                                                                0, G_MAXDOUBLE, 0, 0),
2685                                           GTK_STYLE_PROPERTY_INHERIT,
2686                                           NULL,
2687                                           NULL,
2688                                           NULL,
2689                                           NULL,
2690                                           NULL,
2691                                           NULL,
2692                                           NULL);
2693   _gtk_style_property_register           (g_param_spec_boxed ("font",
2694                                                               "Font Description",
2695                                                               "Font Description",
2696                                                               PANGO_TYPE_FONT_DESCRIPTION, 0),
2697                                           GTK_STYLE_PROPERTY_INHERIT,
2698                                           NULL,
2699                                           unpack_font_description,
2700                                           pack_font_description,
2701                                           font_description_value_parse,
2702                                           font_description_value_print,
2703                                           NULL,
2704                                           unset_font_description);
2705
2706   _gtk_style_property_register           (g_param_spec_boxed ("text-shadow",
2707                                                               "Text shadow",
2708                                                               "Text shadow",
2709                                                               GTK_TYPE_SHADOW, 0),
2710                                           GTK_STYLE_PROPERTY_INHERIT,
2711                                           NULL,
2712                                           NULL,
2713                                           NULL,
2714                                           NULL,
2715                                           NULL,
2716                                           NULL,
2717                                           NULL);
2718
2719   _gtk_style_property_register           (g_param_spec_boxed ("icon-shadow",
2720                                                               "Icon shadow",
2721                                                               "Icon shadow",
2722                                                               GTK_TYPE_SHADOW, 0),
2723                                           GTK_STYLE_PROPERTY_INHERIT,
2724                                           NULL,
2725                                           NULL,
2726                                           NULL,
2727                                           NULL,
2728                                           NULL,
2729                                           NULL,
2730                                           NULL);
2731
2732   gtk_style_properties_register_property (NULL,
2733                                           g_param_spec_boxed ("box-shadow",
2734                                                               "Box shadow",
2735                                                               "Box shadow",
2736                                                               GTK_TYPE_SHADOW, 0));
2737   gtk_style_properties_register_property (NULL,
2738                                           g_param_spec_int ("margin-top",
2739                                                             "margin top",
2740                                                             "Margin at top",
2741                                                             0, G_MAXINT, 0, 0));
2742   gtk_style_properties_register_property (NULL,
2743                                           g_param_spec_int ("margin-left",
2744                                                             "margin left",
2745                                                             "Margin at left",
2746                                                             0, G_MAXINT, 0, 0));
2747   gtk_style_properties_register_property (NULL,
2748                                           g_param_spec_int ("margin-bottom",
2749                                                             "margin bottom",
2750                                                             "Margin at bottom",
2751                                                             0, G_MAXINT, 0, 0));
2752   gtk_style_properties_register_property (NULL,
2753                                           g_param_spec_int ("margin-right",
2754                                                             "margin right",
2755                                                             "Margin at right",
2756                                                             0, G_MAXINT, 0, 0));
2757   _gtk_style_property_register           (g_param_spec_boxed ("margin",
2758                                                               "Margin",
2759                                                               "Margin",
2760                                                               GTK_TYPE_BORDER, 0),
2761                                           0,
2762                                           NULL,
2763                                           unpack_margin,
2764                                           pack_margin,
2765                                           NULL,
2766                                           NULL,
2767                                           NULL,
2768                                           unset_margin);
2769   gtk_style_properties_register_property (NULL,
2770                                           g_param_spec_int ("padding-top",
2771                                                             "padding top",
2772                                                             "Padding at top",
2773                                                             0, G_MAXINT, 0, 0));
2774   gtk_style_properties_register_property (NULL,
2775                                           g_param_spec_int ("padding-left",
2776                                                             "padding left",
2777                                                             "Padding at left",
2778                                                             0, G_MAXINT, 0, 0));
2779   gtk_style_properties_register_property (NULL,
2780                                           g_param_spec_int ("padding-bottom",
2781                                                             "padding bottom",
2782                                                             "Padding at bottom",
2783                                                             0, G_MAXINT, 0, 0));
2784   gtk_style_properties_register_property (NULL,
2785                                           g_param_spec_int ("padding-right",
2786                                                             "padding right",
2787                                                             "Padding at right",
2788                                                             0, G_MAXINT, 0, 0));
2789   _gtk_style_property_register           (g_param_spec_boxed ("padding",
2790                                                               "Padding",
2791                                                               "Padding",
2792                                                               GTK_TYPE_BORDER, 0),
2793                                           0,
2794                                           NULL,
2795                                           unpack_padding,
2796                                           pack_padding,
2797                                           NULL,
2798                                           NULL,
2799                                           NULL,
2800                                           unset_padding);
2801   gtk_style_properties_register_property (NULL,
2802                                           g_param_spec_int ("border-top-width",
2803                                                             "border top width",
2804                                                             "Border width at top",
2805                                                             0, G_MAXINT, 0, 0));
2806   gtk_style_properties_register_property (NULL,
2807                                           g_param_spec_int ("border-left-width",
2808                                                             "border left width",
2809                                                             "Border width at left",
2810                                                             0, G_MAXINT, 0, 0));
2811   gtk_style_properties_register_property (NULL,
2812                                           g_param_spec_int ("border-bottom-width",
2813                                                             "border bottom width",
2814                                                             "Border width at bottom",
2815                                                             0, G_MAXINT, 0, 0));
2816   gtk_style_properties_register_property (NULL,
2817                                           g_param_spec_int ("border-right-width",
2818                                                             "border right width",
2819                                                             "Border width at right",
2820                                                             0, G_MAXINT, 0, 0));
2821   _gtk_style_property_register           (g_param_spec_boxed ("border-width",
2822                                                               "Border width",
2823                                                               "Border width, in pixels",
2824                                                               GTK_TYPE_BORDER, 0),
2825                                           0,
2826                                           NULL,
2827                                           unpack_border_width,
2828                                           pack_border_width,
2829                                           NULL,
2830                                           NULL,
2831                                           NULL,
2832                                           unset_border_width);
2833
2834   _gtk_style_property_register           (g_param_spec_boxed ("border-top-left-radius",
2835                                                               "Border top left radius",
2836                                                               "Border radius of top left corner, in pixels",
2837                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2838                                           0,
2839                                           NULL,
2840                                           NULL,
2841                                           NULL,
2842                                           border_corner_radius_value_parse,
2843                                           border_corner_radius_value_print,
2844                                           NULL,
2845                                           NULL);
2846   _gtk_style_property_register           (g_param_spec_boxed ("border-top-right-radius",
2847                                                               "Border top right radius",
2848                                                               "Border radius of top right corner, in pixels",
2849                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2850                                           0,
2851                                           NULL,
2852                                           NULL,
2853                                           NULL,
2854                                           border_corner_radius_value_parse,
2855                                           border_corner_radius_value_print,
2856                                           NULL,
2857                                           NULL);
2858   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-right-radius",
2859                                                               "Border bottom right radius",
2860                                                               "Border radius of bottom right corner, in pixels",
2861                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2862                                           0,
2863                                           NULL,
2864                                           NULL,
2865                                           NULL,
2866                                           border_corner_radius_value_parse,
2867                                           border_corner_radius_value_print,
2868                                           NULL,
2869                                           NULL);
2870   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-left-radius",
2871                                                               "Border bottom left radius",
2872                                                               "Border radius of bottom left corner, in pixels",
2873                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2874                                           0,
2875                                           NULL,
2876                                           NULL,
2877                                           NULL,
2878                                           border_corner_radius_value_parse,
2879                                           border_corner_radius_value_print,
2880                                           NULL,
2881                                           NULL);
2882   _gtk_style_property_register           (g_param_spec_int ("border-radius",
2883                                                             "Border radius",
2884                                                             "Border radius, in pixels",
2885                                                             0, G_MAXINT, 0, 0),
2886                                           0,
2887                                           NULL,
2888                                           unpack_border_radius,
2889                                           pack_border_radius,
2890                                           border_radius_value_parse,
2891                                           border_radius_value_print,
2892                                           NULL,
2893                                           unset_border_radius);
2894
2895   gtk_style_properties_register_property (NULL,
2896                                           g_param_spec_enum ("border-style",
2897                                                              "Border style",
2898                                                              "Border style",
2899                                                              GTK_TYPE_BORDER_STYLE,
2900                                                              GTK_BORDER_STYLE_NONE, 0));
2901   _gtk_style_property_register           (g_param_spec_boxed ("border-top-color",
2902                                                               "Border top color",
2903                                                               "Border top color",
2904                                                               GDK_TYPE_RGBA, 0),
2905                                           0,
2906                                           NULL,
2907                                           NULL,
2908                                           NULL,
2909                                           transparent_color_value_parse,
2910                                           NULL,
2911                                           border_color_default_value,
2912                                           NULL);
2913   _gtk_style_property_register           (g_param_spec_boxed ("border-right-color",
2914                                                               "Border right color",
2915                                                               "Border right color",
2916                                                               GDK_TYPE_RGBA, 0),
2917                                           0,
2918                                           NULL,
2919                                           NULL,
2920                                           NULL,
2921                                           transparent_color_value_parse,
2922                                           NULL,
2923                                           border_color_default_value,
2924                                           NULL);
2925   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-color",
2926                                                               "Border bottom color",
2927                                                               "Border bottom color",
2928                                                               GDK_TYPE_RGBA, 0),
2929                                           0,
2930                                           NULL,
2931                                           NULL,
2932                                           NULL,
2933                                           transparent_color_value_parse,
2934                                           NULL,
2935                                           border_color_default_value,
2936                                           NULL);
2937   _gtk_style_property_register           (g_param_spec_boxed ("border-left-color",
2938                                                               "Border left color",
2939                                                               "Border left color",
2940                                                               GDK_TYPE_RGBA, 0),
2941                                           0,
2942                                           NULL,
2943                                           NULL,
2944                                           NULL,
2945                                           transparent_color_value_parse,
2946                                           NULL,
2947                                           border_color_default_value,
2948                                           NULL);
2949   _gtk_style_property_register           (g_param_spec_boxed ("border-color",
2950                                                               "Border color",
2951                                                               "Border color",
2952                                                               GDK_TYPE_RGBA, 0),
2953                                           0,
2954                                           NULL,
2955                                           unpack_border_color,
2956                                           pack_border_color,
2957                                           border_color_shorthand_value_parse,
2958                                           NULL,
2959                                           NULL,
2960                                           unset_border_color);
2961
2962   gtk_style_properties_register_property (NULL,
2963                                           g_param_spec_boxed ("background-image",
2964                                                               "Background Image",
2965                                                               "Background Image",
2966                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
2967   gtk_style_properties_register_property (NULL,
2968                                           g_param_spec_boxed ("background-repeat",
2969                                                               "Background repeat",
2970                                                               "Background repeat",
2971                                                               GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
2972
2973   gtk_style_properties_register_property (NULL,
2974                                           g_param_spec_boxed ("border-image-source",
2975                                                               "Border image source",
2976                                                               "Border image source",
2977                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
2978   gtk_style_properties_register_property (NULL,
2979                                           g_param_spec_boxed ("border-image-repeat",
2980                                                               "Border image repeat",
2981                                                               "Border image repeat",
2982                                                               GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
2983   gtk_style_properties_register_property (NULL,
2984                                           g_param_spec_boxed ("border-image-slice",
2985                                                               "Border image slice",
2986                                                               "Border image slice",
2987                                                               GTK_TYPE_BORDER, 0));
2988   _gtk_style_property_register           (g_param_spec_boxed ("border-image-width",
2989                                                               "Border image width",
2990                                                               "Border image width",
2991                                                               GTK_TYPE_BORDER, 0),
2992                                           0,
2993                                           NULL,
2994                                           NULL,
2995                                           NULL,
2996                                           NULL,
2997                                           NULL,
2998                                           border_image_width_default_value,
2999                                           NULL);
3000   _gtk_style_property_register           (g_param_spec_boxed ("border-image",
3001                                                               "Border Image",
3002                                                               "Border Image",
3003                                                               GTK_TYPE_BORDER_IMAGE, 0),
3004                                           0,
3005                                           NULL,
3006                                           _gtk_border_image_unpack,
3007                                           _gtk_border_image_pack,
3008                                           NULL,
3009                                           NULL,
3010                                           NULL,
3011                                           unset_border_image);
3012   gtk_style_properties_register_property (NULL,
3013                                           g_param_spec_object ("engine",
3014                                                                "Theming Engine",
3015                                                                "Theming Engine",
3016                                                                GTK_TYPE_THEMING_ENGINE, 0));
3017   gtk_style_properties_register_property (NULL,
3018                                           g_param_spec_boxed ("transition",
3019                                                               "Transition animation description",
3020                                                               "Transition animation description",
3021                                                               GTK_TYPE_ANIMATION_DESCRIPTION, 0));
3022
3023   /* Private property holding the binding sets */
3024   _gtk_style_property_register           (g_param_spec_boxed ("gtk-key-bindings",
3025                                                               "Key bindings",
3026                                                               "Key bindings",
3027                                                               G_TYPE_PTR_ARRAY, 0),
3028                                           0,
3029                                           NULL,
3030                                           NULL,
3031                                           NULL,
3032                                           bindings_value_parse,
3033                                           bindings_value_print,
3034                                           NULL,
3035                                           NULL);
3036 }
3037
3038 const GtkStyleProperty *
3039 _gtk_style_property_lookup (const char *name)
3040 {
3041   gtk_style_property_init ();
3042
3043   return g_hash_table_lookup (properties, name);
3044 }
3045
3046 void
3047 _gtk_style_property_register (GParamSpec               *pspec,
3048                               GtkStylePropertyFlags     flags,
3049                               GtkStylePropertyParser    property_parse_func,
3050                               GtkStyleUnpackFunc        unpack_func,
3051                               GtkStylePackFunc          pack_func,
3052                               GtkStyleParseFunc         parse_func,
3053                               GtkStylePrintFunc         print_func,
3054                               GtkStyleDefaultValueFunc  default_value_func,
3055                               GtkStyleUnsetFunc         unset_func)
3056 {
3057   const GtkStyleProperty *existing;
3058   GtkStyleProperty *node;
3059
3060   g_return_if_fail ((pack_func == NULL) == (unpack_func == NULL));
3061
3062   gtk_style_property_init ();
3063
3064   existing = _gtk_style_property_lookup (pspec->name);
3065   if (existing != NULL)
3066     {
3067       g_warning ("Property \"%s\" was already registered with type %s",
3068                  pspec->name, g_type_name (existing->pspec->value_type));
3069       return;
3070     }
3071
3072   node = g_slice_new0 (GtkStyleProperty);
3073   node->flags = flags;
3074   node->pspec = pspec;
3075   node->property_parse_func = property_parse_func;
3076   node->pack_func = pack_func;
3077   node->unpack_func = unpack_func;
3078   node->parse_func = parse_func;
3079   node->print_func = print_func;
3080   node->default_value_func = default_value_func;
3081   node->unset_func = unset_func;
3082
3083   /* pspec owns name */
3084   g_hash_table_insert (properties, (gchar *)pspec->name, node);
3085 }