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 	/** */
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 	 * Return: 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 	 * Return: %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 	 * Return: 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 	 * Return: 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 	 * Return: 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 	 * Return: %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 	 * Return: %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 	int[string] connectedSignals;
341 
342 	void delegate(PixbufLoader)[] onAreaPreparedListeners;
343 	/**
344 	 * This signal is emitted when the pixbuf loader has allocated the
345 	 * pixbuf in the desired size.  After this signal is emitted,
346 	 * applications can call gdk_pixbuf_loader_get_pixbuf() to fetch
347 	 * the partially-loaded pixbuf.
348 	 */
349 	void addOnAreaPrepared(void delegate(PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
350 	{
351 		if ( "area-prepared" !in connectedSignals )
352 		{
353 			Signals.connectData(
354 				this,
355 				"area-prepared",
356 				cast(GCallback)&callBackAreaPrepared,
357 				cast(void*)this,
358 				null,
359 				connectFlags);
360 			connectedSignals["area-prepared"] = 1;
361 		}
362 		onAreaPreparedListeners ~= dlg;
363 	}
364 	extern(C) static void callBackAreaPrepared(GdkPixbufLoader* pixbufloaderStruct, PixbufLoader _pixbufloader)
365 	{
366 		foreach ( void delegate(PixbufLoader) dlg; _pixbufloader.onAreaPreparedListeners )
367 		{
368 			dlg(_pixbufloader);
369 		}
370 	}
371 
372 	void delegate(int, int, int, int, PixbufLoader)[] onAreaUpdatedListeners;
373 	/**
374 	 * This signal is emitted when a significant area of the image being
375 	 * loaded has been updated.  Normally it means that a complete
376 	 * scanline has been read in, but it could be a different area as
377 	 * well.  Applications can use this signal to know when to repaint
378 	 * areas of an image that is being loaded.
379 	 *
380 	 * Params:
381 	 *     x = X offset of upper-left corner of the updated area.
382 	 *     y = Y offset of upper-left corner of the updated area.
383 	 *     width = Width of updated area.
384 	 *     height = Height of updated area.
385 	 */
386 	void addOnAreaUpdated(void delegate(int, int, int, int, PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
387 	{
388 		if ( "area-updated" !in connectedSignals )
389 		{
390 			Signals.connectData(
391 				this,
392 				"area-updated",
393 				cast(GCallback)&callBackAreaUpdated,
394 				cast(void*)this,
395 				null,
396 				connectFlags);
397 			connectedSignals["area-updated"] = 1;
398 		}
399 		onAreaUpdatedListeners ~= dlg;
400 	}
401 	extern(C) static void callBackAreaUpdated(GdkPixbufLoader* pixbufloaderStruct, int x, int y, int width, int height, PixbufLoader _pixbufloader)
402 	{
403 		foreach ( void delegate(int, int, int, int, PixbufLoader) dlg; _pixbufloader.onAreaUpdatedListeners )
404 		{
405 			dlg(x, y, width, height, _pixbufloader);
406 		}
407 	}
408 
409 	void delegate(PixbufLoader)[] onClosedListeners;
410 	/**
411 	 * This signal is emitted when gdk_pixbuf_loader_close() is called.
412 	 * It can be used by different parts of an application to receive
413 	 * notification when an image loader is closed by the code that
414 	 * drives it.
415 	 */
416 	void addOnClosed(void delegate(PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
417 	{
418 		if ( "closed" !in connectedSignals )
419 		{
420 			Signals.connectData(
421 				this,
422 				"closed",
423 				cast(GCallback)&callBackClosed,
424 				cast(void*)this,
425 				null,
426 				connectFlags);
427 			connectedSignals["closed"] = 1;
428 		}
429 		onClosedListeners ~= dlg;
430 	}
431 	extern(C) static void callBackClosed(GdkPixbufLoader* pixbufloaderStruct, PixbufLoader _pixbufloader)
432 	{
433 		foreach ( void delegate(PixbufLoader) dlg; _pixbufloader.onClosedListeners )
434 		{
435 			dlg(_pixbufloader);
436 		}
437 	}
438 
439 	void delegate(int, int, PixbufLoader)[] onSizePreparedListeners;
440 	/**
441 	 * This signal is emitted when the pixbuf loader has been fed the
442 	 * initial amount of data that is required to figure out the size
443 	 * of the image that it will create.  Applications can call
444 	 * gdk_pixbuf_loader_set_size() in response to this signal to set
445 	 * the desired size to which the image should be scaled.
446 	 *
447 	 * Params:
448 	 *     width = the original width of the image
449 	 *     height = the original height of the image
450 	 */
451 	void addOnSizePrepared(void delegate(int, int, PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
452 	{
453 		if ( "size-prepared" !in connectedSignals )
454 		{
455 			Signals.connectData(
456 				this,
457 				"size-prepared",
458 				cast(GCallback)&callBackSizePrepared,
459 				cast(void*)this,
460 				null,
461 				connectFlags);
462 			connectedSignals["size-prepared"] = 1;
463 		}
464 		onSizePreparedListeners ~= dlg;
465 	}
466 	extern(C) static void callBackSizePrepared(GdkPixbufLoader* pixbufloaderStruct, int width, int height, PixbufLoader _pixbufloader)
467 	{
468 		foreach ( void delegate(int, int, PixbufLoader) dlg; _pixbufloader.onSizePreparedListeners )
469 		{
470 			dlg(width, height, _pixbufloader);
471 		}
472 	}
473 }