]> git.sur5r.net Git - glabels/blob - glabels2/src/mdi-child.c
Initial revision
[glabels] / glabels2 / src / mdi-child.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  mdi-child.c:  gLabels MDI child module
5  *
6  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 /*
24  * This file is based on gedit-mdi-child.c from gedit2:
25  *
26  * Copyright (C) 1998, 1999 Alex Roberts, Evan Lawrence
27  * Copyright (C) 2000, 2001 Chema Celorio, Paolo Maggi 
28  * Copyright (C) 2002  Paolo Maggi 
29  *
30  */
31 #include <config.h>
32
33 #include "mdi-child.h"
34 #include "xml-label.h"
35 #include "label.h"
36 #include "view.h"
37 #include "marshal.h"
38 #include "debug.h"
39
40 struct _glMDIChildPrivate
41 {
42         GtkWidget *tab_label;
43 };
44
45 enum {
46         STATE_CHANGED,
47         UNDO_REDO_STATE_CHANGED,
48         LAST_SIGNAL
49 };
50
51 static void gl_mdi_child_class_init     (glMDIChildClass        *klass);
52 static void gl_mdi_child_init   (glMDIChild             *mdi);
53 static void gl_mdi_child_finalize       (GObject                *obj);
54 static void gl_mdi_child_destroy        (GtkObject              *obj);
55
56 static void gl_mdi_child_real_state_changed (glMDIChild* child);
57
58 static GtkWidget *gl_mdi_child_create_view (BonoboMDIChild *child);
59
60 static void gl_mdi_child_document_state_changed_handler (glLabel *label, 
61                                                            glMDIChild* child);
62
63 static void gl_mdi_child_document_can_undo_redo_handler (glLabel *label, 
64                                                 gboolean can, glMDIChild* child);
65
66 static gchar* gl_mdi_child_get_config_string (BonoboMDIChild *child, gpointer data);
67
68 static BonoboMDIChildClass *parent_class = NULL;
69 static guint mdi_child_signals[LAST_SIGNAL] = { 0 };
70
71
72 GType
73 gl_mdi_child_get_type (void)
74 {
75         static GType mdi_child_type = 0;
76
77         gl_debug (DEBUG_MDI, "START");
78
79         if (mdi_child_type == 0)
80         {
81                 static const GTypeInfo our_info =
82                 {
83                         sizeof (glMDIChildClass),
84                         NULL,           /* base_init */
85                         NULL,           /* base_finalize */
86                         (GClassInitFunc) gl_mdi_child_class_init,
87                         NULL,           /* class_finalize */
88                         NULL,           /* class_data */
89                         sizeof (glMDIChild),
90                         0,              /* n_preallocs */
91                         (GInstanceInitFunc) gl_mdi_child_init
92                 };
93
94                 mdi_child_type = g_type_register_static (BONOBO_TYPE_MDI_CHILD,
95                                                           "glMDIChild",
96                                                           &our_info,
97                                                           0);
98         }
99
100         gl_debug (DEBUG_MDI, "END");
101
102         return mdi_child_type;
103 }
104
105 static void
106 gl_mdi_child_class_init (glMDIChildClass *klass)
107 {
108         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
109
110         gl_debug (DEBUG_MDI, "START");
111
112         parent_class = g_type_class_peek_parent (klass);
113
114         gobject_class->finalize = gl_mdi_child_finalize;
115
116         klass->state_changed            = gl_mdi_child_real_state_changed;
117         klass->undo_redo_state_changed  = NULL;
118                 
119         mdi_child_signals [STATE_CHANGED] =
120                 g_signal_new ("state_changed",
121                               G_OBJECT_CLASS_TYPE (gobject_class),
122                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
123                               G_STRUCT_OFFSET (glMDIChildClass, state_changed),
124                               NULL, NULL,
125                               gl_marshal_VOID__VOID,
126                               G_TYPE_NONE, 
127                               0);
128
129         mdi_child_signals [UNDO_REDO_STATE_CHANGED] =
130                 g_signal_new ("undo_redo_state_changed",
131                               G_OBJECT_CLASS_TYPE (gobject_class),
132                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
133                               G_STRUCT_OFFSET (glMDIChildClass, undo_redo_state_changed),
134                               NULL, NULL,
135                               gl_marshal_VOID__VOID,
136                               G_TYPE_NONE, 
137                               0);
138                     
139         BONOBO_MDI_CHILD_CLASS (klass)->create_view = 
140                 (BonoboMDIChildViewCreator)(gl_mdi_child_create_view);
141
142         BONOBO_MDI_CHILD_CLASS (klass)->get_config_string = 
143                 (BonoboMDIChildConfigFunc)(gl_mdi_child_get_config_string);
144
145         gl_debug (DEBUG_MDI, "END");
146 }
147
148 static void 
149 gl_mdi_child_init (glMDIChild  *child)
150 {
151         gl_debug (DEBUG_MDI, "START");
152
153         child->priv = g_new0 (glMDIChildPrivate, 1);
154
155         child->priv->tab_label = NULL;
156
157         gl_debug (DEBUG_MDI, "END");
158 }
159
160 static void 
161 gl_mdi_child_finalize (GObject *obj)
162 {
163         glMDIChild *child;
164
165         gl_debug (DEBUG_MDI, "START");
166
167         g_return_if_fail (obj != NULL);
168         
169         child = GL_MDI_CHILD (obj);
170
171         g_return_if_fail (GL_IS_MDI_CHILD (child));
172         g_return_if_fail (child->priv != NULL);
173
174         if (child->label != NULL)
175                 g_object_unref (G_OBJECT (child->label));
176
177         g_free (child->priv);
178
179         G_OBJECT_CLASS (parent_class)->finalize (obj);
180
181         gl_debug (DEBUG_MDI, "END");
182 }
183
184 static void gl_mdi_child_real_state_changed (glMDIChild* child)
185 {
186         gchar* docname = NULL;
187         gchar* tab_name = NULL;
188
189         gl_debug (DEBUG_MDI, "START");
190
191         g_return_if_fail (child != NULL);
192         g_return_if_fail (child->label != NULL);
193
194         docname = gl_label_get_short_name (child->label);
195         g_return_if_fail (docname != NULL);
196         
197         if (gl_label_is_modified (child->label))
198         {
199                 tab_name = g_strdup_printf ("%s*", docname);
200         } 
201         else 
202         {
203                 tab_name = g_strdup_printf ("%s", docname);
204         }
205         
206         g_free (docname);
207
208         g_return_if_fail (tab_name != NULL);
209
210         bonobo_mdi_child_set_name (BONOBO_MDI_CHILD (child), tab_name);
211
212         g_free (tab_name);      
213
214         gl_debug (DEBUG_MDI, "END");
215 }
216
217 static void 
218 gl_mdi_child_document_state_changed_handler (glLabel *label, glMDIChild* child)
219 {
220         gl_debug (DEBUG_MDI, "");
221         g_return_if_fail (child->label == label);
222
223         g_signal_emit (G_OBJECT (child), mdi_child_signals [STATE_CHANGED], 0);
224 }
225
226 static void 
227 gl_mdi_child_document_can_undo_redo_handler (glLabel *label, gboolean can, 
228                                                 glMDIChild* child)
229 {
230         gl_debug (DEBUG_MDI, "");
231         g_return_if_fail (child->label == label);
232
233         g_signal_emit (G_OBJECT (child), mdi_child_signals [UNDO_REDO_STATE_CHANGED], 0);
234 }
235
236
237 static void
238 gl_mdi_child_connect_signals (glMDIChild *child)
239 {
240         gl_debug (DEBUG_MDI, "START");
241
242         g_signal_connect (G_OBJECT (child->label), "name_changed",
243                           G_CALLBACK (gl_mdi_child_document_state_changed_handler), 
244                           child);
245         g_signal_connect (G_OBJECT (child->label), "modified_changed",
246                           G_CALLBACK (gl_mdi_child_document_state_changed_handler), 
247                           child);
248 #if 0
249         g_signal_connect (G_OBJECT (child->label), "can_undo",
250                           G_CALLBACK (gl_mdi_child_document_can_undo_redo_handler), 
251                           child);
252         g_signal_connect (G_OBJECT (child->label), "can_redo",
253                           G_CALLBACK (gl_mdi_child_document_can_undo_redo_handler), 
254                           child);
255 #endif
256
257         gl_debug (DEBUG_MDI, "END");
258 }
259
260 /**
261  * gl_mdi_child_new:
262  * 
263  * Creates a new #glMDIChild object.
264  *
265  * Return value: a new #glMDIChild
266  **/
267 glMDIChild*
268 gl_mdi_child_new (const gchar *tmplt_name,
269                   gboolean rotate_flag)
270 {
271         glMDIChild *child;
272         gchar *doc_name;
273         glTemplate *template;
274         
275         gl_debug (DEBUG_MDI, "START");
276
277         child = GL_MDI_CHILD (g_object_new (GL_TYPE_MDI_CHILD, NULL));
278         g_return_val_if_fail (child != NULL, NULL);
279         
280         child->label = GL_LABEL (gl_label_new ());
281         g_return_val_if_fail (child->label != NULL, NULL);
282         
283         template = gl_template_from_name (tmplt_name);
284         gl_label_set_template (child->label, template);
285         gl_template_free (&template);
286         gl_label_set_rotate_flag (child->label, rotate_flag);
287         gl_label_clear_modified (child->label);
288
289         doc_name = gl_label_get_short_name (child->label);
290         bonobo_mdi_child_set_name (BONOBO_MDI_CHILD (child), doc_name);
291         g_free(doc_name);
292
293         gl_mdi_child_connect_signals (child);
294         
295         gl_debug (DEBUG_MDI, "END");
296         
297         return child;
298 }
299
300 glMDIChild*
301 gl_mdi_child_new_with_uri (const gchar *uri, GError **error)
302 {
303         glMDIChild *child;
304         glLabel    *doc;
305         glXMLLabelStatus status;
306         
307         gl_debug (DEBUG_MDI, "START");
308
309         doc = gl_xml_label_open (uri, &status);
310
311         if (doc == NULL)
312         {
313                 return NULL;
314         }
315
316         child = GL_MDI_CHILD (g_object_new (GL_TYPE_MDI_CHILD, NULL));
317         g_return_val_if_fail (child != NULL, NULL);
318         
319         child->label = doc;
320         g_return_val_if_fail (child->label != NULL, NULL);
321         
322         gl_mdi_child_real_state_changed (child);
323         
324         gl_mdi_child_connect_signals (child);
325
326         gl_debug (DEBUG_MDI, "END");
327         
328         return child;
329 }
330                 
331 static GtkWidget *
332 gl_mdi_child_create_view (BonoboMDIChild *child)
333 {
334         glView  *new_view;
335
336         gl_debug (DEBUG_MDI, "START");
337
338         g_return_val_if_fail (child != NULL, NULL);
339         g_return_val_if_fail (GL_IS_MDI_CHILD (child), NULL);
340
341         new_view = GL_VIEW(gl_view_new (GL_MDI_CHILD (child)->label));
342
343         gtk_widget_show_all (GTK_WIDGET (new_view));
344
345         gl_debug (DEBUG_MDI, "END");
346
347         return GTK_WIDGET (new_view);
348 }
349
350 static gchar* 
351 gl_mdi_child_get_config_string (BonoboMDIChild *child, gpointer data)
352 {
353         glMDIChild *c;
354         
355         gl_debug (DEBUG_MDI, "");
356
357         g_return_val_if_fail (child != NULL, NULL);
358         g_return_val_if_fail (GL_IS_MDI_CHILD (child), NULL);
359
360         c = GL_MDI_CHILD (child);
361         
362         return gl_label_get_filename (c->label);
363 }