gtkiconhelper.c 27.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* GTK - The GIMP Toolkit
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * Authors: Cosimo Cecchi <cosimoc@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 19 20 21
 */

#include "config.h"

22 23
#include "gtkiconhelperprivate.h"

24
#include <math.h>
25

26
#include "gtkcssenumvalueprivate.h"
27
#include "gtkcssiconthemevalueprivate.h"
28 29
#include "gtkcssnodeprivate.h"
#include "gtkcssstyleprivate.h"
30
#include "gtkcssstylepropertyprivate.h"
31
#include "gtkcsstransientnodeprivate.h"
32
#include "gtkiconthemeprivate.h"
33
#include "gtkrendericonprivate.h"
34
#include "deprecated/gtkiconfactoryprivate.h"
35
#include "deprecated/gtkstock.h"
36 37

struct _GtkIconHelperPrivate {
38
  GtkImageDefinition *def;
39 40 41 42

  GtkIconSize icon_size;
  gint pixel_size;

43 44
  guint use_fallback : 1;
  guint force_scale_pixbuf : 1;
45
  guint rendered_surface_is_symbolic : 1;
46

47
  cairo_surface_t *rendered_surface;
48 49
};

Benjamin Otte's avatar
Benjamin Otte committed
50
G_DEFINE_TYPE_WITH_PRIVATE (GtkIconHelper, gtk_icon_helper, GTK_TYPE_CSS_GADGET)
51

52
static void
53 54 55 56 57 58
gtk_icon_helper_invalidate (GtkIconHelper *self)
{
  if (self->priv->rendered_surface != NULL)
    {
      cairo_surface_destroy (self->priv->rendered_surface);
      self->priv->rendered_surface = NULL;
59
      self->priv->rendered_surface_is_symbolic = FALSE;
60
    }
61 62 63

  if (!GTK_IS_CSS_TRANSIENT_NODE (gtk_css_gadget_get_node (GTK_CSS_GADGET (self))))
    gtk_widget_queue_resize (gtk_css_gadget_get_owner (GTK_CSS_GADGET (self)));
64 65
}

66 67 68 69 70 71
void
gtk_icon_helper_invalidate_for_change (GtkIconHelper     *self,
                                       GtkCssStyleChange *change)
{
  GtkIconHelperPrivate *priv = self->priv;

72
  if (change == NULL ||
73 74
      ((gtk_css_style_change_affects (change, GTK_CSS_AFFECTS_SYMBOLIC_ICON) &&
        priv->rendered_surface_is_symbolic) ||
75 76
       (gtk_css_style_change_affects (change, GTK_CSS_AFFECTS_ICON) &&
        !priv->rendered_surface_is_symbolic)))
77 78 79 80 81
    {
      gtk_icon_helper_invalidate (self);
    }
}

82 83 84 85 86 87 88 89 90 91 92 93
static void
gtk_icon_helper_take_definition (GtkIconHelper      *self,
                                 GtkImageDefinition *def)
{
  _gtk_icon_helper_clear (self);

  if (def == NULL)
    return;

  gtk_image_definition_unref (self->priv->def);
  self->priv->def = def;

94
  gtk_icon_helper_invalidate (self);
95 96
}

97 98 99
void
_gtk_icon_helper_clear (GtkIconHelper *self)
{
100
  g_clear_pointer (&self->priv->rendered_surface, cairo_surface_destroy);
101

102 103
  gtk_image_definition_unref (self->priv->def);
  self->priv->def = gtk_image_definition_new_empty ();
104 105

  self->priv->icon_size = GTK_ICON_SIZE_INVALID;
106 107

  gtk_icon_helper_invalidate (self);
108 109
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
static void
gtk_icon_helper_get_preferred_size (GtkCssGadget   *gadget,
                                    GtkOrientation  orientation,
                                    gint            for_size,
                                    gint           *minimum,
                                    gint           *natural,
                                    gint           *minimum_baseline,
                                    gint           *natural_baseline)
{
  GtkIconHelper *self = GTK_ICON_HELPER (gadget);
  int icon_width, icon_height;

  _gtk_icon_helper_get_size (self, &icon_width, &icon_height);

  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    *minimum = *natural = icon_width;
  else
    *minimum = *natural = icon_height;
}

static void
gtk_icon_helper_allocate (GtkCssGadget        *gadget,
                          const GtkAllocation *allocation,
                          int                  baseline,
                          GtkAllocation       *out_clip)
{
  GTK_CSS_GADGET_CLASS (gtk_icon_helper_parent_class)->allocate (gadget, allocation, baseline, out_clip);
}

static gboolean
gtk_icon_helper_draw (GtkCssGadget *gadget,
                      cairo_t      *cr,
                      int           x,
                      int           y,
                      int           width,
                      int           height)
{
  GtkIconHelper *self = GTK_ICON_HELPER (gadget);
  int icon_width, icon_height;

  _gtk_icon_helper_get_size (self, &icon_width, &icon_height);
  _gtk_icon_helper_draw (self,
                         cr,
                         x + (width - icon_width) / 2,
                         y + (height - icon_height) / 2);

  return FALSE;
}

159 160 161
static void
gtk_icon_helper_style_changed (GtkCssGadget      *gadget,
                               GtkCssStyleChange *change)
162
{
163
  gtk_icon_helper_invalidate_for_change (GTK_ICON_HELPER (gadget), change);
164

165 166
  if (!GTK_IS_CSS_TRANSIENT_NODE (gtk_css_gadget_get_node (gadget)))
    GTK_CSS_GADGET_CLASS (gtk_icon_helper_parent_class)->style_changed (gadget, change);
167 168 169 170 171 172 173 174 175 176 177 178 179 180
}

static void
gtk_icon_helper_constructed (GObject *object)
{
  GtkIconHelper *self = GTK_ICON_HELPER (object);
  GtkWidget *widget;

  widget = gtk_css_gadget_get_owner (GTK_CSS_GADGET (self));

  g_signal_connect_swapped (widget, "direction-changed", G_CALLBACK (gtk_icon_helper_invalidate), self);
  g_signal_connect_swapped (widget, "notify::scale-factor", G_CALLBACK (gtk_icon_helper_invalidate), self);

  G_OBJECT_CLASS (gtk_icon_helper_parent_class)->constructed (object);
181 182 183 184 185 186
}

static void
gtk_icon_helper_finalize (GObject *object)
{
  GtkIconHelper *self = GTK_ICON_HELPER (object);
187 188 189 190
  GtkWidget *widget;

  widget = gtk_css_gadget_get_owner (GTK_CSS_GADGET (self));
  g_signal_handlers_disconnect_by_func (widget, G_CALLBACK (gtk_icon_helper_invalidate), self);
191 192

  _gtk_icon_helper_clear (self);
193
  gtk_image_definition_unref (self->priv->def);
194
  
Benjamin Otte's avatar
Benjamin Otte committed
195
  G_OBJECT_CLASS (gtk_icon_helper_parent_class)->finalize (object);
196 197 198
}

static void
Benjamin Otte's avatar
Benjamin Otte committed
199
gtk_icon_helper_class_init (GtkIconHelperClass *klass)
200
{
201 202 203
  GtkCssGadgetClass *gadget_class = GTK_CSS_GADGET_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
204 205 206
  gadget_class->get_preferred_size = gtk_icon_helper_get_preferred_size;
  gadget_class->allocate = gtk_icon_helper_allocate;
  gadget_class->draw = gtk_icon_helper_draw;
207
  gadget_class->style_changed = gtk_icon_helper_style_changed;
208

209 210
  object_class->constructed = gtk_icon_helper_constructed;
  object_class->finalize = gtk_icon_helper_finalize;
211 212 213
}

static void
Benjamin Otte's avatar
Benjamin Otte committed
214
gtk_icon_helper_init (GtkIconHelper *self)
215
{
Benjamin Otte's avatar
Benjamin Otte committed
216
  self->priv = gtk_icon_helper_get_instance_private (self);
217

218 219
  self->priv->def = gtk_image_definition_new_empty ();

220 221
  self->priv->icon_size = GTK_ICON_SIZE_INVALID;
  self->priv->pixel_size = -1;
222
  self->priv->rendered_surface_is_symbolic = FALSE;
223 224 225 226 227 228 229 230 231 232 233 234 235
}

static void
ensure_icon_size (GtkIconHelper *self,
		  gint *width_out,
		  gint *height_out)
{
  gint width, height;

  if (self->priv->pixel_size != -1)
    {
      width = height = self->priv->pixel_size;
    }
236
  else if (!gtk_icon_size_lookup (self->priv->icon_size, &width, &height))
237
    {
238 239 240 241 242 243
      if (self->priv->icon_size == GTK_ICON_SIZE_INVALID)
        {
          width = height = 0;
        }
      else
        {
244
          g_warning ("Invalid icon size %d", self->priv->icon_size);
245 246
          width = height = 24;
        }
247 248 249 250 251 252 253
    }

  *width_out = width;
  *height_out = height;
}

static GtkIconLookupFlags
254 255 256
get_icon_lookup_flags (GtkIconHelper    *self,
                       GtkCssStyle      *style,
                       GtkTextDirection  dir)
257
{
258 259 260 261
  GtkIconLookupFlags flags;
  GtkCssIconStyle icon_style;

  flags = GTK_ICON_LOOKUP_USE_BUILTIN;
262

263
  if (self->priv->pixel_size != -1 || self->priv->force_scale_pixbuf)
264 265
    flags |= GTK_ICON_LOOKUP_FORCE_SIZE;

266
  icon_style = _gtk_css_icon_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_STYLE));
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

  switch (icon_style)
    {
    case GTK_CSS_ICON_STYLE_REGULAR:
      flags |= GTK_ICON_LOOKUP_FORCE_REGULAR;
      break;
    case GTK_CSS_ICON_STYLE_SYMBOLIC:
      flags |= GTK_ICON_LOOKUP_FORCE_SYMBOLIC;
      break;
    case GTK_CSS_ICON_STYLE_REQUESTED:
      break;
    default:
      g_assert_not_reached ();
      return 0;
    }

283
  if (dir == GTK_TEXT_DIR_LTR)
284
    flags |= GTK_ICON_LOOKUP_DIR_LTR;
285
  else if (dir == GTK_TEXT_DIR_RTL)
286
    flags |= GTK_ICON_LOOKUP_DIR_RTL;
287

288 289 290
  return flags;
}

291 292 293 294 295 296
static void
get_surface_size (GtkIconHelper   *self,
		  cairo_surface_t *surface,
		  int *width,
		  int *height)
{
297 298
  GdkRectangle clip;
  cairo_t *cr;
299

300 301
  cr = cairo_create (surface);
  if (gdk_cairo_get_clip_rectangle (cr, &clip))
302
    {
303 304 305 306 307 308
      if (clip.x != 0 || clip.y != 0)
        {
          g_warning ("origin of surface is %d %d, not supported", clip.x, clip.y);
        }
      *width = clip.width;
      *height = clip.height;
309 310
    }
  else
311 312 313 314 315 316
    {
      g_warning ("infinite surface size not supported");
      ensure_icon_size (self, width, height);
    }

  cairo_destroy (cr);
317 318
}

319
static cairo_surface_t *
320
ensure_surface_from_surface (GtkIconHelper   *self,
321
                             cairo_surface_t *orig_surface)
322
{
323
  return cairo_surface_reference (orig_surface);
324 325
}

326 327
static gboolean
get_pixbuf_size (GtkIconHelper   *self,
328
                 gint             scale,
329 330
                 GdkPixbuf       *orig_pixbuf,
                 gint             orig_scale,
331 332 333
                 gint *width_out,
                 gint *height_out,
                 gint *scale_out)
334
{
335
  gboolean scale_pixmap;
336 337
  gint width, height;

338
  scale_pixmap = FALSE;
339 340 341 342 343

  if (self->priv->force_scale_pixbuf &&
      (self->priv->pixel_size != -1 ||
       self->priv->icon_size != GTK_ICON_SIZE_INVALID))
    {
344
      ensure_icon_size (self, &width, &height);
345

346 347 348
      if (scale != orig_scale ||
	  width < gdk_pixbuf_get_width (orig_pixbuf) / orig_scale ||
          height < gdk_pixbuf_get_height (orig_pixbuf) / orig_scale)
349
	{
350 351
	  width = MIN (width * scale, gdk_pixbuf_get_width (orig_pixbuf) * scale / orig_scale);
	  height = MIN (height * scale, gdk_pixbuf_get_height (orig_pixbuf) * scale / orig_scale);
352

353
          scale_pixmap = TRUE;
354 355 356
	}
      else
	{
357 358 359
	  width = gdk_pixbuf_get_width (orig_pixbuf);
	  height = gdk_pixbuf_get_height (orig_pixbuf);
	  scale = orig_scale;
360 361 362 363
	}
    }
  else
    {
364 365 366
      width = gdk_pixbuf_get_width (orig_pixbuf);
      height = gdk_pixbuf_get_height (orig_pixbuf);
      scale = orig_scale;
367 368
    }

369 370 371 372 373 374 375
  *width_out = width;
  *height_out = height;
  *scale_out = scale;

  return scale_pixmap;
}

376
static cairo_surface_t *
377 378 379 380 381
ensure_surface_from_pixbuf (GtkIconHelper *self,
                            GtkCssStyle   *style,
                            gint           scale,
                            GdkPixbuf     *orig_pixbuf,
                            gint           orig_scale)
382 383
{
  gint width, height;
384
  cairo_surface_t *surface;
385
  GdkPixbuf *pixbuf;
386
  GtkCssIconEffect icon_effect;
387

388
  if (get_pixbuf_size (self,
389
                       scale,
390 391 392 393
                       orig_pixbuf,
                       orig_scale,
                       &width, &height, &scale))
    pixbuf = gdk_pixbuf_scale_simple (orig_pixbuf,
394 395 396
                                      width, height,
                                      GDK_INTERP_BILINEAR);
  else
397
    pixbuf = g_object_ref (orig_pixbuf);
398

399
  surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, scale, gtk_widget_get_window (gtk_css_gadget_get_owner (GTK_CSS_GADGET (self))));
400
  icon_effect = _gtk_css_icon_effect_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_EFFECT));
401
  gtk_css_icon_effect_apply (icon_effect, surface);
402
  g_object_unref (pixbuf);
403 404

  return surface;
405 406
}

407
static cairo_surface_t *
408 409 410 411 412
ensure_surface_for_icon_set (GtkIconHelper    *self,
                             GtkCssStyle      *style,
                             GtkTextDirection  direction,
                             gint              scale,
			     GtkIconSet       *icon_set)
413
{
414 415 416 417 418 419 420 421 422 423
  cairo_surface_t *surface;
  GdkPixbuf *pixbuf;

  pixbuf = gtk_icon_set_render_icon_pixbuf_for_scale (icon_set,
                                                      style,
                                                      direction,
                                                      self->priv->icon_size,
                                                      scale);
  surface = gdk_cairo_surface_create_from_pixbuf (pixbuf,
                                                  scale,
424
                                                  gtk_widget_get_window (gtk_css_gadget_get_owner (GTK_CSS_GADGET (self))));
425 426 427
  g_object_unref (pixbuf);

  return surface;
428 429
}

430
static cairo_surface_t *
431 432 433 434 435
ensure_surface_for_gicon (GtkIconHelper    *self,
                          GtkCssStyle      *style,
                          GtkTextDirection  dir,
                          gint              scale,
                          GIcon            *gicon)
436
{
437
  GtkIconHelperPrivate *priv = self->priv;
438 439 440 441
  GtkIconTheme *icon_theme;
  gint width, height;
  GtkIconInfo *info;
  GtkIconLookupFlags flags;
442
  cairo_surface_t *surface;
443
  GdkPixbuf *destination;
444 445
  gboolean symbolic;

446
  icon_theme = gtk_css_icon_theme_value_get_icon_theme
447 448
    (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_THEME));
  flags = get_icon_lookup_flags (self, style, dir);
449

450 451 452 453 454 455
  ensure_icon_size (self, &width, &height);

  info = gtk_icon_theme_lookup_by_gicon_for_scale (icon_theme,
                                                   gicon,
                                                   MIN (width, height),
                                                   scale, flags);
456
  if (info)
457
    {
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
      symbolic = gtk_icon_info_is_symbolic (info);

      if (symbolic)
        {
          GdkRGBA fg, success_color, warning_color, error_color;

          gtk_icon_theme_lookup_symbolic_colors (style, &fg, &success_color, &warning_color, &error_color);

          destination = gtk_icon_info_load_symbolic (info,
                                                     &fg, &success_color,
                                                     &warning_color, &error_color,
                                                     NULL,
                                                     NULL);
        }
      else
        {
          destination = gtk_icon_info_load_icon (info, NULL);
        }

477 478
      g_object_unref (info);
    }
479
  else
480 481 482
    {
      destination = NULL;
    }
483 484 485

  if (destination == NULL)
    {
486 487 488 489 490 491 492 493 494
      destination = gtk_icon_theme_load_icon (icon_theme,
                                              "image-missing",
                                              width,
                                              flags | GTK_ICON_LOOKUP_USE_BUILTIN | GTK_ICON_LOOKUP_GENERIC_FALLBACK,
                                              NULL);
      /* We include this image as resource, so we always have it available or
       * the icontheme code is broken */
      g_assert (destination);
      symbolic = FALSE;
495
    }
496

497 498
  surface = gdk_cairo_surface_create_from_pixbuf (destination, scale, gtk_widget_get_window (gtk_css_gadget_get_owner (GTK_CSS_GADGET (self))));

499
  if (!symbolic)
500 501 502
    {
      GtkCssIconEffect icon_effect;

503
      icon_effect = _gtk_css_icon_effect_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_EFFECT));
504 505
      gtk_css_icon_effect_apply (icon_effect, surface);
    }
506 507 508 509 510
  else
    {
      priv->rendered_surface_is_symbolic = TRUE;
    }

511
  g_object_unref (destination);
512 513

  return surface;
514 515 516
}

cairo_surface_t *
517
gtk_icon_helper_load_surface (GtkIconHelper   *self,
518
                              int              scale)
519
{
520
  cairo_surface_t *surface;
521
  GtkIconSet *icon_set;
522
  GIcon *gicon;
523

524
  switch (gtk_image_definition_get_storage_type (self->priv->def))
525 526
    {
    case GTK_IMAGE_SURFACE:
527
      surface = ensure_surface_from_surface (self, gtk_image_definition_get_surface (self->priv->def));
528 529 530
      break;

    case GTK_IMAGE_PIXBUF:
531
      surface = ensure_surface_from_pixbuf (self,
532
                                            gtk_css_node_get_style (gtk_css_gadget_get_node (GTK_CSS_GADGET (self))),
533
                                            scale,
534 535
                                            gtk_image_definition_get_pixbuf (self->priv->def),
                                            gtk_image_definition_get_scale (self->priv->def));
536 537 538
      break;

    case GTK_IMAGE_STOCK:
539
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
540
      icon_set = gtk_icon_factory_lookup_default (gtk_image_definition_get_stock (self->priv->def));
541
G_GNUC_END_IGNORE_DEPRECATIONS;
542
      if (icon_set != NULL)
543
	surface = ensure_surface_for_icon_set (self,
544
                                               gtk_css_node_get_style (gtk_css_gadget_get_node (GTK_CSS_GADGET (self))),
545
                                               gtk_widget_get_direction (gtk_css_gadget_get_owner (GTK_CSS_GADGET (self))), 
546
                                               scale, icon_set);
547 548 549 550 551
      else
	surface = NULL;
      break;

    case GTK_IMAGE_ICON_SET:
552
      icon_set = gtk_image_definition_get_icon_set (self->priv->def);
553
      surface = ensure_surface_for_icon_set (self,
554
                                             gtk_css_node_get_style (gtk_css_gadget_get_node (GTK_CSS_GADGET (self))),
555
                                             gtk_widget_get_direction (gtk_css_gadget_get_owner (GTK_CSS_GADGET (self))), 
556
                                             scale, icon_set);
557 558 559
      break;

    case GTK_IMAGE_ICON_NAME:
560 561 562 563
      if (self->priv->use_fallback)
        gicon = g_themed_icon_new_with_default_fallbacks (gtk_image_definition_get_icon_name (self->priv->def));
      else
        gicon = g_themed_icon_new (gtk_image_definition_get_icon_name (self->priv->def));
564
      surface = ensure_surface_for_gicon (self,
565
                                          gtk_css_node_get_style (gtk_css_gadget_get_node (GTK_CSS_GADGET (self))),
566
                                          gtk_widget_get_direction (gtk_css_gadget_get_owner (GTK_CSS_GADGET (self))), 
567 568
                                          scale, 
                                          gicon);
569 570 571
      g_object_unref (gicon);
      break;

572
    case GTK_IMAGE_GICON:
573
      surface = ensure_surface_for_gicon (self, 
574
                                          gtk_css_node_get_style (gtk_css_gadget_get_node (GTK_CSS_GADGET (self))),
575
                                          gtk_widget_get_direction (gtk_css_gadget_get_owner (GTK_CSS_GADGET (self))), 
576 577
                                          scale,
                                          gtk_image_definition_get_gicon (self->priv->def));
578 579 580 581 582 583 584 585 586
      break;

    case GTK_IMAGE_ANIMATION:
    case GTK_IMAGE_EMPTY:
    default:
      surface = NULL;
      break;
    }

587
  return surface;
588

589 590 591
}

static void
592
gtk_icon_helper_ensure_surface (GtkIconHelper *self)
593
{
594 595
  int scale;

LRN's avatar
LRN committed
596 597 598
  if (self->priv->rendered_surface)
    return;

599
  scale = gtk_widget_get_scale_factor (gtk_css_gadget_get_owner (GTK_CSS_GADGET (self)));
600

601
  self->priv->rendered_surface = gtk_icon_helper_load_surface (self, scale);
602 603
}

604 605 606 607 608
void
_gtk_icon_helper_get_size (GtkIconHelper *self,
                           gint *width_out,
                           gint *height_out)
{
609
  gint width, height, scale;
610 611 612

  width = height = 0;

613 614 615
  /* Certain kinds of images are easy to calculate the size for, these
     we do immediately to avoid having to potentially load the image
     data for something that may not yet be visible */
616
  switch (gtk_image_definition_get_storage_type (self->priv->def))
617
    {
618
    case GTK_IMAGE_SURFACE:
619 620
      get_surface_size (self,
                        gtk_image_definition_get_surface (self->priv->def),
621 622 623 624 625
                        &width,
                        &height);
      break;

    case GTK_IMAGE_PIXBUF:
626
      get_pixbuf_size (self,
627
                       gtk_widget_get_scale_factor (gtk_css_gadget_get_owner (GTK_CSS_GADGET (self))),
628 629 630
                       gtk_image_definition_get_pixbuf (self->priv->def),
                       gtk_image_definition_get_scale (self->priv->def),
                       &width, &height, &scale);
631 632 633 634
      width = (width + scale - 1) / scale;
      height = (height + scale - 1) / scale;
      break;

635 636 637 638 639 640 641 642
    case GTK_IMAGE_ANIMATION:
      {
        GdkPixbufAnimation *animation = gtk_image_definition_get_animation (self->priv->def);
        width = gdk_pixbuf_animation_get_width (animation);
        height = gdk_pixbuf_animation_get_height (animation);
        break;
      }

643 644 645
    case GTK_IMAGE_ICON_NAME:
    case GTK_IMAGE_GICON:
      if (self->priv->pixel_size != -1 || self->priv->force_scale_pixbuf)
646
        ensure_icon_size (self, &width, &height);
647 648 649 650 651 652 653 654

      break;

    case GTK_IMAGE_STOCK:
    case GTK_IMAGE_ICON_SET:
    case GTK_IMAGE_EMPTY:
    default:
      break;
655
    }
656 657 658

  /* Otherwise we load the surface to guarantee we get a size */
  if (width == 0)
659
    {
660
      gtk_icon_helper_ensure_surface (self);
661

662
      if (self->priv->rendered_surface != NULL)
663
        {
664
          get_surface_size (self, self->priv->rendered_surface, &width, &height);
665
        }
Matthias Clasen's avatar
Matthias Clasen committed
666
      else if (self->priv->icon_size != GTK_ICON_SIZE_INVALID)
667
        {
668
          ensure_icon_size (self, &width, &height);
669
        }
670 671 672 673 674 675 676 677
    }

  if (width_out)
    *width_out = width;
  if (height_out)
    *height_out = height;
}

678 679 680 681 682 683 684 685 686 687
void
_gtk_icon_helper_set_definition (GtkIconHelper *self,
                                 GtkImageDefinition *def)
{
  if (def)
    gtk_icon_helper_take_definition (self, gtk_image_definition_ref (def));
  else
    _gtk_icon_helper_clear (self);
}

688 689 690 691 692
void 
_gtk_icon_helper_set_gicon (GtkIconHelper *self,
                            GIcon *gicon,
                            GtkIconSize icon_size)
{
693 694
  gtk_icon_helper_take_definition (self, gtk_image_definition_new_gicon (gicon));
  _gtk_icon_helper_set_icon_size (self, icon_size);
695 696 697 698 699 700 701
}

void 
_gtk_icon_helper_set_icon_name (GtkIconHelper *self,
                                const gchar *icon_name,
                                GtkIconSize icon_size)
{
702 703
  gtk_icon_helper_take_definition (self, gtk_image_definition_new_icon_name (icon_name));
  _gtk_icon_helper_set_icon_size (self, icon_size);
704 705 706 707 708 709 710
}

void 
_gtk_icon_helper_set_icon_set (GtkIconHelper *self,
                               GtkIconSet *icon_set,
                               GtkIconSize icon_size)
{
711 712
  gtk_icon_helper_take_definition (self, gtk_image_definition_new_icon_set (icon_set));
  _gtk_icon_helper_set_icon_size (self, icon_size);
713 714 715 716 717 718
}

void 
_gtk_icon_helper_set_pixbuf (GtkIconHelper *self,
                             GdkPixbuf *pixbuf)
{
719
  gtk_icon_helper_take_definition (self, gtk_image_definition_new_pixbuf (pixbuf, 1));
720 721 722 723 724 725
}

void 
_gtk_icon_helper_set_animation (GtkIconHelper *self,
                                GdkPixbufAnimation *animation)
{
726
  gtk_icon_helper_take_definition (self, gtk_image_definition_new_animation (animation, 1));
727 728
}

729 730 731 732
void 
_gtk_icon_helper_set_surface (GtkIconHelper *self,
			      cairo_surface_t *surface)
{
733
  gtk_icon_helper_take_definition (self, gtk_image_definition_new_surface (surface));
734 735
}

736 737 738 739 740
void 
_gtk_icon_helper_set_stock_id (GtkIconHelper *self,
                               const gchar *stock_id,
                               GtkIconSize icon_size)
{
741 742
  gtk_icon_helper_take_definition (self, gtk_image_definition_new_stock (stock_id));
  _gtk_icon_helper_set_icon_size (self, icon_size);
743 744
}

745
gboolean
746
_gtk_icon_helper_set_icon_size (GtkIconHelper *self,
747
                                GtkIconSize    icon_size)
748 749 750 751
{
  if (self->priv->icon_size != icon_size)
    {
      self->priv->icon_size = icon_size;
752
      gtk_icon_helper_invalidate (self);
753
      return TRUE;
754
    }
755
  return FALSE;
756 757
}

758
gboolean
759
_gtk_icon_helper_set_pixel_size (GtkIconHelper *self,
760
                                 gint           pixel_size)
761 762 763 764
{
  if (self->priv->pixel_size != pixel_size)
    {
      self->priv->pixel_size = pixel_size;
765
      gtk_icon_helper_invalidate (self);
766
      return TRUE;
767
    }
768
  return FALSE;
769 770
}

771
gboolean
772
_gtk_icon_helper_set_use_fallback (GtkIconHelper *self,
773
                                   gboolean       use_fallback)
774 775 776 777
{
  if (self->priv->use_fallback != use_fallback)
    {
      self->priv->use_fallback = use_fallback;
778
      gtk_icon_helper_invalidate (self);
779
      return TRUE;
780
    }
781
  return FALSE;
782 783 784 785 786
}

GtkImageType
_gtk_icon_helper_get_storage_type (GtkIconHelper *self)
{
787
  return gtk_image_definition_get_storage_type (self->priv->def);
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
}

gboolean
_gtk_icon_helper_get_use_fallback (GtkIconHelper *self)
{
  return self->priv->use_fallback;
}

GtkIconSize
_gtk_icon_helper_get_icon_size (GtkIconHelper *self)
{
  return self->priv->icon_size;
}

gint
_gtk_icon_helper_get_pixel_size (GtkIconHelper *self)
{
  return self->priv->pixel_size;
}

Benjamin Otte's avatar
Benjamin Otte committed
808 809 810 811 812 813
GtkImageDefinition *
gtk_icon_helper_get_definition (GtkIconHelper *self)
{
  return self->priv->def;
}

814 815 816
GdkPixbuf *
_gtk_icon_helper_peek_pixbuf (GtkIconHelper *self)
{
817
  return gtk_image_definition_get_pixbuf (self->priv->def);
818 819 820 821 822
}

GIcon *
_gtk_icon_helper_peek_gicon (GtkIconHelper *self)
{
823
  return gtk_image_definition_get_gicon (self->priv->def);
824 825 826 827 828
}

GdkPixbufAnimation *
_gtk_icon_helper_peek_animation (GtkIconHelper *self)
{
829
  return gtk_image_definition_get_animation (self->priv->def);
830 831 832 833 834
}

GtkIconSet *
_gtk_icon_helper_peek_icon_set (GtkIconHelper *self)
{
835
  return gtk_image_definition_get_icon_set (self->priv->def);
836 837
}

838 839 840
cairo_surface_t *
_gtk_icon_helper_peek_surface (GtkIconHelper *self)
{
841
  return gtk_image_definition_get_surface (self->priv->def);
842 843
}

844 845 846
const gchar *
_gtk_icon_helper_get_stock_id (GtkIconHelper *self)
{
847
  return gtk_image_definition_get_stock (self->priv->def);
848 849 850 851 852
}

const gchar *
_gtk_icon_helper_get_icon_name (GtkIconHelper *self)
{
853
  return gtk_image_definition_get_icon_name (self->priv->def);
854 855 856
}

GtkIconHelper *
857 858
gtk_icon_helper_new (GtkCssNode *node,
                     GtkWidget  *owner)
859
{
860
  g_return_val_if_fail (GTK_IS_CSS_NODE (node), NULL);
861 862
  g_return_val_if_fail (GTK_IS_WIDGET (owner), NULL);

863
  return g_object_new (GTK_TYPE_ICON_HELPER,
864
                       "node", node,
865 866
                       "owner", owner,
                       NULL);
867 868
}

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
GtkCssGadget *
gtk_icon_helper_new_named (const char *name,
                           GtkWidget  *owner)
{
  GtkIconHelper *result;
  GtkCssNode *node;

  g_return_val_if_fail (name != NULL, NULL);
  g_return_val_if_fail (GTK_IS_WIDGET (owner), NULL);

  node = gtk_css_node_new ();
  gtk_css_node_set_name (node, g_intern_string (name));

  result = gtk_icon_helper_new (node, owner);

  g_object_unref (node);

  return GTK_CSS_GADGET (result);
}

889 890 891 892 893 894
void
_gtk_icon_helper_draw (GtkIconHelper *self,
                       cairo_t *cr,
                       gdouble x,
                       gdouble y)
{
895
  gtk_icon_helper_ensure_surface (self);
896

897
  if (self->priv->rendered_surface != NULL)
898
    {
899 900 901 902
      gtk_css_style_render_icon_surface (gtk_css_node_get_style (gtk_css_gadget_get_node (GTK_CSS_GADGET (self))),
                                         cr,
                                         self->priv->rendered_surface,
                                         x, y);
903 904 905 906 907 908
    }
}

gboolean
_gtk_icon_helper_get_is_empty (GtkIconHelper *self)
{
909
  return gtk_image_definition_get_storage_type (self->priv->def) == GTK_IMAGE_EMPTY;
910
}
911 912 913 914 915 916 917 918 919 920 921 922 923 924

gboolean
_gtk_icon_helper_get_force_scale_pixbuf (GtkIconHelper *self)
{
  return self->priv->force_scale_pixbuf;
}

void
_gtk_icon_helper_set_force_scale_pixbuf (GtkIconHelper *self,
                                         gboolean       force_scale)
{
  if (self->priv->force_scale_pixbuf != force_scale)
    {
      self->priv->force_scale_pixbuf = force_scale;
925
      gtk_icon_helper_invalidate (self);
926 927
    }
}
928 929 930 931 932

void 
_gtk_icon_helper_set_pixbuf_scale (GtkIconHelper *self,
				   int scale)
{
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
  switch (gtk_image_definition_get_storage_type (self->priv->def))
  {
    case GTK_IMAGE_PIXBUF:
      gtk_icon_helper_take_definition (self,
                                      gtk_image_definition_new_pixbuf (gtk_image_definition_get_pixbuf (self->priv->def),
                                                                       scale));
      break;

    case GTK_IMAGE_ANIMATION:
      gtk_icon_helper_take_definition (self,
                                      gtk_image_definition_new_animation (gtk_image_definition_get_animation (self->priv->def),
                                                                          scale));
      break;

    default:
      break;
  }
950
}