]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsslookup.c
Change FSF Address
[~andy/gtk] / gtk / gtkcsslookup.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Benjamin Otte <otte@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 "gtkcsslookupprivate.h"
21
22 #include "gtkcsstypesprivate.h"
23 #include "gtkprivatetypebuiltins.h"
24 #include "gtkcssstylepropertyprivate.h"
25 #include "gtkstylepropertiesprivate.h"
26
27 typedef struct {
28   GtkCssSection     *section;
29   const GValue      *value;
30   const GValue      *computed;
31 } GtkCssLookupValue;
32
33 struct _GtkCssLookup {
34   GtkBitmask        *missing;
35   GtkCssLookupValue  values[1];
36 };
37
38 GtkCssLookup *
39 _gtk_css_lookup_new (void)
40 {
41   GtkCssLookup *lookup;
42   guint n = _gtk_css_style_property_get_n_properties ();
43
44   lookup = g_malloc0 (sizeof (GtkCssLookup) + sizeof (GtkCssLookupValue) * n);
45   lookup->missing = _gtk_bitmask_new ();
46   _gtk_bitmask_invert_range (lookup->missing, 0, n);
47
48   return lookup;
49 }
50
51 void
52 _gtk_css_lookup_free (GtkCssLookup *lookup)
53 {
54   g_return_if_fail (lookup != NULL);
55
56   _gtk_bitmask_free (lookup->missing);
57   g_free (lookup);
58 }
59
60 const GtkBitmask *
61 _gtk_css_lookup_get_missing (const GtkCssLookup *lookup)
62 {
63   g_return_val_if_fail (lookup != NULL, NULL);
64
65   return lookup->missing;
66 }
67
68 gboolean
69 _gtk_css_lookup_is_missing (const GtkCssLookup *lookup,
70                             guint               id)
71 {
72   g_return_val_if_fail (lookup != NULL, FALSE);
73
74   return _gtk_bitmask_get (lookup->missing, id);
75 }
76
77 /**
78  * _gtk_css_lookup_set:
79  * @lookup: the lookup
80  * @id: id of the property to set, see _gtk_style_property_get_id()
81  * @section: (allow-none): The @section the value was defined in or %NULL
82  * @value: the "cascading value" to use
83  *
84  * Sets the @value for a given @id. No value may have been set for @id
85  * before. See _gtk_css_lookup_is_missing(). This function is used to
86  * set the "winning declaration" of a lookup. Note that for performance
87  * reasons @value and @section are not copied. It is your responsibility
88  * to ensure they are kept alive until _gtk_css_lookup_free() is called.
89  **/
90 void
91 _gtk_css_lookup_set (GtkCssLookup  *lookup,
92                      guint          id,
93                      GtkCssSection *section,
94                      const GValue  *value)
95 {
96   g_return_if_fail (lookup != NULL);
97   g_return_if_fail (_gtk_bitmask_get (lookup->missing, id));
98   g_return_if_fail (value != NULL);
99
100   _gtk_bitmask_set (lookup->missing, id, FALSE);
101   lookup->values[id].value = value;
102   lookup->values[id].section = section;
103 }
104
105 /**
106  * _gtk_css_lookup_set_computed:
107  * @lookup: the lookup
108  * @id: id of the property to set, see _gtk_style_property_get_id()
109  * @section: (allow-none): The @section the value was defined in or %NULL
110  * @value: the "computed value" to use
111  *
112  * Sets the @value for a given @id. No value may have been set for @id
113  * before. See _gtk_css_lookup_is_missing(). This function is used to
114  * set the "winning declaration" of a lookup. Note that for performance
115  * reasons @value and @section are not copied. It is your responsibility
116  * to ensure they are kept alive until _gtk_css_lookup_free() is called.
117  *
118  * As opposed to _gtk_css_lookup_set(), this function forces a computed
119  * value and will not cause computation to happen. In particular, with this
120  * method relative lengths or symbolic colors can not be used. This is
121  * usually only useful for doing overrides. It should not be used for proper
122  * CSS.
123  **/
124 void
125 _gtk_css_lookup_set_computed (GtkCssLookup  *lookup,
126                               guint          id,
127                               GtkCssSection *section,
128                               const GValue  *value)
129 {
130   g_return_if_fail (lookup != NULL);
131   g_return_if_fail (_gtk_bitmask_get (lookup->missing, id));
132   g_return_if_fail (value != NULL);
133
134   _gtk_bitmask_set (lookup->missing, id, FALSE);
135   lookup->values[id].computed = value;
136   lookup->values[id].section = section;
137 }
138
139 /**
140  * _gtk_css_lookup_resolve:
141  * @lookup: the lookup
142  * @context: the context the values are resolved for
143  * @values: a new #GtkCssComputedValues to be filled with the new properties
144  *
145  * Resolves the current lookup into a styleproperties object. This is done
146  * by converting from the "winning declaration" to the "computed value".
147  *
148  * XXX: This bypasses the notion of "specified value". If this ever becomes
149  * an issue, go fix it.
150  **/
151 void
152 _gtk_css_lookup_resolve (GtkCssLookup         *lookup,
153                          GtkStyleContext      *context,
154                          GtkCssComputedValues *values)
155 {
156   guint i, n;
157
158   g_return_if_fail (lookup != NULL);
159   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
160   g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
161
162   n = _gtk_css_style_property_get_n_properties ();
163
164   for (i = 0; i < n; i++)
165     {
166       if (lookup->values[i].computed)
167         _gtk_css_computed_values_set_value (values,
168                                             i,
169                                             lookup->values[i].computed,
170                                             lookup->values[i].section);
171       else
172         _gtk_css_computed_values_compute_value (values,
173                                                 context,
174                                                 i,
175                                                 lookup->values[i].value,
176                                                 lookup->values[i].section);
177     }
178 }