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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include "gtkcontainer.h"
31 #include "gtkiconhelperprivate.h"
32 #include "gtkimageprivate.h"
33 #include "gtkiconfactory.h"
35 #include "gtkicontheme.h"
36 #include "gtksizerequest.h"
38 #include "gtkprivate.h"
39 #include "gtktypebuiltins.h"
41 #include "a11y/gtkimageaccessible.h"
45 * @Short_description: A widget displaying an image
47 * @See_also:#GdkPixbuf
49 * The #GtkImage widget displays an image. Various kinds of object
50 * can be displayed as an image; most typically, you would load a
51 * #GdkPixbuf ("pixel buffer") from a file, and then display that.
52 * There's a convenience function to do this, gtk_image_new_from_file(),
54 * <informalexample><programlisting>
56 * image = gtk_image_new_from_file ("myfile.png");
57 * </programlisting></informalexample>
58 * If the file isn't loaded successfully, the image will contain a
59 * "broken image" icon similar to that used in many web browsers.
60 * If you want to handle errors in loading the file yourself,
61 * for example by displaying an error message, then load the image with
62 * gdk_pixbuf_new_from_file(), then create the #GtkImage with
63 * gtk_image_new_from_pixbuf().
65 * The image file may contain an animation, if so the #GtkImage will
66 * display an animation (#GdkPixbufAnimation) instead of a static image.
68 * #GtkImage is a subclass of #GtkMisc, which implies that you can
69 * align it (center, left, right) and add padding to it, using
72 * #GtkImage is a "no window" widget (has no #GdkWindow of its own),
73 * so by default does not receive events. If you want to receive events
74 * on the image, such as button clicks, place the image inside a
75 * #GtkEventBox, then connect to the event signals on the event box.
77 * <title>Handling button press events on a
78 * <structname>GtkImage</structname>.</title>
81 * button_press_callback (GtkWidget *event_box,
82 * GdkEventButton *event,
85 * g_print ("Event box clicked at coordinates %f,%f\n",
86 * event->x, event->y);
88 * /<!---->* Returning TRUE means we handled the event, so the signal
89 * * emission should be stopped (don't call any further
90 * * callbacks that may be connected). Return FALSE
91 * * to continue invoking callbacks.
100 * GtkWidget *event_box;
102 * image = gtk_image_new_from_file ("myfile.png");
104 * event_box = gtk_event_box_new (<!-- -->);
106 * gtk_container_add (GTK_CONTAINER (event_box), image);
108 * g_signal_connect (G_OBJECT (event_box),
109 * "button_press_event",
110 * G_CALLBACK (button_press_callback),
118 * When handling events on the event box, keep in mind that coordinates
119 * in the image may be different from event box coordinates due to
120 * the alignment and padding settings on the image (see #GtkMisc).
121 * The simplest way to solve this is to set the alignment to 0.0
122 * (left/top), and set the padding to zero. Then the origin of
123 * the image will be the same as the origin of the event box.
125 * Sometimes an application will want to avoid depending on external data
126 * files, such as image files. GTK+ comes with a program to avoid this,
127 * called <application>gdk-pixbuf-csource</application>. This library
128 * allows you to convert an image into a C variable declaration, which
129 * can then be loaded into a #GdkPixbuf using
130 * gdk_pixbuf_new_from_inline().
134 struct _GtkImagePrivate
136 GtkIconHelper *icon_helper;
138 gint animation_timeout;
139 GdkPixbufAnimationIter *animation_iter;
141 gchar *filename; /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
142 gchar *resource_path; /* Only used with GTK_IMAGE_PIXBUF */
146 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
147 static gint gtk_image_draw (GtkWidget *widget,
149 static void gtk_image_unmap (GtkWidget *widget);
150 static void gtk_image_unrealize (GtkWidget *widget);
151 static void gtk_image_get_preferred_width (GtkWidget *widget,
154 static void gtk_image_get_preferred_height (GtkWidget *widget,
158 static void gtk_image_style_updated (GtkWidget *widget);
159 static void gtk_image_screen_changed (GtkWidget *widget,
160 GdkScreen *prev_screen);
161 static void gtk_image_finalize (GObject *object);
162 static void gtk_image_reset (GtkImage *image);
164 static void gtk_image_set_property (GObject *object,
168 static void gtk_image_get_property (GObject *object,
173 static void icon_theme_changed (GtkImage *image);
184 PROP_PIXBUF_ANIMATION,
192 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
195 gtk_image_class_init (GtkImageClass *class)
197 GObjectClass *gobject_class;
198 GtkWidgetClass *widget_class;
200 gobject_class = G_OBJECT_CLASS (class);
202 gobject_class->set_property = gtk_image_set_property;
203 gobject_class->get_property = gtk_image_get_property;
204 gobject_class->finalize = gtk_image_finalize;
206 widget_class = GTK_WIDGET_CLASS (class);
207 widget_class->draw = gtk_image_draw;
208 widget_class->get_preferred_width = gtk_image_get_preferred_width;
209 widget_class->get_preferred_height = gtk_image_get_preferred_height;
210 widget_class->unmap = gtk_image_unmap;
211 widget_class->unrealize = gtk_image_unrealize;
212 widget_class->style_updated = gtk_image_style_updated;
213 widget_class->screen_changed = gtk_image_screen_changed;
215 g_object_class_install_property (gobject_class,
217 g_param_spec_object ("pixbuf",
219 P_("A GdkPixbuf to display"),
221 GTK_PARAM_READWRITE));
223 g_object_class_install_property (gobject_class,
225 g_param_spec_string ("file",
227 P_("Filename to load and display"),
229 GTK_PARAM_READWRITE));
232 g_object_class_install_property (gobject_class,
234 g_param_spec_string ("stock",
236 P_("Stock ID for a stock image to display"),
238 GTK_PARAM_READWRITE));
240 g_object_class_install_property (gobject_class,
242 g_param_spec_boxed ("icon-set",
244 P_("Icon set to display"),
246 GTK_PARAM_READWRITE));
248 g_object_class_install_property (gobject_class,
250 g_param_spec_int ("icon-size",
252 P_("Symbolic size to use for stock icon, icon set or named icon"),
255 GTK_PARAM_READWRITE));
257 * GtkImage:pixel-size:
259 * The "pixel-size" property can be used to specify a fixed size
260 * overriding the #GtkImage:icon-size property for images of type
261 * %GTK_IMAGE_ICON_NAME.
265 g_object_class_install_property (gobject_class,
267 g_param_spec_int ("pixel-size",
269 P_("Pixel size to use for named icon"),
272 GTK_PARAM_READWRITE));
274 g_object_class_install_property (gobject_class,
275 PROP_PIXBUF_ANIMATION,
276 g_param_spec_object ("pixbuf-animation",
278 P_("GdkPixbufAnimation to display"),
279 GDK_TYPE_PIXBUF_ANIMATION,
280 GTK_PARAM_READWRITE));
283 * GtkImage:icon-name:
285 * The name of the icon in the icon theme. If the icon theme is
286 * changed, the image will be updated automatically.
290 g_object_class_install_property (gobject_class,
292 g_param_spec_string ("icon-name",
294 P_("The name of the icon from the icon theme"),
296 GTK_PARAM_READWRITE));
301 * The GIcon displayed in the GtkImage. For themed icons,
302 * If the icon theme is changed, the image will be updated
307 g_object_class_install_property (gobject_class,
309 g_param_spec_object ("gicon",
311 P_("The GIcon being displayed"),
313 GTK_PARAM_READWRITE));
318 * A path to a resource file to display.
322 g_object_class_install_property (gobject_class,
324 g_param_spec_string ("resource",
326 P_("The resource path being displayed"),
328 GTK_PARAM_READWRITE));
330 g_object_class_install_property (gobject_class,
332 g_param_spec_enum ("storage-type",
334 P_("The representation being used for image data"),
337 GTK_PARAM_READABLE));
340 * GtkImage:use-fallback:
342 * Whether the icon displayed in the GtkImage will use
343 * standard icon names fallback. The value of this property
344 * is only relevant for images of type %GTK_IMAGE_ICON_NAME
345 * and %GTK_IMAGE_GICON.
349 g_object_class_install_property (gobject_class,
351 g_param_spec_boolean ("use-fallback",
353 P_("Whether to use icon names fallback"),
355 GTK_PARAM_READWRITE));
357 g_type_class_add_private (class, sizeof (GtkImagePrivate));
359 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_IMAGE_ACCESSIBLE);
363 gtk_image_init (GtkImage *image)
365 GtkImagePrivate *priv;
367 image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image,
372 gtk_widget_set_has_window (GTK_WIDGET (image), FALSE);
373 priv->icon_helper = _gtk_icon_helper_new ();
375 priv->filename = NULL;
379 gtk_image_finalize (GObject *object)
381 GtkImage *image = GTK_IMAGE (object);
383 g_clear_object (&image->priv->icon_helper);
385 g_free (image->priv->filename);
387 G_OBJECT_CLASS (gtk_image_parent_class)->finalize (object);
391 gtk_image_set_property (GObject *object,
396 GtkImage *image = GTK_IMAGE (object);
397 GtkImagePrivate *priv = image->priv;
398 GtkIconSize icon_size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
400 if (icon_size == GTK_ICON_SIZE_INVALID)
401 icon_size = DEFAULT_ICON_SIZE;
406 gtk_image_set_from_pixbuf (image,
407 g_value_get_object (value));
410 gtk_image_set_from_file (image, g_value_get_string (value));
413 gtk_image_set_from_stock (image, g_value_get_string (value),
417 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
421 _gtk_icon_helper_set_icon_size (priv->icon_helper, g_value_get_int (value));
423 case PROP_PIXEL_SIZE:
424 gtk_image_set_pixel_size (image, g_value_get_int (value));
426 case PROP_PIXBUF_ANIMATION:
427 gtk_image_set_from_animation (image,
428 g_value_get_object (value));
431 gtk_image_set_from_icon_name (image, g_value_get_string (value),
435 gtk_image_set_from_gicon (image, g_value_get_object (value),
439 gtk_image_set_from_resource (image, g_value_get_string (value));
442 case PROP_USE_FALLBACK:
443 _gtk_icon_helper_set_use_fallback (priv->icon_helper, g_value_get_boolean (value));
447 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
453 gtk_image_get_property (GObject *object,
458 GtkImage *image = GTK_IMAGE (object);
459 GtkImagePrivate *priv = image->priv;
464 g_value_set_object (value, _gtk_icon_helper_peek_pixbuf (priv->icon_helper));
467 g_value_set_string (value, priv->filename);
470 g_value_set_string (value, _gtk_icon_helper_get_stock_id (priv->icon_helper));
473 g_value_set_boxed (value, _gtk_icon_helper_peek_icon_set (priv->icon_helper));
476 g_value_set_int (value, _gtk_icon_helper_get_icon_size (priv->icon_helper));
478 case PROP_PIXEL_SIZE:
479 g_value_set_int (value, _gtk_icon_helper_get_pixel_size (priv->icon_helper));
481 case PROP_PIXBUF_ANIMATION:
482 g_value_set_object (value, _gtk_icon_helper_peek_animation (priv->icon_helper));
485 g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
488 g_value_set_object (value, _gtk_icon_helper_peek_gicon (priv->icon_helper));
491 g_value_set_string (value, priv->resource_path);
493 case PROP_USE_FALLBACK:
494 g_value_set_boolean (value, _gtk_icon_helper_get_use_fallback (priv->icon_helper));
496 case PROP_STORAGE_TYPE:
497 g_value_set_enum (value, _gtk_icon_helper_get_storage_type (priv->icon_helper));
500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
507 * gtk_image_new_from_file:
508 * @filename: (type filename): a filename
510 * Creates a new #GtkImage displaying the file @filename. If the file
511 * isn't found or can't be loaded, the resulting #GtkImage will
512 * display a "broken image" icon. This function never returns %NULL,
513 * it always returns a valid #GtkImage widget.
515 * If the file contains an animation, the image will contain an
518 * If you need to detect failures to load the file, use
519 * gdk_pixbuf_new_from_file() to load the file yourself, then create
520 * the #GtkImage from the pixbuf. (Or for animations, use
521 * gdk_pixbuf_animation_new_from_file()).
523 * The storage type (gtk_image_get_storage_type()) of the returned
524 * image is not defined, it will be whatever is appropriate for
525 * displaying the file.
527 * Return value: a new #GtkImage
530 gtk_image_new_from_file (const gchar *filename)
534 image = g_object_new (GTK_TYPE_IMAGE, NULL);
536 gtk_image_set_from_file (image, filename);
538 return GTK_WIDGET (image);
542 * gtk_image_new_from_resource:
543 * @resource_path: a resource path
545 * Creates a new #GtkImage displaying the resource file @resource_path. If the file
546 * isn't found or can't be loaded, the resulting #GtkImage will
547 * display a "broken image" icon. This function never returns %NULL,
548 * it always returns a valid #GtkImage widget.
550 * If the file contains an animation, the image will contain an
553 * If you need to detect failures to load the file, use
554 * gdk_pixbuf_new_from_file() to load the file yourself, then create
555 * the #GtkImage from the pixbuf. (Or for animations, use
556 * gdk_pixbuf_animation_new_from_file()).
558 * The storage type (gtk_image_get_storage_type()) of the returned
559 * image is not defined, it will be whatever is appropriate for
560 * displaying the file.
562 * Return value: a new #GtkImage
567 gtk_image_new_from_resource (const gchar *resource_path)
571 image = g_object_new (GTK_TYPE_IMAGE, NULL);
573 gtk_image_set_from_resource (image, resource_path);
575 return GTK_WIDGET (image);
579 * gtk_image_new_from_pixbuf:
580 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
582 * Creates a new #GtkImage displaying @pixbuf.
583 * The #GtkImage does not assume a reference to the
584 * pixbuf; you still need to unref it if you own references.
585 * #GtkImage will add its own reference rather than adopting yours.
587 * Note that this function just creates an #GtkImage from the pixbuf. The
588 * #GtkImage created will not react to state changes. Should you want that,
589 * you should use gtk_image_new_from_icon_set().
591 * Return value: a new #GtkImage
594 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
598 image = g_object_new (GTK_TYPE_IMAGE, NULL);
600 gtk_image_set_from_pixbuf (image, pixbuf);
602 return GTK_WIDGET (image);
606 * gtk_image_new_from_stock:
607 * @stock_id: a stock icon name
608 * @size: (type int): a stock icon size
610 * Creates a #GtkImage displaying a stock icon. Sample stock icon
611 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
612 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
613 * icon name isn't known, the image will be empty.
614 * You can register your own stock icon names, see
615 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
617 * Return value: a new #GtkImage displaying the stock icon
620 gtk_image_new_from_stock (const gchar *stock_id,
625 image = g_object_new (GTK_TYPE_IMAGE, NULL);
627 gtk_image_set_from_stock (image, stock_id, size);
629 return GTK_WIDGET (image);
633 * gtk_image_new_from_icon_set:
634 * @icon_set: a #GtkIconSet
635 * @size: (type int): a stock icon size
637 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
638 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
639 * this function, usually it's better to create a #GtkIconFactory, put
640 * your icon sets in the icon factory, add the icon factory to the
641 * list of default factories with gtk_icon_factory_add_default(), and
642 * then use gtk_image_new_from_stock(). This will allow themes to
643 * override the icon you ship with your application.
645 * The #GtkImage does not assume a reference to the
646 * icon set; you still need to unref it if you own references.
647 * #GtkImage will add its own reference rather than adopting yours.
649 * Return value: a new #GtkImage
652 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
657 image = g_object_new (GTK_TYPE_IMAGE, NULL);
659 gtk_image_set_from_icon_set (image, icon_set, size);
661 return GTK_WIDGET (image);
665 * gtk_image_new_from_animation:
666 * @animation: an animation
668 * Creates a #GtkImage displaying the given animation.
669 * The #GtkImage does not assume a reference to the
670 * animation; you still need to unref it if you own references.
671 * #GtkImage will add its own reference rather than adopting yours.
673 * Note that the animation frames are shown using a timeout with
674 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
675 * keep in mind that the animation will only be shown if the main loop
676 * is not busy with something that has a higher priority.
678 * Return value: a new #GtkImage widget
681 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
685 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
687 image = g_object_new (GTK_TYPE_IMAGE, NULL);
689 gtk_image_set_from_animation (image, animation);
691 return GTK_WIDGET (image);
695 * gtk_image_new_from_icon_name:
696 * @icon_name: an icon name
697 * @size: (type int): a stock icon size
699 * Creates a #GtkImage displaying an icon from the current icon theme.
700 * If the icon name isn't known, a "broken image" icon will be
701 * displayed instead. If the current icon theme is changed, the icon
702 * will be updated appropriately.
704 * Return value: a new #GtkImage displaying the themed icon
709 gtk_image_new_from_icon_name (const gchar *icon_name,
714 image = g_object_new (GTK_TYPE_IMAGE, NULL);
716 gtk_image_set_from_icon_name (image, icon_name, size);
718 return GTK_WIDGET (image);
722 * gtk_image_new_from_gicon:
724 * @size: (type int): a stock icon size
726 * Creates a #GtkImage displaying an icon from the current icon theme.
727 * If the icon name isn't known, a "broken image" icon will be
728 * displayed instead. If the current icon theme is changed, the icon
729 * will be updated appropriately.
731 * Return value: a new #GtkImage displaying the themed icon
736 gtk_image_new_from_gicon (GIcon *icon,
741 image = g_object_new (GTK_TYPE_IMAGE, NULL);
743 gtk_image_set_from_gicon (image, icon, size);
745 return GTK_WIDGET (image);
749 * gtk_image_set_from_file:
750 * @image: a #GtkImage
751 * @filename: (type filename) (allow-none): a filename or %NULL
753 * See gtk_image_new_from_file() for details.
756 gtk_image_set_from_file (GtkImage *image,
757 const gchar *filename)
759 GtkImagePrivate *priv;
760 GdkPixbufAnimation *anim;
762 g_return_if_fail (GTK_IS_IMAGE (image));
766 g_object_freeze_notify (G_OBJECT (image));
768 gtk_image_clear (image);
770 if (filename == NULL)
772 priv->filename = NULL;
773 g_object_thaw_notify (G_OBJECT (image));
777 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
781 gtk_image_set_from_stock (image,
782 GTK_STOCK_MISSING_IMAGE,
784 g_object_thaw_notify (G_OBJECT (image));
788 /* We could just unconditionally set_from_animation,
789 * but it's nicer for memory if we toss the animation
790 * if it's just a single pixbuf
793 if (gdk_pixbuf_animation_is_static_image (anim))
794 gtk_image_set_from_pixbuf (image,
795 gdk_pixbuf_animation_get_static_image (anim));
797 gtk_image_set_from_animation (image, anim);
799 g_object_unref (anim);
801 priv->filename = g_strdup (filename);
803 g_object_thaw_notify (G_OBJECT (image));
807 * gtk_image_set_from_resource:
808 * @image: a #GtkImage
809 * @resource_path: (allow-none): a resource path or %NULL
811 * See gtk_image_new_from_resource() for details.
814 gtk_image_set_from_resource (GtkImage *image,
815 const gchar *resource_path)
817 GtkImagePrivate *priv;
818 GdkPixbufAnimation *animation;
820 g_return_if_fail (GTK_IS_IMAGE (image));
824 g_object_freeze_notify (G_OBJECT (image));
826 gtk_image_clear (image);
828 if (resource_path == NULL)
830 g_object_thaw_notify (G_OBJECT (image));
834 animation = gdk_pixbuf_animation_new_from_resource (resource_path, NULL);
836 if (animation == NULL)
838 gtk_image_set_from_stock (image,
839 GTK_STOCK_MISSING_IMAGE,
841 g_object_thaw_notify (G_OBJECT (image));
845 priv->resource_path = g_strdup (resource_path);
847 if (gdk_pixbuf_animation_is_static_image (animation))
848 gtk_image_set_from_pixbuf (image, gdk_pixbuf_animation_get_static_image (animation));
850 gtk_image_set_from_animation (image, animation);
852 g_object_notify (G_OBJECT (image), "resource");
854 g_object_unref (animation);
856 g_object_thaw_notify (G_OBJECT (image));
861 * gtk_image_set_from_pixbuf:
862 * @image: a #GtkImage
863 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
865 * See gtk_image_new_from_pixbuf() for details.
868 gtk_image_set_from_pixbuf (GtkImage *image,
871 GtkImagePrivate *priv;
873 g_return_if_fail (GTK_IS_IMAGE (image));
874 g_return_if_fail (pixbuf == NULL ||
875 GDK_IS_PIXBUF (pixbuf));
879 g_object_freeze_notify (G_OBJECT (image));
881 gtk_image_clear (image);
884 _gtk_icon_helper_set_pixbuf (priv->icon_helper, 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: (type int): a stock icon size
897 * See gtk_image_new_from_stock() for details.
900 gtk_image_set_from_stock (GtkImage *image,
901 const gchar *stock_id,
904 GtkImagePrivate *priv;
907 g_return_if_fail (GTK_IS_IMAGE (image));
911 g_object_freeze_notify (G_OBJECT (image));
913 new_id = g_strdup (stock_id);
914 gtk_image_clear (image);
918 _gtk_icon_helper_set_stock_id (priv->icon_helper, new_id, size);
922 g_object_notify (G_OBJECT (image), "stock");
923 g_object_notify (G_OBJECT (image), "icon-size");
925 g_object_thaw_notify (G_OBJECT (image));
929 * gtk_image_set_from_icon_set:
930 * @image: a #GtkImage
931 * @icon_set: a #GtkIconSet
932 * @size: (type int): a stock icon size
934 * See gtk_image_new_from_icon_set() for details.
937 gtk_image_set_from_icon_set (GtkImage *image,
938 GtkIconSet *icon_set,
941 GtkImagePrivate *priv;
943 g_return_if_fail (GTK_IS_IMAGE (image));
947 g_object_freeze_notify (G_OBJECT (image));
950 gtk_icon_set_ref (icon_set);
952 gtk_image_clear (image);
956 _gtk_icon_helper_set_icon_set (priv->icon_helper, icon_set, size);
957 gtk_icon_set_unref (icon_set);
960 g_object_notify (G_OBJECT (image), "icon-set");
961 g_object_notify (G_OBJECT (image), "icon-size");
963 g_object_thaw_notify (G_OBJECT (image));
967 * gtk_image_set_from_animation:
968 * @image: a #GtkImage
969 * @animation: the #GdkPixbufAnimation
971 * Causes the #GtkImage to display the given animation (or display
972 * nothing, if you set the animation to %NULL).
975 gtk_image_set_from_animation (GtkImage *image,
976 GdkPixbufAnimation *animation)
978 GtkImagePrivate *priv;
980 g_return_if_fail (GTK_IS_IMAGE (image));
981 g_return_if_fail (animation == NULL ||
982 GDK_IS_PIXBUF_ANIMATION (animation));
986 g_object_freeze_notify (G_OBJECT (image));
989 g_object_ref (animation);
991 gtk_image_clear (image);
993 if (animation != NULL)
995 _gtk_icon_helper_set_animation (priv->icon_helper, animation);
996 g_object_unref (animation);
999 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1001 g_object_thaw_notify (G_OBJECT (image));
1005 * gtk_image_set_from_icon_name:
1006 * @image: a #GtkImage
1007 * @icon_name: an icon name
1008 * @size: (type int): an icon size
1010 * See gtk_image_new_from_icon_name() for details.
1015 gtk_image_set_from_icon_name (GtkImage *image,
1016 const gchar *icon_name,
1019 GtkImagePrivate *priv;
1022 g_return_if_fail (GTK_IS_IMAGE (image));
1026 g_object_freeze_notify (G_OBJECT (image));
1028 new_name = g_strdup (icon_name);
1029 gtk_image_clear (image);
1033 _gtk_icon_helper_set_icon_name (priv->icon_helper, new_name, size);
1037 g_object_notify (G_OBJECT (image), "icon-name");
1038 g_object_notify (G_OBJECT (image), "icon-size");
1040 g_object_thaw_notify (G_OBJECT (image));
1044 * gtk_image_set_from_gicon:
1045 * @image: a #GtkImage
1047 * @size: (type int): an icon size
1049 * See gtk_image_new_from_gicon() for details.
1054 gtk_image_set_from_gicon (GtkImage *image,
1058 GtkImagePrivate *priv;
1060 g_return_if_fail (GTK_IS_IMAGE (image));
1064 g_object_freeze_notify (G_OBJECT (image));
1067 g_object_ref (icon);
1069 gtk_image_clear (image);
1073 _gtk_icon_helper_set_gicon (priv->icon_helper, icon, size);
1074 g_object_unref (icon);
1077 g_object_notify (G_OBJECT (image), "gicon");
1078 g_object_notify (G_OBJECT (image), "icon-size");
1080 g_object_thaw_notify (G_OBJECT (image));
1084 * gtk_image_get_storage_type:
1085 * @image: a #GtkImage
1087 * Gets the type of representation being used by the #GtkImage
1088 * to store image data. If the #GtkImage has no image data,
1089 * the return value will be %GTK_IMAGE_EMPTY.
1091 * Return value: image representation being used
1094 gtk_image_get_storage_type (GtkImage *image)
1096 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1098 return _gtk_icon_helper_get_storage_type (image->priv->icon_helper);
1102 * gtk_image_get_pixbuf:
1103 * @image: a #GtkImage
1105 * Gets the #GdkPixbuf being displayed by the #GtkImage.
1106 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1107 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1108 * The caller of this function does not own a reference to the
1111 * Return value: (transfer none): the displayed pixbuf, or %NULL if
1112 * the image is empty
1115 gtk_image_get_pixbuf (GtkImage *image)
1117 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1119 return _gtk_icon_helper_peek_pixbuf (image->priv->icon_helper);
1123 * gtk_image_get_stock:
1124 * @image: a #GtkImage
1125 * @stock_id: (out) (transfer none) (allow-none): place to store a
1126 * stock icon name, or %NULL
1127 * @size: (out) (allow-none) (type int): place to store a stock icon
1130 * Gets the stock icon name and size being displayed by the #GtkImage.
1131 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1132 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1133 * The returned string is owned by the #GtkImage and should not
1137 gtk_image_get_stock (GtkImage *image,
1141 GtkImagePrivate *priv;
1143 g_return_if_fail (GTK_IS_IMAGE (image));
1148 *stock_id = (gchar *) _gtk_icon_helper_get_stock_id (priv->icon_helper);
1151 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1155 * gtk_image_get_icon_set:
1156 * @image: a #GtkImage
1157 * @icon_set: (out) (transfer none) (allow-none): location to store a
1158 * #GtkIconSet, or %NULL
1159 * @size: (out) (allow-none) (type int): location to store a stock
1160 * icon size, or %NULL
1162 * Gets the icon set and size being displayed by the #GtkImage.
1163 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1164 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1167 gtk_image_get_icon_set (GtkImage *image,
1168 GtkIconSet **icon_set,
1171 GtkImagePrivate *priv;
1173 g_return_if_fail (GTK_IS_IMAGE (image));
1178 *icon_set = _gtk_icon_helper_peek_icon_set (priv->icon_helper);
1181 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1185 * gtk_image_get_animation:
1186 * @image: a #GtkImage
1188 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1189 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1190 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1191 * The caller of this function does not own a reference to the
1192 * returned animation.
1194 * Return value: (transfer none): the displayed animation, or %NULL if
1195 * the image is empty
1198 gtk_image_get_animation (GtkImage *image)
1200 GtkImagePrivate *priv;
1202 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1206 return _gtk_icon_helper_peek_animation (priv->icon_helper);
1210 * gtk_image_get_icon_name:
1211 * @image: a #GtkImage
1212 * @icon_name: (out) (transfer none) (allow-none): place to store an
1213 * icon name, or %NULL
1214 * @size: (out) (allow-none) (type int): place to store an icon size,
1217 * Gets the icon name and size being displayed by the #GtkImage.
1218 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1219 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1220 * The returned string is owned by the #GtkImage and should not
1226 gtk_image_get_icon_name (GtkImage *image,
1227 const gchar **icon_name,
1230 GtkImagePrivate *priv;
1232 g_return_if_fail (GTK_IS_IMAGE (image));
1237 *icon_name = _gtk_icon_helper_get_icon_name (priv->icon_helper);
1240 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1244 * gtk_image_get_gicon:
1245 * @image: a #GtkImage
1246 * @gicon: (out) (transfer none) (allow-none): place to store a
1248 * @size: (out) (allow-none) (type int): place to store an icon size,
1251 * Gets the #GIcon and size being displayed by the #GtkImage.
1252 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1253 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1254 * The caller of this function does not own a reference to the
1260 gtk_image_get_gicon (GtkImage *image,
1264 GtkImagePrivate *priv;
1266 g_return_if_fail (GTK_IS_IMAGE (image));
1271 *gicon = _gtk_icon_helper_peek_gicon (priv->icon_helper);
1274 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1280 * Creates a new empty #GtkImage widget.
1282 * Return value: a newly created #GtkImage widget.
1285 gtk_image_new (void)
1287 return g_object_new (GTK_TYPE_IMAGE, NULL);
1291 gtk_image_reset_anim_iter (GtkImage *image)
1293 GtkImagePrivate *priv = image->priv;
1295 if (gtk_image_get_storage_type (image) == GTK_IMAGE_ANIMATION)
1297 /* Reset the animation */
1298 if (priv->animation_timeout)
1300 g_source_remove (priv->animation_timeout);
1301 priv->animation_timeout = 0;
1304 g_clear_object (&priv->animation_iter);
1309 gtk_image_unmap (GtkWidget *widget)
1311 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1313 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1317 gtk_image_unrealize (GtkWidget *widget)
1319 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1321 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1325 animation_timeout (gpointer data)
1327 GtkImage *image = GTK_IMAGE (data);
1328 GtkImagePrivate *priv = image->priv;
1331 priv->animation_timeout = 0;
1333 gdk_pixbuf_animation_iter_advance (priv->animation_iter, NULL);
1335 delay = gdk_pixbuf_animation_iter_get_delay_time (priv->animation_iter);
1338 GtkWidget *widget = GTK_WIDGET (image);
1340 priv->animation_timeout =
1341 gdk_threads_add_timeout (delay, animation_timeout, image);
1343 gtk_widget_queue_draw (widget);
1350 get_animation_frame (GtkImage *image)
1352 GtkImagePrivate *priv = image->priv;
1354 if (priv->animation_iter == NULL)
1358 priv->animation_iter =
1359 gdk_pixbuf_animation_get_iter (_gtk_icon_helper_peek_animation (priv->icon_helper), NULL);
1361 delay = gdk_pixbuf_animation_iter_get_delay_time (priv->animation_iter);
1363 priv->animation_timeout =
1364 gdk_threads_add_timeout (delay, animation_timeout, image);
1367 /* don't advance the anim iter here, or we could get frame changes between two
1368 * exposes of different areas.
1370 return g_object_ref (gdk_pixbuf_animation_iter_get_pixbuf (priv->animation_iter));
1374 gtk_image_get_preferred_size (GtkImage *image,
1378 GtkImagePrivate *priv = image->priv;
1381 GtkStyleContext *context;
1383 context = gtk_widget_get_style_context (GTK_WIDGET (image));
1384 _gtk_icon_helper_get_size (priv->icon_helper, context, &width, &height);
1385 _gtk_misc_get_padding_and_border (GTK_MISC (image), &border);
1387 width += border.left + border.right;
1388 height += border.top + border.bottom;
1393 *height_out = height;
1397 gtk_image_draw (GtkWidget *widget,
1401 GtkImagePrivate *priv;
1403 GtkStyleContext *context;
1404 gint x, y, width, height;
1405 gfloat xalign, yalign;
1408 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1410 image = GTK_IMAGE (widget);
1411 misc = GTK_MISC (image);
1414 context = gtk_widget_get_style_context (widget);
1416 gtk_render_background (context, cr,
1418 gtk_widget_get_allocated_width (widget), gtk_widget_get_allocated_height (widget));
1419 gtk_render_frame (context, cr,
1421 gtk_widget_get_allocated_width (widget), gtk_widget_get_allocated_height (widget));
1423 gtk_misc_get_alignment (misc, &xalign, &yalign);
1424 gtk_image_get_preferred_size (image, &width, &height);
1425 _gtk_misc_get_padding_and_border (misc, &border);
1427 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
1428 xalign = 1.0 - xalign;
1430 x = floor ((gtk_widget_get_allocated_width (widget) - width) * xalign) + border.left;
1431 y = floor ((gtk_widget_get_allocated_height (widget) - height) * yalign) + border.top;
1433 if (gtk_image_get_storage_type (image) == GTK_IMAGE_ANIMATION)
1435 GdkPixbuf *pixbuf = get_animation_frame (image);
1436 gtk_render_icon (context, cr,
1439 g_object_unref (pixbuf);
1443 _gtk_icon_helper_draw (priv->icon_helper,
1452 gtk_image_reset (GtkImage *image)
1454 GtkImagePrivate *priv = image->priv;
1455 GtkImageType storage_type;
1457 g_object_freeze_notify (G_OBJECT (image));
1458 storage_type = gtk_image_get_storage_type (image);
1460 if (storage_type != GTK_IMAGE_EMPTY)
1461 g_object_notify (G_OBJECT (image), "storage-type");
1463 g_object_notify (G_OBJECT (image), "icon-size");
1465 switch (storage_type)
1467 case GTK_IMAGE_PIXBUF:
1468 g_object_notify (G_OBJECT (image), "pixbuf");
1470 case GTK_IMAGE_STOCK:
1471 g_object_notify (G_OBJECT (image), "stock");
1473 case GTK_IMAGE_ICON_SET:
1474 g_object_notify (G_OBJECT (image), "icon-set");
1476 case GTK_IMAGE_ANIMATION:
1477 gtk_image_reset_anim_iter (image);
1478 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1480 case GTK_IMAGE_ICON_NAME:
1481 g_object_notify (G_OBJECT (image), "icon-name");
1483 case GTK_IMAGE_GICON:
1484 g_object_notify (G_OBJECT (image), "gicon");
1486 case GTK_IMAGE_EMPTY:
1493 g_free (priv->filename);
1494 priv->filename = NULL;
1495 g_object_notify (G_OBJECT (image), "file");
1498 if (priv->resource_path)
1500 g_free (priv->resource_path);
1501 priv->resource_path = NULL;
1502 g_object_notify (G_OBJECT (image), "resource");
1505 _gtk_icon_helper_clear (priv->icon_helper);
1507 g_object_thaw_notify (G_OBJECT (image));
1512 * @image: a #GtkImage
1514 * Resets the image to be empty.
1519 gtk_image_clear (GtkImage *image)
1521 gtk_image_reset (image);
1523 if (gtk_widget_get_visible (GTK_WIDGET (image)))
1524 gtk_widget_queue_resize (GTK_WIDGET (image));
1528 gtk_image_get_preferred_width (GtkWidget *widget,
1534 gtk_image_get_preferred_size (GTK_IMAGE (widget), &width, NULL);
1535 *minimum = *natural = width;
1539 gtk_image_get_preferred_height (GtkWidget *widget,
1545 gtk_image_get_preferred_size (GTK_IMAGE (widget), NULL, &height);
1546 *minimum = *natural = height;
1550 icon_theme_changed (GtkImage *image)
1552 GtkImagePrivate *priv = image->priv;
1554 _gtk_icon_helper_invalidate (priv->icon_helper);
1555 gtk_widget_queue_draw (GTK_WIDGET (image));
1559 gtk_image_style_updated (GtkWidget *widget)
1561 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_updated (widget);
1563 icon_theme_changed (GTK_IMAGE (widget));
1567 gtk_image_screen_changed (GtkWidget *widget,
1568 GdkScreen *prev_screen)
1572 image = GTK_IMAGE (widget);
1574 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
1575 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
1577 icon_theme_changed (image);
1581 _gtk_image_gicon_data_clear (GtkImageGIconData *data)
1585 g_object_unref (data->pixbuf);
1586 data->pixbuf = NULL;
1590 g_object_unref (data->icon);
1596 * gtk_image_set_pixel_size:
1597 * @image: a #GtkImage
1598 * @pixel_size: the new pixel size
1600 * Sets the pixel size to use for named icons. If the pixel size is set
1601 * to a value != -1, it is used instead of the icon size set by
1602 * gtk_image_set_from_icon_name().
1607 gtk_image_set_pixel_size (GtkImage *image,
1610 GtkImagePrivate *priv;
1611 gint old_pixel_size;
1613 g_return_if_fail (GTK_IS_IMAGE (image));
1616 old_pixel_size = gtk_image_get_pixel_size (image);
1618 if (pixel_size != old_pixel_size)
1620 _gtk_icon_helper_set_pixel_size (priv->icon_helper, pixel_size);
1622 if (gtk_widget_get_visible (GTK_WIDGET (image)))
1623 gtk_widget_queue_resize (GTK_WIDGET (image));
1625 g_object_notify (G_OBJECT (image), "pixel-size");
1630 * gtk_image_get_pixel_size:
1631 * @image: a #GtkImage
1633 * Gets the pixel size used for named icons.
1635 * Returns: the pixel size used for named icons.
1640 gtk_image_get_pixel_size (GtkImage *image)
1642 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
1644 return _gtk_icon_helper_get_pixel_size (image->priv->icon_helper);