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