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