gtkimage.c 60 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
#include "gtkalias.h"
Elliot Lee's avatar
Elliot Lee committed
30 31
#include "gtkcontainer.h"
#include "gtkimage.h"
32
#include "gtkiconfactory.h"
33
#include "gtkstock.h"
34
#include "gtkicontheme.h"
Havoc Pennington's avatar
Havoc Pennington committed
35
#include "gtkintl.h"
Havoc Pennington's avatar
Havoc Pennington committed
36
#include <string.h>
Elliot Lee's avatar
Elliot Lee committed
37

38 39 40 41 42 43 44 45
typedef struct _GtkImagePrivate GtkImagePrivate;

struct _GtkImagePrivate
{
  gint pixel_size;
};

#define GTK_IMAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_IMAGE, GtkImagePrivate))
46 47


Havoc Pennington's avatar
Havoc Pennington committed
48 49
#define DEFAULT_ICON_SIZE GTK_ICON_SIZE_BUTTON

50 51 52 53
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
54
static void gtk_image_unmap        (GtkWidget      *widget);
55
static void gtk_image_unrealize    (GtkWidget      *widget);
56 57
static void gtk_image_size_request (GtkWidget      *widget,
                                    GtkRequisition *requisition);
58 59
static void gtk_image_style_set    (GtkWidget      *widget,
				    GtkStyle       *prev_style);
60 61
static void gtk_image_screen_changed (GtkWidget    *widget,
				      GdkScreen    *prev_screen);
62
static void gtk_image_destroy      (GtkObject      *object);
63
static void gtk_image_clear        (GtkImage       *image);
64
static void gtk_image_reset        (GtkImage       *image);
65 66
static void gtk_image_calc_size    (GtkImage       *image);

67 68 69
static void gtk_image_update_size  (GtkImage       *image,
                                    gint            image_width,
                                    gint            image_height);
Elliot Lee's avatar
Elliot Lee committed
70

Havoc Pennington's avatar
Havoc Pennington committed
71 72 73 74 75 76 77 78 79
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);

80 81
static void icon_theme_changed          (GtkImage         *image);

82
static gpointer parent_class;
Elliot Lee's avatar
Elliot Lee committed
83

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

Manish Singh's avatar
Manish Singh committed
101
GType
102
gtk_image_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
103
{
Manish Singh's avatar
Manish Singh committed
104
  static GType image_type = 0;
Elliot Lee's avatar
Elliot Lee committed
105 106 107

  if (!image_type)
    {
Manish Singh's avatar
Manish Singh committed
108
      static const GTypeInfo image_info =
Elliot Lee's avatar
Elliot Lee committed
109 110
      {
	sizeof (GtkImageClass),
Manish Singh's avatar
Manish Singh committed
111 112 113 114 115 116 117 118
	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
119 120
      };

Manish Singh's avatar
Manish Singh committed
121 122
      image_type = g_type_register_static (GTK_TYPE_MISC, "GtkImage",
					   &image_info, 0);
Elliot Lee's avatar
Elliot Lee committed
123 124 125 126 127 128 129 130
    }

  return image_type;
}

static void
gtk_image_class_init (GtkImageClass *class)
{
Havoc Pennington's avatar
Havoc Pennington committed
131
  GObjectClass *gobject_class;
132
  GtkObjectClass *object_class;
Elliot Lee's avatar
Elliot Lee committed
133 134
  GtkWidgetClass *widget_class;

135
  parent_class = g_type_class_peek_parent (class);
136

Havoc Pennington's avatar
Havoc Pennington committed
137 138 139 140 141 142
  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);
143
  
144 145
  object_class->destroy = gtk_image_destroy;

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

  g_object_class_install_property (gobject_class,
                                   PROP_PIXMAP,
                                   g_param_spec_object ("pixmap",
166 167
                                                        P_("Pixmap"),
                                                        P_("A GdkPixmap to display"),
Havoc Pennington's avatar
Havoc Pennington committed
168 169 170 171 172 173
                                                        GDK_TYPE_PIXMAP,
                                                        G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
                                   PROP_IMAGE,
                                   g_param_spec_object ("image",
174 175
                                                        P_("Image"),
                                                        P_("A GdkImage to display"),
Havoc Pennington's avatar
Havoc Pennington committed
176 177 178 179 180 181
                                                        GDK_TYPE_IMAGE,
                                                        G_PARAM_READWRITE));

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

  g_object_class_install_property (gobject_class,
                                   PROP_STOCK,
                                   g_param_spec_string ("stock",
199 200
                                                        P_("Stock ID"),
                                                        P_("Stock ID for a stock image to display"),
Havoc Pennington's avatar
Havoc Pennington committed
201 202 203 204 205
                                                        NULL,
                                                        G_PARAM_READWRITE));
  
  g_object_class_install_property (gobject_class,
                                   PROP_ICON_SET,
206
                                   g_param_spec_boxed ("icon-set",
207 208
                                                       P_("Icon set"),
                                                       P_("Icon set to display"),
Havoc Pennington's avatar
Havoc Pennington committed
209 210 211 212 213
                                                       GTK_TYPE_ICON_SET,
                                                       G_PARAM_READWRITE));
  
  g_object_class_install_property (gobject_class,
                                   PROP_ICON_SIZE,
214
                                   g_param_spec_int ("icon-size",
215
                                                     P_("Icon size"),
216
                                                     P_("Symbolic size to use for stock icon, icon set or named icon"),
Havoc Pennington's avatar
Havoc Pennington committed
217 218 219
                                                     0, G_MAXINT,
                                                     DEFAULT_ICON_SIZE,
                                                     G_PARAM_READWRITE));
220 221 222 223 224 225 226 227 228 229 230
  /**
   * 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,
231
				   g_param_spec_int ("pixel-size",
232 233 234 235 236 237
						     P_("Pixel size"),
						     P_("Pixel size to use for named icon"),
						     -1, G_MAXINT,
						     -1,
						     G_PARAM_READWRITE));
  
Havoc Pennington's avatar
Havoc Pennington committed
238 239
  g_object_class_install_property (gobject_class,
                                   PROP_PIXBUF_ANIMATION,
240
                                   g_param_spec_object ("pixbuf-animation",
241 242
                                                        P_("Animation"),
                                                        P_("GdkPixbufAnimation to display"),
Havoc Pennington's avatar
Havoc Pennington committed
243 244
                                                        GDK_TYPE_PIXBUF_ANIMATION,
                                                        G_PARAM_READWRITE));
245 246 247 248 249 250 251 252 253 254 255

  /**
   * 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,
256
                                   g_param_spec_string ("icon-name",
257 258 259 260
                                                        P_("Icon Name"),
                                                        P_("The name of the icon from the icon theme"),
                                                        NULL,
                                                        G_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
261 262 263
  
  g_object_class_install_property (gobject_class,
                                   PROP_STORAGE_TYPE,
Matthias Clasen's avatar
Matthias Clasen committed
264
                                   g_param_spec_enum ("storage-type",
265 266
                                                      P_("Storage type"),
                                                      P_("The representation being used for image data"),
Havoc Pennington's avatar
Havoc Pennington committed
267 268 269
                                                      GTK_TYPE_IMAGE_TYPE,
                                                      GTK_IMAGE_EMPTY,
                                                      G_PARAM_READABLE));
270 271

  g_type_class_add_private (object_class, sizeof (GtkImagePrivate));
Elliot Lee's avatar
Elliot Lee committed
272 273 274 275 276
}

static void
gtk_image_init (GtkImage *image)
{
277 278
  GtkImagePrivate *priv = GTK_IMAGE_GET_PRIVATE (image);

Elliot Lee's avatar
Elliot Lee committed
279 280
  GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);

281
  image->storage_type = GTK_IMAGE_EMPTY;
Havoc Pennington's avatar
Havoc Pennington committed
282 283
  image->icon_size = DEFAULT_ICON_SIZE;
  image->mask = NULL;
284 285

  priv->pixel_size = -1;
Elliot Lee's avatar
Elliot Lee committed
286 287
}

288 289 290 291 292 293 294 295 296 297
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
298 299 300 301 302 303 304
static void 
gtk_image_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
			GParamSpec   *pspec)
{
  GtkImage *image;
305
  GtkImagePrivate *priv;
Havoc Pennington's avatar
Havoc Pennington committed
306 307

  image = GTK_IMAGE (object);
308
  priv = GTK_IMAGE_GET_PRIVATE (image);
Havoc Pennington's avatar
Havoc Pennington committed
309 310 311 312 313 314 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
  
  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
342
            g_object_ref (mask);
Havoc Pennington's avatar
Havoc Pennington committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
          
          gtk_image_reset (image);

          image->mask = mask;
        }
      break;
    case PROP_FILE:
      gtk_image_set_from_file (image,
                               g_value_get_string (value));
      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));
370 371 372 373
      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
374 375 376 377
      else
        /* Save to be used when STOCK or ICON_SET property comes in */
        image->icon_size = g_value_get_int (value);
      break;
378
    case PROP_PIXEL_SIZE:
379
      gtk_image_set_pixel_size (image, g_value_get_int (value));
380
      break;
Havoc Pennington's avatar
Havoc Pennington committed
381 382 383 384
    case PROP_PIXBUF_ANIMATION:
      gtk_image_set_from_animation (image,
                                    g_value_get_object (value));
      break;
385 386 387
    case PROP_ICON_NAME:
      gtk_image_set_from_icon_name (image, g_value_get_string (value),
				    image->icon_size);
388 389
      break;

Havoc Pennington's avatar
Havoc Pennington committed
390 391 392 393 394 395 396 397 398 399 400 401 402
    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;
403
  GtkImagePrivate *priv;
Havoc Pennington's avatar
Havoc Pennington committed
404 405

  image = GTK_IMAGE (object);
406
  priv = GTK_IMAGE_GET_PRIVATE (image);
Havoc Pennington's avatar
Havoc Pennington committed
407 408 409 410 411 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 445 446 447 448 449 450 451 452 453 454 455 456

  /* 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;
    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;
457 458 459
    case PROP_PIXEL_SIZE:
      g_value_set_int (value, priv->pixel_size);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
460 461 462 463 464 465 466
    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;
467 468
    case PROP_ICON_NAME:
      if (image->storage_type != GTK_IMAGE_ICON_NAME)
469
	g_value_set_string (value, NULL);
470 471 472 473
      else
	g_value_set_string (value,
			    image->data.name.icon_name);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
474 475 476 477 478 479 480 481 482 483
    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;
    }
}

484

485 486 487 488 489 490
/**
 * 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
491
 * A #GdkPixmap is a server-side image buffer in the pixel format of the
492 493 494 495 496 497
 * 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
498
GtkWidget*
499 500
gtk_image_new_from_pixmap (GdkPixmap *pixmap,
                           GdkBitmap *mask)
Elliot Lee's avatar
Elliot Lee committed
501 502 503
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
504
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
505 506 507 508 509 510

  gtk_image_set_from_pixmap (image, pixmap, mask);

  return GTK_WIDGET (image);
}

511 512 513 514 515 516 517 518 519 520 521 522 523 524
/**
 * 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
 **/
525 526 527 528 529
GtkWidget*
gtk_image_new_from_image  (GdkImage  *gdk_image,
                           GdkBitmap *mask)
{
  GtkImage *image;
Elliot Lee's avatar
Elliot Lee committed
530

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

533 534 535 536 537
  gtk_image_set_from_image (image, gdk_image, mask);

  return GTK_WIDGET (image);
}

538 539 540 541
/**
 * gtk_image_new_from_file:
 * @filename: a filename
 * 
Havoc Pennington's avatar
Havoc Pennington committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
 * 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.
558 559 560
 * 
 * Return value: a new #GtkImage
 **/
561 562 563 564 565
GtkWidget*
gtk_image_new_from_file   (const gchar *filename)
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
566
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
567 568 569 570 571 572

  gtk_image_set_from_file (image, filename);

  return GTK_WIDGET (image);
}

573 574 575 576 577 578 579 580 581
/**
 * 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.
 * 
582 583
 * 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
584
 * should use gtk_image_new_from_icon_set().
585
 * 
586 587
 * Return value: a new #GtkImage
 **/
588 589 590 591 592
GtkWidget*
gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf)
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
593
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
594 595 596 597 598 599

  gtk_image_set_from_pixbuf (image, pixbuf);

  return GTK_WIDGET (image);  
}

600 601 602 603 604 605
/**
 * 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
606
 * names are #GTK_STOCK_OPEN, #GTK_STOCK_EXIT. Sample stock sizes
607 608
 * are #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_SMALL_TOOLBAR. If the stock
 * icon name isn't known, a "broken image" icon will be displayed instead.
609 610
 * You can register your own stock icon names, see
 * gtk_icon_factory_add_default() and gtk_icon_factory_add().
611 612 613
 * 
 * Return value: a new #GtkImage displaying the stock icon
 **/
614 615
GtkWidget*
gtk_image_new_from_stock (const gchar    *stock_id,
616
                          GtkIconSize     size)
617 618 619
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
620
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
621 622 623 624 625 626

  gtk_image_set_from_stock (image, stock_id, size);

  return GTK_WIDGET (image);
}

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
/**
 * 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
 **/
647 648
GtkWidget*
gtk_image_new_from_icon_set (GtkIconSet     *icon_set,
649
                             GtkIconSize     size)
650 651 652
{
  GtkImage *image;

Manish Singh's avatar
Manish Singh committed
653
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
654 655

  gtk_image_set_from_icon_set (image, icon_set, size);
Elliot Lee's avatar
Elliot Lee committed
656 657 658 659

  return GTK_WIDGET (image);
}

Havoc Pennington's avatar
Havoc Pennington committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
/**
 * 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
678
  image = g_object_new (GTK_TYPE_IMAGE, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
679 680 681 682 683 684

  gtk_image_set_from_animation (image, animation);

  return GTK_WIDGET (image);
}

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
/**
 * 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);
}

712 713 714 715 716 717 718 719 720
/**
 * 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
721
void
722 723 724 725 726 727 728 729 730
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
731 732

  g_object_freeze_notify (G_OBJECT (image));
733 734
  
  if (pixmap)
Manish Singh's avatar
Manish Singh committed
735
    g_object_ref (pixmap);
736 737

  if (mask)
Manish Singh's avatar
Manish Singh committed
738
    g_object_ref (mask);
739

740
  gtk_image_reset (image);
741

Havoc Pennington's avatar
Havoc Pennington committed
742 743
  image->mask = mask;
  
744 745 746 747 748 749 750 751 752 753 754 755 756
  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
757 758 759 760 761

  g_object_notify (G_OBJECT (image), "pixmap");
  g_object_notify (G_OBJECT (image), "mask");
  
  g_object_thaw_notify (G_OBJECT (image));
762 763
}

764 765 766 767 768 769 770 771 772
/**
 * 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.
 * 
 **/
773 774 775 776
void
gtk_image_set_from_image  (GtkImage  *image,
                           GdkImage  *gdk_image,
                           GdkBitmap *mask)
Elliot Lee's avatar
Elliot Lee committed
777 778
{
  g_return_if_fail (GTK_IS_IMAGE (image));
779 780 781 782 783
  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
784
  g_object_freeze_notify (G_OBJECT (image));
785 786
  
  if (gdk_image)
Manish Singh's avatar
Manish Singh committed
787
    g_object_ref (gdk_image);
788 789

  if (mask)
Manish Singh's avatar
Manish Singh committed
790
    g_object_ref (mask);
Elliot Lee's avatar
Elliot Lee committed
791

792
  gtk_image_reset (image);
Elliot Lee's avatar
Elliot Lee committed
793

794
  if (gdk_image)
Elliot Lee's avatar
Elliot Lee committed
795
    {
796 797 798
      image->storage_type = GTK_IMAGE_IMAGE;

      image->data.image.image = gdk_image;
Havoc Pennington's avatar
Havoc Pennington committed
799
      image->mask = mask;
800 801

      gtk_image_update_size (image, gdk_image->width, gdk_image->height);
Elliot Lee's avatar
Elliot Lee committed
802 803 804
    }
  else
    {
805 806
      /* Clean up the mask if gdk_image was NULL */
      if (mask)
Manish Singh's avatar
Manish Singh committed
807
        g_object_unref (mask);
Elliot Lee's avatar
Elliot Lee committed
808
    }
Havoc Pennington's avatar
Havoc Pennington committed
809 810 811 812 813

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

816 817 818 819 820 821 822 823
/**
 * gtk_image_set_from_file:
 * @image: a #GtkImage
 * @filename: a filename or %NULL
 *
 * See gtk_image_new_from_file() for details.
 * 
 **/
824 825 826 827
void
gtk_image_set_from_file   (GtkImage    *image,
                           const gchar *filename)
{
Havoc Pennington's avatar
Havoc Pennington committed
828
  GdkPixbufAnimation *anim;
829 830
  
  g_return_if_fail (GTK_IS_IMAGE (image));
Havoc Pennington's avatar
Havoc Pennington committed
831 832

  g_object_freeze_notify (G_OBJECT (image));
833
  
834
  gtk_image_reset (image);
835 836

  if (filename == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
837 838 839 840
    {
      g_object_thaw_notify (G_OBJECT (image));
      return;
    }
841
  
Havoc Pennington's avatar
Havoc Pennington committed
842
  anim = gdk_pixbuf_animation_new_from_file (filename, NULL);
843

Havoc Pennington's avatar
Havoc Pennington committed
844
  if (anim == NULL)
845 846 847 848
    {
      gtk_image_set_from_stock (image,
                                GTK_STOCK_MISSING_IMAGE,
                                GTK_ICON_SIZE_BUTTON);
Havoc Pennington's avatar
Havoc Pennington committed
849
      g_object_thaw_notify (G_OBJECT (image));
850 851
      return;
    }
852

Havoc Pennington's avatar
Havoc Pennington committed
853 854 855 856
  /* 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
   */
857

Havoc Pennington's avatar
Havoc Pennington committed
858 859 860 861 862 863 864 865 866 867
  if (gdk_pixbuf_animation_is_static_image (anim))
    {
      gtk_image_set_from_pixbuf (image,
                                 gdk_pixbuf_animation_get_static_image (anim));
    }
  else
    {
      gtk_image_set_from_animation (image, anim);
    }

Manish Singh's avatar
Manish Singh committed
868
  g_object_unref (anim);
Havoc Pennington's avatar
Havoc Pennington committed
869 870

  g_object_thaw_notify (G_OBJECT (image));
871 872
}

873 874 875 876 877 878 879 880
/**
 * gtk_image_set_from_pixbuf:
 * @image: a #GtkImage
 * @pixbuf: a #GdkPixbuf or %NULL
 *
 * See gtk_image_new_from_pixbuf() for details. 
 * 
 **/
881 882 883 884 885 886 887
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
888 889

  g_object_freeze_notify (G_OBJECT (image));
890 891
  
  if (pixbuf)
Manish Singh's avatar
Manish Singh committed
892
    g_object_ref (pixbuf);
893

894
  gtk_image_reset (image);
895 896 897 898 899 900 901 902 903 904 905

  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
906 907 908 909

  g_object_notify (G_OBJECT (image), "pixbuf");
  
  g_object_thaw_notify (G_OBJECT (image));
910 911
}

912 913 914 915 916 917
/**
 * gtk_image_set_from_stock:
 * @image: a #GtkImage
 * @stock_id: a stock icon name
 * @size: a stock icon size
 *
918
 * See gtk_image_new_from_stock() for details.
919 920
 * 
 **/
921 922 923
void
gtk_image_set_from_stock  (GtkImage       *image,
                           const gchar    *stock_id,
924
                           GtkIconSize     size)
925
{
Havoc Pennington's avatar
Havoc Pennington committed
926 927
  gchar *new_id;
  
928
  g_return_if_fail (GTK_IS_IMAGE (image));
Havoc Pennington's avatar
Havoc Pennington committed
929 930 931 932 933

  g_object_freeze_notify (G_OBJECT (image));

  /* in case stock_id == image->data.stock.stock_id */
  new_id = g_strdup (stock_id);
934
  
935
  gtk_image_reset (image);
936

Havoc Pennington's avatar
Havoc Pennington committed
937 938
  if (new_id)
    {
939 940
      image->storage_type = GTK_IMAGE_STOCK;
      
Havoc Pennington's avatar
Havoc Pennington committed
941 942
      image->data.stock.stock_id = new_id;
      image->icon_size = size;
943 944 945 946 947 948

      /* 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
949 950 951 952 953

  g_object_notify (G_OBJECT (image), "stock");
  g_object_notify (G_OBJECT (image), "icon_size");
  
  g_object_thaw_notify (G_OBJECT (image));
954 955
}

956 957 958 959 960 961 962 963 964
/**
 * 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.
 * 
 **/
965 966 967
void
gtk_image_set_from_icon_set  (GtkImage       *image,
                              GtkIconSet     *icon_set,
968
                              GtkIconSize     size)
969 970 971
{
  g_return_if_fail (GTK_IS_IMAGE (image));

Havoc Pennington's avatar
Havoc Pennington committed
972 973
  g_object_freeze_notify (G_OBJECT (image));
  
974 975 976
  if (icon_set)
    gtk_icon_set_ref (icon_set);
  
977
  gtk_image_reset (image);
978 979 980 981 982 983

  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
984
      image->icon_size = size;
985 986 987 988 989

      /* Size is demand-computed in size request method
       * if we're an icon set
       */
    }
Havoc Pennington's avatar
Havoc Pennington committed
990 991 992 993 994
  
  g_object_notify (G_OBJECT (image), "icon_set");
  g_object_notify (G_OBJECT (image), "icon_size");
  
  g_object_thaw_notify (G_OBJECT (image));
995 996
}

Havoc Pennington's avatar
Havoc Pennington committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
/**
 * 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
1012 1013

  g_object_freeze_notify (G_OBJECT (image));
Havoc Pennington's avatar
Havoc Pennington committed
1014 1015
  
  if (animation)
Manish Singh's avatar
Manish Singh committed
1016
    g_object_ref (animation);
Havoc Pennington's avatar
Havoc Pennington committed
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

  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
1032 1033 1034 1035

  g_object_notify (G_OBJECT (image), "pixbuf_animation");
  
  g_object_thaw_notify (G_OBJECT (image));
Havoc Pennington's avatar
Havoc Pennington committed
1036 1037
}

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
/**
 * 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));

1059
  /* in case icon_name == image->data.name.icon_name */
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
  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
       */
    }

  g_object_notify (G_OBJECT (image), "icon_name");
  g_object_notify (G_OBJECT (image), "icon_size");
  
  g_object_thaw_notify (G_OBJECT (image));
}

1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
/**
 * 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
 **/
1093 1094 1095 1096 1097 1098 1099 1100
GtkImageType
gtk_image_get_storage_type (GtkImage *image)
{
  g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_IMAGE_EMPTY);

  return image->storage_type;
}

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
/**
 * 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.
 * 
 **/
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
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)
Havoc Pennington's avatar
Havoc Pennington committed
1127
    *mask = image->mask;
1128 1129
}

1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
/**
 * gtk_image_get_image:
 * @image: a #GtkImage
 * @gdk_image: return location for a #GtkImage
 * @mask: return location for a #GdkBitmap
 * 
 * Gets the #GdkImage and mask being displayed by the #GtkImage.
 * The storage type of the image must be %GTK_IMAGE_EMPTY or
 * %GTK_IMAGE_IMAGE (see gtk_image_get_storage_type()).
 * The caller of this function does not own a reference to the
 * returned image and mask.
 **/
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
void
gtk_image_get_image  (GtkImage   *image,
                      GdkImage  **gdk_image,
                      GdkBitmap **mask)
{
  g_return_if_fail (GTK_IS_IMAGE (image));
  g_return_if_fail (image->storage_type == GTK_IMAGE_IMAGE ||
                    image->storage_type == GTK_IMAGE_EMPTY);

  if (gdk_image)
    *gdk_image = image->data.image.image;
  
  if (mask)
Havoc Pennington's avatar
Havoc Pennington committed
1155
    *mask = image->mask;
1156 1157
}

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
/**
 * 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.
 * 
 * Return value: the displayed pixbuf, or %NULL if the image is empty
 **/
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
GdkPixbuf*
gtk_image_get_pixbuf (GtkImage *image)
{
  g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
  g_return_val_if_fail (image->storage_type == GTK_IMAGE_PIXBUF ||
                        image->storage_type == GTK_IMAGE_EMPTY, NULL);

  if (image->storage_type == GTK_IMAGE_EMPTY)
    image->data.pixbuf.pixbuf = NULL;
  
  return image->data.pixbuf.pixbuf;
}

1184 1185 1186 1187 1188 1189 1190 1191 1192
/**
 * gtk_image_get_stock:
 * @image: a #GtkImage
 * @stock_id: place to store a stock icon name
 * @size: place to store a stock icon size
 *
 * 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()).
1193
 * The returned string is owned by the #GtkImage and should not
1194 1195 1196
 * be freed.
 * 
 **/
1197 1198 1199
void
gtk_image_get_stock  (GtkImage        *image,
                      gchar          **stock_id,
1200
                      GtkIconSize     *size)
1201 1202 1203 1204 1205 1206 1207 1208 1209
{
  g_return_if_fail (GTK_IS_IMAGE (image));
  g_return_if_fail (image->storage_type == GTK_IMAGE_STOCK ||
                    image->storage_type == GTK_IMAGE_EMPTY);

  if (image->storage_type == GTK_IMAGE_EMPTY)
    image->data.stock.stock_id = NULL;
  
  if (stock_id)
1210
    *stock_id = image->data.stock.stock_id;
1211 1212

  if (size)
Havoc Pennington's avatar
Havoc Pennington committed
1213
    *size = image->icon_size;
1214 1215
}

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
/**
 * gtk_image_get_icon_set:
 * @image: a #GtkImage
 * @icon_set: location to store a #GtkIconSet
 * @size: location to store a stock icon size
 *
 * 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()).
 * 
 **/
1227 1228 1229
void
gtk_image_get_icon_set  (GtkImage        *image,
                         GtkIconSet     **icon_set,
1230
                         GtkIconSize     *size)
1231 1232 1233 1234 1235 1236 1237 1238 1239
{
  g_return_if_fail (GTK_IS_IMAGE (image));
  g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_SET ||
                    image->storage_type == GTK_IMAGE_EMPTY);
      
  if (icon_set)    
    *icon_set = image->data.icon_set.icon_set;

  if (size)
Havoc Pennington's avatar
Havoc Pennington committed
1240
    *size = image->icon_size;
1241 1242
}

Havoc Pennington's avatar
Havoc Pennington committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
/**
 * 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.
 * 
 * Return value: the displayed animation, or %NULL if the image is empty
 **/
GdkPixbufAnimation*
gtk_image_get_animation (GtkImage *image)
{
  g_return_val_if_fail (GTK_IS_IMAGE (image), NULL);
  g_return_val_if_fail (image->storage_type == GTK_IMAGE_ANIMATION ||
                        image->storage_type == GTK_IMAGE_EMPTY,
                        NULL);

  if (image->storage_type == GTK_IMAGE_EMPTY)
    image->data.anim.anim = NULL;
  
  return image->data.anim.anim;
}

1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
/**
 * gtk_image_get_icon_name:
 * @image: a #GtkImage
 * @icon_name: place to store an icon name
 * @size: place to store an icon size
 *
 * 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
1285 1286 1287
gtk_image_get_icon_name  (GtkImage              *image,
			  G_CONST_RETURN gchar **icon_name,
			  GtkIconSize           *size)
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
{
  g_return_if_fail (GTK_IS_IMAGE (image));
  g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME ||
                    image->storage_type == GTK_IMAGE_EMPTY);

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

  if (size)
    *size = image->icon_size;
}

1303 1304 1305 1306 1307 1308 1309
/**
 * gtk_image_new:
 * 
 * Creates a new empty #GtkImage widget.
 * 
 * Return value: a newly created #GtkImage widget. 
 **/
1310
GtkWidget*
1311
gtk_image_new (void)
1312
{
1313
  return g_object_new (GTK_TYPE_IMAGE, NULL);
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
}

void
gtk_image_set (GtkImage  *image,
	       GdkImage  *val,
	       GdkBitmap *mask)
{
  g_return_if_fail (GTK_IS_IMAGE (image));

  gtk_image_set_from_image (image, val, mask);
Elliot Lee's avatar
Elliot Lee committed
1324 1325 1326 1327 1328 1329 1330 1331 1332
}

void
gtk_image_get (GtkImage   *image,
	       GdkImage  **val,
	       GdkBitmap **mask)
{
  g_return_if_fail (GTK_IS_IMAGE (image));

1333
  gtk_image_get_image (image, val, mask);
Elliot Lee's avatar
Elliot Lee committed
1334 1335
}

Havoc Pennington's avatar
Havoc Pennington committed
1336
static void
1337
gtk_image_reset_anim_iter (GtkImage *image)
Havoc Pennington's avatar
Havoc Pennington committed
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
{
  if (image->storage_type == GTK_IMAGE_ANIMATION)
    {
      /* Reset the animation */
      
      if (image->data.anim.frame_timeout)
        {
          g_source_remove (image->data.anim.frame_timeout);
          image->data.anim.frame_timeout = 0;
        }

      if (image->data.anim.iter)
        {
Manish Singh's avatar
Manish Singh committed
1351
          g_object_unref (image->data.anim.iter);
Havoc Pennington's avatar
Havoc Pennington committed
1352 1353 1354
          image->data.anim.iter = NULL;
        }
    }
1355 1356 1357 1358 1359 1360
}

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

  if (GTK_WIDGET_CLASS (parent_class)->unmap)
    GTK_WIDGET_CLASS (parent_class)->unmap (widget);
}

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

  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}

Mark McLoughlin's avatar
Mark McLoughlin committed
1375
static gint
Havoc Pennington's avatar
Havoc Pennington committed
1376 1377 1378 1379
animation_timeout (gpointer data)
{
  GtkImage *image;

1380 1381
  GDK_THREADS_ENTER ();

Havoc Pennington's avatar
Havoc Pennington committed
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
  image = GTK_IMAGE (data);
  
  image->data.anim.frame_timeout = 0;

  gdk_pixbuf_animation_iter_advance (image->data.anim.iter, NULL);

  if (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter) >= 0)
    image->data.anim.frame_timeout =
      g_timeout_add (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter),
                     animation_timeout,
                     image);
  
  gtk_widget_queue_draw (GTK_WIDGET (image));

1396 1397
  GDK_THREADS_LEAVE ();

Havoc Pennington's avatar
Havoc Pennington committed
1398 1399
  return FALSE;
}
Elliot Lee's avatar
Elliot Lee committed
1400

1401
static void
1402
icon_theme_changed (GtkImage *image)
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
{
  if (image->data.name.pixbuf)
    g_object_unref (image->data.name.pixbuf);
  image->data.name.pixbuf = NULL;

  gtk_widget_queue_draw (GTK_WIDGET (image));
}

static void
ensure_pixbuf_for_icon_name (GtkImage *image)
{
  GtkImagePrivate *priv;
  GdkScreen *screen;
  GtkIconTheme *icon_theme;
  GtkSettings *settings;
  gint width, height;
  gint *sizes, *s, dist;
  GError *error = NULL;

  g_return_if_fail (image->storage_type == GTK_IMAGE_ICON_NAME);

  priv = GTK_IMAGE_GET_PRIVATE (image);
  screen = gtk_widget_get_screen (GTK_WIDGET (image));
  icon_theme = gtk_icon_theme_get_for_screen (screen);
  settings = gtk_settings_get_for_screen (screen);
  if (image->data.name.pixbuf == NULL)
    {
      if (priv->pixel_size != -1)
	{
	  width = height = priv->pixel_size;
	}
      else if (!gtk_icon_size_lookup_for_settings (settings,
						   image->icon_size,
						   &width, &height))
	{
	  if (image->icon_size == -1)
	    {
	      /* Find an available size close to 48 */
	      sizes = gtk_icon_theme_get_icon_sizes (icon_theme, image->data.name.icon_name);
	      dist = 100;
	      width = height = 48;
	      for (s = sizes; *s; s++)
		{
		  if (*s == -1)
		    {
		      width = height = 48;
		      break;
		    }
		  if (*s < 48)
		    {
		      if (48 - *s < dist)
			{
			  width = height = *s;
			  dist = 48 - *s;
			}
		    }
		  else
		    {
		      if (*s - 48 < dist)
			{
			  width = height = *s;
			  dist = *s - 48;
			}
		    }
		}
	      g_free (sizes);
	    }
	  else
	    {
	      g_warning ("Invalid icon size %d\n", image->icon_size);
	      width = height = 24;
	    }
	}
      image->data.name.pixbuf =
	gtk_icon_theme_load_icon (icon_theme,
				  image->data.name.icon_name,
				  MIN (width, height), 0, &error);
      if (image->data.name.pixbuf == NULL)
	{
	  g_error_free (error);
	  image->data.name.pixbuf =
	    gtk_widget_render_icon (GTK_WIDGET (image),
				    GTK_STOCK_MISSING_IMAGE,
				    image->icon_size,
				    NULL);
	}
    }
}

1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
/*
 * Like gdk_rectangle_intersect (dest, src, dest), but make 
 * sure that the origin of dest is moved by an "even" offset. 
 * If necessary grow the intersection by one row or column 
 * to achieve this.
 *
 * This is necessary since we can't pass alignment information
 * for the pixelation pattern down to gdk_pixbuf_saturate_and_pixelate(), 
 * thus we have to makesure that the subimages are properly aligned.
 */
static gboolean
rectangle_intersect_even (GdkRectangle *src, 
			  GdkRectangle *dest)
{
  gboolean isect;
  gint x, y;

  x = dest->x;
  y = dest->y;
  isect = gdk_rectangle_intersect (dest, src, dest);

  if ((dest->x - x + dest->y - y) % 2 != 0)
    {
      if (dest->x > x)
	{
	  dest->x--;
	  dest->width++;
	}
      else
	{
	  dest->y--;
	  dest->height++;
	}
    }
  
  return isect;
}

Elliot Lee's avatar
Elliot Lee committed
1530 1531 1532 1533 1534 1535
static gint
gtk_image_expose (GtkWidget      *widget,
		  GdkEventExpose *event)
{
  g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
1536
  
1537
  if (GTK_WIDGET_MAPPED (widget) &&
1538
      GTK_IMAGE (widget)->storage_type != GTK_IMAGE_EMPTY)
Elliot Lee's avatar
Elliot Lee committed
1539
    {
1540 1541
      GtkImage *image;
      GtkMisc *misc;
1542
      GdkRectangle area, image_bound;
1543
      gfloat xalign;
1544
      gint x, y, mask_x, mask_y;
1545 1546 1547
      GdkBitmap *mask;
      GdkPixbuf *pixbuf;
      gboolean needs_state_transform;
1548
      
Elliot Lee's avatar
Elliot Lee committed
1549 1550 1551
      image = GTK_IMAGE (widget);
      misc = GTK_MISC (widget);

1552
      area = event->area;
1553 1554 1555 1556 1557 1558 1559 1560

      /* For stock items and icon sets, we lazily calculate
       * the size; we might get here between a queue_resize()
       * and size_request() if something explicitely forces
       * a redraw.
       */
      if (widget->requisition.width == 0 && widget->requisition.height == 0)
	gtk_image_calc_size (image);
1561 1562 1563 1564
      
      if (!gdk_rectangle_intersect (&area, &widget->allocation, &area))
	return FALSE;

1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
	xalign = misc->xalign;
      else
	xalign = 1.0 - misc->xalign;
  
      x = floor (widget->allocation.x + misc->xpad
		 + ((widget->allocation.width - widget->requisition.width) * xalign)
		 + 0.5);
      y = floor (widget->allocation.y + misc->ypad 
		 + ((widget->allocation.height - widget->requisition.height) * misc->yalign)
		 + 0.5);
1576 1577
      mask_x = x;
      mask_y = y;
1578
      
1579 1580
      image_bound.x = x;
      image_bound.y = y;      
1581 1582
      image_bound.width = 0;
      image_bound.height = 0;      
1583

1584 1585 1586 1587
      mask = NULL;
      pixbuf = NULL;
      needs_state_transform = GTK_WIDGET_STATE (widget) != GTK_STATE_NORMAL;
      
1588 1589 1590
      switch (image->storage_type)
        {
        case GTK_IMAGE_PIXMAP:
Havoc Pennington's avatar
Havoc Pennington committed
1591
          mask = image->mask;
1592 1593 1594
          gdk_drawable_get_size (image->data.pixmap.pixmap,
                                 &image_bound.width,
                                 &image_bound.height);
1595
	  if (rectangle_intersect_even (&area, &image_bound) &&
1596 1597 1598 1599 1600
	      needs_state_transform)
            {
              pixbuf = gdk_pixbuf_get_from_drawable (NULL,
                                                     image->data.pixmap.pixmap,
                                                     gtk_widget_get_colormap (widget),
1601
                                                     image_bound.x - x, image_bound.y - y,
1602 1603 1604 1605 1606 1607 1608 1609
						     0, 0,
                                                     image_bound.width,
                                                     image_bound.height);

	      x = image_bound.x;
	      y = image_bound.y;
            }
	  
1610 1611 1612
          break;

        case GTK_IMAGE_IMAGE:
Havoc Pennington's avatar
Havoc Pennington committed
1613
          mask = image->mask;
1614 1615
          image_bound.width = image->data.image.image->width;
          image_bound.height = image->data.image.image->height;
1616

1617
	  if (rectangle_intersect_even (&area, &image_bound) &&
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
	      needs_state_transform)
            {
              pixbuf = gdk_pixbuf_get_from_image (NULL,
                                                  image->data.image.image,
                                                  gtk_widget_get_colormap (widget),
						  image_bound.x - x, image_bound.y - y,
                                                  0, 0,
                                                  image_bound.width,
                                                  image_bound.height);

	      x = image_bound.x;
	      y = image_bound.y;
            }
1631 1632 1633 1634
          break;

        case GTK_IMAGE_PIXBUF:
          image_bound.width = gdk_pixbuf_get_width (image->data.pixbuf.pixbuf);
1635
          image_bound.height = gdk_pixbuf_get_height (image->data.pixbuf.pixbuf);          
1636
	  
1637

1638
	  if (rectangle_intersect_even (&area, &image_bound) &&
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
	      needs_state_transform)
	    {
	      pixbuf = gdk_pixbuf_new_subpixbuf (image->data.pixbuf.pixbuf,
						 image_bound.x - x, image_bound.y - y,
						 image_bound.width, image_bound.height);

	      x = image_bound.x;
	      y = image_bound.y;
	    }
	  else
	    {
	      pixbuf = image->data.pixbuf.pixbuf;
Manish Singh's avatar
Manish Singh committed
1651
	      g_object_ref (pixbuf);
1652
	    }
1653 1654 1655
          break;

        case GTK_IMAGE_STOCK:
1656 1657 1658 1659 1660
          pixbuf = gtk_widget_render_icon (widget,
                                           image->data.stock.stock_id,
                                           image->icon_size,
                                           NULL);
          if (pixbuf)
1661
            {              
1662 1663
              image_bound.width = gdk_pixbuf_get_width (pixbuf);
              image_bound.height = gdk_pixbuf_get_height (pixbuf);
1664
            }
1665 1666 1667

          /* already done */
          needs_state_transform = FALSE;
1668 1669 1670
          break;

        case GTK_IMAGE_ICON_SET:
1671
          pixbuf =
1672 1673 1674 1675
            gtk_icon_set_render_icon (image->data.icon_set.icon_set,
                                      widget->style,
                                      gtk_widget_get_direction (widget),
                                      GTK_WIDGET_STATE (widget),
Havoc Pennington's avatar
Havoc Pennington committed
1676
                                      image->icon_size,
1677 1678 1679
                                      widget,
                                      NULL);

1680
          if (pixbuf)
1681
            {
1682 1683
              image_bound.width = gdk_pixbuf_get_width (pixbuf);
              image_bound.height = gdk_pixbuf_get_height (pixbuf);
1684
            }
1685 1686 1687

          /* already done */
          needs_state_transform = FALSE;
1688
          break;
Havoc Pennington's avatar
Havoc Pennington committed
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704

        case GTK_IMAGE_ANIMATION:
          {
            if (image->data.anim.iter == NULL)
              {
                image->data.anim.iter = gdk_pixbuf_animation_get_iter (image->data.anim.anim, NULL);
                
                if (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter) >= 0)
                  image->data.anim.frame_timeout =
                    g_timeout_add (gdk_pixbuf_animation_iter_get_delay_time (image->data.anim.iter),
                                   animation_timeout,
                                   image);
              }

            image_bound.width = gdk_pixbuf_animation_get_width (image->data.anim.anim);
            image_bound.height = gdk_pixbuf_animation_get_height (image->data.anim.anim);
1705 1706 1707 1708 1709 1710
                  
            /* don't advance the anim iter here, or we could get frame changes between two
             * exposes of different areas.
             */
            
            pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (image->data.anim.iter);
Manish Singh's avatar
Manish Singh committed
1711
            g_object_ref (pixbuf);
Havoc Pennington's avatar
Havoc Pennington committed
1712 1713
          }
          break;
1714

1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
	case GTK_IMAGE_ICON_NAME:
	  ensure_pixbuf_for_icon_name (image);
	  pixbuf = image->data.name.pixbuf;
	  if (pixbuf)
	    {
	      g_object_ref (pixbuf);
	      image_bound.width = gdk_pixbuf_get_width (pixbuf);
	      image_bound.height = gdk_pixbuf_get_height (pixbuf);
	    }
	  break;

1726 1727
        case GTK_IMAGE_EMPTY:
          g_assert_not_reached ();
1728 1729 1730 1731
          break;
        }

      if (mask)
Elliot Lee's avatar
Elliot Lee committed
1732
	{
1733
	  gdk_gc_set_clip_mask (widget->style->black_gc, mask);
1734
	  gdk_gc_set_clip_origin (widget->style->black_gc, mask_x, mask_y);
Elliot Lee's avatar
Elliot Lee committed
1735 1736
	}

1737
      if (rectangle_intersect_even (&area, &image_bound))
1738
        {
1739
          if (pixbuf)
1740
            {
1741
              if (needs_state_transform)
1742
                {
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
                  GtkIconSource *source;
                  GdkPixbuf *rendered;

                  source = gtk_icon_source_new ();
                  gtk_icon_source_set_pixbuf (source, pixbuf);
                  /* The size here is arbitrary; since size isn't
                   * wildcarded in the souce, it isn't supposed to be
                   * scaled by the engine function
                   */
                  gtk_icon_source_set_size (source,
                                            GTK_ICON_SIZE_SMALL_TOOLBAR);
                  gtk_icon_source_set_size_wildcarded (source, FALSE);
Havoc Pennington's avatar