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 is well broken.");
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)
306 const gchar *authors[] = {
307 N_("Derek B. Noonburg, main author"),
308 N_("Michael Meeks, GNOME port maintainer."),
309 N_("Miguel de Icaza."),
315 for (i = 0; authors[i] != NULL; i++)
316 authors [i] = _(authors [i]);
319 about = gnome_about_new (_("GPDF"), xpdfVersion,
320 _("(C) 1996-1999 Derek B. Noonburg."),
321 authors, NULL, NULL);
323 gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
324 gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
325 gtk_widget_show (about);
330 container_set_view (Container *container, Component *component)
332 BonoboViewFrame *view_frame;
333 GtkWidget *view_widget;
336 * Create the remote view and the local ViewFrame.
338 view_frame = bonobo_client_site_new_view (component->client_site);
339 component->view_frame = view_frame;
342 * Set the BonoboUIHandler for this ViewFrame. That way, the
343 * embedded component can get access to our UIHandler server
344 * so that it can merge menu and toolbar items when it gets
347 bonobo_view_frame_set_ui_handler (view_frame, container->uih);
350 * Embed the view frame into the application.
352 view_widget = bonobo_view_frame_get_wrapper (view_frame);
353 bonobo_wrapper_set_visibility (BONOBO_WRAPPER (view_widget), FALSE);
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 bonobo_view_frame_view_activate (view_frame);
366 bonobo_view_frame_set_covered (view_frame, FALSE);
368 gtk_widget_show_all (GTK_WIDGET (container->scroll));
371 static BonoboObjectClient *
372 container_launch_component (BonoboClientSite *client_site,
373 BonoboContainer *container,
374 char *component_goad_id)
376 BonoboObjectClient *object_server;
379 * Launch the component.
381 object_server = bonobo_object_activate_with_goad_id (
382 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
384 if (object_server == NULL)
388 * Bind it to the local ClientSite. Every embedded component
389 * has a local BonoboClientSite object which serves as a
390 * container-side point of contact for the embeddable. The
391 * container talks to the embeddable through its ClientSite
393 if (!bonobo_client_site_bind_embeddable (client_site, object_server)) {
394 bonobo_object_unref (BONOBO_OBJECT (object_server));
399 * The BonoboContainer object maintains a list of the
400 * ClientSites which it manages. Here we add the new
401 * ClientSite to that list.
403 bonobo_container_add (container, BONOBO_OBJECT (client_site));
405 return object_server;
410 container_activate_component (Container *container, char *component_goad_id)
412 Component *component;
413 BonoboClientSite *client_site;
414 BonoboObjectClient *server;
417 * The ClientSite is the container-side point of contact for
418 * the Embeddable. So there is a one-to-one correspondence
419 * between BonoboClientSites and BonoboEmbeddables. */
420 client_site = bonobo_client_site_new (container->container);
423 * A BonoboObjectClient is a simple wrapper for a remote
424 * BonoboObject (a server supporting Bonobo::Unknown).
426 server = container_launch_component (client_site, container->container,
428 if (server == NULL) {
431 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
433 gnome_warning_dialog (error_msg);
440 * Create the internal data structure which we will use to
441 * keep track of this component.
443 component = g_new0 (Component, 1);
444 component->container = container;
445 component->client_site = client_site;
446 component->server = server;
448 container_set_view (container, component);
454 filenames_dropped (GtkWidget * widget,
455 GdkDragContext *context,
458 GtkSelectionData *selection_data,
461 Container *container)
463 GList *names, *tmp_list;
465 names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
469 const char *fname = (const char *)tmp_list->data;
472 if (container->view_widget)
473 container = container_new (fname);
475 open_pdf (container, fname);
478 tmp_list = g_list_next (tmp_list);
483 * GtkWidget key_press method override
485 * Scrolls the window on keypress
488 key_press_event_cb (GtkWidget *widget, GdkEventKey *event)
490 Container *container = (Container *) gtk_object_get_data (GTK_OBJECT (widget), "container_data");
491 Component *component;
492 GtkScrolledWindow *win;
495 g_return_val_if_fail (container != NULL, FALSE);
497 win = container->scroll;
498 component = container->component;
499 if (component == NULL || win == NULL)
503 * Scrolling the view.
505 if (event->keyval == GDK_Up) {
506 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment (win);
508 if (event->state & GDK_CONTROL_MASK)
509 delta = adj->step_increment * 3;
511 delta = adj->step_increment;
513 adj->value = CLAMP (adj->value - delta,
514 adj->lower, adj->upper - adj->page_size);
516 gtk_adjustment_value_changed (adj);
518 } else if (event->keyval == GDK_Down) {
519 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment (win);
521 if (event->state & GDK_CONTROL_MASK)
522 delta = adj->step_increment * 3;
524 delta = adj->step_increment;
526 adj->value = CLAMP (adj->value + delta,
527 adj->lower, adj->upper - adj->page_size);
528 gtk_adjustment_value_changed (adj);
530 } else if (event->keyval == GDK_Left) {
531 GtkAdjustment *adj = gtk_scrolled_window_get_hadjustment (win);
533 if (event->state & GDK_CONTROL_MASK)
534 delta = adj->step_increment * 3;
536 delta = adj->step_increment;
538 adj->value = CLAMP (adj->value - delta,
539 adj->lower, adj->upper - adj->page_size);
540 gtk_adjustment_value_changed (adj);
542 } else if (event->keyval == GDK_Right) {
543 GtkAdjustment *adj = gtk_scrolled_window_get_hadjustment (win);
545 if (event->state & GDK_CONTROL_MASK)
546 delta = adj->step_increment * 3;
548 delta = adj->step_increment;
550 adj->value = CLAMP (adj->value + delta,
551 adj->lower, adj->upper - adj->page_size);
552 gtk_adjustment_value_changed (adj);
556 * Various shortcuts mapped to verbs.
559 } else if (event->keyval == GDK_Home) {
560 bonobo_view_frame_view_do_verb (component->view_frame, VERB_FIRST);
562 } else if (event->keyval == GDK_End) {
563 bonobo_view_frame_view_do_verb (component->view_frame, VERB_LAST);
565 } else if (event->keyval == GDK_Page_Down ||
566 event->keyval == GDK_Next) {
567 bonobo_view_frame_view_do_verb (component->view_frame, VERB_NEXT);
569 } else if (event->keyval == GDK_Page_Up ||
570 event->keyval == GDK_Prior) {
571 bonobo_view_frame_view_do_verb (component->view_frame, VERB_PREV);
573 } else if (event->keyval == GDK_plus ||
574 event->keyval == GDK_equal) {
575 bonobo_view_frame_view_do_verb (component->view_frame, VERB_Z_IN);
576 } else if (event->keyval == GDK_underscore ||
577 event->keyval == GDK_minus) {
578 bonobo_view_frame_view_do_verb (component->view_frame, VERB_Z_OUT);
585 container_create_menus (Container *container)
587 BonoboUIHandlerMenuItem *menu_list;
589 bonobo_ui_handler_create_menubar (container->uih);
592 * Create the basic menus out of UIInfo structures.
594 menu_list = bonobo_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
595 bonobo_ui_handler_menu_add_list (container->uih, "/", menu_list);
596 bonobo_ui_handler_menu_free_list (menu_list);
600 container_create_toolbar (Container *container)
602 BonoboUIHandlerToolbarItem *toolbar;
604 bonobo_ui_handler_create_toolbar (container->uih, "pdf");
605 toolbar = bonobo_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
606 bonobo_ui_handler_toolbar_add_list (container->uih, "/pdf/", toolbar);
607 bonobo_ui_handler_toolbar_free_list (toolbar);
611 container_new (const char *fname)
613 Container *container;
614 static GtkTargetEntry drag_types[] =
616 { "text/uri-list", 0, 0 },
618 static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
620 container = g_new0 (Container, 1);
622 container->app = gnome_app_new ("pdf-viewer",
625 gtk_drag_dest_set (container->app,
626 GTK_DEST_DEFAULT_ALL,
627 drag_types, n_drag_types,
630 gtk_signal_connect (GTK_OBJECT(container->app),
631 "drag_data_received",
632 GTK_SIGNAL_FUNC(filenames_dropped),
633 (gpointer)container);
635 gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
636 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
638 container->container = bonobo_container_new ();
639 container->view_widget = NULL;
640 container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
641 gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_ALWAYS,
643 gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
645 gtk_object_set_data (GTK_OBJECT (container->app), "container_data", container);
646 gtk_signal_connect (GTK_OBJECT (container->app), "key_press_event",
647 GTK_SIGNAL_FUNC (key_press_event_cb), NULL);
648 gtk_signal_connect (GTK_OBJECT (container->app), "delete_event",
649 GTK_SIGNAL_FUNC (container_destroy_cb), container);
652 * Create the BonoboUIHandler object which will be used to
653 * create the container's menus and toolbars. The UIHandler
654 * also creates a CORBA server which embedded components use
655 * to do menu/toolbar merging.
657 container->uih = bonobo_ui_handler_new ();
658 bonobo_ui_handler_set_app (container->uih, GNOME_APP (container->app));
660 container_create_menus (container);
661 container_create_toolbar (container);
663 gtk_widget_show_all (container->app);
665 containers = g_list_append (containers, container);
668 if (!open_pdf (container, fname)) {
669 container_destroy (container);
673 gtk_widget_show_all (container->app);
679 main (int argc, char **argv)
681 CORBA_Environment ev;
683 const char **view_files = NULL;
687 CORBA_exception_init (&ev);
689 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
691 gpdf_popt_options, 0, &ctx,
692 GNORBA_INIT_SERVER_FUNC, &ev);
694 CORBA_exception_free (&ev);
696 orb = gnome_CORBA_ORB ();
698 if (bonobo_init (orb, NULL, NULL) == FALSE)
699 g_error (_("Could not initialize Bonobo!\n"));
702 view_files = poptGetArgs (ctx);
708 for (i = 0; view_files[i]; i++)
709 if (container_new (view_files[i])) {
711 while (gtk_events_pending ())
712 gtk_main_iteration ();
715 if ((i == 0) || !loaded)
716 container_new (NULL);
718 poptFreeContext (ctx);