]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-animation.c
urg, removed implementation of gtk_marshal_VOID__INT_INT_INT_INT. if
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-animation.c
1 /* GdkPixbuf library - Simple animation support
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Authors: Jonathan Blandford <jrb@redhat.com>
6  *          Havoc Pennington <hp@redhat.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
24 #include <config.h>
25 #include <errno.h>
26 #include "gdk-pixbuf-io.h"
27 #include "gdk-pixbuf-private.h"
28
29 static void gdk_pixbuf_animation_class_init (GdkPixbufAnimationClass *klass);
30 static void gdk_pixbuf_animation_finalize   (GObject        *object);
31
32 \f
33
34 static gpointer parent_class;
35
36 GType
37 gdk_pixbuf_animation_get_type (void)
38 {
39         static GType object_type = 0;
40
41         if (!object_type) {
42                 static const GTypeInfo object_info = {
43                         sizeof (GdkPixbufAnimationClass),
44                         (GBaseInitFunc) NULL,
45                         (GBaseFinalizeFunc) NULL,
46                         (GClassInitFunc) gdk_pixbuf_animation_class_init,
47                         NULL,           /* class_finalize */
48                         NULL,           /* class_data */
49                         sizeof (GdkPixbufAnimation),
50                         0,              /* n_preallocs */
51                         (GInstanceInitFunc) NULL,
52                 };
53       
54                 object_type = g_type_register_static (G_TYPE_OBJECT,
55                                                       "GdkPixbufAnimation",
56                                                       &object_info, 0);
57         }
58   
59         return object_type;
60 }
61
62 static void
63 gdk_pixbuf_animation_class_init (GdkPixbufAnimationClass *klass)
64 {
65         GObjectClass *object_class = G_OBJECT_CLASS (klass);
66         
67         parent_class = g_type_class_peek_parent (klass);
68         
69         object_class->finalize = gdk_pixbuf_animation_finalize;
70 }
71
72 static void
73 gdk_pixbuf_animation_finalize (GObject *object)
74 {
75         GdkPixbufAnimation *animation = GDK_PIXBUF_ANIMATION (object);
76
77         GList *l;
78         GdkPixbufFrame *frame;
79         
80         for (l = animation->frames; l; l = l->next) {
81                 frame = l->data;
82                 gdk_pixbuf_unref (frame->pixbuf);
83                 g_free (frame);
84         }
85         
86         g_list_free (animation->frames);
87         
88         G_OBJECT_CLASS (parent_class)->finalize (object);
89 }
90
91 \f
92
93 /**
94  * gdk_pixbuf_animation_new_from_file:
95  * @filename: Name of file to load.
96  * @error: return location for error
97  *
98  * Creates a new animation by loading it from a file.  The file format is
99  * detected automatically.  If the file's format does not support multi-frame
100  * images, then an animation with a single frame will be created. Possible errors
101  * are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
102  *
103  * Return value: A newly created animation with a reference count of 1, or NULL
104  * if any of several error conditions ocurred:  the file could not be opened,
105  * there was no loader for the file's format, there was not enough memory to
106  * allocate the image buffer, or the image file contained invalid data.
107  **/
108 GdkPixbufAnimation *
109 gdk_pixbuf_animation_new_from_file (const char *filename,
110                                     GError    **error)
111 {
112         GdkPixbufAnimation *animation;
113         int size;
114         FILE *f;
115         guchar buffer [128];
116         GdkPixbufModule *image_module;
117
118         g_return_val_if_fail (filename != NULL, NULL);
119
120         f = fopen (filename, "rb");
121         if (!f) {
122                 g_set_error (error,
123                              G_FILE_ERROR,
124                              g_file_error_from_errno (errno),
125                              _("Failed to open file '%s': %s"),
126                              filename, g_strerror (errno));
127                 return NULL;
128         }
129
130         size = fread (&buffer, 1, sizeof (buffer), f);
131
132         if (size == 0) {
133                 g_set_error (error,
134                              GDK_PIXBUF_ERROR,
135                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
136                              _("Image file '%s' contains no data"),
137                              filename);
138                 
139                 fclose (f);
140                 return NULL;
141         }
142
143         image_module = gdk_pixbuf_get_module (buffer, size, filename, error);
144         if (!image_module) {
145                 fclose (f);
146                 return NULL;
147         }
148
149         if (image_module->module == NULL)
150                 if (!gdk_pixbuf_load_module (image_module, error)) {
151                         fclose (f);
152                         return NULL;
153                 }
154
155         if (image_module->load_animation == NULL) {
156                 GdkPixbuf *pixbuf;
157                 GdkPixbufFrame *frame;
158
159                 /* Keep this logic in sync with gdk_pixbuf_new_from_file() */
160
161                 if (image_module->load == NULL) {
162                         g_set_error (error,
163                                      GDK_PIXBUF_ERROR,
164                                      GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
165                                      _("Don't know how to load the animation in file '%s'"),
166                                      filename);                        
167                         fclose (f);
168                         return NULL;
169                 }
170
171                 fseek (f, 0, SEEK_SET);
172                 pixbuf = (* image_module->load) (f, error);
173                 fclose (f);
174
175                 if (pixbuf == NULL && error != NULL && *error == NULL) {
176                         /* I don't trust these crufty longjmp()'ing image libs
177                          * to maintain proper error invariants, and I don't
178                          * want user code to segfault as a result. We need to maintain
179                          * the invariant that error gets set if NULL is returned.
180                          */
181                         
182                         g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.",
183                                    image_module->module_name);
184                         g_set_error (error,
185                                      GDK_PIXBUF_ERROR,
186                                      GDK_PIXBUF_ERROR_FAILED,
187                                      _("Failed to load image '%s': reason not known, probably a corrupt image file"),
188                                      filename);
189                 }
190                 
191                 if (pixbuf == NULL)
192                         return NULL;
193
194                 frame = g_new (GdkPixbufFrame, 1);
195                 frame->pixbuf = pixbuf;
196                 frame->x_offset = 0;
197                 frame->y_offset = 0;
198                 frame->delay_time = -1;
199                 frame->action = GDK_PIXBUF_FRAME_RETAIN;
200
201                 animation = g_object_new (GDK_TYPE_PIXBUF_ANIMATION, NULL);
202
203                 animation->n_frames = 1;
204                 animation->frames = g_list_prepend (NULL, frame);
205                 animation->width = gdk_pixbuf_get_width (pixbuf);
206                 animation->height = gdk_pixbuf_get_height (pixbuf);
207         } else {
208                 fseek (f, 0, SEEK_SET);
209                 animation = (* image_module->load_animation) (f, error);
210
211                 if (animation == NULL && error != NULL && *error == NULL) {
212                         /* I don't trust these crufty longjmp()'ing
213                          * image libs to maintain proper error
214                          * invariants, and I don't want user code to
215                          * segfault as a result. We need to maintain
216                          * the invariant that error gets set if NULL
217                          * is returned.
218                          */
219                         
220                         g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.",
221                                    image_module->module_name);
222                         g_set_error (error,
223                                      GDK_PIXBUF_ERROR,
224                                      GDK_PIXBUF_ERROR_FAILED,
225                                      _("Failed to load animation '%s': reason not known, probably a corrupt animation file"),
226                                      filename);
227                 }
228                 
229                 fclose (f);
230         }
231
232         return animation;
233 }
234
235 /**
236  * gdk_pixbuf_animation_ref:
237  * @animation: An animation.
238  *
239  * Adds a reference to an animation. Deprecated; use
240  * g_object_ref(). The reference must be released afterwards using
241  * g_object_unref().
242  *
243  * Return value: The same as the @animation argument.
244  **/
245 GdkPixbufAnimation *
246 gdk_pixbuf_animation_ref (GdkPixbufAnimation *animation)
247 {
248         return (GdkPixbufAnimation*) g_object_ref (G_OBJECT (animation));
249 }
250
251 /**
252  * gdk_pixbuf_animation_unref:
253  * @animation: An animation.
254  * 
255  * Removes a reference from an animation. Deprecated; use g_object_unref().
256  **/
257 void
258 gdk_pixbuf_animation_unref (GdkPixbufAnimation *animation)
259 {
260         g_object_unref (G_OBJECT (animation));
261 }
262
263 /**
264  * gdk_pixbuf_animation_get_width:
265  * @animation: An animation.
266  *
267  * Queries the width of the bounding box of a pixbuf animation.
268  * 
269  * Return value: Width of the bounding box of the animation.
270  **/
271 int
272 gdk_pixbuf_animation_get_width (GdkPixbufAnimation *animation)
273 {
274         g_return_val_if_fail (animation != NULL, -1);
275
276         return animation->width;
277 }
278
279 /**
280  * gdk_pixbuf_animation_get_height:
281  * @animation: An animation.
282  *
283  * Queries the height of the bounding box of a pixbuf animation.
284  * 
285  * Return value: Height of the bounding box of the animation.
286  **/
287 int
288 gdk_pixbuf_animation_get_height (GdkPixbufAnimation *animation)
289 {
290         g_return_val_if_fail (animation != NULL, -1);
291
292         return animation->height;
293 }
294
295 /**
296  * gdk_pixbuf_animation_get_num_frames:
297  * @animation: An animation.
298  *
299  * Queries the number of frames in a pixbuf animation.
300  * 
301  * Return value: Number of frames in the animation.
302  **/
303 int
304 gdk_pixbuf_animation_get_num_frames (GdkPixbufAnimation *animation)
305 {
306         g_return_val_if_fail (animation != NULL, -1);
307
308         return animation->n_frames;
309 }
310
311 /**
312  * gdk_pixbuf_animation_get_frames:
313  * @animation: An animation.
314  *
315  * Queries the list of frames of an animation.
316  * 
317  * Return value: List of frames in the animation; this is a #GList of
318  * #GdkPixbufFrame structures.
319  **/
320 GList *
321 gdk_pixbuf_animation_get_frames (GdkPixbufAnimation *animation)
322 {
323         g_return_val_if_fail (animation != NULL, NULL);
324
325         return animation->frames;
326 }
327
328 \f
329
330 /**
331  * gdk_pixbuf_frame_get_pixbuf:
332  * @frame: A pixbuf animation frame.
333  * 
334  * Queries the pixbuf of an animation frame.
335  * 
336  * Return value: A pixbuf.
337  **/
338 GdkPixbuf *
339 gdk_pixbuf_frame_get_pixbuf (GdkPixbufFrame *frame)
340 {
341         g_return_val_if_fail (frame != NULL, NULL);
342
343         return frame->pixbuf;
344 }
345
346 /**
347  * gdk_pixbuf_frame_get_x_offset:
348  * @frame: A pixbuf animation frame.
349  * 
350  * Queries the X offset of an animation frame.
351  * 
352  * Return value: X offset from the top left corner of the animation.
353  **/
354 int
355 gdk_pixbuf_frame_get_x_offset (GdkPixbufFrame *frame)
356 {
357         g_return_val_if_fail (frame != NULL, -1);
358
359         return frame->x_offset;
360 }
361
362 /**
363  * gdk_pixbuf_frame_get_y_offset:
364  * @frame: A pixbuf animation frame.
365  * 
366  * Queries the Y offset of an animation frame.
367  * 
368  * Return value: Y offset from the top left corner of the animation.
369  **/
370 int
371 gdk_pixbuf_frame_get_y_offset (GdkPixbufFrame *frame)
372 {
373         g_return_val_if_fail (frame != NULL, -1);
374
375         return frame->y_offset;
376 }
377
378 /**
379  * gdk_pixbuf_frame_get_delay_time:
380  * @frame: A pixbuf animation frame.
381  * 
382  * Queries the delay time in milliseconds of an animation frame.
383  * 
384  * Return value: Delay time in milliseconds.
385  **/
386 int
387 gdk_pixbuf_frame_get_delay_time (GdkPixbufFrame *frame)
388 {
389         g_return_val_if_fail (frame != NULL, -1);
390
391         return frame->delay_time;
392 }
393
394 /**
395  * gdk_pixbuf_frame_get_action:
396  * @frame: A pixbuf animation frame.
397  * 
398  * Queries the overlay action of an animation frame.
399  * 
400  * Return value: Overlay action for this frame.
401  **/
402 GdkPixbufFrameAction
403 gdk_pixbuf_frame_get_action (GdkPixbufFrame *frame)
404 {
405         g_return_val_if_fail (frame != NULL, GDK_PIXBUF_FRAME_RETAIN);
406
407         return frame->action;
408 }