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 "gtkcssstyleprivate.h"
24 #include "gtkstyleproviderprivate.h"
25 
26 struct _GtkCssValue {
27   GTK_CSS_VALUE_BASE
28 };
29 
G_DEFINE_BOXED_TYPE(GtkCssValue,_gtk_css_value,_gtk_css_value_ref,_gtk_css_value_unref)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 *
_gtk_css_value_ref(GtkCssValue * value)47 _gtk_css_value_ref (GtkCssValue *value)
48 {
49   gtk_internal_return_val_if_fail (value != NULL, NULL);
50 
51   value->ref_count += 1;
52 
53   return value;
54 }
55 
56 void
_gtk_css_value_unref(GtkCssValue * value)57 _gtk_css_value_unref (GtkCssValue *value)
58 {
59   if (value == NULL)
60     return;
61 
62   value->ref_count -= 1;
63   if (value->ref_count > 0)
64     return;
65 
66   value->class->free (value);
67 }
68 
69 /**
70  * _gtk_css_value_compute:
71  * @value: the value to compute from
72  * @property_id: the ID of the property to compute
73  * @provider: Style provider for looking up extra information
74  * @style: Style to compute for
75  * @parent_style: parent style to use for inherited values
76  *
77  * Converts the specified @value into the computed value for the CSS
78  * property given by @property_id using the information in @context.
79  * This step is explained in detail in the
80  * [CSS Documentation](http://www.w3.org/TR/css3-cascade/#computed).
81  *
82  * Returns: the computed value
83  **/
84 GtkCssValue *
_gtk_css_value_compute(GtkCssValue * value,guint property_id,GtkStyleProviderPrivate * provider,GtkCssStyle * style,GtkCssStyle * parent_style)85 _gtk_css_value_compute (GtkCssValue             *value,
86                         guint                    property_id,
87                         GtkStyleProviderPrivate *provider,
88                         GtkCssStyle             *style,
89                         GtkCssStyle             *parent_style)
90 {
91 
92   gtk_internal_return_val_if_fail (value != NULL, NULL);
93   gtk_internal_return_val_if_fail (GTK_IS_STYLE_PROVIDER_PRIVATE (provider), NULL);
94   gtk_internal_return_val_if_fail (GTK_IS_CSS_STYLE (style), NULL);
95   gtk_internal_return_val_if_fail (parent_style == NULL || GTK_IS_CSS_STYLE (parent_style), NULL);
96 
97   return value->class->compute (value, property_id, provider, style, parent_style);
98 }
99 
100 gboolean
_gtk_css_value_equal(const GtkCssValue * value1,const GtkCssValue * value2)101 _gtk_css_value_equal (const GtkCssValue *value1,
102                       const GtkCssValue *value2)
103 {
104   gtk_internal_return_val_if_fail (value1 != NULL, FALSE);
105   gtk_internal_return_val_if_fail (value2 != NULL, FALSE);
106 
107   if (value1 == value2)
108     return TRUE;
109 
110   if (value1->class != value2->class)
111     return FALSE;
112 
113   return value1->class->equal (value1, value2);
114 }
115 
116 gboolean
_gtk_css_value_equal0(const GtkCssValue * value1,const GtkCssValue * value2)117 _gtk_css_value_equal0 (const GtkCssValue *value1,
118                        const GtkCssValue *value2)
119 {
120   /* Inclues both values being NULL */
121   if (value1 == value2)
122     return TRUE;
123 
124   if (value1 == NULL || value2 == NULL)
125     return FALSE;
126 
127   return _gtk_css_value_equal (value1, value2);
128 }
129 
130 GtkCssValue *
_gtk_css_value_transition(GtkCssValue * start,GtkCssValue * end,guint property_id,double progress)131 _gtk_css_value_transition (GtkCssValue *start,
132                            GtkCssValue *end,
133                            guint        property_id,
134                            double       progress)
135 {
136   gtk_internal_return_val_if_fail (start != NULL, FALSE);
137   gtk_internal_return_val_if_fail (end != NULL, FALSE);
138 
139   /* We compare functions here instead of classes so that number
140    * values can all transition to each other */
141   if (start->class->transition != end->class->transition)
142     return NULL;
143 
144   return start->class->transition (start, end, property_id, progress);
145 }
146 
147 char *
_gtk_css_value_to_string(const GtkCssValue * value)148 _gtk_css_value_to_string (const GtkCssValue *value)
149 {
150   GString *string;
151 
152   gtk_internal_return_val_if_fail (value != NULL, NULL);
153 
154   string = g_string_new (NULL);
155   _gtk_css_value_print (value, string);
156   return g_string_free (string, FALSE);
157 }
158 
159 /**
160  * _gtk_css_value_print:
161  * @value: the value to print
162  * @string: the string to print to
163  *
164  * Prints @value to the given @string in CSS format. The @value must be a
165  * valid specified value as parsed using the parse functions or as assigned
166  * via _gtk_style_property_assign().
167  **/
168 void
_gtk_css_value_print(const GtkCssValue * value,GString * string)169 _gtk_css_value_print (const GtkCssValue *value,
170                       GString           *string)
171 {
172   gtk_internal_return_if_fail (value != NULL);
173   gtk_internal_return_if_fail (string != NULL);
174 
175   value->class->print (value, string);
176 }
177 
178