root/trunk/libtinymailui-gtk/tny-gtk-expander-mime-part-view.c

Revision 3666 (checked in by jdapena, 7 months ago)

* Use GOnce registering all types in tinymail to make

registering thread-safe.

Line 
1 /* libtinymailui-gtk - The Tiny Mail UI library for Gtk+
2  * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with self library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * TnyGtkExpanderMimePartView:
22  *
23  * A #TnyMimePartView that wraps another #TnyMimePartView into a #GtkExpander.
24  * The type is a #TnyMimePartView by itself too (it decorates the one it wraps).
25  *
26  * free-function: g_object_unref
27  **/
28
29 #include <config.h>
30
31 #include <gtk/gtk.h>
32
33 #include <tny-gtk-expander-mime-part-view.h>
34
35 static GObjectClass *parent_class = NULL;
36
37 typedef struct _TnyGtkExpanderMimePartViewPriv TnyGtkExpanderMimePartViewPriv;
38
39 struct _TnyGtkExpanderMimePartViewPriv
40 {
41         TnyMimePart *part;
42         TnyMimePartView *decorated;
43         gboolean must_set_on_expand;
44 };
45
46 #define TNY_GTK_EXPANDER_MIME_PART_VIEW_GET_PRIVATE(o) \
47         (G_TYPE_INSTANCE_GET_PRIVATE ((o), TNY_TYPE_GTK_EXPANDER_MIME_PART_VIEW, TnyGtkExpanderMimePartViewPriv))
48
49
50 static TnyMimePart*
51 tny_gtk_expander_mime_part_view_get_part (TnyMimePartView *self)
52 {
53         return TNY_GTK_EXPANDER_MIME_PART_VIEW_GET_CLASS (self)->get_part(self);
54 }
55
56 static TnyMimePart*
57 tny_gtk_expander_mime_part_view_get_part_default (TnyMimePartView *self)
58 {
59         TnyGtkExpanderMimePartViewPriv *priv = TNY_GTK_EXPANDER_MIME_PART_VIEW_GET_PRIVATE (self);
60         return tny_mime_part_view_get_part (priv->decorated);
61 }
62
63 static void
64 tny_gtk_expander_mime_part_view_set_part (TnyMimePartView *self, TnyMimePart *part)
65 {
66         TNY_GTK_EXPANDER_MIME_PART_VIEW_GET_CLASS (self)->set_part(self, part);
67         return;
68 }
69
70 static void
71 expander_callback (GtkExpander *self, GParamSpec *param_spec, gpointer user_data)
72 {
73         TnyGtkExpanderMimePartViewPriv *priv = TNY_GTK_EXPANDER_MIME_PART_VIEW_GET_PRIVATE (self);
74
75         if (priv->must_set_on_expand && gtk_expander_get_expanded (self)) {
76                 tny_mime_part_view_set_part (priv->decorated, priv->part);
77                 priv->must_set_on_expand = FALSE;
78         }
79
80         return;
81 }
82
83 static void
84 tny_gtk_expander_mime_part_view_set_part_default (TnyMimePartView *self, TnyMimePart *part)
85 {
86         TnyGtkExpanderMimePartViewPriv *priv = TNY_GTK_EXPANDER_MIME_PART_VIEW_GET_PRIVATE (self);
87
88         priv->part = TNY_MIME_PART (g_object_ref (part));
89         if (gtk_expander_get_expanded (GTK_EXPANDER (self))) {
90                 tny_mime_part_view_set_part (priv->decorated, priv->part);
91                 priv->must_set_on_expand = FALSE;
92         } else
93                 priv->must_set_on_expand = TRUE;
94
95         return;
96 }
97
98 static void
99 tny_gtk_expander_mime_part_view_clear (TnyMimePartView *self)
100 {
101         TNY_GTK_EXPANDER_MIME_PART_VIEW_GET_CLASS (self)->clear(self);
102         return;
103 }
104
105 static void
106 tny_gtk_expander_mime_part_view_clear_default (TnyMimePartView *self)
107 {
108         TnyGtkExpanderMimePartViewPriv *priv = TNY_GTK_EXPANDER_MIME_PART_VIEW_GET_PRIVATE (self);
109         tny_mime_part_view_clear (priv->decorated);
110         return;
111 }
112
113 /**
114  * tny_gtk_expander_mime_part_view_set_view:
115  * @self: a #TnyGtkMsgView
116  * @view: a #TnyMimePartView to make expandable
117  *
118  * Set @view to become decorated and expanded by @self. The @view must inherit
119  * #GtkWidget and implement #TnyMimePartView.
120  *
121  * since: 1.0
122  * audience: application-developer
123  **/
124 void
125 tny_gtk_expander_mime_part_view_set_view (TnyGtkExpanderMimePartView *self, TnyMimePartView *view)
126 {
127         TnyGtkExpanderMimePartViewPriv *priv = TNY_GTK_EXPANDER_MIME_PART_VIEW_GET_PRIVATE (self);
128
129         g_assert (GTK_IS_WIDGET (view));
130
131         if (priv->decorated)
132                 gtk_container_remove (GTK_CONTAINER (self), GTK_WIDGET (priv->decorated));
133
134         priv->decorated = view;
135
136         /* This adds a reference to msgview (it's a gtkwidget) */
137         gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (view));
138
139         gtk_widget_show (GTK_WIDGET (priv->decorated));
140
141         return;
142 }
143
144 /**
145  * tny_gtk_expander_mime_part_view_new:
146  * @view: a #TnyMimePartView to make expandable
147  *
148  * Create a new #TnyMimePartView that wraps another mime part view with
149  * a #GtkExpander. The @view instance must inherit #GtkWidget. The
150  * returned value will inherit #GtkExpander.
151  *
152  * returns: (caller-owns): a new #TnyMimePartView
153  * since: 1.0
154  * audience: application-developer
155  **/
156 TnyMimePartView*
157 tny_gtk_expander_mime_part_view_new (TnyMimePartView *view)
158 {
159         TnyGtkExpanderMimePartView *self = g_object_new (TNY_TYPE_GTK_EXPANDER_MIME_PART_VIEW, NULL);
160
161         tny_gtk_expander_mime_part_view_set_view (self, view);
162
163         return TNY_MIME_PART_VIEW (self);
164 }
165
166 static void
167 tny_gtk_expander_mime_part_view_instance_init (GTypeInstance *instance, gpointer g_class)
168 {
169         TnyGtkExpanderMimePartView *self = (TnyGtkExpanderMimePartView *)instance;
170         TnyGtkExpanderMimePartViewPriv *priv = TNY_GTK_EXPANDER_MIME_PART_VIEW_GET_PRIVATE (self);
171
172         priv->part = NULL;
173         priv->decorated = NULL;
174         priv->must_set_on_expand = FALSE;
175
176         g_signal_connect (self, "notify::expanded",
177                 G_CALLBACK (expander_callback), self);
178
179         return;
180 }
181
182 static void
183 tny_gtk_expander_mime_part_view_finalize (GObject *object)
184 {
185         TnyGtkExpanderMimePartView *self = (TnyGtkExpanderMimePartView *)object;
186         TnyGtkExpanderMimePartViewPriv *priv = TNY_GTK_EXPANDER_MIME_PART_VIEW_GET_PRIVATE (self);
187
188         if (priv->part)
189                 g_object_unref (priv->part);
190
191         (*parent_class->finalize) (object);
192
193         return;
194 }
195
196 static void
197 tny_mime_part_view_init (gpointer g, gpointer iface_data)
198 {
199         TnyMimePartViewIface *klass = (TnyMimePartViewIface *)g;
200
201         klass->get_part= tny_gtk_expander_mime_part_view_get_part;
202         klass->set_part= tny_gtk_expander_mime_part_view_set_part;
203         klass->clear= tny_gtk_expander_mime_part_view_clear;
204
205         return;
206 }
207
208 static void
209 tny_gtk_expander_mime_part_view_class_init (TnyGtkExpanderMimePartViewClass *class)
210 {
211         GObjectClass *object_class;
212
213         parent_class = g_type_class_peek_parent (class);
214         object_class = (GObjectClass*) class;
215
216         class->get_part= tny_gtk_expander_mime_part_view_get_part_default;
217         class->set_part= tny_gtk_expander_mime_part_view_set_part_default;
218         class->clear= tny_gtk_expander_mime_part_view_clear_default;
219
220         object_class->finalize = tny_gtk_expander_mime_part_view_finalize;
221
222         g_type_class_add_private (object_class, sizeof (TnyGtkExpanderMimePartViewPriv));
223
224         return;
225 }
226
227 static gpointer
228 tny_gtk_expander_mime_part_view_register_type (gpointer notused)
229 {
230         GType type = 0;
231
232         static const GTypeInfo info =
233                 {
234                   sizeof (TnyGtkExpanderMimePartViewClass),
235                   NULL,   /* base_init */
236                   NULL,   /* base_finalize */
237                   (GClassInitFunc) tny_gtk_expander_mime_part_view_class_init,   /* class_init */
238                   NULL,   /* class_finalize */
239                   NULL,   /* class_data */
240                   sizeof (TnyGtkExpanderMimePartView),
241                   0,      /* n_preallocs */
242                   tny_gtk_expander_mime_part_view_instance_init,    /* instance_init */
243                   NULL
244                 };
245
246         static const GInterfaceInfo tny_mime_part_view_info =
247                 {
248                   (GInterfaceInitFunc) tny_mime_part_view_init, /* interface_init */
249                   NULL,         /* interface_finalize */
250                   NULL          /* interface_data */
251                 };
252
253         type = g_type_register_static (GTK_TYPE_EXPANDER,
254                                        "TnyGtkExpanderMimePartView",
255                                        &info, 0);
256
257         g_type_add_interface_static (type, TNY_TYPE_MIME_PART_VIEW,
258                                      &tny_mime_part_view_info);
259
260         return GUINT_TO_POINTER (type);
261 }
262
263 GType
264 tny_gtk_expander_mime_part_view_get_type (void)
265 {
266         static GOnce once = G_ONCE_INIT;
267         g_once (&once, tny_gtk_expander_mime_part_view_register_type, NULL);
268         return GPOINTER_TO_UINT (once.retval);
269 }
Note: See TracBrowser for help on using the browser.