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"
47 const struct poptOption gpdf_popt_options [] = {
48 { "debug", '\0', POPT_ARG_INT, &gpdf_debug, 0,
49 N_("Enables some debugging functions"), N_("LEVEL") },
50 { NULL, '\0', 0, NULL, 0 }
53 /* NB. there is a 1 to 1 Container -> Component mapping, this
54 is due to how much MDI sucks; unutterably */
56 GnomeContainer *container;
59 GnomeViewFrame *active_view_frame;
62 GtkWidget *view_widget;
68 GnomeClientSite *client_site;
69 GnomeViewFrame *view_frame;
70 GnomeObjectClient *server;
73 GList *containers = NULL;
78 static Container *container_new (const char *fname);
79 static void container_destroy (Container *cont);
80 static void container_open_cmd (GtkWidget *widget, Container *container);
81 static void container_close_cmd (GtkWidget *widget, Container *container);
82 static void container_exit_cmd (void);
83 static Component *container_activate_component (Container *container, char *component_goad_id);
89 static GnomeUIInfo container_file_menu [] = {
90 GNOMEUIINFO_MENU_OPEN_ITEM (container_open_cmd, NULL),
91 GNOMEUIINFO_SEPARATOR,
92 GNOMEUIINFO_MENU_CLOSE_ITEM(container_close_cmd, NULL),
93 GNOMEUIINFO_SEPARATOR,
94 GNOMEUIINFO_MENU_EXIT_ITEM (container_exit_cmd, NULL),
98 static GnomeUIInfo container_main_menu [] = {
99 GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
105 open_pdf (Container *container, const char *name)
107 GnomeObjectClient *object;
109 GNOME_PersistStream persist;
111 CORBA_Environment ev;
113 g_return_val_if_fail (container != NULL, FALSE);
114 g_return_val_if_fail (container->view_widget == NULL, FALSE);
116 comp = container_activate_component (container, "bonobo-object:image-x-pdf");
117 if (!comp || !(object = comp->server)) {
118 gnome_error_dialog (_("Could not launch bonobo object."));
122 CORBA_exception_init (&ev);
123 persist = GNOME_Unknown_query_interface (
124 gnome_object_corba_objref (GNOME_OBJECT (object)),
125 "IDL:GNOME/PersistStream:1.0", &ev);
127 if (ev._major != CORBA_NO_EXCEPTION ||
128 persist == CORBA_OBJECT_NIL) {
129 gnome_error_dialog ("Panic: component is well broken.");
133 stream = gnome_stream_fs_open (name, GNOME_Storage_READ);
135 if (stream == NULL) {
136 char *err = g_strconcat (_("Could not open "), name, NULL);
137 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
142 GNOME_PersistStream_load (persist,
143 (GNOME_Stream) gnome_object_corba_objref (GNOME_OBJECT (stream)), &ev);
145 GNOME_Unknown_unref (persist, &ev);
146 CORBA_Object_release (persist, &ev);
147 CORBA_exception_free (&ev);
152 set_ok (GtkWidget *widget, gboolean *dialog_result)
154 *dialog_result = TRUE;
159 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
166 container_open_cmd (GtkWidget *widget, Container *container)
168 GtkFileSelection *fsel;
169 gboolean accepted = FALSE;
171 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
172 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
174 gtk_window_set_transient_for (GTK_WINDOW (fsel),
175 GTK_WINDOW (container->app));
177 /* Connect the signals for Ok and Cancel */
178 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
179 GTK_SIGNAL_FUNC (set_ok), &accepted);
180 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
181 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
182 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
185 * Make sure that we quit the main loop if the window is destroyed
187 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
188 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
191 gtk_widget_show (GTK_WIDGET (fsel));
192 gtk_grab_add (GTK_WIDGET (fsel));
196 char *name = gtk_file_selection_get_filename (fsel);
198 if (name [strlen (name)-1] != '/') {
199 char *fname = g_strdup (name);
200 if (container->view_widget) /* any sort of MDI sucks :-] */
201 container = container_new (fname);
203 open_pdf (container, fname);
207 dialog = gnome_message_box_new ("Can't open a directory",
208 GNOME_MESSAGE_BOX_ERROR,
209 GNOME_STOCK_BUTTON_OK, NULL);
210 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
211 GTK_WINDOW (container->app));
212 gnome_dialog_run (GNOME_DIALOG (dialog));
216 gtk_widget_destroy (GTK_WIDGET (fsel));
220 container_destroy (Container *cont)
222 containers = g_list_remove (containers, cont);
223 gtk_widget_destroy (cont->app);
230 container_close_cmd (GtkWidget *widget, Container *cont)
232 container_destroy (cont);
236 container_exit_cmd (void)
239 container_destroy ((Container *)containers->data);
243 component_user_activate_request_cb (GnomeViewFrame *view_frame, gpointer data)
245 Component *component = (Component *) data;
246 Container *container = component->container;
250 * If there is already an active View, deactivate it.
252 if (container->active_view_frame != NULL) {
254 * This just sends a notice to the embedded View that
255 * it is being deactivated. We will also forcibly
256 * cover it so that it does not receive any Gtk
259 gnome_view_frame_view_deactivate (container->active_view_frame);
262 * Here we manually cover it if it hasn't acquiesced.
263 * If it has consented to be deactivated, then it will
264 * already have notified us that it is inactive, and
265 * we will have covered it and set active_view_frame
266 * to NULL. Which is why this check is here.
268 if (container->active_view_frame != NULL)
269 gnome_view_frame_set_covered (container->active_view_frame, TRUE);
271 container->active_view_frame = NULL;
275 * Activate the View which the user clicked on. This just
276 * sends a request to the embedded View to activate itself.
277 * When it agrees to be activated, it will notify its
278 * ViewFrame, and our view_activated_cb callback will be
281 * We do not uncover the View here, because it may not wish to
282 * be activated, and so we wait until it notifies us that it
283 * has been activated to uncover it.
285 gnome_view_frame_view_activate (view_frame);
289 component_view_activated_cb (GnomeViewFrame *view_frame, gboolean activated, gpointer data)
291 Component *component = (Component *) data;
292 Container *container = component->container;
296 * If the View is requesting to be activated, then we
297 * check whether or not there is already an active
300 if (container->active_view_frame != NULL) {
301 g_warning ("View requested to be activated but there is already "
302 "an active View!\n");
307 * Otherwise, uncover it so that it can receive
308 * events, and set it as the active View.
310 gnome_view_frame_set_covered (view_frame, FALSE);
311 container->active_view_frame = view_frame;
314 * If the View is asking to be deactivated, always
315 * oblige. We may have already deactivated it (see
316 * user_activation_request_cb), but there's no harm in
317 * doing it again. There is always the possibility
318 * that a View will ask to be deactivated when we have
319 * not told it to deactivate itself, and that is
320 * why we cover the view here.
322 gnome_view_frame_set_covered (view_frame, TRUE);
324 if (view_frame == container->active_view_frame)
325 container->active_view_frame = NULL;
330 component_user_context_cb (GnomeViewFrame *view_frame, gpointer data)
332 Component *component = (Component *) data;
337 * See if the remote GnomeEmbeddable supports any verbs at
340 l = gnome_client_site_get_verbs (component->client_site);
343 gnome_client_site_free_verbs (l);
346 * Popup the verb popup and execute the chosen verb. This
347 * function saves us the work of creating the menu, connecting
348 * the callback, and executing the verb on the remove
349 * GnomeView. We could implement all this functionality
350 * ourselves if we wanted.
352 executed_verb = gnome_view_frame_popup_verbs (view_frame);
354 g_free (executed_verb);
359 container_set_view (Container *container, Component *component)
361 GnomeViewFrame *view_frame;
362 GtkWidget *view_widget;
365 * Create the remote view and the local ViewFrame.
367 view_frame = gnome_client_site_embeddable_new_view (component->client_site);
368 component->view_frame = view_frame;
371 * Set the GnomeUIHandler for this ViewFrame. That way, the
372 * embedded component can get access to our UIHandler server
373 * so that it can merge menu and toolbar items when it gets
376 gnome_view_frame_set_ui_handler (view_frame, container->uih);
379 * Embed the view frame into the application.
381 view_widget = gnome_view_frame_get_wrapper (view_frame);
382 container->view_widget = view_widget;
383 /* gtk_box_pack_start (GTK_BOX (container->app), view_widget,
385 gnome_app_set_contents (GNOME_APP (container->app), view_widget);
387 * The "user_activate" signal will be emitted when the user
388 * double clicks on the "cover". The cover is a transparent
389 * window which sits on top of the component and keeps any
390 * events (mouse, keyboard) from reaching it. When the user
391 * double clicks on the cover, the container (that's us)
392 * can choose to activate the component.
394 gtk_signal_connect (GTK_OBJECT (view_frame), "user_activate",
395 GTK_SIGNAL_FUNC (component_user_activate_request_cb), component);
398 * In-place activation of a component is a two-step process.
399 * After the user double clicks on the component, our signal
400 * callback (compoennt_user_activate_request_cb()) asks the
401 * component to activate itself (see
402 * gnome_view_frame_view_activate()). The component can then
403 * choose to either accept or refuse activation. When an
404 * embedded component notifies us of its decision to change
405 * its activation state, the "view_activated" signal is
406 * emitted from the view frame. It is at that point that we
407 * actually remove the cover so that events can get through.
409 gtk_signal_connect (GTK_OBJECT (view_frame), "view_activated",
410 GTK_SIGNAL_FUNC (component_view_activated_cb), component);
413 * The "user_context" signal is emitted when the user right
414 * clicks on the wrapper. We use it to pop up a verb menu.
416 gtk_signal_connect (GTK_OBJECT (view_frame), "user_context",
417 GTK_SIGNAL_FUNC (component_user_context_cb), component);
420 * Show the component.
422 gtk_widget_show_all (view_widget);
425 static GnomeObjectClient *
426 container_launch_component (GnomeClientSite *client_site,
427 GnomeContainer *container,
428 char *component_goad_id)
430 GnomeObjectClient *object_server;
433 * Launch the component.
435 object_server = gnome_object_activate_with_goad_id (
436 NULL, component_goad_id, 0, NULL);
438 if (object_server == NULL)
442 * Bind it to the local ClientSite. Every embedded component
443 * has a local GnomeClientSite object which serves as a
444 * container-side point of contact for the embeddable. The
445 * container talks to the embeddable through its ClientSite
447 if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
448 gnome_object_unref (GNOME_OBJECT (object_server));
453 * The GnomeContainer object maintains a list of the
454 * ClientSites which it manages. Here we add the new
455 * ClientSite to that list.
457 gnome_container_add (container, GNOME_OBJECT (client_site));
459 return object_server;
463 * Use query_interface to see if `obj' has `interface'.
466 gnome_object_has_interface (GnomeObject *obj, char *interface)
468 CORBA_Environment ev;
469 CORBA_Object requested_interface;
471 CORBA_exception_init (&ev);
473 requested_interface = GNOME_Unknown_query_interface (
474 gnome_object_corba_objref (obj), interface, &ev);
476 CORBA_exception_free (&ev);
478 if (!CORBA_Object_is_nil(requested_interface, &ev) &&
479 ev._major == CORBA_NO_EXCEPTION)
481 /* Get rid of the interface we've been passed */
482 CORBA_Object_release (requested_interface, &ev);
491 container_activate_component (Container *container, char *component_goad_id)
493 Component *component;
494 GnomeClientSite *client_site;
495 GnomeObjectClient *server;
498 * The ClientSite is the container-side point of contact for
499 * the Embeddable. So there is a one-to-one correspondence
500 * between GnomeClientSites and GnomeEmbeddables. */
501 client_site = gnome_client_site_new (container->container);
504 * A GnomeObjectClient is a simple wrapper for a remote
505 * GnomeObject (a server supporting GNOME::Unknown).
507 server = container_launch_component (client_site, container->container,
509 if (server == NULL) {
512 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
514 gnome_warning_dialog (error_msg);
521 * Create the internal data structure which we will use to
522 * keep track of this component.
524 component = g_new0 (Component, 1);
525 component->container = container;
526 component->client_site = client_site;
527 component->server = server;
529 container_set_view (container, component);
536 container_create_menus (Container *container)
538 GnomeUIHandlerMenuItem *menu_list;
540 gnome_ui_handler_create_menubar (container->uih);
543 * Create the basic menus out of UIInfo structures.
545 menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
546 gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
547 gnome_ui_handler_menu_free_list (menu_list);
551 container_new (const char *fname)
553 Container *container;
555 container = g_new0 (Container, 1);
557 container->app = gnome_app_new ("pdf-viewer",
560 gtk_window_set_default_size (GTK_WINDOW (container->app), 400, 400);
561 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
563 container->container = gnome_container_new ();
564 container->view_widget = NULL;
567 * Create the GnomeUIHandler object which will be used to
568 * create the container's menus and toolbars. The UIHandler
569 * also creates a CORBA server which embedded components use
570 * to do menu/toolbar merging.
572 container->uih = gnome_ui_handler_new ();
573 gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
578 container_create_menus (container);
580 gtk_widget_show_all (container->app);
583 if (!open_pdf (container, fname)) {
584 container_destroy (container);
588 containers = g_list_append (containers, container);
590 gtk_widget_show_all (container->app);
596 main (int argc, char **argv)
598 CORBA_Environment ev;
600 char **view_files = NULL;
603 CORBA_exception_init (&ev);
605 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
607 gpdf_popt_options, 0, &ctx,
608 GNORBA_INIT_SERVER_FUNC, &ev);
610 CORBA_exception_free (&ev);
612 orb = gnome_CORBA_ORB ();
614 if (bonobo_init (orb, NULL, NULL) == FALSE)
615 g_error (_("Could not initialize Bonobo!\n"));
617 view_files = poptGetArgs (ctx);
622 for (i = 0; view_files[i]; i++)
623 container_new (view_files[i]);
626 container_new (NULL);
628 poptFreeContext (ctx);