2 * Copyright © 2012 Red Hat Inc.
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.
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.
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/>.
17 * Authors: Benjamin Otte <otte@gnome.org>
22 #include "gtkcssanimatedvaluesprivate.h"
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"
33 G_DEFINE_TYPE (GtkCssAnimatedValues, _gtk_css_animated_values, GTK_TYPE_CSS_COMPUTED_VALUES)
36 gtk_css_animated_values_dispose (GObject *object)
38 GtkCssAnimatedValues *values = GTK_CSS_ANIMATED_VALUES (object);
40 g_clear_object (&values->computed);
41 g_slist_free_full (values->animations, g_object_unref);
42 values->animations = NULL;
44 G_OBJECT_CLASS (_gtk_css_animated_values_parent_class)->dispose (object);
48 _gtk_css_animated_values_class_init (GtkCssAnimatedValuesClass *klass)
50 GObjectClass *object_class = G_OBJECT_CLASS (klass);
52 object_class->dispose = gtk_css_animated_values_dispose;
56 _gtk_css_animated_values_init (GtkCssAnimatedValues *animated_values)
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 */
69 transition_info_add (TransitionInfo infos[GTK_CSS_PROPERTY_N_PROPERTIES],
70 GtkStyleProperty *property,
77 for (i = 0; i < _gtk_css_style_property_get_n_properties (); i++)
79 GtkCssStyleProperty *prop = _gtk_css_style_property_lookup_by_id (i);
81 transition_info_add (infos, GTK_STYLE_PROPERTY (prop), index);
84 else if (GTK_IS_CSS_SHORTHAND_PROPERTY (property))
86 GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
89 for (i = 0; i < _gtk_css_shorthand_property_get_n_subproperties (shorthand); i++)
91 GtkCssStyleProperty *prop = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
93 transition_info_add (infos, GTK_STYLE_PROPERTY (prop), index);
96 else if (GTK_IS_CSS_STYLE_PROPERTY (property))
100 if (!_gtk_css_style_property_is_animated (GTK_CSS_STYLE_PROPERTY (property)))
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;
110 g_assert_not_reached ();
115 transition_infos_set (TransitionInfo infos[GTK_CSS_PROPERTY_N_PROPERTIES],
116 GtkCssValue *transitions)
120 for (i = 0; i < _gtk_css_array_value_get_n_values (transitions); i++)
122 GtkStyleProperty *property;
123 GtkCssValue *prop_value;
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)
130 property = _gtk_style_property_lookup (_gtk_css_ident_value_get (prop_value));
131 if (property == NULL)
135 transition_info_add (infos, property, i);
139 static GtkStyleAnimation *
140 gtk_css_animated_values_find_transition (GtkCssAnimatedValues *values,
145 for (list = values->animations; list; list = list->next)
147 if (!GTK_IS_CSS_TRANSITION (list->data))
150 if (_gtk_css_transition_get_property (list->data) == property_id)
158 gtk_css_animated_values_start_transitions (GtkCssAnimatedValues *values,
160 GtkCssComputedValues *source)
162 TransitionInfo transitions[GTK_CSS_PROPERTY_N_PROPERTIES] = { { 0, } };
163 GtkCssComputedValues *source_computed, *computed;
164 GtkCssValue *durations, *delays, *timing_functions;
167 computed = GTK_CSS_COMPUTED_VALUES (values);
168 if (GTK_IS_CSS_ANIMATED_VALUES (source))
169 source_computed = GTK_CSS_ANIMATED_VALUES (source)->computed;
171 source_computed = source;
173 transition_infos_set (transitions, _gtk_css_computed_values_get_value (computed, GTK_CSS_PROPERTY_TRANSITION_PROPERTY));
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);
180 for (i = 0; i < GTK_CSS_PROPERTY_N_PROPERTIES; i++)
182 GtkStyleAnimation *animation;
183 GtkCssValue *start, *end;
184 double duration, delay;
186 if (!transitions[i].pending)
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)
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))
198 if (GTK_IS_CSS_ANIMATED_VALUES (source))
200 animation = gtk_css_animated_values_find_transition (GTK_CSS_ANIMATED_VALUES (source), i);
202 values->animations = g_slist_prepend (values->animations, g_object_ref (animation));
207 animation = _gtk_css_transition_new (i,
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);
221 gtk_css_animated_values_start_animations (GtkCssAnimatedValues *values,
223 GtkCssComputedValues *source)
225 gtk_css_animated_values_start_transitions (values, timestamp, source);
228 GtkCssComputedValues *
229 _gtk_css_animated_values_new (GtkCssComputedValues *computed,
230 GtkCssComputedValues *source,
233 GtkCssAnimatedValues *values;
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);
241 values = g_object_new (GTK_TYPE_CSS_ANIMATED_VALUES, NULL);
243 values->computed = g_object_ref (computed);
246 value = _gtk_css_computed_values_get_value (computed, i);
251 _gtk_css_computed_values_set_value (GTK_CSS_COMPUTED_VALUES (values),
254 _gtk_css_computed_values_get_section (computed, i));
257 gtk_css_animated_values_start_animations (values, timestamp, source);
259 ignore = _gtk_css_animated_values_advance (values, timestamp);
260 _gtk_bitmask_free (ignore);
262 return GTK_CSS_COMPUTED_VALUES (values);
266 _gtk_css_animated_values_advance (GtkCssAnimatedValues *values,
272 g_return_val_if_fail (GTK_IS_CSS_ANIMATED_VALUES (values), NULL);
273 g_return_val_if_fail (timestamp >= values->current_time, NULL);
275 changed = _gtk_bitmask_new ();
277 values->current_time = timestamp;
279 list = values->animations;
282 GtkStyleAnimation *animation = list->data;
286 changed = _gtk_style_animation_set_values (animation,
289 GTK_CSS_COMPUTED_VALUES (values));
291 if (_gtk_style_animation_is_finished (animation, timestamp))
293 values->animations = g_slist_remove (values->animations, animation);
294 g_object_unref (animation);
302 _gtk_css_animated_values_is_finished (GtkCssAnimatedValues *values)
304 g_return_val_if_fail (GTK_IS_CSS_ANIMATED_VALUES (values), TRUE);
306 return values->animations == NULL;