1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Matthias Clasen <mclasen@redhat.com>
23 * Clemens N. Buss <cebuzz@gmail.com>
30 #include "gemblemedicon.h"
36 * SECTION:gemblemedicon
37 * @short_description: Icon with emblems
39 * @see_also: #GIcon, #GLoadableIcon, #GThemedIcon, #GEmblem
41 * #GEmblemedIcon is an implementation of #GIcon that supports
42 * adding an emblem to an icon. Adding multiple emblems to an
43 * icon is ensured via g_emblemed_icon_add_emblem().
45 * Note that #GEmblemedIcon allows no control over the position
46 * of the emblems. See also #GEmblem for more information.
54 struct _GEmblemedIconPrivate {
59 static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
61 static void g_emblemed_icon_icon_iface_init (GIconIface *iface);
63 G_DEFINE_TYPE_WITH_CODE (GEmblemedIcon, g_emblemed_icon, G_TYPE_OBJECT,
64 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
65 g_emblemed_icon_icon_iface_init))
69 g_emblemed_icon_finalize (GObject *object)
71 GEmblemedIcon *emblemed;
73 emblemed = G_EMBLEMED_ICON (object);
75 g_object_unref (emblemed->priv->icon);
76 g_list_foreach (emblemed->priv->emblems, (GFunc) g_object_unref, NULL);
77 g_list_free (emblemed->priv->emblems);
79 (*G_OBJECT_CLASS (g_emblemed_icon_parent_class)->finalize) (object);
83 g_emblemed_icon_set_property (GObject *object,
88 GEmblemedIcon *self = G_EMBLEMED_ICON (object);
93 self->priv->icon = g_value_dup_object (value);
96 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
102 g_emblemed_icon_get_property (GObject *object,
107 GEmblemedIcon *self = G_EMBLEMED_ICON (object);
112 g_value_set_object (value, self->priv->icon);
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
121 g_emblemed_icon_class_init (GEmblemedIconClass *klass)
123 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
125 gobject_class->finalize = g_emblemed_icon_finalize;
126 gobject_class->set_property = g_emblemed_icon_set_property;
127 gobject_class->get_property = g_emblemed_icon_get_property;
129 properties[PROP_GICON] =
130 g_param_spec_object ("gicon",
131 P_("The base GIcon"),
132 P_("The GIcon to attach emblems to"),
134 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
136 g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
138 g_type_class_add_private (klass, sizeof (GEmblemedIconPrivate));
142 g_emblemed_icon_init (GEmblemedIcon *emblemed)
145 G_TYPE_INSTANCE_GET_PRIVATE (emblemed, G_TYPE_EMBLEMED_ICON,
146 GEmblemedIconPrivate);
150 * g_emblemed_icon_new:
152 * @emblem: (allow-none): a #GEmblem, or %NULL
154 * Creates a new emblemed icon for @icon with the emblem @emblem.
156 * Returns: (transfer full): a new #GIcon
161 g_emblemed_icon_new (GIcon *icon,
164 GEmblemedIcon *emblemed;
166 g_return_val_if_fail (G_IS_ICON (icon), NULL);
167 g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
169 emblemed = G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON,
174 g_emblemed_icon_add_emblem (emblemed, emblem);
176 return G_ICON (emblemed);
181 * g_emblemed_icon_get_icon:
182 * @emblemed: a #GEmblemedIcon
184 * Gets the main icon for @emblemed.
186 * Returns: (transfer none): a #GIcon that is owned by @emblemed
191 g_emblemed_icon_get_icon (GEmblemedIcon *emblemed)
193 g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
195 return emblemed->priv->icon;
199 * g_emblemed_icon_get_emblems:
200 * @emblemed: a #GEmblemedIcon
202 * Gets the list of emblems for the @icon.
204 * Returns: (element-type Gio.Emblem) (transfer none): a #GList of
205 * #GEmblem <!-- -->s that is owned by @emblemed
211 g_emblemed_icon_get_emblems (GEmblemedIcon *emblemed)
213 g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
215 return emblemed->priv->emblems;
219 * g_emblemed_icon_clear_emblems:
220 * @emblemed: a #GEmblemedIcon
222 * Removes all the emblems from @icon.
227 g_emblemed_icon_clear_emblems (GEmblemedIcon *emblemed)
229 g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
231 if (emblemed->priv->emblems == NULL)
234 g_list_free_full (emblemed->priv->emblems, g_object_unref);
235 emblemed->priv->emblems = NULL;
239 g_emblem_comp (GEmblem *a,
242 guint hash_a = g_icon_hash (G_ICON (a));
243 guint hash_b = g_icon_hash (G_ICON (b));
255 * g_emblemed_icon_add_emblem:
256 * @emblemed: a #GEmblemedIcon
257 * @emblem: a #GEmblem
259 * Adds @emblem to the #GList of #GEmblem <!-- -->s.
264 g_emblemed_icon_add_emblem (GEmblemedIcon *emblemed,
267 g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
268 g_return_if_fail (G_IS_EMBLEM (emblem));
270 g_object_ref (emblem);
271 emblemed->priv->emblems = g_list_insert_sorted (emblemed->priv->emblems, emblem,
272 (GCompareFunc) g_emblem_comp);
276 g_emblemed_icon_hash (GIcon *icon)
278 GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon);
280 guint hash = g_icon_hash (emblemed->priv->icon);
282 for (list = emblemed->priv->emblems; list != NULL; list = list->next)
283 hash ^= g_icon_hash (G_ICON (list->data));
289 g_emblemed_icon_equal (GIcon *icon1,
292 GEmblemedIcon *emblemed1 = G_EMBLEMED_ICON (icon1);
293 GEmblemedIcon *emblemed2 = G_EMBLEMED_ICON (icon2);
294 GList *list1, *list2;
296 if (!g_icon_equal (emblemed1->priv->icon, emblemed2->priv->icon))
299 list1 = emblemed1->priv->emblems;
300 list2 = emblemed2->priv->emblems;
302 while (list1 && list2)
304 if (!g_icon_equal (G_ICON (list1->data), G_ICON (list2->data)))
311 return list1 == NULL && list2 == NULL;
315 g_emblemed_icon_to_tokens (GIcon *icon,
319 GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon);
323 /* GEmblemedIcons are encoded as
325 * <encoded_icon> [<encoded_emblem_icon>]*
328 g_return_val_if_fail (out_version != NULL, FALSE);
332 s = g_icon_to_string (emblemed_icon->priv->icon);
336 g_ptr_array_add (tokens, s);
338 for (l = emblemed_icon->priv->emblems; l != NULL; l = l->next)
340 GIcon *emblem_icon = G_ICON (l->data);
342 s = g_icon_to_string (emblem_icon);
346 g_ptr_array_add (tokens, s);
353 g_emblemed_icon_from_tokens (gchar **tokens,
358 GEmblemedIcon *emblemed_icon;
361 emblemed_icon = NULL;
367 G_IO_ERROR_INVALID_ARGUMENT,
368 _("Can't handle version %d of GEmblemedIcon encoding"),
377 G_IO_ERROR_INVALID_ARGUMENT,
378 _("Malformed number of tokens (%d) in GEmblemedIcon encoding"),
383 emblemed_icon = g_object_new (G_TYPE_EMBLEMED_ICON, NULL);
384 emblemed_icon->priv->icon = g_icon_new_for_string (tokens[0], error);
385 if (emblemed_icon->priv->icon == NULL)
388 for (n = 1; n < num_tokens; n++)
392 emblem = g_icon_new_for_string (tokens[n], error);
396 if (!G_IS_EMBLEM (emblem))
398 g_set_error_literal (error,
400 G_IO_ERROR_INVALID_ARGUMENT,
401 _("Expected a GEmblem for GEmblemedIcon"));
402 g_object_unref (emblem);
406 emblemed_icon->priv->emblems = g_list_append (emblemed_icon->priv->emblems, emblem);
409 return G_ICON (emblemed_icon);
412 if (emblemed_icon != NULL)
413 g_object_unref (emblemed_icon);
418 g_emblemed_icon_icon_iface_init (GIconIface *iface)
420 iface->hash = g_emblemed_icon_hash;
421 iface->equal = g_emblemed_icon_equal;
422 iface->to_tokens = g_emblemed_icon_to_tokens;
423 iface->from_tokens = g_emblemed_icon_from_tokens;