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