]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsstransition.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkcsstransition.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 "gtkcsstransitionprivate.h"
23
24 #include "gtkcsseasevalueprivate.h"
25
26 G_DEFINE_TYPE (GtkCssTransition, _gtk_css_transition, GTK_TYPE_STYLE_ANIMATION)
27
28 static void
29 gtk_css_transition_set_values (GtkStyleAnimation    *animation,
30                                gint64                for_time_us,
31                                GtkCssComputedValues *values)
32 {
33   GtkCssTransition *transition = GTK_CSS_TRANSITION (animation);
34   GtkCssValue *value, *end;
35   double progress;
36
37   end = _gtk_css_computed_values_get_intrinsic_value (values, transition->property);
38
39   if (transition->start_time >= for_time_us)
40     value = _gtk_css_value_ref (transition->start);
41   else if (transition->end_time > for_time_us)
42     {
43       progress = (double) (for_time_us - transition->start_time) / (transition->end_time - transition->start_time);
44       progress = _gtk_css_ease_value_transform (transition->ease, progress);
45
46       value = _gtk_css_value_transition (transition->start,
47                                          end,
48                                          transition->property,
49                                          progress);
50       if (value == NULL)
51         value = _gtk_css_value_ref (end);
52     }
53   else
54     value = NULL;
55
56   if (value)
57     {
58       _gtk_css_computed_values_set_animated_value (values, transition->property, value);
59       _gtk_css_value_unref (value);
60     }
61 }
62
63 static gboolean
64 gtk_css_transition_is_finished (GtkStyleAnimation *animation,
65                                 gint64             at_time_us)
66 {
67   GtkCssTransition *transition = GTK_CSS_TRANSITION (animation);
68
69   return at_time_us >= transition->end_time;
70 }
71
72 static gboolean
73 gtk_css_transition_is_static (GtkStyleAnimation *animation,
74                               gint64             at_time_us)
75 {
76   GtkCssTransition *transition = GTK_CSS_TRANSITION (animation);
77
78   return at_time_us >= transition->end_time;
79 }
80
81 static void
82 gtk_css_transition_finalize (GObject *object)
83 {
84   GtkCssTransition *transition = GTK_CSS_TRANSITION (object);
85
86   _gtk_css_value_unref (transition->start);
87   _gtk_css_value_unref (transition->ease);
88
89   G_OBJECT_CLASS (_gtk_css_transition_parent_class)->finalize (object);
90 }
91
92 static void
93 _gtk_css_transition_class_init (GtkCssTransitionClass *klass)
94 {
95   GObjectClass *object_class = G_OBJECT_CLASS (klass);
96   GtkStyleAnimationClass *animation_class = GTK_STYLE_ANIMATION_CLASS (klass);
97
98   object_class->finalize = gtk_css_transition_finalize;
99
100   animation_class->set_values = gtk_css_transition_set_values;
101   animation_class->is_finished = gtk_css_transition_is_finished;
102   animation_class->is_static = gtk_css_transition_is_static;
103 }
104
105 static void
106 _gtk_css_transition_init (GtkCssTransition *transition)
107 {
108 }
109
110 GtkStyleAnimation *
111 _gtk_css_transition_new (guint        property,
112                          GtkCssValue *start,
113                          GtkCssValue *ease,
114                          gint64       start_time_us,
115                          gint64       end_time_us)
116 {
117   GtkCssTransition *transition;
118
119   g_return_val_if_fail (start != NULL, NULL);
120   g_return_val_if_fail (ease != NULL, NULL);
121   g_return_val_if_fail (start_time_us <= end_time_us, NULL);
122
123   transition = g_object_new (GTK_TYPE_CSS_TRANSITION, NULL);
124
125   transition->property = property;
126   transition->start = _gtk_css_value_ref (start);
127   transition->ease = _gtk_css_value_ref (ease);
128   transition->start_time = start_time_us;
129   transition->end_time = end_time_us;
130
131   return GTK_STYLE_ANIMATION (transition);
132 }
133
134 guint
135 _gtk_css_transition_get_property (GtkCssTransition *transition)
136 {
137   g_return_val_if_fail (GTK_IS_CSS_TRANSITION (transition), 0);
138
139   return transition->property;
140 }