]> Pileus Git - ~andy/gtk/blob - gtk/gtkimage.c
Change GtkIconSize to int in params/return values
[~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: (type int): 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: (type int): 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: (type int): 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: (type int): 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: (type int): 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: (type int): 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: (type int): 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: (type int): 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) (type int): place to store a stock icon
1269  *     size, or %NULL
1270  *
1271  * Gets the stock icon name and size being displayed by the #GtkImage.
1272  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1273  * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1274  * The returned string is owned by the #GtkImage and should not
1275  * be freed.
1276  **/
1277 void
1278 gtk_image_get_stock  (GtkImage        *image,
1279                       gchar          **stock_id,
1280                       GtkIconSize     *size)
1281 {
1282   g_return_if_fail (GTK_IS_IMAGE (image));
1283   g_return_if_fail (image->storage_type == GTK_IMAGE_STOCK ||
1284                     image->storage_type == GTK_IMAGE_EMPTY);
1285
1286   if (image->storage_type == GTK_IMAGE_EMPTY)
1287     image->data.stock.stock_id = NULL;
1288   
1289   if (stock_id)
1290     *stock_id = image->data.stock.stock_id;
1291
1292   if (size)
1293     *size = image->icon_size;
1294 }
1295
1296 /**
1297  * gtk_image_get_icon_set:
1298  * @image: a #GtkImage
1299  * @icon_set: (out) (transfer none) (allow-none): location to store a
1300  *     #GtkIconSet, or %NULL
1301  * @size: (out) (allow-none) (type int): location to store a stock
1302  *     icon size, or %NULL
1303  *
1304  * Gets the icon set and size being displayed by the #GtkImage.
1305  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1306  * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
1307  **/
1308 void
1309 gtk_image_get_icon_set  (GtkImage        *image,
1310                          GtkIconSet     **icon_set,
1311                          GtkIconSize     *size)
1312 {
1313   g_return_if_fail (GTK_IS_IMAGE (image));
1314   g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_SET ||
1315                     image->storage_type == GTK_IMAGE_EMPTY);
1316       
1317   if (icon_set)    
1318     *icon_set = image->data.icon_set.icon_set;
1319
1320   if (size)
1321     *size = image->icon_size;
1322 }
1323
1324 /**
1325  * gtk_image_get_animation:
1326  * @image: a #GtkImage
1327  *
1328  * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
1329  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1330  * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
1331  * The caller of this function does not own a reference to the
1332  * returned animation.
1333  * 
1334  * Return value: (transfer none): the displayed animation, or %NULL if
1335  * the image is empty
1336  **/
1337 GdkPixbufAnimation*
1338 gtk_image_get_animation (GtkImage *image)
1339 {
1340   g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1341   g_return_val_if_fail (image->storage_type == GTK_IMAGE_ANIMATION ||
1342                         image->storage_type == GTK_IMAGE_EMPTY,
1343                         NULL);
1344
1345   if (image->storage_type == GTK_IMAGE_EMPTY)
1346     image->data.anim.anim = NULL;
1347   
1348   return image->data.anim.anim;
1349 }
1350
1351 /**
1352  * gtk_image_get_icon_name:
1353  * @image: a #GtkImage
1354  * @icon_name: (out) (transfer none) (allow-none): place to store an
1355  *     icon name, or %NULL
1356  * @size: (out) (allow-none) (type int): place to store an icon size,
1357  *     or %NULL
1358  *
1359  * Gets the icon name and size being displayed by the #GtkImage.
1360  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1361  * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
1362  * The returned string is owned by the #GtkImage and should not
1363  * be freed.
1364  * 
1365  * Since: 2.6
1366  **/
1367 void
1368 gtk_image_get_icon_name  (GtkImage              *image,
1369                           G_CONST_RETURN gchar **icon_name,
1370                           GtkIconSize           *size)
1371 {
1372   g_return_if_fail (GTK_IS_IMAGE (image));
1373   g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME ||
1374                     image->storage_type == GTK_IMAGE_EMPTY);
1375
1376   if (image->storage_type == GTK_IMAGE_EMPTY)
1377     image->data.name.icon_name = NULL;
1378   
1379   if (icon_name)
1380     *icon_name = image->data.name.icon_name;
1381
1382   if (size)
1383     *size = image->icon_size;
1384 }
1385
1386 /**
1387  * gtk_image_get_gicon:
1388  * @image: a #GtkImage
1389  * @gicon: (out) (transfer none) (allow-none): place to store a
1390  *     #GIcon, or %NULL
1391  * @size: (out) (allow-none) (type int): place to store an icon size,
1392  *     or %NULL
1393  *
1394  * Gets the #GIcon and size being displayed by the #GtkImage.
1395  * The storage type of the image must be %GTK_IMAGE_EMPTY or
1396  * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
1397  * The caller of this function does not own a reference to the
1398  * returned #GIcon.
1399  * 
1400  * Since: 2.14
1401  **/
1402 void
1403 gtk_image_get_gicon (GtkImage     *image,
1404                      GIcon       **gicon,
1405                      GtkIconSize  *size)
1406 {
1407   g_return_if_fail (GTK_IS_IMAGE (image));
1408   g_return_if_fail (image->storage_type == GTK_IMAGE_GICON ||
1409                     image->storage_type == GTK_IMAGE_EMPTY);
1410
1411   if (image->storage_type == GTK_IMAGE_EMPTY)
1412     image->data.gicon.icon = NULL;
1413   
1414   if (gicon)
1415     *gicon = image->data.gicon.icon;
1416
1417   if (size)
1418     *size = image->icon_size;
1419 }
1420
1421 /**
1422  * gtk_image_new:
1423  * 
1424  * Creates a new empty #GtkImage widget.
1425  * 
1426  * Return value: a newly created #GtkImage widget. 
1427  **/
1428 GtkWidget*
1429 gtk_image_new (void)
1430 {
1431   return g_object_new (GTK_TYPE_IMAGE, NULL);
1432 }
1433
1434 void
1435 gtk_image_set (GtkImage  *image,
1436                GdkImage  *val,
1437                GdkBitmap *mask)
1438 {
1439   g_return_if_fail (GTK_IS_IMAGE (image));
1440
1441   gtk_image_set_from_image (image, val, mask);
1442 }
1443
1444 void
1445 gtk_image_get (GtkImage   *image,
1446                GdkImage  **val,
1447                GdkBitmap **mask)
1448 {
1449   g_return_if_fail (GTK_IS_IMAGE (image));
1450
1451   gtk_image_get_image (image, val, mask);
1452 }
1453
1454 static void
1455 gtk_image_reset_anim_iter (GtkImage *image)
1456 {
1457   if (image->storage_type == GTK_IMAGE_ANIMATION)
1458     {
1459       /* Reset the animation */
1460       
1461       if (image->data.anim.frame_timeout)
1462         {
1463           g_source_remove (image->data.anim.frame_timeout);
1464           image->data.anim.frame_timeout = 0;
1465         }
1466
1467       if (image->data.anim.iter)
1468         {
1469           g_object_unref (image->data.anim.iter);
1470           image->data.anim.iter = NULL;
1471         }
1472     }
1473 }
1474
1475 static void
1476 gtk_image_unmap (GtkWidget *widget)
1477 {
1478   gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1479
1480   GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
1481 }
1482
1483 static void
1484 gtk_image_unrealize (GtkWidget *widget)
1485 {
1486   gtk_image_reset_anim_iter (GTK_IMAGE (widget));
1487
1488   GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1489 }
1490
1491 static gint
1492 animation_timeout (gpointer data)
1493 {
1494   GtkImage *image;
1495   int delay;
1496
1497   image = GTK_IMAGE (data);
1498   
1499   image->data.anim.frame_timeout = 0;
1500
1501   gdk_pixbuf_animation_iter_advance (image->data.anim.iter, NULL);
1502
1503   delay = gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter);
1504   if (delay >= 0)
1505     {
1506       image->data.anim.frame_timeout =
1507         gdk_threads_add_timeout (delay, animation_timeout, image);
1508
1509       gtk_widget_queue_draw (GTK_WIDGET (image));
1510
1511       if (GTK_WIDGET_DRAWABLE (image))
1512         gdk_window_process_updates (GTK_WIDGET (image)->window, TRUE);
1513     }
1514
1515   return FALSE;
1516 }
1517
1518 static void
1519 icon_theme_changed (GtkImage *image)
1520 {
1521   if (image->storage_type == GTK_IMAGE_ICON_NAME) 
1522     {
1523       if (image->data.name.pixbuf)
1524         g_object_unref (image->data.name.pixbuf);
1525       image->data.name.pixbuf = NULL;
1526
1527       gtk_widget_queue_draw (GTK_WIDGET (image));
1528     }
1529   if (image->storage_type == GTK_IMAGE_GICON) 
1530     {
1531       if (image->data.gicon.pixbuf)
1532         g_object_unref (image->data.gicon.pixbuf);
1533       image->data.gicon.pixbuf = NULL;
1534
1535       gtk_widget_queue_draw (GTK_WIDGET (image));
1536     }
1537 }
1538
1539 static void
1540 ensure_pixbuf_for_icon_name (GtkImage *image)
1541 {
1542   GtkImagePrivate *priv;
1543   GdkScreen *screen;
1544   GtkIconTheme *icon_theme;
1545   GtkSettings *settings;
1546   gint width, height;
1547   gint *sizes, *s, dist;
1548   GtkIconLookupFlags flags;
1549   GError *error = NULL;
1550
1551   g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME);
1552
1553   priv = GTK_IMAGE_GET_PRIVATE (image);
1554   screen = gtk_widget_get_screen (GTK_WIDGET (image));
1555   icon_theme = gtk_icon_theme_get_for_screen (screen);
1556   settings = gtk_settings_get_for_screen (screen);
1557   flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1558   if (image->data.name.pixbuf == NULL)
1559     {
1560       if (priv->pixel_size != -1)
1561         {
1562           width = height = priv->pixel_size;
1563           flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1564         }
1565       else if (!gtk_icon_size_lookup_for_settings (settings,
1566                                                    image->icon_size,
1567                                                    &width, &height))
1568         {
1569           if (image->icon_size == -1)
1570             {
1571               /* Find an available size close to 48 */
1572               sizes = gtk_icon_theme_get_icon_sizes (icon_theme, image->data.name.icon_name);
1573               dist = 100;
1574               width = height = 48;
1575               for (s = sizes; *s; s++)
1576                 {
1577                   if (*s == -1)
1578                     {
1579                       width = height = 48;
1580                       break;
1581                     }
1582                   if (*s < 48)
1583                     {
1584                       if (48 - *s < dist)
1585                         {
1586                           width = height = *s;
1587                           dist = 48 - *s;
1588                         }
1589                     }
1590                   else
1591                     {
1592                       if (*s - 48 < dist)
1593                         {
1594                           width = height = *s;
1595                           dist = *s - 48;
1596                         }
1597                     }
1598                 }
1599               g_free (sizes);
1600             }
1601           else
1602             {
1603               g_warning ("Invalid icon size %d\n", image->icon_size);
1604               width = height = 24;
1605             }
1606         }
1607       image->data.name.pixbuf =
1608         gtk_icon_theme_load_icon (icon_theme,
1609                                   image->data.name.icon_name,
1610                                   MIN (width, height), flags, &error);
1611       if (image->data.name.pixbuf == NULL)
1612         {
1613           g_error_free (error);
1614           image->data.name.pixbuf =
1615             gtk_widget_render_icon (GTK_WIDGET (image),
1616                                     GTK_STOCK_MISSING_IMAGE,
1617                                     image->icon_size,
1618                                     NULL);
1619         }
1620     }
1621 }
1622
1623 static void
1624 ensure_pixbuf_for_gicon (GtkImage *image)
1625 {
1626   GtkImagePrivate *priv;
1627   GdkScreen *screen;
1628   GtkIconTheme *icon_theme;
1629   GtkSettings *settings;
1630   gint width, height;
1631   GtkIconInfo *info;
1632   GtkIconLookupFlags flags;
1633
1634   g_return_if_fail (image->storage_type == GTK_IMAGE_GICON);
1635
1636   priv = GTK_IMAGE_GET_PRIVATE (image);
1637   screen = gtk_widget_get_screen (GTK_WIDGET (image));
1638   icon_theme = gtk_icon_theme_get_for_screen (screen);
1639   settings = gtk_settings_get_for_screen (screen);
1640   flags = GTK_ICON_LOOKUP_USE_BUILTIN;
1641   if (image->data.gicon.pixbuf == NULL)
1642     {
1643       if (priv->pixel_size != -1)
1644         {
1645           width = height = priv->pixel_size;
1646           flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
1647         }
1648       else if (!gtk_icon_size_lookup_for_settings (settings,
1649                                                    image->icon_size,
1650                                                    &width, &height))
1651         {
1652           if (image->icon_size == -1)
1653             width = height = 48;
1654           else
1655             {
1656               g_warning ("Invalid icon size %d\n", image->icon_size);
1657               width = height = 24;
1658             }
1659         }
1660
1661       info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1662                                              image->data.gicon.icon,
1663                                              MIN (width, height), flags);
1664       if (info)
1665         {
1666           image->data.gicon.pixbuf = gtk_icon_info_load_icon (info, NULL);
1667           gtk_icon_info_free (info);
1668         }
1669
1670       if (image->data.gicon.pixbuf == NULL)
1671         {
1672           image->data.gicon.pixbuf =
1673             gtk_widget_render_icon (GTK_WIDGET (image),
1674                                     GTK_STOCK_MISSING_IMAGE,
1675                                     image->icon_size,
1676                                     NULL);
1677         }
1678     }
1679 }
1680
1681
1682 /*
1683  * Like gdk_rectangle_intersect (dest, src, dest), but make 
1684  * sure that the origin of dest is moved by an "even" offset. 
1685  * If necessary grow the intersection by one row or column 
1686  * to achieve this.
1687  *
1688  * This is necessary since we can't pass alignment information
1689  * for the pixelation pattern down to gdk_pixbuf_saturate_and_pixelate(), 
1690  * thus we have to makesure that the subimages are properly aligned.
1691  */
1692 static gboolean
1693 rectangle_intersect_even (GdkRectangle *src, 
1694                           GdkRectangle *dest)
1695 {
1696   gboolean isect;
1697   gint x, y;
1698
1699   x = dest->x;
1700   y = dest->y;
1701   isect = gdk_rectangle_intersect (dest, src, dest);
1702
1703   if ((dest->x - x + dest->y - y) % 2 != 0)
1704     {
1705       if (dest->x > x)
1706         {
1707           dest->x--;
1708           dest->width++;
1709         }
1710       else
1711         {
1712           dest->y--;
1713           dest->height++;
1714         }
1715     }
1716   
1717   return isect;
1718 }
1719
1720 static gint
1721 gtk_image_expose (GtkWidget      *widget,
1722                   GdkEventExpose *event)
1723 {
1724   g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
1725   g_return_val_if_fail (event != NULL, FALSE);
1726   
1727   if (GTK_WIDGET_MAPPED (widget) &&
1728       GTK_IMAGE (widget)->storage_type != GTK_IMAGE_EMPTY)
1729     {
1730       GtkImage *image;
1731       GtkMisc *misc;
1732       GtkImagePrivate *priv;
1733       GdkRectangle area, image_bound;
1734       gfloat xalign;
1735       gint x, y, mask_x, mask_y;
1736       GdkBitmap *mask;
1737       GdkPixbuf *pixbuf;
1738       gboolean needs_state_transform;
1739
1740       image = GTK_IMAGE (widget);
1741       misc = GTK_MISC (widget);
1742       priv = GTK_IMAGE_GET_PRIVATE (image);
1743
1744       area = event->area;
1745
1746       /* For stock items and icon sets, we lazily calculate
1747        * the size; we might get here between a queue_resize()
1748        * and size_request() if something explicitely forces
1749        * a redraw.
1750        */
1751       if (priv->need_calc_size)
1752         gtk_image_calc_size (image);
1753       
1754       if (!gdk_rectangle_intersect (&area, &widget->allocation, &area))
1755         return FALSE;
1756
1757       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1758         xalign = misc->xalign;
1759       else
1760         xalign = 1.0 - misc->xalign;
1761
1762       x = floor (widget->allocation.x + misc->xpad
1763                  + ((widget->allocation.width - widget->requisition.width) * xalign));
1764       y = floor (widget->allocation.y + misc->ypad 
1765                  + ((widget->allocation.height - widget->requisition.height) * misc->yalign));
1766       mask_x = x;
1767       mask_y = y;
1768       
1769       image_bound.x = x;
1770       image_bound.y = y;      
1771       image_bound.width = 0;
1772       image_bound.height = 0;      
1773
1774       mask = NULL;
1775       pixbuf = NULL;
1776       needs_state_transform = GTK_WIDGET_STATE (widget) != GTK_STATE_NORMAL;
1777       
1778       switch (image->storage_type)
1779         {
1780         case GTK_IMAGE_PIXMAP:
1781           mask = image->mask;
1782           gdk_drawable_get_size (image->data.pixmap.pixmap,
1783                                  &image_bound.width,
1784                                  &image_bound.height);
1785           if (rectangle_intersect_even (&area, &image_bound) &&
1786               needs_state_transform)
1787             {
1788               pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1789                                                      image->data.pixmap.pixmap,
1790                                                      gtk_widget_get_colormap (widget),
1791                                                      image_bound.x - x, image_bound.y - y,
1792                                                      0, 0,
1793                                                      image_bound.width,
1794                                                      image_bound.height);
1795
1796               x = image_bound.x;
1797               y = image_bound.y;
1798             }
1799           
1800           break;
1801
1802         case GTK_IMAGE_IMAGE:
1803           mask = image->mask;
1804           image_bound.width = image->data.image.image->width;
1805           image_bound.height = image->data.image.image->height;
1806
1807           if (rectangle_intersect_even (&area, &image_bound) &&
1808               needs_state_transform)
1809             {
1810               pixbuf = gdk_pixbuf_get_from_image (NULL,
1811                                                   image->data.image.image,
1812                                                   gtk_widget_get_colormap (widget),
1813                                                   image_bound.x - x, image_bound.y - y,
1814                                                   0, 0,
1815                                                   image_bound.width,
1816                                                   image_bound.height);
1817
1818               x = image_bound.x;
1819               y = image_bound.y;
1820             }
1821           break;
1822
1823         case GTK_IMAGE_PIXBUF:
1824           image_bound.width = gdk_pixbuf_get_width (image->data.pixbuf.pixbuf);
1825           image_bound.height = gdk_pixbuf_get_height (image->data.pixbuf.pixbuf);            
1826
1827           if (rectangle_intersect_even (&area, &image_bound) &&
1828               needs_state_transform)
1829             {
1830               pixbuf = gdk_pixbuf_new_subpixbuf (image->data.pixbuf.pixbuf,
1831                                                  image_bound.x - x, image_bound.y - y,
1832                                                  image_bound.width, image_bound.height);
1833
1834               x = image_bound.x;
1835               y = image_bound.y;
1836             }
1837           else
1838             {
1839               pixbuf = image->data.pixbuf.pixbuf;
1840               g_object_ref (pixbuf);
1841             }
1842           break;
1843
1844         case GTK_IMAGE_STOCK:
1845           pixbuf = gtk_widget_render_icon (widget,
1846                                            image->data.stock.stock_id,
1847                                            image->icon_size,
1848                                            NULL);
1849           if (pixbuf)
1850             {              
1851               image_bound.width = gdk_pixbuf_get_width (pixbuf);
1852               image_bound.height = gdk_pixbuf_get_height (pixbuf);
1853             }
1854
1855           /* already done */
1856           needs_state_transform = FALSE;
1857           break;
1858
1859         case GTK_IMAGE_ICON_SET:
1860           pixbuf =
1861             gtk_icon_set_render_icon (image->data.icon_set.icon_set,
1862                                       widget->style,
1863                                       gtk_widget_get_direction (widget),
1864                                       GTK_WIDGET_STATE (widget),
1865                                       image->icon_size,
1866                                       widget,
1867                                       NULL);
1868
1869           if (pixbuf)
1870             {
1871               image_bound.width = gdk_pixbuf_get_width (pixbuf);
1872               image_bound.height = gdk_pixbuf_get_height (pixbuf);
1873             }
1874
1875           /* already done */
1876           needs_state_transform = FALSE;
1877           break;
1878
1879         case GTK_IMAGE_ANIMATION:
1880           {
1881             if (image->data.anim.iter == NULL)
1882               {
1883                 image->data.anim.iter = gdk_pixbuf_animation_get_iter (image->data.anim.anim, NULL);
1884                 
1885                 if (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter) >= 0)
1886                   image->data.anim.frame_timeout =
1887                     gdk_threads_add_timeout (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter),
1888                                    animation_timeout,
1889                                    image);
1890               }
1891
1892             image_bound.width = gdk_pixbuf_animation_get_width (image->data.anim.anim);
1893             image_bound.height = gdk_pixbuf_animation_get_height (image->data.anim.anim);
1894                   
1895             /* don't advance the anim iter here, or we could get frame changes between two
1896              * exposes of different areas.
1897              */
1898             
1899             pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (image->data.anim.iter);
1900             g_object_ref (pixbuf);
1901           }
1902           break;
1903
1904         case GTK_IMAGE_ICON_NAME:
1905           ensure_pixbuf_for_icon_name (image);
1906           pixbuf = image->data.name.pixbuf;
1907           if (pixbuf)
1908             {
1909               g_object_ref (pixbuf);
1910               image_bound.width = gdk_pixbuf_get_width (pixbuf);
1911               image_bound.height = gdk_pixbuf_get_height (pixbuf);
1912             }
1913           break;
1914
1915         case GTK_IMAGE_GICON:
1916           ensure_pixbuf_for_gicon (image);
1917           pixbuf = image->data.gicon.pixbuf;
1918           if (pixbuf)
1919             {
1920               g_object_ref (pixbuf);
1921               image_bound.width = gdk_pixbuf_get_width (pixbuf);
1922               image_bound.height = gdk_pixbuf_get_height (pixbuf);
1923             }
1924           break;
1925           
1926         case GTK_IMAGE_EMPTY:
1927           g_assert_not_reached ();
1928           break;
1929         }
1930
1931       if (mask)
1932         {
1933           gdk_gc_set_clip_mask (widget->style->black_gc, mask);
1934           gdk_gc_set_clip_origin (widget->style->black_gc, mask_x, mask_y);
1935         }
1936
1937       if (rectangle_intersect_even (&area, &image_bound))
1938         {
1939           if (pixbuf)
1940             {
1941               if (needs_state_transform)
1942                 {
1943                   GtkIconSource *source;
1944                   GdkPixbuf *rendered;
1945
1946                   source = gtk_icon_source_new ();
1947                   gtk_icon_source_set_pixbuf (source, pixbuf);
1948                   /* The size here is arbitrary; since size isn't
1949                    * wildcarded in the souce, it isn't supposed to be
1950                    * scaled by the engine function
1951                    */
1952                   gtk_icon_source_set_size (source,
1953                                             GTK_ICON_SIZE_SMALL_TOOLBAR);
1954                   gtk_icon_source_set_size_wildcarded (source, FALSE);
1955                   
1956                   rendered = gtk_style_render_icon (widget->style,
1957                                                     source,
1958                                                     gtk_widget_get_direction (widget),
1959                                                     GTK_WIDGET_STATE (widget),
1960                                                     /* arbitrary */
1961                                                     (GtkIconSize)-1,
1962                                                     widget,
1963                                                     "gtk-image");
1964
1965                   gtk_icon_source_free (source);
1966
1967                   g_object_unref (pixbuf);
1968                   pixbuf = rendered;
1969                 }
1970
1971               if (pixbuf)
1972                 {
1973                   gdk_draw_pixbuf (widget->window,
1974                                    widget->style->black_gc,
1975                                    pixbuf,
1976                                    image_bound.x - x,
1977                                    image_bound.y - y,
1978                                    image_bound.x,
1979                                    image_bound.y,
1980                                    image_bound.width,
1981                                    image_bound.height,
1982                                    GDK_RGB_DITHER_NORMAL,
1983                                    0, 0);
1984                 }
1985             }
1986           else
1987             {
1988               switch (image->storage_type)
1989                 {
1990                 case GTK_IMAGE_PIXMAP:
1991                   gdk_draw_drawable (widget->window,
1992                                      widget->style->black_gc,
1993                                      image->data.pixmap.pixmap,
1994                                      image_bound.x - x, image_bound.y - y,
1995                                      image_bound.x, image_bound.y,
1996                                      image_bound.width, image_bound.height);
1997                   break;
1998               
1999                 case GTK_IMAGE_IMAGE:
2000                   gdk_draw_image (widget->window,
2001                                   widget->style->black_gc,
2002                                   image->data.image.image,
2003                                   image_bound.x - x, image_bound.y - y,
2004                                   image_bound.x, image_bound.y,
2005                                   image_bound.width, image_bound.height);
2006                   break;
2007
2008                 case GTK_IMAGE_PIXBUF:
2009                 case GTK_IMAGE_STOCK:
2010                 case GTK_IMAGE_ICON_SET:
2011                 case GTK_IMAGE_ANIMATION:
2012                 case GTK_IMAGE_ICON_NAME:
2013                 case GTK_IMAGE_EMPTY:
2014                 case GTK_IMAGE_GICON:
2015                   g_assert_not_reached ();
2016                   break;
2017                 }
2018             }
2019         } /* if rectangle intersects */      
2020
2021       if (mask)
2022         {
2023           gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
2024           gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
2025         }
2026       
2027       if (pixbuf)
2028         g_object_unref (pixbuf);
2029
2030     } /* if widget is drawable */
2031
2032   return FALSE;
2033 }
2034
2035 static void
2036 gtk_image_reset (GtkImage *image)
2037 {
2038   GtkImagePrivate *priv;
2039
2040   priv = GTK_IMAGE_GET_PRIVATE (image);
2041
2042   g_object_freeze_notify (G_OBJECT (image));
2043   
2044   if (image->storage_type != GTK_IMAGE_EMPTY)
2045     g_object_notify (G_OBJECT (image), "storage-type");
2046
2047   if (image->mask)
2048     {
2049       g_object_unref (image->mask);
2050       image->mask = NULL;
2051       g_object_notify (G_OBJECT (image), "mask");
2052     }
2053
2054   if (image->icon_size != DEFAULT_ICON_SIZE)
2055     {
2056       image->icon_size = DEFAULT_ICON_SIZE;
2057       g_object_notify (G_OBJECT (image), "icon-size");
2058     }
2059   
2060   switch (image->storage_type)
2061     {
2062     case GTK_IMAGE_PIXMAP:
2063
2064       if (image->data.pixmap.pixmap)
2065         g_object_unref (image->data.pixmap.pixmap);
2066       image->data.pixmap.pixmap = NULL;
2067       
2068       g_object_notify (G_OBJECT (image), "pixmap");
2069       
2070       break;
2071
2072     case GTK_IMAGE_IMAGE:
2073
2074       if (image->data.image.image)
2075         g_object_unref (image->data.image.image);
2076       image->data.image.image = NULL;
2077       
2078       g_object_notify (G_OBJECT (image), "image");
2079       
2080       break;
2081
2082     case GTK_IMAGE_PIXBUF:
2083
2084       if (image->data.pixbuf.pixbuf)
2085         g_object_unref (image->data.pixbuf.pixbuf);
2086
2087       g_object_notify (G_OBJECT (image), "pixbuf");
2088       
2089       break;
2090
2091     case GTK_IMAGE_STOCK:
2092
2093       g_free (image->data.stock.stock_id);
2094
2095       image->data.stock.stock_id = NULL;
2096       
2097       g_object_notify (G_OBJECT (image), "stock");      
2098       break;
2099
2100     case GTK_IMAGE_ICON_SET:
2101       if (image->data.icon_set.icon_set)
2102         gtk_icon_set_unref (image->data.icon_set.icon_set);
2103       image->data.icon_set.icon_set = NULL;
2104       
2105       g_object_notify (G_OBJECT (image), "icon-set");      
2106       break;
2107
2108     case GTK_IMAGE_ANIMATION:
2109       gtk_image_reset_anim_iter (image);
2110       
2111       if (image->data.anim.anim)
2112         g_object_unref (image->data.anim.anim);
2113       image->data.anim.anim = NULL;
2114       
2115       g_object_notify (G_OBJECT (image), "pixbuf-animation");
2116       
2117       break;
2118
2119     case GTK_IMAGE_ICON_NAME:
2120       g_free (image->data.name.icon_name);
2121       image->data.name.icon_name = NULL;
2122       if (image->data.name.pixbuf)
2123         g_object_unref (image->data.name.pixbuf);
2124       image->data.name.pixbuf = NULL;
2125
2126       g_object_notify (G_OBJECT (image), "icon-name");
2127
2128       break;
2129       
2130     case GTK_IMAGE_GICON:
2131       if (image->data.gicon.icon)
2132         g_object_unref (image->data.gicon.icon);
2133       image->data.gicon.icon = NULL;
2134       if (image->data.gicon.pixbuf)
2135         g_object_unref (image->data.gicon.pixbuf);
2136       image->data.gicon.pixbuf = NULL;
2137
2138       g_object_notify (G_OBJECT (image), "gicon");
2139
2140       break;
2141       
2142     case GTK_IMAGE_EMPTY:
2143     default:
2144       break;
2145       
2146     }
2147
2148   if (priv->filename)
2149     {
2150       g_free (priv->filename);
2151       priv->filename = NULL;
2152       g_object_notify (G_OBJECT (image), "file");
2153     }
2154
2155   image->storage_type = GTK_IMAGE_EMPTY;
2156
2157   memset (&image->data, '\0', sizeof (image->data));
2158
2159   g_object_thaw_notify (G_OBJECT (image));
2160 }
2161
2162 /**
2163  * gtk_image_clear:
2164  * @image: a #GtkImage
2165  *
2166  * Resets the image to be empty.
2167  *
2168  * Since: 2.8
2169  */
2170 void
2171 gtk_image_clear (GtkImage *image)
2172 {
2173   GtkImagePrivate *priv;
2174
2175   priv = GTK_IMAGE_GET_PRIVATE (image);
2176
2177   priv->need_calc_size = 1;
2178
2179   gtk_image_reset (image);
2180   gtk_image_update_size (image, 0, 0);
2181 }
2182
2183 static void
2184 gtk_image_calc_size (GtkImage *image)
2185 {
2186   GtkWidget *widget = GTK_WIDGET (image);
2187   GdkPixbuf *pixbuf = NULL;
2188   GtkImagePrivate *priv;
2189
2190   priv = GTK_IMAGE_GET_PRIVATE (image);
2191
2192   priv->need_calc_size = 0;
2193
2194   /* We update stock/icon set on every size request, because
2195    * the theme could have affected the size; for other kinds of
2196    * image, we just update the requisition when the image data
2197    * is set.
2198    */
2199   switch (image->storage_type)
2200     {
2201     case GTK_IMAGE_STOCK:
2202       pixbuf = gtk_widget_render_icon (widget,
2203                                        image->data.stock.stock_id,
2204                                        image->icon_size,
2205                                        NULL);
2206       break;
2207       
2208     case GTK_IMAGE_ICON_SET:
2209       pixbuf = gtk_icon_set_render_icon (image->data.icon_set.icon_set,
2210                                          widget->style,
2211                                          gtk_widget_get_direction (widget),
2212                                          GTK_WIDGET_STATE (widget),
2213                                          image->icon_size,
2214                                          widget,
2215                                          NULL);
2216       break;
2217     case GTK_IMAGE_ICON_NAME:
2218       ensure_pixbuf_for_icon_name (image);
2219       pixbuf = image->data.name.pixbuf;
2220       if (pixbuf) g_object_ref (pixbuf);
2221       break;
2222     case GTK_IMAGE_GICON:
2223       ensure_pixbuf_for_gicon (image);
2224       pixbuf = image->data.gicon.pixbuf;
2225       if (pixbuf)
2226         g_object_ref (pixbuf);
2227       break;
2228     default:
2229       break;
2230     }
2231
2232   if (pixbuf)
2233     {
2234       widget->requisition.width = gdk_pixbuf_get_width (pixbuf) + GTK_MISC (image)->xpad * 2;
2235       widget->requisition.height = gdk_pixbuf_get_height (pixbuf) + GTK_MISC (image)->ypad * 2;
2236
2237       g_object_unref (pixbuf);
2238     }
2239 }
2240
2241 static void
2242 gtk_image_size_request (GtkWidget      *widget,
2243                         GtkRequisition *requisition)
2244 {
2245   GtkImage *image;
2246   
2247   image = GTK_IMAGE (widget);
2248
2249   gtk_image_calc_size (image);
2250
2251   /* Chain up to default that simply reads current requisition */
2252   GTK_WIDGET_CLASS (gtk_image_parent_class)->size_request (widget, requisition);
2253 }
2254
2255 static void
2256 gtk_image_style_set (GtkWidget      *widget,
2257                      GtkStyle       *prev_style)
2258 {
2259   GtkImage *image;
2260
2261   image = GTK_IMAGE (widget);
2262
2263   GTK_WIDGET_CLASS (gtk_image_parent_class)->style_set (widget, prev_style);
2264
2265   icon_theme_changed (image);
2266 }
2267
2268 static void
2269 gtk_image_screen_changed (GtkWidget *widget,
2270                           GdkScreen *prev_screen)
2271 {
2272   GtkImage *image;
2273
2274   image = GTK_IMAGE (widget);
2275
2276   if (GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed)
2277     GTK_WIDGET_CLASS (gtk_image_parent_class)->screen_changed (widget, prev_screen);
2278
2279   icon_theme_changed (image);
2280 }
2281
2282
2283 static void
2284 gtk_image_update_size (GtkImage *image,
2285                        gint      image_width,
2286                        gint      image_height)
2287 {
2288   GTK_WIDGET (image)->requisition.width = image_width + GTK_MISC (image)->xpad * 2;
2289   GTK_WIDGET (image)->requisition.height = image_height + GTK_MISC (image)->ypad * 2;
2290
2291   if (GTK_WIDGET_VISIBLE (image))
2292     gtk_widget_queue_resize (GTK_WIDGET (image));
2293 }
2294
2295
2296 /**
2297  * gtk_image_set_pixel_size:
2298  * @image: a #GtkImage
2299  * @pixel_size: the new pixel size
2300  * 
2301  * Sets the pixel size to use for named icons. If the pixel size is set
2302  * to a value != -1, it is used instead of the icon size set by
2303  * gtk_image_set_from_icon_name().
2304  *
2305  * Since: 2.6
2306  */
2307 void 
2308 gtk_image_set_pixel_size (GtkImage *image,
2309                           gint      pixel_size)
2310 {
2311   GtkImagePrivate *priv;
2312
2313   g_return_if_fail (GTK_IS_IMAGE (image));
2314   
2315   priv = GTK_IMAGE_GET_PRIVATE (image);
2316
2317   if (priv->pixel_size != pixel_size)
2318     {
2319       priv->pixel_size = pixel_size;
2320       
2321       if (image->storage_type == GTK_IMAGE_ICON_NAME)
2322         {
2323           if (image->data.name.pixbuf)
2324             {
2325               g_object_unref (image->data.name.pixbuf);
2326               image->data.name.pixbuf = NULL;
2327             }
2328           
2329           gtk_image_update_size (image, pixel_size, pixel_size);
2330         }
2331       
2332       if (image->storage_type == GTK_IMAGE_GICON)
2333         {
2334           if (image->data.gicon.pixbuf)
2335             {
2336               g_object_unref (image->data.gicon.pixbuf);
2337               image->data.gicon.pixbuf = NULL;
2338             }
2339           
2340           gtk_image_update_size (image, pixel_size, pixel_size);
2341         }
2342       
2343       g_object_notify (G_OBJECT (image), "pixel-size");
2344     }
2345 }
2346
2347 /**
2348  * gtk_image_get_pixel_size:
2349  * @image: a #GtkImage
2350  * 
2351  * Gets the pixel size used for named icons.
2352  *
2353  * Returns: the pixel size used for named icons.
2354  *
2355  * Since: 2.6
2356  */
2357 gint
2358 gtk_image_get_pixel_size (GtkImage *image)
2359 {
2360   GtkImagePrivate *priv;
2361
2362   g_return_val_if_fail (GTK_IS_IMAGE (image), -1);
2363   
2364   priv = GTK_IMAGE_GET_PRIVATE (image);
2365
2366   return priv->pixel_size;
2367 }
2368
2369 #if defined (G_OS_WIN32) && !defined (_WIN64)
2370
2371 #undef gtk_image_new_from_file
2372
2373 GtkWidget*
2374 gtk_image_new_from_file   (const gchar *filename)
2375 {
2376   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2377   GtkWidget *retval;
2378
2379   retval = gtk_image_new_from_file_utf8 (utf8_filename);
2380
2381   g_free (utf8_filename);
2382
2383   return retval;
2384 }
2385
2386 #undef gtk_image_set_from_file
2387
2388 void
2389 gtk_image_set_from_file   (GtkImage    *image,
2390                            const gchar *filename)
2391 {
2392   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2393
2394   gtk_image_set_from_file_utf8 (image, utf8_filename);
2395
2396   g_free (utf8_filename);
2397 }
2398
2399 #endif
2400
2401 #define __GTK_IMAGE_C__
2402 #include "gtkaliasdef.c"