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 Component *container_activate_component (Container *container, char *component_goad_id);
89 static void zoom_in_cmd (GtkWidget *widget, Container *container);
90 static void zoom_out_cmd (GtkWidget *widget, Container *container);
91 static void zoom_set (Container *container);
97 static GnomeUIInfo container_file_menu [] = {
98 GNOMEUIINFO_MENU_OPEN_ITEM (container_open_cmd, NULL),
99 GNOMEUIINFO_SEPARATOR,
100 GNOMEUIINFO_MENU_CLOSE_ITEM(container_close_cmd, NULL),
101 GNOMEUIINFO_SEPARATOR,
102 GNOMEUIINFO_MENU_EXIT_ITEM (container_exit_cmd, NULL),
106 static GnomeUIInfo container_menu_zoom [] = {
107 { GNOME_APP_UI_ITEM, N_("_Zoom in"),
108 N_("Increase the size of objects in the PDF"),
110 { GNOME_APP_UI_ITEM, N_("_Zoom out"),
111 N_("Decrease the size of objects in the PDF"),
116 static GnomeUIInfo container_main_menu [] = {
117 GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
118 { GNOME_APP_UI_SUBTREE, N_("_Zoom"), NULL, container_menu_zoom },
122 static GnomeUIInfo container_toolbar [] = {
123 GNOMEUIINFO_ITEM_STOCK (
124 N_("Open"), N_("Opens an existing workbook"),
125 container_open_cmd, GNOME_STOCK_PIXMAP_OPEN),
127 GNOMEUIINFO_SEPARATOR,
133 open_pdf (Container *container, const char *name)
135 GnomeObjectClient *object;
137 GNOME_PersistStream persist;
139 CORBA_Environment ev;
141 g_return_val_if_fail (container != NULL, FALSE);
142 g_return_val_if_fail (container->view_widget == NULL, FALSE);
144 comp = container_activate_component (container, "bonobo-object:image-x-pdf");
145 if (!comp || !(object = comp->server)) {
146 gnome_error_dialog (_("Could not launch bonobo object."));
150 CORBA_exception_init (&ev);
151 persist = GNOME_Unknown_query_interface (
152 gnome_object_corba_objref (GNOME_OBJECT (object)),
153 "IDL:GNOME/PersistStream:1.0", &ev);
155 if (ev._major != CORBA_NO_EXCEPTION ||
156 persist == CORBA_OBJECT_NIL) {
157 gnome_error_dialog ("Panic: component is well broken.");
161 stream = gnome_stream_fs_open (name, GNOME_Storage_READ);
163 if (stream == NULL) {
164 char *err = g_strconcat (_("Could not open "), name, NULL);
165 gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
170 GNOME_PersistStream_load (persist,
171 (GNOME_Stream) gnome_object_corba_objref (GNOME_OBJECT (stream)), &ev);
173 zoom_set (container);
175 GNOME_Unknown_unref (persist, &ev);
176 CORBA_Object_release (persist, &ev);
177 CORBA_exception_free (&ev);
182 set_ok (GtkWidget *widget, gboolean *dialog_result)
184 *dialog_result = TRUE;
189 file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
196 container_open_cmd (GtkWidget *widget, Container *container)
198 GtkFileSelection *fsel;
199 gboolean accepted = FALSE;
201 fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
202 gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
204 gtk_window_set_transient_for (GTK_WINDOW (fsel),
205 GTK_WINDOW (container->app));
207 /* Connect the signals for Ok and Cancel */
208 gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
209 GTK_SIGNAL_FUNC (set_ok), &accepted);
210 gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
211 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
212 gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
215 * Make sure that we quit the main loop if the window is destroyed
217 gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
218 GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
221 gtk_widget_show (GTK_WIDGET (fsel));
222 gtk_grab_add (GTK_WIDGET (fsel));
226 char *name = gtk_file_selection_get_filename (fsel);
228 if (name [strlen (name)-1] != '/') {
229 char *fname = g_strdup (name);
230 if (container->view_widget) /* any sort of MDI sucks :-] */
231 container = container_new (fname);
233 open_pdf (container, fname);
237 dialog = gnome_message_box_new ("Can't open a directory",
238 GNOME_MESSAGE_BOX_ERROR,
239 GNOME_STOCK_BUTTON_OK, NULL);
240 gnome_dialog_set_parent (GNOME_DIALOG (dialog),
241 GTK_WINDOW (container->app));
242 gnome_dialog_run (GNOME_DIALOG (dialog));
246 gtk_widget_destroy (GTK_WIDGET (fsel));
250 container_destroy (Container *cont)
252 containers = g_list_remove (containers, cont);
253 gtk_widget_destroy (cont->app);
260 container_close_cmd (GtkWidget *widget, Container *cont)
262 container_destroy (cont);
266 container_exit_cmd (void)
269 container_destroy ((Container *)containers->data);
273 * Enforces the containers zoom factor.
276 zoom_set (Container *container)
278 g_return_if_fail (container != NULL);
279 g_return_if_fail (container->component != NULL);
281 gnome_view_frame_set_zoom_factor (container->component->view_frame,
286 zoom_in_cmd (GtkWidget *widget, Container *container)
288 g_return_if_fail (container != NULL);
289 if (container->zoom < 180.0) {
290 container->zoom *= 1.4;
291 zoom_set (container);
296 zoom_out_cmd (GtkWidget *widget, Container *container)
298 g_return_if_fail (container != NULL);
299 if (container->zoom > 10.0) {
300 container->zoom /= 1.4;
301 zoom_set (container);
307 container_set_view (Container *container, Component *component)
309 GnomeViewFrame *view_frame;
310 GtkWidget *view_widget;
313 * Create the remote view and the local ViewFrame.
315 view_frame = gnome_client_site_new_view (component->client_site);
316 component->view_frame = view_frame;
319 * Set the GnomeUIHandler for this ViewFrame. That way, the
320 * embedded component can get access to our UIHandler server
321 * so that it can merge menu and toolbar items when it gets
324 gnome_view_frame_set_ui_handler (view_frame, container->uih);
327 * Embed the view frame into the application.
329 view_widget = gnome_view_frame_get_wrapper (view_frame);
330 container->view_widget = view_widget;
331 container->component = component;
334 * Show the component.
336 gtk_scrolled_window_add_with_viewport (container->scroll, view_widget);
339 * Activate it ( get it to merge menus etc. )
341 gnome_view_frame_view_activate (view_frame);
343 gtk_widget_show_all (GTK_WIDGET (container->scroll));
346 static GnomeObjectClient *
347 container_launch_component (GnomeClientSite *client_site,
348 GnomeContainer *container,
349 char *component_goad_id)
351 GnomeObjectClient *object_server;
354 * Launch the component.
356 object_server = gnome_object_activate_with_goad_id (
357 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
359 if (object_server == NULL)
363 * Bind it to the local ClientSite. Every embedded component
364 * has a local GnomeClientSite object which serves as a
365 * container-side point of contact for the embeddable. The
366 * container talks to the embeddable through its ClientSite
368 if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
369 gnome_object_unref (GNOME_OBJECT (object_server));
374 * The GnomeContainer object maintains a list of the
375 * ClientSites which it manages. Here we add the new
376 * ClientSite to that list.
378 gnome_container_add (container, GNOME_OBJECT (client_site));
380 return object_server;
384 * Use query_interface to see if `obj' has `interface'.
387 gnome_object_has_interface (GnomeObject *obj, char *interface)
389 CORBA_Environment ev;
390 CORBA_Object requested_interface;
392 CORBA_exception_init (&ev);
394 requested_interface = GNOME_Unknown_query_interface (
395 gnome_object_corba_objref (obj), interface, &ev);
397 CORBA_exception_free (&ev);
399 if (!CORBA_Object_is_nil(requested_interface, &ev) &&
400 ev._major == CORBA_NO_EXCEPTION)
402 /* Get rid of the interface we've been passed */
403 CORBA_Object_release (requested_interface, &ev);
412 container_activate_component (Container *container, char *component_goad_id)
414 Component *component;
415 GnomeClientSite *client_site;
416 GnomeObjectClient *server;
419 * The ClientSite is the container-side point of contact for
420 * the Embeddable. So there is a one-to-one correspondence
421 * between GnomeClientSites and GnomeEmbeddables. */
422 client_site = gnome_client_site_new (container->container);
425 * A GnomeObjectClient is a simple wrapper for a remote
426 * GnomeObject (a server supporting GNOME::Unknown).
428 server = container_launch_component (client_site, container->container,
430 if (server == NULL) {
433 error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
435 gnome_warning_dialog (error_msg);
442 * Create the internal data structure which we will use to
443 * keep track of this component.
445 component = g_new0 (Component, 1);
446 component->container = container;
447 component->client_site = client_site;
448 component->server = server;
450 container_set_view (container, component);
456 filenames_dropped (GtkWidget * widget,
457 GdkDragContext *context,
460 GtkSelectionData *selection_data,
463 Container *container)
465 GList *names, *tmp_list;
467 names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
471 const char *fname = (const char *)tmp_list->data;
474 if (container->view_widget)
475 container = container_new (fname);
477 open_pdf (container, fname);
480 tmp_list = g_list_next (tmp_list);
486 container_create_menus (Container *container)
488 GnomeUIHandlerMenuItem *menu_list;
490 gnome_ui_handler_create_menubar (container->uih);
493 * Create the basic menus out of UIInfo structures.
495 menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
496 gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
497 gnome_ui_handler_menu_free_list (menu_list);
501 container_create_toolbar (Container *container)
503 GnomeUIHandlerToolbarItem *toolbar;
505 gnome_ui_handler_create_toolbar (container->uih, "pdf");
506 toolbar = gnome_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
507 gnome_ui_handler_toolbar_add_list (container->uih, "/", toolbar);
508 gnome_ui_handler_toolbar_free_list (toolbar);
512 container_new (const char *fname)
514 Container *container;
515 static GtkTargetEntry drag_types[] =
517 { "text/uri-list", 0, 0 },
519 static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
521 container = g_new0 (Container, 1);
523 container->app = gnome_app_new ("pdf-viewer",
525 container->zoom = 86.0;
527 gtk_drag_dest_set (container->app,
528 GTK_DEST_DEFAULT_ALL,
529 drag_types, n_drag_types,
532 gtk_signal_connect (GTK_OBJECT(container->app),
533 "drag_data_received",
534 GTK_SIGNAL_FUNC(filenames_dropped), (gpointer)container);
536 gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
537 gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
539 container->container = gnome_container_new ();
540 container->view_widget = NULL;
541 container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
542 gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_AUTOMATIC,
543 GTK_POLICY_AUTOMATIC);
544 gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
547 * Create the GnomeUIHandler object which will be used to
548 * create the container's menus and toolbars. The UIHandler
549 * also creates a CORBA server which embedded components use
550 * to do menu/toolbar merging.
552 container->uih = gnome_ui_handler_new ();
553 gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
555 container_create_menus (container);
556 container_create_toolbar (container);
558 gtk_widget_show_all (container->app);
561 if (!open_pdf (container, fname)) {
562 container_destroy (container);
566 containers = g_list_append (containers, container);
568 gtk_widget_show_all (container->app);
574 main (int argc, char **argv)
576 CORBA_Environment ev;
578 char **view_files = NULL;
581 CORBA_exception_init (&ev);
583 gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
585 gpdf_popt_options, 0, &ctx,
586 GNORBA_INIT_SERVER_FUNC, &ev);
588 CORBA_exception_free (&ev);
590 orb = gnome_CORBA_ORB ();
592 if (bonobo_init (orb, NULL, NULL) == FALSE)
593 g_error (_("Could not initialize Bonobo!\n"));
596 view_files = poptGetArgs (ctx);
601 for (i = 0; view_files[i]; i++)
602 container_new (view_files[i]);
605 container_new (NULL);
607 poptFreeContext (ctx);