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