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