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 gtk.Image; 26 27 private import cairo.Surface; 28 private import gdkpixbuf.Pixbuf; 29 private import gdkpixbuf.PixbufAnimation; 30 private import gio.IconIF; 31 private import glib.ConstructionException; 32 private import glib.Str; 33 private import gobject.ObjectG; 34 private import gtk.IconSet; 35 private import gtk.Misc; 36 private import gtk.Widget; 37 private import gtk.c.functions; 38 public import gtk.c.types; 39 public import gtkc.gtktypes; 40 41 42 /** 43 * The #GtkImage widget displays an image. Various kinds of object 44 * can be displayed as an image; most typically, you would load a 45 * #GdkPixbuf ("pixel buffer") from a file, and then display that. 46 * There’s a convenience function to do this, gtk_image_new_from_file(), 47 * used as follows: 48 * |[<!-- language="C" --> 49 * GtkWidget *image; 50 * image = gtk_image_new_from_file ("myfile.png"); 51 * ]| 52 * If the file isn’t loaded successfully, the image will contain a 53 * “broken image” icon similar to that used in many web browsers. 54 * If you want to handle errors in loading the file yourself, 55 * for example by displaying an error message, then load the image with 56 * gdk_pixbuf_new_from_file(), then create the #GtkImage with 57 * gtk_image_new_from_pixbuf(). 58 * 59 * The image file may contain an animation, if so the #GtkImage will 60 * display an animation (#GdkPixbufAnimation) instead of a static image. 61 * 62 * #GtkImage is a subclass of #GtkMisc, which implies that you can 63 * align it (center, left, right) and add padding to it, using 64 * #GtkMisc methods. 65 * 66 * #GtkImage is a “no window” widget (has no #GdkWindow of its own), 67 * so by default does not receive events. If you want to receive events 68 * on the image, such as button clicks, place the image inside a 69 * #GtkEventBox, then connect to the event signals on the event box. 70 * 71 * ## Handling button press events on a #GtkImage. 72 * 73 * |[<!-- language="C" --> 74 * static gboolean 75 * button_press_callback (GtkWidget *event_box, 76 * GdkEventButton *event, 77 * gpointer data) 78 * { 79 * g_print ("Event box clicked at coordinates %f,%f\n", 80 * event->x, event->y); 81 * 82 * // Returning TRUE means we handled the event, so the signal 83 * // emission should be stopped (don’t call any further callbacks 84 * // that may be connected). Return FALSE to continue invoking callbacks. 85 * return TRUE; 86 * } 87 * 88 * static GtkWidget* 89 * create_image (void) 90 * { 91 * GtkWidget *image; 92 * GtkWidget *event_box; 93 * 94 * image = gtk_image_new_from_file ("myfile.png"); 95 * 96 * event_box = gtk_event_box_new (); 97 * 98 * gtk_container_add (GTK_CONTAINER (event_box), image); 99 * 100 * g_signal_connect (G_OBJECT (event_box), 101 * "button_press_event", 102 * G_CALLBACK (button_press_callback), 103 * image); 104 * 105 * return image; 106 * } 107 * ]| 108 * 109 * When handling events on the event box, keep in mind that coordinates 110 * in the image may be different from event box coordinates due to 111 * the alignment and padding settings on the image (see #GtkMisc). 112 * The simplest way to solve this is to set the alignment to 0.0 113 * (left/top), and set the padding to zero. Then the origin of 114 * the image will be the same as the origin of the event box. 115 * 116 * Sometimes an application will want to avoid depending on external data 117 * files, such as image files. GTK+ comes with a program to avoid this, 118 * called “gdk-pixbuf-csource”. This library 119 * allows you to convert an image into a C variable declaration, which 120 * can then be loaded into a #GdkPixbuf using 121 * gdk_pixbuf_new_from_inline(). 122 * 123 * # CSS nodes 124 * 125 * GtkImage has a single CSS node with the name image. The style classes 126 * may appear on image CSS nodes: .icon-dropshadow, .lowres-icon. 127 */ 128 public class Image : Misc 129 { 130 /** the main Gtk struct */ 131 protected GtkImage* gtkImage; 132 133 /** Get the main Gtk struct */ 134 public GtkImage* getImageStruct(bool transferOwnership = false) 135 { 136 if (transferOwnership) 137 ownedRef = false; 138 return gtkImage; 139 } 140 141 /** the main Gtk struct as a void* */ 142 protected override void* getStruct() 143 { 144 return cast(void*)gtkImage; 145 } 146 147 /** 148 * Sets our main struct and passes it to the parent class. 149 */ 150 public this (GtkImage* gtkImage, bool ownedRef = false) 151 { 152 this.gtkImage = gtkImage; 153 super(cast(GtkMisc*)gtkImage, ownedRef); 154 } 155 156 /** 157 * Creates a Image displaying a stock icon. Sample stock icon 158 * names are StockID.OPEN, StockID.EXIT. Sample stock sizes 159 * are IconSize.MENU, IconSize.SMALL_TOOLBAR. If the stock 160 * icon name isn't known, the image will be empty. 161 * You can register your own stock icon names, see 162 * gtk.IconFactory.IconFactory.addDefault() and gtk.IconFactory.IconFactory.add(). 163 * Params: 164 * StockID = a stock icon name 165 * size = a stock icon size 166 * Returns: 167 * a new GtkImage displaying the stock icon 168 * Throws: ConstructionException GTK+ fails to create the object. 169 */ 170 public this (StockID stockID, GtkIconSize size) 171 { 172 auto p = gtk_image_new_from_stock(Str.toStringz(stockID), size); 173 if(p is null) 174 { 175 throw new ConstructionException("null returned by gtk_image_new_from_stock(Str.toStringz(StockDesc[stockID]), size)"); 176 } 177 this(cast(GtkImage*)p); 178 } 179 180 /** 181 */ 182 183 /** */ 184 public static GType getType() 185 { 186 return gtk_image_get_type(); 187 } 188 189 /** 190 * Creates a new empty #GtkImage widget. 191 * 192 * Returns: a newly created #GtkImage widget. 193 * 194 * Throws: ConstructionException GTK+ fails to create the object. 195 */ 196 public this() 197 { 198 auto p = gtk_image_new(); 199 200 if(p is null) 201 { 202 throw new ConstructionException("null returned by new"); 203 } 204 205 this(cast(GtkImage*) p); 206 } 207 208 /** 209 * Creates a #GtkImage displaying the given animation. 210 * The #GtkImage does not assume a reference to the 211 * animation; you still need to unref it if you own references. 212 * #GtkImage will add its own reference rather than adopting yours. 213 * 214 * Note that the animation frames are shown using a timeout with 215 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness, 216 * keep in mind that the animation will only be shown if the main loop 217 * is not busy with something that has a higher priority. 218 * 219 * Params: 220 * animation = an animation 221 * 222 * Returns: a new #GtkImage widget 223 * 224 * Throws: ConstructionException GTK+ fails to create the object. 225 */ 226 public this(PixbufAnimation animation) 227 { 228 auto p = gtk_image_new_from_animation((animation is null) ? null : animation.getPixbufAnimationStruct()); 229 230 if(p is null) 231 { 232 throw new ConstructionException("null returned by new_from_animation"); 233 } 234 235 this(cast(GtkImage*) p); 236 } 237 238 /** 239 * Creates a new #GtkImage displaying the file @filename. If the file 240 * isn’t found or can’t be loaded, the resulting #GtkImage will 241 * display a “broken image” icon. This function never returns %NULL, 242 * it always returns a valid #GtkImage widget. 243 * 244 * If the file contains an animation, the image will contain an 245 * animation. 246 * 247 * If you need to detect failures to load the file, use 248 * gdk_pixbuf_new_from_file() to load the file yourself, then create 249 * the #GtkImage from the pixbuf. (Or for animations, use 250 * gdk_pixbuf_animation_new_from_file()). 251 * 252 * The storage type (gtk_image_get_storage_type()) of the returned 253 * image is not defined, it will be whatever is appropriate for 254 * displaying the file. 255 * 256 * Params: 257 * filename = a filename 258 * 259 * Returns: a new #GtkImage 260 * 261 * Throws: ConstructionException GTK+ fails to create the object. 262 */ 263 public this(string filename) 264 { 265 auto p = gtk_image_new_from_file(Str.toStringz(filename)); 266 267 if(p is null) 268 { 269 throw new ConstructionException("null returned by new_from_file"); 270 } 271 272 this(cast(GtkImage*) p); 273 } 274 275 /** 276 * Creates a #GtkImage displaying an icon from the current icon theme. 277 * If the icon name isn’t known, a “broken image” icon will be 278 * displayed instead. If the current icon theme is changed, the icon 279 * will be updated appropriately. 280 * 281 * Params: 282 * icon = an icon 283 * size = a stock icon size (#GtkIconSize) 284 * 285 * Returns: a new #GtkImage displaying the themed icon 286 * 287 * Since: 2.14 288 * 289 * Throws: ConstructionException GTK+ fails to create the object. 290 */ 291 public this(IconIF icon, GtkIconSize size) 292 { 293 auto p = gtk_image_new_from_gicon((icon is null) ? null : icon.getIconStruct(), size); 294 295 if(p is null) 296 { 297 throw new ConstructionException("null returned by new_from_gicon"); 298 } 299 300 this(cast(GtkImage*) p); 301 } 302 303 /** 304 * Creates a #GtkImage displaying an icon from the current icon theme. 305 * If the icon name isn’t known, a “broken image” icon will be 306 * displayed instead. If the current icon theme is changed, the icon 307 * will be updated appropriately. 308 * 309 * Params: 310 * iconName = an icon name or %NULL 311 * size = a stock icon size (#GtkIconSize) 312 * 313 * Returns: a new #GtkImage displaying the themed icon 314 * 315 * Since: 2.6 316 * 317 * Throws: ConstructionException GTK+ fails to create the object. 318 */ 319 public this(string iconName, GtkIconSize size) 320 { 321 auto p = gtk_image_new_from_icon_name(Str.toStringz(iconName), size); 322 323 if(p is null) 324 { 325 throw new ConstructionException("null returned by new_from_icon_name"); 326 } 327 328 this(cast(GtkImage*) p); 329 } 330 331 /** 332 * Creates a #GtkImage displaying an icon set. Sample stock sizes are 333 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using 334 * this function, usually it’s better to create a #GtkIconFactory, put 335 * your icon sets in the icon factory, add the icon factory to the 336 * list of default factories with gtk_icon_factory_add_default(), and 337 * then use gtk_image_new_from_stock(). This will allow themes to 338 * override the icon you ship with your application. 339 * 340 * The #GtkImage does not assume a reference to the 341 * icon set; you still need to unref it if you own references. 342 * #GtkImage will add its own reference rather than adopting yours. 343 * 344 * Deprecated: Use gtk_image_new_from_icon_name() instead. 345 * 346 * Params: 347 * iconSet = a #GtkIconSet 348 * size = a stock icon size (#GtkIconSize) 349 * 350 * Returns: a new #GtkImage 351 * 352 * Throws: ConstructionException GTK+ fails to create the object. 353 */ 354 public this(IconSet iconSet, GtkIconSize size) 355 { 356 auto p = gtk_image_new_from_icon_set((iconSet is null) ? null : iconSet.getIconSetStruct(), size); 357 358 if(p is null) 359 { 360 throw new ConstructionException("null returned by new_from_icon_set"); 361 } 362 363 this(cast(GtkImage*) p); 364 } 365 366 /** 367 * Creates a new #GtkImage displaying @pixbuf. 368 * The #GtkImage does not assume a reference to the 369 * pixbuf; you still need to unref it if you own references. 370 * #GtkImage will add its own reference rather than adopting yours. 371 * 372 * Note that this function just creates an #GtkImage from the pixbuf. The 373 * #GtkImage created will not react to state changes. Should you want that, 374 * you should use gtk_image_new_from_icon_name(). 375 * 376 * Params: 377 * pixbuf = a #GdkPixbuf, or %NULL 378 * 379 * Returns: a new #GtkImage 380 * 381 * Throws: ConstructionException GTK+ fails to create the object. 382 */ 383 public this(Pixbuf pixbuf) 384 { 385 auto p = gtk_image_new_from_pixbuf((pixbuf is null) ? null : pixbuf.getPixbufStruct()); 386 387 if(p is null) 388 { 389 throw new ConstructionException("null returned by new_from_pixbuf"); 390 } 391 392 this(cast(GtkImage*) p); 393 } 394 395 /** 396 * Creates a new #GtkImage displaying @surface. 397 * The #GtkImage does not assume a reference to the 398 * surface; you still need to unref it if you own references. 399 * #GtkImage will add its own reference rather than adopting yours. 400 * 401 * Params: 402 * surface = a #cairo_surface_t, or %NULL 403 * 404 * Returns: a new #GtkImage 405 * 406 * Since: 3.10 407 * 408 * Throws: ConstructionException GTK+ fails to create the object. 409 */ 410 public this(Surface surface) 411 { 412 auto p = gtk_image_new_from_surface((surface is null) ? null : surface.getSurfaceStruct()); 413 414 if(p is null) 415 { 416 throw new ConstructionException("null returned by new_from_surface"); 417 } 418 419 this(cast(GtkImage*) p); 420 } 421 422 /** 423 * Resets the image to be empty. 424 * 425 * Since: 2.8 426 */ 427 public void clear() 428 { 429 gtk_image_clear(gtkImage); 430 } 431 432 /** 433 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage. 434 * The storage type of the image must be %GTK_IMAGE_EMPTY or 435 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()). 436 * The caller of this function does not own a reference to the 437 * returned animation. 438 * 439 * Returns: the displayed animation, or %NULL if 440 * the image is empty 441 */ 442 public PixbufAnimation getAnimation() 443 { 444 auto p = gtk_image_get_animation(gtkImage); 445 446 if(p is null) 447 { 448 return null; 449 } 450 451 return ObjectG.getDObject!(PixbufAnimation)(cast(GdkPixbufAnimation*) p); 452 } 453 454 /** 455 * Gets the #GIcon and size being displayed by the #GtkImage. 456 * The storage type of the image must be %GTK_IMAGE_EMPTY or 457 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()). 458 * The caller of this function does not own a reference to the 459 * returned #GIcon. 460 * 461 * Params: 462 * gicon = place to store a 463 * #GIcon, or %NULL 464 * size = place to store an icon size 465 * (#GtkIconSize), or %NULL 466 * 467 * Since: 2.14 468 */ 469 public void getGicon(out IconIF gicon, out GtkIconSize size) 470 { 471 GIcon* outgicon = null; 472 473 gtk_image_get_gicon(gtkImage, &outgicon, &size); 474 475 gicon = ObjectG.getDObject!(IconIF)(outgicon); 476 } 477 478 /** 479 * Gets the icon name and size being displayed by the #GtkImage. 480 * The storage type of the image must be %GTK_IMAGE_EMPTY or 481 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()). 482 * The returned string is owned by the #GtkImage and should not 483 * be freed. 484 * 485 * Params: 486 * iconName = place to store an 487 * icon name, or %NULL 488 * size = place to store an icon size 489 * (#GtkIconSize), or %NULL 490 * 491 * Since: 2.6 492 */ 493 public void getIconName(out string iconName, out GtkIconSize size) 494 { 495 char* outiconName = null; 496 497 gtk_image_get_icon_name(gtkImage, &outiconName, &size); 498 499 iconName = Str.toString(outiconName); 500 } 501 502 /** 503 * Gets the icon set and size being displayed by the #GtkImage. 504 * The storage type of the image must be %GTK_IMAGE_EMPTY or 505 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()). 506 * 507 * Deprecated: Use gtk_image_get_icon_name() instead. 508 * 509 * Params: 510 * iconSet = location to store a 511 * #GtkIconSet, or %NULL 512 * size = location to store a stock 513 * icon size (#GtkIconSize), or %NULL 514 */ 515 public void getIconSet(out IconSet iconSet, out GtkIconSize size) 516 { 517 GtkIconSet* outiconSet = null; 518 519 gtk_image_get_icon_set(gtkImage, &outiconSet, &size); 520 521 iconSet = ObjectG.getDObject!(IconSet)(outiconSet); 522 } 523 524 /** 525 * Gets the #GdkPixbuf being displayed by the #GtkImage. 526 * The storage type of the image must be %GTK_IMAGE_EMPTY or 527 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()). 528 * The caller of this function does not own a reference to the 529 * returned pixbuf. 530 * 531 * Returns: the displayed pixbuf, or %NULL if 532 * the image is empty 533 */ 534 public Pixbuf getPixbuf() 535 { 536 auto p = gtk_image_get_pixbuf(gtkImage); 537 538 if(p is null) 539 { 540 return null; 541 } 542 543 return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p); 544 } 545 546 /** 547 * Gets the pixel size used for named icons. 548 * 549 * Returns: the pixel size used for named icons. 550 * 551 * Since: 2.6 552 */ 553 public int getPixelSize() 554 { 555 return gtk_image_get_pixel_size(gtkImage); 556 } 557 558 /** 559 * Gets the stock icon name and size being displayed by the #GtkImage. 560 * The storage type of the image must be %GTK_IMAGE_EMPTY or 561 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()). 562 * The returned string is owned by the #GtkImage and should not 563 * be freed. 564 * 565 * Deprecated: Use gtk_image_get_icon_name() instead. 566 * 567 * Params: 568 * stockId = place to store a 569 * stock icon name, or %NULL 570 * size = place to store a stock icon 571 * size (#GtkIconSize), or %NULL 572 */ 573 public void getStock(out string stockId, out GtkIconSize size) 574 { 575 char* outstockId = null; 576 577 gtk_image_get_stock(gtkImage, &outstockId, &size); 578 579 stockId = Str.toString(outstockId); 580 } 581 582 /** 583 * Gets the type of representation being used by the #GtkImage 584 * to store image data. If the #GtkImage has no image data, 585 * the return value will be %GTK_IMAGE_EMPTY. 586 * 587 * Returns: image representation being used 588 */ 589 public GtkImageType getStorageType() 590 { 591 return gtk_image_get_storage_type(gtkImage); 592 } 593 594 /** 595 * Causes the #GtkImage to display the given animation (or display 596 * nothing, if you set the animation to %NULL). 597 * 598 * Params: 599 * animation = the #GdkPixbufAnimation 600 */ 601 public void setFromAnimation(PixbufAnimation animation) 602 { 603 gtk_image_set_from_animation(gtkImage, (animation is null) ? null : animation.getPixbufAnimationStruct()); 604 } 605 606 /** 607 * See gtk_image_new_from_file() for details. 608 * 609 * Params: 610 * filename = a filename or %NULL 611 */ 612 public void setFromFile(string filename) 613 { 614 gtk_image_set_from_file(gtkImage, Str.toStringz(filename)); 615 } 616 617 /** 618 * See gtk_image_new_from_gicon() for details. 619 * 620 * Params: 621 * icon = an icon 622 * size = an icon size (#GtkIconSize) 623 * 624 * Since: 2.14 625 */ 626 public void setFromGicon(IconIF icon, GtkIconSize size) 627 { 628 gtk_image_set_from_gicon(gtkImage, (icon is null) ? null : icon.getIconStruct(), size); 629 } 630 631 /** 632 * See gtk_image_new_from_icon_name() for details. 633 * 634 * Params: 635 * iconName = an icon name or %NULL 636 * size = an icon size (#GtkIconSize) 637 * 638 * Since: 2.6 639 */ 640 public void setFromIconName(string iconName, GtkIconSize size) 641 { 642 gtk_image_set_from_icon_name(gtkImage, Str.toStringz(iconName), size); 643 } 644 645 /** 646 * See gtk_image_new_from_icon_set() for details. 647 * 648 * Deprecated: Use gtk_image_set_from_icon_name() instead. 649 * 650 * Params: 651 * iconSet = a #GtkIconSet 652 * size = a stock icon size (#GtkIconSize) 653 */ 654 public void setFromIconSet(IconSet iconSet, GtkIconSize size) 655 { 656 gtk_image_set_from_icon_set(gtkImage, (iconSet is null) ? null : iconSet.getIconSetStruct(), size); 657 } 658 659 /** 660 * See gtk_image_new_from_pixbuf() for details. 661 * 662 * Params: 663 * pixbuf = a #GdkPixbuf or %NULL 664 */ 665 public void setFromPixbuf(Pixbuf pixbuf) 666 { 667 gtk_image_set_from_pixbuf(gtkImage, (pixbuf is null) ? null : pixbuf.getPixbufStruct()); 668 } 669 670 /** 671 * See gtk_image_new_from_resource() for details. 672 * 673 * Params: 674 * resourcePath = a resource path or %NULL 675 */ 676 public void setFromResource(string resourcePath) 677 { 678 gtk_image_set_from_resource(gtkImage, Str.toStringz(resourcePath)); 679 } 680 681 /** 682 * See gtk_image_new_from_stock() for details. 683 * 684 * Deprecated: Use gtk_image_set_from_icon_name() instead. 685 * 686 * Params: 687 * stockId = a stock icon name 688 * size = a stock icon size (#GtkIconSize) 689 */ 690 public void setFromStock(string stockId, GtkIconSize size) 691 { 692 gtk_image_set_from_stock(gtkImage, Str.toStringz(stockId), size); 693 } 694 695 /** 696 * See gtk_image_new_from_surface() for details. 697 * 698 * Params: 699 * surface = a cairo_surface_t or %NULL 700 * 701 * Since: 3.10 702 */ 703 public void setFromSurface(Surface surface) 704 { 705 gtk_image_set_from_surface(gtkImage, (surface is null) ? null : surface.getSurfaceStruct()); 706 } 707 708 /** 709 * Sets the pixel size to use for named icons. If the pixel size is set 710 * to a value != -1, it is used instead of the icon size set by 711 * gtk_image_set_from_icon_name(). 712 * 713 * Params: 714 * pixelSize = the new pixel size 715 * 716 * Since: 2.6 717 */ 718 public void setPixelSize(int pixelSize) 719 { 720 gtk_image_set_pixel_size(gtkImage, pixelSize); 721 } 722 }