2 * bonobo-mdi-child.c - implementation of a BonoboMDI object
4 * Copyright (C) 2001-2002 Free Software Foundation
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.
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.
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.
24 #include "bonobo-mdi-child.h"
26 struct _BonoboMDIChildPrivate
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 *);
43 static GtkWidget *bonobo_mdi_child_set_label (BonoboMDIChild *, GtkWidget *, gpointer);
44 static GtkWidget *bonobo_mdi_child_create_view (BonoboMDIChild *);
46 static void bonobo_mdi_child_real_name_changed (BonoboMDIChild *child, gchar* old_name);
48 static GObjectClass *parent_class = NULL;
49 static guint mdi_child_signals [LAST_SIGNAL] = { 0 };
52 bonobo_mdi_child_get_type (void)
54 static GType bonobo_mdi_child_type = 0;
56 if (bonobo_mdi_child_type == 0)
58 static const GTypeInfo our_info =
60 sizeof (BonoboMDIChildClass),
62 NULL, /* base_finalize */
63 (GClassInitFunc) bonobo_mdi_child_class_init,
64 NULL, /* class_finalize */
65 NULL, /* class_data */
66 sizeof (BonoboMDIChild),
68 (GInstanceInitFunc) bonobo_mdi_child_instance_init
71 bonobo_mdi_child_type = g_type_register_static (G_TYPE_OBJECT,
77 return bonobo_mdi_child_type;
81 bonobo_mdi_child_class_init (BonoboMDIChildClass *klass)
83 GObjectClass *gobject_class;
85 parent_class = g_type_class_peek_parent (klass);
87 gobject_class = (GObjectClass*)klass;
89 gobject_class->finalize = bonobo_mdi_child_finalize;
91 klass->create_view = NULL;
92 klass->get_config_string = NULL;
93 klass->set_label = bonobo_mdi_child_set_label;
95 klass->name_changed = bonobo_mdi_child_real_name_changed;
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),
103 g_cclosure_marshal_VOID__STRING,
110 bonobo_mdi_child_instance_init (BonoboMDIChild *mdi_child)
112 g_return_if_fail (BONOBO_IS_MDI_CHILD (mdi_child));
114 mdi_child->priv = g_new0 (BonoboMDIChildPrivate, 1);
116 mdi_child->priv->name = NULL;
117 mdi_child->priv->parent = NULL;
118 mdi_child->priv->views = NULL;
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!
128 bonobo_mdi_child_set_label (BonoboMDIChild *child, GtkWidget *old_label, gpointer data)
130 g_return_val_if_fail (BONOBO_IS_MDI_CHILD (child), NULL);
131 g_return_val_if_fail (child->priv != NULL, NULL);
133 if (old_label != NULL)
135 gtk_label_set_text (GTK_LABEL (old_label), child->priv->name);
142 label = gtk_label_new (child->priv->name);
143 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
150 bonobo_mdi_child_finalize (GObject *obj)
152 BonoboMDIChild *mdi_child;
154 g_return_if_fail (BONOBO_IS_MDI_CHILD (obj));
156 mdi_child = BONOBO_MDI_CHILD (obj);
157 g_return_if_fail (mdi_child->priv != NULL);
159 while (mdi_child->priv->views)
160 bonobo_mdi_child_remove_view (mdi_child,
161 GTK_WIDGET (mdi_child->priv->views->data));
163 if (mdi_child->priv->name != NULL)
164 g_free (mdi_child->priv->name);
166 if (mdi_child->priv != NULL)
167 g_free (mdi_child->priv);
169 if (G_OBJECT_CLASS (parent_class)->finalize)
170 (* G_OBJECT_CLASS (parent_class)->finalize)(obj);
174 * bonobo_mdi_child_add_view:
175 * @mdi_child: A pointer to a BonoboMDIChild object.
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.
184 * A pointer to the new view.
187 bonobo_mdi_child_add_view (BonoboMDIChild *mdi_child)
189 GtkWidget *view = NULL;
191 g_return_val_if_fail (BONOBO_IS_MDI_CHILD (mdi_child), NULL);
192 g_return_val_if_fail (mdi_child->priv != NULL, NULL);
194 view = bonobo_mdi_child_create_view (mdi_child);
197 mdi_child->priv->views = g_list_append (mdi_child->priv->views, view);
199 g_object_set_data (G_OBJECT (view), "BonoboMDIChild", mdi_child);
206 * bonobo_mdi_child_remove_view:
207 * @mdi_child: A pointer to a BonoboMDIChild object.
208 * @view: View to be removed.
211 * Removes view @view from the list of @mdi_child's views and
215 bonobo_mdi_child_remove_view (BonoboMDIChild *mdi_child, GtkWidget *view)
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));
221 mdi_child->priv->views = g_list_remove (mdi_child->priv->views, view);
223 g_object_unref (G_OBJECT (view));
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.
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.
237 bonobo_mdi_child_set_name (BonoboMDIChild *mdi_child, const gchar *name)
241 g_return_if_fail (BONOBO_IS_MDI_CHILD (mdi_child));
242 g_return_if_fail (mdi_child->priv != NULL);
244 old_name = mdi_child->priv->name;
246 if (old_name == name)
249 mdi_child->priv->name = (gchar *)g_strdup (name);
251 g_signal_emit (G_OBJECT (mdi_child),
252 mdi_child_signals [NAME_CHANGED], 0,
260 bonobo_mdi_child_create_view (BonoboMDIChild *child)
262 g_return_val_if_fail (BONOBO_IS_MDI_CHILD (child), NULL);
264 if (BONOBO_MDI_CHILD_GET_CLASS (child)->create_view)
265 return BONOBO_MDI_CHILD_GET_CLASS (child)->create_view (child, NULL);
271 bonobo_mdi_child_real_name_changed (BonoboMDIChild *child, gchar* old_name)
273 g_return_if_fail (BONOBO_IS_MDI_CHILD (child));
279 bonobo_mdi_child_get_name (const BonoboMDIChild *mdi_child)
281 g_return_val_if_fail (BONOBO_IS_MDI_CHILD (mdi_child), NULL);
282 g_return_val_if_fail (mdi_child->priv != NULL, NULL);
284 if (mdi_child->priv->name)
285 return g_strdup (mdi_child->priv->name);
291 bonobo_mdi_child_get_views (const BonoboMDIChild *mdi_child)
293 g_return_val_if_fail (BONOBO_IS_MDI_CHILD (mdi_child), NULL);
294 g_return_val_if_fail (mdi_child->priv != NULL, NULL);
296 return mdi_child->priv->views;
300 bonobo_mdi_child_get_parent (const BonoboMDIChild *mdi_child)
302 g_return_val_if_fail (BONOBO_IS_MDI_CHILD (mdi_child), NULL);
303 g_return_val_if_fail (mdi_child->priv != NULL, NULL);
305 return G_OBJECT (mdi_child->priv->parent);
310 bonobo_mdi_child_set_parent (BonoboMDIChild *mdi_child, GObject *parent)
312 g_return_if_fail (BONOBO_IS_MDI_CHILD (mdi_child));
313 g_return_if_fail (mdi_child->priv != NULL);
315 mdi_child->priv->parent = parent;