]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/gpdf.cc
e2d11548adb5093fd319ca5bc7a22c57c098a99a
[evince.git] / pdf / xpdf / gpdf.cc
1 /*
2  * PDF viewer Bonobo container.
3  *
4  * Author:
5  *   Michael Meeks <michael@imaginator.com>
6  *
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stddef.h>
11 #include <string.h>
12 extern "C" {
13 #define GString G_String
14 #include <gnome.h>
15 #include <libgnorba/gnorba.h>
16 #include <gdk/gdkprivate.h>
17 #include <gdk/gdkx.h>
18 #include <bonobo/gnome-bonobo.h>
19 #undef  GString 
20 }
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include "gtypes.h"
24 #include "GString.h"
25 #include "parseargs.h"
26 #include "gfile.h"
27 #include "gmem.h"
28 #include "Object.h"
29 #include "Stream.h"
30 #include "Array.h"
31 #include "Dict.h"
32 #include "XRef.h"
33 #include "Catalog.h"
34 #include "Page.h"
35 #include "Link.h"
36 #include "PDFDoc.h"
37 #include "GOutputDev.h"
38 #include "PSOutputDev.h"
39 #include "TextOutputDev.h"
40 #include "Params.h"
41 #include "Error.h"
42 #include "config.h"
43
44 poptContext ctx;
45 gint  gpdf_debug=0;
46
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 }
51 };
52
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 */
57 struct _Container {
58   GnomeContainer    *container;
59   GnomeUIHandler    *uih;
60   
61   GnomeViewFrame    *active_view_frame;
62   
63   GtkWidget         *app;
64   GtkScrolledWindow *scroll;
65   GtkWidget         *view_widget;
66   Component         *component;
67 };
68
69 struct  _Component {
70         Container         *container;
71
72         GnomeClientSite   *client_site;
73         GnomeViewFrame    *view_frame;
74         GnomeObjectClient *server;
75 };
76
77 GList *containers = NULL;
78 /*
79  * Static prototypes.
80  */
81 extern "C" {
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);
89 }
90
91 /*
92  * The menus.
93  */
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),
100         GNOMEUIINFO_END
101 };
102
103 static GnomeUIInfo container_help_menu [] = {
104         GNOMEUIINFO_MENU_ABOUT_ITEM(container_about_cmd, NULL),
105         GNOMEUIINFO_END
106 };
107
108 static GnomeUIInfo container_main_menu [] = {
109         GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
110         GNOMEUIINFO_MENU_HELP_TREE (container_help_menu),
111         GNOMEUIINFO_END
112 };
113
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),
118
119         GNOMEUIINFO_SEPARATOR,
120         GNOMEUIINFO_END
121 };
122
123 extern "C" {
124   static gboolean
125   open_pdf (Container *container, const char *name)
126   {
127     GnomeObjectClient *object;
128     GnomeStream *stream;
129     GNOME_PersistStream persist;
130     Component *comp;
131     CORBA_Environment ev;
132
133     g_return_val_if_fail (container != NULL, FALSE);
134     g_return_val_if_fail (container->view_widget == NULL, FALSE);
135
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."));
139       return FALSE;
140     }
141     
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);
146     
147     if (ev._major != CORBA_NO_EXCEPTION ||
148         persist == CORBA_OBJECT_NIL) {
149       gnome_error_dialog ("Panic: component is well broken.");
150       return FALSE;
151     }
152     
153     stream = gnome_stream_fs_open (name, GNOME_Storage_READ);
154     
155     if (stream == NULL) {
156       char *err = g_strconcat (_("Could not open "), name, NULL);
157       gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
158       g_free (err);
159       return FALSE;
160     }
161     
162     GNOME_PersistStream_load (persist,
163                               (GNOME_Stream) gnome_object_corba_objref (GNOME_OBJECT (stream)), &ev);
164
165     GNOME_Unknown_unref (persist, &ev);
166     CORBA_Object_release (persist, &ev);
167     CORBA_exception_free (&ev);
168
169 /*    gnome_view_frame_view_do_verb (comp->view_frame, "ZoomFit"); */
170     return TRUE;
171   }
172   
173   static void
174   set_ok (GtkWidget *widget, gboolean *dialog_result)
175   {
176     *dialog_result = TRUE;
177     gtk_main_quit ();
178   }
179   
180   static guint
181   file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
182   {
183     gtk_main_quit ();
184     return TRUE;
185   }
186   
187   static void
188   container_open_cmd (GtkWidget *widget, Container *container)
189   {
190     GtkFileSelection *fsel;
191     gboolean accepted = FALSE;
192     
193     fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
194     gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
195     
196     gtk_window_set_transient_for (GTK_WINDOW (fsel),
197                                   GTK_WINDOW (container->app));
198     
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);
205     
206     /*
207      * Make sure that we quit the main loop if the window is destroyed 
208      */
209     gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
210                         GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
211     
212     /* Run the dialog */
213     gtk_widget_show (GTK_WIDGET (fsel));
214     gtk_grab_add (GTK_WIDGET (fsel));
215     gtk_main ();
216     
217     if (accepted) {
218       char *name = gtk_file_selection_get_filename (fsel);
219       
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);
224         else {
225           if (!open_pdf (container, fname))
226             container_destroy (container);
227         }
228         g_free (fname);
229       } else {
230         GtkWidget *dialog;
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));
237       }
238     }
239     
240     gtk_widget_destroy (GTK_WIDGET (fsel));
241   }
242
243   static void 
244   component_destroy (Component *component)
245   {
246     CORBA_Environment ev;
247     g_return_if_fail (component != NULL);
248
249     CORBA_exception_init (&ev);
250     
251     if (component->server)
252       GNOME_Unknown_unref (
253         gnome_object_corba_objref (GNOME_OBJECT (component->server)), &ev);
254     component->server = NULL;
255
256     CORBA_exception_free (&ev);
257   }
258
259   static void
260   container_destroy (Container *cont)
261   {
262     containers = g_list_remove (containers, cont);
263     if (cont->app)
264       gtk_widget_destroy (cont->app);
265     
266     if (cont->component)
267       component_destroy (cont->component);
268     cont->component = NULL;
269     
270     cont->app = NULL;
271     
272     g_free (cont);
273     if (!containers)
274       gtk_main_quit ();
275   }
276   
277   static void
278   container_close_cmd (GtkWidget *widget, Container *cont)
279   {
280     container_destroy (cont);
281   }
282   
283   static void
284   container_exit_cmd (void)
285   {
286     while (containers)
287       container_destroy ((Container *)containers->data);
288   }
289
290
291 static void
292 container_about_cmd (GtkWidget *widget, Container *container)
293 {
294   GtkWidget *about;
295   int i;
296
297   const gchar *authors[] = {
298     N_("Derek B. Noonburg, main author"),
299     N_("Michael Meeks, GNOME port maintainer."),
300     N_("Miguel de Icaza."),
301     N_("Nat Friedman."),
302     NULL
303   };
304   
305 #ifdef ENABLE_NLS
306   for (i = 0; authors[i] != NULL; i++)
307     authors [i] = _(authors [i]);
308 #endif
309   
310   about = gnome_about_new (_("GPDF"), VERSION,
311                            _("(C) 1996-1999 Derek B. Noonburg."),
312                            authors, NULL, NULL);
313   
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);
317 }
318 }
319
320 static void
321 container_set_view (Container *container, Component *component)
322 {
323         GnomeViewFrame *view_frame;
324         GtkWidget *view_widget;
325
326         /*
327          * Create the remote view and the local ViewFrame.
328          */
329         view_frame = gnome_client_site_new_view (component->client_site);
330         component->view_frame = view_frame;
331
332         /*
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
336          * activated.
337          */
338         gnome_view_frame_set_ui_handler (view_frame, container->uih);
339
340         /*
341          * Embed the view frame into the application.
342          */
343         view_widget = gnome_view_frame_get_wrapper (view_frame);
344         container->view_widget = view_widget;
345         container->component   = component;
346
347         /*
348          * Show the component.
349          */
350         gtk_scrolled_window_add_with_viewport (container->scroll, view_widget);
351
352         /*
353          * Activate it ( get it to merge menus etc. )
354          */
355         gnome_view_frame_view_activate (view_frame);
356
357         gtk_widget_show_all (GTK_WIDGET (container->scroll));
358 }
359
360 static GnomeObjectClient *
361 container_launch_component (GnomeClientSite *client_site,
362                             GnomeContainer *container,
363                             char *component_goad_id)
364 {
365         GnomeObjectClient *object_server;
366
367         /*
368          * Launch the component.
369          */
370         object_server = gnome_object_activate_with_goad_id (
371                 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
372
373         if (object_server == NULL)
374                 return NULL;
375
376         /*
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
381          */
382         if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
383                 gnome_object_unref (GNOME_OBJECT (object_server));
384                 return NULL;
385         }
386
387         /*
388          * The GnomeContainer object maintains a list of the
389          * ClientSites which it manages.  Here we add the new
390          * ClientSite to that list.
391          */
392         gnome_container_add (container, GNOME_OBJECT (client_site));
393
394         return object_server;
395 }
396
397 extern "C" {
398   static Component *
399   container_activate_component (Container *container, char *component_goad_id)
400   {
401     Component *component;
402     GnomeClientSite *client_site;
403     GnomeObjectClient *server;
404     
405     /*
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);
410     
411     /*
412      * A GnomeObjectClient is a simple wrapper for a remote
413      * GnomeObject (a server supporting GNOME::Unknown).
414      */
415     server = container_launch_component (client_site, container->container,
416                                          component_goad_id);
417     if (server == NULL) {
418       char *error_msg;
419       
420       error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
421                                    component_goad_id);
422       gnome_warning_dialog (error_msg);
423       g_free (error_msg);
424       
425       return NULL;
426     }
427     
428     /*
429      * Create the internal data structure which we will use to
430      * keep track of this component.
431      */
432     component = g_new0 (Component, 1);
433     component->container = container;
434     component->client_site = client_site;
435     component->server = server;
436     
437     container_set_view (container, component);
438
439     return component;
440   }
441   
442   static void
443   filenames_dropped (GtkWidget * widget,
444                      GdkDragContext   *context,
445                      gint              x,
446                      gint              y,
447                      GtkSelectionData *selection_data,
448                      guint             info,
449                      guint             time,
450                      Container        *container)
451   {
452     GList *names, *tmp_list;
453     
454     names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
455     tmp_list = names;
456     
457     while (tmp_list) {
458       const char *fname = (const char *)tmp_list->data;
459
460       if (fname) {
461         if (container->view_widget)
462           container = container_new (fname);
463         else
464           open_pdf (container, fname);
465       }
466
467       tmp_list = g_list_next (tmp_list);
468     }
469   }
470 }
471
472 static void
473 container_create_menus (Container *container)
474 {
475         GnomeUIHandlerMenuItem *menu_list;
476
477         gnome_ui_handler_create_menubar (container->uih);
478
479         /*
480          * Create the basic menus out of UIInfo structures.
481          */
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);
485 }
486
487 static void
488 container_create_toolbar (Container *container)
489 {
490         GnomeUIHandlerToolbarItem *toolbar;
491
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);
496 }
497
498 static Container *
499 container_new (const char *fname)
500 {
501         Container *container;
502         static GtkTargetEntry drag_types[] =
503         {
504           { "text/uri-list", 0, 0 },
505         };
506         static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
507         
508         container = g_new0 (Container, 1);
509
510         container->app  = gnome_app_new ("pdf-viewer",
511                                          "GNOME PDF viewer");
512
513         gtk_drag_dest_set (container->app,
514                            GTK_DEST_DEFAULT_ALL,
515                            drag_types, n_drag_types,
516                            GDK_ACTION_COPY);
517
518         gtk_signal_connect (GTK_OBJECT(container->app),
519                             "drag_data_received",
520                             GTK_SIGNAL_FUNC(filenames_dropped), (gpointer)container);
521
522         gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
523         gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
524
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));
531
532         /*
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.
537          */
538         container->uih = gnome_ui_handler_new ();
539         gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
540
541         container_create_menus   (container);
542         container_create_toolbar (container);
543
544         gtk_widget_show_all (container->app);
545
546         if (fname)
547           if (!open_pdf (container, fname)) {
548             container_destroy (container);
549             return NULL;
550           }
551
552         containers = g_list_append (containers, container);
553
554         gtk_widget_show_all (container->app);
555
556         return container;
557 }
558
559 int
560 main (int argc, char **argv)
561 {
562   CORBA_Environment ev;
563   CORBA_ORB         orb;
564   char            **view_files = NULL;
565   gboolean          loaded;
566   int               i;
567   
568   CORBA_exception_init (&ev);
569   
570   gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
571                                     &argc, argv,
572                                     gpdf_popt_options, 0, &ctx,
573                                     GNORBA_INIT_SERVER_FUNC, &ev);
574
575   CORBA_exception_free (&ev);
576
577   orb = gnome_CORBA_ORB ();
578
579   if (bonobo_init (orb, NULL, NULL) == FALSE)
580     g_error (_("Could not initialize Bonobo!\n"));
581   bonobo_activate ();
582
583   view_files = poptGetArgs (ctx);
584
585   /* Load files */
586   i = 0;
587   loaded = FALSE;
588   if (view_files) {
589     for (i = 0; view_files[i]; i++)
590       if (container_new (view_files[i]))
591         loaded = TRUE;
592   }
593   if ((i == 0) || !loaded)
594     container_new (NULL);
595   
596   poptFreeContext (ctx);
597
598   gtk_main ();
599         
600   return 0;
601 }