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