]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/gpdf.cc
Re-organise to get mime type right,
[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 void       container_about_cmd (GtkWidget *widget, Container *container);
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           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_help_menu [] = {
118         GNOMEUIINFO_MENU_ABOUT_ITEM(container_about_cmd, NULL),
119         GNOMEUIINFO_END
120 };
121
122 static GnomeUIInfo container_main_menu [] = {
123         GNOMEUIINFO_MENU_FILE_TREE (container_file_menu),
124         { GNOME_APP_UI_SUBTREE, N_("_Zoom"), NULL, container_menu_zoom },
125         GNOMEUIINFO_MENU_HELP_TREE (container_help_menu),
126         GNOMEUIINFO_END
127 };
128
129 static GnomeUIInfo container_toolbar [] = {
130         GNOMEUIINFO_ITEM_STOCK (
131                 N_("Open"), N_("Opens an existing workbook"),
132                 container_open_cmd, GNOME_STOCK_PIXMAP_OPEN),
133
134         GNOMEUIINFO_SEPARATOR,
135         GNOMEUIINFO_END
136 };
137
138 extern "C" {
139   static gboolean
140   open_pdf (Container *container, const char *name)
141   {
142     GnomeObjectClient *object;
143     GnomeStream *stream;
144     GNOME_PersistStream persist;
145     Component *comp;
146     CORBA_Environment ev;
147
148     g_return_val_if_fail (container != NULL, FALSE);
149     g_return_val_if_fail (container->view_widget == NULL, FALSE);
150
151     comp = container_activate_component (container, "bonobo-object:application-x-pdf");
152     if (!comp || !(object = comp->server)) {
153       gnome_error_dialog (_("Could not launch bonobo object."));
154       return FALSE;
155     }
156     
157     CORBA_exception_init (&ev);
158     persist = GNOME_Unknown_query_interface (
159       gnome_object_corba_objref (GNOME_OBJECT (object)),
160       "IDL:GNOME/PersistStream:1.0", &ev);
161     
162     if (ev._major != CORBA_NO_EXCEPTION ||
163         persist == CORBA_OBJECT_NIL) {
164       gnome_error_dialog ("Panic: component is well broken.");
165       return FALSE;
166     }
167     
168     stream = gnome_stream_fs_open (name, GNOME_Storage_READ);
169     
170     if (stream == NULL) {
171       char *err = g_strconcat (_("Could not open "), name, NULL);
172       gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
173       g_free (err);
174       return FALSE;
175     }
176     
177     GNOME_PersistStream_load (persist,
178                               (GNOME_Stream) gnome_object_corba_objref (GNOME_OBJECT (stream)), &ev);
179
180     zoom_set (container);
181         
182     GNOME_Unknown_unref (persist, &ev);
183     CORBA_Object_release (persist, &ev);
184     CORBA_exception_free (&ev);
185     return TRUE;
186   }
187   
188   static void
189   set_ok (GtkWidget *widget, gboolean *dialog_result)
190   {
191     *dialog_result = TRUE;
192     gtk_main_quit ();
193   }
194   
195   static guint
196   file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
197   {
198     gtk_main_quit ();
199     return TRUE;
200   }
201   
202   static void
203   container_open_cmd (GtkWidget *widget, Container *container)
204   {
205     GtkFileSelection *fsel;
206     gboolean accepted = FALSE;
207     
208     fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
209     gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
210     
211     gtk_window_set_transient_for (GTK_WINDOW (fsel),
212                                   GTK_WINDOW (container->app));
213     
214     /* Connect the signals for Ok and Cancel */
215     gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
216                         GTK_SIGNAL_FUNC (set_ok), &accepted);
217     gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
218                         GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
219     gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
220     
221     /*
222      * Make sure that we quit the main loop if the window is destroyed 
223      */
224     gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
225                         GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
226     
227     /* Run the dialog */
228     gtk_widget_show (GTK_WIDGET (fsel));
229     gtk_grab_add (GTK_WIDGET (fsel));
230     gtk_main ();
231     
232     if (accepted) {
233       char *name = gtk_file_selection_get_filename (fsel);
234       
235       if (name [strlen (name)-1] != '/') {
236         char *fname = g_strdup (name);
237         if (container->view_widget) /* any sort of MDI sucks :-] */
238           container = container_new (fname);
239         else {
240           if (!open_pdf (container, fname))
241             container_destroy (container);
242         }
243         g_free (fname);
244       } else {
245         GtkWidget *dialog;
246         dialog = gnome_message_box_new ("Can't open a directory",
247                                         GNOME_MESSAGE_BOX_ERROR,
248                                         GNOME_STOCK_BUTTON_OK, NULL);
249         gnome_dialog_set_parent (GNOME_DIALOG (dialog),
250                                  GTK_WINDOW (container->app));
251         gnome_dialog_run (GNOME_DIALOG (dialog));
252       }
253     }
254     
255     gtk_widget_destroy (GTK_WIDGET (fsel));
256   }
257
258   static void
259   container_destroy (Container *cont)
260   {
261     containers = g_list_remove (containers, cont);
262     if (cont->app)
263       gtk_widget_destroy (cont->app);
264     cont->app = NULL;
265     /* FIXME: Some serious resource freeing needs to be here */
266     
267     g_free (cont);
268     if (!containers)
269       gtk_main_quit ();
270   }
271   
272   static void
273   container_close_cmd (GtkWidget *widget, Container *cont)
274   {
275     container_destroy (cont);
276   }
277   
278   static void
279   container_exit_cmd (void)
280   {
281     while (containers)
282       container_destroy ((Container *)containers->data);
283   }
284
285
286 static void
287 container_about_cmd (GtkWidget *widget, Container *container)
288 {
289   GtkWidget *about;
290   int i;
291
292   const gchar *authors[] = {
293     N_("Michael Meeks, GNOME port maintainer."),
294     N_("Miguel de Icaza."),
295     N_("Nat Friedman."),
296     NULL
297   };
298   
299 #ifdef ENABLE_NLS
300   for (i = 0; authors[i] != NULL; i++)
301     authors [i] = _(authors [i]);
302 #endif
303   
304   about = gnome_about_new (_("GPDF"), VERSION,
305                            _("(C) 1996-1999 Derek B. Noonburg."),
306                            authors, NULL, NULL);
307   
308   gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
309   gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
310   gtk_widget_show (about);
311 }
312
313   /*
314    * Enforces the containers zoom factor.
315    */
316   static void
317   zoom_set (Container *container)
318   {
319     g_return_if_fail (container != NULL);
320     g_return_if_fail (container->component != NULL);
321
322     gnome_view_frame_set_zoom_factor (container->component->view_frame,
323                                       container->zoom);
324   }
325
326   static void
327   zoom_in_cmd (GtkWidget *widget, Container *container)
328   {
329     g_return_if_fail (container != NULL);
330     if (container->zoom < 180.0) {
331       container->zoom *= 1.4;
332       zoom_set (container);
333     }
334   }
335
336   static void
337   zoom_out_cmd (GtkWidget *widget, Container *container)
338   {
339     g_return_if_fail (container != NULL);
340     if (container->zoom > 10.0) {
341       container->zoom /= 1.4;
342       zoom_set (container);
343     }
344   }
345 }
346
347 static void
348 container_set_view (Container *container, Component *component)
349 {
350         GnomeViewFrame *view_frame;
351         GtkWidget *view_widget;
352
353         /*
354          * Create the remote view and the local ViewFrame.
355          */
356         view_frame = gnome_client_site_new_view (component->client_site);
357         component->view_frame = view_frame;
358
359         /*
360          * Set the GnomeUIHandler for this ViewFrame.  That way, the
361          * embedded component can get access to our UIHandler server
362          * so that it can merge menu and toolbar items when it gets
363          * activated.
364          */
365         gnome_view_frame_set_ui_handler (view_frame, container->uih);
366
367         /*
368          * Embed the view frame into the application.
369          */
370         view_widget = gnome_view_frame_get_wrapper (view_frame);
371         container->view_widget = view_widget;
372         container->component   = component;
373
374         /*
375          * Show the component.
376          */
377         gtk_scrolled_window_add_with_viewport (container->scroll, view_widget);
378
379         /*
380          * Activate it ( get it to merge menus etc. )
381          */
382         gnome_view_frame_view_activate (view_frame);
383
384         gtk_widget_show_all (GTK_WIDGET (container->scroll));
385 }
386
387 static GnomeObjectClient *
388 container_launch_component (GnomeClientSite *client_site,
389                             GnomeContainer *container,
390                             char *component_goad_id)
391 {
392         GnomeObjectClient *object_server;
393
394         /*
395          * Launch the component.
396          */
397         object_server = gnome_object_activate_with_goad_id (
398                 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
399
400         if (object_server == NULL)
401                 return NULL;
402
403         /*
404          * Bind it to the local ClientSite.  Every embedded component
405          * has a local GnomeClientSite object which serves as a
406          * container-side point of contact for the embeddable.  The
407          * container talks to the embeddable through its ClientSite
408          */
409         if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
410                 gnome_object_unref (GNOME_OBJECT (object_server));
411                 return NULL;
412         }
413
414         /*
415          * The GnomeContainer object maintains a list of the
416          * ClientSites which it manages.  Here we add the new
417          * ClientSite to that list.
418          */
419         gnome_container_add (container, GNOME_OBJECT (client_site));
420
421         return object_server;
422 }
423
424 /*
425  * Use query_interface to see if `obj' has `interface'.
426  */
427 static gboolean
428 gnome_object_has_interface (GnomeObject *obj, char *interface)
429 {
430         CORBA_Environment ev;
431         CORBA_Object requested_interface;
432
433         CORBA_exception_init (&ev);
434
435         requested_interface = GNOME_Unknown_query_interface (
436                 gnome_object_corba_objref (obj), interface, &ev);
437
438         CORBA_exception_free (&ev);
439
440         if (!CORBA_Object_is_nil(requested_interface, &ev) &&
441             ev._major == CORBA_NO_EXCEPTION)
442         {
443                 /* Get rid of the interface we've been passed */
444                 CORBA_Object_release (requested_interface, &ev);
445                 return TRUE;
446         }
447
448         return FALSE;
449 }
450
451 extern "C" {
452   static Component *
453   container_activate_component (Container *container, char *component_goad_id)
454   {
455     Component *component;
456     GnomeClientSite *client_site;
457     GnomeObjectClient *server;
458     
459     /*
460      * The ClientSite is the container-side point of contact for
461      * the Embeddable.  So there is a one-to-one correspondence
462      * between GnomeClientSites and GnomeEmbeddables.  */
463     client_site = gnome_client_site_new (container->container);
464     
465     /*
466      * A GnomeObjectClient is a simple wrapper for a remote
467      * GnomeObject (a server supporting GNOME::Unknown).
468      */
469     server = container_launch_component (client_site, container->container,
470                                          component_goad_id);
471     if (server == NULL) {
472       char *error_msg;
473       
474       error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
475                                    component_goad_id);
476       gnome_warning_dialog (error_msg);
477       g_free (error_msg);
478       
479       return NULL;
480     }
481     
482     /*
483      * Create the internal data structure which we will use to
484      * keep track of this component.
485      */
486     component = g_new0 (Component, 1);
487     component->container = container;
488     component->client_site = client_site;
489     component->server = server;
490     
491     container_set_view (container, component);
492
493     return component;
494   }
495   
496   static void
497   filenames_dropped (GtkWidget * widget,
498                      GdkDragContext   *context,
499                      gint              x,
500                      gint              y,
501                      GtkSelectionData *selection_data,
502                      guint             info,
503                      guint             time,
504                      Container        *container)
505   {
506     GList *names, *tmp_list;
507     
508     names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
509     tmp_list = names;
510     
511     while (tmp_list) {
512       const char *fname = (const char *)tmp_list->data;
513
514       if (fname) {
515         if (container->view_widget)
516           container = container_new (fname);
517         else
518           open_pdf (container, fname);
519       }
520
521       tmp_list = g_list_next (tmp_list);
522     }
523   }
524 }
525
526 static void
527 container_create_menus (Container *container)
528 {
529         GnomeUIHandlerMenuItem *menu_list;
530
531         gnome_ui_handler_create_menubar (container->uih);
532
533         /*
534          * Create the basic menus out of UIInfo structures.
535          */
536         menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
537         gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
538         gnome_ui_handler_menu_free_list (menu_list);
539 }
540
541 static void
542 container_create_toolbar (Container *container)
543 {
544         GnomeUIHandlerToolbarItem *toolbar;
545
546         gnome_ui_handler_create_toolbar (container->uih, "pdf");
547         toolbar = gnome_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
548         gnome_ui_handler_toolbar_add_list (container->uih, "/", toolbar);
549         gnome_ui_handler_toolbar_free_list (toolbar);
550 }
551
552 static Container *
553 container_new (const char *fname)
554 {
555         Container *container;
556         static GtkTargetEntry drag_types[] =
557         {
558           { "text/uri-list", 0, 0 },
559         };
560         static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
561         
562         container = g_new0 (Container, 1);
563
564         container->app  = gnome_app_new ("pdf-viewer",
565                                          "GNOME PDF viewer");
566         container->zoom = 86.0;
567
568         gtk_drag_dest_set (container->app,
569                            GTK_DEST_DEFAULT_ALL,
570                            drag_types, n_drag_types,
571                            GDK_ACTION_COPY);
572
573         gtk_signal_connect (GTK_OBJECT(container->app),
574                             "drag_data_received",
575                             GTK_SIGNAL_FUNC(filenames_dropped), (gpointer)container);
576
577         gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
578         gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
579
580         container->container   = gnome_container_new ();
581         container->view_widget = NULL;
582         container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
583         gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_AUTOMATIC,
584                                         GTK_POLICY_AUTOMATIC);
585         gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
586
587         /*
588          * Create the GnomeUIHandler object which will be used to
589          * create the container's menus and toolbars.  The UIHandler
590          * also creates a CORBA server which embedded components use
591          * to do menu/toolbar merging.
592          */
593         container->uih = gnome_ui_handler_new ();
594         gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
595
596         container_create_menus   (container);
597         container_create_toolbar (container);
598
599         gtk_widget_show_all (container->app);
600
601         if (fname)
602           if (!open_pdf (container, fname)) {
603             container_destroy (container);
604             return NULL;
605           }
606
607         containers = g_list_append (containers, container);
608
609         gtk_widget_show_all (container->app);
610
611         return container;
612 }
613
614 int
615 main (int argc, char **argv)
616 {
617   CORBA_Environment ev;
618   CORBA_ORB         orb;
619   char            **view_files = NULL;
620   gboolean          loaded;
621   int               i;
622   
623   CORBA_exception_init (&ev);
624   
625   gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
626                                     &argc, argv,
627                                     gpdf_popt_options, 0, &ctx,
628                                     GNORBA_INIT_SERVER_FUNC, &ev);
629
630   CORBA_exception_free (&ev);
631
632   orb = gnome_CORBA_ORB ();
633
634   if (bonobo_init (orb, NULL, NULL) == FALSE)
635     g_error (_("Could not initialize Bonobo!\n"));
636   bonobo_activate ();
637
638   view_files = poptGetArgs (ctx);
639
640   /* Load files */
641   i = 0;
642   loaded = FALSE;
643   if (view_files) {
644     for (i = 0; view_files[i]; i++)
645       if (container_new (view_files[i]))
646         loaded = TRUE;
647   }
648   if ((i == 0) || !loaded)
649     container_new (NULL);
650   
651   poptFreeContext (ctx);
652
653   gtk_main ();
654         
655   return 0;
656 }