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