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