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"
37 #include "gtkprivate.h"
41 * @Short_description: A widget displaying an image
43 * @See_also:#GdkPixbuf
45 * The #GtkImage widget displays an image. Various kinds of object
46 * can be displayed as an image; most typically, you would load a
47 * #GdkPixbuf ("pixel buffer") from a file, and then display that.
48 * There's a convenience function to do this, gtk_image_new_from_file(),
50 * <informalexample><programlisting>
52 * image = gtk_image_new_from_file ("myfile.png");
53 * </programlisting></informalexample>
54 * If the file isn't loaded successfully, the image will contain a
55 * "broken image" icon similar to that used in many web browsers.
56 * If you want to handle errors in loading the file yourself,
57 * for example by displaying an error message, then load the image with
58 * gdk_pixbuf_new_from_file(), then create the #GtkImage with
59 * gtk_image_new_from_pixbuf().
61 * The image file may contain an animation, if so the #GtkImage will
62 * display an animation (#GdkPixbufAnimation) instead of a static image.
64 * #GtkImage is a subclass of #GtkMisc, which implies that you can
65 * align it (center, left, right) and add padding to it, using
68 * #GtkImage is a "no window" widget (has no #GdkWindow of its own),
69 * so by default does not receive events. If you want to receive events
70 * on the image, such as button clicks, place the image inside a
71 * #GtkEventBox, then connect to the event signals on the event box.
73 * <title>Handling button press events on a
74 * <structname>GtkImage</structname>.</title>
77 * button_press_callback (GtkWidget *event_box,
78 * GdkEventButton *event,
81 * g_print ("Event box clicked at coordinates %f,%f\n",
82 * event->x, event->y);
84 * /<!---->* Returning TRUE means we handled the event, so the signal
85 * * emission should be stopped (don't call any further
86 * * callbacks that may be connected). Return FALSE
87 * * to continue invoking callbacks.
96 * GtkWidget *event_box;
98 * image = gtk_image_new_from_file ("myfile.png");
100 * event_box = gtk_event_box_new (<!-- -->);
102 * gtk_container_add (GTK_CONTAINER (event_box), image);
104 * g_signal_connect (G_OBJECT (event_box),
105 * "button_press_event",
106 * G_CALLBACK (button_press_callback),
114 * When handling events on the event box, keep in mind that coordinates
115 * in the image may be different from event box coordinates due to
116 * the alignment and padding settings on the image (see #GtkMisc).
117 * The simplest way to solve this is to set the alignment to 0.0
118 * (left/top), and set the padding to zero. Then the origin of
119 * the image will be the same as the origin of the event box.
121 * Sometimes an application will want to avoid depending on external data
122 * files, such as image files. GTK+ comes with a program to avoid this,
123 * called <application>gdk-pixbuf-csource</application>. This program
124 * allows you to convert an image into a C variable declaration, which
125 * can then be loaded into a #GdkPixbuf using
126 * gdk_pixbuf_new_from_inline().
130 struct _GtkImagePrivate
132 GtkIconSize icon_size; /* Only used with GTK_IMAGE_STOCK, GTK_IMAGE_ICON_SET, GTK_IMAGE_ICON_NAME */
133 GtkImageType storage_type;
134 GdkBitmap *mask; /* Only used with GTK_IMAGE_PIXMAP, GTK_IMAGE_IMAGE */
138 GtkImagePixmapData pixmap;
139 GtkImagePixbufData pixbuf;
140 GtkImageStockData stock;
141 GtkImageIconSetData icon_set;
142 GtkImageAnimationData anim;
143 GtkImageIconNameData name;
144 GtkImageGIconData gicon;
147 gboolean was_symbolic;
148 gchar *filename; /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
149 gint last_rendered_state; /* a GtkStateType, with -1 meaning an invalid state,
150 * only used with GTK_IMAGE_GICON, GTK_IMAGE_ICON_NAME */
152 guint need_calc_size : 1;
156 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
157 static gint gtk_image_expose (GtkWidget *widget,
158 GdkEventExpose *event);
159 static void gtk_image_unmap (GtkWidget *widget);
160 static void gtk_image_unrealize (GtkWidget *widget);
161 static void gtk_image_size_request (GtkWidget *widget,
162 GtkRequisition *requisition);
163 static void gtk_image_style_set (GtkWidget *widget,
164 GtkStyle *prev_style);
165 static void gtk_image_screen_changed (GtkWidget *widget,
166 GdkScreen *prev_screen);
167 static void gtk_image_destroy (GtkObject *object);
168 static void gtk_image_reset (GtkImage *image);
169 static void gtk_image_calc_size (GtkImage *image);
171 static void gtk_image_update_size (GtkImage *image,
175 static void gtk_image_set_property (GObject *object,
179 static void gtk_image_get_property (GObject *object,
184 static void icon_theme_changed (GtkImage *image);
197 PROP_PIXBUF_ANIMATION,
203 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
206 gtk_image_class_init (GtkImageClass *class)
208 GObjectClass *gobject_class;
209 GtkObjectClass *object_class;
210 GtkWidgetClass *widget_class;
212 gobject_class = G_OBJECT_CLASS (class);
214 gobject_class->set_property = gtk_image_set_property;
215 gobject_class->get_property = gtk_image_get_property;
217 object_class = GTK_OBJECT_CLASS (class);
219 object_class->destroy = gtk_image_destroy;
221 widget_class = GTK_WIDGET_CLASS (class);
223 widget_class->expose_event = gtk_image_expose;
224 widget_class->size_request = gtk_image_size_request;
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_object ("pixmap",
242 P_("A GdkPixmap to display"),
244 GTK_PARAM_READWRITE));
246 g_object_class_install_property (gobject_class,
248 g_param_spec_object ("mask",
250 P_("Mask bitmap to use with GdkPixmap"),
252 GTK_PARAM_READWRITE));
254 g_object_class_install_property (gobject_class,
256 g_param_spec_string ("file",
258 P_("Filename to load and display"),
260 GTK_PARAM_READWRITE));
263 g_object_class_install_property (gobject_class,
265 g_param_spec_string ("stock",
267 P_("Stock ID for a stock image to display"),
269 GTK_PARAM_READWRITE));
271 g_object_class_install_property (gobject_class,
273 g_param_spec_boxed ("icon-set",
275 P_("Icon set to display"),
277 GTK_PARAM_READWRITE));
279 g_object_class_install_property (gobject_class,
281 g_param_spec_int ("icon-size",
283 P_("Symbolic size to use for stock icon, icon set or named icon"),
286 GTK_PARAM_READWRITE));
288 * GtkImage:pixel-size:
290 * The "pixel-size" property can be used to specify a fixed size
291 * overriding the #GtkImage:icon-size property for images of type
292 * %GTK_IMAGE_ICON_NAME.
296 g_object_class_install_property (gobject_class,
298 g_param_spec_int ("pixel-size",
300 P_("Pixel size to use for named icon"),
303 GTK_PARAM_READWRITE));
305 g_object_class_install_property (gobject_class,
306 PROP_PIXBUF_ANIMATION,
307 g_param_spec_object ("pixbuf-animation",
309 P_("GdkPixbufAnimation to display"),
310 GDK_TYPE_PIXBUF_ANIMATION,
311 GTK_PARAM_READWRITE));
314 * GtkImage:icon-name:
316 * The name of the icon in the icon theme. If the icon theme is
317 * changed, the image will be updated automatically.
321 g_object_class_install_property (gobject_class,
323 g_param_spec_string ("icon-name",
325 P_("The name of the icon from the icon theme"),
327 GTK_PARAM_READWRITE));
332 * The GIcon displayed in the GtkImage. For themed icons,
333 * If the icon theme is changed, the image will be updated
338 g_object_class_install_property (gobject_class,
340 g_param_spec_object ("gicon",
342 P_("The GIcon being displayed"),
344 GTK_PARAM_READWRITE));
346 g_object_class_install_property (gobject_class,
348 g_param_spec_enum ("storage-type",
350 P_("The representation being used for image data"),
353 GTK_PARAM_READABLE));
355 g_type_class_add_private (object_class, sizeof (GtkImagePrivate));
359 gtk_image_init (GtkImage *image)
361 GtkImagePrivate *priv;
363 image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image,
368 gtk_widget_set_has_window (GTK_WIDGET (image), FALSE);
370 priv->storage_type = GTK_IMAGE_EMPTY;
371 priv->icon_size = DEFAULT_ICON_SIZE;
374 priv->pixel_size = -1;
376 priv->filename = NULL;
380 gtk_image_destroy (GtkObject *object)
382 GtkImage *image = GTK_IMAGE (object);
384 gtk_image_reset (image);
386 GTK_OBJECT_CLASS (gtk_image_parent_class)->destroy (object);
390 gtk_image_set_property (GObject *object,
395 GtkImage *image = GTK_IMAGE (object);
396 GtkImagePrivate *priv = image->priv;
401 gtk_image_set_from_pixbuf (image,
402 g_value_get_object (value));
405 gtk_image_set_from_pixmap (image,
406 g_value_get_object (value),
410 if (priv->storage_type == GTK_IMAGE_PIXMAP)
411 gtk_image_set_from_pixmap (image,
412 priv->data.pixmap.pixmap,
413 g_value_get_object (value));
418 mask = g_value_get_object (value);
423 gtk_image_clear (image);
429 gtk_image_set_from_file (image, g_value_get_string (value));
432 gtk_image_set_from_stock (image, g_value_get_string (value),
436 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
440 if (priv->storage_type == GTK_IMAGE_STOCK)
441 gtk_image_set_from_stock (image,
442 priv->data.stock.stock_id,
443 g_value_get_int (value));
444 else if (priv->storage_type == GTK_IMAGE_ICON_SET)
445 gtk_image_set_from_icon_set (image,
446 priv->data.icon_set.icon_set,
447 g_value_get_int (value));
448 else if (priv->storage_type == GTK_IMAGE_ICON_NAME)
449 gtk_image_set_from_icon_name (image,
450 priv->data.name.icon_name,
451 g_value_get_int (value));
452 else if (priv->storage_type == GTK_IMAGE_GICON)
453 gtk_image_set_from_gicon (image,
454 priv->data.gicon.icon,
455 g_value_get_int (value));
457 /* Save to be used when STOCK, ICON_SET, ICON_NAME or GICON property comes in */
458 priv->icon_size = g_value_get_int (value);
460 case PROP_PIXEL_SIZE:
461 gtk_image_set_pixel_size (image, g_value_get_int (value));
463 case PROP_PIXBUF_ANIMATION:
464 gtk_image_set_from_animation (image,
465 g_value_get_object (value));
468 gtk_image_set_from_icon_name (image, g_value_get_string (value),
472 gtk_image_set_from_gicon (image, g_value_get_object (value),
477 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
483 gtk_image_get_property (GObject *object,
488 GtkImage *image = GTK_IMAGE (object);
489 GtkImagePrivate *priv = image->priv;
491 /* The "getter" functions whine if you try to get the wrong
492 * storage type. This function is instead robust against that,
493 * so that GUI builders don't have to jump through hoops
500 if (priv->storage_type != GTK_IMAGE_PIXBUF)
501 g_value_set_object (value, NULL);
503 g_value_set_object (value,
504 gtk_image_get_pixbuf (image));
507 if (priv->storage_type != GTK_IMAGE_PIXMAP)
508 g_value_set_object (value, NULL);
510 g_value_set_object (value,
511 priv->data.pixmap.pixmap);
514 g_value_set_object (value, priv->mask);
517 g_value_set_string (value, priv->filename);
520 if (priv->storage_type != GTK_IMAGE_STOCK)
521 g_value_set_string (value, NULL);
523 g_value_set_string (value,
524 priv->data.stock.stock_id);
527 if (priv->storage_type != GTK_IMAGE_ICON_SET)
528 g_value_set_boxed (value, NULL);
530 g_value_set_boxed (value,
531 priv->data.icon_set.icon_set);
534 g_value_set_int (value, priv->icon_size);
536 case PROP_PIXEL_SIZE:
537 g_value_set_int (value, priv->pixel_size);
539 case PROP_PIXBUF_ANIMATION:
540 if (priv->storage_type != GTK_IMAGE_ANIMATION)
541 g_value_set_object (value, NULL);
543 g_value_set_object (value,
544 priv->data.anim.anim);
547 if (priv->storage_type != GTK_IMAGE_ICON_NAME)
548 g_value_set_string (value, NULL);
550 g_value_set_string (value,
551 priv->data.name.icon_name);
554 if (priv->storage_type != GTK_IMAGE_GICON)
555 g_value_set_object (value, NULL);
557 g_value_set_object (value,
558 priv->data.gicon.icon);
560 case PROP_STORAGE_TYPE:
561 g_value_set_enum (value, priv->storage_type);
565 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
572 * gtk_image_new_from_pixmap:
573 * @pixmap: (allow-none): a #GdkPixmap, or %NULL
574 * @mask: (allow-none): a #GdkBitmap, or %NULL
576 * Creates a #GtkImage widget displaying @pixmap with a @mask.
577 * A #GdkPixmap is a server-side image buffer in the pixel format of the
578 * current display. The #GtkImage does not assume a reference to the
579 * pixmap or mask; you still need to unref them if you own references.
580 * #GtkImage will add its own reference rather than adopting yours.
582 * Return value: a new #GtkImage
585 gtk_image_new_from_pixmap (GdkPixmap *pixmap,
590 image = g_object_new (GTK_TYPE_IMAGE, NULL);
592 gtk_image_set_from_pixmap (image, pixmap, mask);
594 return GTK_WIDGET (image);
598 * gtk_image_new_from_file:
599 * @filename: a filename
601 * Creates a new #GtkImage displaying the file @filename. If the file
602 * isn't found or can't be loaded, the resulting #GtkImage will
603 * display a "broken image" icon. This function never returns %NULL,
604 * it always returns a valid #GtkImage widget.
606 * If the file contains an animation, the image will contain an
609 * If you need to detect failures to load the file, use
610 * gdk_pixbuf_new_from_file() to load the file yourself, then create
611 * the #GtkImage from the pixbuf. (Or for animations, use
612 * gdk_pixbuf_animation_new_from_file()).
614 * The storage type (gtk_image_get_storage_type()) of the returned
615 * image is not defined, it will be whatever is appropriate for
616 * displaying the file.
618 * Return value: a new #GtkImage
621 gtk_image_new_from_file (const gchar *filename)
625 image = g_object_new (GTK_TYPE_IMAGE, NULL);
627 gtk_image_set_from_file (image, filename);
629 return GTK_WIDGET (image);
633 * gtk_image_new_from_pixbuf:
634 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
636 * Creates a new #GtkImage displaying @pixbuf.
637 * The #GtkImage does not assume a reference to the
638 * pixbuf; you still need to unref it if you own references.
639 * #GtkImage will add its own reference rather than adopting yours.
641 * Note that this function just creates an #GtkImage from the pixbuf. The
642 * #GtkImage created will not react to state changes. Should you want that,
643 * you should use gtk_image_new_from_icon_set().
645 * Return value: a new #GtkImage
648 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
652 image = g_object_new (GTK_TYPE_IMAGE, NULL);
654 gtk_image_set_from_pixbuf (image, pixbuf);
656 return GTK_WIDGET (image);
660 * gtk_image_new_from_stock:
661 * @stock_id: a stock icon name
662 * @size: (type int): a stock icon size
664 * Creates a #GtkImage displaying a stock icon. Sample stock icon
665 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
666 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
667 * icon name isn't known, the image will be empty.
668 * You can register your own stock icon names, see
669 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
671 * Return value: a new #GtkImage displaying the stock icon
674 gtk_image_new_from_stock (const gchar *stock_id,
679 image = g_object_new (GTK_TYPE_IMAGE, NULL);
681 gtk_image_set_from_stock (image, stock_id, size);
683 return GTK_WIDGET (image);
687 * gtk_image_new_from_icon_set:
688 * @icon_set: a #GtkIconSet
689 * @size: (type int): a stock icon size
691 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
692 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
693 * this function, usually it's better to create a #GtkIconFactory, put
694 * your icon sets in the icon factory, add the icon factory to the
695 * list of default factories with gtk_icon_factory_add_default(), and
696 * then use gtk_image_new_from_stock(). This will allow themes to
697 * override the icon you ship with your application.
699 * The #GtkImage does not assume a reference to the
700 * icon set; you still need to unref it if you own references.
701 * #GtkImage will add its own reference rather than adopting yours.
703 * Return value: a new #GtkImage
706 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
711 image = g_object_new (GTK_TYPE_IMAGE, NULL);
713 gtk_image_set_from_icon_set (image, icon_set, size);
715 return GTK_WIDGET (image);
719 * gtk_image_new_from_animation:
720 * @animation: an animation
722 * Creates a #GtkImage displaying the given animation.
723 * The #GtkImage does not assume a reference to the
724 * animation; you still need to unref it if you own references.
725 * #GtkImage will add its own reference rather than adopting yours.
727 * Note that the animation frames are shown using a timeout with
728 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
729 * keep in mind that the animation will only be shown if the main loop
730 * is not busy with something that has a higher priority.
732 * Return value: a new #GtkImage widget
735 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
739 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
741 image = g_object_new (GTK_TYPE_IMAGE, NULL);
743 gtk_image_set_from_animation (image, animation);
745 return GTK_WIDGET (image);
749 * gtk_image_new_from_icon_name:
750 * @icon_name: an icon name
751 * @size: (type int): a stock icon size
753 * Creates a #GtkImage displaying an icon from the current icon theme.
754 * If the icon name isn't known, a "broken image" icon will be
755 * displayed instead. If the current icon theme is changed, the icon
756 * will be updated appropriately.
758 * Return value: a new #GtkImage displaying the themed icon
763 gtk_image_new_from_icon_name (const gchar *icon_name,
768 image = g_object_new (GTK_TYPE_IMAGE, NULL);
770 gtk_image_set_from_icon_name (image, icon_name, size);
772 return GTK_WIDGET (image);
776 * gtk_image_new_from_gicon:
778 * @size: (type int): a stock icon size
780 * Creates a #GtkImage displaying an icon from the current icon theme.
781 * If the icon name isn't known, a "broken image" icon will be
782 * displayed instead. If the current icon theme is changed, the icon
783 * will be updated appropriately.
785 * Return value: a new #GtkImage displaying the themed icon
790 gtk_image_new_from_gicon (GIcon *icon,
795 image = g_object_new (GTK_TYPE_IMAGE, NULL);
797 gtk_image_set_from_gicon (image, icon, size);
799 return GTK_WIDGET (image);
803 * gtk_image_set_from_pixmap:
804 * @image: a #GtkImage
805 * @pixmap: (allow-none): a #GdkPixmap or %NULL
806 * @mask: (allow-none): a #GdkBitmap or %NULL
808 * See gtk_image_new_from_pixmap() for details.
811 gtk_image_set_from_pixmap (GtkImage *image,
815 GtkImagePrivate *priv;
817 g_return_if_fail (GTK_IS_IMAGE (image));
818 g_return_if_fail (pixmap == NULL ||
819 GDK_IS_PIXMAP (pixmap));
820 g_return_if_fail (mask == NULL ||
821 GDK_IS_PIXMAP (mask));
825 g_object_freeze_notify (G_OBJECT (image));
828 g_object_ref (pixmap);
833 gtk_image_clear (image);
842 priv->storage_type = GTK_IMAGE_PIXMAP;
844 priv->data.pixmap.pixmap = pixmap;
846 gdk_drawable_get_size (GDK_DRAWABLE (pixmap), &width, &height);
848 gtk_image_update_size (image, width, height);
851 g_object_notify (G_OBJECT (image), "pixmap");
852 g_object_notify (G_OBJECT (image), "mask");
854 g_object_thaw_notify (G_OBJECT (image));
858 * gtk_image_set_from_file:
859 * @image: a #GtkImage
860 * @filename: (allow-none): a filename or %NULL
862 * See gtk_image_new_from_file() for details.
865 gtk_image_set_from_file (GtkImage *image,
866 const gchar *filename)
868 GtkImagePrivate *priv;
869 GdkPixbufAnimation *anim;
871 g_return_if_fail (GTK_IS_IMAGE (image));
875 g_object_freeze_notify (G_OBJECT (image));
877 gtk_image_clear (image);
879 if (filename == NULL)
881 priv->filename = NULL;
882 g_object_thaw_notify (G_OBJECT (image));
886 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
890 gtk_image_set_from_stock (image,
891 GTK_STOCK_MISSING_IMAGE,
892 GTK_ICON_SIZE_BUTTON);
893 g_object_thaw_notify (G_OBJECT (image));
897 /* We could just unconditionally set_from_animation,
898 * but it's nicer for memory if we toss the animation
899 * if it's just a single pixbuf
902 if (gdk_pixbuf_animation_is_static_image (anim))
903 gtk_image_set_from_pixbuf (image,
904 gdk_pixbuf_animation_get_static_image (anim));
906 gtk_image_set_from_animation (image, anim);
908 g_object_unref (anim);
910 priv->filename = g_strdup (filename);
912 g_object_thaw_notify (G_OBJECT (image));
916 * gtk_image_set_from_pixbuf:
917 * @image: a #GtkImage
918 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
920 * See gtk_image_new_from_pixbuf() for details.
923 gtk_image_set_from_pixbuf (GtkImage *image,
926 GtkImagePrivate *priv;
928 g_return_if_fail (GTK_IS_IMAGE (image));
929 g_return_if_fail (pixbuf == NULL ||
930 GDK_IS_PIXBUF (pixbuf));
934 g_object_freeze_notify (G_OBJECT (image));
937 g_object_ref (pixbuf);
939 gtk_image_clear (image);
943 priv->storage_type = GTK_IMAGE_PIXBUF;
945 priv->data.pixbuf.pixbuf = pixbuf;
947 gtk_image_update_size (image,
948 gdk_pixbuf_get_width (pixbuf),
949 gdk_pixbuf_get_height (pixbuf));
952 g_object_notify (G_OBJECT (image), "pixbuf");
954 g_object_thaw_notify (G_OBJECT (image));
958 * gtk_image_set_from_stock:
959 * @image: a #GtkImage
960 * @stock_id: a stock icon name
961 * @size: (type int): a stock icon size
963 * See gtk_image_new_from_stock() for details.
966 gtk_image_set_from_stock (GtkImage *image,
967 const gchar *stock_id,
970 GtkImagePrivate *priv;
973 g_return_if_fail (GTK_IS_IMAGE (image));
977 g_object_freeze_notify (G_OBJECT (image));
979 /* in case stock_id == priv->data.stock.stock_id */
980 new_id = g_strdup (stock_id);
982 gtk_image_clear (image);
986 priv->storage_type = GTK_IMAGE_STOCK;
988 priv->data.stock.stock_id = new_id;
989 priv->icon_size = size;
991 /* Size is demand-computed in size request method
992 * if we're a stock image, since changing the
993 * style impacts the size request
997 g_object_notify (G_OBJECT (image), "stock");
998 g_object_notify (G_OBJECT (image), "icon-size");
1000 g_object_thaw_notify (G_OBJECT (image));
1004 * gtk_image_set_from_icon_set:
1005 * @image: a #GtkImage
1006 * @icon_set: a #GtkIconSet
1007 * @size: (type int): a stock icon size
1009 * See gtk_image_new_from_icon_set() for details.
1012 gtk_image_set_from_icon_set (GtkImage *image,
1013 GtkIconSet *icon_set,
1016 GtkImagePrivate *priv;
1018 g_return_if_fail (GTK_IS_IMAGE (image));
1022 g_object_freeze_notify (G_OBJECT (image));
1025 gtk_icon_set_ref (icon_set);
1027 gtk_image_clear (image);
1031 priv->storage_type = GTK_IMAGE_ICON_SET;
1033 priv->data.icon_set.icon_set = icon_set;
1034 priv->icon_size = size;
1036 /* Size is demand-computed in size request method
1037 * if we're an icon set
1041 g_object_notify (G_OBJECT (image), "icon-set");
1042 g_object_notify (G_OBJECT (image), "icon-size");
1044 g_object_thaw_notify (G_OBJECT (image));
1048 * gtk_image_set_from_animation:
1049 * @image: a #GtkImage
1050 * @animation: the #GdkPixbufAnimation
1052 * Causes the #GtkImage to display the given animation (or display
1053 * nothing, if you set the animation to %NULL).
1056 gtk_image_set_from_animation (GtkImage *image,
1057 GdkPixbufAnimation *animation)
1059 GtkImagePrivate *priv;
1061 g_return_if_fail (GTK_IS_IMAGE (image));
1062 g_return_if_fail (animation == NULL ||
1063 GDK_IS_PIXBUF_ANIMATION (animation));
1067 g_object_freeze_notify (G_OBJECT (image));
1070 g_object_ref (animation);
1072 gtk_image_clear (image);
1074 if (animation != NULL)
1076 priv->storage_type = GTK_IMAGE_ANIMATION;
1078 priv->data.anim.anim = animation;
1079 priv->data.anim.frame_timeout = 0;
1080 priv->data.anim.iter = NULL;
1082 gtk_image_update_size (image,
1083 gdk_pixbuf_animation_get_width (animation),
1084 gdk_pixbuf_animation_get_height (animation));
1087 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1089 g_object_thaw_notify (G_OBJECT (image));
1093 * gtk_image_set_from_icon_name:
1094 * @image: a #GtkImage
1095 * @icon_name: an icon name
1096 * @size: (type int): an icon size
1098 * See gtk_image_new_from_icon_name() for details.
1103 gtk_image_set_from_icon_name (GtkImage *image,
1104 const gchar *icon_name,
1108 GtkImagePrivate *priv;
1110 g_return_if_fail (GTK_IS_IMAGE (image));
1114 g_object_freeze_notify (G_OBJECT (image));
1116 /* in case icon_name == priv->data.name.icon_name */
1117 new_name = g_strdup (icon_name);
1119 gtk_image_clear (image);
1123 priv->storage_type = GTK_IMAGE_ICON_NAME;
1125 priv->data.name.icon_name = new_name;
1126 priv->icon_size = size;
1128 /* Size is demand-computed in size request method
1129 * if we're a icon theme image, since changing the
1130 * style impacts the size request
1134 g_object_notify (G_OBJECT (image), "icon-name");
1135 g_object_notify (G_OBJECT (image), "icon-size");
1137 g_object_thaw_notify (G_OBJECT (image));
1141 * gtk_image_set_from_gicon:
1142 * @image: a #GtkImage
1144 * @size: (type int): an icon size
1146 * See gtk_image_new_from_gicon() for details.
1151 gtk_image_set_from_gicon (GtkImage *image,
1155 GtkImagePrivate *priv;
1157 g_return_if_fail (GTK_IS_IMAGE (image));
1161 g_object_freeze_notify (G_OBJECT (image));
1163 /* in case icon == priv->data.gicon.icon */
1165 g_object_ref (icon);
1167 gtk_image_clear (image);
1171 priv->storage_type = GTK_IMAGE_GICON;
1173 priv->data.gicon.icon = icon;
1174 priv->icon_size = size;
1176 /* Size is demand-computed in size request method
1177 * if we're a icon theme image, since changing the
1178 * style impacts the size request
1182 g_object_notify (G_OBJECT (image), "gicon");
1183 g_object_notify (G_OBJECT (image), "icon-size");
1185 g_object_thaw_notify (G_OBJECT (image));
1189 * gtk_image_get_storage_type:
1190 * @image: a #GtkImage
1192 * Gets the type of representation being used by the #GtkImage
1193 * to store image data. If the #GtkImage has no image data,
1194 * the return value will be %GTK_IMAGE_EMPTY.
1196 * Return value: image representation being used
1199 gtk_image_get_storage_type (GtkImage *image)
1201 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1203 return image->priv->storage_type;
1207 * gtk_image_get_pixmap:
1208 * @image: a #GtkImage
1209 * @pixmap: (out) (transfer none) (allow-none): location to store the
1211 * @mask: (out) (transfer none) (allow-none): location to store the
1214 * Gets the pixmap and mask being displayed by the #GtkImage.
1215 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1216 * %GTK_IMAGE_PIXMAP (see gtk_image_get_storage_type()).
1217 * The caller of this function does not own a reference to the
1218 * returned pixmap and mask.
1221 gtk_image_get_pixmap (GtkImage *image,
1225 GtkImagePrivate *priv;
1227 g_return_if_fail (GTK_IS_IMAGE (image));
1231 g_return_if_fail (priv->storage_type == GTK_IMAGE_PIXMAP ||
1232 priv->storage_type == GTK_IMAGE_EMPTY);
1235 *pixmap = priv->data.pixmap.pixmap;
1242 * gtk_image_get_pixbuf:
1243 * @image: a #GtkImage
1245 * Gets the #GdkPixbuf being displayed by the #GtkImage.
1246 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1247 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1248 * The caller of this function does not own a reference to the
1251 * Return value: (transfer none): the displayed pixbuf, or %NULL if
1252 * the image is empty
1255 gtk_image_get_pixbuf (GtkImage *image)
1257 GtkImagePrivate *priv;
1259 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1263 g_return_val_if_fail (priv->storage_type == GTK_IMAGE_PIXBUF ||
1264 priv->storage_type == GTK_IMAGE_EMPTY, NULL);
1266 if (priv->storage_type == GTK_IMAGE_EMPTY)
1267 priv->data.pixbuf.pixbuf = NULL;
1269 return priv->data.pixbuf.pixbuf;
1273 * gtk_image_get_stock:
1274 * @image: a #GtkImage
1275 * @stock_id: (out) (transfer none) (allow-none): place to store a
1276 * stock icon name, or %NULL
1277 * @size: (out) (allow-none) (type int): place to store a stock icon
1280 * Gets the stock icon name and size being displayed by the #GtkImage.
1281 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1282 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1283 * The returned string is owned by the #GtkImage and should not
1287 gtk_image_get_stock (GtkImage *image,
1291 GtkImagePrivate *priv;
1293 g_return_if_fail (GTK_IS_IMAGE (image));
1297 g_return_if_fail (priv->storage_type == GTK_IMAGE_STOCK ||
1298 priv->storage_type == GTK_IMAGE_EMPTY);
1300 if (priv->storage_type == GTK_IMAGE_EMPTY)
1301 priv->data.stock.stock_id = NULL;
1304 *stock_id = priv->data.stock.stock_id;
1307 *size = priv->icon_size;
1311 * gtk_image_get_icon_set:
1312 * @image: a #GtkImage
1313 * @icon_set: (out) (transfer none) (allow-none): location to store a
1314 * #GtkIconSet, or %NULL
1315 * @size: (out) (allow-none) (type int): location to store a stock
1316 * icon size, or %NULL
1318 * Gets the icon set and size being displayed by the #GtkImage.
1319 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1320 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1323 gtk_image_get_icon_set (GtkImage *image,
1324 GtkIconSet **icon_set,
1327 GtkImagePrivate *priv;
1329 g_return_if_fail (GTK_IS_IMAGE (image));
1333 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_SET ||
1334 priv->storage_type == GTK_IMAGE_EMPTY);
1337 *icon_set = priv->data.icon_set.icon_set;
1340 *size = priv->icon_size;
1344 * gtk_image_get_animation:
1345 * @image: a #GtkImage
1347 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1348 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1349 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1350 * The caller of this function does not own a reference to the
1351 * returned animation.
1353 * Return value: (transfer none): the displayed animation, or %NULL if
1354 * the image is empty
1357 gtk_image_get_animation (GtkImage *image)
1359 GtkImagePrivate *priv;
1361 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1365 g_return_val_if_fail (priv->storage_type == GTK_IMAGE_ANIMATION ||
1366 priv->storage_type == GTK_IMAGE_EMPTY,
1369 if (priv->storage_type == GTK_IMAGE_EMPTY)
1370 priv->data.anim.anim = NULL;
1372 return priv->data.anim.anim;
1376 * gtk_image_get_icon_name:
1377 * @image: a #GtkImage
1378 * @icon_name: (out) (transfer none) (allow-none): place to store an
1379 * icon name, or %NULL
1380 * @size: (out) (allow-none) (type int): place to store an icon size,
1383 * Gets the icon name and size being displayed by the #GtkImage.
1384 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1385 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1386 * The returned string is owned by the #GtkImage and should not
1392 gtk_image_get_icon_name (GtkImage *image,
1393 G_CONST_RETURN gchar **icon_name,
1396 GtkImagePrivate *priv;
1398 g_return_if_fail (GTK_IS_IMAGE (image));
1402 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME ||
1403 priv->storage_type == GTK_IMAGE_EMPTY);
1405 if (priv->storage_type == GTK_IMAGE_EMPTY)
1406 priv->data.name.icon_name = NULL;
1409 *icon_name = priv->data.name.icon_name;
1412 *size = priv->icon_size;
1416 * gtk_image_get_gicon:
1417 * @image: a #GtkImage
1418 * @gicon: (out) (transfer none) (allow-none): place to store a
1420 * @size: (out) (allow-none) (type int): place to store an icon size,
1423 * Gets the #GIcon and size being displayed by the #GtkImage.
1424 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1425 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1426 * The caller of this function does not own a reference to the
1432 gtk_image_get_gicon (GtkImage *image,
1436 GtkImagePrivate *priv;
1438 g_return_if_fail (GTK_IS_IMAGE (image));
1442 g_return_if_fail (priv->storage_type == GTK_IMAGE_GICON ||
1443 priv->storage_type == GTK_IMAGE_EMPTY);
1445 if (priv->storage_type == GTK_IMAGE_EMPTY)
1446 priv->data.gicon.icon = NULL;
1449 *gicon = priv->data.gicon.icon;
1452 *size = priv->icon_size;
1458 * Creates a new empty #GtkImage widget.
1460 * Return value: a newly created #GtkImage widget.
1463 gtk_image_new (void)
1465 return g_object_new (GTK_TYPE_IMAGE, NULL);
1469 gtk_image_reset_anim_iter (GtkImage *image)
1471 GtkImagePrivate *priv = image->priv;
1473 if (priv->storage_type == GTK_IMAGE_ANIMATION)
1475 /* Reset the animation */
1477 if (priv->data.anim.frame_timeout)
1479 g_source_remove (priv->data.anim.frame_timeout);
1480 priv->data.anim.frame_timeout = 0;
1483 if (priv->data.anim.iter)
1485 g_object_unref (priv->data.anim.iter);
1486 priv->data.anim.iter = NULL;
1492 gtk_image_unmap (GtkWidget *widget)
1494 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1496 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1500 gtk_image_unrealize (GtkWidget *widget)
1502 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1504 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1508 animation_timeout (gpointer data)
1510 GtkImage *image = GTK_IMAGE (data);
1511 GtkImagePrivate *priv = image->priv;
1514 priv->data.anim.frame_timeout = 0;
1516 gdk_pixbuf_animation_iter_advance (priv->data.anim.iter, NULL);
1518 delay = gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter);
1521 priv->data.anim.frame_timeout =
1522 gdk_threads_add_timeout (delay, animation_timeout, image);
1524 gtk_widget_queue_draw (GTK_WIDGET (image));
1526 if (gtk_widget_is_drawable (GTK_WIDGET (image)))
1527 gdk_window_process_updates (GTK_WIDGET (image)->window, TRUE);
1534 icon_theme_changed (GtkImage *image)
1536 GtkImagePrivate *priv = image->priv;
1538 if (priv->storage_type == GTK_IMAGE_ICON_NAME)
1540 if (priv->data.name.pixbuf)
1541 g_object_unref (priv->data.name.pixbuf);
1542 priv->data.name.pixbuf = NULL;
1544 gtk_widget_queue_draw (GTK_WIDGET (image));
1546 if (priv->storage_type == GTK_IMAGE_GICON)
1548 if (priv->data.gicon.pixbuf)
1549 g_object_unref (priv->data.gicon.pixbuf);
1550 priv->data.gicon.pixbuf = NULL;
1552 gtk_widget_queue_draw (GTK_WIDGET (image));
1557 ensure_pixbuf_for_icon_name (GtkImage *image,
1560 GtkImagePrivate *priv = image->priv;
1562 GtkIconTheme *icon_theme;
1563 GtkSettings *settings;
1565 gint *sizes, *s, dist;
1567 GtkIconLookupFlags flags;
1569 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME);
1571 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1572 icon_theme = gtk_icon_theme_get_for_screen (screen);
1573 settings = gtk_settings_get_for_screen (screen);
1574 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1575 if (priv->data.name.pixbuf == NULL ||
1576 (priv->was_symbolic && priv->last_rendered_state != state))
1578 priv->last_rendered_state = state;
1579 if (priv->data.name.pixbuf)
1581 g_object_unref (priv->data.name.pixbuf);
1582 priv->data.name.pixbuf = NULL;
1584 if (priv->pixel_size != -1)
1586 width = height = priv->pixel_size;
1587 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1589 else if (!gtk_icon_size_lookup_for_settings (settings,
1593 if (priv->icon_size == -1)
1595 /* Find an available size close to 48 */
1596 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, priv->data.name.icon_name);
1598 width = height = 48;
1599 for (s = sizes; *s; s++)
1603 width = height = 48;
1610 width = height = *s;
1618 width = height = *s;
1627 g_warning ("Invalid icon size %d\n", priv->icon_size);
1628 width = height = 24;
1632 info = gtk_icon_theme_lookup_icon (icon_theme,
1633 priv->data.name.icon_name,
1634 MIN (width, height), flags);
1638 gboolean was_symbolic;
1640 style = gtk_widget_get_style (GTK_WIDGET (image));
1641 priv->data.name.pixbuf =
1642 gtk_icon_info_load_symbolic_for_style (info,
1646 priv->was_symbolic = was_symbolic;
1647 gtk_icon_info_free (info);
1650 if (priv->data.name.pixbuf == NULL)
1652 priv->data.name.pixbuf =
1653 gtk_widget_render_icon (GTK_WIDGET (image),
1654 GTK_STOCK_MISSING_IMAGE,
1657 priv->was_symbolic = FALSE;
1663 ensure_pixbuf_for_gicon (GtkImage *image,
1666 GtkImagePrivate *priv = image->priv;
1668 GtkIconTheme *icon_theme;
1669 GtkSettings *settings;
1672 GtkIconLookupFlags flags;
1674 g_return_if_fail (priv->storage_type == GTK_IMAGE_GICON);
1676 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1677 icon_theme = gtk_icon_theme_get_for_screen (screen);
1678 settings = gtk_settings_get_for_screen (screen);
1679 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1680 if (priv->data.gicon.pixbuf == NULL ||
1681 (priv->was_symbolic && priv->last_rendered_state != state))
1683 priv->last_rendered_state = state;
1684 if (priv->data.gicon.pixbuf)
1686 g_object_unref (priv->data.gicon.pixbuf);
1687 priv->data.gicon.pixbuf = NULL;
1689 if (priv->pixel_size != -1)
1691 width = height = priv->pixel_size;
1692 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1694 else if (!gtk_icon_size_lookup_for_settings (settings,
1698 if (priv->icon_size == -1)
1699 width = height = 48;
1702 g_warning ("Invalid icon size %d\n", priv->icon_size);
1703 width = height = 24;
1707 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1708 priv->data.gicon.icon,
1709 MIN (width, height), flags);
1713 gboolean was_symbolic;
1715 style = gtk_widget_get_style (GTK_WIDGET (image));
1716 priv->data.gicon.pixbuf =
1717 gtk_icon_info_load_symbolic_for_style (info,
1721 priv->was_symbolic = was_symbolic;
1722 gtk_icon_info_free (info);
1725 if (priv->data.gicon.pixbuf == NULL)
1727 priv->data.gicon.pixbuf =
1728 gtk_widget_render_icon (GTK_WIDGET (image),
1729 GTK_STOCK_MISSING_IMAGE,
1732 priv->was_symbolic = FALSE;
1739 * Like gdk_rectangle_intersect (dest, src, dest), but make
1740 * sure that the origin of dest is moved by an "even" offset.
1741 * If necessary grow the intersection by one row or column
1744 * This is necessary since we can't pass alignment information
1745 * for the pixelation pattern down to gdk_pixbuf_saturate_and_pixelate(),
1746 * thus we have to makesure that the subimages are properly aligned.
1749 rectangle_intersect_even (GdkRectangle *src,
1757 isect = gdk_rectangle_intersect (dest, src, dest);
1759 if ((dest->x - x + dest->y - y) % 2 != 0)
1777 gtk_image_expose (GtkWidget *widget,
1778 GdkEventExpose *event)
1781 GtkImagePrivate *priv;
1783 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1784 g_return_val_if_fail (event != NULL, FALSE);
1786 image = GTK_IMAGE (widget);
1789 if (gtk_widget_get_mapped (widget) &&
1790 priv->storage_type != GTK_IMAGE_EMPTY)
1793 GdkRectangle area, image_bound;
1794 gint x, y, mask_x, mask_y;
1796 gfloat xalign, yalign;
1800 gboolean needs_state_transform;
1802 misc = GTK_MISC (widget);
1805 /* For stock items and icon sets, we lazily calculate
1806 * the size; we might get here between a queue_resize()
1807 * and size_request() if something explicitely forces
1810 if (priv->need_calc_size)
1811 gtk_image_calc_size (image);
1813 if (!gdk_rectangle_intersect (&area, &widget->allocation, &area))
1816 gtk_misc_get_alignment (misc, &xalign, &yalign);
1817 gtk_misc_get_padding (misc, &xpad, &ypad);
1819 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
1820 xalign = 1.0 - xalign;
1822 x = floor (widget->allocation.x + xpad
1823 + ((widget->allocation.width - widget->requisition.width) * xalign));
1824 y = floor (widget->allocation.y + ypad
1825 + ((widget->allocation.height - widget->requisition.height) * yalign));
1831 image_bound.width = 0;
1832 image_bound.height = 0;
1836 needs_state_transform = gtk_widget_get_state (widget) != GTK_STATE_NORMAL;
1838 switch (priv->storage_type)
1840 case GTK_IMAGE_PIXMAP:
1842 gdk_drawable_get_size (priv->data.pixmap.pixmap,
1844 &image_bound.height);
1845 if (rectangle_intersect_even (&area, &image_bound) &&
1846 needs_state_transform)
1848 pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1849 priv->data.pixmap.pixmap,
1850 gtk_widget_get_colormap (widget),
1851 image_bound.x - x, image_bound.y - y,
1854 image_bound.height);
1862 case GTK_IMAGE_PIXBUF:
1863 image_bound.width = gdk_pixbuf_get_width (priv->data.pixbuf.pixbuf);
1864 image_bound.height = gdk_pixbuf_get_height (priv->data.pixbuf.pixbuf);
1866 if (rectangle_intersect_even (&area, &image_bound) &&
1867 needs_state_transform)
1869 pixbuf = gdk_pixbuf_new_subpixbuf (priv->data.pixbuf.pixbuf,
1870 image_bound.x - x, image_bound.y - y,
1871 image_bound.width, image_bound.height);
1878 pixbuf = priv->data.pixbuf.pixbuf;
1879 g_object_ref (pixbuf);
1883 case GTK_IMAGE_STOCK:
1884 pixbuf = gtk_widget_render_icon (widget,
1885 priv->data.stock.stock_id,
1890 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1891 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1895 needs_state_transform = FALSE;
1898 case GTK_IMAGE_ICON_SET:
1900 gtk_icon_set_render_icon (priv->data.icon_set.icon_set,
1902 gtk_widget_get_direction (widget),
1903 gtk_widget_get_state (widget),
1910 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1911 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1915 needs_state_transform = FALSE;
1918 case GTK_IMAGE_ANIMATION:
1920 if (priv->data.anim.iter == NULL)
1922 priv->data.anim.iter = gdk_pixbuf_animation_get_iter (priv->data.anim.anim, NULL);
1924 if (gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter) >= 0)
1925 priv->data.anim.frame_timeout =
1926 gdk_threads_add_timeout (gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter),
1931 image_bound.width = gdk_pixbuf_animation_get_width (priv->data.anim.anim);
1932 image_bound.height = gdk_pixbuf_animation_get_height (priv->data.anim.anim);
1934 /* don't advance the anim iter here, or we could get frame changes between two
1935 * exposes of different areas.
1938 pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (priv->data.anim.iter);
1939 g_object_ref (pixbuf);
1943 case GTK_IMAGE_ICON_NAME:
1944 state = gtk_widget_get_state (widget);
1945 if (state == GTK_STATE_INSENSITIVE)
1947 ensure_pixbuf_for_icon_name (image, GTK_STATE_NORMAL);
1951 ensure_pixbuf_for_icon_name (image, state);
1952 /* Already done by the loading function? */
1953 if (priv->was_symbolic)
1954 needs_state_transform = FALSE;
1956 pixbuf = priv->data.name.pixbuf;
1959 g_object_ref (pixbuf);
1960 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1961 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1965 case GTK_IMAGE_GICON:
1966 state = gtk_widget_get_state (widget);
1967 if (state == GTK_STATE_INSENSITIVE)
1969 ensure_pixbuf_for_gicon (image, GTK_STATE_NORMAL);
1973 ensure_pixbuf_for_gicon (image, state);
1974 /* Already done by the loading function? */
1975 if (priv->was_symbolic)
1976 needs_state_transform = FALSE;
1978 pixbuf = priv->data.gicon.pixbuf;
1981 g_object_ref (pixbuf);
1982 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1983 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1987 case GTK_IMAGE_EMPTY:
1988 g_assert_not_reached ();
1992 if (rectangle_intersect_even (&area, &image_bound))
1996 if (needs_state_transform)
1998 GtkIconSource *source;
1999 GdkPixbuf *rendered;
2001 source = gtk_icon_source_new ();
2002 gtk_icon_source_set_pixbuf (source, pixbuf);
2003 /* The size here is arbitrary; since size isn't
2004 * wildcarded in the souce, it isn't supposed to be
2005 * scaled by the engine function
2007 gtk_icon_source_set_size (source,
2008 GTK_ICON_SIZE_SMALL_TOOLBAR);
2009 gtk_icon_source_set_size_wildcarded (source, FALSE);
2011 rendered = gtk_style_render_icon (widget->style,
2013 gtk_widget_get_direction (widget),
2014 gtk_widget_get_state (widget),
2020 gtk_icon_source_free (source);
2022 g_object_unref (pixbuf);
2028 cairo_t *cr = gdk_cairo_create (widget->window);
2029 gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
2030 gdk_cairo_rectangle (cr, &image_bound);
2038 cairo_pattern_t *mask_pattern;
2040 switch (priv->storage_type)
2042 case GTK_IMAGE_PIXMAP:
2043 cr = gdk_cairo_create (widget->window);
2047 /* hack to get the mask pattern */
2048 gdk_cairo_set_source_pixmap (cr, mask, mask_x, mask_y);
2049 mask_pattern = cairo_get_source (cr);
2050 cairo_pattern_reference (mask_pattern);
2052 gdk_cairo_set_source_pixmap (cr, priv->data.pixmap.pixmap, x, y);
2053 gdk_cairo_rectangle (cr, &image_bound);
2055 cairo_mask (cr, mask_pattern);
2059 gdk_cairo_set_source_pixmap (cr, priv->data.pixmap.pixmap, x, y);
2060 gdk_cairo_rectangle (cr, &image_bound);
2067 case GTK_IMAGE_PIXBUF:
2068 case GTK_IMAGE_STOCK:
2069 case GTK_IMAGE_ICON_SET:
2070 case GTK_IMAGE_ANIMATION:
2071 case GTK_IMAGE_ICON_NAME:
2072 case GTK_IMAGE_EMPTY:
2073 case GTK_IMAGE_GICON:
2074 g_assert_not_reached ();
2078 } /* if rectangle intersects */
2081 g_object_unref (pixbuf);
2083 } /* if widget is drawable */
2089 gtk_image_reset (GtkImage *image)
2091 GtkImagePrivate *priv = image->priv;
2093 g_object_freeze_notify (G_OBJECT (image));
2095 if (priv->storage_type != GTK_IMAGE_EMPTY)
2096 g_object_notify (G_OBJECT (image), "storage-type");
2100 g_object_unref (priv->mask);
2102 g_object_notify (G_OBJECT (image), "mask");
2105 if (priv->icon_size != DEFAULT_ICON_SIZE)
2107 priv->icon_size = DEFAULT_ICON_SIZE;
2108 g_object_notify (G_OBJECT (image), "icon-size");
2111 switch (priv->storage_type)
2113 case GTK_IMAGE_PIXMAP:
2115 if (priv->data.pixmap.pixmap)
2116 g_object_unref (priv->data.pixmap.pixmap);
2117 priv->data.pixmap.pixmap = NULL;
2119 g_object_notify (G_OBJECT (image), "pixmap");
2123 case GTK_IMAGE_PIXBUF:
2125 if (priv->data.pixbuf.pixbuf)
2126 g_object_unref (priv->data.pixbuf.pixbuf);
2128 g_object_notify (G_OBJECT (image), "pixbuf");
2132 case GTK_IMAGE_STOCK:
2134 g_free (priv->data.stock.stock_id);
2136 priv->data.stock.stock_id = NULL;
2138 g_object_notify (G_OBJECT (image), "stock");
2141 case GTK_IMAGE_ICON_SET:
2142 if (priv->data.icon_set.icon_set)
2143 gtk_icon_set_unref (priv->data.icon_set.icon_set);
2144 priv->data.icon_set.icon_set = NULL;
2146 g_object_notify (G_OBJECT (image), "icon-set");
2149 case GTK_IMAGE_ANIMATION:
2150 gtk_image_reset_anim_iter (image);
2152 if (priv->data.anim.anim)
2153 g_object_unref (priv->data.anim.anim);
2154 priv->data.anim.anim = NULL;
2156 g_object_notify (G_OBJECT (image), "pixbuf-animation");
2160 case GTK_IMAGE_ICON_NAME:
2161 g_free (priv->data.name.icon_name);
2162 priv->data.name.icon_name = NULL;
2163 if (priv->data.name.pixbuf)
2164 g_object_unref (priv->data.name.pixbuf);
2165 priv->data.name.pixbuf = NULL;
2167 g_object_notify (G_OBJECT (image), "icon-name");
2171 case GTK_IMAGE_GICON:
2172 if (priv->data.gicon.icon)
2173 g_object_unref (priv->data.gicon.icon);
2174 priv->data.gicon.icon = NULL;
2175 if (priv->data.gicon.pixbuf)
2176 g_object_unref (priv->data.gicon.pixbuf);
2177 priv->data.gicon.pixbuf = NULL;
2179 g_object_notify (G_OBJECT (image), "gicon");
2183 case GTK_IMAGE_EMPTY:
2191 g_free (priv->filename);
2192 priv->filename = NULL;
2193 g_object_notify (G_OBJECT (image), "file");
2196 priv->storage_type = GTK_IMAGE_EMPTY;
2198 memset (&priv->data, '\0', sizeof (priv->data));
2200 g_object_thaw_notify (G_OBJECT (image));
2205 * @image: a #GtkImage
2207 * Resets the image to be empty.
2212 gtk_image_clear (GtkImage *image)
2214 GtkImagePrivate *priv = image->priv;
2216 priv->need_calc_size = 1;
2218 gtk_image_reset (image);
2219 gtk_image_update_size (image, 0, 0);
2223 gtk_image_calc_size (GtkImage *image)
2225 GtkWidget *widget = GTK_WIDGET (image);
2226 GtkImagePrivate *priv = image->priv;
2227 GdkPixbuf *pixbuf = NULL;
2229 priv->need_calc_size = 0;
2231 /* We update stock/icon set on every size request, because
2232 * the theme could have affected the size; for other kinds of
2233 * image, we just update the requisition when the image data
2236 switch (priv->storage_type)
2238 case GTK_IMAGE_STOCK:
2239 pixbuf = gtk_widget_render_icon (widget,
2240 priv->data.stock.stock_id,
2245 case GTK_IMAGE_ICON_SET:
2246 pixbuf = gtk_icon_set_render_icon (priv->data.icon_set.icon_set,
2248 gtk_widget_get_direction (widget),
2249 gtk_widget_get_state (widget),
2254 case GTK_IMAGE_ICON_NAME:
2255 ensure_pixbuf_for_icon_name (image, GTK_STATE_NORMAL);
2256 pixbuf = priv->data.name.pixbuf;
2257 if (pixbuf) g_object_ref (pixbuf);
2259 case GTK_IMAGE_GICON:
2260 ensure_pixbuf_for_gicon (image, GTK_STATE_NORMAL);
2261 pixbuf = priv->data.gicon.pixbuf;
2263 g_object_ref (pixbuf);
2273 gtk_misc_get_padding (GTK_MISC (image), &xpad, &ypad);
2275 widget->requisition.width = gdk_pixbuf_get_width (pixbuf) + xpad * 2;
2276 widget->requisition.height = gdk_pixbuf_get_height (pixbuf) + ypad * 2;
2278 g_object_unref (pixbuf);
2283 gtk_image_size_request (GtkWidget *widget,
2284 GtkRequisition *requisition)
2288 image = GTK_IMAGE (widget);
2290 gtk_image_calc_size (image);
2292 /* Chain up to default that simply reads current requisition */
2293 GTK_WIDGET_CLASS (gtk_image_parent_class)->size_request (widget, requisition);
2297 gtk_image_style_set (GtkWidget *widget,
2298 GtkStyle *prev_style)
2302 image = GTK_IMAGE (widget);
2304 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_set (widget, prev_style);
2306 icon_theme_changed (image);
2310 gtk_image_screen_changed (GtkWidget *widget,
2311 GdkScreen *prev_screen)
2315 image = GTK_IMAGE (widget);
2317 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
2318 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
2320 icon_theme_changed (image);
2325 gtk_image_update_size (GtkImage *image,
2329 GtkWidget *widget = GTK_WIDGET (image);
2332 gtk_misc_get_padding (GTK_MISC (image), &xpad, &ypad);
2334 widget->requisition.width = image_width + xpad * 2;
2335 widget->requisition.height = image_height + ypad * 2;
2337 if (gtk_widget_get_visible (widget))
2338 gtk_widget_queue_resize (widget);
2343 * gtk_image_set_pixel_size:
2344 * @image: a #GtkImage
2345 * @pixel_size: the new pixel size
2347 * Sets the pixel size to use for named icons. If the pixel size is set
2348 * to a value != -1, it is used instead of the icon size set by
2349 * gtk_image_set_from_icon_name().
2354 gtk_image_set_pixel_size (GtkImage *image,
2357 GtkImagePrivate *priv;
2359 g_return_if_fail (GTK_IS_IMAGE (image));
2363 if (priv->pixel_size != pixel_size)
2365 priv->pixel_size = pixel_size;
2367 if (priv->storage_type == GTK_IMAGE_ICON_NAME)
2369 if (priv->data.name.pixbuf)
2371 g_object_unref (priv->data.name.pixbuf);
2372 priv->data.name.pixbuf = NULL;
2375 gtk_image_update_size (image, pixel_size, pixel_size);
2378 if (priv->storage_type == GTK_IMAGE_GICON)
2380 if (priv->data.gicon.pixbuf)
2382 g_object_unref (priv->data.gicon.pixbuf);
2383 priv->data.gicon.pixbuf = NULL;
2386 gtk_image_update_size (image, pixel_size, pixel_size);
2389 g_object_notify (G_OBJECT (image), "pixel-size");
2394 * gtk_image_get_pixel_size:
2395 * @image: a #GtkImage
2397 * Gets the pixel size used for named icons.
2399 * Returns: the pixel size used for named icons.
2404 gtk_image_get_pixel_size (GtkImage *image)
2406 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
2408 return image->priv->pixel_size;