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