]> Pileus Git - ~andy/gtk/blob - gtk/gtkimage.c
Fix a memory leak in GtkImage
[~andy/gtk] / gtk / gtkimage.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 #include "config.h"
26
27 #include <math.h>
28 #include <string.h>
29
30 #include "gtkcontainer.h"
31 #include "gtkiconhelperprivate.h"
32 #include "gtkimageprivate.h"
33 #include "gtkiconfactory.h"
34 #include "gtkstock.h"
35 #include "gtkicontheme.h"
36 #include "gtksizerequest.h"
37 #include "gtkintl.h"
38 #include "gtkprivate.h"
39 #include "gtktypebuiltins.h"
40
41 #include "a11y/gtkimageaccessible.h"
42
43 /**
44  * SECTION:gtkimage
45  * @Short_description: A widget displaying an image
46  * @Title: GtkImage
47  * @See_also:#GdkPixbuf
48  *
49  * The #GtkImage widget displays an image. Various kinds of object
50  * can be displayed as an image; most typically, you would load a
51  * #GdkPixbuf ("pixel buffer") from a file, and then display that.
52  * There's a convenience function to do this, gtk_image_new_from_file(),
53  * used as follows:
54  * <informalexample><programlisting>
55  *   GtkWidget *image;
56  *   image = gtk_image_new_from_file ("myfile.png");
57  * </programlisting></informalexample>
58  * If the file isn't loaded successfully, the image will contain a
59  * "broken image" icon similar to that used in many web browsers.
60  * If you want to handle errors in loading the file yourself,
61  * for example by displaying an error message, then load the image with
62  * gdk_pixbuf_new_from_file(), then create the #GtkImage with
63  * gtk_image_new_from_pixbuf().
64  *
65  * The image file may contain an animation, if so the #GtkImage will
66  * display an animation (#GdkPixbufAnimation) instead of a static image.
67  *
68  * #GtkImage is a subclass of #GtkMisc, which implies that you can
69  * align it (center, left, right) and add padding to it, using
70  * #GtkMisc methods.
71  *
72  * #GtkImage is a "no window" widget (has no #GdkWindow of its own),
73  * so by default does not receive events. If you want to receive events
74  * on the image, such as button clicks, place the image inside a
75  * #GtkEventBox, then connect to the event signals on the event box.
76  * <example>
77  * <title>Handling button press events on a
78  * <structname>GtkImage</structname>.</title>
79  * <programlisting>
80  *   static gboolean
81  *   button_press_callback (GtkWidget      *event_box,
82  *                          GdkEventButton *event,
83  *                          gpointer        data)
84  *   {
85  *     g_print ("Event box clicked at coordinates &percnt;f,&percnt;f\n",
86  *              event->x, event->y);
87  *
88  *     /<!---->* Returning TRUE means we handled the event, so the signal
89  *      * emission should be stopped (don't call any further
90  *      * callbacks that may be connected). Return FALSE
91  *      * to continue invoking callbacks.
92  *      *<!---->/
93  *     return TRUE;
94  *   }
95  *
96  *   static GtkWidget*
97  *   create_image (void)
98  *   {
99  *     GtkWidget *image;
100  *     GtkWidget *event_box;
101  *
102  *     image = gtk_image_new_from_file ("myfile.png");
103  *
104  *     event_box = gtk_event_box_new (<!-- -->);
105  *
106  *     gtk_container_add (GTK_CONTAINER (event_box), image);
107  *
108  *     g_signal_connect (G_OBJECT (event_box),
109  *                       "button_press_event",
110  *                       G_CALLBACK (button_press_callback),
111  *                       image);
112  *
113  *     return image;
114  *   }
115  * </programlisting>
116  * </example>
117  *
118  * When handling events on the event box, keep in mind that coordinates
119  * in the image may be different from event box coordinates due to
120  * the alignment and padding settings on the image (see #GtkMisc).
121  * The simplest way to solve this is to set the alignment to 0.0
122  * (left/top), and set the padding to zero. Then the origin of
123  * the image will be the same as the origin of the event box.
124  *
125  * Sometimes an application will want to avoid depending on external data
126  * files, such as image files. GTK+ comes with a program to avoid this,
127  * called <application>gdk-pixbuf-csource</application>. This library
128  * allows you to convert an image into a C variable declaration, which
129  * can then be loaded into a #GdkPixbuf using
130  * gdk_pixbuf_new_from_inline().
131  */
132
133
134 struct _GtkImagePrivate
135 {
136   GtkIconHelper *icon_helper;
137
138   gint animation_timeout;
139   GdkPixbufAnimationIter *animation_iter;
140
141   gchar                *filename;       /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
142 };
143
144
145 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
146 static gint gtk_image_draw                 (GtkWidget    *widget,
147                                             cairo_t      *cr);
148 static void gtk_image_unmap                (GtkWidget    *widget);
149 static void gtk_image_unrealize            (GtkWidget    *widget);
150 static void gtk_image_get_preferred_width  (GtkWidget    *widget,
151                                             gint         *minimum,
152                                             gint         *natural);
153 static void gtk_image_get_preferred_height (GtkWidget    *widget,
154                                             gint         *minimum,
155                                             gint         *natural);
156
157 static void gtk_image_style_updated        (GtkWidget    *widget);
158 static void gtk_image_screen_changed       (GtkWidget    *widget,
159                                             GdkScreen    *prev_screen);
160 static void gtk_image_finalize             (GObject      *object);
161 static void gtk_image_reset                (GtkImage     *image);
162
163 static void gtk_image_set_property         (GObject      *object,
164                                             guint         prop_id,
165                                             const GValue *value,
166                                             GParamSpec   *pspec);
167 static void gtk_image_get_property         (GObject      *object,
168                                             guint         prop_id,
169                                             GValue       *value,
170                                             GParamSpec   *pspec);
171
172 static void icon_theme_changed             (GtkImage     *image);
173
174 enum
175 {
176   PROP_0,
177   PROP_PIXBUF,
178   PROP_FILE,
179   PROP_STOCK,
180   PROP_ICON_SET,
181   PROP_ICON_SIZE,
182   PROP_PIXEL_SIZE,
183   PROP_PIXBUF_ANIMATION,
184   PROP_ICON_NAME,
185   PROP_STORAGE_TYPE,
186   PROP_GICON,
187   PROP_USE_FALLBACK
188 };
189
190 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
191
192 static void
193 gtk_image_class_init (GtkImageClass *class)
194 {
195   GObjectClass *gobject_class;
196   GtkWidgetClass *widget_class;
197
198   gobject_class = G_OBJECT_CLASS (class);
199   
200   gobject_class->set_property = gtk_image_set_property;
201   gobject_class->get_property = gtk_image_get_property;
202   gobject_class->finalize = gtk_image_finalize;
203
204   widget_class = GTK_WIDGET_CLASS (class);
205   widget_class->draw = gtk_image_draw;
206   widget_class->get_preferred_width = gtk_image_get_preferred_width;
207   widget_class->get_preferred_height = gtk_image_get_preferred_height;
208   widget_class->unmap = gtk_image_unmap;
209   widget_class->unrealize = gtk_image_unrealize;
210   widget_class->style_updated = gtk_image_style_updated;
211   widget_class->screen_changed = gtk_image_screen_changed;
212   
213   g_object_class_install_property (gobject_class,
214                                    PROP_PIXBUF,
215                                    g_param_spec_object ("pixbuf",
216                                                         P_("Pixbuf"),
217                                                         P_("A GdkPixbuf to display"),
218                                                         GDK_TYPE_PIXBUF,
219                                                         GTK_PARAM_READWRITE));
220
221   g_object_class_install_property (gobject_class,
222                                    PROP_FILE,
223                                    g_param_spec_string ("file",
224                                                         P_("Filename"),
225                                                         P_("Filename to load and display"),
226                                                         NULL,
227                                                         GTK_PARAM_READWRITE));
228   
229
230   g_object_class_install_property (gobject_class,
231                                    PROP_STOCK,
232                                    g_param_spec_string ("stock",
233                                                         P_("Stock ID"),
234                                                         P_("Stock ID for a stock image to display"),
235                                                         NULL,
236                                                         GTK_PARAM_READWRITE));
237   
238   g_object_class_install_property (gobject_class,
239                                    PROP_ICON_SET,
240                                    g_param_spec_boxed ("icon-set",
241                                                        P_("Icon set"),
242                                                        P_("Icon set to display"),
243                                                        GTK_TYPE_ICON_SET,
244                                                        GTK_PARAM_READWRITE));
245   
246   g_object_class_install_property (gobject_class,
247                                    PROP_ICON_SIZE,
248                                    g_param_spec_int ("icon-size",
249                                                      P_("Icon size"),
250                                                      P_("Symbolic size to use for stock icon, icon set or named icon"),
251                                                      0, G_MAXINT,
252                                                      DEFAULT_ICON_SIZE,
253                                                      GTK_PARAM_READWRITE));
254   /**
255    * GtkImage:pixel-size:
256    *
257    * The "pixel-size" property can be used to specify a fixed size
258    * overriding the #GtkImage:icon-size property for images of type 
259    * %GTK_IMAGE_ICON_NAME. 
260    *
261    * Since: 2.6
262    */
263   g_object_class_install_property (gobject_class,
264                                    PROP_PIXEL_SIZE,
265                                    g_param_spec_int ("pixel-size",
266                                                      P_("Pixel size"),
267                                                      P_("Pixel size to use for named icon"),
268                                                      -1, G_MAXINT,
269                                                      -1,
270                                                      GTK_PARAM_READWRITE));
271   
272   g_object_class_install_property (gobject_class,
273                                    PROP_PIXBUF_ANIMATION,
274                                    g_param_spec_object ("pixbuf-animation",
275                                                         P_("Animation"),
276                                                         P_("GdkPixbufAnimation to display"),
277                                                         GDK_TYPE_PIXBUF_ANIMATION,
278                                                         GTK_PARAM_READWRITE));
279
280   /**
281    * GtkImage:icon-name:
282    *
283    * The name of the icon in the icon theme. If the icon theme is
284    * changed, the image will be updated automatically.
285    *
286    * Since: 2.6
287    */
288   g_object_class_install_property (gobject_class,
289                                    PROP_ICON_NAME,
290                                    g_param_spec_string ("icon-name",
291                                                         P_("Icon Name"),
292                                                         P_("The name of the icon from the icon theme"),
293                                                         NULL,
294                                                         GTK_PARAM_READWRITE));
295   
296   /**
297    * GtkImage:gicon:
298    *
299    * The GIcon displayed in the GtkImage. For themed icons,
300    * If the icon theme is changed, the image will be updated
301    * automatically.
302    *
303    * Since: 2.14
304    */
305   g_object_class_install_property (gobject_class,
306                                    PROP_GICON,
307                                    g_param_spec_object ("gicon",
308                                                         P_("Icon"),
309                                                         P_("The GIcon being displayed"),
310                                                         G_TYPE_ICON,
311                                                         GTK_PARAM_READWRITE));
312   
313   g_object_class_install_property (gobject_class,
314                                    PROP_STORAGE_TYPE,
315                                    g_param_spec_enum ("storage-type",
316                                                       P_("Storage type"),
317                                                       P_("The representation being used for image data"),
318                                                       GTK_TYPE_IMAGE_TYPE,
319                                                       GTK_IMAGE_EMPTY,
320                                                       GTK_PARAM_READABLE));
321
322   /**
323    * GtkImage:use-fallback:
324    *
325    * Whether the icon displayed in the GtkImage will use
326    * standard icon names fallback. The value of this property
327    * is only relevant for images of type %GTK_IMAGE_ICON_NAME
328    * and %GTK_IMAGE_GICON.
329    *
330    * Since: 3.0
331    */
332   g_object_class_install_property (gobject_class,
333                                    PROP_USE_FALLBACK,
334                                    g_param_spec_boolean ("use-fallback",
335                                                          P_("Use Fallback"),
336                                                          P_("Whether to use icon names fallback"),
337                                                          FALSE,
338                                                          GTK_PARAM_READWRITE));
339
340   g_type_class_add_private (class, sizeof (GtkImagePrivate));
341
342   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_IMAGE_ACCESSIBLE);
343 }
344
345 static void
346 gtk_image_init (GtkImage *image)
347 {
348   GtkImagePrivate *priv;
349
350   image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image,
351                                              GTK_TYPE_IMAGE,
352                                              GtkImagePrivate);
353   priv = image->priv;
354
355   gtk_widget_set_has_window (GTK_WIDGET (image), FALSE);
356   priv->icon_helper = _gtk_icon_helper_new ();
357
358   priv->filename = NULL;
359 }
360
361 static void
362 gtk_image_finalize (GObject *object)
363 {
364   GtkImage *image = GTK_IMAGE (object);
365
366   g_clear_object (&image->priv->icon_helper);
367   
368   g_free (image->priv->filename);
369
370   G_OBJECT_CLASS (gtk_image_parent_class)->finalize (object);
371 };
372
373 static void 
374 gtk_image_set_property (GObject      *object,
375                         guint         prop_id,
376                         const GValue *value,
377                         GParamSpec   *pspec)
378 {
379   GtkImage *image = GTK_IMAGE (object);
380   GtkImagePrivate *priv = image->priv;
381   GtkIconSize icon_size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
382
383   if (icon_size == GTK_ICON_SIZE_INVALID)
384     icon_size = DEFAULT_ICON_SIZE;
385
386   switch (prop_id)
387     {
388     case PROP_PIXBUF:
389       gtk_image_set_from_pixbuf (image,
390                                  g_value_get_object (value));
391       break;
392     case PROP_FILE:
393       gtk_image_set_from_file (image, g_value_get_string (value));
394       break;
395     case PROP_STOCK:
396       gtk_image_set_from_stock (image, g_value_get_string (value),
397                                 icon_size);
398       break;
399     case PROP_ICON_SET:
400       gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
401                                    icon_size);
402       break;
403     case PROP_ICON_SIZE:
404       _gtk_icon_helper_set_icon_size (priv->icon_helper, g_value_get_int (value));
405       break;
406     case PROP_PIXEL_SIZE:
407       gtk_image_set_pixel_size (image, g_value_get_int (value));
408       break;
409     case PROP_PIXBUF_ANIMATION:
410       gtk_image_set_from_animation (image,
411                                     g_value_get_object (value));
412       break;
413     case PROP_ICON_NAME:
414       gtk_image_set_from_icon_name (image, g_value_get_string (value),
415                                     icon_size);
416       break;
417     case PROP_GICON:
418       gtk_image_set_from_gicon (image, g_value_get_object (value),
419                                 icon_size);
420       break;
421
422     case PROP_USE_FALLBACK:
423       _gtk_icon_helper_set_use_fallback (priv->icon_helper, g_value_get_boolean (value));
424       break;
425
426     default:
427       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
428       break;
429     }
430 }
431
432 static void 
433 gtk_image_get_property (GObject     *object,
434                         guint        prop_id,
435                         GValue      *value,
436                         GParamSpec  *pspec)
437 {
438   GtkImage *image = GTK_IMAGE (object);
439   GtkImagePrivate *priv = image->priv;
440
441   switch (prop_id)
442     {
443     case PROP_PIXBUF:
444       g_value_set_object (value, _gtk_icon_helper_peek_pixbuf (priv->icon_helper));
445       break;
446     case PROP_FILE:
447       g_value_set_string (value, priv->filename);
448       break;
449     case PROP_STOCK:
450       g_value_set_string (value, _gtk_icon_helper_get_stock_id (priv->icon_helper));
451       break;
452     case PROP_ICON_SET:
453       g_value_set_boxed (value, _gtk_icon_helper_peek_icon_set (priv->icon_helper));
454       break;      
455     case PROP_ICON_SIZE:
456       g_value_set_int (value, _gtk_icon_helper_get_icon_size (priv->icon_helper));
457       break;
458     case PROP_PIXEL_SIZE:
459       g_value_set_int (value, _gtk_icon_helper_get_pixel_size (priv->icon_helper));
460       break;
461     case PROP_PIXBUF_ANIMATION:
462       g_value_set_object (value, _gtk_icon_helper_peek_animation (priv->icon_helper));
463       break;
464     case PROP_ICON_NAME:
465       g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
466       break;
467     case PROP_GICON:
468       g_value_set_object (value, _gtk_icon_helper_peek_gicon (priv->icon_helper));
469       break;
470     case PROP_USE_FALLBACK:
471       g_value_set_boolean (value, _gtk_icon_helper_get_use_fallback (priv->icon_helper));
472       break;
473     case PROP_STORAGE_TYPE:
474       g_value_set_enum (value, _gtk_icon_helper_get_storage_type (priv->icon_helper));
475       break;
476     default:
477       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
478       break;
479     }
480 }
481
482
483 /**
484  * gtk_image_new_from_file:
485  * @filename: (type filename): a filename
486  * 
487  * Creates a new #GtkImage displaying the file @filename. If the file
488  * isn't found or can't be loaded, the resulting #GtkImage will
489  * display a "broken image" icon. This function never returns %NULL,
490  * it always returns a valid #GtkImage widget.
491  *
492  * If the file contains an animation, the image will contain an
493  * animation.
494  *
495  * If you need to detect failures to load the file, use
496  * gdk_pixbuf_new_from_file() to load the file yourself, then create
497  * the #GtkImage from the pixbuf. (Or for animations, use
498  * gdk_pixbuf_animation_new_from_file()).
499  *
500  * The storage type (gtk_image_get_storage_type()) of the returned
501  * image is not defined, it will be whatever is appropriate for
502  * displaying the file.
503  * 
504  * Return value: a new #GtkImage
505  **/
506 GtkWidget*
507 gtk_image_new_from_file   (const gchar *filename)
508 {
509   GtkImage *image;
510
511   image = g_object_new (GTK_TYPE_IMAGE, NULL);
512
513   gtk_image_set_from_file (image, filename);
514
515   return GTK_WIDGET (image);
516 }
517
518 /**
519  * gtk_image_new_from_resource:
520  * @resource_path: a resource path
521  *
522  * Creates a new #GtkImage displaying the resource file @resource_path. If the file
523  * isn't found or can't be loaded, the resulting #GtkImage will
524  * display a "broken image" icon. This function never returns %NULL,
525  * it always returns a valid #GtkImage widget.
526  *
527  * If the file contains an animation, the image will contain an
528  * animation.
529  *
530  * If you need to detect failures to load the file, use
531  * gdk_pixbuf_new_from_file() to load the file yourself, then create
532  * the #GtkImage from the pixbuf. (Or for animations, use
533  * gdk_pixbuf_animation_new_from_file()).
534  *
535  * The storage type (gtk_image_get_storage_type()) of the returned
536  * image is not defined, it will be whatever is appropriate for
537  * displaying the file.
538  *
539  * Return value: a new #GtkImage
540  *
541  * Since: 3.4
542  **/
543 GtkWidget*
544 gtk_image_new_from_resource (const gchar *resource_path)
545 {
546   GtkImage *image;
547
548   image = g_object_new (GTK_TYPE_IMAGE, NULL);
549
550   gtk_image_set_from_resource (image, resource_path);
551
552   return GTK_WIDGET (image);
553 }
554
555 /**
556  * gtk_image_new_from_pixbuf:
557  * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
558  *
559  * Creates a new #GtkImage displaying @pixbuf.
560  * The #GtkImage does not assume a reference to the
561  * pixbuf; you still need to unref it if you own references.
562  * #GtkImage will add its own reference rather than adopting yours.
563  * 
564  * Note that this function just creates an #GtkImage from the pixbuf. The
565  * #GtkImage created will not react to state changes. Should you want that, 
566  * you should use gtk_image_new_from_icon_set().
567  * 
568  * Return value: a new #GtkImage
569  **/
570 GtkWidget*
571 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
572 {
573   GtkImage *image;
574
575   image = g_object_new (GTK_TYPE_IMAGE, NULL);
576
577   gtk_image_set_from_pixbuf (image, pixbuf);
578
579   return GTK_WIDGET (image);  
580 }
581
582 /**
583  * gtk_image_new_from_stock:
584  * @stock_id: a stock icon name
585  * @size: (type int): a stock icon size
586  * 
587  * Creates a #GtkImage displaying a stock icon. Sample stock icon
588  * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
589  * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
590  * icon name isn't known, the image will be empty.
591  * You can register your own stock icon names, see
592  * gtk_icon_factory_add_default() and gtk_icon_factory_add().
593  * 
594  * Return value: a new #GtkImage displaying the stock icon
595  **/
596 GtkWidget*
597 gtk_image_new_from_stock (const gchar    *stock_id,
598                           GtkIconSize     size)
599 {
600   GtkImage *image;
601
602   image = g_object_new (GTK_TYPE_IMAGE, NULL);
603
604   gtk_image_set_from_stock (image, stock_id, size);
605
606   return GTK_WIDGET (image);
607 }
608
609 /**
610  * gtk_image_new_from_icon_set:
611  * @icon_set: a #GtkIconSet
612  * @size: (type int): a stock icon size
613  *
614  * Creates a #GtkImage displaying an icon set. Sample stock sizes are
615  * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
616  * this function, usually it's better to create a #GtkIconFactory, put
617  * your icon sets in the icon factory, add the icon factory to the
618  * list of default factories with gtk_icon_factory_add_default(), and
619  * then use gtk_image_new_from_stock(). This will allow themes to
620  * override the icon you ship with your application.
621  *
622  * The #GtkImage does not assume a reference to the
623  * icon set; you still need to unref it if you own references.
624  * #GtkImage will add its own reference rather than adopting yours.
625  * 
626  * Return value: a new #GtkImage
627  **/
628 GtkWidget*
629 gtk_image_new_from_icon_set (GtkIconSet     *icon_set,
630                              GtkIconSize     size)
631 {
632   GtkImage *image;
633
634   image = g_object_new (GTK_TYPE_IMAGE, NULL);
635
636   gtk_image_set_from_icon_set (image, icon_set, size);
637
638   return GTK_WIDGET (image);
639 }
640
641 /**
642  * gtk_image_new_from_animation:
643  * @animation: an animation
644  * 
645  * Creates a #GtkImage displaying the given animation.
646  * The #GtkImage does not assume a reference to the
647  * animation; you still need to unref it if you own references.
648  * #GtkImage will add its own reference rather than adopting yours.
649  *
650  * Note that the animation frames are shown using a timeout with
651  * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
652  * keep in mind that the animation will only be shown if the main loop
653  * is not busy with something that has a higher priority.
654  *
655  * Return value: a new #GtkImage widget
656  **/
657 GtkWidget*
658 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
659 {
660   GtkImage *image;
661
662   g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
663   
664   image = g_object_new (GTK_TYPE_IMAGE, NULL);
665
666   gtk_image_set_from_animation (image, animation);
667
668   return GTK_WIDGET (image);
669 }
670
671 /**
672  * gtk_image_new_from_icon_name:
673  * @icon_name: an icon name
674  * @size: (type int): a stock icon size
675  * 
676  * Creates a #GtkImage displaying an icon from the current icon theme.
677  * If the icon name isn't known, a "broken image" icon will be
678  * displayed instead.  If the current icon theme is changed, the icon
679  * will be updated appropriately.
680  * 
681  * Return value: a new #GtkImage displaying the themed icon
682  *
683  * Since: 2.6
684  **/
685 GtkWidget*
686 gtk_image_new_from_icon_name (const gchar    *icon_name,
687                               GtkIconSize     size)
688 {
689   GtkImage *image;
690
691   image = g_object_new (GTK_TYPE_IMAGE, NULL);
692
693   gtk_image_set_from_icon_name (image, icon_name, size);
694
695   return GTK_WIDGET (image);
696 }
697
698 /**
699  * gtk_image_new_from_gicon:
700  * @icon: an icon
701  * @size: (type int): a stock icon size
702  * 
703  * Creates a #GtkImage displaying an icon from the current icon theme.
704  * If the icon name isn't known, a "broken image" icon will be
705  * displayed instead.  If the current icon theme is changed, the icon
706  * will be updated appropriately.
707  * 
708  * Return value: a new #GtkImage displaying the themed icon
709  *
710  * Since: 2.14
711  **/
712 GtkWidget*
713 gtk_image_new_from_gicon (GIcon *icon,
714                           GtkIconSize     size)
715 {
716   GtkImage *image;
717
718   image = g_object_new (GTK_TYPE_IMAGE, NULL);
719
720   gtk_image_set_from_gicon (image, icon, size);
721
722   return GTK_WIDGET (image);
723 }
724
725 /**
726  * gtk_image_set_from_file:
727  * @image: a #GtkImage
728  * @filename: (type filename) (allow-none): a filename or %NULL
729  *
730  * See gtk_image_new_from_file() for details.
731  **/
732 void
733 gtk_image_set_from_file   (GtkImage    *image,
734                            const gchar *filename)
735 {
736   GtkImagePrivate *priv;
737   GdkPixbufAnimation *anim;
738   
739   g_return_if_fail (GTK_IS_IMAGE (image));
740
741   priv = image->priv;
742
743   g_object_freeze_notify (G_OBJECT (image));
744   
745   gtk_image_clear (image);
746
747   if (filename == NULL)
748     {
749       priv->filename = NULL;
750       g_object_thaw_notify (G_OBJECT (image));
751       return;
752     }
753
754   anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
755
756   if (anim == NULL)
757     {
758       gtk_image_set_from_stock (image,
759                                 GTK_STOCK_MISSING_IMAGE,
760                                 DEFAULT_ICON_SIZE);
761       g_object_thaw_notify (G_OBJECT (image));
762       return;
763     }
764
765   /* We could just unconditionally set_from_animation,
766    * but it's nicer for memory if we toss the animation
767    * if it's just a single pixbuf
768    */
769
770   if (gdk_pixbuf_animation_is_static_image (anim))
771     gtk_image_set_from_pixbuf (image,
772                                gdk_pixbuf_animation_get_static_image (anim));
773   else
774     gtk_image_set_from_animation (image, anim);
775
776   g_object_unref (anim);
777
778   priv->filename = g_strdup (filename);
779   
780   g_object_thaw_notify (G_OBJECT (image));
781 }
782
783 /**
784  * gtk_image_set_from_resource:
785  * @image: a #GtkImage
786  * @resource_path: (allow-none): a resource path or %NULL
787  *
788  * See gtk_image_new_from_resource() for details.
789  **/
790 void
791 gtk_image_set_from_resource (GtkImage    *image,
792                              const gchar *resource_path)
793 {
794   GdkPixbuf *pixbuf = NULL;
795   GInputStream *stream;
796
797   g_return_if_fail (GTK_IS_IMAGE (image));
798
799   g_object_freeze_notify (G_OBJECT (image));
800
801   gtk_image_clear (image);
802
803   if (resource_path == NULL)
804     {
805       g_object_thaw_notify (G_OBJECT (image));
806       return;
807     }
808
809   stream = g_resources_open_stream (resource_path, 0, NULL);
810   if (stream != NULL)
811     {
812       pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, NULL);
813       g_object_unref (stream);
814     }
815
816   if (pixbuf == NULL)
817     {
818       gtk_image_set_from_stock (image,
819                                 GTK_STOCK_MISSING_IMAGE,
820                                 DEFAULT_ICON_SIZE);
821       g_object_thaw_notify (G_OBJECT (image));
822       return;
823     }
824
825   gtk_image_set_from_pixbuf (image, pixbuf);
826
827   g_object_unref (pixbuf);
828
829   g_object_thaw_notify (G_OBJECT (image));
830 }
831
832
833 /**
834  * gtk_image_set_from_pixbuf:
835  * @image: a #GtkImage
836  * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
837  *
838  * See gtk_image_new_from_pixbuf() for details.
839  **/
840 void
841 gtk_image_set_from_pixbuf (GtkImage  *image,
842                            GdkPixbuf *pixbuf)
843 {
844   GtkImagePrivate *priv;
845
846   g_return_if_fail (GTK_IS_IMAGE (image));
847   g_return_if_fail (pixbuf == NULL ||
848                     GDK_IS_PIXBUF (pixbuf));
849
850   priv = image->priv;
851
852   g_object_freeze_notify (G_OBJECT (image));
853   
854   gtk_image_clear (image);
855
856   if (pixbuf != NULL)
857     _gtk_icon_helper_set_pixbuf (priv->icon_helper, pixbuf);
858
859   g_object_notify (G_OBJECT (image), "pixbuf");
860   
861   g_object_thaw_notify (G_OBJECT (image));
862 }
863
864 /**
865  * gtk_image_set_from_stock:
866  * @image: a #GtkImage
867  * @stock_id: a stock icon name
868  * @size: (type int): a stock icon size
869  *
870  * See gtk_image_new_from_stock() for details.
871  **/
872 void
873 gtk_image_set_from_stock  (GtkImage       *image,
874                            const gchar    *stock_id,
875                            GtkIconSize     size)
876 {
877   GtkImagePrivate *priv;
878   gchar *new_id;
879
880   g_return_if_fail (GTK_IS_IMAGE (image));
881
882   priv = image->priv;
883
884   g_object_freeze_notify (G_OBJECT (image));
885
886   new_id = g_strdup (stock_id);
887   gtk_image_clear (image);
888
889   if (new_id)
890     {
891       _gtk_icon_helper_set_stock_id (priv->icon_helper, new_id, size);
892       g_free (new_id);
893     }
894
895   g_object_notify (G_OBJECT (image), "stock");
896   g_object_notify (G_OBJECT (image), "icon-size");
897   
898   g_object_thaw_notify (G_OBJECT (image));
899 }
900
901 /**
902  * gtk_image_set_from_icon_set:
903  * @image: a #GtkImage
904  * @icon_set: a #GtkIconSet
905  * @size: (type int): a stock icon size
906  *
907  * See gtk_image_new_from_icon_set() for details.
908  **/
909 void
910 gtk_image_set_from_icon_set  (GtkImage       *image,
911                               GtkIconSet     *icon_set,
912                               GtkIconSize     size)
913 {
914   GtkImagePrivate *priv;
915
916   g_return_if_fail (GTK_IS_IMAGE (image));
917
918   priv = image->priv;
919
920   g_object_freeze_notify (G_OBJECT (image));
921
922   if (icon_set)
923     gtk_icon_set_ref (icon_set);
924   
925   gtk_image_clear (image);
926
927   if (icon_set)
928     {      
929       _gtk_icon_helper_set_icon_set (priv->icon_helper, icon_set, size);
930       gtk_icon_set_unref (icon_set);
931     }
932   
933   g_object_notify (G_OBJECT (image), "icon-set");
934   g_object_notify (G_OBJECT (image), "icon-size");
935   
936   g_object_thaw_notify (G_OBJECT (image));
937 }
938
939 /**
940  * gtk_image_set_from_animation:
941  * @image: a #GtkImage
942  * @animation: the #GdkPixbufAnimation
943  * 
944  * Causes the #GtkImage to display the given animation (or display
945  * nothing, if you set the animation to %NULL).
946  **/
947 void
948 gtk_image_set_from_animation (GtkImage           *image,
949                               GdkPixbufAnimation *animation)
950 {
951   GtkImagePrivate *priv;
952
953   g_return_if_fail (GTK_IS_IMAGE (image));
954   g_return_if_fail (animation == NULL ||
955                     GDK_IS_PIXBUF_ANIMATION (animation));
956
957   priv = image->priv;
958
959   g_object_freeze_notify (G_OBJECT (image));
960   
961   if (animation)
962     g_object_ref (animation);
963
964   gtk_image_clear (image);
965
966   if (animation != NULL)
967     {
968       _gtk_icon_helper_set_animation (priv->icon_helper, animation);
969       g_object_unref (animation);
970     }
971
972   g_object_notify (G_OBJECT (image), "pixbuf-animation");
973   
974   g_object_thaw_notify (G_OBJECT (image));
975 }
976
977 /**
978  * gtk_image_set_from_icon_name:
979  * @image: a #GtkImage
980  * @icon_name: an icon name
981  * @size: (type int): an icon size
982  *
983  * See gtk_image_new_from_icon_name() for details.
984  * 
985  * Since: 2.6
986  **/
987 void
988 gtk_image_set_from_icon_name  (GtkImage       *image,
989                                const gchar    *icon_name,
990                                GtkIconSize     size)
991 {
992   GtkImagePrivate *priv;
993   gchar *new_name;
994
995   g_return_if_fail (GTK_IS_IMAGE (image));
996
997   priv = image->priv;
998
999   g_object_freeze_notify (G_OBJECT (image));
1000
1001   new_name = g_strdup (icon_name);
1002   gtk_image_clear (image);
1003
1004   if (new_name)
1005     {
1006       _gtk_icon_helper_set_icon_name (priv->icon_helper, new_name, size);
1007       g_free (new_name);
1008     }
1009
1010   g_object_notify (G_OBJECT (image), "icon-name");
1011   g_object_notify (G_OBJECT (image), "icon-size");
1012   
1013   g_object_thaw_notify (G_OBJECT (image));
1014 }
1015
1016 /**
1017  * gtk_image_set_from_gicon:
1018  * @image: a #GtkImage
1019  * @icon: an icon
1020  * @size: (type int): an icon size
1021  *
1022  * See gtk_image_new_from_gicon() for details.
1023  * 
1024  * Since: 2.14
1025  **/
1026 void
1027 gtk_image_set_from_gicon  (GtkImage       *image,
1028                            GIcon          *icon,
1029                            GtkIconSize     size)
1030 {
1031   GtkImagePrivate *priv;
1032
1033   g_return_if_fail (GTK_IS_IMAGE (image));
1034
1035   priv = image->priv;
1036
1037   g_object_freeze_notify (G_OBJECT (image));
1038
1039   if (icon)
1040     g_object_ref (icon);
1041
1042   gtk_image_clear (image);
1043
1044   if (icon)
1045     {
1046       _gtk_icon_helper_set_gicon (priv->icon_helper, icon, size);
1047       g_object_unref (icon);
1048     }
1049
1050   g_object_notify (G_OBJECT (image), "gicon");
1051   g_object_notify (G_OBJECT (image), "icon-size");
1052   
1053   g_object_thaw_notify (G_OBJECT (image));
1054 }
1055
1056 /**
1057  * gtk_image_get_storage_type:
1058  * @image: a #GtkImage
1059  * 
1060  * Gets the type of representation being used by the #GtkImage
1061  * to store image data. If the #GtkImage has no image data,
1062  * the return value will be %GTK_IMAGE_EMPTY.
1063  * 
1064  * Return value: image representation being used
1065  **/
1066 GtkImageType
1067 gtk_image_get_storage_type (GtkImage *image)
1068 {
1069   g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1070
1071   return _gtk_icon_helper_get_storage_type (image->priv->icon_helper);
1072 }
1073
1074 /**
1075  * gtk_image_get_pixbuf:
1076  * @image: a #GtkImage
1077  *
1078  * Gets the #GdkPixbuf being displayed by the #GtkImage.
1079  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1080  * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1081  * The caller of this function does not own a reference to the
1082  * returned pixbuf.
1083  * 
1084  * Return value: (transfer none): the displayed pixbuf, or %NULL if
1085  * the image is empty
1086  **/
1087 GdkPixbuf*
1088 gtk_image_get_pixbuf (GtkImage *image)
1089 {
1090   g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1091
1092   return _gtk_icon_helper_peek_pixbuf (image->priv->icon_helper);
1093 }
1094
1095 /**
1096  * gtk_image_get_stock:
1097  * @image: a #GtkImage
1098  * @stock_id: (out) (transfer none) (allow-none): place to store a
1099  *     stock icon name, or %NULL
1100  * @size: (out) (allow-none) (type int): place to store a stock icon
1101  *     size, or %NULL
1102  *
1103  * Gets the stock icon name and size being displayed by the #GtkImage.
1104  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1105  * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1106  * The returned string is owned by the #GtkImage and should not
1107  * be freed.
1108  **/
1109 void
1110 gtk_image_get_stock  (GtkImage        *image,
1111                       gchar          **stock_id,
1112                       GtkIconSize     *size)
1113 {
1114   GtkImagePrivate *priv;
1115
1116   g_return_if_fail (GTK_IS_IMAGE (image));
1117
1118   priv = image->priv;
1119
1120   if (stock_id)
1121     *stock_id = (gchar *) _gtk_icon_helper_get_stock_id (priv->icon_helper);
1122
1123   if (size)
1124     *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1125 }
1126
1127 /**
1128  * gtk_image_get_icon_set:
1129  * @image: a #GtkImage
1130  * @icon_set: (out) (transfer none) (allow-none): location to store a
1131  *     #GtkIconSet, or %NULL
1132  * @size: (out) (allow-none) (type int): location to store a stock
1133  *     icon size, or %NULL
1134  *
1135  * Gets the icon set and size being displayed by the #GtkImage.
1136  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1137  * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1138  **/
1139 void
1140 gtk_image_get_icon_set  (GtkImage        *image,
1141                          GtkIconSet     **icon_set,
1142                          GtkIconSize     *size)
1143 {
1144   GtkImagePrivate *priv;
1145
1146   g_return_if_fail (GTK_IS_IMAGE (image));
1147
1148   priv = image->priv;
1149
1150   if (icon_set)
1151     *icon_set = _gtk_icon_helper_peek_icon_set (priv->icon_helper);
1152
1153   if (size)
1154     *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1155 }
1156
1157 /**
1158  * gtk_image_get_animation:
1159  * @image: a #GtkImage
1160  *
1161  * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1162  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1163  * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1164  * The caller of this function does not own a reference to the
1165  * returned animation.
1166  * 
1167  * Return value: (transfer none): the displayed animation, or %NULL if
1168  * the image is empty
1169  **/
1170 GdkPixbufAnimation*
1171 gtk_image_get_animation (GtkImage *image)
1172 {
1173   GtkImagePrivate *priv;
1174
1175   g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1176
1177   priv = image->priv;
1178
1179   return _gtk_icon_helper_peek_animation (priv->icon_helper);
1180 }
1181
1182 /**
1183  * gtk_image_get_icon_name:
1184  * @image: a #GtkImage
1185  * @icon_name: (out) (transfer none) (allow-none): place to store an
1186  *     icon name, or %NULL
1187  * @size: (out) (allow-none) (type int): place to store an icon size,
1188  *     or %NULL
1189  *
1190  * Gets the icon name and size being displayed by the #GtkImage.
1191  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1192  * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1193  * The returned string is owned by the #GtkImage and should not
1194  * be freed.
1195  * 
1196  * Since: 2.6
1197  **/
1198 void
1199 gtk_image_get_icon_name  (GtkImage     *image,
1200                           const gchar **icon_name,
1201                           GtkIconSize  *size)
1202 {
1203   GtkImagePrivate *priv;
1204
1205   g_return_if_fail (GTK_IS_IMAGE (image));
1206
1207   priv = image->priv;
1208
1209   if (icon_name)
1210     *icon_name = _gtk_icon_helper_get_icon_name (priv->icon_helper);
1211
1212   if (size)
1213     *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1214 }
1215
1216 /**
1217  * gtk_image_get_gicon:
1218  * @image: a #GtkImage
1219  * @gicon: (out) (transfer none) (allow-none): place to store a
1220  *     #GIcon, or %NULL
1221  * @size: (out) (allow-none) (type int): place to store an icon size,
1222  *     or %NULL
1223  *
1224  * Gets the #GIcon and size being displayed by the #GtkImage.
1225  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1226  * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1227  * The caller of this function does not own a reference to the
1228  * returned #GIcon.
1229  * 
1230  * Since: 2.14
1231  **/
1232 void
1233 gtk_image_get_gicon (GtkImage     *image,
1234                      GIcon       **gicon,
1235                      GtkIconSize  *size)
1236 {
1237   GtkImagePrivate *priv;
1238
1239   g_return_if_fail (GTK_IS_IMAGE (image));
1240
1241   priv = image->priv;
1242
1243   if (gicon)
1244     *gicon = _gtk_icon_helper_peek_gicon (priv->icon_helper);
1245
1246   if (size)
1247     *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1248 }
1249
1250 /**
1251  * gtk_image_new:
1252  * 
1253  * Creates a new empty #GtkImage widget.
1254  * 
1255  * Return value: a newly created #GtkImage widget. 
1256  **/
1257 GtkWidget*
1258 gtk_image_new (void)
1259 {
1260   return g_object_new (GTK_TYPE_IMAGE, NULL);
1261 }
1262
1263 static void
1264 gtk_image_reset_anim_iter (GtkImage *image)
1265 {
1266   GtkImagePrivate *priv = image->priv;
1267
1268   if (gtk_image_get_storage_type (image) == GTK_IMAGE_ANIMATION)
1269     {
1270       /* Reset the animation */
1271       if (priv->animation_timeout)
1272         {
1273           g_source_remove (priv->animation_timeout);
1274           priv->animation_timeout = 0;
1275         }
1276
1277       g_clear_object (&priv->animation_iter);
1278     }
1279 }
1280
1281 static void
1282 gtk_image_unmap (GtkWidget *widget)
1283 {
1284   gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1285
1286   GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1287 }
1288
1289 static void
1290 gtk_image_unrealize (GtkWidget *widget)
1291 {
1292   gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1293
1294   GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1295 }
1296
1297 static gint
1298 animation_timeout (gpointer data)
1299 {
1300   GtkImage *image = GTK_IMAGE (data);
1301   GtkImagePrivate *priv = image->priv;
1302   int delay;
1303
1304   priv->animation_timeout = 0;
1305
1306   gdk_pixbuf_animation_iter_advance (priv->animation_iter, NULL);
1307
1308   delay = gdk_pixbuf_animation_iter_get_delay_time (priv->animation_iter);
1309   if (delay >= 0)
1310     {
1311       GtkWidget *widget = GTK_WIDGET (image);
1312
1313       priv->animation_timeout =
1314         gdk_threads_add_timeout (delay, animation_timeout, image);
1315
1316       gtk_widget_queue_draw (widget);
1317     }
1318
1319   return FALSE;
1320 }
1321
1322 static GdkPixbuf *
1323 get_animation_frame (GtkImage *image)
1324 {
1325   GtkImagePrivate *priv = image->priv;
1326
1327   if (priv->animation_iter == NULL)
1328     {
1329       int delay;
1330
1331       priv->animation_iter = 
1332         gdk_pixbuf_animation_get_iter (_gtk_icon_helper_peek_animation (priv->icon_helper), NULL);
1333
1334       delay = gdk_pixbuf_animation_iter_get_delay_time (priv->animation_iter);
1335       if (delay >= 0)
1336         priv->animation_timeout =
1337           gdk_threads_add_timeout (delay, animation_timeout, image);
1338     }
1339
1340   /* don't advance the anim iter here, or we could get frame changes between two
1341    * exposes of different areas.
1342    */
1343   return g_object_ref (gdk_pixbuf_animation_iter_get_pixbuf (priv->animation_iter));
1344 }
1345
1346 static void
1347 gtk_image_get_preferred_size (GtkImage *image,
1348                               gint     *width_out,
1349                               gint     *height_out)
1350 {
1351   GtkImagePrivate *priv = image->priv;
1352   gint width, height;
1353   GtkBorder border;
1354   GtkStyleContext *context;
1355
1356   context = gtk_widget_get_style_context (GTK_WIDGET (image));
1357   _gtk_icon_helper_get_size (priv->icon_helper, context, &width, &height);
1358   _gtk_misc_get_padding_and_border (GTK_MISC (image), &border);
1359
1360   width += border.left + border.right;
1361   height += border.top + border.bottom;
1362
1363   if (width_out)
1364     *width_out = width;
1365   if (height_out)
1366     *height_out = height;
1367 }
1368
1369 static gint
1370 gtk_image_draw (GtkWidget *widget,
1371                 cairo_t   *cr)
1372 {
1373   GtkImage *image;
1374   GtkImagePrivate *priv;
1375   GtkMisc *misc;
1376   GtkStyleContext *context;
1377   gint x, y, width, height;
1378   gfloat xalign, yalign;
1379   GtkBorder border;
1380
1381   g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1382
1383   image = GTK_IMAGE (widget);
1384   misc = GTK_MISC (image);
1385   priv = image->priv;
1386
1387   context = gtk_widget_get_style_context (widget);
1388
1389   gtk_render_background (context, cr,
1390                          0, 0,
1391                          gtk_widget_get_allocated_width (widget), gtk_widget_get_allocated_height (widget));
1392   gtk_render_frame (context, cr,
1393                     0, 0,
1394                     gtk_widget_get_allocated_width (widget), gtk_widget_get_allocated_height (widget));
1395
1396   gtk_misc_get_alignment (misc, &xalign, &yalign);
1397   gtk_image_get_preferred_size (image, &width, &height);
1398   _gtk_misc_get_padding_and_border (misc, &border);
1399
1400   if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
1401     xalign = 1.0 - xalign;
1402
1403   x = floor ((gtk_widget_get_allocated_width (widget) - width) * xalign) + border.left;
1404   y = floor ((gtk_widget_get_allocated_height (widget) - height) * yalign) + border.top;
1405
1406   if (gtk_image_get_storage_type (image) == GTK_IMAGE_ANIMATION)
1407     {
1408       GdkPixbuf *pixbuf = get_animation_frame (image);
1409       gtk_render_icon (context, cr,
1410                        pixbuf,
1411                        x, y);
1412       g_object_unref (pixbuf);
1413     }
1414   else
1415     {
1416       _gtk_icon_helper_draw (priv->icon_helper, 
1417                              context, cr,
1418                              x, y);
1419     }
1420
1421   return FALSE;
1422 }
1423
1424 static void
1425 gtk_image_reset (GtkImage *image)
1426 {
1427   GtkImagePrivate *priv = image->priv;
1428   GtkImageType storage_type;
1429
1430   g_object_freeze_notify (G_OBJECT (image));
1431   storage_type = gtk_image_get_storage_type (image);
1432   
1433   if (storage_type != GTK_IMAGE_EMPTY)
1434     g_object_notify (G_OBJECT (image), "storage-type");
1435
1436   g_object_notify (G_OBJECT (image), "icon-size");
1437   
1438   switch (storage_type)
1439     {
1440     case GTK_IMAGE_PIXBUF:
1441       g_object_notify (G_OBJECT (image), "pixbuf");
1442       break;
1443     case GTK_IMAGE_STOCK:
1444       g_object_notify (G_OBJECT (image), "stock");      
1445       break;
1446     case GTK_IMAGE_ICON_SET:
1447       g_object_notify (G_OBJECT (image), "icon-set");      
1448       break;
1449     case GTK_IMAGE_ANIMATION:
1450       gtk_image_reset_anim_iter (image);      
1451       g_object_notify (G_OBJECT (image), "pixbuf-animation");
1452       break;
1453     case GTK_IMAGE_ICON_NAME:
1454       g_object_notify (G_OBJECT (image), "icon-name");
1455       break;
1456     case GTK_IMAGE_GICON:
1457       g_object_notify (G_OBJECT (image), "gicon");
1458       break;
1459     case GTK_IMAGE_EMPTY:
1460     default:
1461       break;
1462     }
1463
1464   if (priv->filename)
1465     {
1466       g_free (priv->filename);
1467       priv->filename = NULL;
1468       g_object_notify (G_OBJECT (image), "file");
1469     }
1470
1471   _gtk_icon_helper_clear (priv->icon_helper);
1472
1473   g_object_thaw_notify (G_OBJECT (image));
1474 }
1475
1476 /**
1477  * gtk_image_clear:
1478  * @image: a #GtkImage
1479  *
1480  * Resets the image to be empty.
1481  *
1482  * Since: 2.8
1483  */
1484 void
1485 gtk_image_clear (GtkImage *image)
1486 {
1487   gtk_image_reset (image);
1488
1489   if (gtk_widget_get_visible (GTK_WIDGET (image)))
1490       gtk_widget_queue_resize (GTK_WIDGET (image));
1491 }
1492
1493 static void
1494 gtk_image_get_preferred_width (GtkWidget *widget,
1495                                gint      *minimum,
1496                                gint      *natural)
1497 {
1498   gint width;
1499
1500   gtk_image_get_preferred_size (GTK_IMAGE (widget), &width, NULL);
1501   *minimum = *natural = width;
1502
1503
1504 static void
1505 gtk_image_get_preferred_height (GtkWidget *widget,
1506                                 gint      *minimum,
1507                                 gint      *natural)
1508 {
1509   gint height;
1510
1511   gtk_image_get_preferred_size (GTK_IMAGE (widget), NULL, &height);
1512   *minimum = *natural = height;
1513 }
1514
1515 static void
1516 icon_theme_changed (GtkImage *image)
1517 {
1518   GtkImagePrivate *priv = image->priv;
1519
1520   _gtk_icon_helper_invalidate (priv->icon_helper);
1521   gtk_widget_queue_draw (GTK_WIDGET (image));
1522 }
1523
1524 static void
1525 gtk_image_style_updated (GtkWidget *widget)
1526 {
1527   GTK_WIDGET_CLASS (gtk_image_parent_class)->style_updated (widget);
1528
1529   icon_theme_changed (GTK_IMAGE (widget));
1530 }
1531
1532 static void
1533 gtk_image_screen_changed (GtkWidget *widget,
1534                           GdkScreen *prev_screen)
1535 {
1536   GtkImage *image;
1537
1538   image = GTK_IMAGE (widget);
1539
1540   if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
1541     GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
1542
1543   icon_theme_changed (image);
1544 }
1545
1546 void
1547 _gtk_image_gicon_data_clear (GtkImageGIconData *data)
1548 {
1549   if (data->pixbuf)
1550     {
1551       g_object_unref (data->pixbuf);
1552       data->pixbuf = NULL;
1553     }
1554   if (data->icon)
1555     {
1556       g_object_unref (data->icon);
1557       data->icon = NULL;
1558     }
1559 }
1560
1561 /**
1562  * gtk_image_set_pixel_size:
1563  * @image: a #GtkImage
1564  * @pixel_size: the new pixel size
1565  * 
1566  * Sets the pixel size to use for named icons. If the pixel size is set
1567  * to a value != -1, it is used instead of the icon size set by
1568  * gtk_image_set_from_icon_name().
1569  *
1570  * Since: 2.6
1571  */
1572 void 
1573 gtk_image_set_pixel_size (GtkImage *image,
1574                           gint      pixel_size)
1575 {
1576   GtkImagePrivate *priv;
1577   gint old_pixel_size;
1578
1579   g_return_if_fail (GTK_IS_IMAGE (image));
1580
1581   priv = image->priv;
1582   old_pixel_size = gtk_image_get_pixel_size (image);
1583
1584   if (pixel_size != old_pixel_size)
1585     {
1586       _gtk_icon_helper_set_pixel_size (priv->icon_helper, pixel_size);
1587
1588       if (gtk_widget_get_visible (GTK_WIDGET (image)))
1589         gtk_widget_queue_resize (GTK_WIDGET (image));
1590
1591       g_object_notify (G_OBJECT (image), "pixel-size");
1592     }
1593 }
1594
1595 /**
1596  * gtk_image_get_pixel_size:
1597  * @image: a #GtkImage
1598  * 
1599  * Gets the pixel size used for named icons.
1600  *
1601  * Returns: the pixel size used for named icons.
1602  *
1603  * Since: 2.6
1604  */
1605 gint
1606 gtk_image_get_pixel_size (GtkImage *image)
1607 {
1608   g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
1609
1610   return _gtk_icon_helper_get_pixel_size (image->priv->icon_helper);
1611 }