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