]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperty.c
styleproperty: require property in _gtk_style_property_parse_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 gboolean
383 _gtk_style_property_parse_value (GtkStyleProperty *property,
384                                  GValue           *value,
385                                  GtkCssParser     *parser,
386                                  GFile            *base)
387 {
388   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), FALSE);
389   g_return_val_if_fail (value != NULL, FALSE);
390   g_return_val_if_fail (parser != NULL, FALSE);
391
392   if (_gtk_css_parser_try (parser, "initial", TRUE))
393     {
394       /* the initial value can be explicitly specified with the
395        * â€˜initial’ keyword which all properties accept.
396        */
397       g_value_unset (value);
398       g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
399       g_value_set_enum (value, GTK_CSS_INITIAL);
400       return TRUE;
401     }
402   else if (_gtk_css_parser_try (parser, "inherit", TRUE))
403     {
404       /* All properties accept the â€˜inherit’ value which
405        * explicitly specifies that the value will be determined
406        * by inheritance. The â€˜inherit’ value can be used to
407        * strengthen inherited values in the cascade, and it can
408        * also be used on properties that are not normally inherited.
409        */
410       g_value_unset (value);
411       g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
412       g_value_set_enum (value, GTK_CSS_INHERIT);
413       return TRUE;
414     }
415   else if (property->property_parse_func)
416     {
417       GError *error = NULL;
418       char *value_str;
419       gboolean success;
420       
421       value_str = _gtk_css_parser_read_value (parser);
422       if (value_str == NULL)
423         return FALSE;
424       
425       success = (*property->property_parse_func) (value_str, value, &error);
426
427       g_free (value_str);
428
429       return success;
430     }
431   else if (property->parse_func)
432     return (* property->parse_func) (parser, base, value);
433   else
434     return _gtk_css_style_parse_value (value, parser, base);
435 }
436
437 GParameter *
438 _gtk_style_property_unpack (GtkStyleProperty *property,
439                             const GValue     *value,
440                             guint            *n_params)
441 {
442   g_return_val_if_fail (property != NULL, NULL);
443   g_return_val_if_fail (property->unpack_func != NULL, NULL);
444   g_return_val_if_fail (value != NULL, NULL);
445   g_return_val_if_fail (n_params != NULL, NULL);
446
447   return property->unpack_func (value, n_params);
448 }
449
450 /**
451  * _gtk_style_property_assign:
452  * @property: the property
453  * @props: The properties to assign to
454  * @state: The state to assign
455  * @value: (out): the #GValue with the value to be
456  *     assigned
457  *
458  * This function is called by gtk_style_properties_set() and in
459  * turn gtk_style_context_set() and similar functions to set the
460  * value from code using old APIs.
461  **/
462 void
463 _gtk_style_property_assign (GtkStyleProperty   *property,
464                             GtkStyleProperties *props,
465                             GtkStateFlags       state,
466                             const GValue       *value)
467 {
468   GtkStylePropertyClass *klass;
469
470   g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
471   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
472   g_return_if_fail (value != NULL);
473
474   klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
475
476   klass->assign (property, props, state, value);
477 }
478
479 /**
480  * _gtk_style_property_query:
481  * @property: the property
482  * @props: The properties to query
483  * @state: The state to query
484  * @value: (out): an uninitialized #GValue to be filled with the
485  *   contents of the lookup
486  *
487  * This function is called by gtk_style_properties_get() and in
488  * turn gtk_style_context_get() and similar functions to get the
489  * value to return to code using old APIs.
490  **/
491 void
492 _gtk_style_property_query (GtkStyleProperty        *property,
493                            GtkStyleProperties      *props,
494                            GtkStateFlags            state,
495                            GtkStylePropertyContext *context,
496                            GValue                  *value)
497 {
498   GtkStylePropertyClass *klass;
499
500   g_return_if_fail (property != NULL);
501   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
502   g_return_if_fail (context != NULL);
503   g_return_if_fail (value != NULL);
504
505   klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
506
507   g_value_init (value, property->value_type);
508
509   klass->query (property, props, state, context, value);
510 }
511
512 #define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
513   (rgba)->red = (r); \
514   (rgba)->green = (g); \
515   (rgba)->blue = (b); \
516   (rgba)->alpha = (a); \
517 }G_STMT_END
518 static void
519 gtk_style_property_init_properties (void)
520 {
521   static gboolean initialized = FALSE;
522   GValue value = { 0, };
523   char *default_font_family[] = { "Sans", NULL };
524   GdkRGBA rgba;
525
526   if (G_LIKELY (initialized))
527     return;
528
529   initialized = TRUE;
530
531   g_value_init (&value, GDK_TYPE_RGBA);
532   rgba_init (&rgba, 1, 1, 1, 1);
533   g_value_set_boxed (&value, &rgba);
534   _gtk_style_property_register           (g_param_spec_boxed ("color",
535                                           "Foreground color",
536                                           "Foreground color",
537                                           GDK_TYPE_RGBA, 0),
538                                           GTK_STYLE_PROPERTY_INHERIT,
539                                           NULL,
540                                           NULL,
541                                           NULL,
542                                           &value);
543   rgba_init (&rgba, 0, 0, 0, 0);
544   g_value_set_boxed (&value, &rgba);
545   _gtk_style_property_register           (g_param_spec_boxed ("background-color",
546                                           "Background color",
547                                           "Background color",
548                                           GDK_TYPE_RGBA, 0),
549                                           0,
550                                           NULL,
551                                           NULL,
552                                           NULL,
553                                           &value);
554   g_value_unset (&value);
555
556   g_value_init (&value, G_TYPE_STRV);
557   g_value_set_boxed (&value, default_font_family);
558   _gtk_style_property_register           (g_param_spec_boxed ("font-family",
559                                                               "Font family",
560                                                               "Font family",
561                                                               G_TYPE_STRV, 0),
562                                           GTK_STYLE_PROPERTY_INHERIT,
563                                           NULL,
564                                           font_family_parse,
565                                           font_family_value_print,
566                                           &value);
567   g_value_unset (&value);
568   _gtk_style_property_register           (g_param_spec_enum ("font-style",
569                                                              "Font style",
570                                                              "Font style",
571                                                              PANGO_TYPE_STYLE,
572                                                              PANGO_STYLE_NORMAL, 0),
573                                           GTK_STYLE_PROPERTY_INHERIT,
574                                           NULL,
575                                           NULL,
576                                           NULL,
577                                           NULL);
578   _gtk_style_property_register           (g_param_spec_enum ("font-variant",
579                                                              "Font variant",
580                                                              "Font variant",
581                                                              PANGO_TYPE_VARIANT,
582                                                              PANGO_VARIANT_NORMAL, 0),
583                                           GTK_STYLE_PROPERTY_INHERIT,
584                                           NULL,
585                                           NULL,
586                                           NULL,
587                                           NULL);
588   /* xxx: need to parse this properly, ie parse the numbers */
589   _gtk_style_property_register           (g_param_spec_enum ("font-weight",
590                                                              "Font weight",
591                                                              "Font weight",
592                                                              PANGO_TYPE_WEIGHT,
593                                                              PANGO_WEIGHT_NORMAL, 0),
594                                           GTK_STYLE_PROPERTY_INHERIT,
595                                           NULL,
596                                           NULL,
597                                           NULL,
598                                           NULL);
599   g_value_init (&value, G_TYPE_DOUBLE);
600   g_value_set_double (&value, 10);
601   _gtk_style_property_register           (g_param_spec_double ("font-size",
602                                                                "Font size",
603                                                                "Font size",
604                                                                0, G_MAXDOUBLE, 0, 0),
605                                           GTK_STYLE_PROPERTY_INHERIT,
606                                           NULL,
607                                           NULL,
608                                           NULL,
609                                           &value);
610   g_value_unset (&value);
611
612   _gtk_style_property_register           (g_param_spec_boxed ("text-shadow",
613                                                               "Text shadow",
614                                                               "Text shadow",
615                                                               GTK_TYPE_SHADOW, 0),
616                                           GTK_STYLE_PROPERTY_INHERIT,
617                                           NULL,
618                                           NULL,
619                                           NULL,
620                                           NULL);
621
622   _gtk_style_property_register           (g_param_spec_boxed ("icon-shadow",
623                                                               "Icon shadow",
624                                                               "Icon shadow",
625                                                               GTK_TYPE_SHADOW, 0),
626                                           GTK_STYLE_PROPERTY_INHERIT,
627                                           NULL,
628                                           NULL,
629                                           NULL,
630                                           NULL);
631
632   gtk_style_properties_register_property (NULL,
633                                           g_param_spec_boxed ("box-shadow",
634                                                               "Box shadow",
635                                                               "Box shadow",
636                                                               GTK_TYPE_SHADOW, 0));
637   gtk_style_properties_register_property (NULL,
638                                           g_param_spec_int ("margin-top",
639                                                             "margin top",
640                                                             "Margin at top",
641                                                             0, G_MAXINT, 0, 0));
642   gtk_style_properties_register_property (NULL,
643                                           g_param_spec_int ("margin-left",
644                                                             "margin left",
645                                                             "Margin at left",
646                                                             0, G_MAXINT, 0, 0));
647   gtk_style_properties_register_property (NULL,
648                                           g_param_spec_int ("margin-bottom",
649                                                             "margin bottom",
650                                                             "Margin at bottom",
651                                                             0, G_MAXINT, 0, 0));
652   gtk_style_properties_register_property (NULL,
653                                           g_param_spec_int ("margin-right",
654                                                             "margin right",
655                                                             "Margin at right",
656                                                             0, G_MAXINT, 0, 0));
657   gtk_style_properties_register_property (NULL,
658                                           g_param_spec_int ("padding-top",
659                                                             "padding top",
660                                                             "Padding at top",
661                                                             0, G_MAXINT, 0, 0));
662   gtk_style_properties_register_property (NULL,
663                                           g_param_spec_int ("padding-left",
664                                                             "padding left",
665                                                             "Padding at left",
666                                                             0, G_MAXINT, 0, 0));
667   gtk_style_properties_register_property (NULL,
668                                           g_param_spec_int ("padding-bottom",
669                                                             "padding bottom",
670                                                             "Padding at bottom",
671                                                             0, G_MAXINT, 0, 0));
672   gtk_style_properties_register_property (NULL,
673                                           g_param_spec_int ("padding-right",
674                                                             "padding right",
675                                                             "Padding at right",
676                                                             0, G_MAXINT, 0, 0));
677   gtk_style_properties_register_property (NULL,
678                                           g_param_spec_int ("border-top-width",
679                                                             "border top width",
680                                                             "Border width at top",
681                                                             0, G_MAXINT, 0, 0));
682   gtk_style_properties_register_property (NULL,
683                                           g_param_spec_int ("border-left-width",
684                                                             "border left width",
685                                                             "Border width at left",
686                                                             0, G_MAXINT, 0, 0));
687   gtk_style_properties_register_property (NULL,
688                                           g_param_spec_int ("border-bottom-width",
689                                                             "border bottom width",
690                                                             "Border width at bottom",
691                                                             0, G_MAXINT, 0, 0));
692   gtk_style_properties_register_property (NULL,
693                                           g_param_spec_int ("border-right-width",
694                                                             "border right width",
695                                                             "Border width at right",
696                                                             0, G_MAXINT, 0, 0));
697
698   _gtk_style_property_register           (g_param_spec_boxed ("border-top-left-radius",
699                                                               "Border top left radius",
700                                                               "Border radius of top left corner, in pixels",
701                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
702                                           0,
703                                           NULL,
704                                           border_corner_radius_value_parse,
705                                           border_corner_radius_value_print,
706                                           NULL);
707   _gtk_style_property_register           (g_param_spec_boxed ("border-top-right-radius",
708                                                               "Border top right radius",
709                                                               "Border radius of top right corner, in pixels",
710                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
711                                           0,
712                                           NULL,
713                                           border_corner_radius_value_parse,
714                                           border_corner_radius_value_print,
715                                           NULL);
716   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-right-radius",
717                                                               "Border bottom right radius",
718                                                               "Border radius of bottom right corner, in pixels",
719                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
720                                           0,
721                                           NULL,
722                                           border_corner_radius_value_parse,
723                                           border_corner_radius_value_print,
724                                           NULL);
725   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-left-radius",
726                                                               "Border bottom left radius",
727                                                               "Border radius of bottom left corner, in pixels",
728                                                               GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
729                                           0,
730                                           NULL,
731                                           border_corner_radius_value_parse,
732                                           border_corner_radius_value_print,
733                                           NULL);
734
735   gtk_style_properties_register_property (NULL,
736                                           g_param_spec_enum ("border-style",
737                                                              "Border style",
738                                                              "Border style",
739                                                              GTK_TYPE_BORDER_STYLE,
740                                                              GTK_BORDER_STYLE_NONE, 0));
741   gtk_style_properties_register_property (NULL,
742                                           g_param_spec_enum ("background-clip",
743                                                              "Background clip",
744                                                              "Background clip",
745                                                              GTK_TYPE_CSS_AREA,
746                                                              GTK_CSS_AREA_BORDER_BOX, 0));
747   gtk_style_properties_register_property (NULL,
748                                           g_param_spec_enum ("background-origin",
749                                                              "Background origin",
750                                                              "Background origin",
751                                                              GTK_TYPE_CSS_AREA,
752                                                              GTK_CSS_AREA_PADDING_BOX, 0));
753   g_value_init (&value, GTK_TYPE_CSS_SPECIAL_VALUE);
754   g_value_set_enum (&value, GTK_CSS_CURRENT_COLOR);
755   _gtk_style_property_register           (g_param_spec_boxed ("border-top-color",
756                                                               "Border top color",
757                                                               "Border top color",
758                                                               GDK_TYPE_RGBA, 0),
759                                           0,
760                                           NULL,
761                                           NULL,
762                                           NULL,
763                                           &value);
764   _gtk_style_property_register           (g_param_spec_boxed ("border-right-color",
765                                                               "Border right color",
766                                                               "Border right color",
767                                                               GDK_TYPE_RGBA, 0),
768                                           0,
769                                           NULL,
770                                           NULL,
771                                           NULL,
772                                           &value);
773   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-color",
774                                                               "Border bottom color",
775                                                               "Border bottom color",
776                                                               GDK_TYPE_RGBA, 0),
777                                           0,
778                                           NULL,
779                                           NULL,
780                                           NULL,
781                                           &value);
782   _gtk_style_property_register           (g_param_spec_boxed ("border-left-color",
783                                                               "Border left color",
784                                                               "Border left color",
785                                                               GDK_TYPE_RGBA, 0),
786                                           0,
787                                           NULL,
788                                           NULL,
789                                           NULL,
790                                           &value);
791   g_value_unset (&value);
792
793   gtk_style_properties_register_property (NULL,
794                                           g_param_spec_boxed ("background-image",
795                                                               "Background Image",
796                                                               "Background Image",
797                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
798   gtk_style_properties_register_property (NULL,
799                                           g_param_spec_boxed ("background-repeat",
800                                                               "Background repeat",
801                                                               "Background repeat",
802                                                               GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
803
804   gtk_style_properties_register_property (NULL,
805                                           g_param_spec_boxed ("border-image-source",
806                                                               "Border image source",
807                                                               "Border image source",
808                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
809   gtk_style_properties_register_property (NULL,
810                                           g_param_spec_boxed ("border-image-repeat",
811                                                               "Border image repeat",
812                                                               "Border image repeat",
813                                                               GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
814   gtk_style_properties_register_property (NULL,
815                                           g_param_spec_boxed ("border-image-slice",
816                                                               "Border image slice",
817                                                               "Border image slice",
818                                                               GTK_TYPE_BORDER, 0));
819   g_value_init (&value, GTK_TYPE_BORDER);
820   _gtk_style_property_register           (g_param_spec_boxed ("border-image-width",
821                                                               "Border image width",
822                                                               "Border image width",
823                                                               GTK_TYPE_BORDER, 0),
824                                           0,
825                                           NULL,
826                                           NULL,
827                                           NULL,
828                                           &value);
829   g_value_unset (&value);
830   gtk_style_properties_register_property (NULL,
831                                           g_param_spec_object ("engine",
832                                                                "Theming Engine",
833                                                                "Theming Engine",
834                                                                GTK_TYPE_THEMING_ENGINE, 0));
835   gtk_style_properties_register_property (NULL,
836                                           g_param_spec_boxed ("transition",
837                                                               "Transition animation description",
838                                                               "Transition animation description",
839                                                               GTK_TYPE_ANIMATION_DESCRIPTION, 0));
840
841   /* Private property holding the binding sets */
842   _gtk_style_property_register           (g_param_spec_boxed ("gtk-key-bindings",
843                                                               "Key bindings",
844                                                               "Key bindings",
845                                                               G_TYPE_PTR_ARRAY, 0),
846                                           0,
847                                           NULL,
848                                           bindings_value_parse,
849                                           bindings_value_print,
850                                           NULL);
851
852   /* initialize shorthands last, they depend on the real properties existing */
853   _gtk_css_shorthand_property_init_properties ();
854 }
855
856 /**
857  * _gtk_style_property_lookup:
858  * @name: name of the property to lookup
859  *
860  * Looks up the CSS property with the given @name. If no such
861  * property exists, %NULL is returned.
862  *
863  * Returns: (transfer none): The property or %NULL if no
864  *     property with the given name exists.
865  **/
866 GtkStyleProperty *
867 _gtk_style_property_lookup (const char *name)
868 {
869   GtkStylePropertyClass *klass;
870
871   g_return_val_if_fail (name != NULL, NULL);
872
873   gtk_style_property_init_properties ();
874
875   klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY);
876
877   return g_hash_table_lookup (klass->properties, name);
878 }
879
880 /**
881  * _gtk_style_property_get_name:
882  * @property: the property to query
883  *
884  * Gets the name of the given property.
885  *
886  * Returns: the name of the property
887  **/
888 const char *
889 _gtk_style_property_get_name (GtkStyleProperty *property)
890 {
891   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
892
893   return property->name;
894 }
895
896 /**
897  * _gtk_style_property_get_value_type:
898  * @property: the property to query
899  *
900  * Gets the value type of the @property, if the property is usable
901  * in public API via _gtk_style_property_assign() and
902  * _gtk_style_property_query(). If the @property is not usable in that
903  * way, %G_TYPE_NONE is returned.
904  *
905  * Returns: the value type in use or %G_TYPE_NONE if none.
906  **/
907 GType
908 _gtk_style_property_get_value_type (GtkStyleProperty *property)
909 {
910   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), G_TYPE_NONE);
911
912   return property->value_type;
913 }
914
915
916 void
917 _gtk_style_property_register (GParamSpec               *pspec,
918                               GtkStylePropertyFlags     flags,
919                               GtkStylePropertyParser    property_parse_func,
920                               GtkStyleParseFunc         parse_func,
921                               GtkStylePrintFunc         print_func,
922                               const GValue *            initial_value)
923 {
924   GtkStyleProperty *node;
925   GValue initial_fallback = { 0, };
926
927   if (initial_value == NULL)
928     {
929       g_value_init (&initial_fallback, pspec->value_type);
930       if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
931         g_value_set_object (&initial_fallback, gtk_theming_engine_load (NULL));
932       else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
933         g_value_take_boxed (&initial_fallback, pango_font_description_from_string ("Sans 10"));
934       else if (pspec->value_type == GDK_TYPE_RGBA)
935         {
936           GdkRGBA color;
937           gdk_rgba_parse (&color, "pink");
938           g_value_set_boxed (&initial_fallback, &color);
939         }
940       else if (pspec->value_type == GTK_TYPE_BORDER)
941         {
942           g_value_take_boxed (&initial_fallback, gtk_border_new ());
943         }
944       else
945         g_param_value_set_default (pspec, &initial_fallback);
946
947       initial_value = &initial_fallback;
948     }
949
950   node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
951                        "inherit", (flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE,
952                        "initial-value", initial_value,
953                        "name", pspec->name,
954                        "value-type", pspec->value_type,
955                        NULL);
956   g_assert (node->value_type == pspec->value_type);
957   node->pspec = pspec;
958   node->property_parse_func = property_parse_func;
959   node->parse_func = parse_func;
960   node->print_func = print_func;
961
962   if (G_IS_VALUE (&initial_fallback))
963     g_value_unset (&initial_fallback);
964 }