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 }
54 GnomeContainer *container;
57 GnomeViewFrame *active_view_frame;
66 GnomeClientSite *client_site;
67 GnomeViewFrame *view_frame;
68 GnomeObjectClient *server;
70 GtkWidget *views_hbox;
73 GList *containers = NULL;
78 static Container *container_new (void);
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 comp = container_activate_component (container, "bonobo-object:image-x-pdf");
114 if (!comp || !(object = comp->server)) {
115 gnome_error_dialog (_("Could not launch bonobo object."));
119 CORBA_exception_init (&ev);
120 persist = GNOME_Unknown_query_interface (
121 gnome_object_corba_objref (GNOME_OBJECT (object)),
122 "IDL:GNOME/PersistStream:1.0", &ev);
124 if (ev._major != CORBA_NO_EXCEPTION ||
125 persist == CORBA_OBJECT_NIL) {
126 gnome_error_dialog ("Panic: component is well broken.");
130 stream = gnome_stream_fs_open (name, GNOME_Storage_READ);
132 if (stream == NULL) {
133 char *err = g_strconcat (_("Could not open "), name, NULL);
134 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
139 GNOME_PersistStream_load (persist,
140 (GNOME_Stream) gnome_object_corba_objref (GNOME_OBJECT (stream)), &ev);
142 GNOME_Unknown_unref (persist, &ev);
143 CORBA_Object_release (persist, &ev);
144 CORBA_exception_free (&ev);
145 /* app->contains_pdf = TRUE; */
149 set_ok (GtkWidget *widget, gboolean *dialog_result)
151 *dialog_result = TRUE;
156 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
163 container_open_cmd (GtkWidget *widget, Container *app)
165 GtkFileSelection *fsel;
166 gboolean accepted = FALSE;
168 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
169 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
171 gtk_window_set_transient_for (GTK_WINDOW (fsel),
172 GTK_WINDOW (app->app));
174 /* Connect the signals for Ok and Cancel */
175 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
176 GTK_SIGNAL_FUNC (set_ok), &accepted);
177 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
178 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
179 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
182 * Make sure that we quit the main loop if the window is destroyed
184 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
185 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
188 gtk_widget_show (GTK_WIDGET (fsel));
189 gtk_grab_add (GTK_WIDGET (fsel));
193 char *name = gtk_file_selection_get_filename (fsel);
195 if (name [strlen (name)-1] != '/') {
196 /* if (app->contains_pdf)
197 app = application_new ();*/
198 char *fname = g_strdup (name);
199 open_pdf (app, fname);
203 dialog = gnome_message_box_new ("Can't open a directory",
204 GNOME_MESSAGE_BOX_ERROR,
205 GNOME_STOCK_BUTTON_OK, NULL);
206 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
207 GTK_WINDOW (app->app));
208 gnome_dialog_run (GNOME_DIALOG (dialog));
212 gtk_widget_destroy (GTK_WIDGET (fsel));
216 container_destroy (Container *cont)
218 containers = g_list_remove (containers, cont);
219 gtk_widget_destroy (cont->app);
226 container_close_cmd (GtkWidget *widget, Container *cont)
228 container_destroy (cont);
232 container_exit_cmd (void)
235 container_destroy ((Container *)containers->data);
239 component_user_activate_request_cb (GnomeViewFrame *view_frame, gpointer data)
241 Component *component = (Component *) data;
242 Container *container = component->container;
246 * If there is already an active View, deactivate it.
248 if (container->active_view_frame != NULL) {
250 * This just sends a notice to the embedded View that
251 * it is being deactivated. We will also forcibly
252 * cover it so that it does not receive any Gtk
255 gnome_view_frame_view_deactivate (container->active_view_frame);
258 * Here we manually cover it if it hasn't acquiesced.
259 * If it has consented to be deactivated, then it will
260 * already have notified us that it is inactive, and
261 * we will have covered it and set active_view_frame
262 * to NULL. Which is why this check is here.
264 if (container->active_view_frame != NULL)
265 gnome_view_frame_set_covered (container->active_view_frame, TRUE);
267 container->active_view_frame = NULL;
271 * Activate the View which the user clicked on. This just
272 * sends a request to the embedded View to activate itself.
273 * When it agrees to be activated, it will notify its
274 * ViewFrame, and our view_activated_cb callback will be
277 * We do not uncover the View here, because it may not wish to
278 * be activated, and so we wait until it notifies us that it
279 * has been activated to uncover it.
281 gnome_view_frame_view_activate (view_frame);
285 component_view_activated_cb (GnomeViewFrame *view_frame, gboolean activated, gpointer data)
287 Component *component = (Component *) data;
288 Container *container = component->container;
292 * If the View is requesting to be activated, then we
293 * check whether or not there is already an active
296 if (container->active_view_frame != NULL) {
297 g_warning ("View requested to be activated but there is already "
298 "an active View!\n");
303 * Otherwise, uncover it so that it can receive
304 * events, and set it as the active View.
306 gnome_view_frame_set_covered (view_frame, FALSE);
307 container->active_view_frame = view_frame;
310 * If the View is asking to be deactivated, always
311 * oblige. We may have already deactivated it (see
312 * user_activation_request_cb), but there's no harm in
313 * doing it again. There is always the possibility
314 * that a View will ask to be deactivated when we have
315 * not told it to deactivate itself, and that is
316 * why we cover the view here.
318 gnome_view_frame_set_covered (view_frame, TRUE);
320 if (view_frame == container->active_view_frame)
321 container->active_view_frame = NULL;
326 component_user_context_cb (GnomeViewFrame *view_frame, gpointer data)
328 Component *component = (Component *) data;
333 * See if the remote GnomeEmbeddable supports any verbs at
336 l = gnome_client_site_get_verbs (component->client_site);
339 gnome_client_site_free_verbs (l);
342 * Popup the verb popup and execute the chosen verb. This
343 * function saves us the work of creating the menu, connecting
344 * the callback, and executing the verb on the remove
345 * GnomeView. We could implement all this functionality
346 * ourselves if we wanted.
348 executed_verb = gnome_view_frame_popup_verbs (view_frame);
350 g_free (executed_verb);
355 component_add_view (Component *component)
357 GnomeViewFrame *view_frame;
358 GtkWidget *view_widget;
361 * Create the remote view and the local ViewFrame.
363 view_frame = gnome_client_site_embeddable_new_view (component->client_site);
364 component->view_frame = view_frame;
367 * Set the GnomeUIHandler for this ViewFrame. That way, the
368 * embedded component can get access to our UIHandler server
369 * so that it can merge menu and toolbar items when it gets
372 gnome_view_frame_set_ui_handler (view_frame, component->container->uih);
375 * Embed the view frame into the application.
377 view_widget = gnome_view_frame_get_wrapper (view_frame);
378 gtk_box_pack_start (GTK_BOX (component->views_hbox), view_widget,
382 * The "user_activate" signal will be emitted when the user
383 * double clicks on the "cover". The cover is a transparent
384 * window which sits on top of the component and keeps any
385 * events (mouse, keyboard) from reaching it. When the user
386 * double clicks on the cover, the container (that's us)
387 * can choose to activate the component.
389 gtk_signal_connect (GTK_OBJECT (view_frame), "user_activate",
390 GTK_SIGNAL_FUNC (component_user_activate_request_cb), component);
393 * In-place activation of a component is a two-step process.
394 * After the user double clicks on the component, our signal
395 * callback (compoennt_user_activate_request_cb()) asks the
396 * component to activate itself (see
397 * gnome_view_frame_view_activate()). The component can then
398 * choose to either accept or refuse activation. When an
399 * embedded component notifies us of its decision to change
400 * its activation state, the "view_activated" signal is
401 * emitted from the view frame. It is at that point that we
402 * actually remove the cover so that events can get through.
404 gtk_signal_connect (GTK_OBJECT (view_frame), "view_activated",
405 GTK_SIGNAL_FUNC (component_view_activated_cb), component);
408 * The "user_context" signal is emitted when the user right
409 * clicks on the wrapper. We use it to pop up a verb menu.
411 gtk_signal_connect (GTK_OBJECT (view_frame), "user_context",
412 GTK_SIGNAL_FUNC (component_user_context_cb), component);
415 * Show the component.
417 gtk_widget_show_all (view_widget);
421 component_new_view_cb (GtkWidget *button, gpointer data)
423 Component *component = (Component *) data;
425 component_add_view (component);
428 static GnomeObjectClient *
429 container_launch_component (GnomeClientSite *client_site,
430 GnomeContainer *container,
431 char *component_goad_id)
433 GnomeObjectClient *object_server;
436 * Launch the component.
438 object_server = gnome_object_activate_with_goad_id (
439 NULL, component_goad_id, 0, NULL);
441 if (object_server == NULL)
445 * Bind it to the local ClientSite. Every embedded component
446 * has a local GnomeClientSite object which serves as a
447 * container-side point of contact for the embeddable. The
448 * container talks to the embeddable through its ClientSite
450 if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
451 gnome_object_unref (GNOME_OBJECT (object_server));
456 * The GnomeContainer object maintains a list of the
457 * ClientSites which it manages. Here we add the new
458 * ClientSite to that list.
460 gnome_container_add (container, GNOME_OBJECT (client_site));
462 return object_server;
466 * Use query_interface to see if `obj' has `interface'.
469 gnome_object_has_interface (GnomeObject *obj, char *interface)
471 CORBA_Environment ev;
472 CORBA_Object requested_interface;
474 CORBA_exception_init (&ev);
476 requested_interface = GNOME_Unknown_query_interface (
477 gnome_object_corba_objref (obj), interface, &ev);
479 CORBA_exception_free (&ev);
481 if (!CORBA_Object_is_nil(requested_interface, &ev) &&
482 ev._major == CORBA_NO_EXCEPTION)
484 /* Get rid of the interface we've been passed */
485 CORBA_Object_release (requested_interface, &ev);
493 container_create_component_frame (Container *container, Component *component, char *name)
496 * This hbox will store all the views of the component.
498 component->views_hbox = gtk_hbox_new (FALSE, 2);
500 gtk_box_pack_start (GTK_BOX (container->vbox), component->views_hbox,
502 gtk_widget_show_all (component->views_hbox);
507 container_activate_component (Container *container, char *component_goad_id)
509 Component *component;
510 GnomeClientSite *client_site;
511 GnomeObjectClient *server;
514 * The ClientSite is the container-side point of contact for
515 * the Embeddable. So there is a one-to-one correspondence
516 * between GnomeClientSites and GnomeEmbeddables. */
517 client_site = gnome_client_site_new (container->container);
520 * A GnomeObjectClient is a simple wrapper for a remote
521 * GnomeObject (a server supporting GNOME::Unknown).
523 server = container_launch_component (client_site, container->container,
525 if (server == NULL) {
528 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
530 gnome_warning_dialog (error_msg);
537 * Create the internal data structure which we will use to
538 * keep track of this component.
540 component = g_new0 (Component, 1);
541 component->container = container;
542 component->client_site = client_site;
543 component->server = server;
546 * Now we have a GnomeEmbeddable bound to our local
547 * ClientSite. Here we create a little on-screen box to store
548 * the embeddable in, when the user adds views for it.
550 container_create_component_frame (container, component, component_goad_id);
552 component_add_view (component);
559 container_create_menus (Container *container)
561 GnomeUIHandlerMenuItem *menu_list;
563 gnome_ui_handler_create_menubar (container->uih);
566 * Create the basic menus out of UIInfo structures.
568 menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
569 gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
570 gnome_ui_handler_menu_free_list (menu_list);
574 container_create (void)
576 Container *container;
578 container = g_new0 (Container, 1);
580 container->app = gnome_app_new ("pdf-viewer",
583 gtk_window_set_default_size (GTK_WINDOW (container->app), 400, 400);
584 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
586 container->container = gnome_container_new ();
589 * This is the VBox we will stuff embedded components into.
591 container->vbox = gtk_vbox_new (FALSE, 0);
592 gnome_app_set_contents (GNOME_APP (container->app), container->vbox);
595 * Create the GnomeUIHandler object which will be used to
596 * create the container's menus and toolbars. The UIHandler
597 * also creates a CORBA server which embedded components use
598 * to do menu/toolbar merging.
600 container->uih = gnome_ui_handler_new ();
601 gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
606 container_create_menus (container);
608 gtk_widget_show_all (container->app);
612 main (int argc, char **argv)
614 CORBA_Environment ev;
617 CORBA_exception_init (&ev);
619 gnome_CORBA_init ("gnome_xpdf_viewer", "0.1", &argc, argv, 0, &ev);
621 CORBA_exception_free (&ev);
623 orb = gnome_CORBA_ORB ();
625 if (bonobo_init (orb, NULL, NULL) == FALSE)
626 g_error (_("Could not initialize Bonobo!\n"));