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 typedef struct _Component Component;
54 typedef struct _Container Container;
55 /* NB. there is a 1 to 1 Container -> Component mapping, this
56 is due to how much MDI sucks; unutterably */
58 GnomeContainer *container;
61 GnomeViewFrame *active_view_frame;
64 GtkScrolledWindow *scroll;
65 GtkWidget *view_widget;
73 GnomeClientSite *client_site;
74 GnomeViewFrame *view_frame;
75 GnomeObjectClient *server;
78 GList *containers = NULL;
83 static Container *container_new (const char *fname);
84 static void container_destroy (Container *cont);
85 static void container_open_cmd (GtkWidget *widget, Container *container);
86 static void container_close_cmd (GtkWidget *widget, Container *container);
87 static void container_exit_cmd (void);
88 static Component *container_activate_component (Container *container, char *component_goad_id);
89 static void zoom_in_cmd (GtkWidget *widget, Container *container);
90 static void zoom_out_cmd (GtkWidget *widget, Container *container);
91 static void zoom_set (Container *container);
97 static GnomeUIInfo container_file_menu [] = {
98 GNOMEUIINFO_MENU_OPEN_ITEM (container_open_cmd, NULL),
99 GNOMEUIINFO_SEPARATOR,
100 GNOMEUIINFO_MENU_CLOSE_ITEM(container_close_cmd, NULL),
101 GNOMEUIINFO_SEPARATOR,
102 GNOMEUIINFO_MENU_EXIT_ITEM (container_exit_cmd, NULL),
106 static GnomeUIInfo container_menu_zoom [] = {
107 { GNOME_APP_UI_ITEM, N_("_Zoom in"),
108 N_("Increase the size of objects in the PDF"),
110 { GNOME_APP_UI_ITEM, N_("_Zoom out"),
111 N_("Decrease the size of objects in the PDF"),
116 static GnomeUIInfo container_main_menu [] = {
117 GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
118 { GNOME_APP_UI_SUBTREE, N_("_Zoom"), NULL, container_menu_zoom },
122 static GnomeUIInfo container_toolbar [] = {
123 GNOMEUIINFO_ITEM_STOCK (
124 N_("Open"), N_("Opens an existing workbook"),
125 container_open_cmd, GNOME_STOCK_PIXMAP_OPEN),
127 GNOMEUIINFO_SEPARATOR,
133 open_pdf (Container *container, const char *name)
135 GnomeObjectClient *object;
137 GNOME_PersistStream persist;
139 CORBA_Environment ev;
141 g_return_val_if_fail (container != NULL, FALSE);
142 g_return_val_if_fail (container->view_widget == NULL, FALSE);
144 comp = container_activate_component (container, "bonobo-object:image-x-pdf");
145 if (!comp || !(object = comp->server)) {
146 gnome_error_dialog (_("Could not launch bonobo object."));
150 CORBA_exception_init (&ev);
151 persist = GNOME_Unknown_query_interface (
152 gnome_object_corba_objref (GNOME_OBJECT (object)),
153 "IDL:GNOME/PersistStream:1.0", &ev);
155 if (ev._major != CORBA_NO_EXCEPTION ||
156 persist == CORBA_OBJECT_NIL) {
157 gnome_error_dialog ("Panic: component is well broken.");
161 stream = gnome_stream_fs_open (name, GNOME_Storage_READ);
163 if (stream == NULL) {
164 char *err = g_strconcat (_("Could not open "), name, NULL);
165 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
170 GNOME_PersistStream_load (persist,
171 (GNOME_Stream) gnome_object_corba_objref (GNOME_OBJECT (stream)), &ev);
175 GNOME_Unknown_unref (persist, &ev);
176 CORBA_Object_release (persist, &ev);
177 CORBA_exception_free (&ev);
182 set_ok (GtkWidget *widget, gboolean *dialog_result)
184 *dialog_result = TRUE;
189 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
196 container_open_cmd (GtkWidget *widget, Container *container)
198 GtkFileSelection *fsel;
199 gboolean accepted = FALSE;
201 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
202 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
204 gtk_window_set_transient_for (GTK_WINDOW (fsel),
205 GTK_WINDOW (container->app));
207 /* Connect the signals for Ok and Cancel */
208 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
209 GTK_SIGNAL_FUNC (set_ok), &accepted);
210 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
211 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
212 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
215 * Make sure that we quit the main loop if the window is destroyed
217 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
218 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
221 gtk_widget_show (GTK_WIDGET (fsel));
222 gtk_grab_add (GTK_WIDGET (fsel));
226 char *name = gtk_file_selection_get_filename (fsel);
228 if (name [strlen (name)-1] != '/') {
229 char *fname = g_strdup (name);
230 if (container->view_widget) /* any sort of MDI sucks :-] */
231 container = container_new (fname);
233 open_pdf (container, fname);
237 dialog = gnome_message_box_new ("Can't open a directory",
238 GNOME_MESSAGE_BOX_ERROR,
239 GNOME_STOCK_BUTTON_OK, NULL);
240 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
241 GTK_WINDOW (container->app));
242 gnome_dialog_run (GNOME_DIALOG (dialog));
246 gtk_widget_destroy (GTK_WIDGET (fsel));
250 container_destroy (Container *cont)
252 containers = g_list_remove (containers, cont);
253 gtk_widget_destroy (cont->app);
260 container_close_cmd (GtkWidget *widget, Container *cont)
262 container_destroy (cont);
266 container_exit_cmd (void)
269 container_destroy ((Container *)containers->data);
273 * Enforces the containers zoom factor.
276 zoom_set (Container *container)
278 g_return_if_fail (container != NULL);
279 g_return_if_fail (container->component != NULL);
281 gnome_view_frame_set_zoom_factor (container->component->view_frame,
286 zoom_in_cmd (GtkWidget *widget, Container *container)
288 g_return_if_fail (container != NULL);
289 if (container->zoom < 180.0) {
290 container->zoom *= 1.4;
291 zoom_set (container);
296 zoom_out_cmd (GtkWidget *widget, Container *container)
298 g_return_if_fail (container != NULL);
299 if (container->zoom > 10.0) {
300 container->zoom /= 1.4;
301 zoom_set (container);
306 component_view_activated_cb (GnomeViewFrame *view_frame, gboolean activated, gpointer data)
308 Component *component = (Component *) data;
309 Container *container = component->container;
313 * If the View is requesting to be activated, then we
314 * check whether or not there is already an active
317 if (container->active_view_frame != NULL) {
318 g_warning ("View requested to be activated but there is already "
319 "an active View!\n");
324 * Otherwise, uncover it so that it can receive
325 * events, and set it as the active View.
327 gnome_view_frame_set_covered (view_frame, FALSE);
328 container->active_view_frame = view_frame;
331 * If the View is asking to be deactivated, always
332 * oblige. We may have already deactivated it (see
333 * user_activation_request_cb), but there's no harm in
334 * doing it again. There is always the possibility
335 * that a View will ask to be deactivated when we have
336 * not told it to deactivate itself, and that is
337 * why we cover the view here.
339 gnome_view_frame_set_covered (view_frame, TRUE);
341 if (view_frame == container->active_view_frame)
342 container->active_view_frame = NULL;
347 component_user_context_cb (GnomeViewFrame *view_frame, gpointer data)
349 Component *component = (Component *) data;
354 * See if the remote GnomeEmbeddable supports any verbs at
357 l = gnome_client_site_get_verbs (component->client_site);
360 gnome_client_site_free_verbs (l);
363 * Popup the verb popup and execute the chosen verb. This
364 * function saves us the work of creating the menu, connecting
365 * the callback, and executing the verb on the remove
366 * GnomeView. We could implement all this functionality
367 * ourselves if we wanted.
369 executed_verb = gnome_view_frame_popup_verbs (view_frame);
371 g_free (executed_verb);
376 container_set_view (Container *container, Component *component)
378 GnomeViewFrame *view_frame;
379 GtkWidget *view_widget;
382 * Create the remote view and the local ViewFrame.
384 view_frame = gnome_client_site_new_view (component->client_site);
385 component->view_frame = view_frame;
388 * Set the GnomeUIHandler for this ViewFrame. That way, the
389 * embedded component can get access to our UIHandler server
390 * so that it can merge menu and toolbar items when it gets
393 gnome_view_frame_set_ui_handler (view_frame, container->uih);
396 * Embed the view frame into the application.
398 view_widget = gnome_view_frame_get_wrapper (view_frame);
399 container->view_widget = view_widget;
400 container->component = component;
403 * In-place activation of a component is a two-step process.
404 * After the user double clicks on the component, our signal
405 * callback (compoennt_user_activate_request_cb()) asks the
406 * component to activate itself (see
407 * gnome_view_frame_view_activate()). The component can then
408 * choose to either accept or refuse activation. When an
409 * embedded component notifies us of its decision to change
410 * its activation state, the "view_activated" signal is
411 * emitted from the view frame. It is at that point that we
412 * actually remove the cover so that events can get through.
414 gtk_signal_connect (GTK_OBJECT (view_frame), "view_activated",
415 GTK_SIGNAL_FUNC (component_view_activated_cb), component);
418 * The "user_context" signal is emitted when the user right
419 * clicks on the wrapper. We use it to pop up a verb menu.
421 gtk_signal_connect (GTK_OBJECT (view_frame), "user_context",
422 GTK_SIGNAL_FUNC (component_user_context_cb), component);
425 * Show the component.
427 gtk_scrolled_window_add_with_viewport (container->scroll, view_widget);
430 * Activate it ( get it to merge menus etc.
432 gnome_view_frame_view_activate (view_frame);
434 gtk_widget_show_all (GTK_WIDGET (container->scroll));
437 static GnomeObjectClient *
438 container_launch_component (GnomeClientSite *client_site,
439 GnomeContainer *container,
440 char *component_goad_id)
442 GnomeObjectClient *object_server;
445 * Launch the component.
447 object_server = gnome_object_activate_with_goad_id (
448 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
450 if (object_server == NULL)
454 * Bind it to the local ClientSite. Every embedded component
455 * has a local GnomeClientSite object which serves as a
456 * container-side point of contact for the embeddable. The
457 * container talks to the embeddable through its ClientSite
459 if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
460 gnome_object_unref (GNOME_OBJECT (object_server));
465 * The GnomeContainer object maintains a list of the
466 * ClientSites which it manages. Here we add the new
467 * ClientSite to that list.
469 gnome_container_add (container, GNOME_OBJECT (client_site));
471 return object_server;
475 * Use query_interface to see if `obj' has `interface'.
478 gnome_object_has_interface (GnomeObject *obj, char *interface)
480 CORBA_Environment ev;
481 CORBA_Object requested_interface;
483 CORBA_exception_init (&ev);
485 requested_interface = GNOME_Unknown_query_interface (
486 gnome_object_corba_objref (obj), interface, &ev);
488 CORBA_exception_free (&ev);
490 if (!CORBA_Object_is_nil(requested_interface, &ev) &&
491 ev._major == CORBA_NO_EXCEPTION)
493 /* Get rid of the interface we've been passed */
494 CORBA_Object_release (requested_interface, &ev);
503 container_activate_component (Container *container, char *component_goad_id)
505 Component *component;
506 GnomeClientSite *client_site;
507 GnomeObjectClient *server;
510 * The ClientSite is the container-side point of contact for
511 * the Embeddable. So there is a one-to-one correspondence
512 * between GnomeClientSites and GnomeEmbeddables. */
513 client_site = gnome_client_site_new (container->container);
516 * A GnomeObjectClient is a simple wrapper for a remote
517 * GnomeObject (a server supporting GNOME::Unknown).
519 server = container_launch_component (client_site, container->container,
521 if (server == NULL) {
524 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
526 gnome_warning_dialog (error_msg);
533 * Create the internal data structure which we will use to
534 * keep track of this component.
536 component = g_new0 (Component, 1);
537 component->container = container;
538 component->client_site = client_site;
539 component->server = server;
541 container_set_view (container, component);
548 container_create_menus (Container *container)
550 GnomeUIHandlerMenuItem *menu_list;
552 gnome_ui_handler_create_menubar (container->uih);
555 * Create the basic menus out of UIInfo structures.
557 menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
558 gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
559 gnome_ui_handler_menu_free_list (menu_list);
563 container_create_toolbar (Container *container)
565 GnomeUIHandlerToolbarItem *toolbar;
567 gnome_ui_handler_create_toolbar (container->uih, "pdf");
568 toolbar = gnome_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
569 gnome_ui_handler_toolbar_add_list (container->uih, "/", toolbar);
570 gnome_ui_handler_toolbar_free_list (toolbar);
574 container_new (const char *fname)
576 Container *container;
578 container = g_new0 (Container, 1);
580 container->app = gnome_app_new ("pdf-viewer",
582 container->zoom = 86.0;
584 gtk_window_set_default_size (GTK_WINDOW (container->app), 400, 400);
585 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
587 container->container = gnome_container_new ();
588 container->view_widget = NULL;
589 container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
590 gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_AUTOMATIC,
591 GTK_POLICY_AUTOMATIC);
592 gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
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));
603 container_create_menus (container);
604 container_create_toolbar (container);
606 gtk_widget_show_all (container->app);
609 if (!open_pdf (container, fname)) {
610 container_destroy (container);
614 containers = g_list_append (containers, container);
615 zoom_set (container);
617 gtk_widget_show_all (container->app);
623 main (int argc, char **argv)
625 CORBA_Environment ev;
627 char **view_files = NULL;
630 CORBA_exception_init (&ev);
632 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
634 gpdf_popt_options, 0, &ctx,
635 GNORBA_INIT_SERVER_FUNC, &ev);
637 CORBA_exception_free (&ev);
639 orb = gnome_CORBA_ORB ();
641 if (bonobo_init (orb, NULL, NULL) == FALSE)
642 g_error (_("Could not initialize Bonobo!\n"));
645 view_files = poptGetArgs (ctx);
650 for (i = 0; view_files[i]; i++)
651 container_new (view_files[i]);
654 container_new (NULL);
656 poptFreeContext (ctx);