2 * gtknumerableicon.c: an emblemed icon with number emblems
4 * Copyright (C) 2010 Red Hat, Inc.
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.
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.
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/>.
19 * Authors: Cosimo Cecchi <cosimoc@redhat.com>
23 * SECTION:gtknumerableicon
24 * @Title: GtkNumerableIcon
25 * @Short_description: A GIcon that allows numbered emblems
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.
31 * It supports theming by taking font and color information
32 * from a provided #GtkStyleContext; see
33 * gtk_numerable_icon_set_style_context().
36 * <title>Typical numerable icons</title>
37 * <inlinegraphic fileref="numerableicon.png" format="PNG"/>
38 * <inlinegraphic fileref="numerableicon2.png" format="PNG"/>
43 #include "gtknumerableicon.h"
44 #include "gtknumerableiconprivate.h"
46 #include "gtkicontheme.h"
48 #include "gtkwidget.h"
49 #include "gtkwidgetpath.h"
50 #include "gtkwindow.h"
53 #include <pango/pango.h>
56 struct _GtkNumerableIconPrivate {
62 GIcon *background_icon;
63 gchar *background_icon_name;
68 PangoFontDescription *font;
69 cairo_pattern_t *background_image;
72 GtkStyleContext *style;
73 gulong style_changed_id;
75 gchar *rendered_string;
83 PROP_BACKGROUND_ICON_NAME,
87 #define DEFAULT_SURFACE_SIZE 256
88 #define DEFAULT_BORDER_SIZE DEFAULT_SURFACE_SIZE * 0.06
89 #define DEFAULT_RADIUS DEFAULT_SURFACE_SIZE / 2
91 #define DEFAULT_BACKGROUND "#000000"
92 #define DEFAULT_FOREGROUND "#ffffff"
94 static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
96 G_DEFINE_TYPE (GtkNumerableIcon, gtk_numerable_icon, G_TYPE_EMBLEMED_ICON);
99 get_surface_size (cairo_surface_t *surface)
101 return MAX (cairo_image_surface_get_width (surface), cairo_image_surface_get_height (surface));
105 get_border_size (GtkNumerableIcon *self)
107 return self->priv->border_size;
110 static cairo_surface_t *
111 draw_default_surface (GtkNumerableIcon *self)
113 cairo_surface_t *surface;
116 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
117 DEFAULT_SURFACE_SIZE, DEFAULT_SURFACE_SIZE);
119 cr = cairo_create (surface);
121 cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
122 DEFAULT_RADIUS, 0., 2 * G_PI);
124 gdk_cairo_set_source_rgba (cr, self->priv->background);
127 cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
128 DEFAULT_RADIUS - DEFAULT_BORDER_SIZE, 0., 2 * G_PI);
129 gdk_cairo_set_source_rgba (cr, self->priv->foreground);
132 cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
133 DEFAULT_RADIUS - 2 * DEFAULT_BORDER_SIZE, 0., 2 * G_PI);
134 gdk_cairo_set_source_rgba (cr, self->priv->background);
142 static cairo_surface_t *
143 draw_from_gradient (cairo_pattern_t *pattern)
145 cairo_surface_t *surface;
146 cairo_matrix_t matrix;
149 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
150 DEFAULT_SURFACE_SIZE, DEFAULT_SURFACE_SIZE);
152 cr = cairo_create (surface);
154 /* scale the gradient points to the user space coordinates */
155 cairo_matrix_init_scale (&matrix,
156 1. / (double) DEFAULT_SURFACE_SIZE,
157 1. / (double) DEFAULT_SURFACE_SIZE);
158 cairo_pattern_set_matrix (pattern, &matrix);
160 cairo_arc (cr, DEFAULT_SURFACE_SIZE / 2., DEFAULT_SURFACE_SIZE / 2.,
161 DEFAULT_RADIUS, 0., 2 * G_PI);
163 cairo_set_source (cr, pattern);
171 /* copy the surface */
172 static cairo_surface_t *
173 draw_from_image (cairo_surface_t *image)
175 cairo_surface_t *surface;
178 surface = cairo_surface_create_similar (image, CAIRO_CONTENT_COLOR_ALPHA,
179 cairo_image_surface_get_width (image),
180 cairo_image_surface_get_height (image));
181 cr = cairo_create (surface);
183 cairo_set_source_surface (cr, image, 0, 0);
191 static cairo_surface_t *
192 draw_from_gicon (GtkNumerableIcon *self)
198 cairo_surface_t *surface;
201 if (self->priv->style != NULL)
203 screen = gtk_style_context_get_screen (self->priv->style);
204 theme = gtk_icon_theme_get_for_screen (screen);
208 theme = gtk_icon_theme_get_default ();
211 info = gtk_icon_theme_lookup_by_gicon (theme, self->priv->background_icon,
212 self->priv->icon_size,
213 GTK_ICON_LOOKUP_GENERIC_FALLBACK);
217 pixbuf = gtk_icon_info_load_icon (info, NULL);
218 gtk_icon_info_free (info);
223 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
224 gdk_pixbuf_get_width (pixbuf),
225 gdk_pixbuf_get_height (pixbuf));
227 cr = cairo_create (surface);
229 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
233 g_object_unref (pixbuf);
238 static cairo_surface_t *
239 get_image_surface (GtkNumerableIcon *self)
241 cairo_surface_t *retval = NULL, *image;
243 if (self->priv->background_icon != NULL)
245 retval = draw_from_gicon (self);
246 self->priv->border_size = 0;
248 else if (self->priv->background_image != NULL)
250 if (cairo_pattern_get_surface (self->priv->background_image, &image) == CAIRO_STATUS_SUCCESS)
251 retval = draw_from_image (image);
253 retval = draw_from_gradient (self->priv->background_image);
255 self->priv->border_size = 0;
260 retval = draw_default_surface (self);
261 self->priv->border_size = DEFAULT_BORDER_SIZE;
268 get_pango_layout (GtkNumerableIcon *self)
270 PangoContext *context;
274 if (self->priv->style != NULL)
276 screen = gtk_style_context_get_screen (self->priv->style);
277 context = gdk_pango_context_get_for_screen (screen);
278 layout = pango_layout_new (context);
280 if (self->priv->font != NULL)
281 pango_layout_set_font_description (layout, self->priv->font);
283 pango_layout_set_text (layout, self->priv->rendered_string, -1);
285 g_object_unref (context);
291 /* steal gtk text settings from the window */
292 fake = gtk_window_new (GTK_WINDOW_TOPLEVEL);
293 layout = gtk_widget_create_pango_layout (fake, self->priv->rendered_string);
294 gtk_widget_destroy (fake);
301 gtk_numerable_icon_ensure_emblem (GtkNumerableIcon *self)
304 cairo_surface_t *surface;
309 PangoAttrList *attr_list;
310 PangoAttribute *attr;
313 /* don't draw anything if the count is zero */
314 if (self->priv->rendered_string == NULL)
316 g_emblemed_icon_clear_emblems (G_EMBLEMED_ICON (self));
320 surface = get_image_surface (self);
321 cr = cairo_create (surface);
323 layout = get_pango_layout (self);
324 pango_layout_get_pixel_size (layout, &width, &height);
326 /* scale the layout to be 0.75 of the size still available for drawing */
327 scale = ((get_surface_size (surface) - 2 * get_border_size (self)) * 0.75) / (MAX (height, width));
328 attr_list = pango_attr_list_new ();
330 attr = pango_attr_scale_new (scale);
331 pango_attr_list_insert (attr_list, attr);
333 attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
334 pango_attr_list_insert (attr_list, attr);
336 pango_layout_set_attributes (layout, attr_list);
338 /* update these values */
339 pango_layout_get_pixel_size (layout, &width, &height);
341 /* move to the center */
343 get_surface_size (surface) / 2. - (gdouble) width / 2.,
344 get_surface_size (surface) / 2. - (gdouble) height / 2.);
346 gdk_cairo_set_source_rgba (cr, self->priv->foreground);
347 pango_cairo_show_layout (cr, layout);
352 gdk_pixbuf_get_from_surface (surface, 0, 0,
353 get_surface_size (surface), get_surface_size (surface));
355 emblem = g_emblem_new (G_ICON (pixbuf));
356 g_emblemed_icon_clear_emblems (G_EMBLEMED_ICON (self));
357 g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (self), emblem);
359 g_object_unref (layout);
360 g_object_unref (emblem);
361 g_object_unref (pixbuf);
363 cairo_surface_destroy (surface);
364 pango_attr_list_unref (attr_list);
368 gtk_numerable_icon_update_properties_from_style (GtkNumerableIcon *self)
370 GtkStyleContext *style = self->priv->style;
371 GtkWidgetPath *path, *saved;
372 cairo_pattern_t *pattern = NULL;
373 GdkRGBA background, foreground;
374 PangoFontDescription *font = NULL;
376 /* save an unmodified copy of the original widget path, in order
377 * to restore it later */
378 path = gtk_widget_path_copy (gtk_style_context_get_path (style));
379 saved = gtk_widget_path_copy (path);
381 if (!gtk_widget_path_is_type (path, GTK_TYPE_NUMERABLE_ICON))
383 /* append our GType to the style context to fetch appropriate colors */
384 gtk_widget_path_append_type (path, GTK_TYPE_NUMERABLE_ICON);
385 gtk_style_context_set_path (style, path);
388 gtk_style_context_get_background_color (style, gtk_style_context_get_state (style),
390 gtk_style_context_get_color (style, gtk_style_context_get_state (style),
393 if (self->priv->background != NULL)
394 gdk_rgba_free (self->priv->background);
396 self->priv->background = gdk_rgba_copy (&background);
398 if (self->priv->foreground != NULL)
399 gdk_rgba_free (self->priv->foreground);
401 self->priv->foreground = gdk_rgba_copy (&foreground);
403 gtk_style_context_get (style, gtk_style_context_get_state (style),
404 GTK_STYLE_PROPERTY_BACKGROUND_IMAGE, &pattern,
409 if (self->priv->background_image != NULL)
410 cairo_pattern_destroy (self->priv->background_image);
412 self->priv->background_image = pattern;
415 gtk_style_context_get (style, gtk_style_context_get_state (style),
416 GTK_STYLE_PROPERTY_FONT, &font,
421 if (self->priv->font != NULL)
422 pango_font_description_free (self->priv->font);
424 self->priv->font = font;
427 gtk_numerable_icon_ensure_emblem (self);
429 /* restore original widget path */
430 gtk_style_context_set_path (style, saved);
432 gtk_widget_path_free (path);
433 gtk_widget_path_free (saved);
437 gtk_numerable_icon_init_style (GtkNumerableIcon *self)
439 GtkStyleContext *style = self->priv->style;
444 gtk_numerable_icon_update_properties_from_style (self);
446 self->priv->style_changed_id =
447 g_signal_connect_swapped (style, "changed",
448 G_CALLBACK (gtk_numerable_icon_update_properties_from_style), self);
452 gtk_numerable_icon_ensure_and_replace_label (GtkNumerableIcon *self,
456 g_assert (!(label != NULL && count != 0));
458 g_free (self->priv->rendered_string);
459 self->priv->rendered_string = NULL;
463 if (self->priv->label != NULL)
465 g_free (self->priv->label);
466 self->priv->label = NULL;
468 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_LABEL]);
477 self->priv->count = count;
479 /* Translators: the format here is used to build the string that will be rendered
480 * in the number emblem.
482 self->priv->rendered_string = g_strdup_printf (C_("Number format", "%d"), count);
489 if (self->priv->count != 0)
491 self->priv->count = 0;
493 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COUNT]);
496 g_free (self->priv->label);
498 if (g_strcmp0 (label, "") == 0)
500 self->priv->label = NULL;
504 self->priv->label = g_strdup (label);
505 self->priv->rendered_string = g_strdup (label);
510 real_set_background_icon (GtkNumerableIcon *self,
513 if (!g_icon_equal (self->priv->background_icon, icon))
515 g_clear_object (&self->priv->background_icon);
518 self->priv->background_icon = g_object_ref (icon);
520 gtk_numerable_icon_ensure_emblem (self);
529 gtk_numerable_icon_constructed (GObject *object)
531 GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);
533 if (G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->constructed != NULL)
534 G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->constructed (object);
536 gtk_numerable_icon_ensure_emblem (self);
540 gtk_numerable_icon_finalize (GObject *object)
542 GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);
544 g_free (self->priv->label);
545 g_free (self->priv->rendered_string);
547 gdk_rgba_free (self->priv->background);
548 gdk_rgba_free (self->priv->foreground);
550 pango_font_description_free (self->priv->font);
552 cairo_pattern_destroy (self->priv->background_image);
554 G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->finalize (object);
558 gtk_numerable_icon_dispose (GObject *object)
560 GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);
562 if (self->priv->style_changed_id != 0)
564 g_signal_handler_disconnect (self->priv->style,
565 self->priv->style_changed_id);
566 self->priv->style_changed_id = 0;
569 g_clear_object (&self->priv->style);
570 g_clear_object (&self->priv->background_icon);
572 G_OBJECT_CLASS (gtk_numerable_icon_parent_class)->dispose (object);
576 gtk_numerable_icon_set_property (GObject *object,
581 GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);
586 gtk_numerable_icon_set_count (self, g_value_get_int (value));
589 gtk_numerable_icon_set_label (self, g_value_get_string (value));
592 gtk_numerable_icon_set_style_context (self, g_value_get_object (value));
594 case PROP_BACKGROUND_ICON:
595 gtk_numerable_icon_set_background_gicon (self, g_value_get_object (value));
597 case PROP_BACKGROUND_ICON_NAME:
598 gtk_numerable_icon_set_background_icon_name (self, g_value_get_string (value));
601 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
607 gtk_numerable_icon_get_property (GObject *object,
612 GtkNumerableIcon *self = GTK_NUMERABLE_ICON (object);
617 g_value_set_int (value, self->priv->count);
620 g_value_set_string (value, self->priv->label);
623 g_value_set_object (value, self->priv->style);
625 case PROP_BACKGROUND_ICON:
626 if (self->priv->background_icon != NULL)
627 g_value_set_object (value, self->priv->background_icon);
629 case PROP_BACKGROUND_ICON_NAME:
630 g_value_set_string (value, self->priv->background_icon_name);
633 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
639 gtk_numerable_icon_class_init (GtkNumerableIconClass *klass)
641 GObjectClass *oclass = G_OBJECT_CLASS (klass);
643 oclass->get_property = gtk_numerable_icon_get_property;
644 oclass->set_property = gtk_numerable_icon_set_property;
645 oclass->constructed = gtk_numerable_icon_constructed;
646 oclass->dispose = gtk_numerable_icon_dispose;
647 oclass->finalize = gtk_numerable_icon_finalize;
649 g_type_class_add_private (klass, sizeof (GtkNumerableIconPrivate));
651 properties[PROP_COUNT] =
652 g_param_spec_int ("count",
654 P_("The count of the emblem currently displayed"),
656 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
658 properties[PROP_LABEL] =
659 g_param_spec_string ("label",
661 P_("The label to be displayed over the icon"),
663 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
665 properties[PROP_STYLE] =
666 g_param_spec_object ("style-context",
667 P_("Icon's style context"),
668 P_("The style context to theme the icon appearance"),
669 GTK_TYPE_STYLE_CONTEXT,
670 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
672 properties[PROP_BACKGROUND_ICON] =
673 g_param_spec_object ("background-icon",
674 P_("Background icon"),
675 P_("The icon for the number emblem background"),
677 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
679 properties[PROP_BACKGROUND_ICON_NAME] =
680 g_param_spec_string ("background-icon-name",
681 P_("Background icon name"),
682 P_("The icon name for the number emblem background"),
684 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
686 g_object_class_install_properties (oclass, NUM_PROPERTIES, properties);
690 gtk_numerable_icon_init (GtkNumerableIcon *self)
695 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
696 GTK_TYPE_NUMERABLE_ICON,
697 GtkNumerableIconPrivate);
699 gdk_rgba_parse (&bg, DEFAULT_BACKGROUND);
700 gdk_rgba_parse (&fg, DEFAULT_FOREGROUND);
702 self->priv->background = gdk_rgba_copy (&bg);
703 self->priv->foreground = gdk_rgba_copy (&fg);
705 self->priv->icon_size = 48;
710 _gtk_numerable_icon_set_background_icon_size (GtkNumerableIcon *self,
713 if (self->priv->background_icon == NULL)
716 if (self->priv->icon_size != icon_size)
718 self->priv->icon_size = icon_size;
719 gtk_numerable_icon_ensure_emblem (self);
724 * gtk_numerable_icon_get_label:
725 * @self: a #GtkNumerableIcon
727 * Returns the currently displayed label of the icon, or %NULL.
729 * Returns: the currently displayed label
734 gtk_numerable_icon_get_label (GtkNumerableIcon *self)
736 g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);
738 return self->priv->label;
742 * gtk_numerable_icon_set_label:
743 * @self: a #GtkNumerableIcon
744 * @label: (allow-none): a short label, or %NULL
746 * Sets the currently displayed value of @self to the string
747 * in @label. Setting an empty label removes the emblem.
749 * Note that this is meant for displaying short labels, such as
750 * roman numbers, or single letters. For roman numbers, consider
751 * using the Unicode characters U+2160 - U+217F. Strings longer
752 * than two characters will likely not be rendered very well.
754 * If this method is called, and a number was already set on the
755 * icon, it will automatically be reset to zero before rendering
756 * the label, i.e. the last method called between
757 * gtk_numerable_icon_set_label() and gtk_numerable_icon_set_count()
758 * has always priority.
763 gtk_numerable_icon_set_label (GtkNumerableIcon *self,
766 g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));
768 if (g_strcmp0 (label, self->priv->label) != 0)
770 gtk_numerable_icon_ensure_and_replace_label (self, 0, label);
771 gtk_numerable_icon_ensure_emblem (self);
773 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_LABEL]);
778 * gtk_numerable_icon_get_count:
779 * @self: a #GtkNumerableIcon
781 * Returns the value currently displayed by @self.
783 * Returns: the currently displayed value
788 gtk_numerable_icon_get_count (GtkNumerableIcon *self)
790 g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), 0);
792 return self->priv->count;
796 * gtk_numerable_icon_set_count:
797 * @self: a #GtkNumerableIcon
798 * @count: a number between -99 and 99
800 * Sets the currently displayed value of @self to @count.
802 * The numeric value is always clamped to make it two digits, i.e.
803 * between -99 and 99. Setting a count of zero removes the emblem.
804 * If this method is called, and a label was already set on the icon,
805 * it will automatically be reset to %NULL before rendering the number,
806 * i.e. the last method called between gtk_numerable_icon_set_count()
807 * and gtk_numerable_icon_set_label() has always priority.
812 gtk_numerable_icon_set_count (GtkNumerableIcon *self,
815 g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));
817 if (count != self->priv->count)
819 gtk_numerable_icon_ensure_and_replace_label (self, count, NULL);
820 gtk_numerable_icon_ensure_emblem (self);
822 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COUNT]);
827 * gtk_numerable_icon_get_style_context:
828 * @self: a #GtkNumerableIcon
830 * Returns the #GtkStyleContext used by the icon for theming,
831 * or %NULL if there's none.
833 * Returns: (transfer none): a #GtkStyleContext, or %NULL.
834 * This object is internal to GTK+ and should not be unreffed.
835 * Use g_object_ref() if you want to keep it around
840 gtk_numerable_icon_get_style_context (GtkNumerableIcon *self)
842 g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);
844 return self->priv->style;
848 * gtk_numerable_icon_set_style_context:
849 * @self: a #GtkNumerableIcon
850 * @style: a #GtkStyleContext
852 * Updates the icon to fetch theme information from the
853 * given #GtkStyleContext.
858 gtk_numerable_icon_set_style_context (GtkNumerableIcon *self,
859 GtkStyleContext *style)
861 g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));
862 g_return_if_fail (GTK_IS_STYLE_CONTEXT (style));
864 if (style != self->priv->style)
866 if (self->priv->style_changed_id != 0)
867 g_signal_handler_disconnect (self->priv->style,
868 self->priv->style_changed_id);
870 if (self->priv->style != NULL)
871 g_object_unref (self->priv->style);
873 self->priv->style = g_object_ref (style);
875 gtk_numerable_icon_init_style (self);
877 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STYLE]);
882 * gtk_numerable_icon_set_background_gicon:
883 * @self: a #GtkNumerableIcon
884 * @icon: (allow-none): a #GIcon, or %NULL
886 * Updates the icon to use @icon as the base background image.
887 * If @icon is %NULL, @self will go back using style information
888 * or default theming for its background image.
890 * If this method is called and an icon name was already set as
891 * background for the icon, @icon will be used, i.e. the last method
892 * called between gtk_numerable_icon_set_background_gicon() and
893 * gtk_numerable_icon_set_background_icon_name() has always priority.
898 gtk_numerable_icon_set_background_gicon (GtkNumerableIcon *self,
903 g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));
905 if (self->priv->background_icon_name != NULL)
907 g_free (self->priv->background_icon_name);
908 self->priv->background_icon_name = NULL;
911 res = real_set_background_icon (self, icon);
914 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_BACKGROUND_ICON]);
918 * gtk_numerable_icon_get_background_gicon:
919 * @self: a #GtkNumerableIcon
921 * Returns the #GIcon that was set as the base background image, or
922 * %NULL if there's none. The caller of this function does not own
923 * a reference to the returned #GIcon.
925 * Returns: (transfer none): a #GIcon, or %NULL
930 gtk_numerable_icon_get_background_gicon (GtkNumerableIcon *self)
932 GIcon *retval = NULL;
934 g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);
936 /* return the GIcon only if it wasn't created from an icon name */
937 if (self->priv->background_icon_name == NULL)
938 retval = self->priv->background_icon;
944 * gtk_numerable_icon_set_background_icon_name:
945 * @self: a #GtkNumerableIcon
946 * @icon_name: (allow-none): an icon name, or %NULL
948 * Updates the icon to use the icon named @icon_name from the
949 * current icon theme as the base background image. If @icon_name
950 * is %NULL, @self will go back using style information or default
951 * theming for its background image.
953 * If this method is called and a #GIcon was already set as
954 * background for the icon, @icon_name will be used, i.e. the
955 * last method called between gtk_numerable_icon_set_background_icon_name()
956 * and gtk_numerable_icon_set_background_gicon() has always priority.
961 gtk_numerable_icon_set_background_icon_name (GtkNumerableIcon *self,
962 const gchar *icon_name)
967 g_return_if_fail (GTK_IS_NUMERABLE_ICON (self));
969 if (g_strcmp0 (icon_name, self->priv->background_icon_name) != 0)
971 g_free (self->priv->background_icon_name);
972 self->priv->background_icon_name = g_strdup (icon_name);
975 if (icon_name != NULL)
976 icon = g_themed_icon_new_with_default_fallbacks (icon_name);
978 res = real_set_background_icon (self, icon);
981 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_BACKGROUND_ICON_NAME]);
984 g_object_unref (icon);
988 * gtk_numerable_icon_get_background_icon_name:
989 * @self: a #GtkNumerableIcon
991 * Returns the icon name used as the base background image,
992 * or %NULL if there's none.
994 * Returns: an icon name, or %NULL
999 gtk_numerable_icon_get_background_icon_name (GtkNumerableIcon *self)
1001 g_return_val_if_fail (GTK_IS_NUMERABLE_ICON (self), NULL);
1003 return self->priv->background_icon_name;
1007 * gtk_numerable_icon_new:
1008 * @base_icon: a #GIcon to overlay on
1010 * Creates a new unthemed #GtkNumerableIcon.
1012 * Returns: (transfer full): a new #GIcon
1017 gtk_numerable_icon_new (GIcon *base_icon)
1019 g_return_val_if_fail (G_IS_ICON (base_icon), NULL);
1021 return g_object_new (GTK_TYPE_NUMERABLE_ICON,
1027 * gtk_numerable_icon_new_with_style_context:
1028 * @base_icon: a #GIcon to overlay on
1029 * @context: a #GtkStyleContext
1031 * Creates a new #GtkNumerableIcon which will themed according
1032 * to the passed #GtkStyleContext. This is a convenience constructor
1033 * that calls gtk_numerable_icon_set_style_context() internally.
1035 * Returns: (transfer full): a new #GIcon
1040 gtk_numerable_icon_new_with_style_context (GIcon *base_icon,
1041 GtkStyleContext *context)
1043 g_return_val_if_fail (G_IS_ICON (base_icon), NULL);
1045 return g_object_new (GTK_TYPE_NUMERABLE_ICON,
1047 "style-context", context,