2 * PDF viewer Bonobo container.
5 * Michael Meeks <michael@imaginator.com>
12 #define GString G_String
16 #include <liboaf/liboaf.h>
18 #include <libgnorba/gnorba.h>
21 #include <gdk/gdkprivate.h>
27 #include "bonobo-application-x-pdf.h"
32 const struct poptOption gpdf_popt_options [] = {
33 { "debug", '\0', POPT_ARG_INT, &gpdf_debug, 0,
34 N_("Enables some debugging functions"), N_("LEVEL") },
35 { NULL, '\0', 0, NULL, 0 }
38 typedef struct _Component Component;
39 typedef struct _Container Container;
40 /* NB. there is a 1 to 1 Container -> Component mapping, this
41 is due to how much MDI sucks; unutterably */
43 BonoboContainer *container;
48 GtkWidget *view_widget;
55 BonoboClientSite *client_site;
56 BonoboViewFrame *view_frame;
57 BonoboObjectClient *server;
60 GList *containers = NULL;
65 static Container *container_new (const char *fname);
66 static void container_destroy (Container *cont);
67 static void container_open_cmd (GtkWidget *widget, Container *container);
68 static void container_close_cmd (GtkWidget *widget, Container *container);
69 static void container_exit_cmd (void);
70 static void container_about_cmd (GtkWidget *widget, Container *container);
71 static Component *container_activate_component (Container *container, char *component_goad_id);
77 static GnomeUIInfo container_file_menu [] = {
78 GNOMEUIINFO_MENU_OPEN_ITEM (container_open_cmd, NULL),
79 GNOMEUIINFO_SEPARATOR,
80 GNOMEUIINFO_MENU_CLOSE_ITEM(container_close_cmd, NULL),
81 GNOMEUIINFO_SEPARATOR,
82 GNOMEUIINFO_MENU_EXIT_ITEM (container_exit_cmd, NULL),
86 static GnomeUIInfo container_help_menu [] = {
87 GNOMEUIINFO_MENU_ABOUT_ITEM(container_about_cmd, NULL),
91 static GnomeUIInfo container_main_menu [] = {
92 GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
93 GNOMEUIINFO_MENU_HELP_TREE (container_help_menu),
97 static GnomeUIInfo container_toolbar [] = {
98 GNOMEUIINFO_ITEM_STOCK (
99 N_("Open"), N_("Opens an existing workbook"),
100 container_open_cmd, GNOME_STOCK_PIXMAP_OPEN),
102 GNOMEUIINFO_SEPARATOR,
108 open_pdf (Container *container, const char *name)
110 BonoboObjectClient *object;
111 BonoboStream *stream;
112 Bonobo_PersistStream persist;
114 CORBA_Environment ev;
116 g_return_val_if_fail (container != NULL, FALSE);
117 g_return_val_if_fail (container->view_widget == NULL, FALSE);
120 comp = container_activate_component (container, "OAFIID:gpdf_component:892f2727-e2ec-423c-91ad-6f7b75fec6c8");
123 comp = container_activate_component (container, "bonobo-object:application-x-pdf");
125 if (!comp || !(object = comp->server)) {
126 gnome_error_dialog (_("Could not launch bonobo object."));
130 CORBA_exception_init (&ev);
131 persist = Bonobo_Unknown_query_interface (
132 bonobo_object_corba_objref (BONOBO_OBJECT (object)),
133 "IDL:Bonobo/PersistStream:1.0", &ev);
135 if (ev._major != CORBA_NO_EXCEPTION ||
136 persist == CORBA_OBJECT_NIL) {
137 gnome_error_dialog ("Panic: component doesn't implement PersistStream.");
141 stream = bonobo_stream_fs_open (name, Bonobo_Storage_READ);
143 if (stream == NULL) {
144 char *err = g_strconcat (_("Could not open "), name, NULL);
145 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
150 Bonobo_PersistStream_load (persist,
151 (Bonobo_Stream) bonobo_object_corba_objref (BONOBO_OBJECT (stream)),
155 Bonobo_Unknown_unref (persist, &ev);
156 CORBA_Object_release (persist, &ev);
157 CORBA_exception_free (&ev);
159 /* bonobo_view_frame_view_do_verb (comp->view_frame, "ZoomFit"); */
164 set_ok (GtkWidget *widget, gboolean *dialog_result)
166 *dialog_result = TRUE;
171 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
178 container_open_cmd (GtkWidget *widget, Container *container)
180 GtkFileSelection *fsel;
181 gboolean accepted = FALSE;
183 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
184 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
186 gtk_window_set_transient_for (GTK_WINDOW (fsel),
187 GTK_WINDOW (container->app));
189 /* Connect the signals for Ok and Cancel */
190 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
191 GTK_SIGNAL_FUNC (set_ok), &accepted);
192 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
193 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
194 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
197 * Make sure that we quit the main loop if the window is destroyed
199 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
200 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
203 gtk_widget_show (GTK_WIDGET (fsel));
204 gtk_grab_add (GTK_WIDGET (fsel));
208 char *name = gtk_file_selection_get_filename (fsel);
210 if (name [strlen (name)-1] != '/') {
211 char *fname = g_strdup (name);
212 if (container->view_widget) /* any sort of MDI sucks :-] */
213 container = container_new (fname);
215 if (!open_pdf (container, fname))
216 container_destroy (container);
221 dialog = gnome_message_box_new ("Can't open a directory",
222 GNOME_MESSAGE_BOX_ERROR,
223 GNOME_STOCK_BUTTON_OK, NULL);
224 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
225 GTK_WINDOW (container->app));
226 gnome_dialog_run (GNOME_DIALOG (dialog));
230 gtk_widget_destroy (GTK_WIDGET (fsel));
234 component_destroy (Component *component)
236 CORBA_Environment ev;
237 Container *container;
238 g_return_if_fail (component != NULL);
240 CORBA_exception_init (&ev);
242 /* Kill merged menus et al. */
243 bonobo_view_frame_view_deactivate (component->view_frame);
245 container = component->container;
246 gtk_widget_destroy (container->view_widget);
247 container->view_widget = NULL;
249 if (component->server)
250 Bonobo_Unknown_unref (
251 bonobo_object_corba_objref (BONOBO_OBJECT (component->server)), &ev);
252 component->server = NULL;
254 CORBA_exception_free (&ev);
260 container_destroy (Container *cont)
262 g_return_if_fail (g_list_find (containers, cont) != NULL);
264 containers = g_list_remove (containers, cont);
266 gtk_widget_destroy (cont->app);
270 component_destroy (cont->component);
271 cont->component = NULL;
280 container_close (Container *cont)
282 g_return_if_fail (g_list_find (containers, cont) != NULL);
284 if (cont->component) {
285 component_destroy (cont->component);
286 cont->component = NULL;
288 container_destroy (cont);
293 container_close_cmd (GtkWidget *widget, Container *cont)
295 container_close (cont);
299 container_destroy_cb (GtkWidget *widget, GdkEvent *event, Container *cont)
301 container_destroy (cont);
306 container_exit_cmd (void)
309 container_destroy ((Container *)containers->data);
313 container_about_cmd (GtkWidget *widget, Container *container)
317 const gchar *authors[] = {
318 N_("Derek B. Noonburg, main author"),
319 N_("Michael Meeks, GNOME port maintainer."),
320 N_("Miguel de Icaza."),
328 for (i = 0; authors[i] != NULL; i++)
329 authors [i] = _(authors [i]);
332 about = gnome_about_new (_("GPDF"), xpdfVersion,
333 _("(C) 1996-1999 Derek B. Noonburg."),
334 authors, NULL, NULL);
336 gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
337 gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
338 gtk_widget_show (about);
343 container_set_view (Container *container, Component *component)
345 BonoboViewFrame *view_frame;
346 GtkWidget *view_widget;
349 * Create the remote view and the local ViewFrame.
351 view_frame = bonobo_client_site_new_view (component->client_site,
352 bonobo_object_corba_objref (BONOBO_OBJECT (
354 component->view_frame = view_frame;
357 * Embed the view frame into the application.
359 view_widget = bonobo_view_frame_get_wrapper (view_frame);
360 bonobo_wrapper_set_visibility (BONOBO_WRAPPER (view_widget), FALSE);
361 container->view_widget = view_widget;
362 container->component = component;
364 gtk_container_add (GTK_CONTAINER (container->slot), view_widget);
367 * Activate it ( get it to merge menus etc. )
369 bonobo_view_frame_view_activate (view_frame);
370 bonobo_view_frame_set_covered (view_frame, FALSE);
372 gtk_widget_show_all (GTK_WIDGET (container->slot));
375 static BonoboObjectClient *
376 container_launch_component (BonoboClientSite *client_site,
377 BonoboContainer *container,
378 char *component_goad_id)
380 BonoboObjectClient *object_server;
383 * Launch the component.
385 object_server = bonobo_object_activate (component_goad_id, 0);
387 if (object_server == NULL)
391 * Bind it to the local ClientSite. Every embedded component
392 * has a local BonoboClientSite object which serves as a
393 * container-side point of contact for the embeddable. The
394 * container talks to the embeddable through its ClientSite
396 if (!bonobo_client_site_bind_embeddable (client_site, object_server)) {
397 bonobo_object_unref (BONOBO_OBJECT (object_server));
402 * The BonoboContainer object maintains a list of the
403 * ClientSites which it manages. Here we add the new
404 * ClientSite to that list.
406 bonobo_container_add (container, BONOBO_OBJECT (client_site));
408 return object_server;
413 container_activate_component (Container *container, char *component_goad_id)
415 Component *component;
416 BonoboClientSite *client_site;
417 BonoboObjectClient *server;
420 * The ClientSite is the container-side point of contact for
421 * the Embeddable. So there is a one-to-one correspondence
422 * between BonoboClientSites and BonoboEmbeddables. */
423 client_site = bonobo_client_site_new (container->container);
426 * A BonoboObjectClient is a simple wrapper for a remote
427 * BonoboObject (a server supporting Bonobo::Unknown).
429 server = container_launch_component (client_site, container->container,
431 if (server == NULL) {
434 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
436 gnome_warning_dialog (error_msg);
443 * Create the internal data structure which we will use to
444 * keep track of this component.
446 component = g_new0 (Component, 1);
447 component->container = container;
448 component->client_site = client_site;
449 component->server = server;
451 container_set_view (container, component);
457 filenames_dropped (GtkWidget * widget,
458 GdkDragContext *context,
461 GtkSelectionData *selection_data,
464 Container *container)
466 GList *names, *tmp_list;
468 names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
472 const char *fname = (const char *)tmp_list->data;
475 if (container->view_widget)
476 container = container_new (fname);
478 open_pdf (container, fname);
481 tmp_list = g_list_next (tmp_list);
488 container_create_menus (Container *container)
490 BonoboUIHandlerMenuItem *menu_list;
492 bonobo_ui_handler_create_menubar (container->uih);
495 * Create the basic menus out of UIInfo structures.
497 menu_list = bonobo_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
498 bonobo_ui_handler_menu_add_list (container->uih, "/", menu_list);
499 bonobo_ui_handler_menu_free_list (menu_list);
503 container_create_toolbar (Container *container)
505 BonoboUIHandlerToolbarItem *toolbar;
507 bonobo_ui_handler_create_toolbar (container->uih, "pdf");
508 toolbar = bonobo_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
509 bonobo_ui_handler_toolbar_add_list (container->uih, "/pdf/", toolbar);
510 bonobo_ui_handler_toolbar_free_list (toolbar);
514 container_new (const char *fname)
516 Container *container;
517 static GtkTargetEntry drag_types[] =
519 { "text/uri-list", 0, 0 },
521 static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
523 container = g_new0 (Container, 1);
525 container->app = gnome_app_new ("pdf-viewer",
528 gtk_drag_dest_set (container->app,
529 GTK_DEST_DEFAULT_ALL,
530 drag_types, n_drag_types,
533 gtk_signal_connect (GTK_OBJECT(container->app),
534 "drag_data_received",
535 GTK_SIGNAL_FUNC(filenames_dropped),
536 (gpointer)container);
538 gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
539 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
541 container->container = bonobo_container_new ();
542 container->view_widget = NULL;
543 container->slot = gtk_event_box_new ();
544 gtk_widget_show (container->slot);
546 gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->slot));
548 gtk_object_set_data (GTK_OBJECT (container->app), "container_data", container);
549 gtk_signal_connect (GTK_OBJECT (container->app), "delete_event",
550 GTK_SIGNAL_FUNC (container_destroy_cb), container);
553 * Create the BonoboUIHandler object which will be used to
554 * create the container's menus and toolbars. The UIHandler
555 * also creates a CORBA server which embedded components use
556 * to do menu/toolbar merging.
558 container->uih = bonobo_ui_handler_new ();
559 bonobo_ui_handler_set_app (container->uih, GNOME_APP (container->app));
561 container_create_menus (container);
562 container_create_toolbar (container);
564 gtk_widget_show_all (container->app);
566 containers = g_list_append (containers, container);
569 if (!open_pdf (container, fname)) {
570 container_destroy (container);
574 gtk_widget_show_all (container->app);
580 main (int argc, char **argv)
582 CORBA_Environment ev;
584 const char **view_files = NULL;
588 CORBA_exception_init (&ev);
592 gnomelib_register_popt_table (oaf_popt_options, "OAF");
593 gnome_init_with_popt_table("PDFViewer", "0.0.1",
595 gpdf_popt_options, 0, &ctx);
596 orb = oaf_init (argc, argv);
598 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
600 gpdf_popt_options, 0, &ctx,
601 GNORBA_INIT_SERVER_FUNC, &ev);
603 orb = gnome_CORBA_ORB ();
606 CORBA_exception_free (&ev);
608 if (bonobo_init (orb, NULL, NULL) == FALSE)
609 g_error (_("Could not initialize Bonobo!\n"));
612 view_files = poptGetArgs (ctx);
618 for (i = 0; view_files[i]; i++)
619 if (container_new (view_files[i])) {
621 while (gtk_events_pending ())
622 gtk_main_iteration ();
625 if ((i == 0) || !loaded)
626 container_new (NULL);
628 poptFreeContext (ctx);