]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssanimatedvalues.c
stylecontext: Add GtkCssAnimatedValues
[~andy/gtk] / gtk / gtkcssanimatedvalues.c
1 /*
2  * Copyright © 2012 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.1 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  * Authors: Benjamin Otte <otte@gnome.org>
18  */
19
20 #include "config.h"
21
22 #include "gtkcssanimatedvaluesprivate.h"
23
24 #include "gtkcssarrayvalueprivate.h"
25 #include "gtkcssnumbervalueprivate.h"
26 #include "gtkcssshorthandpropertyprivate.h"
27 #include "gtkcssstringvalueprivate.h"
28 #include "gtkcssstylepropertyprivate.h"
29 #include "gtkcsstransitionprivate.h"
30 #include "gtkstyleanimationprivate.h"
31 #include "gtkstylepropertyprivate.h"
32
33 G_DEFINE_TYPE (GtkCssAnimatedValues, _gtk_css_animated_values, GTK_TYPE_CSS_COMPUTED_VALUES)
34
35 static void
36 gtk_css_animated_values_dispose (GObject *object)
37 {
38   GtkCssAnimatedValues *values = GTK_CSS_ANIMATED_VALUES (object);
39
40   g_clear_object (&values->computed);
41   g_slist_free_full (values->animations, g_object_unref);
42   values->animations = NULL;
43
44   G_OBJECT_CLASS (_gtk_css_animated_values_parent_class)->dispose (object);
45 }
46
47 static void
48 _gtk_css_animated_values_class_init (GtkCssAnimatedValuesClass *klass)
49 {
50   GObjectClass *object_class = G_OBJECT_CLASS (klass);
51
52   object_class->dispose = gtk_css_animated_values_dispose;
53 }
54
55 static void
56 _gtk_css_animated_values_init (GtkCssAnimatedValues *animated_values)
57 {
58 }
59
60 /* TRANSITIONS */
61
62 typedef struct _TransitionInfo TransitionInfo;
63 struct _TransitionInfo {
64   guint index;                  /* index into value arrays */
65   gboolean pending;             /* TRUE if we still need to handle it */
66 };
67
68 static void
69 transition_info_add (TransitionInfo    infos[GTK_CSS_PROPERTY_N_PROPERTIES],
70                      GtkStyleProperty *property,
71                      guint             index)
72 {
73   if (property == NULL)
74     {
75       guint i;
76
77       for (i = 0; i < _gtk_css_style_property_get_n_properties (); i++)
78         {
79           GtkCssStyleProperty *prop = _gtk_css_style_property_lookup_by_id (i);
80
81           transition_info_add (infos, GTK_STYLE_PROPERTY (prop), index);
82         }
83     }
84   else if (GTK_IS_CSS_SHORTHAND_PROPERTY (property))
85     {
86       GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
87       guint i;
88
89       for (i = 0; i < _gtk_css_shorthand_property_get_n_subproperties (shorthand); i++)
90         {
91           GtkCssStyleProperty *prop = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
92
93           transition_info_add (infos, GTK_STYLE_PROPERTY (prop), index);
94         }
95     }
96   else if (GTK_IS_CSS_STYLE_PROPERTY (property))
97     {
98       guint id;
99       
100       if (!_gtk_css_style_property_is_animated (GTK_CSS_STYLE_PROPERTY (property)))
101         return;
102
103       id = _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (property));
104       g_assert (id < GTK_CSS_PROPERTY_N_PROPERTIES);
105       infos[id].index = index;
106       infos[id].pending = TRUE;
107     }
108   else
109     {
110       g_assert_not_reached ();
111     }
112 }
113
114 static void
115 transition_infos_set (TransitionInfo  infos[GTK_CSS_PROPERTY_N_PROPERTIES],
116                       GtkCssValue    *transitions)
117 {
118   guint i;
119
120   for (i = 0; i < _gtk_css_array_value_get_n_values (transitions); i++)
121     {
122       GtkStyleProperty *property;
123       GtkCssValue *prop_value;
124
125       prop_value = _gtk_css_array_value_get_nth (transitions, i);
126       if (g_ascii_strcasecmp (_gtk_css_ident_value_get (prop_value), "all") == 0)
127         property = NULL;
128       else
129         {
130           property = _gtk_style_property_lookup (_gtk_css_ident_value_get (prop_value));
131           if (property == NULL)
132             continue;
133         }
134       
135       transition_info_add (infos, property, i);
136     }
137 }
138
139 static GtkStyleAnimation *
140 gtk_css_animated_values_find_transition (GtkCssAnimatedValues *values,
141                                          guint                 property_id)
142 {
143   GSList *list;
144
145   for (list = values->animations; list; list = list->next)
146     {
147       if (!GTK_IS_CSS_TRANSITION (list->data))
148         continue;
149
150       if (_gtk_css_transition_get_property (list->data) == property_id)
151         return list->data;
152     }
153
154   return NULL;
155 }
156
157 static void
158 gtk_css_animated_values_start_transitions (GtkCssAnimatedValues *values,
159                                            gint64                timestamp,
160                                            GtkCssComputedValues *source)
161 {
162   TransitionInfo transitions[GTK_CSS_PROPERTY_N_PROPERTIES] = { { 0, } };
163   GtkCssComputedValues *source_computed, *computed;
164   GtkCssValue *durations, *delays, *timing_functions;
165   guint i;
166
167   computed = GTK_CSS_COMPUTED_VALUES (values);
168   if (GTK_IS_CSS_ANIMATED_VALUES (source))
169     source_computed = GTK_CSS_ANIMATED_VALUES (source)->computed;
170   else
171     source_computed = source;
172
173   transition_infos_set (transitions, _gtk_css_computed_values_get_value (computed, GTK_CSS_PROPERTY_TRANSITION_PROPERTY));
174
175   durations = _gtk_css_computed_values_get_value (computed, GTK_CSS_PROPERTY_TRANSITION_DURATION);
176   delays = _gtk_css_computed_values_get_value (computed, GTK_CSS_PROPERTY_TRANSITION_DELAY);
177   timing_functions = _gtk_css_computed_values_get_value (computed, GTK_CSS_PROPERTY_TRANSITION_TIMING_FUNCTION);
178
179
180   for (i = 0; i < GTK_CSS_PROPERTY_N_PROPERTIES; i++)
181     {
182       GtkStyleAnimation *animation;
183       GtkCssValue *start, *end;
184       double duration, delay;
185
186       if (!transitions[i].pending)
187         continue;
188
189       duration = _gtk_css_number_value_get (_gtk_css_array_value_get_nth (durations, transitions[i].index), 100);
190       delay = _gtk_css_number_value_get (_gtk_css_array_value_get_nth (delays, transitions[i].index), 100);
191       if (duration + delay == 0.0)
192         continue;
193
194       start = _gtk_css_computed_values_get_value (source_computed, i);
195       end = _gtk_css_computed_values_get_value (values->computed, i);
196       if (_gtk_css_value_equal (start, end))
197         {
198           if (GTK_IS_CSS_ANIMATED_VALUES (source))
199             {
200               animation = gtk_css_animated_values_find_transition (GTK_CSS_ANIMATED_VALUES (source), i);
201               if (animation)
202                 values->animations = g_slist_prepend (values->animations, g_object_ref (animation));
203             }
204         }
205       else
206         {
207           animation = _gtk_css_transition_new (i,
208                                                start,
209                                                end,
210                                                _gtk_css_array_value_get_nth (timing_functions, i),
211                                                timestamp + delay * G_USEC_PER_SEC,
212                                                timestamp + (delay + duration) * G_USEC_PER_SEC);
213           values->animations = g_slist_prepend (values->animations, animation);
214         }
215     }
216 }
217
218 /* PUBLIC API */
219
220 static void
221 gtk_css_animated_values_start_animations (GtkCssAnimatedValues *values,
222                                           gint64                timestamp,
223                                           GtkCssComputedValues *source)
224 {
225   gtk_css_animated_values_start_transitions (values, timestamp, source);
226 }
227
228 GtkCssComputedValues *
229 _gtk_css_animated_values_new (GtkCssComputedValues *computed,
230                               GtkCssComputedValues *source,
231                               gint64                timestamp)
232 {
233   GtkCssAnimatedValues *values;
234   GtkCssValue *value;
235   GtkBitmask *ignore;
236   guint i;
237
238   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (computed), NULL);
239   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (source), NULL);
240
241   values = g_object_new (GTK_TYPE_CSS_ANIMATED_VALUES, NULL);
242
243   values->computed = g_object_ref (computed);
244   for (i = 0; ; i++)
245     {
246       value = _gtk_css_computed_values_get_value (computed, i);
247
248       if (value == NULL)
249         break;
250
251       _gtk_css_computed_values_set_value (GTK_CSS_COMPUTED_VALUES (values), 
252                                           i,
253                                           value,
254                                           _gtk_css_computed_values_get_section (computed, i));
255     }
256
257   gtk_css_animated_values_start_animations (values, timestamp, source);
258
259   ignore = _gtk_css_animated_values_advance (values, timestamp);
260   _gtk_bitmask_free (ignore);
261
262   return GTK_CSS_COMPUTED_VALUES (values);
263 }
264
265 GtkBitmask *
266 _gtk_css_animated_values_advance (GtkCssAnimatedValues *values,
267                                   gint64                timestamp)
268 {
269   GtkBitmask *changed;
270   GSList *list;
271
272   g_return_val_if_fail (GTK_IS_CSS_ANIMATED_VALUES (values), NULL);
273   g_return_val_if_fail (timestamp >= values->current_time, NULL);
274
275   changed = _gtk_bitmask_new ();
276
277   values->current_time = timestamp;
278
279   list = values->animations;
280   while (list)
281     {
282       GtkStyleAnimation *animation = list->data;
283       
284       list = list->next;
285
286       changed = _gtk_style_animation_set_values (animation,
287                                                  changed,
288                                                  timestamp,
289                                                  GTK_CSS_COMPUTED_VALUES (values));
290       
291       if (_gtk_style_animation_is_finished (animation, timestamp))
292         {
293           values->animations = g_slist_remove (values->animations, animation);
294           g_object_unref (animation);
295         }
296     }
297   
298   return changed;
299 }
300
301 gboolean
302 _gtk_css_animated_values_is_finished (GtkCssAnimatedValues *values)
303 {
304   g_return_val_if_fail (GTK_IS_CSS_ANIMATED_VALUES (values), TRUE);
305
306   return values->animations == NULL;
307 }