1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkcontainer.h"
33 #include "gtkiconfactory.h"
35 #include "gtkicontheme.h"
36 #include "gtksizerequest.h"
38 #include "gtkprivate.h"
42 * @Short_description: A widget displaying an image
44 * @See_also:#GdkPixbuf
46 * The #GtkImage widget displays an image. Various kinds of object
47 * can be displayed as an image; most typically, you would load a
48 * #GdkPixbuf ("pixel buffer") from a file, and then display that.
49 * There's a convenience function to do this, gtk_image_new_from_file(),
51 * <informalexample><programlisting>
53 * image = gtk_image_new_from_file ("myfile.png");
54 * </programlisting></informalexample>
55 * If the file isn't loaded successfully, the image will contain a
56 * "broken image" icon similar to that used in many web browsers.
57 * If you want to handle errors in loading the file yourself,
58 * for example by displaying an error message, then load the image with
59 * gdk_pixbuf_new_from_file(), then create the #GtkImage with
60 * gtk_image_new_from_pixbuf().
62 * The image file may contain an animation, if so the #GtkImage will
63 * display an animation (#GdkPixbufAnimation) instead of a static image.
65 * #GtkImage is a subclass of #GtkMisc, which implies that you can
66 * align it (center, left, right) and add padding to it, using
69 * #GtkImage is a "no window" widget (has no #GdkWindow of its own),
70 * so by default does not receive events. If you want to receive events
71 * on the image, such as button clicks, place the image inside a
72 * #GtkEventBox, then connect to the event signals on the event box.
74 * <title>Handling button press events on a
75 * <structname>GtkImage</structname>.</title>
78 * button_press_callback (GtkWidget *event_box,
79 * GdkEventButton *event,
82 * g_print ("Event box clicked at coordinates %f,%f\n",
83 * event->x, event->y);
85 * /<!---->* Returning TRUE means we handled the event, so the signal
86 * * emission should be stopped (don't call any further
87 * * callbacks that may be connected). Return FALSE
88 * * to continue invoking callbacks.
97 * GtkWidget *event_box;
99 * image = gtk_image_new_from_file ("myfile.png");
101 * event_box = gtk_event_box_new (<!-- -->);
103 * gtk_container_add (GTK_CONTAINER (event_box), image);
105 * g_signal_connect (G_OBJECT (event_box),
106 * "button_press_event",
107 * G_CALLBACK (button_press_callback),
115 * When handling events on the event box, keep in mind that coordinates
116 * in the image may be different from event box coordinates due to
117 * the alignment and padding settings on the image (see #GtkMisc).
118 * The simplest way to solve this is to set the alignment to 0.0
119 * (left/top), and set the padding to zero. Then the origin of
120 * the image will be the same as the origin of the event box.
122 * Sometimes an application will want to avoid depending on external data
123 * files, such as image files. GTK+ comes with a program to avoid this,
124 * called <application>gdk-pixbuf-csource</application>. This program
125 * allows you to convert an image into a C variable declaration, which
126 * can then be loaded into a #GdkPixbuf using
127 * gdk_pixbuf_new_from_inline().
131 struct _GtkImagePrivate
133 GtkIconSize icon_size; /* Only used with GTK_IMAGE_STOCK, GTK_IMAGE_ICON_SET, GTK_IMAGE_ICON_NAME */
134 GtkImageType storage_type;
138 GtkImagePixbufData pixbuf;
139 GtkImageStockData stock;
140 GtkImageIconSetData icon_set;
141 GtkImageAnimationData anim;
142 GtkImageIconNameData name;
143 GtkImageGIconData gicon;
146 gboolean was_symbolic;
147 gchar *filename; /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
148 gint last_rendered_state; /* a GtkStateType, with -1 meaning an invalid state,
149 * only used with GTK_IMAGE_GICON, GTK_IMAGE_ICON_NAME */
151 guint need_calc_size : 1;
153 gint required_height;
157 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
158 static gint gtk_image_draw (GtkWidget *widget,
160 static void gtk_image_unmap (GtkWidget *widget);
161 static void gtk_image_unrealize (GtkWidget *widget);
162 static void gtk_image_get_preferred_width (GtkWidget *widget,
165 static void gtk_image_get_preferred_height (GtkWidget *widget,
169 static void gtk_image_style_set (GtkWidget *widget,
170 GtkStyle *prev_style);
171 static void gtk_image_screen_changed (GtkWidget *widget,
172 GdkScreen *prev_screen);
173 static void gtk_image_destroy (GtkWidget *widget);
174 static void gtk_image_reset (GtkImage *image);
175 static void gtk_image_calc_size (GtkImage *image);
177 static void gtk_image_update_size (GtkImage *image,
181 static void gtk_image_set_property (GObject *object,
185 static void gtk_image_get_property (GObject *object,
190 static void icon_theme_changed (GtkImage *image);
201 PROP_PIXBUF_ANIMATION,
207 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
210 gtk_image_class_init (GtkImageClass *class)
212 GObjectClass *gobject_class;
213 GtkWidgetClass *widget_class;
215 gobject_class = G_OBJECT_CLASS (class);
217 gobject_class->set_property = gtk_image_set_property;
218 gobject_class->get_property = gtk_image_get_property;
220 widget_class = GTK_WIDGET_CLASS (class);
221 widget_class->draw = gtk_image_draw;
222 widget_class->destroy = gtk_image_destroy;
223 widget_class->get_preferred_width = gtk_image_get_preferred_width;
224 widget_class->get_preferred_height = gtk_image_get_preferred_height;
225 widget_class->unmap = gtk_image_unmap;
226 widget_class->unrealize = gtk_image_unrealize;
227 widget_class->style_set = gtk_image_style_set;
228 widget_class->screen_changed = gtk_image_screen_changed;
230 g_object_class_install_property (gobject_class,
232 g_param_spec_object ("pixbuf",
234 P_("A GdkPixbuf to display"),
236 GTK_PARAM_READWRITE));
238 g_object_class_install_property (gobject_class,
240 g_param_spec_string ("file",
242 P_("Filename to load and display"),
244 GTK_PARAM_READWRITE));
247 g_object_class_install_property (gobject_class,
249 g_param_spec_string ("stock",
251 P_("Stock ID for a stock image to display"),
253 GTK_PARAM_READWRITE));
255 g_object_class_install_property (gobject_class,
257 g_param_spec_boxed ("icon-set",
259 P_("Icon set to display"),
261 GTK_PARAM_READWRITE));
263 g_object_class_install_property (gobject_class,
265 g_param_spec_int ("icon-size",
267 P_("Symbolic size to use for stock icon, icon set or named icon"),
270 GTK_PARAM_READWRITE));
272 * GtkImage:pixel-size:
274 * The "pixel-size" property can be used to specify a fixed size
275 * overriding the #GtkImage:icon-size property for images of type
276 * %GTK_IMAGE_ICON_NAME.
280 g_object_class_install_property (gobject_class,
282 g_param_spec_int ("pixel-size",
284 P_("Pixel size to use for named icon"),
287 GTK_PARAM_READWRITE));
289 g_object_class_install_property (gobject_class,
290 PROP_PIXBUF_ANIMATION,
291 g_param_spec_object ("pixbuf-animation",
293 P_("GdkPixbufAnimation to display"),
294 GDK_TYPE_PIXBUF_ANIMATION,
295 GTK_PARAM_READWRITE));
298 * GtkImage:icon-name:
300 * The name of the icon in the icon theme. If the icon theme is
301 * changed, the image will be updated automatically.
305 g_object_class_install_property (gobject_class,
307 g_param_spec_string ("icon-name",
309 P_("The name of the icon from the icon theme"),
311 GTK_PARAM_READWRITE));
316 * The GIcon displayed in the GtkImage. For themed icons,
317 * If the icon theme is changed, the image will be updated
322 g_object_class_install_property (gobject_class,
324 g_param_spec_object ("gicon",
326 P_("The GIcon being displayed"),
328 GTK_PARAM_READWRITE));
330 g_object_class_install_property (gobject_class,
332 g_param_spec_enum ("storage-type",
334 P_("The representation being used for image data"),
337 GTK_PARAM_READABLE));
339 g_type_class_add_private (class, sizeof (GtkImagePrivate));
343 gtk_image_init (GtkImage *image)
345 GtkImagePrivate *priv;
347 image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image,
352 gtk_widget_set_has_window (GTK_WIDGET (image), FALSE);
354 priv->storage_type = GTK_IMAGE_EMPTY;
355 priv->icon_size = DEFAULT_ICON_SIZE;
357 priv->pixel_size = -1;
359 priv->filename = NULL;
363 gtk_image_destroy (GtkWidget *widget)
365 GtkImage *image = GTK_IMAGE (widget);
367 gtk_image_reset (image);
369 GTK_WIDGET_CLASS (gtk_image_parent_class)->destroy (widget);
373 gtk_image_set_property (GObject *object,
378 GtkImage *image = GTK_IMAGE (object);
379 GtkImagePrivate *priv = image->priv;
384 gtk_image_set_from_pixbuf (image,
385 g_value_get_object (value));
388 gtk_image_set_from_file (image, g_value_get_string (value));
391 gtk_image_set_from_stock (image, g_value_get_string (value),
395 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
399 if (priv->storage_type == GTK_IMAGE_STOCK)
400 gtk_image_set_from_stock (image,
401 priv->data.stock.stock_id,
402 g_value_get_int (value));
403 else if (priv->storage_type == GTK_IMAGE_ICON_SET)
404 gtk_image_set_from_icon_set (image,
405 priv->data.icon_set.icon_set,
406 g_value_get_int (value));
407 else if (priv->storage_type == GTK_IMAGE_ICON_NAME)
408 gtk_image_set_from_icon_name (image,
409 priv->data.name.icon_name,
410 g_value_get_int (value));
411 else if (priv->storage_type == GTK_IMAGE_GICON)
412 gtk_image_set_from_gicon (image,
413 priv->data.gicon.icon,
414 g_value_get_int (value));
416 /* Save to be used when STOCK, ICON_SET, ICON_NAME or GICON property comes in */
417 priv->icon_size = g_value_get_int (value);
419 case PROP_PIXEL_SIZE:
420 gtk_image_set_pixel_size (image, g_value_get_int (value));
422 case PROP_PIXBUF_ANIMATION:
423 gtk_image_set_from_animation (image,
424 g_value_get_object (value));
427 gtk_image_set_from_icon_name (image, g_value_get_string (value),
431 gtk_image_set_from_gicon (image, g_value_get_object (value),
436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
442 gtk_image_get_property (GObject *object,
447 GtkImage *image = GTK_IMAGE (object);
448 GtkImagePrivate *priv = image->priv;
450 /* The "getter" functions whine if you try to get the wrong
451 * storage type. This function is instead robust against that,
452 * so that GUI builders don't have to jump through hoops
459 if (priv->storage_type != GTK_IMAGE_PIXBUF)
460 g_value_set_object (value, NULL);
462 g_value_set_object (value,
463 gtk_image_get_pixbuf (image));
466 g_value_set_string (value, priv->filename);
469 if (priv->storage_type != GTK_IMAGE_STOCK)
470 g_value_set_string (value, NULL);
472 g_value_set_string (value,
473 priv->data.stock.stock_id);
476 if (priv->storage_type != GTK_IMAGE_ICON_SET)
477 g_value_set_boxed (value, NULL);
479 g_value_set_boxed (value,
480 priv->data.icon_set.icon_set);
483 g_value_set_int (value, priv->icon_size);
485 case PROP_PIXEL_SIZE:
486 g_value_set_int (value, priv->pixel_size);
488 case PROP_PIXBUF_ANIMATION:
489 if (priv->storage_type != GTK_IMAGE_ANIMATION)
490 g_value_set_object (value, NULL);
492 g_value_set_object (value,
493 priv->data.anim.anim);
496 if (priv->storage_type != GTK_IMAGE_ICON_NAME)
497 g_value_set_string (value, NULL);
499 g_value_set_string (value,
500 priv->data.name.icon_name);
503 if (priv->storage_type != GTK_IMAGE_GICON)
504 g_value_set_object (value, NULL);
506 g_value_set_object (value,
507 priv->data.gicon.icon);
509 case PROP_STORAGE_TYPE:
510 g_value_set_enum (value, priv->storage_type);
514 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
521 * gtk_image_new_from_file:
522 * @filename: a filename
524 * Creates a new #GtkImage displaying the file @filename. If the file
525 * isn't found or can't be loaded, the resulting #GtkImage will
526 * display a "broken image" icon. This function never returns %NULL,
527 * it always returns a valid #GtkImage widget.
529 * If the file contains an animation, the image will contain an
532 * If you need to detect failures to load the file, use
533 * gdk_pixbuf_new_from_file() to load the file yourself, then create
534 * the #GtkImage from the pixbuf. (Or for animations, use
535 * gdk_pixbuf_animation_new_from_file()).
537 * The storage type (gtk_image_get_storage_type()) of the returned
538 * image is not defined, it will be whatever is appropriate for
539 * displaying the file.
541 * Return value: a new #GtkImage
544 gtk_image_new_from_file (const gchar *filename)
548 image = g_object_new (GTK_TYPE_IMAGE, NULL);
550 gtk_image_set_from_file (image, filename);
552 return GTK_WIDGET (image);
556 * gtk_image_new_from_pixbuf:
557 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
559 * Creates a new #GtkImage displaying @pixbuf.
560 * The #GtkImage does not assume a reference to the
561 * pixbuf; you still need to unref it if you own references.
562 * #GtkImage will add its own reference rather than adopting yours.
564 * Note that this function just creates an #GtkImage from the pixbuf. The
565 * #GtkImage created will not react to state changes. Should you want that,
566 * you should use gtk_image_new_from_icon_set().
568 * Return value: a new #GtkImage
571 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
575 image = g_object_new (GTK_TYPE_IMAGE, NULL);
577 gtk_image_set_from_pixbuf (image, pixbuf);
579 return GTK_WIDGET (image);
583 * gtk_image_new_from_stock:
584 * @stock_id: a stock icon name
585 * @size: (type int): a stock icon size
587 * Creates a #GtkImage displaying a stock icon. Sample stock icon
588 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
589 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
590 * icon name isn't known, the image will be empty.
591 * You can register your own stock icon names, see
592 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
594 * Return value: a new #GtkImage displaying the stock icon
597 gtk_image_new_from_stock (const gchar *stock_id,
602 image = g_object_new (GTK_TYPE_IMAGE, NULL);
604 gtk_image_set_from_stock (image, stock_id, size);
606 return GTK_WIDGET (image);
610 * gtk_image_new_from_icon_set:
611 * @icon_set: a #GtkIconSet
612 * @size: (type int): a stock icon size
614 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
615 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
616 * this function, usually it's better to create a #GtkIconFactory, put
617 * your icon sets in the icon factory, add the icon factory to the
618 * list of default factories with gtk_icon_factory_add_default(), and
619 * then use gtk_image_new_from_stock(). This will allow themes to
620 * override the icon you ship with your application.
622 * The #GtkImage does not assume a reference to the
623 * icon set; you still need to unref it if you own references.
624 * #GtkImage will add its own reference rather than adopting yours.
626 * Return value: a new #GtkImage
629 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
634 image = g_object_new (GTK_TYPE_IMAGE, NULL);
636 gtk_image_set_from_icon_set (image, icon_set, size);
638 return GTK_WIDGET (image);
642 * gtk_image_new_from_animation:
643 * @animation: an animation
645 * Creates a #GtkImage displaying the given animation.
646 * The #GtkImage does not assume a reference to the
647 * animation; you still need to unref it if you own references.
648 * #GtkImage will add its own reference rather than adopting yours.
650 * Note that the animation frames are shown using a timeout with
651 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
652 * keep in mind that the animation will only be shown if the main loop
653 * is not busy with something that has a higher priority.
655 * Return value: a new #GtkImage widget
658 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
662 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
664 image = g_object_new (GTK_TYPE_IMAGE, NULL);
666 gtk_image_set_from_animation (image, animation);
668 return GTK_WIDGET (image);
672 * gtk_image_new_from_icon_name:
673 * @icon_name: an icon name
674 * @size: (type int): a stock icon size
676 * Creates a #GtkImage displaying an icon from the current icon theme.
677 * If the icon name isn't known, a "broken image" icon will be
678 * displayed instead. If the current icon theme is changed, the icon
679 * will be updated appropriately.
681 * Return value: a new #GtkImage displaying the themed icon
686 gtk_image_new_from_icon_name (const gchar *icon_name,
691 image = g_object_new (GTK_TYPE_IMAGE, NULL);
693 gtk_image_set_from_icon_name (image, icon_name, size);
695 return GTK_WIDGET (image);
699 * gtk_image_new_from_gicon:
701 * @size: (type int): a stock icon size
703 * Creates a #GtkImage displaying an icon from the current icon theme.
704 * If the icon name isn't known, a "broken image" icon will be
705 * displayed instead. If the current icon theme is changed, the icon
706 * will be updated appropriately.
708 * Return value: a new #GtkImage displaying the themed icon
713 gtk_image_new_from_gicon (GIcon *icon,
718 image = g_object_new (GTK_TYPE_IMAGE, NULL);
720 gtk_image_set_from_gicon (image, icon, size);
722 return GTK_WIDGET (image);
726 * gtk_image_set_from_file:
727 * @image: a #GtkImage
728 * @filename: (allow-none): a filename or %NULL
730 * See gtk_image_new_from_file() for details.
733 gtk_image_set_from_file (GtkImage *image,
734 const gchar *filename)
736 GtkImagePrivate *priv;
737 GdkPixbufAnimation *anim;
739 g_return_if_fail (GTK_IS_IMAGE (image));
743 g_object_freeze_notify (G_OBJECT (image));
745 gtk_image_clear (image);
747 if (filename == NULL)
749 priv->filename = NULL;
750 g_object_thaw_notify (G_OBJECT (image));
754 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
758 gtk_image_set_from_stock (image,
759 GTK_STOCK_MISSING_IMAGE,
760 GTK_ICON_SIZE_BUTTON);
761 g_object_thaw_notify (G_OBJECT (image));
765 /* We could just unconditionally set_from_animation,
766 * but it's nicer for memory if we toss the animation
767 * if it's just a single pixbuf
770 if (gdk_pixbuf_animation_is_static_image (anim))
771 gtk_image_set_from_pixbuf (image,
772 gdk_pixbuf_animation_get_static_image (anim));
774 gtk_image_set_from_animation (image, anim);
776 g_object_unref (anim);
778 priv->filename = g_strdup (filename);
780 g_object_thaw_notify (G_OBJECT (image));
784 * gtk_image_set_from_pixbuf:
785 * @image: a #GtkImage
786 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
788 * See gtk_image_new_from_pixbuf() for details.
791 gtk_image_set_from_pixbuf (GtkImage *image,
794 GtkImagePrivate *priv;
796 g_return_if_fail (GTK_IS_IMAGE (image));
797 g_return_if_fail (pixbuf == NULL ||
798 GDK_IS_PIXBUF (pixbuf));
802 g_object_freeze_notify (G_OBJECT (image));
805 g_object_ref (pixbuf);
807 gtk_image_clear (image);
811 priv->storage_type = GTK_IMAGE_PIXBUF;
813 priv->data.pixbuf.pixbuf = pixbuf;
815 gtk_image_update_size (image,
816 gdk_pixbuf_get_width (pixbuf),
817 gdk_pixbuf_get_height (pixbuf));
820 g_object_notify (G_OBJECT (image), "pixbuf");
822 g_object_thaw_notify (G_OBJECT (image));
826 * gtk_image_set_from_stock:
827 * @image: a #GtkImage
828 * @stock_id: a stock icon name
829 * @size: (type int): a stock icon size
831 * See gtk_image_new_from_stock() for details.
834 gtk_image_set_from_stock (GtkImage *image,
835 const gchar *stock_id,
838 GtkImagePrivate *priv;
841 g_return_if_fail (GTK_IS_IMAGE (image));
845 g_object_freeze_notify (G_OBJECT (image));
847 /* in case stock_id == priv->data.stock.stock_id */
848 new_id = g_strdup (stock_id);
850 gtk_image_clear (image);
854 priv->storage_type = GTK_IMAGE_STOCK;
856 priv->data.stock.stock_id = new_id;
857 priv->icon_size = size;
859 /* Size is demand-computed in size request method
860 * if we're a stock image, since changing the
861 * style impacts the size request
865 g_object_notify (G_OBJECT (image), "stock");
866 g_object_notify (G_OBJECT (image), "icon-size");
868 g_object_thaw_notify (G_OBJECT (image));
872 * gtk_image_set_from_icon_set:
873 * @image: a #GtkImage
874 * @icon_set: a #GtkIconSet
875 * @size: (type int): a stock icon size
877 * See gtk_image_new_from_icon_set() for details.
880 gtk_image_set_from_icon_set (GtkImage *image,
881 GtkIconSet *icon_set,
884 GtkImagePrivate *priv;
886 g_return_if_fail (GTK_IS_IMAGE (image));
890 g_object_freeze_notify (G_OBJECT (image));
893 gtk_icon_set_ref (icon_set);
895 gtk_image_clear (image);
899 priv->storage_type = GTK_IMAGE_ICON_SET;
901 priv->data.icon_set.icon_set = icon_set;
902 priv->icon_size = size;
904 /* Size is demand-computed in size request method
905 * if we're an icon set
909 g_object_notify (G_OBJECT (image), "icon-set");
910 g_object_notify (G_OBJECT (image), "icon-size");
912 g_object_thaw_notify (G_OBJECT (image));
916 * gtk_image_set_from_animation:
917 * @image: a #GtkImage
918 * @animation: the #GdkPixbufAnimation
920 * Causes the #GtkImage to display the given animation (or display
921 * nothing, if you set the animation to %NULL).
924 gtk_image_set_from_animation (GtkImage *image,
925 GdkPixbufAnimation *animation)
927 GtkImagePrivate *priv;
929 g_return_if_fail (GTK_IS_IMAGE (image));
930 g_return_if_fail (animation == NULL ||
931 GDK_IS_PIXBUF_ANIMATION (animation));
935 g_object_freeze_notify (G_OBJECT (image));
938 g_object_ref (animation);
940 gtk_image_clear (image);
942 if (animation != NULL)
944 priv->storage_type = GTK_IMAGE_ANIMATION;
946 priv->data.anim.anim = animation;
947 priv->data.anim.frame_timeout = 0;
948 priv->data.anim.iter = NULL;
950 gtk_image_update_size (image,
951 gdk_pixbuf_animation_get_width (animation),
952 gdk_pixbuf_animation_get_height (animation));
955 g_object_notify (G_OBJECT (image), "pixbuf-animation");
957 g_object_thaw_notify (G_OBJECT (image));
961 * gtk_image_set_from_icon_name:
962 * @image: a #GtkImage
963 * @icon_name: an icon name
964 * @size: (type int): an icon size
966 * See gtk_image_new_from_icon_name() for details.
971 gtk_image_set_from_icon_name (GtkImage *image,
972 const gchar *icon_name,
976 GtkImagePrivate *priv;
978 g_return_if_fail (GTK_IS_IMAGE (image));
982 g_object_freeze_notify (G_OBJECT (image));
984 /* in case icon_name == priv->data.name.icon_name */
985 new_name = g_strdup (icon_name);
987 gtk_image_clear (image);
991 priv->storage_type = GTK_IMAGE_ICON_NAME;
993 priv->data.name.icon_name = new_name;
994 priv->icon_size = size;
996 /* Size is demand-computed in size request method
997 * if we're a icon theme image, since changing the
998 * style impacts the size request
1002 g_object_notify (G_OBJECT (image), "icon-name");
1003 g_object_notify (G_OBJECT (image), "icon-size");
1005 g_object_thaw_notify (G_OBJECT (image));
1009 * gtk_image_set_from_gicon:
1010 * @image: a #GtkImage
1012 * @size: (type int): an icon size
1014 * See gtk_image_new_from_gicon() for details.
1019 gtk_image_set_from_gicon (GtkImage *image,
1023 GtkImagePrivate *priv;
1025 g_return_if_fail (GTK_IS_IMAGE (image));
1029 g_object_freeze_notify (G_OBJECT (image));
1031 /* in case icon == priv->data.gicon.icon */
1033 g_object_ref (icon);
1035 gtk_image_clear (image);
1039 priv->storage_type = GTK_IMAGE_GICON;
1041 priv->data.gicon.icon = icon;
1042 priv->icon_size = size;
1044 /* Size is demand-computed in size request method
1045 * if we're a icon theme image, since changing the
1046 * style impacts the size request
1050 g_object_notify (G_OBJECT (image), "gicon");
1051 g_object_notify (G_OBJECT (image), "icon-size");
1053 g_object_thaw_notify (G_OBJECT (image));
1057 * gtk_image_get_storage_type:
1058 * @image: a #GtkImage
1060 * Gets the type of representation being used by the #GtkImage
1061 * to store image data. If the #GtkImage has no image data,
1062 * the return value will be %GTK_IMAGE_EMPTY.
1064 * Return value: image representation being used
1067 gtk_image_get_storage_type (GtkImage *image)
1069 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1071 return image->priv->storage_type;
1075 * gtk_image_get_pixbuf:
1076 * @image: a #GtkImage
1078 * Gets the #GdkPixbuf being displayed by the #GtkImage.
1079 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1080 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1081 * The caller of this function does not own a reference to the
1084 * Return value: (transfer none): the displayed pixbuf, or %NULL if
1085 * the image is empty
1088 gtk_image_get_pixbuf (GtkImage *image)
1090 GtkImagePrivate *priv;
1092 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1096 g_return_val_if_fail (priv->storage_type == GTK_IMAGE_PIXBUF ||
1097 priv->storage_type == GTK_IMAGE_EMPTY, NULL);
1099 if (priv->storage_type == GTK_IMAGE_EMPTY)
1100 priv->data.pixbuf.pixbuf = NULL;
1102 return priv->data.pixbuf.pixbuf;
1106 * gtk_image_get_stock:
1107 * @image: a #GtkImage
1108 * @stock_id: (out) (transfer none) (allow-none): place to store a
1109 * stock icon name, or %NULL
1110 * @size: (out) (allow-none) (type int): place to store a stock icon
1113 * Gets the stock icon name and size being displayed by the #GtkImage.
1114 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1115 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1116 * The returned string is owned by the #GtkImage and should not
1120 gtk_image_get_stock (GtkImage *image,
1124 GtkImagePrivate *priv;
1126 g_return_if_fail (GTK_IS_IMAGE (image));
1130 g_return_if_fail (priv->storage_type == GTK_IMAGE_STOCK ||
1131 priv->storage_type == GTK_IMAGE_EMPTY);
1133 if (priv->storage_type == GTK_IMAGE_EMPTY)
1134 priv->data.stock.stock_id = NULL;
1137 *stock_id = priv->data.stock.stock_id;
1140 *size = priv->icon_size;
1144 * gtk_image_get_icon_set:
1145 * @image: a #GtkImage
1146 * @icon_set: (out) (transfer none) (allow-none): location to store a
1147 * #GtkIconSet, or %NULL
1148 * @size: (out) (allow-none) (type int): location to store a stock
1149 * icon size, or %NULL
1151 * Gets the icon set and size being displayed by the #GtkImage.
1152 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1153 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1156 gtk_image_get_icon_set (GtkImage *image,
1157 GtkIconSet **icon_set,
1160 GtkImagePrivate *priv;
1162 g_return_if_fail (GTK_IS_IMAGE (image));
1166 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_SET ||
1167 priv->storage_type == GTK_IMAGE_EMPTY);
1170 *icon_set = priv->data.icon_set.icon_set;
1173 *size = priv->icon_size;
1177 * gtk_image_get_animation:
1178 * @image: a #GtkImage
1180 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1181 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1182 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1183 * The caller of this function does not own a reference to the
1184 * returned animation.
1186 * Return value: (transfer none): the displayed animation, or %NULL if
1187 * the image is empty
1190 gtk_image_get_animation (GtkImage *image)
1192 GtkImagePrivate *priv;
1194 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1198 g_return_val_if_fail (priv->storage_type == GTK_IMAGE_ANIMATION ||
1199 priv->storage_type == GTK_IMAGE_EMPTY,
1202 if (priv->storage_type == GTK_IMAGE_EMPTY)
1203 priv->data.anim.anim = NULL;
1205 return priv->data.anim.anim;
1209 * gtk_image_get_icon_name:
1210 * @image: a #GtkImage
1211 * @icon_name: (out) (transfer none) (allow-none): place to store an
1212 * icon name, or %NULL
1213 * @size: (out) (allow-none) (type int): place to store an icon size,
1216 * Gets the icon name and size being displayed by the #GtkImage.
1217 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1218 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1219 * The returned string is owned by the #GtkImage and should not
1225 gtk_image_get_icon_name (GtkImage *image,
1226 G_CONST_RETURN gchar **icon_name,
1229 GtkImagePrivate *priv;
1231 g_return_if_fail (GTK_IS_IMAGE (image));
1235 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME ||
1236 priv->storage_type == GTK_IMAGE_EMPTY);
1238 if (priv->storage_type == GTK_IMAGE_EMPTY)
1239 priv->data.name.icon_name = NULL;
1242 *icon_name = priv->data.name.icon_name;
1245 *size = priv->icon_size;
1249 * gtk_image_get_gicon:
1250 * @image: a #GtkImage
1251 * @gicon: (out) (transfer none) (allow-none): place to store a
1253 * @size: (out) (allow-none) (type int): place to store an icon size,
1256 * Gets the #GIcon and size being displayed by the #GtkImage.
1257 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1258 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1259 * The caller of this function does not own a reference to the
1265 gtk_image_get_gicon (GtkImage *image,
1269 GtkImagePrivate *priv;
1271 g_return_if_fail (GTK_IS_IMAGE (image));
1275 g_return_if_fail (priv->storage_type == GTK_IMAGE_GICON ||
1276 priv->storage_type == GTK_IMAGE_EMPTY);
1278 if (priv->storage_type == GTK_IMAGE_EMPTY)
1279 priv->data.gicon.icon = NULL;
1282 *gicon = priv->data.gicon.icon;
1285 *size = priv->icon_size;
1291 * Creates a new empty #GtkImage widget.
1293 * Return value: a newly created #GtkImage widget.
1296 gtk_image_new (void)
1298 return g_object_new (GTK_TYPE_IMAGE, NULL);
1302 gtk_image_reset_anim_iter (GtkImage *image)
1304 GtkImagePrivate *priv = image->priv;
1306 if (priv->storage_type == GTK_IMAGE_ANIMATION)
1308 /* Reset the animation */
1310 if (priv->data.anim.frame_timeout)
1312 g_source_remove (priv->data.anim.frame_timeout);
1313 priv->data.anim.frame_timeout = 0;
1316 if (priv->data.anim.iter)
1318 g_object_unref (priv->data.anim.iter);
1319 priv->data.anim.iter = NULL;
1325 gtk_image_unmap (GtkWidget *widget)
1327 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1329 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1333 gtk_image_unrealize (GtkWidget *widget)
1335 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1337 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1341 animation_timeout (gpointer data)
1343 GtkImage *image = GTK_IMAGE (data);
1344 GtkImagePrivate *priv = image->priv;
1347 priv->data.anim.frame_timeout = 0;
1349 gdk_pixbuf_animation_iter_advance (priv->data.anim.iter, NULL);
1351 delay = gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter);
1354 GtkWidget *widget = GTK_WIDGET (image);
1356 priv->data.anim.frame_timeout =
1357 gdk_threads_add_timeout (delay, animation_timeout, image);
1359 gtk_widget_queue_draw (widget);
1361 if (gtk_widget_is_drawable (widget))
1362 gdk_window_process_updates (gtk_widget_get_window (widget), TRUE);
1369 icon_theme_changed (GtkImage *image)
1371 GtkImagePrivate *priv = image->priv;
1373 if (priv->storage_type == GTK_IMAGE_ICON_NAME)
1375 if (priv->data.name.pixbuf)
1376 g_object_unref (priv->data.name.pixbuf);
1377 priv->data.name.pixbuf = NULL;
1379 gtk_widget_queue_draw (GTK_WIDGET (image));
1381 if (priv->storage_type == GTK_IMAGE_GICON)
1383 if (priv->data.gicon.pixbuf)
1384 g_object_unref (priv->data.gicon.pixbuf);
1385 priv->data.gicon.pixbuf = NULL;
1387 gtk_widget_queue_draw (GTK_WIDGET (image));
1392 ensure_pixbuf_for_icon_name (GtkImage *image,
1395 GtkImagePrivate *priv = image->priv;
1397 GtkIconTheme *icon_theme;
1398 GtkSettings *settings;
1400 gint *sizes, *s, dist;
1402 GtkIconLookupFlags flags;
1404 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME);
1406 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1407 icon_theme = gtk_icon_theme_get_for_screen (screen);
1408 settings = gtk_settings_get_for_screen (screen);
1409 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1410 if (priv->data.name.pixbuf == NULL ||
1411 (priv->was_symbolic && priv->last_rendered_state != state))
1413 priv->last_rendered_state = state;
1414 if (priv->data.name.pixbuf)
1416 g_object_unref (priv->data.name.pixbuf);
1417 priv->data.name.pixbuf = NULL;
1419 if (priv->pixel_size != -1)
1421 width = height = priv->pixel_size;
1422 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1424 else if (!gtk_icon_size_lookup_for_settings (settings,
1428 if (priv->icon_size == -1)
1430 /* Find an available size close to 48 */
1431 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, priv->data.name.icon_name);
1433 width = height = 48;
1434 for (s = sizes; *s; s++)
1438 width = height = 48;
1445 width = height = *s;
1453 width = height = *s;
1462 g_warning ("Invalid icon size %d\n", priv->icon_size);
1463 width = height = 24;
1467 info = gtk_icon_theme_lookup_icon (icon_theme,
1468 priv->data.name.icon_name,
1469 MIN (width, height), flags);
1472 GtkStyleContext *context;
1473 gboolean was_symbolic;
1475 context = gtk_widget_get_style_context (GTK_WIDGET (image));
1476 priv->data.name.pixbuf =
1477 gtk_icon_info_load_symbolic_for_context (info,
1481 priv->was_symbolic = was_symbolic;
1482 gtk_icon_info_free (info);
1485 if (priv->data.name.pixbuf == NULL)
1487 priv->data.name.pixbuf =
1488 gtk_widget_render_icon (GTK_WIDGET (image),
1489 GTK_STOCK_MISSING_IMAGE,
1492 priv->was_symbolic = FALSE;
1498 ensure_pixbuf_for_gicon (GtkImage *image,
1501 GtkImagePrivate *priv = image->priv;
1503 GtkIconTheme *icon_theme;
1504 GtkSettings *settings;
1507 GtkIconLookupFlags flags;
1509 g_return_if_fail (priv->storage_type == GTK_IMAGE_GICON);
1511 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1512 icon_theme = gtk_icon_theme_get_for_screen (screen);
1513 settings = gtk_settings_get_for_screen (screen);
1514 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1515 if (priv->data.gicon.pixbuf == NULL ||
1516 (priv->was_symbolic && priv->last_rendered_state != state))
1518 priv->last_rendered_state = state;
1519 if (priv->data.gicon.pixbuf)
1521 g_object_unref (priv->data.gicon.pixbuf);
1522 priv->data.gicon.pixbuf = NULL;
1524 if (priv->pixel_size != -1)
1526 width = height = priv->pixel_size;
1527 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1529 else if (!gtk_icon_size_lookup_for_settings (settings,
1533 if (priv->icon_size == -1)
1534 width = height = 48;
1537 g_warning ("Invalid icon size %d\n", priv->icon_size);
1538 width = height = 24;
1542 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1543 priv->data.gicon.icon,
1544 MIN (width, height), flags);
1547 GtkStyleContext *context;
1548 gboolean was_symbolic;
1550 context = gtk_widget_get_style_context (GTK_WIDGET (image));
1551 priv->data.gicon.pixbuf =
1552 gtk_icon_info_load_symbolic_for_context (info,
1556 priv->was_symbolic = was_symbolic;
1557 gtk_icon_info_free (info);
1560 if (priv->data.gicon.pixbuf == NULL)
1562 priv->data.gicon.pixbuf =
1563 gtk_widget_render_icon (GTK_WIDGET (image),
1564 GTK_STOCK_MISSING_IMAGE,
1567 priv->was_symbolic = FALSE;
1573 gtk_image_draw (GtkWidget *widget,
1577 GtkImagePrivate *priv;
1579 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1581 image = GTK_IMAGE (widget);
1584 if (priv->storage_type != GTK_IMAGE_EMPTY)
1589 gfloat xalign, yalign;
1592 gboolean needs_state_transform;
1594 misc = GTK_MISC (widget);
1596 /* For stock items and icon sets, we lazily calculate
1597 * the size; we might get here between a queue_resize()
1598 * and size_request() if something explicitely forces
1601 if (priv->need_calc_size)
1602 gtk_image_calc_size (image);
1604 gtk_misc_get_alignment (misc, &xalign, &yalign);
1605 gtk_misc_get_padding (misc, &xpad, &ypad);
1607 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
1608 xalign = 1.0 - xalign;
1610 x = floor (xpad + ((gtk_widget_get_allocated_width (widget) - priv->required_width) * xalign));
1611 y = floor (ypad + ((gtk_widget_get_allocated_height (widget) - priv->required_height) * yalign));
1613 needs_state_transform = gtk_widget_get_state (widget) != GTK_STATE_NORMAL;
1615 switch (priv->storage_type)
1618 case GTK_IMAGE_PIXBUF:
1619 pixbuf = priv->data.pixbuf.pixbuf;
1620 g_object_ref (pixbuf);
1623 case GTK_IMAGE_STOCK:
1624 pixbuf = gtk_widget_render_icon (widget,
1625 priv->data.stock.stock_id,
1630 needs_state_transform = FALSE;
1633 case GTK_IMAGE_ICON_SET:
1635 gtk_icon_set_render_icon (priv->data.icon_set.icon_set,
1636 gtk_widget_get_style (widget),
1637 gtk_widget_get_direction (widget),
1638 gtk_widget_get_state (widget),
1644 needs_state_transform = FALSE;
1647 case GTK_IMAGE_ANIMATION:
1649 if (priv->data.anim.iter == NULL)
1651 priv->data.anim.iter = gdk_pixbuf_animation_get_iter (priv->data.anim.anim, NULL);
1653 if (gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter) >= 0)
1654 priv->data.anim.frame_timeout =
1655 gdk_threads_add_timeout (gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter),
1660 /* don't advance the anim iter here, or we could get frame changes between two
1661 * exposes of different areas.
1664 pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (priv->data.anim.iter);
1665 g_object_ref (pixbuf);
1669 case GTK_IMAGE_ICON_NAME:
1670 state = gtk_widget_get_state (widget);
1671 if (state == GTK_STATE_INSENSITIVE)
1673 ensure_pixbuf_for_icon_name (image, GTK_STATE_NORMAL);
1677 ensure_pixbuf_for_icon_name (image, state);
1678 /* Already done by the loading function? */
1679 if (priv->was_symbolic)
1680 needs_state_transform = FALSE;
1682 pixbuf = priv->data.name.pixbuf;
1685 g_object_ref (pixbuf);
1689 case GTK_IMAGE_GICON:
1690 state = gtk_widget_get_state (widget);
1691 if (state == GTK_STATE_INSENSITIVE)
1693 ensure_pixbuf_for_gicon (image, GTK_STATE_NORMAL);
1697 ensure_pixbuf_for_gicon (image, state);
1698 /* Already done by the loading function? */
1699 if (priv->was_symbolic)
1700 needs_state_transform = FALSE;
1702 pixbuf = priv->data.gicon.pixbuf;
1705 g_object_ref (pixbuf);
1709 case GTK_IMAGE_EMPTY:
1711 g_assert_not_reached ();
1718 if (needs_state_transform)
1720 GtkIconSource *source;
1721 GdkPixbuf *rendered;
1723 source = gtk_icon_source_new ();
1724 gtk_icon_source_set_pixbuf (source, pixbuf);
1725 /* The size here is arbitrary; since size isn't
1726 * wildcarded in the souce, it isn't supposed to be
1727 * scaled by the engine function
1729 gtk_icon_source_set_size (source,
1730 GTK_ICON_SIZE_SMALL_TOOLBAR);
1731 gtk_icon_source_set_size_wildcarded (source, FALSE);
1733 rendered = gtk_style_render_icon (gtk_widget_get_style (widget),
1735 gtk_widget_get_direction (widget),
1736 gtk_widget_get_state (widget),
1742 gtk_icon_source_free (source);
1744 g_object_unref (pixbuf);
1748 gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
1751 g_object_unref (pixbuf);
1759 gtk_image_reset (GtkImage *image)
1761 GtkImagePrivate *priv = image->priv;
1763 g_object_freeze_notify (G_OBJECT (image));
1765 if (priv->storage_type != GTK_IMAGE_EMPTY)
1766 g_object_notify (G_OBJECT (image), "storage-type");
1768 if (priv->icon_size != DEFAULT_ICON_SIZE)
1770 priv->icon_size = DEFAULT_ICON_SIZE;
1771 g_object_notify (G_OBJECT (image), "icon-size");
1774 switch (priv->storage_type)
1777 case GTK_IMAGE_PIXBUF:
1779 if (priv->data.pixbuf.pixbuf)
1780 g_object_unref (priv->data.pixbuf.pixbuf);
1782 g_object_notify (G_OBJECT (image), "pixbuf");
1786 case GTK_IMAGE_STOCK:
1788 g_free (priv->data.stock.stock_id);
1790 priv->data.stock.stock_id = NULL;
1792 g_object_notify (G_OBJECT (image), "stock");
1795 case GTK_IMAGE_ICON_SET:
1796 if (priv->data.icon_set.icon_set)
1797 gtk_icon_set_unref (priv->data.icon_set.icon_set);
1798 priv->data.icon_set.icon_set = NULL;
1800 g_object_notify (G_OBJECT (image), "icon-set");
1803 case GTK_IMAGE_ANIMATION:
1804 gtk_image_reset_anim_iter (image);
1806 if (priv->data.anim.anim)
1807 g_object_unref (priv->data.anim.anim);
1808 priv->data.anim.anim = NULL;
1810 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1814 case GTK_IMAGE_ICON_NAME:
1815 g_free (priv->data.name.icon_name);
1816 priv->data.name.icon_name = NULL;
1817 if (priv->data.name.pixbuf)
1818 g_object_unref (priv->data.name.pixbuf);
1819 priv->data.name.pixbuf = NULL;
1821 g_object_notify (G_OBJECT (image), "icon-name");
1825 case GTK_IMAGE_GICON:
1826 if (priv->data.gicon.icon)
1827 g_object_unref (priv->data.gicon.icon);
1828 priv->data.gicon.icon = NULL;
1829 if (priv->data.gicon.pixbuf)
1830 g_object_unref (priv->data.gicon.pixbuf);
1831 priv->data.gicon.pixbuf = NULL;
1833 g_object_notify (G_OBJECT (image), "gicon");
1837 case GTK_IMAGE_EMPTY:
1845 g_free (priv->filename);
1846 priv->filename = NULL;
1847 g_object_notify (G_OBJECT (image), "file");
1850 priv->storage_type = GTK_IMAGE_EMPTY;
1852 memset (&priv->data, '\0', sizeof (priv->data));
1854 g_object_thaw_notify (G_OBJECT (image));
1859 * @image: a #GtkImage
1861 * Resets the image to be empty.
1866 gtk_image_clear (GtkImage *image)
1868 GtkImagePrivate *priv = image->priv;
1870 priv->need_calc_size = 1;
1872 gtk_image_reset (image);
1873 gtk_image_update_size (image, 0, 0);
1877 gtk_image_calc_size (GtkImage *image)
1879 GtkWidget *widget = GTK_WIDGET (image);
1880 GtkImagePrivate *priv = image->priv;
1881 GdkPixbuf *pixbuf = NULL;
1883 priv->need_calc_size = 0;
1885 /* We update stock/icon set on every size request, because
1886 * the theme could have affected the size; for other kinds of
1887 * image, we just update the required width/height when the image data
1890 switch (priv->storage_type)
1892 case GTK_IMAGE_STOCK:
1893 pixbuf = gtk_widget_render_icon (widget,
1894 priv->data.stock.stock_id,
1899 case GTK_IMAGE_ICON_SET:
1900 pixbuf = gtk_icon_set_render_icon (priv->data.icon_set.icon_set,
1901 gtk_widget_get_style (widget),
1902 gtk_widget_get_direction (widget),
1903 gtk_widget_get_state (widget),
1908 case GTK_IMAGE_ICON_NAME:
1909 ensure_pixbuf_for_icon_name (image, GTK_STATE_NORMAL);
1910 pixbuf = priv->data.name.pixbuf;
1911 if (pixbuf) g_object_ref (pixbuf);
1913 case GTK_IMAGE_GICON:
1914 ensure_pixbuf_for_gicon (image, GTK_STATE_NORMAL);
1915 pixbuf = priv->data.gicon.pixbuf;
1917 g_object_ref (pixbuf);
1927 gtk_misc_get_padding (GTK_MISC (image), &xpad, &ypad);
1929 priv->required_width = gdk_pixbuf_get_width (pixbuf) + xpad * 2;
1930 priv->required_height = gdk_pixbuf_get_height (pixbuf) + ypad * 2;
1932 g_object_unref (pixbuf);
1937 gtk_image_get_preferred_width (GtkWidget *widget,
1942 GtkImagePrivate *priv;
1944 image = GTK_IMAGE (widget);
1947 gtk_image_calc_size (image);
1949 *minimum = *natural = priv->required_width;
1953 gtk_image_get_preferred_height (GtkWidget *widget,
1958 GtkImagePrivate *priv;
1960 image = GTK_IMAGE (widget);
1963 gtk_image_calc_size (image);
1965 *minimum = *natural = priv->required_height;
1969 gtk_image_style_set (GtkWidget *widget,
1970 GtkStyle *prev_style)
1974 image = GTK_IMAGE (widget);
1976 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_set (widget, prev_style);
1978 icon_theme_changed (image);
1982 gtk_image_screen_changed (GtkWidget *widget,
1983 GdkScreen *prev_screen)
1987 image = GTK_IMAGE (widget);
1989 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
1990 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
1992 icon_theme_changed (image);
1997 gtk_image_update_size (GtkImage *image,
2001 GtkWidget *widget = GTK_WIDGET (image);
2002 GtkImagePrivate *priv = image->priv;
2005 gtk_misc_get_padding (GTK_MISC (image), &xpad, &ypad);
2007 priv->required_width = image_width + xpad * 2;
2008 priv->required_height = image_height + ypad * 2;
2010 if (gtk_widget_get_visible (widget))
2011 gtk_widget_queue_resize (widget);
2016 * gtk_image_set_pixel_size:
2017 * @image: a #GtkImage
2018 * @pixel_size: the new pixel size
2020 * Sets the pixel size to use for named icons. If the pixel size is set
2021 * to a value != -1, it is used instead of the icon size set by
2022 * gtk_image_set_from_icon_name().
2027 gtk_image_set_pixel_size (GtkImage *image,
2030 GtkImagePrivate *priv;
2032 g_return_if_fail (GTK_IS_IMAGE (image));
2036 if (priv->pixel_size != pixel_size)
2038 priv->pixel_size = pixel_size;
2040 if (priv->storage_type == GTK_IMAGE_ICON_NAME)
2042 if (priv->data.name.pixbuf)
2044 g_object_unref (priv->data.name.pixbuf);
2045 priv->data.name.pixbuf = NULL;
2048 gtk_image_update_size (image, pixel_size, pixel_size);
2051 if (priv->storage_type == GTK_IMAGE_GICON)
2053 if (priv->data.gicon.pixbuf)
2055 g_object_unref (priv->data.gicon.pixbuf);
2056 priv->data.gicon.pixbuf = NULL;
2059 gtk_image_update_size (image, pixel_size, pixel_size);
2062 g_object_notify (G_OBJECT (image), "pixel-size");
2067 * gtk_image_get_pixel_size:
2068 * @image: a #GtkImage
2070 * Gets the pixel size used for named icons.
2072 * Returns: the pixel size used for named icons.
2077 gtk_image_get_pixel_size (GtkImage *image)
2079 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
2081 return image->priv->pixel_size;