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"
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().
130 typedef struct _GtkImagePrivate GtkImagePrivate;
132 struct _GtkImagePrivate
134 /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
138 guint need_calc_size : 1;
141 #define GTK_IMAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_IMAGE, GtkImagePrivate))
144 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
145 static gint gtk_image_expose (GtkWidget *widget,
146 GdkEventExpose *event);
147 static void gtk_image_unmap (GtkWidget *widget);
148 static void gtk_image_unrealize (GtkWidget *widget);
149 static void gtk_image_size_request (GtkWidget *widget,
150 GtkRequisition *requisition);
151 static void gtk_image_style_set (GtkWidget *widget,
152 GtkStyle *prev_style);
153 static void gtk_image_screen_changed (GtkWidget *widget,
154 GdkScreen *prev_screen);
155 static void gtk_image_destroy (GtkObject *object);
156 static void gtk_image_reset (GtkImage *image);
157 static void gtk_image_calc_size (GtkImage *image);
159 static void gtk_image_update_size (GtkImage *image,
163 static void gtk_image_set_property (GObject *object,
167 static void gtk_image_get_property (GObject *object,
172 static void icon_theme_changed (GtkImage *image);
186 PROP_PIXBUF_ANIMATION,
192 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
195 gtk_image_class_init (GtkImageClass *class)
197 GObjectClass *gobject_class;
198 GtkObjectClass *object_class;
199 GtkWidgetClass *widget_class;
201 gobject_class = G_OBJECT_CLASS (class);
203 gobject_class->set_property = gtk_image_set_property;
204 gobject_class->get_property = gtk_image_get_property;
206 object_class = GTK_OBJECT_CLASS (class);
208 object_class->destroy = gtk_image_destroy;
210 widget_class = GTK_WIDGET_CLASS (class);
212 widget_class->expose_event = gtk_image_expose;
213 widget_class->size_request = gtk_image_size_request;
214 widget_class->unmap = gtk_image_unmap;
215 widget_class->unrealize = gtk_image_unrealize;
216 widget_class->style_set = gtk_image_style_set;
217 widget_class->screen_changed = gtk_image_screen_changed;
219 g_object_class_install_property (gobject_class,
221 g_param_spec_object ("pixbuf",
223 P_("A GdkPixbuf to display"),
225 GTK_PARAM_READWRITE));
227 g_object_class_install_property (gobject_class,
229 g_param_spec_object ("pixmap",
231 P_("A GdkPixmap to display"),
233 GTK_PARAM_READWRITE));
235 g_object_class_install_property (gobject_class,
237 g_param_spec_object ("image",
239 P_("A GdkImage to display"),
241 GTK_PARAM_READWRITE));
243 g_object_class_install_property (gobject_class,
245 g_param_spec_object ("mask",
247 P_("Mask bitmap to use with GdkImage or GdkPixmap"),
249 GTK_PARAM_READWRITE));
251 g_object_class_install_property (gobject_class,
253 g_param_spec_string ("file",
255 P_("Filename to load and display"),
257 GTK_PARAM_READWRITE));
260 g_object_class_install_property (gobject_class,
262 g_param_spec_string ("stock",
264 P_("Stock ID for a stock image to display"),
266 GTK_PARAM_READWRITE));
268 g_object_class_install_property (gobject_class,
270 g_param_spec_boxed ("icon-set",
272 P_("Icon set to display"),
274 GTK_PARAM_READWRITE));
276 g_object_class_install_property (gobject_class,
278 g_param_spec_int ("icon-size",
280 P_("Symbolic size to use for stock icon, icon set or named icon"),
283 GTK_PARAM_READWRITE));
285 * GtkImage:pixel-size:
287 * The "pixel-size" property can be used to specify a fixed size
288 * overriding the #GtkImage:icon-size property for images of type
289 * %GTK_IMAGE_ICON_NAME.
293 g_object_class_install_property (gobject_class,
295 g_param_spec_int ("pixel-size",
297 P_("Pixel size to use for named icon"),
300 GTK_PARAM_READWRITE));
302 g_object_class_install_property (gobject_class,
303 PROP_PIXBUF_ANIMATION,
304 g_param_spec_object ("pixbuf-animation",
306 P_("GdkPixbufAnimation to display"),
307 GDK_TYPE_PIXBUF_ANIMATION,
308 GTK_PARAM_READWRITE));
311 * GtkImage:icon-name:
313 * The name of the icon in the icon theme. If the icon theme is
314 * changed, the image will be updated automatically.
318 g_object_class_install_property (gobject_class,
320 g_param_spec_string ("icon-name",
322 P_("The name of the icon from the icon theme"),
324 GTK_PARAM_READWRITE));
329 * The GIcon displayed in the GtkImage. For themed icons,
330 * If the icon theme is changed, the image will be updated
335 g_object_class_install_property (gobject_class,
337 g_param_spec_object ("gicon",
339 P_("The GIcon being displayed"),
341 GTK_PARAM_READWRITE));
343 g_object_class_install_property (gobject_class,
345 g_param_spec_enum ("storage-type",
347 P_("The representation being used for image data"),
350 GTK_PARAM_READABLE));
352 g_type_class_add_private (object_class, sizeof (GtkImagePrivate));
356 gtk_image_init (GtkImage *image)
358 GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);
360 GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);
362 image->storage_type = GTK_IMAGE_EMPTY;
363 image->icon_size = DEFAULT_ICON_SIZE;
366 priv->pixel_size = -1;
368 priv->filename = NULL;
372 gtk_image_destroy (GtkObject *object)
374 GtkImage *image = GTK_IMAGE (object);
376 gtk_image_reset (image);
378 GTK_OBJECT_CLASS (gtk_image_parent_class)->destroy (object);
382 gtk_image_set_property (GObject *object,
389 image = GTK_IMAGE (object);
394 gtk_image_set_from_pixbuf (image,
395 g_value_get_object (value));
398 gtk_image_set_from_pixmap (image,
399 g_value_get_object (value),
403 gtk_image_set_from_image (image,
404 g_value_get_object (value),
408 if (image->storage_type == GTK_IMAGE_PIXMAP)
409 gtk_image_set_from_pixmap (image,
410 image->data.pixmap.pixmap,
411 g_value_get_object (value));
412 else if (image->storage_type == GTK_IMAGE_IMAGE)
413 gtk_image_set_from_image (image,
414 image->data.image.image,
415 g_value_get_object (value));
420 mask = g_value_get_object (value);
425 gtk_image_clear (image);
431 gtk_image_set_from_file (image, g_value_get_string (value));
434 gtk_image_set_from_stock (image, g_value_get_string (value),
438 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
442 if (image->storage_type == GTK_IMAGE_STOCK)
443 gtk_image_set_from_stock (image,
444 image->data.stock.stock_id,
445 g_value_get_int (value));
446 else if (image->storage_type == GTK_IMAGE_ICON_SET)
447 gtk_image_set_from_icon_set (image,
448 image->data.icon_set.icon_set,
449 g_value_get_int (value));
450 else if (image->storage_type == GTK_IMAGE_ICON_NAME)
451 gtk_image_set_from_icon_name (image,
452 image->data.name.icon_name,
453 g_value_get_int (value));
454 else if (image->storage_type == GTK_IMAGE_GICON)
455 gtk_image_set_from_gicon (image,
456 image->data.gicon.icon,
457 g_value_get_int (value));
459 /* Save to be used when STOCK, ICON_SET, ICON_NAME or GICON property comes in */
460 image->icon_size = g_value_get_int (value);
462 case PROP_PIXEL_SIZE:
463 gtk_image_set_pixel_size (image, g_value_get_int (value));
465 case PROP_PIXBUF_ANIMATION:
466 gtk_image_set_from_animation (image,
467 g_value_get_object (value));
470 gtk_image_set_from_icon_name (image, g_value_get_string (value),
474 gtk_image_set_from_gicon (image, g_value_get_object (value),
479 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
485 gtk_image_get_property (GObject *object,
491 GtkImagePrivate *priv;
493 image = GTK_IMAGE (object);
494 priv = GTK_IMAGE_GET_PRIVATE (image);
496 /* The "getter" functions whine if you try to get the wrong
497 * storage type. This function is instead robust against that,
498 * so that GUI builders don't have to jump through hoops
505 if (image->storage_type != GTK_IMAGE_PIXBUF)
506 g_value_set_object (value, NULL);
508 g_value_set_object (value,
509 gtk_image_get_pixbuf (image));
512 if (image->storage_type != GTK_IMAGE_PIXMAP)
513 g_value_set_object (value, NULL);
515 g_value_set_object (value,
516 image->data.pixmap.pixmap);
519 g_value_set_object (value, image->mask);
522 if (image->storage_type != GTK_IMAGE_IMAGE)
523 g_value_set_object (value, NULL);
525 g_value_set_object (value,
526 image->data.image.image);
529 g_value_set_string (value, priv->filename);
532 if (image->storage_type != GTK_IMAGE_STOCK)
533 g_value_set_string (value, NULL);
535 g_value_set_string (value,
536 image->data.stock.stock_id);
539 if (image->storage_type != GTK_IMAGE_ICON_SET)
540 g_value_set_boxed (value, NULL);
542 g_value_set_boxed (value,
543 image->data.icon_set.icon_set);
546 g_value_set_int (value, image->icon_size);
548 case PROP_PIXEL_SIZE:
549 g_value_set_int (value, priv->pixel_size);
551 case PROP_PIXBUF_ANIMATION:
552 if (image->storage_type != GTK_IMAGE_ANIMATION)
553 g_value_set_object (value, NULL);
555 g_value_set_object (value,
556 image->data.anim.anim);
559 if (image->storage_type != GTK_IMAGE_ICON_NAME)
560 g_value_set_string (value, NULL);
562 g_value_set_string (value,
563 image->data.name.icon_name);
566 if (image->storage_type != GTK_IMAGE_GICON)
567 g_value_set_object (value, NULL);
569 g_value_set_object (value,
570 image->data.gicon.icon);
572 case PROP_STORAGE_TYPE:
573 g_value_set_enum (value, image->storage_type);
577 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
584 * gtk_image_new_from_pixmap:
585 * @pixmap: (allow-none): a #GdkPixmap, or %NULL
586 * @mask: (allow-none): a #GdkBitmap, or %NULL
588 * Creates a #GtkImage widget displaying @pixmap with a @mask.
589 * A #GdkPixmap is a server-side image buffer in the pixel format of the
590 * current display. The #GtkImage does not assume a reference to the
591 * pixmap or mask; you still need to unref them if you own references.
592 * #GtkImage will add its own reference rather than adopting yours.
594 * Return value: a new #GtkImage
597 gtk_image_new_from_pixmap (GdkPixmap *pixmap,
602 image = g_object_new (GTK_TYPE_IMAGE, NULL);
604 gtk_image_set_from_pixmap (image, pixmap, mask);
606 return GTK_WIDGET (image);
610 * gtk_image_new_from_image:
611 * @image: (allow-none): a #GdkImage, or %NULL
612 * @mask: (allow-none): a #GdkBitmap, or %NULL
614 * Creates a #GtkImage widget displaying a @image with a @mask.
615 * A #GdkImage is a client-side image buffer in the pixel format of the
616 * current display. The #GtkImage does not assume a reference to the
617 * image or mask; you still need to unref them if you own references.
618 * #GtkImage will add its own reference rather than adopting yours.
620 * Return value: a new #GtkImage
623 gtk_image_new_from_image (GdkImage *gdk_image,
628 image = g_object_new (GTK_TYPE_IMAGE, NULL);
630 gtk_image_set_from_image (image, gdk_image, mask);
632 return GTK_WIDGET (image);
636 * gtk_image_new_from_file:
637 * @filename: a filename
639 * Creates a new #GtkImage displaying the file @filename. If the file
640 * isn't found or can't be loaded, the resulting #GtkImage will
641 * display a "broken image" icon. This function never returns %NULL,
642 * it always returns a valid #GtkImage widget.
644 * If the file contains an animation, the image will contain an
647 * If you need to detect failures to load the file, use
648 * gdk_pixbuf_new_from_file() to load the file yourself, then create
649 * the #GtkImage from the pixbuf. (Or for animations, use
650 * gdk_pixbuf_animation_new_from_file()).
652 * The storage type (gtk_image_get_storage_type()) of the returned
653 * image is not defined, it will be whatever is appropriate for
654 * displaying the file.
656 * Return value: a new #GtkImage
659 gtk_image_new_from_file (const gchar *filename)
663 image = g_object_new (GTK_TYPE_IMAGE, NULL);
665 gtk_image_set_from_file (image, filename);
667 return GTK_WIDGET (image);
671 * gtk_image_new_from_pixbuf:
672 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
674 * Creates a new #GtkImage displaying @pixbuf.
675 * The #GtkImage does not assume a reference to the
676 * pixbuf; you still need to unref it if you own references.
677 * #GtkImage will add its own reference rather than adopting yours.
679 * Note that this function just creates an #GtkImage from the pixbuf. The
680 * #GtkImage created will not react to state changes. Should you want that,
681 * you should use gtk_image_new_from_icon_set().
683 * Return value: a new #GtkImage
686 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
690 image = g_object_new (GTK_TYPE_IMAGE, NULL);
692 gtk_image_set_from_pixbuf (image, pixbuf);
694 return GTK_WIDGET (image);
698 * gtk_image_new_from_stock:
699 * @stock_id: a stock icon name
700 * @size: (type int): a stock icon size
702 * Creates a #GtkImage displaying a stock icon. Sample stock icon
703 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
704 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
705 * icon name isn't known, the image will be empty.
706 * You can register your own stock icon names, see
707 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
709 * Return value: a new #GtkImage displaying the stock icon
712 gtk_image_new_from_stock (const gchar *stock_id,
717 image = g_object_new (GTK_TYPE_IMAGE, NULL);
719 gtk_image_set_from_stock (image, stock_id, size);
721 return GTK_WIDGET (image);
725 * gtk_image_new_from_icon_set:
726 * @icon_set: a #GtkIconSet
727 * @size: (type int): a stock icon size
729 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
730 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
731 * this function, usually it's better to create a #GtkIconFactory, put
732 * your icon sets in the icon factory, add the icon factory to the
733 * list of default factories with gtk_icon_factory_add_default(), and
734 * then use gtk_image_new_from_stock(). This will allow themes to
735 * override the icon you ship with your application.
737 * The #GtkImage does not assume a reference to the
738 * icon set; you still need to unref it if you own references.
739 * #GtkImage will add its own reference rather than adopting yours.
741 * Return value: a new #GtkImage
744 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
749 image = g_object_new (GTK_TYPE_IMAGE, NULL);
751 gtk_image_set_from_icon_set (image, icon_set, size);
753 return GTK_WIDGET (image);
757 * gtk_image_new_from_animation:
758 * @animation: an animation
760 * Creates a #GtkImage displaying the given animation.
761 * The #GtkImage does not assume a reference to the
762 * animation; you still need to unref it if you own references.
763 * #GtkImage will add its own reference rather than adopting yours.
765 * Note that the animation frames are shown using a timeout with
766 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
767 * keep in mind that the animation will only be shown if the main loop
768 * is not busy with something that has a higher priority.
770 * Return value: a new #GtkImage widget
773 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
777 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
779 image = g_object_new (GTK_TYPE_IMAGE, NULL);
781 gtk_image_set_from_animation (image, animation);
783 return GTK_WIDGET (image);
787 * gtk_image_new_from_icon_name:
788 * @icon_name: an icon name
789 * @size: (type int): a stock icon size
791 * Creates a #GtkImage displaying an icon from the current icon theme.
792 * If the icon name isn't known, a "broken image" icon will be
793 * displayed instead. If the current icon theme is changed, the icon
794 * will be updated appropriately.
796 * Return value: a new #GtkImage displaying the themed icon
801 gtk_image_new_from_icon_name (const gchar *icon_name,
806 image = g_object_new (GTK_TYPE_IMAGE, NULL);
808 gtk_image_set_from_icon_name (image, icon_name, size);
810 return GTK_WIDGET (image);
814 * gtk_image_new_from_gicon:
816 * @size: (type int): a stock icon size
818 * Creates a #GtkImage displaying an icon from the current icon theme.
819 * If the icon name isn't known, a "broken image" icon will be
820 * displayed instead. If the current icon theme is changed, the icon
821 * will be updated appropriately.
823 * Return value: a new #GtkImage displaying the themed icon
828 gtk_image_new_from_gicon (GIcon *icon,
833 image = g_object_new (GTK_TYPE_IMAGE, NULL);
835 gtk_image_set_from_gicon (image, icon, size);
837 return GTK_WIDGET (image);
841 * gtk_image_set_from_pixmap:
842 * @image: a #GtkImage
843 * @pixmap: (allow-none): a #GdkPixmap or %NULL
844 * @mask: (allow-none): a #GdkBitmap or %NULL
846 * See gtk_image_new_from_pixmap() for details.
849 gtk_image_set_from_pixmap (GtkImage *image,
853 g_return_if_fail (GTK_IS_IMAGE (image));
854 g_return_if_fail (pixmap == NULL ||
855 GDK_IS_PIXMAP (pixmap));
856 g_return_if_fail (mask == NULL ||
857 GDK_IS_PIXMAP (mask));
859 g_object_freeze_notify (G_OBJECT (image));
862 g_object_ref (pixmap);
867 gtk_image_clear (image);
876 image->storage_type = GTK_IMAGE_PIXMAP;
878 image->data.pixmap.pixmap = pixmap;
880 gdk_drawable_get_size (GDK_DRAWABLE (pixmap), &width, &height);
882 gtk_image_update_size (image, width, height);
885 g_object_notify (G_OBJECT (image), "pixmap");
886 g_object_notify (G_OBJECT (image), "mask");
888 g_object_thaw_notify (G_OBJECT (image));
892 * gtk_image_set_from_image:
893 * @image: a #GtkImage
894 * @gdk_image: (allow-none): a #GdkImage or %NULL
895 * @mask: (allow-none): a #GdkBitmap or %NULL
897 * See gtk_image_new_from_image() for details.
900 gtk_image_set_from_image (GtkImage *image,
904 g_return_if_fail (GTK_IS_IMAGE (image));
905 g_return_if_fail (gdk_image == NULL ||
906 GDK_IS_IMAGE (gdk_image));
907 g_return_if_fail (mask == NULL ||
908 GDK_IS_PIXMAP (mask));
910 g_object_freeze_notify (G_OBJECT (image));
913 g_object_ref (gdk_image);
918 gtk_image_clear (image);
922 image->storage_type = GTK_IMAGE_IMAGE;
924 image->data.image.image = gdk_image;
927 gtk_image_update_size (image, gdk_image->width, gdk_image->height);
931 /* Clean up the mask if gdk_image was NULL */
933 g_object_unref (mask);
936 g_object_notify (G_OBJECT (image), "image");
937 g_object_notify (G_OBJECT (image), "mask");
939 g_object_thaw_notify (G_OBJECT (image));
943 * gtk_image_set_from_file:
944 * @image: a #GtkImage
945 * @filename: (allow-none): a filename or %NULL
947 * See gtk_image_new_from_file() for details.
950 gtk_image_set_from_file (GtkImage *image,
951 const gchar *filename)
953 GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);
954 GdkPixbufAnimation *anim;
956 g_return_if_fail (GTK_IS_IMAGE (image));
958 g_object_freeze_notify (G_OBJECT (image));
960 gtk_image_clear (image);
962 if (filename == NULL)
964 priv->filename = NULL;
965 g_object_thaw_notify (G_OBJECT (image));
969 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
973 gtk_image_set_from_stock (image,
974 GTK_STOCK_MISSING_IMAGE,
975 GTK_ICON_SIZE_BUTTON);
976 g_object_thaw_notify (G_OBJECT (image));
980 /* We could just unconditionally set_from_animation,
981 * but it's nicer for memory if we toss the animation
982 * if it's just a single pixbuf
985 if (gdk_pixbuf_animation_is_static_image (anim))
986 gtk_image_set_from_pixbuf (image,
987 gdk_pixbuf_animation_get_static_image (anim));
989 gtk_image_set_from_animation (image, anim);
991 g_object_unref (anim);
993 priv->filename = g_strdup (filename);
995 g_object_thaw_notify (G_OBJECT (image));
999 * gtk_image_set_from_pixbuf:
1000 * @image: a #GtkImage
1001 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
1003 * See gtk_image_new_from_pixbuf() for details.
1006 gtk_image_set_from_pixbuf (GtkImage *image,
1009 g_return_if_fail (GTK_IS_IMAGE (image));
1010 g_return_if_fail (pixbuf == NULL ||
1011 GDK_IS_PIXBUF (pixbuf));
1013 g_object_freeze_notify (G_OBJECT (image));
1016 g_object_ref (pixbuf);
1018 gtk_image_clear (image);
1022 image->storage_type = GTK_IMAGE_PIXBUF;
1024 image->data.pixbuf.pixbuf = pixbuf;
1026 gtk_image_update_size (image,
1027 gdk_pixbuf_get_width (pixbuf),
1028 gdk_pixbuf_get_height (pixbuf));
1031 g_object_notify (G_OBJECT (image), "pixbuf");
1033 g_object_thaw_notify (G_OBJECT (image));
1037 * gtk_image_set_from_stock:
1038 * @image: a #GtkImage
1039 * @stock_id: a stock icon name
1040 * @size: (type int): a stock icon size
1042 * See gtk_image_new_from_stock() for details.
1045 gtk_image_set_from_stock (GtkImage *image,
1046 const gchar *stock_id,
1051 g_return_if_fail (GTK_IS_IMAGE (image));
1053 g_object_freeze_notify (G_OBJECT (image));
1055 /* in case stock_id == image->data.stock.stock_id */
1056 new_id = g_strdup (stock_id);
1058 gtk_image_clear (image);
1062 image->storage_type = GTK_IMAGE_STOCK;
1064 image->data.stock.stock_id = new_id;
1065 image->icon_size = size;
1067 /* Size is demand-computed in size request method
1068 * if we're a stock image, since changing the
1069 * style impacts the size request
1073 g_object_notify (G_OBJECT (image), "stock");
1074 g_object_notify (G_OBJECT (image), "icon-size");
1076 g_object_thaw_notify (G_OBJECT (image));
1080 * gtk_image_set_from_icon_set:
1081 * @image: a #GtkImage
1082 * @icon_set: a #GtkIconSet
1083 * @size: (type int): a stock icon size
1085 * See gtk_image_new_from_icon_set() for details.
1088 gtk_image_set_from_icon_set (GtkImage *image,
1089 GtkIconSet *icon_set,
1092 g_return_if_fail (GTK_IS_IMAGE (image));
1094 g_object_freeze_notify (G_OBJECT (image));
1097 gtk_icon_set_ref (icon_set);
1099 gtk_image_clear (image);
1103 image->storage_type = GTK_IMAGE_ICON_SET;
1105 image->data.icon_set.icon_set = icon_set;
1106 image->icon_size = size;
1108 /* Size is demand-computed in size request method
1109 * if we're an icon set
1113 g_object_notify (G_OBJECT (image), "icon-set");
1114 g_object_notify (G_OBJECT (image), "icon-size");
1116 g_object_thaw_notify (G_OBJECT (image));
1120 * gtk_image_set_from_animation:
1121 * @image: a #GtkImage
1122 * @animation: the #GdkPixbufAnimation
1124 * Causes the #GtkImage to display the given animation (or display
1125 * nothing, if you set the animation to %NULL).
1128 gtk_image_set_from_animation (GtkImage *image,
1129 GdkPixbufAnimation *animation)
1131 g_return_if_fail (GTK_IS_IMAGE (image));
1132 g_return_if_fail (animation == NULL ||
1133 GDK_IS_PIXBUF_ANIMATION (animation));
1135 g_object_freeze_notify (G_OBJECT (image));
1138 g_object_ref (animation);
1140 gtk_image_clear (image);
1142 if (animation != NULL)
1144 image->storage_type = GTK_IMAGE_ANIMATION;
1146 image->data.anim.anim = animation;
1147 image->data.anim.frame_timeout = 0;
1148 image->data.anim.iter = NULL;
1150 gtk_image_update_size (image,
1151 gdk_pixbuf_animation_get_width (animation),
1152 gdk_pixbuf_animation_get_height (animation));
1155 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1157 g_object_thaw_notify (G_OBJECT (image));
1161 * gtk_image_set_from_icon_name:
1162 * @image: a #GtkImage
1163 * @icon_name: an icon name
1164 * @size: (type int): an icon size
1166 * See gtk_image_new_from_icon_name() for details.
1171 gtk_image_set_from_icon_name (GtkImage *image,
1172 const gchar *icon_name,
1177 g_return_if_fail (GTK_IS_IMAGE (image));
1179 g_object_freeze_notify (G_OBJECT (image));
1181 /* in case icon_name == image->data.name.icon_name */
1182 new_name = g_strdup (icon_name);
1184 gtk_image_clear (image);
1188 image->storage_type = GTK_IMAGE_ICON_NAME;
1190 image->data.name.icon_name = new_name;
1191 image->icon_size = size;
1193 /* Size is demand-computed in size request method
1194 * if we're a icon theme image, since changing the
1195 * style impacts the size request
1199 g_object_notify (G_OBJECT (image), "icon-name");
1200 g_object_notify (G_OBJECT (image), "icon-size");
1202 g_object_thaw_notify (G_OBJECT (image));
1206 * gtk_image_set_from_gicon:
1207 * @image: a #GtkImage
1209 * @size: (type int): an icon size
1211 * See gtk_image_new_from_gicon() for details.
1216 gtk_image_set_from_gicon (GtkImage *image,
1220 g_return_if_fail (GTK_IS_IMAGE (image));
1222 g_object_freeze_notify (G_OBJECT (image));
1224 /* in case icon == image->data.gicon.icon */
1226 g_object_ref (icon);
1228 gtk_image_clear (image);
1232 image->storage_type = GTK_IMAGE_GICON;
1234 image->data.gicon.icon = icon;
1235 image->icon_size = size;
1237 /* Size is demand-computed in size request method
1238 * if we're a icon theme image, since changing the
1239 * style impacts the size request
1243 g_object_notify (G_OBJECT (image), "gicon");
1244 g_object_notify (G_OBJECT (image), "icon-size");
1246 g_object_thaw_notify (G_OBJECT (image));
1250 * gtk_image_get_storage_type:
1251 * @image: a #GtkImage
1253 * Gets the type of representation being used by the #GtkImage
1254 * to store image data. If the #GtkImage has no image data,
1255 * the return value will be %GTK_IMAGE_EMPTY.
1257 * Return value: image representation being used
1260 gtk_image_get_storage_type (GtkImage *image)
1262 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1264 return image->storage_type;
1268 * gtk_image_get_pixmap:
1269 * @image: a #GtkImage
1270 * @pixmap: (out) (transfer none) (allow-none): location to store the
1272 * @mask: (out) (transfer none) (allow-none): location to store the
1275 * Gets the pixmap and mask being displayed by the #GtkImage.
1276 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1277 * %GTK_IMAGE_PIXMAP (see gtk_image_get_storage_type()).
1278 * The caller of this function does not own a reference to the
1279 * returned pixmap and mask.
1282 gtk_image_get_pixmap (GtkImage *image,
1286 g_return_if_fail (GTK_IS_IMAGE (image));
1287 g_return_if_fail (image->storage_type == GTK_IMAGE_PIXMAP ||
1288 image->storage_type == GTK_IMAGE_EMPTY);
1291 *pixmap = image->data.pixmap.pixmap;
1294 *mask = image->mask;
1298 * gtk_image_get_image:
1299 * @image: a #GtkImage
1300 * @gdk_image: (out) (transfer none) (allow-none): return location for
1301 * a #GtkImage, or %NULL
1302 * @mask: (out) (transfer none) (allow-none): return location for a
1303 * #GdkBitmap, or %NULL
1305 * Gets the #GdkImage and mask being displayed by the #GtkImage.
1306 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1307 * %GTK_IMAGE_IMAGE (see gtk_image_get_storage_type()).
1308 * The caller of this function does not own a reference to the
1309 * returned image and mask.
1312 gtk_image_get_image (GtkImage *image,
1313 GdkImage **gdk_image,
1316 g_return_if_fail (GTK_IS_IMAGE (image));
1317 g_return_if_fail (image->storage_type == GTK_IMAGE_IMAGE ||
1318 image->storage_type == GTK_IMAGE_EMPTY);
1321 *gdk_image = image->data.image.image;
1324 *mask = image->mask;
1328 * gtk_image_get_pixbuf:
1329 * @image: a #GtkImage
1331 * Gets the #GdkPixbuf being displayed by the #GtkImage.
1332 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1333 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1334 * The caller of this function does not own a reference to the
1337 * Return value: (transfer none): the displayed pixbuf, or %NULL if
1338 * the image is empty
1341 gtk_image_get_pixbuf (GtkImage *image)
1343 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1344 g_return_val_if_fail (image->storage_type == GTK_IMAGE_PIXBUF ||
1345 image->storage_type == GTK_IMAGE_EMPTY, NULL);
1347 if (image->storage_type == GTK_IMAGE_EMPTY)
1348 image->data.pixbuf.pixbuf = NULL;
1350 return image->data.pixbuf.pixbuf;
1354 * gtk_image_get_stock:
1355 * @image: a #GtkImage
1356 * @stock_id: (out) (transfer none) (allow-none): place to store a
1357 * stock icon name, or %NULL
1358 * @size: (out) (allow-none) (type int): place to store a stock icon
1361 * Gets the stock icon name and size being displayed by the #GtkImage.
1362 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1363 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1364 * The returned string is owned by the #GtkImage and should not
1368 gtk_image_get_stock (GtkImage *image,
1372 g_return_if_fail (GTK_IS_IMAGE (image));
1373 g_return_if_fail (image->storage_type == GTK_IMAGE_STOCK ||
1374 image->storage_type == GTK_IMAGE_EMPTY);
1376 if (image->storage_type == GTK_IMAGE_EMPTY)
1377 image->data.stock.stock_id = NULL;
1380 *stock_id = image->data.stock.stock_id;
1383 *size = image->icon_size;
1387 * gtk_image_get_icon_set:
1388 * @image: a #GtkImage
1389 * @icon_set: (out) (transfer none) (allow-none): location to store a
1390 * #GtkIconSet, or %NULL
1391 * @size: (out) (allow-none) (type int): location to store a stock
1392 * icon size, or %NULL
1394 * Gets the icon set and size being displayed by the #GtkImage.
1395 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1396 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1399 gtk_image_get_icon_set (GtkImage *image,
1400 GtkIconSet **icon_set,
1403 g_return_if_fail (GTK_IS_IMAGE (image));
1404 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_SET ||
1405 image->storage_type == GTK_IMAGE_EMPTY);
1408 *icon_set = image->data.icon_set.icon_set;
1411 *size = image->icon_size;
1415 * gtk_image_get_animation:
1416 * @image: a #GtkImage
1418 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1419 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1420 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1421 * The caller of this function does not own a reference to the
1422 * returned animation.
1424 * Return value: (transfer none): the displayed animation, or %NULL if
1425 * the image is empty
1428 gtk_image_get_animation (GtkImage *image)
1430 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1431 g_return_val_if_fail (image->storage_type == GTK_IMAGE_ANIMATION ||
1432 image->storage_type == GTK_IMAGE_EMPTY,
1435 if (image->storage_type == GTK_IMAGE_EMPTY)
1436 image->data.anim.anim = NULL;
1438 return image->data.anim.anim;
1442 * gtk_image_get_icon_name:
1443 * @image: a #GtkImage
1444 * @icon_name: (out) (transfer none) (allow-none): place to store an
1445 * icon name, or %NULL
1446 * @size: (out) (allow-none) (type int): place to store an icon size,
1449 * Gets the icon name and size being displayed by the #GtkImage.
1450 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1451 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1452 * The returned string is owned by the #GtkImage and should not
1458 gtk_image_get_icon_name (GtkImage *image,
1459 G_CONST_RETURN gchar **icon_name,
1462 g_return_if_fail (GTK_IS_IMAGE (image));
1463 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME ||
1464 image->storage_type == GTK_IMAGE_EMPTY);
1466 if (image->storage_type == GTK_IMAGE_EMPTY)
1467 image->data.name.icon_name = NULL;
1470 *icon_name = image->data.name.icon_name;
1473 *size = image->icon_size;
1477 * gtk_image_get_gicon:
1478 * @image: a #GtkImage
1479 * @gicon: (out) (transfer none) (allow-none): place to store a
1481 * @size: (out) (allow-none) (type int): place to store an icon size,
1484 * Gets the #GIcon and size being displayed by the #GtkImage.
1485 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1486 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1487 * The caller of this function does not own a reference to the
1493 gtk_image_get_gicon (GtkImage *image,
1497 g_return_if_fail (GTK_IS_IMAGE (image));
1498 g_return_if_fail (image->storage_type == GTK_IMAGE_GICON ||
1499 image->storage_type == GTK_IMAGE_EMPTY);
1501 if (image->storage_type == GTK_IMAGE_EMPTY)
1502 image->data.gicon.icon = NULL;
1505 *gicon = image->data.gicon.icon;
1508 *size = image->icon_size;
1514 * Creates a new empty #GtkImage widget.
1516 * Return value: a newly created #GtkImage widget.
1519 gtk_image_new (void)
1521 return g_object_new (GTK_TYPE_IMAGE, NULL);
1526 * @image: a #GtkImage
1528 * @mask: a #GdkBitmap that indicates which parts of the image should be transparent.
1530 * Sets the #GtkImage.
1532 * Deprecated: 2.0: Use gtk_image_set_from_image() instead.
1535 gtk_image_set (GtkImage *image,
1539 g_return_if_fail (GTK_IS_IMAGE (image));
1541 gtk_image_set_from_image (image, val, mask);
1546 * @image: a #GtkImage
1547 * @val: return location for a #GdkImage
1548 * @mask: a #GdkBitmap that indicates which parts of the image should be transparent.
1550 * Gets the #GtkImage.
1552 * Deprecated: 2.0: Use gtk_image_get_image() instead.
1555 gtk_image_get (GtkImage *image,
1559 g_return_if_fail (GTK_IS_IMAGE (image));
1561 gtk_image_get_image (image, val, mask);
1565 gtk_image_reset_anim_iter (GtkImage *image)
1567 if (image->storage_type == GTK_IMAGE_ANIMATION)
1569 /* Reset the animation */
1571 if (image->data.anim.frame_timeout)
1573 g_source_remove (image->data.anim.frame_timeout);
1574 image->data.anim.frame_timeout = 0;
1577 if (image->data.anim.iter)
1579 g_object_unref (image->data.anim.iter);
1580 image->data.anim.iter = NULL;
1586 gtk_image_unmap (GtkWidget *widget)
1588 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1590 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1594 gtk_image_unrealize (GtkWidget *widget)
1596 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1598 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1602 animation_timeout (gpointer data)
1607 image = GTK_IMAGE (data);
1609 image->data.anim.frame_timeout = 0;
1611 gdk_pixbuf_animation_iter_advance (image->data.anim.iter, NULL);
1613 delay = gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter);
1616 image->data.anim.frame_timeout =
1617 gdk_threads_add_timeout (delay, animation_timeout, image);
1619 gtk_widget_queue_draw (GTK_WIDGET (image));
1621 if (gtk_widget_is_drawable (GTK_WIDGET (image)))
1622 gdk_window_process_updates (GTK_WIDGET (image)->window, TRUE);
1629 icon_theme_changed (GtkImage *image)
1631 if (image->storage_type == GTK_IMAGE_ICON_NAME)
1633 if (image->data.name.pixbuf)
1634 g_object_unref (image->data.name.pixbuf);
1635 image->data.name.pixbuf = NULL;
1637 gtk_widget_queue_draw (GTK_WIDGET (image));
1639 if (image->storage_type == GTK_IMAGE_GICON)
1641 if (image->data.gicon.pixbuf)
1642 g_object_unref (image->data.gicon.pixbuf);
1643 image->data.gicon.pixbuf = NULL;
1645 gtk_widget_queue_draw (GTK_WIDGET (image));
1650 ensure_pixbuf_for_icon_name (GtkImage *image)
1652 GtkImagePrivate *priv;
1654 GtkIconTheme *icon_theme;
1655 GtkSettings *settings;
1657 gint *sizes, *s, dist;
1658 GtkIconLookupFlags flags;
1659 GError *error = NULL;
1661 g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME);
1663 priv = GTK_IMAGE_GET_PRIVATE (image);
1664 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1665 icon_theme = gtk_icon_theme_get_for_screen (screen);
1666 settings = gtk_settings_get_for_screen (screen);
1667 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1668 if (image->data.name.pixbuf == NULL)
1670 if (priv->pixel_size != -1)
1672 width = height = priv->pixel_size;
1673 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1675 else if (!gtk_icon_size_lookup_for_settings (settings,
1679 if (image->icon_size == -1)
1681 /* Find an available size close to 48 */
1682 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, image->data.name.icon_name);
1684 width = height = 48;
1685 for (s = sizes; *s; s++)
1689 width = height = 48;
1696 width = height = *s;
1704 width = height = *s;
1713 g_warning ("Invalid icon size %d\n", image->icon_size);
1714 width = height = 24;
1717 image->data.name.pixbuf =
1718 gtk_icon_theme_load_icon (icon_theme,
1719 image->data.name.icon_name,
1720 MIN (width, height), flags, &error);
1721 if (image->data.name.pixbuf == NULL)
1723 g_error_free (error);
1724 image->data.name.pixbuf =
1725 gtk_widget_render_icon (GTK_WIDGET (image),
1726 GTK_STOCK_MISSING_IMAGE,
1734 ensure_pixbuf_for_gicon (GtkImage *image)
1736 GtkImagePrivate *priv;
1738 GtkIconTheme *icon_theme;
1739 GtkSettings *settings;
1742 GtkIconLookupFlags flags;
1744 g_return_if_fail (image->storage_type == GTK_IMAGE_GICON);
1746 priv = GTK_IMAGE_GET_PRIVATE (image);
1747 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1748 icon_theme = gtk_icon_theme_get_for_screen (screen);
1749 settings = gtk_settings_get_for_screen (screen);
1750 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1751 if (image->data.gicon.pixbuf == NULL)
1753 if (priv->pixel_size != -1)
1755 width = height = priv->pixel_size;
1756 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1758 else if (!gtk_icon_size_lookup_for_settings (settings,
1762 if (image->icon_size == -1)
1763 width = height = 48;
1766 g_warning ("Invalid icon size %d\n", image->icon_size);
1767 width = height = 24;
1771 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1772 image->data.gicon.icon,
1773 MIN (width, height), flags);
1776 image->data.gicon.pixbuf = gtk_icon_info_load_icon (info, NULL);
1777 gtk_icon_info_free (info);
1780 if (image->data.gicon.pixbuf == NULL)
1782 image->data.gicon.pixbuf =
1783 gtk_widget_render_icon (GTK_WIDGET (image),
1784 GTK_STOCK_MISSING_IMAGE,
1793 * Like gdk_rectangle_intersect (dest, src, dest), but make
1794 * sure that the origin of dest is moved by an "even" offset.
1795 * If necessary grow the intersection by one row or column
1798 * This is necessary since we can't pass alignment information
1799 * for the pixelation pattern down to gdk_pixbuf_saturate_and_pixelate(),
1800 * thus we have to makesure that the subimages are properly aligned.
1803 rectangle_intersect_even (GdkRectangle *src,
1811 isect = gdk_rectangle_intersect (dest, src, dest);
1813 if ((dest->x - x + dest->y - y) % 2 != 0)
1831 gtk_image_expose (GtkWidget *widget,
1832 GdkEventExpose *event)
1834 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1835 g_return_val_if_fail (event != NULL, FALSE);
1837 if (GTK_WIDGET_MAPPED (widget) &&
1838 GTK_IMAGE (widget)->storage_type != GTK_IMAGE_EMPTY)
1842 GtkImagePrivate *priv;
1843 GdkRectangle area, image_bound;
1845 gint x, y, mask_x, mask_y;
1848 gboolean needs_state_transform;
1850 image = GTK_IMAGE (widget);
1851 misc = GTK_MISC (widget);
1852 priv = GTK_IMAGE_GET_PRIVATE (image);
1856 /* For stock items and icon sets, we lazily calculate
1857 * the size; we might get here between a queue_resize()
1858 * and size_request() if something explicitely forces
1861 if (priv->need_calc_size)
1862 gtk_image_calc_size (image);
1864 if (!gdk_rectangle_intersect (&area, &widget->allocation, &area))
1867 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1868 xalign = misc->xalign;
1870 xalign = 1.0 - misc->xalign;
1872 x = floor (widget->allocation.x + misc->xpad
1873 + ((widget->allocation.width - widget->requisition.width) * xalign));
1874 y = floor (widget->allocation.y + misc->ypad
1875 + ((widget->allocation.height - widget->requisition.height) * misc->yalign));
1881 image_bound.width = 0;
1882 image_bound.height = 0;
1886 needs_state_transform = GTK_WIDGET_STATE (widget) != GTK_STATE_NORMAL;
1888 switch (image->storage_type)
1890 case GTK_IMAGE_PIXMAP:
1892 gdk_drawable_get_size (image->data.pixmap.pixmap,
1894 &image_bound.height);
1895 if (rectangle_intersect_even (&area, &image_bound) &&
1896 needs_state_transform)
1898 pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1899 image->data.pixmap.pixmap,
1900 gtk_widget_get_colormap (widget),
1901 image_bound.x - x, image_bound.y - y,
1904 image_bound.height);
1912 case GTK_IMAGE_IMAGE:
1914 image_bound.width = image->data.image.image->width;
1915 image_bound.height = image->data.image.image->height;
1917 if (rectangle_intersect_even (&area, &image_bound) &&
1918 needs_state_transform)
1920 pixbuf = gdk_pixbuf_get_from_image (NULL,
1921 image->data.image.image,
1922 gtk_widget_get_colormap (widget),
1923 image_bound.x - x, image_bound.y - y,
1926 image_bound.height);
1933 case GTK_IMAGE_PIXBUF:
1934 image_bound.width = gdk_pixbuf_get_width (image->data.pixbuf.pixbuf);
1935 image_bound.height = gdk_pixbuf_get_height (image->data.pixbuf.pixbuf);
1937 if (rectangle_intersect_even (&area, &image_bound) &&
1938 needs_state_transform)
1940 pixbuf = gdk_pixbuf_new_subpixbuf (image->data.pixbuf.pixbuf,
1941 image_bound.x - x, image_bound.y - y,
1942 image_bound.width, image_bound.height);
1949 pixbuf = image->data.pixbuf.pixbuf;
1950 g_object_ref (pixbuf);
1954 case GTK_IMAGE_STOCK:
1955 pixbuf = gtk_widget_render_icon (widget,
1956 image->data.stock.stock_id,
1961 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1962 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1966 needs_state_transform = FALSE;
1969 case GTK_IMAGE_ICON_SET:
1971 gtk_icon_set_render_icon (image->data.icon_set.icon_set,
1973 gtk_widget_get_direction (widget),
1974 GTK_WIDGET_STATE (widget),
1981 image_bound.width = gdk_pixbuf_get_width (pixbuf);
1982 image_bound.height = gdk_pixbuf_get_height (pixbuf);
1986 needs_state_transform = FALSE;
1989 case GTK_IMAGE_ANIMATION:
1991 if (image->data.anim.iter == NULL)
1993 image->data.anim.iter = gdk_pixbuf_animation_get_iter (image->data.anim.anim, NULL);
1995 if (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter) >= 0)
1996 image->data.anim.frame_timeout =
1997 gdk_threads_add_timeout (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter),
2002 image_bound.width = gdk_pixbuf_animation_get_width (image->data.anim.anim);
2003 image_bound.height = gdk_pixbuf_animation_get_height (image->data.anim.anim);
2005 /* don't advance the anim iter here, or we could get frame changes between two
2006 * exposes of different areas.
2009 pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (image->data.anim.iter);
2010 g_object_ref (pixbuf);
2014 case GTK_IMAGE_ICON_NAME:
2015 ensure_pixbuf_for_icon_name (image);
2016 pixbuf = image->data.name.pixbuf;
2019 g_object_ref (pixbuf);
2020 image_bound.width = gdk_pixbuf_get_width (pixbuf);
2021 image_bound.height = gdk_pixbuf_get_height (pixbuf);
2025 case GTK_IMAGE_GICON:
2026 ensure_pixbuf_for_gicon (image);
2027 pixbuf = image->data.gicon.pixbuf;
2030 g_object_ref (pixbuf);
2031 image_bound.width = gdk_pixbuf_get_width (pixbuf);
2032 image_bound.height = gdk_pixbuf_get_height (pixbuf);
2036 case GTK_IMAGE_EMPTY:
2037 g_assert_not_reached ();
2043 gdk_gc_set_clip_mask (widget->style->black_gc, mask);
2044 gdk_gc_set_clip_origin (widget->style->black_gc, mask_x, mask_y);
2047 if (rectangle_intersect_even (&area, &image_bound))
2051 if (needs_state_transform)
2053 GtkIconSource *source;
2054 GdkPixbuf *rendered;
2056 source = gtk_icon_source_new ();
2057 gtk_icon_source_set_pixbuf (source, pixbuf);
2058 /* The size here is arbitrary; since size isn't
2059 * wildcarded in the souce, it isn't supposed to be
2060 * scaled by the engine function
2062 gtk_icon_source_set_size (source,
2063 GTK_ICON_SIZE_SMALL_TOOLBAR);
2064 gtk_icon_source_set_size_wildcarded (source, FALSE);
2066 rendered = gtk_style_render_icon (widget->style,
2068 gtk_widget_get_direction (widget),
2069 GTK_WIDGET_STATE (widget),
2075 gtk_icon_source_free (source);
2077 g_object_unref (pixbuf);
2083 gdk_draw_pixbuf (widget->window,
2084 widget->style->black_gc,
2092 GDK_RGB_DITHER_NORMAL,
2098 switch (image->storage_type)
2100 case GTK_IMAGE_PIXMAP:
2101 gdk_draw_drawable (widget->window,
2102 widget->style->black_gc,
2103 image->data.pixmap.pixmap,
2104 image_bound.x - x, image_bound.y - y,
2105 image_bound.x, image_bound.y,
2106 image_bound.width, image_bound.height);
2109 case GTK_IMAGE_IMAGE:
2110 gdk_draw_image (widget->window,
2111 widget->style->black_gc,
2112 image->data.image.image,
2113 image_bound.x - x, image_bound.y - y,
2114 image_bound.x, image_bound.y,
2115 image_bound.width, image_bound.height);
2118 case GTK_IMAGE_PIXBUF:
2119 case GTK_IMAGE_STOCK:
2120 case GTK_IMAGE_ICON_SET:
2121 case GTK_IMAGE_ANIMATION:
2122 case GTK_IMAGE_ICON_NAME:
2123 case GTK_IMAGE_EMPTY:
2124 case GTK_IMAGE_GICON:
2125 g_assert_not_reached ();
2129 } /* if rectangle intersects */
2133 gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
2134 gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
2138 g_object_unref (pixbuf);
2140 } /* if widget is drawable */
2146 gtk_image_reset (GtkImage *image)
2148 GtkImagePrivate *priv;
2150 priv = GTK_IMAGE_GET_PRIVATE (image);
2152 g_object_freeze_notify (G_OBJECT (image));
2154 if (image->storage_type != GTK_IMAGE_EMPTY)
2155 g_object_notify (G_OBJECT (image), "storage-type");
2159 g_object_unref (image->mask);
2161 g_object_notify (G_OBJECT (image), "mask");
2164 if (image->icon_size != DEFAULT_ICON_SIZE)
2166 image->icon_size = DEFAULT_ICON_SIZE;
2167 g_object_notify (G_OBJECT (image), "icon-size");
2170 switch (image->storage_type)
2172 case GTK_IMAGE_PIXMAP:
2174 if (image->data.pixmap.pixmap)
2175 g_object_unref (image->data.pixmap.pixmap);
2176 image->data.pixmap.pixmap = NULL;
2178 g_object_notify (G_OBJECT (image), "pixmap");
2182 case GTK_IMAGE_IMAGE:
2184 if (image->data.image.image)
2185 g_object_unref (image->data.image.image);
2186 image->data.image.image = NULL;
2188 g_object_notify (G_OBJECT (image), "image");
2192 case GTK_IMAGE_PIXBUF:
2194 if (image->data.pixbuf.pixbuf)
2195 g_object_unref (image->data.pixbuf.pixbuf);
2197 g_object_notify (G_OBJECT (image), "pixbuf");
2201 case GTK_IMAGE_STOCK:
2203 g_free (image->data.stock.stock_id);
2205 image->data.stock.stock_id = NULL;
2207 g_object_notify (G_OBJECT (image), "stock");
2210 case GTK_IMAGE_ICON_SET:
2211 if (image->data.icon_set.icon_set)
2212 gtk_icon_set_unref (image->data.icon_set.icon_set);
2213 image->data.icon_set.icon_set = NULL;
2215 g_object_notify (G_OBJECT (image), "icon-set");
2218 case GTK_IMAGE_ANIMATION:
2219 gtk_image_reset_anim_iter (image);
2221 if (image->data.anim.anim)
2222 g_object_unref (image->data.anim.anim);
2223 image->data.anim.anim = NULL;
2225 g_object_notify (G_OBJECT (image), "pixbuf-animation");
2229 case GTK_IMAGE_ICON_NAME:
2230 g_free (image->data.name.icon_name);
2231 image->data.name.icon_name = NULL;
2232 if (image->data.name.pixbuf)
2233 g_object_unref (image->data.name.pixbuf);
2234 image->data.name.pixbuf = NULL;
2236 g_object_notify (G_OBJECT (image), "icon-name");
2240 case GTK_IMAGE_GICON:
2241 if (image->data.gicon.icon)
2242 g_object_unref (image->data.gicon.icon);
2243 image->data.gicon.icon = NULL;
2244 if (image->data.gicon.pixbuf)
2245 g_object_unref (image->data.gicon.pixbuf);
2246 image->data.gicon.pixbuf = NULL;
2248 g_object_notify (G_OBJECT (image), "gicon");
2252 case GTK_IMAGE_EMPTY:
2260 g_free (priv->filename);
2261 priv->filename = NULL;
2262 g_object_notify (G_OBJECT (image), "file");
2265 image->storage_type = GTK_IMAGE_EMPTY;
2267 memset (&image->data, '\0', sizeof (image->data));
2269 g_object_thaw_notify (G_OBJECT (image));
2274 * @image: a #GtkImage
2276 * Resets the image to be empty.
2281 gtk_image_clear (GtkImage *image)
2283 GtkImagePrivate *priv;
2285 priv = GTK_IMAGE_GET_PRIVATE (image);
2287 priv->need_calc_size = 1;
2289 gtk_image_reset (image);
2290 gtk_image_update_size (image, 0, 0);
2294 gtk_image_calc_size (GtkImage *image)
2296 GtkWidget *widget = GTK_WIDGET (image);
2297 GdkPixbuf *pixbuf = NULL;
2298 GtkImagePrivate *priv;
2300 priv = GTK_IMAGE_GET_PRIVATE (image);
2302 priv->need_calc_size = 0;
2304 /* We update stock/icon set on every size request, because
2305 * the theme could have affected the size; for other kinds of
2306 * image, we just update the requisition when the image data
2309 switch (image->storage_type)
2311 case GTK_IMAGE_STOCK:
2312 pixbuf = gtk_widget_render_icon (widget,
2313 image->data.stock.stock_id,
2318 case GTK_IMAGE_ICON_SET:
2319 pixbuf = gtk_icon_set_render_icon (image->data.icon_set.icon_set,
2321 gtk_widget_get_direction (widget),
2322 GTK_WIDGET_STATE (widget),
2327 case GTK_IMAGE_ICON_NAME:
2328 ensure_pixbuf_for_icon_name (image);
2329 pixbuf = image->data.name.pixbuf;
2330 if (pixbuf) g_object_ref (pixbuf);
2332 case GTK_IMAGE_GICON:
2333 ensure_pixbuf_for_gicon (image);
2334 pixbuf = image->data.gicon.pixbuf;
2336 g_object_ref (pixbuf);
2344 widget->requisition.width = gdk_pixbuf_get_width (pixbuf) + GTK_MISC (image)->xpad * 2;
2345 widget->requisition.height = gdk_pixbuf_get_height (pixbuf) + GTK_MISC (image)->ypad * 2;
2347 g_object_unref (pixbuf);
2352 gtk_image_size_request (GtkWidget *widget,
2353 GtkRequisition *requisition)
2357 image = GTK_IMAGE (widget);
2359 gtk_image_calc_size (image);
2361 /* Chain up to default that simply reads current requisition */
2362 GTK_WIDGET_CLASS (gtk_image_parent_class)->size_request (widget, requisition);
2366 gtk_image_style_set (GtkWidget *widget,
2367 GtkStyle *prev_style)
2371 image = GTK_IMAGE (widget);
2373 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_set (widget, prev_style);
2375 icon_theme_changed (image);
2379 gtk_image_screen_changed (GtkWidget *widget,
2380 GdkScreen *prev_screen)
2384 image = GTK_IMAGE (widget);
2386 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
2387 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
2389 icon_theme_changed (image);
2394 gtk_image_update_size (GtkImage *image,
2398 GtkWidget *widget = GTK_WIDGET (image);
2400 widget->requisition.width = image_width + GTK_MISC (image)->xpad * 2;
2401 widget->requisition.height = image_height + GTK_MISC (image)->ypad * 2;
2403 if (gtk_widget_get_visible (widget))
2404 gtk_widget_queue_resize (widget);
2409 * gtk_image_set_pixel_size:
2410 * @image: a #GtkImage
2411 * @pixel_size: the new pixel size
2413 * Sets the pixel size to use for named icons. If the pixel size is set
2414 * to a value != -1, it is used instead of the icon size set by
2415 * gtk_image_set_from_icon_name().
2420 gtk_image_set_pixel_size (GtkImage *image,
2423 GtkImagePrivate *priv;
2425 g_return_if_fail (GTK_IS_IMAGE (image));
2427 priv = GTK_IMAGE_GET_PRIVATE (image);
2429 if (priv->pixel_size != pixel_size)
2431 priv->pixel_size = pixel_size;
2433 if (image->storage_type == GTK_IMAGE_ICON_NAME)
2435 if (image->data.name.pixbuf)
2437 g_object_unref (image->data.name.pixbuf);
2438 image->data.name.pixbuf = NULL;
2441 gtk_image_update_size (image, pixel_size, pixel_size);
2444 if (image->storage_type == GTK_IMAGE_GICON)
2446 if (image->data.gicon.pixbuf)
2448 g_object_unref (image->data.gicon.pixbuf);
2449 image->data.gicon.pixbuf = NULL;
2452 gtk_image_update_size (image, pixel_size, pixel_size);
2455 g_object_notify (G_OBJECT (image), "pixel-size");
2460 * gtk_image_get_pixel_size:
2461 * @image: a #GtkImage
2463 * Gets the pixel size used for named icons.
2465 * Returns: the pixel size used for named icons.
2470 gtk_image_get_pixel_size (GtkImage *image)
2472 GtkImagePrivate *priv;
2474 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
2476 priv = GTK_IMAGE_GET_PRIVATE (image);
2478 return priv->pixel_size;
2481 #if defined (G_OS_WIN32) && !defined (_WIN64)
2483 #undef gtk_image_new_from_file
2486 gtk_image_new_from_file (const gchar *filename)
2488 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2491 retval = gtk_image_new_from_file_utf8 (utf8_filename);
2493 g_free (utf8_filename);
2498 #undef gtk_image_set_from_file
2501 gtk_image_set_from_file (GtkImage *image,
2502 const gchar *filename)
2504 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2506 gtk_image_set_from_file_utf8 (image, utf8_filename);
2508 g_free (utf8_filename);
2513 #define __GTK_IMAGE_C__
2514 #include "gtkaliasdef.c"