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;
135 GdkBitmap *mask; /* Only used with GTK_IMAGE_PIXMAP, GTK_IMAGE_IMAGE */
139 GtkImagePixmapData pixmap;
140 GtkImagePixbufData pixbuf;
141 GtkImageStockData stock;
142 GtkImageIconSetData icon_set;
143 GtkImageAnimationData anim;
144 GtkImageIconNameData name;
145 GtkImageGIconData gicon;
148 gboolean was_symbolic;
149 gchar *filename; /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
150 gint last_rendered_state; /* a GtkStateType, with -1 meaning an invalid state,
151 * only used with GTK_IMAGE_GICON, GTK_IMAGE_ICON_NAME */
153 guint need_calc_size : 1;
155 gint required_height;
159 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
160 static gint gtk_image_expose (GtkWidget *widget,
161 GdkEventExpose *event);
162 static void gtk_image_unmap (GtkWidget *widget);
163 static void gtk_image_unrealize (GtkWidget *widget);
164 static void gtk_image_size_request (GtkWidget *widget,
165 GtkRequisition *requisition);
166 static void gtk_image_style_set (GtkWidget *widget,
167 GtkStyle *prev_style);
168 static void gtk_image_screen_changed (GtkWidget *widget,
169 GdkScreen *prev_screen);
170 static void gtk_image_destroy (GtkObject *object);
171 static void gtk_image_reset (GtkImage *image);
172 static void gtk_image_calc_size (GtkImage *image);
174 static void gtk_image_update_size (GtkImage *image,
178 static void gtk_image_set_property (GObject *object,
182 static void gtk_image_get_property (GObject *object,
187 static void icon_theme_changed (GtkImage *image);
200 PROP_PIXBUF_ANIMATION,
206 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
209 gtk_image_class_init (GtkImageClass *class)
211 GObjectClass *gobject_class;
212 GtkObjectClass *object_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 object_class = GTK_OBJECT_CLASS (class);
222 object_class->destroy = gtk_image_destroy;
224 widget_class = GTK_WIDGET_CLASS (class);
226 widget_class->expose_event = gtk_image_expose;
227 widget_class->size_request = gtk_image_size_request;
228 widget_class->unmap = gtk_image_unmap;
229 widget_class->unrealize = gtk_image_unrealize;
230 widget_class->style_set = gtk_image_style_set;
231 widget_class->screen_changed = gtk_image_screen_changed;
233 g_object_class_install_property (gobject_class,
235 g_param_spec_object ("pixbuf",
237 P_("A GdkPixbuf to display"),
239 GTK_PARAM_READWRITE));
241 g_object_class_install_property (gobject_class,
243 g_param_spec_object ("pixmap",
245 P_("A GdkPixmap to display"),
247 GTK_PARAM_READWRITE));
249 g_object_class_install_property (gobject_class,
251 g_param_spec_object ("mask",
253 P_("Mask bitmap to use with GdkPixmap"),
255 GTK_PARAM_READWRITE));
257 g_object_class_install_property (gobject_class,
259 g_param_spec_string ("file",
261 P_("Filename to load and display"),
263 GTK_PARAM_READWRITE));
266 g_object_class_install_property (gobject_class,
268 g_param_spec_string ("stock",
270 P_("Stock ID for a stock image to display"),
272 GTK_PARAM_READWRITE));
274 g_object_class_install_property (gobject_class,
276 g_param_spec_boxed ("icon-set",
278 P_("Icon set to display"),
280 GTK_PARAM_READWRITE));
282 g_object_class_install_property (gobject_class,
284 g_param_spec_int ("icon-size",
286 P_("Symbolic size to use for stock icon, icon set or named icon"),
289 GTK_PARAM_READWRITE));
291 * GtkImage:pixel-size:
293 * The "pixel-size" property can be used to specify a fixed size
294 * overriding the #GtkImage:icon-size property for images of type
295 * %GTK_IMAGE_ICON_NAME.
299 g_object_class_install_property (gobject_class,
301 g_param_spec_int ("pixel-size",
303 P_("Pixel size to use for named icon"),
306 GTK_PARAM_READWRITE));
308 g_object_class_install_property (gobject_class,
309 PROP_PIXBUF_ANIMATION,
310 g_param_spec_object ("pixbuf-animation",
312 P_("GdkPixbufAnimation to display"),
313 GDK_TYPE_PIXBUF_ANIMATION,
314 GTK_PARAM_READWRITE));
317 * GtkImage:icon-name:
319 * The name of the icon in the icon theme. If the icon theme is
320 * changed, the image will be updated automatically.
324 g_object_class_install_property (gobject_class,
326 g_param_spec_string ("icon-name",
328 P_("The name of the icon from the icon theme"),
330 GTK_PARAM_READWRITE));
335 * The GIcon displayed in the GtkImage. For themed icons,
336 * If the icon theme is changed, the image will be updated
341 g_object_class_install_property (gobject_class,
343 g_param_spec_object ("gicon",
345 P_("The GIcon being displayed"),
347 GTK_PARAM_READWRITE));
349 g_object_class_install_property (gobject_class,
351 g_param_spec_enum ("storage-type",
353 P_("The representation being used for image data"),
356 GTK_PARAM_READABLE));
358 g_type_class_add_private (object_class, sizeof (GtkImagePrivate));
362 gtk_image_init (GtkImage *image)
364 GtkImagePrivate *priv;
366 image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image,
371 gtk_widget_set_has_window (GTK_WIDGET (image), FALSE);
373 priv->storage_type = GTK_IMAGE_EMPTY;
374 priv->icon_size = DEFAULT_ICON_SIZE;
377 priv->pixel_size = -1;
379 priv->filename = NULL;
383 gtk_image_destroy (GtkObject *object)
385 GtkImage *image = GTK_IMAGE (object);
387 gtk_image_reset (image);
389 GTK_OBJECT_CLASS (gtk_image_parent_class)->destroy (object);
393 gtk_image_set_property (GObject *object,
398 GtkImage *image = GTK_IMAGE (object);
399 GtkImagePrivate *priv = image->priv;
404 gtk_image_set_from_pixbuf (image,
405 g_value_get_object (value));
408 gtk_image_set_from_pixmap (image,
409 g_value_get_object (value),
413 if (priv->storage_type == GTK_IMAGE_PIXMAP)
414 gtk_image_set_from_pixmap (image,
415 priv->data.pixmap.pixmap,
416 g_value_get_object (value));
421 mask = g_value_get_object (value);
426 gtk_image_clear (image);
432 gtk_image_set_from_file (image, g_value_get_string (value));
435 gtk_image_set_from_stock (image, g_value_get_string (value),
439 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
443 if (priv->storage_type == GTK_IMAGE_STOCK)
444 gtk_image_set_from_stock (image,
445 priv->data.stock.stock_id,
446 g_value_get_int (value));
447 else if (priv->storage_type == GTK_IMAGE_ICON_SET)
448 gtk_image_set_from_icon_set (image,
449 priv->data.icon_set.icon_set,
450 g_value_get_int (value));
451 else if (priv->storage_type == GTK_IMAGE_ICON_NAME)
452 gtk_image_set_from_icon_name (image,
453 priv->data.name.icon_name,
454 g_value_get_int (value));
455 else if (priv->storage_type == GTK_IMAGE_GICON)
456 gtk_image_set_from_gicon (image,
457 priv->data.gicon.icon,
458 g_value_get_int (value));
460 /* Save to be used when STOCK, ICON_SET, ICON_NAME or GICON property comes in */
461 priv->icon_size = g_value_get_int (value);
463 case PROP_PIXEL_SIZE:
464 gtk_image_set_pixel_size (image, g_value_get_int (value));
466 case PROP_PIXBUF_ANIMATION:
467 gtk_image_set_from_animation (image,
468 g_value_get_object (value));
471 gtk_image_set_from_icon_name (image, g_value_get_string (value),
475 gtk_image_set_from_gicon (image, g_value_get_object (value),
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486 gtk_image_get_property (GObject *object,
491 GtkImage *image = GTK_IMAGE (object);
492 GtkImagePrivate *priv = image->priv;
494 /* The "getter" functions whine if you try to get the wrong
495 * storage type. This function is instead robust against that,
496 * so that GUI builders don't have to jump through hoops
503 if (priv->storage_type != GTK_IMAGE_PIXBUF)
504 g_value_set_object (value, NULL);
506 g_value_set_object (value,
507 gtk_image_get_pixbuf (image));
510 if (priv->storage_type != GTK_IMAGE_PIXMAP)
511 g_value_set_object (value, NULL);
513 g_value_set_object (value,
514 priv->data.pixmap.pixmap);
517 g_value_set_object (value, priv->mask);
520 g_value_set_string (value, priv->filename);
523 if (priv->storage_type != GTK_IMAGE_STOCK)
524 g_value_set_string (value, NULL);
526 g_value_set_string (value,
527 priv->data.stock.stock_id);
530 if (priv->storage_type != GTK_IMAGE_ICON_SET)
531 g_value_set_boxed (value, NULL);
533 g_value_set_boxed (value,
534 priv->data.icon_set.icon_set);
537 g_value_set_int (value, priv->icon_size);
539 case PROP_PIXEL_SIZE:
540 g_value_set_int (value, priv->pixel_size);
542 case PROP_PIXBUF_ANIMATION:
543 if (priv->storage_type != GTK_IMAGE_ANIMATION)
544 g_value_set_object (value, NULL);
546 g_value_set_object (value,
547 priv->data.anim.anim);
550 if (priv->storage_type != GTK_IMAGE_ICON_NAME)
551 g_value_set_string (value, NULL);
553 g_value_set_string (value,
554 priv->data.name.icon_name);
557 if (priv->storage_type != GTK_IMAGE_GICON)
558 g_value_set_object (value, NULL);
560 g_value_set_object (value,
561 priv->data.gicon.icon);
563 case PROP_STORAGE_TYPE:
564 g_value_set_enum (value, priv->storage_type);
568 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
575 * gtk_image_new_from_pixmap:
576 * @pixmap: (allow-none): a #GdkPixmap, or %NULL
577 * @mask: (allow-none): a #GdkBitmap, or %NULL
579 * Creates a #GtkImage widget displaying @pixmap with a @mask.
580 * A #GdkPixmap is a server-side image buffer in the pixel format of the
581 * current display. The #GtkImage does not assume a reference to the
582 * pixmap or mask; you still need to unref them if you own references.
583 * #GtkImage will add its own reference rather than adopting yours.
585 * Return value: a new #GtkImage
588 gtk_image_new_from_pixmap (GdkPixmap *pixmap,
593 image = g_object_new (GTK_TYPE_IMAGE, NULL);
595 gtk_image_set_from_pixmap (image, pixmap, mask);
597 return GTK_WIDGET (image);
601 * gtk_image_new_from_file:
602 * @filename: a filename
604 * Creates a new #GtkImage displaying the file @filename. If the file
605 * isn't found or can't be loaded, the resulting #GtkImage will
606 * display a "broken image" icon. This function never returns %NULL,
607 * it always returns a valid #GtkImage widget.
609 * If the file contains an animation, the image will contain an
612 * If you need to detect failures to load the file, use
613 * gdk_pixbuf_new_from_file() to load the file yourself, then create
614 * the #GtkImage from the pixbuf. (Or for animations, use
615 * gdk_pixbuf_animation_new_from_file()).
617 * The storage type (gtk_image_get_storage_type()) of the returned
618 * image is not defined, it will be whatever is appropriate for
619 * displaying the file.
621 * Return value: a new #GtkImage
624 gtk_image_new_from_file (const gchar *filename)
628 image = g_object_new (GTK_TYPE_IMAGE, NULL);
630 gtk_image_set_from_file (image, filename);
632 return GTK_WIDGET (image);
636 * gtk_image_new_from_pixbuf:
637 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
639 * Creates a new #GtkImage displaying @pixbuf.
640 * The #GtkImage does not assume a reference to the
641 * pixbuf; you still need to unref it if you own references.
642 * #GtkImage will add its own reference rather than adopting yours.
644 * Note that this function just creates an #GtkImage from the pixbuf. The
645 * #GtkImage created will not react to state changes. Should you want that,
646 * you should use gtk_image_new_from_icon_set().
648 * Return value: a new #GtkImage
651 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
655 image = g_object_new (GTK_TYPE_IMAGE, NULL);
657 gtk_image_set_from_pixbuf (image, pixbuf);
659 return GTK_WIDGET (image);
663 * gtk_image_new_from_stock:
664 * @stock_id: a stock icon name
665 * @size: (type int): a stock icon size
667 * Creates a #GtkImage displaying a stock icon. Sample stock icon
668 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
669 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
670 * icon name isn't known, the image will be empty.
671 * You can register your own stock icon names, see
672 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
674 * Return value: a new #GtkImage displaying the stock icon
677 gtk_image_new_from_stock (const gchar *stock_id,
682 image = g_object_new (GTK_TYPE_IMAGE, NULL);
684 gtk_image_set_from_stock (image, stock_id, size);
686 return GTK_WIDGET (image);
690 * gtk_image_new_from_icon_set:
691 * @icon_set: a #GtkIconSet
692 * @size: (type int): a stock icon size
694 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
695 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
696 * this function, usually it's better to create a #GtkIconFactory, put
697 * your icon sets in the icon factory, add the icon factory to the
698 * list of default factories with gtk_icon_factory_add_default(), and
699 * then use gtk_image_new_from_stock(). This will allow themes to
700 * override the icon you ship with your application.
702 * The #GtkImage does not assume a reference to the
703 * icon set; you still need to unref it if you own references.
704 * #GtkImage will add its own reference rather than adopting yours.
706 * Return value: a new #GtkImage
709 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
714 image = g_object_new (GTK_TYPE_IMAGE, NULL);
716 gtk_image_set_from_icon_set (image, icon_set, size);
718 return GTK_WIDGET (image);
722 * gtk_image_new_from_animation:
723 * @animation: an animation
725 * Creates a #GtkImage displaying the given animation.
726 * The #GtkImage does not assume a reference to the
727 * animation; you still need to unref it if you own references.
728 * #GtkImage will add its own reference rather than adopting yours.
730 * Note that the animation frames are shown using a timeout with
731 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
732 * keep in mind that the animation will only be shown if the main loop
733 * is not busy with something that has a higher priority.
735 * Return value: a new #GtkImage widget
738 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
742 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
744 image = g_object_new (GTK_TYPE_IMAGE, NULL);
746 gtk_image_set_from_animation (image, animation);
748 return GTK_WIDGET (image);
752 * gtk_image_new_from_icon_name:
753 * @icon_name: an icon name
754 * @size: (type int): a stock icon size
756 * Creates a #GtkImage displaying an icon from the current icon theme.
757 * If the icon name isn't known, a "broken image" icon will be
758 * displayed instead. If the current icon theme is changed, the icon
759 * will be updated appropriately.
761 * Return value: a new #GtkImage displaying the themed icon
766 gtk_image_new_from_icon_name (const gchar *icon_name,
771 image = g_object_new (GTK_TYPE_IMAGE, NULL);
773 gtk_image_set_from_icon_name (image, icon_name, size);
775 return GTK_WIDGET (image);
779 * gtk_image_new_from_gicon:
781 * @size: (type int): a stock icon size
783 * Creates a #GtkImage displaying an icon from the current icon theme.
784 * If the icon name isn't known, a "broken image" icon will be
785 * displayed instead. If the current icon theme is changed, the icon
786 * will be updated appropriately.
788 * Return value: a new #GtkImage displaying the themed icon
793 gtk_image_new_from_gicon (GIcon *icon,
798 image = g_object_new (GTK_TYPE_IMAGE, NULL);
800 gtk_image_set_from_gicon (image, icon, size);
802 return GTK_WIDGET (image);
806 * gtk_image_set_from_pixmap:
807 * @image: a #GtkImage
808 * @pixmap: (allow-none): a #GdkPixmap or %NULL
809 * @mask: (allow-none): a #GdkBitmap or %NULL
811 * See gtk_image_new_from_pixmap() for details.
814 gtk_image_set_from_pixmap (GtkImage *image,
818 GtkImagePrivate *priv;
820 g_return_if_fail (GTK_IS_IMAGE (image));
821 g_return_if_fail (pixmap == NULL ||
822 GDK_IS_PIXMAP (pixmap));
823 g_return_if_fail (mask == NULL ||
824 GDK_IS_PIXMAP (mask));
828 g_object_freeze_notify (G_OBJECT (image));
831 g_object_ref (pixmap);
836 gtk_image_clear (image);
845 priv->storage_type = GTK_IMAGE_PIXMAP;
847 priv->data.pixmap.pixmap = pixmap;
849 gdk_drawable_get_size (GDK_DRAWABLE (pixmap), &width, &height);
851 gtk_image_update_size (image, width, height);
854 g_object_notify (G_OBJECT (image), "pixmap");
855 g_object_notify (G_OBJECT (image), "mask");
857 g_object_thaw_notify (G_OBJECT (image));
861 * gtk_image_set_from_file:
862 * @image: a #GtkImage
863 * @filename: (allow-none): a filename or %NULL
865 * See gtk_image_new_from_file() for details.
868 gtk_image_set_from_file (GtkImage *image,
869 const gchar *filename)
871 GtkImagePrivate *priv;
872 GdkPixbufAnimation *anim;
874 g_return_if_fail (GTK_IS_IMAGE (image));
878 g_object_freeze_notify (G_OBJECT (image));
880 gtk_image_clear (image);
882 if (filename == NULL)
884 priv->filename = NULL;
885 g_object_thaw_notify (G_OBJECT (image));
889 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
893 gtk_image_set_from_stock (image,
894 GTK_STOCK_MISSING_IMAGE,
895 GTK_ICON_SIZE_BUTTON);
896 g_object_thaw_notify (G_OBJECT (image));
900 /* We could just unconditionally set_from_animation,
901 * but it's nicer for memory if we toss the animation
902 * if it's just a single pixbuf
905 if (gdk_pixbuf_animation_is_static_image (anim))
906 gtk_image_set_from_pixbuf (image,
907 gdk_pixbuf_animation_get_static_image (anim));
909 gtk_image_set_from_animation (image, anim);
911 g_object_unref (anim);
913 priv->filename = g_strdup (filename);
915 g_object_thaw_notify (G_OBJECT (image));
919 * gtk_image_set_from_pixbuf:
920 * @image: a #GtkImage
921 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
923 * See gtk_image_new_from_pixbuf() for details.
926 gtk_image_set_from_pixbuf (GtkImage *image,
929 GtkImagePrivate *priv;
931 g_return_if_fail (GTK_IS_IMAGE (image));
932 g_return_if_fail (pixbuf == NULL ||
933 GDK_IS_PIXBUF (pixbuf));
937 g_object_freeze_notify (G_OBJECT (image));
940 g_object_ref (pixbuf);
942 gtk_image_clear (image);
946 priv->storage_type = GTK_IMAGE_PIXBUF;
948 priv->data.pixbuf.pixbuf = pixbuf;
950 gtk_image_update_size (image,
951 gdk_pixbuf_get_width (pixbuf),
952 gdk_pixbuf_get_height (pixbuf));
955 g_object_notify (G_OBJECT (image), "pixbuf");
957 g_object_thaw_notify (G_OBJECT (image));
961 * gtk_image_set_from_stock:
962 * @image: a #GtkImage
963 * @stock_id: a stock icon name
964 * @size: (type int): a stock icon size
966 * See gtk_image_new_from_stock() for details.
969 gtk_image_set_from_stock (GtkImage *image,
970 const gchar *stock_id,
973 GtkImagePrivate *priv;
976 g_return_if_fail (GTK_IS_IMAGE (image));
980 g_object_freeze_notify (G_OBJECT (image));
982 /* in case stock_id == priv->data.stock.stock_id */
983 new_id = g_strdup (stock_id);
985 gtk_image_clear (image);
989 priv->storage_type = GTK_IMAGE_STOCK;
991 priv->data.stock.stock_id = new_id;
992 priv->icon_size = size;
994 /* Size is demand-computed in size request method
995 * if we're a stock image, since changing the
996 * style impacts the size request
1000 g_object_notify (G_OBJECT (image), "stock");
1001 g_object_notify (G_OBJECT (image), "icon-size");
1003 g_object_thaw_notify (G_OBJECT (image));
1007 * gtk_image_set_from_icon_set:
1008 * @image: a #GtkImage
1009 * @icon_set: a #GtkIconSet
1010 * @size: (type int): a stock icon size
1012 * See gtk_image_new_from_icon_set() for details.
1015 gtk_image_set_from_icon_set (GtkImage *image,
1016 GtkIconSet *icon_set,
1019 GtkImagePrivate *priv;
1021 g_return_if_fail (GTK_IS_IMAGE (image));
1025 g_object_freeze_notify (G_OBJECT (image));
1028 gtk_icon_set_ref (icon_set);
1030 gtk_image_clear (image);
1034 priv->storage_type = GTK_IMAGE_ICON_SET;
1036 priv->data.icon_set.icon_set = icon_set;
1037 priv->icon_size = size;
1039 /* Size is demand-computed in size request method
1040 * if we're an icon set
1044 g_object_notify (G_OBJECT (image), "icon-set");
1045 g_object_notify (G_OBJECT (image), "icon-size");
1047 g_object_thaw_notify (G_OBJECT (image));
1051 * gtk_image_set_from_animation:
1052 * @image: a #GtkImage
1053 * @animation: the #GdkPixbufAnimation
1055 * Causes the #GtkImage to display the given animation (or display
1056 * nothing, if you set the animation to %NULL).
1059 gtk_image_set_from_animation (GtkImage *image,
1060 GdkPixbufAnimation *animation)
1062 GtkImagePrivate *priv;
1064 g_return_if_fail (GTK_IS_IMAGE (image));
1065 g_return_if_fail (animation == NULL ||
1066 GDK_IS_PIXBUF_ANIMATION (animation));
1070 g_object_freeze_notify (G_OBJECT (image));
1073 g_object_ref (animation);
1075 gtk_image_clear (image);
1077 if (animation != NULL)
1079 priv->storage_type = GTK_IMAGE_ANIMATION;
1081 priv->data.anim.anim = animation;
1082 priv->data.anim.frame_timeout = 0;
1083 priv->data.anim.iter = NULL;
1085 gtk_image_update_size (image,
1086 gdk_pixbuf_animation_get_width (animation),
1087 gdk_pixbuf_animation_get_height (animation));
1090 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1092 g_object_thaw_notify (G_OBJECT (image));
1096 * gtk_image_set_from_icon_name:
1097 * @image: a #GtkImage
1098 * @icon_name: an icon name
1099 * @size: (type int): an icon size
1101 * See gtk_image_new_from_icon_name() for details.
1106 gtk_image_set_from_icon_name (GtkImage *image,
1107 const gchar *icon_name,
1111 GtkImagePrivate *priv;
1113 g_return_if_fail (GTK_IS_IMAGE (image));
1117 g_object_freeze_notify (G_OBJECT (image));
1119 /* in case icon_name == priv->data.name.icon_name */
1120 new_name = g_strdup (icon_name);
1122 gtk_image_clear (image);
1126 priv->storage_type = GTK_IMAGE_ICON_NAME;
1128 priv->data.name.icon_name = new_name;
1129 priv->icon_size = size;
1131 /* Size is demand-computed in size request method
1132 * if we're a icon theme image, since changing the
1133 * style impacts the size request
1137 g_object_notify (G_OBJECT (image), "icon-name");
1138 g_object_notify (G_OBJECT (image), "icon-size");
1140 g_object_thaw_notify (G_OBJECT (image));
1144 * gtk_image_set_from_gicon:
1145 * @image: a #GtkImage
1147 * @size: (type int): an icon size
1149 * See gtk_image_new_from_gicon() for details.
1154 gtk_image_set_from_gicon (GtkImage *image,
1158 GtkImagePrivate *priv;
1160 g_return_if_fail (GTK_IS_IMAGE (image));
1164 g_object_freeze_notify (G_OBJECT (image));
1166 /* in case icon == priv->data.gicon.icon */
1168 g_object_ref (icon);
1170 gtk_image_clear (image);
1174 priv->storage_type = GTK_IMAGE_GICON;
1176 priv->data.gicon.icon = icon;
1177 priv->icon_size = size;
1179 /* Size is demand-computed in size request method
1180 * if we're a icon theme image, since changing the
1181 * style impacts the size request
1185 g_object_notify (G_OBJECT (image), "gicon");
1186 g_object_notify (G_OBJECT (image), "icon-size");
1188 g_object_thaw_notify (G_OBJECT (image));
1192 * gtk_image_get_storage_type:
1193 * @image: a #GtkImage
1195 * Gets the type of representation being used by the #GtkImage
1196 * to store image data. If the #GtkImage has no image data,
1197 * the return value will be %GTK_IMAGE_EMPTY.
1199 * Return value: image representation being used
1202 gtk_image_get_storage_type (GtkImage *image)
1204 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1206 return image->priv->storage_type;
1210 * gtk_image_get_pixmap:
1211 * @image: a #GtkImage
1212 * @pixmap: (out) (transfer none) (allow-none): location to store the
1214 * @mask: (out) (transfer none) (allow-none): location to store the
1217 * Gets the pixmap and mask being displayed by the #GtkImage.
1218 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1219 * %GTK_IMAGE_PIXMAP (see gtk_image_get_storage_type()).
1220 * The caller of this function does not own a reference to the
1221 * returned pixmap and mask.
1224 gtk_image_get_pixmap (GtkImage *image,
1228 GtkImagePrivate *priv;
1230 g_return_if_fail (GTK_IS_IMAGE (image));
1234 g_return_if_fail (priv->storage_type == GTK_IMAGE_PIXMAP ||
1235 priv->storage_type == GTK_IMAGE_EMPTY);
1238 *pixmap = priv->data.pixmap.pixmap;
1245 * gtk_image_get_pixbuf:
1246 * @image: a #GtkImage
1248 * Gets the #GdkPixbuf being displayed by the #GtkImage.
1249 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1250 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1251 * The caller of this function does not own a reference to the
1254 * Return value: (transfer none): the displayed pixbuf, or %NULL if
1255 * the image is empty
1258 gtk_image_get_pixbuf (GtkImage *image)
1260 GtkImagePrivate *priv;
1262 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1266 g_return_val_if_fail (priv->storage_type == GTK_IMAGE_PIXBUF ||
1267 priv->storage_type == GTK_IMAGE_EMPTY, NULL);
1269 if (priv->storage_type == GTK_IMAGE_EMPTY)
1270 priv->data.pixbuf.pixbuf = NULL;
1272 return priv->data.pixbuf.pixbuf;
1276 * gtk_image_get_stock:
1277 * @image: a #GtkImage
1278 * @stock_id: (out) (transfer none) (allow-none): place to store a
1279 * stock icon name, or %NULL
1280 * @size: (out) (allow-none) (type int): place to store a stock icon
1283 * Gets the stock icon name and size being displayed by the #GtkImage.
1284 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1285 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1286 * The returned string is owned by the #GtkImage and should not
1290 gtk_image_get_stock (GtkImage *image,
1294 GtkImagePrivate *priv;
1296 g_return_if_fail (GTK_IS_IMAGE (image));
1300 g_return_if_fail (priv->storage_type == GTK_IMAGE_STOCK ||
1301 priv->storage_type == GTK_IMAGE_EMPTY);
1303 if (priv->storage_type == GTK_IMAGE_EMPTY)
1304 priv->data.stock.stock_id = NULL;
1307 *stock_id = priv->data.stock.stock_id;
1310 *size = priv->icon_size;
1314 * gtk_image_get_icon_set:
1315 * @image: a #GtkImage
1316 * @icon_set: (out) (transfer none) (allow-none): location to store a
1317 * #GtkIconSet, or %NULL
1318 * @size: (out) (allow-none) (type int): location to store a stock
1319 * icon size, or %NULL
1321 * Gets the icon set and size being displayed by the #GtkImage.
1322 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1323 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1326 gtk_image_get_icon_set (GtkImage *image,
1327 GtkIconSet **icon_set,
1330 GtkImagePrivate *priv;
1332 g_return_if_fail (GTK_IS_IMAGE (image));
1336 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_SET ||
1337 priv->storage_type == GTK_IMAGE_EMPTY);
1340 *icon_set = priv->data.icon_set.icon_set;
1343 *size = priv->icon_size;
1347 * gtk_image_get_animation:
1348 * @image: a #GtkImage
1350 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1351 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1352 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1353 * The caller of this function does not own a reference to the
1354 * returned animation.
1356 * Return value: (transfer none): the displayed animation, or %NULL if
1357 * the image is empty
1360 gtk_image_get_animation (GtkImage *image)
1362 GtkImagePrivate *priv;
1364 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1368 g_return_val_if_fail (priv->storage_type == GTK_IMAGE_ANIMATION ||
1369 priv->storage_type == GTK_IMAGE_EMPTY,
1372 if (priv->storage_type == GTK_IMAGE_EMPTY)
1373 priv->data.anim.anim = NULL;
1375 return priv->data.anim.anim;
1379 * gtk_image_get_icon_name:
1380 * @image: a #GtkImage
1381 * @icon_name: (out) (transfer none) (allow-none): place to store an
1382 * icon name, or %NULL
1383 * @size: (out) (allow-none) (type int): place to store an icon size,
1386 * Gets the icon name and size being displayed by the #GtkImage.
1387 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1388 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1389 * The returned string is owned by the #GtkImage and should not
1395 gtk_image_get_icon_name (GtkImage *image,
1396 G_CONST_RETURN gchar **icon_name,
1399 GtkImagePrivate *priv;
1401 g_return_if_fail (GTK_IS_IMAGE (image));
1405 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME ||
1406 priv->storage_type == GTK_IMAGE_EMPTY);
1408 if (priv->storage_type == GTK_IMAGE_EMPTY)
1409 priv->data.name.icon_name = NULL;
1412 *icon_name = priv->data.name.icon_name;
1415 *size = priv->icon_size;
1419 * gtk_image_get_gicon:
1420 * @image: a #GtkImage
1421 * @gicon: (out) (transfer none) (allow-none): place to store a
1423 * @size: (out) (allow-none) (type int): place to store an icon size,
1426 * Gets the #GIcon and size being displayed by the #GtkImage.
1427 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1428 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1429 * The caller of this function does not own a reference to the
1435 gtk_image_get_gicon (GtkImage *image,
1439 GtkImagePrivate *priv;
1441 g_return_if_fail (GTK_IS_IMAGE (image));
1445 g_return_if_fail (priv->storage_type == GTK_IMAGE_GICON ||
1446 priv->storage_type == GTK_IMAGE_EMPTY);
1448 if (priv->storage_type == GTK_IMAGE_EMPTY)
1449 priv->data.gicon.icon = NULL;
1452 *gicon = priv->data.gicon.icon;
1455 *size = priv->icon_size;
1461 * Creates a new empty #GtkImage widget.
1463 * Return value: a newly created #GtkImage widget.
1466 gtk_image_new (void)
1468 return g_object_new (GTK_TYPE_IMAGE, NULL);
1472 gtk_image_reset_anim_iter (GtkImage *image)
1474 GtkImagePrivate *priv = image->priv;
1476 if (priv->storage_type == GTK_IMAGE_ANIMATION)
1478 /* Reset the animation */
1480 if (priv->data.anim.frame_timeout)
1482 g_source_remove (priv->data.anim.frame_timeout);
1483 priv->data.anim.frame_timeout = 0;
1486 if (priv->data.anim.iter)
1488 g_object_unref (priv->data.anim.iter);
1489 priv->data.anim.iter = NULL;
1495 gtk_image_unmap (GtkWidget *widget)
1497 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1499 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1503 gtk_image_unrealize (GtkWidget *widget)
1505 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1507 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1511 animation_timeout (gpointer data)
1513 GtkImage *image = GTK_IMAGE (data);
1514 GtkImagePrivate *priv = image->priv;
1517 priv->data.anim.frame_timeout = 0;
1519 gdk_pixbuf_animation_iter_advance (priv->data.anim.iter, NULL);
1521 delay = gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter);
1524 GtkWidget *widget = GTK_WIDGET (image);
1526 priv->data.anim.frame_timeout =
1527 gdk_threads_add_timeout (delay, animation_timeout, image);
1529 gtk_widget_queue_draw (widget);
1531 if (gtk_widget_is_drawable (widget))
1532 gdk_window_process_updates (gtk_widget_get_window (widget), TRUE);
1539 icon_theme_changed (GtkImage *image)
1541 GtkImagePrivate *priv = image->priv;
1543 if (priv->storage_type == GTK_IMAGE_ICON_NAME)
1545 if (priv->data.name.pixbuf)
1546 g_object_unref (priv->data.name.pixbuf);
1547 priv->data.name.pixbuf = NULL;
1549 gtk_widget_queue_draw (GTK_WIDGET (image));
1551 if (priv->storage_type == GTK_IMAGE_GICON)
1553 if (priv->data.gicon.pixbuf)
1554 g_object_unref (priv->data.gicon.pixbuf);
1555 priv->data.gicon.pixbuf = NULL;
1557 gtk_widget_queue_draw (GTK_WIDGET (image));
1562 ensure_pixbuf_for_icon_name (GtkImage *image,
1565 GtkImagePrivate *priv = image->priv;
1567 GtkIconTheme *icon_theme;
1568 GtkSettings *settings;
1570 gint *sizes, *s, dist;
1572 GtkIconLookupFlags flags;
1574 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME);
1576 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1577 icon_theme = gtk_icon_theme_get_for_screen (screen);
1578 settings = gtk_settings_get_for_screen (screen);
1579 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1580 if (priv->data.name.pixbuf == NULL ||
1581 (priv->was_symbolic && priv->last_rendered_state != state))
1583 priv->last_rendered_state = state;
1584 if (priv->data.name.pixbuf)
1586 g_object_unref (priv->data.name.pixbuf);
1587 priv->data.name.pixbuf = NULL;
1589 if (priv->pixel_size != -1)
1591 width = height = priv->pixel_size;
1592 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1594 else if (!gtk_icon_size_lookup_for_settings (settings,
1598 if (priv->icon_size == -1)
1600 /* Find an available size close to 48 */
1601 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, priv->data.name.icon_name);
1603 width = height = 48;
1604 for (s = sizes; *s; s++)
1608 width = height = 48;
1615 width = height = *s;
1623 width = height = *s;
1632 g_warning ("Invalid icon size %d\n", priv->icon_size);
1633 width = height = 24;
1637 info = gtk_icon_theme_lookup_icon (icon_theme,
1638 priv->data.name.icon_name,
1639 MIN (width, height), flags);
1643 gboolean was_symbolic;
1645 style = gtk_widget_get_style (GTK_WIDGET (image));
1646 priv->data.name.pixbuf =
1647 gtk_icon_info_load_symbolic_for_style (info,
1651 priv->was_symbolic = was_symbolic;
1652 gtk_icon_info_free (info);
1655 if (priv->data.name.pixbuf == NULL)
1657 priv->data.name.pixbuf =
1658 gtk_widget_render_icon (GTK_WIDGET (image),
1659 GTK_STOCK_MISSING_IMAGE,
1662 priv->was_symbolic = FALSE;
1668 ensure_pixbuf_for_gicon (GtkImage *image,
1671 GtkImagePrivate *priv = image->priv;
1673 GtkIconTheme *icon_theme;
1674 GtkSettings *settings;
1677 GtkIconLookupFlags flags;
1679 g_return_if_fail (priv->storage_type == GTK_IMAGE_GICON);
1681 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1682 icon_theme = gtk_icon_theme_get_for_screen (screen);
1683 settings = gtk_settings_get_for_screen (screen);
1684 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1685 if (priv->data.gicon.pixbuf == NULL ||
1686 (priv->was_symbolic && priv->last_rendered_state != state))
1688 priv->last_rendered_state = state;
1689 if (priv->data.gicon.pixbuf)
1691 g_object_unref (priv->data.gicon.pixbuf);
1692 priv->data.gicon.pixbuf = NULL;
1694 if (priv->pixel_size != -1)
1696 width = height = priv->pixel_size;
1697 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1699 else if (!gtk_icon_size_lookup_for_settings (settings,
1703 if (priv->icon_size == -1)
1704 width = height = 48;
1707 g_warning ("Invalid icon size %d\n", priv->icon_size);
1708 width = height = 24;
1712 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1713 priv->data.gicon.icon,
1714 MIN (width, height), flags);
1718 gboolean was_symbolic;
1720 style = gtk_widget_get_style (GTK_WIDGET (image));
1721 priv->data.gicon.pixbuf =
1722 gtk_icon_info_load_symbolic_for_style (info,
1726 priv->was_symbolic = was_symbolic;
1727 gtk_icon_info_free (info);
1730 if (priv->data.gicon.pixbuf == NULL)
1732 priv->data.gicon.pixbuf =
1733 gtk_widget_render_icon (GTK_WIDGET (image),
1734 GTK_STOCK_MISSING_IMAGE,
1737 priv->was_symbolic = FALSE;
1744 * Like gdk_rectangle_intersect (dest, src, dest), but make
1745 * sure that the origin of dest is moved by an "even" offset.
1746 * If necessary grow the intersection by one row or column
1749 * This is necessary since we can't pass alignment information
1750 * for the pixelation pattern down to gdk_pixbuf_saturate_and_pixelate(),
1751 * thus we have to makesure that the subimages are properly aligned.
1754 rectangle_intersect_even (GdkRectangle *src,
1762 isect = gdk_rectangle_intersect (dest, src, dest);
1764 if ((dest->x - x + dest->y - y) % 2 != 0)
1782 gtk_image_expose (GtkWidget *widget,
1783 GdkEventExpose *event)
1786 GtkImagePrivate *priv;
1788 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1789 g_return_val_if_fail (event != NULL, FALSE);
1791 image = GTK_IMAGE (widget);
1794 if (gtk_widget_get_mapped (widget) &&
1795 priv->storage_type != GTK_IMAGE_EMPTY)
1797 GtkAllocation allocation;
1799 GdkRectangle area, image_bound;
1800 gint x, y, mask_x, mask_y;
1802 gfloat xalign, yalign;
1806 gboolean needs_state_transform;
1808 misc = GTK_MISC (widget);
1811 /* For stock items and icon sets, we lazily calculate
1812 * the size; we might get here between a queue_resize()
1813 * and size_request() if something explicitely forces
1816 if (priv->need_calc_size)
1817 gtk_image_calc_size (image);
1819 gtk_widget_get_allocation (widget, &allocation);
1821 if (!gdk_rectangle_intersect (&area, &allocation, &area))
1824 gtk_misc_get_alignment (misc, &xalign, &yalign);
1825 gtk_misc_get_padding (misc, &xpad, &ypad);
1827 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
1828 xalign = 1.0 - xalign;
1830 x = floor (allocation.x + xpad + ((allocation.width - priv->required_width) * xalign));
1831 y = floor (allocation.y + ypad + ((allocation.height - priv->required_height) * yalign));
1837 image_bound.width = 0;
1838 image_bound.height = 0;
1842 needs_state_transform = gtk_widget_get_state (widget) != GTK_STATE_NORMAL;
1844 switch (priv->storage_type)
1846 case GTK_IMAGE_PIXMAP:
1848 gdk_drawable_get_size (priv->data.pixmap.pixmap,
1850 &image_bound.height);
1851 if (rectangle_intersect_even (&area, &image_bound) &&
1852 needs_state_transform)
1854 pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1855 priv->data.pixmap.pixmap,
1856 gtk_widget_get_colormap (widget),
1857 image_bound.x - x, image_bound.y - y,
1860 image_bound.height);
1868 case GTK_IMAGE_PIXBUF:
1869 image_bound.width = gdk_pixbuf_get_width (priv->data.pixbuf.pixbuf);
1870 image_bound.height = gdk_pixbuf_get_height (priv->data.pixbuf.pixbuf);
1872 if (rectangle_intersect_even (&area, &image_bound) &&
1873 needs_state_transform)
1875 pixbuf = gdk_pixbuf_new_subpixbuf (priv->data.pixbuf.pixbuf,
1876 image_bound.x - x, image_bound.y - y,
1877 image_bound.width, image_bound.height);
1884 pixbuf = priv->data.pixbuf.pixbuf;
1885 g_object_ref (pixbuf);
1889 case GTK_IMAGE_STOCK:
1890 pixbuf = gtk_widget_render_icon (widget,
1891 priv->data.stock.stock_id,
1896 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1897 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1901 needs_state_transform = FALSE;
1904 case GTK_IMAGE_ICON_SET:
1906 gtk_icon_set_render_icon (priv->data.icon_set.icon_set,
1907 gtk_widget_get_style (widget),
1908 gtk_widget_get_direction (widget),
1909 gtk_widget_get_state (widget),
1916 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1917 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1921 needs_state_transform = FALSE;
1924 case GTK_IMAGE_ANIMATION:
1926 if (priv->data.anim.iter == NULL)
1928 priv->data.anim.iter = gdk_pixbuf_animation_get_iter (priv->data.anim.anim, NULL);
1930 if (gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter) >= 0)
1931 priv->data.anim.frame_timeout =
1932 gdk_threads_add_timeout (gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter),
1937 image_bound.width = gdk_pixbuf_animation_get_width (priv->data.anim.anim);
1938 image_bound.height = gdk_pixbuf_animation_get_height (priv->data.anim.anim);
1940 /* don't advance the anim iter here, or we could get frame changes between two
1941 * exposes of different areas.
1944 pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (priv->data.anim.iter);
1945 g_object_ref (pixbuf);
1949 case GTK_IMAGE_ICON_NAME:
1950 state = gtk_widget_get_state (widget);
1951 if (state == GTK_STATE_INSENSITIVE)
1953 ensure_pixbuf_for_icon_name (image, GTK_STATE_NORMAL);
1957 ensure_pixbuf_for_icon_name (image, state);
1958 /* Already done by the loading function? */
1959 if (priv->was_symbolic)
1960 needs_state_transform = FALSE;
1962 pixbuf = priv->data.name.pixbuf;
1965 g_object_ref (pixbuf);
1966 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1967 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1971 case GTK_IMAGE_GICON:
1972 state = gtk_widget_get_state (widget);
1973 if (state == GTK_STATE_INSENSITIVE)
1975 ensure_pixbuf_for_gicon (image, GTK_STATE_NORMAL);
1979 ensure_pixbuf_for_gicon (image, state);
1980 /* Already done by the loading function? */
1981 if (priv->was_symbolic)
1982 needs_state_transform = FALSE;
1984 pixbuf = priv->data.gicon.pixbuf;
1987 g_object_ref (pixbuf);
1988 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1989 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1993 case GTK_IMAGE_EMPTY:
1994 g_assert_not_reached ();
1998 if (rectangle_intersect_even (&area, &image_bound))
2002 if (needs_state_transform)
2004 GtkIconSource *source;
2005 GdkPixbuf *rendered;
2007 source = gtk_icon_source_new ();
2008 gtk_icon_source_set_pixbuf (source, pixbuf);
2009 /* The size here is arbitrary; since size isn't
2010 * wildcarded in the souce, it isn't supposed to be
2011 * scaled by the engine function
2013 gtk_icon_source_set_size (source,
2014 GTK_ICON_SIZE_SMALL_TOOLBAR);
2015 gtk_icon_source_set_size_wildcarded (source, FALSE);
2017 rendered = gtk_style_render_icon (gtk_widget_get_style (widget),
2019 gtk_widget_get_direction (widget),
2020 gtk_widget_get_state (widget),
2026 gtk_icon_source_free (source);
2028 g_object_unref (pixbuf);
2034 cairo_t *cr = gdk_cairo_create (gtk_widget_get_window (widget));
2035 gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
2036 gdk_cairo_rectangle (cr, &image_bound);
2044 cairo_pattern_t *mask_pattern;
2046 switch (priv->storage_type)
2048 case GTK_IMAGE_PIXMAP:
2049 cr = gdk_cairo_create (gtk_widget_get_window (widget));
2053 /* hack to get the mask pattern */
2054 gdk_cairo_set_source_pixmap (cr, mask, mask_x, mask_y);
2055 mask_pattern = cairo_get_source (cr);
2056 cairo_pattern_reference (mask_pattern);
2058 gdk_cairo_set_source_pixmap (cr, priv->data.pixmap.pixmap, x, y);
2059 gdk_cairo_rectangle (cr, &image_bound);
2061 cairo_mask (cr, mask_pattern);
2065 gdk_cairo_set_source_pixmap (cr, priv->data.pixmap.pixmap, x, y);
2066 gdk_cairo_rectangle (cr, &image_bound);
2072 case GTK_IMAGE_PIXBUF:
2073 case GTK_IMAGE_STOCK:
2074 case GTK_IMAGE_ICON_SET:
2075 case GTK_IMAGE_ANIMATION:
2076 case GTK_IMAGE_ICON_NAME:
2077 case GTK_IMAGE_EMPTY:
2078 case GTK_IMAGE_GICON:
2079 g_assert_not_reached ();
2083 } /* if rectangle intersects */
2086 g_object_unref (pixbuf);
2088 } /* if widget is drawable */
2094 gtk_image_reset (GtkImage *image)
2096 GtkImagePrivate *priv = image->priv;
2098 g_object_freeze_notify (G_OBJECT (image));
2100 if (priv->storage_type != GTK_IMAGE_EMPTY)
2101 g_object_notify (G_OBJECT (image), "storage-type");
2105 g_object_unref (priv->mask);
2107 g_object_notify (G_OBJECT (image), "mask");
2110 if (priv->icon_size != DEFAULT_ICON_SIZE)
2112 priv->icon_size = DEFAULT_ICON_SIZE;
2113 g_object_notify (G_OBJECT (image), "icon-size");
2116 switch (priv->storage_type)
2118 case GTK_IMAGE_PIXMAP:
2120 if (priv->data.pixmap.pixmap)
2121 g_object_unref (priv->data.pixmap.pixmap);
2122 priv->data.pixmap.pixmap = NULL;
2124 g_object_notify (G_OBJECT (image), "pixmap");
2128 case GTK_IMAGE_PIXBUF:
2130 if (priv->data.pixbuf.pixbuf)
2131 g_object_unref (priv->data.pixbuf.pixbuf);
2133 g_object_notify (G_OBJECT (image), "pixbuf");
2137 case GTK_IMAGE_STOCK:
2139 g_free (priv->data.stock.stock_id);
2141 priv->data.stock.stock_id = NULL;
2143 g_object_notify (G_OBJECT (image), "stock");
2146 case GTK_IMAGE_ICON_SET:
2147 if (priv->data.icon_set.icon_set)
2148 gtk_icon_set_unref (priv->data.icon_set.icon_set);
2149 priv->data.icon_set.icon_set = NULL;
2151 g_object_notify (G_OBJECT (image), "icon-set");
2154 case GTK_IMAGE_ANIMATION:
2155 gtk_image_reset_anim_iter (image);
2157 if (priv->data.anim.anim)
2158 g_object_unref (priv->data.anim.anim);
2159 priv->data.anim.anim = NULL;
2161 g_object_notify (G_OBJECT (image), "pixbuf-animation");
2165 case GTK_IMAGE_ICON_NAME:
2166 g_free (priv->data.name.icon_name);
2167 priv->data.name.icon_name = NULL;
2168 if (priv->data.name.pixbuf)
2169 g_object_unref (priv->data.name.pixbuf);
2170 priv->data.name.pixbuf = NULL;
2172 g_object_notify (G_OBJECT (image), "icon-name");
2176 case GTK_IMAGE_GICON:
2177 if (priv->data.gicon.icon)
2178 g_object_unref (priv->data.gicon.icon);
2179 priv->data.gicon.icon = NULL;
2180 if (priv->data.gicon.pixbuf)
2181 g_object_unref (priv->data.gicon.pixbuf);
2182 priv->data.gicon.pixbuf = NULL;
2184 g_object_notify (G_OBJECT (image), "gicon");
2188 case GTK_IMAGE_EMPTY:
2196 g_free (priv->filename);
2197 priv->filename = NULL;
2198 g_object_notify (G_OBJECT (image), "file");
2201 priv->storage_type = GTK_IMAGE_EMPTY;
2203 memset (&priv->data, '\0', sizeof (priv->data));
2205 g_object_thaw_notify (G_OBJECT (image));
2210 * @image: a #GtkImage
2212 * Resets the image to be empty.
2217 gtk_image_clear (GtkImage *image)
2219 GtkImagePrivate *priv = image->priv;
2221 priv->need_calc_size = 1;
2223 gtk_image_reset (image);
2224 gtk_image_update_size (image, 0, 0);
2228 gtk_image_calc_size (GtkImage *image)
2230 GtkWidget *widget = GTK_WIDGET (image);
2231 GtkImagePrivate *priv = image->priv;
2232 GdkPixbuf *pixbuf = NULL;
2234 priv->need_calc_size = 0;
2236 /* We update stock/icon set on every size request, because
2237 * the theme could have affected the size; for other kinds of
2238 * image, we just update the required width/height when the image data
2241 switch (priv->storage_type)
2243 case GTK_IMAGE_STOCK:
2244 pixbuf = gtk_widget_render_icon (widget,
2245 priv->data.stock.stock_id,
2250 case GTK_IMAGE_ICON_SET:
2251 pixbuf = gtk_icon_set_render_icon (priv->data.icon_set.icon_set,
2252 gtk_widget_get_style (widget),
2253 gtk_widget_get_direction (widget),
2254 gtk_widget_get_state (widget),
2259 case GTK_IMAGE_ICON_NAME:
2260 ensure_pixbuf_for_icon_name (image, GTK_STATE_NORMAL);
2261 pixbuf = priv->data.name.pixbuf;
2262 if (pixbuf) g_object_ref (pixbuf);
2264 case GTK_IMAGE_GICON:
2265 ensure_pixbuf_for_gicon (image, GTK_STATE_NORMAL);
2266 pixbuf = priv->data.gicon.pixbuf;
2268 g_object_ref (pixbuf);
2278 gtk_misc_get_padding (GTK_MISC (image), &xpad, &ypad);
2280 priv->required_width = gdk_pixbuf_get_width (pixbuf) + xpad * 2;
2281 priv->required_height = gdk_pixbuf_get_height (pixbuf) + ypad * 2;
2283 g_object_unref (pixbuf);
2288 gtk_image_size_request (GtkWidget *widget,
2289 GtkRequisition *requisition)
2292 GtkImagePrivate *priv;
2294 image = GTK_IMAGE (widget);
2297 gtk_image_calc_size (image);
2299 requisition->width = priv->required_width;
2300 requisition->height = priv->required_height;
2304 gtk_image_style_set (GtkWidget *widget,
2305 GtkStyle *prev_style)
2309 image = GTK_IMAGE (widget);
2311 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_set (widget, prev_style);
2313 icon_theme_changed (image);
2317 gtk_image_screen_changed (GtkWidget *widget,
2318 GdkScreen *prev_screen)
2322 image = GTK_IMAGE (widget);
2324 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
2325 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
2327 icon_theme_changed (image);
2332 gtk_image_update_size (GtkImage *image,
2336 GtkWidget *widget = GTK_WIDGET (image);
2337 GtkImagePrivate *priv = image->priv;
2340 gtk_misc_get_padding (GTK_MISC (image), &xpad, &ypad);
2342 priv->required_width = image_width + xpad * 2;
2343 priv->required_height = image_height + ypad * 2;
2345 if (gtk_widget_get_visible (widget))
2346 gtk_widget_queue_resize (widget);
2351 * gtk_image_set_pixel_size:
2352 * @image: a #GtkImage
2353 * @pixel_size: the new pixel size
2355 * Sets the pixel size to use for named icons. If the pixel size is set
2356 * to a value != -1, it is used instead of the icon size set by
2357 * gtk_image_set_from_icon_name().
2362 gtk_image_set_pixel_size (GtkImage *image,
2365 GtkImagePrivate *priv;
2367 g_return_if_fail (GTK_IS_IMAGE (image));
2371 if (priv->pixel_size != pixel_size)
2373 priv->pixel_size = pixel_size;
2375 if (priv->storage_type == GTK_IMAGE_ICON_NAME)
2377 if (priv->data.name.pixbuf)
2379 g_object_unref (priv->data.name.pixbuf);
2380 priv->data.name.pixbuf = NULL;
2383 gtk_image_update_size (image, pixel_size, pixel_size);
2386 if (priv->storage_type == GTK_IMAGE_GICON)
2388 if (priv->data.gicon.pixbuf)
2390 g_object_unref (priv->data.gicon.pixbuf);
2391 priv->data.gicon.pixbuf = NULL;
2394 gtk_image_update_size (image, pixel_size, pixel_size);
2397 g_object_notify (G_OBJECT (image), "pixel-size");
2402 * gtk_image_get_pixel_size:
2403 * @image: a #GtkImage
2405 * Gets the pixel size used for named icons.
2407 * Returns: the pixel size used for named icons.
2412 gtk_image_get_pixel_size (GtkImage *image)
2414 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
2416 return image->priv->pixel_size;