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;
72 GnomeClientSite *client_site;
73 GnomeViewFrame *view_frame;
74 GnomeObjectClient *server;
77 GList *containers = NULL;
82 static Container *container_new (const char *fname);
83 static void container_destroy (Container *cont);
84 static void container_open_cmd (GtkWidget *widget, Container *container);
85 static void container_close_cmd (GtkWidget *widget, Container *container);
86 static void container_exit_cmd (void);
87 static void container_about_cmd (GtkWidget *widget, Container *container);
88 static Component *container_activate_component (Container *container, char *component_goad_id);
94 static GnomeUIInfo container_file_menu [] = {
95 GNOMEUIINFO_MENU_OPEN_ITEM (container_open_cmd, NULL),
96 GNOMEUIINFO_SEPARATOR,
97 GNOMEUIINFO_MENU_CLOSE_ITEM(container_close_cmd, NULL),
98 GNOMEUIINFO_SEPARATOR,
99 GNOMEUIINFO_MENU_EXIT_ITEM (container_exit_cmd, NULL),
103 static GnomeUIInfo container_help_menu [] = {
104 GNOMEUIINFO_MENU_ABOUT_ITEM(container_about_cmd, NULL),
108 static GnomeUIInfo container_main_menu [] = {
109 GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
110 GNOMEUIINFO_MENU_HELP_TREE (container_help_menu),
114 static GnomeUIInfo container_toolbar [] = {
115 GNOMEUIINFO_ITEM_STOCK (
116 N_("Open"), N_("Opens an existing workbook"),
117 container_open_cmd, GNOME_STOCK_PIXMAP_OPEN),
119 GNOMEUIINFO_SEPARATOR,
125 open_pdf (Container *container, const char *name)
127 GnomeObjectClient *object;
129 GNOME_PersistStream persist;
131 CORBA_Environment ev;
133 g_return_val_if_fail (container != NULL, FALSE);
134 g_return_val_if_fail (container->view_widget == NULL, FALSE);
136 comp = container_activate_component (container, "bonobo-object:application-x-pdf");
137 if (!comp || !(object = comp->server)) {
138 gnome_error_dialog (_("Could not launch bonobo object."));
142 CORBA_exception_init (&ev);
143 persist = GNOME_Unknown_query_interface (
144 gnome_object_corba_objref (GNOME_OBJECT (object)),
145 "IDL:GNOME/PersistStream:1.0", &ev);
147 if (ev._major != CORBA_NO_EXCEPTION ||
148 persist == CORBA_OBJECT_NIL) {
149 gnome_error_dialog ("Panic: component is well broken.");
153 stream = gnome_stream_fs_open (name, GNOME_Storage_READ);
155 if (stream == NULL) {
156 char *err = g_strconcat (_("Could not open "), name, NULL);
157 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
162 GNOME_PersistStream_load (persist,
163 (GNOME_Stream) gnome_object_corba_objref (GNOME_OBJECT (stream)), &ev);
165 GNOME_Unknown_unref (persist, &ev);
166 CORBA_Object_release (persist, &ev);
167 CORBA_exception_free (&ev);
169 /* gnome_view_frame_view_do_verb (comp->view_frame, "ZoomFit"); */
174 set_ok (GtkWidget *widget, gboolean *dialog_result)
176 *dialog_result = TRUE;
181 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
188 container_open_cmd (GtkWidget *widget, Container *container)
190 GtkFileSelection *fsel;
191 gboolean accepted = FALSE;
193 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
194 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
196 gtk_window_set_transient_for (GTK_WINDOW (fsel),
197 GTK_WINDOW (container->app));
199 /* Connect the signals for Ok and Cancel */
200 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
201 GTK_SIGNAL_FUNC (set_ok), &accepted);
202 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
203 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
204 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
207 * Make sure that we quit the main loop if the window is destroyed
209 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
210 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
213 gtk_widget_show (GTK_WIDGET (fsel));
214 gtk_grab_add (GTK_WIDGET (fsel));
218 char *name = gtk_file_selection_get_filename (fsel);
220 if (name [strlen (name)-1] != '/') {
221 char *fname = g_strdup (name);
222 if (container->view_widget) /* any sort of MDI sucks :-] */
223 container = container_new (fname);
225 if (!open_pdf (container, fname))
226 container_destroy (container);
231 dialog = gnome_message_box_new ("Can't open a directory",
232 GNOME_MESSAGE_BOX_ERROR,
233 GNOME_STOCK_BUTTON_OK, NULL);
234 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
235 GTK_WINDOW (container->app));
236 gnome_dialog_run (GNOME_DIALOG (dialog));
240 gtk_widget_destroy (GTK_WIDGET (fsel));
244 component_destroy (Component *component)
246 CORBA_Environment ev;
247 g_return_if_fail (component != NULL);
249 CORBA_exception_init (&ev);
251 if (component->server)
252 GNOME_Unknown_unref (
253 gnome_object_corba_objref (GNOME_OBJECT (component->server)), &ev);
254 component->server = NULL;
256 CORBA_exception_free (&ev);
260 container_destroy (Container *cont)
262 containers = g_list_remove (containers, cont);
264 gtk_widget_destroy (cont->app);
267 component_destroy (cont->component);
268 cont->component = NULL;
278 container_close_cmd (GtkWidget *widget, Container *cont)
280 container_destroy (cont);
284 container_exit_cmd (void)
287 container_destroy ((Container *)containers->data);
292 container_about_cmd (GtkWidget *widget, Container *container)
297 const gchar *authors[] = {
298 N_("Derek B. Noonburg, main author"),
299 N_("Michael Meeks, GNOME port maintainer."),
300 N_("Miguel de Icaza."),
306 for (i = 0; authors[i] != NULL; i++)
307 authors [i] = _(authors [i]);
310 about = gnome_about_new (_("GPDF"), VERSION,
311 _("(C) 1996-1999 Derek B. Noonburg."),
312 authors, NULL, NULL);
314 gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
315 gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
316 gtk_widget_show (about);
321 container_set_view (Container *container, Component *component)
323 GnomeViewFrame *view_frame;
324 GtkWidget *view_widget;
327 * Create the remote view and the local ViewFrame.
329 view_frame = gnome_client_site_new_view (component->client_site);
330 component->view_frame = view_frame;
333 * Set the GnomeUIHandler for this ViewFrame. That way, the
334 * embedded component can get access to our UIHandler server
335 * so that it can merge menu and toolbar items when it gets
338 gnome_view_frame_set_ui_handler (view_frame, container->uih);
341 * Embed the view frame into the application.
343 view_widget = gnome_view_frame_get_wrapper (view_frame);
344 container->view_widget = view_widget;
345 container->component = component;
348 * Show the component.
350 gtk_scrolled_window_add_with_viewport (container->scroll, view_widget);
353 * Activate it ( get it to merge menus etc. )
355 gnome_view_frame_view_activate (view_frame);
357 gtk_widget_show_all (GTK_WIDGET (container->scroll));
360 static GnomeObjectClient *
361 container_launch_component (GnomeClientSite *client_site,
362 GnomeContainer *container,
363 char *component_goad_id)
365 GnomeObjectClient *object_server;
368 * Launch the component.
370 object_server = gnome_object_activate_with_goad_id (
371 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
373 if (object_server == NULL)
377 * Bind it to the local ClientSite. Every embedded component
378 * has a local GnomeClientSite object which serves as a
379 * container-side point of contact for the embeddable. The
380 * container talks to the embeddable through its ClientSite
382 if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
383 gnome_object_unref (GNOME_OBJECT (object_server));
388 * The GnomeContainer object maintains a list of the
389 * ClientSites which it manages. Here we add the new
390 * ClientSite to that list.
392 gnome_container_add (container, GNOME_OBJECT (client_site));
394 return object_server;
399 container_activate_component (Container *container, char *component_goad_id)
401 Component *component;
402 GnomeClientSite *client_site;
403 GnomeObjectClient *server;
406 * The ClientSite is the container-side point of contact for
407 * the Embeddable. So there is a one-to-one correspondence
408 * between GnomeClientSites and GnomeEmbeddables. */
409 client_site = gnome_client_site_new (container->container);
412 * A GnomeObjectClient is a simple wrapper for a remote
413 * GnomeObject (a server supporting GNOME::Unknown).
415 server = container_launch_component (client_site, container->container,
417 if (server == NULL) {
420 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
422 gnome_warning_dialog (error_msg);
429 * Create the internal data structure which we will use to
430 * keep track of this component.
432 component = g_new0 (Component, 1);
433 component->container = container;
434 component->client_site = client_site;
435 component->server = server;
437 container_set_view (container, component);
443 filenames_dropped (GtkWidget * widget,
444 GdkDragContext *context,
447 GtkSelectionData *selection_data,
450 Container *container)
452 GList *names, *tmp_list;
454 names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
458 const char *fname = (const char *)tmp_list->data;
461 if (container->view_widget)
462 container = container_new (fname);
464 open_pdf (container, fname);
467 tmp_list = g_list_next (tmp_list);
473 container_create_menus (Container *container)
475 GnomeUIHandlerMenuItem *menu_list;
477 gnome_ui_handler_create_menubar (container->uih);
480 * Create the basic menus out of UIInfo structures.
482 menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
483 gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
484 gnome_ui_handler_menu_free_list (menu_list);
488 container_create_toolbar (Container *container)
490 GnomeUIHandlerToolbarItem *toolbar;
492 gnome_ui_handler_create_toolbar (container->uih, "pdf");
493 toolbar = gnome_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
494 gnome_ui_handler_toolbar_add_list (container->uih, "/pdf/", toolbar);
495 gnome_ui_handler_toolbar_free_list (toolbar);
499 container_new (const char *fname)
501 Container *container;
502 static GtkTargetEntry drag_types[] =
504 { "text/uri-list", 0, 0 },
506 static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
508 container = g_new0 (Container, 1);
510 container->app = gnome_app_new ("pdf-viewer",
513 gtk_drag_dest_set (container->app,
514 GTK_DEST_DEFAULT_ALL,
515 drag_types, n_drag_types,
518 gtk_signal_connect (GTK_OBJECT(container->app),
519 "drag_data_received",
520 GTK_SIGNAL_FUNC(filenames_dropped), (gpointer)container);
522 gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
523 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
525 container->container = gnome_container_new ();
526 container->view_widget = NULL;
527 container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
528 gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_AUTOMATIC,
529 GTK_POLICY_AUTOMATIC);
530 gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
533 * Create the GnomeUIHandler object which will be used to
534 * create the container's menus and toolbars. The UIHandler
535 * also creates a CORBA server which embedded components use
536 * to do menu/toolbar merging.
538 container->uih = gnome_ui_handler_new ();
539 gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
541 container_create_menus (container);
542 container_create_toolbar (container);
544 gtk_widget_show_all (container->app);
547 if (!open_pdf (container, fname)) {
548 container_destroy (container);
552 containers = g_list_append (containers, container);
554 gtk_widget_show_all (container->app);
560 main (int argc, char **argv)
562 CORBA_Environment ev;
564 char **view_files = NULL;
568 CORBA_exception_init (&ev);
570 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
572 gpdf_popt_options, 0, &ctx,
573 GNORBA_INIT_SERVER_FUNC, &ev);
575 CORBA_exception_free (&ev);
577 orb = gnome_CORBA_ORB ();
579 if (bonobo_init (orb, NULL, NULL) == FALSE)
580 g_error (_("Could not initialize Bonobo!\n"));
583 view_files = poptGetArgs (ctx);
589 for (i = 0; view_files[i]; i++)
590 if (container_new (view_files[i]))
593 if ((i == 0) || !loaded)
594 container_new (NULL);
596 poptFreeContext (ctx);