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 gdkpixbuf.c.functions;
31 public  import gdkpixbuf.c.types;
32 private import glib.Bytes;
33 private import glib.ConstructionException;
34 private import glib.ErrorG;
35 private import glib.GException;
36 private import glib.Str;
37 private import gobject.ObjectG;
38 private import gobject.Signals;
39 public  import gtkc.gdkpixbuftypes;
40 private import std.algorithm;
41 
42 
43 /**
44  * The GdkPixbufLoader struct contains only private
45  * fields.
46  */
47 public class PixbufLoader : ObjectG
48 {
49 	/** the main Gtk struct */
50 	protected GdkPixbufLoader* gdkPixbufLoader;
51 
52 	/** Get the main Gtk struct */
53 	public GdkPixbufLoader* getPixbufLoaderStruct(bool transferOwnership = false)
54 	{
55 		if (transferOwnership)
56 			ownedRef = false;
57 		return gdkPixbufLoader;
58 	}
59 
60 	/** the main Gtk struct as a void* */
61 	protected override void* getStruct()
62 	{
63 		return cast(void*)gdkPixbufLoader;
64 	}
65 
66 	protected override void setStruct(GObject* obj)
67 	{
68 		gdkPixbufLoader = cast(GdkPixbufLoader*)obj;
69 		super.setStruct(obj);
70 	}
71 
72 	/**
73 	 * Sets our main struct and passes it to the parent class.
74 	 */
75 	public this (GdkPixbufLoader* gdkPixbufLoader, bool ownedRef = false)
76 	{
77 		this.gdkPixbufLoader = gdkPixbufLoader;
78 		super(cast(GObject*)gdkPixbufLoader, ownedRef);
79 	}
80 
81 	/**
82 	 * Creates a new pixbuf loader object that always attempts to parse
83 	 * image data as if it were an image of type @image_type, instead of
84 	 * identifying the type automatically. Useful if you want an error if
85 	 * the image isn't the expected type, for loading image formats
86 	 * that can't be reliably identified by looking at the data, or if
87 	 * the user manually forces a specific type.
88 	 *
89 	 * The list of supported image formats depends on what image loaders
90 	 * are installed, but typically "png", "jpeg", "gif", "tiff" and
91 	 * "xpm" are among the supported formats. To obtain the full list of
92 	 * supported image formats, call gdk_pixbuf_format_get_name() on each
93 	 * of the #GdkPixbufFormat structs returned by gdk_pixbuf_get_formats().
94 	 *
95 	 * Params:
96 	 *     imageType  = name of the image format to be loaded with the image
97 	 *     isMimeType = Set to true if type is a mime type
98 	 *
99 	 * Return: A newly-created pixbuf loader.
100 	 *
101 	 * Throws: GException on failure.
102 	 */
103 	public this(string type, bool isMimeType=false)
104 	{
105 		GError* err = null;
106 		GdkPixbufLoader* p;
107 
108 		if ( isMimeType )
109 		{
110 			p = cast(GdkPixbufLoader*)gdk_pixbuf_loader_new_with_mime_type(Str.toStringz(type), &err);
111 		}
112 		else
113 		{
114 			p = cast(GdkPixbufLoader*)gdk_pixbuf_loader_new_with_type(Str.toStringz(type), &err);
115 		}
116 
117 		if (err !is null)
118 		{
119 			throw new GException( new ErrorG(err) );
120 		}
121 
122 		this(cast(GdkPixbufLoader*) p, true);
123 	}
124 
125 	/**
126 	 */
127 
128 	/** */
129 	public static GType getType()
130 	{
131 		return gdk_pixbuf_loader_get_type();
132 	}
133 
134 	/**
135 	 * Creates a new pixbuf loader object.
136 	 *
137 	 * Returns: A newly-created pixbuf loader.
138 	 *
139 	 * Throws: ConstructionException GTK+ fails to create the object.
140 	 */
141 	public this()
142 	{
143 		auto p = gdk_pixbuf_loader_new();
144 
145 		if(p is null)
146 		{
147 			throw new ConstructionException("null returned by new");
148 		}
149 
150 		this(cast(GdkPixbufLoader*) p, true);
151 	}
152 
153 	/**
154 	 * Informs a pixbuf loader that no further writes with
155 	 * gdk_pixbuf_loader_write() will occur, so that it can free its
156 	 * internal loading structures. Also, tries to parse any data that
157 	 * hasn't yet been parsed; if the remaining data is partial or
158 	 * corrupt, an error will be returned.  If %FALSE is returned, @error
159 	 * will be set to an error from the #GDK_PIXBUF_ERROR or #G_FILE_ERROR
160 	 * domains. If you're just cancelling a load rather than expecting it
161 	 * to be finished, passing %NULL for @error to ignore it is
162 	 * reasonable.
163 	 *
164 	 * Remember that this does not unref the loader, so if you plan not to
165 	 * use it anymore, please g_object_unref() it.
166 	 *
167 	 * Returns: %TRUE if all image data written so far was successfully
168 	 *     passed out via the update_area signal
169 	 *
170 	 * Throws: GException on failure.
171 	 */
172 	public bool close()
173 	{
174 		GError* err = null;
175 
176 		auto p = gdk_pixbuf_loader_close(gdkPixbufLoader, &err) != 0;
177 
178 		if (err !is null)
179 		{
180 			throw new GException( new ErrorG(err) );
181 		}
182 
183 		return p;
184 	}
185 
186 	/**
187 	 * Queries the #GdkPixbufAnimation that a pixbuf loader is currently creating.
188 	 * In general it only makes sense to call this function after the "area-prepared"
189 	 * signal has been emitted by the loader. If the loader doesn't have enough
190 	 * bytes yet (hasn't emitted the "area-prepared" signal) this function will
191 	 * return %NULL.
192 	 *
193 	 * Returns: The #GdkPixbufAnimation that the loader is loading, or %NULL if
194 	 *     not enough data has been read to determine the information.
195 	 */
196 	public PixbufAnimation getAnimation()
197 	{
198 		auto p = gdk_pixbuf_loader_get_animation(gdkPixbufLoader);
199 
200 		if(p is null)
201 		{
202 			return null;
203 		}
204 
205 		return ObjectG.getDObject!(PixbufAnimation)(cast(GdkPixbufAnimation*) p);
206 	}
207 
208 	/**
209 	 * Obtains the available information about the format of the
210 	 * currently loading image file.
211 	 *
212 	 * Returns: A #GdkPixbufFormat or
213 	 *     %NULL. The return value is owned by GdkPixbuf and should not be
214 	 *     freed.
215 	 *
216 	 * Since: 2.2
217 	 */
218 	public PixbufFormat getFormat()
219 	{
220 		auto p = gdk_pixbuf_loader_get_format(gdkPixbufLoader);
221 
222 		if(p is null)
223 		{
224 			return null;
225 		}
226 
227 		return ObjectG.getDObject!(PixbufFormat)(cast(GdkPixbufFormat*) p);
228 	}
229 
230 	/**
231 	 * Queries the #GdkPixbuf that a pixbuf loader is currently creating.
232 	 * In general it only makes sense to call this function after the
233 	 * "area-prepared" signal has been emitted by the loader; this means
234 	 * that enough data has been read to know the size of the image that
235 	 * will be allocated.  If the loader has not received enough data via
236 	 * gdk_pixbuf_loader_write(), then this function returns %NULL.  The
237 	 * returned pixbuf will be the same in all future calls to the loader,
238 	 * so simply calling g_object_ref() should be sufficient to continue
239 	 * using it.  Additionally, if the loader is an animation, it will
240 	 * return the "static image" of the animation
241 	 * (see gdk_pixbuf_animation_get_static_image()).
242 	 *
243 	 * Returns: The #GdkPixbuf that the loader is creating, or %NULL if not
244 	 *     enough data has been read to determine how to create the image buffer.
245 	 */
246 	public Pixbuf getPixbuf()
247 	{
248 		auto p = gdk_pixbuf_loader_get_pixbuf(gdkPixbufLoader);
249 
250 		if(p is null)
251 		{
252 			return null;
253 		}
254 
255 		return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p);
256 	}
257 
258 	/**
259 	 * Causes the image to be scaled while it is loaded. The desired
260 	 * image size can be determined relative to the original size of
261 	 * the image by calling gdk_pixbuf_loader_set_size() from a
262 	 * signal handler for the ::size-prepared signal.
263 	 *
264 	 * Attempts to set the desired image size  are ignored after the
265 	 * emission of the ::size-prepared signal.
266 	 *
267 	 * Params:
268 	 *     width = The desired width of the image being loaded.
269 	 *     height = The desired height of the image being loaded.
270 	 *
271 	 * Since: 2.2
272 	 */
273 	public void setSize(int width, int height)
274 	{
275 		gdk_pixbuf_loader_set_size(gdkPixbufLoader, width, height);
276 	}
277 
278 	/**
279 	 * This will cause a pixbuf loader to parse the next @count bytes of
280 	 * an image.  It will return %TRUE if the data was loaded successfully,
281 	 * and %FALSE if an error occurred.  In the latter case, the loader
282 	 * will be closed, and will not accept further writes. If %FALSE is
283 	 * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
284 	 * or #G_FILE_ERROR domains.
285 	 *
286 	 * Params:
287 	 *     buf = Pointer to image data.
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 }