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 module gdkpixbuf.PixbufLoader;
26 
27 private import gdkpixbuf.Pixbuf;
28 private import gdkpixbuf.PixbufAnimation;
29 private import gdkpixbuf.PixbufFormat;
30 private import glib.Bytes;
31 private import glib.ConstructionException;
32 private import glib.ErrorG;
33 private import glib.GException;
34 private import glib.Str;
35 private import gobject.ObjectG;
36 private import gobject.Signals;
37 private import gtkc.gdkpixbuf;
38 public  import gtkc.gdkpixbuftypes;
39 private import std.algorithm;
40 
41 
42 /**
43  * The GdkPixbufLoader struct contains only private
44  * fields.
45  */
46 public class PixbufLoader : ObjectG
47 {
48 	/** the main Gtk struct */
49 	protected GdkPixbufLoader* gdkPixbufLoader;
50 
51 	/** Get the main Gtk struct */
52 	public GdkPixbufLoader* getPixbufLoaderStruct(bool transferOwnership = false)
53 	{
54 		if (transferOwnership)
55 			ownedRef = false;
56 		return gdkPixbufLoader;
57 	}
58 
59 	/** the main Gtk struct as a void* */
60 	protected override void* getStruct()
61 	{
62 		return cast(void*)gdkPixbufLoader;
63 	}
64 
65 	protected override void setStruct(GObject* obj)
66 	{
67 		gdkPixbufLoader = cast(GdkPixbufLoader*)obj;
68 		super.setStruct(obj);
69 	}
70 
71 	/**
72 	 * Sets our main struct and passes it to the parent class.
73 	 */
74 	public this (GdkPixbufLoader* gdkPixbufLoader, bool ownedRef = false)
75 	{
76 		this.gdkPixbufLoader = gdkPixbufLoader;
77 		super(cast(GObject*)gdkPixbufLoader, ownedRef);
78 	}
79 
80 	/**
81 	 * Creates a new pixbuf loader object that always attempts to parse
82 	 * image data as if it were an image of type @image_type, instead of
83 	 * identifying the type automatically. Useful if you want an error if
84 	 * the image isn't the expected type, for loading image formats
85 	 * that can't be reliably identified by looking at the data, or if
86 	 * the user manually forces a specific type.
87 	 *
88 	 * The list of supported image formats depends on what image loaders
89 	 * are installed, but typically "png", "jpeg", "gif", "tiff" and
90 	 * "xpm" are among the supported formats. To obtain the full list of
91 	 * supported image formats, call gdk_pixbuf_format_get_name() on each
92 	 * of the #GdkPixbufFormat structs returned by gdk_pixbuf_get_formats().
93 	 *
94 	 * Params:
95 	 *     imageType  = name of the image format to be loaded with the image
96 	 *     isMimeType = Set to true if type is a mime type
97 	 *
98 	 * Return: A newly-created pixbuf loader.
99 	 *
100 	 * Throws: GException on failure.
101 	 */
102 	public this(string type, bool isMimeType=false)
103 	{
104 		GError* err = null;
105 		GdkPixbufLoader* p;
106 		
107 		if ( isMimeType )
108 		{
109 			p = cast(GdkPixbufLoader*)gdk_pixbuf_loader_new_with_mime_type(Str.toStringz(type), &err);
110 		}
111 		else
112 		{
113 			p = cast(GdkPixbufLoader*)gdk_pixbuf_loader_new_with_type(Str.toStringz(type), &err);
114 		}
115 		
116 		if (err !is null)
117 		{
118 			throw new GException( new ErrorG(err) );
119 		}
120 		
121 		this(cast(GdkPixbufLoader*) p, true);
122 	}
123 
124 	/**
125 	 */
126 
127 	/** */
128 	public static GType getType()
129 	{
130 		return gdk_pixbuf_loader_get_type();
131 	}
132 
133 	/**
134 	 * Creates a new pixbuf loader object.
135 	 *
136 	 * Returns: A newly-created pixbuf loader.
137 	 *
138 	 * Throws: ConstructionException GTK+ fails to create the object.
139 	 */
140 	public this()
141 	{
142 		auto p = gdk_pixbuf_loader_new();
143 		
144 		if(p is null)
145 		{
146 			throw new ConstructionException("null returned by new");
147 		}
148 		
149 		this(cast(GdkPixbufLoader*) p, true);
150 	}
151 
152 	/**
153 	 * Informs a pixbuf loader that no further writes with
154 	 * gdk_pixbuf_loader_write() will occur, so that it can free its
155 	 * internal loading structures. Also, tries to parse any data that
156 	 * hasn't yet been parsed; if the remaining data is partial or
157 	 * corrupt, an error will be returned.  If %FALSE is returned, @error
158 	 * will be set to an error from the #GDK_PIXBUF_ERROR or #G_FILE_ERROR
159 	 * domains. If you're just cancelling a load rather than expecting it
160 	 * to be finished, passing %NULL for @error to ignore it is
161 	 * reasonable.
162 	 *
163 	 * Remember that this does not unref the loader, so if you plan not to
164 	 * use it anymore, please g_object_unref() it.
165 	 *
166 	 * Returns: %TRUE if all image data written so far was successfully
167 	 *     passed out via the update_area signal
168 	 *
169 	 * Throws: GException on failure.
170 	 */
171 	public bool close()
172 	{
173 		GError* err = null;
174 		
175 		auto p = gdk_pixbuf_loader_close(gdkPixbufLoader, &err) != 0;
176 		
177 		if (err !is null)
178 		{
179 			throw new GException( new ErrorG(err) );
180 		}
181 		
182 		return p;
183 	}
184 
185 	/**
186 	 * Queries the #GdkPixbufAnimation that a pixbuf loader is currently creating.
187 	 * In general it only makes sense to call this function after the "area-prepared"
188 	 * signal has been emitted by the loader. If the loader doesn't have enough
189 	 * bytes yet (hasn't emitted the "area-prepared" signal) this function will
190 	 * return %NULL.
191 	 *
192 	 * Returns: The #GdkPixbufAnimation that the loader is loading, or %NULL if
193 	 *     not enough data has been read to determine the information.
194 	 */
195 	public PixbufAnimation getAnimation()
196 	{
197 		auto p = gdk_pixbuf_loader_get_animation(gdkPixbufLoader);
198 		
199 		if(p is null)
200 		{
201 			return null;
202 		}
203 		
204 		return ObjectG.getDObject!(PixbufAnimation)(cast(GdkPixbufAnimation*) p);
205 	}
206 
207 	/**
208 	 * Obtains the available information about the format of the
209 	 * currently loading image file.
210 	 *
211 	 * Returns: A #GdkPixbufFormat or
212 	 *     %NULL. The return value is owned by GdkPixbuf and should not be
213 	 *     freed.
214 	 *
215 	 * Since: 2.2
216 	 */
217 	public PixbufFormat getFormat()
218 	{
219 		auto p = gdk_pixbuf_loader_get_format(gdkPixbufLoader);
220 		
221 		if(p is null)
222 		{
223 			return null;
224 		}
225 		
226 		return ObjectG.getDObject!(PixbufFormat)(cast(GdkPixbufFormat*) p);
227 	}
228 
229 	/**
230 	 * Queries the #GdkPixbuf that a pixbuf loader is currently creating.
231 	 * In general it only makes sense to call this function after the
232 	 * "area-prepared" signal has been emitted by the loader; this means
233 	 * that enough data has been read to know the size of the image that
234 	 * will be allocated.  If the loader has not received enough data via
235 	 * gdk_pixbuf_loader_write(), then this function returns %NULL.  The
236 	 * returned pixbuf will be the same in all future calls to the loader,
237 	 * so simply calling g_object_ref() should be sufficient to continue
238 	 * using it.  Additionally, if the loader is an animation, it will
239 	 * return the "static image" of the animation
240 	 * (see gdk_pixbuf_animation_get_static_image()).
241 	 *
242 	 * Returns: The #GdkPixbuf that the loader is creating, or %NULL if not
243 	 *     enough data has been read to determine how to create the image buffer.
244 	 */
245 	public Pixbuf getPixbuf()
246 	{
247 		auto p = gdk_pixbuf_loader_get_pixbuf(gdkPixbufLoader);
248 		
249 		if(p is null)
250 		{
251 			return null;
252 		}
253 		
254 		return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p);
255 	}
256 
257 	/**
258 	 * Causes the image to be scaled while it is loaded. The desired
259 	 * image size can be determined relative to the original size of
260 	 * the image by calling gdk_pixbuf_loader_set_size() from a
261 	 * signal handler for the ::size-prepared signal.
262 	 *
263 	 * Attempts to set the desired image size  are ignored after the
264 	 * emission of the ::size-prepared signal.
265 	 *
266 	 * Params:
267 	 *     width = The desired width of the image being loaded.
268 	 *     height = The desired height of the image being loaded.
269 	 *
270 	 * Since: 2.2
271 	 */
272 	public void setSize(int width, int height)
273 	{
274 		gdk_pixbuf_loader_set_size(gdkPixbufLoader, width, height);
275 	}
276 
277 	/**
278 	 * This will cause a pixbuf loader to parse the next @count bytes of
279 	 * an image.  It will return %TRUE if the data was loaded successfully,
280 	 * and %FALSE if an error occurred.  In the latter case, the loader
281 	 * will be closed, and will not accept further writes. If %FALSE is
282 	 * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
283 	 * or #G_FILE_ERROR domains.
284 	 *
285 	 * Params:
286 	 *     buf = Pointer to image data.
287 	 *     count = Length of the @buf buffer in bytes.
288 	 *
289 	 * Returns: %TRUE if the write was successful, or %FALSE if the loader
290 	 *     cannot parse the buffer.
291 	 *
292 	 * Throws: GException on failure.
293 	 */
294 	public bool write(char[] buf)
295 	{
296 		GError* err = null;
297 		
298 		auto p = gdk_pixbuf_loader_write(gdkPixbufLoader, buf.ptr, cast(size_t)buf.length, &err) != 0;
299 		
300 		if (err !is null)
301 		{
302 			throw new GException( new ErrorG(err) );
303 		}
304 		
305 		return p;
306 	}
307 
308 	/**
309 	 * This will cause a pixbuf loader to parse a buffer inside a #GBytes
310 	 * for an image.  It will return %TRUE if the data was loaded successfully,
311 	 * and %FALSE if an error occurred.  In the latter case, the loader
312 	 * will be closed, and will not accept further writes. If %FALSE is
313 	 * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
314 	 * or #G_FILE_ERROR domains.
315 	 *
316 	 * See also: gdk_pixbuf_loader_write()
317 	 *
318 	 * Params:
319 	 *     buffer = The image data as a #GBytes
320 	 *
321 	 * Returns: %TRUE if the write was successful, or %FALSE if the loader
322 	 *     cannot parse the buffer.
323 	 *
324 	 * Since: 2.30
325 	 *
326 	 * Throws: GException on failure.
327 	 */
328 	public bool writeBytes(Bytes buffer)
329 	{
330 		GError* err = null;
331 		
332 		auto p = gdk_pixbuf_loader_write_bytes(gdkPixbufLoader, (buffer is null) ? null : buffer.getBytesStruct(), &err) != 0;
333 		
334 		if (err !is null)
335 		{
336 			throw new GException( new ErrorG(err) );
337 		}
338 		
339 		return p;
340 	}
341 
342 	protected class OnAreaPreparedDelegateWrapper
343 	{
344 		void delegate(PixbufLoader) dlg;
345 		gulong handlerId;
346 		
347 		this(void delegate(PixbufLoader) dlg)
348 		{
349 			this.dlg = dlg;
350 			onAreaPreparedListeners ~= this;
351 		}
352 		
353 		void remove(OnAreaPreparedDelegateWrapper source)
354 		{
355 			foreach(index, wrapper; onAreaPreparedListeners)
356 			{
357 				if (wrapper.handlerId == source.handlerId)
358 				{
359 					onAreaPreparedListeners[index] = null;
360 					onAreaPreparedListeners = std.algorithm.remove(onAreaPreparedListeners, index);
361 					break;
362 				}
363 			}
364 		}
365 	}
366 	OnAreaPreparedDelegateWrapper[] onAreaPreparedListeners;
367 
368 	/**
369 	 * This signal is emitted when the pixbuf loader has allocated the
370 	 * pixbuf in the desired size.  After this signal is emitted,
371 	 * applications can call gdk_pixbuf_loader_get_pixbuf() to fetch
372 	 * the partially-loaded pixbuf.
373 	 */
374 	gulong addOnAreaPrepared(void delegate(PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
375 	{
376 		auto wrapper = new OnAreaPreparedDelegateWrapper(dlg);
377 		wrapper.handlerId = Signals.connectData(
378 			this,
379 			"area-prepared",
380 			cast(GCallback)&callBackAreaPrepared,
381 			cast(void*)wrapper,
382 			cast(GClosureNotify)&callBackAreaPreparedDestroy,
383 			connectFlags);
384 		return wrapper.handlerId;
385 	}
386 	
387 	extern(C) static void callBackAreaPrepared(GdkPixbufLoader* pixbufloaderStruct, OnAreaPreparedDelegateWrapper wrapper)
388 	{
389 		wrapper.dlg(wrapper.outer);
390 	}
391 	
392 	extern(C) static void callBackAreaPreparedDestroy(OnAreaPreparedDelegateWrapper wrapper, GClosure* closure)
393 	{
394 		wrapper.remove(wrapper);
395 	}
396 
397 	protected class OnAreaUpdatedDelegateWrapper
398 	{
399 		void delegate(int, int, int, int, PixbufLoader) dlg;
400 		gulong handlerId;
401 		
402 		this(void delegate(int, int, int, int, PixbufLoader) dlg)
403 		{
404 			this.dlg = dlg;
405 			onAreaUpdatedListeners ~= this;
406 		}
407 		
408 		void remove(OnAreaUpdatedDelegateWrapper source)
409 		{
410 			foreach(index, wrapper; onAreaUpdatedListeners)
411 			{
412 				if (wrapper.handlerId == source.handlerId)
413 				{
414 					onAreaUpdatedListeners[index] = null;
415 					onAreaUpdatedListeners = std.algorithm.remove(onAreaUpdatedListeners, index);
416 					break;
417 				}
418 			}
419 		}
420 	}
421 	OnAreaUpdatedDelegateWrapper[] onAreaUpdatedListeners;
422 
423 	/**
424 	 * This signal is emitted when a significant area of the image being
425 	 * loaded has been updated.  Normally it means that a complete
426 	 * scanline has been read in, but it could be a different area as
427 	 * well.  Applications can use this signal to know when to repaint
428 	 * areas of an image that is being loaded.
429 	 *
430 	 * Params:
431 	 *     x = X offset of upper-left corner of the updated area.
432 	 *     y = Y offset of upper-left corner of the updated area.
433 	 *     width = Width of updated area.
434 	 *     height = Height of updated area.
435 	 */
436 	gulong addOnAreaUpdated(void delegate(int, int, int, int, PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
437 	{
438 		auto wrapper = new OnAreaUpdatedDelegateWrapper(dlg);
439 		wrapper.handlerId = Signals.connectData(
440 			this,
441 			"area-updated",
442 			cast(GCallback)&callBackAreaUpdated,
443 			cast(void*)wrapper,
444 			cast(GClosureNotify)&callBackAreaUpdatedDestroy,
445 			connectFlags);
446 		return wrapper.handlerId;
447 	}
448 	
449 	extern(C) static void callBackAreaUpdated(GdkPixbufLoader* pixbufloaderStruct, int x, int y, int width, int height, OnAreaUpdatedDelegateWrapper wrapper)
450 	{
451 		wrapper.dlg(x, y, width, height, wrapper.outer);
452 	}
453 	
454 	extern(C) static void callBackAreaUpdatedDestroy(OnAreaUpdatedDelegateWrapper wrapper, GClosure* closure)
455 	{
456 		wrapper.remove(wrapper);
457 	}
458 
459 	protected class OnClosedDelegateWrapper
460 	{
461 		void delegate(PixbufLoader) dlg;
462 		gulong handlerId;
463 		
464 		this(void delegate(PixbufLoader) dlg)
465 		{
466 			this.dlg = dlg;
467 			onClosedListeners ~= this;
468 		}
469 		
470 		void remove(OnClosedDelegateWrapper source)
471 		{
472 			foreach(index, wrapper; onClosedListeners)
473 			{
474 				if (wrapper.handlerId == source.handlerId)
475 				{
476 					onClosedListeners[index] = null;
477 					onClosedListeners = std.algorithm.remove(onClosedListeners, index);
478 					break;
479 				}
480 			}
481 		}
482 	}
483 	OnClosedDelegateWrapper[] onClosedListeners;
484 
485 	/**
486 	 * This signal is emitted when gdk_pixbuf_loader_close() is called.
487 	 * It can be used by different parts of an application to receive
488 	 * notification when an image loader is closed by the code that
489 	 * drives it.
490 	 */
491 	gulong addOnClosed(void delegate(PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
492 	{
493 		auto wrapper = new OnClosedDelegateWrapper(dlg);
494 		wrapper.handlerId = Signals.connectData(
495 			this,
496 			"closed",
497 			cast(GCallback)&callBackClosed,
498 			cast(void*)wrapper,
499 			cast(GClosureNotify)&callBackClosedDestroy,
500 			connectFlags);
501 		return wrapper.handlerId;
502 	}
503 	
504 	extern(C) static void callBackClosed(GdkPixbufLoader* pixbufloaderStruct, OnClosedDelegateWrapper wrapper)
505 	{
506 		wrapper.dlg(wrapper.outer);
507 	}
508 	
509 	extern(C) static void callBackClosedDestroy(OnClosedDelegateWrapper wrapper, GClosure* closure)
510 	{
511 		wrapper.remove(wrapper);
512 	}
513 
514 	protected class OnSizePreparedDelegateWrapper
515 	{
516 		void delegate(int, int, PixbufLoader) dlg;
517 		gulong handlerId;
518 		
519 		this(void delegate(int, int, PixbufLoader) dlg)
520 		{
521 			this.dlg = dlg;
522 			onSizePreparedListeners ~= this;
523 		}
524 		
525 		void remove(OnSizePreparedDelegateWrapper source)
526 		{
527 			foreach(index, wrapper; onSizePreparedListeners)
528 			{
529 				if (wrapper.handlerId == source.handlerId)
530 				{
531 					onSizePreparedListeners[index] = null;
532 					onSizePreparedListeners = std.algorithm.remove(onSizePreparedListeners, index);
533 					break;
534 				}
535 			}
536 		}
537 	}
538 	OnSizePreparedDelegateWrapper[] onSizePreparedListeners;
539 
540 	/**
541 	 * This signal is emitted when the pixbuf loader has been fed the
542 	 * initial amount of data that is required to figure out the size
543 	 * of the image that it will create.  Applications can call
544 	 * gdk_pixbuf_loader_set_size() in response to this signal to set
545 	 * the desired size to which the image should be scaled.
546 	 *
547 	 * Params:
548 	 *     width = the original width of the image
549 	 *     height = the original height of the image
550 	 */
551 	gulong addOnSizePrepared(void delegate(int, int, PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
552 	{
553 		auto wrapper = new OnSizePreparedDelegateWrapper(dlg);
554 		wrapper.handlerId = Signals.connectData(
555 			this,
556 			"size-prepared",
557 			cast(GCallback)&callBackSizePrepared,
558 			cast(void*)wrapper,
559 			cast(GClosureNotify)&callBackSizePreparedDestroy,
560 			connectFlags);
561 		return wrapper.handlerId;
562 	}
563 	
564 	extern(C) static void callBackSizePrepared(GdkPixbufLoader* pixbufloaderStruct, int width, int height, OnSizePreparedDelegateWrapper wrapper)
565 	{
566 		wrapper.dlg(width, height, wrapper.outer);
567 	}
568 	
569 	extern(C) static void callBackSizePreparedDestroy(OnSizePreparedDelegateWrapper wrapper, GClosure* closure)
570 	{
571 		wrapper.remove(wrapper);
572 	}
573 }