1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - Simple frame-based animations
4 * Copyright (C) Dom Lachowicz
6 * Authors: Dom Lachowicz <cinamod@hotmail.com>
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.
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.
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.
23 * Based on code originally by:
24 * Jonathan Blandford <jrb@redhat.com>
25 * Havoc Pennington <hp@redhat.com>
31 #define GDK_PIXBUF_C_COMPILATION
32 #include "gdk-pixbuf.h"
33 #include "gdk-pixbuf-private.h"
34 #include "gdk-pixbuf-io.h"
35 #include "gdk-pixbuf-simple-anim.h"
36 #include "gdk-pixbuf-alias.h"
38 struct _GdkPixbufSimpleAnimClass
40 GdkPixbufAnimationClass parent_class;
43 /* Private part of the GdkPixbufSimpleAnim structure */
44 struct _GdkPixbufSimpleAnim
46 GdkPixbufAnimation parent_instance;
62 typedef struct _GdkPixbufSimpleAnimIter GdkPixbufSimpleAnimIter;
63 typedef struct _GdkPixbufSimpleAnimIterClass GdkPixbufSimpleAnimIterClass;
65 #define GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER (gdk_pixbuf_simple_anim_iter_get_type ())
66 #define GDK_PIXBUF_SIMPLE_ANIM_ITER(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, GdkPixbufSimpleAnimIter))
67 #define GDK_IS_PIXBUF_SIMPLE_ANIM_ITER(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER))
69 #define GDK_PIXBUF_SIMPLE_ANIM_ITER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, GdkPixbufSimpleAnimIterClass))
70 #define GDK_IS_PIXBUF_SIMPLE_ANIM_ITER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER))
71 #define GDK_PIXBUF_SIMPLE_ANIM_ITER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, GdkPixbufSimpleAnimIterClass))
73 GType gdk_pixbuf_simple_anim_iter_get_type (void) G_GNUC_CONST;
76 struct _GdkPixbufSimpleAnimIterClass
78 GdkPixbufAnimationIterClass parent_class;
81 struct _GdkPixbufSimpleAnimIter
83 GdkPixbufAnimationIter parent_instance;
85 GdkPixbufSimpleAnim *simple_anim;
88 GTimeVal current_time;
95 typedef struct _GdkPixbufFrame GdkPixbufFrame;
96 struct _GdkPixbufFrame
103 static void gdk_pixbuf_simple_anim_finalize (GObject *object);
105 static gboolean is_static_image (GdkPixbufAnimation *animation);
106 static GdkPixbuf *get_static_image (GdkPixbufAnimation *animation);
108 static void get_size (GdkPixbufAnimation *anim,
111 static GdkPixbufAnimationIter *get_iter (GdkPixbufAnimation *anim,
112 const GTimeVal *start_time);
115 static void gdk_pixbuf_simple_anim_set_property (GObject *object,
119 static void gdk_pixbuf_simple_anim_get_property (GObject *object,
130 G_DEFINE_TYPE (GdkPixbufSimpleAnim, gdk_pixbuf_simple_anim, GDK_TYPE_PIXBUF_ANIMATION)
133 gdk_pixbuf_simple_anim_init (GdkPixbufSimpleAnim *anim)
138 gdk_pixbuf_simple_anim_class_init (GdkPixbufSimpleAnimClass *klass)
140 GObjectClass *object_class;
141 GdkPixbufAnimationClass *anim_class;
143 object_class = G_OBJECT_CLASS (klass);
144 anim_class = GDK_PIXBUF_ANIMATION_CLASS (klass);
146 object_class->set_property = gdk_pixbuf_simple_anim_set_property;
147 object_class->get_property = gdk_pixbuf_simple_anim_get_property;
148 object_class->finalize = gdk_pixbuf_simple_anim_finalize;
150 anim_class->is_static_image = is_static_image;
151 anim_class->get_static_image = get_static_image;
152 anim_class->get_size = get_size;
153 anim_class->get_iter = get_iter;
156 * GdkPixbufSimpleAnim:loop:
158 * Whether the animation should loop when it reaches the end.
162 g_object_class_install_property (object_class,
164 g_param_spec_boolean ("loop",
166 P_("Whether the animation should loop when it reaches the end"),
172 gdk_pixbuf_simple_anim_finalize (GObject *object)
174 GdkPixbufSimpleAnim *anim;
176 GdkPixbufFrame *frame;
178 anim = GDK_PIXBUF_SIMPLE_ANIM (object);
180 for (l = anim->frames; l; l = l->next) {
182 g_object_unref (frame->pixbuf);
186 g_list_free (anim->frames);
188 G_OBJECT_CLASS (gdk_pixbuf_simple_anim_parent_class)->finalize (object);
192 is_static_image (GdkPixbufAnimation *animation)
194 GdkPixbufSimpleAnim *anim;
196 anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
198 return (anim->frames != NULL && anim->frames->next == NULL);
202 get_static_image (GdkPixbufAnimation *animation)
204 GdkPixbufSimpleAnim *anim;
206 anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
208 if (anim->frames == NULL)
211 return ((GdkPixbufFrame *)anim->frames->data)->pixbuf;
215 get_size (GdkPixbufAnimation *animation,
219 GdkPixbufSimpleAnim *anim;
221 anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
224 *width = anim->width;
227 *height = anim->height;
231 iter_clear (GdkPixbufSimpleAnimIter *iter)
233 iter->current_frame = NULL;
237 iter_restart (GdkPixbufSimpleAnimIter *iter)
241 iter->current_frame = iter->simple_anim->frames;
244 static GdkPixbufAnimationIter *
245 get_iter (GdkPixbufAnimation *anim,
246 const GTimeVal *start_time)
248 GdkPixbufSimpleAnimIter *iter;
250 iter = g_object_new (GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, NULL);
252 iter->simple_anim = GDK_PIXBUF_SIMPLE_ANIM (anim);
254 g_object_ref (iter->simple_anim);
258 iter->start_time = *start_time;
259 iter->current_time = *start_time;
261 return GDK_PIXBUF_ANIMATION_ITER (iter);
264 static void gdk_pixbuf_simple_anim_iter_finalize (GObject *object);
266 static gint get_delay_time (GdkPixbufAnimationIter *iter);
267 static GdkPixbuf *get_pixbuf (GdkPixbufAnimationIter *iter);
268 static gboolean on_currently_loading_frame (GdkPixbufAnimationIter *iter);
269 static gboolean advance (GdkPixbufAnimationIter *iter,
270 const GTimeVal *current_time);
272 G_DEFINE_TYPE (GdkPixbufSimpleAnimIter, gdk_pixbuf_simple_anim_iter, GDK_TYPE_PIXBUF_ANIMATION_ITER)
275 gdk_pixbuf_simple_anim_iter_init (GdkPixbufSimpleAnimIter *iter)
280 gdk_pixbuf_simple_anim_iter_class_init (GdkPixbufSimpleAnimIterClass *klass)
282 GObjectClass *object_class;
283 GdkPixbufAnimationIterClass *anim_iter_class;
285 object_class = G_OBJECT_CLASS (klass);
286 anim_iter_class = GDK_PIXBUF_ANIMATION_ITER_CLASS (klass);
288 object_class->finalize = gdk_pixbuf_simple_anim_iter_finalize;
290 anim_iter_class->get_delay_time = get_delay_time;
291 anim_iter_class->get_pixbuf = get_pixbuf;
292 anim_iter_class->on_currently_loading_frame = on_currently_loading_frame;
293 anim_iter_class->advance = advance;
297 gdk_pixbuf_simple_anim_iter_finalize (GObject *object)
299 GdkPixbufSimpleAnimIter *iter;
301 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (object);
304 g_object_unref (iter->simple_anim);
306 G_OBJECT_CLASS (gdk_pixbuf_simple_anim_iter_parent_class)->finalize (object);
310 advance (GdkPixbufAnimationIter *anim_iter,
311 const GTimeVal *current_time)
313 GdkPixbufSimpleAnimIter *iter;
319 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
321 iter->current_time = *current_time;
323 /* We use milliseconds for all times */
324 elapsed = (((iter->current_time.tv_sec - iter->start_time.tv_sec) * G_USEC_PER_SEC +
325 iter->current_time.tv_usec - iter->start_time.tv_usec)) / 1000;
328 /* Try to compensate; probably the system clock
331 iter->start_time = iter->current_time;
335 g_assert (iter->simple_anim->total_time > 0);
337 /* See how many times we've already played the full animation,
338 * and subtract time for that.
340 loop_count = elapsed / iter->simple_anim->total_time;
341 elapsed = elapsed % iter->simple_anim->total_time;
343 iter->position = elapsed;
345 /* Now move to the proper frame */
346 if (loop_count < 1 || iter->simple_anim->loop)
347 tmp = iter->simple_anim->frames;
351 while (tmp != NULL) {
352 GdkPixbufFrame *frame = tmp->data;
354 if (iter->position >= frame->elapsed &&
355 iter->position < (frame->elapsed + frame->delay_time))
361 old = iter->current_frame;
363 iter->current_frame = tmp;
365 return iter->current_frame != old;
369 get_delay_time (GdkPixbufAnimationIter *anim_iter)
371 GdkPixbufFrame *frame;
372 GdkPixbufSimpleAnimIter *iter;
374 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
376 if (iter->current_frame) {
377 frame = iter->current_frame->data;
378 return frame->delay_time - (iter->position - frame->elapsed);
381 return -1; /* show last frame forever */
386 get_pixbuf (GdkPixbufAnimationIter *anim_iter)
388 GdkPixbufSimpleAnimIter *iter;
389 GdkPixbufFrame *frame;
391 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
393 if (iter->current_frame)
394 frame = iter->current_frame->data;
395 else if (g_list_length (iter->simple_anim->frames) > 0)
396 frame = g_list_last (iter->simple_anim->frames)->data;
403 return frame->pixbuf;
407 on_currently_loading_frame (GdkPixbufAnimationIter *anim_iter)
409 GdkPixbufSimpleAnimIter *iter;
411 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
413 return iter->current_frame == NULL || iter->current_frame->next == NULL;
417 * gdk_pixbuf_simple_anim_new:
418 * @width: the width of the animation
419 * @height: the height of the animation
420 * @rate: the speed of the animation, in frames per second
422 * Creates a new, empty animation.
424 * Returns: a newly allocated #GdkPixbufSimpleAnim
428 GdkPixbufSimpleAnim *
429 gdk_pixbuf_simple_anim_new (gint width,
433 GdkPixbufSimpleAnim *anim;
435 anim = g_object_new (GDK_TYPE_PIXBUF_SIMPLE_ANIM, NULL);
437 anim->height = height;
444 * gdk_pixbuf_simple_anim_add_frame:
445 * @animation: a #GdkPixbufSimpleAnim
446 * @pixbuf: the pixbuf to add
448 * Adds a new frame to @animation. The @pixbuf must
449 * have the dimensions specified when the animation
455 gdk_pixbuf_simple_anim_add_frame (GdkPixbufSimpleAnim *animation,
458 GdkPixbufFrame *frame;
461 g_return_if_fail (GDK_IS_PIXBUF_SIMPLE_ANIM (animation));
462 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
464 nframe = g_list_length (animation->frames);
466 frame = g_new0 (GdkPixbufFrame, 1);
467 frame->delay_time = (gint) (1000 / animation->rate);
468 frame->elapsed = (gint) (frame->delay_time * nframe);
469 animation->total_time += frame->delay_time;
470 frame->pixbuf = g_object_ref (pixbuf);
472 animation->frames = g_list_append (animation->frames, frame);
476 gdk_pixbuf_simple_anim_get_property (GObject *object,
481 GdkPixbufSimpleAnim *animation = GDK_PIXBUF_SIMPLE_ANIM (object);
485 g_value_set_boolean (value,
486 gdk_pixbuf_simple_anim_get_loop (animation));
489 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
495 gdk_pixbuf_simple_anim_set_property (GObject *object,
500 GdkPixbufSimpleAnim *animation = GDK_PIXBUF_SIMPLE_ANIM (object);
504 gdk_pixbuf_simple_anim_set_loop (animation,
505 g_value_get_boolean (value));
508 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
514 * gdk_pixbuf_simple_anim_set_loop:
515 * @animation: a #GdkPixbufSimpleAnim
516 * @loop: whether to loop the animation
518 * Sets whether @animation should loop indefinitely when it reaches the end.
523 gdk_pixbuf_simple_anim_set_loop (GdkPixbufSimpleAnim *animation,
526 g_return_if_fail (GDK_IS_PIXBUF_SIMPLE_ANIM (animation));
528 if (loop != animation->loop) {
529 animation->loop = loop;
530 g_object_notify (G_OBJECT (animation), "loop");
535 * gdk_pixbuf_simple_anim_get_loop:
536 * @animation: a #GdkPixbufSimpleAnim
538 * Gets whether @animation should loop indefinitely when it reaches the end.
540 * Returns: %TRUE if the animation loops forever, %FALSE otherwise
545 gdk_pixbuf_simple_anim_get_loop (GdkPixbufSimpleAnim *animation)
547 g_return_val_if_fail (GDK_IS_PIXBUF_SIMPLE_ANIM (animation), FALSE);
549 return animation->loop;
552 #define __GDK_PIXBUF_SIMPLE_ANIM_C__
553 #include "gdk-pixbuf-aliasdef.c"