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