Add a GIcon implementation that can add an emblem to another icon.
[platform/upstream/glib.git] / gio / gemblemedicon.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4  * 
5  * Copyright (C) 2006-2007 Red Hat, Inc.
6  *
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.
11  *
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.
16  *
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.
21  *
22  * Author: Matthias Clasen <mclasen@redhat.com>
23  */
24
25 #include <config.h>
26
27 #include <string.h>
28
29 #include "gemblemedicon.h"
30 #include "glibintl.h"
31
32 #include "gioalias.h"
33
34 /**
35  * SECTION:gemblemedicon
36  * @short_description: Icon with emblems
37  * @include: gio/gio.h
38  * @see_also: #GIcon, #GLoadableIcon, #GThemedIcon
39  *
40  * #GEmblemedIcon is an implementation of #GIcon that supports
41  * adding an emblem to an icon. To add multiple emblems to an
42  * icon, you can create nested #GemblemedIcon<!-- -->s. 
43  *
44  * Note that #GEmblemedIcon allows no control over the position
45  * of the emblems. It is up to the rendering code to pick a position.
46  **/
47
48 static void g_emblemed_icon_icon_iface_init (GIconIface *iface);
49
50 struct _GEmblemedIcon
51 {
52   GObject parent_instance;
53
54   GIcon *icon;
55   GIcon *emblem;
56 };
57
58 struct _GEmblemedIconClass
59 {
60   GObjectClass parent_class;
61 };
62
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))
66
67
68 static void
69 g_emblemed_icon_finalize (GObject *object)
70 {
71   GEmblemedIcon *emblemed;
72
73   emblemed = G_EMBLEMED_ICON (object);
74
75   g_object_unref (emblemed->icon);
76   g_object_unref (emblemed->emblem);
77
78   (*G_OBJECT_CLASS (g_emblemed_icon_parent_class)->finalize) (object);
79 }
80
81 static void
82 g_emblemed_icon_class_init (GEmblemedIconClass *klass)
83 {
84   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
85   gobject_class->finalize = g_emblemed_icon_finalize;
86 }
87
88 static void
89 g_emblemed_icon_init (GEmblemedIcon *emblemed)
90 {
91 }
92
93 /**
94  * g_emblemed_icon_new:
95  * @icon: a #GIcon.
96  * @emblem: a #GIcon
97  *
98  * Creates a new emblemed icon for @icon with emblem @emblem.
99  *
100  * Returns: a new #GEmblemedIcon.
101  *
102  * Since: 2.18
103  **/
104 GIcon *
105 g_emblemed_icon_new (GIcon *icon, 
106                      GIcon *emblem)
107 {
108   GEmblemedIcon *emblemed;
109
110   g_return_val_if_fail (icon != NULL, NULL);
111   g_return_val_if_fail (emblem != NULL, NULL);
112
113   emblemed = G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON, NULL));
114   emblemed->icon = g_object_ref (icon);
115   emblemed->emblem = g_object_ref (emblem);
116
117   return G_ICON (emblemed);
118 }
119
120 /**
121  * g_emblemed_icon_get_icon:
122  * @icon: a #GEmblemedIcon.
123  *
124  * Gets the main icon for @icon.
125  *
126  * Returns: a #GIcon that is owend by @icon
127  *
128  * Since: 2.18
129  **/
130 GIcon *
131 g_emblemed_icon_get_icon (GEmblemedIcon *icon)
132 {
133   g_return_val_if_fail (G_IS_EMBLEMED_ICON (icon), NULL);
134
135   return icon->icon;
136 }
137
138 /**
139  * g_emblemed_icon_get_emblem:
140  * @icon: a #GEmblemedIcon.
141  *
142  * Gets the emblem for @icon.
143  *
144  * Returns: a #GIcon that is owned by @icon
145  *
146  * Since: 2.18
147  **/
148 GIcon *
149 g_emblemed_icon_get_emblem (GEmblemedIcon *icon)
150 {
151   g_return_val_if_fail (G_IS_EMBLEMED_ICON (icon), NULL);
152
153   return icon->emblem;
154 }
155
156 static guint
157 g_emblemed_icon_hash (GIcon *icon)
158 {
159   GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon);
160   guint hash;
161
162   hash = g_icon_hash (emblemed->icon);
163   hash ^= g_icon_hash (emblemed->emblem);
164
165   return hash;
166 }
167
168 static gboolean
169 g_emblemed_icon_equal (GIcon *icon1,
170                        GIcon *icon2)
171 {
172   GEmblemedIcon *emblemed1 = G_EMBLEMED_ICON (icon1);
173   GEmblemedIcon *emblemed2 = G_EMBLEMED_ICON (icon2);
174
175   return g_icon_equal (emblemed1->icon, emblemed2->icon) &&
176          g_icon_equal (emblemed1->emblem, emblemed2->emblem);
177 }
178
179 static void
180 g_emblemed_icon_icon_iface_init (GIconIface *iface)
181 {
182   iface->hash = g_emblemed_icon_hash;
183   iface->equal = g_emblemed_icon_equal;
184 }
185
186 #define __G_EMBLEMED_ICON_C__
187 #include "gioaliasdef.c"