1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 #include "gtkcontainer.h"
30 #include "gtkiconfactory.h"
35 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
37 static void gtk_image_class_init (GtkImageClass *klass);
38 static void gtk_image_init (GtkImage *image);
39 static gint gtk_image_expose (GtkWidget *widget,
40 GdkEventExpose *event);
41 static void gtk_image_unmap (GtkWidget *widget);
42 static void gtk_image_size_request (GtkWidget *widget,
43 GtkRequisition *requisition);
44 static void gtk_image_destroy (GtkObject *object);
45 static void gtk_image_clear (GtkImage *image);
46 static void gtk_image_reset (GtkImage *image);
47 static void gtk_image_update_size (GtkImage *image,
51 static void gtk_image_set_property (GObject *object,
55 static void gtk_image_get_property (GObject *object,
60 static gpointer parent_class;
73 PROP_PIXBUF_ANIMATION,
78 gtk_image_get_type (void)
80 static GtkType image_type = 0;
84 static const GtkTypeInfo image_info =
88 sizeof (GtkImageClass),
89 (GtkClassInitFunc) gtk_image_class_init,
90 (GtkObjectInitFunc) gtk_image_init,
91 /* reserved_1 */ NULL,
92 /* reserved_2 */ NULL,
93 (GtkClassInitFunc) NULL,
96 image_type = gtk_type_unique (GTK_TYPE_MISC, &image_info);
103 gtk_image_class_init (GtkImageClass *class)
105 GObjectClass *gobject_class;
106 GtkObjectClass *object_class;
107 GtkWidgetClass *widget_class;
109 parent_class = g_type_class_peek_parent (class);
111 gobject_class = G_OBJECT_CLASS (class);
113 gobject_class->set_property = gtk_image_set_property;
114 gobject_class->get_property = gtk_image_get_property;
116 object_class = GTK_OBJECT_CLASS (class);
118 object_class->destroy = gtk_image_destroy;
120 widget_class = GTK_WIDGET_CLASS (class);
122 widget_class->expose_event = gtk_image_expose;
123 widget_class->size_request = gtk_image_size_request;
124 widget_class->unmap = gtk_image_unmap;
126 g_object_class_install_property (gobject_class,
128 g_param_spec_object ("pixbuf",
130 _("A GdkPixbuf to display."),
134 g_object_class_install_property (gobject_class,
136 g_param_spec_object ("pixmap",
138 _("A GdkPixmap to display."),
142 g_object_class_install_property (gobject_class,
144 g_param_spec_object ("image",
146 _("A GdkImage to display."),
150 g_object_class_install_property (gobject_class,
152 g_param_spec_object ("mask",
154 _("Mask bitmap to use with GdkImage or GdkPixmap"),
158 g_object_class_install_property (gobject_class,
160 g_param_spec_string ("file",
162 _("Filename to load and display."),
167 g_object_class_install_property (gobject_class,
169 g_param_spec_string ("stock",
171 _("Stock ID for a stock image to display."),
175 g_object_class_install_property (gobject_class,
177 g_param_spec_boxed ("icon_set",
179 _("Icon set to display."),
183 g_object_class_install_property (gobject_class,
185 g_param_spec_int ("icon_size",
187 _("Size to use for stock icon or icon set."),
192 g_object_class_install_property (gobject_class,
193 PROP_PIXBUF_ANIMATION,
194 g_param_spec_object ("pixbuf_animation",
196 _("GdkPixbufAnimation to display."),
197 GDK_TYPE_PIXBUF_ANIMATION,
200 g_object_class_install_property (gobject_class,
202 g_param_spec_enum ("storage_type",
204 _("The representation being used for image data."),
211 gtk_image_init (GtkImage *image)
213 GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);
215 image->storage_type = GTK_IMAGE_EMPTY;
216 image->icon_size = DEFAULT_ICON_SIZE;
221 gtk_image_destroy (GtkObject *object)
223 GtkImage *image = GTK_IMAGE (object);
225 gtk_image_clear (image);
227 GTK_OBJECT_CLASS (parent_class)->destroy (object);
231 gtk_image_set_property (GObject *object,
238 image = GTK_IMAGE (object);
243 gtk_image_set_from_pixbuf (image,
244 g_value_get_object (value));
247 gtk_image_set_from_pixmap (image,
248 g_value_get_object (value),
252 gtk_image_set_from_image (image,
253 g_value_get_object (value),
257 if (image->storage_type == GTK_IMAGE_PIXMAP)
258 gtk_image_set_from_pixmap (image,
259 image->data.pixmap.pixmap,
260 g_value_get_object (value));
261 else if (image->storage_type == GTK_IMAGE_IMAGE)
262 gtk_image_set_from_image (image,
263 image->data.image.image,
264 g_value_get_object (value));
269 mask = g_value_get_object (value);
272 g_object_ref (G_OBJECT (mask));
274 gtk_image_reset (image);
280 gtk_image_set_from_file (image,
281 g_value_get_string (value));
284 gtk_image_set_from_stock (image, g_value_get_string (value),
288 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
292 if (image->storage_type == GTK_IMAGE_STOCK)
293 gtk_image_set_from_stock (image,
294 image->data.stock.stock_id,
295 g_value_get_int (value));
296 else if (image->storage_type == GTK_IMAGE_ICON_SET)
297 gtk_image_set_from_icon_set (image,
298 image->data.icon_set.icon_set,
299 g_value_get_int (value));
301 /* Save to be used when STOCK or ICON_SET property comes in */
302 image->icon_size = g_value_get_int (value);
304 case PROP_PIXBUF_ANIMATION:
305 gtk_image_set_from_animation (image,
306 g_value_get_object (value));
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 gtk_image_get_property (GObject *object,
323 image = GTK_IMAGE (object);
325 /* The "getter" functions whine if you try to get the wrong
326 * storage type. This function is instead robust against that,
327 * so that GUI builders don't have to jump through hoops
334 if (image->storage_type != GTK_IMAGE_PIXBUF)
335 g_value_set_object (value, NULL);
337 g_value_set_object (value,
338 gtk_image_get_pixbuf (image));
341 if (image->storage_type != GTK_IMAGE_PIXMAP)
342 g_value_set_object (value, NULL);
344 g_value_set_object (value,
345 image->data.pixmap.pixmap);
348 g_value_set_object (value, image->mask);
351 if (image->storage_type != GTK_IMAGE_IMAGE)
352 g_value_set_object (value, NULL);
354 g_value_set_object (value,
355 image->data.image.image);
358 if (image->storage_type != GTK_IMAGE_STOCK)
359 g_value_set_string (value, NULL);
361 g_value_set_string (value,
362 image->data.stock.stock_id);
365 if (image->storage_type != GTK_IMAGE_ICON_SET)
366 g_value_set_boxed (value, NULL);
368 g_value_set_boxed (value,
369 image->data.icon_set.icon_set);
372 g_value_set_int (value, image->icon_size);
374 case PROP_PIXBUF_ANIMATION:
375 if (image->storage_type != GTK_IMAGE_ANIMATION)
376 g_value_set_object (value, NULL);
378 g_value_set_object (value,
379 image->data.anim.anim);
381 case PROP_STORAGE_TYPE:
382 g_value_set_enum (value, image->storage_type);
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
393 * gtk_image_new_from_pixmap:
394 * @pixmap: a #GdkPixmap, or %NULL
395 * @mask: a #GdkBitmap, or %NULL
397 * Creates a #GtkImage widget displaying @pixmap with a @mask.
398 * A #GdkImage is a server-side image buffer in the pixel format of the
399 * current display. The #GtkImage does not assume a reference to the
400 * pixmap or mask; you still need to unref them if you own references.
401 * #GtkImage will add its own reference rather than adopting yours.
403 * Return value: a new #GtkImage
406 gtk_image_new_from_pixmap (GdkPixmap *pixmap,
411 image = gtk_type_new (GTK_TYPE_IMAGE);
413 gtk_image_set_from_pixmap (image, pixmap, mask);
415 return GTK_WIDGET (image);
419 * gtk_image_new_from_image:
420 * @image: a #GdkImage, or %NULL
421 * @mask: a #GdkBitmap, or %NULL
423 * Creates a #GtkImage widget displaying a @image with a @mask.
424 * A #GdkImage is a client-side image buffer in the pixel format of the
426 * The #GtkImage does not assume a reference to the
427 * image or mask; you still need to unref them if you own references.
428 * #GtkImage will add its own reference rather than adopting yours.
430 * Return value: a new #GtkImage
433 gtk_image_new_from_image (GdkImage *gdk_image,
438 image = gtk_type_new (GTK_TYPE_IMAGE);
440 gtk_image_set_from_image (image, gdk_image, mask);
442 return GTK_WIDGET (image);
446 * gtk_image_new_from_file:
447 * @filename: a filename
449 * Creates a new #GtkImage displaying the file @filename. If the file
450 * isn't found or can't be loaded, the resulting #GtkImage will
451 * display a "broken image" icon. This function never returns %NULL,
452 * it always returns a valid #GtkImage widget.
454 * If the file contains an animation, the image will contain an
457 * If you need to detect failures to load the file, use
458 * gdk_pixbuf_new_from_file() to load the file yourself, then create
459 * the #GtkImage from the pixbuf. (Or for animations, use
460 * gdk_pixbuf_animation_new_from_file()).
462 * The storage type (gtk_image_get_storage_type()) of the returned
463 * image is not defined, it will be whatever is appropriate for
464 * displaying the file.
466 * Return value: a new #GtkImage
469 gtk_image_new_from_file (const gchar *filename)
473 image = gtk_type_new (GTK_TYPE_IMAGE);
475 gtk_image_set_from_file (image, filename);
477 return GTK_WIDGET (image);
481 * gtk_image_new_from_pixbuf:
482 * @pixbuf: a #GdkPixbuf, or %NULL
484 * Creates a new #GtkImage displaying @pixbuf.
485 * The #GtkImage does not assume a reference to the
486 * pixbuf; you still need to unref it if you own references.
487 * #GtkImage will add its own reference rather than adopting yours.
489 * Note that this function just creates an #GtkImage from the pixbuf. The
490 * #GtkImage created will not react to state changes. Should you want that, you
491 * should use gtk_image_new_from_icon_set().
493 * Return value: a new #GtkImage
496 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
500 image = gtk_type_new (GTK_TYPE_IMAGE);
502 gtk_image_set_from_pixbuf (image, pixbuf);
504 return GTK_WIDGET (image);
508 * gtk_image_new_from_stock:
509 * @stock_id: a stock icon name
510 * @size: a stock icon size
512 * Creates a #GtkImage displaying a stock icon. Sample stock icon
513 * names are #GTK_STOCK_OPEN, #GTK_STOCK_EXIT. Sample stock sizes
514 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
515 * icon name isn't known, a "broken image" icon will be displayed instead.
516 * You can register your own stock icon names, see
517 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
519 * Return value: a new #GtkImage displaying the stock icon
522 gtk_image_new_from_stock (const gchar *stock_id,
527 image = gtk_type_new (GTK_TYPE_IMAGE);
529 gtk_image_set_from_stock (image, stock_id, size);
531 return GTK_WIDGET (image);
535 * gtk_image_new_from_icon_set:
536 * @icon_set: a #GtkIconSet
537 * @size: a stock icon size
539 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
540 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
541 * this function, usually it's better to create a #GtkIconFactory, put
542 * your icon sets in the icon factory, add the icon factory to the
543 * list of default factories with gtk_icon_factory_add_default(), and
544 * then use gtk_image_new_from_stock(). This will allow themes to
545 * override the icon you ship with your application.
547 * The #GtkImage does not assume a reference to the
548 * icon set; you still need to unref it if you own references.
549 * #GtkImage will add its own reference rather than adopting yours.
552 * Return value: a new #GtkImage
555 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
560 image = gtk_type_new (GTK_TYPE_IMAGE);
562 gtk_image_set_from_icon_set (image, icon_set, size);
564 return GTK_WIDGET (image);
568 * gtk_image_new_from_animation:
569 * @animation: an animation
571 * Creates a #GtkImage displaying the given animation.
572 * The #GtkImage does not assume a reference to the
573 * animation; you still need to unref it if you own references.
574 * #GtkImage will add its own reference rather than adopting yours.
576 * Return value: a new #GtkImage widget
579 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
583 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
585 image = gtk_type_new (GTK_TYPE_IMAGE);
587 gtk_image_set_from_animation (image, animation);
589 return GTK_WIDGET (image);
593 * gtk_image_set_from_pixmap:
594 * @image: a #GtkImage
595 * @pixmap: a #GdkPixmap or %NULL
596 * @mask: a #GdkBitmap or %NULL
598 * See gtk_image_new_from_pixmap() for details.
602 gtk_image_set_from_pixmap (GtkImage *image,
606 g_return_if_fail (GTK_IS_IMAGE (image));
607 g_return_if_fail (pixmap == NULL ||
608 GDK_IS_PIXMAP (pixmap));
609 g_return_if_fail (mask == NULL ||
610 GDK_IS_PIXMAP (mask));
612 g_object_freeze_notify (G_OBJECT (image));
615 g_object_ref (G_OBJECT (pixmap));
618 g_object_ref (G_OBJECT (mask));
620 gtk_image_reset (image);
629 image->storage_type = GTK_IMAGE_PIXMAP;
631 image->data.pixmap.pixmap = pixmap;
633 gdk_drawable_get_size (GDK_DRAWABLE (pixmap), &width, &height);
635 gtk_image_update_size (image, width, height);
638 g_object_notify (G_OBJECT (image), "pixmap");
639 g_object_notify (G_OBJECT (image), "mask");
641 g_object_thaw_notify (G_OBJECT (image));
645 * gtk_image_set_from_image:
646 * @image: a #GtkImage
647 * @gdk_image: a #GdkImage or %NULL
648 * @mask: a #GdkBitmap or %NULL
650 * See gtk_image_new_from_image() for details.
654 gtk_image_set_from_image (GtkImage *image,
658 g_return_if_fail (GTK_IS_IMAGE (image));
659 g_return_if_fail (gdk_image == NULL ||
660 GDK_IS_IMAGE (gdk_image));
661 g_return_if_fail (mask == NULL ||
662 GDK_IS_PIXMAP (mask));
664 g_object_freeze_notify (G_OBJECT (image));
667 g_object_ref (G_OBJECT (gdk_image));
670 g_object_ref (G_OBJECT (mask));
672 gtk_image_reset (image);
676 image->storage_type = GTK_IMAGE_IMAGE;
678 image->data.image.image = gdk_image;
681 gtk_image_update_size (image, gdk_image->width, gdk_image->height);
685 /* Clean up the mask if gdk_image was NULL */
687 g_object_unref (G_OBJECT (mask));
690 g_object_notify (G_OBJECT (image), "image");
691 g_object_notify (G_OBJECT (image), "mask");
693 g_object_thaw_notify (G_OBJECT (image));
697 * gtk_image_set_from_file:
698 * @image: a #GtkImage
699 * @filename: a filename or %NULL
701 * See gtk_image_new_from_file() for details.
705 gtk_image_set_from_file (GtkImage *image,
706 const gchar *filename)
708 GdkPixbufAnimation *anim;
710 g_return_if_fail (GTK_IS_IMAGE (image));
712 g_object_freeze_notify (G_OBJECT (image));
714 gtk_image_reset (image);
716 if (filename == NULL)
718 g_object_thaw_notify (G_OBJECT (image));
722 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
726 gtk_image_set_from_stock (image,
727 GTK_STOCK_MISSING_IMAGE,
728 GTK_ICON_SIZE_BUTTON);
729 g_object_thaw_notify (G_OBJECT (image));
733 /* We could just unconditionally set_from_animation,
734 * but it's nicer for memory if we toss the animation
735 * if it's just a single pixbuf
738 if (gdk_pixbuf_animation_is_static_image (anim))
740 gtk_image_set_from_pixbuf (image,
741 gdk_pixbuf_animation_get_static_image (anim));
745 gtk_image_set_from_animation (image, anim);
748 g_object_unref (G_OBJECT (anim));
750 g_object_thaw_notify (G_OBJECT (image));
754 * gtk_image_set_from_pixbuf:
755 * @image: a #GtkImage
756 * @pixbuf: a #GdkPixbuf or %NULL
758 * See gtk_image_new_from_pixbuf() for details.
762 gtk_image_set_from_pixbuf (GtkImage *image,
765 g_return_if_fail (GTK_IS_IMAGE (image));
766 g_return_if_fail (pixbuf == NULL ||
767 GDK_IS_PIXBUF (pixbuf));
769 g_object_freeze_notify (G_OBJECT (image));
772 g_object_ref (G_OBJECT (pixbuf));
774 gtk_image_reset (image);
778 image->storage_type = GTK_IMAGE_PIXBUF;
780 image->data.pixbuf.pixbuf = pixbuf;
782 gtk_image_update_size (image,
783 gdk_pixbuf_get_width (pixbuf),
784 gdk_pixbuf_get_height (pixbuf));
787 g_object_notify (G_OBJECT (image), "pixbuf");
789 g_object_thaw_notify (G_OBJECT (image));
793 * gtk_image_set_from_stock:
794 * @image: a #GtkImage
795 * @stock_id: a stock icon name
796 * @size: a stock icon size
798 * See gtk_image_new_from_stock() for details.
802 gtk_image_set_from_stock (GtkImage *image,
803 const gchar *stock_id,
808 g_return_if_fail (GTK_IS_IMAGE (image));
810 g_object_freeze_notify (G_OBJECT (image));
812 /* in case stock_id == image->data.stock.stock_id */
813 new_id = g_strdup (stock_id);
815 gtk_image_reset (image);
819 image->storage_type = GTK_IMAGE_STOCK;
821 image->data.stock.stock_id = new_id;
822 image->icon_size = size;
824 /* Size is demand-computed in size request method
825 * if we're a stock image, since changing the
826 * style impacts the size request
830 g_object_notify (G_OBJECT (image), "stock");
831 g_object_notify (G_OBJECT (image), "icon_size");
833 g_object_thaw_notify (G_OBJECT (image));
837 * gtk_image_set_from_icon_set:
838 * @image: a #GtkImage
839 * @icon_set: a #GtkIconSet
840 * @size: a stock icon size
842 * See gtk_image_new_from_icon_set() for details.
846 gtk_image_set_from_icon_set (GtkImage *image,
847 GtkIconSet *icon_set,
850 g_return_if_fail (GTK_IS_IMAGE (image));
852 g_object_freeze_notify (G_OBJECT (image));
855 gtk_icon_set_ref (icon_set);
857 gtk_image_reset (image);
861 image->storage_type = GTK_IMAGE_ICON_SET;
863 image->data.icon_set.icon_set = icon_set;
864 image->icon_size = size;
866 /* Size is demand-computed in size request method
867 * if we're an icon set
871 g_object_notify (G_OBJECT (image), "icon_set");
872 g_object_notify (G_OBJECT (image), "icon_size");
874 g_object_thaw_notify (G_OBJECT (image));
878 * gtk_image_set_from_animation:
879 * @image: a #GtkImage
880 * @animation: the #GdkPixbufAnimation
882 * Causes the #GtkImage to display the given animation (or display
883 * nothing, if you set the animation to %NULL).
886 gtk_image_set_from_animation (GtkImage *image,
887 GdkPixbufAnimation *animation)
889 g_return_if_fail (GTK_IS_IMAGE (image));
890 g_return_if_fail (animation == NULL ||
891 GDK_IS_PIXBUF_ANIMATION (animation));
893 g_object_freeze_notify (G_OBJECT (image));
896 g_object_ref (G_OBJECT (animation));
898 gtk_image_reset (image);
900 if (animation != NULL)
902 image->storage_type = GTK_IMAGE_ANIMATION;
904 image->data.anim.anim = animation;
905 image->data.anim.frame_timeout = 0;
906 image->data.anim.iter = NULL;
908 gtk_image_update_size (image,
909 gdk_pixbuf_animation_get_width (animation),
910 gdk_pixbuf_animation_get_height (animation));
913 g_object_notify (G_OBJECT (image), "pixbuf_animation");
915 g_object_thaw_notify (G_OBJECT (image));
919 * gtk_image_get_storage_type:
920 * @image: a #GtkImage
922 * Gets the type of representation being used by the #GtkImage
923 * to store image data. If the #GtkImage has no image data,
924 * the return value will be %GTK_IMAGE_EMPTY.
926 * Return value: image representation being used
929 gtk_image_get_storage_type (GtkImage *image)
931 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
933 return image->storage_type;
937 * gtk_image_get_pixmap:
938 * @image: a #GtkImage
939 * @pixmap: location to store the pixmap, or %NULL
940 * @mask: location to store the mask, or %NULL
942 * Gets the pixmap and mask being displayed by the #GtkImage.
943 * The storage type of the image must be %GTK_IMAGE_EMPTY or
944 * %GTK_IMAGE_PIXMAP (see gtk_image_get_storage_type()).
945 * The caller of this function does not own a reference to the
946 * returned pixmap and mask.
950 gtk_image_get_pixmap (GtkImage *image,
954 g_return_if_fail (GTK_IS_IMAGE (image));
955 g_return_if_fail (image->storage_type == GTK_IMAGE_PIXMAP ||
956 image->storage_type == GTK_IMAGE_EMPTY);
959 *pixmap = image->data.pixmap.pixmap;
966 * gtk_image_get_image:
967 * @image: a #GtkImage
968 * @gdk_image: return location for a #GtkImage
969 * @mask: return location for a #GdkBitmap
971 * Gets the #GdkImage and mask being displayed by the #GtkImage.
972 * The storage type of the image must be %GTK_IMAGE_EMPTY or
973 * %GTK_IMAGE_IMAGE (see gtk_image_get_storage_type()).
974 * The caller of this function does not own a reference to the
975 * returned image and mask.
978 gtk_image_get_image (GtkImage *image,
979 GdkImage **gdk_image,
982 g_return_if_fail (GTK_IS_IMAGE (image));
983 g_return_if_fail (image->storage_type == GTK_IMAGE_IMAGE ||
984 image->storage_type == GTK_IMAGE_EMPTY);
987 *gdk_image = image->data.image.image;
994 * gtk_image_get_pixbuf:
995 * @image: a #GtkImage
998 * Gets the #GdkPixbuf being displayed by the #GtkImage.
999 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1000 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1001 * The caller of this function does not own a reference to the
1004 * Return value: the displayed pixbuf, or %NULL if the image is empty
1007 gtk_image_get_pixbuf (GtkImage *image)
1009 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1010 g_return_val_if_fail (image->storage_type == GTK_IMAGE_PIXBUF ||
1011 image->storage_type == GTK_IMAGE_EMPTY, NULL);
1013 if (image->storage_type == GTK_IMAGE_EMPTY)
1014 image->data.pixbuf.pixbuf = NULL;
1016 return image->data.pixbuf.pixbuf;
1020 * gtk_image_get_stock:
1021 * @image: a #GtkImage
1022 * @stock_id: place to store a stock icon name
1023 * @size: place to store a stock icon size
1025 * Gets the stock icon name and size being displayed by the #GtkImage.
1026 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1027 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1028 * The returned string is owned by the #GtkImage and should not
1033 gtk_image_get_stock (GtkImage *image,
1037 g_return_if_fail (GTK_IS_IMAGE (image));
1038 g_return_if_fail (image->storage_type == GTK_IMAGE_STOCK ||
1039 image->storage_type == GTK_IMAGE_EMPTY);
1041 if (image->storage_type == GTK_IMAGE_EMPTY)
1042 image->data.stock.stock_id = NULL;
1045 *stock_id = image->data.stock.stock_id;
1048 *size = image->icon_size;
1052 * gtk_image_get_icon_set:
1053 * @image: a #GtkImage
1054 * @icon_set: location to store a #GtkIconSet
1055 * @size: location to store a stock icon size
1057 * Gets the icon set and size being displayed by the #GtkImage.
1058 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1059 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1063 gtk_image_get_icon_set (GtkImage *image,
1064 GtkIconSet **icon_set,
1067 g_return_if_fail (GTK_IS_IMAGE (image));
1068 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_SET ||
1069 image->storage_type == GTK_IMAGE_EMPTY);
1072 *icon_set = image->data.icon_set.icon_set;
1075 *size = image->icon_size;
1079 * gtk_image_get_animation:
1080 * @image: a #GtkImage
1083 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1084 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1085 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1086 * The caller of this function does not own a reference to the
1087 * returned animation.
1089 * Return value: the displayed animation, or %NULL if the image is empty
1092 gtk_image_get_animation (GtkImage *image)
1094 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1095 g_return_val_if_fail (image->storage_type == GTK_IMAGE_ANIMATION ||
1096 image->storage_type == GTK_IMAGE_EMPTY,
1099 if (image->storage_type == GTK_IMAGE_EMPTY)
1100 image->data.anim.anim = NULL;
1102 return image->data.anim.anim;
1108 * Creates a new empty #GtkImage widget.
1110 * Return value: a newly created #GtkImage widget.
1113 gtk_image_new (void)
1115 return g_object_new (GTK_TYPE_IMAGE, NULL);
1119 gtk_image_set (GtkImage *image,
1123 g_return_if_fail (GTK_IS_IMAGE (image));
1125 gtk_image_set_from_image (image, val, mask);
1129 gtk_image_get (GtkImage *image,
1133 g_return_if_fail (GTK_IS_IMAGE (image));
1135 gtk_image_get_image (image, val, mask);
1139 gtk_image_unmap (GtkWidget *widget)
1143 image = GTK_IMAGE (widget);
1145 if (image->storage_type == GTK_IMAGE_ANIMATION)
1147 /* Reset the animation */
1149 if (image->data.anim.frame_timeout)
1151 g_source_remove (image->data.anim.frame_timeout);
1152 image->data.anim.frame_timeout = 0;
1155 if (image->data.anim.iter)
1157 g_object_unref (G_OBJECT (image->data.anim.iter));
1158 image->data.anim.iter = NULL;
1162 if (GTK_WIDGET_CLASS (parent_class)->unmap)
1163 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
1167 animation_timeout (gpointer data)
1171 GDK_THREADS_ENTER ();
1173 image = GTK_IMAGE (data);
1175 image->data.anim.frame_timeout = 0;
1177 gdk_pixbuf_animation_iter_advance (image->data.anim.iter, NULL);
1179 if (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter) >= 0)
1180 image->data.anim.frame_timeout =
1181 g_timeout_add (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter),
1185 gtk_widget_queue_draw (GTK_WIDGET (image));
1187 GDK_THREADS_LEAVE ();
1193 gtk_image_expose (GtkWidget *widget,
1194 GdkEventExpose *event)
1196 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1197 g_return_val_if_fail (event != NULL, FALSE);
1199 if (GTK_WIDGET_MAPPED (widget) &&
1200 GTK_IMAGE (widget)->storage_type != GTK_IMAGE_EMPTY)
1204 GdkRectangle area, image_bound;
1209 gboolean needs_state_transform;
1211 image = GTK_IMAGE (widget);
1212 misc = GTK_MISC (widget);
1216 if (!gdk_rectangle_intersect (&area, &widget->allocation, &area))
1219 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1220 xalign = misc->xalign;
1222 xalign = 1.0 - misc->xalign;
1224 x = floor (widget->allocation.x + misc->xpad
1225 + ((widget->allocation.width - widget->requisition.width) * xalign)
1227 y = floor (widget->allocation.y + misc->ypad
1228 + ((widget->allocation.height - widget->requisition.height) * misc->yalign)
1233 image_bound.width = 0;
1234 image_bound.height = 0;
1238 needs_state_transform = GTK_WIDGET_STATE (widget) != GTK_STATE_NORMAL;
1240 switch (image->storage_type)
1242 case GTK_IMAGE_PIXMAP:
1244 gdk_drawable_get_size (image->data.pixmap.pixmap,
1246 &image_bound.height);
1248 if (gdk_rectangle_intersect (&image_bound, &area, &image_bound) &&
1249 needs_state_transform)
1251 pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1252 image->data.pixmap.pixmap,
1253 gtk_widget_get_colormap (widget),
1254 image_bound.x - x, image_bound.y - y,
1257 image_bound.height);
1265 case GTK_IMAGE_IMAGE:
1267 image_bound.width = image->data.image.image->width;
1268 image_bound.height = image->data.image.image->height;
1270 if (gdk_rectangle_intersect (&image_bound, &area, &image_bound) &&
1271 needs_state_transform)
1273 pixbuf = gdk_pixbuf_get_from_image (NULL,
1274 image->data.image.image,
1275 gtk_widget_get_colormap (widget),
1276 image_bound.x - x, image_bound.y - y,
1279 image_bound.height);
1286 case GTK_IMAGE_PIXBUF:
1287 image_bound.width = gdk_pixbuf_get_width (image->data.pixbuf.pixbuf);
1288 image_bound.height = gdk_pixbuf_get_height (image->data.pixbuf.pixbuf);
1290 if (gdk_rectangle_intersect (&image_bound, &area, &image_bound) &&
1291 needs_state_transform)
1293 pixbuf = gdk_pixbuf_new_subpixbuf (image->data.pixbuf.pixbuf,
1294 image_bound.x - x, image_bound.y - y,
1295 image_bound.width, image_bound.height);
1302 pixbuf = image->data.pixbuf.pixbuf;
1303 g_object_ref (G_OBJECT (pixbuf));
1307 case GTK_IMAGE_STOCK:
1308 pixbuf = gtk_widget_render_icon (widget,
1309 image->data.stock.stock_id,
1314 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1315 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1319 needs_state_transform = FALSE;
1322 case GTK_IMAGE_ICON_SET:
1324 gtk_icon_set_render_icon (image->data.icon_set.icon_set,
1326 gtk_widget_get_direction (widget),
1327 GTK_WIDGET_STATE (widget),
1334 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1335 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1339 needs_state_transform = FALSE;
1342 case GTK_IMAGE_ANIMATION:
1344 if (image->data.anim.iter == NULL)
1346 image->data.anim.iter = gdk_pixbuf_animation_get_iter (image->data.anim.anim, NULL);
1348 if (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter) >= 0)
1349 image->data.anim.frame_timeout =
1350 g_timeout_add (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter),
1355 image_bound.width = gdk_pixbuf_animation_get_width (image->data.anim.anim);
1356 image_bound.height = gdk_pixbuf_animation_get_height (image->data.anim.anim);
1358 /* don't advance the anim iter here, or we could get frame changes between two
1359 * exposes of different areas.
1362 pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (image->data.anim.iter);
1363 g_object_ref (G_OBJECT (pixbuf));
1367 case GTK_IMAGE_EMPTY:
1368 g_assert_not_reached ();
1374 gdk_gc_set_clip_mask (widget->style->black_gc, mask);
1375 gdk_gc_set_clip_origin (widget->style->black_gc, x, y);
1378 if (gdk_rectangle_intersect (&image_bound, &area, &image_bound))
1382 if (needs_state_transform)
1384 GtkIconSource *source;
1385 GdkPixbuf *rendered;
1387 source = gtk_icon_source_new ();
1388 gtk_icon_source_set_pixbuf (source, pixbuf);
1389 /* The size here is arbitrary; since size isn't
1390 * wildcarded in the souce, it isn't supposed to be
1391 * scaled by the engine function
1393 gtk_icon_source_set_size (source,
1394 GTK_ICON_SIZE_SMALL_TOOLBAR);
1395 gtk_icon_source_set_size_wildcarded (source, FALSE);
1397 rendered = gtk_style_render_icon (widget->style,
1399 gtk_widget_get_direction (widget),
1400 GTK_WIDGET_STATE (widget),
1406 gtk_icon_source_free (source);
1408 g_object_unref (G_OBJECT (pixbuf));
1414 gdk_pixbuf_render_to_drawable (pixbuf,
1416 widget->style->black_gc,
1423 GDK_RGB_DITHER_NORMAL,
1426 g_object_unref (G_OBJECT (pixbuf));
1432 switch (image->storage_type)
1434 case GTK_IMAGE_PIXMAP:
1435 gdk_draw_drawable (widget->window,
1436 widget->style->black_gc,
1437 image->data.pixmap.pixmap,
1438 image_bound.x - x, image_bound.y - y,
1439 image_bound.x, image_bound.y,
1440 image_bound.width, image_bound.height);
1443 case GTK_IMAGE_IMAGE:
1444 gdk_draw_image (widget->window,
1445 widget->style->black_gc,
1446 image->data.image.image,
1447 image_bound.x - x, image_bound.y - y,
1448 image_bound.x, image_bound.y,
1449 image_bound.width, image_bound.height);
1452 case GTK_IMAGE_PIXBUF:
1453 case GTK_IMAGE_STOCK:
1454 case GTK_IMAGE_ICON_SET:
1455 case GTK_IMAGE_ANIMATION:
1456 case GTK_IMAGE_EMPTY:
1457 g_assert_not_reached ();
1461 } /* if rectangle intersects */
1465 gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
1466 gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
1469 } /* if widget is drawable */
1475 gtk_image_clear (GtkImage *image)
1477 g_object_freeze_notify (G_OBJECT (image));
1479 if (image->storage_type != GTK_IMAGE_EMPTY)
1480 g_object_notify (G_OBJECT (image), "storage_type");
1484 g_object_unref (G_OBJECT (image->mask));
1486 g_object_notify (G_OBJECT (image), "mask");
1489 if (image->icon_size != DEFAULT_ICON_SIZE)
1491 image->icon_size = DEFAULT_ICON_SIZE;
1492 g_object_notify (G_OBJECT (image), "icon_size");
1495 switch (image->storage_type)
1497 case GTK_IMAGE_PIXMAP:
1499 if (image->data.pixmap.pixmap)
1500 g_object_unref (G_OBJECT (image->data.pixmap.pixmap));
1501 image->data.pixmap.pixmap = NULL;
1503 g_object_notify (G_OBJECT (image), "pixmap");
1507 case GTK_IMAGE_IMAGE:
1509 if (image->data.image.image)
1510 g_object_unref (G_OBJECT (image->data.image.image));
1511 image->data.image.image = NULL;
1513 g_object_notify (G_OBJECT (image), "image");
1517 case GTK_IMAGE_PIXBUF:
1519 if (image->data.pixbuf.pixbuf)
1520 g_object_unref (G_OBJECT (image->data.pixbuf.pixbuf));
1522 g_object_notify (G_OBJECT (image), "pixbuf");
1526 case GTK_IMAGE_STOCK:
1528 g_free (image->data.stock.stock_id);
1530 image->data.stock.stock_id = NULL;
1532 g_object_notify (G_OBJECT (image), "stock");
1535 case GTK_IMAGE_ICON_SET:
1536 if (image->data.icon_set.icon_set)
1537 gtk_icon_set_unref (image->data.icon_set.icon_set);
1538 image->data.icon_set.icon_set = NULL;
1540 g_object_notify (G_OBJECT (image), "icon_set");
1543 case GTK_IMAGE_ANIMATION:
1544 if (image->data.anim.frame_timeout)
1545 g_source_remove (image->data.anim.frame_timeout);
1547 if (image->data.anim.anim)
1548 g_object_unref (G_OBJECT (image->data.anim.anim));
1550 image->data.anim.frame_timeout = 0;
1551 image->data.anim.anim = NULL;
1553 g_object_notify (G_OBJECT (image), "pixbuf_animation");
1557 case GTK_IMAGE_EMPTY:
1563 image->storage_type = GTK_IMAGE_EMPTY;
1565 memset (&image->data, '\0', sizeof (image->data));
1567 g_object_thaw_notify (G_OBJECT (image));
1571 gtk_image_reset (GtkImage *image)
1573 gtk_image_clear (image);
1575 gtk_image_update_size (image, 0, 0);
1579 gtk_image_size_request (GtkWidget *widget,
1580 GtkRequisition *requisition)
1583 GdkPixbuf *pixbuf = NULL;
1585 image = GTK_IMAGE (widget);
1587 /* We update stock/icon set on every size request, because
1588 * the theme could have affected the size; for other kinds of
1589 * image, we just update the requisition when the image data
1593 switch (image->storage_type)
1595 case GTK_IMAGE_STOCK:
1596 pixbuf = gtk_widget_render_icon (GTK_WIDGET (image),
1597 image->data.stock.stock_id,
1602 case GTK_IMAGE_ICON_SET:
1603 pixbuf = gtk_icon_set_render_icon (image->data.icon_set.icon_set,
1605 gtk_widget_get_direction (widget),
1606 GTK_WIDGET_STATE (widget),
1618 GTK_WIDGET (image)->requisition.width = gdk_pixbuf_get_width (pixbuf) + GTK_MISC (image)->xpad * 2;
1619 GTK_WIDGET (image)->requisition.height = gdk_pixbuf_get_height (pixbuf) + GTK_MISC (image)->ypad * 2;
1621 g_object_unref (G_OBJECT (pixbuf));
1624 /* Chain up to default that simply reads current requisition */
1625 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
1629 gtk_image_update_size (GtkImage *image,
1633 GTK_WIDGET (image)->requisition.width = image_width + GTK_MISC (image)->xpad * 2;
1634 GTK_WIDGET (image)->requisition.height = image_height + GTK_MISC (image)->ypad * 2;
1636 if (GTK_WIDGET_VISIBLE (image))
1637 gtk_widget_queue_resize (GTK_WIDGET (image));