2 * PDF viewer Bonobo container.
5 * Michael Meeks <michael@imaginator.com>
13 #define GString G_String
15 #include <libgnorba/gnorba.h>
16 #include <gdk/gdkprivate.h>
18 #include <bonobo/gnome-bonobo.h>
25 #include "parseargs.h"
37 #include "GOutputDev.h"
38 #include "PSOutputDev.h"
39 #include "TextOutputDev.h"
49 const struct poptOption gpdf_popt_options [] = {
50 { "debug", '\0', POPT_ARG_INT, &gpdf_debug, 0,
51 N_("Enables some debugging functions"), N_("LEVEL") },
52 { NULL, '\0', 0, NULL, 0 }
55 typedef struct _Component Component;
56 typedef struct _Container Container;
57 /* NB. there is a 1 to 1 Container -> Component mapping, this
58 is due to how much MDI sucks; unutterably */
60 GnomeContainer *container;
63 GnomeViewFrame *active_view_frame;
66 GtkWidget *view_widget;
74 GnomeClientSite *client_site;
75 GnomeViewFrame *view_frame;
76 GnomeObjectClient *server;
79 GList *containers = NULL;
84 static Container *container_new (const char *fname);
85 static void container_destroy (Container *cont);
86 static void container_open_cmd (GtkWidget *widget, Container *container);
87 static void container_close_cmd (GtkWidget *widget, Container *container);
88 static void container_exit_cmd (void);
89 static Component *container_activate_component (Container *container, char *component_goad_id);
90 static void zoom_in_cmd (GtkWidget *widget, Container *container);
91 static void zoom_out_cmd (GtkWidget *widget, Container *container);
92 static void zoom_set (Container *container);
98 static GnomeUIInfo container_file_menu [] = {
99 GNOMEUIINFO_MENU_OPEN_ITEM (container_open_cmd, NULL),
100 GNOMEUIINFO_SEPARATOR,
101 GNOMEUIINFO_MENU_CLOSE_ITEM(container_close_cmd, NULL),
102 GNOMEUIINFO_SEPARATOR,
103 GNOMEUIINFO_MENU_EXIT_ITEM (container_exit_cmd, NULL),
107 static GnomeUIInfo container_menu_zoom [] = {
108 { GNOME_APP_UI_ITEM, N_("_Zoom in"),
109 N_("Increase the size of objects in the PDF"),
111 { GNOME_APP_UI_ITEM, N_("_Zoom out"),
112 N_("Decrease the size of objects in the PDF"),
117 static GnomeUIInfo container_main_menu [] = {
118 GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
119 { GNOME_APP_UI_SUBTREE, N_("_Zoom"), NULL, container_menu_zoom },
123 static GnomeUIInfo container_toolbar [] = {
124 GNOMEUIINFO_ITEM_STOCK (
125 N_("Open"), N_("Opens an existing workbook"),
126 container_open_cmd, GNOME_STOCK_PIXMAP_OPEN),
128 GNOMEUIINFO_SEPARATOR,
134 open_pdf (Container *container, const char *name)
136 GnomeObjectClient *object;
138 GNOME_PersistStream persist;
140 CORBA_Environment ev;
142 g_return_val_if_fail (container != NULL, FALSE);
143 g_return_val_if_fail (container->view_widget == NULL, FALSE);
145 comp = container_activate_component (container, "bonobo-object:image-x-pdf");
146 if (!comp || !(object = comp->server)) {
147 gnome_error_dialog (_("Could not launch bonobo object."));
151 CORBA_exception_init (&ev);
152 persist = GNOME_Unknown_query_interface (
153 gnome_object_corba_objref (GNOME_OBJECT (object)),
154 "IDL:GNOME/PersistStream:1.0", &ev);
156 if (ev._major != CORBA_NO_EXCEPTION ||
157 persist == CORBA_OBJECT_NIL) {
158 gnome_error_dialog ("Panic: component is well broken.");
162 stream = gnome_stream_fs_open (name, GNOME_Storage_READ);
164 if (stream == NULL) {
165 char *err = g_strconcat (_("Could not open "), name, NULL);
166 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
171 GNOME_PersistStream_load (persist,
172 (GNOME_Stream) gnome_object_corba_objref (GNOME_OBJECT (stream)), &ev);
176 GNOME_Unknown_unref (persist, &ev);
177 CORBA_Object_release (persist, &ev);
178 CORBA_exception_free (&ev);
183 set_ok (GtkWidget *widget, gboolean *dialog_result)
185 *dialog_result = TRUE;
190 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
197 container_open_cmd (GtkWidget *widget, Container *container)
199 GtkFileSelection *fsel;
200 gboolean accepted = FALSE;
202 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
203 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
205 gtk_window_set_transient_for (GTK_WINDOW (fsel),
206 GTK_WINDOW (container->app));
208 /* Connect the signals for Ok and Cancel */
209 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
210 GTK_SIGNAL_FUNC (set_ok), &accepted);
211 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
212 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
213 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
216 * Make sure that we quit the main loop if the window is destroyed
218 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
219 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
222 gtk_widget_show (GTK_WIDGET (fsel));
223 gtk_grab_add (GTK_WIDGET (fsel));
227 char *name = gtk_file_selection_get_filename (fsel);
229 if (name [strlen (name)-1] != '/') {
230 char *fname = g_strdup (name);
231 if (container->view_widget) /* any sort of MDI sucks :-] */
232 container = container_new (fname);
234 open_pdf (container, fname);
238 dialog = gnome_message_box_new ("Can't open a directory",
239 GNOME_MESSAGE_BOX_ERROR,
240 GNOME_STOCK_BUTTON_OK, NULL);
241 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
242 GTK_WINDOW (container->app));
243 gnome_dialog_run (GNOME_DIALOG (dialog));
247 gtk_widget_destroy (GTK_WIDGET (fsel));
251 container_destroy (Container *cont)
253 containers = g_list_remove (containers, cont);
254 gtk_widget_destroy (cont->app);
261 container_close_cmd (GtkWidget *widget, Container *cont)
263 container_destroy (cont);
267 container_exit_cmd (void)
270 container_destroy ((Container *)containers->data);
274 * Enforces the containers zoom factor.
277 zoom_set (Container *container)
279 g_return_if_fail (container != NULL);
280 g_return_if_fail (container->component != NULL);
282 gnome_view_frame_set_zoom_factor (container->component->view_frame,
287 zoom_in_cmd (GtkWidget *widget, Container *container)
289 g_return_if_fail (container != NULL);
290 container->zoom *= 1.4;
291 zoom_set (container);
295 zoom_out_cmd (GtkWidget *widget, Container *container)
297 g_return_if_fail (container != NULL);
298 container->zoom /= 1.4;
299 zoom_set (container);
303 component_user_activate_request_cb (GnomeViewFrame *view_frame, gpointer data)
305 Component *component = (Component *) data;
306 Container *container = component->container;
310 * If there is already an active View, deactivate it.
312 if (container->active_view_frame != NULL) {
314 * This just sends a notice to the embedded View that
315 * it is being deactivated. We will also forcibly
316 * cover it so that it does not receive any Gtk
319 gnome_view_frame_view_deactivate (container->active_view_frame);
322 * Here we manually cover it if it hasn't acquiesced.
323 * If it has consented to be deactivated, then it will
324 * already have notified us that it is inactive, and
325 * we will have covered it and set active_view_frame
326 * to NULL. Which is why this check is here.
328 if (container->active_view_frame != NULL)
329 gnome_view_frame_set_covered (container->active_view_frame, TRUE);
331 container->active_view_frame = NULL;
335 * Activate the View which the user clicked on. This just
336 * sends a request to the embedded View to activate itself.
337 * When it agrees to be activated, it will notify its
338 * ViewFrame, and our view_activated_cb callback will be
341 * We do not uncover the View here, because it may not wish to
342 * be activated, and so we wait until it notifies us that it
343 * has been activated to uncover it.
345 gnome_view_frame_view_activate (view_frame);
349 component_view_activated_cb (GnomeViewFrame *view_frame, gboolean activated, gpointer data)
351 Component *component = (Component *) data;
352 Container *container = component->container;
356 * If the View is requesting to be activated, then we
357 * check whether or not there is already an active
360 if (container->active_view_frame != NULL) {
361 g_warning ("View requested to be activated but there is already "
362 "an active View!\n");
367 * Otherwise, uncover it so that it can receive
368 * events, and set it as the active View.
370 gnome_view_frame_set_covered (view_frame, FALSE);
371 container->active_view_frame = view_frame;
374 * If the View is asking to be deactivated, always
375 * oblige. We may have already deactivated it (see
376 * user_activation_request_cb), but there's no harm in
377 * doing it again. There is always the possibility
378 * that a View will ask to be deactivated when we have
379 * not told it to deactivate itself, and that is
380 * why we cover the view here.
382 gnome_view_frame_set_covered (view_frame, TRUE);
384 if (view_frame == container->active_view_frame)
385 container->active_view_frame = NULL;
390 component_user_context_cb (GnomeViewFrame *view_frame, gpointer data)
392 Component *component = (Component *) data;
397 * See if the remote GnomeEmbeddable supports any verbs at
400 l = gnome_client_site_get_verbs (component->client_site);
403 gnome_client_site_free_verbs (l);
406 * Popup the verb popup and execute the chosen verb. This
407 * function saves us the work of creating the menu, connecting
408 * the callback, and executing the verb on the remove
409 * GnomeView. We could implement all this functionality
410 * ourselves if we wanted.
412 executed_verb = gnome_view_frame_popup_verbs (view_frame);
414 g_free (executed_verb);
419 container_set_view (Container *container, Component *component)
421 GnomeViewFrame *view_frame;
422 GtkWidget *view_widget;
425 * Create the remote view and the local ViewFrame.
427 view_frame = gnome_client_site_embeddable_new_view (component->client_site);
428 component->view_frame = view_frame;
431 * Set the GnomeUIHandler for this ViewFrame. That way, the
432 * embedded component can get access to our UIHandler server
433 * so that it can merge menu and toolbar items when it gets
436 gnome_view_frame_set_ui_handler (view_frame, container->uih);
439 * Embed the view frame into the application.
441 view_widget = gnome_view_frame_get_wrapper (view_frame);
442 container->view_widget = view_widget;
443 container->component = component;
444 /* gtk_box_pack_start (GTK_BOX (container->app), view_widget,
446 gnome_app_set_contents (GNOME_APP (container->app), view_widget);
448 * The "user_activate" signal will be emitted when the user
449 * double clicks on the "cover". The cover is a transparent
450 * window which sits on top of the component and keeps any
451 * events (mouse, keyboard) from reaching it. When the user
452 * double clicks on the cover, the container (that's us)
453 * can choose to activate the component.
455 gtk_signal_connect (GTK_OBJECT (view_frame), "user_activate",
456 GTK_SIGNAL_FUNC (component_user_activate_request_cb), component);
459 * In-place activation of a component is a two-step process.
460 * After the user double clicks on the component, our signal
461 * callback (compoennt_user_activate_request_cb()) asks the
462 * component to activate itself (see
463 * gnome_view_frame_view_activate()). The component can then
464 * choose to either accept or refuse activation. When an
465 * embedded component notifies us of its decision to change
466 * its activation state, the "view_activated" signal is
467 * emitted from the view frame. It is at that point that we
468 * actually remove the cover so that events can get through.
470 gtk_signal_connect (GTK_OBJECT (view_frame), "view_activated",
471 GTK_SIGNAL_FUNC (component_view_activated_cb), component);
474 * The "user_context" signal is emitted when the user right
475 * clicks on the wrapper. We use it to pop up a verb menu.
477 gtk_signal_connect (GTK_OBJECT (view_frame), "user_context",
478 GTK_SIGNAL_FUNC (component_user_context_cb), component);
481 * Show the component.
483 gtk_widget_show_all (view_widget);
486 static GnomeObjectClient *
487 container_launch_component (GnomeClientSite *client_site,
488 GnomeContainer *container,
489 char *component_goad_id)
491 GnomeObjectClient *object_server;
494 * Launch the component.
496 object_server = gnome_object_activate_with_goad_id (
497 NULL, component_goad_id, 0, NULL);
499 if (object_server == NULL)
503 * Bind it to the local ClientSite. Every embedded component
504 * has a local GnomeClientSite object which serves as a
505 * container-side point of contact for the embeddable. The
506 * container talks to the embeddable through its ClientSite
508 if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
509 gnome_object_unref (GNOME_OBJECT (object_server));
514 * The GnomeContainer object maintains a list of the
515 * ClientSites which it manages. Here we add the new
516 * ClientSite to that list.
518 gnome_container_add (container, GNOME_OBJECT (client_site));
520 return object_server;
524 * Use query_interface to see if `obj' has `interface'.
527 gnome_object_has_interface (GnomeObject *obj, char *interface)
529 CORBA_Environment ev;
530 CORBA_Object requested_interface;
532 CORBA_exception_init (&ev);
534 requested_interface = GNOME_Unknown_query_interface (
535 gnome_object_corba_objref (obj), interface, &ev);
537 CORBA_exception_free (&ev);
539 if (!CORBA_Object_is_nil(requested_interface, &ev) &&
540 ev._major == CORBA_NO_EXCEPTION)
542 /* Get rid of the interface we've been passed */
543 CORBA_Object_release (requested_interface, &ev);
552 container_activate_component (Container *container, char *component_goad_id)
554 Component *component;
555 GnomeClientSite *client_site;
556 GnomeObjectClient *server;
559 * The ClientSite is the container-side point of contact for
560 * the Embeddable. So there is a one-to-one correspondence
561 * between GnomeClientSites and GnomeEmbeddables. */
562 client_site = gnome_client_site_new (container->container);
565 * A GnomeObjectClient is a simple wrapper for a remote
566 * GnomeObject (a server supporting GNOME::Unknown).
568 server = container_launch_component (client_site, container->container,
570 if (server == NULL) {
573 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
575 gnome_warning_dialog (error_msg);
582 * Create the internal data structure which we will use to
583 * keep track of this component.
585 component = g_new0 (Component, 1);
586 component->container = container;
587 component->client_site = client_site;
588 component->server = server;
590 container_set_view (container, component);
597 container_create_menus (Container *container)
599 GnomeUIHandlerMenuItem *menu_list;
601 gnome_ui_handler_create_menubar (container->uih);
604 * Create the basic menus out of UIInfo structures.
606 menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
607 gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
608 gnome_ui_handler_menu_free_list (menu_list);
612 container_create_toolbar (Container *container)
614 GnomeUIHandlerMenuItem *toolbar;
617 gnome_ui_handler_create_menubar (container->uih);
620 * Create the basic menus out of UIInfo structures.
622 toolbar = gnome_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
623 gnome_ui_handler_toolbar_add_list (container->uih, "/", menu_list);
624 gnome_ui_handler_toolbar_free_list (menu_list);
629 container_new (const char *fname)
631 Container *container;
633 container = g_new0 (Container, 1);
635 container->app = gnome_app_new ("pdf-viewer",
637 container->zoom = 43.0;
639 gtk_window_set_default_size (GTK_WINDOW (container->app), 400, 400);
640 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
642 container->container = gnome_container_new ();
643 container->view_widget = NULL;
646 * Create the GnomeUIHandler object which will be used to
647 * create the container's menus and toolbars. The UIHandler
648 * also creates a CORBA server which embedded components use
649 * to do menu/toolbar merging.
651 container->uih = gnome_ui_handler_new ();
652 gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
654 container_create_menus (container);
655 container_create_toolbar (container);
657 gtk_widget_show_all (container->app);
660 if (!open_pdf (container, fname)) {
661 container_destroy (container);
665 containers = g_list_append (containers, container);
667 gtk_widget_show_all (container->app);
673 main (int argc, char **argv)
675 CORBA_Environment ev;
677 char **view_files = NULL;
680 CORBA_exception_init (&ev);
682 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
684 gpdf_popt_options, 0, &ctx,
685 GNORBA_INIT_SERVER_FUNC, &ev);
687 CORBA_exception_free (&ev);
689 orb = gnome_CORBA_ORB ();
691 if (bonobo_init (orb, NULL, NULL) == FALSE)
692 g_error (_("Could not initialize Bonobo!\n"));
694 view_files = poptGetArgs (ctx);
699 for (i = 0; view_files[i]; i++)
700 container_new (view_files[i]);
703 container_new (NULL);
705 poptFreeContext (ctx);