2 * PDF viewer Bonobo container.
5 * Michael Meeks <michael@imaginator.com>
13 #define GString G_String
17 #include <liboaf/liboaf.h>
19 #include <libgnorba/gnorba.h>
22 #include <gdk/gdkprivate.h>
28 #include "bonobo-application-x-pdf.h"
33 const struct poptOption gpdf_popt_options [] = {
34 { "debug", '\0', POPT_ARG_INT, &gpdf_debug, 0,
35 N_("Enables some debugging functions"), N_("LEVEL") },
36 { NULL, '\0', 0, NULL, 0 }
39 typedef struct _Component Component;
40 typedef struct _Container Container;
41 /* NB. there is a 1 to 1 Container -> Component mapping, this
42 is due to how much MDI sucks; unutterably */
44 BonoboContainer *container;
49 GtkWidget *view_widget;
56 BonoboClientSite *client_site;
57 BonoboViewFrame *view_frame;
58 BonoboObjectClient *server;
61 GList *containers = NULL;
66 static Container *container_new (const char *fname);
67 static void container_destroy (Container *cont);
68 static void container_open_cmd (GtkWidget *widget, Container *container);
69 static void container_close_cmd (GtkWidget *widget, Container *container);
70 static void container_exit_cmd (void);
71 static void container_about_cmd (GtkWidget *widget, Container *container);
72 static Component *container_activate_component (Container *container, char *component_goad_id);
78 static GnomeUIInfo container_file_menu [] = {
79 GNOMEUIINFO_MENU_OPEN_ITEM (container_open_cmd, NULL),
80 GNOMEUIINFO_SEPARATOR,
81 GNOMEUIINFO_MENU_CLOSE_ITEM(container_close_cmd, NULL),
82 GNOMEUIINFO_SEPARATOR,
83 GNOMEUIINFO_MENU_EXIT_ITEM (container_exit_cmd, NULL),
87 static GnomeUIInfo container_help_menu [] = {
88 GNOMEUIINFO_MENU_ABOUT_ITEM(container_about_cmd, NULL),
92 static GnomeUIInfo container_main_menu [] = {
93 GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
94 GNOMEUIINFO_MENU_HELP_TREE (container_help_menu),
98 static GnomeUIInfo container_toolbar [] = {
99 GNOMEUIINFO_ITEM_STOCK (
100 N_("Open"), N_("Opens an existing workbook"),
101 container_open_cmd, GNOME_STOCK_PIXMAP_OPEN),
103 GNOMEUIINFO_SEPARATOR,
109 open_pdf (Container *container, const char *name)
111 BonoboObjectClient *object;
112 BonoboStream *stream;
113 Bonobo_PersistStream persist;
115 CORBA_Environment ev;
117 g_return_val_if_fail (container != NULL, FALSE);
118 g_return_val_if_fail (container->view_widget == NULL, FALSE);
121 comp = container_activate_component (container, "OAFIID:gpdf_component:892f2727-e2ec-423c-91ad-6f7b75fec6c8");
124 comp = container_activate_component (container, "bonobo-object:application-x-pdf");
126 if (!comp || !(object = comp->server)) {
127 gnome_error_dialog (_("Could not launch bonobo object."));
131 CORBA_exception_init (&ev);
132 persist = Bonobo_Unknown_query_interface (
133 bonobo_object_corba_objref (BONOBO_OBJECT (object)),
134 "IDL:Bonobo/PersistStream:1.0", &ev);
136 if (ev._major != CORBA_NO_EXCEPTION ||
137 persist == CORBA_OBJECT_NIL) {
138 gnome_error_dialog ("Panic: component doesn't implement PersistStream.");
142 stream = bonobo_stream_fs_open (name, Bonobo_Storage_READ);
144 if (stream == NULL) {
145 char *err = g_strconcat (_("Could not open "), name, NULL);
146 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
151 Bonobo_PersistStream_load (persist,
152 (Bonobo_Stream) bonobo_object_corba_objref (BONOBO_OBJECT (stream)),
156 Bonobo_Unknown_unref (persist, &ev);
157 CORBA_Object_release (persist, &ev);
158 CORBA_exception_free (&ev);
160 /* bonobo_view_frame_view_do_verb (comp->view_frame, "ZoomFit"); */
165 set_ok (GtkWidget *widget, gboolean *dialog_result)
167 *dialog_result = TRUE;
172 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
179 container_open_cmd (GtkWidget *widget, Container *container)
181 GtkFileSelection *fsel;
182 gboolean accepted = FALSE;
184 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
185 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
187 gtk_window_set_transient_for (GTK_WINDOW (fsel),
188 GTK_WINDOW (container->app));
190 /* Connect the signals for Ok and Cancel */
191 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
192 GTK_SIGNAL_FUNC (set_ok), &accepted);
193 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
194 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
195 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
198 * Make sure that we quit the main loop if the window is destroyed
200 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
201 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
204 gtk_widget_show (GTK_WIDGET (fsel));
205 gtk_grab_add (GTK_WIDGET (fsel));
209 char *name = gtk_file_selection_get_filename (fsel);
211 if (name [strlen (name)-1] != '/') {
212 char *fname = g_strdup (name);
213 if (container->view_widget) /* any sort of MDI sucks :-] */
214 container = container_new (fname);
216 if (!open_pdf (container, fname))
217 container_destroy (container);
222 dialog = gnome_message_box_new ("Can't open a directory",
223 GNOME_MESSAGE_BOX_ERROR,
224 GNOME_STOCK_BUTTON_OK, NULL);
225 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
226 GTK_WINDOW (container->app));
227 gnome_dialog_run (GNOME_DIALOG (dialog));
231 gtk_widget_destroy (GTK_WIDGET (fsel));
235 component_destroy (Component *component)
237 CORBA_Environment ev;
238 Container *container;
239 g_return_if_fail (component != NULL);
241 CORBA_exception_init (&ev);
243 /* Kill merged menus et al. */
244 bonobo_view_frame_view_deactivate (component->view_frame);
246 container = component->container;
247 gtk_widget_destroy (container->view_widget);
248 container->view_widget = NULL;
250 if (component->server)
251 Bonobo_Unknown_unref (
252 bonobo_object_corba_objref (BONOBO_OBJECT (component->server)), &ev);
253 component->server = NULL;
255 CORBA_exception_free (&ev);
261 container_destroy (Container *cont)
263 g_return_if_fail (g_list_find (containers, cont) != NULL);
265 containers = g_list_remove (containers, cont);
267 gtk_widget_destroy (cont->app);
271 component_destroy (cont->component);
272 cont->component = NULL;
281 container_close (Container *cont)
283 g_return_if_fail (g_list_find (containers, cont) != NULL);
285 if (cont->component) {
286 component_destroy (cont->component);
287 cont->component = NULL;
289 container_destroy (cont);
294 container_close_cmd (GtkWidget *widget, Container *cont)
296 container_close (cont);
300 container_destroy_cb (GtkWidget *widget, GdkEvent *event, Container *cont)
302 container_destroy (cont);
307 container_exit_cmd (void)
310 container_destroy ((Container *)containers->data);
314 container_about_cmd (GtkWidget *widget, Container *container)
318 const gchar *authors[] = {
319 N_("Derek B. Noonburg, main author"),
320 N_("Michael Meeks, GNOME port maintainer."),
321 N_("Miguel de Icaza."),
329 for (i = 0; authors[i] != NULL; i++)
330 authors [i] = _(authors [i]);
333 about = gnome_about_new (_("GPDF"), xpdfVersion,
334 _("(C) 1996-1999 Derek B. Noonburg."),
335 authors, NULL, NULL);
337 gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
338 gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
339 gtk_widget_show (about);
344 container_set_view (Container *container, Component *component)
346 BonoboViewFrame *view_frame;
347 GtkWidget *view_widget;
350 * Create the remote view and the local ViewFrame.
352 view_frame = bonobo_client_site_new_view (component->client_site,
353 bonobo_object_corba_objref (BONOBO_OBJECT (
355 component->view_frame = view_frame;
358 * Embed the view frame into the application.
360 view_widget = bonobo_view_frame_get_wrapper (view_frame);
361 bonobo_wrapper_set_visibility (BONOBO_WRAPPER (view_widget), FALSE);
362 container->view_widget = view_widget;
363 container->component = component;
365 gtk_container_add (GTK_CONTAINER (container->slot), view_widget);
368 * Activate it ( get it to merge menus etc. )
370 bonobo_view_frame_view_activate (view_frame);
371 bonobo_view_frame_set_covered (view_frame, FALSE);
373 gtk_widget_show_all (GTK_WIDGET (container->slot));
376 static BonoboObjectClient *
377 container_launch_component (BonoboClientSite *client_site,
378 BonoboContainer *container,
379 char *component_goad_id)
381 BonoboObjectClient *object_server;
384 * Launch the component.
386 object_server = bonobo_object_activate (component_goad_id, 0);
388 if (object_server == NULL)
392 * Bind it to the local ClientSite. Every embedded component
393 * has a local BonoboClientSite object which serves as a
394 * container-side point of contact for the embeddable. The
395 * container talks to the embeddable through its ClientSite
397 if (!bonobo_client_site_bind_embeddable (client_site, object_server)) {
398 bonobo_object_unref (BONOBO_OBJECT (object_server));
403 * The BonoboContainer object maintains a list of the
404 * ClientSites which it manages. Here we add the new
405 * ClientSite to that list.
407 bonobo_container_add (container, BONOBO_OBJECT (client_site));
409 return object_server;
414 container_activate_component (Container *container, char *component_goad_id)
416 Component *component;
417 BonoboClientSite *client_site;
418 BonoboObjectClient *server;
421 * The ClientSite is the container-side point of contact for
422 * the Embeddable. So there is a one-to-one correspondence
423 * between BonoboClientSites and BonoboEmbeddables. */
424 client_site = bonobo_client_site_new (container->container);
427 * A BonoboObjectClient is a simple wrapper for a remote
428 * BonoboObject (a server supporting Bonobo::Unknown).
430 server = container_launch_component (client_site, container->container,
432 if (server == NULL) {
435 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
437 gnome_warning_dialog (error_msg);
444 * Create the internal data structure which we will use to
445 * keep track of this component.
447 component = g_new0 (Component, 1);
448 component->container = container;
449 component->client_site = client_site;
450 component->server = server;
452 container_set_view (container, component);
458 filenames_dropped (GtkWidget * widget,
459 GdkDragContext *context,
462 GtkSelectionData *selection_data,
465 Container *container)
467 GList *names, *tmp_list;
469 names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
473 const char *fname = (const char *)tmp_list->data;
476 if (container->view_widget)
477 container = container_new (fname);
479 open_pdf (container, fname);
482 tmp_list = g_list_next (tmp_list);
489 container_create_menus (Container *container)
491 BonoboUIHandlerMenuItem *menu_list;
493 bonobo_ui_handler_create_menubar (container->uih);
496 * Create the basic menus out of UIInfo structures.
498 menu_list = bonobo_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
499 bonobo_ui_handler_menu_add_list (container->uih, "/", menu_list);
500 bonobo_ui_handler_menu_free_list (menu_list);
504 container_create_toolbar (Container *container)
506 BonoboUIHandlerToolbarItem *toolbar;
508 bonobo_ui_handler_create_toolbar (container->uih, "pdf");
509 toolbar = bonobo_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
510 bonobo_ui_handler_toolbar_add_list (container->uih, "/pdf/", toolbar);
511 bonobo_ui_handler_toolbar_free_list (toolbar);
515 container_new (const char *fname)
517 Container *container;
518 static GtkTargetEntry drag_types[] =
520 { "text/uri-list", 0, 0 },
522 static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
524 container = g_new0 (Container, 1);
526 container->app = gnome_app_new ("pdf-viewer",
529 gtk_drag_dest_set (container->app,
530 GTK_DEST_DEFAULT_ALL,
531 drag_types, n_drag_types,
534 gtk_signal_connect (GTK_OBJECT(container->app),
535 "drag_data_received",
536 GTK_SIGNAL_FUNC(filenames_dropped),
537 (gpointer)container);
539 gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
540 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
542 container->container = bonobo_container_new ();
543 container->view_widget = NULL;
544 container->slot = gtk_event_box_new ();
545 gtk_widget_show (container->slot);
547 gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->slot));
549 gtk_object_set_data (GTK_OBJECT (container->app), "container_data", container);
550 gtk_signal_connect (GTK_OBJECT (container->app), "delete_event",
551 GTK_SIGNAL_FUNC (container_destroy_cb), container);
554 * Create the BonoboUIHandler object which will be used to
555 * create the container's menus and toolbars. The UIHandler
556 * also creates a CORBA server which embedded components use
557 * to do menu/toolbar merging.
559 container->uih = bonobo_ui_handler_new ();
560 bonobo_ui_handler_set_app (container->uih, GNOME_APP (container->app));
562 container_create_menus (container);
563 container_create_toolbar (container);
565 gtk_widget_show_all (container->app);
567 containers = g_list_append (containers, container);
570 if (!open_pdf (container, fname)) {
571 container_destroy (container);
575 gtk_widget_show_all (container->app);
581 main (int argc, char **argv)
583 CORBA_Environment ev;
585 const char **view_files = NULL;
589 CORBA_exception_init (&ev);
593 gnomelib_register_popt_table (oaf_popt_options, "OAF");
594 gnome_init_with_popt_table("PDFViewer", "0.0.1",
596 gpdf_popt_options, 0, &ctx);
597 orb = oaf_init (argc, argv);
599 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
601 gpdf_popt_options, 0, &ctx,
602 GNORBA_INIT_SERVER_FUNC, &ev);
604 orb = gnome_CORBA_ORB ();
607 CORBA_exception_free (&ev);
609 if (bonobo_init (orb, NULL, NULL) == FALSE)
610 g_error (_("Could not initialize Bonobo!\n"));
613 view_files = poptGetArgs (ctx);
619 for (i = 0; view_files[i]; i++)
620 if (container_new (view_files[i])) {
622 while (gtk_events_pending ())
623 gtk_main_iteration ();
626 if ((i == 0) || !loaded)
627 container_new (NULL);
629 poptFreeContext (ctx);