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 <gtk/css/gtkcss.h> |
24 | #include "gtk/css/gtkcsstokenizerprivate.h" |
25 | #include "gtk/css/gtkcssparserprivate.h" |
26 | #include "gtkcssshorthandpropertyprivate.h" |
27 | #include "gtkcssstylepropertyprivate.h" |
28 | #include "gtkcsstypesprivate.h" |
29 | #include "gtkintl.h" |
30 | #include "gtkprivatetypebuiltins.h" |
31 | |
32 | enum { |
33 | PROP_0, |
34 | PROP_NAME |
35 | }; |
36 | |
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 |
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 (hash_table: klass->properties, key: property->name, value: property); |
65 | break; |
66 | default: |
67 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
68 | break; |
69 | } |
70 | } |
71 | |
72 | static void |
73 | gtk_style_property_get_property (GObject *object, |
74 | guint prop_id, |
75 | GValue *value, |
76 | GParamSpec *pspec) |
77 | { |
78 | GtkStyleProperty *property = GTK_STYLE_PROPERTY (object); |
79 | |
80 | switch (prop_id) |
81 | { |
82 | case PROP_NAME: |
83 | g_value_set_string (value, v_string: property->name); |
84 | break; |
85 | default: |
86 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
87 | break; |
88 | } |
89 | } |
90 | |
91 | static void |
92 | _gtk_style_property_class_init (GtkStylePropertyClass *klass) |
93 | { |
94 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
95 | |
96 | object_class->finalize = gtk_style_property_finalize; |
97 | object_class->set_property = gtk_style_property_set_property; |
98 | object_class->get_property = gtk_style_property_get_property; |
99 | |
100 | g_object_class_install_property (oclass: object_class, |
101 | property_id: PROP_NAME, |
102 | pspec: g_param_spec_string (name: "name" , |
103 | P_("Property name" ), |
104 | P_("The name of the property" ), |
105 | NULL, |
106 | flags: G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
107 | |
108 | klass->properties = g_hash_table_new (hash_func: g_str_hash, key_equal_func: g_str_equal); |
109 | } |
110 | |
111 | static void |
112 | _gtk_style_property_init (GtkStyleProperty *property) |
113 | { |
114 | } |
115 | |
116 | /** |
117 | * _gtk_style_property_parse_value: |
118 | * @property: the property |
119 | * @parser: the parser to parse from |
120 | * |
121 | * Tries to parse the given @property from the given @parser into |
122 | * @value. The type that @value will be assigned is dependent on |
123 | * the parser and no assumptions must be made about it. If the |
124 | * parsing fails, %FALSE will be returned and @value will be |
125 | * left uninitialized. |
126 | * |
127 | * Only if @property is a `GtkCssShorthandProperty`, the @value will |
128 | * always be a `GtkCssValue` whose values can be queried with |
129 | * _gtk_css_array_value_get_nth(). |
130 | * |
131 | * Returns: (nullable): %NULL on failure or the parsed `GtkCssValue` |
132 | **/ |
133 | GtkCssValue * |
134 | _gtk_style_property_parse_value (GtkStyleProperty *property, |
135 | GtkCssParser *parser) |
136 | { |
137 | GtkStylePropertyClass *klass; |
138 | |
139 | g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL); |
140 | g_return_val_if_fail (parser != NULL, NULL); |
141 | |
142 | klass = GTK_STYLE_PROPERTY_GET_CLASS (property); |
143 | |
144 | return klass->parse_value (property, parser); |
145 | } |
146 | |
147 | void |
148 | _gtk_style_property_init_properties (void) |
149 | { |
150 | static gboolean initialized = FALSE; |
151 | |
152 | if (G_LIKELY (initialized)) |
153 | return; |
154 | |
155 | initialized = TRUE; |
156 | |
157 | _gtk_css_style_property_init_properties (); |
158 | /* initialize shorthands last, they depend on the real properties existing */ |
159 | _gtk_css_shorthand_property_init_properties (); |
160 | } |
161 | |
162 | /** |
163 | * _gtk_style_property_lookup: |
164 | * @name: name of the property to lookup |
165 | * |
166 | * Looks up the CSS property with the given @name. If no such |
167 | * property exists, %NULL is returned. |
168 | * |
169 | * Returns: (nullable) (transfer none): The property |
170 | */ |
171 | GtkStyleProperty * |
172 | _gtk_style_property_lookup (const char *name) |
173 | { |
174 | GtkStylePropertyClass *klass; |
175 | |
176 | g_return_val_if_fail (name != NULL, NULL); |
177 | |
178 | _gtk_style_property_init_properties (); |
179 | |
180 | klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY); |
181 | |
182 | return g_hash_table_lookup (hash_table: klass->properties, key: name); |
183 | } |
184 | |
185 | /** |
186 | * _gtk_style_property_get_name: |
187 | * @property: the property to query |
188 | * |
189 | * Gets the name of the given property. |
190 | * |
191 | * Returns: the name of the property |
192 | **/ |
193 | const char * |
194 | _gtk_style_property_get_name (GtkStyleProperty *property) |
195 | { |
196 | g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL); |
197 | |
198 | return property->name; |
199 | } |
200 | |