]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-simple-anim.c
Bug 556527 - The current page property is not passed to GtkPrintUnixDialog
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-simple-anim.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - Simple frame-based animations
3  *
4  * Copyright (C) Dom Lachowicz
5  *
6  * Authors: Dom Lachowicz <cinamod@hotmail.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  * Based on code originally by:
24  *          Jonathan Blandford <jrb@redhat.com>
25  *          Havoc Pennington <hp@redhat.com>
26  */
27
28 #include <glib.h>
29
30 #include "gdk-pixbuf.h"
31 #include "gdk-pixbuf-io.h"
32 #include "gdk-pixbuf-simple-anim.h"
33 #include "gdk-pixbuf-alias.h"
34
35 struct _GdkPixbufSimpleAnimClass
36 {
37         GdkPixbufAnimationClass parent_class;
38 };
39
40 /* Private part of the GdkPixbufSimpleAnim structure */
41 struct _GdkPixbufSimpleAnim
42 {
43         GdkPixbufAnimation parent_instance;
44         
45         gint n_frames;
46         
47         gfloat rate;
48         gint total_time;
49         
50         GList *frames;
51         
52         gint width;
53         gint height;
54         
55         gboolean loop;
56 };
57
58
59 typedef struct _GdkPixbufSimpleAnimIter GdkPixbufSimpleAnimIter;
60 typedef struct _GdkPixbufSimpleAnimIterClass GdkPixbufSimpleAnimIterClass;
61
62 #define GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER              (gdk_pixbuf_simple_anim_iter_get_type ())
63 #define GDK_PIXBUF_SIMPLE_ANIM_ITER(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, GdkPixbufSimpleAnimIter))
64 #define GDK_IS_PIXBUF_SIMPLE_ANIM_ITER(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER))
65
66 #define GDK_PIXBUF_SIMPLE_ANIM_ITER_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, GdkPixbufSimpleAnimIterClass))
67 #define GDK_IS_PIXBUF_SIMPLE_ANIM_ITER_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER))
68 #define GDK_PIXBUF_SIMPLE_ANIM_ITER_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, GdkPixbufSimpleAnimIterClass))
69
70 GType gdk_pixbuf_simple_anim_iter_get_type (void) G_GNUC_CONST;
71
72
73 struct _GdkPixbufSimpleAnimIterClass
74 {
75         GdkPixbufAnimationIterClass parent_class;
76 };
77
78 struct _GdkPixbufSimpleAnimIter
79 {
80         GdkPixbufAnimationIter parent_instance;
81         
82         GdkPixbufSimpleAnim *simple_anim;
83         
84         GTimeVal start_time;
85         GTimeVal current_time;
86         
87         gint position;
88         
89         GList *current_frame;
90 };
91
92 typedef struct _GdkPixbufFrame GdkPixbufFrame;
93 struct _GdkPixbufFrame
94 {
95         GdkPixbuf *pixbuf;
96         gint delay_time;
97         gint elapsed;
98 };
99
100 static void gdk_pixbuf_simple_anim_finalize (GObject *object);
101
102 static gboolean   is_static_image  (GdkPixbufAnimation *animation);
103 static GdkPixbuf *get_static_image (GdkPixbufAnimation *animation);
104
105 static void       get_size         (GdkPixbufAnimation *anim,
106                                     gint               *width, 
107                                     gint               *height);
108 static GdkPixbufAnimationIter *get_iter (GdkPixbufAnimation *anim,
109                                          const GTimeVal     *start_time);
110
111
112 G_DEFINE_TYPE (GdkPixbufSimpleAnim, gdk_pixbuf_simple_anim, GDK_TYPE_PIXBUF_ANIMATION)
113
114 static void
115 gdk_pixbuf_simple_anim_init (GdkPixbufSimpleAnim *anim)
116 {
117 }
118
119 static void
120 gdk_pixbuf_simple_anim_class_init (GdkPixbufSimpleAnimClass *klass)
121 {
122         GObjectClass *object_class;
123         GdkPixbufAnimationClass *anim_class;
124
125         object_class = G_OBJECT_CLASS (klass);
126         anim_class = GDK_PIXBUF_ANIMATION_CLASS (klass);
127         
128         object_class->finalize = gdk_pixbuf_simple_anim_finalize;
129         
130         anim_class->is_static_image = is_static_image;
131         anim_class->get_static_image = get_static_image;
132         anim_class->get_size = get_size;
133         anim_class->get_iter = get_iter;
134 }
135
136 static void
137 gdk_pixbuf_simple_anim_finalize (GObject *object)
138 {
139         GdkPixbufSimpleAnim *anim;
140         GList *l;
141         GdkPixbufFrame *frame;
142         
143         anim = GDK_PIXBUF_SIMPLE_ANIM (object);        
144         
145         for (l = anim->frames; l; l = l->next) {
146                 frame = l->data;
147                 g_object_unref (frame->pixbuf);
148                 g_free (frame);
149         }
150         
151         g_list_free (anim->frames);
152         
153         G_OBJECT_CLASS (gdk_pixbuf_simple_anim_parent_class)->finalize (object);
154 }
155
156 static gboolean
157 is_static_image (GdkPixbufAnimation *animation)
158 {
159         GdkPixbufSimpleAnim *anim;
160         
161         anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
162
163         return (anim->frames != NULL && anim->frames->next == NULL);
164 }
165
166 static GdkPixbuf *
167 get_static_image (GdkPixbufAnimation *animation)
168 {
169         GdkPixbufSimpleAnim *anim;
170         
171         anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
172         
173         if (anim->frames == NULL)
174                 return NULL;
175         else
176                 return ((GdkPixbufFrame *)anim->frames->data)->pixbuf;
177 }
178
179 static void
180 get_size (GdkPixbufAnimation *animation,
181           gint               *width, 
182           gint               *height)
183 {
184         GdkPixbufSimpleAnim *anim;
185
186         anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
187         
188         if (width)
189                 *width = anim->width;
190         
191         if (height)
192                 *height = anim->height;
193 }
194
195 static void
196 iter_clear (GdkPixbufSimpleAnimIter *iter)
197 {
198         iter->current_frame = NULL;
199 }
200
201 static void
202 iter_restart (GdkPixbufSimpleAnimIter *iter)
203 {
204         iter_clear (iter);
205         
206         iter->current_frame = iter->simple_anim->frames;
207 }
208
209 static GdkPixbufAnimationIter *
210 get_iter (GdkPixbufAnimation *anim,
211           const GTimeVal    *start_time)
212 {
213         GdkPixbufSimpleAnimIter *iter;
214         
215         iter = g_object_new (GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, NULL);
216
217         iter->simple_anim = GDK_PIXBUF_SIMPLE_ANIM (anim);
218
219         g_object_ref (iter->simple_anim);
220         
221         iter_restart (iter);
222         
223         iter->start_time = *start_time;
224         iter->current_time = *start_time;
225         
226         return GDK_PIXBUF_ANIMATION_ITER (iter);
227 }
228
229 static void gdk_pixbuf_simple_anim_iter_finalize (GObject *object);
230
231 static gint       get_delay_time             (GdkPixbufAnimationIter *iter);
232 static GdkPixbuf *get_pixbuf                 (GdkPixbufAnimationIter *iter);
233 static gboolean   on_currently_loading_frame (GdkPixbufAnimationIter *iter);
234 static gboolean   advance                    (GdkPixbufAnimationIter *iter,
235                                               const GTimeVal         *current_time);
236
237 G_DEFINE_TYPE (GdkPixbufSimpleAnimIter, gdk_pixbuf_simple_anim_iter, GDK_TYPE_PIXBUF_ANIMATION_ITER)
238
239 static void
240 gdk_pixbuf_simple_anim_iter_init (GdkPixbufSimpleAnimIter *iter)
241 {
242 }
243
244 static void
245 gdk_pixbuf_simple_anim_iter_class_init (GdkPixbufSimpleAnimIterClass *klass)
246 {
247         GObjectClass *object_class;
248         GdkPixbufAnimationIterClass *anim_iter_class;
249
250         object_class = G_OBJECT_CLASS (klass);
251         anim_iter_class = GDK_PIXBUF_ANIMATION_ITER_CLASS (klass);
252         
253         object_class->finalize = gdk_pixbuf_simple_anim_iter_finalize;
254         
255         anim_iter_class->get_delay_time = get_delay_time;
256         anim_iter_class->get_pixbuf = get_pixbuf;
257         anim_iter_class->on_currently_loading_frame = on_currently_loading_frame;
258         anim_iter_class->advance = advance;
259 }
260
261 static void
262 gdk_pixbuf_simple_anim_iter_finalize (GObject *object)
263 {
264         GdkPixbufSimpleAnimIter *iter;
265         
266         iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (object);
267         iter_clear (iter);
268         
269         g_object_unref (iter->simple_anim);
270         
271         G_OBJECT_CLASS (gdk_pixbuf_simple_anim_iter_parent_class)->finalize (object);
272 }
273
274 static gboolean
275 advance (GdkPixbufAnimationIter *anim_iter,
276          const GTimeVal         *current_time)
277 {
278         GdkPixbufSimpleAnimIter *iter;
279         gint elapsed;
280         gint loop;
281         GList *tmp;
282         GList *old;
283         
284         iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
285         
286         iter->current_time = *current_time;
287         
288         /* We use milliseconds for all times */
289         elapsed = (((iter->current_time.tv_sec - iter->start_time.tv_sec) * G_USEC_PER_SEC +
290                     iter->current_time.tv_usec - iter->start_time.tv_usec)) / 1000;
291         
292         if (elapsed < 0) {
293                 /* Try to compensate; probably the system clock
294                  * was set backwards
295                  */
296                 iter->start_time = iter->current_time;
297                 elapsed = 0;
298         }
299         
300         g_assert (iter->simple_anim->total_time > 0);
301         
302         /* See how many times we've already played the full animation,
303          * and subtract time for that.
304          */
305         loop = elapsed / iter->simple_anim->total_time;
306         elapsed = elapsed % iter->simple_anim->total_time;
307         
308         iter->position = elapsed;
309         
310         /* Now move to the proper frame */
311         if (loop < 1)
312                 tmp = iter->simple_anim->frames;
313         else
314                 tmp = NULL;
315         
316         while (tmp != NULL) {
317                 GdkPixbufFrame *frame = tmp->data;
318                 
319                 if (iter->position >= frame->elapsed &&
320                     iter->position < (frame->elapsed + frame->delay_time))
321                         break;
322                 
323                 tmp = tmp->next;
324         }
325         
326         old = iter->current_frame;
327         
328         iter->current_frame = tmp;
329         
330         return iter->current_frame != old;
331 }
332
333 static gint
334 get_delay_time (GdkPixbufAnimationIter *anim_iter)
335 {
336         GdkPixbufFrame *frame;
337         GdkPixbufSimpleAnimIter *iter;
338
339         iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
340         
341         if (iter->current_frame) {
342                 frame = iter->current_frame->data;
343                 return frame->delay_time - (iter->position - frame->elapsed);
344         }
345         else {
346                 return -1;              /* show last frame forever */
347         }
348 }
349
350 static GdkPixbuf *
351 get_pixbuf (GdkPixbufAnimationIter *anim_iter)
352 {
353         GdkPixbufSimpleAnimIter *iter;
354         GdkPixbufFrame *frame;
355         
356         iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
357         
358         if (iter->current_frame)
359                 frame = iter->current_frame->data;
360         else if (g_list_length (iter->simple_anim->frames) > 0)
361                 frame = g_list_last (iter->simple_anim->frames)->data;
362         else
363                 frame = NULL;
364
365         if (frame == NULL)
366                 return NULL;
367         
368         return frame->pixbuf;
369 }
370
371 static gboolean
372 on_currently_loading_frame (GdkPixbufAnimationIter *anim_iter)
373 {
374   GdkPixbufSimpleAnimIter *iter;
375
376   iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
377
378   return iter->current_frame == NULL || iter->current_frame->next == NULL;
379 }
380
381 /**
382  * gdk_pixbuf_simple_anim_new:
383  * @width: the width of the animation
384  * @height: the height of the animation
385  * @rate: the speed of the animation, in frames per second
386  *
387  * Creates a new, empty animation.
388  *
389  * Returns: a newly allocated #GdkPixbufSimpleAnim 
390  *
391  * Since: 2.8
392  */
393 GdkPixbufSimpleAnim *
394 gdk_pixbuf_simple_anim_new (gint   width, 
395                             gint   height, 
396                             gfloat rate)
397 {
398   GdkPixbufSimpleAnim *anim;
399
400   anim = g_object_new (GDK_TYPE_PIXBUF_SIMPLE_ANIM, NULL);
401   anim->width = width;
402   anim->height = height;
403   anim->rate = rate;
404
405   return anim;
406 }
407
408 /**
409  * gdk_pixbuf_simple_anim_add_frame:
410  * @animation: a #GdkPixbufSimpleAnim
411  * @pixbuf: the pixbuf to add 
412  *
413  * Adds a new frame to @animation. The @pixbuf must
414  * have the dimensions specified when the animation 
415  * was constructed.
416  *
417  * Since: 2.8
418  */
419 void
420 gdk_pixbuf_simple_anim_add_frame (GdkPixbufSimpleAnim *animation,
421                                   GdkPixbuf           *pixbuf)
422 {
423   GdkPixbufFrame *frame;
424   int nframe = 0;
425   
426   g_return_if_fail (animation != NULL);
427   g_return_if_fail (pixbuf != NULL);
428   
429   nframe = g_list_length (animation->frames);
430   
431   frame = g_new0 (GdkPixbufFrame, 1);
432   frame->delay_time = (gint) (1000 / animation->rate);
433   frame->elapsed = (gint) (frame->delay_time * nframe);
434   animation->total_time += frame->delay_time;
435   frame->pixbuf = GDK_PIXBUF (g_object_ref (pixbuf));
436
437   animation->frames = g_list_append (animation->frames, frame);
438 }
439
440
441 #define __GDK_PIXBUF_SIMPLE_ANIM_C__
442 #include "gdk-pixbuf-aliasdef.c"