2 * PDF viewer Bonobo container.
5 * Michael Meeks <michael@imaginator.com>
14 #define GString G_String
17 #include <liboaf/liboaf.h>
19 #include <gdk/gdkprivate.h>
24 #include "bonobo-application-x-pdf.h"
29 const struct poptOption gpdf_popt_options [] = {
30 { "debug", '\0', POPT_ARG_INT, &gpdf_debug, 0,
31 N_("Enables some debugging functions"), N_("LEVEL") },
32 { NULL, '\0', 0, NULL, 0 }
35 typedef struct _Component Component;
36 typedef struct _Container Container;
37 /* NB. there is a 1 to 1 Container -> Component mapping, this
38 is due to how much MDI sucks; unutterably */
40 BonoboItemContainer *container;
41 BonoboUIComponent *ui_component;
45 GtkWidget *view_widget;
52 BonoboClientSite *client_site;
53 BonoboViewFrame *view_frame;
54 BonoboObjectClient *server;
57 GList *containers = NULL;
62 static Container *container_new (const char *fname);
63 static void container_destroy (Container *cont);
64 static void container_open_cmd (GtkWidget *widget, Container *container);
65 static void container_close_cmd (GtkWidget *widget, Container *container);
66 static void container_exit_cmd (void);
67 static void container_about_cmd (GtkWidget *widget, Container *container);
68 static void container_dump_cmd (GtkWidget *widget, Container *container);
69 static Component *container_activate_component (Container *container, char *component_goad_id);
75 BonoboUIVerb verbs [] = {
76 BONOBO_UI_UNSAFE_VERB ("FileOpen", container_open_cmd),
77 BONOBO_UI_UNSAFE_VERB ("FileClose", container_close_cmd),
78 BONOBO_UI_UNSAFE_VERB ("FileExit", container_exit_cmd),
80 BONOBO_UI_UNSAFE_VERB ("HelpAbout", container_about_cmd),
82 BONOBO_UI_UNSAFE_VERB ("DebugDumpXml", container_dump_cmd),
90 open_pdf (Container *container, const char *name)
92 BonoboObjectClient *object;
94 Bonobo_PersistStream persist;
98 g_return_val_if_fail (container != NULL, FALSE);
99 g_return_val_if_fail (container->view_widget == NULL, FALSE);
101 comp = container_activate_component (
102 container, "OAFIID:GNOME_XPDF_Embeddable");
104 if (!comp || !(object = comp->server)) {
105 gnome_error_dialog (_("Could not launch bonobo object."));
109 CORBA_exception_init (&ev);
110 persist = Bonobo_Unknown_queryInterface (
111 bonobo_object_corba_objref (BONOBO_OBJECT (object)),
112 "IDL:Bonobo/PersistStream:1.0", &ev);
114 if (ev._major != CORBA_NO_EXCEPTION ||
115 persist == CORBA_OBJECT_NIL) {
116 gnome_error_dialog ("Panic: component doesn't implement PersistStream.");
120 stream = bonobo_stream_open (BONOBO_IO_DRIVER_FS, name, Bonobo_Storage_READ, 0);
122 if (stream == NULL) {
123 char *err = g_strdup_printf (_("Could not open %s"), name);
124 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
129 Bonobo_PersistStream_load (persist,
130 (Bonobo_Stream) bonobo_object_corba_objref (BONOBO_OBJECT (stream)),
134 Bonobo_Unknown_unref (persist, &ev);
135 CORBA_Object_release (persist, &ev);
136 CORBA_exception_free (&ev);
138 /* bonobo_view_frame_view_do_verb (comp->view_frame, "ZoomFit"); */
143 set_ok (GtkWidget *widget, gboolean *dialog_result)
145 *dialog_result = TRUE;
150 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
157 container_open_cmd (GtkWidget *widget, Container *container)
159 GtkFileSelection *fsel;
160 gboolean accepted = FALSE;
162 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
163 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
165 gtk_window_set_transient_for (GTK_WINDOW (fsel),
166 GTK_WINDOW (container->app));
168 /* Connect the signals for Ok and Cancel */
169 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
170 GTK_SIGNAL_FUNC (set_ok), &accepted);
171 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
172 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
173 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
176 * Make sure that we quit the main loop if the window is destroyed
178 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
179 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
182 gtk_widget_show (GTK_WIDGET (fsel));
183 gtk_grab_add (GTK_WIDGET (fsel));
187 char *name = gtk_file_selection_get_filename (fsel);
189 if (name [strlen (name)-1] != '/') {
190 char *fname = g_strdup (name);
191 if (container->view_widget) /* any sort of MDI sucks :-] */
192 container = container_new (fname);
194 if (!open_pdf (container, fname))
195 container_destroy (container);
200 dialog = gnome_message_box_new (_("Can't open a directory"),
201 GNOME_MESSAGE_BOX_ERROR,
202 GNOME_STOCK_BUTTON_OK, NULL);
203 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
204 GTK_WINDOW (container->app));
205 gnome_dialog_run (GNOME_DIALOG (dialog));
209 gtk_widget_destroy (GTK_WIDGET (fsel));
213 component_destroy (Component *component)
215 CORBA_Environment ev;
216 Container *container;
217 g_return_if_fail (component != NULL);
219 CORBA_exception_init (&ev);
221 /* Kill merged menus et al. */
222 bonobo_view_frame_view_deactivate (component->view_frame);
224 container = component->container;
225 gtk_widget_destroy (container->view_widget);
226 container->view_widget = NULL;
228 if (component->server)
229 Bonobo_Unknown_unref (
230 bonobo_object_corba_objref (BONOBO_OBJECT (component->server)), &ev);
231 component->server = NULL;
233 CORBA_exception_free (&ev);
239 container_destroy (Container *cont)
241 g_return_if_fail (g_list_find (containers, cont) != NULL);
243 containers = g_list_remove (containers, cont);
245 gtk_widget_destroy (cont->app);
249 component_destroy (cont->component);
250 cont->component = NULL;
259 container_close (Container *cont)
261 g_return_if_fail (g_list_find (containers, cont) != NULL);
263 if (cont->component) {
264 component_destroy (cont->component);
265 cont->component = NULL;
267 container_destroy (cont);
272 container_close_cmd (GtkWidget *widget, Container *cont)
274 container_close (cont);
278 container_destroy_cb (GtkWidget *widget, GdkEvent *event, Container *cont)
280 container_destroy (cont);
285 container_exit_cmd (void)
288 container_destroy ((Container *)containers->data);
292 container_dump_cmd (GtkWidget *widget, Container *container)
294 bonobo_window_dump (BONOBO_WINDOW(container->app), "on demand");
298 container_about_cmd (GtkWidget *widget, Container *container)
302 const gchar *authors[] = {
303 N_("Derek B. Noonburg, main author"),
304 N_("Michael Meeks, GNOME port maintainer."),
305 N_("Miguel de Icaza."),
313 for (i = 0; authors[i] != NULL; i++)
314 authors [i] = _(authors [i]);
317 about = gnome_about_new (_("GPDF"), xpdfVersion,
318 _("(C) 1996-1999 Derek B. Noonburg."),
319 authors, NULL, NULL);
321 gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
322 gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
323 gtk_widget_show (about);
328 container_set_view (Container *container, Component *component)
330 BonoboViewFrame *view_frame;
331 GtkWidget *view_widget;
334 * Create the remote view and the local ViewFrame.
336 view_frame = bonobo_client_site_new_view (
337 component->client_site,
338 bonobo_ui_component_get_container (container->ui_component));
340 component->view_frame = view_frame;
343 * Embed the view frame into the application.
345 view_widget = bonobo_view_frame_get_wrapper (view_frame);
346 bonobo_wrapper_set_visibility (BONOBO_WRAPPER (view_widget), FALSE);
347 container->view_widget = view_widget;
348 container->component = component;
350 gtk_container_add (GTK_CONTAINER (container->slot), view_widget);
353 * Activate it ( get it to merge menus etc. )
355 bonobo_view_frame_view_activate (view_frame);
356 bonobo_view_frame_set_covered (view_frame, FALSE);
358 gtk_widget_show_all (GTK_WIDGET (container->slot));
361 static BonoboObjectClient *
362 container_launch_component (BonoboClientSite *client_site,
363 BonoboItemContainer *container,
364 char *component_goad_id)
366 BonoboObjectClient *object_server;
369 * Launch the component.
371 object_server = bonobo_object_activate (component_goad_id, 0);
373 if (object_server == NULL)
377 * Bind it to the local ClientSite. Every embedded component
378 * has a local BonoboClientSite object which serves as a
379 * container-side point of contact for the embeddable. The
380 * container talks to the embeddable through its ClientSite
382 if (!bonobo_client_site_bind_embeddable (client_site, object_server)) {
383 bonobo_object_unref (BONOBO_OBJECT (object_server));
387 return object_server;
392 container_activate_component (Container *container, char *component_goad_id)
394 Component *component;
395 BonoboClientSite *client_site;
396 BonoboObjectClient *server;
399 * The ClientSite is the container-side point of contact for
400 * the Embeddable. So there is a one-to-one correspondence
401 * between BonoboClientSites and BonoboEmbeddables. */
402 client_site = bonobo_client_site_new (container->container);
405 * A BonoboObjectClient is a simple wrapper for a remote
406 * BonoboObject (a server supporting Bonobo::Unknown).
408 server = container_launch_component (client_site, container->container,
410 if (server == NULL) {
413 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
415 gnome_warning_dialog (error_msg);
422 * Create the internal data structure which we will use to
423 * keep track of this component.
425 component = g_new0 (Component, 1);
426 component->container = container;
427 component->client_site = client_site;
428 component->server = server;
430 container_set_view (container, component);
436 filenames_dropped (GtkWidget * widget,
437 GdkDragContext *context,
440 GtkSelectionData *selection_data,
443 Container *container)
445 GList *names, *tmp_list;
447 names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
451 const char *fname = (const char *)tmp_list->data;
454 if (container->view_widget)
455 container = container_new (fname);
457 open_pdf (container, fname);
460 tmp_list = g_list_next (tmp_list);
466 container_new (const char *fname)
468 Container *container;
469 static GtkTargetEntry drag_types[] =
471 { "text/uri-list", 0, 0 },
473 static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
474 BonoboUIContainer *ui_container;
476 container = g_new0 (Container, 1);
478 container->app = bonobo_window_new ("pdf-viewer",
479 _("GNOME PDF viewer"));
481 gtk_drag_dest_set (container->app,
482 GTK_DEST_DEFAULT_ALL,
483 drag_types, n_drag_types,
486 gtk_signal_connect (GTK_OBJECT(container->app),
487 "drag_data_received",
488 GTK_SIGNAL_FUNC(filenames_dropped),
489 (gpointer)container);
491 gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
492 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
494 container->container = bonobo_item_container_new ();
495 container->view_widget = NULL;
496 container->slot = gtk_event_box_new ();
497 gtk_widget_show (container->slot);
499 bonobo_window_set_contents (BONOBO_WINDOW(container->app),
500 GTK_WIDGET (container->slot));
501 gtk_widget_show_all (container->slot);
503 gtk_object_set_data (GTK_OBJECT (container->app), "container_data", container);
504 gtk_signal_connect (GTK_OBJECT (container->app), "delete_event",
505 GTK_SIGNAL_FUNC (container_destroy_cb), container);
507 ui_container = bonobo_ui_container_new ();
508 bonobo_ui_container_set_win (ui_container, BONOBO_WINDOW(container->app));
510 container->ui_component = bonobo_ui_component_new ("gpdf");
511 bonobo_ui_component_set_container (
512 container->ui_component,
513 bonobo_object_corba_objref (BONOBO_OBJECT (ui_container)));
515 bonobo_ui_component_add_verb_list_with_data (
516 container->ui_component, verbs, container);
518 bonobo_ui_util_set_ui (container->ui_component, DATADIR, "gpdf-ui.xml", "gpdf");
520 gtk_widget_show (container->app);
522 containers = g_list_append (containers, container);
525 if (!open_pdf (container, fname)) {
526 container_destroy (container);
530 gtk_widget_show (container->app);
536 main (int argc, char **argv)
538 CORBA_Environment ev;
540 const char **view_files = NULL;
544 bindtextdomain (PACKAGE, GNOMELOCALEDIR);
545 textdomain (PACKAGE);
546 setlocale (LC_ALL, "");
548 CORBA_exception_init (&ev);
550 gnomelib_register_popt_table (oaf_popt_options, "OAF");
551 gnome_init_with_popt_table("PDFViewer", "0.0.1",
553 gpdf_popt_options, 0, &ctx);
554 orb = oaf_init (argc, argv);
556 CORBA_exception_free (&ev);
558 if (bonobo_init (orb, NULL, NULL) == FALSE)
559 g_error (_("Could not initialize Bonobo!\n"));
562 view_files = poptGetArgs (ctx);
568 for (i = 0; view_files[i]; i++)
569 if (container_new (view_files[i])) {
571 while (gtk_events_pending ())
572 gtk_main_iteration ();
575 if ((i == 0) || !loaded)
576 container_new (NULL);
578 poptFreeContext (ctx);