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 */
50 #define GTK_IMAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_IMAGE, GtkImagePrivate))
53 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
54 static gint gtk_image_expose (GtkWidget *widget,
55 GdkEventExpose *event);
56 static void gtk_image_unmap (GtkWidget *widget);
57 static void gtk_image_unrealize (GtkWidget *widget);
58 static void gtk_image_size_request (GtkWidget *widget,
59 GtkRequisition *requisition);
60 static void gtk_image_style_set (GtkWidget *widget,
61 GtkStyle *prev_style);
62 static void gtk_image_screen_changed (GtkWidget *widget,
63 GdkScreen *prev_screen);
64 static void gtk_image_destroy (GtkObject *object);
65 static void gtk_image_reset (GtkImage *image);
66 static void gtk_image_calc_size (GtkImage *image);
68 static void gtk_image_update_size (GtkImage *image,
72 static void gtk_image_set_property (GObject *object,
76 static void gtk_image_get_property (GObject *object,
81 static void icon_theme_changed (GtkImage *image);
95 PROP_PIXBUF_ANIMATION,
100 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
103 gtk_image_class_init (GtkImageClass *class)
105 GObjectClass *gobject_class;
106 GtkObjectClass *object_class;
107 GtkWidgetClass *widget_class;
109 gobject_class = G_OBJECT_CLASS (class);
111 gobject_class->set_property = gtk_image_set_property;
112 gobject_class->get_property = gtk_image_get_property;
114 object_class = GTK_OBJECT_CLASS (class);
116 object_class->destroy = gtk_image_destroy;
118 widget_class = GTK_WIDGET_CLASS (class);
120 widget_class->expose_event = gtk_image_expose;
121 widget_class->size_request = gtk_image_size_request;
122 widget_class->unmap = gtk_image_unmap;
123 widget_class->unrealize = gtk_image_unrealize;
124 widget_class->style_set = gtk_image_style_set;
125 widget_class->screen_changed = gtk_image_screen_changed;
127 g_object_class_install_property (gobject_class,
129 g_param_spec_object ("pixbuf",
131 P_("A GdkPixbuf to display"),
133 GTK_PARAM_READWRITE));
135 g_object_class_install_property (gobject_class,
137 g_param_spec_object ("pixmap",
139 P_("A GdkPixmap to display"),
141 GTK_PARAM_READWRITE));
143 g_object_class_install_property (gobject_class,
145 g_param_spec_object ("image",
147 P_("A GdkImage to display"),
149 GTK_PARAM_READWRITE));
151 g_object_class_install_property (gobject_class,
153 g_param_spec_object ("mask",
155 P_("Mask bitmap to use with GdkImage or GdkPixmap"),
157 GTK_PARAM_READWRITE));
159 g_object_class_install_property (gobject_class,
161 g_param_spec_string ("file",
163 P_("Filename to load and display"),
165 GTK_PARAM_READWRITE));
168 g_object_class_install_property (gobject_class,
170 g_param_spec_string ("stock",
172 P_("Stock ID for a stock image to display"),
174 GTK_PARAM_READWRITE));
176 g_object_class_install_property (gobject_class,
178 g_param_spec_boxed ("icon-set",
180 P_("Icon set to display"),
182 GTK_PARAM_READWRITE));
184 g_object_class_install_property (gobject_class,
186 g_param_spec_int ("icon-size",
188 P_("Symbolic size to use for stock icon, icon set or named icon"),
191 GTK_PARAM_READWRITE));
193 * GtkImage:pixel-size:
195 * The :pixel-size property can be used to specify a fixed size
196 * overriding the :icon-size property for images of type
197 * %GTK_IMAGE_ICON_NAME.
201 g_object_class_install_property (gobject_class,
203 g_param_spec_int ("pixel-size",
205 P_("Pixel size to use for named icon"),
208 GTK_PARAM_READWRITE));
210 g_object_class_install_property (gobject_class,
211 PROP_PIXBUF_ANIMATION,
212 g_param_spec_object ("pixbuf-animation",
214 P_("GdkPixbufAnimation to display"),
215 GDK_TYPE_PIXBUF_ANIMATION,
216 GTK_PARAM_READWRITE));
219 * GtkImage:icon-name:
221 * The name of the icon in the icon theme. If the icon theme is
222 * changed, the image will be updated automatically.
226 g_object_class_install_property (gobject_class,
228 g_param_spec_string ("icon-name",
230 P_("The name of the icon from the icon theme"),
232 GTK_PARAM_READWRITE));
234 g_object_class_install_property (gobject_class,
236 g_param_spec_enum ("storage-type",
238 P_("The representation being used for image data"),
241 GTK_PARAM_READABLE));
243 g_type_class_add_private (object_class, sizeof (GtkImagePrivate));
247 gtk_image_init (GtkImage *image)
249 GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);
251 GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);
253 image->storage_type = GTK_IMAGE_EMPTY;
254 image->icon_size = DEFAULT_ICON_SIZE;
257 priv->pixel_size = -1;
259 priv->filename = NULL;
263 gtk_image_destroy (GtkObject *object)
265 GtkImage *image = GTK_IMAGE (object);
267 gtk_image_reset (image);
269 GTK_OBJECT_CLASS (gtk_image_parent_class)->destroy (object);
273 gtk_image_set_property (GObject *object,
280 image = GTK_IMAGE (object);
285 gtk_image_set_from_pixbuf (image,
286 g_value_get_object (value));
289 gtk_image_set_from_pixmap (image,
290 g_value_get_object (value),
294 gtk_image_set_from_image (image,
295 g_value_get_object (value),
299 if (image->storage_type == GTK_IMAGE_PIXMAP)
300 gtk_image_set_from_pixmap (image,
301 image->data.pixmap.pixmap,
302 g_value_get_object (value));
303 else if (image->storage_type == GTK_IMAGE_IMAGE)
304 gtk_image_set_from_image (image,
305 image->data.image.image,
306 g_value_get_object (value));
311 mask = g_value_get_object (value);
316 gtk_image_clear (image);
322 gtk_image_set_from_file (image, g_value_get_string (value));
325 gtk_image_set_from_stock (image, g_value_get_string (value),
329 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
333 if (image->storage_type == GTK_IMAGE_STOCK)
334 gtk_image_set_from_stock (image,
335 image->data.stock.stock_id,
336 g_value_get_int (value));
337 else if (image->storage_type == GTK_IMAGE_ICON_SET)
338 gtk_image_set_from_icon_set (image,
339 image->data.icon_set.icon_set,
340 g_value_get_int (value));
341 else if (image->storage_type == GTK_IMAGE_ICON_NAME)
342 gtk_image_set_from_icon_name (image,
343 image->data.name.icon_name,
344 g_value_get_int (value));
346 /* Save to be used when STOCK or ICON_SET property comes in */
347 image->icon_size = g_value_get_int (value);
349 case PROP_PIXEL_SIZE:
350 gtk_image_set_pixel_size (image, g_value_get_int (value));
352 case PROP_PIXBUF_ANIMATION:
353 gtk_image_set_from_animation (image,
354 g_value_get_object (value));
357 gtk_image_set_from_icon_name (image, g_value_get_string (value),
362 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
368 gtk_image_get_property (GObject *object,
374 GtkImagePrivate *priv;
376 image = GTK_IMAGE (object);
377 priv = GTK_IMAGE_GET_PRIVATE (image);
379 /* The "getter" functions whine if you try to get the wrong
380 * storage type. This function is instead robust against that,
381 * so that GUI builders don't have to jump through hoops
388 if (image->storage_type != GTK_IMAGE_PIXBUF)
389 g_value_set_object (value, NULL);
391 g_value_set_object (value,
392 gtk_image_get_pixbuf (image));
395 if (image->storage_type != GTK_IMAGE_PIXMAP)
396 g_value_set_object (value, NULL);
398 g_value_set_object (value,
399 image->data.pixmap.pixmap);
402 g_value_set_object (value, image->mask);
405 if (image->storage_type != GTK_IMAGE_IMAGE)
406 g_value_set_object (value, NULL);
408 g_value_set_object (value,
409 image->data.image.image);
412 g_value_set_string (value, priv->filename);
415 if (image->storage_type != GTK_IMAGE_STOCK)
416 g_value_set_string (value, NULL);
418 g_value_set_string (value,
419 image->data.stock.stock_id);
422 if (image->storage_type != GTK_IMAGE_ICON_SET)
423 g_value_set_boxed (value, NULL);
425 g_value_set_boxed (value,
426 image->data.icon_set.icon_set);
429 g_value_set_int (value, image->icon_size);
431 case PROP_PIXEL_SIZE:
432 g_value_set_int (value, priv->pixel_size);
434 case PROP_PIXBUF_ANIMATION:
435 if (image->storage_type != GTK_IMAGE_ANIMATION)
436 g_value_set_object (value, NULL);
438 g_value_set_object (value,
439 image->data.anim.anim);
442 if (image->storage_type != GTK_IMAGE_ICON_NAME)
443 g_value_set_string (value, NULL);
445 g_value_set_string (value,
446 image->data.name.icon_name);
448 case PROP_STORAGE_TYPE:
449 g_value_set_enum (value, image->storage_type);
453 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
460 * gtk_image_new_from_pixmap:
461 * @pixmap: a #GdkPixmap, or %NULL
462 * @mask: a #GdkBitmap, or %NULL
464 * Creates a #GtkImage widget displaying @pixmap with a @mask.
465 * A #GdkPixmap is a server-side image buffer in the pixel format of the
466 * current display. The #GtkImage does not assume a reference to the
467 * pixmap or mask; you still need to unref them if you own references.
468 * #GtkImage will add its own reference rather than adopting yours.
470 * Return value: a new #GtkImage
473 gtk_image_new_from_pixmap (GdkPixmap *pixmap,
478 image = g_object_new (GTK_TYPE_IMAGE, NULL);
480 gtk_image_set_from_pixmap (image, pixmap, mask);
482 return GTK_WIDGET (image);
486 * gtk_image_new_from_image:
487 * @image: a #GdkImage, or %NULL
488 * @mask: a #GdkBitmap, or %NULL
490 * Creates a #GtkImage widget displaying a @image with a @mask.
491 * A #GdkImage is a client-side image buffer in the pixel format of the
493 * The #GtkImage does not assume a reference to the
494 * image or mask; you still need to unref them if you own references.
495 * #GtkImage will add its own reference rather than adopting yours.
497 * Return value: a new #GtkImage
500 gtk_image_new_from_image (GdkImage *gdk_image,
505 image = g_object_new (GTK_TYPE_IMAGE, NULL);
507 gtk_image_set_from_image (image, gdk_image, mask);
509 return GTK_WIDGET (image);
513 * gtk_image_new_from_file:
514 * @filename: a filename
516 * Creates a new #GtkImage displaying the file @filename. If the file
517 * isn't found or can't be loaded, the resulting #GtkImage will
518 * display a "broken image" icon. This function never returns %NULL,
519 * it always returns a valid #GtkImage widget.
521 * If the file contains an animation, the image will contain an
524 * If you need to detect failures to load the file, use
525 * gdk_pixbuf_new_from_file() to load the file yourself, then create
526 * the #GtkImage from the pixbuf. (Or for animations, use
527 * gdk_pixbuf_animation_new_from_file()).
529 * The storage type (gtk_image_get_storage_type()) of the returned
530 * image is not defined, it will be whatever is appropriate for
531 * displaying the file.
533 * Return value: a new #GtkImage
536 gtk_image_new_from_file (const gchar *filename)
540 image = g_object_new (GTK_TYPE_IMAGE, NULL);
542 gtk_image_set_from_file (image, filename);
544 return GTK_WIDGET (image);
548 * gtk_image_new_from_pixbuf:
549 * @pixbuf: a #GdkPixbuf, or %NULL
551 * Creates a new #GtkImage displaying @pixbuf.
552 * The #GtkImage does not assume a reference to the
553 * pixbuf; you still need to unref it if you own references.
554 * #GtkImage will add its own reference rather than adopting yours.
556 * Note that this function just creates an #GtkImage from the pixbuf. The
557 * #GtkImage created will not react to state changes. Should you want that, you
558 * should use gtk_image_new_from_icon_set().
560 * Return value: a new #GtkImage
563 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
567 image = g_object_new (GTK_TYPE_IMAGE, NULL);
569 gtk_image_set_from_pixbuf (image, pixbuf);
571 return GTK_WIDGET (image);
575 * gtk_image_new_from_stock:
576 * @stock_id: a stock icon name
577 * @size: a stock icon size
579 * Creates a #GtkImage displaying a stock icon. Sample stock icon
580 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
581 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
582 * icon name isn't known, the image will be empty.
583 * You can register your own stock icon names, see
584 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
586 * Return value: a new #GtkImage displaying the stock icon
589 gtk_image_new_from_stock (const gchar *stock_id,
594 image = g_object_new (GTK_TYPE_IMAGE, NULL);
596 gtk_image_set_from_stock (image, stock_id, size);
598 return GTK_WIDGET (image);
602 * gtk_image_new_from_icon_set:
603 * @icon_set: a #GtkIconSet
604 * @size: a stock icon size
606 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
607 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
608 * this function, usually it's better to create a #GtkIconFactory, put
609 * your icon sets in the icon factory, add the icon factory to the
610 * list of default factories with gtk_icon_factory_add_default(), and
611 * then use gtk_image_new_from_stock(). This will allow themes to
612 * override the icon you ship with your application.
614 * The #GtkImage does not assume a reference to the
615 * icon set; you still need to unref it if you own references.
616 * #GtkImage will add its own reference rather than adopting yours.
619 * Return value: a new #GtkImage
622 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
627 image = g_object_new (GTK_TYPE_IMAGE, NULL);
629 gtk_image_set_from_icon_set (image, icon_set, size);
631 return GTK_WIDGET (image);
635 * gtk_image_new_from_animation:
636 * @animation: an animation
638 * Creates a #GtkImage displaying the given animation.
639 * The #GtkImage does not assume a reference to the
640 * animation; you still need to unref it if you own references.
641 * #GtkImage will add its own reference rather than adopting yours.
643 * Note that the animation frames are shown using a timeout with
644 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
645 * keep in mind that the animation will only be shown if the main loop
646 * is not busy with something that has a higher priority.
648 * Return value: a new #GtkImage widget
651 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
655 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
657 image = g_object_new (GTK_TYPE_IMAGE, NULL);
659 gtk_image_set_from_animation (image, animation);
661 return GTK_WIDGET (image);
665 * gtk_image_new_from_icon_name:
666 * @icon_name: an icon name
667 * @size: a stock icon size
669 * Creates a #GtkImage displaying an icon from the current icon theme.
670 * If the icon name isn't known, a "broken image" icon will be
671 * displayed instead. If the current icon theme is changed, the icon
672 * will be updated appropriately.
674 * Return value: a new #GtkImage displaying the themed icon
679 gtk_image_new_from_icon_name (const gchar *icon_name,
684 image = g_object_new (GTK_TYPE_IMAGE, NULL);
686 gtk_image_set_from_icon_name (image, icon_name, size);
688 return GTK_WIDGET (image);
692 * gtk_image_set_from_pixmap:
693 * @image: a #GtkImage
694 * @pixmap: a #GdkPixmap or %NULL
695 * @mask: a #GdkBitmap or %NULL
697 * See gtk_image_new_from_pixmap() for details.
701 gtk_image_set_from_pixmap (GtkImage *image,
705 g_return_if_fail (GTK_IS_IMAGE (image));
706 g_return_if_fail (pixmap == NULL ||
707 GDK_IS_PIXMAP (pixmap));
708 g_return_if_fail (mask == NULL ||
709 GDK_IS_PIXMAP (mask));
711 g_object_freeze_notify (G_OBJECT (image));
714 g_object_ref (pixmap);
719 gtk_image_clear (image);
728 image->storage_type = GTK_IMAGE_PIXMAP;
730 image->data.pixmap.pixmap = pixmap;
732 gdk_drawable_get_size (GDK_DRAWABLE (pixmap), &width, &height);
734 gtk_image_update_size (image, width, height);
737 g_object_notify (G_OBJECT (image), "pixmap");
738 g_object_notify (G_OBJECT (image), "mask");
740 g_object_thaw_notify (G_OBJECT (image));
744 * gtk_image_set_from_image:
745 * @image: a #GtkImage
746 * @gdk_image: a #GdkImage or %NULL
747 * @mask: a #GdkBitmap or %NULL
749 * See gtk_image_new_from_image() for details.
753 gtk_image_set_from_image (GtkImage *image,
757 g_return_if_fail (GTK_IS_IMAGE (image));
758 g_return_if_fail (gdk_image == NULL ||
759 GDK_IS_IMAGE (gdk_image));
760 g_return_if_fail (mask == NULL ||
761 GDK_IS_PIXMAP (mask));
763 g_object_freeze_notify (G_OBJECT (image));
766 g_object_ref (gdk_image);
771 gtk_image_clear (image);
775 image->storage_type = GTK_IMAGE_IMAGE;
777 image->data.image.image = gdk_image;
780 gtk_image_update_size (image, gdk_image->width, gdk_image->height);
784 /* Clean up the mask if gdk_image was NULL */
786 g_object_unref (mask);
789 g_object_notify (G_OBJECT (image), "image");
790 g_object_notify (G_OBJECT (image), "mask");
792 g_object_thaw_notify (G_OBJECT (image));
796 * gtk_image_set_from_file:
797 * @image: a #GtkImage
798 * @filename: a filename or %NULL
800 * See gtk_image_new_from_file() for details.
804 gtk_image_set_from_file (GtkImage *image,
805 const gchar *filename)
807 GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);
808 GdkPixbufAnimation *anim;
810 g_return_if_fail (GTK_IS_IMAGE (image));
812 g_object_freeze_notify (G_OBJECT (image));
814 gtk_image_clear (image);
816 if (filename == NULL)
818 priv->filename = NULL;
819 g_object_thaw_notify (G_OBJECT (image));
823 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
827 gtk_image_set_from_stock (image,
828 GTK_STOCK_MISSING_IMAGE,
829 GTK_ICON_SIZE_BUTTON);
830 g_object_thaw_notify (G_OBJECT (image));
834 /* We could just unconditionally set_from_animation,
835 * but it's nicer for memory if we toss the animation
836 * if it's just a single pixbuf
839 if (gdk_pixbuf_animation_is_static_image (anim))
840 gtk_image_set_from_pixbuf (image,
841 gdk_pixbuf_animation_get_static_image (anim));
843 gtk_image_set_from_animation (image, anim);
845 g_object_unref (anim);
847 priv->filename = g_strdup (filename);
849 g_object_thaw_notify (G_OBJECT (image));
853 * gtk_image_set_from_pixbuf:
854 * @image: a #GtkImage
855 * @pixbuf: a #GdkPixbuf or %NULL
857 * See gtk_image_new_from_pixbuf() for details.
861 gtk_image_set_from_pixbuf (GtkImage *image,
864 g_return_if_fail (GTK_IS_IMAGE (image));
865 g_return_if_fail (pixbuf == NULL ||
866 GDK_IS_PIXBUF (pixbuf));
868 g_object_freeze_notify (G_OBJECT (image));
871 g_object_ref (pixbuf);
873 gtk_image_clear (image);
877 image->storage_type = GTK_IMAGE_PIXBUF;
879 image->data.pixbuf.pixbuf = pixbuf;
881 gtk_image_update_size (image,
882 gdk_pixbuf_get_width (pixbuf),
883 gdk_pixbuf_get_height (pixbuf));
886 g_object_notify (G_OBJECT (image), "pixbuf");
888 g_object_thaw_notify (G_OBJECT (image));
892 * gtk_image_set_from_stock:
893 * @image: a #GtkImage
894 * @stock_id: a stock icon name
895 * @size: a stock icon size
897 * See gtk_image_new_from_stock() for details.
901 gtk_image_set_from_stock (GtkImage *image,
902 const gchar *stock_id,
907 g_return_if_fail (GTK_IS_IMAGE (image));
909 g_object_freeze_notify (G_OBJECT (image));
911 /* in case stock_id == image->data.stock.stock_id */
912 new_id = g_strdup (stock_id);
914 gtk_image_clear (image);
918 image->storage_type = GTK_IMAGE_STOCK;
920 image->data.stock.stock_id = new_id;
921 image->icon_size = size;
923 /* Size is demand-computed in size request method
924 * if we're a stock image, since changing the
925 * style impacts the size request
929 g_object_notify (G_OBJECT (image), "stock");
930 g_object_notify (G_OBJECT (image), "icon-size");
932 g_object_thaw_notify (G_OBJECT (image));
936 * gtk_image_set_from_icon_set:
937 * @image: a #GtkImage
938 * @icon_set: a #GtkIconSet
939 * @size: a stock icon size
941 * See gtk_image_new_from_icon_set() for details.
945 gtk_image_set_from_icon_set (GtkImage *image,
946 GtkIconSet *icon_set,
949 g_return_if_fail (GTK_IS_IMAGE (image));
951 g_object_freeze_notify (G_OBJECT (image));
954 gtk_icon_set_ref (icon_set);
956 gtk_image_clear (image);
960 image->storage_type = GTK_IMAGE_ICON_SET;
962 image->data.icon_set.icon_set = icon_set;
963 image->icon_size = size;
965 /* Size is demand-computed in size request method
966 * if we're an icon set
970 g_object_notify (G_OBJECT (image), "icon-set");
971 g_object_notify (G_OBJECT (image), "icon-size");
973 g_object_thaw_notify (G_OBJECT (image));
977 * gtk_image_set_from_animation:
978 * @image: a #GtkImage
979 * @animation: the #GdkPixbufAnimation
981 * Causes the #GtkImage to display the given animation (or display
982 * nothing, if you set the animation to %NULL).
985 gtk_image_set_from_animation (GtkImage *image,
986 GdkPixbufAnimation *animation)
988 g_return_if_fail (GTK_IS_IMAGE (image));
989 g_return_if_fail (animation == NULL ||
990 GDK_IS_PIXBUF_ANIMATION (animation));
992 g_object_freeze_notify (G_OBJECT (image));
995 g_object_ref (animation);
997 gtk_image_clear (image);
999 if (animation != NULL)
1001 image->storage_type = GTK_IMAGE_ANIMATION;
1003 image->data.anim.anim = animation;
1004 image->data.anim.frame_timeout = 0;
1005 image->data.anim.iter = NULL;
1007 gtk_image_update_size (image,
1008 gdk_pixbuf_animation_get_width (animation),
1009 gdk_pixbuf_animation_get_height (animation));
1012 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1014 g_object_thaw_notify (G_OBJECT (image));
1018 * gtk_image_set_from_icon_name:
1019 * @image: a #GtkImage
1020 * @icon_name: an icon name
1021 * @size: an icon size
1023 * See gtk_image_new_from_icon_name() for details.
1028 gtk_image_set_from_icon_name (GtkImage *image,
1029 const gchar *icon_name,
1034 g_return_if_fail (GTK_IS_IMAGE (image));
1036 g_object_freeze_notify (G_OBJECT (image));
1038 /* in case icon_name == image->data.name.icon_name */
1039 new_name = g_strdup (icon_name);
1041 gtk_image_clear (image);
1045 image->storage_type = GTK_IMAGE_ICON_NAME;
1047 image->data.name.icon_name = new_name;
1048 image->icon_size = size;
1050 /* Size is demand-computed in size request method
1051 * if we're a icon theme image, since changing the
1052 * style impacts the size request
1056 g_object_notify (G_OBJECT (image), "icon-name");
1057 g_object_notify (G_OBJECT (image), "icon-size");
1059 g_object_thaw_notify (G_OBJECT (image));
1063 * gtk_image_get_storage_type:
1064 * @image: a #GtkImage
1066 * Gets the type of representation being used by the #GtkImage
1067 * to store image data. If the #GtkImage has no image data,
1068 * the return value will be %GTK_IMAGE_EMPTY.
1070 * Return value: image representation being used
1073 gtk_image_get_storage_type (GtkImage *image)
1075 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1077 return image->storage_type;
1081 * gtk_image_get_pixmap:
1082 * @image: a #GtkImage
1083 * @pixmap: location to store the pixmap, or %NULL
1084 * @mask: location to store the mask, or %NULL
1086 * Gets the pixmap and mask being displayed by the #GtkImage.
1087 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1088 * %GTK_IMAGE_PIXMAP (see gtk_image_get_storage_type()).
1089 * The caller of this function does not own a reference to the
1090 * returned pixmap and mask.
1094 gtk_image_get_pixmap (GtkImage *image,
1098 g_return_if_fail (GTK_IS_IMAGE (image));
1099 g_return_if_fail (image->storage_type == GTK_IMAGE_PIXMAP ||
1100 image->storage_type == GTK_IMAGE_EMPTY);
1103 *pixmap = image->data.pixmap.pixmap;
1106 *mask = image->mask;
1110 * gtk_image_get_image:
1111 * @image: a #GtkImage
1112 * @gdk_image: return location for a #GtkImage
1113 * @mask: return location for a #GdkBitmap
1115 * Gets the #GdkImage and mask being displayed by the #GtkImage.
1116 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1117 * %GTK_IMAGE_IMAGE (see gtk_image_get_storage_type()).
1118 * The caller of this function does not own a reference to the
1119 * returned image and mask.
1122 gtk_image_get_image (GtkImage *image,
1123 GdkImage **gdk_image,
1126 g_return_if_fail (GTK_IS_IMAGE (image));
1127 g_return_if_fail (image->storage_type == GTK_IMAGE_IMAGE ||
1128 image->storage_type == GTK_IMAGE_EMPTY);
1131 *gdk_image = image->data.image.image;
1134 *mask = image->mask;
1138 * gtk_image_get_pixbuf:
1139 * @image: a #GtkImage
1142 * Gets the #GdkPixbuf being displayed by the #GtkImage.
1143 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1144 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1145 * The caller of this function does not own a reference to the
1148 * Return value: the displayed pixbuf, or %NULL if the image is empty
1151 gtk_image_get_pixbuf (GtkImage *image)
1153 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1154 g_return_val_if_fail (image->storage_type == GTK_IMAGE_PIXBUF ||
1155 image->storage_type == GTK_IMAGE_EMPTY, NULL);
1157 if (image->storage_type == GTK_IMAGE_EMPTY)
1158 image->data.pixbuf.pixbuf = NULL;
1160 return image->data.pixbuf.pixbuf;
1164 * gtk_image_get_stock:
1165 * @image: a #GtkImage
1166 * @stock_id: place to store a stock icon name
1167 * @size: place to store a stock icon size
1169 * Gets the stock icon name and size being displayed by the #GtkImage.
1170 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1171 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1172 * The returned string is owned by the #GtkImage and should not
1177 gtk_image_get_stock (GtkImage *image,
1181 g_return_if_fail (GTK_IS_IMAGE (image));
1182 g_return_if_fail (image->storage_type == GTK_IMAGE_STOCK ||
1183 image->storage_type == GTK_IMAGE_EMPTY);
1185 if (image->storage_type == GTK_IMAGE_EMPTY)
1186 image->data.stock.stock_id = NULL;
1189 *stock_id = image->data.stock.stock_id;
1192 *size = image->icon_size;
1196 * gtk_image_get_icon_set:
1197 * @image: a #GtkImage
1198 * @icon_set: location to store a #GtkIconSet
1199 * @size: location to store a stock icon size
1201 * Gets the icon set and size being displayed by the #GtkImage.
1202 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1203 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1207 gtk_image_get_icon_set (GtkImage *image,
1208 GtkIconSet **icon_set,
1211 g_return_if_fail (GTK_IS_IMAGE (image));
1212 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_SET ||
1213 image->storage_type == GTK_IMAGE_EMPTY);
1216 *icon_set = image->data.icon_set.icon_set;
1219 *size = image->icon_size;
1223 * gtk_image_get_animation:
1224 * @image: a #GtkImage
1227 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1228 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1229 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1230 * The caller of this function does not own a reference to the
1231 * returned animation.
1233 * Return value: the displayed animation, or %NULL if the image is empty
1236 gtk_image_get_animation (GtkImage *image)
1238 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1239 g_return_val_if_fail (image->storage_type == GTK_IMAGE_ANIMATION ||
1240 image->storage_type == GTK_IMAGE_EMPTY,
1243 if (image->storage_type == GTK_IMAGE_EMPTY)
1244 image->data.anim.anim = NULL;
1246 return image->data.anim.anim;
1250 * gtk_image_get_icon_name:
1251 * @image: a #GtkImage
1252 * @icon_name: place to store an icon name
1253 * @size: place to store an icon size
1255 * Gets the icon name and size being displayed by the #GtkImage.
1256 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1257 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1258 * The returned string is owned by the #GtkImage and should not
1264 gtk_image_get_icon_name (GtkImage *image,
1265 G_CONST_RETURN gchar **icon_name,
1268 g_return_if_fail (GTK_IS_IMAGE (image));
1269 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME ||
1270 image->storage_type == GTK_IMAGE_EMPTY);
1272 if (image->storage_type == GTK_IMAGE_EMPTY)
1273 image->data.name.icon_name = NULL;
1276 *icon_name = image->data.name.icon_name;
1279 *size = image->icon_size;
1285 * Creates a new empty #GtkImage widget.
1287 * Return value: a newly created #GtkImage widget.
1290 gtk_image_new (void)
1292 return g_object_new (GTK_TYPE_IMAGE, NULL);
1296 gtk_image_set (GtkImage *image,
1300 g_return_if_fail (GTK_IS_IMAGE (image));
1302 gtk_image_set_from_image (image, val, mask);
1306 gtk_image_get (GtkImage *image,
1310 g_return_if_fail (GTK_IS_IMAGE (image));
1312 gtk_image_get_image (image, val, mask);
1316 gtk_image_reset_anim_iter (GtkImage *image)
1318 if (image->storage_type == GTK_IMAGE_ANIMATION)
1320 /* Reset the animation */
1322 if (image->data.anim.frame_timeout)
1324 g_source_remove (image->data.anim.frame_timeout);
1325 image->data.anim.frame_timeout = 0;
1328 if (image->data.anim.iter)
1330 g_object_unref (image->data.anim.iter);
1331 image->data.anim.iter = NULL;
1337 gtk_image_unmap (GtkWidget *widget)
1339 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1341 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap)
1342 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1346 gtk_image_unrealize (GtkWidget *widget)
1348 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1350 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize)
1351 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1355 animation_timeout (gpointer data)
1359 GDK_THREADS_ENTER ();
1361 image = GTK_IMAGE (data);
1363 image->data.anim.frame_timeout = 0;
1365 gdk_pixbuf_animation_iter_advance (image->data.anim.iter, NULL);
1367 if (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter) >= 0)
1368 image->data.anim.frame_timeout =
1369 g_timeout_add (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter),
1373 gtk_widget_queue_draw (GTK_WIDGET (image));
1375 if (GTK_WIDGET_DRAWABLE (image))
1376 gdk_window_process_updates (GTK_WIDGET (image)->window, TRUE);
1378 GDK_THREADS_LEAVE ();
1384 icon_theme_changed (GtkImage *image)
1386 if (image->data.name.pixbuf)
1387 g_object_unref (image->data.name.pixbuf);
1388 image->data.name.pixbuf = NULL;
1390 gtk_widget_queue_draw (GTK_WIDGET (image));
1394 ensure_pixbuf_for_icon_name (GtkImage *image)
1396 GtkImagePrivate *priv;
1398 GtkIconTheme *icon_theme;
1399 GtkSettings *settings;
1401 gint *sizes, *s, dist;
1402 GError *error = NULL;
1404 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME);
1406 priv = GTK_IMAGE_GET_PRIVATE (image);
1407 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1408 icon_theme = gtk_icon_theme_get_for_screen (screen);
1409 settings = gtk_settings_get_for_screen (screen);
1410 if (image->data.name.pixbuf == NULL)
1412 if (priv->pixel_size != -1)
1414 width = height = priv->pixel_size;
1416 else if (!gtk_icon_size_lookup_for_settings (settings,
1420 if (image->icon_size == -1)
1422 /* Find an available size close to 48 */
1423 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, image->data.name.icon_name);
1425 width = height = 48;
1426 for (s = sizes; *s; s++)
1430 width = height = 48;
1437 width = height = *s;
1445 width = height = *s;
1454 g_warning ("Invalid icon size %d\n", image->icon_size);
1455 width = height = 24;
1458 image->data.name.pixbuf =
1459 gtk_icon_theme_load_icon (icon_theme,
1460 image->data.name.icon_name,
1461 MIN (width, height), 0, &error);
1462 if (image->data.name.pixbuf == NULL)
1464 g_error_free (error);
1465 image->data.name.pixbuf =
1466 gtk_widget_render_icon (GTK_WIDGET (image),
1467 GTK_STOCK_MISSING_IMAGE,
1475 * Like gdk_rectangle_intersect (dest, src, dest), but make
1476 * sure that the origin of dest is moved by an "even" offset.
1477 * If necessary grow the intersection by one row or column
1480 * This is necessary since we can't pass alignment information
1481 * for the pixelation pattern down to gdk_pixbuf_saturate_and_pixelate(),
1482 * thus we have to makesure that the subimages are properly aligned.
1485 rectangle_intersect_even (GdkRectangle *src,
1493 isect = gdk_rectangle_intersect (dest, src, dest);
1495 if ((dest->x - x + dest->y - y) % 2 != 0)
1513 gtk_image_expose (GtkWidget *widget,
1514 GdkEventExpose *event)
1516 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1517 g_return_val_if_fail (event != NULL, FALSE);
1519 if (GTK_WIDGET_MAPPED (widget) &&
1520 GTK_IMAGE (widget)->storage_type != GTK_IMAGE_EMPTY)
1524 GdkRectangle area, image_bound;
1526 gint x, y, mask_x, mask_y;
1529 gboolean needs_state_transform;
1531 image = GTK_IMAGE (widget);
1532 misc = GTK_MISC (widget);
1536 /* For stock items and icon sets, we lazily calculate
1537 * the size; we might get here between a queue_resize()
1538 * and size_request() if something explicitely forces
1541 if (widget->requisition.width == 0 && widget->requisition.height == 0)
1542 gtk_image_calc_size (image);
1544 if (!gdk_rectangle_intersect (&area, &widget->allocation, &area))
1547 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1548 xalign = misc->xalign;
1550 xalign = 1.0 - misc->xalign;
1552 x = floor (widget->allocation.x + misc->xpad
1553 + ((widget->allocation.width - widget->requisition.width) * xalign));
1554 y = floor (widget->allocation.y + misc->ypad
1555 + ((widget->allocation.height - widget->requisition.height) * misc->yalign));
1561 image_bound.width = 0;
1562 image_bound.height = 0;
1566 needs_state_transform = GTK_WIDGET_STATE (widget) != GTK_STATE_NORMAL;
1568 switch (image->storage_type)
1570 case GTK_IMAGE_PIXMAP:
1572 gdk_drawable_get_size (image->data.pixmap.pixmap,
1574 &image_bound.height);
1575 if (rectangle_intersect_even (&area, &image_bound) &&
1576 needs_state_transform)
1578 pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1579 image->data.pixmap.pixmap,
1580 gtk_widget_get_colormap (widget),
1581 image_bound.x - x, image_bound.y - y,
1584 image_bound.height);
1592 case GTK_IMAGE_IMAGE:
1594 image_bound.width = image->data.image.image->width;
1595 image_bound.height = image->data.image.image->height;
1597 if (rectangle_intersect_even (&area, &image_bound) &&
1598 needs_state_transform)
1600 pixbuf = gdk_pixbuf_get_from_image (NULL,
1601 image->data.image.image,
1602 gtk_widget_get_colormap (widget),
1603 image_bound.x - x, image_bound.y - y,
1606 image_bound.height);
1613 case GTK_IMAGE_PIXBUF:
1614 image_bound.width = gdk_pixbuf_get_width (image->data.pixbuf.pixbuf);
1615 image_bound.height = gdk_pixbuf_get_height (image->data.pixbuf.pixbuf);
1617 if (rectangle_intersect_even (&area, &image_bound) &&
1618 needs_state_transform)
1620 pixbuf = gdk_pixbuf_new_subpixbuf (image->data.pixbuf.pixbuf,
1621 image_bound.x - x, image_bound.y - y,
1622 image_bound.width, image_bound.height);
1629 pixbuf = image->data.pixbuf.pixbuf;
1630 g_object_ref (pixbuf);
1634 case GTK_IMAGE_STOCK:
1635 pixbuf = gtk_widget_render_icon (widget,
1636 image->data.stock.stock_id,
1641 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1642 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1646 needs_state_transform = FALSE;
1649 case GTK_IMAGE_ICON_SET:
1651 gtk_icon_set_render_icon (image->data.icon_set.icon_set,
1653 gtk_widget_get_direction (widget),
1654 GTK_WIDGET_STATE (widget),
1661 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1662 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1666 needs_state_transform = FALSE;
1669 case GTK_IMAGE_ANIMATION:
1671 if (image->data.anim.iter == NULL)
1673 image->data.anim.iter = gdk_pixbuf_animation_get_iter (image->data.anim.anim, NULL);
1675 if (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter) >= 0)
1676 image->data.anim.frame_timeout =
1677 g_timeout_add (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter),
1682 image_bound.width = gdk_pixbuf_animation_get_width (image->data.anim.anim);
1683 image_bound.height = gdk_pixbuf_animation_get_height (image->data.anim.anim);
1685 /* don't advance the anim iter here, or we could get frame changes between two
1686 * exposes of different areas.
1689 pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (image->data.anim.iter);
1690 g_object_ref (pixbuf);
1694 case GTK_IMAGE_ICON_NAME:
1695 ensure_pixbuf_for_icon_name (image);
1696 pixbuf = image->data.name.pixbuf;
1699 g_object_ref (pixbuf);
1700 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1701 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1705 case GTK_IMAGE_EMPTY:
1706 g_assert_not_reached ();
1712 gdk_gc_set_clip_mask (widget->style->black_gc, mask);
1713 gdk_gc_set_clip_origin (widget->style->black_gc, mask_x, mask_y);
1716 if (rectangle_intersect_even (&area, &image_bound))
1720 if (needs_state_transform)
1722 GtkIconSource *source;
1723 GdkPixbuf *rendered;
1725 source = gtk_icon_source_new ();
1726 gtk_icon_source_set_pixbuf (source, pixbuf);
1727 /* The size here is arbitrary; since size isn't
1728 * wildcarded in the souce, it isn't supposed to be
1729 * scaled by the engine function
1731 gtk_icon_source_set_size (source,
1732 GTK_ICON_SIZE_SMALL_TOOLBAR);
1733 gtk_icon_source_set_size_wildcarded (source, FALSE);
1735 rendered = gtk_style_render_icon (widget->style,
1737 gtk_widget_get_direction (widget),
1738 GTK_WIDGET_STATE (widget),
1744 gtk_icon_source_free (source);
1746 g_object_unref (pixbuf);
1752 gdk_draw_pixbuf (widget->window,
1753 widget->style->black_gc,
1761 GDK_RGB_DITHER_NORMAL,
1767 switch (image->storage_type)
1769 case GTK_IMAGE_PIXMAP:
1770 gdk_draw_drawable (widget->window,
1771 widget->style->black_gc,
1772 image->data.pixmap.pixmap,
1773 image_bound.x - x, image_bound.y - y,
1774 image_bound.x, image_bound.y,
1775 image_bound.width, image_bound.height);
1778 case GTK_IMAGE_IMAGE:
1779 gdk_draw_image (widget->window,
1780 widget->style->black_gc,
1781 image->data.image.image,
1782 image_bound.x - x, image_bound.y - y,
1783 image_bound.x, image_bound.y,
1784 image_bound.width, image_bound.height);
1787 case GTK_IMAGE_PIXBUF:
1788 case GTK_IMAGE_STOCK:
1789 case GTK_IMAGE_ICON_SET:
1790 case GTK_IMAGE_ANIMATION:
1791 case GTK_IMAGE_ICON_NAME:
1792 case GTK_IMAGE_EMPTY:
1793 g_assert_not_reached ();
1797 } /* if rectangle intersects */
1801 gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
1802 gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
1806 g_object_unref (pixbuf);
1808 } /* if widget is drawable */
1814 gtk_image_reset (GtkImage *image)
1816 GtkImagePrivate *priv;
1818 priv = GTK_IMAGE_GET_PRIVATE (image);
1820 g_object_freeze_notify (G_OBJECT (image));
1822 if (image->storage_type != GTK_IMAGE_EMPTY)
1823 g_object_notify (G_OBJECT (image), "storage-type");
1827 g_object_unref (image->mask);
1829 g_object_notify (G_OBJECT (image), "mask");
1832 if (image->icon_size != DEFAULT_ICON_SIZE)
1834 image->icon_size = DEFAULT_ICON_SIZE;
1835 g_object_notify (G_OBJECT (image), "icon-size");
1838 switch (image->storage_type)
1840 case GTK_IMAGE_PIXMAP:
1842 if (image->data.pixmap.pixmap)
1843 g_object_unref (image->data.pixmap.pixmap);
1844 image->data.pixmap.pixmap = NULL;
1846 g_object_notify (G_OBJECT (image), "pixmap");
1850 case GTK_IMAGE_IMAGE:
1852 if (image->data.image.image)
1853 g_object_unref (image->data.image.image);
1854 image->data.image.image = NULL;
1856 g_object_notify (G_OBJECT (image), "image");
1860 case GTK_IMAGE_PIXBUF:
1862 if (image->data.pixbuf.pixbuf)
1863 g_object_unref (image->data.pixbuf.pixbuf);
1865 g_object_notify (G_OBJECT (image), "pixbuf");
1869 case GTK_IMAGE_STOCK:
1871 g_free (image->data.stock.stock_id);
1873 image->data.stock.stock_id = NULL;
1875 g_object_notify (G_OBJECT (image), "stock");
1878 case GTK_IMAGE_ICON_SET:
1879 if (image->data.icon_set.icon_set)
1880 gtk_icon_set_unref (image->data.icon_set.icon_set);
1881 image->data.icon_set.icon_set = NULL;
1883 g_object_notify (G_OBJECT (image), "icon-set");
1886 case GTK_IMAGE_ANIMATION:
1887 gtk_image_reset_anim_iter (image);
1889 if (image->data.anim.anim)
1890 g_object_unref (image->data.anim.anim);
1891 image->data.anim.anim = NULL;
1893 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1897 case GTK_IMAGE_ICON_NAME:
1898 if (image->data.name.icon_name)
1899 g_free (image->data.name.icon_name);
1900 image->data.name.icon_name = NULL;
1901 if (image->data.name.pixbuf)
1902 g_object_unref (image->data.name.pixbuf);
1903 image->data.name.pixbuf = NULL;
1905 g_object_notify (G_OBJECT (image), "icon-name");
1909 case GTK_IMAGE_EMPTY:
1917 g_free (priv->filename);
1918 priv->filename = NULL;
1919 g_object_notify (G_OBJECT (image), "file");
1922 image->storage_type = GTK_IMAGE_EMPTY;
1924 memset (&image->data, '\0', sizeof (image->data));
1926 g_object_thaw_notify (G_OBJECT (image));
1931 * @image: a #GtkImage
1933 * Resets the image to be empty.
1938 gtk_image_clear (GtkImage *image)
1940 gtk_image_reset (image);
1942 gtk_image_update_size (image, 0, 0);
1946 gtk_image_calc_size (GtkImage *image)
1948 GtkWidget *widget = GTK_WIDGET (image);
1949 GdkPixbuf *pixbuf = NULL;
1951 /* We update stock/icon set on every size request, because
1952 * the theme could have affected the size; for other kinds of
1953 * image, we just update the requisition when the image data
1956 switch (image->storage_type)
1958 case GTK_IMAGE_STOCK:
1959 pixbuf = gtk_widget_render_icon (widget,
1960 image->data.stock.stock_id,
1965 case GTK_IMAGE_ICON_SET:
1966 pixbuf = gtk_icon_set_render_icon (image->data.icon_set.icon_set,
1968 gtk_widget_get_direction (widget),
1969 GTK_WIDGET_STATE (widget),
1974 case GTK_IMAGE_ICON_NAME:
1975 ensure_pixbuf_for_icon_name (image);
1976 pixbuf = image->data.name.pixbuf;
1977 if (pixbuf) g_object_ref (pixbuf);
1985 widget->requisition.width = gdk_pixbuf_get_width (pixbuf) + GTK_MISC (image)->xpad * 2;
1986 widget->requisition.height = gdk_pixbuf_get_height (pixbuf) + GTK_MISC (image)->ypad * 2;
1988 g_object_unref (pixbuf);
1993 gtk_image_size_request (GtkWidget *widget,
1994 GtkRequisition *requisition)
1998 image = GTK_IMAGE (widget);
2000 gtk_image_calc_size (image);
2002 /* Chain up to default that simply reads current requisition */
2003 GTK_WIDGET_CLASS (gtk_image_parent_class)->size_request (widget, requisition);
2007 gtk_image_style_set (GtkWidget *widget,
2008 GtkStyle *prev_style)
2012 image = GTK_IMAGE (widget);
2014 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->style_set)
2015 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_set (widget, prev_style);
2017 icon_theme_changed (image);
2021 gtk_image_screen_changed (GtkWidget *widget,
2022 GdkScreen *prev_screen)
2026 image = GTK_IMAGE (widget);
2028 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
2029 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
2031 icon_theme_changed (image);
2036 gtk_image_update_size (GtkImage *image,
2040 GTK_WIDGET (image)->requisition.width = image_width + GTK_MISC (image)->xpad * 2;
2041 GTK_WIDGET (image)->requisition.height = image_height + GTK_MISC (image)->ypad * 2;
2043 if (GTK_WIDGET_VISIBLE (image))
2044 gtk_widget_queue_resize (GTK_WIDGET (image));
2049 * gtk_image_set_pixel_size:
2050 * @image: a #GtkImage
2051 * @pixel_size: the new pixel size
2053 * Sets the pixel size to use for named icons. If the pixel size is set
2054 * to a value != -1, it is used instead of the icon size set by
2055 * gtk_image_set_from_icon_name().
2060 gtk_image_set_pixel_size (GtkImage *image,
2063 GtkImagePrivate *priv;
2065 g_return_if_fail (GTK_IS_IMAGE (image));
2067 priv = GTK_IMAGE_GET_PRIVATE (image);
2069 if (priv->pixel_size != pixel_size)
2071 priv->pixel_size = pixel_size;
2073 if (image->storage_type == GTK_IMAGE_ICON_NAME)
2075 if (image->data.name.pixbuf)
2077 g_object_unref (image->data.name.pixbuf);
2078 image->data.name.pixbuf = NULL;
2081 gtk_image_update_size (image, pixel_size, pixel_size);
2084 g_object_notify (G_OBJECT (image), "pixel-size");
2089 * gtk_image_get_pixel_size:
2090 * @image: a #GtkImage
2092 * Gets the pixel size used for named icons.
2094 * Returns: the pixel size used for named icons.
2099 gtk_image_get_pixel_size (GtkImage *image)
2101 GtkImagePrivate *priv;
2103 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
2105 priv = GTK_IMAGE_GET_PRIVATE (image);
2107 return priv->pixel_size;
2112 #undef gtk_image_new_from_file
2115 gtk_image_new_from_file (const gchar *filename)
2117 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2120 retval = gtk_image_new_from_file_utf8 (utf8_filename);
2122 g_free (utf8_filename);
2127 #undef gtk_image_set_from_file
2130 gtk_image_set_from_file (GtkImage *image,
2131 const gchar *filename)
2133 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2135 gtk_image_set_from_file_utf8 (image, utf8_filename);
2137 g_free (utf8_filename);
2142 #define __GTK_IMAGE_C__
2143 #include "gtkaliasdef.c"