]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperty.c
css: Split generic parse/print functions out
[~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 "gtkcssstylefuncsprivate.h"
36 #include "gtkcssstylepropertyprivate.h"
37 #include "gtkcsstypesprivate.h"
38 #include "gtkintl.h"
39 #include "gtkprivatetypebuiltins.h"
40 #include "gtkstylepropertiesprivate.h"
41
42 /* the actual parsers we have */
43 #include "gtkanimationdescription.h"
44 #include "gtkbindings.h"
45 #include "gtkgradient.h"
46 #include "gtkshadowprivate.h"
47 #include "gtkthemingengine.h"
48 #include "gtktypebuiltins.h"
49 #include "gtkwin32themeprivate.h"
50
51 /* this is in case round() is not provided by the compiler, 
52  * such as in the case of C89 compilers, like MSVC
53  */
54 #include "fallback-c89.c"
55
56 enum {
57   PROP_0,
58   PROP_NAME,
59   PROP_VALUE_TYPE
60 };
61
62 G_DEFINE_ABSTRACT_TYPE (GtkStyleProperty, _gtk_style_property, G_TYPE_OBJECT)
63
64 static void
65 gtk_style_property_finalize (GObject *object)
66 {
67   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
68
69   g_warning ("finalizing %s `%s', how could this happen?", G_OBJECT_TYPE_NAME (object), property->name);
70
71   G_OBJECT_CLASS (_gtk_style_property_parent_class)->finalize (object);
72 }
73
74 static void
75 gtk_style_property_set_property (GObject      *object,
76                                  guint         prop_id,
77                                  const GValue *value,
78                                  GParamSpec   *pspec)
79 {
80   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
81   GtkStylePropertyClass *klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
82
83   switch (prop_id)
84     {
85     case PROP_NAME:
86       property->name = g_value_dup_string (value);
87       g_assert (property->name);
88       g_assert (g_hash_table_lookup (klass->properties, property->name) == NULL);
89       g_hash_table_insert (klass->properties, property->name, property);
90       break;
91     case PROP_VALUE_TYPE:
92       property->value_type = g_value_get_gtype (value);
93       break;
94     default:
95       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
96       break;
97     }
98 }
99
100 static void
101 gtk_style_property_get_property (GObject    *object,
102                                  guint       prop_id,
103                                  GValue     *value,
104                                  GParamSpec *pspec)
105 {
106   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
107
108   switch (prop_id)
109     {
110     case PROP_NAME:
111       g_value_set_string (value, property->name);
112       break;
113     case PROP_VALUE_TYPE:
114       g_value_set_gtype (value, property->value_type);
115       break;
116     default:
117       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118       break;
119     }
120 }
121
122 static void
123 _gtk_style_property_class_init (GtkStylePropertyClass *klass)
124 {
125   GObjectClass *object_class = G_OBJECT_CLASS (klass);
126
127   object_class->finalize = gtk_style_property_finalize;
128   object_class->set_property = gtk_style_property_set_property;
129   object_class->get_property = gtk_style_property_get_property;
130
131   g_object_class_install_property (object_class,
132                                    PROP_NAME,
133                                    g_param_spec_string ("name",
134                                                         P_("Property name"),
135                                                         P_("The name of the property"),
136                                                         NULL,
137                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
138   g_object_class_install_property (object_class,
139                                    PROP_VALUE_TYPE,
140                                    g_param_spec_gtype ("value-type",
141                                                        P_("Value type"),
142                                                        P_("The value type returned by GtkStyleContext"),
143                                                        G_TYPE_NONE,
144                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
145
146   klass->properties = g_hash_table_new (g_str_hash, g_str_equal);
147 }
148
149 static void
150 _gtk_style_property_init (GtkStyleProperty *property)
151 {
152   property->value_type = G_TYPE_NONE;
153 }
154
155 static void
156 string_append_double (GString *string,
157                       double   d)
158 {
159   char buf[G_ASCII_DTOSTR_BUF_SIZE];
160
161   g_ascii_dtostr (buf, sizeof (buf), d);
162   g_string_append (string, buf);
163 }
164
165 static void
166 string_append_string (GString    *str,
167                       const char *string)
168 {
169   gsize len;
170
171   g_string_append_c (str, '"');
172
173   do {
174     len = strcspn (string, "\"\n\r\f");
175     g_string_append (str, string);
176     string += len;
177     switch (*string)
178       {
179       case '\0':
180         break;
181       case '\n':
182         g_string_append (str, "\\A ");
183         break;
184       case '\r':
185         g_string_append (str, "\\D ");
186         break;
187       case '\f':
188         g_string_append (str, "\\C ");
189         break;
190       case '\"':
191         g_string_append (str, "\\\"");
192         break;
193       default:
194         g_assert_not_reached ();
195         break;
196       }
197   } while (*string);
198
199   g_string_append_c (str, '"');
200 }
201
202 /*** IMPLEMENTATIONS ***/
203
204 static void
205 enum_print (int         value,
206             GType       type,
207             GString    *string)
208 {
209   GEnumClass *enum_class;
210   GEnumValue *enum_value;
211
212   enum_class = g_type_class_ref (type);
213   enum_value = g_enum_get_value (enum_class, value);
214
215   g_string_append (string, enum_value->value_nick);
216
217   g_type_class_unref (enum_class);
218 }
219
220 static gboolean
221 font_family_parse (GtkCssParser *parser,
222                    GFile        *base,
223                    GValue       *value)
224 {
225   GPtrArray *names;
226   char *name;
227
228   /* We don't special case generic families. Pango should do
229    * that for us */
230
231   names = g_ptr_array_new ();
232
233   do {
234     name = _gtk_css_parser_try_ident (parser, TRUE);
235     if (name)
236       {
237         GString *string = g_string_new (name);
238         g_free (name);
239         while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
240           {
241             g_string_append_c (string, ' ');
242             g_string_append (string, name);
243             g_free (name);
244           }
245         name = g_string_free (string, FALSE);
246       }
247     else 
248       {
249         name = _gtk_css_parser_read_string (parser);
250         if (name == NULL)
251           {
252             g_ptr_array_free (names, TRUE);
253             return FALSE;
254           }
255       }
256
257     g_ptr_array_add (names, name);
258   } while (_gtk_css_parser_try (parser, ",", TRUE));
259
260   /* NULL-terminate array */
261   g_ptr_array_add (names, NULL);
262   g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
263   return TRUE;
264 }
265
266 static void
267 font_family_value_print (const GValue *value,
268                          GString      *string)
269 {
270   const char **names = g_value_get_boxed (value);
271
272   if (names == NULL || *names == NULL)
273     {
274       g_string_append (string, "none");
275       return;
276     }
277
278   string_append_string (string, *names);
279   names++;
280   while (*names)
281     {
282       g_string_append (string, ", ");
283       string_append_string (string, *names);
284       names++;
285     }
286 }
287
288 static gboolean 
289 bindings_value_parse (GtkCssParser *parser,
290                       GFile        *base,
291                       GValue       *value)
292 {
293   GPtrArray *array;
294   GtkBindingSet *binding_set;
295   char *name;
296
297   array = g_ptr_array_new ();
298
299   do {
300       name = _gtk_css_parser_try_ident (parser, TRUE);
301       if (name == NULL)
302         {
303           _gtk_css_parser_error (parser, "Not a valid binding name");
304           g_ptr_array_free (array, TRUE);
305           return FALSE;
306         }
307
308       binding_set = gtk_binding_set_find (name);
309
310       if (!binding_set)
311         {
312           _gtk_css_parser_error (parser, "No binding set named '%s'", name);
313           g_free (name);
314           continue;
315         }
316
317       g_ptr_array_add (array, binding_set);
318       g_free (name);
319     }
320   while (_gtk_css_parser_try (parser, ",", TRUE));
321
322   g_value_take_boxed (value, array);
323
324   return TRUE;
325 }
326
327 static void
328 bindings_value_print (const GValue *value,
329                       GString      *string)
330 {
331   GPtrArray *array;
332   guint i;
333
334   array = g_value_get_boxed (value);
335
336   for (i = 0; i < array->len; i++)
337     {
338       GtkBindingSet *binding_set = g_ptr_array_index (array, i);
339
340       if (i > 0)
341         g_string_append (string, ", ");
342       g_string_append (string, binding_set->set_name);
343     }
344 }
345
346 static gboolean 
347 border_corner_radius_value_parse (GtkCssParser *parser,
348                                   GFile        *base,
349                                   GValue       *value)
350 {
351   GtkCssBorderCornerRadius corner;
352
353   if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
354     {
355       _gtk_css_parser_error (parser, "Expected a number");
356       return FALSE;
357     }
358   else if (corner.horizontal < 0)
359     goto negative;
360
361   if (!_gtk_css_parser_try_double (parser, &corner.vertical))
362     corner.vertical = corner.horizontal;
363   else if (corner.vertical < 0)
364     goto negative;
365
366   g_value_set_boxed (value, &corner);
367   return TRUE;
368
369 negative:
370   _gtk_css_parser_error (parser, "Border radius values cannot be negative");
371   return FALSE;
372 }
373
374 static void
375 border_corner_radius_value_print (const GValue *value,
376                                   GString      *string)
377 {
378   GtkCssBorderCornerRadius *corner;
379
380   corner = g_value_get_boxed (value);
381
382   if (corner == NULL)
383     {
384       g_string_append (string, "none");
385       return;
386     }
387
388   string_append_double (string, corner->horizontal);
389   if (corner->horizontal != corner->vertical)
390     {
391       g_string_append_c (string, ' ');
392       string_append_double (string, corner->vertical);
393     }
394 }
395
396 /*** API ***/
397
398 gboolean
399 _gtk_style_property_parse_value (GtkStyleProperty *property,
400                                  GValue           *value,
401                                  GtkCssParser     *parser,
402                                  GFile            *base)
403 {
404   g_return_val_if_fail (value != NULL, FALSE);
405   g_return_val_if_fail (parser != NULL, FALSE);
406
407   if (property)
408     {
409       if (_gtk_css_parser_try (parser, "initial", TRUE))
410         {
411           /* the initial value can be explicitly specified with the
412            * â€˜initial’ keyword which all properties accept.
413            */
414           g_value_unset (value);
415           g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
416           g_value_set_enum (value, GTK_CSS_INITIAL);
417           return TRUE;
418         }
419       else if (_gtk_css_parser_try (parser, "inherit", TRUE))
420         {
421           /* All properties accept the â€˜inherit’ value which
422            * explicitly specifies that the value will be determined
423            * by inheritance. The â€˜inherit’ value can be used to
424            * strengthen inherited values in the cascade, and it can
425            * also be used on properties that are not normally inherited.
426            */
427           g_value_unset (value);
428           g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
429           g_value_set_enum (value, GTK_CSS_INHERIT);
430           return TRUE;
431         }
432       else if (property->property_parse_func)
433         {
434           GError *error = NULL;
435           char *value_str;
436           gboolean success;
437           
438           value_str = _gtk_css_parser_read_value (parser);
439           if (value_str == NULL)
440             return FALSE;
441           
442           success = (*property->property_parse_func) (value_str, value, &error);
443
444           g_free (value_str);
445
446           return success;
447         }
448
449       if (property->parse_func)
450         return (* property->parse_func) (parser, base, value);
451     }
452
453   return _gtk_css_style_parse_value (value, parser, base);
454 }
455
456 void
457 _gtk_style_property_print_value (GtkStyleProperty *property,
458                                  const GValue     *value,
459                                  GString          *string)
460 {
461   if (G_VALUE_HOLDS (value, GTK_TYPE_CSS_SPECIAL_VALUE))
462     enum_print (g_value_get_enum (value), GTK_TYPE_CSS_SPECIAL_VALUE, string);
463   else if (property && property->print_func)
464     (* property->print_func) (value, string);
465   else
466     _gtk_css_style_print_value (value, string);
467 }
468
469 static void
470 _gtk_style_property_default_value (GtkStyleProperty   *property,
471                                    GtkStyleProperties *properties,
472                                    GtkStateFlags       state,
473                                    GValue             *value)
474 {
475   g_value_copy (_gtk_css_style_property_get_initial_value (GTK_CSS_STYLE_PROPERTY (property)), value);
476 }
477
478 static gboolean
479 resolve_color (GtkStyleProperties *props,
480                GValue             *value)
481 {
482   GdkRGBA color;
483
484   /* Resolve symbolic color to GdkRGBA */
485   if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
486     return FALSE;
487
488   /* Store it back, this is where GdkRGBA caching happens */
489   g_value_unset (value);
490   g_value_init (value, GDK_TYPE_RGBA);
491   g_value_set_boxed (value, &color);
492
493   return TRUE;
494 }
495
496 static gboolean
497 resolve_color_rgb (GtkStyleProperties *props,
498                    GValue             *value)
499 {
500   GdkColor color = { 0 };
501   GdkRGBA rgba;
502
503   if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
504     return FALSE;
505
506   color.red = rgba.red * 65535. + 0.5;
507   color.green = rgba.green * 65535. + 0.5;
508   color.blue = rgba.blue * 65535. + 0.5;
509
510   g_value_unset (value);
511   g_value_init (value, GDK_TYPE_COLOR);
512   g_value_set_boxed (value, &color);
513
514   return TRUE;
515 }
516
517 static gboolean
518 resolve_win32_theme_part (GtkStyleProperties *props,
519                           GValue             *value,
520                           GValue             *value_out,
521                           GtkStylePropertyContext *context)
522 {
523   GtkWin32ThemePart  *part;
524   cairo_pattern_t *pattern;
525
526   part = g_value_get_boxed (value);
527   if (part == NULL)
528     return FALSE;
529
530   pattern = _gtk_win32_theme_part_render (part, context->width, context->height);
531
532   g_value_take_boxed (value_out, pattern);
533
534   return TRUE;
535 }
536
537
538 static gboolean
539 resolve_gradient (GtkStyleProperties *props,
540                   GValue             *value)
541 {
542   cairo_pattern_t *gradient;
543
544   if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
545     return FALSE;
546
547   /* Store it back, this is where cairo_pattern_t caching happens */
548   g_value_unset (value);
549   g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
550   g_value_take_boxed (value, gradient);
551
552   return TRUE;
553 }
554
555 static gboolean
556 resolve_shadow (GtkStyleProperties *props,
557                 GValue *value)
558 {
559   GtkShadow *resolved, *base;
560
561   base = g_value_get_boxed (value);
562
563   if (base == NULL)
564     return TRUE;
565   
566   if (_gtk_shadow_get_resolved (base))
567     return TRUE;
568
569   resolved = _gtk_shadow_resolve (base, props);
570   if (resolved == NULL)
571     return FALSE;
572
573   g_value_take_boxed (value, resolved);
574
575   return TRUE;
576 }
577
578 static void
579 _gtk_style_property_resolve (GtkStyleProperty       *property,
580                              GtkStyleProperties     *props,
581                              GtkStateFlags           state,
582                              GtkStylePropertyContext *context,
583                              GValue                 *val,
584                              GValue                 *val_out)
585 {
586   if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_SPECIAL_VALUE)
587     {
588       GtkCssSpecialValue special = g_value_get_enum (val);
589
590       g_value_unset (val);
591       switch (special)
592         {
593         case GTK_CSS_CURRENT_COLOR:
594           g_assert (property->pspec->value_type == GDK_TYPE_RGBA);
595           gtk_style_properties_get_property (props, "color", state, val);
596           break;
597         case GTK_CSS_INHERIT:
598         case GTK_CSS_INITIAL:
599         default:
600           g_assert_not_reached ();
601         }
602     }
603   else if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
604     {
605       if (property->pspec->value_type == GDK_TYPE_RGBA)
606         {
607           if (resolve_color (props, val))
608             goto out;
609         }
610       else if (property->pspec->value_type == GDK_TYPE_COLOR)
611         {
612           if (resolve_color_rgb (props, val))
613             goto out;
614         }
615       
616       g_value_unset (val);
617       g_value_init (val, property->pspec->value_type);
618       _gtk_style_property_default_value (property, props, state, val);
619     }
620   else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
621     {
622       if (g_value_get_boxed (val) == NULL)
623         _gtk_style_property_default_value (property, props, state, val);
624     }
625   else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
626     {
627       g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
628
629       if (!resolve_gradient (props, val))
630         {
631           g_value_unset (val);
632           g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
633           _gtk_style_property_default_value (property, props, state, val);
634         }
635     }
636   else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
637     {
638       if (!resolve_shadow (props, val))
639         _gtk_style_property_default_value (property, props, state, val);
640     }
641   else if (G_VALUE_TYPE (val) == GTK_TYPE_WIN32_THEME_PART)
642     {
643       if (resolve_win32_theme_part (props, val, val_out, context))
644         return; /* Don't copy val, this sets val_out */
645       _gtk_style_property_default_value (property, props, state, val);
646     }
647
648  out:
649   g_value_copy (val, val_out);
650 }
651
652 GParameter *
653 _gtk_style_property_unpack (GtkStyleProperty *property,
654                             const GValue     *value,
655                             guint            *n_params)
656 {
657   g_return_val_if_fail (property != NULL, NULL);
658   g_return_val_if_fail (property->unpack_func != NULL, NULL);
659   g_return_val_if_fail (value != NULL, NULL);
660   g_return_val_if_fail (n_params != NULL, NULL);
661
662   return property->unpack_func (value, n_params);
663 }
664
665 static void
666 _gtk_style_property_pack (GtkStyleProperty   *property,
667                           GtkStyleProperties *props,
668                           GtkStateFlags       state,
669                           GtkStylePropertyContext *context,
670                           GValue             *value)
671 {
672   g_return_if_fail (property != NULL);
673   g_return_if_fail (property->pack_func != NULL);
674   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
675   g_return_if_fail (G_IS_VALUE (value));
676
677   property->pack_func (value, props, state, context);
678 }
679
680 void
681 _gtk_style_property_assign (GtkStyleProperty   *property,
682                             GtkStyleProperties *props,
683                             GtkStateFlags       state,
684                             const GValue       *value)
685 {
686   g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
687   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
688   g_return_if_fail (value != NULL);
689
690   if (GTK_IS_CSS_SHORTHAND_PROPERTY (property))
691     {
692       GParameter *parameters;
693       guint i, n_parameters;
694
695       parameters = _gtk_style_property_unpack (property, value, &n_parameters);
696
697       for (i = 0; i < n_parameters; i++)
698         {
699           _gtk_style_property_assign (_gtk_style_property_lookup (parameters[i].name),
700                                       props,
701                                       state,
702                                       &parameters[i].value);
703           g_value_unset (&parameters[i].value);
704         }
705       g_free (parameters);
706       return;
707     }
708   else if (GTK_IS_CSS_STYLE_PROPERTY (property))
709     {
710       _gtk_style_properties_set_property_by_property (props,
711                                                       GTK_CSS_STYLE_PROPERTY (property),
712                                                       state,
713                                                       value);
714     }
715   else
716     {
717       g_assert_not_reached ();
718     }
719 }
720
721 void
722 _gtk_style_property_query (GtkStyleProperty        *property,
723                            GtkStyleProperties      *props,
724                            GtkStateFlags            state,
725                            GtkStylePropertyContext *context,
726                            GValue                  *value)
727 {
728
729   g_return_if_fail (property != NULL);
730   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
731   g_return_if_fail (context != NULL);
732   g_return_if_fail (value != NULL);
733
734   g_value_init (value, property->pspec->value_type);
735
736   if (GTK_IS_CSS_STYLE_PROPERTY (property))
737     {
738       const GValue *val;
739       
740       val = _gtk_style_properties_peek_property (props, GTK_CSS_STYLE_PROPERTY (property), state);
741       if (val)
742         _gtk_style_property_resolve (property, props, state, context, (GValue *) val, value);
743       else
744         _gtk_style_property_default_value (property, props, state, value);
745     }
746   else if (GTK_IS_CSS_SHORTHAND_PROPERTY (property))
747     {
748       _gtk_style_property_pack (property, props, state, context, value);
749     }
750   else
751     {
752       g_assert_not_reached ();
753     }
754 }
755
756 #define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
757   (rgba)->red = (r); \
758   (rgba)->green = (g); \
759   (rgba)->blue = (b); \
760   (rgba)->alpha = (a); \
761 }G_STMT_END
762 static void
763 gtk_style_property_init_properties (void)
764 {
765   static gboolean initialized = FALSE;
766   GValue value = { 0, };
767   char *default_font_family[] = { "Sans", NULL };
768   GdkRGBA rgba;
769
770   if (G_LIKELY (initialized))
771     return;
772
773   initialized = TRUE;
774
775   g_value_init (&value, GDK_TYPE_RGBA);
776   rgba_init (&rgba, 1, 1, 1, 1);
777   g_value_set_boxed (&value, &rgba);
778   _gtk_style_property_register           (g_param_spec_boxed ("color",
779                                           "Foreground color",
780                                           "Foreground color",
781                                           GDK_TYPE_RGBA, 0),
782                                           GTK_STYLE_PROPERTY_INHERIT,
783                                           NULL,
784                                           NULL,
785                                           NULL,
786                                           &value);
787   rgba_init (&rgba, 0, 0, 0, 0);
788   g_value_set_boxed (&value, &rgba);
789   _gtk_style_property_register           (g_param_spec_boxed ("background-color",
790                                           "Background color",
791                                           "Background color",
792                                           GDK_TYPE_RGBA, 0),
793                                           0,
794                                           NULL,
795                                           NULL,
796                                           NULL,
797                                           &value);
798   g_value_unset (&value);
799
800   g_value_init (&value, G_TYPE_STRV);
801   g_value_set_boxed (&value, default_font_family);
802   _gtk_style_property_register           (g_param_spec_boxed ("font-family",
803                                                               "Font family",
804                                                               "Font family",
805                                                               G_TYPE_STRV, 0),
806                                           GTK_STYLE_PROPERTY_INHERIT,
807                                           NULL,
808                                           font_family_parse,
809                                           font_family_value_print,
810                                           &value);
811   g_value_unset (&value);
812   _gtk_style_property_register           (g_param_spec_enum ("font-style",
813                                                              "Font style",
814                                                              "Font style",
815                                                              PANGO_TYPE_STYLE,
816                                                              PANGO_STYLE_NORMAL, 0),
817                                           GTK_STYLE_PROPERTY_INHERIT,
818                                           NULL,
819                                           NULL,
820                                           NULL,
821                                           NULL);
822   _gtk_style_property_register           (g_param_spec_enum ("font-variant",
823                                                              "Font variant",
824                                                              "Font variant",
825                                                              PANGO_TYPE_VARIANT,
826                                                              PANGO_VARIANT_NORMAL, 0),
827                                           GTK_STYLE_PROPERTY_INHERIT,
828                                           NULL,
829                                           NULL,
830                                           NULL,
831                                           NULL);
832   /* xxx: need to parse this properly, ie parse the numbers */
833   _gtk_style_property_register           (g_param_spec_enum ("font-weight",
834                                                              "Font weight",
835                                                              "Font weight",
836                                                              PANGO_TYPE_WEIGHT,
837                                                              PANGO_WEIGHT_NORMAL, 0),
838                                           GTK_STYLE_PROPERTY_INHERIT,
839                                           NULL,
840                                           NULL,
841                                           NULL,
842                                           NULL);
843   g_value_init (&value, G_TYPE_DOUBLE);
844   g_value_set_double (&value, 10);
845   _gtk_style_property_register           (g_param_spec_double ("font-size",
846                                                                "Font size",
847                                                                "Font size",
848                                                                0, G_MAXDOUBLE, 0, 0),
849                                           GTK_STYLE_PROPERTY_INHERIT,
850                                           NULL,
851                                           NULL,
852                                           NULL,
853                                           &value);
854   g_value_unset (&value);
855
856   _gtk_style_property_register           (g_param_spec_boxed ("text-shadow",
857                                                               "Text shadow",
858                                                               "Text shadow",
859                                                               GTK_TYPE_SHADOW, 0),
860                                           GTK_STYLE_PROPERTY_INHERIT,
861                                           NULL,
862                                           NULL,
863                                           NULL,
864                                           NULL);
865
866   _gtk_style_property_register           (g_param_spec_boxed ("icon-shadow",
867                                                               "Icon shadow",
868                                                               "Icon shadow",
869                                                               GTK_TYPE_SHADOW, 0),
870                                           GTK_STYLE_PROPERTY_INHERIT,
871                                           NULL,
872                                           NULL,
873                                           NULL,
874                                           NULL);
875
876   gtk_style_properties_register_property (NULL,
877                                           g_param_spec_boxed ("box-shadow",
878                                                               "Box shadow",
879                                                               "Box shadow",
880                                                               GTK_TYPE_SHADOW, 0));
881   gtk_style_properties_register_property (NULL,
882                                           g_param_spec_int ("margin-top",
883                                                             "margin top",
884                                                             "Margin at top",
885                                                             0, G_MAXINT, 0, 0));
886   gtk_style_properties_register_property (NULL,
887                                           g_param_spec_int ("margin-left",
888                                                             "margin left",
889                                                             "Margin at left",
890                                                             0, G_MAXINT, 0, 0));
891   gtk_style_properties_register_property (NULL,
892                                           g_param_spec_int ("margin-bottom",
893                                                             "margin bottom",
894                                                             "Margin at bottom",
895                                                             0, G_MAXINT, 0, 0));
896   gtk_style_properties_register_property (NULL,
897                                           g_param_spec_int ("margin-right",
898                                                             "margin right",
899                                                             "Margin at right",
900                                                             0, G_MAXINT, 0, 0));
901   gtk_style_properties_register_property (NULL,
902                                           g_param_spec_int ("padding-top",
903                                                             "padding top",
904                                                             "Padding at top",
905                                                             0, G_MAXINT, 0, 0));
906   gtk_style_properties_register_property (NULL,
907                                           g_param_spec_int ("padding-left",
908                                                             "padding left",
909                                                             "Padding at left",
910                                                             0, G_MAXINT, 0, 0));
911   gtk_style_properties_register_property (NULL,
912                                           g_param_spec_int ("padding-bottom",
913                                                             "padding bottom",
914                                                             "Padding at bottom",
915                                                             0, G_MAXINT, 0, 0));
916   gtk_style_properties_register_property (NULL,
917                                           g_param_spec_int ("padding-right",
918                                                             "padding right",
919                                                             "Padding at right",
920                                                             0, G_MAXINT, 0, 0));
921   gtk_style_properties_register_property (NULL,
922                                           g_param_spec_int ("border-top-width",
923                                                             "border top width",
924                                                             "Border width at top",
925                                                             0, G_MAXINT, 0, 0));
926   gtk_style_properties_register_property (NULL,
927                                           g_param_spec_int ("border-left-width",
928                                                             "border left width",
929                                                             "Border width at left",
930                                                             0, G_MAXINT, 0, 0));
931   gtk_style_properties_register_property (NULL,
932                                           g_param_spec_int ("border-bottom-width",
933                                                             "border bottom width",
934                                                             "Border width at bottom",
935                                                             0, G_MAXINT, 0, 0));
936   gtk_style_properties_register_property (NULL,
937                                           g_param_spec_int ("border-right-width",
938                                                             "border right width",
939                                                             "Border width at right",
940                                                             0, G_MAXINT, 0, 0));
941
942   _gtk_style_property_register           (g_param_spec_boxed ("border-top-left-radius",
943                                                               "Border top left radius",
944                                                               "Border radius of top left corner, in pixels",
945                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
946                                           0,
947                                           NULL,
948                                           border_corner_radius_value_parse,
949                                           border_corner_radius_value_print,
950                                           NULL);
951   _gtk_style_property_register           (g_param_spec_boxed ("border-top-right-radius",
952                                                               "Border top right radius",
953                                                               "Border radius of top right corner, in pixels",
954                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
955                                           0,
956                                           NULL,
957                                           border_corner_radius_value_parse,
958                                           border_corner_radius_value_print,
959                                           NULL);
960   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-right-radius",
961                                                               "Border bottom right radius",
962                                                               "Border radius of bottom right corner, in pixels",
963                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
964                                           0,
965                                           NULL,
966                                           border_corner_radius_value_parse,
967                                           border_corner_radius_value_print,
968                                           NULL);
969   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-left-radius",
970                                                               "Border bottom left radius",
971                                                               "Border radius of bottom left corner, in pixels",
972                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
973                                           0,
974                                           NULL,
975                                           border_corner_radius_value_parse,
976                                           border_corner_radius_value_print,
977                                           NULL);
978
979   gtk_style_properties_register_property (NULL,
980                                           g_param_spec_enum ("border-style",
981                                                              "Border style",
982                                                              "Border style",
983                                                              GTK_TYPE_BORDER_STYLE,
984                                                              GTK_BORDER_STYLE_NONE, 0));
985   gtk_style_properties_register_property (NULL,
986                                           g_param_spec_enum ("background-clip",
987                                                              "Background clip",
988                                                              "Background clip",
989                                                              GTK_TYPE_CSS_AREA,
990                                                              GTK_CSS_AREA_BORDER_BOX, 0));
991   gtk_style_properties_register_property (NULL,
992                                           g_param_spec_enum ("background-origin",
993                                                              "Background origin",
994                                                              "Background origin",
995                                                              GTK_TYPE_CSS_AREA,
996                                                              GTK_CSS_AREA_PADDING_BOX, 0));
997   g_value_init (&value, GTK_TYPE_CSS_SPECIAL_VALUE);
998   g_value_set_enum (&value, GTK_CSS_CURRENT_COLOR);
999   _gtk_style_property_register           (g_param_spec_boxed ("border-top-color",
1000                                                               "Border top color",
1001                                                               "Border top color",
1002                                                               GDK_TYPE_RGBA, 0),
1003                                           0,
1004                                           NULL,
1005                                           NULL,
1006                                           NULL,
1007                                           &value);
1008   _gtk_style_property_register           (g_param_spec_boxed ("border-right-color",
1009                                                               "Border right color",
1010                                                               "Border right color",
1011                                                               GDK_TYPE_RGBA, 0),
1012                                           0,
1013                                           NULL,
1014                                           NULL,
1015                                           NULL,
1016                                           &value);
1017   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-color",
1018                                                               "Border bottom color",
1019                                                               "Border bottom color",
1020                                                               GDK_TYPE_RGBA, 0),
1021                                           0,
1022                                           NULL,
1023                                           NULL,
1024                                           NULL,
1025                                           &value);
1026   _gtk_style_property_register           (g_param_spec_boxed ("border-left-color",
1027                                                               "Border left color",
1028                                                               "Border left color",
1029                                                               GDK_TYPE_RGBA, 0),
1030                                           0,
1031                                           NULL,
1032                                           NULL,
1033                                           NULL,
1034                                           &value);
1035   g_value_unset (&value);
1036
1037   gtk_style_properties_register_property (NULL,
1038                                           g_param_spec_boxed ("background-image",
1039                                                               "Background Image",
1040                                                               "Background Image",
1041                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
1042   gtk_style_properties_register_property (NULL,
1043                                           g_param_spec_boxed ("background-repeat",
1044                                                               "Background repeat",
1045                                                               "Background repeat",
1046                                                               GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
1047
1048   gtk_style_properties_register_property (NULL,
1049                                           g_param_spec_boxed ("border-image-source",
1050                                                               "Border image source",
1051                                                               "Border image source",
1052                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
1053   gtk_style_properties_register_property (NULL,
1054                                           g_param_spec_boxed ("border-image-repeat",
1055                                                               "Border image repeat",
1056                                                               "Border image repeat",
1057                                                               GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
1058   gtk_style_properties_register_property (NULL,
1059                                           g_param_spec_boxed ("border-image-slice",
1060                                                               "Border image slice",
1061                                                               "Border image slice",
1062                                                               GTK_TYPE_BORDER, 0));
1063   g_value_init (&value, GTK_TYPE_BORDER);
1064   _gtk_style_property_register           (g_param_spec_boxed ("border-image-width",
1065                                                               "Border image width",
1066                                                               "Border image width",
1067                                                               GTK_TYPE_BORDER, 0),
1068                                           0,
1069                                           NULL,
1070                                           NULL,
1071                                           NULL,
1072                                           &value);
1073   g_value_unset (&value);
1074   gtk_style_properties_register_property (NULL,
1075                                           g_param_spec_object ("engine",
1076                                                                "Theming Engine",
1077                                                                "Theming Engine",
1078                                                                GTK_TYPE_THEMING_ENGINE, 0));
1079   gtk_style_properties_register_property (NULL,
1080                                           g_param_spec_boxed ("transition",
1081                                                               "Transition animation description",
1082                                                               "Transition animation description",
1083                                                               GTK_TYPE_ANIMATION_DESCRIPTION, 0));
1084
1085   /* Private property holding the binding sets */
1086   _gtk_style_property_register           (g_param_spec_boxed ("gtk-key-bindings",
1087                                                               "Key bindings",
1088                                                               "Key bindings",
1089                                                               G_TYPE_PTR_ARRAY, 0),
1090                                           0,
1091                                           NULL,
1092                                           bindings_value_parse,
1093                                           bindings_value_print,
1094                                           NULL);
1095
1096   /* initialize shorthands last, they depend on the real properties existing */
1097   _gtk_css_shorthand_property_init_properties ();
1098 }
1099
1100 /**
1101  * _gtk_style_property_lookup:
1102  * @name: name of the property to lookup
1103  *
1104  * Looks up the CSS property with the given @name. If no such
1105  * property exists, %NULL is returned.
1106  *
1107  * Returns: (transfer none): The property or %NULL if no
1108  *     property with the given name exists.
1109  **/
1110 GtkStyleProperty *
1111 _gtk_style_property_lookup (const char *name)
1112 {
1113   GtkStylePropertyClass *klass;
1114
1115   g_return_val_if_fail (name != NULL, NULL);
1116
1117   gtk_style_property_init_properties ();
1118
1119   klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY);
1120
1121   return g_hash_table_lookup (klass->properties, name);
1122 }
1123
1124 /**
1125  * _gtk_style_property_get_name:
1126  * @property: the property to query
1127  *
1128  * Gets the name of the given property.
1129  *
1130  * Returns: the name of the property
1131  **/
1132 const char *
1133 _gtk_style_property_get_name (GtkStyleProperty *property)
1134 {
1135   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
1136
1137   return property->name;
1138 }
1139
1140 /**
1141  * _gtk_style_property_get_value_type:
1142  * @property: the property to query
1143  *
1144  * Gets the value type of the @property, if the property is usable
1145  * in public API via _gtk_style_property_assign() and
1146  * _gtk_style_property_query(). If the @property is not usable in that
1147  * way, %G_TYPE_NONE is returned.
1148  *
1149  * Returns: the value type in use or %G_TYPE_NONE if none.
1150  **/
1151 GType
1152 _gtk_style_property_get_value_type (GtkStyleProperty *property)
1153 {
1154   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), G_TYPE_NONE);
1155
1156   return property->value_type;
1157 }
1158
1159
1160 void
1161 _gtk_style_property_register (GParamSpec               *pspec,
1162                               GtkStylePropertyFlags     flags,
1163                               GtkStylePropertyParser    property_parse_func,
1164                               GtkStyleParseFunc         parse_func,
1165                               GtkStylePrintFunc         print_func,
1166                               const GValue *            initial_value)
1167 {
1168   GtkStyleProperty *node;
1169   GValue initial_fallback = { 0, };
1170
1171   if (initial_value == NULL)
1172     {
1173       g_value_init (&initial_fallback, pspec->value_type);
1174       if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
1175         g_value_set_object (&initial_fallback, gtk_theming_engine_load (NULL));
1176       else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
1177         g_value_take_boxed (&initial_fallback, pango_font_description_from_string ("Sans 10"));
1178       else if (pspec->value_type == GDK_TYPE_RGBA)
1179         {
1180           GdkRGBA color;
1181           gdk_rgba_parse (&color, "pink");
1182           g_value_set_boxed (&initial_fallback, &color);
1183         }
1184       else if (pspec->value_type == GTK_TYPE_BORDER)
1185         {
1186           g_value_take_boxed (&initial_fallback, gtk_border_new ());
1187         }
1188       else
1189         g_param_value_set_default (pspec, &initial_fallback);
1190
1191       initial_value = &initial_fallback;
1192     }
1193
1194   node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
1195                        "inherit", (flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE,
1196                        "initial-value", initial_value,
1197                        "name", pspec->name,
1198                        "value-type", pspec->value_type,
1199                        NULL);
1200   node->pspec = pspec;
1201   node->property_parse_func = property_parse_func;
1202   node->parse_func = parse_func;
1203   node->print_func = print_func;
1204
1205   if (G_IS_VALUE (&initial_fallback))
1206     g_value_unset (&initial_fallback);
1207 }