source: trunk/third/bonobo/bonobo/bonobo-win.c @ 16750

Revision 16750, 16.2 KB checked in by ghudson, 23 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r16749, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2/*
3 * bonobo-win.c: The Bonobo Window implementation.
4 *
5 * Author:
6 *      Michael Meeks (michael@helixcode.com)
7 *
8 * Copyright 2000 Helix Code, Inc.
9 */
10#include "config.h"
11#include <libgnomeui/gnome-dock-item.h>
12#include <libgnomeui/gnome-dock.h>
13#include <libgnomeui/gnome-preferences.h>
14#include <libgnomeui/gnome-window-icon.h>
15#include <liboaf/liboaf.h>
16
17#include <bonobo/bonobo-ui-engine.h>
18#include <bonobo/bonobo-ui-sync-menu.h>
19#include <bonobo/bonobo-ui-sync-keys.h>
20#include <bonobo/bonobo-ui-sync-status.h>
21#include <bonobo/bonobo-ui-sync-toolbar.h>
22#include <bonobo/bonobo-win.h>
23
24#include <gnome-xml/tree.h>
25#include <gnome-xml/parser.h>
26
27GtkWindowClass *bonobo_window_parent_class = NULL;
28
29struct _BonoboWindowPrivate {
30        BonoboUIEngine *engine;
31
32        BonoboUISync   *sync_menu;
33        BonoboUISync   *sync_keys;
34        BonoboUISync   *sync_status;
35        BonoboUISync   *sync_toolbar;
36       
37        GnomeDock      *dock;
38
39        GnomeDockItem  *menu_item;
40        GtkMenuBar     *menu;
41
42        GtkAccelGroup  *accel_group;
43
44        char           *name;           /* Win name */
45        char           *prefix;         /* Win prefix */
46
47        GtkWidget      *main_vbox;
48
49        GtkBox         *status;
50
51        GtkWidget      *client_area;
52};
53
54/**
55 * bonobo_window_deregister_dead_components:
56 * @win:
57 *
58 * Deprecated
59 **/
60void
61bonobo_window_deregister_dead_components (BonoboWindow *win)
62{
63        g_return_if_fail (BONOBO_IS_WINDOW (win));
64
65        bonobo_ui_engine_deregister_dead_components (
66                win->priv->engine);
67}
68
69/**
70 * bonobo_window_register_component:
71 * @win:
72 * @name:
73 * @component:
74 *
75 * Deprecated
76 **/
77void
78bonobo_window_register_component (BonoboWindow  *win,
79                                  const char    *name,
80                                  Bonobo_Unknown component)
81{
82        g_return_if_fail (BONOBO_IS_WINDOW (win));
83
84        bonobo_ui_engine_register_component (
85                win->priv->engine, name, component);
86}
87
88/**
89 * bonobo_window_deregister_component:
90 * @win:
91 * @name:
92 *
93 * Deprecated
94 **/
95void
96bonobo_window_deregister_component (BonoboWindow *win,
97                                    const char   *name)
98{
99        g_return_if_fail (BONOBO_IS_WINDOW (win));
100
101        bonobo_ui_engine_deregister_component (
102                win->priv->engine, name);
103}
104
105/**
106 * bonobo_window_deregister_component_by_ref:
107 * @win:
108 * @ref:
109 *
110 * Deprecated
111 **/
112void
113bonobo_window_deregister_component_by_ref (BonoboWindow  *win,
114                                           Bonobo_Unknown ref)
115{
116        g_return_if_fail (BONOBO_IS_WINDOW (win));
117
118        bonobo_ui_engine_deregister_component_by_ref (
119                win->priv->engine, ref);
120}
121
122/**
123 * bonobo_window_remove_popup:
124 * @win: the window
125 * @path: the path
126 *
127 * Remove the popup at @path
128 **/
129void
130bonobo_window_remove_popup (BonoboWindow     *win,
131                            const char    *path)
132{
133        g_return_if_fail (path != NULL);
134        g_return_if_fail (BONOBO_IS_WINDOW (win));
135
136        bonobo_ui_sync_menu_remove_popup (
137                BONOBO_UI_SYNC_MENU (win->priv->sync_menu), path);
138}
139
140/**
141 * bonobo_window_add_popup:
142 * @win: the window
143 * @menu: the menu widget
144 * @path: the path
145 *
146 * Add a popup @menu at @path
147 **/
148void
149bonobo_window_add_popup (BonoboWindow *win,
150                         GtkMenu      *menu,
151                         const char   *path)
152{
153        g_return_if_fail (path != NULL);
154        g_return_if_fail (BONOBO_IS_WINDOW (win));
155
156        bonobo_ui_sync_menu_add_popup (
157                BONOBO_UI_SYNC_MENU (win->priv->sync_menu), menu, path);
158}
159
160/**
161 * bonobo_window_deregister_get_component_names:
162 * @win:
163 *
164 * Deprecated
165 *
166 * Return value:
167 **/
168GList *
169bonobo_window_deregister_get_component_names (BonoboWindow *win)
170{
171        g_return_val_if_fail (BONOBO_IS_WINDOW (win), NULL);
172
173        return bonobo_ui_engine_get_component_names (win->priv->engine);
174}
175
176
177/**
178 * bonobo_window_component_get:
179 * @win:
180 * @name:
181 *
182 * Deprecated
183 *
184 * Return value:
185 **/
186Bonobo_Unknown
187bonobo_window_component_get (BonoboWindow *win,
188                             const char  *name)
189{
190        g_return_val_if_fail (BONOBO_IS_WINDOW (win), CORBA_OBJECT_NIL);
191
192        return bonobo_ui_engine_get_component (win->priv->engine, name);
193}
194
195/**
196 * bonobo_window_set_contents:
197 * @win: the bonobo window
198 * @contents: the new widget for it to contain.
199 *
200 * Insert a widget into the main window contents.
201 **/
202void
203bonobo_window_set_contents (BonoboWindow *win,
204                            GtkWidget    *contents)
205{
206        g_return_if_fail (win != NULL);
207        g_return_if_fail (win->priv != NULL);
208        g_return_if_fail (win->priv->client_area != NULL);
209
210        gtk_container_add (GTK_CONTAINER (win->priv->client_area), contents);
211}
212
213/**
214 * bonobo_window_get_contents:
215 * @win: the bonobo window
216 *
217 * Return value: the contained widget
218 **/
219GtkWidget *
220bonobo_window_get_contents (BonoboWindow *win)
221{
222        GList     *children;
223        GtkWidget *widget;
224
225        g_return_val_if_fail (win != NULL, NULL);
226        g_return_val_if_fail (win->priv != NULL, NULL);
227        g_return_val_if_fail (win->priv->dock != NULL, NULL);
228
229        children = gtk_container_children (
230                GTK_CONTAINER (win->priv->client_area));
231
232        widget = children ? children->data : NULL;
233
234        g_list_free (children);
235
236        return widget;
237}
238
239static void
240destroy_priv (BonoboWindowPrivate *priv)
241{
242        gtk_object_unref (GTK_OBJECT (priv->engine));
243        priv->engine = NULL;
244
245        g_free (priv->name);
246        priv->name = NULL;
247
248        g_free (priv->prefix);
249        priv->prefix = NULL;
250       
251        g_free (priv);
252}
253
254static void
255bonobo_window_finalize (GtkObject *object)
256{
257        BonoboWindow *win = (BonoboWindow *)object;
258       
259        if (win) {
260                if (win->priv)
261                        destroy_priv (win->priv);
262                win->priv = NULL;
263        }
264
265        GTK_OBJECT_CLASS (bonobo_window_parent_class)->finalize (object);
266}
267
268char *
269bonobo_window_xml_get (BonoboWindow *win,
270                       const char   *path,
271                       gboolean      node_only)
272{
273        g_return_val_if_fail (BONOBO_IS_WINDOW (win), NULL);
274
275        return bonobo_ui_engine_xml_get (win->priv->engine, path, node_only);
276}
277
278/**
279 * bonobo_window_xml_node_exists:
280 * @win:
281 * @path:
282 *
283 * Deprecated
284 *
285 * Return value:
286 **/
287gboolean
288bonobo_window_xml_node_exists (BonoboWindow *win,
289                               const char   *path)
290{
291        g_return_val_if_fail (BONOBO_IS_WINDOW (win), FALSE);
292
293        return bonobo_ui_engine_xml_node_exists (
294                win->priv->engine, path);
295}
296
297/**
298 * bonobo_window_object_set:
299 * @win:
300 * @path:
301 * @object:
302 * @ev:
303 *
304 * Deprecated
305 *
306 * Return value:
307 **/
308BonoboUIError   
309bonobo_window_object_set (BonoboWindow  *win,
310                          const char    *path,
311                          Bonobo_Unknown object,
312                          CORBA_Environment *ev)
313{
314        g_return_val_if_fail (BONOBO_IS_WINDOW (win),
315                              BONOBO_UI_ERROR_BAD_PARAM);
316
317        return bonobo_ui_engine_object_set (
318                win->priv->engine, path, object, ev);
319}
320
321/**
322 * bonobo_window_object_get:
323 * @win:
324 * @path:
325 * @object:
326 * @ev:
327 *
328 * Deprecated
329 *
330 * Return value:
331 **/
332BonoboUIError   
333bonobo_window_object_get (BonoboWindow   *win,
334                          const char     *path,
335                          Bonobo_Unknown *object,
336                          CORBA_Environment *ev)
337{
338        g_return_val_if_fail (BONOBO_IS_WINDOW (win),
339                              BONOBO_UI_ERROR_BAD_PARAM);
340
341        return bonobo_ui_engine_object_get (
342                win->priv->engine, path, object, ev);
343}
344
345/**
346 * bonobo_window_xml_merge_tree:
347 * @win:
348 * @path:
349 * @tree:
350 * @component:
351 *
352 * Deprecated
353 *
354 * Return value:
355 **/
356BonoboUIError   
357bonobo_window_xml_merge_tree (BonoboWindow *win,
358                              const char   *path,
359                              BonoboUINode *tree,
360                              const char   *component)
361{
362        g_return_val_if_fail (BONOBO_IS_WINDOW (win),
363                              BONOBO_UI_ERROR_BAD_PARAM);
364
365        return bonobo_ui_engine_xml_merge_tree (
366                win->priv->engine, path, tree, component);
367}
368
369/**
370 * bonobo_window_xml_merge:
371 * @win:
372 * @path:
373 * @xml:
374 * @component:
375 *
376 * Deprecated
377 *
378 * Return value:
379 **/
380BonoboUIError   
381bonobo_window_xml_merge (BonoboWindow *win,
382                         const char   *path,
383                         const char   *xml,
384                         const char   *component)
385{
386        BonoboUIError    err;
387        BonoboUINode *node;
388       
389        g_return_val_if_fail (win != NULL, BONOBO_UI_ERROR_BAD_PARAM);
390        g_return_val_if_fail (xml != NULL, BONOBO_UI_ERROR_BAD_PARAM);
391        g_return_val_if_fail (win->priv != NULL, BONOBO_UI_ERROR_BAD_PARAM);
392
393/*      fprintf (stderr, "Merging :\n%s\n", xml);*/
394
395        node = bonobo_ui_node_from_string (xml);
396       
397        if (!node)
398                return BONOBO_UI_ERROR_INVALID_XML;
399
400        err = bonobo_window_xml_merge_tree (win, path, node, component);
401
402        return err;
403}
404
405/**
406 * bonobo_window_xml_rm:
407 * @win:
408 * @path:
409 * @by_component:
410 *
411 * Deprecated
412 *
413 * Return value:
414 **/
415BonoboUIError   
416bonobo_window_xml_rm (BonoboWindow *win,
417                      const char   *path,
418                      const char   *by_component)
419{
420        g_return_val_if_fail (BONOBO_IS_WINDOW (win), BONOBO_UI_ERROR_BAD_PARAM);
421
422        return bonobo_ui_engine_xml_rm (
423                win->priv->engine, path, by_component);
424}
425
426/**
427 * bonobo_window_freeze:
428 * @win:
429 *
430 * Deprecated
431 **/
432void
433bonobo_window_freeze (BonoboWindow *win)
434{
435        g_return_if_fail (BONOBO_IS_WINDOW (win));
436
437        bonobo_ui_engine_freeze (win->priv->engine);
438}
439
440/**
441 * bonobo_window_thaw:
442 * @win:
443 *
444 * Deprecated
445 **/
446void
447bonobo_window_thaw (BonoboWindow *win)
448{
449        g_return_if_fail (BONOBO_IS_WINDOW (win));
450       
451        bonobo_ui_engine_thaw (win->priv->engine);
452}
453
454/**
455 * bonobo_window_dump:
456 * @win:
457 * @msg:
458 *
459 * Deprecated
460 **/
461void
462bonobo_window_dump (BonoboWindow *win,
463                    const char   *msg)
464{
465        g_return_if_fail (BONOBO_IS_WINDOW (win));
466
467        fprintf (stderr, "Bonobo Win '%s'\n", win->priv->name);
468
469        bonobo_ui_engine_dump (win->priv->engine, stderr, msg);
470}
471
472/**
473 * bonobo_window_get_accel_group:
474 * @win: the bonobo window
475 *
476 * Return value: the associated accelerator group for this window
477 **/
478GtkAccelGroup *
479bonobo_window_get_accel_group (BonoboWindow *win)
480{
481        g_return_val_if_fail (BONOBO_IS_WINDOW (win), NULL);
482
483        return win->priv->accel_group;
484}
485
486static BonoboWindowPrivate *
487construct_priv (BonoboWindow *win)
488{
489        BonoboWindowPrivate *priv;
490        GnomeDockItemBehavior behavior;
491
492        priv = g_new0 (BonoboWindowPrivate, 1);
493
494        priv->engine = bonobo_ui_engine_new ();
495
496        priv->dock = GNOME_DOCK (gnome_dock_new ());
497        gtk_container_add (GTK_CONTAINER (win),
498                           GTK_WIDGET    (priv->dock));
499
500        behavior = (GNOME_DOCK_ITEM_BEH_EXCLUSIVE
501                    | GNOME_DOCK_ITEM_BEH_NEVER_VERTICAL);
502        if (!gnome_preferences_get_menubar_detachable ())
503                behavior |= GNOME_DOCK_ITEM_BEH_LOCKED;
504
505        priv->menu_item = GNOME_DOCK_ITEM (gnome_dock_item_new (
506                "menu", behavior));
507        priv->menu      = GTK_MENU_BAR (gtk_menu_bar_new ());
508        gtk_container_add (GTK_CONTAINER (priv->menu_item),
509                           GTK_WIDGET    (priv->menu));
510        gnome_dock_add_item (priv->dock, priv->menu_item,
511                             GNOME_DOCK_TOP, 0, 0, 0, TRUE);
512
513        /*
514         * To have menubar relief agree with the toolbar (and have the relief outside of
515         * smaller handles), substitute the dock item's relief for the menubar's relief,
516         * but don't change the size of the menubar in the process.
517         */
518        gtk_menu_bar_set_shadow_type (GTK_MENU_BAR (priv->menu), GTK_SHADOW_NONE);
519        if (gnome_preferences_get_menubar_relief ()) {
520                guint border_width;
521
522                gtk_container_set_border_width (GTK_CONTAINER (priv->menu_item), 2);
523                border_width = GTK_CONTAINER (priv->menu)->border_width;
524                if (border_width >= 2)
525                        border_width -= 2;
526                gtk_container_set_border_width (GTK_CONTAINER (priv->menu), border_width);
527        } else
528                gnome_dock_item_set_shadow_type (GNOME_DOCK_ITEM (priv->menu_item), GTK_SHADOW_NONE);
529
530        priv->main_vbox = gtk_vbox_new (FALSE, 0);
531        gnome_dock_set_client_area (priv->dock, priv->main_vbox);
532
533        priv->client_area = gtk_vbox_new (FALSE, 0);
534        gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->client_area, TRUE, TRUE, 0);
535
536        priv->status = GTK_BOX (gtk_hbox_new (FALSE, 0));
537        gtk_box_pack_start (GTK_BOX (priv->main_vbox), GTK_WIDGET (priv->status), FALSE, FALSE, 0);
538
539        priv->accel_group = gtk_accel_group_new ();
540        gtk_window_add_accel_group (GTK_WINDOW (win),
541                                    priv->accel_group);
542
543        gtk_widget_show_all (GTK_WIDGET (priv->dock));
544        gtk_widget_hide (GTK_WIDGET (priv->status));
545
546        priv->sync_menu = bonobo_ui_sync_menu_new (
547                priv->engine, priv->menu,
548                GTK_WIDGET (priv->menu_item),
549                priv->accel_group);
550
551        bonobo_ui_engine_add_sync (priv->engine, priv->sync_menu);
552
553
554        priv->sync_toolbar = bonobo_ui_sync_toolbar_new (
555                priv->engine, GNOME_DOCK (priv->dock));
556
557        bonobo_ui_engine_add_sync (priv->engine, priv->sync_toolbar);
558
559        /* Keybindings; the gtk_binding stuff is just too evil */
560        priv->sync_keys = bonobo_ui_sync_keys_new (priv->engine);
561        bonobo_ui_engine_add_sync (priv->engine, priv->sync_keys);
562
563        priv->sync_status = bonobo_ui_sync_status_new (
564                priv->engine, priv->status);
565        bonobo_ui_engine_add_sync (priv->engine, priv->sync_status);
566
567        return priv;
568}
569
570/*
571 *   To kill bug reports of hiding not working
572 * we want to stop show_all showing hidden menus etc.
573 */
574static void
575bonobo_window_show_all (GtkWidget *widget)
576{
577        BonoboWindow *win = BONOBO_WINDOW (widget);
578
579        if (win->priv->client_area)
580                gtk_widget_show_all (win->priv->client_area);
581
582        gtk_widget_show (widget);
583}
584
585static gboolean
586bonobo_window_key_press_event (GtkWidget *widget,
587                               GdkEventKey *event)
588{
589        gboolean handled;
590        BonoboUISync *sync;
591
592        handled = GTK_WIDGET_CLASS (bonobo_window_parent_class)->key_press_event (widget, event);
593        if (handled)
594                return TRUE;
595
596        sync = BONOBO_WINDOW (widget)->priv->sync_keys;
597        if (sync)
598                return bonobo_ui_sync_keys_binding_handle
599                        (widget, event, BONOBO_UI_SYNC_KEYS (sync));
600
601        return FALSE;
602}
603
604static void
605bonobo_window_class_init (BonoboWindowClass *klass)
606{
607        GtkObjectClass *object_class = (GtkObjectClass *) klass;
608        GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
609
610        bonobo_window_parent_class =
611                gtk_type_class (gtk_window_get_type ());
612
613        object_class->finalize = bonobo_window_finalize;
614
615        widget_class->show_all = bonobo_window_show_all;
616        widget_class->key_press_event = bonobo_window_key_press_event;
617}
618
619static void
620bonobo_window_init (BonoboWindow *win)
621{
622        win->priv = construct_priv (win);
623        gnome_window_icon_set_from_default (GTK_WINDOW (win));
624}
625
626/**
627 * bonobo_window_set_ui_container:
628 * @win:
629 * @container:
630 *
631 * Deprecated
632 **/
633void
634bonobo_window_set_ui_container (BonoboWindow *win,
635                                BonoboObject *container)
636{
637        g_return_if_fail (BONOBO_IS_WINDOW (win));
638
639        g_warning ("bonobo_window_set_ui_container is deprecated");
640
641        bonobo_ui_engine_set_ui_container (
642                win->priv->engine, container);
643}
644
645/**
646 * bonobo_window_set_name:
647 * @win: the bonobo window
648 * @win_name: the window name
649 *
650 * Set the name of the window - used for configuration
651 * serialization.
652 **/
653void
654bonobo_window_set_name (BonoboWindow  *win,
655                        const char *win_name)
656{
657        BonoboWindowPrivate *priv;
658
659        g_return_if_fail (BONOBO_IS_WINDOW (win));
660
661        priv = win->priv;
662
663        g_free (priv->name);
664        g_free (priv->prefix);
665
666        if (win_name) {
667                priv->name = g_strdup (win_name);
668                priv->prefix = g_strconcat ("/", win_name, "/", NULL);
669        } else {
670                priv->name = NULL;
671                priv->prefix = g_strdup ("/");
672        }
673}
674
675/**
676 * bonobo_window_get_name:
677 * @win: the bonobo window
678 *
679 * Gets the name of a window.
680 *
681 * Return value: the name of the window
682 **/
683char *
684bonobo_window_get_name (BonoboWindow *win)
685{
686        g_return_val_if_fail (BONOBO_IS_WINDOW (win), NULL);
687        g_return_val_if_fail (win->priv != NULL, NULL);
688
689        if (win->priv->name)
690                return g_strdup (win->priv->name);
691        else
692                return NULL;
693}
694
695/**
696 * bonobo_window_get_ui_engine:
697 * @win: the bonobo window
698 *
699 * Gets the associated UIEngine.
700 *
701 * Return value: the #BonoboUIEngine
702 **/
703BonoboUIEngine *
704bonobo_window_get_ui_engine (BonoboWindow *win)
705{
706        g_return_val_if_fail (BONOBO_IS_WINDOW (win), NULL);
707        g_return_val_if_fail (win->priv != NULL, NULL);
708
709        return win->priv->engine;
710}
711
712/**
713 * bonobo_window_construct:
714 * @win: the window to construct
715 * @win_name: the window name
716 * @title: the window's title for the title bar
717 *
718 * Construct a new BonoboWindow
719 *
720 * Return value: a constructed window
721 **/
722GtkWidget *
723bonobo_window_construct (BonoboWindow *win,
724                         const char   *win_name,
725                         const char   *title)
726{
727        g_return_val_if_fail (BONOBO_IS_WINDOW (win), NULL);
728
729        bonobo_window_set_name (win, win_name);
730
731        if (title)
732                gtk_window_set_title (GTK_WINDOW (win), title);
733
734        return GTK_WIDGET (win);
735}
736
737/**
738 * bonobo_window_new:
739 * @win_name: the window name
740 * @title: the window's title for the title bar
741 *
742 * Return value: a new BonoboWindow
743 **/
744GtkWidget *
745bonobo_window_new (const char   *win_name,
746                   const char   *title)
747{
748        BonoboWindow *win;
749
750        win = gtk_type_new (BONOBO_TYPE_WINDOW);
751
752        return bonobo_window_construct (win, win_name, title);
753}
754
755/**
756 * bonobo_window_get_type:
757 *
758 * Returns: The GtkType for the BonoboWindow class.
759 */
760GtkType
761bonobo_window_get_type (void)
762{
763        static GtkType type = 0;
764
765        if (!type) {
766                GtkTypeInfo info = {
767                        "BonoboWindow",
768                        sizeof (BonoboWindow),
769                        sizeof (BonoboWindowClass),
770                        (GtkClassInitFunc) bonobo_window_class_init,
771                        (GtkObjectInitFunc) bonobo_window_init,
772                        NULL, /* reserved 1 */
773                        NULL, /* reserved 2 */
774                        (GtkClassInitFunc) NULL
775                };
776
777                type = gtk_type_unique (gtk_window_get_type (), &info);
778        }
779
780        return type;
781}
Note: See TracBrowser for help on using the repository browser.