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 public  import gtkc.gdktypes;
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 	public static GType getType()
126 	{
127 		return gdk_pixbuf_loader_get_type();
128 	}
129 
130 	/**
131 	 * Creates a new pixbuf loader object.
132 	 *
133 	 * Return: A newly-created pixbuf loader.
134 	 *
135 	 * Throws: ConstructionException GTK+ fails to create the object.
136 	 */
137 	public this()
138 	{
139 		auto p = gdk_pixbuf_loader_new();
140 		
141 		if(p is null)
142 		{
143 			throw new ConstructionException("null returned by new");
144 		}
145 		
146 		this(cast(GdkPixbufLoader*) p, true);
147 	}
148 
149 	/**
150 	 * Informs a pixbuf loader that no further writes with
151 	 * gdk_pixbuf_loader_write() will occur, so that it can free its
152 	 * internal loading structures. Also, tries to parse any data that
153 	 * hasn't yet been parsed; if the remaining data is partial or
154 	 * corrupt, an error will be returned.  If %FALSE is returned, @error
155 	 * will be set to an error from the #GDK_PIXBUF_ERROR or #G_FILE_ERROR
156 	 * domains. If you're just cancelling a load rather than expecting it
157 	 * to be finished, passing %NULL for @error to ignore it is
158 	 * reasonable.
159 	 *
160 	 * Remember that this does not unref the loader, so if you plan not to
161 	 * use it anymore, please g_object_unref() it.
162 	 *
163 	 * Return: %TRUE if all image data written so far was successfully
164 	 *     passed out via the update_area signal
165 	 *
166 	 * Throws: GException on failure.
167 	 */
168 	public bool close()
169 	{
170 		GError* err = null;
171 		
172 		auto p = gdk_pixbuf_loader_close(gdkPixbufLoader, &err) != 0;
173 		
174 		if (err !is null)
175 		{
176 			throw new GException( new ErrorG(err) );
177 		}
178 		
179 		return p;
180 	}
181 
182 	/**
183 	 * Queries the #GdkPixbufAnimation that a pixbuf loader is currently creating.
184 	 * In general it only makes sense to call this function after the "area-prepared"
185 	 * signal has been emitted by the loader. If the loader doesn't have enough
186 	 * bytes yet (hasn't emitted the "area-prepared" signal) this function will
187 	 * return %NULL.
188 	 *
189 	 * Return: The #GdkPixbufAnimation that the loader is loading, or %NULL if
190 	 *     not enough data has been read to determine the information.
191 	 */
192 	public PixbufAnimation getAnimation()
193 	{
194 		auto p = gdk_pixbuf_loader_get_animation(gdkPixbufLoader);
195 		
196 		if(p is null)
197 		{
198 			return null;
199 		}
200 		
201 		return ObjectG.getDObject!(PixbufAnimation)(cast(GdkPixbufAnimation*) p);
202 	}
203 
204 	/**
205 	 * Obtains the available information about the format of the
206 	 * currently loading image file.
207 	 *
208 	 * Return: A #GdkPixbufFormat or
209 	 *     %NULL. The return value is owned by GdkPixbuf and should not be
210 	 *     freed.
211 	 *
212 	 * Since: 2.2
213 	 */
214 	public PixbufFormat getFormat()
215 	{
216 		auto p = gdk_pixbuf_loader_get_format(gdkPixbufLoader);
217 		
218 		if(p is null)
219 		{
220 			return null;
221 		}
222 		
223 		return ObjectG.getDObject!(PixbufFormat)(cast(GdkPixbufFormat*) p);
224 	}
225 
226 	/**
227 	 * Queries the #GdkPixbuf that a pixbuf loader is currently creating.
228 	 * In general it only makes sense to call this function after the
229 	 * "area-prepared" signal has been emitted by the loader; this means
230 	 * that enough data has been read to know the size of the image that
231 	 * will be allocated.  If the loader has not received enough data via
232 	 * gdk_pixbuf_loader_write(), then this function returns %NULL.  The
233 	 * returned pixbuf will be the same in all future calls to the loader,
234 	 * so simply calling g_object_ref() should be sufficient to continue
235 	 * using it.  Additionally, if the loader is an animation, it will
236 	 * return the "static image" of the animation
237 	 * (see gdk_pixbuf_animation_get_static_image()).
238 	 *
239 	 * Return: The #GdkPixbuf that the loader is creating, or %NULL if not
240 	 *     enough data has been read to determine how to create the image buffer.
241 	 */
242 	public Pixbuf getPixbuf()
243 	{
244 		auto p = gdk_pixbuf_loader_get_pixbuf(gdkPixbufLoader);
245 		
246 		if(p is null)
247 		{
248 			return null;
249 		}
250 		
251 		return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p);
252 	}
253 
254 	/**
255 	 * Causes the image to be scaled while it is loaded. The desired
256 	 * image size can be determined relative to the original size of
257 	 * the image by calling gdk_pixbuf_loader_set_size() from a
258 	 * signal handler for the ::size-prepared signal.
259 	 *
260 	 * Attempts to set the desired image size  are ignored after the
261 	 * emission of the ::size-prepared signal.
262 	 *
263 	 * Params:
264 	 *     width = The desired width of the image being loaded.
265 	 *     height = The desired height of the image being loaded.
266 	 *
267 	 * Since: 2.2
268 	 */
269 	public void setSize(int width, int height)
270 	{
271 		gdk_pixbuf_loader_set_size(gdkPixbufLoader, width, height);
272 	}
273 
274 	/**
275 	 * This will cause a pixbuf loader to parse the next @count bytes of
276 	 * an image.  It will return %TRUE if the data was loaded successfully,
277 	 * and %FALSE if an error occurred.  In the latter case, the loader
278 	 * will be closed, and will not accept further writes. If %FALSE is
279 	 * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
280 	 * or #G_FILE_ERROR domains.
281 	 *
282 	 * Params:
283 	 *     buf = Pointer to image data.
284 	 *     count = Length of the @buf buffer in bytes.
285 	 *
286 	 * Return: %TRUE if the write was successful, or %FALSE if the loader
287 	 *     cannot parse the buffer.
288 	 *
289 	 * Throws: GException on failure.
290 	 */
291 	public bool write(char[] buf)
292 	{
293 		GError* err = null;
294 		
295 		auto p = gdk_pixbuf_loader_write(gdkPixbufLoader, buf.ptr, cast(size_t)buf.length, &err) != 0;
296 		
297 		if (err !is null)
298 		{
299 			throw new GException( new ErrorG(err) );
300 		}
301 		
302 		return p;
303 	}
304 
305 	/**
306 	 * This will cause a pixbuf loader to parse a buffer inside a #GBytes
307 	 * for an image.  It will return %TRUE if the data was loaded successfully,
308 	 * and %FALSE if an error occurred.  In the latter case, the loader
309 	 * will be closed, and will not accept further writes. If %FALSE is
310 	 * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
311 	 * or #G_FILE_ERROR domains.
312 	 *
313 	 * See also: gdk_pixbuf_loader_write()
314 	 *
315 	 * Params:
316 	 *     buffer = The image data as a #GBytes
317 	 *
318 	 * Return: %TRUE if the write was successful, or %FALSE if the loader
319 	 *     cannot parse the buffer.
320 	 *
321 	 * Since: 2.30
322 	 *
323 	 * Throws: GException on failure.
324 	 */
325 	public bool writeBytes(Bytes buffer)
326 	{
327 		GError* err = null;
328 		
329 		auto p = gdk_pixbuf_loader_write_bytes(gdkPixbufLoader, (buffer is null) ? null : buffer.getBytesStruct(), &err) != 0;
330 		
331 		if (err !is null)
332 		{
333 			throw new GException( new ErrorG(err) );
334 		}
335 		
336 		return p;
337 	}
338 
339 	int[string] connectedSignals;
340 
341 	void delegate(PixbufLoader)[] onAreaPreparedListeners;
342 	/**
343 	 * This signal is emitted when the pixbuf loader has allocated the
344 	 * pixbuf in the desired size.  After this signal is emitted,
345 	 * applications can call gdk_pixbuf_loader_get_pixbuf() to fetch
346 	 * the partially-loaded pixbuf.
347 	 */
348 	void addOnAreaPrepared(void delegate(PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
349 	{
350 		if ( "area-prepared" !in connectedSignals )
351 		{
352 			Signals.connectData(
353 				this,
354 				"area-prepared",
355 				cast(GCallback)&callBackAreaPrepared,
356 				cast(void*)this,
357 				null,
358 				connectFlags);
359 			connectedSignals["area-prepared"] = 1;
360 		}
361 		onAreaPreparedListeners ~= dlg;
362 	}
363 	extern(C) static void callBackAreaPrepared(GdkPixbufLoader* pixbufloaderStruct, PixbufLoader _pixbufloader)
364 	{
365 		foreach ( void delegate(PixbufLoader) dlg; _pixbufloader.onAreaPreparedListeners )
366 		{
367 			dlg(_pixbufloader);
368 		}
369 	}
370 
371 	void delegate(int, int, int, int, PixbufLoader)[] onAreaUpdatedListeners;
372 	/**
373 	 * This signal is emitted when a significant area of the image being
374 	 * loaded has been updated.  Normally it means that a complete
375 	 * scanline has been read in, but it could be a different area as
376 	 * well.  Applications can use this signal to know when to repaint
377 	 * areas of an image that is being loaded.
378 	 *
379 	 * Params:
380 	 *     x = X offset of upper-left corner of the updated area.
381 	 *     y = Y offset of upper-left corner of the updated area.
382 	 *     width = Width of updated area.
383 	 *     height = Height of updated area.
384 	 */
385 	void addOnAreaUpdated(void delegate(int, int, int, int, PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
386 	{
387 		if ( "area-updated" !in connectedSignals )
388 		{
389 			Signals.connectData(
390 				this,
391 				"area-updated",
392 				cast(GCallback)&callBackAreaUpdated,
393 				cast(void*)this,
394 				null,
395 				connectFlags);
396 			connectedSignals["area-updated"] = 1;
397 		}
398 		onAreaUpdatedListeners ~= dlg;
399 	}
400 	extern(C) static void callBackAreaUpdated(GdkPixbufLoader* pixbufloaderStruct, int x, int y, int width, int height, PixbufLoader _pixbufloader)
401 	{
402 		foreach ( void delegate(int, int, int, int, PixbufLoader) dlg; _pixbufloader.onAreaUpdatedListeners )
403 		{
404 			dlg(x, y, width, height, _pixbufloader);
405 		}
406 	}
407 
408 	void delegate(PixbufLoader)[] onClosedListeners;
409 	/**
410 	 * This signal is emitted when gdk_pixbuf_loader_close() is called.
411 	 * It can be used by different parts of an application to receive
412 	 * notification when an image loader is closed by the code that
413 	 * drives it.
414 	 */
415 	void addOnClosed(void delegate(PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
416 	{
417 		if ( "closed" !in connectedSignals )
418 		{
419 			Signals.connectData(
420 				this,
421 				"closed",
422 				cast(GCallback)&callBackClosed,
423 				cast(void*)this,
424 				null,
425 				connectFlags);
426 			connectedSignals["closed"] = 1;
427 		}
428 		onClosedListeners ~= dlg;
429 	}
430 	extern(C) static void callBackClosed(GdkPixbufLoader* pixbufloaderStruct, PixbufLoader _pixbufloader)
431 	{
432 		foreach ( void delegate(PixbufLoader) dlg; _pixbufloader.onClosedListeners )
433 		{
434 			dlg(_pixbufloader);
435 		}
436 	}
437 
438 	void delegate(int, int, PixbufLoader)[] onSizePreparedListeners;
439 	/**
440 	 * This signal is emitted when the pixbuf loader has been fed the
441 	 * initial amount of data that is required to figure out the size
442 	 * of the image that it will create.  Applications can call
443 	 * gdk_pixbuf_loader_set_size() in response to this signal to set
444 	 * the desired size to which the image should be scaled.
445 	 *
446 	 * Params:
447 	 *     width = the original width of the image
448 	 *     height = the original height of the image
449 	 */
450 	void addOnSizePrepared(void delegate(int, int, PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
451 	{
452 		if ( "size-prepared" !in connectedSignals )
453 		{
454 			Signals.connectData(
455 				this,
456 				"size-prepared",
457 				cast(GCallback)&callBackSizePrepared,
458 				cast(void*)this,
459 				null,
460 				connectFlags);
461 			connectedSignals["size-prepared"] = 1;
462 		}
463 		onSizePreparedListeners ~= dlg;
464 	}
465 	extern(C) static void callBackSizePrepared(GdkPixbufLoader* pixbufloaderStruct, int width, int height, PixbufLoader _pixbufloader)
466 	{
467 		foreach ( void delegate(int, int, PixbufLoader) dlg; _pixbufloader.onSizePreparedListeners )
468 		{
469 			dlg(width, height, _pixbufloader);
470 		}
471 	}
472 }