]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssvalue.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkcssvalue.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Red Hat, Inc.
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 "gtkprivate.h"
21 #include "gtkcssvalueprivate.h"
22
23 #include "gtkcsscomputedvaluesprivate.h"
24 #include "gtkstyleproviderprivate.h"
25
26 struct _GtkCssValue {
27   GTK_CSS_VALUE_BASE
28 };
29
30 G_DEFINE_BOXED_TYPE (GtkCssValue, _gtk_css_value, _gtk_css_value_ref, _gtk_css_value_unref)
31
32 GtkCssValue *
33 _gtk_css_value_alloc (const GtkCssValueClass *klass,
34                       gsize                   size)
35 {
36   GtkCssValue *value;
37
38   value = g_slice_alloc0 (size);
39
40   value->class = klass;
41   value->ref_count = 1;
42
43   return value;
44 }
45
46 GtkCssValue *
47 _gtk_css_value_ref (GtkCssValue *value)
48 {
49   gtk_internal_return_val_if_fail (value != NULL, NULL);
50
51   g_atomic_int_add (&value->ref_count, 1);
52
53   return value;
54 }
55
56 void
57 _gtk_css_value_unref (GtkCssValue *value)
58 {
59   if (value == NULL)
60     return;
61
62   if (!g_atomic_int_dec_and_test (&value->ref_count))
63     return;
64
65   value->class->free (value);
66 }
67
68 /**
69  * _gtk_css_value_compute:
70  * @value: the value to compute from
71  * @property_id: the ID of the property to compute
72  * @provider: Style provider for looking up extra information
73  * @values: values to compute for
74  * @parent_values: parent values to use for inherited values
75  * @dependencies: (out) (allow-none): Set to the dependencies of the
76  *     computed values that indicate when this value needs to be
77  *     recomputed and how.
78  *
79  * Converts the specified @value into the computed value for the CSS
80  * property given by @property_id using the information in @context.
81  * This step is explained in detail in
82  * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
83  * the CSS documentation</ulink>.
84  *
85  * Returns: the computed value
86  **/
87 GtkCssValue *
88 _gtk_css_value_compute (GtkCssValue             *value,
89                         guint                    property_id,
90                         GtkStyleProviderPrivate *provider,
91                         GtkCssComputedValues    *values,
92                         GtkCssComputedValues    *parent_values,
93                         GtkCssDependencies      *dependencies)
94 {
95   GtkCssDependencies fallback;
96
97   gtk_internal_return_val_if_fail (value != NULL, NULL);
98   gtk_internal_return_val_if_fail (GTK_IS_STYLE_PROVIDER_PRIVATE (provider), NULL);
99   gtk_internal_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
100   gtk_internal_return_val_if_fail (parent_values == NULL || GTK_IS_CSS_COMPUTED_VALUES (parent_values), NULL);
101
102   if (dependencies == NULL)
103     dependencies = &fallback;
104   *dependencies = 0;
105
106   return value->class->compute (value, property_id, provider, values, parent_values, dependencies);
107 }
108
109 gboolean
110 _gtk_css_value_equal (const GtkCssValue *value1,
111                       const GtkCssValue *value2)
112 {
113   gtk_internal_return_val_if_fail (value1 != NULL, FALSE);
114   gtk_internal_return_val_if_fail (value2 != NULL, FALSE);
115
116   if (value1 == value2)
117     return TRUE;
118
119   if (value1->class != value2->class)
120     return FALSE;
121
122   return value1->class->equal (value1, value2);
123 }
124
125 gboolean
126 _gtk_css_value_equal0 (const GtkCssValue *value1,
127                        const GtkCssValue *value2)
128 {
129   /* Inclues both values being NULL */
130   if (value1 == value2)
131     return TRUE;
132
133   if (value1 == NULL || value2 == NULL)
134     return FALSE;
135
136   return _gtk_css_value_equal (value1, value2);
137 }
138
139 GtkCssValue *
140 _gtk_css_value_transition (GtkCssValue *start,
141                            GtkCssValue *end,
142                            guint        property_id,
143                            double       progress)
144 {
145   gtk_internal_return_val_if_fail (start != NULL, FALSE);
146   gtk_internal_return_val_if_fail (end != NULL, FALSE);
147
148   if (start->class != end->class)
149     return NULL;
150
151   return start->class->transition (start, end, property_id, progress);
152 }
153
154 char *
155 _gtk_css_value_to_string (const GtkCssValue *value)
156 {
157   GString *string;
158
159   gtk_internal_return_val_if_fail (value != NULL, NULL);
160
161   string = g_string_new (NULL);
162   _gtk_css_value_print (value, string);
163   return g_string_free (string, FALSE);
164 }
165
166 /**
167  * _gtk_css_value_print:
168  * @value: the value to print
169  * @string: the string to print to
170  *
171  * Prints @value to the given @string in CSS format. The @value must be a
172  * valid specified value as parsed using the parse functions or as assigned
173  * via _gtk_style_property_assign().
174  **/
175 void
176 _gtk_css_value_print (const GtkCssValue *value,
177                       GString           *string)
178 {
179   gtk_internal_return_if_fail (value != NULL);
180   gtk_internal_return_if_fail (string != NULL);
181
182   value->class->print (value, string);
183 }
184