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 */
145 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
146 static gint gtk_image_draw (GtkWidget *widget,
148 static void gtk_image_unmap (GtkWidget *widget);
149 static void gtk_image_unrealize (GtkWidget *widget);
150 static void gtk_image_get_preferred_width (GtkWidget *widget,
153 static void gtk_image_get_preferred_height (GtkWidget *widget,
157 static void gtk_image_style_updated (GtkWidget *widget);
158 static void gtk_image_screen_changed (GtkWidget *widget,
159 GdkScreen *prev_screen);
160 static void gtk_image_destroy (GtkWidget *widget);
161 static void gtk_image_reset (GtkImage *image);
163 static void gtk_image_set_property (GObject *object,
167 static void gtk_image_get_property (GObject *object,
172 static void icon_theme_changed (GtkImage *image);
183 PROP_PIXBUF_ANIMATION,
190 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
193 gtk_image_class_init (GtkImageClass *class)
195 GObjectClass *gobject_class;
196 GtkWidgetClass *widget_class;
198 gobject_class = G_OBJECT_CLASS (class);
200 gobject_class->set_property = gtk_image_set_property;
201 gobject_class->get_property = gtk_image_get_property;
203 widget_class = GTK_WIDGET_CLASS (class);
204 widget_class->draw = gtk_image_draw;
205 widget_class->destroy = gtk_image_destroy;
206 widget_class->get_preferred_width = gtk_image_get_preferred_width;
207 widget_class->get_preferred_height = gtk_image_get_preferred_height;
208 widget_class->unmap = gtk_image_unmap;
209 widget_class->unrealize = gtk_image_unrealize;
210 widget_class->style_updated = gtk_image_style_updated;
211 widget_class->screen_changed = gtk_image_screen_changed;
213 g_object_class_install_property (gobject_class,
215 g_param_spec_object ("pixbuf",
217 P_("A GdkPixbuf to display"),
219 GTK_PARAM_READWRITE));
221 g_object_class_install_property (gobject_class,
223 g_param_spec_string ("file",
225 P_("Filename to load and display"),
227 GTK_PARAM_READWRITE));
230 g_object_class_install_property (gobject_class,
232 g_param_spec_string ("stock",
234 P_("Stock ID for a stock image to display"),
236 GTK_PARAM_READWRITE));
238 g_object_class_install_property (gobject_class,
240 g_param_spec_boxed ("icon-set",
242 P_("Icon set to display"),
244 GTK_PARAM_READWRITE));
246 g_object_class_install_property (gobject_class,
248 g_param_spec_int ("icon-size",
250 P_("Symbolic size to use for stock icon, icon set or named icon"),
253 GTK_PARAM_READWRITE));
255 * GtkImage:pixel-size:
257 * The "pixel-size" property can be used to specify a fixed size
258 * overriding the #GtkImage:icon-size property for images of type
259 * %GTK_IMAGE_ICON_NAME.
263 g_object_class_install_property (gobject_class,
265 g_param_spec_int ("pixel-size",
267 P_("Pixel size to use for named icon"),
270 GTK_PARAM_READWRITE));
272 g_object_class_install_property (gobject_class,
273 PROP_PIXBUF_ANIMATION,
274 g_param_spec_object ("pixbuf-animation",
276 P_("GdkPixbufAnimation to display"),
277 GDK_TYPE_PIXBUF_ANIMATION,
278 GTK_PARAM_READWRITE));
281 * GtkImage:icon-name:
283 * The name of the icon in the icon theme. If the icon theme is
284 * changed, the image will be updated automatically.
288 g_object_class_install_property (gobject_class,
290 g_param_spec_string ("icon-name",
292 P_("The name of the icon from the icon theme"),
294 GTK_PARAM_READWRITE));
299 * The GIcon displayed in the GtkImage. For themed icons,
300 * If the icon theme is changed, the image will be updated
305 g_object_class_install_property (gobject_class,
307 g_param_spec_object ("gicon",
309 P_("The GIcon being displayed"),
311 GTK_PARAM_READWRITE));
313 g_object_class_install_property (gobject_class,
315 g_param_spec_enum ("storage-type",
317 P_("The representation being used for image data"),
320 GTK_PARAM_READABLE));
323 * GtkImage:use-fallback:
325 * Whether the icon displayed in the GtkImage will use
326 * standard icon names fallback. The value of this property
327 * is only relevant for images of type %GTK_IMAGE_ICON_NAME
328 * and %GTK_IMAGE_GICON.
332 g_object_class_install_property (gobject_class,
334 g_param_spec_boolean ("use-fallback",
336 P_("Whether to use icon names fallback"),
338 GTK_PARAM_READWRITE));
340 g_type_class_add_private (class, sizeof (GtkImagePrivate));
342 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_IMAGE_ACCESSIBLE);
346 gtk_image_init (GtkImage *image)
348 GtkImagePrivate *priv;
350 image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image,
355 gtk_widget_set_has_window (GTK_WIDGET (image), FALSE);
356 priv->icon_helper = _gtk_icon_helper_new ();
358 priv->filename = NULL;
362 gtk_image_destroy (GtkWidget *widget)
364 GtkImage *image = GTK_IMAGE (widget);
366 g_clear_object (&image->priv->icon_helper);
368 GTK_WIDGET_CLASS (gtk_image_parent_class)->destroy (widget);
372 gtk_image_set_property (GObject *object,
377 GtkImage *image = GTK_IMAGE (object);
378 GtkImagePrivate *priv = image->priv;
379 GtkIconSize icon_size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
381 if (icon_size == GTK_ICON_SIZE_INVALID)
382 icon_size = DEFAULT_ICON_SIZE;
387 gtk_image_set_from_pixbuf (image,
388 g_value_get_object (value));
391 gtk_image_set_from_file (image, g_value_get_string (value));
394 gtk_image_set_from_stock (image, g_value_get_string (value),
398 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
402 _gtk_icon_helper_set_icon_size (priv->icon_helper, g_value_get_int (value));
404 case PROP_PIXEL_SIZE:
405 gtk_image_set_pixel_size (image, g_value_get_int (value));
407 case PROP_PIXBUF_ANIMATION:
408 gtk_image_set_from_animation (image,
409 g_value_get_object (value));
412 gtk_image_set_from_icon_name (image, g_value_get_string (value),
416 gtk_image_set_from_gicon (image, g_value_get_object (value),
420 case PROP_USE_FALLBACK:
421 _gtk_icon_helper_set_use_fallback (priv->icon_helper, g_value_get_boolean (value));
425 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
431 gtk_image_get_property (GObject *object,
436 GtkImage *image = GTK_IMAGE (object);
437 GtkImagePrivate *priv = image->priv;
442 g_value_set_object (value, _gtk_icon_helper_peek_pixbuf (priv->icon_helper));
445 g_value_set_string (value, priv->filename);
448 g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
451 g_value_set_boxed (value, _gtk_icon_helper_peek_icon_set (priv->icon_helper));
454 g_value_set_int (value, _gtk_icon_helper_get_icon_size (priv->icon_helper));
456 case PROP_PIXEL_SIZE:
457 g_value_set_int (value, _gtk_icon_helper_get_pixel_size (priv->icon_helper));
459 case PROP_PIXBUF_ANIMATION:
460 g_value_set_object (value, _gtk_icon_helper_peek_animation (priv->icon_helper));
463 g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
466 g_value_set_object (value, _gtk_icon_helper_peek_gicon (priv->icon_helper));
468 case PROP_USE_FALLBACK:
469 g_value_set_boolean (value, _gtk_icon_helper_get_use_fallback (priv->icon_helper));
471 case PROP_STORAGE_TYPE:
472 g_value_set_enum (value, _gtk_icon_helper_get_storage_type (priv->icon_helper));
475 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482 * gtk_image_new_from_file:
483 * @filename: (type filename): a filename
485 * Creates a new #GtkImage displaying the file @filename. If the file
486 * isn't found or can't be loaded, the resulting #GtkImage will
487 * display a "broken image" icon. This function never returns %NULL,
488 * it always returns a valid #GtkImage widget.
490 * If the file contains an animation, the image will contain an
493 * If you need to detect failures to load the file, use
494 * gdk_pixbuf_new_from_file() to load the file yourself, then create
495 * the #GtkImage from the pixbuf. (Or for animations, use
496 * gdk_pixbuf_animation_new_from_file()).
498 * The storage type (gtk_image_get_storage_type()) of the returned
499 * image is not defined, it will be whatever is appropriate for
500 * displaying the file.
502 * Return value: a new #GtkImage
505 gtk_image_new_from_file (const gchar *filename)
509 image = g_object_new (GTK_TYPE_IMAGE, NULL);
511 gtk_image_set_from_file (image, filename);
513 return GTK_WIDGET (image);
517 * gtk_image_new_from_resource:
518 * @resource_path: a resource path
520 * Creates a new #GtkImage displaying the resource file @resource_path. If the file
521 * isn't found or can't be loaded, the resulting #GtkImage will
522 * display a "broken image" icon. This function never returns %NULL,
523 * it always returns a valid #GtkImage widget.
525 * If the file contains an animation, the image will contain an
528 * If you need to detect failures to load the file, use
529 * gdk_pixbuf_new_from_file() to load the file yourself, then create
530 * the #GtkImage from the pixbuf. (Or for animations, use
531 * gdk_pixbuf_animation_new_from_file()).
533 * The storage type (gtk_image_get_storage_type()) of the returned
534 * image is not defined, it will be whatever is appropriate for
535 * displaying the file.
537 * Return value: a new #GtkImage
542 gtk_image_new_from_resource (const gchar *resource_path)
546 image = g_object_new (GTK_TYPE_IMAGE, NULL);
548 gtk_image_set_from_resource (image, resource_path);
550 return GTK_WIDGET (image);
554 * gtk_image_new_from_pixbuf:
555 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
557 * Creates a new #GtkImage displaying @pixbuf.
558 * The #GtkImage does not assume a reference to the
559 * pixbuf; you still need to unref it if you own references.
560 * #GtkImage will add its own reference rather than adopting yours.
562 * Note that this function just creates an #GtkImage from the pixbuf. The
563 * #GtkImage created will not react to state changes. Should you want that,
564 * you should use gtk_image_new_from_icon_set().
566 * Return value: a new #GtkImage
569 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
573 image = g_object_new (GTK_TYPE_IMAGE, NULL);
575 gtk_image_set_from_pixbuf (image, pixbuf);
577 return GTK_WIDGET (image);
581 * gtk_image_new_from_stock:
582 * @stock_id: a stock icon name
583 * @size: (type int): a stock icon size
585 * Creates a #GtkImage displaying a stock icon. Sample stock icon
586 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
587 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
588 * icon name isn't known, the image will be empty.
589 * You can register your own stock icon names, see
590 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
592 * Return value: a new #GtkImage displaying the stock icon
595 gtk_image_new_from_stock (const gchar *stock_id,
600 image = g_object_new (GTK_TYPE_IMAGE, NULL);
602 gtk_image_set_from_stock (image, stock_id, size);
604 return GTK_WIDGET (image);
608 * gtk_image_new_from_icon_set:
609 * @icon_set: a #GtkIconSet
610 * @size: (type int): a stock icon size
612 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
613 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
614 * this function, usually it's better to create a #GtkIconFactory, put
615 * your icon sets in the icon factory, add the icon factory to the
616 * list of default factories with gtk_icon_factory_add_default(), and
617 * then use gtk_image_new_from_stock(). This will allow themes to
618 * override the icon you ship with your application.
620 * The #GtkImage does not assume a reference to the
621 * icon set; you still need to unref it if you own references.
622 * #GtkImage will add its own reference rather than adopting yours.
624 * Return value: a new #GtkImage
627 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
632 image = g_object_new (GTK_TYPE_IMAGE, NULL);
634 gtk_image_set_from_icon_set (image, icon_set, size);
636 return GTK_WIDGET (image);
640 * gtk_image_new_from_animation:
641 * @animation: an animation
643 * Creates a #GtkImage displaying the given animation.
644 * The #GtkImage does not assume a reference to the
645 * animation; you still need to unref it if you own references.
646 * #GtkImage will add its own reference rather than adopting yours.
648 * Note that the animation frames are shown using a timeout with
649 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
650 * keep in mind that the animation will only be shown if the main loop
651 * is not busy with something that has a higher priority.
653 * Return value: a new #GtkImage widget
656 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
660 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
662 image = g_object_new (GTK_TYPE_IMAGE, NULL);
664 gtk_image_set_from_animation (image, animation);
666 return GTK_WIDGET (image);
670 * gtk_image_new_from_icon_name:
671 * @icon_name: an icon name
672 * @size: (type int): a stock icon size
674 * Creates a #GtkImage displaying an icon from the current icon theme.
675 * If the icon name isn't known, a "broken image" icon will be
676 * displayed instead. If the current icon theme is changed, the icon
677 * will be updated appropriately.
679 * Return value: a new #GtkImage displaying the themed icon
684 gtk_image_new_from_icon_name (const gchar *icon_name,
689 image = g_object_new (GTK_TYPE_IMAGE, NULL);
691 gtk_image_set_from_icon_name (image, icon_name, size);
693 return GTK_WIDGET (image);
697 * gtk_image_new_from_gicon:
699 * @size: (type int): a stock icon size
701 * Creates a #GtkImage displaying an icon from the current icon theme.
702 * If the icon name isn't known, a "broken image" icon will be
703 * displayed instead. If the current icon theme is changed, the icon
704 * will be updated appropriately.
706 * Return value: a new #GtkImage displaying the themed icon
711 gtk_image_new_from_gicon (GIcon *icon,
716 image = g_object_new (GTK_TYPE_IMAGE, NULL);
718 gtk_image_set_from_gicon (image, icon, size);
720 return GTK_WIDGET (image);
724 * gtk_image_set_from_file:
725 * @image: a #GtkImage
726 * @filename: (type filename) (allow-none): a filename or %NULL
728 * See gtk_image_new_from_file() for details.
731 gtk_image_set_from_file (GtkImage *image,
732 const gchar *filename)
734 GtkImagePrivate *priv;
735 GdkPixbufAnimation *anim;
737 g_return_if_fail (GTK_IS_IMAGE (image));
741 g_object_freeze_notify (G_OBJECT (image));
743 gtk_image_clear (image);
745 if (filename == NULL)
747 priv->filename = NULL;
748 g_object_thaw_notify (G_OBJECT (image));
752 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
756 gtk_image_set_from_stock (image,
757 GTK_STOCK_MISSING_IMAGE,
759 g_object_thaw_notify (G_OBJECT (image));
763 /* We could just unconditionally set_from_animation,
764 * but it's nicer for memory if we toss the animation
765 * if it's just a single pixbuf
768 if (gdk_pixbuf_animation_is_static_image (anim))
769 gtk_image_set_from_pixbuf (image,
770 gdk_pixbuf_animation_get_static_image (anim));
772 gtk_image_set_from_animation (image, anim);
774 g_object_unref (anim);
776 priv->filename = g_strdup (filename);
778 g_object_thaw_notify (G_OBJECT (image));
782 * gtk_image_set_from_resource:
783 * @image: a #GtkImage
784 * @resource_path: (allow-none): a resource path or %NULL
786 * See gtk_image_new_from_resource() for details.
789 gtk_image_set_from_resource (GtkImage *image,
790 const gchar *resource_path)
792 GdkPixbuf *pixbuf = NULL;
793 GInputStream *stream;
795 g_return_if_fail (GTK_IS_IMAGE (image));
797 g_object_freeze_notify (G_OBJECT (image));
799 gtk_image_clear (image);
801 if (resource_path == NULL)
803 g_object_thaw_notify (G_OBJECT (image));
807 stream = g_resources_open_stream (resource_path, 0, NULL);
810 pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, NULL);
811 g_object_unref (stream);
816 gtk_image_set_from_stock (image,
817 GTK_STOCK_MISSING_IMAGE,
819 g_object_thaw_notify (G_OBJECT (image));
823 gtk_image_set_from_pixbuf (image, pixbuf);
825 g_object_unref (pixbuf);
827 g_object_thaw_notify (G_OBJECT (image));
832 * gtk_image_set_from_pixbuf:
833 * @image: a #GtkImage
834 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
836 * See gtk_image_new_from_pixbuf() for details.
839 gtk_image_set_from_pixbuf (GtkImage *image,
842 GtkImagePrivate *priv;
844 g_return_if_fail (GTK_IS_IMAGE (image));
845 g_return_if_fail (pixbuf == NULL ||
846 GDK_IS_PIXBUF (pixbuf));
850 g_object_freeze_notify (G_OBJECT (image));
852 gtk_image_clear (image);
855 _gtk_icon_helper_set_pixbuf (priv->icon_helper, pixbuf);
857 g_object_notify (G_OBJECT (image), "pixbuf");
859 g_object_thaw_notify (G_OBJECT (image));
863 * gtk_image_set_from_stock:
864 * @image: a #GtkImage
865 * @stock_id: a stock icon name
866 * @size: (type int): a stock icon size
868 * See gtk_image_new_from_stock() for details.
871 gtk_image_set_from_stock (GtkImage *image,
872 const gchar *stock_id,
875 GtkImagePrivate *priv;
878 g_return_if_fail (GTK_IS_IMAGE (image));
882 g_object_freeze_notify (G_OBJECT (image));
884 new_id = g_strdup (stock_id);
885 gtk_image_clear (image);
889 _gtk_icon_helper_set_stock_id (priv->icon_helper, new_id, size);
893 g_object_notify (G_OBJECT (image), "stock");
894 g_object_notify (G_OBJECT (image), "icon-size");
896 g_object_thaw_notify (G_OBJECT (image));
900 * gtk_image_set_from_icon_set:
901 * @image: a #GtkImage
902 * @icon_set: a #GtkIconSet
903 * @size: (type int): a stock icon size
905 * See gtk_image_new_from_icon_set() for details.
908 gtk_image_set_from_icon_set (GtkImage *image,
909 GtkIconSet *icon_set,
912 GtkImagePrivate *priv;
914 g_return_if_fail (GTK_IS_IMAGE (image));
918 g_object_freeze_notify (G_OBJECT (image));
921 gtk_icon_set_ref (icon_set);
923 gtk_image_clear (image);
927 _gtk_icon_helper_set_icon_set (priv->icon_helper, icon_set, size);
928 gtk_icon_set_unref (icon_set);
931 g_object_notify (G_OBJECT (image), "icon-set");
932 g_object_notify (G_OBJECT (image), "icon-size");
934 g_object_thaw_notify (G_OBJECT (image));
938 * gtk_image_set_from_animation:
939 * @image: a #GtkImage
940 * @animation: the #GdkPixbufAnimation
942 * Causes the #GtkImage to display the given animation (or display
943 * nothing, if you set the animation to %NULL).
946 gtk_image_set_from_animation (GtkImage *image,
947 GdkPixbufAnimation *animation)
949 GtkImagePrivate *priv;
951 g_return_if_fail (GTK_IS_IMAGE (image));
952 g_return_if_fail (animation == NULL ||
953 GDK_IS_PIXBUF_ANIMATION (animation));
957 g_object_freeze_notify (G_OBJECT (image));
960 g_object_ref (animation);
962 gtk_image_clear (image);
964 if (animation != NULL)
966 _gtk_icon_helper_set_animation (priv->icon_helper, animation);
967 g_object_unref (animation);
970 g_object_notify (G_OBJECT (image), "pixbuf-animation");
972 g_object_thaw_notify (G_OBJECT (image));
976 * gtk_image_set_from_icon_name:
977 * @image: a #GtkImage
978 * @icon_name: an icon name
979 * @size: (type int): an icon size
981 * See gtk_image_new_from_icon_name() for details.
986 gtk_image_set_from_icon_name (GtkImage *image,
987 const gchar *icon_name,
990 GtkImagePrivate *priv;
993 g_return_if_fail (GTK_IS_IMAGE (image));
997 g_object_freeze_notify (G_OBJECT (image));
999 new_name = g_strdup (icon_name);
1000 gtk_image_clear (image);
1004 _gtk_icon_helper_set_icon_name (priv->icon_helper, new_name, size);
1008 g_object_notify (G_OBJECT (image), "icon-name");
1009 g_object_notify (G_OBJECT (image), "icon-size");
1011 g_object_thaw_notify (G_OBJECT (image));
1015 * gtk_image_set_from_gicon:
1016 * @image: a #GtkImage
1018 * @size: (type int): an icon size
1020 * See gtk_image_new_from_gicon() for details.
1025 gtk_image_set_from_gicon (GtkImage *image,
1029 GtkImagePrivate *priv;
1031 g_return_if_fail (GTK_IS_IMAGE (image));
1035 g_object_freeze_notify (G_OBJECT (image));
1038 g_object_ref (icon);
1040 gtk_image_clear (image);
1044 _gtk_icon_helper_set_gicon (priv->icon_helper, icon, size);
1045 g_object_unref (icon);
1048 g_object_notify (G_OBJECT (image), "gicon");
1049 g_object_notify (G_OBJECT (image), "icon-size");
1051 g_object_thaw_notify (G_OBJECT (image));
1055 * gtk_image_get_storage_type:
1056 * @image: a #GtkImage
1058 * Gets the type of representation being used by the #GtkImage
1059 * to store image data. If the #GtkImage has no image data,
1060 * the return value will be %GTK_IMAGE_EMPTY.
1062 * Return value: image representation being used
1065 gtk_image_get_storage_type (GtkImage *image)
1067 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1069 return _gtk_icon_helper_get_storage_type (image->priv->icon_helper);
1073 * gtk_image_get_pixbuf:
1074 * @image: a #GtkImage
1076 * Gets the #GdkPixbuf being displayed by the #GtkImage.
1077 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1078 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1079 * The caller of this function does not own a reference to the
1082 * Return value: (transfer none): the displayed pixbuf, or %NULL if
1083 * the image is empty
1086 gtk_image_get_pixbuf (GtkImage *image)
1088 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1090 return _gtk_icon_helper_peek_pixbuf (image->priv->icon_helper);
1094 * gtk_image_get_stock:
1095 * @image: a #GtkImage
1096 * @stock_id: (out) (transfer none) (allow-none): place to store a
1097 * stock icon name, or %NULL
1098 * @size: (out) (allow-none) (type int): place to store a stock icon
1101 * Gets the stock icon name and size being displayed by the #GtkImage.
1102 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1103 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1104 * The returned string is owned by the #GtkImage and should not
1108 gtk_image_get_stock (GtkImage *image,
1112 GtkImagePrivate *priv;
1114 g_return_if_fail (GTK_IS_IMAGE (image));
1119 *stock_id = (gchar *) _gtk_icon_helper_get_stock_id (priv->icon_helper);
1122 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1126 * gtk_image_get_icon_set:
1127 * @image: a #GtkImage
1128 * @icon_set: (out) (transfer none) (allow-none): location to store a
1129 * #GtkIconSet, or %NULL
1130 * @size: (out) (allow-none) (type int): location to store a stock
1131 * icon size, or %NULL
1133 * Gets the icon set and size being displayed by the #GtkImage.
1134 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1135 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1138 gtk_image_get_icon_set (GtkImage *image,
1139 GtkIconSet **icon_set,
1142 GtkImagePrivate *priv;
1144 g_return_if_fail (GTK_IS_IMAGE (image));
1149 *icon_set = _gtk_icon_helper_peek_icon_set (priv->icon_helper);
1152 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1156 * gtk_image_get_animation:
1157 * @image: a #GtkImage
1159 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1160 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1161 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1162 * The caller of this function does not own a reference to the
1163 * returned animation.
1165 * Return value: (transfer none): the displayed animation, or %NULL if
1166 * the image is empty
1169 gtk_image_get_animation (GtkImage *image)
1171 GtkImagePrivate *priv;
1173 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1177 return _gtk_icon_helper_peek_animation (priv->icon_helper);
1181 * gtk_image_get_icon_name:
1182 * @image: a #GtkImage
1183 * @icon_name: (out) (transfer none) (allow-none): place to store an
1184 * icon name, or %NULL
1185 * @size: (out) (allow-none) (type int): place to store an icon size,
1188 * Gets the icon name and size being displayed by the #GtkImage.
1189 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1190 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1191 * The returned string is owned by the #GtkImage and should not
1197 gtk_image_get_icon_name (GtkImage *image,
1198 const gchar **icon_name,
1201 GtkImagePrivate *priv;
1203 g_return_if_fail (GTK_IS_IMAGE (image));
1208 *icon_name = _gtk_icon_helper_get_icon_name (priv->icon_helper);
1211 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1215 * gtk_image_get_gicon:
1216 * @image: a #GtkImage
1217 * @gicon: (out) (transfer none) (allow-none): place to store a
1219 * @size: (out) (allow-none) (type int): place to store an icon size,
1222 * Gets the #GIcon and size being displayed by the #GtkImage.
1223 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1224 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1225 * The caller of this function does not own a reference to the
1231 gtk_image_get_gicon (GtkImage *image,
1235 GtkImagePrivate *priv;
1237 g_return_if_fail (GTK_IS_IMAGE (image));
1242 *gicon = _gtk_icon_helper_peek_gicon (priv->icon_helper);
1245 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1251 * Creates a new empty #GtkImage widget.
1253 * Return value: a newly created #GtkImage widget.
1256 gtk_image_new (void)
1258 return g_object_new (GTK_TYPE_IMAGE, NULL);
1262 gtk_image_reset_anim_iter (GtkImage *image)
1264 GtkImagePrivate *priv = image->priv;
1266 if (gtk_image_get_storage_type (image) == GTK_IMAGE_ANIMATION)
1268 /* Reset the animation */
1269 if (priv->animation_timeout)
1271 g_source_remove (priv->animation_timeout);
1272 priv->animation_timeout = 0;
1275 g_clear_object (&priv->animation_iter);
1280 gtk_image_unmap (GtkWidget *widget)
1282 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1284 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1288 gtk_image_unrealize (GtkWidget *widget)
1290 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1292 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1296 animation_timeout (gpointer data)
1298 GtkImage *image = GTK_IMAGE (data);
1299 GtkImagePrivate *priv = image->priv;
1302 priv->animation_timeout = 0;
1304 gdk_pixbuf_animation_iter_advance (priv->animation_iter, NULL);
1306 delay = gdk_pixbuf_animation_iter_get_delay_time (priv->animation_iter);
1309 GtkWidget *widget = GTK_WIDGET (image);
1311 priv->animation_timeout =
1312 gdk_threads_add_timeout (delay, animation_timeout, image);
1314 gtk_widget_queue_draw (widget);
1321 get_animation_frame (GtkImage *image)
1323 GtkImagePrivate *priv = image->priv;
1325 if (priv->animation_iter == NULL)
1329 priv->animation_iter =
1330 gdk_pixbuf_animation_get_iter (_gtk_icon_helper_peek_animation (priv->icon_helper), NULL);
1332 delay = gdk_pixbuf_animation_iter_get_delay_time (priv->animation_iter);
1334 priv->animation_timeout =
1335 gdk_threads_add_timeout (delay, animation_timeout, image);
1338 /* don't advance the anim iter here, or we could get frame changes between two
1339 * exposes of different areas.
1341 return g_object_ref (gdk_pixbuf_animation_iter_get_pixbuf (priv->animation_iter));
1345 gtk_image_get_preferred_size (GtkImage *image,
1349 GtkImagePrivate *priv = image->priv;
1352 GtkStyleContext *context;
1354 context = gtk_widget_get_style_context (GTK_WIDGET (image));
1355 _gtk_icon_helper_get_size (priv->icon_helper, context, &width, &height);
1356 _gtk_misc_get_padding_and_border (GTK_MISC (image), &border);
1358 width += border.left + border.right;
1359 height += border.top + border.bottom;
1364 *height_out = height;
1368 gtk_image_draw (GtkWidget *widget,
1372 GtkImagePrivate *priv;
1374 GtkStyleContext *context;
1375 gint x, y, width, height;
1376 gfloat xalign, yalign;
1379 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1381 image = GTK_IMAGE (widget);
1382 misc = GTK_MISC (image);
1385 context = gtk_widget_get_style_context (widget);
1387 gtk_misc_get_alignment (misc, &xalign, &yalign);
1388 gtk_image_get_preferred_size (image, &width, &height);
1389 _gtk_misc_get_padding_and_border (misc, &border);
1391 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
1392 xalign = 1.0 - xalign;
1394 x = floor ((gtk_widget_get_allocated_width (widget) - width) * xalign);
1395 y = floor ((gtk_widget_get_allocated_height (widget) - height) * yalign);
1397 gtk_render_background (context, cr, x, y, width, height);
1398 gtk_render_frame (context, cr, x, y, width, height);
1403 if (gtk_image_get_storage_type (image) == GTK_IMAGE_ANIMATION)
1405 GdkPixbuf *pixbuf = get_animation_frame (image);
1406 gtk_render_icon (context, cr,
1409 g_object_unref (pixbuf);
1413 _gtk_icon_helper_draw (priv->icon_helper,
1422 gtk_image_reset (GtkImage *image)
1424 GtkImagePrivate *priv = image->priv;
1425 GtkImageType storage_type;
1427 g_object_freeze_notify (G_OBJECT (image));
1428 storage_type = gtk_image_get_storage_type (image);
1430 if (storage_type != GTK_IMAGE_EMPTY)
1431 g_object_notify (G_OBJECT (image), "storage-type");
1433 g_object_notify (G_OBJECT (image), "icon-size");
1435 switch (storage_type)
1437 case GTK_IMAGE_PIXBUF:
1438 g_object_notify (G_OBJECT (image), "pixbuf");
1440 case GTK_IMAGE_STOCK:
1441 g_object_notify (G_OBJECT (image), "stock");
1443 case GTK_IMAGE_ICON_SET:
1444 g_object_notify (G_OBJECT (image), "icon-set");
1446 case GTK_IMAGE_ANIMATION:
1447 gtk_image_reset_anim_iter (image);
1448 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1450 case GTK_IMAGE_ICON_NAME:
1451 g_object_notify (G_OBJECT (image), "icon-name");
1453 case GTK_IMAGE_GICON:
1454 g_object_notify (G_OBJECT (image), "gicon");
1456 case GTK_IMAGE_EMPTY:
1463 g_free (priv->filename);
1464 priv->filename = NULL;
1465 g_object_notify (G_OBJECT (image), "file");
1468 _gtk_icon_helper_clear (priv->icon_helper);
1470 g_object_thaw_notify (G_OBJECT (image));
1475 * @image: a #GtkImage
1477 * Resets the image to be empty.
1482 gtk_image_clear (GtkImage *image)
1484 gtk_image_reset (image);
1486 if (gtk_widget_get_visible (GTK_WIDGET (image)))
1487 gtk_widget_queue_resize (GTK_WIDGET (image));
1491 gtk_image_get_preferred_width (GtkWidget *widget,
1497 gtk_image_get_preferred_size (GTK_IMAGE (widget), &width, NULL);
1498 *minimum = *natural = width;
1502 gtk_image_get_preferred_height (GtkWidget *widget,
1508 gtk_image_get_preferred_size (GTK_IMAGE (widget), NULL, &height);
1509 *minimum = *natural = height;
1513 icon_theme_changed (GtkImage *image)
1515 GtkImagePrivate *priv = image->priv;
1517 _gtk_icon_helper_invalidate (priv->icon_helper);
1518 gtk_widget_queue_draw (GTK_WIDGET (image));
1522 gtk_image_style_updated (GtkWidget *widget)
1524 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_updated (widget);
1526 icon_theme_changed (GTK_IMAGE (widget));
1530 gtk_image_screen_changed (GtkWidget *widget,
1531 GdkScreen *prev_screen)
1535 image = GTK_IMAGE (widget);
1537 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
1538 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
1540 icon_theme_changed (image);
1544 _gtk_image_gicon_data_clear (GtkImageGIconData *data)
1548 g_object_unref (data->pixbuf);
1549 data->pixbuf = NULL;
1553 g_object_unref (data->icon);
1559 * gtk_image_set_pixel_size:
1560 * @image: a #GtkImage
1561 * @pixel_size: the new pixel size
1563 * Sets the pixel size to use for named icons. If the pixel size is set
1564 * to a value != -1, it is used instead of the icon size set by
1565 * gtk_image_set_from_icon_name().
1570 gtk_image_set_pixel_size (GtkImage *image,
1573 GtkImagePrivate *priv;
1574 gint old_pixel_size;
1576 g_return_if_fail (GTK_IS_IMAGE (image));
1579 old_pixel_size = gtk_image_get_pixel_size (image);
1581 if (pixel_size != old_pixel_size)
1583 _gtk_icon_helper_set_pixel_size (priv->icon_helper, pixel_size);
1585 if (gtk_widget_get_visible (GTK_WIDGET (image)))
1586 gtk_widget_queue_resize (GTK_WIDGET (image));
1588 g_object_notify (G_OBJECT (image), "pixel-size");
1593 * gtk_image_get_pixel_size:
1594 * @image: a #GtkImage
1596 * Gets the pixel size used for named icons.
1598 * Returns: the pixel size used for named icons.
1603 gtk_image_get_pixel_size (GtkImage *image)
1605 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
1607 return _gtk_icon_helper_get_pixel_size (image->priv->icon_helper);