2 * PDF viewer Bonobo container.
5 * Michael Meeks <michael@imaginator.com>
12 #define GString G_String
15 #include <liboaf/liboaf.h>
17 #include <gdk/gdkprivate.h>
23 #include "bonobo-application-x-pdf.h"
28 const struct poptOption gpdf_popt_options [] = {
29 { "debug", '\0', POPT_ARG_INT, &gpdf_debug, 0,
30 N_("Enables some debugging functions"), N_("LEVEL") },
31 { NULL, '\0', 0, NULL, 0 }
34 typedef struct _Component Component;
35 typedef struct _Container Container;
36 /* NB. there is a 1 to 1 Container -> Component mapping, this
37 is due to how much MDI sucks; unutterably */
39 BonoboItemContainer *container;
40 BonoboUIComponent *ui_component;
44 GtkWidget *view_widget;
51 BonoboClientSite *client_site;
52 BonoboViewFrame *view_frame;
53 BonoboObjectClient *server;
56 GList *containers = NULL;
61 static Container *container_new (const char *fname);
62 static void container_destroy (Container *cont);
63 static void container_open_cmd (GtkWidget *widget, Container *container);
64 static void container_close_cmd (GtkWidget *widget, Container *container);
65 static void container_exit_cmd (void);
66 static void container_about_cmd (GtkWidget *widget, Container *container);
67 static void container_dump_cmd (GtkWidget *widget, Container *container);
68 static Component *container_activate_component (Container *container, char *component_goad_id);
74 BonoboUIVerb verbs [] = {
75 BONOBO_UI_UNSAFE_VERB ("FileOpen", container_open_cmd),
76 BONOBO_UI_UNSAFE_VERB ("FileClose", container_close_cmd),
77 BONOBO_UI_UNSAFE_VERB ("FileExit", container_exit_cmd),
79 BONOBO_UI_UNSAFE_VERB ("HelpAbout", container_about_cmd),
81 BONOBO_UI_UNSAFE_VERB ("DebugDumpXml", container_dump_cmd),
89 open_pdf (Container *container, const char *name)
91 BonoboObjectClient *object;
93 Bonobo_PersistStream persist;
97 g_return_val_if_fail (container != NULL, FALSE);
98 g_return_val_if_fail (container->view_widget == NULL, FALSE);
100 comp = container_activate_component (
101 container, "OAFIID:gpdf_component:892f2727-e2ec-423c-91ad-6f7b75fec6c8");
103 if (!comp || !(object = comp->server)) {
104 gnome_error_dialog (_("Could not launch bonobo object."));
108 CORBA_exception_init (&ev);
109 persist = Bonobo_Unknown_query_interface (
110 bonobo_object_corba_objref (BONOBO_OBJECT (object)),
111 "IDL:Bonobo/PersistStream:1.0", &ev);
113 if (ev._major != CORBA_NO_EXCEPTION ||
114 persist == CORBA_OBJECT_NIL) {
115 gnome_error_dialog ("Panic: component doesn't implement PersistStream.");
119 stream = bonobo_stream_fs_open (name, Bonobo_Storage_READ);
121 if (stream == NULL) {
122 char *err = g_strconcat (_("Could not open "), name, NULL);
123 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
128 Bonobo_PersistStream_load (persist,
129 (Bonobo_Stream) bonobo_object_corba_objref (BONOBO_OBJECT (stream)),
133 Bonobo_Unknown_unref (persist, &ev);
134 CORBA_Object_release (persist, &ev);
135 CORBA_exception_free (&ev);
137 /* bonobo_view_frame_view_do_verb (comp->view_frame, "ZoomFit"); */
142 set_ok (GtkWidget *widget, gboolean *dialog_result)
144 *dialog_result = TRUE;
149 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
156 container_open_cmd (GtkWidget *widget, Container *container)
158 GtkFileSelection *fsel;
159 gboolean accepted = FALSE;
161 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
162 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
164 gtk_window_set_transient_for (GTK_WINDOW (fsel),
165 GTK_WINDOW (container->app));
167 /* Connect the signals for Ok and Cancel */
168 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
169 GTK_SIGNAL_FUNC (set_ok), &accepted);
170 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
171 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
172 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
175 * Make sure that we quit the main loop if the window is destroyed
177 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
178 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
181 gtk_widget_show (GTK_WIDGET (fsel));
182 gtk_grab_add (GTK_WIDGET (fsel));
186 char *name = gtk_file_selection_get_filename (fsel);
188 if (name [strlen (name)-1] != '/') {
189 char *fname = g_strdup (name);
190 if (container->view_widget) /* any sort of MDI sucks :-] */
191 container = container_new (fname);
193 if (!open_pdf (container, fname))
194 container_destroy (container);
199 dialog = gnome_message_box_new ("Can't open a directory",
200 GNOME_MESSAGE_BOX_ERROR,
201 GNOME_STOCK_BUTTON_OK, NULL);
202 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
203 GTK_WINDOW (container->app));
204 gnome_dialog_run (GNOME_DIALOG (dialog));
208 gtk_widget_destroy (GTK_WIDGET (fsel));
212 component_destroy (Component *component)
214 CORBA_Environment ev;
215 Container *container;
216 g_return_if_fail (component != NULL);
218 CORBA_exception_init (&ev);
220 /* Kill merged menus et al. */
221 bonobo_view_frame_view_deactivate (component->view_frame);
223 container = component->container;
224 gtk_widget_destroy (container->view_widget);
225 container->view_widget = NULL;
227 if (component->server)
228 Bonobo_Unknown_unref (
229 bonobo_object_corba_objref (BONOBO_OBJECT (component->server)), &ev);
230 component->server = NULL;
232 CORBA_exception_free (&ev);
238 container_destroy (Container *cont)
240 g_return_if_fail (g_list_find (containers, cont) != NULL);
242 containers = g_list_remove (containers, cont);
244 gtk_widget_destroy (cont->app);
248 component_destroy (cont->component);
249 cont->component = NULL;
258 container_close (Container *cont)
260 g_return_if_fail (g_list_find (containers, cont) != NULL);
262 if (cont->component) {
263 component_destroy (cont->component);
264 cont->component = NULL;
266 container_destroy (cont);
271 container_close_cmd (GtkWidget *widget, Container *cont)
273 container_close (cont);
277 container_destroy_cb (GtkWidget *widget, GdkEvent *event, Container *cont)
279 container_destroy (cont);
284 container_exit_cmd (void)
287 container_destroy ((Container *)containers->data);
291 container_dump_cmd (GtkWidget *widget, Container *container)
293 bonobo_win_dump (BONOBO_WIN (container->app), "on demand");
297 container_about_cmd (GtkWidget *widget, Container *container)
301 const gchar *authors[] = {
302 N_("Derek B. Noonburg, main author"),
303 N_("Michael Meeks, GNOME port maintainer."),
304 N_("Miguel de Icaza."),
312 for (i = 0; authors[i] != NULL; i++)
313 authors [i] = _(authors [i]);
316 about = gnome_about_new (_("GPDF"), xpdfVersion,
317 _("(C) 1996-1999 Derek B. Noonburg."),
318 authors, NULL, NULL);
320 gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
321 gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
322 gtk_widget_show (about);
327 container_set_view (Container *container, Component *component)
329 BonoboViewFrame *view_frame;
330 GtkWidget *view_widget;
333 * Create the remote view and the local ViewFrame.
335 view_frame = bonobo_client_site_new_view (
336 component->client_site,
337 bonobo_ui_component_get_container (container->ui_component));
339 component->view_frame = view_frame;
342 * Embed the view frame into the application.
344 view_widget = bonobo_view_frame_get_wrapper (view_frame);
345 bonobo_wrapper_set_visibility (BONOBO_WRAPPER (view_widget), FALSE);
346 container->view_widget = view_widget;
347 container->component = component;
349 gtk_container_add (GTK_CONTAINER (container->slot), view_widget);
352 * Activate it ( get it to merge menus etc. )
354 bonobo_view_frame_view_activate (view_frame);
355 bonobo_view_frame_set_covered (view_frame, FALSE);
357 gtk_widget_show_all (GTK_WIDGET (container->slot));
360 static BonoboObjectClient *
361 container_launch_component (BonoboClientSite *client_site,
362 BonoboItemContainer *container,
363 char *component_goad_id)
365 BonoboObjectClient *object_server;
368 * Launch the component.
370 object_server = bonobo_object_activate (component_goad_id, 0);
372 if (object_server == NULL)
376 * Bind it to the local ClientSite. Every embedded component
377 * has a local BonoboClientSite object which serves as a
378 * container-side point of contact for the embeddable. The
379 * container talks to the embeddable through its ClientSite
381 if (!bonobo_client_site_bind_embeddable (client_site, object_server)) {
382 bonobo_object_unref (BONOBO_OBJECT (object_server));
387 * The BonoboContainer object maintains a list of the
388 * ClientSites which it manages. Here we add the new
389 * ClientSite to that list.
391 bonobo_item_container_add (container, BONOBO_OBJECT (client_site));
393 return object_server;
398 container_activate_component (Container *container, char *component_goad_id)
400 Component *component;
401 BonoboClientSite *client_site;
402 BonoboObjectClient *server;
405 * The ClientSite is the container-side point of contact for
406 * the Embeddable. So there is a one-to-one correspondence
407 * between BonoboClientSites and BonoboEmbeddables. */
408 client_site = bonobo_client_site_new (container->container);
411 * A BonoboObjectClient is a simple wrapper for a remote
412 * BonoboObject (a server supporting Bonobo::Unknown).
414 server = container_launch_component (client_site, container->container,
416 if (server == NULL) {
419 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
421 gnome_warning_dialog (error_msg);
428 * Create the internal data structure which we will use to
429 * keep track of this component.
431 component = g_new0 (Component, 1);
432 component->container = container;
433 component->client_site = client_site;
434 component->server = server;
436 container_set_view (container, component);
442 filenames_dropped (GtkWidget * widget,
443 GdkDragContext *context,
446 GtkSelectionData *selection_data,
449 Container *container)
451 GList *names, *tmp_list;
453 names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
457 const char *fname = (const char *)tmp_list->data;
460 if (container->view_widget)
461 container = container_new (fname);
463 open_pdf (container, fname);
466 tmp_list = g_list_next (tmp_list);
472 container_new (const char *fname)
474 Container *container;
475 static GtkTargetEntry drag_types[] =
477 { "text/uri-list", 0, 0 },
479 static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
480 BonoboUIContainer *ui_container;
482 container = g_new0 (Container, 1);
484 container->app = bonobo_win_new ("pdf-viewer",
487 gtk_drag_dest_set (container->app,
488 GTK_DEST_DEFAULT_ALL,
489 drag_types, n_drag_types,
492 gtk_signal_connect (GTK_OBJECT(container->app),
493 "drag_data_received",
494 GTK_SIGNAL_FUNC(filenames_dropped),
495 (gpointer)container);
497 gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
498 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
500 container->container = bonobo_item_container_new ();
501 container->view_widget = NULL;
502 container->slot = gtk_event_box_new ();
503 gtk_widget_show (container->slot);
505 bonobo_win_set_contents (BONOBO_WIN (container->app),
506 GTK_WIDGET (container->slot));
507 gtk_widget_show_all (container->slot);
509 gtk_object_set_data (GTK_OBJECT (container->app), "container_data", container);
510 gtk_signal_connect (GTK_OBJECT (container->app), "delete_event",
511 GTK_SIGNAL_FUNC (container_destroy_cb), container);
513 ui_container = bonobo_ui_container_new ();
514 bonobo_ui_container_set_win (ui_container, BONOBO_WIN (container->app));
516 container->ui_component = bonobo_ui_component_new ("gpdf");
517 bonobo_ui_component_set_container (
518 container->ui_component,
519 bonobo_object_corba_objref (BONOBO_OBJECT (ui_container)));
521 bonobo_ui_component_add_verb_list_with_data (
522 container->ui_component, verbs, container);
524 bonobo_ui_util_set_ui (container->ui_component, DATADIR, "gpdf-ui.xml", "gpdf");
526 gtk_widget_show (container->app);
528 containers = g_list_append (containers, container);
531 if (!open_pdf (container, fname)) {
532 container_destroy (container);
536 gtk_widget_show (container->app);
542 main (int argc, char **argv)
544 CORBA_Environment ev;
546 const char **view_files = NULL;
550 CORBA_exception_init (&ev);
553 gnomelib_register_popt_table (oaf_popt_options, "OAF");
554 gnome_init_with_popt_table("PDFViewer", "0.0.1",
556 gpdf_popt_options, 0, &ctx);
557 orb = oaf_init (argc, argv);
559 CORBA_exception_free (&ev);
561 if (bonobo_init (orb, NULL, NULL) == FALSE)
562 g_error (_("Could not initialize Bonobo!\n"));
565 view_files = poptGetArgs (ctx);
571 for (i = 0; view_files[i]; i++)
572 if (container_new (view_files[i])) {
574 while (gtk_events_pending ())
575 gtk_main_iteration ();
578 if ((i == 0) || !loaded)
579 container_new (NULL);
581 poptFreeContext (ctx);