]> Pileus Git - ~andy/gtk/blob - gtk/gtkimage.c
73d5fdf86870b8e384125dc24360f4e810ff405b
[~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_destroy              (GtkWidget    *widget);
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
203   widget_class = GTK_WIDGET_CLASS (class);
204   widget_class->draw = gtk_image_draw;
205   widget_class->destroy = gtk_image_destroy;
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_destroy (GtkWidget *widget)
363 {
364   GtkImage *image = GTK_IMAGE (widget);
365
366   g_clear_object (&image->priv->icon_helper);
367
368   GTK_WIDGET_CLASS (gtk_image_parent_class)->destroy (widget);
369 }
370
371 static void 
372 gtk_image_set_property (GObject      *object,
373                         guint         prop_id,
374                         const GValue *value,
375                         GParamSpec   *pspec)
376 {
377   GtkImage *image = GTK_IMAGE (object);
378   GtkImagePrivate *priv = image->priv;
379   GtkIconSize icon_size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
380
381   if (icon_size == GTK_ICON_SIZE_INVALID)
382     icon_size = DEFAULT_ICON_SIZE;
383
384   switch (prop_id)
385     {
386     case PROP_PIXBUF:
387       gtk_image_set_from_pixbuf (image,
388                                  g_value_get_object (value));
389       break;
390     case PROP_FILE:
391       gtk_image_set_from_file (image, g_value_get_string (value));
392       break;
393     case PROP_STOCK:
394       gtk_image_set_from_stock (image, g_value_get_string (value),
395                                 icon_size);
396       break;
397     case PROP_ICON_SET:
398       gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
399                                    icon_size);
400       break;
401     case PROP_ICON_SIZE:
402       _gtk_icon_helper_set_icon_size (priv->icon_helper, g_value_get_int (value));
403       break;
404     case PROP_PIXEL_SIZE:
405       gtk_image_set_pixel_size (image, g_value_get_int (value));
406       break;
407     case PROP_PIXBUF_ANIMATION:
408       gtk_image_set_from_animation (image,
409                                     g_value_get_object (value));
410       break;
411     case PROP_ICON_NAME:
412       gtk_image_set_from_icon_name (image, g_value_get_string (value),
413                                     icon_size);
414       break;
415     case PROP_GICON:
416       gtk_image_set_from_gicon (image, g_value_get_object (value),
417                                 icon_size);
418       break;
419
420     case PROP_USE_FALLBACK:
421       _gtk_icon_helper_set_use_fallback (priv->icon_helper, g_value_get_boolean (value));
422       break;
423
424     default:
425       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
426       break;
427     }
428 }
429
430 static void 
431 gtk_image_get_property (GObject     *object,
432                         guint        prop_id,
433                         GValue      *value,
434                         GParamSpec  *pspec)
435 {
436   GtkImage *image = GTK_IMAGE (object);
437   GtkImagePrivate *priv = image->priv;
438
439   switch (prop_id)
440     {
441     case PROP_PIXBUF:
442       g_value_set_object (value, _gtk_icon_helper_peek_pixbuf (priv->icon_helper));
443       break;
444     case PROP_FILE:
445       g_value_set_string (value, priv->filename);
446       break;
447     case PROP_STOCK:
448       g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
449       break;
450     case PROP_ICON_SET:
451       g_value_set_boxed (value, _gtk_icon_helper_peek_icon_set (priv->icon_helper));
452       break;      
453     case PROP_ICON_SIZE:
454       g_value_set_int (value, _gtk_icon_helper_get_icon_size (priv->icon_helper));
455       break;
456     case PROP_PIXEL_SIZE:
457       g_value_set_int (value, _gtk_icon_helper_get_pixel_size (priv->icon_helper));
458       break;
459     case PROP_PIXBUF_ANIMATION:
460       g_value_set_object (value, _gtk_icon_helper_peek_animation (priv->icon_helper));
461       break;
462     case PROP_ICON_NAME:
463       g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
464       break;
465     case PROP_GICON:
466       g_value_set_object (value, _gtk_icon_helper_peek_gicon (priv->icon_helper));
467       break;
468     case PROP_USE_FALLBACK:
469       g_value_set_boolean (value, _gtk_icon_helper_get_use_fallback (priv->icon_helper));
470       break;
471     default:
472       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
473       break;
474     }
475 }
476
477
478 /**
479  * gtk_image_new_from_file:
480  * @filename: (type filename): a filename
481  * 
482  * Creates a new #GtkImage displaying the file @filename. If the file
483  * isn't found or can't be loaded, the resulting #GtkImage will
484  * display a "broken image" icon. This function never returns %NULL,
485  * it always returns a valid #GtkImage widget.
486  *
487  * If the file contains an animation, the image will contain an
488  * animation.
489  *
490  * If you need to detect failures to load the file, use
491  * gdk_pixbuf_new_from_file() to load the file yourself, then create
492  * the #GtkImage from the pixbuf. (Or for animations, use
493  * gdk_pixbuf_animation_new_from_file()).
494  *
495  * The storage type (gtk_image_get_storage_type()) of the returned
496  * image is not defined, it will be whatever is appropriate for
497  * displaying the file.
498  * 
499  * Return value: a new #GtkImage
500  **/
501 GtkWidget*
502 gtk_image_new_from_file   (const gchar *filename)
503 {
504   GtkImage *image;
505
506   image = g_object_new (GTK_TYPE_IMAGE, NULL);
507
508   gtk_image_set_from_file (image, filename);
509
510   return GTK_WIDGET (image);
511 }
512
513 /**
514  * gtk_image_new_from_resource:
515  * @resource_path: a resource path
516  *
517  * Creates a new #GtkImage displaying the resource file @resource_path. If the file
518  * isn't found or can't be loaded, the resulting #GtkImage will
519  * display a "broken image" icon. This function never returns %NULL,
520  * it always returns a valid #GtkImage widget.
521  *
522  * If the file contains an animation, the image will contain an
523  * animation.
524  *
525  * If you need to detect failures to load the file, use
526  * gdk_pixbuf_new_from_file() to load the file yourself, then create
527  * the #GtkImage from the pixbuf. (Or for animations, use
528  * gdk_pixbuf_animation_new_from_file()).
529  *
530  * The storage type (gtk_image_get_storage_type()) of the returned
531  * image is not defined, it will be whatever is appropriate for
532  * displaying the file.
533  *
534  * Return value: a new #GtkImage
535  *
536  * Since: 3.4
537  **/
538 GtkWidget*
539 gtk_image_new_from_resource (const gchar *resource_path)
540 {
541   GtkImage *image;
542
543   image = g_object_new (GTK_TYPE_IMAGE, NULL);
544
545   gtk_image_set_from_resource (image, resource_path);
546
547   return GTK_WIDGET (image);
548 }
549
550 /**
551  * gtk_image_new_from_pixbuf:
552  * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
553  *
554  * Creates a new #GtkImage displaying @pixbuf.
555  * The #GtkImage does not assume a reference to the
556  * pixbuf; you still need to unref it if you own references.
557  * #GtkImage will add its own reference rather than adopting yours.
558  * 
559  * Note that this function just creates an #GtkImage from the pixbuf. The
560  * #GtkImage created will not react to state changes. Should you want that, 
561  * you should use gtk_image_new_from_icon_set().
562  * 
563  * Return value: a new #GtkImage
564  **/
565 GtkWidget*
566 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
567 {
568   GtkImage *image;
569
570   image = g_object_new (GTK_TYPE_IMAGE, NULL);
571
572   gtk_image_set_from_pixbuf (image, pixbuf);
573
574   return GTK_WIDGET (image);  
575 }
576
577 /**
578  * gtk_image_new_from_stock:
579  * @stock_id: a stock icon name
580  * @size: (type int): a stock icon size
581  * 
582  * Creates a #GtkImage displaying a stock icon. Sample stock icon
583  * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
584  * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
585  * icon name isn't known, the image will be empty.
586  * You can register your own stock icon names, see
587  * gtk_icon_factory_add_default() and gtk_icon_factory_add().
588  * 
589  * Return value: a new #GtkImage displaying the stock icon
590  **/
591 GtkWidget*
592 gtk_image_new_from_stock (const gchar    *stock_id,
593                           GtkIconSize     size)
594 {
595   GtkImage *image;
596
597   image = g_object_new (GTK_TYPE_IMAGE, NULL);
598
599   gtk_image_set_from_stock (image, stock_id, size);
600
601   return GTK_WIDGET (image);
602 }
603
604 /**
605  * gtk_image_new_from_icon_set:
606  * @icon_set: a #GtkIconSet
607  * @size: (type int): a stock icon size
608  *
609  * Creates a #GtkImage displaying an icon set. Sample stock sizes are
610  * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
611  * this function, usually it's better to create a #GtkIconFactory, put
612  * your icon sets in the icon factory, add the icon factory to the
613  * list of default factories with gtk_icon_factory_add_default(), and
614  * then use gtk_image_new_from_stock(). This will allow themes to
615  * override the icon you ship with your application.
616  *
617  * The #GtkImage does not assume a reference to the
618  * icon set; you still need to unref it if you own references.
619  * #GtkImage will add its own reference rather than adopting yours.
620  * 
621  * Return value: a new #GtkImage
622  **/
623 GtkWidget*
624 gtk_image_new_from_icon_set (GtkIconSet     *icon_set,
625                              GtkIconSize     size)
626 {
627   GtkImage *image;
628
629   image = g_object_new (GTK_TYPE_IMAGE, NULL);
630
631   gtk_image_set_from_icon_set (image, icon_set, size);
632
633   return GTK_WIDGET (image);
634 }
635
636 /**
637  * gtk_image_new_from_animation:
638  * @animation: an animation
639  * 
640  * Creates a #GtkImage displaying the given animation.
641  * The #GtkImage does not assume a reference to the
642  * animation; you still need to unref it if you own references.
643  * #GtkImage will add its own reference rather than adopting yours.
644  *
645  * Note that the animation frames are shown using a timeout with
646  * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
647  * keep in mind that the animation will only be shown if the main loop
648  * is not busy with something that has a higher priority.
649  *
650  * Return value: a new #GtkImage widget
651  **/
652 GtkWidget*
653 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
654 {
655   GtkImage *image;
656
657   g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
658   
659   image = g_object_new (GTK_TYPE_IMAGE, NULL);
660
661   gtk_image_set_from_animation (image, animation);
662
663   return GTK_WIDGET (image);
664 }
665
666 /**
667  * gtk_image_new_from_icon_name:
668  * @icon_name: an icon name
669  * @size: (type int): a stock icon size
670  * 
671  * Creates a #GtkImage displaying an icon from the current icon theme.
672  * If the icon name isn't known, a "broken image" icon will be
673  * displayed instead.  If the current icon theme is changed, the icon
674  * will be updated appropriately.
675  * 
676  * Return value: a new #GtkImage displaying the themed icon
677  *
678  * Since: 2.6
679  **/
680 GtkWidget*
681 gtk_image_new_from_icon_name (const gchar    *icon_name,
682                               GtkIconSize     size)
683 {
684   GtkImage *image;
685
686   image = g_object_new (GTK_TYPE_IMAGE, NULL);
687
688   gtk_image_set_from_icon_name (image, icon_name, size);
689
690   return GTK_WIDGET (image);
691 }
692
693 /**
694  * gtk_image_new_from_gicon:
695  * @icon: an icon
696  * @size: (type int): a stock icon size
697  * 
698  * Creates a #GtkImage displaying an icon from the current icon theme.
699  * If the icon name isn't known, a "broken image" icon will be
700  * displayed instead.  If the current icon theme is changed, the icon
701  * will be updated appropriately.
702  * 
703  * Return value: a new #GtkImage displaying the themed icon
704  *
705  * Since: 2.14
706  **/
707 GtkWidget*
708 gtk_image_new_from_gicon (GIcon *icon,
709                           GtkIconSize     size)
710 {
711   GtkImage *image;
712
713   image = g_object_new (GTK_TYPE_IMAGE, NULL);
714
715   gtk_image_set_from_gicon (image, icon, size);
716
717   return GTK_WIDGET (image);
718 }
719
720 /**
721  * gtk_image_set_from_file:
722  * @image: a #GtkImage
723  * @filename: (type filename) (allow-none): a filename or %NULL
724  *
725  * See gtk_image_new_from_file() for details.
726  **/
727 void
728 gtk_image_set_from_file   (GtkImage    *image,
729                            const gchar *filename)
730 {
731   GtkImagePrivate *priv;
732   GdkPixbufAnimation *anim;
733   
734   g_return_if_fail (GTK_IS_IMAGE (image));
735
736   priv = image->priv;
737
738   g_object_freeze_notify (G_OBJECT (image));
739   
740   gtk_image_clear (image);
741
742   if (filename == NULL)
743     {
744       priv->filename = NULL;
745       g_object_thaw_notify (G_OBJECT (image));
746       return;
747     }
748
749   anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
750
751   if (anim == NULL)
752     {
753       gtk_image_set_from_stock (image,
754                                 GTK_STOCK_MISSING_IMAGE,
755                                 DEFAULT_ICON_SIZE);
756       g_object_thaw_notify (G_OBJECT (image));
757       return;
758     }
759
760   /* We could just unconditionally set_from_animation,
761    * but it's nicer for memory if we toss the animation
762    * if it's just a single pixbuf
763    */
764
765   if (gdk_pixbuf_animation_is_static_image (anim))
766     gtk_image_set_from_pixbuf (image,
767                                gdk_pixbuf_animation_get_static_image (anim));
768   else
769     gtk_image_set_from_animation (image, anim);
770
771   g_object_unref (anim);
772
773   priv->filename = g_strdup (filename);
774   
775   g_object_thaw_notify (G_OBJECT (image));
776 }
777
778 /**
779  * gtk_image_set_from_resource:
780  * @image: a #GtkImage
781  * @resource_path: (allow-none): a resource path or %NULL
782  *
783  * See gtk_image_new_from_resource() for details.
784  **/
785 void
786 gtk_image_set_from_resource (GtkImage    *image,
787                              const gchar *resource_path)
788 {
789   GdkPixbuf *pixbuf = NULL;
790   GInputStream *stream;
791
792   g_return_if_fail (GTK_IS_IMAGE (image));
793
794   g_object_freeze_notify (G_OBJECT (image));
795
796   gtk_image_clear (image);
797
798   if (resource_path == NULL)
799     {
800       g_object_thaw_notify (G_OBJECT (image));
801       return;
802     }
803
804   stream = g_resources_open_stream (resource_path, 0, NULL);
805   if (stream != NULL)
806     {
807       pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, NULL);
808       g_object_unref (stream);
809     }
810
811   if (pixbuf == NULL)
812     {
813       gtk_image_set_from_stock (image,
814                                 GTK_STOCK_MISSING_IMAGE,
815                                 DEFAULT_ICON_SIZE);
816       g_object_thaw_notify (G_OBJECT (image));
817       return;
818     }
819
820   gtk_image_set_from_pixbuf (image, pixbuf);
821
822   g_object_unref (pixbuf);
823
824   g_object_thaw_notify (G_OBJECT (image));
825 }
826
827
828 /**
829  * gtk_image_set_from_pixbuf:
830  * @image: a #GtkImage
831  * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
832  *
833  * See gtk_image_new_from_pixbuf() for details.
834  **/
835 void
836 gtk_image_set_from_pixbuf (GtkImage  *image,
837                            GdkPixbuf *pixbuf)
838 {
839   GtkImagePrivate *priv;
840
841   g_return_if_fail (GTK_IS_IMAGE (image));
842   g_return_if_fail (pixbuf == NULL ||
843                     GDK_IS_PIXBUF (pixbuf));
844
845   priv = image->priv;
846
847   g_object_freeze_notify (G_OBJECT (image));
848   
849   gtk_image_clear (image);
850
851   if (pixbuf != NULL)
852     _gtk_icon_helper_set_pixbuf (priv->icon_helper, pixbuf);
853
854   g_object_notify (G_OBJECT (image), "pixbuf");
855   
856   g_object_thaw_notify (G_OBJECT (image));
857 }
858
859 /**
860  * gtk_image_set_from_stock:
861  * @image: a #GtkImage
862  * @stock_id: a stock icon name
863  * @size: (type int): a stock icon size
864  *
865  * See gtk_image_new_from_stock() for details.
866  **/
867 void
868 gtk_image_set_from_stock  (GtkImage       *image,
869                            const gchar    *stock_id,
870                            GtkIconSize     size)
871 {
872   GtkImagePrivate *priv;
873   gchar *new_id;
874
875   g_return_if_fail (GTK_IS_IMAGE (image));
876
877   priv = image->priv;
878
879   g_object_freeze_notify (G_OBJECT (image));
880
881   new_id = g_strdup (stock_id);
882   gtk_image_clear (image);
883
884   if (new_id)
885     {
886       _gtk_icon_helper_set_stock_id (priv->icon_helper, new_id, size);
887       g_free (new_id);
888     }
889
890   g_object_notify (G_OBJECT (image), "stock");
891   g_object_notify (G_OBJECT (image), "icon-size");
892   
893   g_object_thaw_notify (G_OBJECT (image));
894 }
895
896 /**
897  * gtk_image_set_from_icon_set:
898  * @image: a #GtkImage
899  * @icon_set: a #GtkIconSet
900  * @size: (type int): a stock icon size
901  *
902  * See gtk_image_new_from_icon_set() for details.
903  **/
904 void
905 gtk_image_set_from_icon_set  (GtkImage       *image,
906                               GtkIconSet     *icon_set,
907                               GtkIconSize     size)
908 {
909   GtkImagePrivate *priv;
910
911   g_return_if_fail (GTK_IS_IMAGE (image));
912
913   priv = image->priv;
914
915   g_object_freeze_notify (G_OBJECT (image));
916
917   if (icon_set)
918     gtk_icon_set_ref (icon_set);
919   
920   gtk_image_clear (image);
921
922   if (icon_set)
923     {      
924       _gtk_icon_helper_set_icon_set (priv->icon_helper, icon_set, size);
925       gtk_icon_set_unref (icon_set);
926     }
927   
928   g_object_notify (G_OBJECT (image), "icon-set");
929   g_object_notify (G_OBJECT (image), "icon-size");
930   
931   g_object_thaw_notify (G_OBJECT (image));
932 }
933
934 /**
935  * gtk_image_set_from_animation:
936  * @image: a #GtkImage
937  * @animation: the #GdkPixbufAnimation
938  * 
939  * Causes the #GtkImage to display the given animation (or display
940  * nothing, if you set the animation to %NULL).
941  **/
942 void
943 gtk_image_set_from_animation (GtkImage           *image,
944                               GdkPixbufAnimation *animation)
945 {
946   GtkImagePrivate *priv;
947
948   g_return_if_fail (GTK_IS_IMAGE (image));
949   g_return_if_fail (animation == NULL ||
950                     GDK_IS_PIXBUF_ANIMATION (animation));
951
952   priv = image->priv;
953
954   g_object_freeze_notify (G_OBJECT (image));
955   
956   if (animation)
957     g_object_ref (animation);
958
959   gtk_image_clear (image);
960
961   if (animation != NULL)
962     {
963       _gtk_icon_helper_set_animation (priv->icon_helper, animation);
964       g_object_unref (animation);
965     }
966
967   g_object_notify (G_OBJECT (image), "pixbuf-animation");
968   
969   g_object_thaw_notify (G_OBJECT (image));
970 }
971
972 /**
973  * gtk_image_set_from_icon_name:
974  * @image: a #GtkImage
975  * @icon_name: an icon name
976  * @size: (type int): an icon size
977  *
978  * See gtk_image_new_from_icon_name() for details.
979  * 
980  * Since: 2.6
981  **/
982 void
983 gtk_image_set_from_icon_name  (GtkImage       *image,
984                                const gchar    *icon_name,
985                                GtkIconSize     size)
986 {
987   GtkImagePrivate *priv;
988   gchar *new_name;
989
990   g_return_if_fail (GTK_IS_IMAGE (image));
991
992   priv = image->priv;
993
994   g_object_freeze_notify (G_OBJECT (image));
995
996   new_name = g_strdup (icon_name);
997   gtk_image_clear (image);
998
999   if (new_name)
1000     {
1001       _gtk_icon_helper_set_icon_name (priv->icon_helper, new_name, size);
1002       g_free (new_name);
1003     }
1004
1005   g_object_notify (G_OBJECT (image), "icon-name");
1006   g_object_notify (G_OBJECT (image), "icon-size");
1007   
1008   g_object_thaw_notify (G_OBJECT (image));
1009 }
1010
1011 /**
1012  * gtk_image_set_from_gicon:
1013  * @image: a #GtkImage
1014  * @icon: an icon
1015  * @size: (type int): an icon size
1016  *
1017  * See gtk_image_new_from_gicon() for details.
1018  * 
1019  * Since: 2.14
1020  **/
1021 void
1022 gtk_image_set_from_gicon  (GtkImage       *image,
1023                            GIcon          *icon,
1024                            GtkIconSize     size)
1025 {
1026   GtkImagePrivate *priv;
1027
1028   g_return_if_fail (GTK_IS_IMAGE (image));
1029
1030   priv = image->priv;
1031
1032   g_object_freeze_notify (G_OBJECT (image));
1033
1034   if (icon)
1035     g_object_ref (icon);
1036
1037   gtk_image_clear (image);
1038
1039   if (icon)
1040     {
1041       _gtk_icon_helper_set_gicon (priv->icon_helper, icon, size);
1042       g_object_unref (icon);
1043     }
1044
1045   g_object_notify (G_OBJECT (image), "gicon");
1046   g_object_notify (G_OBJECT (image), "icon-size");
1047   
1048   g_object_thaw_notify (G_OBJECT (image));
1049 }
1050
1051 /**
1052  * gtk_image_get_storage_type:
1053  * @image: a #GtkImage
1054  * 
1055  * Gets the type of representation being used by the #GtkImage
1056  * to store image data. If the #GtkImage has no image data,
1057  * the return value will be %GTK_IMAGE_EMPTY.
1058  * 
1059  * Return value: image representation being used
1060  **/
1061 GtkImageType
1062 gtk_image_get_storage_type (GtkImage *image)
1063 {
1064   g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1065
1066   return _gtk_icon_helper_get_storage_type (image->priv->icon_helper);
1067 }
1068
1069 /**
1070  * gtk_image_get_pixbuf:
1071  * @image: a #GtkImage
1072  *
1073  * Gets the #GdkPixbuf being displayed by the #GtkImage.
1074  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1075  * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1076  * The caller of this function does not own a reference to the
1077  * returned pixbuf.
1078  * 
1079  * Return value: (transfer none): the displayed pixbuf, or %NULL if
1080  * the image is empty
1081  **/
1082 GdkPixbuf*
1083 gtk_image_get_pixbuf (GtkImage *image)
1084 {
1085   g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1086
1087   return _gtk_icon_helper_peek_pixbuf (image->priv->icon_helper);
1088 }
1089
1090 /**
1091  * gtk_image_get_stock:
1092  * @image: a #GtkImage
1093  * @stock_id: (out) (transfer none) (allow-none): place to store a
1094  *     stock icon name, or %NULL
1095  * @size: (out) (allow-none) (type int): place to store a stock icon
1096  *     size, or %NULL
1097  *
1098  * Gets the stock icon name and size being displayed by the #GtkImage.
1099  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1100  * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1101  * The returned string is owned by the #GtkImage and should not
1102  * be freed.
1103  **/
1104 void
1105 gtk_image_get_stock  (GtkImage        *image,
1106                       gchar          **stock_id,
1107                       GtkIconSize     *size)
1108 {
1109   GtkImagePrivate *priv;
1110
1111   g_return_if_fail (GTK_IS_IMAGE (image));
1112
1113   priv = image->priv;
1114
1115   if (stock_id)
1116     *stock_id = (gchar *) _gtk_icon_helper_get_stock_id (priv->icon_helper);
1117
1118   if (size)
1119     *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1120 }
1121
1122 /**
1123  * gtk_image_get_icon_set:
1124  * @image: a #GtkImage
1125  * @icon_set: (out) (transfer none) (allow-none): location to store a
1126  *     #GtkIconSet, or %NULL
1127  * @size: (out) (allow-none) (type int): location to store a stock
1128  *     icon size, or %NULL
1129  *
1130  * Gets the icon set and size being displayed by the #GtkImage.
1131  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1132  * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1133  **/
1134 void
1135 gtk_image_get_icon_set  (GtkImage        *image,
1136                          GtkIconSet     **icon_set,
1137                          GtkIconSize     *size)
1138 {
1139   GtkImagePrivate *priv;
1140
1141   g_return_if_fail (GTK_IS_IMAGE (image));
1142
1143   priv = image->priv;
1144
1145   if (icon_set)
1146     *icon_set = _gtk_icon_helper_peek_icon_set (priv->icon_helper);
1147
1148   if (size)
1149     *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1150 }
1151
1152 /**
1153  * gtk_image_get_animation:
1154  * @image: a #GtkImage
1155  *
1156  * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1157  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1158  * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1159  * The caller of this function does not own a reference to the
1160  * returned animation.
1161  * 
1162  * Return value: (transfer none): the displayed animation, or %NULL if
1163  * the image is empty
1164  **/
1165 GdkPixbufAnimation*
1166 gtk_image_get_animation (GtkImage *image)
1167 {
1168   GtkImagePrivate *priv;
1169
1170   g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1171
1172   priv = image->priv;
1173
1174   return _gtk_icon_helper_peek_animation (priv->icon_helper);
1175 }
1176
1177 /**
1178  * gtk_image_get_icon_name:
1179  * @image: a #GtkImage
1180  * @icon_name: (out) (transfer none) (allow-none): place to store an
1181  *     icon name, or %NULL
1182  * @size: (out) (allow-none) (type int): place to store an icon size,
1183  *     or %NULL
1184  *
1185  * Gets the icon name and size being displayed by the #GtkImage.
1186  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1187  * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1188  * The returned string is owned by the #GtkImage and should not
1189  * be freed.
1190  * 
1191  * Since: 2.6
1192  **/
1193 void
1194 gtk_image_get_icon_name  (GtkImage     *image,
1195                           const gchar **icon_name,
1196                           GtkIconSize  *size)
1197 {
1198   GtkImagePrivate *priv;
1199
1200   g_return_if_fail (GTK_IS_IMAGE (image));
1201
1202   priv = image->priv;
1203
1204   if (icon_name)
1205     *icon_name = _gtk_icon_helper_get_icon_name (priv->icon_helper);
1206
1207   if (size)
1208     *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1209 }
1210
1211 /**
1212  * gtk_image_get_gicon:
1213  * @image: a #GtkImage
1214  * @gicon: (out) (transfer none) (allow-none): place to store a
1215  *     #GIcon, or %NULL
1216  * @size: (out) (allow-none) (type int): place to store an icon size,
1217  *     or %NULL
1218  *
1219  * Gets the #GIcon and size being displayed by the #GtkImage.
1220  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1221  * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1222  * The caller of this function does not own a reference to the
1223  * returned #GIcon.
1224  * 
1225  * Since: 2.14
1226  **/
1227 void
1228 gtk_image_get_gicon (GtkImage     *image,
1229                      GIcon       **gicon,
1230                      GtkIconSize  *size)
1231 {
1232   GtkImagePrivate *priv;
1233
1234   g_return_if_fail (GTK_IS_IMAGE (image));
1235
1236   priv = image->priv;
1237
1238   if (gicon)
1239     *gicon = _gtk_icon_helper_peek_gicon (priv->icon_helper);
1240
1241   if (size)
1242     *size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
1243 }
1244
1245 /**
1246  * gtk_image_new:
1247  * 
1248  * Creates a new empty #GtkImage widget.
1249  * 
1250  * Return value: a newly created #GtkImage widget. 
1251  **/
1252 GtkWidget*
1253 gtk_image_new (void)
1254 {
1255   return g_object_new (GTK_TYPE_IMAGE, NULL);
1256 }
1257
1258 static void
1259 gtk_image_reset_anim_iter (GtkImage *image)
1260 {
1261   GtkImagePrivate *priv = image->priv;
1262
1263   if (gtk_image_get_storage_type (image) == GTK_IMAGE_ANIMATION)
1264     {
1265       /* Reset the animation */
1266       if (priv->animation_timeout)
1267         {
1268           g_source_remove (priv->animation_timeout);
1269           priv->animation_timeout = 0;
1270         }
1271
1272       g_clear_object (&priv->animation_iter);
1273     }
1274 }
1275
1276 static void
1277 gtk_image_unmap (GtkWidget *widget)
1278 {
1279   gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1280
1281   GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1282 }
1283
1284 static void
1285 gtk_image_unrealize (GtkWidget *widget)
1286 {
1287   gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1288
1289   GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1290 }
1291
1292 static gint
1293 animation_timeout (gpointer data)
1294 {
1295   GtkImage *image = GTK_IMAGE (data);
1296   GtkImagePrivate *priv = image->priv;
1297   int delay;
1298
1299   priv->animation_timeout = 0;
1300
1301   gdk_pixbuf_animation_iter_advance (priv->animation_iter, NULL);
1302
1303   delay = gdk_pixbuf_animation_iter_get_delay_time (priv->animation_iter);
1304   if (delay >= 0)
1305     {
1306       GtkWidget *widget = GTK_WIDGET (image);
1307
1308       priv->animation_timeout =
1309         gdk_threads_add_timeout (delay, animation_timeout, image);
1310
1311       gtk_widget_queue_draw (widget);
1312
1313       if (gtk_widget_is_drawable (widget))
1314         gdk_window_process_updates (gtk_widget_get_window (widget), TRUE);
1315     }
1316
1317   return FALSE;
1318 }
1319
1320 static GdkPixbuf *
1321 get_animation_frame (GtkImage *image)
1322 {
1323   GtkImagePrivate *priv = image->priv;
1324
1325   if (priv->animation_iter == NULL)
1326     {
1327       int delay;
1328
1329       priv->animation_iter = 
1330         gdk_pixbuf_animation_get_iter (_gtk_icon_helper_peek_animation (priv->icon_helper), NULL);
1331
1332       delay = gdk_pixbuf_animation_iter_get_delay_time (priv->animation_iter);
1333       if (delay >= 0)
1334         priv->animation_timeout =
1335           gdk_threads_add_timeout (delay, animation_timeout, image);
1336     }
1337
1338   /* don't advance the anim iter here, or we could get frame changes between two
1339    * exposes of different areas.
1340    */
1341   return g_object_ref (gdk_pixbuf_animation_iter_get_pixbuf (priv->animation_iter));
1342 }
1343
1344 static void
1345 gtk_image_get_preferred_size (GtkImage *image,
1346                               gint     *width_out,
1347                               gint     *height_out)
1348 {
1349   GtkImagePrivate *priv = image->priv;
1350   gint width, height;
1351   GtkBorder border;
1352   GtkStyleContext *context;
1353
1354   context = gtk_widget_get_style_context (GTK_WIDGET (image));
1355   _gtk_icon_helper_get_size (priv->icon_helper, context, &width, &height);
1356   _gtk_misc_get_padding_and_border (GTK_MISC (image), &border);
1357
1358   width += border.left + border.right;
1359   height += border.top + border.bottom;
1360
1361   if (width_out)
1362     *width_out = width;
1363   if (height_out)
1364     *height_out = height;
1365 }
1366
1367 static gint
1368 gtk_image_draw (GtkWidget *widget,
1369                 cairo_t   *cr)
1370 {
1371   GtkImage *image;
1372   GtkImagePrivate *priv;
1373   GtkMisc *misc;
1374   GtkStyleContext *context;
1375   gint x, y, width, height;
1376   gfloat xalign, yalign;
1377   GtkBorder border;
1378
1379   g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1380
1381   image = GTK_IMAGE (widget);
1382   misc = GTK_MISC (image);
1383   priv = image->priv;
1384
1385   context = gtk_widget_get_style_context (widget);
1386
1387   gtk_misc_get_alignment (misc, &xalign, &yalign);
1388   gtk_image_get_preferred_size (image, &width, &height);
1389   _gtk_misc_get_padding_and_border (misc, &border);
1390
1391   if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
1392     xalign = 1.0 - xalign;
1393
1394   x = floor ((gtk_widget_get_allocated_width (widget) - width) * xalign);
1395   y = floor ((gtk_widget_get_allocated_height (widget) - height) * yalign);
1396
1397   gtk_render_background (context, cr, x, y, width, height);
1398   gtk_render_frame (context, cr, x, y, width, height);
1399
1400   x += border.left;
1401   y += border.top;
1402
1403   if (gtk_image_get_storage_type (image) == GTK_IMAGE_ANIMATION)
1404     {
1405       GdkPixbuf *pixbuf = get_animation_frame (image);
1406       gtk_render_icon (context, cr,
1407                        pixbuf,
1408                        x, y);
1409       g_object_unref (pixbuf);
1410     }
1411   else
1412     {
1413       _gtk_icon_helper_draw (priv->icon_helper, 
1414                              context, cr,
1415                              x, y);
1416     }
1417
1418   return FALSE;
1419 }
1420
1421 static void
1422 gtk_image_reset (GtkImage *image)
1423 {
1424   GtkImagePrivate *priv = image->priv;
1425   GtkImageType storage_type;
1426
1427   g_object_freeze_notify (G_OBJECT (image));
1428   storage_type = gtk_image_get_storage_type (image);
1429   
1430   if (storage_type != GTK_IMAGE_EMPTY)
1431     g_object_notify (G_OBJECT (image), "storage-type");
1432
1433   g_object_notify (G_OBJECT (image), "icon-size");
1434   
1435   switch (storage_type)
1436     {
1437     case GTK_IMAGE_PIXBUF:
1438       g_object_notify (G_OBJECT (image), "pixbuf");
1439       break;
1440     case GTK_IMAGE_STOCK:
1441       g_object_notify (G_OBJECT (image), "stock");      
1442       break;
1443     case GTK_IMAGE_ICON_SET:
1444       g_object_notify (G_OBJECT (image), "icon-set");      
1445       break;
1446     case GTK_IMAGE_ANIMATION:
1447       gtk_image_reset_anim_iter (image);      
1448       g_object_notify (G_OBJECT (image), "pixbuf-animation");
1449       break;
1450     case GTK_IMAGE_ICON_NAME:
1451       g_object_notify (G_OBJECT (image), "icon-name");
1452       break;
1453     case GTK_IMAGE_GICON:
1454       g_object_notify (G_OBJECT (image), "gicon");
1455       break;
1456     case GTK_IMAGE_EMPTY:
1457     default:
1458       break;
1459     }
1460
1461   if (priv->filename)
1462     {
1463       g_free (priv->filename);
1464       priv->filename = NULL;
1465       g_object_notify (G_OBJECT (image), "file");
1466     }
1467
1468   _gtk_icon_helper_clear (priv->icon_helper);
1469
1470   g_object_thaw_notify (G_OBJECT (image));
1471 }
1472
1473 /**
1474  * gtk_image_clear:
1475  * @image: a #GtkImage
1476  *
1477  * Resets the image to be empty.
1478  *
1479  * Since: 2.8
1480  */
1481 void
1482 gtk_image_clear (GtkImage *image)
1483 {
1484   gtk_image_reset (image);
1485
1486   if (gtk_widget_get_visible (GTK_WIDGET (image)))
1487       gtk_widget_queue_resize (GTK_WIDGET (image));
1488 }
1489
1490 static void
1491 gtk_image_get_preferred_width (GtkWidget *widget,
1492                                gint      *minimum,
1493                                gint      *natural)
1494 {
1495   gint width;
1496
1497   gtk_image_get_preferred_size (GTK_IMAGE (widget), &width, NULL);
1498   *minimum = *natural = width;
1499
1500
1501 static void
1502 gtk_image_get_preferred_height (GtkWidget *widget,
1503                                 gint      *minimum,
1504                                 gint      *natural)
1505 {
1506   gint height;
1507
1508   gtk_image_get_preferred_size (GTK_IMAGE (widget), NULL, &height);
1509   *minimum = *natural = height;
1510 }
1511
1512 static void
1513 icon_theme_changed (GtkImage *image)
1514 {
1515   GtkImagePrivate *priv = image->priv;
1516
1517   _gtk_icon_helper_invalidate (priv->icon_helper);
1518   gtk_widget_queue_draw (GTK_WIDGET (image));
1519 }
1520
1521 static void
1522 gtk_image_style_updated (GtkWidget *widget)
1523 {
1524   GTK_WIDGET_CLASS (gtk_image_parent_class)->style_updated (widget);
1525
1526   icon_theme_changed (GTK_IMAGE (widget));
1527 }
1528
1529 static void
1530 gtk_image_screen_changed (GtkWidget *widget,
1531                           GdkScreen *prev_screen)
1532 {
1533   GtkImage *image;
1534
1535   image = GTK_IMAGE (widget);
1536
1537   if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
1538     GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
1539
1540   icon_theme_changed (image);
1541 }
1542
1543 void
1544 _gtk_image_gicon_data_clear (GtkImageGIconData *data)
1545 {
1546   if (data->pixbuf)
1547     {
1548       g_object_unref (data->pixbuf);
1549       data->pixbuf = NULL;
1550     }
1551   if (data->icon)
1552     {
1553       g_object_unref (data->icon);
1554       data->icon = NULL;
1555     }
1556 }
1557
1558 /**
1559  * gtk_image_set_pixel_size:
1560  * @image: a #GtkImage
1561  * @pixel_size: the new pixel size
1562  * 
1563  * Sets the pixel size to use for named icons. If the pixel size is set
1564  * to a value != -1, it is used instead of the icon size set by
1565  * gtk_image_set_from_icon_name().
1566  *
1567  * Since: 2.6
1568  */
1569 void 
1570 gtk_image_set_pixel_size (GtkImage *image,
1571                           gint      pixel_size)
1572 {
1573   GtkImagePrivate *priv;
1574   gint old_pixel_size;
1575
1576   g_return_if_fail (GTK_IS_IMAGE (image));
1577
1578   priv = image->priv;
1579   old_pixel_size = gtk_image_get_pixel_size (image);
1580
1581   if (pixel_size != old_pixel_size)
1582     {
1583       _gtk_icon_helper_set_pixel_size (priv->icon_helper, pixel_size);
1584
1585       if (gtk_widget_get_visible (GTK_WIDGET (image)))
1586         gtk_widget_queue_resize (GTK_WIDGET (image));
1587
1588       g_object_notify (G_OBJECT (image), "pixel-size");
1589     }
1590 }
1591
1592 /**
1593  * gtk_image_get_pixel_size:
1594  * @image: a #GtkImage
1595  * 
1596  * Gets the pixel size used for named icons.
1597  *
1598  * Returns: the pixel size used for named icons.
1599  *
1600  * Since: 2.6
1601  */
1602 gint
1603 gtk_image_get_pixel_size (GtkImage *image)
1604 {
1605   g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
1606
1607   return _gtk_icon_helper_get_pixel_size (image->priv->icon_helper);
1608 }