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_componet: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)), &ev);
154 Bonobo_Unknown_unref (persist, &ev);
155 CORBA_Object_release (persist, &ev);
156 CORBA_exception_free (&ev);
158 /* bonobo_view_frame_view_do_verb (comp->view_frame, "ZoomFit"); */
163 set_ok (GtkWidget *widget, gboolean *dialog_result)
165 *dialog_result = TRUE;
170 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
177 container_open_cmd (GtkWidget *widget, Container *container)
179 GtkFileSelection *fsel;
180 gboolean accepted = FALSE;
182 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
183 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
185 gtk_window_set_transient_for (GTK_WINDOW (fsel),
186 GTK_WINDOW (container->app));
188 /* Connect the signals for Ok and Cancel */
189 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
190 GTK_SIGNAL_FUNC (set_ok), &accepted);
191 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
192 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
193 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
196 * Make sure that we quit the main loop if the window is destroyed
198 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
199 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
202 gtk_widget_show (GTK_WIDGET (fsel));
203 gtk_grab_add (GTK_WIDGET (fsel));
207 char *name = gtk_file_selection_get_filename (fsel);
209 if (name [strlen (name)-1] != '/') {
210 char *fname = g_strdup (name);
211 if (container->view_widget) /* any sort of MDI sucks :-] */
212 container = container_new (fname);
214 if (!open_pdf (container, fname))
215 container_destroy (container);
220 dialog = gnome_message_box_new ("Can't open a directory",
221 GNOME_MESSAGE_BOX_ERROR,
222 GNOME_STOCK_BUTTON_OK, NULL);
223 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
224 GTK_WINDOW (container->app));
225 gnome_dialog_run (GNOME_DIALOG (dialog));
229 gtk_widget_destroy (GTK_WIDGET (fsel));
233 component_destroy (Component *component)
235 CORBA_Environment ev;
236 Container *container;
237 g_return_if_fail (component != NULL);
239 CORBA_exception_init (&ev);
241 /* Kill merged menus et al. */
242 bonobo_view_frame_view_deactivate (component->view_frame);
244 container = component->container;
245 gtk_widget_destroy (container->view_widget);
246 container->view_widget = NULL;
248 if (component->server)
249 Bonobo_Unknown_unref (
250 bonobo_object_corba_objref (BONOBO_OBJECT (component->server)), &ev);
251 component->server = NULL;
253 CORBA_exception_free (&ev);
259 container_destroy (Container *cont)
261 g_return_if_fail (g_list_find (containers, cont) != NULL);
263 containers = g_list_remove (containers, cont);
265 gtk_widget_destroy (cont->app);
269 component_destroy (cont->component);
270 cont->component = NULL;
279 container_close (Container *cont)
281 g_return_if_fail (g_list_find (containers, cont) != NULL);
283 if (cont->component) {
284 component_destroy (cont->component);
285 cont->component = NULL;
287 container_destroy (cont);
292 container_close_cmd (GtkWidget *widget, Container *cont)
294 container_close (cont);
298 container_destroy_cb (GtkWidget *widget, GdkEvent *event, Container *cont)
300 container_destroy (cont);
305 container_exit_cmd (void)
308 container_destroy ((Container *)containers->data);
312 container_about_cmd (GtkWidget *widget, Container *container)
316 const gchar *authors[] = {
317 N_("Derek B. Noonburg, main author"),
318 N_("Michael Meeks, GNOME port maintainer."),
319 N_("Miguel de Icaza."),
327 for (i = 0; authors[i] != NULL; i++)
328 authors [i] = _(authors [i]);
331 about = gnome_about_new (_("GPDF"), xpdfVersion,
332 _("(C) 1996-1999 Derek B. Noonburg."),
333 authors, NULL, NULL);
335 gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
336 gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
337 gtk_widget_show (about);
342 container_set_view (Container *container, Component *component)
344 BonoboViewFrame *view_frame;
345 GtkWidget *view_widget;
348 * Create the remote view and the local ViewFrame.
350 view_frame = bonobo_client_site_new_view (component->client_site,
351 bonobo_object_corba_objref (BONOBO_OBJECT (
353 component->view_frame = view_frame;
356 * Embed the view frame into the application.
358 view_widget = bonobo_view_frame_get_wrapper (view_frame);
359 bonobo_wrapper_set_visibility (BONOBO_WRAPPER (view_widget), FALSE);
360 container->view_widget = view_widget;
361 container->component = component;
363 gtk_container_add (GTK_CONTAINER (container->slot), view_widget);
366 * Activate it ( get it to merge menus etc. )
368 bonobo_view_frame_view_activate (view_frame);
369 bonobo_view_frame_set_covered (view_frame, FALSE);
371 gtk_widget_show_all (GTK_WIDGET (container->slot));
374 static BonoboObjectClient *
375 container_launch_component (BonoboClientSite *client_site,
376 BonoboContainer *container,
377 char *component_goad_id)
379 BonoboObjectClient *object_server;
382 * Launch the component.
384 object_server = bonobo_object_activate (component_goad_id, 0);
386 if (object_server == NULL)
390 * Bind it to the local ClientSite. Every embedded component
391 * has a local BonoboClientSite object which serves as a
392 * container-side point of contact for the embeddable. The
393 * container talks to the embeddable through its ClientSite
395 if (!bonobo_client_site_bind_embeddable (client_site, object_server)) {
396 bonobo_object_unref (BONOBO_OBJECT (object_server));
401 * The BonoboContainer object maintains a list of the
402 * ClientSites which it manages. Here we add the new
403 * ClientSite to that list.
405 bonobo_container_add (container, BONOBO_OBJECT (client_site));
407 return object_server;
412 container_activate_component (Container *container, char *component_goad_id)
414 Component *component;
415 BonoboClientSite *client_site;
416 BonoboObjectClient *server;
419 * The ClientSite is the container-side point of contact for
420 * the Embeddable. So there is a one-to-one correspondence
421 * between BonoboClientSites and BonoboEmbeddables. */
422 client_site = bonobo_client_site_new (container->container);
425 * A BonoboObjectClient is a simple wrapper for a remote
426 * BonoboObject (a server supporting Bonobo::Unknown).
428 server = container_launch_component (client_site, container->container,
430 if (server == NULL) {
433 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
435 gnome_warning_dialog (error_msg);
442 * Create the internal data structure which we will use to
443 * keep track of this component.
445 component = g_new0 (Component, 1);
446 component->container = container;
447 component->client_site = client_site;
448 component->server = server;
450 container_set_view (container, component);
456 filenames_dropped (GtkWidget * widget,
457 GdkDragContext *context,
460 GtkSelectionData *selection_data,
463 Container *container)
465 GList *names, *tmp_list;
467 names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
471 const char *fname = (const char *)tmp_list->data;
474 if (container->view_widget)
475 container = container_new (fname);
477 open_pdf (container, fname);
480 tmp_list = g_list_next (tmp_list);
487 container_create_menus (Container *container)
489 BonoboUIHandlerMenuItem *menu_list;
491 bonobo_ui_handler_create_menubar (container->uih);
494 * Create the basic menus out of UIInfo structures.
496 menu_list = bonobo_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
497 bonobo_ui_handler_menu_add_list (container->uih, "/", menu_list);
498 bonobo_ui_handler_menu_free_list (menu_list);
502 container_create_toolbar (Container *container)
504 BonoboUIHandlerToolbarItem *toolbar;
506 bonobo_ui_handler_create_toolbar (container->uih, "pdf");
507 toolbar = bonobo_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
508 bonobo_ui_handler_toolbar_add_list (container->uih, "/pdf/", toolbar);
509 bonobo_ui_handler_toolbar_free_list (toolbar);
513 container_new (const char *fname)
515 Container *container;
516 static GtkTargetEntry drag_types[] =
518 { "text/uri-list", 0, 0 },
520 static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
522 container = g_new0 (Container, 1);
524 container->app = gnome_app_new ("pdf-viewer",
527 gtk_drag_dest_set (container->app,
528 GTK_DEST_DEFAULT_ALL,
529 drag_types, n_drag_types,
532 gtk_signal_connect (GTK_OBJECT(container->app),
533 "drag_data_received",
534 GTK_SIGNAL_FUNC(filenames_dropped),
535 (gpointer)container);
537 gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
538 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
540 container->container = bonobo_container_new ();
541 container->view_widget = NULL;
542 container->slot = gtk_event_box_new ();
543 gtk_widget_show (container->slot);
545 gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->slot));
547 gtk_object_set_data (GTK_OBJECT (container->app), "container_data", container);
548 gtk_signal_connect (GTK_OBJECT (container->app), "delete_event",
549 GTK_SIGNAL_FUNC (container_destroy_cb), container);
552 * Create the BonoboUIHandler object which will be used to
553 * create the container's menus and toolbars. The UIHandler
554 * also creates a CORBA server which embedded components use
555 * to do menu/toolbar merging.
557 container->uih = bonobo_ui_handler_new ();
558 bonobo_ui_handler_set_app (container->uih, GNOME_APP (container->app));
560 container_create_menus (container);
561 container_create_toolbar (container);
563 gtk_widget_show_all (container->app);
565 containers = g_list_append (containers, container);
568 if (!open_pdf (container, fname)) {
569 container_destroy (container);
573 gtk_widget_show_all (container->app);
579 main (int argc, char **argv)
581 CORBA_Environment ev;
583 const char **view_files = NULL;
587 CORBA_exception_init (&ev);
591 gnomelib_register_popt_table (oaf_popt_options, "OAF");
592 gnome_init_with_popt_table("PDFViewer", "0.0.1",
594 gpdf_popt_options, 0, &ctx);
595 orb = oaf_init (argc, argv);
597 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
599 gpdf_popt_options, 0, &ctx,
600 GNORBA_INIT_SERVER_FUNC, &ev);
602 orb = gnome_CORBA_ORB ();
605 CORBA_exception_free (&ev);
607 if (bonobo_init (orb, NULL, NULL) == FALSE)
608 g_error (_("Could not initialize Bonobo!\n"));
611 view_files = poptGetArgs (ctx);
617 for (i = 0; view_files[i]; i++)
618 if (container_new (view_files[i])) {
620 while (gtk_events_pending ())
621 gtk_main_iteration ();
624 if ((i == 0) || !loaded)
625 container_new (NULL);
627 poptFreeContext (ctx);