]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-loader.c
started work on the loader
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-loader.c
1 /* GdkPixbuf library - Main header file
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Authors: Mark Crichton <crichton@gimp.org>
6  *          Miguel de Icaza <miguel@gnu.org>
7  *          Federico Mena-Quintero <federico@gimp.org>
8  *          Jonathan Blandford <jrb@redhat.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 #include "gdk-pixbuf-loader.h"
27
28
29 static GtkObjectClass *parent_class; 
30
31 static void gdk_pixbuf_loader_class_init    (GdkPixbufLoaderClass   *klass);
32 static void gdk_pixbuf_loader_init          (GdkPixbufLoader        *loader);
33 static void gdk_pixbuf_loader_destroy       (GdkPixbufLoader        *loader);
34 static void gdk_pixbuf_loader_finalize      (GdkPixbufLoader        *loader);
35
36 /* Internal data */
37 typedef struct _GdkPixbufLoaderPrivate GdkPixbufLoaderPrivate;
38 struct _GdkPixbufLoaderPrivate
39 {
40         GdkPixbuf *pixbuf;
41         gboolean closed;
42 };
43
44 GtkType
45 gdk_pixbuf_loader_get_type (void)
46 {
47         static GtkType loader_type = 0;
48
49         if (!loader_type) {
50                 static const GtkTypeInfo loader_info = {
51                         "GdkPixbufLoader",
52                         sizeof (GdkPixbufLoader),
53                         sizeof (GdkPixbufLoaderClass),
54                         (GtkClassInitFunc) gdk_pixbuf_loader_class_init,
55                         (GtkObjectInitFunc) gdk_pixbuf_loader_init,
56                         /* reserved_1 */ NULL,
57                         /* reserved_2 */ NULL,
58                         (GtkClassInitFunc) NULL,
59                 };
60
61                 loader_type = gtk_type_unique (GTK_TYPE_OBJECT, &loader_info);
62         }
63
64         return loader_type;
65 }
66
67 static void
68 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *klass)
69 {
70         parent_class = GTK_OBJECT_CLASS (klass);
71
72         parent_class->destroy = gdk_pixbuf_loader_destroy;
73         parent_class->finalize = gdk_pixbuf_loader_finalize;
74 }
75
76 static void
77 gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
78 {
79         GdkPixbuf *pixbuf;
80         loader->private = g_new (GdkPixbufLoaderPrivate, 1);
81
82         loader->pixbuf = NULL;
83         loader->closed = FALSE;
84 }
85
86 static void
87 gdk_pixbuf_loader_destroy (GdkPixbufLoader *loader)
88 {
89         GdkPixbufLoaderPrivate *priv;
90
91         priv = loader->private;
92         gdk_pixbuf_unref (priv->pixbuf);
93 }
94
95 static void
96 gdk_pixbuf_loader_finalize (GdkPixbufLoader *loader)
97 {
98         GdkPixbufLoaderPrivate *priv;
99
100         priv = loader->private;
101         g_free (priv);
102 }
103
104 /* Public functions */
105 GtkObject *
106 gdk_pixbuf_loader_new (void)
107 {
108         GdkPixbufLoader *loader;
109
110         loader = gtk_type_new (gdk_pixbuf_loader_get_type ());
111
112         return GTK_OBJECT (loader);
113 }
114
115 /**
116  * gdk_pixbuf_loader_write:
117  * @loader: A loader.
118  * @buf: The image data.
119  * @count: The length of @buf in bytes.
120  * 
121  * This will load the next @size bytes of the image.  It will return TRUE if the
122  * data was loaded successfully, and FALSE if an error occurred. In this case,
123  * the loader will be closed, and will not accept further writes.
124  * 
125  * Return value: Returns TRUE if the write was successful -- FALSE if the loader
126  * cannot parse the buf.
127  **/
128 gboolean
129 gdk_pixbuf_loader_write (GdkPixbufLoader *loader, gchar *buf, size_t count)
130 {
131         GdkPixbufLoaderPrivate *priv;
132
133         g_return_val_if_fail (loader != NULL, FALSE);
134         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
135
136         priv = loader->private;
137
138         /* we expect it's not to be closed */
139         g_return_val_if_fail (priv->closed == FALSE, FALSE);
140
141         return TRUE;
142 }
143
144 /**
145  * gdk_pixbuf_loader_get_pixbuf:
146  * @loader: A loader.
147  * 
148  * Gets the GdkPixbuf that the loader is currently loading.  If the loader
149  * hasn't been enough data via gdk_pixbuf_loader_write, then NULL is returned.
150  * Any application using this function should check for this value when it is
151  * used.  The pixbuf returned will be the same in all future calls to the
152  * loader, so simply calling a gdk_pixbuf_ref() should be sufficient to continue
153  * using it.
154  * 
155  * Return value: The GdkPixbuf that the loader is loading.
156  **/
157 GdkPixbuf *
158 gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
159 {
160         GdkPixbufLoaderPrivate *priv;
161
162         g_return_val_if_fail (loader != NULL, NULL);
163         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
164         priv = loader->private;
165
166         return priv->pixbuf;
167 }
168
169 /**
170  * gdk_pixbuf_loader_close:
171  * @loader: A loader.
172  * 
173  * Tells the loader to stop accepting writes
174  *
175  **/
176 void
177 gdk_pixbuf_loader_close (GdkPixbufLoader *loader)
178 {
179         GdkPixbufLoaderPrivate *priv;
180
181         g_return_if_fail (loader != NULL);
182         g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
183
184         priv = loader->private;
185
186         /* we expect it's not closed */
187         g_return_if_fail (priv->closed == FALSE);
188         priv->closed = TRUE;
189 }
190