]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperty.c
styleproperty: Let parse_value() initialize the value
[~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 gboolean
205 font_family_parse (GtkCssParser *parser,
206                    GFile        *base,
207                    GValue       *value)
208 {
209   GPtrArray *names;
210   char *name;
211
212   /* We don't special case generic families. Pango should do
213    * that for us */
214
215   names = g_ptr_array_new ();
216
217   do {
218     name = _gtk_css_parser_try_ident (parser, TRUE);
219     if (name)
220       {
221         GString *string = g_string_new (name);
222         g_free (name);
223         while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
224           {
225             g_string_append_c (string, ' ');
226             g_string_append (string, name);
227             g_free (name);
228           }
229         name = g_string_free (string, FALSE);
230       }
231     else 
232       {
233         name = _gtk_css_parser_read_string (parser);
234         if (name == NULL)
235           {
236             g_ptr_array_free (names, TRUE);
237             return FALSE;
238           }
239       }
240
241     g_ptr_array_add (names, name);
242   } while (_gtk_css_parser_try (parser, ",", TRUE));
243
244   /* NULL-terminate array */
245   g_ptr_array_add (names, NULL);
246   g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
247   return TRUE;
248 }
249
250 static void
251 font_family_value_print (const GValue *value,
252                          GString      *string)
253 {
254   const char **names = g_value_get_boxed (value);
255
256   if (names == NULL || *names == NULL)
257     {
258       g_string_append (string, "none");
259       return;
260     }
261
262   string_append_string (string, *names);
263   names++;
264   while (*names)
265     {
266       g_string_append (string, ", ");
267       string_append_string (string, *names);
268       names++;
269     }
270 }
271
272 static gboolean 
273 bindings_value_parse (GtkCssParser *parser,
274                       GFile        *base,
275                       GValue       *value)
276 {
277   GPtrArray *array;
278   GtkBindingSet *binding_set;
279   char *name;
280
281   array = g_ptr_array_new ();
282
283   do {
284       name = _gtk_css_parser_try_ident (parser, TRUE);
285       if (name == NULL)
286         {
287           _gtk_css_parser_error (parser, "Not a valid binding name");
288           g_ptr_array_free (array, TRUE);
289           return FALSE;
290         }
291
292       binding_set = gtk_binding_set_find (name);
293
294       if (!binding_set)
295         {
296           _gtk_css_parser_error (parser, "No binding set named '%s'", name);
297           g_free (name);
298           continue;
299         }
300
301       g_ptr_array_add (array, binding_set);
302       g_free (name);
303     }
304   while (_gtk_css_parser_try (parser, ",", TRUE));
305
306   g_value_take_boxed (value, array);
307
308   return TRUE;
309 }
310
311 static void
312 bindings_value_print (const GValue *value,
313                       GString      *string)
314 {
315   GPtrArray *array;
316   guint i;
317
318   array = g_value_get_boxed (value);
319
320   for (i = 0; i < array->len; i++)
321     {
322       GtkBindingSet *binding_set = g_ptr_array_index (array, i);
323
324       if (i > 0)
325         g_string_append (string, ", ");
326       g_string_append (string, binding_set->set_name);
327     }
328 }
329
330 static gboolean 
331 border_corner_radius_value_parse (GtkCssParser *parser,
332                                   GFile        *base,
333                                   GValue       *value)
334 {
335   GtkCssBorderCornerRadius corner;
336
337   if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
338     {
339       _gtk_css_parser_error (parser, "Expected a number");
340       return FALSE;
341     }
342   else if (corner.horizontal < 0)
343     goto negative;
344
345   if (!_gtk_css_parser_try_double (parser, &corner.vertical))
346     corner.vertical = corner.horizontal;
347   else if (corner.vertical < 0)
348     goto negative;
349
350   g_value_set_boxed (value, &corner);
351   return TRUE;
352
353 negative:
354   _gtk_css_parser_error (parser, "Border radius values cannot be negative");
355   return FALSE;
356 }
357
358 static void
359 border_corner_radius_value_print (const GValue *value,
360                                   GString      *string)
361 {
362   GtkCssBorderCornerRadius *corner;
363
364   corner = g_value_get_boxed (value);
365
366   if (corner == NULL)
367     {
368       g_string_append (string, "none");
369       return;
370     }
371
372   string_append_double (string, corner->horizontal);
373   if (corner->horizontal != corner->vertical)
374     {
375       g_string_append_c (string, ' ');
376       string_append_double (string, corner->vertical);
377     }
378 }
379
380 /*** API ***/
381
382 /**
383  * _gtk_style_property_parse_value:
384  * @property: the property
385  * @value: an uninitialized value
386  * @parser: the parser to parse from
387  * @base: the base file for @aprser
388  *
389  * Tries to parse the given @property from the given @parser into
390  * @value. The type that @value will be assigned is dependant on
391  * the parser and no assumptions must be made about it. If the
392  * parsing fails, %FALSE will be returned and @value will be
393  * left uninitialized.
394  *
395  * Only if @property is a #GtkCssShorthandProperty, the @value will
396  * always contain a #GValueArray with the values to be used for
397  * the subproperties.
398  *
399  * Returns: %TRUE on success
400  **/
401 gboolean
402 _gtk_style_property_parse_value (GtkStyleProperty *property,
403                                  GValue           *value,
404                                  GtkCssParser     *parser,
405                                  GFile            *base)
406 {
407   GtkStylePropertyClass *klass;
408
409   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), FALSE);
410   g_return_val_if_fail (value != NULL, FALSE);
411   g_return_val_if_fail (parser != NULL, FALSE);
412
413   klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
414
415   return klass->parse_value (property, value, parser, base);
416 }
417
418 /**
419  * _gtk_style_property_assign:
420  * @property: the property
421  * @props: The properties to assign to
422  * @state: The state to assign
423  * @value: (out): the #GValue with the value to be
424  *     assigned
425  *
426  * This function is called by gtk_style_properties_set() and in
427  * turn gtk_style_context_set() and similar functions to set the
428  * value from code using old APIs.
429  **/
430 void
431 _gtk_style_property_assign (GtkStyleProperty   *property,
432                             GtkStyleProperties *props,
433                             GtkStateFlags       state,
434                             const GValue       *value)
435 {
436   GtkStylePropertyClass *klass;
437
438   g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
439   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
440   g_return_if_fail (value != NULL);
441
442   klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
443
444   klass->assign (property, props, state, value);
445 }
446
447 /**
448  * _gtk_style_property_query:
449  * @property: the property
450  * @props: The properties to query
451  * @state: The state to query
452  * @value: (out): an uninitialized #GValue to be filled with the
453  *   contents of the lookup
454  *
455  * This function is called by gtk_style_properties_get() and in
456  * turn gtk_style_context_get() and similar functions to get the
457  * value to return to code using old APIs.
458  **/
459 void
460 _gtk_style_property_query (GtkStyleProperty        *property,
461                            GtkStyleProperties      *props,
462                            GtkStateFlags            state,
463                            GtkStylePropertyContext *context,
464                            GValue                  *value)
465 {
466   GtkStylePropertyClass *klass;
467
468   g_return_if_fail (property != NULL);
469   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
470   g_return_if_fail (context != NULL);
471   g_return_if_fail (value != NULL);
472
473   klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
474
475   g_value_init (value, property->value_type);
476
477   klass->query (property, props, state, context, value);
478 }
479
480 #define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
481   (rgba)->red = (r); \
482   (rgba)->green = (g); \
483   (rgba)->blue = (b); \
484   (rgba)->alpha = (a); \
485 }G_STMT_END
486 static void
487 gtk_style_property_init_properties (void)
488 {
489   static gboolean initialized = FALSE;
490   GValue value = { 0, };
491   char *default_font_family[] = { "Sans", NULL };
492   GdkRGBA rgba;
493   GtkCssBorderCornerRadius no_corner_radius = { 0, };
494
495   if (G_LIKELY (initialized))
496     return;
497
498   initialized = TRUE;
499
500   g_value_init (&value, GDK_TYPE_RGBA);
501   rgba_init (&rgba, 1, 1, 1, 1);
502   g_value_set_boxed (&value, &rgba);
503   _gtk_style_property_register           (g_param_spec_boxed ("color",
504                                           "Foreground color",
505                                           "Foreground color",
506                                           GDK_TYPE_RGBA, 0),
507                                           GTK_STYLE_PROPERTY_INHERIT,
508                                           NULL,
509                                           NULL,
510                                           NULL,
511                                           &value);
512   rgba_init (&rgba, 0, 0, 0, 0);
513   g_value_set_boxed (&value, &rgba);
514   _gtk_style_property_register           (g_param_spec_boxed ("background-color",
515                                           "Background color",
516                                           "Background color",
517                                           GDK_TYPE_RGBA, 0),
518                                           0,
519                                           NULL,
520                                           NULL,
521                                           NULL,
522                                           &value);
523   g_value_unset (&value);
524
525   g_value_init (&value, G_TYPE_STRV);
526   g_value_set_boxed (&value, default_font_family);
527   _gtk_style_property_register           (g_param_spec_boxed ("font-family",
528                                                               "Font family",
529                                                               "Font family",
530                                                               G_TYPE_STRV, 0),
531                                           GTK_STYLE_PROPERTY_INHERIT,
532                                           NULL,
533                                           font_family_parse,
534                                           font_family_value_print,
535                                           &value);
536   g_value_unset (&value);
537   _gtk_style_property_register           (g_param_spec_enum ("font-style",
538                                                              "Font style",
539                                                              "Font style",
540                                                              PANGO_TYPE_STYLE,
541                                                              PANGO_STYLE_NORMAL, 0),
542                                           GTK_STYLE_PROPERTY_INHERIT,
543                                           NULL,
544                                           NULL,
545                                           NULL,
546                                           NULL);
547   _gtk_style_property_register           (g_param_spec_enum ("font-variant",
548                                                              "Font variant",
549                                                              "Font variant",
550                                                              PANGO_TYPE_VARIANT,
551                                                              PANGO_VARIANT_NORMAL, 0),
552                                           GTK_STYLE_PROPERTY_INHERIT,
553                                           NULL,
554                                           NULL,
555                                           NULL,
556                                           NULL);
557   /* xxx: need to parse this properly, ie parse the numbers */
558   _gtk_style_property_register           (g_param_spec_enum ("font-weight",
559                                                              "Font weight",
560                                                              "Font weight",
561                                                              PANGO_TYPE_WEIGHT,
562                                                              PANGO_WEIGHT_NORMAL, 0),
563                                           GTK_STYLE_PROPERTY_INHERIT,
564                                           NULL,
565                                           NULL,
566                                           NULL,
567                                           NULL);
568   g_value_init (&value, G_TYPE_DOUBLE);
569   g_value_set_double (&value, 10);
570   _gtk_style_property_register           (g_param_spec_double ("font-size",
571                                                                "Font size",
572                                                                "Font size",
573                                                                0, G_MAXDOUBLE, 0, 0),
574                                           GTK_STYLE_PROPERTY_INHERIT,
575                                           NULL,
576                                           NULL,
577                                           NULL,
578                                           &value);
579   g_value_unset (&value);
580
581   _gtk_style_property_register           (g_param_spec_boxed ("text-shadow",
582                                                               "Text shadow",
583                                                               "Text shadow",
584                                                               GTK_TYPE_SHADOW, 0),
585                                           GTK_STYLE_PROPERTY_INHERIT,
586                                           NULL,
587                                           NULL,
588                                           NULL,
589                                           NULL);
590
591   _gtk_style_property_register           (g_param_spec_boxed ("icon-shadow",
592                                                               "Icon shadow",
593                                                               "Icon shadow",
594                                                               GTK_TYPE_SHADOW, 0),
595                                           GTK_STYLE_PROPERTY_INHERIT,
596                                           NULL,
597                                           NULL,
598                                           NULL,
599                                           NULL);
600
601   gtk_style_properties_register_property (NULL,
602                                           g_param_spec_boxed ("box-shadow",
603                                                               "Box shadow",
604                                                               "Box shadow",
605                                                               GTK_TYPE_SHADOW, 0));
606   gtk_style_properties_register_property (NULL,
607                                           g_param_spec_int ("margin-top",
608                                                             "margin top",
609                                                             "Margin at top",
610                                                             0, G_MAXINT, 0, 0));
611   gtk_style_properties_register_property (NULL,
612                                           g_param_spec_int ("margin-left",
613                                                             "margin left",
614                                                             "Margin at left",
615                                                             0, G_MAXINT, 0, 0));
616   gtk_style_properties_register_property (NULL,
617                                           g_param_spec_int ("margin-bottom",
618                                                             "margin bottom",
619                                                             "Margin at bottom",
620                                                             0, G_MAXINT, 0, 0));
621   gtk_style_properties_register_property (NULL,
622                                           g_param_spec_int ("margin-right",
623                                                             "margin right",
624                                                             "Margin at right",
625                                                             0, G_MAXINT, 0, 0));
626   gtk_style_properties_register_property (NULL,
627                                           g_param_spec_int ("padding-top",
628                                                             "padding top",
629                                                             "Padding at top",
630                                                             0, G_MAXINT, 0, 0));
631   gtk_style_properties_register_property (NULL,
632                                           g_param_spec_int ("padding-left",
633                                                             "padding left",
634                                                             "Padding at left",
635                                                             0, G_MAXINT, 0, 0));
636   gtk_style_properties_register_property (NULL,
637                                           g_param_spec_int ("padding-bottom",
638                                                             "padding bottom",
639                                                             "Padding at bottom",
640                                                             0, G_MAXINT, 0, 0));
641   gtk_style_properties_register_property (NULL,
642                                           g_param_spec_int ("padding-right",
643                                                             "padding right",
644                                                             "Padding at right",
645                                                             0, G_MAXINT, 0, 0));
646   gtk_style_properties_register_property (NULL,
647                                           g_param_spec_int ("border-top-width",
648                                                             "border top width",
649                                                             "Border width at top",
650                                                             0, G_MAXINT, 0, 0));
651   gtk_style_properties_register_property (NULL,
652                                           g_param_spec_int ("border-left-width",
653                                                             "border left width",
654                                                             "Border width at left",
655                                                             0, G_MAXINT, 0, 0));
656   gtk_style_properties_register_property (NULL,
657                                           g_param_spec_int ("border-bottom-width",
658                                                             "border bottom width",
659                                                             "Border width at bottom",
660                                                             0, G_MAXINT, 0, 0));
661   gtk_style_properties_register_property (NULL,
662                                           g_param_spec_int ("border-right-width",
663                                                             "border right width",
664                                                             "Border width at right",
665                                                             0, G_MAXINT, 0, 0));
666
667   g_value_init (&value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
668   g_value_set_boxed (&value, &no_corner_radius);
669   _gtk_style_property_register           (g_param_spec_boxed ("border-top-left-radius",
670                                                               "Border top left radius",
671                                                               "Border radius of top left corner, in pixels",
672                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
673                                           0,
674                                           NULL,
675                                           border_corner_radius_value_parse,
676                                           border_corner_radius_value_print,
677                                           &value);
678   _gtk_style_property_register           (g_param_spec_boxed ("border-top-right-radius",
679                                                               "Border top right radius",
680                                                               "Border radius of top right corner, in pixels",
681                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
682                                           0,
683                                           NULL,
684                                           border_corner_radius_value_parse,
685                                           border_corner_radius_value_print,
686                                           &value);
687   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-right-radius",
688                                                               "Border bottom right radius",
689                                                               "Border radius of bottom right corner, in pixels",
690                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
691                                           0,
692                                           NULL,
693                                           border_corner_radius_value_parse,
694                                           border_corner_radius_value_print,
695                                           &value);
696   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-left-radius",
697                                                               "Border bottom left radius",
698                                                               "Border radius of bottom left corner, in pixels",
699                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
700                                           0,
701                                           NULL,
702                                           border_corner_radius_value_parse,
703                                           border_corner_radius_value_print,
704                                           &value);
705   g_value_unset (&value);
706
707   gtk_style_properties_register_property (NULL,
708                                           g_param_spec_enum ("border-style",
709                                                              "Border style",
710                                                              "Border style",
711                                                              GTK_TYPE_BORDER_STYLE,
712                                                              GTK_BORDER_STYLE_NONE, 0));
713   gtk_style_properties_register_property (NULL,
714                                           g_param_spec_enum ("background-clip",
715                                                              "Background clip",
716                                                              "Background clip",
717                                                              GTK_TYPE_CSS_AREA,
718                                                              GTK_CSS_AREA_BORDER_BOX, 0));
719   gtk_style_properties_register_property (NULL,
720                                           g_param_spec_enum ("background-origin",
721                                                              "Background origin",
722                                                              "Background origin",
723                                                              GTK_TYPE_CSS_AREA,
724                                                              GTK_CSS_AREA_PADDING_BOX, 0));
725   g_value_init (&value, GTK_TYPE_CSS_SPECIAL_VALUE);
726   g_value_set_enum (&value, GTK_CSS_CURRENT_COLOR);
727   _gtk_style_property_register           (g_param_spec_boxed ("border-top-color",
728                                                               "Border top color",
729                                                               "Border top color",
730                                                               GDK_TYPE_RGBA, 0),
731                                           0,
732                                           NULL,
733                                           NULL,
734                                           NULL,
735                                           &value);
736   _gtk_style_property_register           (g_param_spec_boxed ("border-right-color",
737                                                               "Border right color",
738                                                               "Border right color",
739                                                               GDK_TYPE_RGBA, 0),
740                                           0,
741                                           NULL,
742                                           NULL,
743                                           NULL,
744                                           &value);
745   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-color",
746                                                               "Border bottom color",
747                                                               "Border bottom color",
748                                                               GDK_TYPE_RGBA, 0),
749                                           0,
750                                           NULL,
751                                           NULL,
752                                           NULL,
753                                           &value);
754   _gtk_style_property_register           (g_param_spec_boxed ("border-left-color",
755                                                               "Border left color",
756                                                               "Border left color",
757                                                               GDK_TYPE_RGBA, 0),
758                                           0,
759                                           NULL,
760                                           NULL,
761                                           NULL,
762                                           &value);
763   g_value_unset (&value);
764
765   gtk_style_properties_register_property (NULL,
766                                           g_param_spec_boxed ("background-image",
767                                                               "Background Image",
768                                                               "Background Image",
769                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
770   gtk_style_properties_register_property (NULL,
771                                           g_param_spec_boxed ("background-repeat",
772                                                               "Background repeat",
773                                                               "Background repeat",
774                                                               GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
775
776   gtk_style_properties_register_property (NULL,
777                                           g_param_spec_boxed ("border-image-source",
778                                                               "Border image source",
779                                                               "Border image source",
780                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
781   gtk_style_properties_register_property (NULL,
782                                           g_param_spec_boxed ("border-image-repeat",
783                                                               "Border image repeat",
784                                                               "Border image repeat",
785                                                               GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
786   gtk_style_properties_register_property (NULL,
787                                           g_param_spec_boxed ("border-image-slice",
788                                                               "Border image slice",
789                                                               "Border image slice",
790                                                               GTK_TYPE_BORDER, 0));
791   g_value_init (&value, GTK_TYPE_BORDER);
792   _gtk_style_property_register           (g_param_spec_boxed ("border-image-width",
793                                                               "Border image width",
794                                                               "Border image width",
795                                                               GTK_TYPE_BORDER, 0),
796                                           0,
797                                           NULL,
798                                           NULL,
799                                           NULL,
800                                           &value);
801   g_value_unset (&value);
802   gtk_style_properties_register_property (NULL,
803                                           g_param_spec_object ("engine",
804                                                                "Theming Engine",
805                                                                "Theming Engine",
806                                                                GTK_TYPE_THEMING_ENGINE, 0));
807   gtk_style_properties_register_property (NULL,
808                                           g_param_spec_boxed ("transition",
809                                                               "Transition animation description",
810                                                               "Transition animation description",
811                                                               GTK_TYPE_ANIMATION_DESCRIPTION, 0));
812
813   /* Private property holding the binding sets */
814   _gtk_style_property_register           (g_param_spec_boxed ("gtk-key-bindings",
815                                                               "Key bindings",
816                                                               "Key bindings",
817                                                               G_TYPE_PTR_ARRAY, 0),
818                                           0,
819                                           NULL,
820                                           bindings_value_parse,
821                                           bindings_value_print,
822                                           NULL);
823
824   /* initialize shorthands last, they depend on the real properties existing */
825   _gtk_css_shorthand_property_init_properties ();
826 }
827
828 /**
829  * _gtk_style_property_lookup:
830  * @name: name of the property to lookup
831  *
832  * Looks up the CSS property with the given @name. If no such
833  * property exists, %NULL is returned.
834  *
835  * Returns: (transfer none): The property or %NULL if no
836  *     property with the given name exists.
837  **/
838 GtkStyleProperty *
839 _gtk_style_property_lookup (const char *name)
840 {
841   GtkStylePropertyClass *klass;
842
843   g_return_val_if_fail (name != NULL, NULL);
844
845   gtk_style_property_init_properties ();
846
847   klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY);
848
849   return g_hash_table_lookup (klass->properties, name);
850 }
851
852 /**
853  * _gtk_style_property_get_name:
854  * @property: the property to query
855  *
856  * Gets the name of the given property.
857  *
858  * Returns: the name of the property
859  **/
860 const char *
861 _gtk_style_property_get_name (GtkStyleProperty *property)
862 {
863   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
864
865   return property->name;
866 }
867
868 /**
869  * _gtk_style_property_get_value_type:
870  * @property: the property to query
871  *
872  * Gets the value type of the @property, if the property is usable
873  * in public API via _gtk_style_property_assign() and
874  * _gtk_style_property_query(). If the @property is not usable in that
875  * way, %G_TYPE_NONE is returned.
876  *
877  * Returns: the value type in use or %G_TYPE_NONE if none.
878  **/
879 GType
880 _gtk_style_property_get_value_type (GtkStyleProperty *property)
881 {
882   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), G_TYPE_NONE);
883
884   return property->value_type;
885 }
886
887
888 void
889 _gtk_style_property_register (GParamSpec               *pspec,
890                               GtkStylePropertyFlags     flags,
891                               GtkStylePropertyParser    property_parse_func,
892                               GtkStyleParseFunc         parse_func,
893                               GtkStylePrintFunc         print_func,
894                               const GValue *            initial_value)
895 {
896   GtkStyleProperty *node;
897   GValue initial_fallback = { 0, };
898
899   if (initial_value == NULL)
900     {
901       g_value_init (&initial_fallback, pspec->value_type);
902       if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
903         g_value_set_object (&initial_fallback, gtk_theming_engine_load (NULL));
904       else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
905         g_value_take_boxed (&initial_fallback, pango_font_description_from_string ("Sans 10"));
906       else if (pspec->value_type == GDK_TYPE_RGBA)
907         {
908           GdkRGBA color;
909           gdk_rgba_parse (&color, "pink");
910           g_value_set_boxed (&initial_fallback, &color);
911         }
912       else if (pspec->value_type == GTK_TYPE_BORDER)
913         {
914           g_value_take_boxed (&initial_fallback, gtk_border_new ());
915         }
916       else
917         g_param_value_set_default (pspec, &initial_fallback);
918
919       initial_value = &initial_fallback;
920     }
921
922   node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
923                        "inherit", (flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE,
924                        "initial-value", initial_value,
925                        "name", pspec->name,
926                        "value-type", pspec->value_type,
927                        NULL);
928   g_assert (node->value_type == pspec->value_type);
929   GTK_CSS_STYLE_PROPERTY (node)->pspec = pspec;
930   node->property_parse_func = property_parse_func;
931   node->parse_func = parse_func;
932   node->print_func = print_func;
933
934   if (G_IS_VALUE (&initial_fallback))
935     g_value_unset (&initial_fallback);
936 }