]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssshorthandproperty.c
css: Return GArrays from shorthand parsing
[~andy/gtk] / gtk / gtkcssshorthandproperty.c
1 /*
2  * Copyright © 2011 Red Hat Inc.
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.1 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 Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Benjamin Otte <otte@gnome.org>
19  */
20
21 #include "config.h"
22
23 #include "gtkcssshorthandpropertyprivate.h"
24
25 #include "gtkcssstylefuncsprivate.h"
26 #include "gtkcsstypesprivate.h"
27 #include "gtkintl.h"
28 #include "gtkprivatetypebuiltins.h"
29
30 enum {
31   PROP_0,
32   PROP_SUBPROPERTIES,
33 };
34
35 G_DEFINE_TYPE (GtkCssShorthandProperty, _gtk_css_shorthand_property, GTK_TYPE_STYLE_PROPERTY)
36
37 static void
38 gtk_css_shorthand_property_set_property (GObject      *object,
39                                          guint         prop_id,
40                                          const GValue *value,
41                                          GParamSpec   *pspec)
42 {
43   GtkCssShorthandProperty *property = GTK_CSS_SHORTHAND_PROPERTY (object);
44   const char **subproperties;
45   guint i;
46
47   switch (prop_id)
48     {
49     case PROP_SUBPROPERTIES:
50       subproperties = g_value_get_boxed (value);
51       g_assert (subproperties);
52       for (i = 0; subproperties[i] != NULL; i++)
53         {
54           GtkStyleProperty *subproperty = _gtk_style_property_lookup (subproperties[i]);
55           g_assert (GTK_IS_CSS_STYLE_PROPERTY (subproperty));
56           g_ptr_array_add (property->subproperties, subproperty);
57         }
58       break;
59     default:
60       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
61       break;
62     }
63 }
64
65 static void
66 _gtk_css_shorthand_property_assign (GtkStyleProperty   *property,
67                                     GtkStyleProperties *props,
68                                     GtkStateFlags       state,
69                                     const GValue       *value)
70 {
71   GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
72
73   shorthand->assign (shorthand, props, state, value);
74 }
75
76 static void
77 _gtk_css_shorthand_property_query (GtkStyleProperty   *property,
78                                    GValue             *value,
79                                    GtkStyleQueryFunc   query_func,
80                                    gpointer            query_data)
81 {
82   GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
83
84   shorthand->query (shorthand, value, query_func, query_data);
85 }
86
87 static gboolean
88 gtk_css_shorthand_property_parse_value (GtkStyleProperty *property,
89                                         GValue           *value,
90                                         GtkCssParser     *parser,
91                                         GFile            *base)
92 {
93   GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
94   GArray *array;
95   guint i;
96
97   array = g_array_new (FALSE, TRUE, sizeof (GValue));
98   g_array_set_clear_func (array, (GDestroyNotify) g_value_unset);
99   g_array_set_size (array, shorthand->subproperties->len);
100
101   if (_gtk_css_parser_try (parser, "initial", TRUE))
102     {
103       /* the initial value can be explicitly specified with the
104        * ‘initial’ keyword which all properties accept.
105        */
106       for (i = 0; i < shorthand->subproperties->len; i++)
107         {
108           GValue *val = &g_array_index (array, GValue, i);
109           g_value_init (val, GTK_TYPE_CSS_SPECIAL_VALUE);
110           g_value_set_enum (val, GTK_CSS_INITIAL);
111         }
112     }
113   else if (_gtk_css_parser_try (parser, "inherit", TRUE))
114     {
115       /* All properties accept the ‘inherit’ value which
116        * explicitly specifies that the value will be determined
117        * by inheritance. The ‘inherit’ value can be used to
118        * strengthen inherited values in the cascade, and it can
119        * also be used on properties that are not normally inherited.
120        */
121       for (i = 0; i < shorthand->subproperties->len; i++)
122         {
123           GValue *val = &g_array_index (array, GValue, i);
124           g_value_init (val, GTK_TYPE_CSS_SPECIAL_VALUE);
125           g_value_set_enum (val, GTK_CSS_INHERIT);
126         }
127     }
128   else if (!shorthand->parse (shorthand, (GValue *) array->data, parser, base))
129     {
130       g_array_free (array, TRUE);
131       return FALSE;
132     }
133
134   /* All values that aren't set by the parse func are set to their
135    * default values here.
136    * XXX: Is the default always initial or can it be inherit? */
137   for (i = 0; i < shorthand->subproperties->len; i++)
138     {
139       GValue *val = &g_array_index (array, GValue, i);
140       if (G_IS_VALUE (val))
141         continue;
142       g_value_init (val, GTK_TYPE_CSS_SPECIAL_VALUE);
143       g_value_set_enum (val, GTK_CSS_INITIAL);
144     }
145
146   g_value_init (value, G_TYPE_ARRAY);
147   g_value_take_boxed (value, array);
148   return TRUE;
149 }
150
151 static void
152 _gtk_css_shorthand_property_class_init (GtkCssShorthandPropertyClass *klass)
153 {
154   GObjectClass *object_class = G_OBJECT_CLASS (klass);
155   GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
156
157   object_class->set_property = gtk_css_shorthand_property_set_property;
158
159   g_object_class_install_property (object_class,
160                                    PROP_SUBPROPERTIES,
161                                    g_param_spec_boxed ("subproperties",
162                                                        P_("Subproperties"),
163                                                        P_("The list of subproperties"),
164                                                        G_TYPE_STRV,
165                                                        G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
166
167   property_class->assign = _gtk_css_shorthand_property_assign;
168   property_class->query = _gtk_css_shorthand_property_query;
169   property_class->parse_value = gtk_css_shorthand_property_parse_value;
170 }
171
172 static void
173 _gtk_css_shorthand_property_init (GtkCssShorthandProperty *shorthand)
174 {
175   shorthand->subproperties = g_ptr_array_new_with_free_func (g_object_unref);
176 }
177
178 GtkCssStyleProperty *
179 _gtk_css_shorthand_property_get_subproperty (GtkCssShorthandProperty *shorthand,
180                                              guint                    property)
181 {
182   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), NULL);
183   g_return_val_if_fail (property < shorthand->subproperties->len, NULL);
184
185   return g_ptr_array_index (shorthand->subproperties, property);
186 }
187
188 guint
189 _gtk_css_shorthand_property_get_n_subproperties (GtkCssShorthandProperty *shorthand)
190 {
191   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), 0);
192   
193   return shorthand->subproperties->len;
194 }
195