gtkimage.c 60.5 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * 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
#include <math.h>
29 30
#include <string.h>

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

40 41 42 43
typedef struct _GtkImagePrivate GtkImagePrivate;

struct _GtkImagePrivate
{
44 45 46
  /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
  gchar *filename;

47 48 49 50
  gint pixel_size;
};

#define GTK_IMAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_IMAGE, GtkImagePrivate))
51 52


Havoc Pennington's avatar
Havoc Pennington committed
53 54
#define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON

55 56 57 58
static void gtk_image_class_init   (GtkImageClass  *klass);
static void gtk_image_init         (GtkImage       *image);
static gint gtk_image_expose       (GtkWidget      *widget,
                                    GdkEventExpose *event);
Havoc Pennington's avatar
Havoc Pennington committed
59
static void gtk_image_unmap        (GtkWidget      *widget);
60
static void gtk_image_unrealize    (GtkWidget      *widget);
61 62
static void gtk_image_size_request (GtkWidget      *widget,
                                    GtkRequisition *requisition);
63 64
static void gtk_image_style_set    (GtkWidget      *widget,
				    GtkStyle       *prev_style);
65 66
static void gtk_image_screen_changed (GtkWidget    *widget,
				      GdkScreen    *prev_screen);
67 68
static void gtk_image_destroy      (GtkObject      *object);
static void gtk_image_reset        (GtkImage       *image);
69 70
static void gtk_image_calc_size    (GtkImage       *image);

71 72 73
static void gtk_image_update_size  (GtkImage       *image,
                                    gint            image_width,
                                    gint            image_height);
Elliot Lee's avatar
Elliot Lee committed
74

Havoc Pennington's avatar
Havoc Pennington committed
75 76 77 78 79 80 81 82 83
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);

84 85
static void icon_theme_changed          (GtkImage         *image);

86
static gpointer parent_class;
Elliot Lee's avatar
Elliot Lee committed
87

Havoc Pennington's avatar
Havoc Pennington committed
88 89 90 91 92 93 94 95 96 97 98
enum
{
  PROP_0,
  PROP_PIXBUF,
  PROP_PIXMAP,
  PROP_IMAGE,
  PROP_MASK,
  PROP_FILE,
  PROP_STOCK,
  PROP_ICON_SET,
  PROP_ICON_SIZE,
99
  PROP_PIXEL_SIZE,
Havoc Pennington's avatar
Havoc Pennington committed
100
  PROP_PIXBUF_ANIMATION,
101
  PROP_ICON_NAME,
Havoc Pennington's avatar
Havoc Pennington committed
102 103 104
  PROP_STORAGE_TYPE
};

Manish Singh's avatar
Manish Singh committed
105
GType
106
gtk_image_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
107
{
Manish Singh's avatar
Manish Singh committed
108
  static GType image_type = 0;
Elliot Lee's avatar
Elliot Lee committed
109 110 111

  if (!image_type)
    {
Manish Singh's avatar
Manish Singh committed
112
      static const GTypeInfo image_info =
Elliot Lee's avatar
Elliot Lee committed
113 114
      {
	sizeof (GtkImageClass),
Manish Singh's avatar
Manish Singh committed
115 116 117 118 119 120 121 122
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_image_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkImage),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_image_init,
Elliot Lee's avatar
Elliot Lee committed
123 124
      };

Manish Singh's avatar
Manish Singh committed
125 126
      image_type = g_type_register_static (GTK_TYPE_MISC, "GtkImage",
					   &image_info, 0);
Elliot Lee's avatar
Elliot Lee committed
127 128 129 130 131 132 133 134
    }

  return image_type;
}

static void
gtk_image_class_init (GtkImageClass *class)
{
Havoc Pennington's avatar
Havoc Pennington committed
135
  GObjectClass *gobject_class;
136
  GtkObjectClass *object_class;
Elliot Lee's avatar
Elliot Lee committed
137 138
  GtkWidgetClass *widget_class;

139
  parent_class = g_type_class_peek_parent (class);
140

Havoc Pennington's avatar
Havoc Pennington committed
141 142 143 144 145 146
  gobject_class = G_OBJECT_CLASS (class);
  
  gobject_class->set_property = gtk_image_set_property;
  gobject_class->get_property = gtk_image_get_property;
  
  object_class = GTK_OBJECT_CLASS (class);
147
  
148 149
  object_class->destroy = gtk_image_destroy;

Havoc Pennington's avatar
Havoc Pennington committed
150 151
  widget_class = GTK_WIDGET_CLASS (class);
  
Elliot Lee's avatar
Elliot Lee committed
152
  widget_class->expose_event = gtk_image_expose;
153
  widget_class->size_request = gtk_image_size_request;
Havoc Pennington's avatar
Havoc Pennington committed
154
  widget_class->unmap = gtk_image_unmap;
155
  widget_class->unrealize = gtk_image_unrealize;
156
  widget_class->style_set = gtk_image_style_set;
157
  widget_class->screen_changed = gtk_image_screen_changed;
Havoc Pennington's avatar
Havoc Pennington committed
158 159 160 161
  
  g_object_class_install_property (gobject_class,
                                   PROP_PIXBUF,
                                   g_param_spec_object ("pixbuf",
162 163
                                                        P_("Pixbuf"),
                                                        P_("A GdkPixbuf to display"),
Havoc Pennington's avatar
Havoc Pennington committed
164
                                                        GDK_TYPE_PIXBUF,
165
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
166 167 168 169

  g_object_class_install_property (gobject_class,
                                   PROP_PIXMAP,
                                   g_param_spec_object ("pixmap",
170 171
                                                        P_("Pixmap"),
                                                        P_("A GdkPixmap to display"),
Havoc Pennington's avatar
Havoc Pennington committed
172
                                                        GDK_TYPE_PIXMAP,
173
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
174 175 176 177

  g_object_class_install_property (gobject_class,
                                   PROP_IMAGE,
                                   g_param_spec_object ("image",
178 179
                                                        P_("Image"),
                                                        P_("A GdkImage to display"),
Havoc Pennington's avatar
Havoc Pennington committed
180
                                                        GDK_TYPE_IMAGE,
181
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
182 183 184 185

  g_object_class_install_property (gobject_class,
                                   PROP_MASK,
                                   g_param_spec_object ("mask",
186 187
                                                        P_("Mask"),
                                                        P_("Mask bitmap to use with GdkImage or GdkPixmap"),
Havoc Pennington's avatar
Havoc Pennington committed
188
                                                        GDK_TYPE_PIXMAP,
189
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
190 191 192 193
  
  g_object_class_install_property (gobject_class,
                                   PROP_FILE,
                                   g_param_spec_string ("file",
194 195
                                                        P_("Filename"),
                                                        P_("Filename to load and display"),
Havoc Pennington's avatar
Havoc Pennington committed
196
                                                        NULL,
197
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
198 199 200 201 202
  

  g_object_class_install_property (gobject_class,
                                   PROP_STOCK,
                                   g_param_spec_string ("stock",
203 204
                                                        P_("Stock ID"),
                                                        P_("Stock ID for a stock image to display"),
Havoc Pennington's avatar
Havoc Pennington committed
205
                                                        NULL,
206
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
207 208 209
  
  g_object_class_install_property (gobject_class,
                                   PROP_ICON_SET,
210
                                   g_param_spec_boxed ("icon-set",
211 212
                                                       P_("Icon set"),
                                                       P_("Icon set to display"),
Havoc Pennington's avatar
Havoc Pennington committed
213
                                                       GTK_TYPE_ICON_SET,
214
                                                       GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
215 216 217
  
  g_object_class_install_property (gobject_class,
                                   PROP_ICON_SIZE,
218
                                   g_param_spec_int ("icon-size",
219
                                                     P_("Icon size"),
220
                                                     P_("Symbolic size to use for stock icon, icon set or named icon"),
Havoc Pennington's avatar
Havoc Pennington committed
221 222
                                                     0, G_MAXINT,
                                                     DEFAULT_ICON_SIZE,
223
                                                     GTK_PARAM_READWRITE));
224 225 226 227 228 229 230 231 232 233 234
  /**
   * GtkImage:pixel-size:
   *
   * The :pixel-size property can be used to specify a fixed size
   * overriding the :icon-size property for images of type 
   * %GTK_IMAGE_ICON_NAME. 
   *
   * Since: 2.6
   */
  g_object_class_install_property (gobject_class,
				   PROP_PIXEL_SIZE,
235
				   g_param_spec_int ("pixel-size",
236 237 238 239
						     P_("Pixel size"),
						     P_("Pixel size to use for named icon"),
						     -1, G_MAXINT,
						     -1,
240
						     GTK_PARAM_READWRITE));
241
  
Havoc Pennington's avatar
Havoc Pennington committed
242 243
  g_object_class_install_property (gobject_class,
                                   PROP_PIXBUF_ANIMATION,
244
                                   g_param_spec_object ("pixbuf-animation",
245 246
                                                        P_("Animation"),
                                                        P_("GdkPixbufAnimation to display"),
Havoc Pennington's avatar
Havoc Pennington committed
247
                                                        GDK_TYPE_PIXBUF_ANIMATION,
248
                                                        GTK_PARAM_READWRITE));
249 250 251 252 253 254 255 256 257 258 259

  /**
   * GtkImage:icon-name:
   *
   * The name of the icon in the icon theme.  If the icon theme is
   * changed, the image will be updated automatically.
   *
   * Since: 2.6
   */
  g_object_class_install_property (gobject_class,
                                   PROP_ICON_NAME,
260
                                   g_param_spec_string ("icon-name",
261 262 263
                                                        P_("Icon Name"),
                                                        P_("The name of the icon from the icon theme"),
                                                        NULL,
264
                                                        GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
265 266 267
  
  g_object_class_install_property (gobject_class,
                                   PROP_STORAGE_TYPE,
Matthias Clasen's avatar
Matthias Clasen committed
268
                                   g_param_spec_enum ("storage-type",
269 270
                                                      P_("Storage type"),
                                                      P_("The representation being used for image data"),
Havoc Pennington's avatar
Havoc Pennington committed
271 272
                                                      GTK_TYPE_IMAGE_TYPE,
                                                      GTK_IMAGE_EMPTY,
273
                                                      GTK_PARAM_READABLE));
274 275

  g_type_class_add_private (object_class, sizeof (GtkImagePrivate));
Elliot Lee's avatar
Elliot Lee committed
276 277 278 279 280
}

static void
gtk_image_init (GtkImage *image)
{
281 282
  GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);

Elliot Lee's avatar
Elliot Lee committed
283 284
  GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);

285
  image->storage_type = GTK_IMAGE_EMPTY;
Havoc Pennington's avatar
Havoc Pennington committed
286 287
  image->icon_size = DEFAULT_ICON_SIZE;
  image->mask = NULL;
288 289

  priv->pixel_size = -1;
290 291

  priv->filename = NULL;
Elliot Lee's avatar
Elliot Lee committed
292 293
}

294 295 296 297 298 299 300 301 302 303
static void
gtk_image_destroy (GtkObject *object)
{
  GtkImage *image = GTK_IMAGE (object);

  gtk_image_clear (image);
  
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

Havoc Pennington's avatar
Havoc Pennington committed
304 305 306 307 308 309 310
static void 
gtk_image_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
			GParamSpec   *pspec)
{
  GtkImage *image;
311
  GtkImagePrivate *priv;
Havoc Pennington's avatar
Havoc Pennington committed
312 313

  image = GTK_IMAGE (object);
314
  priv = GTK_IMAGE_GET_PRIVATE (image);
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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  
  switch (prop_id)
    {
    case PROP_PIXBUF:
      gtk_image_set_from_pixbuf (image,
                                 g_value_get_object (value));
      break;
    case PROP_PIXMAP:
      gtk_image_set_from_pixmap (image,
                                 g_value_get_object (value),
                                 image->mask);
      break;
    case PROP_IMAGE:
      gtk_image_set_from_image (image,
                                g_value_get_object (value),
                                image->mask);
      break;
    case PROP_MASK:
      if (image->storage_type == GTK_IMAGE_PIXMAP)
        gtk_image_set_from_pixmap (image,
                                   image->data.pixmap.pixmap,
                                   g_value_get_object (value));
      else if (image->storage_type == GTK_IMAGE_IMAGE)
        gtk_image_set_from_image (image,
                                  image->data.image.image,
                                  g_value_get_object (value));
      else
        {
          GdkBitmap *mask;

          mask = g_value_get_object (value);

          if (mask)
Manish Singh's avatar
Manish Singh committed
348
            g_object_ref (mask);
Havoc Pennington's avatar
Havoc Pennington committed
349 350 351 352 353 354 355
          
          gtk_image_reset (image);

          image->mask = mask;
        }
      break;
    case PROP_FILE:
356
      gtk_image_set_from_file (image, g_value_get_string (value));
Havoc Pennington's avatar
Havoc Pennington committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
      break;
    case PROP_STOCK:
      gtk_image_set_from_stock (image, g_value_get_string (value),
                                image->icon_size);
      break;
    case PROP_ICON_SET:
      gtk_image_set_from_icon_set (image, g_value_get_boxed (value),
                                   image->icon_size);
      break;
    case PROP_ICON_SIZE:
      if (image->storage_type == GTK_IMAGE_STOCK)
        gtk_image_set_from_stock (image,
                                  image->data.stock.stock_id,
                                  g_value_get_int (value));
      else if (image->storage_type == GTK_IMAGE_ICON_SET)
        gtk_image_set_from_icon_set (image,
                                     image->data.icon_set.icon_set,
                                     g_value_get_int (value));
375 376 377 378
      else if (image->storage_type == GTK_IMAGE_ICON_NAME)
        gtk_image_set_from_icon_name (image,
				      image->data.name.icon_name,
				      g_value_get_int (value));
Havoc Pennington's avatar
Havoc Pennington committed
379 380 381 382
      else
        /* Save to be used when STOCK or ICON_SET property comes in */
        image->icon_size = g_value_get_int (value);
      break;
383
    case PROP_PIXEL_SIZE:
384
      gtk_image_set_pixel_size (image, g_value_get_int (value));
385
      break;
Havoc Pennington's avatar
Havoc Pennington committed
386 387 388 389
    case PROP_PIXBUF_ANIMATION:
      gtk_image_set_from_animation (image,
                                    g_value_get_object (value));
      break;
390 391 392
    case PROP_ICON_NAME:
      gtk_image_set_from_icon_name (image, g_value_get_string (value),
				    image->icon_size);
393 394
      break;

Havoc Pennington's avatar
Havoc Pennington committed
395 396 397 398 399 400 401 402 403 404 405 406 407
    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)
{
  GtkImage *image;
408
  GtkImagePrivate *priv;
Havoc Pennington's avatar
Havoc Pennington committed
409 410

  image = GTK_IMAGE (object);
411
  priv = GTK_IMAGE_GET_PRIVATE (image);
Havoc Pennington's avatar
Havoc Pennington committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

  /* 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:
      if (image->storage_type != GTK_IMAGE_PIXBUF)
        g_value_set_object (value, NULL);
      else
        g_value_set_object (value,
                            gtk_image_get_pixbuf (image));
      break;
    case PROP_PIXMAP:
      if (image->storage_type != GTK_IMAGE_PIXMAP)
        g_value_set_object (value, NULL);
      else
        g_value_set_object (value,
                            image->data.pixmap.pixmap);
      break;
    case PROP_MASK:
      g_value_set_object (value, image->mask);
      break;
    case PROP_IMAGE:
      if (image->storage_type != GTK_IMAGE_IMAGE)
        g_value_set_object (value, NULL);
      else
        g_value_set_object (value,
                            image->data.image.image);
      break;
445 446 447
    case PROP_FILE:
      g_value_set_string (value, priv->filename);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
    case PROP_STOCK:
      if (image->storage_type != GTK_IMAGE_STOCK)
        g_value_set_string (value, NULL);
      else
        g_value_set_string (value,
                            image->data.stock.stock_id);
      break;
    case PROP_ICON_SET:
      if (image->storage_type != GTK_IMAGE_ICON_SET)
        g_value_set_boxed (value, NULL);
      else
        g_value_set_boxed (value,
                           image->data.icon_set.icon_set);
      break;      
    case PROP_ICON_SIZE:
      g_value_set_int (value, image->icon_size);
      break;
465 466 467
    case PROP_PIXEL_SIZE:
      g_value_set_int (value, priv->pixel_size);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
468 469 470 471 472 473 474
    case PROP_PIXBUF_ANIMATION:
      if (image->storage_type != GTK_IMAGE_ANIMATION)
        g_value_set_object (value, NULL);
      else
        g_value_set_object (value,
                            image->data.anim.anim);
      break;
475 476
    case PROP_ICON_NAME:
      if (image->storage_type != GTK_IMAGE_ICON_NAME)
477
	g_value_set_string (value, NULL);
478 479 480 481
      else
	g_value_set_string (value,
			    image->data.name.icon_name);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
482 483 484 485 486 487 488 489 490 491
    case PROP_STORAGE_TYPE:
      g_value_set_enum (value, image->storage_type);
      break;
      
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

492

493 494 495 496 497 498
/**
 * gtk_image_new_from_pixmap:
 * @pixmap: a #GdkPixmap, or %NULL
 * @mask: a #GdkBitmap, or %NULL
 * 
 * Creates a #GtkImage widget displaying @pixmap with a @mask.
Matthias Clasen's avatar
Matthias Clasen committed
499
 * A #GdkPixmap is a server-side image buffer in the pixel format of the
500 501 502 503 504 505
 * current display. The #GtkImage does not assume a reference to the
 * pixmap or mask; you still need to unref them if you own references.
 * #GtkImage will add its own reference rather than adopting yours.
 * 
 * Return value: a new #GtkImage
 **/
Elliot Lee's avatar
Elliot Lee committed
506
GtkWidget*
507 508
gtk_image_new_from_pixmap (GdkPixmap *pixmap,
                           GdkBitmap *mask)
Elliot Lee's avatar
Elliot Lee committed
509 510 511
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
512
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
513 514 515 516 517 518

  gtk_image_set_from_pixmap (image, pixmap, mask);

  return GTK_WIDGET (image);
}

519 520 521 522 523 524 525 526 527 528 529 530 531 532
/**
 * gtk_image_new_from_image:
 * @image: a #GdkImage, or %NULL
 * @mask: a #GdkBitmap, or %NULL 
 * 
 * Creates a #GtkImage widget displaying a @image with a @mask.
 * A #GdkImage is a client-side image buffer in the pixel format of the
 * current display.
 * The #GtkImage does not assume a reference to the
 * image or mask; you still need to unref them if you own references.
 * #GtkImage will add its own reference rather than adopting yours.
 * 
 * Return value: a new #GtkImage
 **/
533 534 535 536 537
GtkWidget*
gtk_image_new_from_image  (GdkImage  *gdk_image,
                           GdkBitmap *mask)
{
  GtkImage *image;
Elliot Lee's avatar
Elliot Lee committed
538

Manish Singh's avatar
Manish Singh committed
539
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
Elliot Lee's avatar
Elliot Lee committed
540

541 542 543 544 545
  gtk_image_set_from_image (image, gdk_image, mask);

  return GTK_WIDGET (image);
}

546 547 548 549
/**
 * gtk_image_new_from_file:
 * @filename: a filename
 * 
Havoc Pennington's avatar
Havoc Pennington committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
 * 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.
566 567 568
 * 
 * Return value: a new #GtkImage
 **/
569 570 571 572 573
GtkWidget*
gtk_image_new_from_file   (const gchar *filename)
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
574
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
575 576 577 578 579 580

  gtk_image_set_from_file (image, filename);

  return GTK_WIDGET (image);
}

581 582 583 584 585 586 587 588 589
/**
 * gtk_image_new_from_pixbuf:
 * @pixbuf: a #GdkPixbuf, or %NULL
 * 
 * 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.
 * 
590 591
 * 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
Havoc Pennington's avatar
Havoc Pennington committed
592
 * should use gtk_image_new_from_icon_set().
593
 * 
594 595
 * Return value: a new #GtkImage
 **/
596 597 598 599 600
GtkWidget*
gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
601
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
602 603 604 605 606 607

  gtk_image_set_from_pixbuf (image, pixbuf);

  return GTK_WIDGET (image);  
}

608 609 610 611 612 613
/**
 * gtk_image_new_from_stock:
 * @stock_id: a stock icon name
 * @size: a stock icon size
 * 
 * Creates a #GtkImage displaying a stock icon. Sample stock icon
Havoc Pennington's avatar
Havoc Pennington committed
614
 * names are #GTK_STOCK_OPEN, #GTK_STOCK_EXIT. Sample stock sizes
615
 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
Matthias Clasen's avatar
Matthias Clasen committed
616
 * icon name isn't known, the image will be empty.
Havoc Pennington's avatar
Havoc Pennington committed
617 618
 * You can register your own stock icon names, see
 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
619 620 621
 * 
 * Return value: a new #GtkImage displaying the stock icon
 **/
622 623
GtkWidget*
gtk_image_new_from_stock (const gchar    *stock_id,
624
                          GtkIconSize     size)
625 626 627
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
628
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
629 630 631 632 633 634

  gtk_image_set_from_stock (image, stock_id, size);

  return GTK_WIDGET (image);
}

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
/**
 * gtk_image_new_from_icon_set:
 * @icon_set: a #GtkIconSet
 * @size: a stock icon size
 *
 * 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
 **/
655 656
GtkWidget*
gtk_image_new_from_icon_set (GtkIconSet     *icon_set,
657
                             GtkIconSize     size)
658 659 660
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
661
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
662 663

  gtk_image_set_from_icon_set (image, icon_set, size);
Elliot Lee's avatar
Elliot Lee committed
664 665 666 667

  return GTK_WIDGET (image);
}

Havoc Pennington's avatar
Havoc Pennington committed
668 669 670 671 672 673 674 675 676 677 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.
 * 
 * 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
686
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
687 688 689 690 691 692

  gtk_image_set_from_animation (image, animation);

  return GTK_WIDGET (image);
}

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
/**
 * gtk_image_new_from_icon_name:
 * @icon_name: an icon name
 * @size: a stock icon size
 * 
 * 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);
}

720 721 722 723 724 725 726 727 728
/**
 * gtk_image_set_from_pixmap:
 * @image: a #GtkImage
 * @pixmap: a #GdkPixmap or %NULL
 * @mask: a #GdkBitmap or %NULL
 *
 * See gtk_image_new_from_pixmap() for details.
 * 
 **/
Elliot Lee's avatar
Elliot Lee committed
729
void
730 731 732 733 734 735 736 737 738
gtk_image_set_from_pixmap (GtkImage  *image,
                           GdkPixmap *pixmap,
                           GdkBitmap *mask)
{
  g_return_if_fail (GTK_IS_IMAGE (image));
  g_return_if_fail (pixmap == NULL ||
                    GDK_IS_PIXMAP (pixmap));
  g_return_if_fail (mask == NULL ||
                    GDK_IS_PIXMAP (mask));
Havoc Pennington's avatar
Havoc Pennington committed
739 740

  g_object_freeze_notify (G_OBJECT (image));
741 742
  
  if (pixmap)
Manish Singh's avatar
Manish Singh committed
743
    g_object_ref (pixmap);
744 745

  if (mask)
Manish Singh's avatar
Manish Singh committed
746
    g_object_ref (mask);
747

748
  gtk_image_reset (image);
749

Havoc Pennington's avatar
Havoc Pennington committed
750 751
  image->mask = mask;
  
752 753 754 755 756 757 758 759 760 761 762 763 764
  if (pixmap)
    {
      int width;
      int height;
      
      image->storage_type = GTK_IMAGE_PIXMAP;

      image->data.pixmap.pixmap = pixmap;

      gdk_drawable_get_size (GDK_DRAWABLE (pixmap), &width, &height);

      gtk_image_update_size (image, width, height);
    }
Havoc Pennington's avatar
Havoc Pennington committed
765 766 767 768 769

  g_object_notify (G_OBJECT (image), "pixmap");
  g_object_notify (G_OBJECT (image), "mask");
  
  g_object_thaw_notify (G_OBJECT (image));
770 771
}

772 773 774 775 776 777 778 779 780
/**
 * gtk_image_set_from_image:
 * @image: a #GtkImage
 * @gdk_image: a #GdkImage or %NULL
 * @mask: a #GdkBitmap or %NULL
 *
 * See gtk_image_new_from_image() for details.
 * 
 **/
781 782 783 784
void
gtk_image_set_from_image  (GtkImage  *image,
                           GdkImage  *gdk_image,
                           GdkBitmap *mask)
Elliot Lee's avatar
Elliot Lee committed
785 786
{
  g_return_if_fail (GTK_IS_IMAGE (image));
787 788 789 790 791
  g_return_if_fail (gdk_image == NULL ||
                    GDK_IS_IMAGE (gdk_image));
  g_return_if_fail (mask == NULL ||
                    GDK_IS_PIXMAP (mask));

Havoc Pennington's avatar
Havoc Pennington committed
792
  g_object_freeze_notify (G_OBJECT (image));
793 794
  
  if (gdk_image)
Manish Singh's avatar
Manish Singh committed
795
    g_object_ref (gdk_image);
796 797

  if (mask)
Manish Singh's avatar
Manish Singh committed
798
    g_object_ref (mask);
Elliot Lee's avatar
Elliot Lee committed
799

800
  gtk_image_reset (image);
Elliot Lee's avatar
Elliot Lee committed
801

802
  if (gdk_image)
Elliot Lee's avatar
Elliot Lee committed
803
    {
804 805 806
      image->storage_type = GTK_IMAGE_IMAGE;

      image->data.image.image = gdk_image;
Havoc Pennington's avatar
Havoc Pennington committed
807
      image->mask = mask;
808 809

      gtk_image_update_size (image, gdk_image->width, gdk_image->height);
Elliot Lee's avatar
Elliot Lee committed
810 811 812
    }
  else
    {
813 814
      /* Clean up the mask if gdk_image was NULL */
      if (mask)
Manish Singh's avatar
Manish Singh committed
815
        g_object_unref (mask);
Elliot Lee's avatar
Elliot Lee committed
816
    }
Havoc Pennington's avatar
Havoc Pennington committed
817 818 819 820 821

  g_object_notify (G_OBJECT (image), "image");
  g_object_notify (G_OBJECT (image), "mask");
  
  g_object_thaw_notify (G_OBJECT (image));
822
}
Elliot Lee's avatar
Elliot Lee committed
823

824 825 826 827 828 829 830 831
/**
 * gtk_image_set_from_file:
 * @image: a #GtkImage
 * @filename: a filename or %NULL
 *
 * See gtk_image_new_from_file() for details.
 * 
 **/
832 833 834 835
void
gtk_image_set_from_file   (GtkImage    *image,
                           const gchar *filename)
{
836
  GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);
Havoc Pennington's avatar
Havoc Pennington committed
837
  GdkPixbufAnimation *anim;
838 839
  
  g_return_if_fail (GTK_IS_IMAGE (image));
Havoc Pennington's avatar
Havoc Pennington committed
840 841

  g_object_freeze_notify (G_OBJECT (image));
842
  
843
  gtk_image_reset (image);
844 845

  if (filename == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
846
    {
847
      priv->filename = NULL;
Havoc Pennington's avatar
Havoc Pennington committed
848 849 850
      g_object_thaw_notify (G_OBJECT (image));
      return;
    }
851

Havoc Pennington's avatar
Havoc Pennington committed
852
  anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
853

Havoc Pennington's avatar
Havoc Pennington committed
854
  if (anim == NULL)
855 856 857 858
    {
      gtk_image_set_from_stock (image,
                                GTK_STOCK_MISSING_IMAGE,
                                GTK_ICON_SIZE_BUTTON);
Havoc Pennington's avatar
Havoc Pennington committed
859
      g_object_thaw_notify (G_OBJECT (image));
860 861
      return;
    }
862

Havoc Pennington's avatar
Havoc Pennington committed
863 864 865 866
  /* 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
   */
867

Havoc Pennington's avatar
Havoc Pennington committed
868
  if (gdk_pixbuf_animation_is_static_image (anim))
869 870
    gtk_image_set_from_pixbuf (image,
			       gdk_pixbuf_animation_get_static_image (anim));
Havoc Pennington's avatar
Havoc Pennington committed
871
  else
872
    gtk_image_set_from_animation (image, anim);
Havoc Pennington's avatar
Havoc Pennington committed
873

Manish Singh's avatar
Manish Singh committed
874
  g_object_unref (anim);
Havoc Pennington's avatar
Havoc Pennington committed
875

876 877
  priv->filename = g_strdup (filename);
  
Havoc Pennington's avatar
Havoc Pennington committed
878
  g_object_thaw_notify (G_OBJECT (image));
879 880
}

881 882 883 884 885 886 887 888
/**
 * gtk_image_set_from_pixbuf:
 * @image: a #GtkImage
 * @pixbuf: a #GdkPixbuf or %NULL
 *
 * See gtk_image_new_from_pixbuf() for details. 
 * 
 **/
889 890 891 892 893 894 895
void
gtk_image_set_from_pixbuf (GtkImage  *image,
                           GdkPixbuf *pixbuf)
{
  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
896 897

  g_object_freeze_notify (G_OBJECT (image));
898 899
  
  if (pixbuf)
Manish Singh's avatar
Manish Singh committed
900
    g_object_ref (pixbuf);
901

902
  gtk_image_reset (image);
903 904 905 906 907 908 909 910 911 912 913

  if (pixbuf != NULL)
    {
      image->storage_type = GTK_IMAGE_PIXBUF;

      image->data.pixbuf.pixbuf = pixbuf;

      gtk_image_update_size (image,
                             gdk_pixbuf_get_width (pixbuf),
                             gdk_pixbuf_get_height (pixbuf));
    }
Havoc Pennington's avatar
Havoc Pennington committed
914 915 916 917

  g_object_notify (G_OBJECT (image), "pixbuf");
  
  g_object_thaw_notify (G_OBJECT (image));
918 919
}

920 921 922 923 924 925
/**
 * gtk_image_set_from_stock:
 * @image: a #GtkImage
 * @stock_id: a stock icon name
 * @size: a stock icon size
 *
926
 * See gtk_image_new_from_stock() for details.
927 928
 * 
 **/
929 930 931
void
gtk_image_set_from_stock  (GtkImage       *image,
                           const gchar    *stock_id,
932
                           GtkIconSize     size)
933
{
Havoc Pennington's avatar
Havoc Pennington committed
934 935
  gchar *new_id;
  
936
  g_return_if_fail (GTK_IS_IMAGE (image));
Havoc Pennington's avatar
Havoc Pennington committed
937 938 939 940 941

  g_object_freeze_notify (G_OBJECT (image));

  /* in case stock_id == image->data.stock.stock_id */
  new_id = g_strdup (stock_id);
942
  
943
  gtk_image_reset (image);
944

Havoc Pennington's avatar
Havoc Pennington committed
945 946
  if (new_id)
    {
947 948
      image->storage_type = GTK_IMAGE_STOCK;
      
Havoc Pennington's avatar
Havoc Pennington committed
949 950
      image->data.stock.stock_id = new_id;
      image->icon_size = size;
951 952 953 954 955 956

      /* 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
957 958

  g_object_notify (G_OBJECT (image), "stock");
959
  g_object_notify (G_OBJECT (image), "icon-size");
Havoc Pennington's avatar
Havoc Pennington committed
960 961
  
  g_object_thaw_notify (G_OBJECT (image));
962 963
}

964 965 966 967 968 969 970 971 972
/**
 * gtk_image_set_from_icon_set:
 * @image: a #GtkImage
 * @icon_set: a #GtkIconSet
 * @size: a stock icon size
 *
 * See gtk_image_new_from_icon_set() for details.
 * 
 **/
973 974 975
void
gtk_image_set_from_icon_set  (GtkImage       *image,
                              GtkIconSet     *icon_set,
976
                              GtkIconSize     size)
977 978 979
{
  g_return_if_fail (GTK_IS_IMAGE (image));

Havoc Pennington's avatar
Havoc Pennington committed
980 981
  g_object_freeze_notify (G_OBJECT (image));
  
982 983 984
  if (icon_set)
    gtk_icon_set_ref (icon_set);
  
985
  gtk_image_reset (image);
986 987 988 989 990 991

  if (icon_set)
    {      
      image->storage_type = GTK_IMAGE_ICON_SET;
      
      image->data.icon_set.icon_set = icon_set;
Havoc Pennington's avatar
Havoc Pennington committed
992
      image->icon_size = size;
993 994 995 996 997

      /* Size is demand-computed in size request method
       * if we're an icon set
       */
    }
Havoc Pennington's avatar
Havoc Pennington committed
998
  
999 1000
  g_object_notify (G_OBJECT (image), "icon-set");
  g_object_notify (G_OBJECT (image), "icon-size");
Havoc Pennington's avatar
Havoc Pennington committed
1001 1002
  
  g_object_thaw_notify (G_OBJECT (image));
1003 1004
}

Havoc Pennington's avatar
Havoc Pennington committed
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
/**
 * 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)
{
  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
1020 1021

  g_object_freeze_notify (G_OBJECT (image));
Havoc Pennington's avatar
Havoc Pennington committed
1022 1023
  
  if (animation)
Manish Singh's avatar
Manish Singh committed
1024
    g_object_ref (animation);
Havoc Pennington's avatar
Havoc Pennington committed
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

  gtk_image_reset (image);

  if (animation != NULL)
    {
      image->storage_type = GTK_IMAGE_ANIMATION;

      image->data.anim.anim = animation;
      image->data.anim.frame_timeout = 0;
      image->data.anim.iter = NULL;
      
      gtk_image_update_size (image,
                             gdk_pixbuf_animation_get_width (animation),
                             gdk_pixbuf_animation_get_height (animation));
    }
Havoc Pennington's avatar
Havoc Pennington committed
1040

1041
  g_object_notify (G_OBJECT (image), "pixbuf-animation");
Havoc Pennington's avatar
Havoc Pennington committed
1042 1043
  
  g_object_thaw_notify (G_OBJECT (image));
Havoc Pennington's avatar
Havoc Pennington committed
1044 1045
}

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
/**
 * gtk_image_set_from_icon_name:
 * @image: a #GtkImage
 * @icon_name: an icon name
 * @size: an icon size
 *
 * 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;
  
  g_return_if_fail (GTK_IS_IMAGE (image));

  g_object_freeze_notify (G_OBJECT (image));

1067
  /* in case icon_name == image->data.name.icon_name */
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
  new_name = g_strdup (icon_name);
  
  gtk_image_reset (image);

  if (new_name)
    {
      image->storage_type = GTK_IMAGE_ICON_NAME;
      
      image->data.name.icon_name = new_name;
      image->icon_size = size;

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

1085 1086
  g_object_notify (G_OBJECT (image), "icon-name");
  g_object_notify (G_OBJECT (image), "icon-size");
1087 1088 1089 1090
  
  g_object_thaw_notify (G_OBJECT (image));
}

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
/**
 * 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
 **/
1101 1102 1103 1104 1105 1106 1107 1108
GtkImageType
gtk_image_get_storage_type (GtkImage *image)
{
  g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);

  return image->storage_type;
}

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
/**
 * gtk_image_get_pixmap:
 * @image: a #GtkImage
 * @pixmap: location to store the pixmap, or %NULL
 * @mask: location to store the mask, or %NULL
 *
 * Gets the pixmap and mask being displayed by the #GtkImage.
 * The storage type of the image must be %GTK_IMAGE_EMPTY or
 * %GTK_IMAGE_PIXMAP (see gtk_image_get_storage_type()).
 * The caller of this function does not own a reference to the
 * returned pixmap and mask.
 * 
 **/
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
void
gtk_image_get_pixmap (GtkImage   *image,
                      GdkPixmap **pixmap,
                      GdkBitmap **mask)
{
  g_return_if_fail (GTK_IS_IMAGE (image)); 
  g_return_if_fail (image->storage_type == GTK_IMAGE_PIXMAP ||
                    image->storage_type == GTK_IMAGE_EMPTY);
  
  if (pixmap)
    *pixmap = image->data.pixmap.pixmap;
  
  if (mask)