]> git.sur5r.net Git - glabels/blob - glabels2/src/bonobo-mdi-child.c
Initial revision
[glabels] / glabels2 / src / bonobo-mdi-child.c
1 /*
2  * bonobo-mdi-child.c - implementation of a BonoboMDI object
3  *
4  * Copyright (C) 2001-2002 Free Software Foundation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, 
19  * Boston, MA 02111-1307, USA.
20  *
21  * Author: Paolo Maggi 
22  */
23
24 #include "bonobo-mdi-child.h"
25
26 struct _BonoboMDIChildPrivate
27 {
28         GObject         *parent;
29
30         gchar           *name;
31         GList           *views;
32 };
33
34 enum {
35         NAME_CHANGED,
36         LAST_SIGNAL
37 };
38
39 static void       bonobo_mdi_child_class_init       (BonoboMDIChildClass *klass);
40 static void       bonobo_mdi_child_instance_init    (BonoboMDIChild *);
41 static void       bonobo_mdi_child_finalize         (GObject *);
42
43 static GtkWidget *bonobo_mdi_child_set_label        (BonoboMDIChild *, GtkWidget *, gpointer);
44 static GtkWidget *bonobo_mdi_child_create_view      (BonoboMDIChild *);
45
46 static void bonobo_mdi_child_real_name_changed (BonoboMDIChild *child, gchar* old_name);
47
48 static GObjectClass *parent_class = NULL;
49 static guint mdi_child_signals [LAST_SIGNAL] = { 0 };
50
51 GType
52 bonobo_mdi_child_get_type (void)
53 {
54         static GType bonobo_mdi_child_type = 0;
55
56         if (bonobo_mdi_child_type == 0)
57         {
58                 static const GTypeInfo our_info =
59                 {
60                         sizeof (BonoboMDIChildClass),
61                         NULL,           /* base_init */
62                         NULL,           /* base_finalize */
63                         (GClassInitFunc) bonobo_mdi_child_class_init,
64                         NULL,           /* class_finalize */
65                         NULL,           /* class_data */
66                         sizeof (BonoboMDIChild),
67                         0,              /* n_preallocs */
68                         (GInstanceInitFunc) bonobo_mdi_child_instance_init
69                 };
70
71                 bonobo_mdi_child_type = g_type_register_static (G_TYPE_OBJECT,
72                                                     "BonoboMDIChild",
73                                                     &our_info,
74                                                     0);
75         }
76
77         return bonobo_mdi_child_type;
78 }
79
80 static void 
81 bonobo_mdi_child_class_init (BonoboMDIChildClass *klass)
82 {
83         GObjectClass *gobject_class;
84
85         parent_class = g_type_class_peek_parent (klass);
86
87         gobject_class = (GObjectClass*)klass;
88   
89         gobject_class->finalize = bonobo_mdi_child_finalize;
90   
91         klass->create_view = NULL;
92         klass->get_config_string = NULL;
93         klass->set_label = bonobo_mdi_child_set_label;
94
95         klass->name_changed = bonobo_mdi_child_real_name_changed;
96
97         mdi_child_signals[NAME_CHANGED] =
98                 g_signal_new ("name_changed",
99                               G_OBJECT_CLASS_TYPE (gobject_class),
100                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
101                               G_STRUCT_OFFSET (BonoboMDIChildClass, name_changed),
102                               NULL, NULL,
103                               g_cclosure_marshal_VOID__STRING,
104                               G_TYPE_NONE, 
105                               1, 
106                               G_TYPE_STRING);
107 }
108
109 static void
110 bonobo_mdi_child_instance_init (BonoboMDIChild *mdi_child)
111 {
112         g_return_if_fail (BONOBO_IS_MDI_CHILD (mdi_child));
113
114         mdi_child->priv = g_new0 (BonoboMDIChildPrivate, 1);
115         
116         mdi_child->priv->name = NULL;
117         mdi_child->priv->parent = NULL;
118         mdi_child->priv->views = NULL;
119 }
120
121
122 /* the default set_label function: returns a GtkLabel with child->priv->name
123  * if you provide your own, it should return a new widget if its old_label
124  * parameter is NULL and modify and return the old widget otherwise. it
125  * should (obviously) NOT call the parent class handler!
126  */
127 static GtkWidget *
128 bonobo_mdi_child_set_label (BonoboMDIChild *child, GtkWidget *old_label, gpointer data)
129 {
130         g_return_val_if_fail (BONOBO_IS_MDI_CHILD (child), NULL);
131         g_return_val_if_fail (child->priv != NULL, NULL);
132
133         if (old_label != NULL) 
134         {
135                 gtk_label_set_text (GTK_LABEL (old_label), child->priv->name);
136                 return old_label;
137         }
138         else 
139         {
140                 GtkWidget *label;
141
142                 label = gtk_label_new (child->priv->name);
143                 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
144                 
145                 return label;
146         }
147 }
148
149 static void 
150 bonobo_mdi_child_finalize (GObject *obj)
151 {
152         BonoboMDIChild *mdi_child;
153
154         g_return_if_fail (BONOBO_IS_MDI_CHILD (obj));
155
156         mdi_child = BONOBO_MDI_CHILD (obj);
157         g_return_if_fail (mdi_child->priv != NULL);
158
159         while (mdi_child->priv->views)
160                 bonobo_mdi_child_remove_view (mdi_child, 
161                                 GTK_WIDGET (mdi_child->priv->views->data));
162
163         if (mdi_child->priv->name != NULL)
164                 g_free (mdi_child->priv->name);
165
166         if (mdi_child->priv != NULL)
167                 g_free (mdi_child->priv);
168         
169         if (G_OBJECT_CLASS (parent_class)->finalize)
170                 (* G_OBJECT_CLASS (parent_class)->finalize)(obj);
171 }
172
173 /**
174  * bonobo_mdi_child_add_view:
175  * @mdi_child: A pointer to a BonoboMDIChild object.
176  * 
177  * Description:
178  * Creates a new view of a child (a GtkWidget) adds it to the list
179  * of the views and returns a pointer to it. Virtual function
180  * that has to be specified for classes derived from BonoboMDIChild
181  * is used to create the new view.
182  * 
183  * Return value:
184  * A pointer to the new view.
185  **/
186 GtkWidget *
187 bonobo_mdi_child_add_view (BonoboMDIChild *mdi_child)
188 {
189         GtkWidget *view = NULL;
190
191         g_return_val_if_fail (BONOBO_IS_MDI_CHILD (mdi_child), NULL);
192         g_return_val_if_fail (mdi_child->priv != NULL, NULL);
193
194         view = bonobo_mdi_child_create_view (mdi_child);
195
196         if (view) {
197                 mdi_child->priv->views = g_list_append (mdi_child->priv->views, view);
198
199                 g_object_set_data (G_OBJECT (view), "BonoboMDIChild", mdi_child);
200         }
201
202         return view;
203 }
204
205 /**
206  * bonobo_mdi_child_remove_view:
207  * @mdi_child: A pointer to a BonoboMDIChild object.
208  * @view: View to be removed.
209  * 
210  * Description:
211  * Removes view @view from the list of @mdi_child's views and
212  * unrefs it.
213  **/
214 void 
215 bonobo_mdi_child_remove_view (BonoboMDIChild *mdi_child, GtkWidget *view)
216 {
217         g_return_if_fail (BONOBO_IS_MDI_CHILD (mdi_child));
218         g_return_if_fail (mdi_child->priv != NULL);
219         g_return_if_fail (GTK_IS_WIDGET (view));
220
221         mdi_child->priv->views = g_list_remove (mdi_child->priv->views, view);
222
223         g_object_unref (G_OBJECT (view));       
224 }
225
226 /**
227  * bonobo_mdi_child_set_name:
228  * @mdi_child: A pointer to a BonoboMDIChild object.
229  * @name: String containing the new name for the child.
230  * 
231  * Description:
232  * Changes name of @mdi_child to @name. @name is duplicated and stored
233  * in @mdi_child. If @mdi_child has already been added to BonoboMDI,
234  * it also takes care of updating it.
235  **/
236 void 
237 bonobo_mdi_child_set_name (BonoboMDIChild *mdi_child, const gchar *name)
238 {
239         gchar *old_name;
240         
241         g_return_if_fail (BONOBO_IS_MDI_CHILD (mdi_child));
242         g_return_if_fail (mdi_child->priv != NULL);
243
244         old_name = mdi_child->priv->name;
245
246         if (old_name == name)
247                 return;
248         
249         mdi_child->priv->name = (gchar *)g_strdup (name);
250
251         g_signal_emit (G_OBJECT (mdi_child),
252                        mdi_child_signals [NAME_CHANGED], 0,
253                        old_name);
254
255         if (old_name)
256                 g_free (old_name);
257 }
258
259 static GtkWidget *
260 bonobo_mdi_child_create_view (BonoboMDIChild *child)
261 {
262         g_return_val_if_fail (BONOBO_IS_MDI_CHILD (child), NULL);
263
264         if (BONOBO_MDI_CHILD_GET_CLASS (child)->create_view)
265                 return BONOBO_MDI_CHILD_GET_CLASS (child)->create_view (child, NULL);
266
267         return NULL;
268 }
269
270 static void 
271 bonobo_mdi_child_real_name_changed (BonoboMDIChild *child, gchar* old_name)
272 {
273         g_return_if_fail (BONOBO_IS_MDI_CHILD (child));
274
275         return;
276 }
277
278 gchar * 
279 bonobo_mdi_child_get_name (const BonoboMDIChild *mdi_child)
280 {
281         g_return_val_if_fail (BONOBO_IS_MDI_CHILD (mdi_child), NULL);
282         g_return_val_if_fail (mdi_child->priv != NULL, NULL);
283
284         if (mdi_child->priv->name)
285                 return g_strdup (mdi_child->priv->name);
286         else
287                 return NULL;
288 }
289
290 GList *       
291 bonobo_mdi_child_get_views (const BonoboMDIChild *mdi_child)
292 {
293         g_return_val_if_fail (BONOBO_IS_MDI_CHILD (mdi_child), NULL);
294         g_return_val_if_fail (mdi_child->priv != NULL, NULL);
295
296         return mdi_child->priv->views;
297 }
298
299 GObject *
300 bonobo_mdi_child_get_parent (const BonoboMDIChild *mdi_child)
301 {
302         g_return_val_if_fail (BONOBO_IS_MDI_CHILD (mdi_child), NULL);
303         g_return_val_if_fail (mdi_child->priv != NULL, NULL);
304
305         return G_OBJECT (mdi_child->priv->parent);
306         
307 }
308
309 void
310 bonobo_mdi_child_set_parent (BonoboMDIChild *mdi_child, GObject *parent)
311 {
312         g_return_if_fail (BONOBO_IS_MDI_CHILD (mdi_child));
313         g_return_if_fail (mdi_child->priv != NULL);
314
315         mdi_child->priv->parent = parent;
316 }