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