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