]> git.sur5r.net Git - glabels/blob - glabels2/src/mdi.c
Renamed menus module to ui.
[glabels] / glabels2 / src / mdi.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  mdi.c:  gLabels MDI 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.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 <libgnome/libgnome.h>
34 #include <libgnomeui/libgnomeui.h>
35 #include <libgnomevfs/gnome-vfs.h>
36
37 #include <string.h>
38
39 #include "mdi.h"
40 #include "mdi-child.h"
41 #include "glabels.h"
42 #include "ui.h"
43 #include "prefs.h"
44 #include "recent.h" 
45 #include "file.h"
46 #include "view.h"
47 #include "debug.h"
48 #include "gnome-recent-view.h"
49 #include "alert.h"
50
51 #include <bonobo/bonobo-ui-util.h>
52 #include <bonobo/bonobo-control.h>
53
54 #include <gconf/gconf-client.h>
55
56 /*========================================================*/
57 /* Private macros and constants.                          */
58 /*========================================================*/
59
60 #define DEFAULT_WINDOW_WIDTH  500
61 #define DEFAULT_WINDOW_HEIGHT 375
62
63 /*========================================================*/
64 /* Private types.                                         */
65 /*========================================================*/
66
67 struct _glMDIPrivate
68 {
69         gint untitled_number;
70 };
71
72
73 /*========================================================*/
74 /* Private globals.                                       */
75 /*========================================================*/
76
77 static BonoboMDIClass *parent_class = NULL;
78
79
80 /*========================================================*/
81 /* Private function prototypes.                           */
82 /*========================================================*/
83
84 static void gl_mdi_class_init   (glMDIClass     *klass);
85 static void gl_mdi_init                 (glMDI  *mdi);
86 static void gl_mdi_finalize             (GObject        *object);
87
88 static void gl_mdi_app_created_cb       (BonoboMDI *mdi, BonoboWindow *win);
89 static void gl_mdi_set_app_toolbar_style        (BonoboWindow *win);
90 static void gl_mdi_set_app_statusbar_style      (BonoboWindow *win);
91
92 static gint gl_mdi_add_child_cb (BonoboMDI *mdi, BonoboMDIChild *child);
93 static gint gl_mdi_add_view_cb (BonoboMDI *mdi, GtkWidget *view);
94 static gint gl_mdi_remove_child_cb (BonoboMDI *mdi, BonoboMDIChild *child);
95 static gint gl_mdi_remove_view_cb (BonoboMDI *mdi, GtkWidget *view);
96
97 static void gl_mdi_view_changed_cb (BonoboMDI *mdi, GtkWidget *old_view);
98 static void gl_mdi_child_changed_cb (BonoboMDI *mdi, BonoboMDIChild *old_child);
99 static void gl_mdi_child_state_changed_cb (glMDIChild *child);
100
101 static void gl_mdi_set_active_window_undo_redo_verbs_sensitivity (BonoboMDI *mdi);
102 static void gl_mdi_set_active_window_selection_verbs_sensitivity (BonoboMDI *mdi);
103
104 static void gl_mdi_view_menu_item_toggled_cb (
105                         BonoboUIComponent           *ui_component,
106                         const char                  *path,
107                         Bonobo_UIComponent_EventType type,
108                         const char                  *state,
109                         BonoboWindow                *win);
110
111 \f
112 /*****************************************************************************/
113 /* Boilerplate object stuff.                                                 */
114 /*****************************************************************************/
115 GType
116 gl_mdi_get_type (void)
117 {
118         static GType mdi_type = 0;
119
120         if (mdi_type == 0)
121         {
122                 static const GTypeInfo our_info =
123                 {
124                         sizeof (glMDIClass),
125                         NULL,           /* base_init */
126                         NULL,           /* base_finalize */
127                         (GClassInitFunc) gl_mdi_class_init,
128                         NULL,           /* class_finalize */
129                         NULL,           /* class_data */
130                         sizeof (glMDI),
131                         0,              /* n_preallocs */
132                         (GInstanceInitFunc) gl_mdi_init
133                 };
134
135                 mdi_type = g_type_register_static (BONOBO_TYPE_MDI,
136                                                     "glMDI",
137                                                     &our_info,
138                                                     0);
139         }
140
141         return mdi_type;
142 }
143
144 static void
145 gl_mdi_class_init (glMDIClass *klass)
146 {
147         GObjectClass *object_class = G_OBJECT_CLASS (klass);
148
149         gl_debug (DEBUG_MDI, "START");
150
151         parent_class = g_type_class_peek_parent (klass);
152
153         object_class->finalize = gl_mdi_finalize;
154
155         gl_debug (DEBUG_MDI, "END");
156 }
157
158 static void 
159 gl_mdi_init (glMDI  *mdi)
160 {
161         gl_debug (DEBUG_MDI, "START");
162
163         bonobo_mdi_construct (BONOBO_MDI (mdi), "glabels", "gLabels",
164                               DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
165         
166         mdi->priv = g_new0 (glMDIPrivate, 1);
167
168         mdi->priv->untitled_number = 0; 
169
170         bonobo_mdi_set_ui_template_file (BONOBO_MDI (mdi),
171                                          GLABELS_UI_DIR "glabels-ui.xml",
172                                          gl_ui_verbs);
173         
174         bonobo_mdi_set_child_list_path (BONOBO_MDI (mdi), "/menu/Documents/");
175
176         /* Connect signals */
177         g_signal_connect (G_OBJECT (mdi), "top_window_created",
178                           G_CALLBACK (gl_mdi_app_created_cb), NULL);
179         
180         g_signal_connect (G_OBJECT (mdi), "add_child",
181                           G_CALLBACK (gl_mdi_add_child_cb), NULL);
182         g_signal_connect (G_OBJECT (mdi), "add_view",
183                           G_CALLBACK (gl_mdi_add_view_cb), NULL);
184         
185         g_signal_connect (G_OBJECT (mdi), "remove_child",
186                           G_CALLBACK (gl_mdi_remove_child_cb), NULL);
187         g_signal_connect (G_OBJECT (mdi), "remove_view",
188                           G_CALLBACK (gl_mdi_remove_view_cb), NULL);
189
190         g_signal_connect (G_OBJECT (mdi), "child_changed",
191                           G_CALLBACK (gl_mdi_child_changed_cb), NULL);
192         g_signal_connect (G_OBJECT (mdi), "view_changed",
193                           G_CALLBACK (gl_mdi_view_changed_cb), NULL);
194
195         g_signal_connect (G_OBJECT (mdi), "all_windows_destroyed",
196                           G_CALLBACK (gl_file_exit), NULL);
197
198         gl_debug (DEBUG_MDI, "END");
199 }
200
201 static void
202 gl_mdi_finalize (GObject *object)
203 {
204         glMDI *mdi;
205
206         gl_debug (DEBUG_MDI, "START");
207
208         g_return_if_fail (object != NULL);
209         
210         mdi = GL_MDI (object);
211
212         g_return_if_fail (GL_IS_MDI (mdi));
213         g_return_if_fail (mdi->priv != NULL);
214
215         G_OBJECT_CLASS (parent_class)->finalize (object);
216
217         g_free (mdi->priv);
218
219         gl_debug (DEBUG_MDI, "END");
220 }
221
222
223 /*****************************************************************************/
224 /* NEW mdi object.                                                           */
225 /*****************************************************************************/
226 glMDI*
227 gl_mdi_new (void)
228 {
229         glMDI *mdi;
230
231         gl_debug (DEBUG_MDI, "START");
232
233         mdi = GL_MDI (g_object_new (GL_TYPE_MDI, NULL));
234         g_return_val_if_fail (mdi != NULL, NULL);
235         
236         gl_debug (DEBUG_MDI, "END");
237         return mdi;
238 }
239
240 /*---------------------------------------------------------------------------*/
241 /* App created callback.                                                     */
242 /*---------------------------------------------------------------------------*/
243 static void
244 gl_mdi_app_created_cb (BonoboMDI *mdi, BonoboWindow *win)
245 {
246         GtkWidget *widget;
247         BonoboControl *control;
248         BonoboUIComponent *ui_component;
249         GnomeRecentView *view;
250         GnomeRecentModel *model;
251
252         static GtkTargetEntry drag_types[] =
253         {
254                 { "text/uri-list", 0, 0 },
255         };
256
257         static gint n_drag_types =
258                 sizeof (drag_types) / sizeof (drag_types [0]);
259
260         gl_debug (DEBUG_MDI, "START");
261         
262         ui_component = bonobo_mdi_get_ui_component_from_window (win);
263         g_return_if_fail (ui_component != NULL);
264         
265         /* Set the toolbar style according to prefs */
266         gl_mdi_set_app_toolbar_style (win);
267                 
268         /* Add listener fo the view menu */
269         bonobo_ui_component_add_listener (ui_component, "ViewToolbar", 
270                         (BonoboUIListenerFn)gl_mdi_view_menu_item_toggled_cb, 
271                         (gpointer)win);
272
273         bonobo_ui_component_add_listener (ui_component, "ToolbarSystem", 
274                         (BonoboUIListenerFn)gl_mdi_view_menu_item_toggled_cb, 
275                         (gpointer)win);
276         bonobo_ui_component_add_listener (ui_component, "ToolbarIcon", 
277                         (BonoboUIListenerFn)gl_mdi_view_menu_item_toggled_cb, 
278                         (gpointer)win);
279         bonobo_ui_component_add_listener (ui_component, "ToolbarIconText", 
280                         (BonoboUIListenerFn)gl_mdi_view_menu_item_toggled_cb, 
281                         (gpointer)win);
282         bonobo_ui_component_add_listener (ui_component, "ToolbarTooltips", 
283                         (BonoboUIListenerFn)gl_mdi_view_menu_item_toggled_cb, 
284                         (gpointer)win);
285
286
287         gl_ui_set_verb_list_sensitive (ui_component, 
288                                        gl_ui_no_docs_sensible_verbs,
289                                        FALSE);
290
291         /* add a GeditRecentView object */
292         model = gl_recent_get_model ();
293         view = GNOME_RECENT_VIEW (gnome_recent_view_bonobo_new (ui_component,
294                                                                 "/menu/File/Recents"));
295         gnome_recent_view_set_model (view, model);
296         
297         g_signal_connect (G_OBJECT (view), "activate",
298                           G_CALLBACK (gl_file_open_recent), win);
299
300         gl_debug (DEBUG_MDI, "END");
301 }
302
303 /*---------------------------------------------------------------------------*/
304 /* Menu item togglded callback.                                              */
305 /*---------------------------------------------------------------------------*/
306 static void
307 gl_mdi_view_menu_item_toggled_cb (
308                         BonoboUIComponent           *ui_component,
309                         const char                  *path,
310                         Bonobo_UIComponent_EventType type,
311                         const char                  *state,
312                         BonoboWindow                *win)
313 {
314         gboolean s;
315
316         gl_debug (DEBUG_MDI, "%s toggled to '%s'", path, state);
317
318         s = (strcmp (state, "1") == 0);
319
320         if ((strcmp (path, "ViewToolbar") == 0) &&
321             (s != gl_prefs->toolbar_visible))
322         {
323                 gl_prefs->toolbar_visible = s;
324                 gl_mdi_set_app_toolbar_style (win);
325
326                 return;
327         }
328
329         if (s && (strcmp (path, "ToolbarSystem") == 0) &&
330             (gl_prefs->toolbar_buttons_style != GL_TOOLBAR_SYSTEM))
331         {               
332                 gl_prefs->toolbar_buttons_style = GL_TOOLBAR_SYSTEM;
333                 gl_mdi_set_app_toolbar_style (win);
334
335                 return;
336         }
337
338         if (s && (strcmp (path, "ToolbarIcon") == 0) &&
339             (gl_prefs->toolbar_buttons_style != GL_TOOLBAR_ICONS))
340         {               
341                 gl_prefs->toolbar_buttons_style = GL_TOOLBAR_ICONS;
342                 gl_mdi_set_app_toolbar_style (win);
343
344                 return;
345         }
346
347         if (s && (strcmp (path, "ToolbarIconText") == 0) &&
348             (gl_prefs->toolbar_buttons_style != GL_TOOLBAR_ICONS_AND_TEXT))
349         {               
350                 gl_prefs->toolbar_buttons_style = GL_TOOLBAR_ICONS_AND_TEXT;
351                 gl_mdi_set_app_toolbar_style (win);
352
353                 return;
354         }
355
356         if ((strcmp (path, "ToolbarTooltips") == 0) &&
357             (s != gl_prefs->toolbar_view_tooltips))
358         {
359                 gl_prefs->toolbar_view_tooltips = s;
360                 gl_mdi_set_app_toolbar_style (win);
361
362                 return;
363         }
364
365 }
366
367 /*---------------------------------------------------------------------------*/
368 /* Set toolbar style.                                                        */
369 /*---------------------------------------------------------------------------*/
370 static void
371 gl_mdi_set_app_toolbar_style (BonoboWindow *win)
372 {
373         BonoboUIComponent *ui_component;
374         GConfClient *client;
375         gboolean labels;
376
377         gl_debug (DEBUG_MDI, "START");
378         
379         g_return_if_fail (BONOBO_IS_WINDOW (win));
380                         
381         ui_component = bonobo_mdi_get_ui_component_from_window (win);
382         g_return_if_fail (ui_component != NULL);
383                         
384         bonobo_ui_component_freeze (ui_component, NULL);
385
386         /* Updated view menu */
387         gl_ui_set_verb_state (ui_component, 
388                               "/commands/ViewToolbar",
389                               gl_prefs->toolbar_visible);
390
391         gl_ui_set_verb_sensitive (ui_component, 
392                                   "/commands/ToolbarSystem",
393                                   gl_prefs->toolbar_visible);
394         gl_ui_set_verb_sensitive (ui_component, 
395                                   "/commands/ToolbarIcon",
396                                   gl_prefs->toolbar_visible);
397         gl_ui_set_verb_sensitive (ui_component, 
398                                   "/commands/ToolbarIconText",
399                                   gl_prefs->toolbar_visible);
400         gl_ui_set_verb_sensitive (ui_component, 
401                                   "/commands/ToolbarTooltips",
402                                   gl_prefs->toolbar_visible);
403
404         gl_ui_set_verb_state (ui_component, 
405                               "/commands/ToolbarSystem",
406                               gl_prefs->toolbar_buttons_style == GL_TOOLBAR_SYSTEM);
407
408         gl_ui_set_verb_state (ui_component, 
409                               "/commands/ToolbarIcon",
410                               gl_prefs->toolbar_buttons_style == GL_TOOLBAR_ICONS);
411
412         gl_ui_set_verb_state (ui_component, 
413                               "/commands/ToolbarIconText",
414                               gl_prefs->toolbar_buttons_style == GL_TOOLBAR_ICONS_AND_TEXT);
415
416         gl_ui_set_verb_state (ui_component, 
417                               "/commands/ToolbarTooltips",
418                               gl_prefs->toolbar_view_tooltips);
419
420         
421         /* Actually update toolbar style */
422         bonobo_ui_component_set_prop (
423                 ui_component, "/Toolbar",
424                 "tips", gl_prefs->toolbar_view_tooltips ? "1" : "0",
425                 NULL);
426         
427         switch (gl_prefs->toolbar_buttons_style)
428         {
429                 case GL_TOOLBAR_SYSTEM:
430                                                 
431                         client = gconf_client_get_default ();
432                         if (client == NULL) 
433                                 goto error;
434
435                         labels = gconf_client_get_bool (client, 
436                                         "/desktop/gnome/interface/toolbar-labels", NULL);
437
438                         g_object_unref (G_OBJECT (client));
439                         
440                         if (labels)
441                         {                       
442                                 gl_debug (DEBUG_MDI, "SYSTEM: BOTH");
443                                 bonobo_ui_component_set_prop (
444                                         ui_component, "/Toolbar", "look", "both", NULL);
445                         
446                         }
447                         else
448                         {
449                                 gl_debug (DEBUG_MDI, "SYSTEM: ICONS");
450                                 bonobo_ui_component_set_prop (
451                                         ui_component, "/Toolbar", "look", "icons", NULL);
452                         }
453         
454                         break;
455                         
456                 case GL_TOOLBAR_ICONS:
457                         gl_debug (DEBUG_MDI, "GLABELS: ICONS");
458                         bonobo_ui_component_set_prop (
459                                 ui_component, "/Toolbar", "look", "icon", NULL);
460                         
461                         break;
462                         
463                 case GL_TOOLBAR_ICONS_AND_TEXT:
464                         gl_debug (DEBUG_MDI, "GLABELS: ICONS_AND_TEXT");
465                         bonobo_ui_component_set_prop (
466                                 ui_component, "/Toolbar", "look", "both", NULL);
467                         
468                         break;
469                 default:
470                         goto error;
471                         break;
472         }
473         
474         bonobo_ui_component_set_prop (
475                         ui_component, "/Toolbar",
476                         "hidden", gl_prefs->toolbar_visible ? "0":"1", NULL);
477
478  error:
479         bonobo_ui_component_thaw (ui_component, NULL);
480
481         gl_debug (DEBUG_MDI, "END");
482 }
483
484
485 /*---------------------------------------------------------------------------*/
486 /* Child state changed callback.                                             */
487 /*---------------------------------------------------------------------------*/
488 static void 
489 gl_mdi_child_state_changed_cb (glMDIChild *child)
490 {
491         gl_debug (DEBUG_MDI, "START");
492
493         if (bonobo_mdi_get_active_child (BONOBO_MDI (glabels_mdi)) != BONOBO_MDI_CHILD (child))
494                 return;
495         
496         gl_mdi_set_active_window_title (BONOBO_MDI (glabels_mdi));
497         gl_mdi_set_active_window_verbs_sensitivity (BONOBO_MDI (glabels_mdi));
498
499         gl_debug (DEBUG_MDI, "END");
500 }
501
502 /*---------------------------------------------------------------------------*/
503 /* Child undo/redo state changed callback.                                   */
504 /*---------------------------------------------------------------------------*/
505 static void 
506 gl_mdi_child_undo_redo_state_changed_cb (glMDIChild *child)
507 {
508         gl_debug (DEBUG_MDI, "START");
509
510         if (bonobo_mdi_get_active_child (BONOBO_MDI (glabels_mdi)) != BONOBO_MDI_CHILD (child))
511                 return;
512         
513         gl_mdi_set_active_window_undo_redo_verbs_sensitivity (BONOBO_MDI (glabels_mdi));
514         gl_debug (DEBUG_MDI, "END");
515 }
516
517 /*---------------------------------------------------------------------------*/
518 /* Add child callback.                                                       */
519 /*---------------------------------------------------------------------------*/
520 static gint 
521 gl_mdi_add_child_cb (BonoboMDI *mdi, BonoboMDIChild *child)
522 {
523         gl_debug (DEBUG_MDI, "START");
524
525         g_signal_connect (G_OBJECT (child), "state_changed",
526                           G_CALLBACK (gl_mdi_child_state_changed_cb), 
527                           NULL);
528         g_signal_connect (G_OBJECT (child), "undo_redo_state_changed",
529                           G_CALLBACK (gl_mdi_child_undo_redo_state_changed_cb), 
530                           NULL);
531
532         gl_debug (DEBUG_MDI, "END");
533         return TRUE;
534 }
535
536 /*---------------------------------------------------------------------------*/
537 /* View selection state changed callback.                                    */
538 /*---------------------------------------------------------------------------*/
539 static void 
540 gl_mdi_view_selection_state_changed_cb (glView *view)
541 {
542         gl_debug (DEBUG_MDI, "START");
543
544         if (bonobo_mdi_get_active_view (BONOBO_MDI (glabels_mdi)) != GTK_WIDGET (view))
545                 return;
546         
547         gl_mdi_set_active_window_selection_verbs_sensitivity (BONOBO_MDI (glabels_mdi));
548         gl_debug (DEBUG_MDI, "END");
549 }
550
551 /*---------------------------------------------------------------------------*/
552 /* Add view callback.                                                        */
553 /*---------------------------------------------------------------------------*/
554 static gint 
555 gl_mdi_add_view_cb (BonoboMDI *mdi, GtkWidget *view)
556 {
557         gl_debug (DEBUG_MDI, "START");
558
559         g_signal_connect (G_OBJECT (view), "selection_changed",
560                           G_CALLBACK (gl_mdi_view_selection_state_changed_cb), 
561                           NULL);
562
563         gl_debug (DEBUG_MDI, "END");
564         return TRUE;
565 }
566
567 /*---------------------------------------------------------------------------*/
568 /* Remove child callback.                                                    */
569 /*---------------------------------------------------------------------------*/
570 static gint 
571 gl_mdi_remove_child_cb (BonoboMDI *mdi, BonoboMDIChild *child)
572 {
573         glLabel* doc;
574         gboolean close = TRUE;
575         
576         gl_debug (DEBUG_MDI, "START");
577
578         g_return_val_if_fail (child != NULL, FALSE);
579         g_return_val_if_fail (GL_MDI_CHILD (child)->label != NULL, FALSE);
580
581         doc = GL_MDI_CHILD (child)->label;
582
583         if (gl_label_is_modified (doc))
584         {
585                 GtkWidget *msgbox, *w;
586                 gchar *fname = NULL, *msg = NULL;
587                 gint ret;
588                 gboolean exiting;
589
590                 w = GTK_WIDGET (g_list_nth_data (bonobo_mdi_child_get_views (child), 0));
591                         
592                 if(w != NULL)
593                         bonobo_mdi_set_active_view (mdi, w);
594
595                 fname = gl_label_get_short_name (doc);
596
597                 msg = g_strdup_printf (_("Save changes to document \"%s\" before closing?"),
598                                         fname);
599
600                 msgbox = gl_alert_dialog_new (GTK_WINDOW (bonobo_mdi_get_active_window (mdi)),
601                                 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
602                                 GTK_MESSAGE_WARNING,
603                                 GTK_BUTTONS_NONE,
604                                 msg,
605                                 _("Your changes will be lost if you don't save them."));
606
607                 gtk_dialog_add_button (GTK_DIALOG (msgbox),
608                                        _("Close without saving"),
609                                        GTK_RESPONSE_NO);
610
611                 if (glabels_close_x_button_pressed)
612                         exiting = FALSE;
613                 else if (glabels_exit_button_pressed)
614                         exiting = TRUE;
615                 else
616                 {
617                         /* Delete event generated */
618                         if (g_list_length (bonobo_mdi_get_windows (BONOBO_MDI (glabels_mdi))) == 1)
619                                 exiting = TRUE;
620                         else
621                                 exiting = FALSE;
622                 }
623
624                 
625                 gtk_dialog_add_button (GTK_DIALOG (msgbox), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
626
627                 gtk_dialog_add_button (GTK_DIALOG (msgbox),
628                                 GTK_STOCK_SAVE,
629                                 GTK_RESPONSE_YES);
630
631                 gtk_dialog_set_default_response (GTK_DIALOG (msgbox), GTK_RESPONSE_YES);
632
633                 gtk_window_set_resizable (GTK_WINDOW (msgbox), FALSE);
634
635                 ret = gtk_dialog_run (GTK_DIALOG (msgbox));
636                 
637                 gtk_widget_destroy (msgbox);
638
639                 g_free (fname);
640                 g_free (msg);
641                 
642                 switch (ret)
643                 {
644                         case GTK_RESPONSE_YES:
645                                 close = gl_file_save (GL_MDI_CHILD (child));
646                                 break;
647                         case GTK_RESPONSE_NO:
648                                 close = TRUE;
649                                 break;
650                         default:
651                                 close = FALSE;
652                 }
653
654                 gl_debug (DEBUG_MDI, "CLOSE: %s", close ? "TRUE" : "FALSE");
655         }
656         
657         /* FIXME: there is a bug if you "close all" >1 docs, don't save the document
658          * and then don't close the last one.
659          */
660         /* Disable to avoid the bug */
661         /*
662         if (close)
663         {
664                 g_signal_handlers_disconnect_by_func (child, 
665                                                       G_CALLBACK (gl_mdi_child_state_changed_cb),
666                                                       NULL);
667                 g_signal_handlers_disconnect_by_func (GTK_OBJECT (child), 
668                                                       G_CALLBACK (gl_mdi_child_undo_redo_state_changed_cb),
669                                                       NULL);
670         }
671         */
672         
673         gl_debug (DEBUG_MDI, "END");
674         return close;
675 }
676
677 /*---------------------------------------------------------------------------*/
678 /* Remove view callback.                                                     */
679 /*---------------------------------------------------------------------------*/
680 static gint 
681 gl_mdi_remove_view_cb (BonoboMDI *mdi,  GtkWidget *view)
682 {
683         gl_debug (DEBUG_MDI, "");
684         gl_debug (DEBUG_MDI, "END");
685
686         return TRUE;
687 }
688
689 /*****************************************************************************/
690 /* Set title of active window.                                               */
691 /*****************************************************************************/
692 void 
693 gl_mdi_set_active_window_title (BonoboMDI *mdi)
694 {
695         BonoboMDIChild* active_child = NULL;
696         glLabel* doc = NULL;
697         gchar* docname = NULL;
698         gchar* title = NULL;
699         
700         gl_debug (DEBUG_MDI, "START");
701
702         
703         active_child = bonobo_mdi_get_active_child (mdi);
704         if (active_child == NULL)
705                 return;
706
707         doc = GL_MDI_CHILD (active_child)->label;
708         g_return_if_fail (doc != NULL);
709         
710         /* Set active window title */
711         docname = gl_label_get_short_name (doc);
712         g_return_if_fail (docname != NULL);
713
714         if (gl_label_is_modified (doc))
715         {
716                 title = g_strdup_printf ("%s %s - glabels",
717                                          docname, _("(modified)"));
718         } 
719         else 
720         {
721                 title = g_strdup_printf ("%s - glabels", docname);
722
723         }
724
725         gtk_window_set_title (GTK_WINDOW (bonobo_mdi_get_active_window (mdi)), title);
726         
727         g_free (docname);
728         g_free (title);
729
730         gl_debug (DEBUG_MDI, "END");
731 }
732
733 /*---------------------------------------------------------------------------*/
734 /* Child changed callback.                                                   */
735 /*---------------------------------------------------------------------------*/
736 static 
737 void gl_mdi_child_changed_cb (BonoboMDI *mdi, BonoboMDIChild *old_child)
738 {
739         gl_debug (DEBUG_MDI, "START");
740
741         gl_mdi_set_active_window_title (mdi);   
742
743         gl_debug (DEBUG_MDI, "END");
744 }
745
746 /*---------------------------------------------------------------------------*/
747 /* View changed callback.                                                    */
748 /*---------------------------------------------------------------------------*/
749 static 
750 void gl_mdi_view_changed_cb (BonoboMDI *mdi, GtkWidget *old_view)
751 {
752         BonoboWindow *win;
753         GtkWidget* status;
754         GtkWidget *active_view;
755         
756         gl_debug (DEBUG_MDI, "START");
757
758         gl_mdi_set_active_window_verbs_sensitivity (mdi);
759
760         active_view = bonobo_mdi_get_active_view (mdi);
761                 
762         win = bonobo_mdi_get_active_window (mdi);
763         g_return_if_fail (win != NULL);
764
765         gl_debug (DEBUG_MDI, "END");
766 }
767
768 /*****************************************************************************/
769 /* Set sensitivity of verbs in active window.                                */
770 /*****************************************************************************/
771 void 
772 gl_mdi_set_active_window_verbs_sensitivity (BonoboMDI *mdi)
773 {
774         /* FIXME: it is too slooooooow! - Paolo */
775
776         BonoboWindow      *active_window = NULL;
777         BonoboMDIChild    *active_child = NULL;
778         glView            *view = NULL;
779         glLabel           *doc = NULL;
780         BonoboUIComponent *ui_component;
781         
782         gl_debug (DEBUG_MDI, "START");
783         
784         active_window = bonobo_mdi_get_active_window (mdi);
785
786         if (active_window == NULL)
787                 return;
788         
789         ui_component = bonobo_mdi_get_ui_component_from_window (active_window);
790         g_return_if_fail (ui_component != NULL);
791         
792         active_child = bonobo_mdi_get_active_child (mdi);
793         
794         bonobo_ui_component_freeze (ui_component, NULL);
795         
796         if (active_child == NULL)
797         {
798                 gl_ui_set_verb_list_sensitive (ui_component, 
799                                                gl_ui_no_docs_sensible_verbs,
800                                                FALSE);
801                 goto end;
802         }
803         else
804         {
805                 gl_ui_set_verb_list_sensitive (ui_component, 
806                                                gl_ui_all_sensible_verbs,
807                                                TRUE);
808         }
809
810         doc = GL_MDI_CHILD (active_child)->label;
811         g_return_if_fail (doc != NULL);
812
813         if (!gl_label_can_undo (doc))
814                 gl_ui_set_verb_sensitive (ui_component,
815                                           "/commands/EditUndo",
816                                           FALSE);
817
818         if (!gl_label_can_redo (doc))
819                 gl_ui_set_verb_sensitive (ui_component,
820                                           "/commands/EditRedo",
821                                           FALSE);
822
823         gl_ui_set_verb_list_sensitive (ui_component, 
824                                        gl_ui_not_modified_doc_sensible_verbs,
825                                        gl_label_is_modified (doc));
826
827         view = GL_VIEW (bonobo_mdi_get_active_view (mdi));
828         g_return_if_fail (view != NULL);
829
830         gl_ui_set_verb_list_sensitive (ui_component,
831                                        gl_ui_selection_sensible_verbs,
832                                        !gl_view_is_selection_empty (view));
833         gl_ui_set_verb_list_sensitive (ui_component,
834                                        gl_ui_atomic_selection_sensible_verbs,
835                                        gl_view_is_selection_atomic (view));
836
837 end:
838         bonobo_ui_component_thaw (ui_component, NULL);
839
840         gl_debug (DEBUG_MDI, "END");
841 }
842
843
844 /*****************************************************************************/
845 /* Set sensitivity of undo/redo verbs in active window.                      */
846 /*****************************************************************************/
847 static void 
848 gl_mdi_set_active_window_undo_redo_verbs_sensitivity (BonoboMDI *mdi)
849 {
850         BonoboWindow* active_window = NULL;
851         BonoboMDIChild* active_child = NULL;
852         glLabel* doc = NULL;
853         BonoboUIComponent *ui_component;
854         
855         gl_debug (DEBUG_MDI, "START");
856         
857         active_window = bonobo_mdi_get_active_window (mdi);
858         g_return_if_fail (active_window != NULL);
859         
860         ui_component = bonobo_mdi_get_ui_component_from_window (active_window);
861         g_return_if_fail (ui_component != NULL);
862         
863         active_child = bonobo_mdi_get_active_child (mdi);
864         doc = GL_MDI_CHILD (active_child)->label;
865         g_return_if_fail (doc != NULL);
866
867         bonobo_ui_component_freeze (ui_component, NULL);
868
869         gl_ui_set_verb_sensitive (ui_component, "/commands/EditUndo", 
870                                   gl_label_can_undo (doc));     
871
872         gl_ui_set_verb_sensitive (ui_component, "/commands/EditRedo", 
873                                   gl_label_can_redo (doc));     
874
875         bonobo_ui_component_thaw (ui_component, NULL);
876
877         gl_debug (DEBUG_MDI, "END");
878 }
879
880 /*****************************************************************************/
881 /* Set sensitivity of selection verbs in active window.                      */
882 /*****************************************************************************/
883 static void 
884 gl_mdi_set_active_window_selection_verbs_sensitivity (BonoboMDI *mdi)
885 {
886         BonoboWindow      *active_window = NULL;
887         glView            *view = NULL;
888         BonoboUIComponent *ui_component;
889         
890         gl_debug (DEBUG_MDI, "START");
891         
892         active_window = bonobo_mdi_get_active_window (mdi);
893         g_return_if_fail (active_window != NULL);
894         
895         ui_component = bonobo_mdi_get_ui_component_from_window (active_window);
896         g_return_if_fail (ui_component != NULL);
897         
898         view = GL_VIEW (bonobo_mdi_get_active_view (mdi));
899         g_return_if_fail (view != NULL);
900
901         bonobo_ui_component_freeze (ui_component, NULL);
902
903         gl_ui_set_verb_list_sensitive (ui_component,
904                                        gl_ui_selection_sensible_verbs,
905                                        !gl_view_is_selection_empty (view));
906         gl_ui_set_verb_list_sensitive (ui_component,
907                                        gl_ui_atomic_selection_sensible_verbs,
908                                        gl_view_is_selection_atomic (view));
909
910         bonobo_ui_component_thaw (ui_component, NULL);
911
912         gl_debug (DEBUG_MDI, "END");
913 }
914