]> Pileus Git - ~andy/gtk/blob - gtk/gtknumerableicon.c
gtk: Get gtkwidgetpath.h includes out of the public headers
[~andy/gtk] / gtk / gtknumerableicon.c
1 /*
2  * gtknumerableicon.c: an emblemed icon with number emblems
3  *
4  * Copyright (C) 2010 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Authors: Cosimo Cecchi <cosimoc@redhat.com>
20  */
21
22 /**
23  * SECTION:gtknumerableicon
24  * @Title: GtkNumerableIcon
25  * @Short_description: A GIcon that allows numbered emblems
26  *
27  * GtkNumerableIcon is a subclass of #GEmblemedIcon that can
28  * show a number or short string as an emblem. The number can
29  * be overlayed on top of another emblem, if desired.
30  *
31  * It supports theming by taking font and color information
32  * from a provided #GtkStyleContext; see
33  * gtk_numerable_icon_set_style_context().
34  *
35  * <example>
36  * <title>Typical numerable icons</title>
37  * <inlinegraphic fileref="numerableicon.png" format="PNG"/>
38  * <inlinegraphic fileref="numerableicon2.png" format="PNG"/>
39  * </example>
40  */
41 #include <config.h>
42
43 #include "gtknumerableicon.h"
44
45 #include "gtkicontheme.h"
46 #include "gtkintl.h"
47 #include "gtkwidget.h"
48 #include "gtkwidgetpath.h"
49 #include "gtkwindow.h"
50
51 #include <gdk/gdk.h>
52 #include <pango/pango.h>
53 #include <math.h>
54
55 struct _GtkNumerableIconPrivate {
56   gint count;
57   gint icon_size;
58
59   gchar *label;
60
61   GIcon *background_icon;
62   gchar *background_icon_name;
63
64   GdkRGBA *background;
65   GdkRGBA *foreground;
66
67   PangoFontDescription *font;
68   cairo_pattern_t *background_image;
69   gint border_size;
70
71   GtkStyleContext *style;
72   gulong style_changed_id;
73
74   gchar *rendered_string;
75 };
76
77 enum {
78   PROP_COUNT = 1,
79   PROP_LABEL,
80   PROP_STYLE,
81   PROP_BACKGROUND_ICON,
82   PROP_BACKGROUND_ICON_NAME,
83   NUM_PROPERTIES
84 };
85
86 #define DEFAULT_SURFACE_SIZE 256
87 #define DEFAULT_BORDER_SIZE DEFAULT_SURFACE_SIZE * 0.06
88 #define DEFAULT_RADIUS DEFAULT_SURFACE_SIZE / 2
89
90 #define DEFAULT_BACKGROUND "#000000"
91 #define DEFAULT_FOREGROUND "#ffffff"
92
93 static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
94
95 G_DEFINE_TYPE (GtkNumerableIcon, gtk_numerable_icon, G_TYPE_EMBLEMED_ICON);
96
97 static gint
98 get_surface_size (cairo_surface_t *surface)
99 {
100   return MAX (cairo_image_surface_get_width (surface), cairo_image_surface_get_height (surface));
101 }
102
103 static gdouble
104 get_border_size (GtkNumerableIcon *self)
105 {
106   return self->priv->border_size;
107 }
108
109 static cairo_surface_t *
110 draw_default_surface (GtkNumerableIcon *self)
111 {
112   cairo_surface_t *surface;
113   cairo_t *cr;
114
115   surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
116                                         DEFAULT_SURFACE_SIZE, DEFAULT_SURFACE_SIZE);
117
118   cr = cairo_create (surface);
119
120   cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
121              DEFAULT_RADIUS, 0., 2 * G_PI);
122
123   gdk_cairo_set_source_rgba (cr, self->priv->background);
124   cairo_fill (cr);
125
126   cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
127              DEFAULT_RADIUS - DEFAULT_BORDER_SIZE, 0., 2 * G_PI);
128   gdk_cairo_set_source_rgba  (cr, self->priv->foreground);
129   cairo_fill (cr);
130
131   cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
132              DEFAULT_RADIUS - 2 * DEFAULT_BORDER_SIZE, 0., 2 * G_PI);
133   gdk_cairo_set_source_rgba  (cr, self->priv->background);
134   cairo_fill (cr);
135
136   cairo_destroy (cr);
137
138   return surface;
139 }
140
141 static cairo_surface_t *
142 draw_from_gradient (cairo_pattern_t *pattern)
143 {
144   cairo_surface_t *surface;
145   cairo_matrix_t matrix;
146   cairo_t *cr;
147
148   surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
149                                         DEFAULT_SURFACE_SIZE, DEFAULT_SURFACE_SIZE);
150
151   cr = cairo_create (surface);
152
153   /* scale the gradient points to the user space coordinates */
154   cairo_matrix_init_scale (&matrix,
155                            1. / (double) DEFAULT_SURFACE_SIZE,
156                            1. / (double) DEFAULT_SURFACE_SIZE);
157   cairo_pattern_set_matrix (pattern, &matrix);
158
159   cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
160              DEFAULT_RADIUS, 0., 2 * G_PI);
161
162   cairo_set_source (cr, pattern);
163   cairo_fill (cr);
164
165   cairo_destroy (cr);
166
167   return surface;
168 }
169
170 /* copy the surface */
171 static cairo_surface_t *
172 draw_from_image (cairo_surface_t *image)
173 {
174   cairo_surface_t *surface;
175   cairo_t *cr;
176
177   surface = cairo_surface_create_similar (image, CAIRO_CONTENT_COLOR_ALPHA,
178                                           cairo_image_surface_get_width (image),
179                                           cairo_image_surface_get_height (image));
180   cr = cairo_create (surface);
181
182   cairo_set_source_surface (cr, image, 0, 0);
183   cairo_paint (cr);
184
185   cairo_destroy (cr);
186
187   return surface;
188 }
189
190 static cairo_surface_t *
191 draw_from_gicon (GtkNumerableIcon *self)
192 {
193   GtkIconTheme *theme;
194   GdkScreen *screen;
195   GtkIconInfo *info;
196   GdkPixbuf *pixbuf;
197   cairo_surface_t *surface;
198   cairo_t *cr;
199
200   if (self->priv->style != NULL)
201     {
202       screen = gtk_style_context_get_screen (self->priv->style);
203       theme = gtk_icon_theme_get_for_screen (screen);
204     }
205   else
206     {
207       theme = gtk_icon_theme_get_default ();
208     }
209
210   info = gtk_icon_theme_lookup_by_gicon (theme, self->priv->background_icon,
211                                          self->priv->icon_size,
212                                          GTK_ICON_LOOKUP_GENERIC_FALLBACK);
213   if (info == NULL)
214     return NULL;
215
216   pixbuf = gtk_icon_info_load_icon (info, NULL);
217   gtk_icon_info_free (info);
218
219   if (pixbuf == NULL)
220     return NULL;
221
222   surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
223                                         gdk_pixbuf_get_width (pixbuf),
224                                         gdk_pixbuf_get_height (pixbuf));
225
226   cr = cairo_create (surface);
227
228   gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
229   cairo_paint (cr);
230
231   cairo_destroy (cr);
232   g_object_unref (pixbuf);
233
234   return surface;
235 }
236
237 static cairo_surface_t *
238 get_image_surface (GtkNumerableIcon *self)
239 {
240   cairo_surface_t *retval = NULL, *image;
241
242   if (self->priv->background_icon != NULL)
243     {
244       retval = draw_from_gicon (self);
245       self->priv->border_size = 0;
246     }
247   else if (self->priv->background_image != NULL)
248     {
249       if (cairo_pattern_get_surface (self->priv->background_image, &image) == CAIRO_STATUS_SUCCESS)
250         retval = draw_from_image (image);
251       else
252         retval = draw_from_gradient (self->priv->background_image);
253
254       self->priv->border_size = 0;
255     }
256
257   if (retval == NULL)
258     {
259       retval = draw_default_surface (self);
260       self->priv->border_size = DEFAULT_BORDER_SIZE;
261     }
262
263   return retval;
264 }
265
266 static PangoLayout *
267 get_pango_layout (GtkNumerableIcon *self)
268 {
269   PangoContext *context;
270   GdkScreen *screen;
271   PangoLayout *layout;
272
273   if (self->priv->style != NULL)
274     {
275       screen = gtk_style_context_get_screen (self->priv->style);
276       context = gdk_pango_context_get_for_screen (screen);
277       layout = pango_layout_new (context);
278
279       if (self->priv->font != NULL)
280         pango_layout_set_font_description (layout, self->priv->font);
281
282       pango_layout_set_text (layout, self->priv->rendered_string, -1);
283
284       g_object_unref (context);
285     }
286   else
287     {
288       GtkWidget *fake;
289
290       /* steal gtk text settings from the window */
291       fake = gtk_window_new (GTK_WINDOW_TOPLEVEL);
292       layout = gtk_widget_create_pango_layout (fake, self->priv->rendered_string);
293       gtk_widget_destroy (fake);
294     }
295
296   return layout;
297 }
298
299 static void
300 gtk_numerable_icon_ensure_emblem (GtkNumerableIcon *self)
301 {
302   cairo_t *cr;
303   cairo_surface_t *surface;
304   PangoLayout *layout;
305   GEmblem *emblem;
306   gint width, height;
307   gdouble scale;
308   PangoAttrList *attr_list;
309   PangoAttribute *attr;
310   GdkPixbuf *pixbuf;
311
312   /* don't draw anything if the count is zero */
313   if (self->priv->rendered_string == NULL)
314     {
315       g_emblemed_icon_clear_emblems (G_EMBLEMED_ICON (self));
316       return;
317     }
318
319   surface = get_image_surface (self);
320   cr = cairo_create (surface);
321
322   layout = get_pango_layout (self);
323   pango_layout_get_pixel_size (layout, &width, &height);
324
325   /* scale the layout to be 0.75 of the size still available for drawing */
326   scale = ((get_surface_size (surface) - 2 * get_border_size (self)) * 0.75) / (MAX (height, width));
327   attr_list = pango_attr_list_new ();
328
329   attr = pango_attr_scale_new (scale);
330   pango_attr_list_insert (attr_list, attr);
331
332   attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
333   pango_attr_list_insert (attr_list, attr);
334
335   pango_layout_set_attributes (layout, attr_list);
336
337   /* update these values */
338   pango_layout_get_pixel_size (layout, &width, &height);
339
340   /* move to the center */
341   cairo_move_to (cr,
342                  get_surface_size (surface) / 2. - (gdouble) width / 2.,
343                  get_surface_size (surface) / 2. - (gdouble) height / 2.);
344
345   gdk_cairo_set_source_rgba (cr, self->priv->foreground);
346   pango_cairo_show_layout (cr, layout);
347
348   cairo_destroy (cr);
349
350   pixbuf =
351     gdk_pixbuf_get_from_surface (surface, 0, 0,
352                                  get_surface_size (surface), get_surface_size (surface));
353
354   emblem = g_emblem_new (G_ICON (pixbuf));
355   g_emblemed_icon_clear_emblems (G_EMBLEMED_ICON (self));
356   g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (self), emblem);
357
358   g_object_unref (layout);
359   g_object_unref (emblem);
360   g_object_unref (pixbuf);
361
362   cairo_surface_destroy (surface);
363   pango_attr_list_unref (attr_list);
364 }
365
366 static void
367 gtk_numerable_icon_update_properties_from_style (GtkNumerableIcon *self)
368 {
369   GtkStyleContext *style = self->priv->style;
370   GtkWidgetPath *path, *saved;
371   cairo_pattern_t *pattern = NULL;
372   GdkRGBA background, foreground;
373   PangoFontDescription *font = NULL;
374
375   /* save an unmodified copy of the original widget path, in order
376    * to restore it later */
377   path = gtk_widget_path_copy (gtk_style_context_get_path (style));
378   saved = gtk_widget_path_copy (path);
379
380   if (!gtk_widget_path_is_type (path, GTK_TYPE_NUMERABLE_ICON))
381     {
382       /* append our GType to the style context to fetch appropriate colors */
383       gtk_widget_path_append_type (path, GTK_TYPE_NUMERABLE_ICON);
384       gtk_style_context_set_path (style, path);
385     }
386
387   gtk_style_context_get_background_color (style, gtk_style_context_get_state (style),
388                                           &background);
389   gtk_style_context_get_color (style, gtk_style_context_get_state (style),
390                                &foreground);
391
392   if (self->priv->background != NULL)
393     gdk_rgba_free (self->priv->background);
394
395   self->priv->background = gdk_rgba_copy (&background);
396
397   if (self->priv->foreground != NULL)
398     gdk_rgba_free (self->priv->foreground);
399
400   self->priv->foreground = gdk_rgba_copy (&foreground);
401
402   gtk_style_context_get (style, gtk_style_context_get_state (style),
403                          GTK_STYLE_PROPERTY_BACKGROUND_IMAGE, &pattern,
404                          NULL);
405
406   if (pattern != NULL)
407     {
408       if (self->priv->background_image != NULL)
409         cairo_pattern_destroy (self->priv->background_image);
410
411       self->priv->background_image = pattern;
412     }
413
414   gtk_style_context_get (style, gtk_style_context_get_state (style),
415                          GTK_STYLE_PROPERTY_FONT, &font,
416                          NULL);
417
418   if (font != NULL)
419     {
420       if (self->priv->font != NULL)
421         pango_font_description_free (self->priv->font);
422
423       self->priv->font = font;
424     }
425
426   gtk_numerable_icon_ensure_emblem (self);
427
428   /* restore original widget path */
429   gtk_style_context_set_path (style, saved);
430
431   gtk_widget_path_free (path);
432   gtk_widget_path_free (saved);
433 }
434
435 static void
436 gtk_numerable_icon_init_style (GtkNumerableIcon *self)
437 {
438   GtkStyleContext *style = self->priv->style;
439
440   if (style == NULL)
441     return;
442
443   gtk_numerable_icon_update_properties_from_style (self);
444
445   self->priv->style_changed_id =
446     g_signal_connect_swapped (style, "changed",
447                               G_CALLBACK (gtk_numerable_icon_update_properties_from_style), self);
448 }
449
450 static void
451 gtk_numerable_icon_ensure_and_replace_label (GtkNumerableIcon *self,
452                                              gint              count,
453                                              const gchar      *label)
454 {
455   g_assert (!(label != NULL && count != 0));
456
457   g_free (self->priv->rendered_string);
458   self->priv->rendered_string = NULL;
459
460   if (count != 0)
461     {
462       if (self->priv->label != NULL)
463         {
464           g_free (self->priv->label);
465           self->priv->label = NULL;
466
467           g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_LABEL]);
468         }
469
470       if (count > 99)
471         count = 99;
472
473       if (count < -99)
474         count = -99;
475
476       self->priv->count = count;
477
478       /* Translators: the format here is used to build the string that will be rendered
479        * in the number emblem.
480        */
481       self->priv->rendered_string = g_strdup_printf (C_("Number format", "%d"), count);
482
483       return;
484     }
485
486   if (label != NULL)
487     {
488       if (self->priv->count != 0)
489         {
490           self->priv->count = 0;
491
492           g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COUNT]);
493         }
494
495       g_free (self->priv->label);
496
497       if (g_strcmp0 (label, "") == 0)
498         {
499           self->priv->label = NULL;
500           return;
501         }
502
503       self->priv->label = g_strdup (label);
504       self->priv->rendered_string = g_strdup (label);
505     }
506 }
507
508 static gboolean
509 real_set_background_icon (GtkNumerableIcon *self,
510                           GIcon            *icon)
511 {
512   if (!g_icon_equal (self->priv->background_icon, icon))
513     {
514       g_clear_object (&self->priv->background_icon);
515
516       if (icon != NULL)
517         self->priv->background_icon = g_object_ref (icon);
518
519       gtk_numerable_icon_ensure_emblem (self);
520
521       return TRUE;
522     }
523
524   return FALSE;
525 }
526
527 static void
528 gtk_numerable_icon_constructed (GObject *object)
529 {
530   GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);
531
532   if (G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->constructed != NULL)
533     G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->constructed (object);
534
535   gtk_numerable_icon_ensure_emblem (self);
536 }
537
538 static void
539 gtk_numerable_icon_finalize (GObject *object)
540 {
541   GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);
542
543   g_free (self->priv->label);
544   g_free (self->priv->rendered_string);
545
546   gdk_rgba_free (self->priv->background);
547   gdk_rgba_free (self->priv->foreground);
548
549   pango_font_description_free (self->priv->font);
550
551   cairo_pattern_destroy (self->priv->background_image);
552
553   G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->finalize (object);
554 }
555
556 static void
557 gtk_numerable_icon_dispose (GObject *object)
558 {
559   GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);
560
561   if (self->priv->style_changed_id != 0)
562     {
563       g_signal_handler_disconnect (self->priv->style,
564                                    self->priv->style_changed_id);
565       self->priv->style_changed_id = 0;
566     }
567
568   g_clear_object (&self->priv->style);
569   g_clear_object (&self->priv->background_icon);
570
571   G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->dispose (object);
572 }
573
574 static void
575 gtk_numerable_icon_set_property (GObject      *object,
576                                  guint         property_id,
577                                  const GValue *value,
578                                  GParamSpec   *pspec)
579 {
580   GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);
581
582   switch (property_id)
583     {
584     case PROP_COUNT:
585       gtk_numerable_icon_set_count (self, g_value_get_int (value));
586       break;
587     case PROP_LABEL:
588       gtk_numerable_icon_set_label (self, g_value_get_string (value));
589       break;
590     case PROP_STYLE:
591       gtk_numerable_icon_set_style_context (self, g_value_get_object (value));
592       break;
593     case PROP_BACKGROUND_ICON:
594       gtk_numerable_icon_set_background_gicon (self, g_value_get_object (value));
595       break;
596     case PROP_BACKGROUND_ICON_NAME:
597       gtk_numerable_icon_set_background_icon_name (self, g_value_get_string (value));
598       break;
599     default:
600       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
601       break;
602     }
603 }
604
605 static void
606 gtk_numerable_icon_get_property (GObject    *object,
607                                  guint       property_id,
608                                  GValue     *value,
609                                  GParamSpec *pspec)
610 {
611   GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);
612
613   switch (property_id)
614     {
615     case PROP_COUNT:
616       g_value_set_int (value, self->priv->count);
617       break;
618     case PROP_LABEL:
619       g_value_set_string (value, self->priv->label);
620       break;
621     case PROP_STYLE:
622       g_value_set_object (value, self->priv->style);
623       break;
624     case PROP_BACKGROUND_ICON:
625       if (self->priv->background_icon != NULL)
626         g_value_set_object (value, self->priv->background_icon);
627       break;
628     case PROP_BACKGROUND_ICON_NAME:
629       g_value_set_string (value, self->priv->background_icon_name);
630       break;
631     default:
632       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
633       break;
634     }
635 }
636
637 static void
638 gtk_numerable_icon_class_init (GtkNumerableIconClass *klass)
639 {
640   GObjectClass *oclass = G_OBJECT_CLASS (klass);
641
642   oclass->get_property = gtk_numerable_icon_get_property;
643   oclass->set_property = gtk_numerable_icon_set_property;
644   oclass->constructed = gtk_numerable_icon_constructed;
645   oclass->dispose = gtk_numerable_icon_dispose;
646   oclass->finalize = gtk_numerable_icon_finalize;
647
648   g_type_class_add_private (klass, sizeof (GtkNumerableIconPrivate));
649
650   properties[PROP_COUNT] =
651     g_param_spec_int ("count",
652                       P_("Icon's count"),
653                       P_("The count of the emblem currently displayed"),
654                       -99, 99, 0,
655                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
656
657   properties[PROP_LABEL] =
658     g_param_spec_string ("label",
659                          P_("Icon's label"),
660                          P_("The label to be displayed over the icon"),
661                          NULL,
662                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
663
664   properties[PROP_STYLE] =
665     g_param_spec_object ("style-context",
666                          P_("Icon's style context"),
667                          P_("The style context to theme the icon appearance"),
668                          GTK_TYPE_STYLE_CONTEXT,
669                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
670
671   properties[PROP_BACKGROUND_ICON] =
672     g_param_spec_object ("background-icon",
673                          P_("Background icon"),
674                          P_("The icon for the number emblem background"),
675                          G_TYPE_ICON,
676                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
677
678   properties[PROP_BACKGROUND_ICON_NAME] =
679     g_param_spec_string ("background-icon-name",
680                          P_("Background icon name"),
681                          P_("The icon name for the number emblem background"),
682                          NULL,
683                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
684
685   g_object_class_install_properties (oclass, NUM_PROPERTIES, properties);
686 }
687
688 static void
689 gtk_numerable_icon_init (GtkNumerableIcon *self)
690 {
691   GdkRGBA bg;
692   GdkRGBA fg;
693
694   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
695                                             GTK_TYPE_NUMERABLE_ICON,
696                                             GtkNumerableIconPrivate);
697
698   gdk_rgba_parse (&bg, DEFAULT_BACKGROUND);
699   gdk_rgba_parse (&fg, DEFAULT_FOREGROUND);
700
701   self->priv->background = gdk_rgba_copy (&bg);
702   self->priv->foreground = gdk_rgba_copy (&fg);
703
704   self->priv->icon_size = 48;
705 }
706
707 /* private */
708 void
709 _gtk_numerable_icon_set_background_icon_size (GtkNumerableIcon *self,
710                                               gint              icon_size)
711 {
712   if (self->priv->background_icon == NULL)
713     return;
714
715   if (self->priv->icon_size != icon_size)
716     {
717       self->priv->icon_size = icon_size;
718       gtk_numerable_icon_ensure_emblem (self);
719     }
720 }
721
722 /**
723  * gtk_numerable_icon_get_label:
724  * @self: a #GtkNumerableIcon
725  *
726  * Returns the currently displayed label of the icon, or %NULL.
727  *
728  * Returns: the currently displayed label
729  *
730  * Since: 3.0
731  */
732 const gchar *
733 gtk_numerable_icon_get_label (GtkNumerableIcon *self)
734 {
735   g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);
736
737   return self->priv->label;
738 }
739
740 /**
741  * gtk_numerable_icon_set_label:
742  * @self: a #GtkNumerableIcon
743  * @label: (allow-none): a short label, or %NULL
744  *
745  * Sets the currently displayed value of @self to the string
746  * in @label. Setting an empty label removes the emblem.
747  *
748  * Note that this is meant for displaying short labels, such as
749  * roman numbers, or single letters. For roman numbers, consider
750  * using the Unicode characters U+2160 - U+217F. Strings longer
751  * than two characters will likely not be rendered very well.
752  *
753  * If this method is called, and a number was already set on the
754  * icon, it will automatically be reset to zero before rendering
755  * the label, i.e. the last method called between
756  * gtk_numerable_icon_set_label() and gtk_numerable_icon_set_count()
757  * has always priority.
758  *
759  * Since: 3.0
760  */
761 void
762 gtk_numerable_icon_set_label (GtkNumerableIcon *self,
763                               const gchar      *label)
764 {
765   g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));
766
767   if (g_strcmp0 (label, self->priv->label) != 0)
768     {
769       gtk_numerable_icon_ensure_and_replace_label (self, 0, label);
770       gtk_numerable_icon_ensure_emblem (self);
771
772       g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_LABEL]);
773     }
774 }
775
776 /**
777  * gtk_numerable_icon_get_count:
778  * @self: a #GtkNumerableIcon
779  *
780  * Returns the value currently displayed by @self.
781  *
782  * Returns: the currently displayed value
783  *
784  * Since: 3.0
785  */
786 gint
787 gtk_numerable_icon_get_count (GtkNumerableIcon *self)
788 {
789   g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), 0);
790
791   return self->priv->count;
792 }
793
794 /**
795  * gtk_numerable_icon_set_count:
796  * @self: a #GtkNumerableIcon
797  * @count: a number between -99 and 99
798  *
799  * Sets the currently displayed value of @self to @count.
800  *
801  * The numeric value is always clamped to make it two digits, i.e.
802  * between -99 and 99. Setting a count of zero removes the emblem.
803  * If this method is called, and a label was already set on the icon,
804  * it will automatically be reset to %NULL before rendering the number,
805  * i.e. the last method called between gtk_numerable_icon_set_count()
806  * and gtk_numerable_icon_set_label() has always priority.
807  *
808  * Since: 3.0
809  */
810 void
811 gtk_numerable_icon_set_count (GtkNumerableIcon *self,
812                               gint              count)
813 {
814   g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));
815
816   if (count != self->priv->count)
817     {
818       gtk_numerable_icon_ensure_and_replace_label (self, count, NULL);
819       gtk_numerable_icon_ensure_emblem (self);
820
821       g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COUNT]);
822     }
823 }
824
825 /**
826  * gtk_numerable_icon_get_style_context:
827  * @self: a #GtkNumerableIcon
828  *
829  * Returns the #GtkStyleContext used by the icon for theming,
830  * or %NULL if there's none.
831  *
832  * Returns: (transfer none): a #GtkStyleContext, or %NULL.
833  *     This object is internal to GTK+ and should not be unreffed.
834  *     Use g_object_ref() if you want to keep it around
835  *
836  * Since: 3.0
837  */
838 GtkStyleContext *
839 gtk_numerable_icon_get_style_context (GtkNumerableIcon *self)
840 {
841   g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);
842
843   return self->priv->style;
844 }
845
846 /**
847  * gtk_numerable_icon_set_style_context:
848  * @self: a #GtkNumerableIcon
849  * @style: a #GtkStyleContext
850  *
851  * Updates the icon to fetch theme information from the
852  * given #GtkStyleContext.
853  *
854  * Since: 3.0
855  */
856 void
857 gtk_numerable_icon_set_style_context (GtkNumerableIcon *self,
858                                       GtkStyleContext  *style)
859 {
860   g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));
861   g_return_if_fail (GTK_IS_STYLE_CONTEXT (style));
862
863   if (style != self->priv->style)
864     {
865       if (self->priv->style_changed_id != 0)
866         g_signal_handler_disconnect (self->priv->style,
867                                      self->priv->style_changed_id);
868
869       if (self->priv->style != NULL)
870         g_object_unref (self->priv->style);
871
872       self->priv->style = g_object_ref (style);
873
874       gtk_numerable_icon_init_style (self);
875
876       g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STYLE]);
877     }
878 }
879
880 /**
881  * gtk_numerable_icon_set_background_gicon:
882  * @self: a #GtkNumerableIcon
883  * @icon: (allow-none): a #GIcon, or %NULL
884  *
885  * Updates the icon to use @icon as the base background image.
886  * If @icon is %NULL, @self will go back using style information
887  * or default theming for its background image.
888  *
889  * If this method is called and an icon name was already set as
890  * background for the icon, @icon will be used, i.e. the last method
891  * called between gtk_numerable_icon_set_background_gicon() and
892  * gtk_numerable_icon_set_background_icon_name() has always priority.
893  *
894  * Since: 3.0
895  */
896 void
897 gtk_numerable_icon_set_background_gicon (GtkNumerableIcon *self,
898                                          GIcon            *icon)
899 {
900   gboolean res;
901
902   g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));
903
904   if (self->priv->background_icon_name != NULL)
905     {
906       g_free (self->priv->background_icon_name);
907       self->priv->background_icon_name = NULL;
908     }
909
910   res = real_set_background_icon (self, icon);
911
912   if (res)
913     g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_BACKGROUND_ICON]);
914 }
915
916 /**
917  * gtk_numerable_icon_get_background_gicon:
918  * @self: a #GtkNumerableIcon
919  *
920  * Returns the #GIcon that was set as the base background image, or
921  * %NULL if there's none. The caller of this function does not own
922  * a reference to the returned #GIcon.
923  *
924  * Returns: (transfer none): a #GIcon, or %NULL
925  *
926  * Since: 3.0
927  */
928 GIcon *
929 gtk_numerable_icon_get_background_gicon (GtkNumerableIcon *self)
930 {
931   GIcon *retval = NULL;
932
933   g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);
934
935   /* return the GIcon only if it wasn't created from an icon name */
936   if (self->priv->background_icon_name == NULL)
937     retval = self->priv->background_icon;
938
939   return retval;
940 }
941
942 /**
943  * gtk_numerable_icon_set_background_icon_name:
944  * @self: a #GtkNumerableIcon
945  * @icon_name: (allow-none): an icon name, or %NULL
946  *
947  * Updates the icon to use the icon named @icon_name from the
948  * current icon theme as the base background image. If @icon_name
949  * is %NULL, @self will go back using style information or default
950  * theming for its background image.
951  *
952  * If this method is called and a #GIcon was already set as
953  * background for the icon, @icon_name will be used, i.e. the
954  * last method called between gtk_numerable_icon_set_background_icon_name()
955  * and gtk_numerable_icon_set_background_gicon() has always priority.
956  *
957  * Since: 3.0
958  */
959 void
960 gtk_numerable_icon_set_background_icon_name (GtkNumerableIcon *self,
961                                              const gchar      *icon_name)
962 {
963   GIcon *icon = NULL;
964   gboolean res;
965
966   g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));
967
968   if (g_strcmp0 (icon_name, self->priv->background_icon_name) != 0)
969     {
970       g_free (self->priv->background_icon_name);
971       self->priv->background_icon_name = g_strdup (icon_name);
972     }
973
974   if (icon_name != NULL)
975     icon = g_themed_icon_new_with_default_fallbacks (icon_name);
976
977   res = real_set_background_icon (self, icon);
978
979   if (res)
980     g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_BACKGROUND_ICON_NAME]);
981
982   if (icon != NULL)
983     g_object_unref (icon);
984 }
985
986 /**
987  * gtk_numerable_icon_get_background_icon_name:
988  * @self: a #GtkNumerableIcon
989  *
990  * Returns the icon name used as the base background image,
991  * or %NULL if there's none.
992  *
993  * Returns: an icon name, or %NULL
994  *
995  * Since: 3.0
996  */
997 const gchar *
998 gtk_numerable_icon_get_background_icon_name (GtkNumerableIcon *self)
999 {
1000   g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);
1001
1002   return self->priv->background_icon_name;
1003 }
1004
1005 /**
1006  * gtk_numerable_icon_new:
1007  * @base_icon: a #GIcon to overlay on
1008  *
1009  * Creates a new unthemed #GtkNumerableIcon.
1010  *
1011  * Returns: (transfer full): a new #GIcon
1012  *
1013  * Since: 3.0
1014  */
1015 GIcon *
1016 gtk_numerable_icon_new (GIcon *base_icon)
1017 {
1018   g_return_val_if_fail (G_IS_ICON (base_icon), NULL);
1019
1020   return g_object_new (GTK_TYPE_NUMERABLE_ICON,
1021                        "gicon", base_icon,
1022                        NULL);
1023 }
1024
1025 /**
1026  * gtk_numerable_icon_new_with_style_context:
1027  * @base_icon: a #GIcon to overlay on
1028  * @context: a #GtkStyleContext
1029  *
1030  * Creates a new #GtkNumerableIcon which will themed according
1031  * to the passed #GtkStyleContext. This is a convenience constructor
1032  * that calls gtk_numerable_icon_set_style_context() internally.
1033  *
1034  * Returns: (transfer full): a new #GIcon
1035  *
1036  * Since: 3.0
1037  */
1038 GIcon *
1039 gtk_numerable_icon_new_with_style_context (GIcon           *base_icon,
1040                                            GtkStyleContext *context)
1041 {
1042   g_return_val_if_fail (G_IS_ICON (base_icon), NULL);
1043
1044   return g_object_new (GTK_TYPE_NUMERABLE_ICON,
1045                        "gicon", base_icon,
1046                        "style-context", context,
1047                        NULL);
1048 }