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