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 void container_about_cmd (GtkWidget *widget, Container *container);
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_help_menu [] = {
118 GNOMEUIINFO_MENU_ABOUT_ITEM(container_about_cmd, NULL),
122 static GnomeUIInfo container_main_menu [] = {
123 GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
124 { GNOME_APP_UI_SUBTREE, N_("_Zoom"), NULL, container_menu_zoom },
125 GNOMEUIINFO_MENU_HELP_TREE (container_help_menu),
129 static GnomeUIInfo container_toolbar [] = {
130 GNOMEUIINFO_ITEM_STOCK (
131 N_("Open"), N_("Opens an existing workbook"),
132 container_open_cmd, GNOME_STOCK_PIXMAP_OPEN),
134 GNOMEUIINFO_SEPARATOR,
140 open_pdf (Container *container, const char *name)
142 GnomeObjectClient *object;
144 GNOME_PersistStream persist;
146 CORBA_Environment ev;
148 g_return_val_if_fail (container != NULL, FALSE);
149 g_return_val_if_fail (container->view_widget == NULL, FALSE);
151 comp = container_activate_component (container, "bonobo-object:application-x-pdf");
152 if (!comp || !(object = comp->server)) {
153 gnome_error_dialog (_("Could not launch bonobo object."));
157 CORBA_exception_init (&ev);
158 persist = GNOME_Unknown_query_interface (
159 gnome_object_corba_objref (GNOME_OBJECT (object)),
160 "IDL:GNOME/PersistStream:1.0", &ev);
162 if (ev._major != CORBA_NO_EXCEPTION ||
163 persist == CORBA_OBJECT_NIL) {
164 gnome_error_dialog ("Panic: component is well broken.");
168 stream = gnome_stream_fs_open (name, GNOME_Storage_READ);
170 if (stream == NULL) {
171 char *err = g_strconcat (_("Could not open "), name, NULL);
172 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
177 GNOME_PersistStream_load (persist,
178 (GNOME_Stream) gnome_object_corba_objref (GNOME_OBJECT (stream)), &ev);
180 zoom_set (container);
182 GNOME_Unknown_unref (persist, &ev);
183 CORBA_Object_release (persist, &ev);
184 CORBA_exception_free (&ev);
189 set_ok (GtkWidget *widget, gboolean *dialog_result)
191 *dialog_result = TRUE;
196 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
203 container_open_cmd (GtkWidget *widget, Container *container)
205 GtkFileSelection *fsel;
206 gboolean accepted = FALSE;
208 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
209 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
211 gtk_window_set_transient_for (GTK_WINDOW (fsel),
212 GTK_WINDOW (container->app));
214 /* Connect the signals for Ok and Cancel */
215 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
216 GTK_SIGNAL_FUNC (set_ok), &accepted);
217 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
218 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
219 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
222 * Make sure that we quit the main loop if the window is destroyed
224 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
225 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
228 gtk_widget_show (GTK_WIDGET (fsel));
229 gtk_grab_add (GTK_WIDGET (fsel));
233 char *name = gtk_file_selection_get_filename (fsel);
235 if (name [strlen (name)-1] != '/') {
236 char *fname = g_strdup (name);
237 if (container->view_widget) /* any sort of MDI sucks :-] */
238 container = container_new (fname);
240 if (!open_pdf (container, fname))
241 container_destroy (container);
246 dialog = gnome_message_box_new ("Can't open a directory",
247 GNOME_MESSAGE_BOX_ERROR,
248 GNOME_STOCK_BUTTON_OK, NULL);
249 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
250 GTK_WINDOW (container->app));
251 gnome_dialog_run (GNOME_DIALOG (dialog));
255 gtk_widget_destroy (GTK_WIDGET (fsel));
259 component_destroy (Component *component)
261 CORBA_Environment ev;
262 g_return_if_fail (component != NULL);
264 CORBA_exception_init (&ev);
266 if (component->server)
267 GNOME_Unknown_unref (
268 gnome_object_corba_objref (GNOME_OBJECT (component->server)), &ev);
269 component->server = NULL;
271 CORBA_exception_free (&ev);
275 container_destroy (Container *cont)
277 containers = g_list_remove (containers, cont);
279 gtk_widget_destroy (cont->app);
282 component_destroy (cont->component);
283 cont->component = NULL;
293 container_close_cmd (GtkWidget *widget, Container *cont)
295 container_destroy (cont);
299 container_exit_cmd (void)
302 container_destroy ((Container *)containers->data);
307 container_about_cmd (GtkWidget *widget, Container *container)
312 const gchar *authors[] = {
313 N_("Michael Meeks, GNOME port maintainer."),
314 N_("Miguel de Icaza."),
320 for (i = 0; authors[i] != NULL; i++)
321 authors [i] = _(authors [i]);
324 about = gnome_about_new (_("GPDF"), VERSION,
325 _("(C) 1996-1999 Derek B. Noonburg."),
326 authors, NULL, NULL);
328 gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
329 gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
330 gtk_widget_show (about);
334 * Enforces the containers zoom factor.
337 zoom_set (Container *container)
339 g_return_if_fail (container != NULL);
340 g_return_if_fail (container->component != NULL);
342 gnome_view_frame_set_zoom_factor (container->component->view_frame,
347 zoom_in_cmd (GtkWidget *widget, Container *container)
349 g_return_if_fail (container != NULL);
350 if (container->zoom < 180.0) {
351 container->zoom *= 1.4;
352 zoom_set (container);
357 zoom_out_cmd (GtkWidget *widget, Container *container)
359 g_return_if_fail (container != NULL);
360 if (container->zoom > 10.0) {
361 container->zoom /= 1.4;
362 zoom_set (container);
368 container_set_view (Container *container, Component *component)
370 GnomeViewFrame *view_frame;
371 GtkWidget *view_widget;
374 * Create the remote view and the local ViewFrame.
376 view_frame = gnome_client_site_new_view (component->client_site);
377 component->view_frame = view_frame;
380 * Set the GnomeUIHandler for this ViewFrame. That way, the
381 * embedded component can get access to our UIHandler server
382 * so that it can merge menu and toolbar items when it gets
385 gnome_view_frame_set_ui_handler (view_frame, container->uih);
388 * Embed the view frame into the application.
390 view_widget = gnome_view_frame_get_wrapper (view_frame);
391 container->view_widget = view_widget;
392 container->component = component;
395 * Show the component.
397 gtk_scrolled_window_add_with_viewport (container->scroll, view_widget);
400 * Activate it ( get it to merge menus etc. )
402 gnome_view_frame_view_activate (view_frame);
404 gtk_widget_show_all (GTK_WIDGET (container->scroll));
407 static GnomeObjectClient *
408 container_launch_component (GnomeClientSite *client_site,
409 GnomeContainer *container,
410 char *component_goad_id)
412 GnomeObjectClient *object_server;
415 * Launch the component.
417 object_server = gnome_object_activate_with_goad_id (
418 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
420 if (object_server == NULL)
424 * Bind it to the local ClientSite. Every embedded component
425 * has a local GnomeClientSite object which serves as a
426 * container-side point of contact for the embeddable. The
427 * container talks to the embeddable through its ClientSite
429 if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
430 gnome_object_unref (GNOME_OBJECT (object_server));
435 * The GnomeContainer object maintains a list of the
436 * ClientSites which it manages. Here we add the new
437 * ClientSite to that list.
439 gnome_container_add (container, GNOME_OBJECT (client_site));
441 return object_server;
445 * Use query_interface to see if `obj' has `interface'.
448 gnome_object_has_interface (GnomeObject *obj, char *interface)
450 CORBA_Environment ev;
451 CORBA_Object requested_interface;
453 CORBA_exception_init (&ev);
455 requested_interface = GNOME_Unknown_query_interface (
456 gnome_object_corba_objref (obj), interface, &ev);
458 CORBA_exception_free (&ev);
460 if (!CORBA_Object_is_nil(requested_interface, &ev) &&
461 ev._major == CORBA_NO_EXCEPTION)
463 /* Get rid of the interface we've been passed */
464 CORBA_Object_release (requested_interface, &ev);
473 container_activate_component (Container *container, char *component_goad_id)
475 Component *component;
476 GnomeClientSite *client_site;
477 GnomeObjectClient *server;
480 * The ClientSite is the container-side point of contact for
481 * the Embeddable. So there is a one-to-one correspondence
482 * between GnomeClientSites and GnomeEmbeddables. */
483 client_site = gnome_client_site_new (container->container);
486 * A GnomeObjectClient is a simple wrapper for a remote
487 * GnomeObject (a server supporting GNOME::Unknown).
489 server = container_launch_component (client_site, container->container,
491 if (server == NULL) {
494 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
496 gnome_warning_dialog (error_msg);
503 * Create the internal data structure which we will use to
504 * keep track of this component.
506 component = g_new0 (Component, 1);
507 component->container = container;
508 component->client_site = client_site;
509 component->server = server;
511 container_set_view (container, component);
517 filenames_dropped (GtkWidget * widget,
518 GdkDragContext *context,
521 GtkSelectionData *selection_data,
524 Container *container)
526 GList *names, *tmp_list;
528 names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
532 const char *fname = (const char *)tmp_list->data;
535 if (container->view_widget)
536 container = container_new (fname);
538 open_pdf (container, fname);
541 tmp_list = g_list_next (tmp_list);
547 container_create_menus (Container *container)
549 GnomeUIHandlerMenuItem *menu_list;
551 gnome_ui_handler_create_menubar (container->uih);
554 * Create the basic menus out of UIInfo structures.
556 menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
557 gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
558 gnome_ui_handler_menu_free_list (menu_list);
562 container_create_toolbar (Container *container)
564 GnomeUIHandlerToolbarItem *toolbar;
566 gnome_ui_handler_create_toolbar (container->uih, "pdf");
567 toolbar = gnome_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
568 gnome_ui_handler_toolbar_add_list (container->uih, "/", toolbar);
569 gnome_ui_handler_toolbar_free_list (toolbar);
573 container_new (const char *fname)
575 Container *container;
576 static GtkTargetEntry drag_types[] =
578 { "text/uri-list", 0, 0 },
580 static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
582 container = g_new0 (Container, 1);
584 container->app = gnome_app_new ("pdf-viewer",
586 container->zoom = 86.0;
588 gtk_drag_dest_set (container->app,
589 GTK_DEST_DEFAULT_ALL,
590 drag_types, n_drag_types,
593 gtk_signal_connect (GTK_OBJECT(container->app),
594 "drag_data_received",
595 GTK_SIGNAL_FUNC(filenames_dropped), (gpointer)container);
597 gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
598 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
600 container->container = gnome_container_new ();
601 container->view_widget = NULL;
602 container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
603 gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_AUTOMATIC,
604 GTK_POLICY_AUTOMATIC);
605 gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
608 * Create the GnomeUIHandler object which will be used to
609 * create the container's menus and toolbars. The UIHandler
610 * also creates a CORBA server which embedded components use
611 * to do menu/toolbar merging.
613 container->uih = gnome_ui_handler_new ();
614 gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
616 container_create_menus (container);
617 container_create_toolbar (container);
619 gtk_widget_show_all (container->app);
622 if (!open_pdf (container, fname)) {
623 container_destroy (container);
627 containers = g_list_append (containers, container);
629 gtk_widget_show_all (container->app);
635 main (int argc, char **argv)
637 CORBA_Environment ev;
639 char **view_files = NULL;
643 CORBA_exception_init (&ev);
645 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
647 gpdf_popt_options, 0, &ctx,
648 GNORBA_INIT_SERVER_FUNC, &ev);
650 CORBA_exception_free (&ev);
652 orb = gnome_CORBA_ORB ();
654 if (bonobo_init (orb, NULL, NULL) == FALSE)
655 g_error (_("Could not initialize Bonobo!\n"));
658 view_files = poptGetArgs (ctx);
664 for (i = 0; view_files[i]; i++)
665 if (container_new (view_files[i]))
668 if ((i == 0) || !loaded)
669 container_new (NULL);
671 poptFreeContext (ctx);