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>
22 #include "bonobo-application-x-pdf.h"
27 const struct poptOption gpdf_popt_options [] = {
28 { "debug", '\0', POPT_ARG_INT, &gpdf_debug, 0,
29 N_("Enables some debugging functions"), N_("LEVEL") },
30 { NULL, '\0', 0, NULL, 0 }
33 typedef struct _Component Component;
34 typedef struct _Container Container;
35 /* NB. there is a 1 to 1 Container -> Component mapping, this
36 is due to how much MDI sucks; unutterably */
38 BonoboContainer *container;
42 GtkScrolledWindow *scroll;
43 GtkWidget *view_widget;
50 BonoboClientSite *client_site;
51 BonoboViewFrame *view_frame;
52 BonoboObjectClient *server;
55 GList *containers = NULL;
60 static Container *container_new (const char *fname);
61 static void container_destroy (Container *cont);
62 static void container_open_cmd (GtkWidget *widget, Container *container);
63 static void container_close_cmd (GtkWidget *widget, Container *container);
64 static void container_exit_cmd (void);
65 static void container_about_cmd (GtkWidget *widget, Container *container);
66 static Component *container_activate_component (Container *container, char *component_goad_id);
72 static GnomeUIInfo container_file_menu [] = {
73 GNOMEUIINFO_MENU_OPEN_ITEM (container_open_cmd, NULL),
74 GNOMEUIINFO_SEPARATOR,
75 GNOMEUIINFO_MENU_CLOSE_ITEM(container_close_cmd, NULL),
76 GNOMEUIINFO_SEPARATOR,
77 GNOMEUIINFO_MENU_EXIT_ITEM (container_exit_cmd, NULL),
81 static GnomeUIInfo container_help_menu [] = {
82 GNOMEUIINFO_MENU_ABOUT_ITEM(container_about_cmd, NULL),
86 static GnomeUIInfo container_main_menu [] = {
87 GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
88 GNOMEUIINFO_MENU_HELP_TREE (container_help_menu),
92 static GnomeUIInfo container_toolbar [] = {
93 GNOMEUIINFO_ITEM_STOCK (
94 N_("Open"), N_("Opens an existing workbook"),
95 container_open_cmd, GNOME_STOCK_PIXMAP_OPEN),
97 GNOMEUIINFO_SEPARATOR,
103 open_pdf (Container *container, const char *name)
105 BonoboObjectClient *object;
106 BonoboStream *stream;
107 Bonobo_PersistStream persist;
109 CORBA_Environment ev;
111 g_return_val_if_fail (container != NULL, FALSE);
112 g_return_val_if_fail (container->view_widget == NULL, FALSE);
114 comp = container_activate_component (container, "bonobo-object:application-x-pdf");
115 if (!comp || !(object = comp->server)) {
116 gnome_error_dialog (_("Could not launch bonobo object."));
120 CORBA_exception_init (&ev);
121 persist = Bonobo_Unknown_query_interface (
122 bonobo_object_corba_objref (BONOBO_OBJECT (object)),
123 "IDL:GNOME/PersistStream:1.0", &ev);
125 if (ev._major != CORBA_NO_EXCEPTION ||
126 persist == CORBA_OBJECT_NIL) {
127 gnome_error_dialog ("Panic: component doesn't implement PersistStream.");
131 stream = bonobo_stream_fs_open (name, Bonobo_Storage_READ);
133 if (stream == NULL) {
134 char *err = g_strconcat (_("Could not open "), name, NULL);
135 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
140 Bonobo_PersistStream_load (persist,
141 (Bonobo_Stream) bonobo_object_corba_objref (BONOBO_OBJECT (stream)), &ev);
143 Bonobo_Unknown_unref (persist, &ev);
144 CORBA_Object_release (persist, &ev);
145 CORBA_exception_free (&ev);
147 /* bonobo_view_frame_view_do_verb (comp->view_frame, "ZoomFit"); */
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 if (!open_pdf (container, fname))
204 container_destroy (container);
209 dialog = gnome_message_box_new ("Can't open a directory",
210 GNOME_MESSAGE_BOX_ERROR,
211 GNOME_STOCK_BUTTON_OK, NULL);
212 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
213 GTK_WINDOW (container->app));
214 gnome_dialog_run (GNOME_DIALOG (dialog));
218 gtk_widget_destroy (GTK_WIDGET (fsel));
222 component_destroy (Component *component)
224 CORBA_Environment ev;
225 Container *container;
226 g_return_if_fail (component != NULL);
228 CORBA_exception_init (&ev);
230 /* Kill merged menus et al. */
231 bonobo_view_frame_view_deactivate (component->view_frame);
233 container = component->container;
234 gtk_widget_destroy (container->view_widget);
235 container->view_widget = NULL;
237 if (component->server)
238 Bonobo_Unknown_unref (
239 bonobo_object_corba_objref (BONOBO_OBJECT (component->server)), &ev);
240 component->server = NULL;
242 CORBA_exception_free (&ev);
248 container_destroy (Container *cont)
250 g_return_if_fail (g_list_find (containers, cont) != NULL);
252 containers = g_list_remove (containers, cont);
254 gtk_widget_destroy (cont->app);
258 component_destroy (cont->component);
259 cont->component = NULL;
268 container_close (Container *cont)
270 g_return_if_fail (g_list_find (containers, cont) != NULL);
272 if (cont->component) {
273 component_destroy (cont->component);
274 cont->component = NULL;
276 container_destroy (cont);
281 container_close_cmd (GtkWidget *widget, Container *cont)
283 container_close (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);
301 container_about_cmd (GtkWidget *widget, Container *container)
305 const gchar *authors[] = {
306 N_("Derek B. Noonburg, main author"),
307 N_("Michael Meeks, GNOME port maintainer."),
308 N_("Miguel de Icaza."),
316 for (i = 0; authors[i] != NULL; i++)
317 authors [i] = _(authors [i]);
320 about = gnome_about_new (_("GPDF"), xpdfVersion,
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 BonoboViewFrame *view_frame;
334 GtkWidget *view_widget;
337 * Create the remote view and the local ViewFrame.
339 view_frame = bonobo_client_site_new_view (component->client_site,
340 bonobo_object_corba_objref (BONOBO_OBJECT (
342 component->view_frame = view_frame;
345 * Embed the view frame into the application.
347 view_widget = bonobo_view_frame_get_wrapper (view_frame);
348 bonobo_wrapper_set_visibility (BONOBO_WRAPPER (view_widget), FALSE);
349 container->view_widget = view_widget;
350 container->component = component;
353 * Show the component.
355 gtk_scrolled_window_add_with_viewport (container->scroll, view_widget);
358 * Activate it ( get it to merge menus etc. )
360 bonobo_view_frame_view_activate (view_frame);
361 bonobo_view_frame_set_covered (view_frame, FALSE);
363 gtk_widget_show_all (GTK_WIDGET (container->scroll));
366 static BonoboObjectClient *
367 container_launch_component (BonoboClientSite *client_site,
368 BonoboContainer *container,
369 char *component_goad_id)
371 BonoboObjectClient *object_server;
374 * Launch the component.
376 object_server = bonobo_object_activate_with_goad_id (
377 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
379 if (object_server == NULL)
383 * Bind it to the local ClientSite. Every embedded component
384 * has a local BonoboClientSite object which serves as a
385 * container-side point of contact for the embeddable. The
386 * container talks to the embeddable through its ClientSite
388 if (!bonobo_client_site_bind_embeddable (client_site, object_server)) {
389 bonobo_object_unref (BONOBO_OBJECT (object_server));
394 * The BonoboContainer object maintains a list of the
395 * ClientSites which it manages. Here we add the new
396 * ClientSite to that list.
398 bonobo_container_add (container, BONOBO_OBJECT (client_site));
400 return object_server;
405 container_activate_component (Container *container, char *component_goad_id)
407 Component *component;
408 BonoboClientSite *client_site;
409 BonoboObjectClient *server;
412 * The ClientSite is the container-side point of contact for
413 * the Embeddable. So there is a one-to-one correspondence
414 * between BonoboClientSites and BonoboEmbeddables. */
415 client_site = bonobo_client_site_new (container->container);
418 * A BonoboObjectClient is a simple wrapper for a remote
419 * BonoboObject (a server supporting Bonobo::Unknown).
421 server = container_launch_component (client_site, container->container,
423 if (server == NULL) {
426 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
428 gnome_warning_dialog (error_msg);
435 * Create the internal data structure which we will use to
436 * keep track of this component.
438 component = g_new0 (Component, 1);
439 component->container = container;
440 component->client_site = client_site;
441 component->server = server;
443 container_set_view (container, component);
449 filenames_dropped (GtkWidget * widget,
450 GdkDragContext *context,
453 GtkSelectionData *selection_data,
456 Container *container)
458 GList *names, *tmp_list;
460 names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
464 const char *fname = (const char *)tmp_list->data;
467 if (container->view_widget)
468 container = container_new (fname);
470 open_pdf (container, fname);
473 tmp_list = g_list_next (tmp_list);
478 * GtkWidget key_press method override
480 * Scrolls the window on keypress
483 key_press_event_cb (GtkWidget *widget, GdkEventKey *event)
485 Container *container = (Container *) gtk_object_get_data (GTK_OBJECT (widget), "container_data");
486 Component *component;
487 GtkScrolledWindow *win;
490 g_return_val_if_fail (container != NULL, FALSE);
492 win = container->scroll;
493 component = container->component;
494 if (component == NULL || win == NULL)
498 * Scrolling the view.
500 if (event->keyval == GDK_Up) {
501 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment (win);
503 if (event->state & GDK_CONTROL_MASK)
504 delta = adj->step_increment * 3;
506 delta = adj->step_increment;
508 adj->value = CLAMP (adj->value - delta,
509 adj->lower, adj->upper - adj->page_size);
511 gtk_adjustment_value_changed (adj);
513 } else if (event->keyval == GDK_Down) {
514 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment (win);
516 if (event->state & GDK_CONTROL_MASK)
517 delta = adj->step_increment * 3;
519 delta = adj->step_increment;
521 adj->value = CLAMP (adj->value + delta,
522 adj->lower, adj->upper - adj->page_size);
523 gtk_adjustment_value_changed (adj);
525 } else if (event->keyval == GDK_Left) {
526 GtkAdjustment *adj = gtk_scrolled_window_get_hadjustment (win);
528 if (event->state & GDK_CONTROL_MASK)
529 delta = adj->step_increment * 3;
531 delta = adj->step_increment;
533 adj->value = CLAMP (adj->value - delta,
534 adj->lower, adj->upper - adj->page_size);
535 gtk_adjustment_value_changed (adj);
537 } else if (event->keyval == GDK_Right) {
538 GtkAdjustment *adj = gtk_scrolled_window_get_hadjustment (win);
540 if (event->state & GDK_CONTROL_MASK)
541 delta = adj->step_increment * 3;
543 delta = adj->step_increment;
545 adj->value = CLAMP (adj->value + delta,
546 adj->lower, adj->upper - adj->page_size);
547 gtk_adjustment_value_changed (adj);
551 * Various shortcuts mapped to verbs.
554 } else if (event->keyval == GDK_Home) {
555 bonobo_view_frame_view_do_verb (component->view_frame, VERB_FIRST);
557 } else if (event->keyval == GDK_End) {
558 bonobo_view_frame_view_do_verb (component->view_frame, VERB_LAST);
560 } else if (event->keyval == GDK_Page_Down ||
561 event->keyval == GDK_Next) {
562 bonobo_view_frame_view_do_verb (component->view_frame, VERB_NEXT);
564 } else if (event->keyval == GDK_Page_Up ||
565 event->keyval == GDK_Prior) {
566 bonobo_view_frame_view_do_verb (component->view_frame, VERB_PREV);
568 } else if (event->keyval == GDK_plus ||
569 event->keyval == GDK_equal) {
570 bonobo_view_frame_view_do_verb (component->view_frame, VERB_Z_IN);
571 } else if (event->keyval == GDK_underscore ||
572 event->keyval == GDK_minus) {
573 bonobo_view_frame_view_do_verb (component->view_frame, VERB_Z_OUT);
580 container_create_menus (Container *container)
582 BonoboUIHandlerMenuItem *menu_list;
584 bonobo_ui_handler_create_menubar (container->uih);
587 * Create the basic menus out of UIInfo structures.
589 menu_list = bonobo_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
590 bonobo_ui_handler_menu_add_list (container->uih, "/", menu_list);
591 bonobo_ui_handler_menu_free_list (menu_list);
595 container_create_toolbar (Container *container)
597 BonoboUIHandlerToolbarItem *toolbar;
599 bonobo_ui_handler_create_toolbar (container->uih, "pdf");
600 toolbar = bonobo_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
601 bonobo_ui_handler_toolbar_add_list (container->uih, "/pdf/", toolbar);
602 bonobo_ui_handler_toolbar_free_list (toolbar);
606 container_new (const char *fname)
608 Container *container;
609 static GtkTargetEntry drag_types[] =
611 { "text/uri-list", 0, 0 },
613 static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
615 container = g_new0 (Container, 1);
617 container->app = gnome_app_new ("pdf-viewer",
620 gtk_drag_dest_set (container->app,
621 GTK_DEST_DEFAULT_ALL,
622 drag_types, n_drag_types,
625 gtk_signal_connect (GTK_OBJECT(container->app),
626 "drag_data_received",
627 GTK_SIGNAL_FUNC(filenames_dropped),
628 (gpointer)container);
630 gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
631 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
633 container->container = bonobo_container_new ();
634 container->view_widget = NULL;
635 container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
636 gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_ALWAYS,
638 gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
640 gtk_object_set_data (GTK_OBJECT (container->app), "container_data", container);
641 gtk_signal_connect (GTK_OBJECT (container->app), "key_press_event",
642 GTK_SIGNAL_FUNC (key_press_event_cb), NULL);
643 gtk_signal_connect (GTK_OBJECT (container->app), "delete_event",
644 GTK_SIGNAL_FUNC (container_destroy_cb), container);
647 * Create the BonoboUIHandler object which will be used to
648 * create the container's menus and toolbars. The UIHandler
649 * also creates a CORBA server which embedded components use
650 * to do menu/toolbar merging.
652 container->uih = bonobo_ui_handler_new ();
653 bonobo_ui_handler_set_app (container->uih, GNOME_APP (container->app));
655 container_create_menus (container);
656 container_create_toolbar (container);
658 gtk_widget_show_all (container->app);
660 containers = g_list_append (containers, container);
663 if (!open_pdf (container, fname)) {
664 container_destroy (container);
668 gtk_widget_show_all (container->app);
674 main (int argc, char **argv)
676 CORBA_Environment ev;
678 const char **view_files = NULL;
682 CORBA_exception_init (&ev);
684 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
686 gpdf_popt_options, 0, &ctx,
687 GNORBA_INIT_SERVER_FUNC, &ev);
689 CORBA_exception_free (&ev);
691 orb = gnome_CORBA_ORB ();
693 if (bonobo_init (orb, NULL, NULL) == FALSE)
694 g_error (_("Could not initialize Bonobo!\n"));
697 view_files = poptGetArgs (ctx);
703 for (i = 0; view_files[i]; i++)
704 if (container_new (view_files[i])) {
706 while (gtk_events_pending ())
707 gtk_main_iteration ();
710 if ((i == 0) || !loaded)
711 container_new (NULL);
713 poptFreeContext (ctx);