]> Pileus Git - ~andy/gtk/blob - gtk/gtkimage.c
Fix out/transfer annotations on GtkImage getters
[~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 #include <math.h>
29 #include <string.h>
30
31 #include "gtkcontainer.h"
32 #include "gtkimage.h"
33 #include "gtkiconfactory.h"
34 #include "gtkstock.h"
35 #include "gtkicontheme.h"
36 #include "gtkintl.h"
37 #include "gtkprivate.h"
38 #include "gtkalias.h"
39
40 typedef struct _GtkImagePrivate GtkImagePrivate;
41
42 struct _GtkImagePrivate
43 {
44   /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
45   gchar *filename;
46
47   gint pixel_size;
48   guint need_calc_size : 1;
49 };
50
51 #define GTK_IMAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_IMAGE, GtkImagePrivate))
52
53
54 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
55 static gint gtk_image_expose       (GtkWidget      *widget,
56                                     GdkEventExpose *event);
57 static void gtk_image_unmap        (GtkWidget      *widget);
58 static void gtk_image_unrealize    (GtkWidget      *widget);
59 static void gtk_image_size_request (GtkWidget      *widget,
60                                     GtkRequisition *requisition);
61 static void gtk_image_style_set    (GtkWidget      *widget,
62                                     GtkStyle       *prev_style);
63 static void gtk_image_screen_changed (GtkWidget    *widget,
64                                       GdkScreen    *prev_screen);
65 static void gtk_image_destroy      (GtkObject      *object);
66 static void gtk_image_reset        (GtkImage       *image);
67 static void gtk_image_calc_size    (GtkImage       *image);
68
69 static void gtk_image_update_size  (GtkImage       *image,
70                                     gint            image_width,
71                                     gint            image_height);
72
73 static void gtk_image_set_property      (GObject          *object,
74                                          guint             prop_id,
75                                          const GValue     *value,
76                                          GParamSpec       *pspec);
77 static void gtk_image_get_property      (GObject          *object,
78                                          guint             prop_id,
79                                          GValue           *value,
80                                          GParamSpec       *pspec);
81
82 static void icon_theme_changed          (GtkImage         *image);
83
84 enum
85 {
86   PROP_0,
87   PROP_PIXBUF,
88   PROP_PIXMAP,
89   PROP_IMAGE,
90   PROP_MASK,
91   PROP_FILE,
92   PROP_STOCK,
93   PROP_ICON_SET,
94   PROP_ICON_SIZE,
95   PROP_PIXEL_SIZE,
96   PROP_PIXBUF_ANIMATION,
97   PROP_ICON_NAME,
98   PROP_STORAGE_TYPE,
99   PROP_GICON
100 };
101
102 G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
103
104 static void
105 gtk_image_class_init (GtkImageClass *class)
106 {
107   GObjectClass *gobject_class;
108   GtkObjectClass *object_class;
109   GtkWidgetClass *widget_class;
110
111   gobject_class = G_OBJECT_CLASS (class);
112   
113   gobject_class->set_property = gtk_image_set_property;
114   gobject_class->get_property = gtk_image_get_property;
115   
116   object_class = GTK_OBJECT_CLASS (class);
117   
118   object_class->destroy = gtk_image_destroy;
119
120   widget_class = GTK_WIDGET_CLASS (class);
121   
122   widget_class->expose_event = gtk_image_expose;
123   widget_class->size_request = gtk_image_size_request;
124   widget_class->unmap = gtk_image_unmap;
125   widget_class->unrealize = gtk_image_unrealize;
126   widget_class->style_set = gtk_image_style_set;
127   widget_class->screen_changed = gtk_image_screen_changed;
128   
129   g_object_class_install_property (gobject_class,
130                                    PROP_PIXBUF,
131                                    g_param_spec_object ("pixbuf",
132                                                         P_("Pixbuf"),
133                                                         P_("A GdkPixbuf to display"),
134                                                         GDK_TYPE_PIXBUF,
135                                                         GTK_PARAM_READWRITE));
136
137   g_object_class_install_property (gobject_class,
138                                    PROP_PIXMAP,
139                                    g_param_spec_object ("pixmap",
140                                                         P_("Pixmap"),
141                                                         P_("A GdkPixmap to display"),
142                                                         GDK_TYPE_PIXMAP,
143                                                         GTK_PARAM_READWRITE));
144
145   g_object_class_install_property (gobject_class,
146                                    PROP_IMAGE,
147                                    g_param_spec_object ("image",
148                                                         P_("Image"),
149                                                         P_("A GdkImage to display"),
150                                                         GDK_TYPE_IMAGE,
151                                                         GTK_PARAM_READWRITE));
152
153   g_object_class_install_property (gobject_class,
154                                    PROP_MASK,
155                                    g_param_spec_object ("mask",
156                                                         P_("Mask"),
157                                                         P_("Mask bitmap to use with GdkImage or GdkPixmap"),
158                                                         GDK_TYPE_PIXMAP,
159                                                         GTK_PARAM_READWRITE));
160   
161   g_object_class_install_property (gobject_class,
162                                    PROP_FILE,
163                                    g_param_spec_string ("file",
164                                                         P_("Filename"),
165                                                         P_("Filename to load and display"),
166                                                         NULL,
167                                                         GTK_PARAM_READWRITE));
168   
169
170   g_object_class_install_property (gobject_class,
171                                    PROP_STOCK,
172                                    g_param_spec_string ("stock",
173                                                         P_("Stock ID"),
174                                                         P_("Stock ID for a stock image to display"),
175                                                         NULL,
176                                                         GTK_PARAM_READWRITE));
177   
178   g_object_class_install_property (gobject_class,
179                                    PROP_ICON_SET,
180                                    g_param_spec_boxed ("icon-set",
181                                                        P_("Icon set"),
182                                                        P_("Icon set to display"),
183                                                        GTK_TYPE_ICON_SET,
184                                                        GTK_PARAM_READWRITE));
185   
186   g_object_class_install_property (gobject_class,
187                                    PROP_ICON_SIZE,
188                                    g_param_spec_int ("icon-size",
189                                                      P_("Icon size"),
190                                                      P_("Symbolic size to use for stock icon, icon set or named icon"),
191                                                      0, G_MAXINT,
192                                                      DEFAULT_ICON_SIZE,
193                                                      GTK_PARAM_READWRITE));
194   /**
195    * GtkImage:pixel-size:
196    *
197    * The "pixel-size" property can be used to specify a fixed size
198    * overriding the #GtkImage:icon-size property for images of type 
199    * %GTK_IMAGE_ICON_NAME. 
200    *
201    * Since: 2.6
202    */
203   g_object_class_install_property (gobject_class,
204                                    PROP_PIXEL_SIZE,
205                                    g_param_spec_int ("pixel-size",
206                                                      P_("Pixel size"),
207                                                      P_("Pixel size to use for named icon"),
208                                                      -1, G_MAXINT,
209                                                      -1,
210                                                      GTK_PARAM_READWRITE));
211   
212   g_object_class_install_property (gobject_class,
213                                    PROP_PIXBUF_ANIMATION,
214                                    g_param_spec_object ("pixbuf-animation",
215                                                         P_("Animation"),
216                                                         P_("GdkPixbufAnimation to display"),
217                                                         GDK_TYPE_PIXBUF_ANIMATION,
218                                                         GTK_PARAM_READWRITE));
219
220   /**
221    * GtkImage:icon-name:
222    *
223    * The name of the icon in the icon theme. If the icon theme is
224    * changed, the image will be updated automatically.
225    *
226    * Since: 2.6
227    */
228   g_object_class_install_property (gobject_class,
229                                    PROP_ICON_NAME,
230                                    g_param_spec_string ("icon-name",
231                                                         P_("Icon Name"),
232                                                         P_("The name of the icon from the icon theme"),
233                                                         NULL,
234                                                         GTK_PARAM_READWRITE));
235   
236   /**
237    * GtkImage:gicon:
238    *
239    * The GIcon displayed in the GtkImage. For themed icons,
240    * If the icon theme is changed, the image will be updated
241    * automatically.
242    *
243    * Since: 2.14
244    */
245   g_object_class_install_property (gobject_class,
246                                    PROP_GICON,
247                                    g_param_spec_object ("gicon",
248                                                         P_("Icon"),
249                                                         P_("The GIcon being displayed"),
250                                                         G_TYPE_ICON,
251                                                         GTK_PARAM_READWRITE));
252   
253   g_object_class_install_property (gobject_class,
254                                    PROP_STORAGE_TYPE,
255                                    g_param_spec_enum ("storage-type",
256                                                       P_("Storage type"),
257                                                       P_("The representation being used for image data"),
258                                                       GTK_TYPE_IMAGE_TYPE,
259                                                       GTK_IMAGE_EMPTY,
260                                                       GTK_PARAM_READABLE));
261
262   g_type_class_add_private (object_class, sizeof (GtkImagePrivate));
263 }
264
265 static void
266 gtk_image_init (GtkImage *image)
267 {
268   GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);
269
270   GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);
271
272   image->storage_type = GTK_IMAGE_EMPTY;
273   image->icon_size = DEFAULT_ICON_SIZE;
274   image->mask = NULL;
275
276   priv->pixel_size = -1;
277
278   priv->filename = NULL;
279 }
280
281 static void
282 gtk_image_destroy (GtkObject *object)
283 {
284   GtkImage *image = GTK_IMAGE (object);
285
286   gtk_image_reset (image);
287   
288   GTK_OBJECT_CLASS (gtk_image_parent_class)->destroy (object);
289 }
290
291 static void 
292 gtk_image_set_property (GObject      *object,
293                         guint         prop_id,
294                         const GValue *value,
295                         GParamSpec   *pspec)
296 {
297   GtkImage *image;
298
299   image = GTK_IMAGE (object);
300   
301   switch (prop_id)
302     {
303     case PROP_PIXBUF:
304       gtk_image_set_from_pixbuf (image,
305                                  g_value_get_object (value));
306       break;
307     case PROP_PIXMAP:
308       gtk_image_set_from_pixmap (image,
309                                  g_value_get_object (value),
310                                  image->mask);
311       break;
312     case PROP_IMAGE:
313       gtk_image_set_from_image (image,
314                                 g_value_get_object (value),
315                                 image->mask);
316       break;
317     case PROP_MASK:
318       if (image->storage_type == GTK_IMAGE_PIXMAP)
319         gtk_image_set_from_pixmap (image,
320                                    image->data.pixmap.pixmap,
321                                    g_value_get_object (value));
322       else if (image->storage_type == GTK_IMAGE_IMAGE)
323         gtk_image_set_from_image (image,
324                                   image->data.image.image,
325                                   g_value_get_object (value));
326       else
327         {
328           GdkBitmap *mask;
329
330           mask = g_value_get_object (value);
331
332           if (mask)
333             g_object_ref (mask);
334           
335           gtk_image_clear (image);
336
337           image->mask = mask;
338         }
339       break;
340     case PROP_FILE:
341       gtk_image_set_from_file (image, g_value_get_string (value));
342       break;
343     case PROP_STOCK:
344       gtk_image_set_from_stock (image, g_value_get_string (value),
345                                 image->icon_size);
346       break;
347     case PROP_ICON_SET:
348       gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
349                                    image->icon_size);
350       break;
351     case PROP_ICON_SIZE:
352       if (image->storage_type == GTK_IMAGE_STOCK)
353         gtk_image_set_from_stock (image,
354                                   image->data.stock.stock_id,
355                                   g_value_get_int (value));
356       else if (image->storage_type == GTK_IMAGE_ICON_SET)
357         gtk_image_set_from_icon_set (image,
358                                      image->data.icon_set.icon_set,
359                                      g_value_get_int (value));
360       else if (image->storage_type == GTK_IMAGE_ICON_NAME)
361         gtk_image_set_from_icon_name (image,
362                                       image->data.name.icon_name,
363                                       g_value_get_int (value));
364       else if (image->storage_type == GTK_IMAGE_GICON)
365         gtk_image_set_from_gicon (image,
366                                   image->data.gicon.icon,
367                                   g_value_get_int (value));
368       else
369         /* Save to be used when STOCK, ICON_SET, ICON_NAME or GICON property comes in */
370         image->icon_size = g_value_get_int (value);
371       break;
372     case PROP_PIXEL_SIZE:
373       gtk_image_set_pixel_size (image, g_value_get_int (value));
374       break;
375     case PROP_PIXBUF_ANIMATION:
376       gtk_image_set_from_animation (image,
377                                     g_value_get_object (value));
378       break;
379     case PROP_ICON_NAME:
380       gtk_image_set_from_icon_name (image, g_value_get_string (value),
381                                     image->icon_size);
382       break;
383     case PROP_GICON:
384       gtk_image_set_from_gicon (image, g_value_get_object (value),
385                                 image->icon_size);
386       break;
387
388     default:
389       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
390       break;
391     }
392 }
393
394 static void 
395 gtk_image_get_property (GObject     *object,
396                         guint        prop_id,
397                         GValue      *value,
398                         GParamSpec  *pspec)
399 {
400   GtkImage *image;
401   GtkImagePrivate *priv;
402
403   image = GTK_IMAGE (object);
404   priv = GTK_IMAGE_GET_PRIVATE (image);
405
406   /* The "getter" functions whine if you try to get the wrong
407    * storage type. This function is instead robust against that,
408    * so that GUI builders don't have to jump through hoops
409    * to avoid g_warning
410    */
411   
412   switch (prop_id)
413     {
414     case PROP_PIXBUF:
415       if (image->storage_type != GTK_IMAGE_PIXBUF)
416         g_value_set_object (value, NULL);
417       else
418         g_value_set_object (value,
419                             gtk_image_get_pixbuf (image));
420       break;
421     case PROP_PIXMAP:
422       if (image->storage_type != GTK_IMAGE_PIXMAP)
423         g_value_set_object (value, NULL);
424       else
425         g_value_set_object (value,
426                             image->data.pixmap.pixmap);
427       break;
428     case PROP_MASK:
429       g_value_set_object (value, image->mask);
430       break;
431     case PROP_IMAGE:
432       if (image->storage_type != GTK_IMAGE_IMAGE)
433         g_value_set_object (value, NULL);
434       else
435         g_value_set_object (value,
436                             image->data.image.image);
437       break;
438     case PROP_FILE:
439       g_value_set_string (value, priv->filename);
440       break;
441     case PROP_STOCK:
442       if (image->storage_type != GTK_IMAGE_STOCK)
443         g_value_set_string (value, NULL);
444       else
445         g_value_set_string (value,
446                             image->data.stock.stock_id);
447       break;
448     case PROP_ICON_SET:
449       if (image->storage_type != GTK_IMAGE_ICON_SET)
450         g_value_set_boxed (value, NULL);
451       else
452         g_value_set_boxed (value,
453                            image->data.icon_set.icon_set);
454       break;      
455     case PROP_ICON_SIZE:
456       g_value_set_int (value, image->icon_size);
457       break;
458     case PROP_PIXEL_SIZE:
459       g_value_set_int (value, priv->pixel_size);
460       break;
461     case PROP_PIXBUF_ANIMATION:
462       if (image->storage_type != GTK_IMAGE_ANIMATION)
463         g_value_set_object (value, NULL);
464       else
465         g_value_set_object (value,
466                             image->data.anim.anim);
467       break;
468     case PROP_ICON_NAME:
469       if (image->storage_type != GTK_IMAGE_ICON_NAME)
470         g_value_set_string (value, NULL);
471       else
472         g_value_set_string (value,
473                             image->data.name.icon_name);
474       break;
475     case PROP_GICON:
476       if (image->storage_type != GTK_IMAGE_GICON)
477         g_value_set_object (value, NULL);
478       else
479         g_value_set_object (value,
480                             image->data.gicon.icon);
481       break;
482     case PROP_STORAGE_TYPE:
483       g_value_set_enum (value, image->storage_type);
484       break;
485       
486     default:
487       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
488       break;
489     }
490 }
491
492
493 /**
494  * gtk_image_new_from_pixmap:
495  * @pixmap: (allow-none): a #GdkPixmap, or %NULL
496  * @mask: (allow-none): a #GdkBitmap, or %NULL
497  *
498  * Creates a #GtkImage widget displaying @pixmap with a @mask.
499  * A #GdkPixmap is a server-side image buffer in the pixel format of the
500  * current display. The #GtkImage does not assume a reference to the
501  * pixmap or mask; you still need to unref them if you own references.
502  * #GtkImage will add its own reference rather than adopting yours.
503  * 
504  * Return value: a new #GtkImage
505  **/
506 GtkWidget*
507 gtk_image_new_from_pixmap (GdkPixmap *pixmap,
508                            GdkBitmap *mask)
509 {
510   GtkImage *image;
511
512   image = g_object_new (GTK_TYPE_IMAGE, NULL);
513
514   gtk_image_set_from_pixmap (image, pixmap, mask);
515
516   return GTK_WIDGET (image);
517 }
518
519 /**
520  * gtk_image_new_from_image:
521  * @image: (allow-none): a #GdkImage, or %NULL
522  * @mask: (allow-none): a #GdkBitmap, or %NULL
523  *
524  * Creates a #GtkImage widget displaying a @image with a @mask.
525  * A #GdkImage is a client-side image buffer in the pixel format of the
526  * current display. The #GtkImage does not assume a reference to the
527  * image or mask; you still need to unref them if you own references.
528  * #GtkImage will add its own reference rather than adopting yours.
529  * 
530  * Return value: a new #GtkImage
531  **/
532 GtkWidget*
533 gtk_image_new_from_image  (GdkImage  *gdk_image,
534                            GdkBitmap *mask)
535 {
536   GtkImage *image;
537
538   image = g_object_new (GTK_TYPE_IMAGE, NULL);
539
540   gtk_image_set_from_image (image, gdk_image, mask);
541
542   return GTK_WIDGET (image);
543 }
544
545 /**
546  * gtk_image_new_from_file:
547  * @filename: a filename
548  * 
549  * Creates a new #GtkImage displaying the file @filename. If the file
550  * isn't found or can't be loaded, the resulting #GtkImage will
551  * display a "broken image" icon. This function never returns %NULL,
552  * it always returns a valid #GtkImage widget.
553  *
554  * If the file contains an animation, the image will contain an
555  * animation.
556  *
557  * If you need to detect failures to load the file, use
558  * gdk_pixbuf_new_from_file() to load the file yourself, then create
559  * the #GtkImage from the pixbuf. (Or for animations, use
560  * gdk_pixbuf_animation_new_from_file()).
561  *
562  * The storage type (gtk_image_get_storage_type()) of the returned
563  * image is not defined, it will be whatever is appropriate for
564  * displaying the file.
565  * 
566  * Return value: a new #GtkImage
567  **/
568 GtkWidget*
569 gtk_image_new_from_file   (const gchar *filename)
570 {
571   GtkImage *image;
572
573   image = g_object_new (GTK_TYPE_IMAGE, NULL);
574
575   gtk_image_set_from_file (image, filename);
576
577   return GTK_WIDGET (image);
578 }
579
580 /**
581  * gtk_image_new_from_pixbuf:
582  * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
583  *
584  * Creates a new #GtkImage displaying @pixbuf.
585  * The #GtkImage does not assume a reference to the
586  * pixbuf; you still need to unref it if you own references.
587  * #GtkImage will add its own reference rather than adopting yours.
588  * 
589  * Note that this function just creates an #GtkImage from the pixbuf. The
590  * #GtkImage created will not react to state changes. Should you want that, 
591  * you should use gtk_image_new_from_icon_set().
592  * 
593  * Return value: a new #GtkImage
594  **/
595 GtkWidget*
596 gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
597 {
598   GtkImage *image;
599
600   image = g_object_new (GTK_TYPE_IMAGE, NULL);
601
602   gtk_image_set_from_pixbuf (image, pixbuf);
603
604   return GTK_WIDGET (image);  
605 }
606
607 /**
608  * gtk_image_new_from_stock:
609  * @stock_id: a stock icon name
610  * @size: a stock icon size
611  * 
612  * Creates a #GtkImage displaying a stock icon. Sample stock icon
613  * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
614  * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
615  * icon name isn't known, the image will be empty.
616  * You can register your own stock icon names, see
617  * gtk_icon_factory_add_default() and gtk_icon_factory_add().
618  * 
619  * Return value: a new #GtkImage displaying the stock icon
620  **/
621 GtkWidget*
622 gtk_image_new_from_stock (const gchar    *stock_id,
623                           GtkIconSize     size)
624 {
625   GtkImage *image;
626
627   image = g_object_new (GTK_TYPE_IMAGE, NULL);
628
629   gtk_image_set_from_stock (image, stock_id, size);
630
631   return GTK_WIDGET (image);
632 }
633
634 /**
635  * gtk_image_new_from_icon_set:
636  * @icon_set: a #GtkIconSet
637  * @size: a stock icon size
638  *
639  * Creates a #GtkImage displaying an icon set. Sample stock sizes are
640  * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
641  * this function, usually it's better to create a #GtkIconFactory, put
642  * your icon sets in the icon factory, add the icon factory to the
643  * list of default factories with gtk_icon_factory_add_default(), and
644  * then use gtk_image_new_from_stock(). This will allow themes to
645  * override the icon you ship with your application.
646  *
647  * The #GtkImage does not assume a reference to the
648  * icon set; you still need to unref it if you own references.
649  * #GtkImage will add its own reference rather than adopting yours.
650  * 
651  * Return value: a new #GtkImage
652  **/
653 GtkWidget*
654 gtk_image_new_from_icon_set (GtkIconSet     *icon_set,
655                              GtkIconSize     size)
656 {
657   GtkImage *image;
658
659   image = g_object_new (GTK_TYPE_IMAGE, NULL);
660
661   gtk_image_set_from_icon_set (image, icon_set, size);
662
663   return GTK_WIDGET (image);
664 }
665
666 /**
667  * gtk_image_new_from_animation:
668  * @animation: an animation
669  * 
670  * Creates a #GtkImage displaying the given animation.
671  * The #GtkImage does not assume a reference to the
672  * animation; you still need to unref it if you own references.
673  * #GtkImage will add its own reference rather than adopting yours.
674  *
675  * Note that the animation frames are shown using a timeout with
676  * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
677  * keep in mind that the animation will only be shown if the main loop
678  * is not busy with something that has a higher priority.
679  *
680  * Return value: a new #GtkImage widget
681  **/
682 GtkWidget*
683 gtk_image_new_from_animation (GdkPixbufAnimation *animation)
684 {
685   GtkImage *image;
686
687   g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
688   
689   image = g_object_new (GTK_TYPE_IMAGE, NULL);
690
691   gtk_image_set_from_animation (image, animation);
692
693   return GTK_WIDGET (image);
694 }
695
696 /**
697  * gtk_image_new_from_icon_name:
698  * @icon_name: an icon name
699  * @size: a stock icon size
700  * 
701  * Creates a #GtkImage displaying an icon from the current icon theme.
702  * If the icon name isn't known, a "broken image" icon will be
703  * displayed instead.  If the current icon theme is changed, the icon
704  * will be updated appropriately.
705  * 
706  * Return value: a new #GtkImage displaying the themed icon
707  *
708  * Since: 2.6
709  **/
710 GtkWidget*
711 gtk_image_new_from_icon_name (const gchar    *icon_name,
712                               GtkIconSize     size)
713 {
714   GtkImage *image;
715
716   image = g_object_new (GTK_TYPE_IMAGE, NULL);
717
718   gtk_image_set_from_icon_name (image, icon_name, size);
719
720   return GTK_WIDGET (image);
721 }
722
723 /**
724  * gtk_image_new_from_gicon:
725  * @icon: an icon
726  * @size: a stock icon size
727  * 
728  * Creates a #GtkImage displaying an icon from the current icon theme.
729  * If the icon name isn't known, a "broken image" icon will be
730  * displayed instead.  If the current icon theme is changed, the icon
731  * will be updated appropriately.
732  * 
733  * Return value: a new #GtkImage displaying the themed icon
734  *
735  * Since: 2.14
736  **/
737 GtkWidget*
738 gtk_image_new_from_gicon (GIcon *icon,
739                           GtkIconSize     size)
740 {
741   GtkImage *image;
742
743   image = g_object_new (GTK_TYPE_IMAGE, NULL);
744
745   gtk_image_set_from_gicon (image, icon, size);
746
747   return GTK_WIDGET (image);
748 }
749
750 /**
751  * gtk_image_set_from_pixmap:
752  * @image: a #GtkImage
753  * @pixmap: (allow-none): a #GdkPixmap or %NULL
754  * @mask: (allow-none): a #GdkBitmap or %NULL
755  *
756  * See gtk_image_new_from_pixmap() for details.
757  **/
758 void
759 gtk_image_set_from_pixmap (GtkImage  *image,
760                            GdkPixmap *pixmap,
761                            GdkBitmap *mask)
762 {
763   g_return_if_fail (GTK_IS_IMAGE (image));
764   g_return_if_fail (pixmap == NULL ||
765                     GDK_IS_PIXMAP (pixmap));
766   g_return_if_fail (mask == NULL ||
767                     GDK_IS_PIXMAP (mask));
768
769   g_object_freeze_notify (G_OBJECT (image));
770   
771   if (pixmap)
772     g_object_ref (pixmap);
773
774   if (mask)
775     g_object_ref (mask);
776
777   gtk_image_clear (image);
778
779   image->mask = mask;
780   
781   if (pixmap)
782     {
783       int width;
784       int height;
785       
786       image->storage_type = GTK_IMAGE_PIXMAP;
787
788       image->data.pixmap.pixmap = pixmap;
789
790       gdk_drawable_get_size (GDK_DRAWABLE (pixmap), &width, &height);
791
792       gtk_image_update_size (image, width, height);
793     }
794
795   g_object_notify (G_OBJECT (image), "pixmap");
796   g_object_notify (G_OBJECT (image), "mask");
797   
798   g_object_thaw_notify (G_OBJECT (image));
799 }
800
801 /**
802  * gtk_image_set_from_image:
803  * @image: a #GtkImage
804  * @gdk_image: (allow-none): a #GdkImage or %NULL
805  * @mask:  (allow-none): a #GdkBitmap or %NULL
806  *
807  * See gtk_image_new_from_image() for details.
808  **/
809 void
810 gtk_image_set_from_image  (GtkImage  *image,
811                            GdkImage  *gdk_image,
812                            GdkBitmap *mask)
813 {
814   g_return_if_fail (GTK_IS_IMAGE (image));
815   g_return_if_fail (gdk_image == NULL ||
816                     GDK_IS_IMAGE (gdk_image));
817   g_return_if_fail (mask == NULL ||
818                     GDK_IS_PIXMAP (mask));
819
820   g_object_freeze_notify (G_OBJECT (image));
821   
822   if (gdk_image)
823     g_object_ref (gdk_image);
824
825   if (mask)
826     g_object_ref (mask);
827
828   gtk_image_clear (image);
829
830   if (gdk_image)
831     {
832       image->storage_type = GTK_IMAGE_IMAGE;
833
834       image->data.image.image = gdk_image;
835       image->mask = mask;
836
837       gtk_image_update_size (image, gdk_image->width, gdk_image->height);
838     }
839   else
840     {
841       /* Clean up the mask if gdk_image was NULL */
842       if (mask)
843         g_object_unref (mask);
844     }
845
846   g_object_notify (G_OBJECT (image), "image");
847   g_object_notify (G_OBJECT (image), "mask");
848   
849   g_object_thaw_notify (G_OBJECT (image));
850 }
851
852 /**
853  * gtk_image_set_from_file:
854  * @image: a #GtkImage
855  * @filename: (allow-none): a filename or %NULL
856  *
857  * See gtk_image_new_from_file() for details.
858  **/
859 void
860 gtk_image_set_from_file   (GtkImage    *image,
861                            const gchar *filename)
862 {
863   GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);
864   GdkPixbufAnimation *anim;
865   
866   g_return_if_fail (GTK_IS_IMAGE (image));
867
868   g_object_freeze_notify (G_OBJECT (image));
869   
870   gtk_image_clear (image);
871
872   if (filename == NULL)
873     {
874       priv->filename = NULL;
875       g_object_thaw_notify (G_OBJECT (image));
876       return;
877     }
878
879   anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
880
881   if (anim == NULL)
882     {
883       gtk_image_set_from_stock (image,
884                                 GTK_STOCK_MISSING_IMAGE,
885                                 GTK_ICON_SIZE_BUTTON);
886       g_object_thaw_notify (G_OBJECT (image));
887       return;
888     }
889
890   /* We could just unconditionally set_from_animation,
891    * but it's nicer for memory if we toss the animation
892    * if it's just a single pixbuf
893    */
894
895   if (gdk_pixbuf_animation_is_static_image (anim))
896     gtk_image_set_from_pixbuf (image,
897                                gdk_pixbuf_animation_get_static_image (anim));
898   else
899     gtk_image_set_from_animation (image, anim);
900
901   g_object_unref (anim);
902
903   priv->filename = g_strdup (filename);
904   
905   g_object_thaw_notify (G_OBJECT (image));
906 }
907
908 /**
909  * gtk_image_set_from_pixbuf:
910  * @image: a #GtkImage
911  * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
912  *
913  * See gtk_image_new_from_pixbuf() for details.
914  **/
915 void
916 gtk_image_set_from_pixbuf (GtkImage  *image,
917                            GdkPixbuf *pixbuf)
918 {
919   g_return_if_fail (GTK_IS_IMAGE (image));
920   g_return_if_fail (pixbuf == NULL ||
921                     GDK_IS_PIXBUF (pixbuf));
922
923   g_object_freeze_notify (G_OBJECT (image));
924   
925   if (pixbuf)
926     g_object_ref (pixbuf);
927
928   gtk_image_clear (image);
929
930   if (pixbuf != NULL)
931     {
932       image->storage_type = GTK_IMAGE_PIXBUF;
933
934       image->data.pixbuf.pixbuf = pixbuf;
935
936       gtk_image_update_size (image,
937                              gdk_pixbuf_get_width (pixbuf),
938                              gdk_pixbuf_get_height (pixbuf));
939     }
940
941   g_object_notify (G_OBJECT (image), "pixbuf");
942   
943   g_object_thaw_notify (G_OBJECT (image));
944 }
945
946 /**
947  * gtk_image_set_from_stock:
948  * @image: a #GtkImage
949  * @stock_id: a stock icon name
950  * @size: a stock icon size
951  *
952  * See gtk_image_new_from_stock() for details.
953  **/
954 void
955 gtk_image_set_from_stock  (GtkImage       *image,
956                            const gchar    *stock_id,
957                            GtkIconSize     size)
958 {
959   gchar *new_id;
960   
961   g_return_if_fail (GTK_IS_IMAGE (image));
962
963   g_object_freeze_notify (G_OBJECT (image));
964
965   /* in case stock_id == image->data.stock.stock_id */
966   new_id = g_strdup (stock_id);
967   
968   gtk_image_clear (image);
969
970   if (new_id)
971     {
972       image->storage_type = GTK_IMAGE_STOCK;
973       
974       image->data.stock.stock_id = new_id;
975       image->icon_size = size;
976
977       /* Size is demand-computed in size request method
978        * if we're a stock image, since changing the
979        * style impacts the size request
980        */
981     }
982
983   g_object_notify (G_OBJECT (image), "stock");
984   g_object_notify (G_OBJECT (image), "icon-size");
985   
986   g_object_thaw_notify (G_OBJECT (image));
987 }
988
989 /**
990  * gtk_image_set_from_icon_set:
991  * @image: a #GtkImage
992  * @icon_set: a #GtkIconSet
993  * @size: a stock icon size
994  *
995  * See gtk_image_new_from_icon_set() for details.
996  **/
997 void
998 gtk_image_set_from_icon_set  (GtkImage       *image,
999                               GtkIconSet     *icon_set,
1000                               GtkIconSize     size)
1001 {
1002   g_return_if_fail (GTK_IS_IMAGE (image));
1003
1004   g_object_freeze_notify (G_OBJECT (image));
1005   
1006   if (icon_set)
1007     gtk_icon_set_ref (icon_set);
1008   
1009   gtk_image_clear (image);
1010
1011   if (icon_set)
1012     {      
1013       image->storage_type = GTK_IMAGE_ICON_SET;
1014       
1015       image->data.icon_set.icon_set = icon_set;
1016       image->icon_size = size;
1017
1018       /* Size is demand-computed in size request method
1019        * if we're an icon set
1020        */
1021     }
1022   
1023   g_object_notify (G_OBJECT (image), "icon-set");
1024   g_object_notify (G_OBJECT (image), "icon-size");
1025   
1026   g_object_thaw_notify (G_OBJECT (image));
1027 }
1028
1029 /**
1030  * gtk_image_set_from_animation:
1031  * @image: a #GtkImage
1032  * @animation: the #GdkPixbufAnimation
1033  * 
1034  * Causes the #GtkImage to display the given animation (or display
1035  * nothing, if you set the animation to %NULL).
1036  **/
1037 void
1038 gtk_image_set_from_animation (GtkImage           *image,
1039                               GdkPixbufAnimation *animation)
1040 {
1041   g_return_if_fail (GTK_IS_IMAGE (image));
1042   g_return_if_fail (animation == NULL ||
1043                     GDK_IS_PIXBUF_ANIMATION (animation));
1044
1045   g_object_freeze_notify (G_OBJECT (image));
1046   
1047   if (animation)
1048     g_object_ref (animation);
1049
1050   gtk_image_clear (image);
1051
1052   if (animation != NULL)
1053     {
1054       image->storage_type = GTK_IMAGE_ANIMATION;
1055
1056       image->data.anim.anim = animation;
1057       image->data.anim.frame_timeout = 0;
1058       image->data.anim.iter = NULL;
1059       
1060       gtk_image_update_size (image,
1061                              gdk_pixbuf_animation_get_width (animation),
1062                              gdk_pixbuf_animation_get_height (animation));
1063     }
1064
1065   g_object_notify (G_OBJECT (image), "pixbuf-animation");
1066   
1067   g_object_thaw_notify (G_OBJECT (image));
1068 }
1069
1070 /**
1071  * gtk_image_set_from_icon_name:
1072  * @image: a #GtkImage
1073  * @icon_name: an icon name
1074  * @size: an icon size
1075  *
1076  * See gtk_image_new_from_icon_name() for details.
1077  * 
1078  * Since: 2.6
1079  **/
1080 void
1081 gtk_image_set_from_icon_name  (GtkImage       *image,
1082                                const gchar    *icon_name,
1083                                GtkIconSize     size)
1084 {
1085   gchar *new_name;
1086   
1087   g_return_if_fail (GTK_IS_IMAGE (image));
1088
1089   g_object_freeze_notify (G_OBJECT (image));
1090
1091   /* in case icon_name == image->data.name.icon_name */
1092   new_name = g_strdup (icon_name);
1093   
1094   gtk_image_clear (image);
1095
1096   if (new_name)
1097     {
1098       image->storage_type = GTK_IMAGE_ICON_NAME;
1099       
1100       image->data.name.icon_name = new_name;
1101       image->icon_size = size;
1102
1103       /* Size is demand-computed in size request method
1104        * if we're a icon theme image, since changing the
1105        * style impacts the size request
1106        */
1107     }
1108
1109   g_object_notify (G_OBJECT (image), "icon-name");
1110   g_object_notify (G_OBJECT (image), "icon-size");
1111   
1112   g_object_thaw_notify (G_OBJECT (image));
1113 }
1114
1115 /**
1116  * gtk_image_set_from_gicon:
1117  * @image: a #GtkImage
1118  * @icon: an icon
1119  * @size: an icon size
1120  *
1121  * See gtk_image_new_from_gicon() for details.
1122  * 
1123  * Since: 2.14
1124  **/
1125 void
1126 gtk_image_set_from_gicon  (GtkImage       *image,
1127                            GIcon          *icon,
1128                            GtkIconSize     size)
1129 {
1130   g_return_if_fail (GTK_IS_IMAGE (image));
1131
1132   g_object_freeze_notify (G_OBJECT (image));
1133
1134   /* in case icon == image->data.gicon.icon */
1135   if (icon)
1136     g_object_ref (icon);
1137   
1138   gtk_image_clear (image);
1139
1140   if (icon)
1141     {
1142       image->storage_type = GTK_IMAGE_GICON;
1143       
1144       image->data.gicon.icon = icon;
1145       image->icon_size = size;
1146
1147       /* Size is demand-computed in size request method
1148        * if we're a icon theme image, since changing the
1149        * style impacts the size request
1150        */
1151     }
1152
1153   g_object_notify (G_OBJECT (image), "gicon");
1154   g_object_notify (G_OBJECT (image), "icon-size");
1155   
1156   g_object_thaw_notify (G_OBJECT (image));
1157 }
1158
1159 /**
1160  * gtk_image_get_storage_type:
1161  * @image: a #GtkImage
1162  * 
1163  * Gets the type of representation being used by the #GtkImage
1164  * to store image data. If the #GtkImage has no image data,
1165  * the return value will be %GTK_IMAGE_EMPTY.
1166  * 
1167  * Return value: image representation being used
1168  **/
1169 GtkImageType
1170 gtk_image_get_storage_type (GtkImage *image)
1171 {
1172   g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);
1173
1174   return image->storage_type;
1175 }
1176
1177 /**
1178  * gtk_image_get_pixmap:
1179  * @image: a #GtkImage
1180  * @pixmap: (out) (transfer none) (allow-none): location to store the
1181  *     pixmap, or %NULL
1182  * @mask: (out) (transfer none) (allow-none): location to store the
1183  *     mask, or %NULL
1184  *
1185  * Gets the pixmap and mask being displayed by the #GtkImage.
1186  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1187  * %GTK_IMAGE_PIXMAP (see gtk_image_get_storage_type()).
1188  * The caller of this function does not own a reference to the
1189  * returned pixmap and mask.
1190  **/
1191 void
1192 gtk_image_get_pixmap (GtkImage   *image,
1193                       GdkPixmap **pixmap,
1194                       GdkBitmap **mask)
1195 {
1196   g_return_if_fail (GTK_IS_IMAGE (image)); 
1197   g_return_if_fail (image->storage_type == GTK_IMAGE_PIXMAP ||
1198                     image->storage_type == GTK_IMAGE_EMPTY);
1199   
1200   if (pixmap)
1201     *pixmap = image->data.pixmap.pixmap;
1202   
1203   if (mask)
1204     *mask = image->mask;
1205 }
1206
1207 /**
1208  * gtk_image_get_image:
1209  * @image: a #GtkImage
1210  * @gdk_image: (out) (transfer none) (allow-none): return location for
1211  *     a #GtkImage, or %NULL
1212  * @mask: (out) (transfer none) (allow-none): return location for a
1213  *     #GdkBitmap, or %NULL
1214  * 
1215  * Gets the #GdkImage and mask being displayed by the #GtkImage.
1216  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1217  * %GTK_IMAGE_IMAGE (see gtk_image_get_storage_type()).
1218  * The caller of this function does not own a reference to the
1219  * returned image and mask.
1220  **/
1221 void
1222 gtk_image_get_image  (GtkImage   *image,
1223                       GdkImage  **gdk_image,
1224                       GdkBitmap **mask)
1225 {
1226   g_return_if_fail (GTK_IS_IMAGE (image));
1227   g_return_if_fail (image->storage_type == GTK_IMAGE_IMAGE ||
1228                     image->storage_type == GTK_IMAGE_EMPTY);
1229
1230   if (gdk_image)
1231     *gdk_image = image->data.image.image;
1232   
1233   if (mask)
1234     *mask = image->mask;
1235 }
1236
1237 /**
1238  * gtk_image_get_pixbuf:
1239  * @image: a #GtkImage
1240  *
1241  * Gets the #GdkPixbuf being displayed by the #GtkImage.
1242  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1243  * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
1244  * The caller of this function does not own a reference to the
1245  * returned pixbuf.
1246  * 
1247  * Return value: (transfer none): the displayed pixbuf, or %NULL if
1248  * the image is empty
1249  **/
1250 GdkPixbuf*
1251 gtk_image_get_pixbuf (GtkImage *image)
1252 {
1253   g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1254   g_return_val_if_fail (image->storage_type == GTK_IMAGE_PIXBUF ||
1255                         image->storage_type == GTK_IMAGE_EMPTY, NULL);
1256
1257   if (image->storage_type == GTK_IMAGE_EMPTY)
1258     image->data.pixbuf.pixbuf = NULL;
1259   
1260   return image->data.pixbuf.pixbuf;
1261 }
1262
1263 /**
1264  * gtk_image_get_stock:
1265  * @image: a #GtkImage
1266  * @stock_id: (out) (transfer none) (allow-none): place to store a
1267  *     stock icon name, or %NULL
1268  * @size: (out) (allow-none): place to store a stock icon size, or %NULL
1269  *
1270  * Gets the stock icon name and size being displayed by the #GtkImage.
1271  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1272  * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1273  * The returned string is owned by the #GtkImage and should not
1274  * be freed.
1275  **/
1276 void
1277 gtk_image_get_stock  (GtkImage        *image,
1278                       gchar          **stock_id,
1279                       GtkIconSize     *size)
1280 {
1281   g_return_if_fail (GTK_IS_IMAGE (image));
1282   g_return_if_fail (image->storage_type == GTK_IMAGE_STOCK ||
1283                     image->storage_type == GTK_IMAGE_EMPTY);
1284
1285   if (image->storage_type == GTK_IMAGE_EMPTY)
1286     image->data.stock.stock_id = NULL;
1287   
1288   if (stock_id)
1289     *stock_id = image->data.stock.stock_id;
1290
1291   if (size)
1292     *size = image->icon_size;
1293 }
1294
1295 /**
1296  * gtk_image_get_icon_set:
1297  * @image: a #GtkImage
1298  * @icon_set: (out) (transfer none) (allow-none): location to store a
1299  *     #GtkIconSet, or %NULL
1300  * @size: (out) (allow-none): location to store a stock icon size, or %NULL
1301  *
1302  * Gets the icon set and size being displayed by the #GtkImage.
1303  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1304  * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1305  **/
1306 void
1307 gtk_image_get_icon_set  (GtkImage        *image,
1308                          GtkIconSet     **icon_set,
1309                          GtkIconSize     *size)
1310 {
1311   g_return_if_fail (GTK_IS_IMAGE (image));
1312   g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_SET ||
1313                     image->storage_type == GTK_IMAGE_EMPTY);
1314       
1315   if (icon_set)    
1316     *icon_set = image->data.icon_set.icon_set;
1317
1318   if (size)
1319     *size = image->icon_size;
1320 }
1321
1322 /**
1323  * gtk_image_get_animation:
1324  * @image: a #GtkImage
1325  *
1326  * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1327  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1328  * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1329  * The caller of this function does not own a reference to the
1330  * returned animation.
1331  * 
1332  * Return value: (transfer none): the displayed animation, or %NULL if
1333  * the image is empty
1334  **/
1335 GdkPixbufAnimation*
1336 gtk_image_get_animation (GtkImage *image)
1337 {
1338   g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1339   g_return_val_if_fail (image->storage_type == GTK_IMAGE_ANIMATION ||
1340                         image->storage_type == GTK_IMAGE_EMPTY,
1341                         NULL);
1342
1343   if (image->storage_type == GTK_IMAGE_EMPTY)
1344     image->data.anim.anim = NULL;
1345   
1346   return image->data.anim.anim;
1347 }
1348
1349 /**
1350  * gtk_image_get_icon_name:
1351  * @image: a #GtkImage
1352  * @icon_name: (out) (transfer none) (allow-none): place to store an
1353  *     icon name, or %NULL
1354  * @size: (out) (allow-none): place to store an icon size, or %NULL
1355  *
1356  * Gets the icon name and size being displayed by the #GtkImage.
1357  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1358  * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1359  * The returned string is owned by the #GtkImage and should not
1360  * be freed.
1361  * 
1362  * Since: 2.6
1363  **/
1364 void
1365 gtk_image_get_icon_name  (GtkImage              *image,
1366                           G_CONST_RETURN gchar **icon_name,
1367                           GtkIconSize           *size)
1368 {
1369   g_return_if_fail (GTK_IS_IMAGE (image));
1370   g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME ||
1371                     image->storage_type == GTK_IMAGE_EMPTY);
1372
1373   if (image->storage_type == GTK_IMAGE_EMPTY)
1374     image->data.name.icon_name = NULL;
1375   
1376   if (icon_name)
1377     *icon_name = image->data.name.icon_name;
1378
1379   if (size)
1380     *size = image->icon_size;
1381 }
1382
1383 /**
1384  * gtk_image_get_gicon:
1385  * @image: a #GtkImage
1386  * @gicon: (out) (transfer none) (allow-none): place to store a
1387  *     #GIcon, or %NULL
1388  * @size: (out) (allow-none): place to store an icon size, or %NULL
1389  *
1390  * Gets the #GIcon and size being displayed by the #GtkImage.
1391  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1392  * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1393  * The caller of this function does not own a reference to the
1394  * returned #GIcon.
1395  * 
1396  * Since: 2.14
1397  **/
1398 void
1399 gtk_image_get_gicon (GtkImage     *image,
1400                      GIcon       **gicon,
1401                      GtkIconSize  *size)
1402 {
1403   g_return_if_fail (GTK_IS_IMAGE (image));
1404   g_return_if_fail (image->storage_type == GTK_IMAGE_GICON ||
1405                     image->storage_type == GTK_IMAGE_EMPTY);
1406
1407   if (image->storage_type == GTK_IMAGE_EMPTY)
1408     image->data.gicon.icon = NULL;
1409   
1410   if (gicon)
1411     *gicon = image->data.gicon.icon;
1412
1413   if (size)
1414     *size = image->icon_size;
1415 }
1416
1417 /**
1418  * gtk_image_new:
1419  * 
1420  * Creates a new empty #GtkImage widget.
1421  * 
1422  * Return value: a newly created #GtkImage widget. 
1423  **/
1424 GtkWidget*
1425 gtk_image_new (void)
1426 {
1427   return g_object_new (GTK_TYPE_IMAGE, NULL);
1428 }
1429
1430 void
1431 gtk_image_set (GtkImage  *image,
1432                GdkImage  *val,
1433                GdkBitmap *mask)
1434 {
1435   g_return_if_fail (GTK_IS_IMAGE (image));
1436
1437   gtk_image_set_from_image (image, val, mask);
1438 }
1439
1440 void
1441 gtk_image_get (GtkImage   *image,
1442                GdkImage  **val,
1443                GdkBitmap **mask)
1444 {
1445   g_return_if_fail (GTK_IS_IMAGE (image));
1446
1447   gtk_image_get_image (image, val, mask);
1448 }
1449
1450 static void
1451 gtk_image_reset_anim_iter (GtkImage *image)
1452 {
1453   if (image->storage_type == GTK_IMAGE_ANIMATION)
1454     {
1455       /* Reset the animation */
1456       
1457       if (image->data.anim.frame_timeout)
1458         {
1459           g_source_remove (image->data.anim.frame_timeout);
1460           image->data.anim.frame_timeout = 0;
1461         }
1462
1463       if (image->data.anim.iter)
1464         {
1465           g_object_unref (image->data.anim.iter);
1466           image->data.anim.iter = NULL;
1467         }
1468     }
1469 }
1470
1471 static void
1472 gtk_image_unmap (GtkWidget *widget)
1473 {
1474   gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1475
1476   GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1477 }
1478
1479 static void
1480 gtk_image_unrealize (GtkWidget *widget)
1481 {
1482   gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1483
1484   GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1485 }
1486
1487 static gint
1488 animation_timeout (gpointer data)
1489 {
1490   GtkImage *image;
1491   int delay;
1492
1493   image = GTK_IMAGE (data);
1494   
1495   image->data.anim.frame_timeout = 0;
1496
1497   gdk_pixbuf_animation_iter_advance (image->data.anim.iter, NULL);
1498
1499   delay = gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter);
1500   if (delay >= 0)
1501     {
1502       image->data.anim.frame_timeout =
1503         gdk_threads_add_timeout (delay, animation_timeout, image);
1504
1505       gtk_widget_queue_draw (GTK_WIDGET (image));
1506
1507       if (GTK_WIDGET_DRAWABLE (image))
1508         gdk_window_process_updates (GTK_WIDGET (image)->window, TRUE);
1509     }
1510
1511   return FALSE;
1512 }
1513
1514 static void
1515 icon_theme_changed (GtkImage *image)
1516 {
1517   if (image->storage_type == GTK_IMAGE_ICON_NAME) 
1518     {
1519       if (image->data.name.pixbuf)
1520         g_object_unref (image->data.name.pixbuf);
1521       image->data.name.pixbuf = NULL;
1522
1523       gtk_widget_queue_draw (GTK_WIDGET (image));
1524     }
1525   if (image->storage_type == GTK_IMAGE_GICON) 
1526     {
1527       if (image->data.gicon.pixbuf)
1528         g_object_unref (image->data.gicon.pixbuf);
1529       image->data.gicon.pixbuf = NULL;
1530
1531       gtk_widget_queue_draw (GTK_WIDGET (image));
1532     }
1533 }
1534
1535 static void
1536 ensure_pixbuf_for_icon_name (GtkImage *image)
1537 {
1538   GtkImagePrivate *priv;
1539   GdkScreen *screen;
1540   GtkIconTheme *icon_theme;
1541   GtkSettings *settings;
1542   gint width, height;
1543   gint *sizes, *s, dist;
1544   GtkIconLookupFlags flags;
1545   GError *error = NULL;
1546
1547   g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME);
1548
1549   priv = GTK_IMAGE_GET_PRIVATE (image);
1550   screen = gtk_widget_get_screen (GTK_WIDGET (image));
1551   icon_theme = gtk_icon_theme_get_for_screen (screen);
1552   settings = gtk_settings_get_for_screen (screen);
1553   flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1554   if (image->data.name.pixbuf == NULL)
1555     {
1556       if (priv->pixel_size != -1)
1557         {
1558           width = height = priv->pixel_size;
1559           flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1560         }
1561       else if (!gtk_icon_size_lookup_for_settings (settings,
1562                                                    image->icon_size,
1563                                                    &width, &height))
1564         {
1565           if (image->icon_size == -1)
1566             {
1567               /* Find an available size close to 48 */
1568               sizes = gtk_icon_theme_get_icon_sizes (icon_theme, image->data.name.icon_name);
1569               dist = 100;
1570               width = height = 48;
1571               for (s = sizes; *s; s++)
1572                 {
1573                   if (*s == -1)
1574                     {
1575                       width = height = 48;
1576                       break;
1577                     }
1578                   if (*s < 48)
1579                     {
1580                       if (48 - *s < dist)
1581                         {
1582                           width = height = *s;
1583                           dist = 48 - *s;
1584                         }
1585                     }
1586                   else
1587                     {
1588                       if (*s - 48 < dist)
1589                         {
1590                           width = height = *s;
1591                           dist = *s - 48;
1592                         }
1593                     }
1594                 }
1595               g_free (sizes);
1596             }
1597           else
1598             {
1599               g_warning ("Invalid icon size %d\n", image->icon_size);
1600               width = height = 24;
1601             }
1602         }
1603       image->data.name.pixbuf =
1604         gtk_icon_theme_load_icon (icon_theme,
1605                                   image->data.name.icon_name,
1606                                   MIN (width, height), flags, &error);
1607       if (image->data.name.pixbuf == NULL)
1608         {
1609           g_error_free (error);
1610           image->data.name.pixbuf =
1611             gtk_widget_render_icon (GTK_WIDGET (image),
1612                                     GTK_STOCK_MISSING_IMAGE,
1613                                     image->icon_size,
1614                                     NULL);
1615         }
1616     }
1617 }
1618
1619 static void
1620 ensure_pixbuf_for_gicon (GtkImage *image)
1621 {
1622   GtkImagePrivate *priv;
1623   GdkScreen *screen;
1624   GtkIconTheme *icon_theme;
1625   GtkSettings *settings;
1626   gint width, height;
1627   GtkIconInfo *info;
1628   GtkIconLookupFlags flags;
1629
1630   g_return_if_fail (image->storage_type == GTK_IMAGE_GICON);
1631
1632   priv = GTK_IMAGE_GET_PRIVATE (image);
1633   screen = gtk_widget_get_screen (GTK_WIDGET (image));
1634   icon_theme = gtk_icon_theme_get_for_screen (screen);
1635   settings = gtk_settings_get_for_screen (screen);
1636   flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1637   if (image->data.gicon.pixbuf == NULL)
1638     {
1639       if (priv->pixel_size != -1)
1640         {
1641           width = height = priv->pixel_size;
1642           flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1643         }
1644       else if (!gtk_icon_size_lookup_for_settings (settings,
1645                                                    image->icon_size,
1646                                                    &width, &height))
1647         {
1648           if (image->icon_size == -1)
1649             width = height = 48;
1650           else
1651             {
1652               g_warning ("Invalid icon size %d\n", image->icon_size);
1653               width = height = 24;
1654             }
1655         }
1656
1657       info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1658                                              image->data.gicon.icon,
1659                                              MIN (width, height), flags);
1660       if (info)
1661         {
1662           image->data.gicon.pixbuf = gtk_icon_info_load_icon (info, NULL);
1663           gtk_icon_info_free (info);
1664         }
1665
1666       if (image->data.gicon.pixbuf == NULL)
1667         {
1668           image->data.gicon.pixbuf =
1669             gtk_widget_render_icon (GTK_WIDGET (image),
1670                                     GTK_STOCK_MISSING_IMAGE,
1671                                     image->icon_size,
1672                                     NULL);
1673         }
1674     }
1675 }
1676
1677
1678 /*
1679  * Like gdk_rectangle_intersect (dest, src, dest), but make 
1680  * sure that the origin of dest is moved by an "even" offset. 
1681  * If necessary grow the intersection by one row or column 
1682  * to achieve this.
1683  *
1684  * This is necessary since we can't pass alignment information
1685  * for the pixelation pattern down to gdk_pixbuf_saturate_and_pixelate(), 
1686  * thus we have to makesure that the subimages are properly aligned.
1687  */
1688 static gboolean
1689 rectangle_intersect_even (GdkRectangle *src, 
1690                           GdkRectangle *dest)
1691 {
1692   gboolean isect;
1693   gint x, y;
1694
1695   x = dest->x;
1696   y = dest->y;
1697   isect = gdk_rectangle_intersect (dest, src, dest);
1698
1699   if ((dest->x - x + dest->y - y) % 2 != 0)
1700     {
1701       if (dest->x > x)
1702         {
1703           dest->x--;
1704           dest->width++;
1705         }
1706       else
1707         {
1708           dest->y--;
1709           dest->height++;
1710         }
1711     }
1712   
1713   return isect;
1714 }
1715
1716 static gint
1717 gtk_image_expose (GtkWidget      *widget,
1718                   GdkEventExpose *event)
1719 {
1720   g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1721   g_return_val_if_fail (event != NULL, FALSE);
1722   
1723   if (GTK_WIDGET_MAPPED (widget) &&
1724       GTK_IMAGE (widget)->storage_type != GTK_IMAGE_EMPTY)
1725     {
1726       GtkImage *image;
1727       GtkMisc *misc;
1728       GtkImagePrivate *priv;
1729       GdkRectangle area, image_bound;
1730       gfloat xalign;
1731       gint x, y, mask_x, mask_y;
1732       GdkBitmap *mask;
1733       GdkPixbuf *pixbuf;
1734       gboolean needs_state_transform;
1735
1736       image = GTK_IMAGE (widget);
1737       misc = GTK_MISC (widget);
1738       priv = GTK_IMAGE_GET_PRIVATE (image);
1739
1740       area = event->area;
1741
1742       /* For stock items and icon sets, we lazily calculate
1743        * the size; we might get here between a queue_resize()
1744        * and size_request() if something explicitely forces
1745        * a redraw.
1746        */
1747       if (priv->need_calc_size)
1748         gtk_image_calc_size (image);
1749       
1750       if (!gdk_rectangle_intersect (&area, &widget->allocation, &area))
1751         return FALSE;
1752
1753       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1754         xalign = misc->xalign;
1755       else
1756         xalign = 1.0 - misc->xalign;
1757
1758       x = floor (widget->allocation.x + misc->xpad
1759                  + ((widget->allocation.width - widget->requisition.width) * xalign));
1760       y = floor (widget->allocation.y + misc->ypad 
1761                  + ((widget->allocation.height - widget->requisition.height) * misc->yalign));
1762       mask_x = x;
1763       mask_y = y;
1764       
1765       image_bound.x = x;
1766       image_bound.y = y;      
1767       image_bound.width = 0;
1768       image_bound.height = 0;      
1769
1770       mask = NULL;
1771       pixbuf = NULL;
1772       needs_state_transform = GTK_WIDGET_STATE (widget) != GTK_STATE_NORMAL;
1773       
1774       switch (image->storage_type)
1775         {
1776         case GTK_IMAGE_PIXMAP:
1777           mask = image->mask;
1778           gdk_drawable_get_size (image->data.pixmap.pixmap,
1779                                  &image_bound.width,
1780                                  &image_bound.height);
1781           if (rectangle_intersect_even (&area, &image_bound) &&
1782               needs_state_transform)
1783             {
1784               pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1785                                                      image->data.pixmap.pixmap,
1786                                                      gtk_widget_get_colormap (widget),
1787                                                      image_bound.x - x, image_bound.y - y,
1788                                                      0, 0,
1789                                                      image_bound.width,
1790                                                      image_bound.height);
1791
1792               x = image_bound.x;
1793               y = image_bound.y;
1794             }
1795           
1796           break;
1797
1798         case GTK_IMAGE_IMAGE:
1799           mask = image->mask;
1800           image_bound.width = image->data.image.image->width;
1801           image_bound.height = image->data.image.image->height;
1802
1803           if (rectangle_intersect_even (&area, &image_bound) &&
1804               needs_state_transform)
1805             {
1806               pixbuf = gdk_pixbuf_get_from_image (NULL,
1807                                                   image->data.image.image,
1808                                                   gtk_widget_get_colormap (widget),
1809                                                   image_bound.x - x, image_bound.y - y,
1810                                                   0, 0,
1811                                                   image_bound.width,
1812                                                   image_bound.height);
1813
1814               x = image_bound.x;
1815               y = image_bound.y;
1816             }
1817           break;
1818
1819         case GTK_IMAGE_PIXBUF:
1820           image_bound.width = gdk_pixbuf_get_width (image->data.pixbuf.pixbuf);
1821           image_bound.height = gdk_pixbuf_get_height (image->data.pixbuf.pixbuf);            
1822
1823           if (rectangle_intersect_even (&area, &image_bound) &&
1824               needs_state_transform)
1825             {
1826               pixbuf = gdk_pixbuf_new_subpixbuf (image->data.pixbuf.pixbuf,
1827                                                  image_bound.x - x, image_bound.y - y,
1828                                                  image_bound.width, image_bound.height);
1829
1830               x = image_bound.x;
1831               y = image_bound.y;
1832             }
1833           else
1834             {
1835               pixbuf = image->data.pixbuf.pixbuf;
1836               g_object_ref (pixbuf);
1837             }
1838           break;
1839
1840         case GTK_IMAGE_STOCK:
1841           pixbuf = gtk_widget_render_icon (widget,
1842                                            image->data.stock.stock_id,
1843                                            image->icon_size,
1844                                            NULL);
1845           if (pixbuf)
1846             {              
1847               image_bound.width = gdk_pixbuf_get_width (pixbuf);
1848               image_bound.height = gdk_pixbuf_get_height (pixbuf);
1849             }
1850
1851           /* already done */
1852           needs_state_transform = FALSE;
1853           break;
1854
1855         case GTK_IMAGE_ICON_SET:
1856           pixbuf =
1857             gtk_icon_set_render_icon (image->data.icon_set.icon_set,
1858                                       widget->style,
1859                                       gtk_widget_get_direction (widget),
1860                                       GTK_WIDGET_STATE (widget),
1861                                       image->icon_size,
1862                                       widget,
1863                                       NULL);
1864
1865           if (pixbuf)
1866             {
1867               image_bound.width = gdk_pixbuf_get_width (pixbuf);
1868               image_bound.height = gdk_pixbuf_get_height (pixbuf);
1869             }
1870
1871           /* already done */
1872           needs_state_transform = FALSE;
1873           break;
1874
1875         case GTK_IMAGE_ANIMATION:
1876           {
1877             if (image->data.anim.iter == NULL)
1878               {
1879                 image->data.anim.iter = gdk_pixbuf_animation_get_iter (image->data.anim.anim, NULL);
1880                 
1881                 if (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter) >= 0)
1882                   image->data.anim.frame_timeout =
1883                     gdk_threads_add_timeout (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter),
1884                                    animation_timeout,
1885                                    image);
1886               }
1887
1888             image_bound.width = gdk_pixbuf_animation_get_width (image->data.anim.anim);
1889             image_bound.height = gdk_pixbuf_animation_get_height (image->data.anim.anim);
1890                   
1891             /* don't advance the anim iter here, or we could get frame changes between two
1892              * exposes of different areas.
1893              */
1894             
1895             pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (image->data.anim.iter);
1896             g_object_ref (pixbuf);
1897           }
1898           break;
1899
1900         case GTK_IMAGE_ICON_NAME:
1901           ensure_pixbuf_for_icon_name (image);
1902           pixbuf = image->data.name.pixbuf;
1903           if (pixbuf)
1904             {
1905               g_object_ref (pixbuf);
1906               image_bound.width = gdk_pixbuf_get_width (pixbuf);
1907               image_bound.height = gdk_pixbuf_get_height (pixbuf);
1908             }
1909           break;
1910
1911         case GTK_IMAGE_GICON:
1912           ensure_pixbuf_for_gicon (image);
1913           pixbuf = image->data.gicon.pixbuf;
1914           if (pixbuf)
1915             {
1916               g_object_ref (pixbuf);
1917               image_bound.width = gdk_pixbuf_get_width (pixbuf);
1918               image_bound.height = gdk_pixbuf_get_height (pixbuf);
1919             }
1920           break;
1921           
1922         case GTK_IMAGE_EMPTY:
1923           g_assert_not_reached ();
1924           break;
1925         }
1926
1927       if (mask)
1928         {
1929           gdk_gc_set_clip_mask (widget->style->black_gc, mask);
1930           gdk_gc_set_clip_origin (widget->style->black_gc, mask_x, mask_y);
1931         }
1932
1933       if (rectangle_intersect_even (&area, &image_bound))
1934         {
1935           if (pixbuf)
1936             {
1937               if (needs_state_transform)
1938                 {
1939                   GtkIconSource *source;
1940                   GdkPixbuf *rendered;
1941
1942                   source = gtk_icon_source_new ();
1943                   gtk_icon_source_set_pixbuf (source, pixbuf);
1944                   /* The size here is arbitrary; since size isn't
1945                    * wildcarded in the souce, it isn't supposed to be
1946                    * scaled by the engine function
1947                    */
1948                   gtk_icon_source_set_size (source,
1949                                             GTK_ICON_SIZE_SMALL_TOOLBAR);
1950                   gtk_icon_source_set_size_wildcarded (source, FALSE);
1951                   
1952                   rendered = gtk_style_render_icon (widget->style,
1953                                                     source,
1954                                                     gtk_widget_get_direction (widget),
1955                                                     GTK_WIDGET_STATE (widget),
1956                                                     /* arbitrary */
1957                                                     (GtkIconSize)-1,
1958                                                     widget,
1959                                                     "gtk-image");
1960
1961                   gtk_icon_source_free (source);
1962
1963                   g_object_unref (pixbuf);
1964                   pixbuf = rendered;
1965                 }
1966
1967               if (pixbuf)
1968                 {
1969                   gdk_draw_pixbuf (widget->window,
1970                                    widget->style->black_gc,
1971                                    pixbuf,
1972                                    image_bound.x - x,
1973                                    image_bound.y - y,
1974                                    image_bound.x,
1975                                    image_bound.y,
1976                                    image_bound.width,
1977                                    image_bound.height,
1978                                    GDK_RGB_DITHER_NORMAL,
1979                                    0, 0);
1980                 }
1981             }
1982           else
1983             {
1984               switch (image->storage_type)
1985                 {
1986                 case GTK_IMAGE_PIXMAP:
1987                   gdk_draw_drawable (widget->window,
1988                                      widget->style->black_gc,
1989                                      image->data.pixmap.pixmap,
1990                                      image_bound.x - x, image_bound.y - y,
1991                                      image_bound.x, image_bound.y,
1992                                      image_bound.width, image_bound.height);
1993                   break;
1994               
1995                 case GTK_IMAGE_IMAGE:
1996                   gdk_draw_image (widget->window,
1997                                   widget->style->black_gc,
1998                                   image->data.image.image,
1999                                   image_bound.x - x, image_bound.y - y,
2000                                   image_bound.x, image_bound.y,
2001                                   image_bound.width, image_bound.height);
2002                   break;
2003
2004                 case GTK_IMAGE_PIXBUF:
2005                 case GTK_IMAGE_STOCK:
2006                 case GTK_IMAGE_ICON_SET:
2007                 case GTK_IMAGE_ANIMATION:
2008                 case GTK_IMAGE_ICON_NAME:
2009                 case GTK_IMAGE_EMPTY:
2010                 case GTK_IMAGE_GICON:
2011                   g_assert_not_reached ();
2012                   break;
2013                 }
2014             }
2015         } /* if rectangle intersects */      
2016
2017       if (mask)
2018         {
2019           gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
2020           gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
2021         }
2022       
2023       if (pixbuf)
2024         g_object_unref (pixbuf);
2025
2026     } /* if widget is drawable */
2027
2028   return FALSE;
2029 }
2030
2031 static void
2032 gtk_image_reset (GtkImage *image)
2033 {
2034   GtkImagePrivate *priv;
2035
2036   priv = GTK_IMAGE_GET_PRIVATE (image);
2037
2038   g_object_freeze_notify (G_OBJECT (image));
2039   
2040   if (image->storage_type != GTK_IMAGE_EMPTY)
2041     g_object_notify (G_OBJECT (image), "storage-type");
2042
2043   if (image->mask)
2044     {
2045       g_object_unref (image->mask);
2046       image->mask = NULL;
2047       g_object_notify (G_OBJECT (image), "mask");
2048     }
2049
2050   if (image->icon_size != DEFAULT_ICON_SIZE)
2051     {
2052       image->icon_size = DEFAULT_ICON_SIZE;
2053       g_object_notify (G_OBJECT (image), "icon-size");
2054     }
2055   
2056   switch (image->storage_type)
2057     {
2058     case GTK_IMAGE_PIXMAP:
2059
2060       if (image->data.pixmap.pixmap)
2061         g_object_unref (image->data.pixmap.pixmap);
2062       image->data.pixmap.pixmap = NULL;
2063       
2064       g_object_notify (G_OBJECT (image), "pixmap");
2065       
2066       break;
2067
2068     case GTK_IMAGE_IMAGE:
2069
2070       if (image->data.image.image)
2071         g_object_unref (image->data.image.image);
2072       image->data.image.image = NULL;
2073       
2074       g_object_notify (G_OBJECT (image), "image");
2075       
2076       break;
2077
2078     case GTK_IMAGE_PIXBUF:
2079
2080       if (image->data.pixbuf.pixbuf)
2081         g_object_unref (image->data.pixbuf.pixbuf);
2082
2083       g_object_notify (G_OBJECT (image), "pixbuf");
2084       
2085       break;
2086
2087     case GTK_IMAGE_STOCK:
2088
2089       g_free (image->data.stock.stock_id);
2090
2091       image->data.stock.stock_id = NULL;
2092       
2093       g_object_notify (G_OBJECT (image), "stock");      
2094       break;
2095
2096     case GTK_IMAGE_ICON_SET:
2097       if (image->data.icon_set.icon_set)
2098         gtk_icon_set_unref (image->data.icon_set.icon_set);
2099       image->data.icon_set.icon_set = NULL;
2100       
2101       g_object_notify (G_OBJECT (image), "icon-set");      
2102       break;
2103
2104     case GTK_IMAGE_ANIMATION:
2105       gtk_image_reset_anim_iter (image);
2106       
2107       if (image->data.anim.anim)
2108         g_object_unref (image->data.anim.anim);
2109       image->data.anim.anim = NULL;
2110       
2111       g_object_notify (G_OBJECT (image), "pixbuf-animation");
2112       
2113       break;
2114
2115     case GTK_IMAGE_ICON_NAME:
2116       g_free (image->data.name.icon_name);
2117       image->data.name.icon_name = NULL;
2118       if (image->data.name.pixbuf)
2119         g_object_unref (image->data.name.pixbuf);
2120       image->data.name.pixbuf = NULL;
2121
2122       g_object_notify (G_OBJECT (image), "icon-name");
2123
2124       break;
2125       
2126     case GTK_IMAGE_GICON:
2127       if (image->data.gicon.icon)
2128         g_object_unref (image->data.gicon.icon);
2129       image->data.gicon.icon = NULL;
2130       if (image->data.gicon.pixbuf)
2131         g_object_unref (image->data.gicon.pixbuf);
2132       image->data.gicon.pixbuf = NULL;
2133
2134       g_object_notify (G_OBJECT (image), "gicon");
2135
2136       break;
2137       
2138     case GTK_IMAGE_EMPTY:
2139     default:
2140       break;
2141       
2142     }
2143
2144   if (priv->filename)
2145     {
2146       g_free (priv->filename);
2147       priv->filename = NULL;
2148       g_object_notify (G_OBJECT (image), "file");
2149     }
2150
2151   image->storage_type = GTK_IMAGE_EMPTY;
2152
2153   memset (&image->data, '\0', sizeof (image->data));
2154
2155   g_object_thaw_notify (G_OBJECT (image));
2156 }
2157
2158 /**
2159  * gtk_image_clear:
2160  * @image: a #GtkImage
2161  *
2162  * Resets the image to be empty.
2163  *
2164  * Since: 2.8
2165  */
2166 void
2167 gtk_image_clear (GtkImage *image)
2168 {
2169   GtkImagePrivate *priv;
2170
2171   priv = GTK_IMAGE_GET_PRIVATE (image);
2172
2173   priv->need_calc_size = 1;
2174
2175   gtk_image_reset (image);
2176   gtk_image_update_size (image, 0, 0);
2177 }
2178
2179 static void
2180 gtk_image_calc_size (GtkImage *image)
2181 {
2182   GtkWidget *widget = GTK_WIDGET (image);
2183   GdkPixbuf *pixbuf = NULL;
2184   GtkImagePrivate *priv;
2185
2186   priv = GTK_IMAGE_GET_PRIVATE (image);
2187
2188   priv->need_calc_size = 0;
2189
2190   /* We update stock/icon set on every size request, because
2191    * the theme could have affected the size; for other kinds of
2192    * image, we just update the requisition when the image data
2193    * is set.
2194    */
2195   switch (image->storage_type)
2196     {
2197     case GTK_IMAGE_STOCK:
2198       pixbuf = gtk_widget_render_icon (widget,
2199                                        image->data.stock.stock_id,
2200                                        image->icon_size,
2201                                        NULL);
2202       break;
2203       
2204     case GTK_IMAGE_ICON_SET:
2205       pixbuf = gtk_icon_set_render_icon (image->data.icon_set.icon_set,
2206                                          widget->style,
2207                                          gtk_widget_get_direction (widget),
2208                                          GTK_WIDGET_STATE (widget),
2209                                          image->icon_size,
2210                                          widget,
2211                                          NULL);
2212       break;
2213     case GTK_IMAGE_ICON_NAME:
2214       ensure_pixbuf_for_icon_name (image);
2215       pixbuf = image->data.name.pixbuf;
2216       if (pixbuf) g_object_ref (pixbuf);
2217       break;
2218     case GTK_IMAGE_GICON:
2219       ensure_pixbuf_for_gicon (image);
2220       pixbuf = image->data.gicon.pixbuf;
2221       if (pixbuf)
2222         g_object_ref (pixbuf);
2223       break;
2224     default:
2225       break;
2226     }
2227
2228   if (pixbuf)
2229     {
2230       widget->requisition.width = gdk_pixbuf_get_width (pixbuf) + GTK_MISC (image)->xpad * 2;
2231       widget->requisition.height = gdk_pixbuf_get_height (pixbuf) + GTK_MISC (image)->ypad * 2;
2232
2233       g_object_unref (pixbuf);
2234     }
2235 }
2236
2237 static void
2238 gtk_image_size_request (GtkWidget      *widget,
2239                         GtkRequisition *requisition)
2240 {
2241   GtkImage *image;
2242   
2243   image = GTK_IMAGE (widget);
2244
2245   gtk_image_calc_size (image);
2246
2247   /* Chain up to default that simply reads current requisition */
2248   GTK_WIDGET_CLASS (gtk_image_parent_class)->size_request (widget, requisition);
2249 }
2250
2251 static void
2252 gtk_image_style_set (GtkWidget      *widget,
2253                      GtkStyle       *prev_style)
2254 {
2255   GtkImage *image;
2256
2257   image = GTK_IMAGE (widget);
2258
2259   GTK_WIDGET_CLASS (gtk_image_parent_class)->style_set (widget, prev_style);
2260
2261   icon_theme_changed (image);
2262 }
2263
2264 static void
2265 gtk_image_screen_changed (GtkWidget *widget,
2266                           GdkScreen *prev_screen)
2267 {
2268   GtkImage *image;
2269
2270   image = GTK_IMAGE (widget);
2271
2272   if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
2273     GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
2274
2275   icon_theme_changed (image);
2276 }
2277
2278
2279 static void
2280 gtk_image_update_size (GtkImage *image,
2281                        gint      image_width,
2282                        gint      image_height)
2283 {
2284   GTK_WIDGET (image)->requisition.width = image_width + GTK_MISC (image)->xpad * 2;
2285   GTK_WIDGET (image)->requisition.height = image_height + GTK_MISC (image)->ypad * 2;
2286
2287   if (GTK_WIDGET_VISIBLE (image))
2288     gtk_widget_queue_resize (GTK_WIDGET (image));
2289 }
2290
2291
2292 /**
2293  * gtk_image_set_pixel_size:
2294  * @image: a #GtkImage
2295  * @pixel_size: the new pixel size
2296  * 
2297  * Sets the pixel size to use for named icons. If the pixel size is set
2298  * to a value != -1, it is used instead of the icon size set by
2299  * gtk_image_set_from_icon_name().
2300  *
2301  * Since: 2.6
2302  */
2303 void 
2304 gtk_image_set_pixel_size (GtkImage *image,
2305                           gint      pixel_size)
2306 {
2307   GtkImagePrivate *priv;
2308
2309   g_return_if_fail (GTK_IS_IMAGE (image));
2310   
2311   priv = GTK_IMAGE_GET_PRIVATE (image);
2312
2313   if (priv->pixel_size != pixel_size)
2314     {
2315       priv->pixel_size = pixel_size;
2316       
2317       if (image->storage_type == GTK_IMAGE_ICON_NAME)
2318         {
2319           if (image->data.name.pixbuf)
2320             {
2321               g_object_unref (image->data.name.pixbuf);
2322               image->data.name.pixbuf = NULL;
2323             }
2324           
2325           gtk_image_update_size (image, pixel_size, pixel_size);
2326         }
2327       
2328       if (image->storage_type == GTK_IMAGE_GICON)
2329         {
2330           if (image->data.gicon.pixbuf)
2331             {
2332               g_object_unref (image->data.gicon.pixbuf);
2333               image->data.gicon.pixbuf = NULL;
2334             }
2335           
2336           gtk_image_update_size (image, pixel_size, pixel_size);
2337         }
2338       
2339       g_object_notify (G_OBJECT (image), "pixel-size");
2340     }
2341 }
2342
2343 /**
2344  * gtk_image_get_pixel_size:
2345  * @image: a #GtkImage
2346  * 
2347  * Gets the pixel size used for named icons.
2348  *
2349  * Returns: the pixel size used for named icons.
2350  *
2351  * Since: 2.6
2352  */
2353 gint
2354 gtk_image_get_pixel_size (GtkImage *image)
2355 {
2356   GtkImagePrivate *priv;
2357
2358   g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
2359   
2360   priv = GTK_IMAGE_GET_PRIVATE (image);
2361
2362   return priv->pixel_size;
2363 }
2364
2365 #if defined (G_OS_WIN32) && !defined (_WIN64)
2366
2367 #undef gtk_image_new_from_file
2368
2369 GtkWidget*
2370 gtk_image_new_from_file   (const gchar *filename)
2371 {
2372   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2373   GtkWidget *retval;
2374
2375   retval = gtk_image_new_from_file_utf8 (utf8_filename);
2376
2377   g_free (utf8_filename);
2378
2379   return retval;
2380 }
2381
2382 #undef gtk_image_set_from_file
2383
2384 void
2385 gtk_image_set_from_file   (GtkImage    *image,
2386                            const gchar *filename)
2387 {
2388   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2389
2390   gtk_image_set_from_file_utf8 (image, utf8_filename);
2391
2392   g_free (utf8_filename);
2393 }
2394
2395 #endif
2396
2397 #define __GTK_IMAGE_C__
2398 #include "gtkaliasdef.c"