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 "gtkimageprivate.h"
34 #include "gtkiconfactory.h"
36 #include "gtkicontheme.h"
37 #include "gtksizerequest.h"
39 #include "gtkprivate.h"
40 #include "gtktypebuiltins.h"
44 * @Short_description: A widget displaying an image
46 * @See_also:#GdkPixbuf
48 * The #GtkImage widget displays an image. Various kinds of object
49 * can be displayed as an image; most typically, you would load a
50 * #GdkPixbuf ("pixel buffer") from a file, and then display that.
51 * There's a convenience function to do this, gtk_image_new_from_file(),
53 * <informalexample><programlisting>
55 * image = gtk_image_new_from_file ("myfile.png");
56 * </programlisting></informalexample>
57 * If the file isn't loaded successfully, the image will contain a
58 * "broken image" icon similar to that used in many web browsers.
59 * If you want to handle errors in loading the file yourself,
60 * for example by displaying an error message, then load the image with
61 * gdk_pixbuf_new_from_file(), then create the #GtkImage with
62 * gtk_image_new_from_pixbuf().
64 * The image file may contain an animation, if so the #GtkImage will
65 * display an animation (#GdkPixbufAnimation) instead of a static image.
67 * #GtkImage is a subclass of #GtkMisc, which implies that you can
68 * align it (center, left, right) and add padding to it, using
71 * #GtkImage is a "no window" widget (has no #GdkWindow of its own),
72 * so by default does not receive events. If you want to receive events
73 * on the image, such as button clicks, place the image inside a
74 * #GtkEventBox, then connect to the event signals on the event box.
76 * <title>Handling button press events on a
77 * <structname>GtkImage</structname>.</title>
80 * button_press_callback (GtkWidget *event_box,
81 * GdkEventButton *event,
84 * g_print ("Event box clicked at coordinates %f,%f\n",
85 * event->x, event->y);
87 * /<!---->* Returning TRUE means we handled the event, so the signal
88 * * emission should be stopped (don't call any further
89 * * callbacks that may be connected). Return FALSE
90 * * to continue invoking callbacks.
99 * GtkWidget *event_box;
101 * image = gtk_image_new_from_file ("myfile.png");
103 * event_box = gtk_event_box_new (<!-- -->);
105 * gtk_container_add (GTK_CONTAINER (event_box), image);
107 * g_signal_connect (G_OBJECT (event_box),
108 * "button_press_event",
109 * G_CALLBACK (button_press_callback),
117 * When handling events on the event box, keep in mind that coordinates
118 * in the image may be different from event box coordinates due to
119 * the alignment and padding settings on the image (see #GtkMisc).
120 * The simplest way to solve this is to set the alignment to 0.0
121 * (left/top), and set the padding to zero. Then the origin of
122 * the image will be the same as the origin of the event box.
124 * Sometimes an application will want to avoid depending on external data
125 * files, such as image files. GTK+ comes with a program to avoid this,
126 * called <application>gdk-pixbuf-csource</application>. This program
127 * allows you to convert an image into a C variable declaration, which
128 * can then be loaded into a #GdkPixbuf using
129 * gdk_pixbuf_new_from_inline().
133 struct _GtkImagePrivate
135 GtkIconSize icon_size; /* Only used with GTK_IMAGE_STOCK, GTK_IMAGE_ICON_SET, GTK_IMAGE_ICON_NAME */
136 GtkImageType storage_type;
140 GtkImagePixbufData pixbuf;
141 GtkImageStockData stock;
142 GtkImageIconSetData icon_set;
143 GtkImageAnimationData anim;
144 GtkImageIconNameData name;
145 GtkImageGIconData gicon;
148 gchar *filename; /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
149 gint last_rendered_state; /* a GtkStateFlags, with -1 meaning an invalid state,
150 * only used with GTK_IMAGE_GICON, GTK_IMAGE_ICON_NAME */
153 gint required_height;
154 guint need_calc_size : 1;
155 guint use_fallback : 1;
159 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
160 static gint gtk_image_draw (GtkWidget *widget,
162 static void gtk_image_unmap (GtkWidget *widget);
163 static void gtk_image_unrealize (GtkWidget *widget);
164 static void gtk_image_get_preferred_width (GtkWidget *widget,
167 static void gtk_image_get_preferred_height (GtkWidget *widget,
171 static void gtk_image_style_updated (GtkWidget *widget);
172 static void gtk_image_screen_changed (GtkWidget *widget,
173 GdkScreen *prev_screen);
174 static void gtk_image_destroy (GtkWidget *widget);
175 static void gtk_image_reset (GtkImage *image);
176 static void gtk_image_calc_size (GtkImage *image);
178 static void gtk_image_update_size (GtkImage *image,
182 static void gtk_image_set_property (GObject *object,
186 static void gtk_image_get_property (GObject *object,
191 static void icon_theme_changed (GtkImage *image);
202 PROP_PIXBUF_ANIMATION,
209 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
212 gtk_image_class_init (GtkImageClass *class)
214 GObjectClass *gobject_class;
215 GtkWidgetClass *widget_class;
217 gobject_class = G_OBJECT_CLASS (class);
219 gobject_class->set_property = gtk_image_set_property;
220 gobject_class->get_property = gtk_image_get_property;
222 widget_class = GTK_WIDGET_CLASS (class);
223 widget_class->draw = gtk_image_draw;
224 widget_class->destroy = gtk_image_destroy;
225 widget_class->get_preferred_width = gtk_image_get_preferred_width;
226 widget_class->get_preferred_height = gtk_image_get_preferred_height;
227 widget_class->unmap = gtk_image_unmap;
228 widget_class->unrealize = gtk_image_unrealize;
229 widget_class->style_updated = gtk_image_style_updated;
230 widget_class->screen_changed = gtk_image_screen_changed;
232 g_object_class_install_property (gobject_class,
234 g_param_spec_object ("pixbuf",
236 P_("A GdkPixbuf to display"),
238 GTK_PARAM_READWRITE));
240 g_object_class_install_property (gobject_class,
242 g_param_spec_string ("file",
244 P_("Filename to load and display"),
246 GTK_PARAM_READWRITE));
249 g_object_class_install_property (gobject_class,
251 g_param_spec_string ("stock",
253 P_("Stock ID for a stock image to display"),
255 GTK_PARAM_READWRITE));
257 g_object_class_install_property (gobject_class,
259 g_param_spec_boxed ("icon-set",
261 P_("Icon set to display"),
263 GTK_PARAM_READWRITE));
265 g_object_class_install_property (gobject_class,
267 g_param_spec_int ("icon-size",
269 P_("Symbolic size to use for stock icon, icon set or named icon"),
272 GTK_PARAM_READWRITE));
274 * GtkImage:pixel-size:
276 * The "pixel-size" property can be used to specify a fixed size
277 * overriding the #GtkImage:icon-size property for images of type
278 * %GTK_IMAGE_ICON_NAME.
282 g_object_class_install_property (gobject_class,
284 g_param_spec_int ("pixel-size",
286 P_("Pixel size to use for named icon"),
289 GTK_PARAM_READWRITE));
291 g_object_class_install_property (gobject_class,
292 PROP_PIXBUF_ANIMATION,
293 g_param_spec_object ("pixbuf-animation",
295 P_("GdkPixbufAnimation to display"),
296 GDK_TYPE_PIXBUF_ANIMATION,
297 GTK_PARAM_READWRITE));
300 * GtkImage:icon-name:
302 * The name of the icon in the icon theme. If the icon theme is
303 * changed, the image will be updated automatically.
307 g_object_class_install_property (gobject_class,
309 g_param_spec_string ("icon-name",
311 P_("The name of the icon from the icon theme"),
313 GTK_PARAM_READWRITE));
318 * The GIcon displayed in the GtkImage. For themed icons,
319 * If the icon theme is changed, the image will be updated
324 g_object_class_install_property (gobject_class,
326 g_param_spec_object ("gicon",
328 P_("The GIcon being displayed"),
330 GTK_PARAM_READWRITE));
332 g_object_class_install_property (gobject_class,
334 g_param_spec_enum ("storage-type",
336 P_("The representation being used for image data"),
339 GTK_PARAM_READABLE));
342 * GtkImage:use-fallback:
344 * Whether the icon displayed in the GtkImage will use
345 * standard icon names fallback. The value of this property
346 * is only relevant for images of type %GTK_IMAGE_ICON_NAME
347 * and %GTK_IMAGE_GICON.
351 g_object_class_install_property (gobject_class,
353 g_param_spec_boolean ("use-fallback",
355 P_("Whether to use icon names fallback"),
357 GTK_PARAM_READWRITE));
359 g_type_class_add_private (class, sizeof (GtkImagePrivate));
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);
374 priv->storage_type = GTK_IMAGE_EMPTY;
375 priv->icon_size = DEFAULT_ICON_SIZE;
377 priv->pixel_size = -1;
379 priv->filename = NULL;
383 gtk_image_destroy (GtkWidget *widget)
385 GtkImage *image = GTK_IMAGE (widget);
387 gtk_image_reset (image);
389 GTK_WIDGET_CLASS (gtk_image_parent_class)->destroy (widget);
393 gtk_image_set_property (GObject *object,
398 GtkImage *image = GTK_IMAGE (object);
399 GtkImagePrivate *priv = image->priv;
404 gtk_image_set_from_pixbuf (image,
405 g_value_get_object (value));
408 gtk_image_set_from_file (image, g_value_get_string (value));
411 gtk_image_set_from_stock (image, g_value_get_string (value),
415 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
419 if (priv->storage_type == GTK_IMAGE_STOCK)
420 gtk_image_set_from_stock (image,
421 priv->data.stock.stock_id,
422 g_value_get_int (value));
423 else if (priv->storage_type == GTK_IMAGE_ICON_SET)
424 gtk_image_set_from_icon_set (image,
425 priv->data.icon_set.icon_set,
426 g_value_get_int (value));
427 else if (priv->storage_type == GTK_IMAGE_ICON_NAME)
428 gtk_image_set_from_icon_name (image,
429 priv->data.name.icon_name,
430 g_value_get_int (value));
431 else if (priv->storage_type == GTK_IMAGE_GICON)
432 gtk_image_set_from_gicon (image,
433 priv->data.gicon.icon,
434 g_value_get_int (value));
436 /* Save to be used when STOCK, ICON_SET, ICON_NAME or GICON property comes in */
437 priv->icon_size = g_value_get_int (value);
439 case PROP_PIXEL_SIZE:
440 gtk_image_set_pixel_size (image, g_value_get_int (value));
442 case PROP_PIXBUF_ANIMATION:
443 gtk_image_set_from_animation (image,
444 g_value_get_object (value));
447 gtk_image_set_from_icon_name (image, g_value_get_string (value),
451 gtk_image_set_from_gicon (image, g_value_get_object (value),
455 case PROP_USE_FALLBACK:
456 priv->use_fallback = g_value_get_boolean (value);
457 if (priv->storage_type == GTK_IMAGE_ICON_NAME)
458 gtk_image_set_from_icon_name (image,
459 priv->data.name.icon_name,
461 else if (priv->storage_type == GTK_IMAGE_GICON)
462 gtk_image_set_from_gicon (image,
463 priv->data.gicon.icon,
468 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
474 gtk_image_get_property (GObject *object,
479 GtkImage *image = GTK_IMAGE (object);
480 GtkImagePrivate *priv = image->priv;
482 /* The "getter" functions whine if you try to get the wrong
483 * storage type. This function is instead robust against that,
484 * so that GUI builders don't have to jump through hoops
491 if (priv->storage_type != GTK_IMAGE_PIXBUF)
492 g_value_set_object (value, NULL);
494 g_value_set_object (value,
495 gtk_image_get_pixbuf (image));
498 g_value_set_string (value, priv->filename);
501 if (priv->storage_type != GTK_IMAGE_STOCK)
502 g_value_set_string (value, NULL);
504 g_value_set_string (value,
505 priv->data.stock.stock_id);
508 if (priv->storage_type != GTK_IMAGE_ICON_SET)
509 g_value_set_boxed (value, NULL);
511 g_value_set_boxed (value,
512 priv->data.icon_set.icon_set);
515 g_value_set_int (value, priv->icon_size);
517 case PROP_PIXEL_SIZE:
518 g_value_set_int (value, priv->pixel_size);
520 case PROP_PIXBUF_ANIMATION:
521 if (priv->storage_type != GTK_IMAGE_ANIMATION)
522 g_value_set_object (value, NULL);
524 g_value_set_object (value,
525 priv->data.anim.anim);
528 if (priv->storage_type != GTK_IMAGE_ICON_NAME)
529 g_value_set_string (value, NULL);
531 g_value_set_string (value,
532 priv->data.name.icon_name);
535 if (priv->storage_type != GTK_IMAGE_GICON)
536 g_value_set_object (value, NULL);
538 g_value_set_object (value,
539 priv->data.gicon.icon);
541 case PROP_STORAGE_TYPE:
542 g_value_set_enum (value, priv->storage_type);
545 case PROP_USE_FALLBACK:
546 g_value_set_boolean (value, priv->use_fallback);
550 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
557 * gtk_image_new_from_file:
558 * @filename: (type filename): a filename
560 * Creates a new #GtkImage displaying the file @filename. If the file
561 * isn't found or can't be loaded, the resulting #GtkImage will
562 * display a "broken image" icon. This function never returns %NULL,
563 * it always returns a valid #GtkImage widget.
565 * If the file contains an animation, the image will contain an
568 * If you need to detect failures to load the file, use
569 * gdk_pixbuf_new_from_file() to load the file yourself, then create
570 * the #GtkImage from the pixbuf. (Or for animations, use
571 * gdk_pixbuf_animation_new_from_file()).
573 * The storage type (gtk_image_get_storage_type()) of the returned
574 * image is not defined, it will be whatever is appropriate for
575 * displaying the file.
577 * Return value: a new #GtkImage
580 gtk_image_new_from_file (const gchar *filename)
584 image = g_object_new (GTK_TYPE_IMAGE, NULL);
586 gtk_image_set_from_file (image, filename);
588 return GTK_WIDGET (image);
592 * gtk_image_new_from_pixbuf:
593 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
595 * Creates a new #GtkImage displaying @pixbuf.
596 * The #GtkImage does not assume a reference to the
597 * pixbuf; you still need to unref it if you own references.
598 * #GtkImage will add its own reference rather than adopting yours.
600 * Note that this function just creates an #GtkImage from the pixbuf. The
601 * #GtkImage created will not react to state changes. Should you want that,
602 * you should use gtk_image_new_from_icon_set().
604 * Return value: a new #GtkImage
607 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
611 image = g_object_new (GTK_TYPE_IMAGE, NULL);
613 gtk_image_set_from_pixbuf (image, pixbuf);
615 return GTK_WIDGET (image);
619 * gtk_image_new_from_stock:
620 * @stock_id: a stock icon name
621 * @size: (type int): a stock icon size
623 * Creates a #GtkImage displaying a stock icon. Sample stock icon
624 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
625 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
626 * icon name isn't known, the image will be empty.
627 * You can register your own stock icon names, see
628 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
630 * Return value: a new #GtkImage displaying the stock icon
633 gtk_image_new_from_stock (const gchar *stock_id,
638 image = g_object_new (GTK_TYPE_IMAGE, NULL);
640 gtk_image_set_from_stock (image, stock_id, size);
642 return GTK_WIDGET (image);
646 * gtk_image_new_from_icon_set:
647 * @icon_set: a #GtkIconSet
648 * @size: (type int): a stock icon size
650 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
651 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
652 * this function, usually it's better to create a #GtkIconFactory, put
653 * your icon sets in the icon factory, add the icon factory to the
654 * list of default factories with gtk_icon_factory_add_default(), and
655 * then use gtk_image_new_from_stock(). This will allow themes to
656 * override the icon you ship with your application.
658 * The #GtkImage does not assume a reference to the
659 * icon set; you still need to unref it if you own references.
660 * #GtkImage will add its own reference rather than adopting yours.
662 * Return value: a new #GtkImage
665 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
670 image = g_object_new (GTK_TYPE_IMAGE, NULL);
672 gtk_image_set_from_icon_set (image, icon_set, size);
674 return GTK_WIDGET (image);
678 * gtk_image_new_from_animation:
679 * @animation: an animation
681 * Creates a #GtkImage displaying the given animation.
682 * The #GtkImage does not assume a reference to the
683 * animation; you still need to unref it if you own references.
684 * #GtkImage will add its own reference rather than adopting yours.
686 * Note that the animation frames are shown using a timeout with
687 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
688 * keep in mind that the animation will only be shown if the main loop
689 * is not busy with something that has a higher priority.
691 * Return value: a new #GtkImage widget
694 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
698 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
700 image = g_object_new (GTK_TYPE_IMAGE, NULL);
702 gtk_image_set_from_animation (image, animation);
704 return GTK_WIDGET (image);
708 * gtk_image_new_from_icon_name:
709 * @icon_name: an icon name
710 * @size: (type int): a stock icon size
712 * Creates a #GtkImage displaying an icon from the current icon theme.
713 * If the icon name isn't known, a "broken image" icon will be
714 * displayed instead. If the current icon theme is changed, the icon
715 * will be updated appropriately.
717 * Return value: a new #GtkImage displaying the themed icon
722 gtk_image_new_from_icon_name (const gchar *icon_name,
727 image = g_object_new (GTK_TYPE_IMAGE, NULL);
729 gtk_image_set_from_icon_name (image, icon_name, size);
731 return GTK_WIDGET (image);
735 * gtk_image_new_from_gicon:
737 * @size: (type int): a stock icon size
739 * Creates a #GtkImage displaying an icon from the current icon theme.
740 * If the icon name isn't known, a "broken image" icon will be
741 * displayed instead. If the current icon theme is changed, the icon
742 * will be updated appropriately.
744 * Return value: a new #GtkImage displaying the themed icon
749 gtk_image_new_from_gicon (GIcon *icon,
754 image = g_object_new (GTK_TYPE_IMAGE, NULL);
756 gtk_image_set_from_gicon (image, icon, size);
758 return GTK_WIDGET (image);
762 * gtk_image_set_from_file:
763 * @image: a #GtkImage
764 * @filename: (type filename) (allow-none): a filename or %NULL
766 * See gtk_image_new_from_file() for details.
769 gtk_image_set_from_file (GtkImage *image,
770 const gchar *filename)
772 GtkImagePrivate *priv;
773 GdkPixbufAnimation *anim;
775 g_return_if_fail (GTK_IS_IMAGE (image));
779 g_object_freeze_notify (G_OBJECT (image));
781 gtk_image_clear (image);
783 if (filename == NULL)
785 priv->filename = NULL;
786 g_object_thaw_notify (G_OBJECT (image));
790 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
794 gtk_image_set_from_stock (image,
795 GTK_STOCK_MISSING_IMAGE,
796 GTK_ICON_SIZE_BUTTON);
797 g_object_thaw_notify (G_OBJECT (image));
801 /* We could just unconditionally set_from_animation,
802 * but it's nicer for memory if we toss the animation
803 * if it's just a single pixbuf
806 if (gdk_pixbuf_animation_is_static_image (anim))
807 gtk_image_set_from_pixbuf (image,
808 gdk_pixbuf_animation_get_static_image (anim));
810 gtk_image_set_from_animation (image, anim);
812 g_object_unref (anim);
814 priv->filename = g_strdup (filename);
816 g_object_thaw_notify (G_OBJECT (image));
820 * gtk_image_set_from_pixbuf:
821 * @image: a #GtkImage
822 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
824 * See gtk_image_new_from_pixbuf() for details.
827 gtk_image_set_from_pixbuf (GtkImage *image,
830 GtkImagePrivate *priv;
832 g_return_if_fail (GTK_IS_IMAGE (image));
833 g_return_if_fail (pixbuf == NULL ||
834 GDK_IS_PIXBUF (pixbuf));
838 g_object_freeze_notify (G_OBJECT (image));
841 g_object_ref (pixbuf);
843 gtk_image_clear (image);
847 priv->storage_type = GTK_IMAGE_PIXBUF;
849 priv->data.pixbuf.pixbuf = pixbuf;
851 gtk_image_update_size (image,
852 gdk_pixbuf_get_width (pixbuf),
853 gdk_pixbuf_get_height (pixbuf));
856 g_object_notify (G_OBJECT (image), "pixbuf");
858 g_object_thaw_notify (G_OBJECT (image));
862 * gtk_image_set_from_stock:
863 * @image: a #GtkImage
864 * @stock_id: a stock icon name
865 * @size: (type int): a stock icon size
867 * See gtk_image_new_from_stock() for details.
870 gtk_image_set_from_stock (GtkImage *image,
871 const gchar *stock_id,
874 GtkImagePrivate *priv;
877 g_return_if_fail (GTK_IS_IMAGE (image));
881 g_object_freeze_notify (G_OBJECT (image));
883 /* in case stock_id == priv->data.stock.stock_id */
884 new_id = g_strdup (stock_id);
886 gtk_image_clear (image);
890 priv->storage_type = GTK_IMAGE_STOCK;
892 priv->data.stock.stock_id = new_id;
893 priv->icon_size = size;
895 /* Size is demand-computed in size request method
896 * if we're a stock image, since changing the
897 * style impacts the size request
901 g_object_notify (G_OBJECT (image), "stock");
902 g_object_notify (G_OBJECT (image), "icon-size");
904 g_object_thaw_notify (G_OBJECT (image));
908 * gtk_image_set_from_icon_set:
909 * @image: a #GtkImage
910 * @icon_set: a #GtkIconSet
911 * @size: (type int): a stock icon size
913 * See gtk_image_new_from_icon_set() for details.
916 gtk_image_set_from_icon_set (GtkImage *image,
917 GtkIconSet *icon_set,
920 GtkImagePrivate *priv;
922 g_return_if_fail (GTK_IS_IMAGE (image));
926 g_object_freeze_notify (G_OBJECT (image));
929 gtk_icon_set_ref (icon_set);
931 gtk_image_clear (image);
935 priv->storage_type = GTK_IMAGE_ICON_SET;
937 priv->data.icon_set.icon_set = icon_set;
938 priv->icon_size = size;
940 /* Size is demand-computed in size request method
941 * if we're an icon set
945 g_object_notify (G_OBJECT (image), "icon-set");
946 g_object_notify (G_OBJECT (image), "icon-size");
948 g_object_thaw_notify (G_OBJECT (image));
952 * gtk_image_set_from_animation:
953 * @image: a #GtkImage
954 * @animation: the #GdkPixbufAnimation
956 * Causes the #GtkImage to display the given animation (or display
957 * nothing, if you set the animation to %NULL).
960 gtk_image_set_from_animation (GtkImage *image,
961 GdkPixbufAnimation *animation)
963 GtkImagePrivate *priv;
965 g_return_if_fail (GTK_IS_IMAGE (image));
966 g_return_if_fail (animation == NULL ||
967 GDK_IS_PIXBUF_ANIMATION (animation));
971 g_object_freeze_notify (G_OBJECT (image));
974 g_object_ref (animation);
976 gtk_image_clear (image);
978 if (animation != NULL)
980 priv->storage_type = GTK_IMAGE_ANIMATION;
982 priv->data.anim.anim = animation;
983 priv->data.anim.frame_timeout = 0;
984 priv->data.anim.iter = NULL;
986 gtk_image_update_size (image,
987 gdk_pixbuf_animation_get_width (animation),
988 gdk_pixbuf_animation_get_height (animation));
991 g_object_notify (G_OBJECT (image), "pixbuf-animation");
993 g_object_thaw_notify (G_OBJECT (image));
997 * gtk_image_set_from_icon_name:
998 * @image: a #GtkImage
999 * @icon_name: an icon name
1000 * @size: (type int): an icon size
1002 * See gtk_image_new_from_icon_name() for details.
1007 gtk_image_set_from_icon_name (GtkImage *image,
1008 const gchar *icon_name,
1012 GtkImagePrivate *priv;
1014 g_return_if_fail (GTK_IS_IMAGE (image));
1018 g_object_freeze_notify (G_OBJECT (image));
1020 /* in case icon_name == priv->data.name.icon_name */
1021 new_name = g_strdup (icon_name);
1023 gtk_image_clear (image);
1027 priv->storage_type = GTK_IMAGE_ICON_NAME;
1029 priv->data.name.icon_name = new_name;
1030 priv->icon_size = size;
1032 /* Size is demand-computed in size request method
1033 * if we're a icon theme image, since changing the
1034 * style impacts the size request
1038 g_object_notify (G_OBJECT (image), "icon-name");
1039 g_object_notify (G_OBJECT (image), "icon-size");
1041 g_object_thaw_notify (G_OBJECT (image));
1045 * gtk_image_set_from_gicon:
1046 * @image: a #GtkImage
1048 * @size: (type int): an icon size
1050 * See gtk_image_new_from_gicon() for details.
1055 gtk_image_set_from_gicon (GtkImage *image,
1059 GtkImagePrivate *priv;
1061 g_return_if_fail (GTK_IS_IMAGE (image));
1065 g_object_freeze_notify (G_OBJECT (image));
1067 /* in case icon == priv->data.gicon.icon */
1069 g_object_ref (icon);
1071 gtk_image_clear (image);
1075 priv->storage_type = GTK_IMAGE_GICON;
1077 priv->data.gicon.icon = icon;
1078 priv->icon_size = size;
1080 /* Size is demand-computed in size request method
1081 * if we're a icon theme image, since changing the
1082 * style impacts the size request
1086 g_object_notify (G_OBJECT (image), "gicon");
1087 g_object_notify (G_OBJECT (image), "icon-size");
1089 g_object_thaw_notify (G_OBJECT (image));
1093 * gtk_image_get_storage_type:
1094 * @image: a #GtkImage
1096 * Gets the type of representation being used by the #GtkImage
1097 * to store image data. If the #GtkImage has no image data,
1098 * the return value will be %GTK_IMAGE_EMPTY.
1100 * Return value: image representation being used
1103 gtk_image_get_storage_type (GtkImage *image)
1105 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1107 return image->priv->storage_type;
1111 * gtk_image_get_pixbuf:
1112 * @image: a #GtkImage
1114 * Gets the #GdkPixbuf being displayed by the #GtkImage.
1115 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1116 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1117 * The caller of this function does not own a reference to the
1120 * Return value: (transfer none): the displayed pixbuf, or %NULL if
1121 * the image is empty
1124 gtk_image_get_pixbuf (GtkImage *image)
1126 GtkImagePrivate *priv;
1128 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1132 g_return_val_if_fail (priv->storage_type == GTK_IMAGE_PIXBUF ||
1133 priv->storage_type == GTK_IMAGE_EMPTY, NULL);
1135 if (priv->storage_type == GTK_IMAGE_EMPTY)
1136 priv->data.pixbuf.pixbuf = NULL;
1138 return priv->data.pixbuf.pixbuf;
1142 * gtk_image_get_stock:
1143 * @image: a #GtkImage
1144 * @stock_id: (out) (transfer none) (allow-none): place to store a
1145 * stock icon name, or %NULL
1146 * @size: (out) (allow-none) (type int): place to store a stock icon
1149 * Gets the stock icon name and size being displayed by the #GtkImage.
1150 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1151 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1152 * The returned string is owned by the #GtkImage and should not
1156 gtk_image_get_stock (GtkImage *image,
1160 GtkImagePrivate *priv;
1162 g_return_if_fail (GTK_IS_IMAGE (image));
1166 g_return_if_fail (priv->storage_type == GTK_IMAGE_STOCK ||
1167 priv->storage_type == GTK_IMAGE_EMPTY);
1169 if (priv->storage_type == GTK_IMAGE_EMPTY)
1170 priv->data.stock.stock_id = NULL;
1173 *stock_id = priv->data.stock.stock_id;
1176 *size = priv->icon_size;
1180 * gtk_image_get_icon_set:
1181 * @image: a #GtkImage
1182 * @icon_set: (out) (transfer none) (allow-none): location to store a
1183 * #GtkIconSet, or %NULL
1184 * @size: (out) (allow-none) (type int): location to store a stock
1185 * icon size, or %NULL
1187 * Gets the icon set and size being displayed by the #GtkImage.
1188 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1189 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1192 gtk_image_get_icon_set (GtkImage *image,
1193 GtkIconSet **icon_set,
1196 GtkImagePrivate *priv;
1198 g_return_if_fail (GTK_IS_IMAGE (image));
1202 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_SET ||
1203 priv->storage_type == GTK_IMAGE_EMPTY);
1206 *icon_set = priv->data.icon_set.icon_set;
1209 *size = priv->icon_size;
1213 * gtk_image_get_animation:
1214 * @image: a #GtkImage
1216 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1217 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1218 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1219 * The caller of this function does not own a reference to the
1220 * returned animation.
1222 * Return value: (transfer none): the displayed animation, or %NULL if
1223 * the image is empty
1226 gtk_image_get_animation (GtkImage *image)
1228 GtkImagePrivate *priv;
1230 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1234 g_return_val_if_fail (priv->storage_type == GTK_IMAGE_ANIMATION ||
1235 priv->storage_type == GTK_IMAGE_EMPTY,
1238 if (priv->storage_type == GTK_IMAGE_EMPTY)
1239 priv->data.anim.anim = NULL;
1241 return priv->data.anim.anim;
1245 * gtk_image_get_icon_name:
1246 * @image: a #GtkImage
1247 * @icon_name: (out) (transfer none) (allow-none): place to store an
1248 * icon name, or %NULL
1249 * @size: (out) (allow-none) (type int): place to store an icon size,
1252 * Gets the icon name and size being displayed by the #GtkImage.
1253 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1254 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1255 * The returned string is owned by the #GtkImage and should not
1261 gtk_image_get_icon_name (GtkImage *image,
1262 const gchar **icon_name,
1265 GtkImagePrivate *priv;
1267 g_return_if_fail (GTK_IS_IMAGE (image));
1271 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME ||
1272 priv->storage_type == GTK_IMAGE_EMPTY);
1274 if (priv->storage_type == GTK_IMAGE_EMPTY)
1275 priv->data.name.icon_name = NULL;
1278 *icon_name = priv->data.name.icon_name;
1281 *size = priv->icon_size;
1285 * gtk_image_get_gicon:
1286 * @image: a #GtkImage
1287 * @gicon: (out) (transfer none) (allow-none): place to store a
1289 * @size: (out) (allow-none) (type int): place to store an icon size,
1292 * Gets the #GIcon and size being displayed by the #GtkImage.
1293 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1294 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1295 * The caller of this function does not own a reference to the
1301 gtk_image_get_gicon (GtkImage *image,
1305 GtkImagePrivate *priv;
1307 g_return_if_fail (GTK_IS_IMAGE (image));
1311 g_return_if_fail (priv->storage_type == GTK_IMAGE_GICON ||
1312 priv->storage_type == GTK_IMAGE_EMPTY);
1314 if (priv->storage_type == GTK_IMAGE_EMPTY)
1315 priv->data.gicon.icon = NULL;
1318 *gicon = priv->data.gicon.icon;
1321 *size = priv->icon_size;
1327 * Creates a new empty #GtkImage widget.
1329 * Return value: a newly created #GtkImage widget.
1332 gtk_image_new (void)
1334 return g_object_new (GTK_TYPE_IMAGE, NULL);
1338 gtk_image_reset_anim_iter (GtkImage *image)
1340 GtkImagePrivate *priv = image->priv;
1342 if (priv->storage_type == GTK_IMAGE_ANIMATION)
1344 /* Reset the animation */
1346 if (priv->data.anim.frame_timeout)
1348 g_source_remove (priv->data.anim.frame_timeout);
1349 priv->data.anim.frame_timeout = 0;
1352 if (priv->data.anim.iter)
1354 g_object_unref (priv->data.anim.iter);
1355 priv->data.anim.iter = NULL;
1361 gtk_image_unmap (GtkWidget *widget)
1363 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1365 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1369 gtk_image_unrealize (GtkWidget *widget)
1371 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1373 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1377 animation_timeout (gpointer data)
1379 GtkImage *image = GTK_IMAGE (data);
1380 GtkImagePrivate *priv = image->priv;
1383 priv->data.anim.frame_timeout = 0;
1385 gdk_pixbuf_animation_iter_advance (priv->data.anim.iter, NULL);
1387 delay = gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter);
1390 GtkWidget *widget = GTK_WIDGET (image);
1392 priv->data.anim.frame_timeout =
1393 gdk_threads_add_timeout (delay, animation_timeout, image);
1395 gtk_widget_queue_draw (widget);
1397 if (gtk_widget_is_drawable (widget))
1398 gdk_window_process_updates (gtk_widget_get_window (widget), TRUE);
1405 icon_theme_changed (GtkImage *image)
1407 GtkImagePrivate *priv = image->priv;
1409 if (priv->storage_type == GTK_IMAGE_ICON_NAME)
1411 if (priv->data.name.pixbuf)
1412 g_object_unref (priv->data.name.pixbuf);
1413 priv->data.name.pixbuf = NULL;
1415 gtk_widget_queue_draw (GTK_WIDGET (image));
1417 if (priv->storage_type == GTK_IMAGE_GICON)
1419 if (priv->data.gicon.pixbuf)
1420 g_object_unref (priv->data.gicon.pixbuf);
1421 priv->data.gicon.pixbuf = NULL;
1423 gtk_widget_queue_draw (GTK_WIDGET (image));
1428 ensure_icon_size (GtkImage *image,
1429 GtkIconLookupFlags *flags,
1433 GtkImagePrivate *priv = image->priv;
1435 GtkSettings *settings;
1437 GtkIconTheme *icon_theme;
1438 gint *s, *sizes, dist;
1440 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1441 settings = gtk_settings_get_for_screen (screen);
1442 icon_theme = gtk_icon_theme_get_for_screen (screen);
1444 if (priv->pixel_size != -1)
1446 width = height = priv->pixel_size;
1447 *flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1449 else if (!gtk_icon_size_lookup_for_settings (settings,
1453 if (priv->icon_size == -1)
1455 /* Find an available size close to 48 */
1456 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, priv->data.name.icon_name);
1458 width = height = 48;
1459 for (s = sizes; *s; s++)
1463 width = height = 48;
1470 width = height = *s;
1478 width = height = *s;
1487 g_warning ("Invalid icon size %d\n", priv->icon_size);
1488 width = height = 24;
1493 *height_out = height;
1497 ensure_stated_icon_from_info (GtkImage *image,
1500 GtkImagePrivate *priv = image->priv;
1501 GtkStyleContext *context;
1502 GdkPixbuf *destination = NULL;
1505 context = gtk_widget_get_style_context (GTK_WIDGET (image));
1510 gtk_icon_info_load_symbolic_for_context (info,
1515 if (destination == NULL)
1518 gtk_widget_render_icon_pixbuf (GTK_WIDGET (image),
1519 GTK_STOCK_MISSING_IMAGE,
1524 GtkIconSource *source;
1525 GdkPixbuf *rendered;
1527 source = gtk_icon_source_new ();
1528 gtk_icon_source_set_pixbuf (source, destination);
1529 /* The size here is arbitrary; since size isn't
1530 * wildcarded in the source, it isn't supposed to be
1531 * scaled by the engine function
1533 gtk_icon_source_set_size (source,
1534 GTK_ICON_SIZE_SMALL_TOOLBAR);
1535 gtk_icon_source_set_size_wildcarded (source, FALSE);
1537 rendered = gtk_render_icon_pixbuf (context, source, (GtkIconSize) -1);
1538 gtk_icon_source_free (source);
1540 g_object_unref (destination);
1541 destination = rendered;
1548 ensure_pixbuf_for_icon_name (GtkImage *image,
1549 GtkStateFlags state)
1551 GtkImagePrivate *priv = image->priv;
1553 GtkIconTheme *icon_theme;
1556 GtkIconLookupFlags flags;
1558 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME);
1560 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1561 icon_theme = gtk_icon_theme_get_for_screen (screen);
1563 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1565 if (priv->use_fallback)
1566 flags |= GTK_ICON_LOOKUP_GENERIC_FALLBACK;
1568 if ((priv->data.name.pixbuf == NULL) ||
1569 (priv->last_rendered_state != state))
1571 priv->last_rendered_state = state;
1573 if (priv->data.name.pixbuf)
1575 g_object_unref (priv->data.name.pixbuf);
1576 priv->data.name.pixbuf = NULL;
1579 ensure_icon_size (image, &flags, &width, &height);
1580 info = gtk_icon_theme_lookup_icon (icon_theme,
1581 priv->data.name.icon_name,
1582 MIN (width, height), flags);
1584 priv->data.name.pixbuf = ensure_stated_icon_from_info (image, info);
1587 gtk_icon_info_free (info);
1592 ensure_pixbuf_for_gicon (GtkImage *image,
1593 GtkStateFlags state)
1595 GtkImagePrivate *priv = image->priv;
1597 GtkIconTheme *icon_theme;
1600 GtkIconLookupFlags flags;
1602 g_return_if_fail (priv->storage_type == GTK_IMAGE_GICON);
1604 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1605 icon_theme = gtk_icon_theme_get_for_screen (screen);
1607 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1609 if (priv->use_fallback)
1610 flags |= GTK_ICON_LOOKUP_GENERIC_FALLBACK;
1612 if ((priv->data.gicon.pixbuf == NULL) ||
1613 (priv->last_rendered_state != state))
1615 priv->last_rendered_state = state;
1617 if (priv->data.gicon.pixbuf)
1619 g_object_unref (priv->data.gicon.pixbuf);
1620 priv->data.gicon.pixbuf = NULL;
1623 ensure_icon_size (image, &flags, &width, &height);
1624 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1625 priv->data.gicon.icon,
1626 MIN (width, height), flags);
1628 priv->data.gicon.pixbuf = ensure_stated_icon_from_info (image, info);
1631 gtk_icon_info_free (info);
1636 gtk_image_draw (GtkWidget *widget,
1640 GtkImagePrivate *priv;
1642 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1644 image = GTK_IMAGE (widget);
1647 if (priv->storage_type != GTK_IMAGE_EMPTY)
1652 gfloat xalign, yalign;
1654 GtkStateFlags state;
1655 GtkStyleContext *context;
1657 misc = GTK_MISC (widget);
1658 context = gtk_widget_get_style_context (widget);
1660 state = gtk_widget_get_state_flags (widget);
1662 gtk_style_context_save (context);
1663 gtk_style_context_set_state (context, state);
1665 /* For stock items and icon sets, we lazily calculate
1666 * the size; we might get here between a queue_resize()
1667 * and size_request() if something explicitely forces
1670 if (priv->need_calc_size)
1671 gtk_image_calc_size (image);
1673 gtk_misc_get_alignment (misc, &xalign, &yalign);
1674 gtk_misc_get_padding (misc, &xpad, &ypad);
1676 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
1677 xalign = 1.0 - xalign;
1679 x = floor (xpad + ((gtk_widget_get_allocated_width (widget) - priv->required_width) * xalign));
1680 y = floor (ypad + ((gtk_widget_get_allocated_height (widget) - priv->required_height) * yalign));
1682 switch (priv->storage_type)
1685 case GTK_IMAGE_PIXBUF:
1686 pixbuf = priv->data.pixbuf.pixbuf;
1687 g_object_ref (pixbuf);
1690 case GTK_IMAGE_STOCK:
1691 pixbuf = gtk_widget_render_icon_pixbuf (widget,
1692 priv->data.stock.stock_id,
1696 case GTK_IMAGE_ICON_SET:
1698 gtk_icon_set_render_icon_pixbuf (priv->data.icon_set.icon_set,
1699 context, priv->icon_size);
1702 case GTK_IMAGE_ANIMATION:
1704 if (priv->data.anim.iter == NULL)
1706 priv->data.anim.iter = gdk_pixbuf_animation_get_iter (priv->data.anim.anim, NULL);
1708 if (gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter) >= 0)
1709 priv->data.anim.frame_timeout =
1710 gdk_threads_add_timeout (gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter),
1715 /* don't advance the anim iter here, or we could get frame changes between two
1716 * exposes of different areas.
1719 pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (priv->data.anim.iter);
1720 g_object_ref (pixbuf);
1724 case GTK_IMAGE_ICON_NAME:
1725 ensure_pixbuf_for_icon_name (image, state);
1726 pixbuf = priv->data.name.pixbuf;
1729 g_object_ref (pixbuf);
1733 case GTK_IMAGE_GICON:
1734 ensure_pixbuf_for_gicon (image, state);
1735 pixbuf = priv->data.gicon.pixbuf;
1738 g_object_ref (pixbuf);
1742 case GTK_IMAGE_EMPTY:
1744 g_assert_not_reached ();
1751 gtk_render_icon (context, cr,
1755 g_object_unref (pixbuf);
1758 gtk_style_context_restore (context);
1765 gtk_image_reset (GtkImage *image)
1767 GtkImagePrivate *priv = image->priv;
1769 g_object_freeze_notify (G_OBJECT (image));
1771 if (priv->storage_type != GTK_IMAGE_EMPTY)
1772 g_object_notify (G_OBJECT (image), "storage-type");
1774 if (priv->icon_size != DEFAULT_ICON_SIZE)
1776 priv->icon_size = DEFAULT_ICON_SIZE;
1777 g_object_notify (G_OBJECT (image), "icon-size");
1780 switch (priv->storage_type)
1783 case GTK_IMAGE_PIXBUF:
1785 if (priv->data.pixbuf.pixbuf)
1786 g_object_unref (priv->data.pixbuf.pixbuf);
1788 g_object_notify (G_OBJECT (image), "pixbuf");
1792 case GTK_IMAGE_STOCK:
1794 g_free (priv->data.stock.stock_id);
1796 priv->data.stock.stock_id = NULL;
1798 g_object_notify (G_OBJECT (image), "stock");
1801 case GTK_IMAGE_ICON_SET:
1802 if (priv->data.icon_set.icon_set)
1803 gtk_icon_set_unref (priv->data.icon_set.icon_set);
1804 priv->data.icon_set.icon_set = NULL;
1806 g_object_notify (G_OBJECT (image), "icon-set");
1809 case GTK_IMAGE_ANIMATION:
1810 gtk_image_reset_anim_iter (image);
1812 if (priv->data.anim.anim)
1813 g_object_unref (priv->data.anim.anim);
1814 priv->data.anim.anim = NULL;
1816 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1820 case GTK_IMAGE_ICON_NAME:
1821 g_free (priv->data.name.icon_name);
1822 priv->data.name.icon_name = NULL;
1823 if (priv->data.name.pixbuf)
1824 g_object_unref (priv->data.name.pixbuf);
1825 priv->data.name.pixbuf = NULL;
1827 g_object_notify (G_OBJECT (image), "icon-name");
1831 case GTK_IMAGE_GICON:
1832 if (priv->data.gicon.icon)
1833 g_object_unref (priv->data.gicon.icon);
1834 priv->data.gicon.icon = NULL;
1835 if (priv->data.gicon.pixbuf)
1836 g_object_unref (priv->data.gicon.pixbuf);
1837 priv->data.gicon.pixbuf = NULL;
1839 g_object_notify (G_OBJECT (image), "gicon");
1843 case GTK_IMAGE_EMPTY:
1851 g_free (priv->filename);
1852 priv->filename = NULL;
1853 g_object_notify (G_OBJECT (image), "file");
1856 priv->storage_type = GTK_IMAGE_EMPTY;
1858 memset (&priv->data, '\0', sizeof (priv->data));
1860 g_object_thaw_notify (G_OBJECT (image));
1865 * @image: a #GtkImage
1867 * Resets the image to be empty.
1872 gtk_image_clear (GtkImage *image)
1874 GtkImagePrivate *priv = image->priv;
1876 priv->need_calc_size = 1;
1878 gtk_image_reset (image);
1879 gtk_image_update_size (image, 0, 0);
1883 gtk_image_calc_size (GtkImage *image)
1885 GtkWidget *widget = GTK_WIDGET (image);
1886 GtkImagePrivate *priv = image->priv;
1887 GdkPixbuf *pixbuf = NULL;
1888 GtkStyleContext *context;
1889 GtkStateFlags state;
1891 priv->need_calc_size = 0;
1892 context = gtk_widget_get_style_context (widget);
1894 state = gtk_widget_get_state_flags (widget);
1895 gtk_style_context_save (context);
1896 gtk_style_context_set_state (context, state);
1898 /* We update stock/icon set on every size request, because
1899 * the theme could have affected the size; for other kinds of
1900 * image, we just update the required width/height when the image data
1903 switch (priv->storage_type)
1905 case GTK_IMAGE_STOCK:
1906 pixbuf = gtk_widget_render_icon_pixbuf (widget,
1907 priv->data.stock.stock_id,
1911 case GTK_IMAGE_ICON_SET:
1912 pixbuf = gtk_icon_set_render_icon_pixbuf (priv->data.icon_set.icon_set,
1913 context, priv->icon_size);
1915 case GTK_IMAGE_ICON_NAME:
1916 ensure_pixbuf_for_icon_name (image, state);
1917 pixbuf = priv->data.name.pixbuf;
1918 if (pixbuf) g_object_ref (pixbuf);
1920 case GTK_IMAGE_GICON:
1921 ensure_pixbuf_for_gicon (image, state);
1922 pixbuf = priv->data.gicon.pixbuf;
1924 g_object_ref (pixbuf);
1934 gtk_misc_get_padding (GTK_MISC (image), &xpad, &ypad);
1936 priv->required_width = gdk_pixbuf_get_width (pixbuf) + xpad * 2;
1937 priv->required_height = gdk_pixbuf_get_height (pixbuf) + ypad * 2;
1939 g_object_unref (pixbuf);
1942 gtk_style_context_restore (context);
1946 gtk_image_get_preferred_width (GtkWidget *widget,
1951 GtkImagePrivate *priv;
1953 image = GTK_IMAGE (widget);
1956 gtk_image_calc_size (image);
1958 *minimum = *natural = priv->required_width;
1962 gtk_image_get_preferred_height (GtkWidget *widget,
1967 GtkImagePrivate *priv;
1969 image = GTK_IMAGE (widget);
1972 gtk_image_calc_size (image);
1974 *minimum = *natural = priv->required_height;
1978 gtk_image_style_updated (GtkWidget *widget)
1980 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_updated (widget);
1982 icon_theme_changed (GTK_IMAGE (widget));
1986 gtk_image_screen_changed (GtkWidget *widget,
1987 GdkScreen *prev_screen)
1991 image = GTK_IMAGE (widget);
1993 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
1994 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
1996 icon_theme_changed (image);
2001 gtk_image_update_size (GtkImage *image,
2005 GtkWidget *widget = GTK_WIDGET (image);
2006 GtkImagePrivate *priv = image->priv;
2009 gtk_misc_get_padding (GTK_MISC (image), &xpad, &ypad);
2011 priv->required_width = image_width + xpad * 2;
2012 priv->required_height = image_height + ypad * 2;
2014 if (gtk_widget_get_visible (widget))
2015 gtk_widget_queue_resize (widget);
2019 _gtk_image_gicon_data_clear (GtkImageGIconData *data)
2023 g_object_unref (data->pixbuf);
2024 data->pixbuf = NULL;
2028 g_object_unref (data->icon);
2034 * gtk_image_set_pixel_size:
2035 * @image: a #GtkImage
2036 * @pixel_size: the new pixel size
2038 * Sets the pixel size to use for named icons. If the pixel size is set
2039 * to a value != -1, it is used instead of the icon size set by
2040 * gtk_image_set_from_icon_name().
2045 gtk_image_set_pixel_size (GtkImage *image,
2048 GtkImagePrivate *priv;
2050 g_return_if_fail (GTK_IS_IMAGE (image));
2054 if (priv->pixel_size != pixel_size)
2056 priv->pixel_size = pixel_size;
2058 if (priv->storage_type == GTK_IMAGE_ICON_NAME)
2060 if (priv->data.name.pixbuf)
2062 g_object_unref (priv->data.name.pixbuf);
2063 priv->data.name.pixbuf = NULL;
2066 gtk_image_update_size (image, pixel_size, pixel_size);
2069 if (priv->storage_type == GTK_IMAGE_GICON)
2071 if (priv->data.gicon.pixbuf)
2073 g_object_unref (priv->data.gicon.pixbuf);
2074 priv->data.gicon.pixbuf = NULL;
2077 gtk_image_update_size (image, pixel_size, pixel_size);
2080 g_object_notify (G_OBJECT (image), "pixel-size");
2085 * gtk_image_get_pixel_size:
2086 * @image: a #GtkImage
2088 * Gets the pixel size used for named icons.
2090 * Returns: the pixel size used for named icons.
2095 gtk_image_get_pixel_size (GtkImage *image)
2097 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
2099 return image->priv->pixel_size;