1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkcontainer.h"
33 #include "gtkiconfactory.h"
35 #include "gtkicontheme.h"
37 #include "gtkprivate.h"
41 * @Short_description: A widget displaying an image
43 * @See_also:#GdkPixbuf
45 * The #GtkImage widget displays an image. Various kinds of object
46 * can be displayed as an image; most typically, you would load a
47 * #GdkPixbuf ("pixel buffer") from a file, and then display that.
48 * There's a convenience function to do this, gtk_image_new_from_file(),
50 * <informalexample><programlisting>
52 * image = gtk_image_new_from_file ("myfile.png");
53 * </programlisting></informalexample>
54 * If the file isn't loaded successfully, the image will contain a
55 * "broken image" icon similar to that used in many web browsers.
56 * If you want to handle errors in loading the file yourself,
57 * for example by displaying an error message, then load the image with
58 * gdk_pixbuf_new_from_file(), then create the #GtkImage with
59 * gtk_image_new_from_pixbuf().
61 * The image file may contain an animation, if so the #GtkImage will
62 * display an animation (#GdkPixbufAnimation) instead of a static image.
64 * #GtkImage is a subclass of #GtkMisc, which implies that you can
65 * align it (center, left, right) and add padding to it, using
68 * #GtkImage is a "no window" widget (has no #GdkWindow of its own),
69 * so by default does not receive events. If you want to receive events
70 * on the image, such as button clicks, place the image inside a
71 * #GtkEventBox, then connect to the event signals on the event box.
73 * <title>Handling button press events on a
74 * <structname>GtkImage</structname>.</title>
77 * button_press_callback (GtkWidget *event_box,
78 * GdkEventButton *event,
81 * g_print ("Event box clicked at coordinates %f,%f\n",
82 * event->x, event->y);
84 * /<!---->* Returning TRUE means we handled the event, so the signal
85 * * emission should be stopped (don't call any further
86 * * callbacks that may be connected). Return FALSE
87 * * to continue invoking callbacks.
96 * GtkWidget *event_box;
98 * image = gtk_image_new_from_file ("myfile.png");
100 * event_box = gtk_event_box_new (<!-- -->);
102 * gtk_container_add (GTK_CONTAINER (event_box), image);
104 * g_signal_connect (G_OBJECT (event_box),
105 * "button_press_event",
106 * G_CALLBACK (button_press_callback),
114 * When handling events on the event box, keep in mind that coordinates
115 * in the image may be different from event box coordinates due to
116 * the alignment and padding settings on the image (see #GtkMisc).
117 * The simplest way to solve this is to set the alignment to 0.0
118 * (left/top), and set the padding to zero. Then the origin of
119 * the image will be the same as the origin of the event box.
121 * Sometimes an application will want to avoid depending on external data
122 * files, such as image files. GTK+ comes with a program to avoid this,
123 * called <application>gdk-pixbuf-csource</application>. This program
124 * allows you to convert an image into a C variable declaration, which
125 * can then be loaded into a #GdkPixbuf using
126 * gdk_pixbuf_new_from_inline().
132 GtkIconSize icon_size; /* Only used with GTK_IMAGE_STOCK, GTK_IMAGE_ICON_SET, GTK_IMAGE_ICON_NAME */
133 GtkImageType storage_type;
134 GdkBitmap *mask; /* Only used with GTK_IMAGE_PIXMAP, GTK_IMAGE_IMAGE */
138 GtkImagePixmapData pixmap;
139 GtkImageImageData image;
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;
157 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
158 static gint gtk_image_expose (GtkWidget *widget,
159 GdkEventExpose *event);
160 static void gtk_image_unmap (GtkWidget *widget);
161 static void gtk_image_unrealize (GtkWidget *widget);
162 static void gtk_image_size_request (GtkWidget *widget,
163 GtkRequisition *requisition);
164 static void gtk_image_style_set (GtkWidget *widget,
165 GtkStyle *prev_style);
166 static void gtk_image_screen_changed (GtkWidget *widget,
167 GdkScreen *prev_screen);
168 static void gtk_image_destroy (GtkObject *object);
169 static void gtk_image_reset (GtkImage *image);
170 static void gtk_image_calc_size (GtkImage *image);
172 static void gtk_image_update_size (GtkImage *image,
176 static void gtk_image_set_property (GObject *object,
180 static void gtk_image_get_property (GObject *object,
185 static void icon_theme_changed (GtkImage *image);
199 PROP_PIXBUF_ANIMATION,
205 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
208 gtk_image_class_init (GtkImageClass *class)
210 GObjectClass *gobject_class;
211 GtkObjectClass *object_class;
212 GtkWidgetClass *widget_class;
214 gobject_class = G_OBJECT_CLASS (class);
216 gobject_class->set_property = gtk_image_set_property;
217 gobject_class->get_property = gtk_image_get_property;
219 object_class = GTK_OBJECT_CLASS (class);
221 object_class->destroy = gtk_image_destroy;
223 widget_class = GTK_WIDGET_CLASS (class);
225 widget_class->expose_event = gtk_image_expose;
226 widget_class->size_request = gtk_image_size_request;
227 widget_class->unmap = gtk_image_unmap;
228 widget_class->unrealize = gtk_image_unrealize;
229 widget_class->style_set = gtk_image_style_set;
230 widget_class->screen_changed = gtk_image_screen_changed;
232 g_object_class_install_property (gobject_class,
234 g_param_spec_object ("pixbuf",
236 P_("A GdkPixbuf to display"),
238 GTK_PARAM_READWRITE));
240 g_object_class_install_property (gobject_class,
242 g_param_spec_object ("pixmap",
244 P_("A GdkPixmap to display"),
246 GTK_PARAM_READWRITE));
248 g_object_class_install_property (gobject_class,
250 g_param_spec_object ("image",
252 P_("A GdkImage to display"),
254 GTK_PARAM_READWRITE));
256 g_object_class_install_property (gobject_class,
258 g_param_spec_object ("mask",
260 P_("Mask bitmap to use with GdkImage or GdkPixmap"),
262 GTK_PARAM_READWRITE));
264 g_object_class_install_property (gobject_class,
266 g_param_spec_string ("file",
268 P_("Filename to load and display"),
270 GTK_PARAM_READWRITE));
273 g_object_class_install_property (gobject_class,
275 g_param_spec_string ("stock",
277 P_("Stock ID for a stock image to display"),
279 GTK_PARAM_READWRITE));
281 g_object_class_install_property (gobject_class,
283 g_param_spec_boxed ("icon-set",
285 P_("Icon set to display"),
287 GTK_PARAM_READWRITE));
289 g_object_class_install_property (gobject_class,
291 g_param_spec_int ("icon-size",
293 P_("Symbolic size to use for stock icon, icon set or named icon"),
296 GTK_PARAM_READWRITE));
298 * GtkImage:pixel-size:
300 * The "pixel-size" property can be used to specify a fixed size
301 * overriding the #GtkImage:icon-size property for images of type
302 * %GTK_IMAGE_ICON_NAME.
306 g_object_class_install_property (gobject_class,
308 g_param_spec_int ("pixel-size",
310 P_("Pixel size to use for named icon"),
313 GTK_PARAM_READWRITE));
315 g_object_class_install_property (gobject_class,
316 PROP_PIXBUF_ANIMATION,
317 g_param_spec_object ("pixbuf-animation",
319 P_("GdkPixbufAnimation to display"),
320 GDK_TYPE_PIXBUF_ANIMATION,
321 GTK_PARAM_READWRITE));
324 * GtkImage:icon-name:
326 * The name of the icon in the icon theme. If the icon theme is
327 * changed, the image will be updated automatically.
331 g_object_class_install_property (gobject_class,
333 g_param_spec_string ("icon-name",
335 P_("The name of the icon from the icon theme"),
337 GTK_PARAM_READWRITE));
342 * The GIcon displayed in the GtkImage. For themed icons,
343 * If the icon theme is changed, the image will be updated
348 g_object_class_install_property (gobject_class,
350 g_param_spec_object ("gicon",
352 P_("The GIcon being displayed"),
354 GTK_PARAM_READWRITE));
356 g_object_class_install_property (gobject_class,
358 g_param_spec_enum ("storage-type",
360 P_("The representation being used for image data"),
363 GTK_PARAM_READABLE));
365 g_type_class_add_private (object_class, sizeof (GtkImagePriv));
369 gtk_image_init (GtkImage *image)
373 image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image,
378 gtk_widget_set_has_window (GTK_WIDGET (image), FALSE);
380 priv->storage_type = GTK_IMAGE_EMPTY;
381 priv->icon_size = DEFAULT_ICON_SIZE;
384 priv->pixel_size = -1;
386 priv->filename = NULL;
390 gtk_image_destroy (GtkObject *object)
392 GtkImage *image = GTK_IMAGE (object);
394 gtk_image_reset (image);
396 GTK_OBJECT_CLASS (gtk_image_parent_class)->destroy (object);
400 gtk_image_set_property (GObject *object,
405 GtkImage *image = GTK_IMAGE (object);
406 GtkImagePriv *priv = image->priv;
411 gtk_image_set_from_pixbuf (image,
412 g_value_get_object (value));
415 gtk_image_set_from_pixmap (image,
416 g_value_get_object (value),
420 gtk_image_set_from_image (image,
421 g_value_get_object (value),
425 if (priv->storage_type == GTK_IMAGE_PIXMAP)
426 gtk_image_set_from_pixmap (image,
427 priv->data.pixmap.pixmap,
428 g_value_get_object (value));
429 else if (priv->storage_type == GTK_IMAGE_IMAGE)
430 gtk_image_set_from_image (image,
431 priv->data.image.image,
432 g_value_get_object (value));
437 mask = g_value_get_object (value);
442 gtk_image_clear (image);
448 gtk_image_set_from_file (image, g_value_get_string (value));
451 gtk_image_set_from_stock (image, g_value_get_string (value),
455 gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
459 if (priv->storage_type == GTK_IMAGE_STOCK)
460 gtk_image_set_from_stock (image,
461 priv->data.stock.stock_id,
462 g_value_get_int (value));
463 else if (priv->storage_type == GTK_IMAGE_ICON_SET)
464 gtk_image_set_from_icon_set (image,
465 priv->data.icon_set.icon_set,
466 g_value_get_int (value));
467 else if (priv->storage_type == GTK_IMAGE_ICON_NAME)
468 gtk_image_set_from_icon_name (image,
469 priv->data.name.icon_name,
470 g_value_get_int (value));
471 else if (priv->storage_type == GTK_IMAGE_GICON)
472 gtk_image_set_from_gicon (image,
473 priv->data.gicon.icon,
474 g_value_get_int (value));
476 /* Save to be used when STOCK, ICON_SET, ICON_NAME or GICON property comes in */
477 priv->icon_size = g_value_get_int (value);
479 case PROP_PIXEL_SIZE:
480 gtk_image_set_pixel_size (image, g_value_get_int (value));
482 case PROP_PIXBUF_ANIMATION:
483 gtk_image_set_from_animation (image,
484 g_value_get_object (value));
487 gtk_image_set_from_icon_name (image, g_value_get_string (value),
491 gtk_image_set_from_gicon (image, g_value_get_object (value),
496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
502 gtk_image_get_property (GObject *object,
507 GtkImage *image = GTK_IMAGE (object);
508 GtkImagePriv *priv = image->priv;
510 /* The "getter" functions whine if you try to get the wrong
511 * storage type. This function is instead robust against that,
512 * so that GUI builders don't have to jump through hoops
519 if (priv->storage_type != GTK_IMAGE_PIXBUF)
520 g_value_set_object (value, NULL);
522 g_value_set_object (value,
523 gtk_image_get_pixbuf (image));
526 if (priv->storage_type != GTK_IMAGE_PIXMAP)
527 g_value_set_object (value, NULL);
529 g_value_set_object (value,
530 priv->data.pixmap.pixmap);
533 g_value_set_object (value, priv->mask);
536 if (priv->storage_type != GTK_IMAGE_IMAGE)
537 g_value_set_object (value, NULL);
539 g_value_set_object (value,
540 priv->data.image.image);
543 g_value_set_string (value, priv->filename);
546 if (priv->storage_type != GTK_IMAGE_STOCK)
547 g_value_set_string (value, NULL);
549 g_value_set_string (value,
550 priv->data.stock.stock_id);
553 if (priv->storage_type != GTK_IMAGE_ICON_SET)
554 g_value_set_boxed (value, NULL);
556 g_value_set_boxed (value,
557 priv->data.icon_set.icon_set);
560 g_value_set_int (value, priv->icon_size);
562 case PROP_PIXEL_SIZE:
563 g_value_set_int (value, priv->pixel_size);
565 case PROP_PIXBUF_ANIMATION:
566 if (priv->storage_type != GTK_IMAGE_ANIMATION)
567 g_value_set_object (value, NULL);
569 g_value_set_object (value,
570 priv->data.anim.anim);
573 if (priv->storage_type != GTK_IMAGE_ICON_NAME)
574 g_value_set_string (value, NULL);
576 g_value_set_string (value,
577 priv->data.name.icon_name);
580 if (priv->storage_type != GTK_IMAGE_GICON)
581 g_value_set_object (value, NULL);
583 g_value_set_object (value,
584 priv->data.gicon.icon);
586 case PROP_STORAGE_TYPE:
587 g_value_set_enum (value, priv->storage_type);
591 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
598 * gtk_image_new_from_pixmap:
599 * @pixmap: (allow-none): a #GdkPixmap, or %NULL
600 * @mask: (allow-none): a #GdkBitmap, or %NULL
602 * Creates a #GtkImage widget displaying @pixmap with a @mask.
603 * A #GdkPixmap is a server-side image buffer in the pixel format of the
604 * current display. The #GtkImage does not assume a reference to the
605 * pixmap or mask; you still need to unref them if you own references.
606 * #GtkImage will add its own reference rather than adopting yours.
608 * Return value: a new #GtkImage
611 gtk_image_new_from_pixmap (GdkPixmap *pixmap,
616 image = g_object_new (GTK_TYPE_IMAGE, NULL);
618 gtk_image_set_from_pixmap (image, pixmap, mask);
620 return GTK_WIDGET (image);
624 * gtk_image_new_from_image:
625 * @image: (allow-none): a #GdkImage, or %NULL
626 * @mask: (allow-none): a #GdkBitmap, or %NULL
628 * Creates a #GtkImage widget displaying a @image with a @mask.
629 * A #GdkImage is a client-side image buffer in the pixel format of the
630 * current display. The #GtkImage does not assume a reference to the
631 * image or mask; you still need to unref them if you own references.
632 * #GtkImage will add its own reference rather than adopting yours.
634 * Return value: a new #GtkImage
637 gtk_image_new_from_image (GdkImage *gdk_image,
642 image = g_object_new (GTK_TYPE_IMAGE, NULL);
644 gtk_image_set_from_image (image, gdk_image, mask);
646 return GTK_WIDGET (image);
650 * gtk_image_new_from_file:
651 * @filename: a filename
653 * Creates a new #GtkImage displaying the file @filename. If the file
654 * isn't found or can't be loaded, the resulting #GtkImage will
655 * display a "broken image" icon. This function never returns %NULL,
656 * it always returns a valid #GtkImage widget.
658 * If the file contains an animation, the image will contain an
661 * If you need to detect failures to load the file, use
662 * gdk_pixbuf_new_from_file() to load the file yourself, then create
663 * the #GtkImage from the pixbuf. (Or for animations, use
664 * gdk_pixbuf_animation_new_from_file()).
666 * The storage type (gtk_image_get_storage_type()) of the returned
667 * image is not defined, it will be whatever is appropriate for
668 * displaying the file.
670 * Return value: a new #GtkImage
673 gtk_image_new_from_file (const gchar *filename)
677 image = g_object_new (GTK_TYPE_IMAGE, NULL);
679 gtk_image_set_from_file (image, filename);
681 return GTK_WIDGET (image);
685 * gtk_image_new_from_pixbuf:
686 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
688 * Creates a new #GtkImage displaying @pixbuf.
689 * The #GtkImage does not assume a reference to the
690 * pixbuf; you still need to unref it if you own references.
691 * #GtkImage will add its own reference rather than adopting yours.
693 * Note that this function just creates an #GtkImage from the pixbuf. The
694 * #GtkImage created will not react to state changes. Should you want that,
695 * you should use gtk_image_new_from_icon_set().
697 * Return value: a new #GtkImage
700 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
704 image = g_object_new (GTK_TYPE_IMAGE, NULL);
706 gtk_image_set_from_pixbuf (image, pixbuf);
708 return GTK_WIDGET (image);
712 * gtk_image_new_from_stock:
713 * @stock_id: a stock icon name
714 * @size: (type int): a stock icon size
716 * Creates a #GtkImage displaying a stock icon. Sample stock icon
717 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
718 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
719 * icon name isn't known, the image will be empty.
720 * You can register your own stock icon names, see
721 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
723 * Return value: a new #GtkImage displaying the stock icon
726 gtk_image_new_from_stock (const gchar *stock_id,
731 image = g_object_new (GTK_TYPE_IMAGE, NULL);
733 gtk_image_set_from_stock (image, stock_id, size);
735 return GTK_WIDGET (image);
739 * gtk_image_new_from_icon_set:
740 * @icon_set: a #GtkIconSet
741 * @size: (type int): a stock icon size
743 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
744 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
745 * this function, usually it's better to create a #GtkIconFactory, put
746 * your icon sets in the icon factory, add the icon factory to the
747 * list of default factories with gtk_icon_factory_add_default(), and
748 * then use gtk_image_new_from_stock(). This will allow themes to
749 * override the icon you ship with your application.
751 * The #GtkImage does not assume a reference to the
752 * icon set; you still need to unref it if you own references.
753 * #GtkImage will add its own reference rather than adopting yours.
755 * Return value: a new #GtkImage
758 gtk_image_new_from_icon_set (GtkIconSet *icon_set,
763 image = g_object_new (GTK_TYPE_IMAGE, NULL);
765 gtk_image_set_from_icon_set (image, icon_set, size);
767 return GTK_WIDGET (image);
771 * gtk_image_new_from_animation:
772 * @animation: an animation
774 * Creates a #GtkImage displaying the given animation.
775 * The #GtkImage does not assume a reference to the
776 * animation; you still need to unref it if you own references.
777 * #GtkImage will add its own reference rather than adopting yours.
779 * Note that the animation frames are shown using a timeout with
780 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
781 * keep in mind that the animation will only be shown if the main loop
782 * is not busy with something that has a higher priority.
784 * Return value: a new #GtkImage widget
787 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
791 g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
793 image = g_object_new (GTK_TYPE_IMAGE, NULL);
795 gtk_image_set_from_animation (image, animation);
797 return GTK_WIDGET (image);
801 * gtk_image_new_from_icon_name:
802 * @icon_name: an icon name
803 * @size: (type int): a stock icon size
805 * Creates a #GtkImage displaying an icon from the current icon theme.
806 * If the icon name isn't known, a "broken image" icon will be
807 * displayed instead. If the current icon theme is changed, the icon
808 * will be updated appropriately.
810 * Return value: a new #GtkImage displaying the themed icon
815 gtk_image_new_from_icon_name (const gchar *icon_name,
820 image = g_object_new (GTK_TYPE_IMAGE, NULL);
822 gtk_image_set_from_icon_name (image, icon_name, size);
824 return GTK_WIDGET (image);
828 * gtk_image_new_from_gicon:
830 * @size: (type int): a stock icon size
832 * Creates a #GtkImage displaying an icon from the current icon theme.
833 * If the icon name isn't known, a "broken image" icon will be
834 * displayed instead. If the current icon theme is changed, the icon
835 * will be updated appropriately.
837 * Return value: a new #GtkImage displaying the themed icon
842 gtk_image_new_from_gicon (GIcon *icon,
847 image = g_object_new (GTK_TYPE_IMAGE, NULL);
849 gtk_image_set_from_gicon (image, icon, size);
851 return GTK_WIDGET (image);
855 * gtk_image_set_from_pixmap:
856 * @image: a #GtkImage
857 * @pixmap: (allow-none): a #GdkPixmap or %NULL
858 * @mask: (allow-none): a #GdkBitmap or %NULL
860 * See gtk_image_new_from_pixmap() for details.
863 gtk_image_set_from_pixmap (GtkImage *image,
869 g_return_if_fail (GTK_IS_IMAGE (image));
870 g_return_if_fail (pixmap == NULL ||
871 GDK_IS_PIXMAP (pixmap));
872 g_return_if_fail (mask == NULL ||
873 GDK_IS_PIXMAP (mask));
877 g_object_freeze_notify (G_OBJECT (image));
880 g_object_ref (pixmap);
885 gtk_image_clear (image);
894 priv->storage_type = GTK_IMAGE_PIXMAP;
896 priv->data.pixmap.pixmap = pixmap;
898 gdk_drawable_get_size (GDK_DRAWABLE (pixmap), &width, &height);
900 gtk_image_update_size (image, width, height);
903 g_object_notify (G_OBJECT (image), "pixmap");
904 g_object_notify (G_OBJECT (image), "mask");
906 g_object_thaw_notify (G_OBJECT (image));
910 * gtk_image_set_from_image:
911 * @image: a #GtkImage
912 * @gdk_image: (allow-none): a #GdkImage or %NULL
913 * @mask: (allow-none): a #GdkBitmap or %NULL
915 * See gtk_image_new_from_image() for details.
918 gtk_image_set_from_image (GtkImage *image,
924 g_return_if_fail (GTK_IS_IMAGE (image));
925 g_return_if_fail (gdk_image == NULL ||
926 GDK_IS_IMAGE (gdk_image));
927 g_return_if_fail (mask == NULL ||
928 GDK_IS_PIXMAP (mask));
932 g_object_freeze_notify (G_OBJECT (image));
935 g_object_ref (gdk_image);
940 gtk_image_clear (image);
944 priv->storage_type = GTK_IMAGE_IMAGE;
946 priv->data.image.image = gdk_image;
949 gtk_image_update_size (image, gdk_image->width, gdk_image->height);
953 /* Clean up the mask if gdk_image was NULL */
955 g_object_unref (mask);
958 g_object_notify (G_OBJECT (image), "image");
959 g_object_notify (G_OBJECT (image), "mask");
961 g_object_thaw_notify (G_OBJECT (image));
965 * gtk_image_set_from_file:
966 * @image: a #GtkImage
967 * @filename: (allow-none): a filename or %NULL
969 * See gtk_image_new_from_file() for details.
972 gtk_image_set_from_file (GtkImage *image,
973 const gchar *filename)
976 GdkPixbufAnimation *anim;
978 g_return_if_fail (GTK_IS_IMAGE (image));
982 g_object_freeze_notify (G_OBJECT (image));
984 gtk_image_clear (image);
986 if (filename == NULL)
988 priv->filename = NULL;
989 g_object_thaw_notify (G_OBJECT (image));
993 anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
997 gtk_image_set_from_stock (image,
998 GTK_STOCK_MISSING_IMAGE,
999 GTK_ICON_SIZE_BUTTON);
1000 g_object_thaw_notify (G_OBJECT (image));
1004 /* We could just unconditionally set_from_animation,
1005 * but it's nicer for memory if we toss the animation
1006 * if it's just a single pixbuf
1009 if (gdk_pixbuf_animation_is_static_image (anim))
1010 gtk_image_set_from_pixbuf (image,
1011 gdk_pixbuf_animation_get_static_image (anim));
1013 gtk_image_set_from_animation (image, anim);
1015 g_object_unref (anim);
1017 priv->filename = g_strdup (filename);
1019 g_object_thaw_notify (G_OBJECT (image));
1023 * gtk_image_set_from_pixbuf:
1024 * @image: a #GtkImage
1025 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
1027 * See gtk_image_new_from_pixbuf() for details.
1030 gtk_image_set_from_pixbuf (GtkImage *image,
1035 g_return_if_fail (GTK_IS_IMAGE (image));
1036 g_return_if_fail (pixbuf == NULL ||
1037 GDK_IS_PIXBUF (pixbuf));
1041 g_object_freeze_notify (G_OBJECT (image));
1044 g_object_ref (pixbuf);
1046 gtk_image_clear (image);
1050 priv->storage_type = GTK_IMAGE_PIXBUF;
1052 priv->data.pixbuf.pixbuf = pixbuf;
1054 gtk_image_update_size (image,
1055 gdk_pixbuf_get_width (pixbuf),
1056 gdk_pixbuf_get_height (pixbuf));
1059 g_object_notify (G_OBJECT (image), "pixbuf");
1061 g_object_thaw_notify (G_OBJECT (image));
1065 * gtk_image_set_from_stock:
1066 * @image: a #GtkImage
1067 * @stock_id: a stock icon name
1068 * @size: (type int): a stock icon size
1070 * See gtk_image_new_from_stock() for details.
1073 gtk_image_set_from_stock (GtkImage *image,
1074 const gchar *stock_id,
1080 g_return_if_fail (GTK_IS_IMAGE (image));
1084 g_object_freeze_notify (G_OBJECT (image));
1086 /* in case stock_id == priv->data.stock.stock_id */
1087 new_id = g_strdup (stock_id);
1089 gtk_image_clear (image);
1093 priv->storage_type = GTK_IMAGE_STOCK;
1095 priv->data.stock.stock_id = new_id;
1096 priv->icon_size = size;
1098 /* Size is demand-computed in size request method
1099 * if we're a stock image, since changing the
1100 * style impacts the size request
1104 g_object_notify (G_OBJECT (image), "stock");
1105 g_object_notify (G_OBJECT (image), "icon-size");
1107 g_object_thaw_notify (G_OBJECT (image));
1111 * gtk_image_set_from_icon_set:
1112 * @image: a #GtkImage
1113 * @icon_set: a #GtkIconSet
1114 * @size: (type int): a stock icon size
1116 * See gtk_image_new_from_icon_set() for details.
1119 gtk_image_set_from_icon_set (GtkImage *image,
1120 GtkIconSet *icon_set,
1125 g_return_if_fail (GTK_IS_IMAGE (image));
1129 g_object_freeze_notify (G_OBJECT (image));
1132 gtk_icon_set_ref (icon_set);
1134 gtk_image_clear (image);
1138 priv->storage_type = GTK_IMAGE_ICON_SET;
1140 priv->data.icon_set.icon_set = icon_set;
1141 priv->icon_size = size;
1143 /* Size is demand-computed in size request method
1144 * if we're an icon set
1148 g_object_notify (G_OBJECT (image), "icon-set");
1149 g_object_notify (G_OBJECT (image), "icon-size");
1151 g_object_thaw_notify (G_OBJECT (image));
1155 * gtk_image_set_from_animation:
1156 * @image: a #GtkImage
1157 * @animation: the #GdkPixbufAnimation
1159 * Causes the #GtkImage to display the given animation (or display
1160 * nothing, if you set the animation to %NULL).
1163 gtk_image_set_from_animation (GtkImage *image,
1164 GdkPixbufAnimation *animation)
1168 g_return_if_fail (GTK_IS_IMAGE (image));
1169 g_return_if_fail (animation == NULL ||
1170 GDK_IS_PIXBUF_ANIMATION (animation));
1174 g_object_freeze_notify (G_OBJECT (image));
1177 g_object_ref (animation);
1179 gtk_image_clear (image);
1181 if (animation != NULL)
1183 priv->storage_type = GTK_IMAGE_ANIMATION;
1185 priv->data.anim.anim = animation;
1186 priv->data.anim.frame_timeout = 0;
1187 priv->data.anim.iter = NULL;
1189 gtk_image_update_size (image,
1190 gdk_pixbuf_animation_get_width (animation),
1191 gdk_pixbuf_animation_get_height (animation));
1194 g_object_notify (G_OBJECT (image), "pixbuf-animation");
1196 g_object_thaw_notify (G_OBJECT (image));
1200 * gtk_image_set_from_icon_name:
1201 * @image: a #GtkImage
1202 * @icon_name: an icon name
1203 * @size: (type int): an icon size
1205 * See gtk_image_new_from_icon_name() for details.
1210 gtk_image_set_from_icon_name (GtkImage *image,
1211 const gchar *icon_name,
1217 g_return_if_fail (GTK_IS_IMAGE (image));
1221 g_object_freeze_notify (G_OBJECT (image));
1223 /* in case icon_name == priv->data.name.icon_name */
1224 new_name = g_strdup (icon_name);
1226 gtk_image_clear (image);
1230 priv->storage_type = GTK_IMAGE_ICON_NAME;
1232 priv->data.name.icon_name = new_name;
1233 priv->icon_size = size;
1235 /* Size is demand-computed in size request method
1236 * if we're a icon theme image, since changing the
1237 * style impacts the size request
1241 g_object_notify (G_OBJECT (image), "icon-name");
1242 g_object_notify (G_OBJECT (image), "icon-size");
1244 g_object_thaw_notify (G_OBJECT (image));
1248 * gtk_image_set_from_gicon:
1249 * @image: a #GtkImage
1251 * @size: (type int): an icon size
1253 * See gtk_image_new_from_gicon() for details.
1258 gtk_image_set_from_gicon (GtkImage *image,
1264 g_return_if_fail (GTK_IS_IMAGE (image));
1268 g_object_freeze_notify (G_OBJECT (image));
1270 /* in case icon == priv->data.gicon.icon */
1272 g_object_ref (icon);
1274 gtk_image_clear (image);
1278 priv->storage_type = GTK_IMAGE_GICON;
1280 priv->data.gicon.icon = icon;
1281 priv->icon_size = size;
1283 /* Size is demand-computed in size request method
1284 * if we're a icon theme image, since changing the
1285 * style impacts the size request
1289 g_object_notify (G_OBJECT (image), "gicon");
1290 g_object_notify (G_OBJECT (image), "icon-size");
1292 g_object_thaw_notify (G_OBJECT (image));
1296 * gtk_image_get_storage_type:
1297 * @image: a #GtkImage
1299 * Gets the type of representation being used by the #GtkImage
1300 * to store image data. If the #GtkImage has no image data,
1301 * the return value will be %GTK_IMAGE_EMPTY.
1303 * Return value: image representation being used
1306 gtk_image_get_storage_type (GtkImage *image)
1308 g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1310 return image->priv->storage_type;
1314 * gtk_image_get_pixmap:
1315 * @image: a #GtkImage
1316 * @pixmap: (out) (transfer none) (allow-none): location to store the
1318 * @mask: (out) (transfer none) (allow-none): location to store the
1321 * Gets the pixmap and mask being displayed by the #GtkImage.
1322 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1323 * %GTK_IMAGE_PIXMAP (see gtk_image_get_storage_type()).
1324 * The caller of this function does not own a reference to the
1325 * returned pixmap and mask.
1328 gtk_image_get_pixmap (GtkImage *image,
1334 g_return_if_fail (GTK_IS_IMAGE (image));
1338 g_return_if_fail (priv->storage_type == GTK_IMAGE_PIXMAP ||
1339 priv->storage_type == GTK_IMAGE_EMPTY);
1342 *pixmap = priv->data.pixmap.pixmap;
1349 * gtk_image_get_image:
1350 * @image: a #GtkImage
1351 * @gdk_image: (out) (transfer none) (allow-none): return location for
1352 * a #GtkImage, or %NULL
1353 * @mask: (out) (transfer none) (allow-none): return location for a
1354 * #GdkBitmap, or %NULL
1356 * Gets the #GdkImage and mask being displayed by the #GtkImage.
1357 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1358 * %GTK_IMAGE_IMAGE (see gtk_image_get_storage_type()).
1359 * The caller of this function does not own a reference to the
1360 * returned image and mask.
1363 gtk_image_get_image (GtkImage *image,
1364 GdkImage **gdk_image,
1369 g_return_if_fail (GTK_IS_IMAGE (image));
1373 g_return_if_fail (priv->storage_type == GTK_IMAGE_IMAGE ||
1374 priv->storage_type == GTK_IMAGE_EMPTY);
1377 *gdk_image = priv->data.image.image;
1384 * gtk_image_get_pixbuf:
1385 * @image: a #GtkImage
1387 * Gets the #GdkPixbuf being displayed by the #GtkImage.
1388 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1389 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1390 * The caller of this function does not own a reference to the
1393 * Return value: (transfer none): the displayed pixbuf, or %NULL if
1394 * the image is empty
1397 gtk_image_get_pixbuf (GtkImage *image)
1401 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1405 g_return_val_if_fail (priv->storage_type == GTK_IMAGE_PIXBUF ||
1406 priv->storage_type == GTK_IMAGE_EMPTY, NULL);
1408 if (priv->storage_type == GTK_IMAGE_EMPTY)
1409 priv->data.pixbuf.pixbuf = NULL;
1411 return priv->data.pixbuf.pixbuf;
1415 * gtk_image_get_stock:
1416 * @image: a #GtkImage
1417 * @stock_id: (out) (transfer none) (allow-none): place to store a
1418 * stock icon name, or %NULL
1419 * @size: (out) (allow-none) (type int): place to store a stock icon
1422 * Gets the stock icon name and size being displayed by the #GtkImage.
1423 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1424 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1425 * The returned string is owned by the #GtkImage and should not
1429 gtk_image_get_stock (GtkImage *image,
1435 g_return_if_fail (GTK_IS_IMAGE (image));
1439 g_return_if_fail (priv->storage_type == GTK_IMAGE_STOCK ||
1440 priv->storage_type == GTK_IMAGE_EMPTY);
1442 if (priv->storage_type == GTK_IMAGE_EMPTY)
1443 priv->data.stock.stock_id = NULL;
1446 *stock_id = priv->data.stock.stock_id;
1449 *size = priv->icon_size;
1453 * gtk_image_get_icon_set:
1454 * @image: a #GtkImage
1455 * @icon_set: (out) (transfer none) (allow-none): location to store a
1456 * #GtkIconSet, or %NULL
1457 * @size: (out) (allow-none) (type int): location to store a stock
1458 * icon size, or %NULL
1460 * Gets the icon set and size being displayed by the #GtkImage.
1461 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1462 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1465 gtk_image_get_icon_set (GtkImage *image,
1466 GtkIconSet **icon_set,
1471 g_return_if_fail (GTK_IS_IMAGE (image));
1475 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_SET ||
1476 priv->storage_type == GTK_IMAGE_EMPTY);
1479 *icon_set = priv->data.icon_set.icon_set;
1482 *size = priv->icon_size;
1486 * gtk_image_get_animation:
1487 * @image: a #GtkImage
1489 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1490 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1491 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1492 * The caller of this function does not own a reference to the
1493 * returned animation.
1495 * Return value: (transfer none): the displayed animation, or %NULL if
1496 * the image is empty
1499 gtk_image_get_animation (GtkImage *image)
1503 g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1507 g_return_val_if_fail (priv->storage_type == GTK_IMAGE_ANIMATION ||
1508 priv->storage_type == GTK_IMAGE_EMPTY,
1511 if (priv->storage_type == GTK_IMAGE_EMPTY)
1512 priv->data.anim.anim = NULL;
1514 return priv->data.anim.anim;
1518 * gtk_image_get_icon_name:
1519 * @image: a #GtkImage
1520 * @icon_name: (out) (transfer none) (allow-none): place to store an
1521 * icon name, or %NULL
1522 * @size: (out) (allow-none) (type int): place to store an icon size,
1525 * Gets the icon name and size being displayed by the #GtkImage.
1526 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1527 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1528 * The returned string is owned by the #GtkImage and should not
1534 gtk_image_get_icon_name (GtkImage *image,
1535 G_CONST_RETURN gchar **icon_name,
1540 g_return_if_fail (GTK_IS_IMAGE (image));
1544 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME ||
1545 priv->storage_type == GTK_IMAGE_EMPTY);
1547 if (priv->storage_type == GTK_IMAGE_EMPTY)
1548 priv->data.name.icon_name = NULL;
1551 *icon_name = priv->data.name.icon_name;
1554 *size = priv->icon_size;
1558 * gtk_image_get_gicon:
1559 * @image: a #GtkImage
1560 * @gicon: (out) (transfer none) (allow-none): place to store a
1562 * @size: (out) (allow-none) (type int): place to store an icon size,
1565 * Gets the #GIcon and size being displayed by the #GtkImage.
1566 * The storage type of the image must be %GTK_IMAGE_EMPTY or
1567 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1568 * The caller of this function does not own a reference to the
1574 gtk_image_get_gicon (GtkImage *image,
1580 g_return_if_fail (GTK_IS_IMAGE (image));
1584 g_return_if_fail (priv->storage_type == GTK_IMAGE_GICON ||
1585 priv->storage_type == GTK_IMAGE_EMPTY);
1587 if (priv->storage_type == GTK_IMAGE_EMPTY)
1588 priv->data.gicon.icon = NULL;
1591 *gicon = priv->data.gicon.icon;
1594 *size = priv->icon_size;
1600 * Creates a new empty #GtkImage widget.
1602 * Return value: a newly created #GtkImage widget.
1605 gtk_image_new (void)
1607 return g_object_new (GTK_TYPE_IMAGE, NULL);
1611 gtk_image_reset_anim_iter (GtkImage *image)
1613 GtkImagePriv *priv = image->priv;
1615 if (priv->storage_type == GTK_IMAGE_ANIMATION)
1617 /* Reset the animation */
1619 if (priv->data.anim.frame_timeout)
1621 g_source_remove (priv->data.anim.frame_timeout);
1622 priv->data.anim.frame_timeout = 0;
1625 if (priv->data.anim.iter)
1627 g_object_unref (priv->data.anim.iter);
1628 priv->data.anim.iter = NULL;
1634 gtk_image_unmap (GtkWidget *widget)
1636 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1638 GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1642 gtk_image_unrealize (GtkWidget *widget)
1644 gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1646 GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1650 animation_timeout (gpointer data)
1652 GtkImage *image = GTK_IMAGE (data);
1653 GtkImagePriv *priv = image->priv;
1656 priv->data.anim.frame_timeout = 0;
1658 gdk_pixbuf_animation_iter_advance (priv->data.anim.iter, NULL);
1660 delay = gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter);
1663 priv->data.anim.frame_timeout =
1664 gdk_threads_add_timeout (delay, animation_timeout, image);
1666 gtk_widget_queue_draw (GTK_WIDGET (image));
1668 if (gtk_widget_is_drawable (GTK_WIDGET (image)))
1669 gdk_window_process_updates (GTK_WIDGET (image)->window, TRUE);
1676 icon_theme_changed (GtkImage *image)
1678 GtkImagePriv *priv = image->priv;
1680 if (priv->storage_type == GTK_IMAGE_ICON_NAME)
1682 if (priv->data.name.pixbuf)
1683 g_object_unref (priv->data.name.pixbuf);
1684 priv->data.name.pixbuf = NULL;
1686 gtk_widget_queue_draw (GTK_WIDGET (image));
1688 if (priv->storage_type == GTK_IMAGE_GICON)
1690 if (priv->data.gicon.pixbuf)
1691 g_object_unref (priv->data.gicon.pixbuf);
1692 priv->data.gicon.pixbuf = NULL;
1694 gtk_widget_queue_draw (GTK_WIDGET (image));
1699 ensure_pixbuf_for_icon_name (GtkImage *image,
1702 GtkImagePriv *priv = image->priv;
1704 GtkIconTheme *icon_theme;
1705 GtkSettings *settings;
1707 gint *sizes, *s, dist;
1709 GtkIconLookupFlags flags;
1711 g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME);
1713 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1714 icon_theme = gtk_icon_theme_get_for_screen (screen);
1715 settings = gtk_settings_get_for_screen (screen);
1716 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1717 if (priv->data.name.pixbuf == NULL ||
1718 (priv->was_symbolic && priv->last_rendered_state != state))
1720 priv->last_rendered_state = state;
1721 if (priv->data.name.pixbuf)
1723 g_object_unref (priv->data.name.pixbuf);
1724 priv->data.name.pixbuf = NULL;
1726 if (priv->pixel_size != -1)
1728 width = height = priv->pixel_size;
1729 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1731 else if (!gtk_icon_size_lookup_for_settings (settings,
1735 if (priv->icon_size == -1)
1737 /* Find an available size close to 48 */
1738 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, priv->data.name.icon_name);
1740 width = height = 48;
1741 for (s = sizes; *s; s++)
1745 width = height = 48;
1752 width = height = *s;
1760 width = height = *s;
1769 g_warning ("Invalid icon size %d\n", priv->icon_size);
1770 width = height = 24;
1774 info = gtk_icon_theme_lookup_icon (icon_theme,
1775 priv->data.name.icon_name,
1776 MIN (width, height), flags);
1780 gboolean was_symbolic;
1782 style = gtk_widget_get_style (GTK_WIDGET (image));
1783 priv->data.name.pixbuf =
1784 gtk_icon_info_load_symbolic_for_style (info,
1788 priv->was_symbolic = was_symbolic;
1789 gtk_icon_info_free (info);
1792 if (priv->data.name.pixbuf == NULL)
1794 priv->data.name.pixbuf =
1795 gtk_widget_render_icon (GTK_WIDGET (image),
1796 GTK_STOCK_MISSING_IMAGE,
1799 priv->was_symbolic = FALSE;
1805 ensure_pixbuf_for_gicon (GtkImage *image,
1808 GtkImagePriv *priv = image->priv;
1810 GtkIconTheme *icon_theme;
1811 GtkSettings *settings;
1814 GtkIconLookupFlags flags;
1816 g_return_if_fail (priv->storage_type == GTK_IMAGE_GICON);
1818 screen = gtk_widget_get_screen (GTK_WIDGET (image));
1819 icon_theme = gtk_icon_theme_get_for_screen (screen);
1820 settings = gtk_settings_get_for_screen (screen);
1821 flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1822 if (priv->data.gicon.pixbuf == NULL ||
1823 (priv->was_symbolic && priv->last_rendered_state != state))
1825 priv->last_rendered_state = state;
1826 if (priv->data.gicon.pixbuf)
1828 g_object_unref (priv->data.gicon.pixbuf);
1829 priv->data.gicon.pixbuf = NULL;
1831 if (priv->pixel_size != -1)
1833 width = height = priv->pixel_size;
1834 flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1836 else if (!gtk_icon_size_lookup_for_settings (settings,
1840 if (priv->icon_size == -1)
1841 width = height = 48;
1844 g_warning ("Invalid icon size %d\n", priv->icon_size);
1845 width = height = 24;
1849 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1850 priv->data.gicon.icon,
1851 MIN (width, height), flags);
1855 gboolean was_symbolic;
1857 style = gtk_widget_get_style (GTK_WIDGET (image));
1858 priv->data.gicon.pixbuf =
1859 gtk_icon_info_load_symbolic_for_style (info,
1863 priv->was_symbolic = was_symbolic;
1864 gtk_icon_info_free (info);
1867 if (priv->data.gicon.pixbuf == NULL)
1869 priv->data.gicon.pixbuf =
1870 gtk_widget_render_icon (GTK_WIDGET (image),
1871 GTK_STOCK_MISSING_IMAGE,
1874 priv->was_symbolic = FALSE;
1881 * Like gdk_rectangle_intersect (dest, src, dest), but make
1882 * sure that the origin of dest is moved by an "even" offset.
1883 * If necessary grow the intersection by one row or column
1886 * This is necessary since we can't pass alignment information
1887 * for the pixelation pattern down to gdk_pixbuf_saturate_and_pixelate(),
1888 * thus we have to makesure that the subimages are properly aligned.
1891 rectangle_intersect_even (GdkRectangle *src,
1899 isect = gdk_rectangle_intersect (dest, src, dest);
1901 if ((dest->x - x + dest->y - y) % 2 != 0)
1919 gtk_image_expose (GtkWidget *widget,
1920 GdkEventExpose *event)
1925 g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1926 g_return_val_if_fail (event != NULL, FALSE);
1928 image = GTK_IMAGE (widget);
1931 if (gtk_widget_get_mapped (widget) &&
1932 priv->storage_type != GTK_IMAGE_EMPTY)
1935 GdkRectangle area, image_bound;
1936 gint x, y, mask_x, mask_y;
1938 gfloat xalign, yalign;
1942 gboolean needs_state_transform;
1944 misc = GTK_MISC (widget);
1947 /* For stock items and icon sets, we lazily calculate
1948 * the size; we might get here between a queue_resize()
1949 * and size_request() if something explicitely forces
1952 if (priv->need_calc_size)
1953 gtk_image_calc_size (image);
1955 if (!gdk_rectangle_intersect (&area, &widget->allocation, &area))
1958 gtk_misc_get_alignment (misc, &xalign, &yalign);
1959 gtk_misc_get_padding (misc, &xpad, &ypad);
1961 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
1962 xalign = 1.0 - xalign;
1964 x = floor (widget->allocation.x + xpad
1965 + ((widget->allocation.width - widget->requisition.width) * xalign));
1966 y = floor (widget->allocation.y + ypad
1967 + ((widget->allocation.height - widget->requisition.height) * yalign));
1973 image_bound.width = 0;
1974 image_bound.height = 0;
1978 needs_state_transform = gtk_widget_get_state (widget) != GTK_STATE_NORMAL;
1980 switch (priv->storage_type)
1982 case GTK_IMAGE_PIXMAP:
1984 gdk_drawable_get_size (priv->data.pixmap.pixmap,
1986 &image_bound.height);
1987 if (rectangle_intersect_even (&area, &image_bound) &&
1988 needs_state_transform)
1990 pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1991 priv->data.pixmap.pixmap,
1992 gtk_widget_get_colormap (widget),
1993 image_bound.x - x, image_bound.y - y,
1996 image_bound.height);
2004 case GTK_IMAGE_IMAGE:
2006 image_bound.width = priv->data.image.image->width;
2007 image_bound.height = priv->data.image.image->height;
2009 if (rectangle_intersect_even (&area, &image_bound) &&
2010 needs_state_transform)
2012 pixbuf = gdk_pixbuf_get_from_image (NULL,
2013 priv->data.image.image,
2014 gtk_widget_get_colormap (widget),
2015 image_bound.x - x, image_bound.y - y,
2018 image_bound.height);
2025 case GTK_IMAGE_PIXBUF:
2026 image_bound.width = gdk_pixbuf_get_width (priv->data.pixbuf.pixbuf);
2027 image_bound.height = gdk_pixbuf_get_height (priv->data.pixbuf.pixbuf);
2029 if (rectangle_intersect_even (&area, &image_bound) &&
2030 needs_state_transform)
2032 pixbuf = gdk_pixbuf_new_subpixbuf (priv->data.pixbuf.pixbuf,
2033 image_bound.x - x, image_bound.y - y,
2034 image_bound.width, image_bound.height);
2041 pixbuf = priv->data.pixbuf.pixbuf;
2042 g_object_ref (pixbuf);
2046 case GTK_IMAGE_STOCK:
2047 pixbuf = gtk_widget_render_icon (widget,
2048 priv->data.stock.stock_id,
2053 image_bound.width = gdk_pixbuf_get_width (pixbuf);
2054 image_bound.height = gdk_pixbuf_get_height (pixbuf);
2058 needs_state_transform = FALSE;
2061 case GTK_IMAGE_ICON_SET:
2063 gtk_icon_set_render_icon (priv->data.icon_set.icon_set,
2065 gtk_widget_get_direction (widget),
2066 gtk_widget_get_state (widget),
2073 image_bound.width = gdk_pixbuf_get_width (pixbuf);
2074 image_bound.height = gdk_pixbuf_get_height (pixbuf);
2078 needs_state_transform = FALSE;
2081 case GTK_IMAGE_ANIMATION:
2083 if (priv->data.anim.iter == NULL)
2085 priv->data.anim.iter = gdk_pixbuf_animation_get_iter (priv->data.anim.anim, NULL);
2087 if (gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter) >= 0)
2088 priv->data.anim.frame_timeout =
2089 gdk_threads_add_timeout (gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter),
2094 image_bound.width = gdk_pixbuf_animation_get_width (priv->data.anim.anim);
2095 image_bound.height = gdk_pixbuf_animation_get_height (priv->data.anim.anim);
2097 /* don't advance the anim iter here, or we could get frame changes between two
2098 * exposes of different areas.
2101 pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (priv->data.anim.iter);
2102 g_object_ref (pixbuf);
2106 case GTK_IMAGE_ICON_NAME:
2107 state = gtk_widget_get_state (widget);
2108 if (state == GTK_STATE_INSENSITIVE)
2110 ensure_pixbuf_for_icon_name (image, GTK_STATE_NORMAL);
2114 ensure_pixbuf_for_icon_name (image, state);
2115 /* Already done by the loading function? */
2116 if (priv->was_symbolic)
2117 needs_state_transform = FALSE;
2119 pixbuf = priv->data.name.pixbuf;
2122 g_object_ref (pixbuf);
2123 image_bound.width = gdk_pixbuf_get_width (pixbuf);
2124 image_bound.height = gdk_pixbuf_get_height (pixbuf);
2128 case GTK_IMAGE_GICON:
2129 state = gtk_widget_get_state (widget);
2130 if (state == GTK_STATE_INSENSITIVE)
2132 ensure_pixbuf_for_gicon (image, GTK_STATE_NORMAL);
2136 ensure_pixbuf_for_gicon (image, state);
2137 /* Already done by the loading function? */
2138 if (priv->was_symbolic)
2139 needs_state_transform = FALSE;
2141 pixbuf = priv->data.gicon.pixbuf;
2144 g_object_ref (pixbuf);
2145 image_bound.width = gdk_pixbuf_get_width (pixbuf);
2146 image_bound.height = gdk_pixbuf_get_height (pixbuf);
2150 case GTK_IMAGE_EMPTY:
2151 g_assert_not_reached ();
2157 gdk_gc_set_clip_mask (widget->style->black_gc, mask);
2158 gdk_gc_set_clip_origin (widget->style->black_gc, mask_x, mask_y);
2161 if (rectangle_intersect_even (&area, &image_bound))
2165 if (needs_state_transform)
2167 GtkIconSource *source;
2168 GdkPixbuf *rendered;
2170 source = gtk_icon_source_new ();
2171 gtk_icon_source_set_pixbuf (source, pixbuf);
2172 /* The size here is arbitrary; since size isn't
2173 * wildcarded in the souce, it isn't supposed to be
2174 * scaled by the engine function
2176 gtk_icon_source_set_size (source,
2177 GTK_ICON_SIZE_SMALL_TOOLBAR);
2178 gtk_icon_source_set_size_wildcarded (source, FALSE);
2180 rendered = gtk_style_render_icon (widget->style,
2182 gtk_widget_get_direction (widget),
2183 gtk_widget_get_state (widget),
2189 gtk_icon_source_free (source);
2191 g_object_unref (pixbuf);
2197 cairo_t *cr = gdk_cairo_create (widget->window);
2198 gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
2199 gdk_cairo_rectangle (cr, &image_bound);
2206 switch (priv->storage_type)
2208 case GTK_IMAGE_PIXMAP:
2209 gdk_draw_drawable (widget->window,
2210 widget->style->black_gc,
2211 priv->data.pixmap.pixmap,
2212 image_bound.x - x, image_bound.y - y,
2213 image_bound.x, image_bound.y,
2214 image_bound.width, image_bound.height);
2217 case GTK_IMAGE_IMAGE:
2218 gdk_draw_image (widget->window,
2219 widget->style->black_gc,
2220 priv->data.image.image,
2221 image_bound.x - x, image_bound.y - y,
2222 image_bound.x, image_bound.y,
2223 image_bound.width, image_bound.height);
2226 case GTK_IMAGE_PIXBUF:
2227 case GTK_IMAGE_STOCK:
2228 case GTK_IMAGE_ICON_SET:
2229 case GTK_IMAGE_ANIMATION:
2230 case GTK_IMAGE_ICON_NAME:
2231 case GTK_IMAGE_EMPTY:
2232 case GTK_IMAGE_GICON:
2233 g_assert_not_reached ();
2237 } /* if rectangle intersects */
2241 gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
2242 gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
2246 g_object_unref (pixbuf);
2248 } /* if widget is drawable */
2254 gtk_image_reset (GtkImage *image)
2256 GtkImagePriv *priv = image->priv;
2258 g_object_freeze_notify (G_OBJECT (image));
2260 if (priv->storage_type != GTK_IMAGE_EMPTY)
2261 g_object_notify (G_OBJECT (image), "storage-type");
2265 g_object_unref (priv->mask);
2267 g_object_notify (G_OBJECT (image), "mask");
2270 if (priv->icon_size != DEFAULT_ICON_SIZE)
2272 priv->icon_size = DEFAULT_ICON_SIZE;
2273 g_object_notify (G_OBJECT (image), "icon-size");
2276 switch (priv->storage_type)
2278 case GTK_IMAGE_PIXMAP:
2280 if (priv->data.pixmap.pixmap)
2281 g_object_unref (priv->data.pixmap.pixmap);
2282 priv->data.pixmap.pixmap = NULL;
2284 g_object_notify (G_OBJECT (image), "pixmap");
2288 case GTK_IMAGE_IMAGE:
2290 if (priv->data.image.image)
2291 g_object_unref (priv->data.image.image);
2292 priv->data.image.image = NULL;
2294 g_object_notify (G_OBJECT (image), "image");
2298 case GTK_IMAGE_PIXBUF:
2300 if (priv->data.pixbuf.pixbuf)
2301 g_object_unref (priv->data.pixbuf.pixbuf);
2303 g_object_notify (G_OBJECT (image), "pixbuf");
2307 case GTK_IMAGE_STOCK:
2309 g_free (priv->data.stock.stock_id);
2311 priv->data.stock.stock_id = NULL;
2313 g_object_notify (G_OBJECT (image), "stock");
2316 case GTK_IMAGE_ICON_SET:
2317 if (priv->data.icon_set.icon_set)
2318 gtk_icon_set_unref (priv->data.icon_set.icon_set);
2319 priv->data.icon_set.icon_set = NULL;
2321 g_object_notify (G_OBJECT (image), "icon-set");
2324 case GTK_IMAGE_ANIMATION:
2325 gtk_image_reset_anim_iter (image);
2327 if (priv->data.anim.anim)
2328 g_object_unref (priv->data.anim.anim);
2329 priv->data.anim.anim = NULL;
2331 g_object_notify (G_OBJECT (image), "pixbuf-animation");
2335 case GTK_IMAGE_ICON_NAME:
2336 g_free (priv->data.name.icon_name);
2337 priv->data.name.icon_name = NULL;
2338 if (priv->data.name.pixbuf)
2339 g_object_unref (priv->data.name.pixbuf);
2340 priv->data.name.pixbuf = NULL;
2342 g_object_notify (G_OBJECT (image), "icon-name");
2346 case GTK_IMAGE_GICON:
2347 if (priv->data.gicon.icon)
2348 g_object_unref (priv->data.gicon.icon);
2349 priv->data.gicon.icon = NULL;
2350 if (priv->data.gicon.pixbuf)
2351 g_object_unref (priv->data.gicon.pixbuf);
2352 priv->data.gicon.pixbuf = NULL;
2354 g_object_notify (G_OBJECT (image), "gicon");
2358 case GTK_IMAGE_EMPTY:
2366 g_free (priv->filename);
2367 priv->filename = NULL;
2368 g_object_notify (G_OBJECT (image), "file");
2371 priv->storage_type = GTK_IMAGE_EMPTY;
2373 memset (&priv->data, '\0', sizeof (priv->data));
2375 g_object_thaw_notify (G_OBJECT (image));
2380 * @image: a #GtkImage
2382 * Resets the image to be empty.
2387 gtk_image_clear (GtkImage *image)
2389 GtkImagePriv *priv = image->priv;
2391 priv->need_calc_size = 1;
2393 gtk_image_reset (image);
2394 gtk_image_update_size (image, 0, 0);
2398 gtk_image_calc_size (GtkImage *image)
2400 GtkWidget *widget = GTK_WIDGET (image);
2401 GtkImagePriv *priv = image->priv;
2402 GdkPixbuf *pixbuf = NULL;
2404 priv->need_calc_size = 0;
2406 /* We update stock/icon set on every size request, because
2407 * the theme could have affected the size; for other kinds of
2408 * image, we just update the requisition when the image data
2411 switch (priv->storage_type)
2413 case GTK_IMAGE_STOCK:
2414 pixbuf = gtk_widget_render_icon (widget,
2415 priv->data.stock.stock_id,
2420 case GTK_IMAGE_ICON_SET:
2421 pixbuf = gtk_icon_set_render_icon (priv->data.icon_set.icon_set,
2423 gtk_widget_get_direction (widget),
2424 gtk_widget_get_state (widget),
2429 case GTK_IMAGE_ICON_NAME:
2430 ensure_pixbuf_for_icon_name (image, GTK_STATE_NORMAL);
2431 pixbuf = priv->data.name.pixbuf;
2432 if (pixbuf) g_object_ref (pixbuf);
2434 case GTK_IMAGE_GICON:
2435 ensure_pixbuf_for_gicon (image, GTK_STATE_NORMAL);
2436 pixbuf = priv->data.gicon.pixbuf;
2438 g_object_ref (pixbuf);
2448 gtk_misc_get_padding (GTK_MISC (image), &xpad, &ypad);
2450 widget->requisition.width = gdk_pixbuf_get_width (pixbuf) + xpad * 2;
2451 widget->requisition.height = gdk_pixbuf_get_height (pixbuf) + ypad * 2;
2453 g_object_unref (pixbuf);
2458 gtk_image_size_request (GtkWidget *widget,
2459 GtkRequisition *requisition)
2463 image = GTK_IMAGE (widget);
2465 gtk_image_calc_size (image);
2467 /* Chain up to default that simply reads current requisition */
2468 GTK_WIDGET_CLASS (gtk_image_parent_class)->size_request (widget, requisition);
2472 gtk_image_style_set (GtkWidget *widget,
2473 GtkStyle *prev_style)
2477 image = GTK_IMAGE (widget);
2479 GTK_WIDGET_CLASS (gtk_image_parent_class)->style_set (widget, prev_style);
2481 icon_theme_changed (image);
2485 gtk_image_screen_changed (GtkWidget *widget,
2486 GdkScreen *prev_screen)
2490 image = GTK_IMAGE (widget);
2492 if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
2493 GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
2495 icon_theme_changed (image);
2500 gtk_image_update_size (GtkImage *image,
2504 GtkWidget *widget = GTK_WIDGET (image);
2507 gtk_misc_get_padding (GTK_MISC (image), &xpad, &ypad);
2509 widget->requisition.width = image_width + xpad * 2;
2510 widget->requisition.height = image_height + ypad * 2;
2512 if (gtk_widget_get_visible (widget))
2513 gtk_widget_queue_resize (widget);
2518 * gtk_image_set_pixel_size:
2519 * @image: a #GtkImage
2520 * @pixel_size: the new pixel size
2522 * Sets the pixel size to use for named icons. If the pixel size is set
2523 * to a value != -1, it is used instead of the icon size set by
2524 * gtk_image_set_from_icon_name().
2529 gtk_image_set_pixel_size (GtkImage *image,
2534 g_return_if_fail (GTK_IS_IMAGE (image));
2538 if (priv->pixel_size != pixel_size)
2540 priv->pixel_size = pixel_size;
2542 if (priv->storage_type == GTK_IMAGE_ICON_NAME)
2544 if (priv->data.name.pixbuf)
2546 g_object_unref (priv->data.name.pixbuf);
2547 priv->data.name.pixbuf = NULL;
2550 gtk_image_update_size (image, pixel_size, pixel_size);
2553 if (priv->storage_type == GTK_IMAGE_GICON)
2555 if (priv->data.gicon.pixbuf)
2557 g_object_unref (priv->data.gicon.pixbuf);
2558 priv->data.gicon.pixbuf = NULL;
2561 gtk_image_update_size (image, pixel_size, pixel_size);
2564 g_object_notify (G_OBJECT (image), "pixel-size");
2569 * gtk_image_get_pixel_size:
2570 * @image: a #GtkImage
2572 * Gets the pixel size used for named icons.
2574 * Returns: the pixel size used for named icons.
2579 gtk_image_get_pixel_size (GtkImage *image)
2581 g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
2583 return image->priv->pixel_size;
2586 #if defined (G_OS_WIN32) && !defined (_WIN64)
2588 #undef gtk_image_new_from_file
2591 gtk_image_new_from_file (const gchar *filename)
2593 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2596 retval = gtk_image_new_from_file_utf8 (utf8_filename);
2598 g_free (utf8_filename);
2603 #undef gtk_image_set_from_file
2606 gtk_image_set_from_file (GtkImage *image,
2607 const gchar *filename)
2609 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2611 gtk_image_set_from_file_utf8 (image, utf8_filename);
2613 g_free (utf8_filename);