]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/gpdf.cc
d7e91347f44c710667421dc9dc0ed8de11f641a6
[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 #define UNTESTED 0
45
46 poptContext ctx;
47 gint  gpdf_debug=0;
48
49 const struct poptOption gpdf_popt_options [] = {
50   { "debug", '\0', POPT_ARG_INT, &gpdf_debug, 0,
51     N_("Enables some debugging functions"), N_("LEVEL") },
52   { NULL, '\0', 0, NULL, 0 }
53 };
54
55 typedef struct _Component Component;
56 typedef struct _Container Container;
57 /* NB. there is a 1 to 1 Container -> Component mapping, this
58    is due to how much MDI sucks; unutterably */
59 struct _Container {
60   GnomeContainer  *container;
61   GnomeUIHandler  *uih;
62   
63   GnomeViewFrame  *active_view_frame;
64   
65   GtkWidget     *app;
66   GtkWidget     *view_widget;
67   Component     *component;
68   gdouble zoom;
69 };
70
71 struct  _Component {
72         Container         *container;
73
74         GnomeClientSite   *client_site;
75         GnomeViewFrame    *view_frame;
76         GnomeObjectClient *server;
77 };
78
79 GList *containers = NULL;
80 /*
81  * Static prototypes.
82  */
83 extern "C" {
84   static Container *container_new       (const char *fname);
85   static void       container_destroy   (Container *cont);
86   static void       container_open_cmd  (GtkWidget *widget, Container *container);
87   static void       container_close_cmd (GtkWidget *widget, Container *container);
88   static void       container_exit_cmd  (void);
89   static Component *container_activate_component (Container *container, char *component_goad_id);
90   static void       zoom_in_cmd         (GtkWidget *widget, Container *container);
91   static void       zoom_out_cmd        (GtkWidget *widget, Container *container);
92   static void       zoom_set            (Container *container);
93 }
94
95 /*
96  * The menus.
97  */
98 static GnomeUIInfo container_file_menu [] = {
99         GNOMEUIINFO_MENU_OPEN_ITEM (container_open_cmd, NULL),
100         GNOMEUIINFO_SEPARATOR,
101         GNOMEUIINFO_MENU_CLOSE_ITEM(container_close_cmd, NULL),
102         GNOMEUIINFO_SEPARATOR,
103         GNOMEUIINFO_MENU_EXIT_ITEM (container_exit_cmd, NULL),
104         GNOMEUIINFO_END
105 };
106
107 static GnomeUIInfo container_menu_zoom [] = {
108         { GNOME_APP_UI_ITEM, N_("_Zoom in"),
109           N_("Increase the size of objects in the PDF"),
110           NULL, zoom_in_cmd },
111         { GNOME_APP_UI_ITEM, N_("_Zoom out"),
112           N_("Decrease the size of objects in the PDF"),
113           zoom_out_cmd },
114         GNOMEUIINFO_END
115 };
116
117 static GnomeUIInfo container_main_menu [] = {
118         GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
119         { GNOME_APP_UI_SUBTREE, N_("_Zoom"), NULL, container_menu_zoom },
120         GNOMEUIINFO_END
121 };
122
123 static GnomeUIInfo container_toolbar [] = {
124         GNOMEUIINFO_ITEM_STOCK (
125                 N_("Open"), N_("Opens an existing workbook"),
126                 container_open_cmd, GNOME_STOCK_PIXMAP_OPEN),
127
128         GNOMEUIINFO_SEPARATOR,
129         GNOMEUIINFO_END
130 };
131
132 extern "C" {
133   static gboolean
134   open_pdf (Container *container, const char *name)
135   {
136     GnomeObjectClient *object;
137     GnomeStream *stream;
138     GNOME_PersistStream persist;
139     Component *comp;
140     CORBA_Environment ev;
141
142     g_return_val_if_fail (container != NULL, FALSE);
143     g_return_val_if_fail (container->view_widget == NULL, FALSE);
144
145     comp = container_activate_component (container, "bonobo-object:image-x-pdf");
146     if (!comp || !(object = comp->server)) {
147       gnome_error_dialog (_("Could not launch bonobo object."));
148       return FALSE;
149     }
150     
151     CORBA_exception_init (&ev);
152     persist = GNOME_Unknown_query_interface (
153       gnome_object_corba_objref (GNOME_OBJECT (object)),
154       "IDL:GNOME/PersistStream:1.0", &ev);
155     
156     if (ev._major != CORBA_NO_EXCEPTION ||
157         persist == CORBA_OBJECT_NIL) {
158       gnome_error_dialog ("Panic: component is well broken.");
159       return FALSE;
160     }
161     
162     stream = gnome_stream_fs_open (name, GNOME_Storage_READ);
163     
164     if (stream == NULL) {
165       char *err = g_strconcat (_("Could not open "), name, NULL);
166       gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
167       g_free (err);
168       return FALSE;
169     }
170     
171     GNOME_PersistStream_load (persist,
172                               (GNOME_Stream) gnome_object_corba_objref (GNOME_OBJECT (stream)), &ev);
173
174     
175     
176     GNOME_Unknown_unref (persist, &ev);
177     CORBA_Object_release (persist, &ev);
178     CORBA_exception_free (&ev);
179     return TRUE;
180   }
181   
182   static void
183   set_ok (GtkWidget *widget, gboolean *dialog_result)
184   {
185     *dialog_result = TRUE;
186     gtk_main_quit ();
187   }
188   
189   static guint
190   file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
191   {
192     gtk_main_quit ();
193     return TRUE;
194   }
195   
196   static void
197   container_open_cmd (GtkWidget *widget, Container *container)
198   {
199     GtkFileSelection *fsel;
200     gboolean accepted = FALSE;
201     
202     fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
203     gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
204     
205     gtk_window_set_transient_for (GTK_WINDOW (fsel),
206                                   GTK_WINDOW (container->app));
207     
208     /* Connect the signals for Ok and Cancel */
209     gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
210                         GTK_SIGNAL_FUNC (set_ok), &accepted);
211     gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
212                         GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
213     gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
214     
215     /*
216      * Make sure that we quit the main loop if the window is destroyed 
217      */
218     gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
219                         GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
220     
221     /* Run the dialog */
222     gtk_widget_show (GTK_WIDGET (fsel));
223     gtk_grab_add (GTK_WIDGET (fsel));
224     gtk_main ();
225     
226     if (accepted) {
227       char *name = gtk_file_selection_get_filename (fsel);
228       
229       if (name [strlen (name)-1] != '/') {
230         char *fname = g_strdup (name);
231         if (container->view_widget) /* any sort of MDI sucks :-] */
232           container = container_new (fname);
233         else
234           open_pdf (container, fname);
235         g_free (fname);
236       } else {
237         GtkWidget *dialog;
238         dialog = gnome_message_box_new ("Can't open a directory",
239                                         GNOME_MESSAGE_BOX_ERROR,
240                                         GNOME_STOCK_BUTTON_OK, NULL);
241         gnome_dialog_set_parent (GNOME_DIALOG (dialog),
242                                  GTK_WINDOW (container->app));
243         gnome_dialog_run (GNOME_DIALOG (dialog));
244       }
245     }
246     
247     gtk_widget_destroy (GTK_WIDGET (fsel));
248   }
249
250   static void
251   container_destroy (Container *cont)
252   {
253     containers = g_list_remove (containers, cont);
254     gtk_widget_destroy (cont->app);
255     g_free (cont);
256     if (!containers)
257       gtk_main_quit ();
258   }
259   
260   static void
261   container_close_cmd (GtkWidget *widget, Container *cont)
262   {
263     container_destroy (cont);
264   }
265   
266   static void
267   container_exit_cmd (void)
268   {
269     while (containers)
270       container_destroy ((Container *)containers->data);
271   }
272
273   /*
274    * Enforces the containers zoom factor.
275    */
276   static void
277   zoom_set (Container *container)
278   {
279     g_return_if_fail (container != NULL);
280     g_return_if_fail (container->component != NULL);
281
282     gnome_view_frame_set_zoom_factor (container->component->view_frame,
283                                       container->zoom);
284   }
285
286   static void
287   zoom_in_cmd (GtkWidget *widget, Container *container)
288   {
289     g_return_if_fail (container != NULL);
290     container->zoom *= 1.4;
291     zoom_set (container);
292   }
293
294   static void
295   zoom_out_cmd (GtkWidget *widget, Container *container)
296   {
297     g_return_if_fail (container != NULL);
298     container->zoom /= 1.4;
299     zoom_set (container);
300   }
301
302   static void
303   component_user_activate_request_cb (GnomeViewFrame *view_frame, gpointer data)
304   {
305     Component *component = (Component *) data;
306     Container *container = component->container;
307     
308     /*
309      * If there is a
310      * If there is already an active View, deactivate it.
311      */
312     if (container->active_view_frame != NULL) {
313       /*
314        * This just sends a notice to the embedded View that
315        * it is being deactivated.  We will also forcibly
316        * cover it so that it does not receive any Gtk
317        * events.
318        */
319       gnome_view_frame_view_deactivate (container->active_view_frame);
320       
321       /*
322        * Here we manually cover it if it hasn't acquiesced.
323        * If it has consented to be deactivated, then it will
324        * already have notified us that it is inactive, and
325        * we will have covered it and set active_view_frame
326        * to NULL.  Which is why this check is here.
327        */
328       if (container->active_view_frame != NULL)
329         gnome_view_frame_set_covered (container->active_view_frame, TRUE);
330       
331       container->active_view_frame = NULL;
332     }
333     
334     /*
335      * Activate the View which the user clicked on.  This just
336      * sends a request to the embedded View to activate itself.
337      * When it agrees to be activated, it will notify its
338      * ViewFrame, and our view_activated_cb callback will be
339      * called.
340      *
341      * We do not uncover the View here, because it may not wish to
342      * be activated, and so we wait until it notifies us that it
343      * has been activated to uncover it.
344      */
345     gnome_view_frame_view_activate (view_frame);
346   }
347   
348   static void
349   component_view_activated_cb (GnomeViewFrame *view_frame, gboolean activated, gpointer data)
350   {
351     Component *component = (Component *) data;
352     Container *container = component->container;
353     
354     if (activated) {
355       /*
356        * If the View is requesting to be activated, then we
357        * check whether or not there is already an active
358        * View.
359        */
360       if (container->active_view_frame != NULL) {
361         g_warning ("View requested to be activated but there is already "
362                    "an active View!\n");
363         return;
364       }
365       
366       /*
367        * Otherwise, uncover it so that it can receive
368        * events, and set it as the active View.
369        */
370       gnome_view_frame_set_covered (view_frame, FALSE);
371       container->active_view_frame = view_frame;
372     } else {
373       /*
374        * If the View is asking to be deactivated, always
375        * oblige.  We may have already deactivated it (see
376        * user_activation_request_cb), but there's no harm in
377        * doing it again.  There is always the possibility
378        * that a View will ask to be deactivated when we have
379        * not told it to deactivate itself, and that is
380        * why we cover the view here.
381        */
382       gnome_view_frame_set_covered (view_frame, TRUE);
383       
384       if (view_frame == container->active_view_frame)
385         container->active_view_frame = NULL;
386     }                                                                       
387   }
388   
389   static void
390   component_user_context_cb (GnomeViewFrame *view_frame, gpointer data)
391   {
392     Component *component = (Component *) data;
393     char *executed_verb;
394     GList *l;
395     
396     /*
397      * See if the remote GnomeEmbeddable supports any verbs at
398      * all.
399      */
400     l = gnome_client_site_get_verbs (component->client_site);
401     if (l == NULL)
402       return;
403     gnome_client_site_free_verbs (l);
404     
405     /*
406      * Popup the verb popup and execute the chosen verb.  This
407      * function saves us the work of creating the menu, connecting
408      * the callback, and executing the verb on the remove
409      * GnomeView.  We could implement all this functionality
410      * ourselves if we wanted.
411      */
412     executed_verb = gnome_view_frame_popup_verbs (view_frame);
413     
414     g_free (executed_verb);
415   }
416 }
417
418 static void
419 container_set_view (Container *container, Component *component)
420 {
421         GnomeViewFrame *view_frame;
422         GtkWidget *view_widget;
423
424         /*
425          * Create the remote view and the local ViewFrame.
426          */
427         view_frame = gnome_client_site_embeddable_new_view (component->client_site);
428         component->view_frame = view_frame;
429
430         /*
431          * Set the GnomeUIHandler for this ViewFrame.  That way, the
432          * embedded component can get access to our UIHandler server
433          * so that it can merge menu and toolbar items when it gets
434          * activated.
435          */
436         gnome_view_frame_set_ui_handler (view_frame, container->uih);
437
438         /*
439          * Embed the view frame into the application.
440          */
441         view_widget = gnome_view_frame_get_wrapper (view_frame);
442         container->view_widget = view_widget;
443         container->component   = component;
444 /*      gtk_box_pack_start (GTK_BOX (container->app), view_widget,
445         FALSE, FALSE, 5);*/
446         gnome_app_set_contents (GNOME_APP (container->app), view_widget);
447         /*
448          * The "user_activate" signal will be emitted when the user
449          * double clicks on the "cover".  The cover is a transparent
450          * window which sits on top of the component and keeps any
451          * events (mouse, keyboard) from reaching it.  When the user
452          * double clicks on the cover, the container (that's us)
453          * can choose to activate the component.
454          */
455         gtk_signal_connect (GTK_OBJECT (view_frame), "user_activate",
456                             GTK_SIGNAL_FUNC (component_user_activate_request_cb), component);
457
458         /*
459          * In-place activation of a component is a two-step process.
460          * After the user double clicks on the component, our signal
461          * callback (compoennt_user_activate_request_cb()) asks the
462          * component to activate itself (see
463          * gnome_view_frame_view_activate()).  The component can then
464          * choose to either accept or refuse activation.  When an
465          * embedded component notifies us of its decision to change
466          * its activation state, the "view_activated" signal is
467          * emitted from the view frame.  It is at that point that we
468          * actually remove the cover so that events can get through.
469          */
470         gtk_signal_connect (GTK_OBJECT (view_frame), "view_activated",
471                             GTK_SIGNAL_FUNC (component_view_activated_cb), component);
472
473         /*
474          * The "user_context" signal is emitted when the user right
475          * clicks on the wrapper.  We use it to pop up a verb menu.
476          */
477         gtk_signal_connect (GTK_OBJECT (view_frame), "user_context",
478                             GTK_SIGNAL_FUNC (component_user_context_cb), component);
479
480         /*
481          * Show the component.
482          */
483         gtk_widget_show_all (view_widget);
484 }
485
486 static GnomeObjectClient *
487 container_launch_component (GnomeClientSite *client_site,
488                             GnomeContainer *container,
489                             char *component_goad_id)
490 {
491         GnomeObjectClient *object_server;
492
493         /*
494          * Launch the component.
495          */
496         object_server = gnome_object_activate_with_goad_id (
497                 NULL, component_goad_id, 0, NULL);
498
499         if (object_server == NULL)
500                 return NULL;
501
502         /*
503          * Bind it to the local ClientSite.  Every embedded component
504          * has a local GnomeClientSite object which serves as a
505          * container-side point of contact for the embeddable.  The
506          * container talks to the embeddable through its ClientSite
507          */
508         if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
509                 gnome_object_unref (GNOME_OBJECT (object_server));
510                 return NULL;
511         }
512
513         /*
514          * The GnomeContainer object maintains a list of the
515          * ClientSites which it manages.  Here we add the new
516          * ClientSite to that list.
517          */
518         gnome_container_add (container, GNOME_OBJECT (client_site));
519
520         return object_server;
521 }
522
523 /*
524  * Use query_interface to see if `obj' has `interface'.
525  */
526 static gboolean
527 gnome_object_has_interface (GnomeObject *obj, char *interface)
528 {
529         CORBA_Environment ev;
530         CORBA_Object requested_interface;
531
532         CORBA_exception_init (&ev);
533
534         requested_interface = GNOME_Unknown_query_interface (
535                 gnome_object_corba_objref (obj), interface, &ev);
536
537         CORBA_exception_free (&ev);
538
539         if (!CORBA_Object_is_nil(requested_interface, &ev) &&
540             ev._major == CORBA_NO_EXCEPTION)
541         {
542                 /* Get rid of the interface we've been passed */
543                 CORBA_Object_release (requested_interface, &ev);
544                 return TRUE;
545         }
546
547         return FALSE;
548 }
549
550 extern "C" {
551   static Component *
552   container_activate_component (Container *container, char *component_goad_id)
553   {
554     Component *component;
555     GnomeClientSite *client_site;
556     GnomeObjectClient *server;
557     
558     /*
559      * The ClientSite is the container-side point of contact for
560      * the Embeddable.  So there is a one-to-one correspondence
561      * between GnomeClientSites and GnomeEmbeddables.  */
562     client_site = gnome_client_site_new (container->container);
563     
564     /*
565      * A GnomeObjectClient is a simple wrapper for a remote
566      * GnomeObject (a server supporting GNOME::Unknown).
567      */
568     server = container_launch_component (client_site, container->container,
569                                          component_goad_id);
570     if (server == NULL) {
571       char *error_msg;
572       
573       error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
574                                    component_goad_id);
575       gnome_warning_dialog (error_msg);
576       g_free (error_msg);
577       
578       return NULL;
579     }
580     
581     /*
582      * Create the internal data structure which we will use to
583      * keep track of this component.
584      */
585     component = g_new0 (Component, 1);
586     component->container = container;
587     component->client_site = client_site;
588     component->server = server;
589     
590     container_set_view (container, component);
591
592     return component;
593   }
594 }
595
596 static void
597 container_create_menus (Container *container)
598 {
599         GnomeUIHandlerMenuItem *menu_list;
600
601         gnome_ui_handler_create_menubar (container->uih);
602
603         /*
604          * Create the basic menus out of UIInfo structures.
605          */
606         menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
607         gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
608         gnome_ui_handler_menu_free_list (menu_list);
609 }
610
611 static void
612 container_create_toolbar (Container *container)
613 {
614         GnomeUIHandlerMenuItem *toolbar;
615
616 #if UNTESTED > 0
617         gnome_ui_handler_create_menubar (container->uih);
618
619         /*
620          * Create the basic menus out of UIInfo structures.
621          */
622         toolbar = gnome_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
623         gnome_ui_handler_toolbar_add_list (container->uih, "/", menu_list);
624         gnome_ui_handler_toolbar_free_list (menu_list);
625 #endif
626 }
627
628 static Container *
629 container_new (const char *fname)
630 {
631         Container *container;
632
633         container = g_new0 (Container, 1);
634
635         container->app  = gnome_app_new ("pdf-viewer",
636                                          "GNOME PDF viewer");
637         container->zoom = 43.0;
638
639         gtk_window_set_default_size (GTK_WINDOW (container->app), 400, 400);
640         gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
641
642         container->container   = gnome_container_new ();
643         container->view_widget = NULL;
644
645         /*
646          * Create the GnomeUIHandler object which will be used to
647          * create the container's menus and toolbars.  The UIHandler
648          * also creates a CORBA server which embedded components use
649          * to do menu/toolbar merging.
650          */
651         container->uih = gnome_ui_handler_new ();
652         gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
653
654         container_create_menus   (container);
655         container_create_toolbar (container);
656
657         gtk_widget_show_all (container->app);
658
659         if (fname)
660           if (!open_pdf (container, fname)) {
661             container_destroy (container);
662             return NULL;
663           }
664
665         containers = g_list_append (containers, container);
666
667         gtk_widget_show_all (container->app);
668
669         return container;
670 }
671
672 int
673 main (int argc, char **argv)
674 {
675   CORBA_Environment ev;
676   CORBA_ORB orb;
677   char **view_files = NULL;
678   int    i;
679   
680   CORBA_exception_init (&ev);
681   
682   gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
683                                     &argc, argv,
684                                     gpdf_popt_options, 0, &ctx,
685                                     GNORBA_INIT_SERVER_FUNC, &ev);
686
687   CORBA_exception_free (&ev);
688
689   orb = gnome_CORBA_ORB ();
690
691   if (bonobo_init (orb, NULL, NULL) == FALSE)
692     g_error (_("Could not initialize Bonobo!\n"));
693
694   view_files = poptGetArgs (ctx);
695
696   /* Load files */
697   i = 0;
698   if (view_files) {
699     for (i = 0; view_files[i]; i++)
700       container_new (view_files[i]);
701   }
702   if (i == 0)
703     container_new (NULL);
704   
705   poptFreeContext (ctx);
706
707   bonobo_activate ();  
708   gtk_main ();
709         
710   return 0;
711 }