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.
49 static void g_emblemed_icon_icon_iface_init (GIconIface *iface);
53 GObject parent_instance;
59 struct _GEmblemedIconClass
61 GObjectClass parent_class;
64 G_DEFINE_TYPE_WITH_CODE (GEmblemedIcon, g_emblemed_icon, G_TYPE_OBJECT,
65 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
66 g_emblemed_icon_icon_iface_init))
70 g_emblemed_icon_finalize (GObject *object)
72 GEmblemedIcon *emblemed;
74 emblemed = G_EMBLEMED_ICON (object);
76 g_object_unref (emblemed->icon);
77 g_list_foreach (emblemed->emblems, (GFunc) g_object_unref, NULL);
78 g_list_free (emblemed->emblems);
80 (*G_OBJECT_CLASS (g_emblemed_icon_parent_class)->finalize) (object);
84 g_emblemed_icon_class_init (GEmblemedIconClass *klass)
86 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
87 gobject_class->finalize = g_emblemed_icon_finalize;
91 g_emblemed_icon_init (GEmblemedIcon *emblemed)
96 * g_emblemed_icon_new:
98 * @emblem: (allow-none): a #GEmblem, or %NULL
100 * Creates a new emblemed icon for @icon with the emblem @emblem.
102 * Returns: (transfer full): a new #GIcon
107 g_emblemed_icon_new (GIcon *icon,
110 GEmblemedIcon *emblemed;
112 g_return_val_if_fail (G_IS_ICON (icon), NULL);
113 g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
115 emblemed = G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON, NULL));
116 emblemed->icon = g_object_ref (icon);
119 g_emblemed_icon_add_emblem (emblemed, emblem);
121 return G_ICON (emblemed);
126 * g_emblemed_icon_get_icon:
127 * @emblemed: a #GEmblemedIcon
129 * Gets the main icon for @emblemed.
131 * Returns: (transfer full): a #GIcon that is owned by @emblemed
136 g_emblemed_icon_get_icon (GEmblemedIcon *emblemed)
138 g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
140 return emblemed->icon;
144 * g_emblemed_icon_get_emblems:
145 * @emblemed: a #GEmblemedIcon
147 * Gets the list of emblems for the @icon.
149 * Returns: (element-type utf8) (transfer none): a #GList of #GEmblem <!-- -->s that
150 * is owned by @emblemed
156 g_emblemed_icon_get_emblems (GEmblemedIcon *emblemed)
158 g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
160 return emblemed->emblems;
164 g_emblemed_icon_clear_emblems (GEmblemedIcon *emblemed)
166 g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
168 if (emblemed->emblems == NULL)
171 g_list_free_full (emblemed->emblems, g_object_unref);
172 emblemed->emblems = NULL;
176 g_emblem_comp (GEmblem *a,
179 guint hash_a = g_icon_hash (G_ICON (a));
180 guint hash_b = g_icon_hash (G_ICON (b));
192 * g_emblemed_icon_add_emblem:
193 * @emblemed: a #GEmblemedIcon
194 * @emblem: a #GEmblem
196 * Adds @emblem to the #GList of #GEmblem <!-- -->s.
201 g_emblemed_icon_add_emblem (GEmblemedIcon *emblemed,
204 g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
205 g_return_if_fail (G_IS_EMBLEM (emblem));
207 g_object_ref (emblem);
208 emblemed->emblems = g_list_insert_sorted (emblemed->emblems, emblem,
209 (GCompareFunc) g_emblem_comp);
213 g_emblemed_icon_hash (GIcon *icon)
215 GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon);
217 guint hash = g_icon_hash (emblemed->icon);
219 for (list = emblemed->emblems; list != NULL; list = list->next)
220 hash ^= g_icon_hash (G_ICON (list->data));
226 g_emblemed_icon_equal (GIcon *icon1,
229 GEmblemedIcon *emblemed1 = G_EMBLEMED_ICON (icon1);
230 GEmblemedIcon *emblemed2 = G_EMBLEMED_ICON (icon2);
231 GList *list1, *list2;
233 if (!g_icon_equal (emblemed1->icon, emblemed2->icon))
236 list1 = emblemed1->emblems;
237 list2 = emblemed2->emblems;
239 while (list1 && list2)
241 if (!g_icon_equal (G_ICON (list1->data), G_ICON (list2->data)))
248 return list1 == NULL && list2 == NULL;
252 g_emblemed_icon_to_tokens (GIcon *icon,
256 GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon);
260 /* GEmblemedIcons are encoded as
262 * <encoded_icon> [<encoded_emblem_icon>]*
265 g_return_val_if_fail (out_version != NULL, FALSE);
269 s = g_icon_to_string (emblemed_icon->icon);
273 g_ptr_array_add (tokens, s);
275 for (l = emblemed_icon->emblems; l != NULL; l = l->next)
277 GIcon *emblem_icon = G_ICON (l->data);
279 s = g_icon_to_string (emblem_icon);
283 g_ptr_array_add (tokens, s);
290 g_emblemed_icon_from_tokens (gchar **tokens,
295 GEmblemedIcon *emblemed_icon;
298 emblemed_icon = NULL;
304 G_IO_ERROR_INVALID_ARGUMENT,
305 _("Can't handle version %d of GEmblemedIcon encoding"),
314 G_IO_ERROR_INVALID_ARGUMENT,
315 _("Malformed number of tokens (%d) in GEmblemedIcon encoding"),
320 emblemed_icon = g_object_new (G_TYPE_EMBLEMED_ICON, NULL);
321 emblemed_icon->icon = g_icon_new_for_string (tokens[0], error);
322 if (emblemed_icon->icon == NULL)
325 for (n = 1; n < num_tokens; n++)
329 emblem = g_icon_new_for_string (tokens[n], error);
333 if (!G_IS_EMBLEM (emblem))
335 g_set_error_literal (error,
337 G_IO_ERROR_INVALID_ARGUMENT,
338 _("Expected a GEmblem for GEmblemedIcon"));
339 g_object_unref (emblem);
343 emblemed_icon->emblems = g_list_append (emblemed_icon->emblems, emblem);
346 return G_ICON (emblemed_icon);
349 if (emblemed_icon != NULL)
350 g_object_unref (emblemed_icon);
355 g_emblemed_icon_icon_iface_init (GIconIface *iface)
357 iface->hash = g_emblemed_icon_hash;
358 iface->equal = g_emblemed_icon_equal;
359 iface->to_tokens = g_emblemed_icon_to_tokens;
360 iface->from_tokens = g_emblemed_icon_from_tokens;