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/.
31 #include "gtkcontainer.h"
33 #include "gtkiconfactory.h"
35 #include "gtkicontheme.h"
37 #include "gtkprivate.h"
40 typedef struct _GtkImagePrivate GtkImagePrivate;
42 struct _GtkImagePrivate
44 /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
48 guint need_calc_size : 1;
51 #define GTK_IMAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_IMAGE, GtkImagePrivate))
54 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
55 static gint gtk_image_expose (GtkWidget *widget,
56 GdkEventExpose *event);
57 static void gtk_image_unmap (GtkWidget *widget);
58 static void gtk_image_unrealize (GtkWidget *widget);
59 static void gtk_image_size_request (GtkWidget *widget,
60 GtkRequisition *requisition);
61 static void gtk_image_style_set (GtkWidget *widget,
62 GtkStyle *prev_style);
63 static void gtk_image_screen_changed (GtkWidget *widget,
64 GdkScreen *prev_screen);
65 static void gtk_image_destroy (GtkObject *object);
66 static void gtk_image_reset (GtkImage *image);
67 static void gtk_image_calc_size (GtkImage *image);
69 static void gtk_image_update_size (GtkImage *image,
73 static void gtk_image_set_property (GObject *object,
77 static void gtk_image_get_property (GObject *object,
82 static void icon_theme_changed (GtkImage *image);
96 PROP_PIXBUF_ANIMATION,
102 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
105 gtk_image_class_init (GtkImageClass *class)
107 GObjectClass *gobject_class;
108 GtkObjectClass *object_class;
109 GtkWidgetClass *widget_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;
125 widget_class->unrealize = gtk_image_unrealize;
126 widget_class->style_set = gtk_image_style_set;
127 widget_class->screen_changed = gtk_image_screen_changed;
129 g_object_class_install_property (gobject_class,
131 g_param_spec_object ("pixbuf",
133 P_("A GdkPixbuf to display"),
135 GTK_PARAM_READWRITE));
137 g_object_class_install_property (gobject_class,
139 g_param_spec_object ("pixmap",
141 P_("A GdkPixmap to display"),
143 GTK_PARAM_READWRITE));
145 g_object_class_install_property (gobject_class,
147 g_param_spec_object ("image",
149 P_("A GdkImage to display"),
151 GTK_PARAM_READWRITE));
153 g_object_class_install_property (gobject_class,
155 g_param_spec_object ("mask",
157 P_("Mask bitmap to use with GdkImage or GdkPixmap"),
159 GTK_PARAM_READWRITE));
161 g_object_class_install_property (gobject_class,
163 g_param_spec_string ("file",
165 P_("Filename to load and display"),
167 GTK_PARAM_READWRITE));
170 g_object_class_install_property (gobject_class,
172 g_param_spec_string ("stock",
174 P_("Stock ID for a stock image to display"),
176 GTK_PARAM_READWRITE));
178 g_object_class_install_property (gobject_class,
180 g_param_spec_boxed ("icon-set",
182 P_("Icon set to display"),
184 GTK_PARAM_READWRITE));
186 g_object_class_install_property (gobject_class,
188 g_param_spec_int ("icon-size",
190 P_("Symbolic size to use for stock icon, icon set or named icon"),
193 GTK_PARAM_READWRITE));
195 * GtkImage:pixel-size:
197 * The "pixel-size" property can be used to specify a fixed size
198 * overriding the #GtkImage:icon-size property for images of type
199 * %GTK_IMAGE_ICON_NAME.
203 g_object_class_install_property (gobject_class,
205 g_param_spec_int ("pixel-size",
207 P_("Pixel size to use for named icon"),
210 GTK_PARAM_READWRITE));
212 g_object_class_install_property (gobject_class,
213 PROP_PIXBUF_ANIMATION,
214 g_param_spec_object ("pixbuf-animation",
216 P_("GdkPixbufAnimation to display"),
217 GDK_TYPE_PIXBUF_ANIMATION,
218 GTK_PARAM_READWRITE));
221 * GtkImage:icon-name:
223 * The name of the icon in the icon theme. If the icon theme is
224 * changed, the image will be updated automatically.
228 g_object_class_install_property (gobject_class,
230 g_param_spec_string ("icon-name",
232 P_("The name of the icon from the icon theme"),
234 GTK_PARAM_READWRITE));
239 * The GIcon displayed in the GtkImage. For themed icons,
240 * If the icon theme is changed, the image will be updated
245 g_object_class_install_property (gobject_class,
247 g_param_spec_object ("gicon",
249 P_("The GIcon being displayed"),
251 GTK_PARAM_READWRITE));
253 g_object_class_install_property (gobject_class,
255 g_param_spec_enum ("storage-type",
257 P_("The representation being used for image data"),
260 GTK_PARAM_READABLE));
262 g_type_class_add_private (object_class, sizeof (GtkImagePrivate));
266 gtk_image_init (GtkImage *image)
268 GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);
270 GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);
272 image->storage_type = GTK_IMAGE_EMPTY;
273 image->icon_size = DEFAULT_ICON_SIZE;
276 priv->pixel_size = -1;
278 priv->filename = NULL;
282 gtk_image_destroy (GtkObject *object)
284 GtkImage *image = GTK_IMAGE (object);
286 gtk_image_reset (image);
288 GTK_OBJECT_CLASS (gtk_image_parent_class)->destroy (object);
292 gtk_image_set_property (GObject *object,
299 image = GTK_IMAGE (object);
304 gtk_image_set_from_pixbuf (image,
305 g_value_get_object (value));
308 gtk_image_set_from_pixmap (image,
309 g_value_get_object (value),
313 gtk_image_set_from_image (image,
314 g_value_get_object (value),
318 if (image->storage_type == GTK_IMAGE_PIXMAP)
319 gtk_image_set_from_pixmap (image,
320 image->data.pixmap.pixmap,
321 g_value_get_object (value));
322 else if (image->storage_type == GTK_IMAGE_IMAGE)
323 gtk_image_set_from_image (image,
324 image->data.image.image,
325 g_value_get_object (value));
330 mask = g_value_get_object (value);
335 gtk_image_clear (image);
341 gtk_image_set_from_file (image, g_value_get_string (value));
344 gtk_image_set_from_stock (image, g_value_get_string (value),
348 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
352 if (image->storage_type == GTK_IMAGE_STOCK)
353 gtk_image_set_from_stock (image,
354 image->data.stock.stock_id,
355 g_value_get_int (value));
356 else if (image->storage_type == GTK_IMAGE_ICON_SET)
357 gtk_image_set_from_icon_set (image,
358 image->data.icon_set.icon_set,
359 g_value_get_int (value));
360 else if (image->storage_type == GTK_IMAGE_ICON_NAME)
361 gtk_image_set_from_icon_name (image,
362 image->data.name.icon_name,
363 g_value_get_int (value));
364 else if (image->storage_type == GTK_IMAGE_GICON)
365 gtk_image_set_from_gicon (image,
366 image->data.gicon.icon,
367 g_value_get_int (value));
369 /* Save to be used when STOCK, ICON_SET, ICON_NAME or GICON property comes in */
370 image->icon_size = g_value_get_int (value);
372 case PROP_PIXEL_SIZE:
373 gtk_image_set_pixel_size (image, g_value_get_int (value));
375 case PROP_PIXBUF_ANIMATION:
376 gtk_image_set_from_animation (image,
377 g_value_get_object (value));
380 gtk_image_set_from_icon_name (image, g_value_get_string (value),
384 gtk_image_set_from_gicon (image, g_value_get_object (value),
389 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
395 gtk_image_get_property (GObject *object,
401 GtkImagePrivate *priv;
403 image = GTK_IMAGE (object);
404 priv = GTK_IMAGE_GET_PRIVATE (image);
406 /* The "getter" functions whine if you try to get the wrong
407 * storage type. This function is instead robust against that,
408 * so that GUI builders don't have to jump through hoops
415 if (image->storage_type != GTK_IMAGE_PIXBUF)
416 g_value_set_object (value, NULL);
418 g_value_set_object (value,
419 gtk_image_get_pixbuf (image));
422 if (image->storage_type != GTK_IMAGE_PIXMAP)
423 g_value_set_object (value, NULL);
425 g_value_set_object (value,
426 image->data.pixmap.pixmap);
429 g_value_set_object (value, image->mask);
432 if (image->storage_type != GTK_IMAGE_IMAGE)
433 g_value_set_object (value, NULL);
435 g_value_set_object (value,
436 image->data.image.image);
439 g_value_set_string (value, priv->filename);
442 if (image->storage_type != GTK_IMAGE_STOCK)
443 g_value_set_string (value, NULL);
445 g_value_set_string (value,
446 image->data.stock.stock_id);
449 if (image->storage_type != GTK_IMAGE_ICON_SET)
450 g_value_set_boxed (value, NULL);
452 g_value_set_boxed (value,
453 image->data.icon_set.icon_set);
456 g_value_set_int (value, image->icon_size);
458 case PROP_PIXEL_SIZE:
459 g_value_set_int (value, priv->pixel_size);
461 case PROP_PIXBUF_ANIMATION:
462 if (image->storage_type != GTK_IMAGE_ANIMATION)
463 g_value_set_object (value, NULL);
465 g_value_set_object (value,
466 image->data.anim.anim);
469 if (image->storage_type != GTK_IMAGE_ICON_NAME)
470 g_value_set_string (value, NULL);
472 g_value_set_string (value,
473 image->data.name.icon_name);
476 if (image->storage_type != GTK_IMAGE_GICON)
477 g_value_set_object (value, NULL);
479 g_value_set_object (value,
480 image->data.gicon.icon);
482 case PROP_STORAGE_TYPE:
483 g_value_set_enum (value, image->storage_type);
487 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
494 * gtk_image_new_from_pixmap:
495 * @pixmap: (allow-none): a #GdkPixmap, or %NULL
496 * @mask: (allow-none): a #GdkBitmap, or %NULL
498 * Creates a #GtkImage widget displaying @pixmap with a @mask.
499 * A #GdkPixmap is a server-side image buffer in the pixel format of the
500 * current display. The #GtkImage does not assume a reference to the
501 * pixmap or mask; you still need to unref them if you own references.
502 * #GtkImage will add its own reference rather than adopting yours.
504 * Return value: a new #GtkImage
507 gtk_image_new_from_pixmap (GdkPixmap *pixmap,
512 image = g_object_new (GTK_TYPE_IMAGE, NULL);
514 gtk_image_set_from_pixmap (image, pixmap, mask);
516 return GTK_WIDGET (image);
520 * gtk_image_new_from_image:
521 * @image: (allow-none): a #GdkImage, or %NULL
522 * @mask: (allow-none): a #GdkBitmap, or %NULL
524 * Creates a #GtkImage widget displaying a @image with a @mask.
525 * A #GdkImage is a client-side image buffer in the pixel format of the
526 * current display. The #GtkImage does not assume a reference to the
527 * image or mask; you still need to unref them if you own references.
528 * #GtkImage will add its own reference rather than adopting yours.
530 * Return value: a new #GtkImage
533 gtk_image_new_from_image (GdkImage *gdk_image,
538 image = g_object_new (GTK_TYPE_IMAGE, NULL);
540 gtk_image_set_from_image (image, gdk_image, mask);
542 return GTK_WIDGET (image);
546 * gtk_image_new_from_file:
547 * @filename: a filename
549 * Creates a new #GtkImage displaying the file @filename. If the file
550 * isn't found or can't be loaded, the resulting #GtkImage will
551 * display a "broken image" icon. This function never returns %NULL,
552 * it always returns a valid #GtkImage widget.
554 * If the file contains an animation, the image will contain an
557 * If you need to detect failures to load the file, use
558 * gdk_pixbuf_new_from_file() to load the file yourself, then create
559 * the #GtkImage from the pixbuf. (Or for animations, use
560 * gdk_pixbuf_animation_new_from_file()).
562 * The storage type (gtk_image_get_storage_type()) of the returned
563 * image is not defined, it will be whatever is appropriate for
564 * displaying the file.
566 * Return value: a new #GtkImage
569 gtk_image_new_from_file (const gchar *filename)
573 image = g_object_new (GTK_TYPE_IMAGE, NULL);
575 gtk_image_set_from_file (image, filename);
577 return GTK_WIDGET (image);
581 * gtk_image_new_from_pixbuf:
582 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
584 * Creates a new #GtkImage displaying @pixbuf.
585 * The #GtkImage does not assume a reference to the
586 * pixbuf; you still need to unref it if you own references.
587 * #GtkImage will add its own reference rather than adopting yours.
589 * Note that this function just creates an #GtkImage from the pixbuf. The
590 * #GtkImage created will not react to state changes. Should you want that,
591 * you should use gtk_image_new_from_icon_set().
593 * Return value: a new #GtkImage
596 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
600 image = g_object_new (GTK_TYPE_IMAGE, NULL);
602 gtk_image_set_from_pixbuf (image, pixbuf);
604 return GTK_WIDGET (image);
608 * gtk_image_new_from_stock:
609 * @stock_id: a stock icon name
610 * @size: (type int): a stock icon size
612 * Creates a #GtkImage displaying a stock icon. Sample stock icon
613 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
614 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
615 * icon name isn't known, the image will be empty.
616 * You can register your own stock icon names, see
617 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
619 * Return value: a new #GtkImage displaying the stock icon
622 gtk_image_new_from_stock (const gchar *stock_id,
627 image = g_object_new (GTK_TYPE_IMAGE, NULL);
629 gtk_image_set_from_stock (image, stock_id, size);
631 return GTK_WIDGET (image);
635 * gtk_image_new_from_icon_set:
636 * @icon_set: a #GtkIconSet
637 * @size: (type int): a stock icon size
639 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
640 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
641 * this function, usually it's better to create a #GtkIconFactory, put
642 * your icon sets in the icon factory, add the icon factory to the
643 * list of default factories with gtk_icon_factory_add_default(), and
644 * then use gtk_image_new_from_stock(). This will allow themes to
645 * override the icon you ship with your application.
647 * The #GtkImage does not assume a reference to the
648 * icon set; you still need to unref it if you own references.
649 * #GtkImage will add its own reference rather than adopting yours.
651 * Return value: a new #GtkImage
654 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
659 image = g_object_new (GTK_TYPE_IMAGE, NULL);
661 gtk_image_set_from_icon_set (image, icon_set, size);
663 return GTK_WIDGET (image);
667 * gtk_image_new_from_animation:
668 * @animation: an animation
670 * Creates a #GtkImage displaying the given animation.
671 * The #GtkImage does not assume a reference to the
672 * animation; you still need to unref it if you own references.
673 * #GtkImage will add its own reference rather than adopting yours.
675 * Note that the animation frames are shown using a timeout with
676 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
677 * keep in mind that the animation will only be shown if the main loop
678 * is not busy with something that has a higher priority.
680 * Return value: a new #GtkImage widget
683 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
687 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
689 image = g_object_new (GTK_TYPE_IMAGE, NULL);
691 gtk_image_set_from_animation (image, animation);
693 return GTK_WIDGET (image);
697 * gtk_image_new_from_icon_name:
698 * @icon_name: an icon name
699 * @size: (type int): a stock icon size
701 * Creates a #GtkImage displaying an icon from the current icon theme.
702 * If the icon name isn't known, a "broken image" icon will be
703 * displayed instead. If the current icon theme is changed, the icon
704 * will be updated appropriately.
706 * Return value: a new #GtkImage displaying the themed icon
711 gtk_image_new_from_icon_name (const gchar *icon_name,
716 image = g_object_new (GTK_TYPE_IMAGE, NULL);
718 gtk_image_set_from_icon_name (image, icon_name, size);
720 return GTK_WIDGET (image);
724 * gtk_image_new_from_gicon:
726 * @size: (type int): a stock icon size
728 * Creates a #GtkImage displaying an icon from the current icon theme.
729 * If the icon name isn't known, a "broken image" icon will be
730 * displayed instead. If the current icon theme is changed, the icon
731 * will be updated appropriately.
733 * Return value: a new #GtkImage displaying the themed icon
738 gtk_image_new_from_gicon (GIcon *icon,
743 image = g_object_new (GTK_TYPE_IMAGE, NULL);
745 gtk_image_set_from_gicon (image, icon, size);
747 return GTK_WIDGET (image);
751 * gtk_image_set_from_pixmap:
752 * @image: a #GtkImage
753 * @pixmap: (allow-none): a #GdkPixmap or %NULL
754 * @mask: (allow-none): a #GdkBitmap or %NULL
756 * See gtk_image_new_from_pixmap() for details.
759 gtk_image_set_from_pixmap (GtkImage *image,
763 g_return_if_fail (GTK_IS_IMAGE (image));
764 g_return_if_fail (pixmap == NULL ||
765 GDK_IS_PIXMAP (pixmap));
766 g_return_if_fail (mask == NULL ||
767 GDK_IS_PIXMAP (mask));
769 g_object_freeze_notify (G_OBJECT (image));
772 g_object_ref (pixmap);
777 gtk_image_clear (image);
786 image->storage_type = GTK_IMAGE_PIXMAP;
788 image->data.pixmap.pixmap = pixmap;
790 gdk_drawable_get_size (GDK_DRAWABLE (pixmap), &width, &height);
792 gtk_image_update_size (image, width, height);
795 g_object_notify (G_OBJECT (image), "pixmap");
796 g_object_notify (G_OBJECT (image), "mask");
798 g_object_thaw_notify (G_OBJECT (image));
802 * gtk_image_set_from_image:
803 * @image: a #GtkImage
804 * @gdk_image: (allow-none): a #GdkImage or %NULL
805 * @mask: (allow-none): a #GdkBitmap or %NULL
807 * See gtk_image_new_from_image() for details.
810 gtk_image_set_from_image (GtkImage *image,
814 g_return_if_fail (GTK_IS_IMAGE (image));
815 g_return_if_fail (gdk_image == NULL ||
816 GDK_IS_IMAGE (gdk_image));
817 g_return_if_fail (mask == NULL ||
818 GDK_IS_PIXMAP (mask));
820 g_object_freeze_notify (G_OBJECT (image));
823 g_object_ref (gdk_image);
828 gtk_image_clear (image);
832 image->storage_type = GTK_IMAGE_IMAGE;
834 image->data.image.image = gdk_image;
837 gtk_image_update_size (image, gdk_image->width, gdk_image->height);
841 /* Clean up the mask if gdk_image was NULL */
843 g_object_unref (mask);
846 g_object_notify (G_OBJECT (image), "image");
847 g_object_notify (G_OBJECT (image), "mask");
849 g_object_thaw_notify (G_OBJECT (image));
853 * gtk_image_set_from_file:
854 * @image: a #GtkImage
855 * @filename: (allow-none): a filename or %NULL
857 * See gtk_image_new_from_file() for details.
860 gtk_image_set_from_file (GtkImage *image,
861 const gchar *filename)
863 GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);
864 GdkPixbufAnimation *anim;
866 g_return_if_fail (GTK_IS_IMAGE (image));
868 g_object_freeze_notify (G_OBJECT (image));
870 gtk_image_clear (image);
872 if (filename == NULL)
874 priv->filename = NULL;
875 g_object_thaw_notify (G_OBJECT (image));
879 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
883 gtk_image_set_from_stock (image,
884 GTK_STOCK_MISSING_IMAGE,
885 GTK_ICON_SIZE_BUTTON);
886 g_object_thaw_notify (G_OBJECT (image));
890 /* We could just unconditionally set_from_animation,
891 * but it's nicer for memory if we toss the animation
892 * if it's just a single pixbuf
895 if (gdk_pixbuf_animation_is_static_image (anim))
896 gtk_image_set_from_pixbuf (image,
897 gdk_pixbuf_animation_get_static_image (anim));
899 gtk_image_set_from_animation (image, anim);
901 g_object_unref (anim);
903 priv->filename = g_strdup (filename);
905 g_object_thaw_notify (G_OBJECT (image));
909 * gtk_image_set_from_pixbuf:
910 * @image: a #GtkImage
911 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
913 * See gtk_image_new_from_pixbuf() for details.
916 gtk_image_set_from_pixbuf (GtkImage *image,
919 g_return_if_fail (GTK_IS_IMAGE (image));
920 g_return_if_fail (pixbuf == NULL ||
921 GDK_IS_PIXBUF (pixbuf));
923 g_object_freeze_notify (G_OBJECT (image));
926 g_object_ref (pixbuf);
928 gtk_image_clear (image);
932 image->storage_type = GTK_IMAGE_PIXBUF;
934 image->data.pixbuf.pixbuf = pixbuf;
936 gtk_image_update_size (image,
937 gdk_pixbuf_get_width (pixbuf),
938 gdk_pixbuf_get_height (pixbuf));
941 g_object_notify (G_OBJECT (image), "pixbuf");
943 g_object_thaw_notify (G_OBJECT (image));
947 * gtk_image_set_from_stock:
948 * @image: a #GtkImage
949 * @stock_id: a stock icon name
950 * @size: (type int): a stock icon size
952 * See gtk_image_new_from_stock() for details.
955 gtk_image_set_from_stock (GtkImage *image,
956 const gchar *stock_id,
961 g_return_if_fail (GTK_IS_IMAGE (image));
963 g_object_freeze_notify (G_OBJECT (image));
965 /* in case stock_id == image->data.stock.stock_id */
966 new_id = g_strdup (stock_id);
968 gtk_image_clear (image);
972 image->storage_type = GTK_IMAGE_STOCK;
974 image->data.stock.stock_id = new_id;
975 image->icon_size = size;
977 /* Size is demand-computed in size request method
978 * if we're a stock image, since changing the
979 * style impacts the size request
983 g_object_notify (G_OBJECT (image), "stock");
984 g_object_notify (G_OBJECT (image), "icon-size");
986 g_object_thaw_notify (G_OBJECT (image));
990 * gtk_image_set_from_icon_set:
991 * @image: a #GtkImage
992 * @icon_set: a #GtkIconSet
993 * @size: (type int): a stock icon size
995 * See gtk_image_new_from_icon_set() for details.
998 gtk_image_set_from_icon_set (GtkImage *image,
999 GtkIconSet *icon_set,
1002 g_return_if_fail (GTK_IS_IMAGE (image));
1004 g_object_freeze_notify (G_OBJECT (image));
1007 gtk_icon_set_ref (icon_set);
1009 gtk_image_clear (image);
1013 image->storage_type = GTK_IMAGE_ICON_SET;
1015 image->data.icon_set.icon_set = icon_set;
1016 image->icon_size = size;
1018 /* Size is demand-computed in size request method
1019 * if we're an icon set
1023 g_object_notify (G_OBJECT (image), "icon-set");
1024 g_object_notify (G_OBJECT (image), "icon-size");
1026 g_object_thaw_notify (G_OBJECT (image));
1030 * gtk_image_set_from_animation:
1031 * @image: a #GtkImage
1032 * @animation: the #GdkPixbufAnimation
1034 * Causes the #GtkImage to display the given animation (or display
1035 * nothing, if you set the animation to %NULL).
1038 gtk_image_set_from_animation (GtkImage *image,
1039 GdkPixbufAnimation *animation)
1041 g_return_if_fail (GTK_IS_IMAGE (image));
1042 g_return_if_fail (animation == NULL ||
1043 GDK_IS_PIXBUF_ANIMATION (animation));
1045 g_object_freeze_notify (G_OBJECT (image));
1048 g_object_ref (animation);
1050 gtk_image_clear (image);
1052 if (animation != NULL)
1054 image->storage_type = GTK_IMAGE_ANIMATION;
1056 image->data.anim.anim = animation;
1057 image->data.anim.frame_timeout = 0;
1058 image->data.anim.iter = NULL;
1060 gtk_image_update_size (image,
1061 gdk_pixbuf_animation_get_width (animation),
1062 gdk_pixbuf_animation_get_height (animation));
1065 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1067 g_object_thaw_notify (G_OBJECT (image));
1071 * gtk_image_set_from_icon_name:
1072 * @image: a #GtkImage
1073 * @icon_name: an icon name
1074 * @size: (type int): an icon size
1076 * See gtk_image_new_from_icon_name() for details.
1081 gtk_image_set_from_icon_name (GtkImage *image,
1082 const gchar *icon_name,
1087 g_return_if_fail (GTK_IS_IMAGE (image));
1089 g_object_freeze_notify (G_OBJECT (image));
1091 /* in case icon_name == image->data.name.icon_name */
1092 new_name = g_strdup (icon_name);
1094 gtk_image_clear (image);
1098 image->storage_type = GTK_IMAGE_ICON_NAME;
1100 image->data.name.icon_name = new_name;
1101 image->icon_size = size;
1103 /* Size is demand-computed in size request method
1104 * if we're a icon theme image, since changing the
1105 * style impacts the size request
1109 g_object_notify (G_OBJECT (image), "icon-name");
1110 g_object_notify (G_OBJECT (image), "icon-size");
1112 g_object_thaw_notify (G_OBJECT (image));
1116 * gtk_image_set_from_gicon:
1117 * @image: a #GtkImage
1119 * @size: (type int): an icon size
1121 * See gtk_image_new_from_gicon() for details.
1126 gtk_image_set_from_gicon (GtkImage *image,
1130 g_return_if_fail (GTK_IS_IMAGE (image));
1132 g_object_freeze_notify (G_OBJECT (image));
1134 /* in case icon == image->data.gicon.icon */
1136 g_object_ref (icon);
1138 gtk_image_clear (image);
1142 image->storage_type = GTK_IMAGE_GICON;
1144 image->data.gicon.icon = icon;
1145 image->icon_size = size;
1147 /* Size is demand-computed in size request method
1148 * if we're a icon theme image, since changing the
1149 * style impacts the size request
1153 g_object_notify (G_OBJECT (image), "gicon");
1154 g_object_notify (G_OBJECT (image), "icon-size");
1156 g_object_thaw_notify (G_OBJECT (image));
1160 * gtk_image_get_storage_type:
1161 * @image: a #GtkImage
1163 * Gets the type of representation being used by the #GtkImage
1164 * to store image data. If the #GtkImage has no image data,
1165 * the return value will be %GTK_IMAGE_EMPTY.
1167 * Return value: image representation being used
1170 gtk_image_get_storage_type (GtkImage *image)
1172 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1174 return image->storage_type;
1178 * gtk_image_get_pixmap:
1179 * @image: a #GtkImage
1180 * @pixmap: (out) (transfer none) (allow-none): location to store the
1182 * @mask: (out) (transfer none) (allow-none): location to store the
1185 * Gets the pixmap and mask being displayed by the #GtkImage.
1186 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1187 * %GTK_IMAGE_PIXMAP (see gtk_image_get_storage_type()).
1188 * The caller of this function does not own a reference to the
1189 * returned pixmap and mask.
1192 gtk_image_get_pixmap (GtkImage *image,
1196 g_return_if_fail (GTK_IS_IMAGE (image));
1197 g_return_if_fail (image->storage_type == GTK_IMAGE_PIXMAP ||
1198 image->storage_type == GTK_IMAGE_EMPTY);
1201 *pixmap = image->data.pixmap.pixmap;
1204 *mask = image->mask;
1208 * gtk_image_get_image:
1209 * @image: a #GtkImage
1210 * @gdk_image: (out) (transfer none) (allow-none): return location for
1211 * a #GtkImage, or %NULL
1212 * @mask: (out) (transfer none) (allow-none): return location for a
1213 * #GdkBitmap, or %NULL
1215 * Gets the #GdkImage and mask being displayed by the #GtkImage.
1216 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1217 * %GTK_IMAGE_IMAGE (see gtk_image_get_storage_type()).
1218 * The caller of this function does not own a reference to the
1219 * returned image and mask.
1222 gtk_image_get_image (GtkImage *image,
1223 GdkImage **gdk_image,
1226 g_return_if_fail (GTK_IS_IMAGE (image));
1227 g_return_if_fail (image->storage_type == GTK_IMAGE_IMAGE ||
1228 image->storage_type == GTK_IMAGE_EMPTY);
1231 *gdk_image = image->data.image.image;
1234 *mask = image->mask;
1238 * gtk_image_get_pixbuf:
1239 * @image: a #GtkImage
1241 * Gets the #GdkPixbuf being displayed by the #GtkImage.
1242 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1243 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1244 * The caller of this function does not own a reference to the
1247 * Return value: (transfer none): the displayed pixbuf, or %NULL if
1248 * the image is empty
1251 gtk_image_get_pixbuf (GtkImage *image)
1253 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1254 g_return_val_if_fail (image->storage_type == GTK_IMAGE_PIXBUF ||
1255 image->storage_type == GTK_IMAGE_EMPTY, NULL);
1257 if (image->storage_type == GTK_IMAGE_EMPTY)
1258 image->data.pixbuf.pixbuf = NULL;
1260 return image->data.pixbuf.pixbuf;
1264 * gtk_image_get_stock:
1265 * @image: a #GtkImage
1266 * @stock_id: (out) (transfer none) (allow-none): place to store a
1267 * stock icon name, or %NULL
1268 * @size: (out) (allow-none) (type int): place to store a stock icon
1271 * Gets the stock icon name and size being displayed by the #GtkImage.
1272 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1273 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1274 * The returned string is owned by the #GtkImage and should not
1278 gtk_image_get_stock (GtkImage *image,
1282 g_return_if_fail (GTK_IS_IMAGE (image));
1283 g_return_if_fail (image->storage_type == GTK_IMAGE_STOCK ||
1284 image->storage_type == GTK_IMAGE_EMPTY);
1286 if (image->storage_type == GTK_IMAGE_EMPTY)
1287 image->data.stock.stock_id = NULL;
1290 *stock_id = image->data.stock.stock_id;
1293 *size = image->icon_size;
1297 * gtk_image_get_icon_set:
1298 * @image: a #GtkImage
1299 * @icon_set: (out) (transfer none) (allow-none): location to store a
1300 * #GtkIconSet, or %NULL
1301 * @size: (out) (allow-none) (type int): location to store a stock
1302 * icon size, or %NULL
1304 * Gets the icon set and size being displayed by the #GtkImage.
1305 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1306 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1309 gtk_image_get_icon_set (GtkImage *image,
1310 GtkIconSet **icon_set,
1313 g_return_if_fail (GTK_IS_IMAGE (image));
1314 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_SET ||
1315 image->storage_type == GTK_IMAGE_EMPTY);
1318 *icon_set = image->data.icon_set.icon_set;
1321 *size = image->icon_size;
1325 * gtk_image_get_animation:
1326 * @image: a #GtkImage
1328 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1329 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1330 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1331 * The caller of this function does not own a reference to the
1332 * returned animation.
1334 * Return value: (transfer none): the displayed animation, or %NULL if
1335 * the image is empty
1338 gtk_image_get_animation (GtkImage *image)
1340 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1341 g_return_val_if_fail (image->storage_type == GTK_IMAGE_ANIMATION ||
1342 image->storage_type == GTK_IMAGE_EMPTY,
1345 if (image->storage_type == GTK_IMAGE_EMPTY)
1346 image->data.anim.anim = NULL;
1348 return image->data.anim.anim;
1352 * gtk_image_get_icon_name:
1353 * @image: a #GtkImage
1354 * @icon_name: (out) (transfer none) (allow-none): place to store an
1355 * icon name, or %NULL
1356 * @size: (out) (allow-none) (type int): place to store an icon size,
1359 * Gets the icon name and size being displayed by the #GtkImage.
1360 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1361 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1362 * The returned string is owned by the #GtkImage and should not
1368 gtk_image_get_icon_name (GtkImage *image,
1369 G_CONST_RETURN gchar **icon_name,
1372 g_return_if_fail (GTK_IS_IMAGE (image));
1373 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME ||
1374 image->storage_type == GTK_IMAGE_EMPTY);
1376 if (image->storage_type == GTK_IMAGE_EMPTY)
1377 image->data.name.icon_name = NULL;
1380 *icon_name = image->data.name.icon_name;
1383 *size = image->icon_size;
1387 * gtk_image_get_gicon:
1388 * @image: a #GtkImage
1389 * @gicon: (out) (transfer none) (allow-none): place to store a
1391 * @size: (out) (allow-none) (type int): place to store an icon size,
1394 * Gets the #GIcon and size being displayed by the #GtkImage.
1395 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1396 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1397 * The caller of this function does not own a reference to the
1403 gtk_image_get_gicon (GtkImage *image,
1407 g_return_if_fail (GTK_IS_IMAGE (image));
1408 g_return_if_fail (image->storage_type == GTK_IMAGE_GICON ||
1409 image->storage_type == GTK_IMAGE_EMPTY);
1411 if (image->storage_type == GTK_IMAGE_EMPTY)
1412 image->data.gicon.icon = NULL;
1415 *gicon = image->data.gicon.icon;
1418 *size = image->icon_size;
1424 * Creates a new empty #GtkImage widget.
1426 * Return value: a newly created #GtkImage widget.
1429 gtk_image_new (void)
1431 return g_object_new (GTK_TYPE_IMAGE, NULL);
1435 gtk_image_set (GtkImage *image,
1439 g_return_if_fail (GTK_IS_IMAGE (image));
1441 gtk_image_set_from_image (image, val, mask);
1445 gtk_image_get (GtkImage *image,
1449 g_return_if_fail (GTK_IS_IMAGE (image));
1451 gtk_image_get_image (image, val, mask);
1455 gtk_image_reset_anim_iter (GtkImage *image)
1457 if (image->storage_type == GTK_IMAGE_ANIMATION)
1459 /* Reset the animation */
1461 if (image->data.anim.frame_timeout)
1463 g_source_remove (image->data.anim.frame_timeout);
1464 image->data.anim.frame_timeout = 0;
1467 if (image->data.anim.iter)
1469 g_object_unref (image->data.anim.iter);
1470 image->data.anim.iter = NULL;
1476 gtk_image_unmap (GtkWidget *widget)
1478 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1480 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1484 gtk_image_unrealize (GtkWidget *widget)
1486 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1488 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1492 animation_timeout (gpointer data)
1497 image = GTK_IMAGE (data);
1499 image->data.anim.frame_timeout = 0;
1501 gdk_pixbuf_animation_iter_advance (image->data.anim.iter, NULL);
1503 delay = gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter);
1506 image->data.anim.frame_timeout =
1507 gdk_threads_add_timeout (delay, animation_timeout, image);
1509 gtk_widget_queue_draw (GTK_WIDGET (image));
1511 if (GTK_WIDGET_DRAWABLE (image))
1512 gdk_window_process_updates (GTK_WIDGET (image)->window, TRUE);
1519 icon_theme_changed (GtkImage *image)
1521 if (image->storage_type == GTK_IMAGE_ICON_NAME)
1523 if (image->data.name.pixbuf)
1524 g_object_unref (image->data.name.pixbuf);
1525 image->data.name.pixbuf = NULL;
1527 gtk_widget_queue_draw (GTK_WIDGET (image));
1529 if (image->storage_type == GTK_IMAGE_GICON)
1531 if (image->data.gicon.pixbuf)
1532 g_object_unref (image->data.gicon.pixbuf);
1533 image->data.gicon.pixbuf = NULL;
1535 gtk_widget_queue_draw (GTK_WIDGET (image));
1540 ensure_pixbuf_for_icon_name (GtkImage *image)
1542 GtkImagePrivate *priv;
1544 GtkIconTheme *icon_theme;
1545 GtkSettings *settings;
1547 gint *sizes, *s, dist;
1548 GtkIconLookupFlags flags;
1549 GError *error = NULL;
1551 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME);
1553 priv = GTK_IMAGE_GET_PRIVATE (image);
1554 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1555 icon_theme = gtk_icon_theme_get_for_screen (screen);
1556 settings = gtk_settings_get_for_screen (screen);
1557 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1558 if (image->data.name.pixbuf == NULL)
1560 if (priv->pixel_size != -1)
1562 width = height = priv->pixel_size;
1563 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1565 else if (!gtk_icon_size_lookup_for_settings (settings,
1569 if (image->icon_size == -1)
1571 /* Find an available size close to 48 */
1572 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, image->data.name.icon_name);
1574 width = height = 48;
1575 for (s = sizes; *s; s++)
1579 width = height = 48;
1586 width = height = *s;
1594 width = height = *s;
1603 g_warning ("Invalid icon size %d\n", image->icon_size);
1604 width = height = 24;
1607 image->data.name.pixbuf =
1608 gtk_icon_theme_load_icon (icon_theme,
1609 image->data.name.icon_name,
1610 MIN (width, height), flags, &error);
1611 if (image->data.name.pixbuf == NULL)
1613 g_error_free (error);
1614 image->data.name.pixbuf =
1615 gtk_widget_render_icon (GTK_WIDGET (image),
1616 GTK_STOCK_MISSING_IMAGE,
1624 ensure_pixbuf_for_gicon (GtkImage *image)
1626 GtkImagePrivate *priv;
1628 GtkIconTheme *icon_theme;
1629 GtkSettings *settings;
1632 GtkIconLookupFlags flags;
1634 g_return_if_fail (image->storage_type == GTK_IMAGE_GICON);
1636 priv = GTK_IMAGE_GET_PRIVATE (image);
1637 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1638 icon_theme = gtk_icon_theme_get_for_screen (screen);
1639 settings = gtk_settings_get_for_screen (screen);
1640 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1641 if (image->data.gicon.pixbuf == NULL)
1643 if (priv->pixel_size != -1)
1645 width = height = priv->pixel_size;
1646 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1648 else if (!gtk_icon_size_lookup_for_settings (settings,
1652 if (image->icon_size == -1)
1653 width = height = 48;
1656 g_warning ("Invalid icon size %d\n", image->icon_size);
1657 width = height = 24;
1661 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1662 image->data.gicon.icon,
1663 MIN (width, height), flags);
1666 image->data.gicon.pixbuf = gtk_icon_info_load_icon (info, NULL);
1667 gtk_icon_info_free (info);
1670 if (image->data.gicon.pixbuf == NULL)
1672 image->data.gicon.pixbuf =
1673 gtk_widget_render_icon (GTK_WIDGET (image),
1674 GTK_STOCK_MISSING_IMAGE,
1683 * Like gdk_rectangle_intersect (dest, src, dest), but make
1684 * sure that the origin of dest is moved by an "even" offset.
1685 * If necessary grow the intersection by one row or column
1688 * This is necessary since we can't pass alignment information
1689 * for the pixelation pattern down to gdk_pixbuf_saturate_and_pixelate(),
1690 * thus we have to makesure that the subimages are properly aligned.
1693 rectangle_intersect_even (GdkRectangle *src,
1701 isect = gdk_rectangle_intersect (dest, src, dest);
1703 if ((dest->x - x + dest->y - y) % 2 != 0)
1721 gtk_image_expose (GtkWidget *widget,
1722 GdkEventExpose *event)
1724 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1725 g_return_val_if_fail (event != NULL, FALSE);
1727 if (GTK_WIDGET_MAPPED (widget) &&
1728 GTK_IMAGE (widget)->storage_type != GTK_IMAGE_EMPTY)
1732 GtkImagePrivate *priv;
1733 GdkRectangle area, image_bound;
1735 gint x, y, mask_x, mask_y;
1738 gboolean needs_state_transform;
1740 image = GTK_IMAGE (widget);
1741 misc = GTK_MISC (widget);
1742 priv = GTK_IMAGE_GET_PRIVATE (image);
1746 /* For stock items and icon sets, we lazily calculate
1747 * the size; we might get here between a queue_resize()
1748 * and size_request() if something explicitely forces
1751 if (priv->need_calc_size)
1752 gtk_image_calc_size (image);
1754 if (!gdk_rectangle_intersect (&area, &widget->allocation, &area))
1757 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1758 xalign = misc->xalign;
1760 xalign = 1.0 - misc->xalign;
1762 x = floor (widget->allocation.x + misc->xpad
1763 + ((widget->allocation.width - widget->requisition.width) * xalign));
1764 y = floor (widget->allocation.y + misc->ypad
1765 + ((widget->allocation.height - widget->requisition.height) * misc->yalign));
1771 image_bound.width = 0;
1772 image_bound.height = 0;
1776 needs_state_transform = GTK_WIDGET_STATE (widget) != GTK_STATE_NORMAL;
1778 switch (image->storage_type)
1780 case GTK_IMAGE_PIXMAP:
1782 gdk_drawable_get_size (image->data.pixmap.pixmap,
1784 &image_bound.height);
1785 if (rectangle_intersect_even (&area, &image_bound) &&
1786 needs_state_transform)
1788 pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1789 image->data.pixmap.pixmap,
1790 gtk_widget_get_colormap (widget),
1791 image_bound.x - x, image_bound.y - y,
1794 image_bound.height);
1802 case GTK_IMAGE_IMAGE:
1804 image_bound.width = image->data.image.image->width;
1805 image_bound.height = image->data.image.image->height;
1807 if (rectangle_intersect_even (&area, &image_bound) &&
1808 needs_state_transform)
1810 pixbuf = gdk_pixbuf_get_from_image (NULL,
1811 image->data.image.image,
1812 gtk_widget_get_colormap (widget),
1813 image_bound.x - x, image_bound.y - y,
1816 image_bound.height);
1823 case GTK_IMAGE_PIXBUF:
1824 image_bound.width = gdk_pixbuf_get_width (image->data.pixbuf.pixbuf);
1825 image_bound.height = gdk_pixbuf_get_height (image->data.pixbuf.pixbuf);
1827 if (rectangle_intersect_even (&area, &image_bound) &&
1828 needs_state_transform)
1830 pixbuf = gdk_pixbuf_new_subpixbuf (image->data.pixbuf.pixbuf,
1831 image_bound.x - x, image_bound.y - y,
1832 image_bound.width, image_bound.height);
1839 pixbuf = image->data.pixbuf.pixbuf;
1840 g_object_ref (pixbuf);
1844 case GTK_IMAGE_STOCK:
1845 pixbuf = gtk_widget_render_icon (widget,
1846 image->data.stock.stock_id,
1851 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1852 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1856 needs_state_transform = FALSE;
1859 case GTK_IMAGE_ICON_SET:
1861 gtk_icon_set_render_icon (image->data.icon_set.icon_set,
1863 gtk_widget_get_direction (widget),
1864 GTK_WIDGET_STATE (widget),
1871 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1872 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1876 needs_state_transform = FALSE;
1879 case GTK_IMAGE_ANIMATION:
1881 if (image->data.anim.iter == NULL)
1883 image->data.anim.iter = gdk_pixbuf_animation_get_iter (image->data.anim.anim, NULL);
1885 if (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter) >= 0)
1886 image->data.anim.frame_timeout =
1887 gdk_threads_add_timeout (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter),
1892 image_bound.width = gdk_pixbuf_animation_get_width (image->data.anim.anim);
1893 image_bound.height = gdk_pixbuf_animation_get_height (image->data.anim.anim);
1895 /* don't advance the anim iter here, or we could get frame changes between two
1896 * exposes of different areas.
1899 pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (image->data.anim.iter);
1900 g_object_ref (pixbuf);
1904 case GTK_IMAGE_ICON_NAME:
1905 ensure_pixbuf_for_icon_name (image);
1906 pixbuf = image->data.name.pixbuf;
1909 g_object_ref (pixbuf);
1910 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1911 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1915 case GTK_IMAGE_GICON:
1916 ensure_pixbuf_for_gicon (image);
1917 pixbuf = image->data.gicon.pixbuf;
1920 g_object_ref (pixbuf);
1921 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1922 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1926 case GTK_IMAGE_EMPTY:
1927 g_assert_not_reached ();
1933 gdk_gc_set_clip_mask (widget->style->black_gc, mask);
1934 gdk_gc_set_clip_origin (widget->style->black_gc, mask_x, mask_y);
1937 if (rectangle_intersect_even (&area, &image_bound))
1941 if (needs_state_transform)
1943 GtkIconSource *source;
1944 GdkPixbuf *rendered;
1946 source = gtk_icon_source_new ();
1947 gtk_icon_source_set_pixbuf (source, pixbuf);
1948 /* The size here is arbitrary; since size isn't
1949 * wildcarded in the souce, it isn't supposed to be
1950 * scaled by the engine function
1952 gtk_icon_source_set_size (source,
1953 GTK_ICON_SIZE_SMALL_TOOLBAR);
1954 gtk_icon_source_set_size_wildcarded (source, FALSE);
1956 rendered = gtk_style_render_icon (widget->style,
1958 gtk_widget_get_direction (widget),
1959 GTK_WIDGET_STATE (widget),
1965 gtk_icon_source_free (source);
1967 g_object_unref (pixbuf);
1973 gdk_draw_pixbuf (widget->window,
1974 widget->style->black_gc,
1982 GDK_RGB_DITHER_NORMAL,
1988 switch (image->storage_type)
1990 case GTK_IMAGE_PIXMAP:
1991 gdk_draw_drawable (widget->window,
1992 widget->style->black_gc,
1993 image->data.pixmap.pixmap,
1994 image_bound.x - x, image_bound.y - y,
1995 image_bound.x, image_bound.y,
1996 image_bound.width, image_bound.height);
1999 case GTK_IMAGE_IMAGE:
2000 gdk_draw_image (widget->window,
2001 widget->style->black_gc,
2002 image->data.image.image,
2003 image_bound.x - x, image_bound.y - y,
2004 image_bound.x, image_bound.y,
2005 image_bound.width, image_bound.height);
2008 case GTK_IMAGE_PIXBUF:
2009 case GTK_IMAGE_STOCK:
2010 case GTK_IMAGE_ICON_SET:
2011 case GTK_IMAGE_ANIMATION:
2012 case GTK_IMAGE_ICON_NAME:
2013 case GTK_IMAGE_EMPTY:
2014 case GTK_IMAGE_GICON:
2015 g_assert_not_reached ();
2019 } /* if rectangle intersects */
2023 gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
2024 gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
2028 g_object_unref (pixbuf);
2030 } /* if widget is drawable */
2036 gtk_image_reset (GtkImage *image)
2038 GtkImagePrivate *priv;
2040 priv = GTK_IMAGE_GET_PRIVATE (image);
2042 g_object_freeze_notify (G_OBJECT (image));
2044 if (image->storage_type != GTK_IMAGE_EMPTY)
2045 g_object_notify (G_OBJECT (image), "storage-type");
2049 g_object_unref (image->mask);
2051 g_object_notify (G_OBJECT (image), "mask");
2054 if (image->icon_size != DEFAULT_ICON_SIZE)
2056 image->icon_size = DEFAULT_ICON_SIZE;
2057 g_object_notify (G_OBJECT (image), "icon-size");
2060 switch (image->storage_type)
2062 case GTK_IMAGE_PIXMAP:
2064 if (image->data.pixmap.pixmap)
2065 g_object_unref (image->data.pixmap.pixmap);
2066 image->data.pixmap.pixmap = NULL;
2068 g_object_notify (G_OBJECT (image), "pixmap");
2072 case GTK_IMAGE_IMAGE:
2074 if (image->data.image.image)
2075 g_object_unref (image->data.image.image);
2076 image->data.image.image = NULL;
2078 g_object_notify (G_OBJECT (image), "image");
2082 case GTK_IMAGE_PIXBUF:
2084 if (image->data.pixbuf.pixbuf)
2085 g_object_unref (image->data.pixbuf.pixbuf);
2087 g_object_notify (G_OBJECT (image), "pixbuf");
2091 case GTK_IMAGE_STOCK:
2093 g_free (image->data.stock.stock_id);
2095 image->data.stock.stock_id = NULL;
2097 g_object_notify (G_OBJECT (image), "stock");
2100 case GTK_IMAGE_ICON_SET:
2101 if (image->data.icon_set.icon_set)
2102 gtk_icon_set_unref (image->data.icon_set.icon_set);
2103 image->data.icon_set.icon_set = NULL;
2105 g_object_notify (G_OBJECT (image), "icon-set");
2108 case GTK_IMAGE_ANIMATION:
2109 gtk_image_reset_anim_iter (image);
2111 if (image->data.anim.anim)
2112 g_object_unref (image->data.anim.anim);
2113 image->data.anim.anim = NULL;
2115 g_object_notify (G_OBJECT (image), "pixbuf-animation");
2119 case GTK_IMAGE_ICON_NAME:
2120 g_free (image->data.name.icon_name);
2121 image->data.name.icon_name = NULL;
2122 if (image->data.name.pixbuf)
2123 g_object_unref (image->data.name.pixbuf);
2124 image->data.name.pixbuf = NULL;
2126 g_object_notify (G_OBJECT (image), "icon-name");
2130 case GTK_IMAGE_GICON:
2131 if (image->data.gicon.icon)
2132 g_object_unref (image->data.gicon.icon);
2133 image->data.gicon.icon = NULL;
2134 if (image->data.gicon.pixbuf)
2135 g_object_unref (image->data.gicon.pixbuf);
2136 image->data.gicon.pixbuf = NULL;
2138 g_object_notify (G_OBJECT (image), "gicon");
2142 case GTK_IMAGE_EMPTY:
2150 g_free (priv->filename);
2151 priv->filename = NULL;
2152 g_object_notify (G_OBJECT (image), "file");
2155 image->storage_type = GTK_IMAGE_EMPTY;
2157 memset (&image->data, '\0', sizeof (image->data));
2159 g_object_thaw_notify (G_OBJECT (image));
2164 * @image: a #GtkImage
2166 * Resets the image to be empty.
2171 gtk_image_clear (GtkImage *image)
2173 GtkImagePrivate *priv;
2175 priv = GTK_IMAGE_GET_PRIVATE (image);
2177 priv->need_calc_size = 1;
2179 gtk_image_reset (image);
2180 gtk_image_update_size (image, 0, 0);
2184 gtk_image_calc_size (GtkImage *image)
2186 GtkWidget *widget = GTK_WIDGET (image);
2187 GdkPixbuf *pixbuf = NULL;
2188 GtkImagePrivate *priv;
2190 priv = GTK_IMAGE_GET_PRIVATE (image);
2192 priv->need_calc_size = 0;
2194 /* We update stock/icon set on every size request, because
2195 * the theme could have affected the size; for other kinds of
2196 * image, we just update the requisition when the image data
2199 switch (image->storage_type)
2201 case GTK_IMAGE_STOCK:
2202 pixbuf = gtk_widget_render_icon (widget,
2203 image->data.stock.stock_id,
2208 case GTK_IMAGE_ICON_SET:
2209 pixbuf = gtk_icon_set_render_icon (image->data.icon_set.icon_set,
2211 gtk_widget_get_direction (widget),
2212 GTK_WIDGET_STATE (widget),
2217 case GTK_IMAGE_ICON_NAME:
2218 ensure_pixbuf_for_icon_name (image);
2219 pixbuf = image->data.name.pixbuf;
2220 if (pixbuf) g_object_ref (pixbuf);
2222 case GTK_IMAGE_GICON:
2223 ensure_pixbuf_for_gicon (image);
2224 pixbuf = image->data.gicon.pixbuf;
2226 g_object_ref (pixbuf);
2234 widget->requisition.width = gdk_pixbuf_get_width (pixbuf) + GTK_MISC (image)->xpad * 2;
2235 widget->requisition.height = gdk_pixbuf_get_height (pixbuf) + GTK_MISC (image)->ypad * 2;
2237 g_object_unref (pixbuf);
2242 gtk_image_size_request (GtkWidget *widget,
2243 GtkRequisition *requisition)
2247 image = GTK_IMAGE (widget);
2249 gtk_image_calc_size (image);
2251 /* Chain up to default that simply reads current requisition */
2252 GTK_WIDGET_CLASS (gtk_image_parent_class)->size_request (widget, requisition);
2256 gtk_image_style_set (GtkWidget *widget,
2257 GtkStyle *prev_style)
2261 image = GTK_IMAGE (widget);
2263 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_set (widget, prev_style);
2265 icon_theme_changed (image);
2269 gtk_image_screen_changed (GtkWidget *widget,
2270 GdkScreen *prev_screen)
2274 image = GTK_IMAGE (widget);
2276 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
2277 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
2279 icon_theme_changed (image);
2284 gtk_image_update_size (GtkImage *image,
2288 GTK_WIDGET (image)->requisition.width = image_width + GTK_MISC (image)->xpad * 2;
2289 GTK_WIDGET (image)->requisition.height = image_height + GTK_MISC (image)->ypad * 2;
2291 if (GTK_WIDGET_VISIBLE (image))
2292 gtk_widget_queue_resize (GTK_WIDGET (image));
2297 * gtk_image_set_pixel_size:
2298 * @image: a #GtkImage
2299 * @pixel_size: the new pixel size
2301 * Sets the pixel size to use for named icons. If the pixel size is set
2302 * to a value != -1, it is used instead of the icon size set by
2303 * gtk_image_set_from_icon_name().
2308 gtk_image_set_pixel_size (GtkImage *image,
2311 GtkImagePrivate *priv;
2313 g_return_if_fail (GTK_IS_IMAGE (image));
2315 priv = GTK_IMAGE_GET_PRIVATE (image);
2317 if (priv->pixel_size != pixel_size)
2319 priv->pixel_size = pixel_size;
2321 if (image->storage_type == GTK_IMAGE_ICON_NAME)
2323 if (image->data.name.pixbuf)
2325 g_object_unref (image->data.name.pixbuf);
2326 image->data.name.pixbuf = NULL;
2329 gtk_image_update_size (image, pixel_size, pixel_size);
2332 if (image->storage_type == GTK_IMAGE_GICON)
2334 if (image->data.gicon.pixbuf)
2336 g_object_unref (image->data.gicon.pixbuf);
2337 image->data.gicon.pixbuf = NULL;
2340 gtk_image_update_size (image, pixel_size, pixel_size);
2343 g_object_notify (G_OBJECT (image), "pixel-size");
2348 * gtk_image_get_pixel_size:
2349 * @image: a #GtkImage
2351 * Gets the pixel size used for named icons.
2353 * Returns: the pixel size used for named icons.
2358 gtk_image_get_pixel_size (GtkImage *image)
2360 GtkImagePrivate *priv;
2362 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
2364 priv = GTK_IMAGE_GET_PRIVATE (image);
2366 return priv->pixel_size;
2369 #if defined (G_OS_WIN32) && !defined (_WIN64)
2371 #undef gtk_image_new_from_file
2374 gtk_image_new_from_file (const gchar *filename)
2376 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2379 retval = gtk_image_new_from_file_utf8 (utf8_filename);
2381 g_free (utf8_filename);
2386 #undef gtk_image_set_from_file
2389 gtk_image_set_from_file (GtkImage *image,
2390 const gchar *filename)
2392 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2394 gtk_image_set_from_file_utf8 (image, utf8_filename);
2396 g_free (utf8_filename);
2401 #define __GTK_IMAGE_C__
2402 #include "gtkaliasdef.c"