gtkimage.c 59.6 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include "config.h"
28

29
#include <math.h>
30 31
#include <string.h>

Elliot Lee's avatar
Elliot Lee committed
32 33
#include "gtkcontainer.h"
#include "gtkimage.h"
34
#include "gtkiconfactory.h"
35
#include "gtkstock.h"
36
#include "gtkicontheme.h"
37
#include "gtksizerequest.h"
Havoc Pennington's avatar
Havoc Pennington committed
38
#include "gtkintl.h"
39
#include "gtkprivate.h"
40
#include "gtktypebuiltins.h"
Elliot Lee's avatar
Elliot Lee committed
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/**
 * SECTION:gtkimage
 * @Short_description: A widget displaying an image
 * @Title: GtkImage
 * @See_also:#GdkPixbuf
 *
 * The #GtkImage widget displays an image. Various kinds of object
 * can be displayed as an image; most typically, you would load a
 * #GdkPixbuf ("pixel buffer") from a file, and then display that.
 * There's a convenience function to do this, gtk_image_new_from_file(),
 * used as follows:
 * <informalexample><programlisting>
 *   GtkWidget *image;
 *   image = gtk_image_new_from_file ("myfile.png");
 * </programlisting></informalexample>
 * If the file isn't loaded successfully, the image will contain a
 * "broken image" icon similar to that used in many web browsers.
 * If you want to handle errors in loading the file yourself,
 * for example by displaying an error message, then load the image with
 * gdk_pixbuf_new_from_file(), then create the #GtkImage with
 * gtk_image_new_from_pixbuf().
 *
 * The image file may contain an animation, if so the #GtkImage will
 * display an animation (#GdkPixbufAnimation) instead of a static image.
 *
 * #GtkImage is a subclass of #GtkMisc, which implies that you can
 * align it (center, left, right) and add padding to it, using
 * #GtkMisc methods.
 *
 * #GtkImage is a "no window" widget (has no #GdkWindow of its own),
 * so by default does not receive events. If you want to receive events
 * on the image, such as button clicks, place the image inside a
 * #GtkEventBox, then connect to the event signals on the event box.
 * <example>
 * <title>Handling button press events on a
 * <structname>GtkImage</structname>.</title>
 * <programlisting>
 *   static gboolean
 *   button_press_callback (GtkWidget      *event_box,
 *                          GdkEventButton *event,
 *                          gpointer        data)
 *   {
 *     g_print ("Event box clicked at coordinates &percnt;f,&percnt;f\n",
 *              event->x, event->y);
 *
 *     /<!---->* Returning TRUE means we handled the event, so the signal
 *      * emission should be stopped (don't call any further
 *      * callbacks that may be connected). Return FALSE
 *      * to continue invoking callbacks.
 *      *<!---->/
 *     return TRUE;
 *   }
 *
 *   static GtkWidget*
 *   create_image (void)
 *   {
 *     GtkWidget *image;
 *     GtkWidget *event_box;
 *
 *     image = gtk_image_new_from_file ("myfile.png");
 *
 *     event_box = gtk_event_box_new (<!-- -->);
 *
 *     gtk_container_add (GTK_CONTAINER (event_box), image);
 *
 *     g_signal_connect (G_OBJECT (event_box),
 *                       "button_press_event",
 *                       G_CALLBACK (button_press_callback),
 *                       image);
 *
 *     return image;
 *   }
 * </programlisting>
 * </example>
 *
 * When handling events on the event box, keep in mind that coordinates
 * in the image may be different from event box coordinates due to
 * the alignment and padding settings on the image (see #GtkMisc).
 * The simplest way to solve this is to set the alignment to 0.0
 * (left/top), and set the padding to zero. Then the origin of
 * the image will be the same as the origin of the event box.
 *
 * Sometimes an application will want to avoid depending on external data
 * files, such as image files. GTK+ comes with a program to avoid this,
 * called <application>gdk-pixbuf-csource</application>. This program
 * allows you to convert an image into a C variable declaration, which
 * can then be loaded into a #GdkPixbuf using
 * gdk_pixbuf_new_from_inline().
 */

132

133
struct _GtkImagePrivate
134
{
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  GtkIconSize           icon_size;      /* Only used with GTK_IMAGE_STOCK, GTK_IMAGE_ICON_SET, GTK_IMAGE_ICON_NAME */
  GtkImageType          storage_type;

  union
  {
    GtkImagePixbufData     pixbuf;
    GtkImageStockData      stock;
    GtkImageIconSetData    icon_set;
    GtkImageAnimationData  anim;
    GtkImageIconNameData   name;
    GtkImageGIconData      gicon;
  } data;

  gboolean              was_symbolic;
  gchar                *filename;       /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
150
  gint                  last_rendered_state;  /* a GtkStateFlags, with -1 meaning an invalid state,
151 152
                                               * only used with GTK_IMAGE_GICON, GTK_IMAGE_ICON_NAME */
  gint                  pixel_size;
153 154
  gint                  required_width;
  gint                  required_height;
155 156
  guint                 need_calc_size : 1;
  guint                 use_fallback : 1;
157 158
};

159

Havoc Pennington's avatar
Havoc Pennington committed
160
#define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON
161 162 163 164 165 166 167 168 169 170 171
static gint gtk_image_draw                 (GtkWidget    *widget,
                                            cairo_t      *cr);
static void gtk_image_unmap                (GtkWidget    *widget);
static void gtk_image_unrealize            (GtkWidget    *widget);
static void gtk_image_get_preferred_width  (GtkWidget    *widget,
                                            gint         *minimum,
                                            gint         *natural);
static void gtk_image_get_preferred_height (GtkWidget    *widget,
                                            gint         *minimum,
                                            gint         *natural);

172
static void gtk_image_style_updated        (GtkWidget    *widget);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
static void gtk_image_screen_changed       (GtkWidget    *widget,
                                            GdkScreen    *prev_screen);
static void gtk_image_destroy              (GtkWidget    *widget);
static void gtk_image_reset                (GtkImage     *image);
static void gtk_image_calc_size            (GtkImage     *image);

static void gtk_image_update_size          (GtkImage     *image,
                                            gint          image_width,
                                            gint          image_height);

static void gtk_image_set_property         (GObject      *object,
                                            guint         prop_id,
                                            const GValue *value,
                                            GParamSpec   *pspec);
static void gtk_image_get_property         (GObject      *object,
                                            guint         prop_id,
                                            GValue       *value,
                                            GParamSpec   *pspec);

static void icon_theme_changed             (GtkImage     *image);
193

Havoc Pennington's avatar
Havoc Pennington committed
194 195 196 197 198 199 200 201
enum
{
  PROP_0,
  PROP_PIXBUF,
  PROP_FILE,
  PROP_STOCK,
  PROP_ICON_SET,
  PROP_ICON_SIZE,
202
  PROP_PIXEL_SIZE,
Havoc Pennington's avatar
Havoc Pennington committed
203
  PROP_PIXBUF_ANIMATION,
204
  PROP_ICON_NAME,
205
  PROP_STORAGE_TYPE,
206 207
  PROP_GICON,
  PROP_USE_FALLBACK
Havoc Pennington's avatar
Havoc Pennington committed
208 209
};

Matthias Clasen's avatar
Matthias Clasen committed
210
G_DEFINE_TYPE (GtkImage, gtk_image, GTK_TYPE_MISC)
Elliot Lee's avatar
Elliot Lee committed
211 212 213 214

static void
gtk_image_class_init (GtkImageClass *class)
{
Havoc Pennington's avatar
Havoc Pennington committed
215
  GObjectClass *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
216 217
  GtkWidgetClass *widget_class;

Havoc Pennington's avatar
Havoc Pennington committed
218 219 220 221
  gobject_class = G_OBJECT_CLASS (class);
  
  gobject_class->set_property = gtk_image_set_property;
  gobject_class->get_property = gtk_image_get_property;
222

Havoc Pennington's avatar
Havoc Pennington committed
223
  widget_class = GTK_WIDGET_CLASS (class);
224
  widget_class->draw = gtk_image_draw;
225
  widget_class->destroy = gtk_image_destroy;
226 227
  widget_class->get_preferred_width = gtk_image_get_preferred_width;
  widget_class->get_preferred_height = gtk_image_get_preferred_height;
Havoc Pennington's avatar
Havoc Pennington committed
228
  widget_class->unmap = gtk_image_unmap;
229
  widget_class->unrealize = gtk_image_unrealize;
230
  widget_class->style_updated = gtk_image_style_updated;
231
  widget_class->screen_changed = gtk_image_screen_changed;
Havoc Pennington's avatar
Havoc Pennington committed
232 233 234 235
  
  g_object_class_install_property (gobject_class,
                                   PROP_PIXBUF,
                                   g_param_spec_object ("pixbuf",
236 237
                                                        P_("Pixbuf"),
                                                        P_("A GdkPixbuf to display"),
Havoc Pennington's avatar
Havoc Pennington committed
238
                                                        GDK_TYPE_PIXBUF,
239
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
240 241 242 243

  g_object_class_install_property (gobject_class,
                                   PROP_FILE,
                                   g_param_spec_string ("file",
244 245
                                                        P_("Filename"),
                                                        P_("Filename to load and display"),
Havoc Pennington's avatar
Havoc Pennington committed
246
                                                        NULL,
247
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
248 249 250 251 252
  

  g_object_class_install_property (gobject_class,
                                   PROP_STOCK,
                                   g_param_spec_string ("stock",
253 254
                                                        P_("Stock ID"),
                                                        P_("Stock ID for a stock image to display"),
Havoc Pennington's avatar
Havoc Pennington committed
255
                                                        NULL,
256
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
257 258 259
  
  g_object_class_install_property (gobject_class,
                                   PROP_ICON_SET,
260
                                   g_param_spec_boxed ("icon-set",
261 262
                                                       P_("Icon set"),
                                                       P_("Icon set to display"),
Havoc Pennington's avatar
Havoc Pennington committed
263
                                                       GTK_TYPE_ICON_SET,
264
                                                       GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
265 266 267
  
  g_object_class_install_property (gobject_class,
                                   PROP_ICON_SIZE,
268
                                   g_param_spec_int ("icon-size",
269
                                                     P_("Icon size"),
270
                                                     P_("Symbolic size to use for stock icon, icon set or named icon"),
Havoc Pennington's avatar
Havoc Pennington committed
271 272
                                                     0, G_MAXINT,
                                                     DEFAULT_ICON_SIZE,
273
                                                     GTK_PARAM_READWRITE));
274 275 276
  /**
   * GtkImage:pixel-size:
   *
277 278
   * The "pixel-size" property can be used to specify a fixed size
   * overriding the #GtkImage:icon-size property for images of type 
279 280 281 282 283 284
   * %GTK_IMAGE_ICON_NAME. 
   *
   * Since: 2.6
   */
  g_object_class_install_property (gobject_class,
				   PROP_PIXEL_SIZE,
285
				   g_param_spec_int ("pixel-size",
286 287 288 289
						     P_("Pixel size"),
						     P_("Pixel size to use for named icon"),
						     -1, G_MAXINT,
						     -1,
290
						     GTK_PARAM_READWRITE));
291
  
Havoc Pennington's avatar
Havoc Pennington committed
292 293
  g_object_class_install_property (gobject_class,
                                   PROP_PIXBUF_ANIMATION,
294
                                   g_param_spec_object ("pixbuf-animation",
295 296
                                                        P_("Animation"),
                                                        P_("GdkPixbufAnimation to display"),
Havoc Pennington's avatar
Havoc Pennington committed
297
                                                        GDK_TYPE_PIXBUF_ANIMATION,
298
                                                        GTK_PARAM_READWRITE));
299 300 301 302

  /**
   * GtkImage:icon-name:
   *
303
   * The name of the icon in the icon theme. If the icon theme is
304 305 306 307 308 309
   * changed, the image will be updated automatically.
   *
   * Since: 2.6
   */
  g_object_class_install_property (gobject_class,
                                   PROP_ICON_NAME,
310
                                   g_param_spec_string ("icon-name",
311 312 313
                                                        P_("Icon Name"),
                                                        P_("The name of the icon from the icon theme"),
                                                        NULL,
314
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
315
  
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
  /**
   * GtkImage:gicon:
   *
   * The GIcon displayed in the GtkImage. For themed icons,
   * If the icon theme is changed, the image will be updated
   * automatically.
   *
   * Since: 2.14
   */
  g_object_class_install_property (gobject_class,
                                   PROP_GICON,
                                   g_param_spec_object ("gicon",
                                                        P_("Icon"),
                                                        P_("The GIcon being displayed"),
                                                        G_TYPE_ICON,
                                                        GTK_PARAM_READWRITE));
  
Havoc Pennington's avatar
Havoc Pennington committed
333 334
  g_object_class_install_property (gobject_class,
                                   PROP_STORAGE_TYPE,
Matthias Clasen's avatar
Matthias Clasen committed
335
                                   g_param_spec_enum ("storage-type",
336 337
                                                      P_("Storage type"),
                                                      P_("The representation being used for image data"),
Havoc Pennington's avatar
Havoc Pennington committed
338 339
                                                      GTK_TYPE_IMAGE_TYPE,
                                                      GTK_IMAGE_EMPTY,
340
                                                      GTK_PARAM_READABLE));
341

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
  /**
   * GtkImage:use-fallback:
   *
   * Whether the icon displayed in the GtkImage will use
   * standard icon names fallback. The value of this property
   * is only relevant for images of type %GTK_IMAGE_ICON_NAME
   * and %GTK_IMAGE_GICON.
   *
   * Since: 3.0
   */
  g_object_class_install_property (gobject_class,
                                   PROP_USE_FALLBACK,
                                   g_param_spec_boolean ("use-fallback",
                                                         P_("Use Fallback"),
                                                         P_("Whether to use icon names fallback"),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE));

360
  g_type_class_add_private (class, sizeof (GtkImagePrivate));
Elliot Lee's avatar
Elliot Lee committed
361 362 363 364 365
}

static void
gtk_image_init (GtkImage *image)
{
366
  GtkImagePrivate *priv;
367 368 369

  image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image,
                                             GTK_TYPE_IMAGE,
370
                                             GtkImagePrivate);
371
  priv = image->priv;
372

373
  gtk_widget_set_has_window (GTK_WIDGET (image), FALSE);
Elliot Lee's avatar
Elliot Lee committed
374

375 376
  priv->storage_type = GTK_IMAGE_EMPTY;
  priv->icon_size = DEFAULT_ICON_SIZE;
377 378

  priv->pixel_size = -1;
379 380

  priv->filename = NULL;
Elliot Lee's avatar
Elliot Lee committed
381 382
}

383
static void
384
gtk_image_destroy (GtkWidget *widget)
385
{
386
  GtkImage *image = GTK_IMAGE (widget);
387

388
  gtk_image_reset (image);
389 390

  GTK_WIDGET_CLASS (gtk_image_parent_class)->destroy (widget);
391 392
}

Havoc Pennington's avatar
Havoc Pennington committed
393 394 395 396 397 398
static void 
gtk_image_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
			GParamSpec   *pspec)
{
399
  GtkImage *image = GTK_IMAGE (object);
400
  GtkImagePrivate *priv = image->priv;
Havoc Pennington's avatar
Havoc Pennington committed
401 402 403 404 405 406 407 408

  switch (prop_id)
    {
    case PROP_PIXBUF:
      gtk_image_set_from_pixbuf (image,
                                 g_value_get_object (value));
      break;
    case PROP_FILE:
409
      gtk_image_set_from_file (image, g_value_get_string (value));
Havoc Pennington's avatar
Havoc Pennington committed
410 411 412
      break;
    case PROP_STOCK:
      gtk_image_set_from_stock (image, g_value_get_string (value),
413
                                priv->icon_size);
Havoc Pennington's avatar
Havoc Pennington committed
414 415 416
      break;
    case PROP_ICON_SET:
      gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
417
                                   priv->icon_size);
Havoc Pennington's avatar
Havoc Pennington committed
418 419
      break;
    case PROP_ICON_SIZE:
420
      if (priv->storage_type == GTK_IMAGE_STOCK)
Havoc Pennington's avatar
Havoc Pennington committed
421
        gtk_image_set_from_stock (image,
422
                                  priv->data.stock.stock_id,
Havoc Pennington's avatar
Havoc Pennington committed
423
                                  g_value_get_int (value));
424
      else if (priv->storage_type == GTK_IMAGE_ICON_SET)
Havoc Pennington's avatar
Havoc Pennington committed
425
        gtk_image_set_from_icon_set (image,
426
                                     priv->data.icon_set.icon_set,
Havoc Pennington's avatar
Havoc Pennington committed
427
                                     g_value_get_int (value));
428
      else if (priv->storage_type == GTK_IMAGE_ICON_NAME)
429
        gtk_image_set_from_icon_name (image,
430
				      priv->data.name.icon_name,
431
				      g_value_get_int (value));
432
      else if (priv->storage_type == GTK_IMAGE_GICON)
433
        gtk_image_set_from_gicon (image,
434
                                  priv->data.gicon.icon,
435
                                  g_value_get_int (value));
Havoc Pennington's avatar
Havoc Pennington committed
436
      else
437
        /* Save to be used when STOCK, ICON_SET, ICON_NAME or GICON property comes in */
438
        priv->icon_size = g_value_get_int (value);
Havoc Pennington's avatar
Havoc Pennington committed
439
      break;
440
    case PROP_PIXEL_SIZE:
441
      gtk_image_set_pixel_size (image, g_value_get_int (value));
442
      break;
Havoc Pennington's avatar
Havoc Pennington committed
443 444 445 446
    case PROP_PIXBUF_ANIMATION:
      gtk_image_set_from_animation (image,
                                    g_value_get_object (value));
      break;
447 448
    case PROP_ICON_NAME:
      gtk_image_set_from_icon_name (image, g_value_get_string (value),
449
				    priv->icon_size);
450
      break;
451 452
    case PROP_GICON:
      gtk_image_set_from_gicon (image, g_value_get_object (value),
453
				priv->icon_size);
454
      break;
455

456 457 458 459 460 461 462 463 464 465 466 467
    case PROP_USE_FALLBACK:
      priv->use_fallback = g_value_get_boolean (value);
      if (priv->storage_type == GTK_IMAGE_ICON_NAME)
        gtk_image_set_from_icon_name (image,
				      priv->data.name.icon_name,
				      priv->icon_size);
      else if (priv->storage_type == GTK_IMAGE_GICON)
        gtk_image_set_from_gicon (image,
                                  priv->data.gicon.icon,
                                  priv->icon_size);
      break;

Havoc Pennington's avatar
Havoc Pennington committed
468 469 470 471 472 473 474 475 476 477 478 479
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void 
gtk_image_get_property (GObject     *object,
			guint        prop_id,
			GValue      *value,
			GParamSpec  *pspec)
{
480
  GtkImage *image = GTK_IMAGE (object);
481
  GtkImagePrivate *priv = image->priv;
Havoc Pennington's avatar
Havoc Pennington committed
482 483 484 485 486 487 488 489 490 491

  /* The "getter" functions whine if you try to get the wrong
   * storage type. This function is instead robust against that,
   * so that GUI builders don't have to jump through hoops
   * to avoid g_warning
   */
  
  switch (prop_id)
    {
    case PROP_PIXBUF:
492
      if (priv->storage_type != GTK_IMAGE_PIXBUF)
Havoc Pennington's avatar
Havoc Pennington committed
493 494 495 496 497
        g_value_set_object (value, NULL);
      else
        g_value_set_object (value,
                            gtk_image_get_pixbuf (image));
      break;
498 499 500
    case PROP_FILE:
      g_value_set_string (value, priv->filename);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
501
    case PROP_STOCK:
502
      if (priv->storage_type != GTK_IMAGE_STOCK)
Havoc Pennington's avatar
Havoc Pennington committed
503 504 505
        g_value_set_string (value, NULL);
      else
        g_value_set_string (value,
506
                            priv->data.stock.stock_id);
Havoc Pennington's avatar
Havoc Pennington committed
507 508
      break;
    case PROP_ICON_SET:
509
      if (priv->storage_type != GTK_IMAGE_ICON_SET)
Havoc Pennington's avatar
Havoc Pennington committed
510 511 512
        g_value_set_boxed (value, NULL);
      else
        g_value_set_boxed (value,
513
                           priv->data.icon_set.icon_set);
Havoc Pennington's avatar
Havoc Pennington committed
514 515
      break;      
    case PROP_ICON_SIZE:
516
      g_value_set_int (value, priv->icon_size);
Havoc Pennington's avatar
Havoc Pennington committed
517
      break;
518 519 520
    case PROP_PIXEL_SIZE:
      g_value_set_int (value, priv->pixel_size);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
521
    case PROP_PIXBUF_ANIMATION:
522
      if (priv->storage_type != GTK_IMAGE_ANIMATION)
Havoc Pennington's avatar
Havoc Pennington committed
523 524 525
        g_value_set_object (value, NULL);
      else
        g_value_set_object (value,
526
                            priv->data.anim.anim);
Havoc Pennington's avatar
Havoc Pennington committed
527
      break;
528
    case PROP_ICON_NAME:
529
      if (priv->storage_type != GTK_IMAGE_ICON_NAME)
530
	g_value_set_string (value, NULL);
531 532
      else
	g_value_set_string (value,
533
			    priv->data.name.icon_name);
534
      break;
535
    case PROP_GICON:
536
      if (priv->storage_type != GTK_IMAGE_GICON)
537 538 539
	g_value_set_object (value, NULL);
      else
	g_value_set_object (value,
540
			    priv->data.gicon.icon);
541
      break;
Havoc Pennington's avatar
Havoc Pennington committed
542
    case PROP_STORAGE_TYPE:
543
      g_value_set_enum (value, priv->storage_type);
Havoc Pennington's avatar
Havoc Pennington committed
544
      break;
545 546 547 548

    case PROP_USE_FALLBACK:
      g_value_set_boolean (value, priv->use_fallback);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
549 550 551 552 553 554 555
      
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

556

557 558 559 560
/**
 * gtk_image_new_from_file:
 * @filename: a filename
 * 
Havoc Pennington's avatar
Havoc Pennington committed
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
 * Creates a new #GtkImage displaying the file @filename. If the file
 * isn't found or can't be loaded, the resulting #GtkImage will
 * display a "broken image" icon. This function never returns %NULL,
 * it always returns a valid #GtkImage widget.
 *
 * If the file contains an animation, the image will contain an
 * animation.
 *
 * If you need to detect failures to load the file, use
 * gdk_pixbuf_new_from_file() to load the file yourself, then create
 * the #GtkImage from the pixbuf. (Or for animations, use
 * gdk_pixbuf_animation_new_from_file()).
 *
 * The storage type (gtk_image_get_storage_type()) of the returned
 * image is not defined, it will be whatever is appropriate for
 * displaying the file.
577 578 579
 * 
 * Return value: a new #GtkImage
 **/
580 581 582 583 584
GtkWidget*
gtk_image_new_from_file   (const gchar *filename)
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
585
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
586 587 588 589 590 591

  gtk_image_set_from_file (image, filename);

  return GTK_WIDGET (image);
}

592 593
/**
 * gtk_image_new_from_pixbuf:
594 595
 * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
 *
596 597 598 599 600
 * Creates a new #GtkImage displaying @pixbuf.
 * The #GtkImage does not assume a reference to the
 * pixbuf; you still need to unref it if you own references.
 * #GtkImage will add its own reference rather than adopting yours.
 * 
601 602 603
 * Note that this function just creates an #GtkImage from the pixbuf. The
 * #GtkImage created will not react to state changes. Should you want that, 
 * you should use gtk_image_new_from_icon_set().
604
 * 
605 606
 * Return value: a new #GtkImage
 **/
607 608 609 610 611
GtkWidget*
gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
612
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
613 614 615 616 617 618

  gtk_image_set_from_pixbuf (image, pixbuf);

  return GTK_WIDGET (image);  
}

619 620 621
/**
 * gtk_image_new_from_stock:
 * @stock_id: a stock icon name
622
 * @size: (type int): a stock icon size
623 624
 * 
 * Creates a #GtkImage displaying a stock icon. Sample stock icon
625
 * names are #GTK_STOCK_OPEN, #GTK_STOCK_QUIT. Sample stock sizes
626
 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
Matthias Clasen's avatar
Matthias Clasen committed
627
 * icon name isn't known, the image will be empty.
628 629
 * You can register your own stock icon names, see
 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
630 631 632
 * 
 * Return value: a new #GtkImage displaying the stock icon
 **/
633 634
GtkWidget*
gtk_image_new_from_stock (const gchar    *stock_id,
635
                          GtkIconSize     size)
636 637 638
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
639
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
640 641 642 643 644 645

  gtk_image_set_from_stock (image, stock_id, size);

  return GTK_WIDGET (image);
}

646 647 648
/**
 * gtk_image_new_from_icon_set:
 * @icon_set: a #GtkIconSet
649
 * @size: (type int): a stock icon size
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
 *
 * Creates a #GtkImage displaying an icon set. Sample stock sizes are
 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of using
 * this function, usually it's better to create a #GtkIconFactory, put
 * your icon sets in the icon factory, add the icon factory to the
 * list of default factories with gtk_icon_factory_add_default(), and
 * then use gtk_image_new_from_stock(). This will allow themes to
 * override the icon you ship with your application.
 *
 * The #GtkImage does not assume a reference to the
 * icon set; you still need to unref it if you own references.
 * #GtkImage will add its own reference rather than adopting yours.
 * 
 * Return value: a new #GtkImage
 **/
665 666
GtkWidget*
gtk_image_new_from_icon_set (GtkIconSet     *icon_set,
667
                             GtkIconSize     size)
668 669 670
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
671
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
672 673

  gtk_image_set_from_icon_set (image, icon_set, size);
Elliot Lee's avatar
Elliot Lee committed
674 675 676 677

  return GTK_WIDGET (image);
}

Havoc Pennington's avatar
Havoc Pennington committed
678 679 680 681 682 683 684 685
/**
 * gtk_image_new_from_animation:
 * @animation: an animation
 * 
 * Creates a #GtkImage displaying the given animation.
 * The #GtkImage does not assume a reference to the
 * animation; you still need to unref it if you own references.
 * #GtkImage will add its own reference rather than adopting yours.
686 687 688 689 690 691
 *
 * Note that the animation frames are shown using a timeout with
 * #G_PRIORITY_DEFAULT. When using animations to indicate busyness,
 * keep in mind that the animation will only be shown if the main loop
 * is not busy with something that has a higher priority.
 *
Havoc Pennington's avatar
Havoc Pennington committed
692 693 694 695 696 697 698 699 700
 * Return value: a new #GtkImage widget
 **/
GtkWidget*
gtk_image_new_from_animation (GdkPixbufAnimation *animation)
{
  GtkImage *image;

  g_return_val_if_fail (GDK_IS_PIXBUF_ANIMATION (animation), NULL);
  
Manish Singh's avatar
Manish Singh committed
701
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
702 703 704 705 706 707

  gtk_image_set_from_animation (image, animation);

  return GTK_WIDGET (image);
}

708 709 710
/**
 * gtk_image_new_from_icon_name:
 * @icon_name: an icon name
711
 * @size: (type int): a stock icon size
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
 * 
 * Creates a #GtkImage displaying an icon from the current icon theme.
 * If the icon name isn't known, a "broken image" icon will be
 * displayed instead.  If the current icon theme is changed, the icon
 * will be updated appropriately.
 * 
 * Return value: a new #GtkImage displaying the themed icon
 *
 * Since: 2.6
 **/
GtkWidget*
gtk_image_new_from_icon_name (const gchar    *icon_name,
			      GtkIconSize     size)
{
  GtkImage *image;

  image = g_object_new (GTK_TYPE_IMAGE, NULL);

  gtk_image_set_from_icon_name (image, icon_name, size);

  return GTK_WIDGET (image);
}

735 736 737
/**
 * gtk_image_new_from_gicon:
 * @icon: an icon
738
 * @size: (type int): a stock icon size
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
 * 
 * Creates a #GtkImage displaying an icon from the current icon theme.
 * If the icon name isn't known, a "broken image" icon will be
 * displayed instead.  If the current icon theme is changed, the icon
 * will be updated appropriately.
 * 
 * Return value: a new #GtkImage displaying the themed icon
 *
 * Since: 2.14
 **/
GtkWidget*
gtk_image_new_from_gicon (GIcon *icon,
			  GtkIconSize     size)
{
  GtkImage *image;

  image = g_object_new (GTK_TYPE_IMAGE, NULL);

  gtk_image_set_from_gicon (image, icon, size);

  return GTK_WIDGET (image);
}

762 763 764
/**
 * gtk_image_set_from_file:
 * @image: a #GtkImage
765
 * @filename: (allow-none): a filename or %NULL
766 767 768
 *
 * See gtk_image_new_from_file() for details.
 **/
769 770 771 772
void
gtk_image_set_from_file   (GtkImage    *image,
                           const gchar *filename)
{
773
  GtkImagePrivate *priv;
Havoc Pennington's avatar
Havoc Pennington committed
774
  GdkPixbufAnimation *anim;
775 776
  
  g_return_if_fail (GTK_IS_IMAGE (image));
Havoc Pennington's avatar
Havoc Pennington committed
777

778 779
  priv = image->priv;

Havoc Pennington's avatar
Havoc Pennington committed
780
  g_object_freeze_notify (G_OBJECT (image));
781
  
782
  gtk_image_clear (image);
783 784

  if (filename == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
785
    {
786
      priv->filename = NULL;
Havoc Pennington's avatar
Havoc Pennington committed
787 788 789
      g_object_thaw_notify (G_OBJECT (image));
      return;
    }
790

Havoc Pennington's avatar
Havoc Pennington committed
791
  anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
792

Havoc Pennington's avatar
Havoc Pennington committed
793
  if (anim == NULL)
794 795 796 797
    {
      gtk_image_set_from_stock (image,
                                GTK_STOCK_MISSING_IMAGE,
                                GTK_ICON_SIZE_BUTTON);
Havoc Pennington's avatar
Havoc Pennington committed
798
      g_object_thaw_notify (G_OBJECT (image));
799 800
      return;
    }
801

Havoc Pennington's avatar
Havoc Pennington committed
802 803 804 805
  /* We could just unconditionally set_from_animation,
   * but it's nicer for memory if we toss the animation
   * if it's just a single pixbuf
   */
806

Havoc Pennington's avatar
Havoc Pennington committed
807
  if (gdk_pixbuf_animation_is_static_image (anim))
808 809
    gtk_image_set_from_pixbuf (image,
			       gdk_pixbuf_animation_get_static_image (anim));
Havoc Pennington's avatar
Havoc Pennington committed
810
  else
811
    gtk_image_set_from_animation (image, anim);
Havoc Pennington's avatar
Havoc Pennington committed
812

Manish Singh's avatar
Manish Singh committed
813
  g_object_unref (anim);
Havoc Pennington's avatar
Havoc Pennington committed
814

815 816
  priv->filename = g_strdup (filename);
  
Havoc Pennington's avatar
Havoc Pennington committed
817
  g_object_thaw_notify (G_OBJECT (image));
818 819
}

820 821 822
/**
 * gtk_image_set_from_pixbuf:
 * @image: a #GtkImage
823
 * @pixbuf: (allow-none): a #GdkPixbuf or %NULL
824
 *
825
 * See gtk_image_new_from_pixbuf() for details.
826
 **/
827 828 829 830
void
gtk_image_set_from_pixbuf (GtkImage  *image,
                           GdkPixbuf *pixbuf)
{
831
  GtkImagePrivate *priv;
832

833 834 835
  g_return_if_fail (GTK_IS_IMAGE (image));
  g_return_if_fail (pixbuf == NULL ||
                    GDK_IS_PIXBUF (pixbuf));
Havoc Pennington's avatar
Havoc Pennington committed
836

837 838
  priv = image->priv;

Havoc Pennington's avatar
Havoc Pennington committed
839
  g_object_freeze_notify (G_OBJECT (image));
840 841
  
  if (pixbuf)
Manish Singh's avatar
Manish Singh committed
842
    g_object_ref (pixbuf);
843

844
  gtk_image_clear (image);
845 846 847

  if (pixbuf != NULL)
    {
848
      priv->storage_type = GTK_IMAGE_PIXBUF;
849

850
      priv->data.pixbuf.pixbuf = pixbuf;
851 852 853 854 855

      gtk_image_update_size (image,
                             gdk_pixbuf_get_width (pixbuf),
                             gdk_pixbuf_get_height (pixbuf));
    }
Havoc Pennington's avatar
Havoc Pennington committed
856 857 858 859

  g_object_notify (G_OBJECT (image), "pixbuf");
  
  g_object_thaw_notify (G_OBJECT (image));
860 861
}

862 863 864 865
/**
 * gtk_image_set_from_stock:
 * @image: a #GtkImage
 * @stock_id: a stock icon name
866
 * @size: (type int): a stock icon size
867
 *
868
 * See gtk_image_new_from_stock() for details.
869
 **/
870 871 872
void
gtk_image_set_from_stock  (GtkImage       *image,
                           const gchar    *stock_id,
873
                           GtkIconSize     size)
874
{
875
  GtkImagePrivate *priv;
Havoc Pennington's avatar
Havoc Pennington committed
876
  gchar *new_id;
877

878
  g_return_if_fail (GTK_IS_IMAGE (image));
Havoc Pennington's avatar
Havoc Pennington committed
879

880 881
  priv = image->priv;

Havoc Pennington's avatar
Havoc Pennington committed
882 883
  g_object_freeze_notify (G_OBJECT (image));

884
  /* in case stock_id == priv->data.stock.stock_id */
Havoc Pennington's avatar
Havoc Pennington committed
885
  new_id = g_strdup (stock_id);
886
  
887
  gtk_image_clear (image);
888

Havoc Pennington's avatar
Havoc Pennington committed
889 890
  if (new_id)
    {
891
      priv->storage_type = GTK_IMAGE_STOCK;
892
      
893 894
      priv->data.stock.stock_id = new_id;
      priv->icon_size = size;
895 896 897 898 899 900

      /* Size is demand-computed in size request method
       * if we're a stock image, since changing the
       * style impacts the size request
       */
    }
Havoc Pennington's avatar
Havoc Pennington committed
901 902

  g_object_notify (G_OBJECT (image), "stock");
903
  g_object_notify (G_OBJECT (image), "icon-size");
Havoc Pennington's avatar
Havoc Pennington committed
904 905
  
  g_object_thaw_notify (G_OBJECT (image));
906 907
}

908 909 910 911
/**
 * gtk_image_set_from_icon_set:
 * @image: a #GtkImage
 * @icon_set: a #GtkIconSet
912
 * @size: (type int): a stock icon size
913 914 915
 *
 * See gtk_image_new_from_icon_set() for details.
 **/
916 917 918
void
gtk_image_set_from_icon_set  (GtkImage       *image,
                              GtkIconSet     *icon_set,
919
                              GtkIconSize     size)
920
{
921
  GtkImagePrivate *priv;
922

923 924
  g_return_if_fail (GTK_IS_IMAGE (image));

925 926
  priv = image->priv;

Havoc Pennington's avatar
Havoc Pennington committed
927 928
  g_object_freeze_notify (G_OBJECT (image));
  
929 930 931
  if (icon_set)
    gtk_icon_set_ref (icon_set);
  
932
  gtk_image_clear (image);
933 934 935

  if (icon_set)
    {      
936
      priv->storage_type = GTK_IMAGE_ICON_SET;
937
      
938 939
      priv->data.icon_set.icon_set = icon_set;
      priv->icon_size = size;
940 941 942 943 944

      /* Size is demand-computed in size request method
       * if we're an icon set
       */
    }
Havoc Pennington's avatar
Havoc Pennington committed
945
  
946 947
  g_object_notify (G_OBJECT (image), "icon-set");
  g_object_notify (G_OBJECT (image), "icon-size");
Havoc Pennington's avatar
Havoc Pennington committed
948 949
  
  g_object_thaw_notify (G_OBJECT (image));
950 951
}

Havoc Pennington's avatar
Havoc Pennington committed
952 953 954 955 956 957 958 959 960 961 962 963
/**
 * gtk_image_set_from_animation:
 * @image: a #GtkImage
 * @animation: the #GdkPixbufAnimation
 * 
 * Causes the #GtkImage to display the given animation (or display
 * nothing, if you set the animation to %NULL).
 **/
void
gtk_image_set_from_animation (GtkImage           *image,
                              GdkPixbufAnimation *animation)
{
964
  GtkImagePrivate *priv;
965

Havoc Pennington's avatar
Havoc Pennington committed
966 967 968
  g_return_if_fail (GTK_IS_IMAGE (image));
  g_return_if_fail (animation == NULL ||
                    GDK_IS_PIXBUF_ANIMATION (animation));
Havoc Pennington's avatar
Havoc Pennington committed
969

970 971
  priv = image->priv;

Havoc Pennington's avatar
Havoc Pennington committed
972
  g_object_freeze_notify (G_OBJECT (image));
Havoc Pennington's avatar
Havoc Pennington committed
973 974
  
  if (animation)
Manish Singh's avatar
Manish Singh committed
975
    g_object_ref (animation);
Havoc Pennington's avatar
Havoc Pennington committed
976

977
  gtk_image_clear (image);
Havoc Pennington's avatar
Havoc Pennington committed
978 979 980

  if (animation != NULL)
    {
981
      priv->storage_type = GTK_IMAGE_ANIMATION;
Havoc Pennington's avatar
Havoc Pennington committed
982

983 984 985
      priv->data.anim.anim = animation;
      priv->data.anim.frame_timeout = 0;
      priv->data.anim.iter = NULL;
Havoc Pennington's avatar
Havoc Pennington committed
986 987 988 989 990
      
      gtk_image_update_size (image,
                             gdk_pixbuf_animation_get_width (animation),
                             gdk_pixbuf_animation_get_height (animation));
    }
Havoc Pennington's avatar
Havoc Pennington committed
991

992
  g_object_notify (G_OBJECT (image), "pixbuf-animation");
Havoc Pennington's avatar
Havoc Pennington committed
993 994
  
  g_object_thaw_notify (G_OBJECT (image));
Havoc Pennington's avatar
Havoc Pennington committed
995 996
}

997 998 999 1000
/**
 * gtk_image_set_from_icon_name:
 * @image: a #GtkImage
 * @icon_name: an icon name
1001
 * @size: (type int): an icon size
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
 *
 * See gtk_image_new_from_icon_name() for details.
 * 
 * Since: 2.6
 **/
void
gtk_image_set_from_icon_name  (GtkImage       *image,
			       const gchar    *icon_name,
			       GtkIconSize     size)
{
  gchar *new_name;
1013
  GtkImagePrivate *priv;
1014

1015 1016
  g_return_if_fail (GTK_IS_IMAGE (image));

1017 1018
  priv = image->priv;

1019 1020
  g_object_freeze_notify (G_OBJECT (image));

1021
  /* in case icon_name == priv->data.name.icon_name */
1022 1023
  new_name = g_strdup (icon_name);
  
1024
  gtk_image_clear (image);
1025 1026 1027

  if (new_name)
    {
1028
      priv->storage_type = GTK_IMAGE_ICON_NAME;
1029
      
1030 1031
      priv->data.name.icon_name = new_name;
      priv->icon_size = size;
1032 1033 1034 1035 1036 1037 1038

      /* Size is demand-computed in size request method
       * if we're a icon theme image, since changing the
       * style impacts the size request
       */
    }

1039 1040
  g_object_notify (G_OBJECT (image), "icon-name");
  g_object_notify (G_OBJECT (image), "icon-size");
1041 1042 1043 1044
  
  g_object_thaw_notify (G_OBJECT (image));
}

1045 1046 1047 1048
/**
 * gtk_image_set_from_gicon:
 * @image: a #GtkImage
 * @icon: an icon
1049
 * @size: (type int): an icon size
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
 *
 * See gtk_image_new_from_gicon() for details.
 * 
 * Since: 2.14
 **/
void
gtk_image_set_from_gicon  (GtkImage       *image,
			   GIcon          *icon,
			   GtkIconSize     size)
{
1060
  GtkImagePrivate *priv;
1061

1062 1063
  g_return_if_fail (GTK_IS_IMAGE (image));

1064 1065
  priv = image->priv;

1066 1067
  g_object_freeze_notify (G_OBJECT (image));

1068
  /* in case icon == priv->data.gicon.icon */
1069 1070 1071 1072 1073 1074 1075
  if (icon)
    g_object_ref (icon);
  
  gtk_image_clear (image);

  if (icon)
    {
1076 1077 1078 1079
      priv->storage_type = GTK_IMAGE_GICON;

      priv->data.gicon.icon = icon;
      priv->icon_size = size;
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092

      /* Size is demand-computed in size request method
       * if we're a icon theme image, since changing the
       * style impacts the size request
       */
    }

  g_object_notify (G_OBJECT (image), "gicon");
  g_object_notify (G_OBJECT (image), "icon-size");
  
  g_object_thaw_notify (G_OBJECT (image));
}

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
/**
 * gtk_image_get_storage_type:
 * @image: a #GtkImage
 * 
 * Gets the type of representation being used by the #GtkImage
 * to store image data. If the #GtkImage has no image data,
 * the return value will be %GTK_IMAGE_EMPTY.
 * 
 * Return value: image representation being used
 **/
1103 1104 1105 1106 1107
GtkImageType
gtk_image_get_storage_type (GtkImage *image)
{
  g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);

1108
  return image->priv->storage_type;
1109 1110
}

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
/**
 * gtk_image_get_pixbuf:
 * @image: a #GtkImage
 *
 * Gets the #GdkPixbuf being displayed by the #GtkImage.
 * The storage type of the image must be %GTK_IMAGE_EMPTY or
 * %GTK_IMAGE_PIXBUF (see gtk_image_get_storage_type()).
 * The caller of this function does not own a reference to the
 * returned pixbuf.
 * 
1121 1122
 * Return value: (transfer none): the displayed pixbuf, or %NULL if
 * the image is empty
1123
 **/
1124 1125 1126
GdkPixbuf*
gtk_image_get_pixbuf (GtkImage *image)
{
1127
  GtkImagePrivate *priv;
1128

1129 1130
  g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);

1131 1132 1133 1134 1135 1136 1137 1138 1139
  priv = image->priv;

  g_return_val_if_fail (priv->storage_type == GTK_IMAGE_PIXBUF ||
                        priv->storage_type == GTK_IMAGE_EMPTY, NULL);

  if (priv->storage_type == GTK_IMAGE_EMPTY)
    priv->data.pixbuf.pixbuf = NULL;

  return priv->data.pixbuf.pixbuf;
1140 1141
}

1142 1143 1144
/**
 * gtk_image_get_stock:
 * @image: a #GtkImage
1145 1146
 * @stock_id: (out) (transfer none) (allow-none): place to store a
 *     stock icon name, or %NULL
1147 1148
 * @size: (out) (allow-none) (type int): place to store a stock icon
 *     size, or %NULL
1149 1150 1151 1152
 *
 * Gets the stock icon name and size being displayed by the #GtkImage.
 * The storage type of the image must be %GTK_IMAGE_EMPTY or
 * %GTK_IMAGE_STOCK (see gtk_image_get_storage_type()).
1153
 * The returned string is owned by the #GtkImage and should not
1154 1155
 * be freed.
 **/
1156 1157 1158
void
gtk_image_get_stock  (GtkImage        *image,
                      gchar          **stock_id,
1159
                      GtkIconSize     *size)
1160
{
1161
  GtkImagePrivate *priv;
1162

1163 1164
  g_return_if_fail (GTK_IS_IMAGE (image));

1165 1166 1167 1168 1169 1170 1171 1172
  priv = image->priv;

  g_return_if_fail (priv->storage_type == GTK_IMAGE_STOCK ||
                    priv->storage_type == GTK_IMAGE_EMPTY);

  if (priv->storage_type == GTK_IMAGE_EMPTY)
    priv->data.stock.stock_id = NULL;

1173
  if (stock_id)
1174
    *stock_id = priv->data.stock.stock_id;
1175 1176

  if (size)
1177
    *size = priv->icon_size;
1178 1179
}

1180 1181 1182
/**
 * gtk_image_get_icon_set:
 * @image: a #GtkImage
1183 1184
 * @icon_set: (out) (transfer none) (allow-none): location to store a
 *     #GtkIconSet, or %NULL
1185 1186
 * @size: (out) (allow-none) (type int): location to store a stock
 *     icon size, or %NULL
1187 1188 1189 1190 1191
 *
 * Gets the icon set and size being displayed by the #GtkImage.
 * The storage type of the image must be %GTK_IMAGE_EMPTY or
 * %GTK_IMAGE_ICON_SET (see gtk_image_get_storage_type()).
 **/
1192 1193 1194
void
gtk_image_get_icon_set  (GtkImage        *image,
                         GtkIconSet     **icon_set,
1195
                         GtkIconSize     *size)
1196
{
1197
  GtkImagePrivate *priv;
1198

1199
  g_return_if_fail (GTK_IS_IMAGE (image));
1200 1201 1202 1203 1204 1205 1206 1207

  priv = image->priv;

  g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_SET ||
                    priv->storage_type == GTK_IMAGE_EMPTY);

  if (icon_set)
    *icon_set = priv->data.icon_set.icon_set;
1208 1209

  if (size)
1210
    *size = priv->icon_size;
1211 1212
}

Havoc Pennington's avatar
Havoc Pennington committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
/**
 * gtk_image_get_animation:
 * @image: a #GtkImage
 *
 * Gets the #GdkPixbufAnimation being displayed by the #GtkImage.
 * The storage type of the image must be %GTK_IMAGE_EMPTY or
 * %GTK_IMAGE_ANIMATION (see gtk_image_get_storage_type()).
 * The caller of this function does not own a reference to the
 * returned animation.
 * 
1223 1224
 * Return value: (transfer none): the displayed animation, or %NULL if
 * the image is empty
Havoc Pennington's avatar
Havoc Pennington committed
1225 1226 1227 1228
 **/
GdkPixbufAnimation*
gtk_image_get_animation (GtkImage *image)
{
1229
  GtkImagePrivate *priv;
1230

Havoc Pennington's avatar
Havoc Pennington committed
1231
  g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
1232 1233 1234 1235 1236

  priv = image->priv;

  g_return_val_if_fail (priv->storage_type == GTK_IMAGE_ANIMATION ||
                        priv->storage_type == GTK_IMAGE_EMPTY,
Havoc Pennington's avatar
Havoc Pennington committed
1237 1238
                        NULL);

1239 1240 1241 1242
  if (priv->storage_type == GTK_IMAGE_EMPTY)
    priv->data.anim.anim = NULL;

  return priv->data.anim.anim;
Havoc Pennington's avatar
Havoc Pennington committed
1243 1244
}

1245 1246 1247
/**
 * gtk_image_get_icon_name:
 * @image: a #GtkImage
1248 1249
 * @icon_name: (out) (transfer none) (allow-none): place to store an
 *     icon name, or %NULL
1250 1251
 * @size: (out) (allow-none) (type int): place to store an icon size,
 *     or %NULL
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
 *
 * Gets the icon name and size being displayed by the #GtkImage.
 * The storage type of the image must be %GTK_IMAGE_EMPTY or
 * %GTK_IMAGE_ICON_NAME (see gtk_image_get_storage_type()).
 * The returned string is owned by the #GtkImage and should not
 * be freed.
 * 
 * Since: 2.6
 **/
void
1262 1263 1264
gtk_image_get_icon_name  (GtkImage              *image,
			  G_CONST_RETURN gchar **icon_name,
			  GtkIconSize           *size)
1265
{
1266
  GtkImagePrivate *priv;
1267

1268 1269
  g_return_if_fail (GTK_IS_IMAGE (image));

1270 1271 1272 1273 1274 1275 1276 1277
  priv = image->priv;

  g_return_if_fail (priv->storage_type == GTK_IMAGE_ICON_NAME ||
                    priv->storage_type == GTK_IMAGE_EMPTY);

  if (priv->storage_type == GTK_IMAGE_EMPTY)
    priv->data.name.icon_name = NULL;

1278
  if (icon_name)
1279
    *icon_name = priv->data.name.icon_name;
1280 1281

  if (size)
1282
    *size = priv->icon_size;
1283 1284
}

1285 1286 1287
/**
 * gtk_image_get_gicon:
 * @image: a #GtkImage
1288 1289
 * @gicon: (out) (transfer none) (allow-none): place to store a
 *     #GIcon, or %NULL
1290 1291
 * @size: (out) (allow-none) (type int): place to store an icon size,
 *     or %NULL
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
 *
 * Gets the #GIcon and size being displayed by the #GtkImage.
 * The storage type of the image must be %GTK_IMAGE_EMPTY or
 * %GTK_IMAGE_GICON (see gtk_image_get_storage_type()).
 * The caller of this function does not own a reference to the
 * returned #GIcon.
 * 
 * Since: 2.14
 **/
void
gtk_image_get_gicon (GtkImage     *image,
		     GIcon       **gicon,
		     GtkIconSize  *size)
{
1306
  GtkImagePrivate *priv;
1307

1308 1309
  g_return_if_fail (GTK_IS_IMAGE (image));

1310 1311 1312 1313 1314 1315 1316 1317
  priv = image->priv;

  g_return_if_fail (priv->storage_type == GTK_IMAGE_GICON ||
                    priv->storage_type == GTK_IMAGE_EMPTY);

  if (priv->storage_type == GTK_IMAGE_EMPTY)
    priv->data.gicon.icon = NULL;

1318
  if (gicon)
1319
    *gicon = priv->data.gicon.icon;
1320 1321

  if (size)
1322
    *size = priv->icon_size;
1323 1324
}

1325 1326 1327 1328 1329 1330 1331
/**
 * gtk_image_new:
 * 
 * Creates a new empty #GtkImage widget.
 * 
 * Return value: a newly created #GtkImage widget. 
 **/
1332
GtkWidget*
1333
gtk_image_new (void)
1334
{
1335
  return g_object_new (GTK_TYPE_IMAGE, NULL);
1336 1337
}

Havoc Pennington's avatar
Havoc Pennington committed
1338
static void
1339
gtk_image_reset_anim_iter (GtkImage *image)
Havoc Pennington's avatar
Havoc Pennington committed
1340
{
1341
  GtkImagePrivate *priv = image->priv;
1342 1343

  if (priv->storage_type == GTK_IMAGE_ANIMATION)
Havoc Pennington's avatar
Havoc Pennington committed
1344 1345 1346
    {
      /* Reset the animation */
      
1347
      if (priv->data.anim.frame_timeout)
Havoc Pennington's avatar
Havoc Pennington committed
1348
        {
1349 1350
          g_source_remove (priv->data.anim.frame_timeout);
          priv->data.anim.frame_timeout = 0;
Havoc Pennington's avatar
Havoc Pennington committed
1351 1352
        }

1353
      if (priv->data.anim.iter)
Havoc Pennington's avatar
Havoc Pennington committed
1354
        {
1355 1356
          g_object_unref (priv->data.anim.iter);
          priv->data.anim.iter = NULL;
Havoc Pennington's avatar
Havoc Pennington committed
1357 1358
        }
    }
1359 1360 1361 1362 1363 1364
}

static void
gtk_image_unmap (GtkWidget *widget)
{
  gtk_image_reset_anim_iter (GTK_IMAGE (widget));
Havoc Pennington's avatar
Havoc Pennington committed
1365

1366
  GTK_WIDGET_CLASS (gtk_image_parent_class)->unmap (widget);
Havoc Pennington's avatar
Havoc Pennington committed
1367 1368
}

1369 1370 1371 1372 1373
static void
gtk_image_unrealize (GtkWidget *widget)
{
  gtk_image_reset_anim_iter (GTK_IMAGE (widget));

1374
  GTK_WIDGET_CLASS (gtk_image_parent_class)->unrealize (widget);
1375 1376
}

Mark McLoughlin's avatar
Mark McLoughlin committed
1377
static gint
Havoc Pennington's avatar
Havoc Pennington committed
1378 1379
animation_timeout (gpointer data)
{
1380
  GtkImage *image = GTK_IMAGE (data);
1381
  GtkImagePrivate *priv = image->priv;
1382
  int delay;
Havoc Pennington's avatar
Havoc Pennington committed
1383

1384
  priv->data.anim.frame_timeout = 0;
Havoc Pennington's avatar
Havoc Pennington committed
1385

1386
  gdk_pixbuf_animation_iter_advance (priv->data.anim.iter, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
1387

1388
  delay = gdk_pixbuf_animation_iter_get_delay_time (priv->data.anim.iter);
1389 1390
  if (delay >= 0)
    {
1391 1392
      GtkWidget *widget = GTK_WIDGET (image);

1393
      priv->data.anim.frame_timeout =
1394
        gdk_threads_add_timeout (delay, animation_timeout, image);