gdk-pixbuf-animation.c 25.6 KB
Newer Older
Havoc Pennington's avatar
Havoc Pennington committed
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 3 4 5 6 7 8 9
/* GdkPixbuf library - Simple animation support
 *
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Authors: Jonathan Blandford <jrb@redhat.com>
 *          Havoc Pennington <hp@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11 12 13 14 15 16
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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 21 22 23 24
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

25
#include "config.h"
Havoc Pennington's avatar
Havoc Pennington committed
26
#include <errno.h>
27
#include "gdk-pixbuf-private.h"
28
#include "gdk-pixbuf-animation.h"
29

30 31
#include <glib/gstdio.h>

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/**
 * SECTION:animation
 * @Short_description: Animated images.
 * @Title: Animations
 * @See_also: #GdkPixbufLoader.
 * 
 * The &gdk-pixbuf; library provides a simple mechanism to load and represent
 * animations. An animation is conceptually a series of frames to be displayed
 * over time. Each frame is the same size. The animation may not be represented
 * as a series of frames internally; for example, it may be stored as a 
 * sprite and instructions for moving the sprite around a background. To display 
 * an animation you don't need to understand its representation, however; you just
 * ask &gdk-pixbuf; what should be displayed at a given point in time. 
 * 
 */

Havoc Pennington's avatar
Havoc Pennington committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
typedef struct _GdkPixbufNonAnim GdkPixbufNonAnim;
typedef struct _GdkPixbufNonAnimClass GdkPixbufNonAnimClass;

#define GDK_TYPE_PIXBUF_NON_ANIM              (gdk_pixbuf_non_anim_get_type ())
#define GDK_PIXBUF_NON_ANIM(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_PIXBUF_NON_ANIM, GdkPixbufNonAnim))
#define GDK_IS_PIXBUF_NON_ANIM(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_PIXBUF_NON_ANIM))

#define GDK_PIXBUF_NON_ANIM_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_PIXBUF_NON_ANIM, GdkPixbufNonAnimClass))
#define GDK_IS_PIXBUF_NON_ANIM_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_PIXBUF_NON_ANIM))
#define GDK_PIXBUF_NON_ANIM_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_PIXBUF_NON_ANIM, GdkPixbufNonAnimClass))

/* Private part of the GdkPixbufNonAnim structure */
struct _GdkPixbufNonAnim {
        GdkPixbufAnimation parent_instance;

        GdkPixbuf *pixbuf;
};

struct _GdkPixbufNonAnimClass {
        GdkPixbufAnimationClass parent_class;
        
};

71

Havoc Pennington's avatar
Havoc Pennington committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
typedef struct _GdkPixbufNonAnimIter GdkPixbufNonAnimIter;
typedef struct _GdkPixbufNonAnimIterClass GdkPixbufNonAnimIterClass;


#define GDK_TYPE_PIXBUF_NON_ANIM_ITER              (gdk_pixbuf_non_anim_iter_get_type ())
#define GDK_PIXBUF_NON_ANIM_ITER(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_PIXBUF_NON_ANIM_ITER, GdkPixbufNonAnimIter))
#define GDK_IS_PIXBUF_NON_ANIM_ITER(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_PIXBUF_NON_ANIM_ITER))

#define GDK_PIXBUF_NON_ANIM_ITER_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_PIXBUF_NON_ANIM_ITER, GdkPixbufNonAnimIterClass))
#define GDK_IS_PIXBUF_NON_ANIM_ITER_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_PIXBUF_NON_ANIM_ITER))
#define GDK_PIXBUF_NON_ANIM_ITER_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_PIXBUF_NON_ANIM_ITER, GdkPixbufNonAnimIterClass))

struct _GdkPixbufNonAnimIter {
        GdkPixbufAnimationIter parent_instance;

        GdkPixbufNonAnim   *non_anim;
};

struct _GdkPixbufNonAnimIterClass {
        GdkPixbufAnimationIterClass parent_class;

};

static GType gdk_pixbuf_non_anim_iter_get_type (void) G_GNUC_CONST;

97 98 99 100 101
G_DEFINE_TYPE (GdkPixbufAnimation, gdk_pixbuf_animation, G_TYPE_OBJECT);

static void
gdk_pixbuf_animation_class_init (GdkPixbufAnimationClass *klass)
{
102 103
}

104 105 106 107
static void
gdk_pixbuf_animation_init (GdkPixbufAnimation *animation)
{
}
108 109 110

/**
 * gdk_pixbuf_animation_new_from_file:
111
 * @filename: Name of file to load, in the GLib file name encoding
Havoc Pennington's avatar
Havoc Pennington committed
112
 * @error: return location for error
113 114 115
 *
 * Creates a new animation by loading it from a file.  The file format is
 * detected automatically.  If the file's format does not support multi-frame
Havoc Pennington's avatar
Havoc Pennington committed
116 117
 * images, then an animation with a single frame will be created. Possible errors
 * are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
118
 *
119
 * Return value: A newly-created animation with a reference count of 1, or %NULL
120 121 122 123 124
 * if any of several error conditions ocurred:  the file could not be opened,
 * there was no loader for the file's format, there was not enough memory to
 * allocate the image buffer, or the image file contained invalid data.
 **/
GdkPixbufAnimation *
Havoc Pennington's avatar
Havoc Pennington committed
125 126
gdk_pixbuf_animation_new_from_file (const char *filename,
                                    GError    **error)
127 128 129 130
{
	GdkPixbufAnimation *animation;
	int size;
	FILE *f;
131
	guchar buffer [1024];
132
	GdkPixbufModule *image_module;
133
        gchar *display_name;
134
        gboolean locked = FALSE;
135 136

	g_return_val_if_fail (filename != NULL, NULL);
137
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);
138

139
        display_name = g_filename_display_name (filename);
140
	f = g_fopen (filename, "rb");
Havoc Pennington's avatar
Havoc Pennington committed
141
	if (!f) {
142
                gint save_errno = errno;
Havoc Pennington's avatar
Havoc Pennington committed
143 144
                g_set_error (error,
                             G_FILE_ERROR,
145
                             g_file_error_from_errno (save_errno),
Havoc Pennington's avatar
Havoc Pennington committed
146
                             _("Failed to open file '%s': %s"),
147
                             display_name,
148
                             g_strerror (save_errno));
149
                g_free (display_name);
150
		return NULL;
Havoc Pennington's avatar
Havoc Pennington committed
151
        }
152 153 154 155

	size = fread (&buffer, 1, sizeof (buffer), f);

	if (size == 0) {
Havoc Pennington's avatar
Havoc Pennington committed
156 157 158 159
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                             _("Image file '%s' contains no data"),
160 161
                             display_name);
                g_free (display_name);
162 163 164 165
		fclose (f);
		return NULL;
	}

166
	image_module = _gdk_pixbuf_get_module (buffer, size, filename, error);
167
	if (!image_module) {
168
                g_free (display_name);
169 170 171 172 173
		fclose (f);
		return NULL;
	}

	if (image_module->module == NULL)
174
                if (!_gdk_pixbuf_load_module (image_module, error)) {
175
                        g_free (display_name);
Havoc Pennington's avatar
Havoc Pennington committed
176 177 178
                        fclose (f);
                        return NULL;
                }
179 180 181 182 183 184 185

	if (image_module->load_animation == NULL) {
		GdkPixbuf *pixbuf;

		/* Keep this logic in sync with gdk_pixbuf_new_from_file() */

		fseek (f, 0, SEEK_SET);
186
		pixbuf = _gdk_pixbuf_generic_image_load (image_module, f, error);
187 188
		fclose (f);

Havoc Pennington's avatar
Havoc Pennington committed
189 190 191 192 193 194 195 196 197 198 199 200 201
                if (pixbuf == NULL && error != NULL && *error == NULL) {
                        /* I don't trust these crufty longjmp()'ing image libs
                         * to maintain proper error invariants, and I don't
                         * want user code to segfault as a result. We need to maintain
                         * the invariant that error gets set if NULL is returned.
                         */
                        
                        g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.",
                                   image_module->module_name);
                        g_set_error (error,
                                     GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_FAILED,
                                     _("Failed to load image '%s': reason not known, probably a corrupt image file"),
202
                                     display_name);
Havoc Pennington's avatar
Havoc Pennington committed
203 204
                }
                
205 206
		if (pixbuf == NULL) {
                        g_free (display_name);
207 208
                        animation = NULL;
                        goto out_unlock;
209
                }
210

211
                animation = gdk_pixbuf_non_anim_new (pixbuf);
212

213
                g_object_unref (pixbuf);
214
	} else {
215 216
                locked = _gdk_pixbuf_lock (image_module);

217
		fseek (f, 0, SEEK_SET);
Havoc Pennington's avatar
Havoc Pennington committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		animation = (* image_module->load_animation) (f, error);

                if (animation == NULL && error != NULL && *error == NULL) {
                        /* I don't trust these crufty longjmp()'ing
                         * image libs to maintain proper error
                         * invariants, and I don't want user code to
                         * segfault as a result. We need to maintain
                         * the invariant that error gets set if NULL
                         * is returned.
                         */
                        
                        g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.",
                                   image_module->module_name);
                        g_set_error (error,
                                     GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_FAILED,
                                     _("Failed to load animation '%s': reason not known, probably a corrupt animation file"),
235
                                     display_name);
Havoc Pennington's avatar
Havoc Pennington committed
236 237
                }
                
238 239 240
		fclose (f);
	}

241 242
        g_free (display_name);

243
 out_unlock:
244 245
        if (locked)
                _gdk_pixbuf_unlock (image_module);
246 247 248
	return animation;
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
#ifdef G_OS_WIN32

#undef gdk_pixbuf_animation_new_from_file

GdkPixbufAnimation *
gdk_pixbuf_animation_new_from_file (const char *filename,
                                    GError    **error)
{
	gchar *utf8_filename =
		g_locale_to_utf8 (filename, -1, NULL, NULL, error);
	GdkPixbufAnimation *retval;

	if (utf8_filename == NULL)
		return NULL;

	retval = gdk_pixbuf_animation_new_from_file_utf8 (utf8_filename, error);

	g_free (utf8_filename);

	return retval;
}

#endif

273
/**
274
 * gdk_pixbuf_animation_ref: (skip)
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
 * @animation: An animation.
 *
 * Adds a reference to an animation.
 *
 * Return value: The same as the @animation argument.
 *
 * Deprecated: 2.0: Use g_object_ref().
 **/
GdkPixbufAnimation *
gdk_pixbuf_animation_ref (GdkPixbufAnimation *animation)
{
        return (GdkPixbufAnimation*) g_object_ref (animation);
}

/**
290
 * gdk_pixbuf_animation_unref: (skip)
291 292 293 294 295 296 297 298 299 300 301 302
 * @animation: An animation.
 *
 * Removes a reference from an animation.
 *
 * Deprecated: 2.0: Use g_object_unref().
 **/
void
gdk_pixbuf_animation_unref (GdkPixbufAnimation *animation)
{
        g_object_unref (animation);
}

Havoc Pennington's avatar
Havoc Pennington committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
/**
 * gdk_pixbuf_animation_is_static_image:
 * @animation: a #GdkPixbufAnimation
 * 
 * If you load a file with gdk_pixbuf_animation_new_from_file() and it turns
 * out to be a plain, unanimated image, then this function will return
 * %TRUE. Use gdk_pixbuf_animation_get_static_image() to retrieve
 * the image.
 * 
 * Return value: %TRUE if the "animation" was really just an image
 **/
gboolean
gdk_pixbuf_animation_is_static_image (GdkPixbufAnimation *animation)
{
	g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), FALSE);

        return GDK_PIXBUF_ANIMATION_GET_CLASS (animation)->is_static_image (animation);
}

/**
 * gdk_pixbuf_animation_get_static_image:
 * @animation: a #GdkPixbufAnimation
 * 
 * If an animation is really just a plain image (has only one frame),
 * this function returns that image. If the animation is an animation,
 * this function returns a reasonable thing to display as a static
 * unanimated image, which might be the first frame, or something more
 * sophisticated. If an animation hasn't loaded any frames yet, this
 * function will return %NULL.
 * 
333
 * Return value: (transfer none): unanimated image representing the animation
Havoc Pennington's avatar
Havoc Pennington committed
334 335 336 337 338 339 340 341 342
 **/
GdkPixbuf*
gdk_pixbuf_animation_get_static_image (GdkPixbufAnimation *animation)
{
	g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
        
        return GDK_PIXBUF_ANIMATION_GET_CLASS (animation)->get_static_image (animation);
}

343 344 345
/**
 * gdk_pixbuf_animation_get_width:
 * @animation: An animation.
346 347
 *
 * Queries the width of the bounding box of a pixbuf animation.
348
 * 
349
 * Return value: Width of the bounding box of the animation.
350 351 352 353
 **/
int
gdk_pixbuf_animation_get_width (GdkPixbufAnimation *animation)
{
Havoc Pennington's avatar
Havoc Pennington committed
354 355 356 357 358 359 360 361
        int width;
        
	g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), 0);

        width = 0;
        GDK_PIXBUF_ANIMATION_GET_CLASS (animation)->get_size (animation,
                                                              &width, NULL);
        
362

Havoc Pennington's avatar
Havoc Pennington committed
363
	return width;
364 365 366 367 368
}

/**
 * gdk_pixbuf_animation_get_height:
 * @animation: An animation.
369 370
 *
 * Queries the height of the bounding box of a pixbuf animation.
371
 * 
372
 * Return value: Height of the bounding box of the animation.
373 374 375 376
 **/
int
gdk_pixbuf_animation_get_height (GdkPixbufAnimation *animation)
{
Havoc Pennington's avatar
Havoc Pennington committed
377 378 379
        int height;
        
	g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), 0);
380

Havoc Pennington's avatar
Havoc Pennington committed
381 382 383 384
        height = 0;
        GDK_PIXBUF_ANIMATION_GET_CLASS (animation)->get_size (animation,
                                                              NULL, &height);
        
385

Havoc Pennington's avatar
Havoc Pennington committed
386
	return height;
387 388
}

Havoc Pennington's avatar
Havoc Pennington committed
389

390
/**
Havoc Pennington's avatar
Havoc Pennington committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
 * gdk_pixbuf_animation_get_iter:
 * @animation: a #GdkPixbufAnimation
 * @start_time: time when the animation starts playing
 * 
 * Get an iterator for displaying an animation. The iterator provides
 * the frames that should be displayed at a given time.
 * It should be freed after use with g_object_unref().
 * 
 * @start_time would normally come from g_get_current_time(), and
 * marks the beginning of animation playback. After creating an
 * iterator, you should immediately display the pixbuf returned by
 * gdk_pixbuf_animation_iter_get_pixbuf(). Then, you should install a
 * timeout (with g_timeout_add()) or by some other mechanism ensure
 * that you'll update the image after
 * gdk_pixbuf_animation_iter_get_delay_time() milliseconds. Each time
 * the image is updated, you should reinstall the timeout with the new,
 * possibly-changed delay time.
 *
 * As a shortcut, if @start_time is %NULL, the result of
 * g_get_current_time() will be used automatically.
411
 *
Havoc Pennington's avatar
Havoc Pennington committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
 * To update the image (i.e. possibly change the result of
 * gdk_pixbuf_animation_iter_get_pixbuf() to a new frame of the animation),
 * call gdk_pixbuf_animation_iter_advance().
 *
 * If you're using #GdkPixbufLoader, in addition to updating the image
 * after the delay time, you should also update it whenever you
 * receive the area_updated signal and
 * gdk_pixbuf_animation_iter_on_currently_loading_frame() returns
 * %TRUE. In this case, the frame currently being fed into the loader
 * has received new data, so needs to be refreshed. The delay time for
 * a frame may also be modified after an area_updated signal, for
 * example if the delay time for a frame is encoded in the data after
 * the frame itself. So your timeout should be reinstalled after any
 * area_updated signal.
 *
 * A delay time of -1 is possible, indicating "infinite."
428
 * 
429
 * Return value: (transfer full): an iterator to move over the animation
430
 **/
Havoc Pennington's avatar
Havoc Pennington committed
431 432 433
GdkPixbufAnimationIter*
gdk_pixbuf_animation_get_iter (GdkPixbufAnimation *animation,
                               const GTimeVal     *start_time)
434
{
Havoc Pennington's avatar
Havoc Pennington committed
435 436 437 438
        GTimeVal val;
        
        g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);

439

Havoc Pennington's avatar
Havoc Pennington committed
440 441 442 443 444 445
        if (start_time)
                val = *start_time;
        else
                g_get_current_time (&val);
        
        return GDK_PIXBUF_ANIMATION_GET_CLASS (animation)->get_iter (animation, &val);
446
}
447

448
G_DEFINE_TYPE (GdkPixbufAnimationIter, gdk_pixbuf_animation_iter, G_TYPE_OBJECT);
449

450 451
static void
gdk_pixbuf_animation_iter_class_init (GdkPixbufAnimationIterClass *klass)
452
{
453
}
454

455 456 457
static void
gdk_pixbuf_animation_iter_init (GdkPixbufAnimationIter *iter)
{
458 459 460
}

/**
Havoc Pennington's avatar
Havoc Pennington committed
461 462
 * gdk_pixbuf_animation_iter_get_delay_time:
 * @iter: an animation iterator
463
 *
Havoc Pennington's avatar
Havoc Pennington committed
464 465 466 467 468 469
 * Gets the number of milliseconds the current pixbuf should be displayed,
 * or -1 if the current pixbuf should be displayed forever. g_timeout_add()
 * conveniently takes a timeout in milliseconds, so you can use a timeout
 * to schedule the next update.
 *
 * Return value: delay time in milliseconds (thousandths of a second)
470 471
 **/
int
Havoc Pennington's avatar
Havoc Pennington committed
472
gdk_pixbuf_animation_iter_get_delay_time (GdkPixbufAnimationIter *iter)
473
{
Havoc Pennington's avatar
Havoc Pennington committed
474
        g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION_ITER (iter), -1);
475 476
        g_return_val_if_fail (GDK_PIXBUF_ANIMATION_ITER_GET_CLASS (iter)->get_delay_time, -1);

Havoc Pennington's avatar
Havoc Pennington committed
477
        return GDK_PIXBUF_ANIMATION_ITER_GET_CLASS (iter)->get_delay_time (iter);
478 479 480
}

/**
Havoc Pennington's avatar
Havoc Pennington committed
481 482
 * gdk_pixbuf_animation_iter_get_pixbuf:
 * @iter: an animation iterator
483
 * 
Havoc Pennington's avatar
Havoc Pennington committed
484 485
 * Gets the current pixbuf which should be displayed; the pixbuf will
 * be the same size as the animation itself
486 487 488
 * (gdk_pixbuf_animation_get_width(), gdk_pixbuf_animation_get_height()). 
 * This pixbuf should be displayed for 
 * gdk_pixbuf_animation_iter_get_delay_time() milliseconds.  The caller
Havoc Pennington's avatar
Havoc Pennington committed
489 490 491 492 493 494 495
 * of this function does not own a reference to the returned pixbuf;
 * the returned pixbuf will become invalid when the iterator advances
 * to the next frame, which may happen anytime you call
 * gdk_pixbuf_animation_iter_advance(). Copy the pixbuf to keep it
 * (don't just add a reference), as it may get recycled as you advance
 * the iterator.
 *
496
 * Return value: (transfer none): the pixbuf to be displayed
497
 **/
Havoc Pennington's avatar
Havoc Pennington committed
498 499
GdkPixbuf*
gdk_pixbuf_animation_iter_get_pixbuf (GdkPixbufAnimationIter *iter)
500
{
Havoc Pennington's avatar
Havoc Pennington committed
501
        g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION_ITER (iter), NULL);
502 503
        g_return_val_if_fail (GDK_PIXBUF_ANIMATION_ITER_GET_CLASS (iter)->get_pixbuf, NULL);

Havoc Pennington's avatar
Havoc Pennington committed
504
        return GDK_PIXBUF_ANIMATION_ITER_GET_CLASS (iter)->get_pixbuf (iter);
505 506 507
}

/**
Havoc Pennington's avatar
Havoc Pennington committed
508 509 510 511 512 513 514 515
 * gdk_pixbuf_animation_iter_on_currently_loading_frame:
 * @iter: a #GdkPixbufAnimationIter
 *
 * Used to determine how to respond to the area_updated signal on
 * #GdkPixbufLoader when loading an animation. area_updated is emitted
 * for an area of the frame currently streaming in to the loader. So if
 * you're on the currently loading frame, you need to redraw the screen for
 * the updated area.
516
 *
Havoc Pennington's avatar
Havoc Pennington committed
517
 * Return value: %TRUE if the frame we're on is partially loaded, or the last frame
518
 **/
Havoc Pennington's avatar
Havoc Pennington committed
519 520
gboolean
gdk_pixbuf_animation_iter_on_currently_loading_frame (GdkPixbufAnimationIter *iter)
521
{
Havoc Pennington's avatar
Havoc Pennington committed
522
        g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION_ITER (iter), FALSE);
523 524
        g_return_val_if_fail (GDK_PIXBUF_ANIMATION_ITER_GET_CLASS (iter)->on_currently_loading_frame, FALSE);

Havoc Pennington's avatar
Havoc Pennington committed
525
        return GDK_PIXBUF_ANIMATION_ITER_GET_CLASS (iter)->on_currently_loading_frame (iter);
526 527 528
}

/**
Havoc Pennington's avatar
Havoc Pennington committed
529 530 531 532 533 534
 * gdk_pixbuf_animation_iter_advance:
 * @iter: a #GdkPixbufAnimationIter
 * @current_time: current time
 *
 * Possibly advances an animation to a new frame. Chooses the frame based
 * on the start time passed to gdk_pixbuf_animation_get_iter().
535
 *
Havoc Pennington's avatar
Havoc Pennington committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
 * @current_time would normally come from g_get_current_time(), and
 * must be greater than or equal to the time passed to
 * gdk_pixbuf_animation_get_iter(), and must increase or remain
 * unchanged each time gdk_pixbuf_animation_iter_get_pixbuf() is
 * called. That is, you can't go backward in time; animations only
 * play forward.
 *
 * As a shortcut, pass %NULL for the current time and g_get_current_time()
 * will be invoked on your behalf. So you only need to explicitly pass
 * @current_time if you're doing something odd like playing the animation
 * at double speed.
 *
 * If this function returns %FALSE, there's no need to update the animation
 * display, assuming the display had been rendered prior to advancing;
 * if %TRUE, you need to call gdk_animation_iter_get_pixbuf() and update the
 * display with the new pixbuf.
 *
 * Returns: %TRUE if the image may need updating
554 555
 * 
 **/
Havoc Pennington's avatar
Havoc Pennington committed
556 557 558
gboolean
gdk_pixbuf_animation_iter_advance (GdkPixbufAnimationIter *iter,
                                   const GTimeVal         *current_time)
559
{
Havoc Pennington's avatar
Havoc Pennington committed
560
        GTimeVal val;
561

Havoc Pennington's avatar
Havoc Pennington committed
562
        g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION_ITER (iter), FALSE);
563
        g_return_val_if_fail (GDK_PIXBUF_ANIMATION_ITER_GET_CLASS (iter)->advance, FALSE);
564

Havoc Pennington's avatar
Havoc Pennington committed
565 566 567 568
        if (current_time)
                val = *current_time;
        else
                g_get_current_time (&val);
569

Havoc Pennington's avatar
Havoc Pennington committed
570
        return GDK_PIXBUF_ANIMATION_ITER_GET_CLASS (iter)->advance (iter, &val);
571
}
572

573
static void                    gdk_pixbuf_non_anim_finalize         (GObject            *object);
Havoc Pennington's avatar
Havoc Pennington committed
574 575 576 577 578 579 580 581
static gboolean                gdk_pixbuf_non_anim_is_static_image  (GdkPixbufAnimation *animation);
static GdkPixbuf*              gdk_pixbuf_non_anim_get_static_image (GdkPixbufAnimation *animation);
static void                    gdk_pixbuf_non_anim_get_size         (GdkPixbufAnimation *anim,
                                                                     int                *width,
                                                                     int                *height);
static GdkPixbufAnimationIter* gdk_pixbuf_non_anim_get_iter         (GdkPixbufAnimation *anim,
                                                                     const GTimeVal     *start_time);

582
G_DEFINE_TYPE (GdkPixbufNonAnim, gdk_pixbuf_non_anim, GDK_TYPE_PIXBUF_ANIMATION);
583

Havoc Pennington's avatar
Havoc Pennington committed
584 585
static void
gdk_pixbuf_non_anim_class_init (GdkPixbufNonAnimClass *klass)
586
{
Havoc Pennington's avatar
Havoc Pennington committed
587 588 589 590 591 592 593 594 595 596 597
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        GdkPixbufAnimationClass *anim_class = GDK_PIXBUF_ANIMATION_CLASS (klass);
        
        object_class->finalize = gdk_pixbuf_non_anim_finalize;

        anim_class->is_static_image = gdk_pixbuf_non_anim_is_static_image;
        anim_class->get_static_image = gdk_pixbuf_non_anim_get_static_image;
        anim_class->get_size = gdk_pixbuf_non_anim_get_size;
        anim_class->get_iter = gdk_pixbuf_non_anim_get_iter;
}

598 599 600 601 602
static void
gdk_pixbuf_non_anim_init (GdkPixbufNonAnim *non_anim)
{
}

Havoc Pennington's avatar
Havoc Pennington committed
603 604 605 606 607 608
static void
gdk_pixbuf_non_anim_finalize (GObject *object)
{
        GdkPixbufNonAnim *non_anim = GDK_PIXBUF_NON_ANIM (object);

        if (non_anim->pixbuf)
609
                g_object_unref (non_anim->pixbuf);
Havoc Pennington's avatar
Havoc Pennington committed
610
        
611
        G_OBJECT_CLASS (gdk_pixbuf_non_anim_parent_class)->finalize (object);
Havoc Pennington's avatar
Havoc Pennington committed
612 613 614
}

GdkPixbufAnimation*
615
gdk_pixbuf_non_anim_new (GdkPixbuf *pixbuf)
Havoc Pennington's avatar
Havoc Pennington committed
616 617 618 619 620 621 622 623
{
        GdkPixbufNonAnim *non_anim;

        non_anim = g_object_new (GDK_TYPE_PIXBUF_NON_ANIM, NULL);

        non_anim->pixbuf = pixbuf;

        if (pixbuf)
624
                g_object_ref (pixbuf);
625

Havoc Pennington's avatar
Havoc Pennington committed
626
        return GDK_PIXBUF_ANIMATION (non_anim);
627 628
}

Havoc Pennington's avatar
Havoc Pennington committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
static gboolean
gdk_pixbuf_non_anim_is_static_image  (GdkPixbufAnimation *animation)
{

        return TRUE;
}

static GdkPixbuf*
gdk_pixbuf_non_anim_get_static_image (GdkPixbufAnimation *animation)
{
        GdkPixbufNonAnim *non_anim;

        non_anim = GDK_PIXBUF_NON_ANIM (animation);

        return non_anim->pixbuf;
}

static void
gdk_pixbuf_non_anim_get_size (GdkPixbufAnimation *anim,
                              int                *width,
                              int                *height)
{
        GdkPixbufNonAnim *non_anim;

        non_anim = GDK_PIXBUF_NON_ANIM (anim);

        if (width)
                *width = gdk_pixbuf_get_width (non_anim->pixbuf);

        if (height)
                *height = gdk_pixbuf_get_height (non_anim->pixbuf);
}

static GdkPixbufAnimationIter*
gdk_pixbuf_non_anim_get_iter (GdkPixbufAnimation *anim,
                              const GTimeVal     *start_time)
{
        GdkPixbufNonAnimIter *iter;

        iter = g_object_new (GDK_TYPE_PIXBUF_NON_ANIM_ITER, NULL);

        iter->non_anim = GDK_PIXBUF_NON_ANIM (anim);

672
        g_object_ref (iter->non_anim);
Havoc Pennington's avatar
Havoc Pennington committed
673 674 675 676
        
        return GDK_PIXBUF_ANIMATION_ITER (iter);
}

677
static void       gdk_pixbuf_non_anim_iter_finalize                   (GObject                *object);
Havoc Pennington's avatar
Havoc Pennington committed
678 679 680 681 682 683
static int        gdk_pixbuf_non_anim_iter_get_delay_time             (GdkPixbufAnimationIter *iter);
static GdkPixbuf* gdk_pixbuf_non_anim_iter_get_pixbuf                 (GdkPixbufAnimationIter *iter);
static gboolean   gdk_pixbuf_non_anim_iter_on_currently_loading_frame (GdkPixbufAnimationIter *iter);
static gboolean   gdk_pixbuf_non_anim_iter_advance                    (GdkPixbufAnimationIter *iter,
                                                                       const GTimeVal         *current_time);

684 685 686
G_DEFINE_TYPE (GdkPixbufNonAnimIter,
               gdk_pixbuf_non_anim_iter,
               GDK_TYPE_PIXBUF_ANIMATION_ITER);
Havoc Pennington's avatar
Havoc Pennington committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

static void
gdk_pixbuf_non_anim_iter_class_init (GdkPixbufNonAnimIterClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        GdkPixbufAnimationIterClass *anim_iter_class =
                GDK_PIXBUF_ANIMATION_ITER_CLASS (klass);
        
        object_class->finalize = gdk_pixbuf_non_anim_iter_finalize;

        anim_iter_class->get_delay_time = gdk_pixbuf_non_anim_iter_get_delay_time;
        anim_iter_class->get_pixbuf = gdk_pixbuf_non_anim_iter_get_pixbuf;
        anim_iter_class->on_currently_loading_frame = gdk_pixbuf_non_anim_iter_on_currently_loading_frame;
        anim_iter_class->advance = gdk_pixbuf_non_anim_iter_advance;
}

703 704 705 706 707
static void
gdk_pixbuf_non_anim_iter_init (GdkPixbufNonAnimIter *non_iter)
{
}

Havoc Pennington's avatar
Havoc Pennington committed
708 709
static void
gdk_pixbuf_non_anim_iter_finalize (GObject *object)
710
{
Havoc Pennington's avatar
Havoc Pennington committed
711
        GdkPixbufNonAnimIter *iter = GDK_PIXBUF_NON_ANIM_ITER (object);
712

713
        g_object_unref (iter->non_anim);
Havoc Pennington's avatar
Havoc Pennington committed
714
        
715
        G_OBJECT_CLASS (gdk_pixbuf_non_anim_iter_parent_class)->finalize (object);
Havoc Pennington's avatar
Havoc Pennington committed
716 717 718 719 720 721 722
}

static int
gdk_pixbuf_non_anim_iter_get_delay_time (GdkPixbufAnimationIter *iter)
{
        return -1; /* show only frame forever */
}
723

Havoc Pennington's avatar
Havoc Pennington committed
724 725 726 727
static GdkPixbuf*
gdk_pixbuf_non_anim_iter_get_pixbuf (GdkPixbufAnimationIter *iter)
{
        return GDK_PIXBUF_NON_ANIM_ITER (iter)->non_anim->pixbuf;
728
}
Havoc Pennington's avatar
Havoc Pennington committed
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744


static gboolean
gdk_pixbuf_non_anim_iter_on_currently_loading_frame (GdkPixbufAnimationIter *iter)
{
        return TRUE;
}
        
static gboolean
gdk_pixbuf_non_anim_iter_advance (GdkPixbufAnimationIter *iter,
                                  const GTimeVal         *current_time)
{

        /* Advancing never requires a refresh */
        return FALSE;
}