1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 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
 159
 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
 215
 216
 217
 218
 219
 220
 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
 681
 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
 815
 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
 877
 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
 906
 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
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
/*
 * ctknumerableicon.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
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Cosimo Cecchi <cosimoc@redhat.com>
 */

/**
 * SECTION:ctknumerableicon
 * @Title: CtkNumerableIcon
 * @Short_description: A GIcon that allows numbered emblems
 *
 * CtkNumerableIcon 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 #CtkStyleContext; see
 * ctk_numerable_icon_set_style_context().
 *
 * Typical numerable icons:
 * ![](numerableicon.png)
 * ![](numerableicon2.png)
 */
#include <config.h>

#include "ctknumerableicon.h"
#include "ctknumerableiconprivate.h"

#include "ctkcssiconthemevalueprivate.h"
#include "ctkicontheme.h"
#include "ctkintl.h"
#include "ctkstylepropertyprivate.h"
#include "ctkwidget.h"
#include "ctkwidgetpath.h"
#include "ctkwindow.h"

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

G_GNUC_BEGIN_IGNORE_DEPRECATIONS

struct _CtkNumerableIconPrivate {
  gint count;
  gint icon_size;

  gchar *label;

  GIcon *background_icon;
  gchar *background_icon_name;

  CdkRGBA *background;
  CdkRGBA *foreground;

  PangoFontDescription *font;
  cairo_pattern_t *background_image;
  gint border_size;

  CtkStyleContext *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, };

G_DEFINE_TYPE_WITH_PRIVATE (CtkNumerableIcon, ctk_numerable_icon, G_TYPE_EMBLEMED_ICON)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE_WITH_PRIVATE is a macro then please configure it.

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 (CtkNumerableIcon *self)
{
  return self->priv->border_size;
}

static cairo_surface_t *
draw_default_surface (CtkNumerableIcon *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.,
             DEFAULT_RADIUS, 0., 2 * G_PI);

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

  cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
             DEFAULT_RADIUS - DEFAULT_BORDER_SIZE, 0., 2 * G_PI);
  cdk_cairo_set_source_rgba  (cr, self->priv->foreground);
  cairo_fill (cr);

  cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
             DEFAULT_RADIUS - 2 * DEFAULT_BORDER_SIZE, 0., 2 * G_PI);
  cdk_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.,
             DEFAULT_RADIUS, 0., 2 * G_PI);

  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 (CtkNumerableIcon *self)
{
  CtkIconTheme *theme;
  CtkIconInfo *info;
  GdkPixbuf *pixbuf;
  cairo_surface_t *surface;

  if (self->priv->style != NULL)
    {
      theme = ctk_css_icon_theme_value_get_icon_theme
          (_ctk_style_context_peek_property (self->priv->style, CTK_CSS_PROPERTY_ICON_THEME));
    }
  else
    {
      theme = ctk_icon_theme_get_default ();
    }

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

  pixbuf = ctk_icon_info_load_icon (info, NULL);
  g_object_unref (info);

  if (pixbuf == NULL)
    return NULL;

  surface = cdk_cairo_surface_create_from_pixbuf (pixbuf, 1, NULL);
  g_object_unref (pixbuf);

  return surface;
}

static cairo_surface_t *
get_image_surface (CtkNumerableIcon *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 (CtkNumerableIcon *self)
{
  PangoLayout *layout;

  if (self->priv->style != NULL)
    {
      PangoContext *context;
      CdkScreen *screen;

      screen = ctk_style_context_get_screen (self->priv->style);
      context = cdk_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
    {
      CtkWidget *fake;

      /* steal ctk text settings from the window */
      fake = ctk_window_new (CTK_WINDOW_TOPLEVEL);
      layout = ctk_widget_create_pango_layout (fake, self->priv->rendered_string);
      ctk_widget_destroy (fake);
    }

  return layout;
}

static void
ctk_numerable_icon_ensure_emblem (CtkNumerableIcon *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.);

  cdk_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
ctk_numerable_icon_update_properties_from_style (CtkNumerableIcon *self)
{
  CtkStyleContext *style = self->priv->style;
  CtkWidgetPath *path, *saved;
  cairo_pattern_t *pattern = NULL;
  CdkRGBA background, foreground;
  PangoFontDescription *font = NULL;

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

  if (!ctk_widget_path_is_type (path, CTK_TYPE_NUMERABLE_ICON))
    {
      /* append our GType to the style context to fetch appropriate colors */
      ctk_widget_path_append_type (path, CTK_TYPE_NUMERABLE_ICON);
      ctk_style_context_set_path (style, path);
    }

  ctk_style_context_get_background_color (style, ctk_style_context_get_state (style),
                                          &background);
  ctk_style_context_get_color (style, ctk_style_context_get_state (style),
                               &foreground);

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

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

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

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

  ctk_style_context_get (style, ctk_style_context_get_state (style),
                         CTK_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;
    }

  ctk_style_context_get (style, ctk_style_context_get_state (style),
                         CTK_STYLE_PROPERTY_FONT, &font,
                         NULL);

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

      self->priv->font = font;
    }

  ctk_numerable_icon_ensure_emblem (self);

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

  ctk_widget_path_free (path);
  ctk_widget_path_free (saved);
}

static void
ctk_numerable_icon_init_style (CtkNumerableIcon *self)
{
  CtkStyleContext *style = self->priv->style;

  if (style == NULL)
    return;

  ctk_numerable_icon_update_properties_from_style (self);

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

static void
ctk_numerable_icon_ensure_and_replace_label (CtkNumerableIcon *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 (CtkNumerableIcon *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);

      ctk_numerable_icon_ensure_emblem (self);

      return TRUE;
    }

  return FALSE;
}

static void
ctk_numerable_icon_constructed (GObject *object)
{
  CtkNumerableIcon *self = CTK_NUMERABLE_ICON (object);

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

  ctk_numerable_icon_ensure_emblem (self);
}

static void
ctk_numerable_icon_finalize (GObject *object)
{
  CtkNumerableIcon *self = CTK_NUMERABLE_ICON (object);

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

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

  pango_font_description_free (self->priv->font);

  cairo_pattern_destroy (self->priv->background_image);

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

static void
ctk_numerable_icon_dispose (GObject *object)
{
  CtkNumerableIcon *self = CTK_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 (ctk_numerable_icon_parent_class)->dispose (object);
}

static void
ctk_numerable_icon_set_property (GObject      *object,
                                 guint         property_id,
                                 const GValue *value,
                                 GParamSpec   *pspec)
{
  CtkNumerableIcon *self = CTK_NUMERABLE_ICON (object);

  switch (property_id)
    {
    case PROP_COUNT:
      ctk_numerable_icon_set_count (self, g_value_get_int (value));
      break;
    case PROP_LABEL:
      ctk_numerable_icon_set_label (self, g_value_get_string (value));
      break;
    case PROP_STYLE:
      ctk_numerable_icon_set_style_context (self, g_value_get_object (value));
      break;
    case PROP_BACKGROUND_ICON:
      ctk_numerable_icon_set_background_gicon (self, g_value_get_object (value));
      break;
    case PROP_BACKGROUND_ICON_NAME:
      ctk_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
ctk_numerable_icon_get_property (GObject    *object,
                                 guint       property_id,
                                 GValue     *value,
                                 GParamSpec *pspec)
{
  CtkNumerableIcon *self = CTK_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
ctk_numerable_icon_class_init (CtkNumerableIconClass *klass)
{
  GObjectClass *oclass = G_OBJECT_CLASS (klass);

  oclass->get_property = ctk_numerable_icon_get_property;
  oclass->set_property = ctk_numerable_icon_set_property;
  oclass->constructed = ctk_numerable_icon_constructed;
  oclass->dispose = ctk_numerable_icon_dispose;
  oclass->finalize = ctk_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"),
                         CTK_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
ctk_numerable_icon_init (CtkNumerableIcon *self)
{
  CdkRGBA bg;
  CdkRGBA fg;

  self->priv = ctk_numerable_icon_get_instance_private (self);

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

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

  self->priv->icon_size = 48;
}

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

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

/**
 * ctk_numerable_icon_get_label:
 * @self: a #CtkNumerableIcon
 *
 * Returns the currently displayed label of the icon, or %NULL.
 *
 * Returns: (nullable): the currently displayed label
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
const gchar *
ctk_numerable_icon_get_label (CtkNumerableIcon *self)
{
  g_return_val_if_fail (CTK_IS_NUMERABLE_ICON (self), NULL);

  return self->priv->label;
}

/**
 * ctk_numerable_icon_set_label:
 * @self: a #CtkNumerableIcon
 * @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
 * ctk_numerable_icon_set_label() and ctk_numerable_icon_set_count()
 * has always priority.
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
void
ctk_numerable_icon_set_label (CtkNumerableIcon *self,
                              const gchar      *label)
{
  g_return_if_fail (CTK_IS_NUMERABLE_ICON (self));

  if (g_strcmp0 (label, self->priv->label) != 0)
    {
      ctk_numerable_icon_ensure_and_replace_label (self, 0, label);
      ctk_numerable_icon_ensure_emblem (self);

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

/**
 * ctk_numerable_icon_get_count:
 * @self: a #CtkNumerableIcon
 *
 * Returns the value currently displayed by @self.
 *
 * Returns: the currently displayed value
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
gint
ctk_numerable_icon_get_count (CtkNumerableIcon *self)
{
  g_return_val_if_fail (CTK_IS_NUMERABLE_ICON (self), 0);

  return self->priv->count;
}

/**
 * ctk_numerable_icon_set_count:
 * @self: a #CtkNumerableIcon
 * @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 ctk_numerable_icon_set_count()
 * and ctk_numerable_icon_set_label() has always priority.
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
void
ctk_numerable_icon_set_count (CtkNumerableIcon *self,
                              gint              count)
{
  g_return_if_fail (CTK_IS_NUMERABLE_ICON (self));

  if (count != self->priv->count)
    {
      ctk_numerable_icon_ensure_and_replace_label (self, count, NULL);
      ctk_numerable_icon_ensure_emblem (self);

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

/**
 * ctk_numerable_icon_get_style_context:
 * @self: a #CtkNumerableIcon
 *
 * Returns the #CtkStyleContext used by the icon for theming,
 * or %NULL if there’s none.
 *
 * Returns: (nullable) (transfer none): a #CtkStyleContext, or %NULL.
 *     This object is internal to CTK+ and should not be unreffed.
 *     Use g_object_ref() if you want to keep it around
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
CtkStyleContext *
ctk_numerable_icon_get_style_context (CtkNumerableIcon *self)
{
  g_return_val_if_fail (CTK_IS_NUMERABLE_ICON (self), NULL);

  return self->priv->style;
}

/**
 * ctk_numerable_icon_set_style_context:
 * @self: a #CtkNumerableIcon
 * @style: a #CtkStyleContext
 *
 * Updates the icon to fetch theme information from the
 * given #CtkStyleContext.
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
void
ctk_numerable_icon_set_style_context (CtkNumerableIcon *self,
                                      CtkStyleContext  *style)
{
  g_return_if_fail (CTK_IS_NUMERABLE_ICON (self));
  g_return_if_fail (CTK_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);

      ctk_numerable_icon_init_style (self);

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

/**
 * ctk_numerable_icon_set_background_gicon:
 * @self: a #CtkNumerableIcon
 * @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 ctk_numerable_icon_set_background_gicon() and
 * ctk_numerable_icon_set_background_icon_name() has always priority.
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
void
ctk_numerable_icon_set_background_gicon (CtkNumerableIcon *self,
                                         GIcon            *icon)
{
  gboolean res;

  g_return_if_fail (CTK_IS_NUMERABLE_ICON (self));

  g_clear_pointer (&self->priv->background_icon_name, g_free);

  res = real_set_background_icon (self, icon);

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

/**
 * ctk_numerable_icon_get_background_gicon:
 * @self: a #CtkNumerableIcon
 *
 * Returns the #GIcon that was set as the base background image, or
 * %NULL if there’s none. The caller of this function does not own
 * a reference to the returned #GIcon.
 *
 * Returns: (nullable) (transfer none): a #GIcon, or %NULL
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
GIcon *
ctk_numerable_icon_get_background_gicon (CtkNumerableIcon *self)
{
  GIcon *retval = NULL;

  g_return_val_if_fail (CTK_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;
}

/**
 * ctk_numerable_icon_set_background_icon_name:
 * @self: a #CtkNumerableIcon
 * @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 ctk_numerable_icon_set_background_icon_name()
 * and ctk_numerable_icon_set_background_gicon() has always priority.
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
void
ctk_numerable_icon_set_background_icon_name (CtkNumerableIcon *self,
                                             const gchar      *icon_name)
{
  GIcon *icon = NULL;
  gboolean res;

  g_return_if_fail (CTK_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);
}

/**
 * ctk_numerable_icon_get_background_icon_name:
 * @self: a #CtkNumerableIcon
 *
 * Returns the icon name used as the base background image,
 * or %NULL if there’s none.
 *
 * Returns: (nullable): an icon name, or %NULL
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
const gchar *
ctk_numerable_icon_get_background_icon_name (CtkNumerableIcon *self)
{
  g_return_val_if_fail (CTK_IS_NUMERABLE_ICON (self), NULL);

  return self->priv->background_icon_name;
}

/**
 * ctk_numerable_icon_new:
 * @base_icon: a #GIcon to overlay on
 *
 * Creates a new unthemed #CtkNumerableIcon.
 *
 * Returns: (transfer full): a new #GIcon
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
GIcon *
ctk_numerable_icon_new (GIcon *base_icon)
{
  g_return_val_if_fail (G_IS_ICON (base_icon), NULL);

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

/**
 * ctk_numerable_icon_new_with_style_context:
 * @base_icon: a #GIcon to overlay on
 * @context: a #CtkStyleContext
 *
 * Creates a new #CtkNumerableIcon which will themed according
 * to the passed #CtkStyleContext. This is a convenience constructor
 * that calls ctk_numerable_icon_set_style_context() internally.
 *
 * Returns: (transfer full): a new #GIcon
 *
 * Since: 3.0
 *
 * Deprecated: 3.14
 */
GIcon *
ctk_numerable_icon_new_with_style_context (GIcon           *base_icon,
                                           CtkStyleContext *context)
{
  g_return_val_if_fail (G_IS_ICON (base_icon), NULL);

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