]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperty.c
styleproperty: Move implementations to separate file
[~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 "gtkcssprovider.h"
25 #include "gtkcssparserprivate.h"
26 #include "gtkcssshorthandpropertyprivate.h"
27 #include "gtkcssstylefuncsprivate.h"
28 #include "gtkcssstylepropertyprivate.h"
29 #include "gtkcsstypesprivate.h"
30 #include "gtkintl.h"
31 #include "gtkprivatetypebuiltins.h"
32 #include "gtkstylepropertiesprivate.h"
33
34 enum {
35   PROP_0,
36   PROP_NAME,
37   PROP_VALUE_TYPE
38 };
39
40 G_DEFINE_ABSTRACT_TYPE (GtkStyleProperty, _gtk_style_property, G_TYPE_OBJECT)
41
42 static void
43 gtk_style_property_finalize (GObject *object)
44 {
45   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
46
47   g_warning ("finalizing %s `%s', how could this happen?", G_OBJECT_TYPE_NAME (object), property->name);
48
49   G_OBJECT_CLASS (_gtk_style_property_parent_class)->finalize (object);
50 }
51
52 static void
53 gtk_style_property_set_property (GObject      *object,
54                                  guint         prop_id,
55                                  const GValue *value,
56                                  GParamSpec   *pspec)
57 {
58   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
59   GtkStylePropertyClass *klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
60
61   switch (prop_id)
62     {
63     case PROP_NAME:
64       property->name = g_value_dup_string (value);
65       g_assert (property->name);
66       g_assert (g_hash_table_lookup (klass->properties, property->name) == NULL);
67       g_hash_table_insert (klass->properties, property->name, property);
68       break;
69     case PROP_VALUE_TYPE:
70       property->value_type = g_value_get_gtype (value);
71       break;
72     default:
73       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
74       break;
75     }
76 }
77
78 static void
79 gtk_style_property_get_property (GObject    *object,
80                                  guint       prop_id,
81                                  GValue     *value,
82                                  GParamSpec *pspec)
83 {
84   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
85
86   switch (prop_id)
87     {
88     case PROP_NAME:
89       g_value_set_string (value, property->name);
90       break;
91     case PROP_VALUE_TYPE:
92       g_value_set_gtype (value, property->value_type);
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_class_init (GtkStylePropertyClass *klass)
102 {
103   GObjectClass *object_class = G_OBJECT_CLASS (klass);
104
105   object_class->finalize = gtk_style_property_finalize;
106   object_class->set_property = gtk_style_property_set_property;
107   object_class->get_property = gtk_style_property_get_property;
108
109   g_object_class_install_property (object_class,
110                                    PROP_NAME,
111                                    g_param_spec_string ("name",
112                                                         P_("Property name"),
113                                                         P_("The name of the property"),
114                                                         NULL,
115                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
116   g_object_class_install_property (object_class,
117                                    PROP_VALUE_TYPE,
118                                    g_param_spec_gtype ("value-type",
119                                                        P_("Value type"),
120                                                        P_("The value type returned by GtkStyleContext"),
121                                                        G_TYPE_NONE,
122                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
123
124   klass->properties = g_hash_table_new (g_str_hash, g_str_equal);
125 }
126
127 static void
128 _gtk_style_property_init (GtkStyleProperty *property)
129 {
130   property->value_type = G_TYPE_NONE;
131 }
132
133 /**
134  * _gtk_style_property_parse_value:
135  * @property: the property
136  * @value: an uninitialized value
137  * @parser: the parser to parse from
138  * @base: the base file for @aprser
139  *
140  * Tries to parse the given @property from the given @parser into
141  * @value. The type that @value will be assigned is dependant on
142  * the parser and no assumptions must be made about it. If the
143  * parsing fails, %FALSE will be returned and @value will be
144  * left uninitialized.
145  *
146  * Only if @property is a #GtkCssShorthandProperty, the @value will
147  * always contain a #GValueArray with the values to be used for
148  * the subproperties.
149  *
150  * Returns: %TRUE on success
151  **/
152 gboolean
153 _gtk_style_property_parse_value (GtkStyleProperty *property,
154                                  GValue           *value,
155                                  GtkCssParser     *parser,
156                                  GFile            *base)
157 {
158   GtkStylePropertyClass *klass;
159
160   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), FALSE);
161   g_return_val_if_fail (value != NULL, FALSE);
162   g_return_val_if_fail (parser != NULL, FALSE);
163
164   klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
165
166   return klass->parse_value (property, value, parser, base);
167 }
168
169 /**
170  * _gtk_style_property_assign:
171  * @property: the property
172  * @props: The properties to assign to
173  * @state: The state to assign
174  * @value: (out): the #GValue with the value to be
175  *     assigned
176  *
177  * This function is called by gtk_style_properties_set() and in
178  * turn gtk_style_context_set() and similar functions to set the
179  * value from code using old APIs.
180  **/
181 void
182 _gtk_style_property_assign (GtkStyleProperty   *property,
183                             GtkStyleProperties *props,
184                             GtkStateFlags       state,
185                             const GValue       *value)
186 {
187   GtkStylePropertyClass *klass;
188
189   g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
190   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
191   g_return_if_fail (value != NULL);
192
193   klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
194
195   klass->assign (property, props, state, value);
196 }
197
198 /**
199  * _gtk_style_property_query:
200  * @property: the property
201  * @props: The properties to query
202  * @state: The state to query
203  * @value: (out): an uninitialized #GValue to be filled with the
204  *   contents of the lookup
205  *
206  * This function is called by gtk_style_properties_get() and in
207  * turn gtk_style_context_get() and similar functions to get the
208  * value to return to code using old APIs.
209  **/
210 void
211 _gtk_style_property_query (GtkStyleProperty        *property,
212                            GtkStyleProperties      *props,
213                            GtkStateFlags            state,
214                            GtkStylePropertyContext *context,
215                            GValue                  *value)
216 {
217   GtkStylePropertyClass *klass;
218
219   g_return_if_fail (property != NULL);
220   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
221   g_return_if_fail (context != NULL);
222   g_return_if_fail (value != NULL);
223
224   klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
225
226   g_value_init (value, property->value_type);
227
228   klass->query (property, props, state, context, value);
229 }
230
231 static void
232 gtk_style_property_init_properties (void)
233 {
234   static gboolean initialized = FALSE;
235
236   if (G_LIKELY (initialized))
237     return;
238
239   initialized = TRUE;
240
241   _gtk_css_style_property_init_properties ();
242   /* initialize shorthands last, they depend on the real properties existing */
243   _gtk_css_shorthand_property_init_properties ();
244 }
245
246 /**
247  * _gtk_style_property_lookup:
248  * @name: name of the property to lookup
249  *
250  * Looks up the CSS property with the given @name. If no such
251  * property exists, %NULL is returned.
252  *
253  * Returns: (transfer none): The property or %NULL if no
254  *     property with the given name exists.
255  **/
256 GtkStyleProperty *
257 _gtk_style_property_lookup (const char *name)
258 {
259   GtkStylePropertyClass *klass;
260
261   g_return_val_if_fail (name != NULL, NULL);
262
263   gtk_style_property_init_properties ();
264
265   klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY);
266
267   return g_hash_table_lookup (klass->properties, name);
268 }
269
270 /**
271  * _gtk_style_property_get_name:
272  * @property: the property to query
273  *
274  * Gets the name of the given property.
275  *
276  * Returns: the name of the property
277  **/
278 const char *
279 _gtk_style_property_get_name (GtkStyleProperty *property)
280 {
281   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
282
283   return property->name;
284 }
285
286 /**
287  * _gtk_style_property_get_value_type:
288  * @property: the property to query
289  *
290  * Gets the value type of the @property, if the property is usable
291  * in public API via _gtk_style_property_assign() and
292  * _gtk_style_property_query(). If the @property is not usable in that
293  * way, %G_TYPE_NONE is returned.
294  *
295  * Returns: the value type in use or %G_TYPE_NONE if none.
296  **/
297 GType
298 _gtk_style_property_get_value_type (GtkStyleProperty *property)
299 {
300   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), G_TYPE_NONE);
301
302   return property->value_type;
303 }