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