gtknumerableicon.c 28.2 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * gtknumerableicon.c: an emblemed icon with number emblems
 *
 * Copyright (C) 2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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/>.
Matthias Clasen's avatar
Matthias Clasen committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * Authors: Cosimo Cecchi <cosimoc@redhat.com>
 */

/**
 * SECTION:gtknumerableicon
 * @Title: GtkNumerableIcon
 * @Short_description: A GIcon that allows numbered emblems
 *
 * GtkNumerableIcon is a subclass of #GEmblemedIcon that can
 * show a number or short string as an emblem. The number can
 * be overlayed on top of another emblem, if desired.
 *
 * It supports theming by taking font and color information
 * from a provided #GtkStyleContext; see
 * gtk_numerable_icon_set_style_context().
 *
35 36 37
 * Typical numerable icons:
 * ![](numerableicon.png)
 * ![](numerableicon2.png)
Matthias Clasen's avatar
Matthias Clasen committed
38 39 40 41
 */
#include <config.h>

#include "gtknumerableicon.h"
42
#include "gtknumerableiconprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
43 44 45 46

#include "gtkicontheme.h"
#include "gtkintl.h"
#include "gtkwidget.h"
47
#include "gtkwidgetpath.h"
Matthias Clasen's avatar
Matthias Clasen committed
48 49 50 51 52 53 54 55
#include "gtkwindow.h"

#include <gdk/gdk.h>
#include <pango/pango.h>
#include <math.h>

struct _GtkNumerableIconPrivate {
  gint count;
56 57
  gint icon_size;

Matthias Clasen's avatar
Matthias Clasen committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  gchar *label;

  GIcon *background_icon;
  gchar *background_icon_name;

  GdkRGBA *background;
  GdkRGBA *foreground;

  PangoFontDescription *font;
  cairo_pattern_t *background_image;
  gint border_size;

  GtkStyleContext *style;
  gulong style_changed_id;

  gchar *rendered_string;
};

enum {
  PROP_COUNT = 1,
  PROP_LABEL,
  PROP_STYLE,
  PROP_BACKGROUND_ICON,
  PROP_BACKGROUND_ICON_NAME,
  NUM_PROPERTIES
};

#define DEFAULT_SURFACE_SIZE 256
#define DEFAULT_BORDER_SIZE DEFAULT_SURFACE_SIZE * 0.06
#define DEFAULT_RADIUS DEFAULT_SURFACE_SIZE / 2

#define DEFAULT_BACKGROUND "#000000"
#define DEFAULT_FOREGROUND "#ffffff"

static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };

94
G_DEFINE_TYPE_WITH_PRIVATE (GtkNumerableIcon, gtk_numerable_icon, G_TYPE_EMBLEMED_ICON)
Matthias Clasen's avatar
Matthias Clasen committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

static gint
get_surface_size (cairo_surface_t *surface)
{
  return MAX (cairo_image_surface_get_width (surface), cairo_image_surface_get_height (surface));
}

static gdouble
get_border_size (GtkNumerableIcon *self)
{
  return self->priv->border_size;
}

static cairo_surface_t *
draw_default_surface (GtkNumerableIcon *self)
{
  cairo_surface_t *surface;
  cairo_t *cr;

  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                        DEFAULT_SURFACE_SIZE, DEFAULT_SURFACE_SIZE);

  cr = cairo_create (surface);

  cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
120
             DEFAULT_RADIUS, 0., 2 * G_PI);
Matthias Clasen's avatar
Matthias Clasen committed
121 122 123 124 125

  gdk_cairo_set_source_rgba (cr, self->priv->background);
  cairo_fill (cr);

  cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
126
             DEFAULT_RADIUS - DEFAULT_BORDER_SIZE, 0., 2 * G_PI);
Matthias Clasen's avatar
Matthias Clasen committed
127 128 129 130
  gdk_cairo_set_source_rgba  (cr, self->priv->foreground);
  cairo_fill (cr);

  cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
131
             DEFAULT_RADIUS - 2 * DEFAULT_BORDER_SIZE, 0., 2 * G_PI);
Matthias Clasen's avatar
Matthias Clasen committed
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
  gdk_cairo_set_source_rgba  (cr, self->priv->background);
  cairo_fill (cr);

  cairo_destroy (cr);

  return surface;
}

static cairo_surface_t *
draw_from_gradient (cairo_pattern_t *pattern)
{
  cairo_surface_t *surface;
  cairo_matrix_t matrix;
  cairo_t *cr;

  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                        DEFAULT_SURFACE_SIZE, DEFAULT_SURFACE_SIZE);

  cr = cairo_create (surface);

  /* scale the gradient points to the user space coordinates */
  cairo_matrix_init_scale (&matrix,
                           1. / (double) DEFAULT_SURFACE_SIZE,
                           1. / (double) DEFAULT_SURFACE_SIZE);
  cairo_pattern_set_matrix (pattern, &matrix);

  cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
159
             DEFAULT_RADIUS, 0., 2 * G_PI);
Matthias Clasen's avatar
Matthias Clasen committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

  cairo_set_source (cr, pattern);
  cairo_fill (cr);

  cairo_destroy (cr);

  return surface;
}

/* copy the surface */
static cairo_surface_t *
draw_from_image (cairo_surface_t *image)
{
  cairo_surface_t *surface;
  cairo_t *cr;

  surface = cairo_surface_create_similar (image, CAIRO_CONTENT_COLOR_ALPHA,
                                          cairo_image_surface_get_width (image),
                                          cairo_image_surface_get_height (image));
  cr = cairo_create (surface);

  cairo_set_source_surface (cr, image, 0, 0);
  cairo_paint (cr);

  cairo_destroy (cr);

  return surface;
}

static cairo_surface_t *
draw_from_gicon (GtkNumerableIcon *self)
{
  GtkIconTheme *theme;
  GdkScreen *screen;
  GtkIconInfo *info;
  GdkPixbuf *pixbuf;
  cairo_surface_t *surface;

  if (self->priv->style != NULL)
    {
      screen = gtk_style_context_get_screen (self->priv->style);
      theme = gtk_icon_theme_get_for_screen (screen);
    }
  else
    {
      theme = gtk_icon_theme_get_default ();
    }

  info = gtk_icon_theme_lookup_by_gicon (theme, self->priv->background_icon,
                                         self->priv->icon_size,
                                         GTK_ICON_LOOKUP_GENERIC_FALLBACK);
  if (info == NULL)
    return NULL;

  pixbuf = gtk_icon_info_load_icon (info, NULL);
215
  g_object_unref (info);
Matthias Clasen's avatar
Matthias Clasen committed
216 217 218 219

  if (pixbuf == NULL)
    return NULL;

220
  surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, 1, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 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 342 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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
  g_object_unref (pixbuf);

  return surface;
}

static cairo_surface_t *
get_image_surface (GtkNumerableIcon *self)
{
  cairo_surface_t *retval = NULL, *image;

  if (self->priv->background_icon != NULL)
    {
      retval = draw_from_gicon (self);
      self->priv->border_size = 0;
    }
  else if (self->priv->background_image != NULL)
    {
      if (cairo_pattern_get_surface (self->priv->background_image, &image) == CAIRO_STATUS_SUCCESS)
        retval = draw_from_image (image);
      else
        retval = draw_from_gradient (self->priv->background_image);

      self->priv->border_size = 0;
    }

  if (retval == NULL)
    {
      retval = draw_default_surface (self);
      self->priv->border_size = DEFAULT_BORDER_SIZE;
    }

  return retval;
}

static PangoLayout *
get_pango_layout (GtkNumerableIcon *self)
{
  PangoContext *context;
  GdkScreen *screen;
  PangoLayout *layout;

  if (self->priv->style != NULL)
    {
      screen = gtk_style_context_get_screen (self->priv->style);
      context = gdk_pango_context_get_for_screen (screen);
      layout = pango_layout_new (context);

      if (self->priv->font != NULL)
        pango_layout_set_font_description (layout, self->priv->font);

      pango_layout_set_text (layout, self->priv->rendered_string, -1);

      g_object_unref (context);
    }
  else
    {
      GtkWidget *fake;

      /* steal gtk text settings from the window */
      fake = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      layout = gtk_widget_create_pango_layout (fake, self->priv->rendered_string);
      gtk_widget_destroy (fake);
    }

  return layout;
}

static void
gtk_numerable_icon_ensure_emblem (GtkNumerableIcon *self)
{
  cairo_t *cr;
  cairo_surface_t *surface;
  PangoLayout *layout;
  GEmblem *emblem;
  gint width, height;
  gdouble scale;
  PangoAttrList *attr_list;
  PangoAttribute *attr;
  GdkPixbuf *pixbuf;

  /* don't draw anything if the count is zero */
  if (self->priv->rendered_string == NULL)
    {
      g_emblemed_icon_clear_emblems (G_EMBLEMED_ICON (self));
      return;
    }

  surface = get_image_surface (self);
  cr = cairo_create (surface);

  layout = get_pango_layout (self);
  pango_layout_get_pixel_size (layout, &width, &height);

  /* scale the layout to be 0.75 of the size still available for drawing */
  scale = ((get_surface_size (surface) - 2 * get_border_size (self)) * 0.75) / (MAX (height, width));
  attr_list = pango_attr_list_new ();

  attr = pango_attr_scale_new (scale);
  pango_attr_list_insert (attr_list, attr);

  attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
  pango_attr_list_insert (attr_list, attr);

  pango_layout_set_attributes (layout, attr_list);

  /* update these values */
  pango_layout_get_pixel_size (layout, &width, &height);

  /* move to the center */
  cairo_move_to (cr,
                 get_surface_size (surface) / 2. - (gdouble) width / 2.,
                 get_surface_size (surface) / 2. - (gdouble) height / 2.);

  gdk_cairo_set_source_rgba (cr, self->priv->foreground);
  pango_cairo_show_layout (cr, layout);

  cairo_destroy (cr);

  pixbuf =
    gdk_pixbuf_get_from_surface (surface, 0, 0,
                                 get_surface_size (surface), get_surface_size (surface));

  emblem = g_emblem_new (G_ICON (pixbuf));
  g_emblemed_icon_clear_emblems (G_EMBLEMED_ICON (self));
  g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (self), emblem);

  g_object_unref (layout);
  g_object_unref (emblem);
  g_object_unref (pixbuf);

  cairo_surface_destroy (surface);
  pango_attr_list_unref (attr_list);
}

static void
gtk_numerable_icon_update_properties_from_style (GtkNumerableIcon *self)
{
  GtkStyleContext *style = self->priv->style;
  GtkWidgetPath *path, *saved;
  cairo_pattern_t *pattern = NULL;
  GdkRGBA background, foreground;
  PangoFontDescription *font = NULL;

  /* save an unmodified copy of the original widget path, in order
   * to restore it later */
  path = gtk_widget_path_copy (gtk_style_context_get_path (style));
  saved = gtk_widget_path_copy (path);

  if (!gtk_widget_path_is_type (path, GTK_TYPE_NUMERABLE_ICON))
    {
      /* append our GType to the style context to fetch appropriate colors */
      gtk_widget_path_append_type (path, GTK_TYPE_NUMERABLE_ICON);
      gtk_style_context_set_path (style, path);
    }

  gtk_style_context_get_background_color (style, gtk_style_context_get_state (style),
                                          &background);
  gtk_style_context_get_color (style, gtk_style_context_get_state (style),
                               &foreground);

  if (self->priv->background != NULL)
    gdk_rgba_free (self->priv->background);

  self->priv->background = gdk_rgba_copy (&background);

  if (self->priv->foreground != NULL)
    gdk_rgba_free (self->priv->foreground);

  self->priv->foreground = gdk_rgba_copy (&foreground);

  gtk_style_context_get (style, gtk_style_context_get_state (style),
                         GTK_STYLE_PROPERTY_BACKGROUND_IMAGE, &pattern,
                         NULL);

  if (pattern != NULL)
    {
      if (self->priv->background_image != NULL)
        cairo_pattern_destroy (self->priv->background_image);

      self->priv->background_image = pattern;
    }

  gtk_style_context_get (style, gtk_style_context_get_state (style),
                         GTK_STYLE_PROPERTY_FONT, &font,
                         NULL);

  if (font != NULL)
    {
      if (self->priv->font != NULL)
        pango_font_description_free (self->priv->font);

      self->priv->font = font;
    }

  gtk_numerable_icon_ensure_emblem (self);

  /* restore original widget path */
  gtk_style_context_set_path (style, saved);

  gtk_widget_path_free (path);
  gtk_widget_path_free (saved);
}

static void
gtk_numerable_icon_init_style (GtkNumerableIcon *self)
{
  GtkStyleContext *style = self->priv->style;

  if (style == NULL)
    return;

  gtk_numerable_icon_update_properties_from_style (self);

  self->priv->style_changed_id =
    g_signal_connect_swapped (style, "changed",
                              G_CALLBACK (gtk_numerable_icon_update_properties_from_style), self);
}

static void
gtk_numerable_icon_ensure_and_replace_label (GtkNumerableIcon *self,
                                             gint              count,
                                             const gchar      *label)
{
  g_assert (!(label != NULL && count != 0));

  g_free (self->priv->rendered_string);
  self->priv->rendered_string = NULL;

  if (count != 0)
    {
      if (self->priv->label != NULL)
        {
          g_free (self->priv->label);
          self->priv->label = NULL;

          g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_LABEL]);
        }

      if (count > 99)
        count = 99;

      if (count < -99)
        count = -99;

      self->priv->count = count;

      /* Translators: the format here is used to build the string that will be rendered
       * in the number emblem.
       */
      self->priv->rendered_string = g_strdup_printf (C_("Number format", "%d"), count);

      return;
    }

  if (label != NULL)
    {
      if (self->priv->count != 0)
        {
          self->priv->count = 0;

          g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COUNT]);
        }

      g_free (self->priv->label);

      if (g_strcmp0 (label, "") == 0)
        {
          self->priv->label = NULL;
          return;
        }

      self->priv->label = g_strdup (label);
      self->priv->rendered_string = g_strdup (label);
    }
}

static gboolean
real_set_background_icon (GtkNumerableIcon *self,
                          GIcon            *icon)
{
  if (!g_icon_equal (self->priv->background_icon, icon))
    {
      g_clear_object (&self->priv->background_icon);

      if (icon != NULL)
        self->priv->background_icon = g_object_ref (icon);

      gtk_numerable_icon_ensure_emblem (self);

      return TRUE;
    }

  return FALSE;
}

static void
gtk_numerable_icon_constructed (GObject *object)
{
  GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);

  if (G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->constructed != NULL)
    G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->constructed (object);

  gtk_numerable_icon_ensure_emblem (self);
}

static void
gtk_numerable_icon_finalize (GObject *object)
{
  GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);

  g_free (self->priv->label);
  g_free (self->priv->rendered_string);

  gdk_rgba_free (self->priv->background);
  gdk_rgba_free (self->priv->foreground);

  pango_font_description_free (self->priv->font);

  cairo_pattern_destroy (self->priv->background_image);

  G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->finalize (object);
}

static void
gtk_numerable_icon_dispose (GObject *object)
{
  GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);

  if (self->priv->style_changed_id != 0)
    {
      g_signal_handler_disconnect (self->priv->style,
                                   self->priv->style_changed_id);
      self->priv->style_changed_id = 0;
    }

  g_clear_object (&self->priv->style);
  g_clear_object (&self->priv->background_icon);

  G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->dispose (object);
}

static void
gtk_numerable_icon_set_property (GObject      *object,
                                 guint         property_id,
                                 const GValue *value,
                                 GParamSpec   *pspec)
{
  GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);

  switch (property_id)
    {
    case PROP_COUNT:
      gtk_numerable_icon_set_count (self, g_value_get_int (value));
      break;
    case PROP_LABEL:
      gtk_numerable_icon_set_label (self, g_value_get_string (value));
      break;
    case PROP_STYLE:
      gtk_numerable_icon_set_style_context (self, g_value_get_object (value));
      break;
    case PROP_BACKGROUND_ICON:
      gtk_numerable_icon_set_background_gicon (self, g_value_get_object (value));
      break;
    case PROP_BACKGROUND_ICON_NAME:
      gtk_numerable_icon_set_background_icon_name (self, g_value_get_string (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gtk_numerable_icon_get_property (GObject    *object,
                                 guint       property_id,
                                 GValue     *value,
                                 GParamSpec *pspec)
{
  GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);

  switch (property_id)
    {
    case PROP_COUNT:
      g_value_set_int (value, self->priv->count);
      break;
    case PROP_LABEL:
      g_value_set_string (value, self->priv->label);
      break;
    case PROP_STYLE:
      g_value_set_object (value, self->priv->style);
      break;
    case PROP_BACKGROUND_ICON:
      if (self->priv->background_icon != NULL)
        g_value_set_object (value, self->priv->background_icon);
      break;
    case PROP_BACKGROUND_ICON_NAME:
      g_value_set_string (value, self->priv->background_icon_name);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gtk_numerable_icon_class_init (GtkNumerableIconClass *klass)
{
  GObjectClass *oclass = G_OBJECT_CLASS (klass);

  oclass->get_property = gtk_numerable_icon_get_property;
  oclass->set_property = gtk_numerable_icon_set_property;
  oclass->constructed = gtk_numerable_icon_constructed;
  oclass->dispose = gtk_numerable_icon_dispose;
  oclass->finalize = gtk_numerable_icon_finalize;

  properties[PROP_COUNT] =
    g_param_spec_int ("count",
                      P_("Icon's count"),
                      P_("The count of the emblem currently displayed"),
                      -99, 99, 0,
                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  properties[PROP_LABEL] =
    g_param_spec_string ("label",
                         P_("Icon's label"),
                         P_("The label to be displayed over the icon"),
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  properties[PROP_STYLE] =
    g_param_spec_object ("style-context",
                         P_("Icon's style context"),
                         P_("The style context to theme the icon appearance"),
                         GTK_TYPE_STYLE_CONTEXT,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  properties[PROP_BACKGROUND_ICON] =
    g_param_spec_object ("background-icon",
                         P_("Background icon"),
                         P_("The icon for the number emblem background"),
                         G_TYPE_ICON,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  properties[PROP_BACKGROUND_ICON_NAME] =
    g_param_spec_string ("background-icon-name",
                         P_("Background icon name"),
                         P_("The icon name for the number emblem background"),
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (oclass, NUM_PROPERTIES, properties);
}

static void
gtk_numerable_icon_init (GtkNumerableIcon *self)
{
  GdkRGBA bg;
  GdkRGBA fg;

681
  self->priv = gtk_numerable_icon_get_instance_private (self);
Matthias Clasen's avatar
Matthias Clasen committed
682 683 684 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 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

  gdk_rgba_parse (&bg, DEFAULT_BACKGROUND);
  gdk_rgba_parse (&fg, DEFAULT_FOREGROUND);

  self->priv->background = gdk_rgba_copy (&bg);
  self->priv->foreground = gdk_rgba_copy (&fg);

  self->priv->icon_size = 48;
}

/* private */
void
_gtk_numerable_icon_set_background_icon_size (GtkNumerableIcon *self,
                                              gint              icon_size)
{
  if (self->priv->background_icon == NULL)
    return;

  if (self->priv->icon_size != icon_size)
    {
      self->priv->icon_size = icon_size;
      gtk_numerable_icon_ensure_emblem (self);
    }
}

/**
 * gtk_numerable_icon_get_label:
 * @self: a #GtkNumerableIcon
 *
 * Returns the currently displayed label of the icon, or %NULL.
 *
 * Returns: the currently displayed label
 *
 * Since: 3.0
 */
const gchar *
gtk_numerable_icon_get_label (GtkNumerableIcon *self)
{
  g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);

  return self->priv->label;
}

/**
 * gtk_numerable_icon_set_label:
 * @self: a #GtkNumerableIcon
 * @label: (allow-none): a short label, or %NULL
 *
 * Sets the currently displayed value of @self to the string
 * in @label. Setting an empty label removes the emblem.
 *
 * Note that this is meant for displaying short labels, such as
 * roman numbers, or single letters. For roman numbers, consider
 * using the Unicode characters U+2160 - U+217F. Strings longer
 * than two characters will likely not be rendered very well.
 *
 * If this method is called, and a number was already set on the
 * icon, it will automatically be reset to zero before rendering
 * the label, i.e. the last method called between
 * gtk_numerable_icon_set_label() and gtk_numerable_icon_set_count()
 * has always priority.
 *
 * Since: 3.0
 */
void
gtk_numerable_icon_set_label (GtkNumerableIcon *self,
                              const gchar      *label)
{
  g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));

  if (g_strcmp0 (label, self->priv->label) != 0)
    {
      gtk_numerable_icon_ensure_and_replace_label (self, 0, label);
      gtk_numerable_icon_ensure_emblem (self);

      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_LABEL]);
    }
}

/**
 * gtk_numerable_icon_get_count:
 * @self: a #GtkNumerableIcon
 *
 * Returns the value currently displayed by @self.
 *
 * Returns: the currently displayed value
 *
 * Since: 3.0
 */
gint
gtk_numerable_icon_get_count (GtkNumerableIcon *self)
{
  g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), 0);

  return self->priv->count;
}

/**
 * gtk_numerable_icon_set_count:
 * @self: a #GtkNumerableIcon
 * @count: a number between -99 and 99
 *
 * Sets the currently displayed value of @self to @count.
 *
 * The numeric value is always clamped to make it two digits, i.e.
 * between -99 and 99. Setting a count of zero removes the emblem.
 * If this method is called, and a label was already set on the icon,
 * it will automatically be reset to %NULL before rendering the number,
 * i.e. the last method called between gtk_numerable_icon_set_count()
 * and gtk_numerable_icon_set_label() has always priority.
 *
 * Since: 3.0
 */
void
gtk_numerable_icon_set_count (GtkNumerableIcon *self,
                              gint              count)
{
  g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));

  if (count != self->priv->count)
    {
      gtk_numerable_icon_ensure_and_replace_label (self, count, NULL);
      gtk_numerable_icon_ensure_emblem (self);

      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COUNT]);
    }
}

/**
 * gtk_numerable_icon_get_style_context:
 * @self: a #GtkNumerableIcon
 *
 * Returns the #GtkStyleContext used by the icon for theming,
815
 * or %NULL if there’s none.
Matthias Clasen's avatar
Matthias Clasen committed
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
 *
 * Returns: (transfer none): a #GtkStyleContext, or %NULL.
 *     This object is internal to GTK+ and should not be unreffed.
 *     Use g_object_ref() if you want to keep it around
 *
 * Since: 3.0
 */
GtkStyleContext *
gtk_numerable_icon_get_style_context (GtkNumerableIcon *self)
{
  g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);

  return self->priv->style;
}

/**
 * gtk_numerable_icon_set_style_context:
 * @self: a #GtkNumerableIcon
 * @style: a #GtkStyleContext
 *
 * Updates the icon to fetch theme information from the
 * given #GtkStyleContext.
 *
 * Since: 3.0
 */
void
gtk_numerable_icon_set_style_context (GtkNumerableIcon *self,
                                      GtkStyleContext  *style)
{
  g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (style));

  if (style != self->priv->style)
    {
      if (self->priv->style_changed_id != 0)
        g_signal_handler_disconnect (self->priv->style,
                                     self->priv->style_changed_id);

      if (self->priv->style != NULL)
        g_object_unref (self->priv->style);

      self->priv->style = g_object_ref (style);

      gtk_numerable_icon_init_style (self);

      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STYLE]);
    }
}

/**
 * gtk_numerable_icon_set_background_gicon:
 * @self: a #GtkNumerableIcon
 * @icon: (allow-none): a #GIcon, or %NULL
 *
 * Updates the icon to use @icon as the base background image.
 * If @icon is %NULL, @self will go back using style information
 * or default theming for its background image.
 *
 * If this method is called and an icon name was already set as
 * background for the icon, @icon will be used, i.e. the last method
 * called between gtk_numerable_icon_set_background_gicon() and
877
 * gtk_numerable_icon_set_background_icon_name() has always priority.
Matthias Clasen's avatar
Matthias Clasen committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
 *
 * Since: 3.0
 */
void
gtk_numerable_icon_set_background_gicon (GtkNumerableIcon *self,
                                         GIcon            *icon)
{
  gboolean res;

  g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));

  if (self->priv->background_icon_name != NULL)
    {
      g_free (self->priv->background_icon_name);
      self->priv->background_icon_name = NULL;
    }

  res = real_set_background_icon (self, icon);

  if (res)
    g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_BACKGROUND_ICON]);
}

/**
 * gtk_numerable_icon_get_background_gicon:
 * @self: a #GtkNumerableIcon
 *
 * Returns the #GIcon that was set as the base background image, or
906
 * %NULL if there’s none. The caller of this function does not own
Matthias Clasen's avatar
Matthias Clasen committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
 * a reference to the returned #GIcon.
 *
 * Returns: (transfer none): a #GIcon, or %NULL
 *
 * Since: 3.0
 */
GIcon *
gtk_numerable_icon_get_background_gicon (GtkNumerableIcon *self)
{
  GIcon *retval = NULL;

  g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);

  /* return the GIcon only if it wasn't created from an icon name */
  if (self->priv->background_icon_name == NULL)
    retval = self->priv->background_icon;

  return retval;
}

/**
 * gtk_numerable_icon_set_background_icon_name:
 * @self: a #GtkNumerableIcon
 * @icon_name: (allow-none): an icon name, or %NULL
 *
 * Updates the icon to use the icon named @icon_name from the
 * current icon theme as the base background image. If @icon_name
 * is %NULL, @self will go back using style information or default
 * theming for its background image.
 *
 * If this method is called and a #GIcon was already set as
 * background for the icon, @icon_name will be used, i.e. the
 * last method called between gtk_numerable_icon_set_background_icon_name()
 * and gtk_numerable_icon_set_background_gicon() has always priority.
 *
 * Since: 3.0
 */
void
gtk_numerable_icon_set_background_icon_name (GtkNumerableIcon *self,
                                             const gchar      *icon_name)
{
  GIcon *icon = NULL;
  gboolean res;

  g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));

  if (g_strcmp0 (icon_name, self->priv->background_icon_name) != 0)
    {
      g_free (self->priv->background_icon_name);
      self->priv->background_icon_name = g_strdup (icon_name);
    }

  if (icon_name != NULL)
    icon = g_themed_icon_new_with_default_fallbacks (icon_name);

  res = real_set_background_icon (self, icon);

  if (res)
    g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_BACKGROUND_ICON_NAME]);

  if (icon != NULL)
    g_object_unref (icon);
}

/**
 * gtk_numerable_icon_get_background_icon_name:
 * @self: a #GtkNumerableIcon
 *
 * Returns the icon name used as the base background image,
976
 * or %NULL if there’s none.
Matthias Clasen's avatar
Matthias Clasen committed
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
 *
 * Returns: an icon name, or %NULL
 *
 * Since: 3.0
 */
const gchar *
gtk_numerable_icon_get_background_icon_name (GtkNumerableIcon *self)
{
  g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);

  return self->priv->background_icon_name;
}

/**
 * gtk_numerable_icon_new:
 * @base_icon: a #GIcon to overlay on
 *
 * Creates a new unthemed #GtkNumerableIcon.
 *
996
 * Returns: (transfer full): a new #GIcon
Matthias Clasen's avatar
Matthias Clasen committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
 *
 * Since: 3.0
 */
GIcon *
gtk_numerable_icon_new (GIcon *base_icon)
{
  g_return_val_if_fail (G_IS_ICON (base_icon), NULL);

  return g_object_new (GTK_TYPE_NUMERABLE_ICON,
                       "gicon", base_icon,
                       NULL);
}

/**
 * gtk_numerable_icon_new_with_style_context:
 * @base_icon: a #GIcon to overlay on
 * @context: a #GtkStyleContext
 *
 * Creates a new #GtkNumerableIcon which will themed according
 * to the passed #GtkStyleContext. This is a convenience constructor
 * that calls gtk_numerable_icon_set_style_context() internally.
 *
1019
 * Returns: (transfer full): a new #GIcon
Matthias Clasen's avatar
Matthias Clasen committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
 *
 * Since: 3.0
 */
GIcon *
gtk_numerable_icon_new_with_style_context (GIcon           *base_icon,
                                           GtkStyleContext *context)
{
  g_return_val_if_fail (G_IS_ICON (base_icon), NULL);

  return g_object_new (GTK_TYPE_NUMERABLE_ICON,
                       "gicon", base_icon,
                       "style-context", context,
                       NULL);
}