]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-gdip-animation.c
[quartz] Delete the typedef of GdkDevicePrivate
[~andy/gtk] / gdk-pixbuf / io-gdip-animation.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* GdkPixbuf library - animated gdip support
3  *
4  * Copyright (C) 1999 The Free Software Foundation
5  *
6  * Authors: Jonathan Blandford <jrb@redhat.com>
7  *          Havoc Pennington <hp@redhat.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <errno.h>
26 #include "io-gdip-native.h"
27 #include "io-gdip-animation.h"
28
29 static void gdk_pixbuf_gdip_anim_class_init (GdkPixbufGdipAnimClass *klass);
30 static void gdk_pixbuf_gdip_anim_finalize   (GObject        *object);
31
32 static gboolean                gdk_pixbuf_gdip_anim_is_static_image  (GdkPixbufAnimation *animation);
33 static GdkPixbuf*              gdk_pixbuf_gdip_anim_get_static_image (GdkPixbufAnimation *animation);
34
35 static void                    gdk_pixbuf_gdip_anim_get_size (GdkPixbufAnimation *anim,
36                                                              int                *width,
37                                                              int                *height);
38 static GdkPixbufAnimationIter* gdk_pixbuf_gdip_anim_get_iter (GdkPixbufAnimation *anim,
39                                                              const GTimeVal     *start_time);
40
41 static gpointer parent_class;
42
43 GType
44 gdk_pixbuf_gdip_anim_get_type (void)
45 {
46         static GType object_type = 0;
47
48         if (!object_type) {
49                 const GTypeInfo object_info = {
50                         sizeof (GdkPixbufGdipAnimClass),
51                         (GBaseInitFunc) NULL,
52                         (GBaseFinalizeFunc) NULL,
53                         (GClassInitFunc) gdk_pixbuf_gdip_anim_class_init,
54                         NULL,           /* class_finalize */
55                         NULL,           /* class_data */
56                         sizeof (GdkPixbufGdipAnim),
57                         0,              /* n_preallocs */
58                         (GInstanceInitFunc) NULL,
59                 };
60
61                 object_type = g_type_from_name ("GdkPixbufGdipAnim");
62                 if (object_type == 0) {
63                   object_type = g_type_register_static (GDK_TYPE_PIXBUF_ANIMATION,
64                                                         g_intern_static_string ("GdkPixbufGdipAnim"),
65                                                         &object_info, 0);
66                 }
67         }
68         
69         return object_type;
70 }
71
72 static void
73 gdk_pixbuf_gdip_anim_class_init (GdkPixbufGdipAnimClass *klass)
74 {
75         GObjectClass *object_class = G_OBJECT_CLASS (klass);
76         GdkPixbufAnimationClass *anim_class = GDK_PIXBUF_ANIMATION_CLASS (klass);
77         
78         parent_class = g_type_class_peek_parent (klass);
79         
80         object_class->finalize = gdk_pixbuf_gdip_anim_finalize;
81
82         anim_class->is_static_image = gdk_pixbuf_gdip_anim_is_static_image;
83         anim_class->get_static_image = gdk_pixbuf_gdip_anim_get_static_image;
84         anim_class->get_size = gdk_pixbuf_gdip_anim_get_size;
85         anim_class->get_iter = gdk_pixbuf_gdip_anim_get_iter;
86 }
87
88 static void
89 gdk_pixbuf_gdip_anim_finalize (GObject *object)
90 {
91         GdkPixbufGdipAnim *gdip_anim = GDK_PIXBUF_GDIP_ANIM (object);
92
93         GList *l;
94         GdkPixbufFrame *frame;
95         
96         for (l = gdip_anim->frames; l; l = l->next) {
97                 frame = l->data;
98                 g_object_unref (frame->pixbuf);
99                 g_free (frame);
100         }
101         
102         g_list_free (gdip_anim->frames);
103         
104         G_OBJECT_CLASS (parent_class)->finalize (object);
105 }
106
107 static gboolean
108 gdk_pixbuf_gdip_anim_is_static_image  (GdkPixbufAnimation *animation)
109 {
110         GdkPixbufGdipAnim *gdip_anim;
111
112         gdip_anim = GDK_PIXBUF_GDIP_ANIM (animation);
113
114         return (gdip_anim->frames != NULL &&
115                 gdip_anim->frames->next == NULL);
116 }
117
118 static GdkPixbuf*
119 gdk_pixbuf_gdip_anim_get_static_image (GdkPixbufAnimation *animation)
120 {
121         GdkPixbufGdipAnim *gdip_anim;
122
123         gdip_anim = GDK_PIXBUF_GDIP_ANIM (animation);
124
125         if (gdip_anim->frames == NULL)
126                 return NULL;
127         else
128                 return GDK_PIXBUF (((GdkPixbufFrame*)gdip_anim->frames->data)->pixbuf);        
129 }
130
131 static void
132 gdk_pixbuf_gdip_anim_get_size (GdkPixbufAnimation *anim,
133                               int                *width,
134                               int                *height)
135 {
136         GdkPixbufGdipAnim *gdip_anim;
137
138         gdip_anim = GDK_PIXBUF_GDIP_ANIM (anim);
139
140         if (width)
141                 *width = gdip_anim->width;
142
143         if (height)
144                 *height = gdip_anim->height;
145 }
146
147
148 static void
149 iter_clear (GdkPixbufGdipAnimIter *iter)
150 {
151         iter->current_frame = NULL;
152 }
153
154 static void
155 iter_restart (GdkPixbufGdipAnimIter *iter)
156 {
157         iter_clear (iter);
158   
159         iter->current_frame = iter->gdip_anim->frames;
160 }
161
162 static GdkPixbufAnimationIter*
163 gdk_pixbuf_gdip_anim_get_iter (GdkPixbufAnimation *anim,
164                               const GTimeVal     *start_time)
165 {
166         GdkPixbufGdipAnimIter *iter;
167
168         iter = g_object_new (GDK_TYPE_PIXBUF_GDIP_ANIM_ITER, NULL);
169
170         iter->gdip_anim = GDK_PIXBUF_GDIP_ANIM (anim);
171
172         g_object_ref (iter->gdip_anim);
173         
174         iter_restart (iter);
175
176         iter->start_time = *start_time;
177         iter->current_time = *start_time;
178         iter->first_loop_slowness = 0;
179         
180         return GDK_PIXBUF_ANIMATION_ITER (iter);
181 }
182
183 static void gdk_pixbuf_gdip_anim_iter_class_init (GdkPixbufGdipAnimIterClass *klass);
184 static void gdk_pixbuf_gdip_anim_iter_finalize   (GObject                   *object);
185
186 static int        gdk_pixbuf_gdip_anim_iter_get_delay_time             (GdkPixbufAnimationIter *iter);
187 static GdkPixbuf* gdk_pixbuf_gdip_anim_iter_get_pixbuf                 (GdkPixbufAnimationIter *iter);
188 static gboolean   gdk_pixbuf_gdip_anim_iter_on_currently_loading_frame (GdkPixbufAnimationIter *iter);
189 static gboolean   gdk_pixbuf_gdip_anim_iter_advance                    (GdkPixbufAnimationIter *iter,
190                                                                        const GTimeVal         *current_time);
191
192 static gpointer iter_parent_class;
193
194 GType
195 gdk_pixbuf_gdip_anim_iter_get_type (void)
196 {
197         static GType object_type = 0;
198
199         if (!object_type) {
200                 const GTypeInfo object_info = {
201                         sizeof (GdkPixbufGdipAnimIterClass),
202                         (GBaseInitFunc) NULL,
203                         (GBaseFinalizeFunc) NULL,
204                         (GClassInitFunc) gdk_pixbuf_gdip_anim_iter_class_init,
205                         NULL,           /* class_finalize */
206                         NULL,           /* class_data */
207                         sizeof (GdkPixbufGdipAnimIter),
208                         0,              /* n_preallocs */
209                         (GInstanceInitFunc) NULL,
210                 };
211                 
212                 object_type = g_type_from_name ("GdkPixbufGdipAnimIter");
213                 if (object_type == 0) {
214                   object_type = g_type_register_static (GDK_TYPE_PIXBUF_ANIMATION_ITER,
215                                                         g_intern_static_string ("GdkPixbufGdipAnimIter"),
216                                                         &object_info, 0);
217                 }
218         }
219         
220         return object_type;
221 }
222
223 static void
224 gdk_pixbuf_gdip_anim_iter_class_init (GdkPixbufGdipAnimIterClass *klass)
225 {
226         GObjectClass *object_class = G_OBJECT_CLASS (klass);
227         GdkPixbufAnimationIterClass *anim_iter_class =
228                 GDK_PIXBUF_ANIMATION_ITER_CLASS (klass);
229         
230         iter_parent_class = g_type_class_peek_parent (klass);
231         
232         object_class->finalize = gdk_pixbuf_gdip_anim_iter_finalize;
233
234         anim_iter_class->get_delay_time = gdk_pixbuf_gdip_anim_iter_get_delay_time;
235         anim_iter_class->get_pixbuf = gdk_pixbuf_gdip_anim_iter_get_pixbuf;
236         anim_iter_class->on_currently_loading_frame = gdk_pixbuf_gdip_anim_iter_on_currently_loading_frame;
237         anim_iter_class->advance = gdk_pixbuf_gdip_anim_iter_advance;
238 }
239
240 static void
241 gdk_pixbuf_gdip_anim_iter_finalize (GObject *object)
242 {
243         GdkPixbufGdipAnimIter *iter = GDK_PIXBUF_GDIP_ANIM_ITER (object);
244
245         iter_clear (iter);
246
247         g_object_unref (iter->gdip_anim);
248         
249         G_OBJECT_CLASS (iter_parent_class)->finalize (object);
250 }
251
252 static gboolean
253 gdk_pixbuf_gdip_anim_iter_advance (GdkPixbufAnimationIter *anim_iter,
254                                   const GTimeVal         *current_time)
255 {
256         GdkPixbufGdipAnimIter *iter;
257         gint elapsed;
258         gint loop;
259         GList *tmp;
260         GList *old;
261         
262         iter = GDK_PIXBUF_GDIP_ANIM_ITER (anim_iter);
263         
264         iter->current_time = *current_time;
265
266         /* We use milliseconds for all times */
267         elapsed =
268           (((iter->current_time.tv_sec - iter->start_time.tv_sec) * G_USEC_PER_SEC +
269             iter->current_time.tv_usec - iter->start_time.tv_usec)) / 1000;
270
271         if (elapsed < 0) {
272                 /* Try to compensate; probably the system clock
273                  * was set backwards
274                  */
275                 iter->start_time = iter->current_time;
276                 elapsed = 0;
277         }
278
279         g_assert (iter->gdip_anim->total_time > 0);
280         
281         /* See how many times we've already played the full animation,
282          * and subtract time for that.
283          */
284
285         if (iter->gdip_anim->loading)
286                 loop = 0;
287         else {
288                 /* If current_frame is NULL at this point, we have loaded the
289                  * animation from a source which fell behind the speed of the 
290                  * display. We remember how much slower the first loop was due
291                  * to this and correct the position calculation in order to not
292                  * jump in the middle of the second loop.
293                  */
294                 if (iter->current_frame == NULL)
295                         iter->first_loop_slowness = MAX(0, elapsed - iter->gdip_anim->total_time);
296
297                 loop = (elapsed - iter->first_loop_slowness) / iter->gdip_anim->total_time;
298                 elapsed = (elapsed - iter->first_loop_slowness) % iter->gdip_anim->total_time;
299         }
300
301         iter->position = elapsed;
302
303         /* Now move to the proper frame */
304         if (iter->gdip_anim->loop == 0 || loop < iter->gdip_anim->loop) 
305                 tmp = iter->gdip_anim->frames;
306         else 
307                 tmp = NULL;
308         while (tmp != NULL) {
309                 GdkPixbufFrame *frame = tmp->data;
310                 
311                 if (iter->position >= frame->elapsed &&
312                     iter->position < (frame->elapsed + frame->delay_time))
313                         break;
314                 
315                 tmp = tmp->next;
316         }
317
318         old = iter->current_frame;
319         
320         iter->current_frame = tmp;
321
322         return iter->current_frame != old;
323 }
324
325 int
326 gdk_pixbuf_gdip_anim_iter_get_delay_time (GdkPixbufAnimationIter *anim_iter)
327 {
328         GdkPixbufFrame *frame;
329         GdkPixbufGdipAnimIter *iter;
330   
331         iter = GDK_PIXBUF_GDIP_ANIM_ITER (anim_iter);
332
333         if (iter->current_frame) {
334                 frame = iter->current_frame->data;
335
336 #if 0
337                 g_print ("frame start: %d pos: %d frame len: %d frame remaining: %d\n",
338                          frame->elapsed,
339                          iter->position,
340                          frame->delay_time,
341                          frame->delay_time - (iter->position - frame->elapsed));
342 #endif
343                 
344                 return frame->delay_time - (iter->position - frame->elapsed);
345         } else 
346                 return -1; /* show last frame forever */
347 }
348
349 GdkPixbuf*
350 gdk_pixbuf_gdip_anim_iter_get_pixbuf (GdkPixbufAnimationIter *anim_iter)
351 {
352         GdkPixbufGdipAnimIter *iter;
353         GdkPixbufFrame *frame;
354         
355         iter = GDK_PIXBUF_GDIP_ANIM_ITER (anim_iter);
356
357         frame = iter->current_frame ? iter->current_frame->data : g_list_last (iter->gdip_anim->frames)->data;
358
359 #if 0
360         if (FALSE && frame)
361           g_print ("current frame %d dispose mode %d  %d x %d\n",
362                    g_list_index (iter->gdip_anim->frames,
363                                  frame),
364                    frame->action,
365                    gdk_pixbuf_get_width (frame->pixbuf),
366                    gdk_pixbuf_get_height (frame->pixbuf));
367 #endif
368         
369         if (frame == NULL)
370                 return NULL;
371
372         return frame->pixbuf;
373 }
374
375 static gboolean
376 gdk_pixbuf_gdip_anim_iter_on_currently_loading_frame (GdkPixbufAnimationIter *anim_iter)
377 {
378         GdkPixbufGdipAnimIter *iter;
379   
380         iter = GDK_PIXBUF_GDIP_ANIM_ITER (anim_iter);
381
382         return iter->current_frame == NULL || iter->current_frame->next == NULL;  
383 }