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/.
32 #include "gtkcontainer.h"
33 #include "gtkiconhelperprivate.h"
34 #include "gtkimageprivate.h"
35 #include "gtkiconfactory.h"
37 #include "gtkicontheme.h"
38 #include "gtksizerequest.h"
40 #include "gtkprivate.h"
41 #include "gtktypebuiltins.h"
43 #include "a11y/gtkimageaccessible.h"
47 * @Short_description: A widget displaying an image
49 * @See_also:#GdkPixbuf
51 * The #GtkImage widget displays an image. Various kinds of object
52 * can be displayed as an image; most typically, you would load a
53 * #GdkPixbuf ("pixel buffer") from a file, and then display that.
54 * There's a convenience function to do this, gtk_image_new_from_file(),
56 * <informalexample><programlisting>
58 * image = gtk_image_new_from_file ("myfile.png");
59 * </programlisting></informalexample>
60 * If the file isn't loaded successfully, the image will contain a
61 * "broken image" icon similar to that used in many web browsers.
62 * If you want to handle errors in loading the file yourself,
63 * for example by displaying an error message, then load the image with
64 * gdk_pixbuf_new_from_file(), then create the #GtkImage with
65 * gtk_image_new_from_pixbuf().
67 * The image file may contain an animation, if so the #GtkImage will
68 * display an animation (#GdkPixbufAnimation) instead of a static image.
70 * #GtkImage is a subclass of #GtkMisc, which implies that you can
71 * align it (center, left, right) and add padding to it, using
74 * #GtkImage is a "no window" widget (has no #GdkWindow of its own),
75 * so by default does not receive events. If you want to receive events
76 * on the image, such as button clicks, place the image inside a
77 * #GtkEventBox, then connect to the event signals on the event box.
79 * <title>Handling button press events on a
80 * <structname>GtkImage</structname>.</title>
83 * button_press_callback (GtkWidget *event_box,
84 * GdkEventButton *event,
87 * g_print ("Event box clicked at coordinates %f,%f\n",
88 * event->x, event->y);
90 * /<!---->* Returning TRUE means we handled the event, so the signal
91 * * emission should be stopped (don't call any further
92 * * callbacks that may be connected). Return FALSE
93 * * to continue invoking callbacks.
102 * GtkWidget *event_box;
104 * image = gtk_image_new_from_file ("myfile.png");
106 * event_box = gtk_event_box_new (<!-- -->);
108 * gtk_container_add (GTK_CONTAINER (event_box), image);
110 * g_signal_connect (G_OBJECT (event_box),
111 * "button_press_event",
112 * G_CALLBACK (button_press_callback),
120 * When handling events on the event box, keep in mind that coordinates
121 * in the image may be different from event box coordinates due to
122 * the alignment and padding settings on the image (see #GtkMisc).
123 * The simplest way to solve this is to set the alignment to 0.0
124 * (left/top), and set the padding to zero. Then the origin of
125 * the image will be the same as the origin of the event box.
127 * Sometimes an application will want to avoid depending on external data
128 * files, such as image files. GTK+ comes with a program to avoid this,
129 * called <application>gdk-pixbuf-csource</application>. This program
130 * allows you to convert an image into a C variable declaration, which
131 * can then be loaded into a #GdkPixbuf using
132 * gdk_pixbuf_new_from_inline().
136 struct _GtkImagePrivate
138 GtkIconHelper *icon_helper;
140 gint animation_timeout;
141 GdkPixbufAnimationIter *animation_iter;
143 gchar *filename; /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
147 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
148 static gint gtk_image_draw (GtkWidget *widget,
150 static void gtk_image_unmap (GtkWidget *widget);
151 static void gtk_image_unrealize (GtkWidget *widget);
152 static void gtk_image_get_preferred_width (GtkWidget *widget,
155 static void gtk_image_get_preferred_height (GtkWidget *widget,
159 static void gtk_image_style_updated (GtkWidget *widget);
160 static void gtk_image_screen_changed (GtkWidget *widget,
161 GdkScreen *prev_screen);
162 static void gtk_image_destroy (GtkWidget *widget);
163 static void gtk_image_reset (GtkImage *image);
165 static void gtk_image_set_property (GObject *object,
169 static void gtk_image_get_property (GObject *object,
174 static void icon_theme_changed (GtkImage *image);
185 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;
205 widget_class = GTK_WIDGET_CLASS (class);
206 widget_class->draw = gtk_image_draw;
207 widget_class->destroy = gtk_image_destroy;
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));
315 g_object_class_install_property (gobject_class,
317 g_param_spec_enum ("storage-type",
319 P_("The representation being used for image data"),
322 GTK_PARAM_READABLE));
325 * GtkImage:use-fallback:
327 * Whether the icon displayed in the GtkImage will use
328 * standard icon names fallback. The value of this property
329 * is only relevant for images of type %GTK_IMAGE_ICON_NAME
330 * and %GTK_IMAGE_GICON.
334 g_object_class_install_property (gobject_class,
336 g_param_spec_boolean ("use-fallback",
338 P_("Whether to use icon names fallback"),
340 GTK_PARAM_READWRITE));
342 g_type_class_add_private (class, sizeof (GtkImagePrivate));
344 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_IMAGE_ACCESSIBLE);
348 gtk_image_init (GtkImage *image)
350 GtkImagePrivate *priv;
352 image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image,
357 gtk_widget_set_has_window (GTK_WIDGET (image), FALSE);
358 priv->icon_helper = _gtk_icon_helper_new ();
360 priv->filename = NULL;
364 gtk_image_destroy (GtkWidget *widget)
366 GtkImage *image = GTK_IMAGE (widget);
368 g_clear_object (&image->priv->icon_helper);
370 GTK_WIDGET_CLASS (gtk_image_parent_class)->destroy (widget);
374 gtk_image_set_property (GObject *object,
379 GtkImage *image = GTK_IMAGE (object);
380 GtkImagePrivate *priv = image->priv;
381 GtkIconSize icon_size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
383 if (icon_size == GTK_ICON_SIZE_INVALID)
384 icon_size = DEFAULT_ICON_SIZE;
389 gtk_image_set_from_pixbuf (image,
390 g_value_get_object (value));
393 gtk_image_set_from_file (image, g_value_get_string (value));
396 gtk_image_set_from_stock (image, g_value_get_string (value),
400 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
404 _gtk_icon_helper_set_icon_size (priv->icon_helper, g_value_get_int (value));
406 case PROP_PIXEL_SIZE:
407 gtk_image_set_pixel_size (image, g_value_get_int (value));
409 case PROP_PIXBUF_ANIMATION:
410 gtk_image_set_from_animation (image,
411 g_value_get_object (value));
414 gtk_image_set_from_icon_name (image, g_value_get_string (value),
418 gtk_image_set_from_gicon (image, g_value_get_object (value),
422 case PROP_USE_FALLBACK:
423 _gtk_icon_helper_set_use_fallback (priv->icon_helper, g_value_get_boolean (value));
427 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
433 gtk_image_get_property (GObject *object,
438 GtkImage *image = GTK_IMAGE (object);
439 GtkImagePrivate *priv = image->priv;
444 g_value_set_object (value, _gtk_icon_helper_peek_pixbuf (priv->icon_helper));
447 g_value_set_string (value, priv->filename);
450 g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
453 g_value_set_boxed (value, _gtk_icon_helper_peek_icon_set (priv->icon_helper));
456 g_value_set_int (value, _gtk_icon_helper_get_icon_size (priv->icon_helper));
458 case PROP_PIXEL_SIZE:
459 g_value_set_int (value, _gtk_icon_helper_get_pixel_size (priv->icon_helper));
461 case PROP_PIXBUF_ANIMATION:
462 g_value_set_object (value, _gtk_icon_helper_peek_animation (priv->icon_helper));
465 g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
468 g_value_set_object (value, _gtk_icon_helper_peek_gicon (priv->icon_helper));
470 case PROP_USE_FALLBACK:
471 g_value_set_boolean (value, _gtk_icon_helper_get_use_fallback (priv->icon_helper));
474 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
481 * gtk_image_new_from_file:
482 * @filename: (type filename): a filename
484 * Creates a new #GtkImage displaying the file @filename. If the file
485 * isn't found or can't be loaded, the resulting #GtkImage will
486 * display a "broken image" icon. This function never returns %NULL,
487 * it always returns a valid #GtkImage widget.
489 * If the file contains an animation, the image will contain an
492 * If you need to detect failures to load the file, use
493 * gdk_pixbuf_new_from_file() to load the file yourself, then create
494 * the #GtkImage from the pixbuf. (Or for animations, use
495 * gdk_pixbuf_animation_new_from_file()).
497 * The storage type (gtk_image_get_storage_type()) of the returned
498 * image is not defined, it will be whatever is appropriate for
499 * displaying the file.
501 * Return value: a new #GtkImage
504 gtk_image_new_from_file (const gchar *filename)
508 image = g_object_new (GTK_TYPE_IMAGE, NULL);
510 gtk_image_set_from_file (image, filename);
512 return GTK_WIDGET (image);
516 * gtk_image_new_from_pixbuf:
517 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
519 * Creates a new #GtkImage displaying @pixbuf.
520 * The #GtkImage does not assume a reference to the
521 * pixbuf; you still need to unref it if you own references.
522 * #GtkImage will add its own reference rather than adopting yours.
524 * Note that this function just creates an #GtkImage from the pixbuf. The
525 * #GtkImage created will not react to state changes. Should you want that,
526 * you should use gtk_image_new_from_icon_set().
528 * Return value: a new #GtkImage
531 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
535 image = g_object_new (GTK_TYPE_IMAGE, NULL);
537 gtk_image_set_from_pixbuf (image, pixbuf);
539 return GTK_WIDGET (image);
543 * gtk_image_new_from_stock:
544 * @stock_id: a stock icon name
545 * @size: (type int): a stock icon size
547 * Creates a #GtkImage displaying a stock icon. Sample stock icon
548 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
549 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
550 * icon name isn't known, the image will be empty.
551 * You can register your own stock icon names, see
552 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
554 * Return value: a new #GtkImage displaying the stock icon
557 gtk_image_new_from_stock (const gchar *stock_id,
562 image = g_object_new (GTK_TYPE_IMAGE, NULL);
564 gtk_image_set_from_stock (image, stock_id, size);
566 return GTK_WIDGET (image);
570 * gtk_image_new_from_icon_set:
571 * @icon_set: a #GtkIconSet
572 * @size: (type int): a stock icon size
574 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
575 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
576 * this function, usually it's better to create a #GtkIconFactory, put
577 * your icon sets in the icon factory, add the icon factory to the
578 * list of default factories with gtk_icon_factory_add_default(), and
579 * then use gtk_image_new_from_stock(). This will allow themes to
580 * override the icon you ship with your application.
582 * The #GtkImage does not assume a reference to the
583 * icon set; you still need to unref it if you own references.
584 * #GtkImage will add its own reference rather than adopting yours.
586 * Return value: a new #GtkImage
589 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
594 image = g_object_new (GTK_TYPE_IMAGE, NULL);
596 gtk_image_set_from_icon_set (image, icon_set, size);
598 return GTK_WIDGET (image);
602 * gtk_image_new_from_animation:
603 * @animation: an animation
605 * Creates a #GtkImage displaying the given animation.
606 * The #GtkImage does not assume a reference to the
607 * animation; you still need to unref it if you own references.
608 * #GtkImage will add its own reference rather than adopting yours.
610 * Note that the animation frames are shown using a timeout with
611 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
612 * keep in mind that the animation will only be shown if the main loop
613 * is not busy with something that has a higher priority.
615 * Return value: a new #GtkImage widget
618 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
622 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
624 image = g_object_new (GTK_TYPE_IMAGE, NULL);
626 gtk_image_set_from_animation (image, animation);
628 return GTK_WIDGET (image);
632 * gtk_image_new_from_icon_name:
633 * @icon_name: an icon name
634 * @size: (type int): a stock icon size
636 * Creates a #GtkImage displaying an icon from the current icon theme.
637 * If the icon name isn't known, a "broken image" icon will be
638 * displayed instead. If the current icon theme is changed, the icon
639 * will be updated appropriately.
641 * Return value: a new #GtkImage displaying the themed icon
646 gtk_image_new_from_icon_name (const gchar *icon_name,
651 image = g_object_new (GTK_TYPE_IMAGE, NULL);
653 gtk_image_set_from_icon_name (image, icon_name, size);
655 return GTK_WIDGET (image);
659 * gtk_image_new_from_gicon:
661 * @size: (type int): a stock icon size
663 * Creates a #GtkImage displaying an icon from the current icon theme.
664 * If the icon name isn't known, a "broken image" icon will be
665 * displayed instead. If the current icon theme is changed, the icon
666 * will be updated appropriately.
668 * Return value: a new #GtkImage displaying the themed icon
673 gtk_image_new_from_gicon (GIcon *icon,
678 image = g_object_new (GTK_TYPE_IMAGE, NULL);
680 gtk_image_set_from_gicon (image, icon, size);
682 return GTK_WIDGET (image);
686 * gtk_image_set_from_file:
687 * @image: a #GtkImage
688 * @filename: (type filename) (allow-none): a filename or %NULL
690 * See gtk_image_new_from_file() for details.
693 gtk_image_set_from_file (GtkImage *image,
694 const gchar *filename)
696 GtkImagePrivate *priv;
697 GdkPixbufAnimation *anim;
699 g_return_if_fail (GTK_IS_IMAGE (image));
703 g_object_freeze_notify (G_OBJECT (image));
705 gtk_image_clear (image);
707 if (filename == NULL)
709 priv->filename = NULL;
710 g_object_thaw_notify (G_OBJECT (image));
714 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
718 gtk_image_set_from_stock (image,
719 GTK_STOCK_MISSING_IMAGE,
721 g_object_thaw_notify (G_OBJECT (image));
725 /* We could just unconditionally set_from_animation,
726 * but it's nicer for memory if we toss the animation
727 * if it's just a single pixbuf
730 if (gdk_pixbuf_animation_is_static_image (anim))
731 gtk_image_set_from_pixbuf (image,
732 gdk_pixbuf_animation_get_static_image (anim));
734 gtk_image_set_from_animation (image, anim);
736 g_object_unref (anim);
738 priv->filename = g_strdup (filename);
740 g_object_thaw_notify (G_OBJECT (image));
744 * gtk_image_set_from_pixbuf:
745 * @image: a #GtkImage
746 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
748 * See gtk_image_new_from_pixbuf() for details.
751 gtk_image_set_from_pixbuf (GtkImage *image,
754 GtkImagePrivate *priv;
756 g_return_if_fail (GTK_IS_IMAGE (image));
757 g_return_if_fail (pixbuf == NULL ||
758 GDK_IS_PIXBUF (pixbuf));
762 g_object_freeze_notify (G_OBJECT (image));
764 gtk_image_clear (image);
767 _gtk_icon_helper_set_pixbuf (priv->icon_helper, pixbuf);
769 g_object_notify (G_OBJECT (image), "pixbuf");
771 g_object_thaw_notify (G_OBJECT (image));
775 * gtk_image_set_from_stock:
776 * @image: a #GtkImage
777 * @stock_id: a stock icon name
778 * @size: (type int): a stock icon size
780 * See gtk_image_new_from_stock() for details.
783 gtk_image_set_from_stock (GtkImage *image,
784 const gchar *stock_id,
787 GtkImagePrivate *priv;
790 g_return_if_fail (GTK_IS_IMAGE (image));
794 g_object_freeze_notify (G_OBJECT (image));
796 new_id = g_strdup (stock_id);
797 gtk_image_clear (image);
801 _gtk_icon_helper_set_stock_id (priv->icon_helper, new_id, size);
805 g_object_notify (G_OBJECT (image), "stock");
806 g_object_notify (G_OBJECT (image), "icon-size");
808 g_object_thaw_notify (G_OBJECT (image));
812 * gtk_image_set_from_icon_set:
813 * @image: a #GtkImage
814 * @icon_set: a #GtkIconSet
815 * @size: (type int): a stock icon size
817 * See gtk_image_new_from_icon_set() for details.
820 gtk_image_set_from_icon_set (GtkImage *image,
821 GtkIconSet *icon_set,
824 GtkImagePrivate *priv;
826 g_return_if_fail (GTK_IS_IMAGE (image));
830 g_object_freeze_notify (G_OBJECT (image));
833 gtk_icon_set_ref (icon_set);
835 gtk_image_clear (image);
839 _gtk_icon_helper_set_icon_set (priv->icon_helper, icon_set, size);
840 gtk_icon_set_unref (icon_set);
843 g_object_notify (G_OBJECT (image), "icon-set");
844 g_object_notify (G_OBJECT (image), "icon-size");
846 g_object_thaw_notify (G_OBJECT (image));
850 * gtk_image_set_from_animation:
851 * @image: a #GtkImage
852 * @animation: the #GdkPixbufAnimation
854 * Causes the #GtkImage to display the given animation (or display
855 * nothing, if you set the animation to %NULL).
858 gtk_image_set_from_animation (GtkImage *image,
859 GdkPixbufAnimation *animation)
861 GtkImagePrivate *priv;
863 g_return_if_fail (GTK_IS_IMAGE (image));
864 g_return_if_fail (animation == NULL ||
865 GDK_IS_PIXBUF_ANIMATION (animation));
869 g_object_freeze_notify (G_OBJECT (image));
872 g_object_ref (animation);
874 gtk_image_clear (image);
876 if (animation != NULL)
878 _gtk_icon_helper_set_animation (priv->icon_helper, animation);
879 g_object_unref (animation);
882 g_object_notify (G_OBJECT (image), "pixbuf-animation");
884 g_object_thaw_notify (G_OBJECT (image));
888 * gtk_image_set_from_icon_name:
889 * @image: a #GtkImage
890 * @icon_name: an icon name
891 * @size: (type int): an icon size
893 * See gtk_image_new_from_icon_name() for details.
898 gtk_image_set_from_icon_name (GtkImage *image,
899 const gchar *icon_name,
902 GtkImagePrivate *priv;
905 g_return_if_fail (GTK_IS_IMAGE (image));
909 g_object_freeze_notify (G_OBJECT (image));
911 new_name = g_strdup (icon_name);
912 gtk_image_clear (image);
916 _gtk_icon_helper_set_icon_name (priv->icon_helper, new_name, size);
920 g_object_notify (G_OBJECT (image), "icon-name");
921 g_object_notify (G_OBJECT (image), "icon-size");
923 g_object_thaw_notify (G_OBJECT (image));
927 * gtk_image_set_from_gicon:
928 * @image: a #GtkImage
930 * @size: (type int): an icon size
932 * See gtk_image_new_from_gicon() for details.
937 gtk_image_set_from_gicon (GtkImage *image,
941 GtkImagePrivate *priv;
943 g_return_if_fail (GTK_IS_IMAGE (image));
947 g_object_freeze_notify (G_OBJECT (image));
952 gtk_image_clear (image);
956 _gtk_icon_helper_set_gicon (priv->icon_helper, icon, size);
957 g_object_unref (icon);
960 g_object_notify (G_OBJECT (image), "gicon");
961 g_object_notify (G_OBJECT (image), "icon-size");
963 g_object_thaw_notify (G_OBJECT (image));
967 * gtk_image_get_storage_type:
968 * @image: a #GtkImage
970 * Gets the type of representation being used by the #GtkImage
971 * to store image data. If the #GtkImage has no image data,
972 * the return value will be %GTK_IMAGE_EMPTY.
974 * Return value: image representation being used
977 gtk_image_get_storage_type (GtkImage *image)
979 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
981 return _gtk_icon_helper_get_storage_type (image->priv->icon_helper);
985 * gtk_image_get_pixbuf:
986 * @image: a #GtkImage
988 * Gets the #GdkPixbuf being displayed by the #GtkImage.
989 * The storage type of the image must be %GTK_IMAGE_EMPTY or
990 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
991 * The caller of this function does not own a reference to the
994 * Return value: (transfer none): the displayed pixbuf, or %NULL if
998 gtk_image_get_pixbuf (GtkImage *image)
1000 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1002 return _gtk_icon_helper_peek_pixbuf (image->priv->icon_helper);
1006 * gtk_image_get_stock:
1007 * @image: a #GtkImage
1008 * @stock_id: (out) (transfer none) (allow-none): place to store a
1009 * stock icon name, or %NULL
1010 * @size: (out) (allow-none) (type int): place to store a stock icon
1013 * Gets the stock icon name and size being displayed by the #GtkImage.
1014 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1015 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1016 * The returned string is owned by the #GtkImage and should not
1020 gtk_image_get_stock (GtkImage *image,
1024 GtkImagePrivate *priv;
1026 g_return_if_fail (GTK_IS_IMAGE (image));
1031 *stock_id = (gchar *) _gtk_icon_helper_get_stock_id (priv->icon_helper);
1034 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1038 * gtk_image_get_icon_set:
1039 * @image: a #GtkImage
1040 * @icon_set: (out) (transfer none) (allow-none): location to store a
1041 * #GtkIconSet, or %NULL
1042 * @size: (out) (allow-none) (type int): location to store a stock
1043 * icon size, or %NULL
1045 * Gets the icon set and size being displayed by the #GtkImage.
1046 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1047 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1050 gtk_image_get_icon_set (GtkImage *image,
1051 GtkIconSet **icon_set,
1054 GtkImagePrivate *priv;
1056 g_return_if_fail (GTK_IS_IMAGE (image));
1061 *icon_set = _gtk_icon_helper_peek_icon_set (priv->icon_helper);
1064 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1068 * gtk_image_get_animation:
1069 * @image: a #GtkImage
1071 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1072 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1073 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1074 * The caller of this function does not own a reference to the
1075 * returned animation.
1077 * Return value: (transfer none): the displayed animation, or %NULL if
1078 * the image is empty
1081 gtk_image_get_animation (GtkImage *image)
1083 GtkImagePrivate *priv;
1085 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1089 return _gtk_icon_helper_peek_animation (priv->icon_helper);
1093 * gtk_image_get_icon_name:
1094 * @image: a #GtkImage
1095 * @icon_name: (out) (transfer none) (allow-none): place to store an
1096 * icon name, or %NULL
1097 * @size: (out) (allow-none) (type int): place to store an icon size,
1100 * Gets the icon name and size being displayed by the #GtkImage.
1101 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1102 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1103 * The returned string is owned by the #GtkImage and should not
1109 gtk_image_get_icon_name (GtkImage *image,
1110 const gchar **icon_name,
1113 GtkImagePrivate *priv;
1115 g_return_if_fail (GTK_IS_IMAGE (image));
1120 *icon_name = _gtk_icon_helper_get_icon_name (priv->icon_helper);
1123 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1127 * gtk_image_get_gicon:
1128 * @image: a #GtkImage
1129 * @gicon: (out) (transfer none) (allow-none): place to store a
1131 * @size: (out) (allow-none) (type int): place to store an icon size,
1134 * Gets the #GIcon and size being displayed by the #GtkImage.
1135 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1136 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1137 * The caller of this function does not own a reference to the
1143 gtk_image_get_gicon (GtkImage *image,
1147 GtkImagePrivate *priv;
1149 g_return_if_fail (GTK_IS_IMAGE (image));
1154 *gicon = _gtk_icon_helper_peek_gicon (priv->icon_helper);
1157 *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1163 * Creates a new empty #GtkImage widget.
1165 * Return value: a newly created #GtkImage widget.
1168 gtk_image_new (void)
1170 return g_object_new (GTK_TYPE_IMAGE, NULL);
1174 gtk_image_reset_anim_iter (GtkImage *image)
1176 GtkImagePrivate *priv = image->priv;
1178 if (gtk_image_get_storage_type (image) == GTK_IMAGE_ANIMATION)
1180 /* Reset the animation */
1181 if (priv->animation_timeout)
1183 g_source_remove (priv->animation_timeout);
1184 priv->animation_timeout = 0;
1187 g_clear_object (&priv->animation_iter);
1192 gtk_image_unmap (GtkWidget *widget)
1194 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1196 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1200 gtk_image_unrealize (GtkWidget *widget)
1202 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1204 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1208 animation_timeout (gpointer data)
1210 GtkImage *image = GTK_IMAGE (data);
1211 GtkImagePrivate *priv = image->priv;
1214 priv->animation_timeout = 0;
1216 gdk_pixbuf_animation_iter_advance (priv->animation_iter, NULL);
1218 delay = gdk_pixbuf_animation_iter_get_delay_time (priv->animation_iter);
1221 GtkWidget *widget = GTK_WIDGET (image);
1223 priv->animation_timeout =
1224 gdk_threads_add_timeout (delay, animation_timeout, image);
1226 gtk_widget_queue_draw (widget);
1228 if (gtk_widget_is_drawable (widget))
1229 gdk_window_process_updates (gtk_widget_get_window (widget), TRUE);
1236 get_animation_frame (GtkImage *image)
1238 GtkImagePrivate *priv = image->priv;
1240 if (priv->animation_iter == NULL)
1244 priv->animation_iter =
1245 gdk_pixbuf_animation_get_iter (_gtk_icon_helper_peek_animation (priv->icon_helper), NULL);
1247 delay = gdk_pixbuf_animation_iter_get_delay_time (priv->animation_iter);
1249 priv->animation_timeout =
1250 gdk_threads_add_timeout (delay, animation_timeout, image);
1253 /* don't advance the anim iter here, or we could get frame changes between two
1254 * exposes of different areas.
1256 return g_object_ref (gdk_pixbuf_animation_iter_get_pixbuf (priv->animation_iter));
1260 gtk_image_get_preferred_size (GtkImage *image,
1264 GtkImagePrivate *priv = image->priv;
1265 gint xpad, ypad, width, height;
1266 GtkStyleContext *context;
1268 context = gtk_widget_get_style_context (GTK_WIDGET (image));
1269 gtk_misc_get_padding (GTK_MISC (image), &xpad, &ypad);
1270 _gtk_icon_helper_get_size (priv->icon_helper, context, &width, &height);
1278 *height_out = height;
1282 gtk_image_draw (GtkWidget *widget,
1286 GtkImagePrivate *priv;
1288 GtkStyleContext *context;
1289 gint x, y, width, height;
1291 gfloat xalign, yalign;
1293 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1295 image = GTK_IMAGE (widget);
1296 misc = GTK_MISC (image);
1299 context = gtk_widget_get_style_context (widget);
1301 gtk_misc_get_alignment (misc, &xalign, &yalign);
1302 gtk_misc_get_padding (misc, &xpad, &ypad);
1303 gtk_image_get_preferred_size (image, &width, &height);
1305 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
1306 xalign = 1.0 - xalign;
1308 x = floor (xpad + ((gtk_widget_get_allocated_width (widget) - width) * xalign));
1309 y = floor (ypad + ((gtk_widget_get_allocated_height (widget) - height) * yalign));
1311 if (gtk_image_get_storage_type (image) == GTK_IMAGE_ANIMATION)
1313 GdkPixbuf *pixbuf = get_animation_frame (image);
1314 gtk_render_icon (context, cr,
1317 g_object_unref (pixbuf);
1321 _gtk_icon_helper_draw (priv->icon_helper,
1330 gtk_image_reset (GtkImage *image)
1332 GtkImagePrivate *priv = image->priv;
1333 GtkImageType storage_type;
1335 g_object_freeze_notify (G_OBJECT (image));
1336 storage_type = gtk_image_get_storage_type (image);
1338 if (storage_type != GTK_IMAGE_EMPTY)
1339 g_object_notify (G_OBJECT (image), "storage-type");
1341 g_object_notify (G_OBJECT (image), "icon-size");
1343 switch (storage_type)
1345 case GTK_IMAGE_PIXBUF:
1346 g_object_notify (G_OBJECT (image), "pixbuf");
1348 case GTK_IMAGE_STOCK:
1349 g_object_notify (G_OBJECT (image), "stock");
1351 case GTK_IMAGE_ICON_SET:
1352 g_object_notify (G_OBJECT (image), "icon-set");
1354 case GTK_IMAGE_ANIMATION:
1355 gtk_image_reset_anim_iter (image);
1356 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1358 case GTK_IMAGE_ICON_NAME:
1359 g_object_notify (G_OBJECT (image), "icon-name");
1361 case GTK_IMAGE_GICON:
1362 g_object_notify (G_OBJECT (image), "gicon");
1364 case GTK_IMAGE_EMPTY:
1371 g_free (priv->filename);
1372 priv->filename = NULL;
1373 g_object_notify (G_OBJECT (image), "file");
1376 _gtk_icon_helper_clear (priv->icon_helper);
1378 g_object_thaw_notify (G_OBJECT (image));
1383 * @image: a #GtkImage
1385 * Resets the image to be empty.
1390 gtk_image_clear (GtkImage *image)
1392 gtk_image_reset (image);
1394 if (gtk_widget_get_visible (GTK_WIDGET (image)))
1395 gtk_widget_queue_resize (GTK_WIDGET (image));
1399 gtk_image_get_preferred_width (GtkWidget *widget,
1405 gtk_image_get_preferred_size (GTK_IMAGE (widget), &width, NULL);
1406 *minimum = *natural = width;
1410 gtk_image_get_preferred_height (GtkWidget *widget,
1416 gtk_image_get_preferred_size (GTK_IMAGE (widget), NULL, &height);
1417 *minimum = *natural = height;
1421 icon_theme_changed (GtkImage *image)
1423 GtkImagePrivate *priv = image->priv;
1425 _gtk_icon_helper_invalidate (priv->icon_helper);
1426 gtk_widget_queue_draw (GTK_WIDGET (image));
1430 gtk_image_style_updated (GtkWidget *widget)
1432 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_updated (widget);
1434 icon_theme_changed (GTK_IMAGE (widget));
1438 gtk_image_screen_changed (GtkWidget *widget,
1439 GdkScreen *prev_screen)
1443 image = GTK_IMAGE (widget);
1445 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
1446 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
1448 icon_theme_changed (image);
1452 _gtk_image_gicon_data_clear (GtkImageGIconData *data)
1456 g_object_unref (data->pixbuf);
1457 data->pixbuf = NULL;
1461 g_object_unref (data->icon);
1467 * gtk_image_set_pixel_size:
1468 * @image: a #GtkImage
1469 * @pixel_size: the new pixel size
1471 * Sets the pixel size to use for named icons. If the pixel size is set
1472 * to a value != -1, it is used instead of the icon size set by
1473 * gtk_image_set_from_icon_name().
1478 gtk_image_set_pixel_size (GtkImage *image,
1481 GtkImagePrivate *priv;
1482 gint old_pixel_size;
1484 g_return_if_fail (GTK_IS_IMAGE (image));
1487 old_pixel_size = gtk_image_get_pixel_size (image);
1489 if (pixel_size != old_pixel_size)
1491 _gtk_icon_helper_set_pixel_size (priv->icon_helper, pixel_size);
1493 if (gtk_widget_get_visible (GTK_WIDGET (image)))
1494 gtk_widget_queue_resize (GTK_WIDGET (image));
1496 g_object_notify (G_OBJECT (image), "pixel-size");
1501 * gtk_image_get_pixel_size:
1502 * @image: a #GtkImage
1504 * Gets the pixel size used for named icons.
1506 * Returns: the pixel size used for named icons.
1511 gtk_image_get_pixel_size (GtkImage *image)
1513 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
1515 return _gtk_icon_helper_get_pixel_size (image->priv->icon_helper);