]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/gpdf.cc
05bdad905876f017db24918243fe1aad24710722
[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     zoom_set (container);
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
306 static void
307 container_set_view (Container *container, Component *component)
308 {
309         GnomeViewFrame *view_frame;
310         GtkWidget *view_widget;
311
312         /*
313          * Create the remote view and the local ViewFrame.
314          */
315         view_frame = gnome_client_site_new_view (component->client_site);
316         component->view_frame = view_frame;
317
318         /*
319          * Set the GnomeUIHandler for this ViewFrame.  That way, the
320          * embedded component can get access to our UIHandler server
321          * so that it can merge menu and toolbar items when it gets
322          * activated.
323          */
324         gnome_view_frame_set_ui_handler (view_frame, container->uih);
325
326         /*
327          * Embed the view frame into the application.
328          */
329         view_widget = gnome_view_frame_get_wrapper (view_frame);
330         container->view_widget = view_widget;
331         container->component   = component;
332
333         /*
334          * Show the component.
335          */
336         gtk_scrolled_window_add_with_viewport (container->scroll, view_widget);
337
338         /*
339          * Activate it ( get it to merge menus etc. )
340          */
341         gnome_view_frame_view_activate (view_frame);
342
343         gtk_widget_show_all (GTK_WIDGET (container->scroll));
344 }
345
346 static GnomeObjectClient *
347 container_launch_component (GnomeClientSite *client_site,
348                             GnomeContainer *container,
349                             char *component_goad_id)
350 {
351         GnomeObjectClient *object_server;
352
353         /*
354          * Launch the component.
355          */
356         object_server = gnome_object_activate_with_goad_id (
357                 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
358
359         if (object_server == NULL)
360                 return NULL;
361
362         /*
363          * Bind it to the local ClientSite.  Every embedded component
364          * has a local GnomeClientSite object which serves as a
365          * container-side point of contact for the embeddable.  The
366          * container talks to the embeddable through its ClientSite
367          */
368         if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
369                 gnome_object_unref (GNOME_OBJECT (object_server));
370                 return NULL;
371         }
372
373         /*
374          * The GnomeContainer object maintains a list of the
375          * ClientSites which it manages.  Here we add the new
376          * ClientSite to that list.
377          */
378         gnome_container_add (container, GNOME_OBJECT (client_site));
379
380         return object_server;
381 }
382
383 /*
384  * Use query_interface to see if `obj' has `interface'.
385  */
386 static gboolean
387 gnome_object_has_interface (GnomeObject *obj, char *interface)
388 {
389         CORBA_Environment ev;
390         CORBA_Object requested_interface;
391
392         CORBA_exception_init (&ev);
393
394         requested_interface = GNOME_Unknown_query_interface (
395                 gnome_object_corba_objref (obj), interface, &ev);
396
397         CORBA_exception_free (&ev);
398
399         if (!CORBA_Object_is_nil(requested_interface, &ev) &&
400             ev._major == CORBA_NO_EXCEPTION)
401         {
402                 /* Get rid of the interface we've been passed */
403                 CORBA_Object_release (requested_interface, &ev);
404                 return TRUE;
405         }
406
407         return FALSE;
408 }
409
410 extern "C" {
411   static Component *
412   container_activate_component (Container *container, char *component_goad_id)
413   {
414     Component *component;
415     GnomeClientSite *client_site;
416     GnomeObjectClient *server;
417     
418     /*
419      * The ClientSite is the container-side point of contact for
420      * the Embeddable.  So there is a one-to-one correspondence
421      * between GnomeClientSites and GnomeEmbeddables.  */
422     client_site = gnome_client_site_new (container->container);
423     
424     /*
425      * A GnomeObjectClient is a simple wrapper for a remote
426      * GnomeObject (a server supporting GNOME::Unknown).
427      */
428     server = container_launch_component (client_site, container->container,
429                                          component_goad_id);
430     if (server == NULL) {
431       char *error_msg;
432       
433       error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
434                                    component_goad_id);
435       gnome_warning_dialog (error_msg);
436       g_free (error_msg);
437       
438       return NULL;
439     }
440     
441     /*
442      * Create the internal data structure which we will use to
443      * keep track of this component.
444      */
445     component = g_new0 (Component, 1);
446     component->container = container;
447     component->client_site = client_site;
448     component->server = server;
449     
450     container_set_view (container, component);
451
452     return component;
453   }
454   
455   static void
456   filenames_dropped (GtkWidget * widget,
457                      GdkDragContext   *context,
458                      gint              x,
459                      gint              y,
460                      GtkSelectionData *selection_data,
461                      guint             info,
462                      guint             time,
463                      Container        *container)
464   {
465     GList *names, *tmp_list;
466     
467     names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
468     tmp_list = names;
469     
470     while (tmp_list) {
471       const char *fname = (const char *)tmp_list->data;
472
473       if (fname) {
474         if (container->view_widget)
475           container = container_new (fname);
476         else
477           open_pdf (container, fname);
478       }
479
480       tmp_list = g_list_next (tmp_list);
481     }
482   }
483 }
484
485 static void
486 container_create_menus (Container *container)
487 {
488         GnomeUIHandlerMenuItem *menu_list;
489
490         gnome_ui_handler_create_menubar (container->uih);
491
492         /*
493          * Create the basic menus out of UIInfo structures.
494          */
495         menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
496         gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
497         gnome_ui_handler_menu_free_list (menu_list);
498 }
499
500 static void
501 container_create_toolbar (Container *container)
502 {
503         GnomeUIHandlerToolbarItem *toolbar;
504
505         gnome_ui_handler_create_toolbar (container->uih, "pdf");
506         toolbar = gnome_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
507         gnome_ui_handler_toolbar_add_list (container->uih, "/", toolbar);
508         gnome_ui_handler_toolbar_free_list (toolbar);
509 }
510
511 static Container *
512 container_new (const char *fname)
513 {
514         Container *container;
515         static GtkTargetEntry drag_types[] =
516         {
517           { "text/uri-list", 0, 0 },
518         };
519         static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
520         
521         container = g_new0 (Container, 1);
522
523         container->app  = gnome_app_new ("pdf-viewer",
524                                          "GNOME PDF viewer");
525         container->zoom = 86.0;
526
527         gtk_drag_dest_set (container->app,
528                            GTK_DEST_DEFAULT_ALL,
529                            drag_types, n_drag_types,
530                            GDK_ACTION_COPY);
531
532         gtk_signal_connect (GTK_OBJECT(container->app),
533                             "drag_data_received",
534                             GTK_SIGNAL_FUNC(filenames_dropped), (gpointer)container);
535
536         gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
537         gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
538
539         container->container   = gnome_container_new ();
540         container->view_widget = NULL;
541         container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
542         gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_AUTOMATIC,
543                                         GTK_POLICY_AUTOMATIC);
544         gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
545
546         /*
547          * Create the GnomeUIHandler object which will be used to
548          * create the container's menus and toolbars.  The UIHandler
549          * also creates a CORBA server which embedded components use
550          * to do menu/toolbar merging.
551          */
552         container->uih = gnome_ui_handler_new ();
553         gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
554
555         container_create_menus   (container);
556         container_create_toolbar (container);
557
558         gtk_widget_show_all (container->app);
559
560         if (fname)
561           if (!open_pdf (container, fname)) {
562             container_destroy (container);
563             return NULL;
564           }
565
566         containers = g_list_append (containers, container);
567
568         gtk_widget_show_all (container->app);
569
570         return container;
571 }
572
573 int
574 main (int argc, char **argv)
575 {
576   CORBA_Environment ev;
577   CORBA_ORB orb;
578   char **view_files = NULL;
579   int    i;
580   
581   CORBA_exception_init (&ev);
582   
583   gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
584                                     &argc, argv,
585                                     gpdf_popt_options, 0, &ctx,
586                                     GNORBA_INIT_SERVER_FUNC, &ev);
587
588   CORBA_exception_free (&ev);
589
590   orb = gnome_CORBA_ORB ();
591
592   if (bonobo_init (orb, NULL, NULL) == FALSE)
593     g_error (_("Could not initialize Bonobo!\n"));
594   bonobo_activate ();
595
596   view_files = poptGetArgs (ctx);
597
598   /* Load files */
599   i = 0;
600   if (view_files) {
601     for (i = 0; view_files[i]; i++)
602       container_new (view_files[i]);
603   }
604   if (i == 0)
605     container_new (NULL);
606   
607   poptFreeContext (ctx);
608
609   gtk_main ();
610         
611   return 0;
612 }