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 * Conversion parameters: 26 * inFile = GdkPixbufLoader.html 27 * outPack = gdkpixbuf 28 * outFile = PixbufLoader 29 * strct = GdkPixbufLoader 30 * realStrct= 31 * ctorStrct= 32 * clss = PixbufLoader 33 * interf = 34 * class Code: Yes 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - gdk_pixbuf_loader_ 41 * omit structs: 42 * omit prefixes: 43 * omit code: 44 * - gdk_pixbuf_loader_new_with_type 45 * - gdk_pixbuf_loader_new_with_mime_type 46 * omit signals: 47 * imports: 48 * - gdkpixbuf.PixbufFormat 49 * - glib.ErrorG 50 * - glib.GException 51 * - gdk.Pixbuf 52 * - gdkpixbuf.PixbufAnimation 53 * - glib.Str 54 * structWrap: 55 * - GdkPixbuf* -> Pixbuf 56 * - GdkPixbufAnimation* -> PixbufAnimation 57 * - GdkPixbufFormat* -> PixbufFormat 58 * module aliases: 59 * local aliases: 60 * overrides: 61 */ 62 63 module gdkpixbuf.PixbufLoader; 64 65 public import gtkc.gdkpixbuftypes; 66 67 private import gtkc.gdkpixbuf; 68 private import glib.ConstructionException; 69 private import gobject.ObjectG; 70 71 private import gobject.Signals; 72 public import gtkc.gdktypes; 73 74 private import gdkpixbuf.PixbufFormat; 75 private import glib.ErrorG; 76 private import glib.GException; 77 private import gdk.Pixbuf; 78 private import gdkpixbuf.PixbufAnimation; 79 private import glib.Str; 80 81 82 83 private import gobject.ObjectG; 84 85 /** 86 * Description 87 * GdkPixbufLoader provides a way for applications to drive the 88 * process of loading an image, by letting them send the image data 89 * directly to the loader instead of having the loader read the data 90 * from a file. Applications can use this functionality instead of 91 * gdk_pixbuf_new_from_file() or gdk_pixbuf_animation_new_from_file() 92 * when they need to parse image data in 93 * small chunks. For example, it should be used when reading an 94 * image from a (potentially) slow network connection, or when 95 * loading an extremely large file. 96 * To use GdkPixbufLoader to load an image, just create a new one, 97 * and call gdk_pixbuf_loader_write() to send the data to it. When 98 * done, gdk_pixbuf_loader_close() should be called to end the stream 99 * and finalize everything. The loader will emit three important 100 * signals throughout the process. The first, "size_prepared", 101 * will be called as soon as the image has enough information to 102 * determine the size of the image to be used. If you want to scale 103 * the image while loading it, you can call gdk_pixbuf_loader_set_size() 104 * in response to this signal. 105 * The second signal, "area_prepared", 106 * will be called as soon as the pixbuf of the desired has been 107 * allocated. You can obtain it by calling gdk_pixbuf_loader_get_pixbuf(). 108 * If you want to use it, simply ref it. 109 * In addition, no actual information will be passed in yet, so the 110 * pixbuf can be safely filled with any temporary graphics (or an 111 * initial color) as needed. You can also call 112 * gdk_pixbuf_loader_get_pixbuf() later and get the same pixbuf. 113 * The last signal, "area_updated" gets 114 * called every time a region is updated. This way you can update a 115 * partially completed image. Note that you do not know anything 116 * about the completeness of an image from the area updated. For 117 * example, in an interlaced image, you need to make several passes 118 * before the image is done loading. 119 * Loading an animation 120 * Loading an animation is almost as easy as loading an 121 * image. Once the first "area_prepared" signal 122 * has been emitted, you can call gdk_pixbuf_loader_get_animation() 123 * to get the GdkPixbufAnimation struct and gdk_pixbuf_animation_get_iter() 124 * to get an GdkPixbufAnimationIter for displaying it. 125 */ 126 public class PixbufLoader : ObjectG 127 { 128 129 /** the main Gtk struct */ 130 protected GdkPixbufLoader* gdkPixbufLoader; 131 132 133 public GdkPixbufLoader* getPixbufLoaderStruct() 134 { 135 return gdkPixbufLoader; 136 } 137 138 139 /** the main Gtk struct as a void* */ 140 protected override void* getStruct() 141 { 142 return cast(void*)gdkPixbufLoader; 143 } 144 145 /** 146 * Sets our main struct and passes it to the parent class 147 */ 148 public this (GdkPixbufLoader* gdkPixbufLoader) 149 { 150 super(cast(GObject*)gdkPixbufLoader); 151 this.gdkPixbufLoader = gdkPixbufLoader; 152 } 153 154 protected override void setStruct(GObject* obj) 155 { 156 super.setStruct(obj); 157 gdkPixbufLoader = cast(GdkPixbufLoader*)obj; 158 } 159 160 /** 161 * Creates a new pixbuf loader object that always attempts to parse 162 * image data as if it were an image of type image_type or mime_type, instead of 163 * identifying the type automatically. Useful if you want an error if 164 * the image isn't the expected type, for loading image formats 165 * that can't be reliably identified by looking at the data, or if 166 * the user manually forces a specific type. 167 * Params: 168 * type = name of the image format or mime to be loaded with the image 169 */ 170 public this (string type, bool isMimeType=false) 171 { 172 GdkPixbufLoader* p; 173 GError* err = null; 174 175 if ( isMimeType ) 176 { 177 // GdkPixbufLoader* gdk_pixbuf_loader_new_with_mime_type (const char *mime_type, GError **error); 178 p = cast(GdkPixbufLoader*)gdk_pixbuf_loader_new_with_mime_type(Str.toStringz(type), &err); 179 } 180 else 181 { 182 // GdkPixbufLoader* gdk_pixbuf_loader_new_with_type (const char *image_type, GError **error); 183 p = cast(GdkPixbufLoader*)gdk_pixbuf_loader_new_with_type(Str.toStringz(type), &err); 184 } 185 186 if (err !is null) 187 { 188 throw new GException( new ErrorG(err) ); 189 } 190 191 if(p is null) 192 { 193 throw new ConstructionException("null returned by gtk_button_new()"); 194 } 195 196 this(p); 197 } 198 199 /** 200 */ 201 int[string] connectedSignals; 202 203 void delegate(PixbufLoader)[] onAreaPreparedListeners; 204 /** 205 * This signal is emitted when the pixbuf loader has allocated the 206 * pixbuf in the desired size. After this signal is emitted, 207 * applications can call gdk_pixbuf_loader_get_pixbuf() to fetch 208 * the partially-loaded pixbuf. 209 */ 210 void addOnAreaPrepared(void delegate(PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 211 { 212 if ( !("area-prepared" in connectedSignals) ) 213 { 214 Signals.connectData( 215 getStruct(), 216 "area-prepared", 217 cast(GCallback)&callBackAreaPrepared, 218 cast(void*)this, 219 null, 220 connectFlags); 221 connectedSignals["area-prepared"] = 1; 222 } 223 onAreaPreparedListeners ~= dlg; 224 } 225 extern(C) static void callBackAreaPrepared(GdkPixbufLoader* loaderStruct, PixbufLoader _pixbufLoader) 226 { 227 foreach ( void delegate(PixbufLoader) dlg ; _pixbufLoader.onAreaPreparedListeners ) 228 { 229 dlg(_pixbufLoader); 230 } 231 } 232 233 void delegate(gint, gint, gint, gint, PixbufLoader)[] onAreaUpdatedListeners; 234 /** 235 * This signal is emitted when a significant area of the image being 236 * loaded has been updated. Normally it means that a complete 237 * scanline has been read in, but it could be a different area as 238 * well. Applications can use this signal to know when to repaint 239 * areas of an image that is being loaded. 240 */ 241 void addOnAreaUpdated(void delegate(gint, gint, gint, gint, PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 242 { 243 if ( !("area-updated" in connectedSignals) ) 244 { 245 Signals.connectData( 246 getStruct(), 247 "area-updated", 248 cast(GCallback)&callBackAreaUpdated, 249 cast(void*)this, 250 null, 251 connectFlags); 252 connectedSignals["area-updated"] = 1; 253 } 254 onAreaUpdatedListeners ~= dlg; 255 } 256 extern(C) static void callBackAreaUpdated(GdkPixbufLoader* loaderStruct, gint x, gint y, gint width, gint height, PixbufLoader _pixbufLoader) 257 { 258 foreach ( void delegate(gint, gint, gint, gint, PixbufLoader) dlg ; _pixbufLoader.onAreaUpdatedListeners ) 259 { 260 dlg(x, y, width, height, _pixbufLoader); 261 } 262 } 263 264 void delegate(PixbufLoader)[] onClosedListeners; 265 /** 266 * This signal is emitted when gdk_pixbuf_loader_close() is called. 267 * It can be used by different parts of an application to receive 268 * notification when an image loader is closed by the code that 269 * drives it. 270 */ 271 void addOnClosed(void delegate(PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 272 { 273 if ( !("closed" in connectedSignals) ) 274 { 275 Signals.connectData( 276 getStruct(), 277 "closed", 278 cast(GCallback)&callBackClosed, 279 cast(void*)this, 280 null, 281 connectFlags); 282 connectedSignals["closed"] = 1; 283 } 284 onClosedListeners ~= dlg; 285 } 286 extern(C) static void callBackClosed(GdkPixbufLoader* loaderStruct, PixbufLoader _pixbufLoader) 287 { 288 foreach ( void delegate(PixbufLoader) dlg ; _pixbufLoader.onClosedListeners ) 289 { 290 dlg(_pixbufLoader); 291 } 292 } 293 294 void delegate(gint, gint, PixbufLoader)[] onSizePreparedListeners; 295 /** 296 * This signal is emitted when the pixbuf loader has been fed the 297 * initial amount of data that is required to figure out the size 298 * of the image that it will create. Applications can call 299 * gdk_pixbuf_loader_set_size() in response to this signal to set 300 * the desired size to which the image should be scaled. 301 * See Also 302 * gdk_pixbuf_new_from_file(), gdk_pixbuf_animation_new_from_file() 303 */ 304 void addOnSizePrepared(void delegate(gint, gint, PixbufLoader) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 305 { 306 if ( !("size-prepared" in connectedSignals) ) 307 { 308 Signals.connectData( 309 getStruct(), 310 "size-prepared", 311 cast(GCallback)&callBackSizePrepared, 312 cast(void*)this, 313 null, 314 connectFlags); 315 connectedSignals["size-prepared"] = 1; 316 } 317 onSizePreparedListeners ~= dlg; 318 } 319 extern(C) static void callBackSizePrepared(GdkPixbufLoader* loaderStruct, gint width, gint height, PixbufLoader _pixbufLoader) 320 { 321 foreach ( void delegate(gint, gint, PixbufLoader) dlg ; _pixbufLoader.onSizePreparedListeners ) 322 { 323 dlg(width, height, _pixbufLoader); 324 } 325 } 326 327 328 /** 329 * Creates a new pixbuf loader object. 330 * Throws: ConstructionException GTK+ fails to create the object. 331 */ 332 public this () 333 { 334 // GdkPixbufLoader * gdk_pixbuf_loader_new (void); 335 auto p = gdk_pixbuf_loader_new(); 336 if(p is null) 337 { 338 throw new ConstructionException("null returned by gdk_pixbuf_loader_new()"); 339 } 340 this(cast(GdkPixbufLoader*) p); 341 } 342 343 /** 344 * Obtains the available information about the format of the 345 * currently loading image file. 346 * Since 2.2 347 * Returns: A GdkPixbufFormat or NULL. The return value is owned by GdkPixbuf and should not be freed. 348 */ 349 public PixbufFormat getFormat() 350 { 351 // GdkPixbufFormat * gdk_pixbuf_loader_get_format (GdkPixbufLoader *loader); 352 auto p = gdk_pixbuf_loader_get_format(gdkPixbufLoader); 353 354 if(p is null) 355 { 356 return null; 357 } 358 359 return ObjectG.getDObject!(PixbufFormat)(cast(GdkPixbufFormat*) p); 360 } 361 362 /** 363 * This will cause a pixbuf loader to parse the next count bytes of 364 * an image. It will return TRUE if the data was loaded successfully, 365 * and FALSE if an error occurred. In the latter case, the loader 366 * will be closed, and will not accept further writes. If FALSE is 367 * returned, error will be set to an error from the GDK_PIXBUF_ERROR 368 * or G_FILE_ERROR domains. 369 * Params: 370 * buf = Pointer to image data. [array length=count] 371 * Returns: TRUE if the write was successful, or FALSE if the loader cannot parse the buffer. 372 * Throws: GException on failure. 373 */ 374 public int write(char[] buf) 375 { 376 // gboolean gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const guchar *buf, gsize count, GError **error); 377 GError* err = null; 378 379 auto p = gdk_pixbuf_loader_write(gdkPixbufLoader, buf.ptr, cast(int) buf.length, &err); 380 381 if (err !is null) 382 { 383 throw new GException( new ErrorG(err) ); 384 } 385 386 return p; 387 } 388 389 /** 390 * Causes the image to be scaled while it is loaded. The desired 391 * image size can be determined relative to the original size of 392 * the image by calling gdk_pixbuf_loader_set_size() from a 393 * signal handler for the ::size-prepared signal. 394 * Attempts to set the desired image size are ignored after the 395 * emission of the ::size-prepared signal. 396 * Since 2.2 397 * Params: 398 * width = The desired width of the image being loaded. 399 * height = The desired height of the image being loaded. 400 */ 401 public void setSize(int width, int height) 402 { 403 // void gdk_pixbuf_loader_set_size (GdkPixbufLoader *loader, int width, int height); 404 gdk_pixbuf_loader_set_size(gdkPixbufLoader, width, height); 405 } 406 407 /** 408 * Queries the GdkPixbuf that a pixbuf loader is currently creating. 409 * In general it only makes sense to call this function after the 410 * "area-prepared" signal has been emitted by the loader; this means 411 * that enough data has been read to know the size of the image that 412 * will be allocated. If the loader has not received enough data via 413 * gdk_pixbuf_loader_write(), then this function returns NULL. The 414 * returned pixbuf will be the same in all future calls to the loader, 415 * so simply calling g_object_ref() should be sufficient to continue 416 * using it. Additionally, if the loader is an animation, it will 417 * return the "static image" of the animation 418 * (see gdk_pixbuf_animation_get_static_image()). 419 * Returns: The GdkPixbuf that the loader is creating, or NULL if not enough data has been read to determine how to create the image buffer. [transfer none] 420 */ 421 public Pixbuf getPixbuf() 422 { 423 // GdkPixbuf * gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader); 424 auto p = gdk_pixbuf_loader_get_pixbuf(gdkPixbufLoader); 425 426 if(p is null) 427 { 428 return null; 429 } 430 431 return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p); 432 } 433 434 /** 435 * Queries the GdkPixbufAnimation that a pixbuf loader is currently creating. 436 * In general it only makes sense to call this function after the "area-prepared" 437 * signal has been emitted by the loader. If the loader doesn't have enough 438 * bytes yet (hasn't emitted the "area-prepared" signal) this function will 439 * return NULL. 440 * Returns: The GdkPixbufAnimation that the loader is loading, or NULL if not enough data has been read to determine the information. [transfer none] 441 */ 442 public PixbufAnimation getAnimation() 443 { 444 // GdkPixbufAnimation * gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader); 445 auto p = gdk_pixbuf_loader_get_animation(gdkPixbufLoader); 446 447 if(p is null) 448 { 449 return null; 450 } 451 452 return ObjectG.getDObject!(PixbufAnimation)(cast(GdkPixbufAnimation*) p); 453 } 454 455 /** 456 * Informs a pixbuf loader that no further writes with 457 * gdk_pixbuf_loader_write() will occur, so that it can free its 458 * internal loading structures. Also, tries to parse any data that 459 * hasn't yet been parsed; if the remaining data is partial or 460 * corrupt, an error will be returned. If FALSE is returned, error 461 * will be set to an error from the GDK_PIXBUF_ERROR or G_FILE_ERROR 462 * domains. If you're just cancelling a load rather than expecting it 463 * to be finished, passing NULL for error to ignore it is 464 * reasonable. 465 * Remember that this does not unref the loader, so if you plan not to 466 * use it anymore, please g_object_unref() it. 467 * Returns: TRUE if all image data written so far was successfully passed out via the update_area signal Signal Details The "area-prepared" signal void user_function (GdkPixbufLoader *loader, gpointer user_data) : Run Last This signal is emitted when the pixbuf loader has allocated the pixbuf in the desired size. After this signal is emitted, applications can call gdk_pixbuf_loader_get_pixbuf() to fetch the partially-loaded pixbuf. 468 * Throws: GException on failure. 469 */ 470 public int close() 471 { 472 // gboolean gdk_pixbuf_loader_close (GdkPixbufLoader *loader, GError **error); 473 GError* err = null; 474 475 auto p = gdk_pixbuf_loader_close(gdkPixbufLoader, &err); 476 477 if (err !is null) 478 { 479 throw new GException( new ErrorG(err) ); 480 } 481 482 return p; 483 } 484 }