1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19  
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 /*
25  * Conversion parameters:
26  * inFile  = GdkPixbufLoader.html
27  * outPack = gdkpixbuf
28  * outFile = PixbufLoader
29  * strct   = GdkPixbufLoader
30  * realStrct=
31  * ctorStrct=
32  * clss    = PixbufLoader
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- gdk_pixbuf_loader_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * 	- gdk_pixbuf_loader_new_with_type
45  * 	- gdk_pixbuf_loader_new_with_mime_type
46  * omit signals:
47  * imports:
48  * 	- gdkpixbuf.PixbufFormat
49  * 	- glib.ErrorG
50  * 	- glib.GException
51  * 	- glib.Bytes
52  * 	- gdk.Pixbuf
53  * 	- gdkpixbuf.PixbufAnimation
54  * 	- glib.Str
55  * structWrap:
56  * 	- GBytes* -> Bytes
57  * 	- GdkPixbuf* -> Pixbuf
58  * 	- GdkPixbufAnimation* -> PixbufAnimation
59  * 	- GdkPixbufFormat* -> PixbufFormat
60  * module aliases:
61  * local aliases:
62  * overrides:
63  */
64 
65 module gdkpixbuf.PixbufLoader;
66 
67 public  import gtkc.gdkpixbuftypes;
68 
69 private import gtkc.gdkpixbuf;
70 private import glib.ConstructionException;
71 private import gobject.ObjectG;
72 
73 private import gobject.Signals;
74 public  import gtkc.gdktypes;
75 private import gdkpixbuf.PixbufFormat;
76 private import glib.ErrorG;
77 private import glib.GException;
78 private import glib.Bytes;
79 private import gdk.Pixbuf;
80 private import gdkpixbuf.PixbufAnimation;
81 private import glib.Str;
82 
83 
84 private import gobject.ObjectG;
85 
86 /**
87  * GdkPixbufLoader provides a way for applications to drive the
88  * process of loading an image, by letting them send the image data
89  * directly to the loader instead of having the loader read the data
90  * from a file. Applications can use this functionality instead of
91  * gdk_pixbuf_new_from_file() or gdk_pixbuf_animation_new_from_file()
92  * when they need to parse image data in
93  * small chunks. For example, it should be used when reading an
94  * image from a (potentially) slow network connection, or when
95  * loading an extremely large file.
96  *
97  * To use GdkPixbufLoader to load an image, just create a new one,
98  * and call gdk_pixbuf_loader_write() to send the data to it. When
99  * done, gdk_pixbuf_loader_close() should be called to end the stream
100  * and finalize everything. The loader will emit three important
101  * signals throughout the process. The first, "size_prepared",
102  * will be called as soon as the image has enough information to
103  * determine the size of the image to be used. If you want to scale
104  * the image while loading it, you can call gdk_pixbuf_loader_set_size()
105  * in response to this signal.
106  *
107  * The second signal, "area_prepared",
108  * will be called as soon as the pixbuf of the desired has been
109  * allocated. You can obtain it by calling gdk_pixbuf_loader_get_pixbuf().
110  * If you want to use it, simply ref it.
111  * In addition, no actual information will be passed in yet, so the
112  * pixbuf can be safely filled with any temporary graphics (or an
113  * initial color) as needed. You can also call
114  * gdk_pixbuf_loader_get_pixbuf() later and get the same pixbuf.
115  *
116  * The last signal, "area_updated" gets
117  * called every time a region is updated. This way you can update a
118  * partially completed image. Note that you do not know anything
119  * about the completeness of an image from the area updated. For
120  * example, in an interlaced image, you need to make several passes
121  * before the image is done loading.
122  *
123  * Loading an animation
124  *
125  * Loading an animation is almost as easy as loading an
126  * image. Once the first "area_prepared" signal
127  * has been emitted, you can call gdk_pixbuf_loader_get_animation()
128  * to get the GdkPixbufAnimation struct and gdk_pixbuf_animation_get_iter()
129  * to get an GdkPixbufAnimationIter for displaying it.
130  */
131 public class PixbufLoader : ObjectG
132 {
133 	
134 	/** the main Gtk struct */
135 	protected GdkPixbufLoader* gdkPixbufLoader;
136 	
137 	
138 	/** Get the main Gtk struct */
139 	public GdkPixbufLoader* getPixbufLoaderStruct()
140 	{
141 		return gdkPixbufLoader;
142 	}
143 	
144 	
145 	/** the main Gtk struct as a void* */
146 	protected override void* getStruct()
147 	{
148 		return cast(void*)gdkPixbufLoader;
149 	}
150 	
151 	/**
152 	 * Sets our main struct and passes it to the parent class
153 	 */
154 	public this (GdkPixbufLoader* gdkPixbufLoader)
155 	{
156 		super(cast(GObject*)gdkPixbufLoader);
157 		this.gdkPixbufLoader = gdkPixbufLoader;
158 	}
159 	
160 	protected override void setStruct(GObject* obj)
161 	{
162 		super.setStruct(obj);
163 		gdkPixbufLoader = cast(GdkPixbufLoader*)obj;
164 	}
165 	
166 	/**
167 	 * Creates a new pixbuf loader object that always attempts to parse
168 	 * image data as if it were an image of type image_type or mime_type, instead of
169 	 * identifying the type automatically. Useful if you want an error if
170 	 * the image isn't the expected type, for loading image formats
171 	 * that can't be reliably identified by looking at the data, or if
172 	 * the user manually forces a specific type.
173 	 * Params:
174 	 *  type = name of the image format or mime to be loaded with the image
175 	 */
176 	public this (string type, bool isMimeType=false)
177 	{
178 		GdkPixbufLoader* p;
179 		GError* err = null;
180 		
181 		if ( isMimeType )
182 		{
183 			// GdkPixbufLoader* gdk_pixbuf_loader_new_with_mime_type  (const char *mime_type,  GError **error);
184 			p = cast(GdkPixbufLoader*)gdk_pixbuf_loader_new_with_mime_type(Str.toStringz(type), &err);
185 		}
186 		else
187 		{
188 			// GdkPixbufLoader* gdk_pixbuf_loader_new_with_type  (const char *image_type,  GError **error);
189 			p = cast(GdkPixbufLoader*)gdk_pixbuf_loader_new_with_type(Str.toStringz(type), &err);
190 		}
191 		
192 		if (err !is null)
193 		{
194 			throw new GException( new ErrorG(err) );
195 		}
196 		
197 		if(p is null)
198 		{
199 			throw new ConstructionException("null returned by gtk_button_new()");
200 		}
201 		
202 		this(p);
203 	}
204 	
205 	/**
206 	 */
207 	int[string] connectedSignals;
208 	
209 	void delegate(PixbufLoader)[] onAreaPreparedListeners;
210 	/**
211 	 * This signal is emitted when the pixbuf loader has allocated the
212 	 * pixbuf in the desired size. After this signal is emitted,
213 	 * applications can call gdk_pixbuf_loader_get_pixbuf() to fetch
214 	 * the partially-loaded pixbuf.
215 	 */
216 	void addOnAreaPrepared(void delegate(PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
217 	{
218 		if ( !("area-prepared" in connectedSignals) )
219 		{
220 			Signals.connectData(
221 			getStruct(),
222 			"area-prepared",
223 			cast(GCallback)&callBackAreaPrepared,
224 			cast(void*)this,
225 			null,
226 			connectFlags);
227 			connectedSignals["area-prepared"] = 1;
228 		}
229 		onAreaPreparedListeners ~= dlg;
230 	}
231 	extern(C) static void callBackAreaPrepared(GdkPixbufLoader* loaderStruct, PixbufLoader _pixbufLoader)
232 	{
233 		foreach ( void delegate(PixbufLoader) dlg ; _pixbufLoader.onAreaPreparedListeners )
234 		{
235 			dlg(_pixbufLoader);
236 		}
237 	}
238 	
239 	void delegate(gint, gint, gint, gint, PixbufLoader)[] onAreaUpdatedListeners;
240 	/**
241 	 * This signal is emitted when a significant area of the image being
242 	 * loaded has been updated. Normally it means that a complete
243 	 * scanline has been read in, but it could be a different area as
244 	 * well. Applications can use this signal to know when to repaint
245 	 * areas of an image that is being loaded.
246 	 */
247 	void addOnAreaUpdated(void delegate(gint, gint, gint, gint, PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
248 	{
249 		if ( !("area-updated" in connectedSignals) )
250 		{
251 			Signals.connectData(
252 			getStruct(),
253 			"area-updated",
254 			cast(GCallback)&callBackAreaUpdated,
255 			cast(void*)this,
256 			null,
257 			connectFlags);
258 			connectedSignals["area-updated"] = 1;
259 		}
260 		onAreaUpdatedListeners ~= dlg;
261 	}
262 	extern(C) static void callBackAreaUpdated(GdkPixbufLoader* loaderStruct, gint x, gint y, gint width, gint height, PixbufLoader _pixbufLoader)
263 	{
264 		foreach ( void delegate(gint, gint, gint, gint, PixbufLoader) dlg ; _pixbufLoader.onAreaUpdatedListeners )
265 		{
266 			dlg(x, y, width, height, _pixbufLoader);
267 		}
268 	}
269 	
270 	void delegate(PixbufLoader)[] onClosedListeners;
271 	/**
272 	 * This signal is emitted when gdk_pixbuf_loader_close() is called.
273 	 * It can be used by different parts of an application to receive
274 	 * notification when an image loader is closed by the code that
275 	 * drives it.
276 	 */
277 	void addOnClosed(void delegate(PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
278 	{
279 		if ( !("closed" in connectedSignals) )
280 		{
281 			Signals.connectData(
282 			getStruct(),
283 			"closed",
284 			cast(GCallback)&callBackClosed,
285 			cast(void*)this,
286 			null,
287 			connectFlags);
288 			connectedSignals["closed"] = 1;
289 		}
290 		onClosedListeners ~= dlg;
291 	}
292 	extern(C) static void callBackClosed(GdkPixbufLoader* loaderStruct, PixbufLoader _pixbufLoader)
293 	{
294 		foreach ( void delegate(PixbufLoader) dlg ; _pixbufLoader.onClosedListeners )
295 		{
296 			dlg(_pixbufLoader);
297 		}
298 	}
299 	
300 	void delegate(gint, gint, PixbufLoader)[] onSizePreparedListeners;
301 	/**
302 	 * This signal is emitted when the pixbuf loader has been fed the
303 	 * initial amount of data that is required to figure out the size
304 	 * of the image that it will create. Applications can call
305 	 * gdk_pixbuf_loader_set_size() in response to this signal to set
306 	 * the desired size to which the image should be scaled.
307 	 * See Also
308 	 * gdk_pixbuf_new_from_file(), gdk_pixbuf_animation_new_from_file()
309 	 */
310 	void addOnSizePrepared(void delegate(gint, gint, PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
311 	{
312 		if ( !("size-prepared" in connectedSignals) )
313 		{
314 			Signals.connectData(
315 			getStruct(),
316 			"size-prepared",
317 			cast(GCallback)&callBackSizePrepared,
318 			cast(void*)this,
319 			null,
320 			connectFlags);
321 			connectedSignals["size-prepared"] = 1;
322 		}
323 		onSizePreparedListeners ~= dlg;
324 	}
325 	extern(C) static void callBackSizePrepared(GdkPixbufLoader* loaderStruct, gint width, gint height, PixbufLoader _pixbufLoader)
326 	{
327 		foreach ( void delegate(gint, gint, PixbufLoader) dlg ; _pixbufLoader.onSizePreparedListeners )
328 		{
329 			dlg(width, height, _pixbufLoader);
330 		}
331 	}
332 	
333 	
334 	/**
335 	 * Creates a new pixbuf loader object.
336 	 * Throws: ConstructionException GTK+ fails to create the object.
337 	 */
338 	public this ()
339 	{
340 		// GdkPixbufLoader * gdk_pixbuf_loader_new (void);
341 		auto p = gdk_pixbuf_loader_new();
342 		if(p is null)
343 		{
344 			throw new ConstructionException("null returned by gdk_pixbuf_loader_new()");
345 		}
346 		this(cast(GdkPixbufLoader*) p);
347 	}
348 	
349 	/**
350 	 * Obtains the available information about the format of the
351 	 * currently loading image file.
352 	 * Since 2.2
353 	 * Returns: A GdkPixbufFormat or NULL. The return value is owned by GdkPixbuf and should not be freed. [transfer none]
354 	 */
355 	public PixbufFormat getFormat()
356 	{
357 		// GdkPixbufFormat * gdk_pixbuf_loader_get_format (GdkPixbufLoader *loader);
358 		auto p = gdk_pixbuf_loader_get_format(gdkPixbufLoader);
359 		
360 		if(p is null)
361 		{
362 			return null;
363 		}
364 		
365 		return ObjectG.getDObject!(PixbufFormat)(cast(GdkPixbufFormat*) p);
366 	}
367 	
368 	/**
369 	 * This will cause a pixbuf loader to parse the next count bytes of
370 	 * an image. It will return TRUE if the data was loaded successfully,
371 	 * and FALSE if an error occurred. In the latter case, the loader
372 	 * will be closed, and will not accept further writes. If FALSE is
373 	 * returned, error will be set to an error from the GDK_PIXBUF_ERROR
374 	 * or G_FILE_ERROR domains.
375 	 * Params:
376 	 * buf = Pointer to image data. [array length=count]
377 	 * Returns: TRUE if the write was successful, or FALSE if the loader cannot parse the buffer.
378 	 * Throws: GException on failure.
379 	 */
380 	public int write(char[] buf)
381 	{
382 		// gboolean gdk_pixbuf_loader_write (GdkPixbufLoader *loader,  const guchar *buf,  gsize count,  GError **error);
383 		GError* err = null;
384 		
385 		auto p = gdk_pixbuf_loader_write(gdkPixbufLoader, buf.ptr, cast(int) buf.length, &err);
386 		
387 		if (err !is null)
388 		{
389 			throw new GException( new ErrorG(err) );
390 		}
391 		
392 		return p;
393 	}
394 	
395 	/**
396 	 * This will cause a pixbuf loader to parse a buffer inside a GBytes
397 	 * for an image. It will return TRUE if the data was loaded successfully,
398 	 * and FALSE if an error occurred. In the latter case, the loader
399 	 * will be closed, and will not accept further writes. If FALSE is
400 	 * returned, error will be set to an error from the GDK_PIXBUF_ERROR
401 	 * or G_FILE_ERROR domains.
402 	 * See also: gdk_pixbuf_loader_write()
403 	 * Since 2.30
404 	 * Params:
405 	 * buffer = The image data as a GBytes
406 	 * Returns: TRUE if the write was successful, or FALSE if the loader cannot parse the buffer.
407 	 * Throws: GException on failure.
408 	 */
409 	public int writeBytes(Bytes buffer)
410 	{
411 		// gboolean gdk_pixbuf_loader_write_bytes (GdkPixbufLoader *loader,  GBytes *buffer,  GError **error);
412 		GError* err = null;
413 		
414 		auto p = gdk_pixbuf_loader_write_bytes(gdkPixbufLoader, (buffer is null) ? null : buffer.getBytesStruct(), &err);
415 		
416 		if (err !is null)
417 		{
418 			throw new GException( new ErrorG(err) );
419 		}
420 		
421 		return p;
422 	}
423 	
424 	/**
425 	 * Causes the image to be scaled while it is loaded. The desired
426 	 * image size can be determined relative to the original size of
427 	 * the image by calling gdk_pixbuf_loader_set_size() from a
428 	 * signal handler for the ::size-prepared signal.
429 	 * Attempts to set the desired image size are ignored after the
430 	 * emission of the ::size-prepared signal.
431 	 * Since 2.2
432 	 * Params:
433 	 * width = The desired width of the image being loaded.
434 	 * height = The desired height of the image being loaded.
435 	 */
436 	public void setSize(int width, int height)
437 	{
438 		// void gdk_pixbuf_loader_set_size (GdkPixbufLoader *loader,  int width,  int height);
439 		gdk_pixbuf_loader_set_size(gdkPixbufLoader, width, height);
440 	}
441 	
442 	/**
443 	 * Queries the GdkPixbuf that a pixbuf loader is currently creating.
444 	 * In general it only makes sense to call this function after the
445 	 * "area-prepared" signal has been emitted by the loader; this means
446 	 * that enough data has been read to know the size of the image that
447 	 * will be allocated. If the loader has not received enough data via
448 	 * gdk_pixbuf_loader_write(), then this function returns NULL. The
449 	 * returned pixbuf will be the same in all future calls to the loader,
450 	 * so simply calling g_object_ref() should be sufficient to continue
451 	 * using it. Additionally, if the loader is an animation, it will
452 	 * return the "static image" of the animation
453 	 * (see gdk_pixbuf_animation_get_static_image()).
454 	 * Returns: The GdkPixbuf that the loader is creating, or NULL if not enough data has been read to determine how to create the image buffer. [transfer none]
455 	 */
456 	public Pixbuf getPixbuf()
457 	{
458 		// GdkPixbuf * gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader);
459 		auto p = gdk_pixbuf_loader_get_pixbuf(gdkPixbufLoader);
460 		
461 		if(p is null)
462 		{
463 			return null;
464 		}
465 		
466 		return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p);
467 	}
468 	
469 	/**
470 	 * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating.
471 	 * In general it only makes sense to call this function after the "area-prepared"
472 	 * signal has been emitted by the loader. If the loader doesn't have enough
473 	 * bytes yet (hasn't emitted the "area-prepared" signal) this function will
474 	 * return NULL.
475 	 * Returns: The GdkPixbufAnimation that the loader is loading, or NULL if not enough data has been read to determine the information. [transfer none]
476 	 */
477 	public PixbufAnimation getAnimation()
478 	{
479 		// GdkPixbufAnimation * gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader);
480 		auto p = gdk_pixbuf_loader_get_animation(gdkPixbufLoader);
481 		
482 		if(p is null)
483 		{
484 			return null;
485 		}
486 		
487 		return ObjectG.getDObject!(PixbufAnimation)(cast(GdkPixbufAnimation*) p);
488 	}
489 	
490 	/**
491 	 * Informs a pixbuf loader that no further writes with
492 	 * gdk_pixbuf_loader_write() will occur, so that it can free its
493 	 * internal loading structures. Also, tries to parse any data that
494 	 * hasn't yet been parsed; if the remaining data is partial or
495 	 * corrupt, an error will be returned. If FALSE is returned, error
496 	 * will be set to an error from the GDK_PIXBUF_ERROR or G_FILE_ERROR
497 	 * domains. If you're just cancelling a load rather than expecting it
498 	 * to be finished, passing NULL for error to ignore it is
499 	 * reasonable.
500 	 * Remember that this does not unref the loader, so if you plan not to
501 	 * use it anymore, please g_object_unref() it.
502 	 * Returns: TRUE if all image data written so far was successfully passed out via the update_area signal Signal Details The "area-prepared" signal void user_function (GdkPixbufLoader *loader, gpointer user_data) : Run Last This signal is emitted when the pixbuf loader has allocated the pixbuf in the desired size. After this signal is emitted, applications can call gdk_pixbuf_loader_get_pixbuf() to fetch the partially-loaded pixbuf.
503 	 * Throws: GException on failure.
504 	 */
505 	public int close()
506 	{
507 		// gboolean gdk_pixbuf_loader_close (GdkPixbufLoader *loader,  GError **error);
508 		GError* err = null;
509 		
510 		auto p = gdk_pixbuf_loader_close(gdkPixbufLoader, &err);
511 		
512 		if (err !is null)
513 		{
514 			throw new GException( new ErrorG(err) );
515 		}
516 		
517 		return p;
518 	}
519 }