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"
42 * @Short_description: A widget displaying an image
44 * @See_also:#GdkPixbuf
46 * The #GtkImage widget displays an image. Various kinds of object
47 * can be displayed as an image; most typically, you would load a
48 * #GdkPixbuf ("pixel buffer") from a file, and then display that.
49 * There's a convenience function to do this, gtk_image_new_from_file(),
51 * <informalexample><programlisting>
53 * image = gtk_image_new_from_file ("myfile.png");
54 * </programlisting></informalexample>
55 * If the file isn't loaded successfully, the image will contain a
56 * "broken image" icon similar to that used in many web browsers.
57 * If you want to handle errors in loading the file yourself,
58 * for example by displaying an error message, then load the image with
59 * gdk_pixbuf_new_from_file(), then create the #GtkImage with
60 * gtk_image_new_from_pixbuf().
62 * The image file may contain an animation, if so the #GtkImage will
63 * display an animation (#GdkPixbufAnimation) instead of a static image.
65 * #GtkImage is a subclass of #GtkMisc, which implies that you can
66 * align it (center, left, right) and add padding to it, using
69 * #GtkImage is a "no window" widget (has no #GdkWindow of its own),
70 * so by default does not receive events. If you want to receive events
71 * on the image, such as button clicks, place the image inside a
72 * #GtkEventBox, then connect to the event signals on the event box.
74 * <title>Handling button press events on a
75 * <structname>GtkImage</structname>.</title>
78 * button_press_callback (GtkWidget *event_box,
79 * GdkEventButton *event,
82 * g_print ("Event box clicked at coordinates %f,%f\n",
83 * event->x, event->y);
85 * /<!---->* Returning TRUE means we handled the event, so the signal
86 * * emission should be stopped (don't call any further
87 * * callbacks that may be connected). Return FALSE
88 * * to continue invoking callbacks.
97 * GtkWidget *event_box;
99 * image = gtk_image_new_from_file ("myfile.png");
101 * event_box = gtk_event_box_new (<!-- -->);
103 * gtk_container_add (GTK_CONTAINER (event_box), image);
105 * g_signal_connect (G_OBJECT (event_box),
106 * "button_press_event",
107 * G_CALLBACK (button_press_callback),
115 * When handling events on the event box, keep in mind that coordinates
116 * in the image may be different from event box coordinates due to
117 * the alignment and padding settings on the image (see #GtkMisc).
118 * The simplest way to solve this is to set the alignment to 0.0
119 * (left/top), and set the padding to zero. Then the origin of
120 * the image will be the same as the origin of the event box.
122 * Sometimes an application will want to avoid depending on external data
123 * files, such as image files. GTK+ comes with a program to avoid this,
124 * called <application>gdk-pixbuf-csource</application>. This program
125 * allows you to convert an image into a C variable declaration, which
126 * can then be loaded into a #GdkPixbuf using
127 * gdk_pixbuf_new_from_inline().
130 typedef struct _GtkImagePrivate GtkImagePrivate;
132 struct _GtkImagePrivate
134 /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
137 /* a GtkStateType, with -1 meaning an invalid state,
138 * only used with GTK_IMAGE_GICON, GTK_IMAGE_ICON_NAME */
139 gint last_rendered_state;
140 gboolean was_symbolic;
143 guint need_calc_size : 1;
146 #define GTK_IMAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_IMAGE, GtkImagePrivate))
149 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
150 static gint gtk_image_expose (GtkWidget *widget,
151 GdkEventExpose *event);
152 static void gtk_image_unmap (GtkWidget *widget);
153 static void gtk_image_unrealize (GtkWidget *widget);
154 static void gtk_image_size_request (GtkWidget *widget,
155 GtkRequisition *requisition);
156 static void gtk_image_style_set (GtkWidget *widget,
157 GtkStyle *prev_style);
158 static void gtk_image_screen_changed (GtkWidget *widget,
159 GdkScreen *prev_screen);
160 static void gtk_image_destroy (GtkObject *object);
161 static void gtk_image_reset (GtkImage *image);
162 static void gtk_image_calc_size (GtkImage *image);
164 static void gtk_image_update_size (GtkImage *image,
168 static void gtk_image_set_property (GObject *object,
172 static void gtk_image_get_property (GObject *object,
177 static void icon_theme_changed (GtkImage *image);
191 PROP_PIXBUF_ANIMATION,
197 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
200 gtk_image_class_init (GtkImageClass *class)
202 GObjectClass *gobject_class;
203 GtkObjectClass *object_class;
204 GtkWidgetClass *widget_class;
206 gobject_class = G_OBJECT_CLASS (class);
208 gobject_class->set_property = gtk_image_set_property;
209 gobject_class->get_property = gtk_image_get_property;
211 object_class = GTK_OBJECT_CLASS (class);
213 object_class->destroy = gtk_image_destroy;
215 widget_class = GTK_WIDGET_CLASS (class);
217 widget_class->expose_event = gtk_image_expose;
218 widget_class->size_request = gtk_image_size_request;
219 widget_class->unmap = gtk_image_unmap;
220 widget_class->unrealize = gtk_image_unrealize;
221 widget_class->style_set = gtk_image_style_set;
222 widget_class->screen_changed = gtk_image_screen_changed;
224 g_object_class_install_property (gobject_class,
226 g_param_spec_object ("pixbuf",
228 P_("A GdkPixbuf to display"),
230 GTK_PARAM_READWRITE));
232 g_object_class_install_property (gobject_class,
234 g_param_spec_object ("pixmap",
236 P_("A GdkPixmap to display"),
238 GTK_PARAM_READWRITE));
240 g_object_class_install_property (gobject_class,
242 g_param_spec_object ("image",
244 P_("A GdkImage to display"),
246 GTK_PARAM_READWRITE));
248 g_object_class_install_property (gobject_class,
250 g_param_spec_object ("mask",
252 P_("Mask bitmap to use with GdkImage or GdkPixmap"),
254 GTK_PARAM_READWRITE));
256 g_object_class_install_property (gobject_class,
258 g_param_spec_string ("file",
260 P_("Filename to load and display"),
262 GTK_PARAM_READWRITE));
265 g_object_class_install_property (gobject_class,
267 g_param_spec_string ("stock",
269 P_("Stock ID for a stock image to display"),
271 GTK_PARAM_READWRITE));
273 g_object_class_install_property (gobject_class,
275 g_param_spec_boxed ("icon-set",
277 P_("Icon set to display"),
279 GTK_PARAM_READWRITE));
281 g_object_class_install_property (gobject_class,
283 g_param_spec_int ("icon-size",
285 P_("Symbolic size to use for stock icon, icon set or named icon"),
288 GTK_PARAM_READWRITE));
290 * GtkImage:pixel-size:
292 * The "pixel-size" property can be used to specify a fixed size
293 * overriding the #GtkImage:icon-size property for images of type
294 * %GTK_IMAGE_ICON_NAME.
298 g_object_class_install_property (gobject_class,
300 g_param_spec_int ("pixel-size",
302 P_("Pixel size to use for named icon"),
305 GTK_PARAM_READWRITE));
307 g_object_class_install_property (gobject_class,
308 PROP_PIXBUF_ANIMATION,
309 g_param_spec_object ("pixbuf-animation",
311 P_("GdkPixbufAnimation to display"),
312 GDK_TYPE_PIXBUF_ANIMATION,
313 GTK_PARAM_READWRITE));
316 * GtkImage:icon-name:
318 * The name of the icon in the icon theme. If the icon theme is
319 * changed, the image will be updated automatically.
323 g_object_class_install_property (gobject_class,
325 g_param_spec_string ("icon-name",
327 P_("The name of the icon from the icon theme"),
329 GTK_PARAM_READWRITE));
334 * The GIcon displayed in the GtkImage. For themed icons,
335 * If the icon theme is changed, the image will be updated
340 g_object_class_install_property (gobject_class,
342 g_param_spec_object ("gicon",
344 P_("The GIcon being displayed"),
346 GTK_PARAM_READWRITE));
348 g_object_class_install_property (gobject_class,
350 g_param_spec_enum ("storage-type",
352 P_("The representation being used for image data"),
355 GTK_PARAM_READABLE));
357 g_type_class_add_private (object_class, sizeof (GtkImagePrivate));
361 gtk_image_init (GtkImage *image)
363 GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);
365 gtk_widget_set_has_window (GTK_WIDGET (image), FALSE);
367 image->storage_type = GTK_IMAGE_EMPTY;
368 image->icon_size = DEFAULT_ICON_SIZE;
371 priv->pixel_size = -1;
373 priv->filename = NULL;
377 gtk_image_destroy (GtkObject *object)
379 GtkImage *image = GTK_IMAGE (object);
381 gtk_image_reset (image);
383 GTK_OBJECT_CLASS (gtk_image_parent_class)->destroy (object);
387 gtk_image_set_property (GObject *object,
394 image = GTK_IMAGE (object);
399 gtk_image_set_from_pixbuf (image,
400 g_value_get_object (value));
403 gtk_image_set_from_pixmap (image,
404 g_value_get_object (value),
408 gtk_image_set_from_image (image,
409 g_value_get_object (value),
413 if (image->storage_type == GTK_IMAGE_PIXMAP)
414 gtk_image_set_from_pixmap (image,
415 image->data.pixmap.pixmap,
416 g_value_get_object (value));
417 else if (image->storage_type == GTK_IMAGE_IMAGE)
418 gtk_image_set_from_image (image,
419 image->data.image.image,
420 g_value_get_object (value));
425 mask = g_value_get_object (value);
430 gtk_image_clear (image);
436 gtk_image_set_from_file (image, g_value_get_string (value));
439 gtk_image_set_from_stock (image, g_value_get_string (value),
443 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
447 if (image->storage_type == GTK_IMAGE_STOCK)
448 gtk_image_set_from_stock (image,
449 image->data.stock.stock_id,
450 g_value_get_int (value));
451 else if (image->storage_type == GTK_IMAGE_ICON_SET)
452 gtk_image_set_from_icon_set (image,
453 image->data.icon_set.icon_set,
454 g_value_get_int (value));
455 else if (image->storage_type == GTK_IMAGE_ICON_NAME)
456 gtk_image_set_from_icon_name (image,
457 image->data.name.icon_name,
458 g_value_get_int (value));
459 else if (image->storage_type == GTK_IMAGE_GICON)
460 gtk_image_set_from_gicon (image,
461 image->data.gicon.icon,
462 g_value_get_int (value));
464 /* Save to be used when STOCK, ICON_SET, ICON_NAME or GICON property comes in */
465 image->icon_size = g_value_get_int (value);
467 case PROP_PIXEL_SIZE:
468 gtk_image_set_pixel_size (image, g_value_get_int (value));
470 case PROP_PIXBUF_ANIMATION:
471 gtk_image_set_from_animation (image,
472 g_value_get_object (value));
475 gtk_image_set_from_icon_name (image, g_value_get_string (value),
479 gtk_image_set_from_gicon (image, g_value_get_object (value),
484 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
490 gtk_image_get_property (GObject *object,
496 GtkImagePrivate *priv;
498 image = GTK_IMAGE (object);
499 priv = GTK_IMAGE_GET_PRIVATE (image);
501 /* The "getter" functions whine if you try to get the wrong
502 * storage type. This function is instead robust against that,
503 * so that GUI builders don't have to jump through hoops
510 if (image->storage_type != GTK_IMAGE_PIXBUF)
511 g_value_set_object (value, NULL);
513 g_value_set_object (value,
514 gtk_image_get_pixbuf (image));
517 if (image->storage_type != GTK_IMAGE_PIXMAP)
518 g_value_set_object (value, NULL);
520 g_value_set_object (value,
521 image->data.pixmap.pixmap);
524 g_value_set_object (value, image->mask);
527 if (image->storage_type != GTK_IMAGE_IMAGE)
528 g_value_set_object (value, NULL);
530 g_value_set_object (value,
531 image->data.image.image);
534 g_value_set_string (value, priv->filename);
537 if (image->storage_type != GTK_IMAGE_STOCK)
538 g_value_set_string (value, NULL);
540 g_value_set_string (value,
541 image->data.stock.stock_id);
544 if (image->storage_type != GTK_IMAGE_ICON_SET)
545 g_value_set_boxed (value, NULL);
547 g_value_set_boxed (value,
548 image->data.icon_set.icon_set);
551 g_value_set_int (value, image->icon_size);
553 case PROP_PIXEL_SIZE:
554 g_value_set_int (value, priv->pixel_size);
556 case PROP_PIXBUF_ANIMATION:
557 if (image->storage_type != GTK_IMAGE_ANIMATION)
558 g_value_set_object (value, NULL);
560 g_value_set_object (value,
561 image->data.anim.anim);
564 if (image->storage_type != GTK_IMAGE_ICON_NAME)
565 g_value_set_string (value, NULL);
567 g_value_set_string (value,
568 image->data.name.icon_name);
571 if (image->storage_type != GTK_IMAGE_GICON)
572 g_value_set_object (value, NULL);
574 g_value_set_object (value,
575 image->data.gicon.icon);
577 case PROP_STORAGE_TYPE:
578 g_value_set_enum (value, image->storage_type);
582 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
589 * gtk_image_new_from_pixmap:
590 * @pixmap: (allow-none): a #GdkPixmap, or %NULL
591 * @mask: (allow-none): a #GdkBitmap, or %NULL
593 * Creates a #GtkImage widget displaying @pixmap with a @mask.
594 * A #GdkPixmap is a server-side image buffer in the pixel format of the
595 * current display. The #GtkImage does not assume a reference to the
596 * pixmap or mask; you still need to unref them if you own references.
597 * #GtkImage will add its own reference rather than adopting yours.
599 * Return value: a new #GtkImage
602 gtk_image_new_from_pixmap (GdkPixmap *pixmap,
607 image = g_object_new (GTK_TYPE_IMAGE, NULL);
609 gtk_image_set_from_pixmap (image, pixmap, mask);
611 return GTK_WIDGET (image);
615 * gtk_image_new_from_image:
616 * @image: (allow-none): a #GdkImage, or %NULL
617 * @mask: (allow-none): a #GdkBitmap, or %NULL
619 * Creates a #GtkImage widget displaying a @image with a @mask.
620 * A #GdkImage is a client-side image buffer in the pixel format of the
621 * current display. The #GtkImage does not assume a reference to the
622 * image or mask; you still need to unref them if you own references.
623 * #GtkImage will add its own reference rather than adopting yours.
625 * Return value: a new #GtkImage
628 gtk_image_new_from_image (GdkImage *gdk_image,
633 image = g_object_new (GTK_TYPE_IMAGE, NULL);
635 gtk_image_set_from_image (image, gdk_image, mask);
637 return GTK_WIDGET (image);
641 * gtk_image_new_from_file:
642 * @filename: a filename
644 * Creates a new #GtkImage displaying the file @filename. If the file
645 * isn't found or can't be loaded, the resulting #GtkImage will
646 * display a "broken image" icon. This function never returns %NULL,
647 * it always returns a valid #GtkImage widget.
649 * If the file contains an animation, the image will contain an
652 * If you need to detect failures to load the file, use
653 * gdk_pixbuf_new_from_file() to load the file yourself, then create
654 * the #GtkImage from the pixbuf. (Or for animations, use
655 * gdk_pixbuf_animation_new_from_file()).
657 * The storage type (gtk_image_get_storage_type()) of the returned
658 * image is not defined, it will be whatever is appropriate for
659 * displaying the file.
661 * Return value: a new #GtkImage
664 gtk_image_new_from_file (const gchar *filename)
668 image = g_object_new (GTK_TYPE_IMAGE, NULL);
670 gtk_image_set_from_file (image, filename);
672 return GTK_WIDGET (image);
676 * gtk_image_new_from_pixbuf:
677 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
679 * Creates a new #GtkImage displaying @pixbuf.
680 * The #GtkImage does not assume a reference to the
681 * pixbuf; you still need to unref it if you own references.
682 * #GtkImage will add its own reference rather than adopting yours.
684 * Note that this function just creates an #GtkImage from the pixbuf. The
685 * #GtkImage created will not react to state changes. Should you want that,
686 * you should use gtk_image_new_from_icon_set().
688 * Return value: a new #GtkImage
691 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
695 image = g_object_new (GTK_TYPE_IMAGE, NULL);
697 gtk_image_set_from_pixbuf (image, pixbuf);
699 return GTK_WIDGET (image);
703 * gtk_image_new_from_stock:
704 * @stock_id: a stock icon name
705 * @size: (type int): a stock icon size
707 * Creates a #GtkImage displaying a stock icon. Sample stock icon
708 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
709 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
710 * icon name isn't known, the image will be empty.
711 * You can register your own stock icon names, see
712 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
714 * Return value: a new #GtkImage displaying the stock icon
717 gtk_image_new_from_stock (const gchar *stock_id,
722 image = g_object_new (GTK_TYPE_IMAGE, NULL);
724 gtk_image_set_from_stock (image, stock_id, size);
726 return GTK_WIDGET (image);
730 * gtk_image_new_from_icon_set:
731 * @icon_set: a #GtkIconSet
732 * @size: (type int): a stock icon size
734 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
735 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
736 * this function, usually it's better to create a #GtkIconFactory, put
737 * your icon sets in the icon factory, add the icon factory to the
738 * list of default factories with gtk_icon_factory_add_default(), and
739 * then use gtk_image_new_from_stock(). This will allow themes to
740 * override the icon you ship with your application.
742 * The #GtkImage does not assume a reference to the
743 * icon set; you still need to unref it if you own references.
744 * #GtkImage will add its own reference rather than adopting yours.
746 * Return value: a new #GtkImage
749 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
754 image = g_object_new (GTK_TYPE_IMAGE, NULL);
756 gtk_image_set_from_icon_set (image, icon_set, size);
758 return GTK_WIDGET (image);
762 * gtk_image_new_from_animation:
763 * @animation: an animation
765 * Creates a #GtkImage displaying the given animation.
766 * The #GtkImage does not assume a reference to the
767 * animation; you still need to unref it if you own references.
768 * #GtkImage will add its own reference rather than adopting yours.
770 * Note that the animation frames are shown using a timeout with
771 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
772 * keep in mind that the animation will only be shown if the main loop
773 * is not busy with something that has a higher priority.
775 * Return value: a new #GtkImage widget
778 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
782 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
784 image = g_object_new (GTK_TYPE_IMAGE, NULL);
786 gtk_image_set_from_animation (image, animation);
788 return GTK_WIDGET (image);
792 * gtk_image_new_from_icon_name:
793 * @icon_name: an icon name
794 * @size: (type int): a stock icon size
796 * Creates a #GtkImage displaying an icon from the current icon theme.
797 * If the icon name isn't known, a "broken image" icon will be
798 * displayed instead. If the current icon theme is changed, the icon
799 * will be updated appropriately.
801 * Return value: a new #GtkImage displaying the themed icon
806 gtk_image_new_from_icon_name (const gchar *icon_name,
811 image = g_object_new (GTK_TYPE_IMAGE, NULL);
813 gtk_image_set_from_icon_name (image, icon_name, size);
815 return GTK_WIDGET (image);
819 * gtk_image_new_from_gicon:
821 * @size: (type int): a stock icon size
823 * Creates a #GtkImage displaying an icon from the current icon theme.
824 * If the icon name isn't known, a "broken image" icon will be
825 * displayed instead. If the current icon theme is changed, the icon
826 * will be updated appropriately.
828 * Return value: a new #GtkImage displaying the themed icon
833 gtk_image_new_from_gicon (GIcon *icon,
838 image = g_object_new (GTK_TYPE_IMAGE, NULL);
840 gtk_image_set_from_gicon (image, icon, size);
842 return GTK_WIDGET (image);
846 * gtk_image_set_from_pixmap:
847 * @image: a #GtkImage
848 * @pixmap: (allow-none): a #GdkPixmap or %NULL
849 * @mask: (allow-none): a #GdkBitmap or %NULL
851 * See gtk_image_new_from_pixmap() for details.
854 gtk_image_set_from_pixmap (GtkImage *image,
858 g_return_if_fail (GTK_IS_IMAGE (image));
859 g_return_if_fail (pixmap == NULL ||
860 GDK_IS_PIXMAP (pixmap));
861 g_return_if_fail (mask == NULL ||
862 GDK_IS_PIXMAP (mask));
864 g_object_freeze_notify (G_OBJECT (image));
867 g_object_ref (pixmap);
872 gtk_image_clear (image);
881 image->storage_type = GTK_IMAGE_PIXMAP;
883 image->data.pixmap.pixmap = pixmap;
885 gdk_drawable_get_size (GDK_DRAWABLE (pixmap), &width, &height);
887 gtk_image_update_size (image, width, height);
890 g_object_notify (G_OBJECT (image), "pixmap");
891 g_object_notify (G_OBJECT (image), "mask");
893 g_object_thaw_notify (G_OBJECT (image));
897 * gtk_image_set_from_image:
898 * @image: a #GtkImage
899 * @gdk_image: (allow-none): a #GdkImage or %NULL
900 * @mask: (allow-none): a #GdkBitmap or %NULL
902 * See gtk_image_new_from_image() for details.
905 gtk_image_set_from_image (GtkImage *image,
909 g_return_if_fail (GTK_IS_IMAGE (image));
910 g_return_if_fail (gdk_image == NULL ||
911 GDK_IS_IMAGE (gdk_image));
912 g_return_if_fail (mask == NULL ||
913 GDK_IS_PIXMAP (mask));
915 g_object_freeze_notify (G_OBJECT (image));
918 g_object_ref (gdk_image);
923 gtk_image_clear (image);
927 image->storage_type = GTK_IMAGE_IMAGE;
929 image->data.image.image = gdk_image;
932 gtk_image_update_size (image, gdk_image->width, gdk_image->height);
936 /* Clean up the mask if gdk_image was NULL */
938 g_object_unref (mask);
941 g_object_notify (G_OBJECT (image), "image");
942 g_object_notify (G_OBJECT (image), "mask");
944 g_object_thaw_notify (G_OBJECT (image));
948 * gtk_image_set_from_file:
949 * @image: a #GtkImage
950 * @filename: (allow-none): a filename or %NULL
952 * See gtk_image_new_from_file() for details.
955 gtk_image_set_from_file (GtkImage *image,
956 const gchar *filename)
958 GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);
959 GdkPixbufAnimation *anim;
961 g_return_if_fail (GTK_IS_IMAGE (image));
963 g_object_freeze_notify (G_OBJECT (image));
965 gtk_image_clear (image);
967 if (filename == NULL)
969 priv->filename = NULL;
970 g_object_thaw_notify (G_OBJECT (image));
974 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
978 gtk_image_set_from_stock (image,
979 GTK_STOCK_MISSING_IMAGE,
980 GTK_ICON_SIZE_BUTTON);
981 g_object_thaw_notify (G_OBJECT (image));
985 /* We could just unconditionally set_from_animation,
986 * but it's nicer for memory if we toss the animation
987 * if it's just a single pixbuf
990 if (gdk_pixbuf_animation_is_static_image (anim))
991 gtk_image_set_from_pixbuf (image,
992 gdk_pixbuf_animation_get_static_image (anim));
994 gtk_image_set_from_animation (image, anim);
996 g_object_unref (anim);
998 priv->filename = g_strdup (filename);
1000 g_object_thaw_notify (G_OBJECT (image));
1004 * gtk_image_set_from_pixbuf:
1005 * @image: a #GtkImage
1006 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
1008 * See gtk_image_new_from_pixbuf() for details.
1011 gtk_image_set_from_pixbuf (GtkImage *image,
1014 g_return_if_fail (GTK_IS_IMAGE (image));
1015 g_return_if_fail (pixbuf == NULL ||
1016 GDK_IS_PIXBUF (pixbuf));
1018 g_object_freeze_notify (G_OBJECT (image));
1021 g_object_ref (pixbuf);
1023 gtk_image_clear (image);
1027 image->storage_type = GTK_IMAGE_PIXBUF;
1029 image->data.pixbuf.pixbuf = pixbuf;
1031 gtk_image_update_size (image,
1032 gdk_pixbuf_get_width (pixbuf),
1033 gdk_pixbuf_get_height (pixbuf));
1036 g_object_notify (G_OBJECT (image), "pixbuf");
1038 g_object_thaw_notify (G_OBJECT (image));
1042 * gtk_image_set_from_stock:
1043 * @image: a #GtkImage
1044 * @stock_id: a stock icon name
1045 * @size: (type int): a stock icon size
1047 * See gtk_image_new_from_stock() for details.
1050 gtk_image_set_from_stock (GtkImage *image,
1051 const gchar *stock_id,
1056 g_return_if_fail (GTK_IS_IMAGE (image));
1058 g_object_freeze_notify (G_OBJECT (image));
1060 /* in case stock_id == image->data.stock.stock_id */
1061 new_id = g_strdup (stock_id);
1063 gtk_image_clear (image);
1067 image->storage_type = GTK_IMAGE_STOCK;
1069 image->data.stock.stock_id = new_id;
1070 image->icon_size = size;
1072 /* Size is demand-computed in size request method
1073 * if we're a stock image, since changing the
1074 * style impacts the size request
1078 g_object_notify (G_OBJECT (image), "stock");
1079 g_object_notify (G_OBJECT (image), "icon-size");
1081 g_object_thaw_notify (G_OBJECT (image));
1085 * gtk_image_set_from_icon_set:
1086 * @image: a #GtkImage
1087 * @icon_set: a #GtkIconSet
1088 * @size: (type int): a stock icon size
1090 * See gtk_image_new_from_icon_set() for details.
1093 gtk_image_set_from_icon_set (GtkImage *image,
1094 GtkIconSet *icon_set,
1097 g_return_if_fail (GTK_IS_IMAGE (image));
1099 g_object_freeze_notify (G_OBJECT (image));
1102 gtk_icon_set_ref (icon_set);
1104 gtk_image_clear (image);
1108 image->storage_type = GTK_IMAGE_ICON_SET;
1110 image->data.icon_set.icon_set = icon_set;
1111 image->icon_size = size;
1113 /* Size is demand-computed in size request method
1114 * if we're an icon set
1118 g_object_notify (G_OBJECT (image), "icon-set");
1119 g_object_notify (G_OBJECT (image), "icon-size");
1121 g_object_thaw_notify (G_OBJECT (image));
1125 * gtk_image_set_from_animation:
1126 * @image: a #GtkImage
1127 * @animation: the #GdkPixbufAnimation
1129 * Causes the #GtkImage to display the given animation (or display
1130 * nothing, if you set the animation to %NULL).
1133 gtk_image_set_from_animation (GtkImage *image,
1134 GdkPixbufAnimation *animation)
1136 g_return_if_fail (GTK_IS_IMAGE (image));
1137 g_return_if_fail (animation == NULL ||
1138 GDK_IS_PIXBUF_ANIMATION (animation));
1140 g_object_freeze_notify (G_OBJECT (image));
1143 g_object_ref (animation);
1145 gtk_image_clear (image);
1147 if (animation != NULL)
1149 image->storage_type = GTK_IMAGE_ANIMATION;
1151 image->data.anim.anim = animation;
1152 image->data.anim.frame_timeout = 0;
1153 image->data.anim.iter = NULL;
1155 gtk_image_update_size (image,
1156 gdk_pixbuf_animation_get_width (animation),
1157 gdk_pixbuf_animation_get_height (animation));
1160 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1162 g_object_thaw_notify (G_OBJECT (image));
1166 * gtk_image_set_from_icon_name:
1167 * @image: a #GtkImage
1168 * @icon_name: an icon name
1169 * @size: (type int): an icon size
1171 * See gtk_image_new_from_icon_name() for details.
1176 gtk_image_set_from_icon_name (GtkImage *image,
1177 const gchar *icon_name,
1182 g_return_if_fail (GTK_IS_IMAGE (image));
1184 g_object_freeze_notify (G_OBJECT (image));
1186 /* in case icon_name == image->data.name.icon_name */
1187 new_name = g_strdup (icon_name);
1189 gtk_image_clear (image);
1193 image->storage_type = GTK_IMAGE_ICON_NAME;
1195 image->data.name.icon_name = new_name;
1196 image->icon_size = size;
1198 /* Size is demand-computed in size request method
1199 * if we're a icon theme image, since changing the
1200 * style impacts the size request
1204 g_object_notify (G_OBJECT (image), "icon-name");
1205 g_object_notify (G_OBJECT (image), "icon-size");
1207 g_object_thaw_notify (G_OBJECT (image));
1211 * gtk_image_set_from_gicon:
1212 * @image: a #GtkImage
1214 * @size: (type int): an icon size
1216 * See gtk_image_new_from_gicon() for details.
1221 gtk_image_set_from_gicon (GtkImage *image,
1225 g_return_if_fail (GTK_IS_IMAGE (image));
1227 g_object_freeze_notify (G_OBJECT (image));
1229 /* in case icon == image->data.gicon.icon */
1231 g_object_ref (icon);
1233 gtk_image_clear (image);
1237 image->storage_type = GTK_IMAGE_GICON;
1239 image->data.gicon.icon = icon;
1240 image->icon_size = size;
1242 /* Size is demand-computed in size request method
1243 * if we're a icon theme image, since changing the
1244 * style impacts the size request
1248 g_object_notify (G_OBJECT (image), "gicon");
1249 g_object_notify (G_OBJECT (image), "icon-size");
1251 g_object_thaw_notify (G_OBJECT (image));
1255 * gtk_image_get_storage_type:
1256 * @image: a #GtkImage
1258 * Gets the type of representation being used by the #GtkImage
1259 * to store image data. If the #GtkImage has no image data,
1260 * the return value will be %GTK_IMAGE_EMPTY.
1262 * Return value: image representation being used
1265 gtk_image_get_storage_type (GtkImage *image)
1267 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1269 return image->storage_type;
1273 * gtk_image_get_pixmap:
1274 * @image: a #GtkImage
1275 * @pixmap: (out) (transfer none) (allow-none): location to store the
1277 * @mask: (out) (transfer none) (allow-none): location to store the
1280 * Gets the pixmap and mask being displayed by the #GtkImage.
1281 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1282 * %GTK_IMAGE_PIXMAP (see gtk_image_get_storage_type()).
1283 * The caller of this function does not own a reference to the
1284 * returned pixmap and mask.
1287 gtk_image_get_pixmap (GtkImage *image,
1291 g_return_if_fail (GTK_IS_IMAGE (image));
1292 g_return_if_fail (image->storage_type == GTK_IMAGE_PIXMAP ||
1293 image->storage_type == GTK_IMAGE_EMPTY);
1296 *pixmap = image->data.pixmap.pixmap;
1299 *mask = image->mask;
1303 * gtk_image_get_image:
1304 * @image: a #GtkImage
1305 * @gdk_image: (out) (transfer none) (allow-none): return location for
1306 * a #GtkImage, or %NULL
1307 * @mask: (out) (transfer none) (allow-none): return location for a
1308 * #GdkBitmap, or %NULL
1310 * Gets the #GdkImage and mask being displayed by the #GtkImage.
1311 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1312 * %GTK_IMAGE_IMAGE (see gtk_image_get_storage_type()).
1313 * The caller of this function does not own a reference to the
1314 * returned image and mask.
1317 gtk_image_get_image (GtkImage *image,
1318 GdkImage **gdk_image,
1321 g_return_if_fail (GTK_IS_IMAGE (image));
1322 g_return_if_fail (image->storage_type == GTK_IMAGE_IMAGE ||
1323 image->storage_type == GTK_IMAGE_EMPTY);
1326 *gdk_image = image->data.image.image;
1329 *mask = image->mask;
1333 * gtk_image_get_pixbuf:
1334 * @image: a #GtkImage
1336 * Gets the #GdkPixbuf being displayed by the #GtkImage.
1337 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1338 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1339 * The caller of this function does not own a reference to the
1342 * Return value: (transfer none): the displayed pixbuf, or %NULL if
1343 * the image is empty
1346 gtk_image_get_pixbuf (GtkImage *image)
1348 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1349 g_return_val_if_fail (image->storage_type == GTK_IMAGE_PIXBUF ||
1350 image->storage_type == GTK_IMAGE_EMPTY, NULL);
1352 if (image->storage_type == GTK_IMAGE_EMPTY)
1353 image->data.pixbuf.pixbuf = NULL;
1355 return image->data.pixbuf.pixbuf;
1359 * gtk_image_get_stock:
1360 * @image: a #GtkImage
1361 * @stock_id: (out) (transfer none) (allow-none): place to store a
1362 * stock icon name, or %NULL
1363 * @size: (out) (allow-none) (type int): place to store a stock icon
1366 * Gets the stock icon name and size being displayed by the #GtkImage.
1367 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1368 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1369 * The returned string is owned by the #GtkImage and should not
1373 gtk_image_get_stock (GtkImage *image,
1377 g_return_if_fail (GTK_IS_IMAGE (image));
1378 g_return_if_fail (image->storage_type == GTK_IMAGE_STOCK ||
1379 image->storage_type == GTK_IMAGE_EMPTY);
1381 if (image->storage_type == GTK_IMAGE_EMPTY)
1382 image->data.stock.stock_id = NULL;
1385 *stock_id = image->data.stock.stock_id;
1388 *size = image->icon_size;
1392 * gtk_image_get_icon_set:
1393 * @image: a #GtkImage
1394 * @icon_set: (out) (transfer none) (allow-none): location to store a
1395 * #GtkIconSet, or %NULL
1396 * @size: (out) (allow-none) (type int): location to store a stock
1397 * icon size, or %NULL
1399 * Gets the icon set and size being displayed by the #GtkImage.
1400 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1401 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1404 gtk_image_get_icon_set (GtkImage *image,
1405 GtkIconSet **icon_set,
1408 g_return_if_fail (GTK_IS_IMAGE (image));
1409 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_SET ||
1410 image->storage_type == GTK_IMAGE_EMPTY);
1413 *icon_set = image->data.icon_set.icon_set;
1416 *size = image->icon_size;
1420 * gtk_image_get_animation:
1421 * @image: a #GtkImage
1423 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1424 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1425 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1426 * The caller of this function does not own a reference to the
1427 * returned animation.
1429 * Return value: (transfer none): the displayed animation, or %NULL if
1430 * the image is empty
1433 gtk_image_get_animation (GtkImage *image)
1435 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1436 g_return_val_if_fail (image->storage_type == GTK_IMAGE_ANIMATION ||
1437 image->storage_type == GTK_IMAGE_EMPTY,
1440 if (image->storage_type == GTK_IMAGE_EMPTY)
1441 image->data.anim.anim = NULL;
1443 return image->data.anim.anim;
1447 * gtk_image_get_icon_name:
1448 * @image: a #GtkImage
1449 * @icon_name: (out) (transfer none) (allow-none): place to store an
1450 * icon name, or %NULL
1451 * @size: (out) (allow-none) (type int): place to store an icon size,
1454 * Gets the icon name and size being displayed by the #GtkImage.
1455 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1456 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1457 * The returned string is owned by the #GtkImage and should not
1463 gtk_image_get_icon_name (GtkImage *image,
1464 G_CONST_RETURN gchar **icon_name,
1467 g_return_if_fail (GTK_IS_IMAGE (image));
1468 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME ||
1469 image->storage_type == GTK_IMAGE_EMPTY);
1471 if (image->storage_type == GTK_IMAGE_EMPTY)
1472 image->data.name.icon_name = NULL;
1475 *icon_name = image->data.name.icon_name;
1478 *size = image->icon_size;
1482 * gtk_image_get_gicon:
1483 * @image: a #GtkImage
1484 * @gicon: (out) (transfer none) (allow-none): place to store a
1486 * @size: (out) (allow-none) (type int): place to store an icon size,
1489 * Gets the #GIcon and size being displayed by the #GtkImage.
1490 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1491 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1492 * The caller of this function does not own a reference to the
1498 gtk_image_get_gicon (GtkImage *image,
1502 g_return_if_fail (GTK_IS_IMAGE (image));
1503 g_return_if_fail (image->storage_type == GTK_IMAGE_GICON ||
1504 image->storage_type == GTK_IMAGE_EMPTY);
1506 if (image->storage_type == GTK_IMAGE_EMPTY)
1507 image->data.gicon.icon = NULL;
1510 *gicon = image->data.gicon.icon;
1513 *size = image->icon_size;
1519 * Creates a new empty #GtkImage widget.
1521 * Return value: a newly created #GtkImage widget.
1524 gtk_image_new (void)
1526 return g_object_new (GTK_TYPE_IMAGE, NULL);
1530 gtk_image_reset_anim_iter (GtkImage *image)
1532 if (image->storage_type == GTK_IMAGE_ANIMATION)
1534 /* Reset the animation */
1536 if (image->data.anim.frame_timeout)
1538 g_source_remove (image->data.anim.frame_timeout);
1539 image->data.anim.frame_timeout = 0;
1542 if (image->data.anim.iter)
1544 g_object_unref (image->data.anim.iter);
1545 image->data.anim.iter = NULL;
1551 gtk_image_unmap (GtkWidget *widget)
1553 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1555 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1559 gtk_image_unrealize (GtkWidget *widget)
1561 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1563 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1567 animation_timeout (gpointer data)
1572 image = GTK_IMAGE (data);
1574 image->data.anim.frame_timeout = 0;
1576 gdk_pixbuf_animation_iter_advance (image->data.anim.iter, NULL);
1578 delay = gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter);
1581 image->data.anim.frame_timeout =
1582 gdk_threads_add_timeout (delay, animation_timeout, image);
1584 gtk_widget_queue_draw (GTK_WIDGET (image));
1586 if (gtk_widget_is_drawable (GTK_WIDGET (image)))
1587 gdk_window_process_updates (GTK_WIDGET (image)->window, TRUE);
1594 icon_theme_changed (GtkImage *image)
1596 GtkImagePrivate *priv;
1598 priv = GTK_IMAGE_GET_PRIVATE (image);
1600 if (image->storage_type == GTK_IMAGE_ICON_NAME)
1602 if (image->data.name.pixbuf)
1603 g_object_unref (image->data.name.pixbuf);
1604 image->data.name.pixbuf = NULL;
1606 gtk_widget_queue_draw (GTK_WIDGET (image));
1608 if (image->storage_type == GTK_IMAGE_GICON)
1610 if (image->data.gicon.pixbuf)
1611 g_object_unref (image->data.gicon.pixbuf);
1612 image->data.gicon.pixbuf = NULL;
1614 gtk_widget_queue_draw (GTK_WIDGET (image));
1619 ensure_pixbuf_for_icon_name (GtkImage *image,
1622 GtkImagePrivate *priv;
1624 GtkIconTheme *icon_theme;
1625 GtkSettings *settings;
1627 gint *sizes, *s, dist;
1629 GtkIconLookupFlags flags;
1631 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME);
1633 priv = GTK_IMAGE_GET_PRIVATE (image);
1634 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1635 icon_theme = gtk_icon_theme_get_for_screen (screen);
1636 settings = gtk_settings_get_for_screen (screen);
1637 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1638 if (image->data.name.pixbuf == NULL ||
1639 (priv->was_symbolic && priv->last_rendered_state != state))
1641 priv->last_rendered_state = state;
1642 if (image->data.name.pixbuf)
1644 g_object_unref (image->data.name.pixbuf);
1645 image->data.name.pixbuf = NULL;
1647 if (priv->pixel_size != -1)
1649 width = height = priv->pixel_size;
1650 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1652 else if (!gtk_icon_size_lookup_for_settings (settings,
1656 if (image->icon_size == -1)
1658 /* Find an available size close to 48 */
1659 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, image->data.name.icon_name);
1661 width = height = 48;
1662 for (s = sizes; *s; s++)
1666 width = height = 48;
1673 width = height = *s;
1681 width = height = *s;
1690 g_warning ("Invalid icon size %d\n", image->icon_size);
1691 width = height = 24;
1695 info = gtk_icon_theme_lookup_icon (icon_theme,
1696 image->data.name.icon_name,
1697 MIN (width, height), flags);
1700 GdkColor error_color, warning_color, success_color;
1701 GdkColor *error_ptr, *warning_ptr, *success_ptr;
1703 gboolean was_symbolic;
1705 style = gtk_widget_get_style (GTK_WIDGET (image));
1706 if (!gtk_style_lookup_color (style, "error_color", &error_color))
1709 error_ptr = &error_color;
1710 if (!gtk_style_lookup_color (style, "warning_color", &warning_color))
1713 warning_ptr = &warning_color;
1714 if (!gtk_style_lookup_color (style, "success_color", &success_color))
1717 success_ptr = &success_color;
1719 image->data.name.pixbuf = gtk_icon_info_load_symbolic (info,
1726 priv->was_symbolic = was_symbolic;
1727 gtk_icon_info_free (info);
1730 if (image->data.name.pixbuf == NULL)
1732 image->data.name.pixbuf =
1733 gtk_widget_render_icon (GTK_WIDGET (image),
1734 GTK_STOCK_MISSING_IMAGE,
1737 priv->was_symbolic = FALSE;
1743 ensure_pixbuf_for_gicon (GtkImage *image,
1746 GtkImagePrivate *priv;
1748 GtkIconTheme *icon_theme;
1749 GtkSettings *settings;
1752 GtkIconLookupFlags flags;
1754 g_return_if_fail (image->storage_type == GTK_IMAGE_GICON);
1756 priv = GTK_IMAGE_GET_PRIVATE (image);
1757 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1758 icon_theme = gtk_icon_theme_get_for_screen (screen);
1759 settings = gtk_settings_get_for_screen (screen);
1760 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1761 if (image->data.gicon.pixbuf == NULL ||
1762 (priv->was_symbolic && priv->last_rendered_state != state))
1764 priv->last_rendered_state = state;
1765 if (image->data.gicon.pixbuf)
1767 g_object_unref (image->data.gicon.pixbuf);
1768 image->data.gicon.pixbuf = NULL;
1770 if (priv->pixel_size != -1)
1772 width = height = priv->pixel_size;
1773 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1775 else if (!gtk_icon_size_lookup_for_settings (settings,
1779 if (image->icon_size == -1)
1780 width = height = 48;
1783 g_warning ("Invalid icon size %d\n", image->icon_size);
1784 width = height = 24;
1788 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1789 image->data.gicon.icon,
1790 MIN (width, height), flags);
1793 GdkColor error_color, warning_color, success_color;
1794 GdkColor *error_ptr, *warning_ptr, *success_ptr;
1796 gboolean was_symbolic;
1798 style = gtk_widget_get_style (GTK_WIDGET (image));
1799 if (!gtk_style_lookup_color (style, "error_color", &error_color))
1802 error_ptr = &error_color;
1803 if (!gtk_style_lookup_color (style, "warning_color", &warning_color))
1806 warning_ptr = &warning_color;
1807 if (!gtk_style_lookup_color (style, "success_color", &success_color))
1810 success_ptr = &success_color;
1812 image->data.gicon.pixbuf = gtk_icon_info_load_symbolic (info,
1819 priv->was_symbolic = was_symbolic;
1820 gtk_icon_info_free (info);
1823 if (image->data.gicon.pixbuf == NULL)
1825 image->data.gicon.pixbuf =
1826 gtk_widget_render_icon (GTK_WIDGET (image),
1827 GTK_STOCK_MISSING_IMAGE,
1830 priv->was_symbolic = FALSE;
1837 * Like gdk_rectangle_intersect (dest, src, dest), but make
1838 * sure that the origin of dest is moved by an "even" offset.
1839 * If necessary grow the intersection by one row or column
1842 * This is necessary since we can't pass alignment information
1843 * for the pixelation pattern down to gdk_pixbuf_saturate_and_pixelate(),
1844 * thus we have to makesure that the subimages are properly aligned.
1847 rectangle_intersect_even (GdkRectangle *src,
1855 isect = gdk_rectangle_intersect (dest, src, dest);
1857 if ((dest->x - x + dest->y - y) % 2 != 0)
1875 gtk_image_expose (GtkWidget *widget,
1876 GdkEventExpose *event)
1878 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1879 g_return_val_if_fail (event != NULL, FALSE);
1881 if (gtk_widget_get_mapped (widget) &&
1882 GTK_IMAGE (widget)->storage_type != GTK_IMAGE_EMPTY)
1886 GtkImagePrivate *priv;
1887 GdkRectangle area, image_bound;
1889 gint x, y, mask_x, mask_y;
1893 gboolean needs_state_transform;
1895 image = GTK_IMAGE (widget);
1896 misc = GTK_MISC (widget);
1897 priv = GTK_IMAGE_GET_PRIVATE (image);
1901 /* For stock items and icon sets, we lazily calculate
1902 * the size; we might get here between a queue_resize()
1903 * and size_request() if something explicitely forces
1906 if (priv->need_calc_size)
1907 gtk_image_calc_size (image);
1909 if (!gdk_rectangle_intersect (&area, &widget->allocation, &area))
1912 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1913 xalign = misc->xalign;
1915 xalign = 1.0 - misc->xalign;
1917 x = floor (widget->allocation.x + misc->xpad
1918 + ((widget->allocation.width - widget->requisition.width) * xalign));
1919 y = floor (widget->allocation.y + misc->ypad
1920 + ((widget->allocation.height - widget->requisition.height) * misc->yalign));
1926 image_bound.width = 0;
1927 image_bound.height = 0;
1931 needs_state_transform = gtk_widget_get_state (widget) != GTK_STATE_NORMAL;
1933 switch (image->storage_type)
1935 case GTK_IMAGE_PIXMAP:
1937 gdk_drawable_get_size (image->data.pixmap.pixmap,
1939 &image_bound.height);
1940 if (rectangle_intersect_even (&area, &image_bound) &&
1941 needs_state_transform)
1943 pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1944 image->data.pixmap.pixmap,
1945 gtk_widget_get_colormap (widget),
1946 image_bound.x - x, image_bound.y - y,
1949 image_bound.height);
1957 case GTK_IMAGE_IMAGE:
1959 image_bound.width = image->data.image.image->width;
1960 image_bound.height = image->data.image.image->height;
1962 if (rectangle_intersect_even (&area, &image_bound) &&
1963 needs_state_transform)
1965 pixbuf = gdk_pixbuf_get_from_image (NULL,
1966 image->data.image.image,
1967 gtk_widget_get_colormap (widget),
1968 image_bound.x - x, image_bound.y - y,
1971 image_bound.height);
1978 case GTK_IMAGE_PIXBUF:
1979 image_bound.width = gdk_pixbuf_get_width (image->data.pixbuf.pixbuf);
1980 image_bound.height = gdk_pixbuf_get_height (image->data.pixbuf.pixbuf);
1982 if (rectangle_intersect_even (&area, &image_bound) &&
1983 needs_state_transform)
1985 pixbuf = gdk_pixbuf_new_subpixbuf (image->data.pixbuf.pixbuf,
1986 image_bound.x - x, image_bound.y - y,
1987 image_bound.width, image_bound.height);
1994 pixbuf = image->data.pixbuf.pixbuf;
1995 g_object_ref (pixbuf);
1999 case GTK_IMAGE_STOCK:
2000 pixbuf = gtk_widget_render_icon (widget,
2001 image->data.stock.stock_id,
2006 image_bound.width = gdk_pixbuf_get_width (pixbuf);
2007 image_bound.height = gdk_pixbuf_get_height (pixbuf);
2011 needs_state_transform = FALSE;
2014 case GTK_IMAGE_ICON_SET:
2016 gtk_icon_set_render_icon (image->data.icon_set.icon_set,
2018 gtk_widget_get_direction (widget),
2019 gtk_widget_get_state (widget),
2026 image_bound.width = gdk_pixbuf_get_width (pixbuf);
2027 image_bound.height = gdk_pixbuf_get_height (pixbuf);
2031 needs_state_transform = FALSE;
2034 case GTK_IMAGE_ANIMATION:
2036 if (image->data.anim.iter == NULL)
2038 image->data.anim.iter = gdk_pixbuf_animation_get_iter (image->data.anim.anim, NULL);
2040 if (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter) >= 0)
2041 image->data.anim.frame_timeout =
2042 gdk_threads_add_timeout (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter),
2047 image_bound.width = gdk_pixbuf_animation_get_width (image->data.anim.anim);
2048 image_bound.height = gdk_pixbuf_animation_get_height (image->data.anim.anim);
2050 /* don't advance the anim iter here, or we could get frame changes between two
2051 * exposes of different areas.
2054 pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (image->data.anim.iter);
2055 g_object_ref (pixbuf);
2059 case GTK_IMAGE_ICON_NAME:
2060 state = gtk_widget_get_state (widget);
2061 if (state == GTK_STATE_INSENSITIVE)
2063 ensure_pixbuf_for_icon_name (image, GTK_STATE_NORMAL);
2067 ensure_pixbuf_for_icon_name (image, state);
2068 /* Already done by the loading function? */
2069 if (priv->was_symbolic)
2070 needs_state_transform = FALSE;
2072 pixbuf = image->data.name.pixbuf;
2075 g_object_ref (pixbuf);
2076 image_bound.width = gdk_pixbuf_get_width (pixbuf);
2077 image_bound.height = gdk_pixbuf_get_height (pixbuf);
2081 case GTK_IMAGE_GICON:
2082 state = gtk_widget_get_state (widget);
2083 if (state == GTK_STATE_INSENSITIVE)
2085 ensure_pixbuf_for_gicon (image, GTK_STATE_NORMAL);
2089 ensure_pixbuf_for_gicon (image, state);
2090 /* Already done by the loading function? */
2091 if (priv->was_symbolic)
2092 needs_state_transform = FALSE;
2094 pixbuf = image->data.gicon.pixbuf;
2097 g_object_ref (pixbuf);
2098 image_bound.width = gdk_pixbuf_get_width (pixbuf);
2099 image_bound.height = gdk_pixbuf_get_height (pixbuf);
2103 case GTK_IMAGE_EMPTY:
2104 g_assert_not_reached ();
2110 gdk_gc_set_clip_mask (widget->style->black_gc, mask);
2111 gdk_gc_set_clip_origin (widget->style->black_gc, mask_x, mask_y);
2114 if (rectangle_intersect_even (&area, &image_bound))
2118 if (needs_state_transform)
2120 GtkIconSource *source;
2121 GdkPixbuf *rendered;
2123 source = gtk_icon_source_new ();
2124 gtk_icon_source_set_pixbuf (source, pixbuf);
2125 /* The size here is arbitrary; since size isn't
2126 * wildcarded in the souce, it isn't supposed to be
2127 * scaled by the engine function
2129 gtk_icon_source_set_size (source,
2130 GTK_ICON_SIZE_SMALL_TOOLBAR);
2131 gtk_icon_source_set_size_wildcarded (source, FALSE);
2133 rendered = gtk_style_render_icon (widget->style,
2135 gtk_widget_get_direction (widget),
2136 gtk_widget_get_state (widget),
2142 gtk_icon_source_free (source);
2144 g_object_unref (pixbuf);
2150 gdk_draw_pixbuf (widget->window,
2151 widget->style->black_gc,
2159 GDK_RGB_DITHER_NORMAL,
2165 switch (image->storage_type)
2167 case GTK_IMAGE_PIXMAP:
2168 gdk_draw_drawable (widget->window,
2169 widget->style->black_gc,
2170 image->data.pixmap.pixmap,
2171 image_bound.x - x, image_bound.y - y,
2172 image_bound.x, image_bound.y,
2173 image_bound.width, image_bound.height);
2176 case GTK_IMAGE_IMAGE:
2177 gdk_draw_image (widget->window,
2178 widget->style->black_gc,
2179 image->data.image.image,
2180 image_bound.x - x, image_bound.y - y,
2181 image_bound.x, image_bound.y,
2182 image_bound.width, image_bound.height);
2185 case GTK_IMAGE_PIXBUF:
2186 case GTK_IMAGE_STOCK:
2187 case GTK_IMAGE_ICON_SET:
2188 case GTK_IMAGE_ANIMATION:
2189 case GTK_IMAGE_ICON_NAME:
2190 case GTK_IMAGE_EMPTY:
2191 case GTK_IMAGE_GICON:
2192 g_assert_not_reached ();
2196 } /* if rectangle intersects */
2200 gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
2201 gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
2205 g_object_unref (pixbuf);
2207 } /* if widget is drawable */
2213 gtk_image_reset (GtkImage *image)
2215 GtkImagePrivate *priv;
2217 priv = GTK_IMAGE_GET_PRIVATE (image);
2219 g_object_freeze_notify (G_OBJECT (image));
2221 if (image->storage_type != GTK_IMAGE_EMPTY)
2222 g_object_notify (G_OBJECT (image), "storage-type");
2226 g_object_unref (image->mask);
2228 g_object_notify (G_OBJECT (image), "mask");
2231 if (image->icon_size != DEFAULT_ICON_SIZE)
2233 image->icon_size = DEFAULT_ICON_SIZE;
2234 g_object_notify (G_OBJECT (image), "icon-size");
2237 switch (image->storage_type)
2239 case GTK_IMAGE_PIXMAP:
2241 if (image->data.pixmap.pixmap)
2242 g_object_unref (image->data.pixmap.pixmap);
2243 image->data.pixmap.pixmap = NULL;
2245 g_object_notify (G_OBJECT (image), "pixmap");
2249 case GTK_IMAGE_IMAGE:
2251 if (image->data.image.image)
2252 g_object_unref (image->data.image.image);
2253 image->data.image.image = NULL;
2255 g_object_notify (G_OBJECT (image), "image");
2259 case GTK_IMAGE_PIXBUF:
2261 if (image->data.pixbuf.pixbuf)
2262 g_object_unref (image->data.pixbuf.pixbuf);
2264 g_object_notify (G_OBJECT (image), "pixbuf");
2268 case GTK_IMAGE_STOCK:
2270 g_free (image->data.stock.stock_id);
2272 image->data.stock.stock_id = NULL;
2274 g_object_notify (G_OBJECT (image), "stock");
2277 case GTK_IMAGE_ICON_SET:
2278 if (image->data.icon_set.icon_set)
2279 gtk_icon_set_unref (image->data.icon_set.icon_set);
2280 image->data.icon_set.icon_set = NULL;
2282 g_object_notify (G_OBJECT (image), "icon-set");
2285 case GTK_IMAGE_ANIMATION:
2286 gtk_image_reset_anim_iter (image);
2288 if (image->data.anim.anim)
2289 g_object_unref (image->data.anim.anim);
2290 image->data.anim.anim = NULL;
2292 g_object_notify (G_OBJECT (image), "pixbuf-animation");
2296 case GTK_IMAGE_ICON_NAME:
2297 g_free (image->data.name.icon_name);
2298 image->data.name.icon_name = NULL;
2299 if (image->data.name.pixbuf)
2300 g_object_unref (image->data.name.pixbuf);
2301 image->data.name.pixbuf = NULL;
2303 g_object_notify (G_OBJECT (image), "icon-name");
2307 case GTK_IMAGE_GICON:
2308 if (image->data.gicon.icon)
2309 g_object_unref (image->data.gicon.icon);
2310 image->data.gicon.icon = NULL;
2311 if (image->data.gicon.pixbuf)
2312 g_object_unref (image->data.gicon.pixbuf);
2313 image->data.gicon.pixbuf = NULL;
2315 g_object_notify (G_OBJECT (image), "gicon");
2319 case GTK_IMAGE_EMPTY:
2327 g_free (priv->filename);
2328 priv->filename = NULL;
2329 g_object_notify (G_OBJECT (image), "file");
2332 image->storage_type = GTK_IMAGE_EMPTY;
2334 memset (&image->data, '\0', sizeof (image->data));
2336 g_object_thaw_notify (G_OBJECT (image));
2341 * @image: a #GtkImage
2343 * Resets the image to be empty.
2348 gtk_image_clear (GtkImage *image)
2350 GtkImagePrivate *priv;
2352 priv = GTK_IMAGE_GET_PRIVATE (image);
2354 priv->need_calc_size = 1;
2356 gtk_image_reset (image);
2357 gtk_image_update_size (image, 0, 0);
2361 gtk_image_calc_size (GtkImage *image)
2363 GtkWidget *widget = GTK_WIDGET (image);
2364 GdkPixbuf *pixbuf = NULL;
2365 GtkImagePrivate *priv;
2367 priv = GTK_IMAGE_GET_PRIVATE (image);
2369 priv->need_calc_size = 0;
2371 /* We update stock/icon set on every size request, because
2372 * the theme could have affected the size; for other kinds of
2373 * image, we just update the requisition when the image data
2376 switch (image->storage_type)
2378 case GTK_IMAGE_STOCK:
2379 pixbuf = gtk_widget_render_icon (widget,
2380 image->data.stock.stock_id,
2385 case GTK_IMAGE_ICON_SET:
2386 pixbuf = gtk_icon_set_render_icon (image->data.icon_set.icon_set,
2388 gtk_widget_get_direction (widget),
2389 gtk_widget_get_state (widget),
2394 case GTK_IMAGE_ICON_NAME:
2395 ensure_pixbuf_for_icon_name (image, GTK_STATE_NORMAL);
2396 pixbuf = image->data.name.pixbuf;
2397 if (pixbuf) g_object_ref (pixbuf);
2399 case GTK_IMAGE_GICON:
2400 ensure_pixbuf_for_gicon (image, GTK_STATE_NORMAL);
2401 pixbuf = image->data.gicon.pixbuf;
2403 g_object_ref (pixbuf);
2411 widget->requisition.width = gdk_pixbuf_get_width (pixbuf) + GTK_MISC (image)->xpad * 2;
2412 widget->requisition.height = gdk_pixbuf_get_height (pixbuf) + GTK_MISC (image)->ypad * 2;
2414 g_object_unref (pixbuf);
2419 gtk_image_size_request (GtkWidget *widget,
2420 GtkRequisition *requisition)
2424 image = GTK_IMAGE (widget);
2426 gtk_image_calc_size (image);
2428 /* Chain up to default that simply reads current requisition */
2429 GTK_WIDGET_CLASS (gtk_image_parent_class)->size_request (widget, requisition);
2433 gtk_image_style_set (GtkWidget *widget,
2434 GtkStyle *prev_style)
2438 image = GTK_IMAGE (widget);
2440 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_set (widget, prev_style);
2442 icon_theme_changed (image);
2446 gtk_image_screen_changed (GtkWidget *widget,
2447 GdkScreen *prev_screen)
2451 image = GTK_IMAGE (widget);
2453 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
2454 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
2456 icon_theme_changed (image);
2461 gtk_image_update_size (GtkImage *image,
2465 GtkWidget *widget = GTK_WIDGET (image);
2467 widget->requisition.width = image_width + GTK_MISC (image)->xpad * 2;
2468 widget->requisition.height = image_height + GTK_MISC (image)->ypad * 2;
2470 if (gtk_widget_get_visible (widget))
2471 gtk_widget_queue_resize (widget);
2476 * gtk_image_set_pixel_size:
2477 * @image: a #GtkImage
2478 * @pixel_size: the new pixel size
2480 * Sets the pixel size to use for named icons. If the pixel size is set
2481 * to a value != -1, it is used instead of the icon size set by
2482 * gtk_image_set_from_icon_name().
2487 gtk_image_set_pixel_size (GtkImage *image,
2490 GtkImagePrivate *priv;
2492 g_return_if_fail (GTK_IS_IMAGE (image));
2494 priv = GTK_IMAGE_GET_PRIVATE (image);
2496 if (priv->pixel_size != pixel_size)
2498 priv->pixel_size = pixel_size;
2500 if (image->storage_type == GTK_IMAGE_ICON_NAME)
2502 if (image->data.name.pixbuf)
2504 g_object_unref (image->data.name.pixbuf);
2505 image->data.name.pixbuf = NULL;
2508 gtk_image_update_size (image, pixel_size, pixel_size);
2511 if (image->storage_type == GTK_IMAGE_GICON)
2513 if (image->data.gicon.pixbuf)
2515 g_object_unref (image->data.gicon.pixbuf);
2516 image->data.gicon.pixbuf = NULL;
2519 gtk_image_update_size (image, pixel_size, pixel_size);
2522 g_object_notify (G_OBJECT (image), "pixel-size");
2527 * gtk_image_get_pixel_size:
2528 * @image: a #GtkImage
2530 * Gets the pixel size used for named icons.
2532 * Returns: the pixel size used for named icons.
2537 gtk_image_get_pixel_size (GtkImage *image)
2539 GtkImagePrivate *priv;
2541 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
2543 priv = GTK_IMAGE_GET_PRIVATE (image);
2545 return priv->pixel_size;
2548 #if defined (G_OS_WIN32) && !defined (_WIN64)
2550 #undef gtk_image_new_from_file
2553 gtk_image_new_from_file (const gchar *filename)
2555 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2558 retval = gtk_image_new_from_file_utf8 (utf8_filename);
2560 g_free (utf8_filename);
2565 #undef gtk_image_set_from_file
2568 gtk_image_set_from_file (GtkImage *image,
2569 const gchar *filename)
2571 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2573 gtk_image_set_from_file_utf8 (image, utf8_filename);
2575 g_free (utf8_filename);
2580 #define __GTK_IMAGE_C__
2581 #include "gtkaliasdef.c"