]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/gpdf.cc
New verb zoom support + zoom to fit.
[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 /*
398  * Use query_interface to see if `obj' has `interface'.
399  */
400 static gboolean
401 gnome_object_has_interface (GnomeObject *obj, char *interface)
402 {
403         CORBA_Environment ev;
404         CORBA_Object requested_interface;
405
406         CORBA_exception_init (&ev);
407
408         requested_interface = GNOME_Unknown_query_interface (
409                 gnome_object_corba_objref (obj), interface, &ev);
410
411         CORBA_exception_free (&ev);
412
413         if (!CORBA_Object_is_nil(requested_interface, &ev) &&
414             ev._major == CORBA_NO_EXCEPTION)
415         {
416                 /* Get rid of the interface we've been passed */
417                 CORBA_Object_release (requested_interface, &ev);
418                 return TRUE;
419         }
420
421         return FALSE;
422 }
423
424 extern "C" {
425   static Component *
426   container_activate_component (Container *container, char *component_goad_id)
427   {
428     Component *component;
429     GnomeClientSite *client_site;
430     GnomeObjectClient *server;
431     
432     /*
433      * The ClientSite is the container-side point of contact for
434      * the Embeddable.  So there is a one-to-one correspondence
435      * between GnomeClientSites and GnomeEmbeddables.  */
436     client_site = gnome_client_site_new (container->container);
437     
438     /*
439      * A GnomeObjectClient is a simple wrapper for a remote
440      * GnomeObject (a server supporting GNOME::Unknown).
441      */
442     server = container_launch_component (client_site, container->container,
443                                          component_goad_id);
444     if (server == NULL) {
445       char *error_msg;
446       
447       error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
448                                    component_goad_id);
449       gnome_warning_dialog (error_msg);
450       g_free (error_msg);
451       
452       return NULL;
453     }
454     
455     /*
456      * Create the internal data structure which we will use to
457      * keep track of this component.
458      */
459     component = g_new0 (Component, 1);
460     component->container = container;
461     component->client_site = client_site;
462     component->server = server;
463     
464     container_set_view (container, component);
465
466     return component;
467   }
468   
469   static void
470   filenames_dropped (GtkWidget * widget,
471                      GdkDragContext   *context,
472                      gint              x,
473                      gint              y,
474                      GtkSelectionData *selection_data,
475                      guint             info,
476                      guint             time,
477                      Container        *container)
478   {
479     GList *names, *tmp_list;
480     
481     names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
482     tmp_list = names;
483     
484     while (tmp_list) {
485       const char *fname = (const char *)tmp_list->data;
486
487       if (fname) {
488         if (container->view_widget)
489           container = container_new (fname);
490         else
491           open_pdf (container, fname);
492       }
493
494       tmp_list = g_list_next (tmp_list);
495     }
496   }
497 }
498
499 static void
500 container_create_menus (Container *container)
501 {
502         GnomeUIHandlerMenuItem *menu_list;
503
504         gnome_ui_handler_create_menubar (container->uih);
505
506         /*
507          * Create the basic menus out of UIInfo structures.
508          */
509         menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
510         gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
511         gnome_ui_handler_menu_free_list (menu_list);
512 }
513
514 static void
515 container_create_toolbar (Container *container)
516 {
517         GnomeUIHandlerToolbarItem *toolbar;
518
519         gnome_ui_handler_create_toolbar (container->uih, "pdf");
520         toolbar = gnome_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
521         gnome_ui_handler_toolbar_add_list (container->uih, "/", toolbar);
522         gnome_ui_handler_toolbar_free_list (toolbar);
523 }
524
525 static Container *
526 container_new (const char *fname)
527 {
528         Container *container;
529         static GtkTargetEntry drag_types[] =
530         {
531           { "text/uri-list", 0, 0 },
532         };
533         static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
534         
535         container = g_new0 (Container, 1);
536
537         container->app  = gnome_app_new ("pdf-viewer",
538                                          "GNOME PDF viewer");
539
540         gtk_drag_dest_set (container->app,
541                            GTK_DEST_DEFAULT_ALL,
542                            drag_types, n_drag_types,
543                            GDK_ACTION_COPY);
544
545         gtk_signal_connect (GTK_OBJECT(container->app),
546                             "drag_data_received",
547                             GTK_SIGNAL_FUNC(filenames_dropped), (gpointer)container);
548
549         gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
550         gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
551
552         container->container   = gnome_container_new ();
553         container->view_widget = NULL;
554         container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
555         gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_AUTOMATIC,
556                                         GTK_POLICY_AUTOMATIC);
557         gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
558
559         /*
560          * Create the GnomeUIHandler object which will be used to
561          * create the container's menus and toolbars.  The UIHandler
562          * also creates a CORBA server which embedded components use
563          * to do menu/toolbar merging.
564          */
565         container->uih = gnome_ui_handler_new ();
566         gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
567
568         container_create_menus   (container);
569         container_create_toolbar (container);
570
571         gtk_widget_show_all (container->app);
572
573         if (fname)
574           if (!open_pdf (container, fname)) {
575             container_destroy (container);
576             return NULL;
577           }
578
579         containers = g_list_append (containers, container);
580
581         gtk_widget_show_all (container->app);
582
583         return container;
584 }
585
586 int
587 main (int argc, char **argv)
588 {
589   CORBA_Environment ev;
590   CORBA_ORB         orb;
591   char            **view_files = NULL;
592   gboolean          loaded;
593   int               i;
594   
595   CORBA_exception_init (&ev);
596   
597   gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
598                                     &argc, argv,
599                                     gpdf_popt_options, 0, &ctx,
600                                     GNORBA_INIT_SERVER_FUNC, &ev);
601
602   CORBA_exception_free (&ev);
603
604   orb = gnome_CORBA_ORB ();
605
606   if (bonobo_init (orb, NULL, NULL) == FALSE)
607     g_error (_("Could not initialize Bonobo!\n"));
608   bonobo_activate ();
609
610   view_files = poptGetArgs (ctx);
611
612   /* Load files */
613   i = 0;
614   loaded = FALSE;
615   if (view_files) {
616     for (i = 0; view_files[i]; i++)
617       if (container_new (view_files[i]))
618         loaded = TRUE;
619   }
620   if ((i == 0) || !loaded)
621     container_new (NULL);
622   
623   poptFreeContext (ctx);
624
625   gtk_main ();
626         
627   return 0;
628 }