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"
43 #include "bonobo-application-x-pdf.h"
48 const struct poptOption gpdf_popt_options [] = {
49 { "debug", '\0', POPT_ARG_INT, &gpdf_debug, 0,
50 N_("Enables some debugging functions"), N_("LEVEL") },
51 { NULL, '\0', 0, NULL, 0 }
54 typedef struct _Component Component;
55 typedef struct _Container Container;
56 /* NB. there is a 1 to 1 Container -> Component mapping, this
57 is due to how much MDI sucks; unutterably */
59 GnomeContainer *container;
62 GnomeViewFrame *active_view_frame;
65 GtkScrolledWindow *scroll;
66 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);
95 static GnomeUIInfo container_file_menu [] = {
96 GNOMEUIINFO_MENU_OPEN_ITEM (container_open_cmd, NULL),
97 GNOMEUIINFO_SEPARATOR,
98 GNOMEUIINFO_MENU_CLOSE_ITEM(container_close_cmd, NULL),
99 GNOMEUIINFO_SEPARATOR,
100 GNOMEUIINFO_MENU_EXIT_ITEM (container_exit_cmd, NULL),
104 static GnomeUIInfo container_help_menu [] = {
105 GNOMEUIINFO_MENU_ABOUT_ITEM(container_about_cmd, NULL),
109 static GnomeUIInfo container_main_menu [] = {
110 GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
111 GNOMEUIINFO_MENU_HELP_TREE (container_help_menu),
115 static GnomeUIInfo container_toolbar [] = {
116 GNOMEUIINFO_ITEM_STOCK (
117 N_("Open"), N_("Opens an existing workbook"),
118 container_open_cmd, GNOME_STOCK_PIXMAP_OPEN),
120 GNOMEUIINFO_SEPARATOR,
126 open_pdf (Container *container, const char *name)
128 GnomeObjectClient *object;
130 GNOME_PersistStream persist;
132 CORBA_Environment ev;
134 g_return_val_if_fail (container != NULL, FALSE);
135 g_return_val_if_fail (container->view_widget == NULL, FALSE);
137 comp = container_activate_component (container, "bonobo-object:application-x-pdf");
138 if (!comp || !(object = comp->server)) {
139 gnome_error_dialog (_("Could not launch bonobo object."));
143 CORBA_exception_init (&ev);
144 persist = GNOME_Unknown_query_interface (
145 gnome_object_corba_objref (GNOME_OBJECT (object)),
146 "IDL:GNOME/PersistStream:1.0", &ev);
148 if (ev._major != CORBA_NO_EXCEPTION ||
149 persist == CORBA_OBJECT_NIL) {
150 gnome_error_dialog ("Panic: component is well broken.");
154 stream = gnome_stream_fs_open (name, GNOME_Storage_READ);
156 if (stream == NULL) {
157 char *err = g_strconcat (_("Could not open "), name, NULL);
158 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
163 GNOME_PersistStream_load (persist,
164 (GNOME_Stream) gnome_object_corba_objref (GNOME_OBJECT (stream)), &ev);
166 GNOME_Unknown_unref (persist, &ev);
167 CORBA_Object_release (persist, &ev);
168 CORBA_exception_free (&ev);
170 /* gnome_view_frame_view_do_verb (comp->view_frame, "ZoomFit"); */
175 set_ok (GtkWidget *widget, gboolean *dialog_result)
177 *dialog_result = TRUE;
182 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
189 container_open_cmd (GtkWidget *widget, Container *container)
191 GtkFileSelection *fsel;
192 gboolean accepted = FALSE;
194 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
195 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
197 gtk_window_set_transient_for (GTK_WINDOW (fsel),
198 GTK_WINDOW (container->app));
200 /* Connect the signals for Ok and Cancel */
201 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
202 GTK_SIGNAL_FUNC (set_ok), &accepted);
203 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
204 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
205 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
208 * Make sure that we quit the main loop if the window is destroyed
210 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
211 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
214 gtk_widget_show (GTK_WIDGET (fsel));
215 gtk_grab_add (GTK_WIDGET (fsel));
219 char *name = gtk_file_selection_get_filename (fsel);
221 if (name [strlen (name)-1] != '/') {
222 char *fname = g_strdup (name);
223 if (container->view_widget) /* any sort of MDI sucks :-] */
224 container = container_new (fname);
226 if (!open_pdf (container, fname))
227 container_destroy (container);
232 dialog = gnome_message_box_new ("Can't open a directory",
233 GNOME_MESSAGE_BOX_ERROR,
234 GNOME_STOCK_BUTTON_OK, NULL);
235 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
236 GTK_WINDOW (container->app));
237 gnome_dialog_run (GNOME_DIALOG (dialog));
241 gtk_widget_destroy (GTK_WIDGET (fsel));
245 component_destroy (Component *component)
247 CORBA_Environment ev;
248 g_return_if_fail (component != NULL);
250 CORBA_exception_init (&ev);
252 if (component->server)
253 GNOME_Unknown_unref (
254 gnome_object_corba_objref (GNOME_OBJECT (component->server)), &ev);
255 component->server = NULL;
257 CORBA_exception_free (&ev);
261 container_destroy (Container *cont)
263 g_return_if_fail (g_list_find (containers, cont) != NULL);
265 containers = g_list_remove (containers, cont);
267 gtk_widget_destroy (cont->app);
271 component_destroy (cont->component);
272 cont->component = NULL;
281 container_close_cmd (GtkWidget *widget, Container *cont)
283 container_destroy (cont);
287 container_destroy_cb (GtkWidget *widget, GdkEvent *event, Container *cont)
289 container_destroy (cont);
294 container_exit_cmd (void)
297 container_destroy ((Container *)containers->data);
302 container_about_cmd (GtkWidget *widget, Container *container)
307 const gchar *authors[] = {
308 N_("Derek B. Noonburg, main author"),
309 N_("Michael Meeks, GNOME port maintainer."),
310 N_("Miguel de Icaza."),
316 for (i = 0; authors[i] != NULL; i++)
317 authors [i] = _(authors [i]);
320 about = gnome_about_new (_("GPDF"), VERSION,
321 _("(C) 1996-1999 Derek B. Noonburg."),
322 authors, NULL, NULL);
324 gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
325 gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
326 gtk_widget_show (about);
331 container_set_view (Container *container, Component *component)
333 GnomeViewFrame *view_frame;
334 GtkWidget *view_widget;
337 * Create the remote view and the local ViewFrame.
339 view_frame = gnome_client_site_new_view (component->client_site);
340 component->view_frame = view_frame;
343 * Set the GnomeUIHandler for this ViewFrame. That way, the
344 * embedded component can get access to our UIHandler server
345 * so that it can merge menu and toolbar items when it gets
348 gnome_view_frame_set_ui_handler (view_frame, container->uih);
351 * Embed the view frame into the application.
353 view_widget = gnome_view_frame_get_wrapper (view_frame);
354 container->view_widget = view_widget;
355 container->component = component;
358 * Show the component.
360 gtk_scrolled_window_add_with_viewport (container->scroll, view_widget);
363 * Activate it ( get it to merge menus etc. )
365 gnome_view_frame_view_activate (view_frame);
367 gtk_widget_show_all (GTK_WIDGET (container->scroll));
370 static GnomeObjectClient *
371 container_launch_component (GnomeClientSite *client_site,
372 GnomeContainer *container,
373 char *component_goad_id)
375 GnomeObjectClient *object_server;
378 * Launch the component.
380 object_server = gnome_object_activate_with_goad_id (
381 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
383 if (object_server == NULL)
387 * Bind it to the local ClientSite. Every embedded component
388 * has a local GnomeClientSite object which serves as a
389 * container-side point of contact for the embeddable. The
390 * container talks to the embeddable through its ClientSite
392 if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
393 gnome_object_unref (GNOME_OBJECT (object_server));
398 * The GnomeContainer object maintains a list of the
399 * ClientSites which it manages. Here we add the new
400 * ClientSite to that list.
402 gnome_container_add (container, GNOME_OBJECT (client_site));
404 return object_server;
409 container_activate_component (Container *container, char *component_goad_id)
411 Component *component;
412 GnomeClientSite *client_site;
413 GnomeObjectClient *server;
416 * The ClientSite is the container-side point of contact for
417 * the Embeddable. So there is a one-to-one correspondence
418 * between GnomeClientSites and GnomeEmbeddables. */
419 client_site = gnome_client_site_new (container->container);
422 * A GnomeObjectClient is a simple wrapper for a remote
423 * GnomeObject (a server supporting GNOME::Unknown).
425 server = container_launch_component (client_site, container->container,
427 if (server == NULL) {
430 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
432 gnome_warning_dialog (error_msg);
439 * Create the internal data structure which we will use to
440 * keep track of this component.
442 component = g_new0 (Component, 1);
443 component->container = container;
444 component->client_site = client_site;
445 component->server = server;
447 container_set_view (container, component);
453 filenames_dropped (GtkWidget * widget,
454 GdkDragContext *context,
457 GtkSelectionData *selection_data,
460 Container *container)
462 GList *names, *tmp_list;
464 names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
468 const char *fname = (const char *)tmp_list->data;
471 if (container->view_widget)
472 container = container_new (fname);
474 open_pdf (container, fname);
477 tmp_list = g_list_next (tmp_list);
482 * GtkWidget key_press method override
484 * Scrolls the window on keypress
487 key_press_event_cb (GtkWidget *widget, GdkEventKey *event)
489 Container *container = (Container *) gtk_object_get_data (GTK_OBJECT (widget), "container_data");
490 Component *component;
491 GtkScrolledWindow *win;
494 g_return_val_if_fail (container != NULL, FALSE);
496 win = container->scroll;
497 component = container->component;
498 if (component == NULL || win == NULL)
502 * Scrolling the view.
504 if (event->keyval == GDK_Up) {
505 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment (win);
507 if (event->state & GDK_CONTROL_MASK)
508 delta = adj->step_increment * 3;
510 delta = adj->step_increment;
512 adj->value = CLAMP (adj->value - delta,
513 adj->lower, adj->upper - adj->page_size);
515 gtk_adjustment_value_changed (adj);
517 } else if (event->keyval == GDK_Down) {
518 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment (win);
520 if (event->state & GDK_CONTROL_MASK)
521 delta = adj->step_increment * 3;
523 delta = adj->step_increment;
525 adj->value = CLAMP (adj->value + delta,
526 adj->lower, adj->upper - adj->page_size);
527 gtk_adjustment_value_changed (adj);
529 } else if (event->keyval == GDK_Left) {
530 GtkAdjustment *adj = gtk_scrolled_window_get_hadjustment (win);
532 if (event->state & GDK_CONTROL_MASK)
533 delta = adj->step_increment * 3;
535 delta = adj->step_increment;
537 adj->value = CLAMP (adj->value - delta,
538 adj->lower, adj->upper - adj->page_size);
539 gtk_adjustment_value_changed (adj);
541 } else if (event->keyval == GDK_Right) {
542 GtkAdjustment *adj = gtk_scrolled_window_get_hadjustment (win);
544 if (event->state & GDK_CONTROL_MASK)
545 delta = adj->step_increment * 3;
547 delta = adj->step_increment;
549 adj->value = CLAMP (adj->value + delta,
550 adj->lower, adj->upper - adj->page_size);
551 gtk_adjustment_value_changed (adj);
555 * Various shortcuts mapped to verbs.
558 } else if (event->keyval == GDK_Home) {
559 gnome_view_frame_view_do_verb (component->view_frame, VERB_FIRST);
561 } else if (event->keyval == GDK_End) {
562 gnome_view_frame_view_do_verb (component->view_frame, VERB_LAST);
564 } else if (event->keyval == GDK_Page_Down ||
565 event->keyval == GDK_Next) {
566 gnome_view_frame_view_do_verb (component->view_frame, VERB_NEXT);
568 } else if (event->keyval == GDK_Page_Up ||
569 event->keyval == GDK_Prior) {
570 gnome_view_frame_view_do_verb (component->view_frame, VERB_PREV);
572 } else if (event->keyval == GDK_plus ||
573 event->keyval == GDK_equal) {
574 gnome_view_frame_view_do_verb (component->view_frame, VERB_Z_IN);
575 } else if (event->keyval == GDK_underscore ||
576 event->keyval == GDK_minus) {
577 gnome_view_frame_view_do_verb (component->view_frame, VERB_Z_OUT);
584 container_create_menus (Container *container)
586 GnomeUIHandlerMenuItem *menu_list;
588 gnome_ui_handler_create_menubar (container->uih);
591 * Create the basic menus out of UIInfo structures.
593 menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
594 gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
595 gnome_ui_handler_menu_free_list (menu_list);
599 container_create_toolbar (Container *container)
601 GnomeUIHandlerToolbarItem *toolbar;
603 gnome_ui_handler_create_toolbar (container->uih, "pdf");
604 toolbar = gnome_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
605 gnome_ui_handler_toolbar_add_list (container->uih, "/pdf/", toolbar);
606 gnome_ui_handler_toolbar_free_list (toolbar);
610 container_new (const char *fname)
612 Container *container;
613 static GtkTargetEntry drag_types[] =
615 { "text/uri-list", 0, 0 },
617 static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
619 container = g_new0 (Container, 1);
621 container->app = gnome_app_new ("pdf-viewer",
624 gtk_drag_dest_set (container->app,
625 GTK_DEST_DEFAULT_ALL,
626 drag_types, n_drag_types,
629 gtk_signal_connect (GTK_OBJECT(container->app),
630 "drag_data_received",
631 GTK_SIGNAL_FUNC(filenames_dropped), (gpointer)container);
633 gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
634 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
636 container->container = gnome_container_new ();
637 container->view_widget = NULL;
638 container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
639 gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_ALWAYS,
641 gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
643 gtk_object_set_data (GTK_OBJECT (container->app), "container_data", container);
644 gtk_signal_connect (GTK_OBJECT (container->app), "key_press_event",
645 GTK_SIGNAL_FUNC (key_press_event_cb), NULL);
646 gtk_signal_connect (GTK_OBJECT (container->app), "delete_event",
647 GTK_SIGNAL_FUNC (container_destroy_cb), container);
650 * Create the GnomeUIHandler object which will be used to
651 * create the container's menus and toolbars. The UIHandler
652 * also creates a CORBA server which embedded components use
653 * to do menu/toolbar merging.
655 container->uih = gnome_ui_handler_new ();
656 gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
658 container_create_menus (container);
659 container_create_toolbar (container);
661 gtk_widget_show_all (container->app);
664 if (!open_pdf (container, fname)) {
665 container_destroy (container);
669 containers = g_list_append (containers, container);
671 gtk_widget_show_all (container->app);
677 main (int argc, char **argv)
679 CORBA_Environment ev;
681 char **view_files = NULL;
685 CORBA_exception_init (&ev);
687 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
689 gpdf_popt_options, 0, &ctx,
690 GNORBA_INIT_SERVER_FUNC, &ev);
692 CORBA_exception_free (&ev);
694 orb = gnome_CORBA_ORB ();
696 if (bonobo_init (orb, NULL, NULL) == FALSE)
697 g_error (_("Could not initialize Bonobo!\n"));
700 view_files = poptGetArgs (ctx);
706 for (i = 0; view_files[i]; i++)
707 if (container_new (view_files[i])) {
709 while (gtk_events_pending ())
710 gtk_main_iteration ();
713 if ((i == 0) || !loaded)
714 container_new (NULL);
716 poptFreeContext (ctx);