]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/gpdf.cc
Attempts at cleaning up component destruction - failed :-(
[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   component_destroy (Component *component)
260   {
261     CORBA_Environment ev;
262     g_return_if_fail (component != NULL);
263
264     CORBA_exception_init (&ev);
265     
266     if (component->server)
267       GNOME_Unknown_unref (
268         gnome_object_corba_objref (GNOME_OBJECT (component->server)), &ev);
269     component->server = NULL;
270
271     CORBA_exception_free (&ev);
272   }
273
274   static void
275   container_destroy (Container *cont)
276   {
277     containers = g_list_remove (containers, cont);
278     if (cont->app)
279       gtk_widget_destroy (cont->app);
280     
281     if (cont->component)
282       component_destroy (cont->component);
283     cont->component = NULL;
284     
285     cont->app = NULL;
286     
287     g_free (cont);
288     if (!containers)
289       gtk_main_quit ();
290   }
291   
292   static void
293   container_close_cmd (GtkWidget *widget, Container *cont)
294   {
295     container_destroy (cont);
296   }
297   
298   static void
299   container_exit_cmd (void)
300   {
301     while (containers)
302       container_destroy ((Container *)containers->data);
303   }
304
305
306 static void
307 container_about_cmd (GtkWidget *widget, Container *container)
308 {
309   GtkWidget *about;
310   int i;
311
312   const gchar *authors[] = {
313     N_("Michael Meeks, GNOME port maintainer."),
314     N_("Miguel de Icaza."),
315     N_("Nat Friedman."),
316     NULL
317   };
318   
319 #ifdef ENABLE_NLS
320   for (i = 0; authors[i] != NULL; i++)
321     authors [i] = _(authors [i]);
322 #endif
323   
324   about = gnome_about_new (_("GPDF"), VERSION,
325                            _("(C) 1996-1999 Derek B. Noonburg."),
326                            authors, NULL, NULL);
327   
328   gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
329   gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
330   gtk_widget_show (about);
331 }
332
333   /*
334    * Enforces the containers zoom factor.
335    */
336   static void
337   zoom_set (Container *container)
338   {
339     g_return_if_fail (container != NULL);
340     g_return_if_fail (container->component != NULL);
341
342     gnome_view_frame_set_zoom_factor (container->component->view_frame,
343                                       container->zoom);
344   }
345
346   static void
347   zoom_in_cmd (GtkWidget *widget, Container *container)
348   {
349     g_return_if_fail (container != NULL);
350     if (container->zoom < 180.0) {
351       container->zoom *= 1.4;
352       zoom_set (container);
353     }
354   }
355
356   static void
357   zoom_out_cmd (GtkWidget *widget, Container *container)
358   {
359     g_return_if_fail (container != NULL);
360     if (container->zoom > 10.0) {
361       container->zoom /= 1.4;
362       zoom_set (container);
363     }
364   }
365 }
366
367 static void
368 container_set_view (Container *container, Component *component)
369 {
370         GnomeViewFrame *view_frame;
371         GtkWidget *view_widget;
372
373         /*
374          * Create the remote view and the local ViewFrame.
375          */
376         view_frame = gnome_client_site_new_view (component->client_site);
377         component->view_frame = view_frame;
378
379         /*
380          * Set the GnomeUIHandler for this ViewFrame.  That way, the
381          * embedded component can get access to our UIHandler server
382          * so that it can merge menu and toolbar items when it gets
383          * activated.
384          */
385         gnome_view_frame_set_ui_handler (view_frame, container->uih);
386
387         /*
388          * Embed the view frame into the application.
389          */
390         view_widget = gnome_view_frame_get_wrapper (view_frame);
391         container->view_widget = view_widget;
392         container->component   = component;
393
394         /*
395          * Show the component.
396          */
397         gtk_scrolled_window_add_with_viewport (container->scroll, view_widget);
398
399         /*
400          * Activate it ( get it to merge menus etc. )
401          */
402         gnome_view_frame_view_activate (view_frame);
403
404         gtk_widget_show_all (GTK_WIDGET (container->scroll));
405 }
406
407 static GnomeObjectClient *
408 container_launch_component (GnomeClientSite *client_site,
409                             GnomeContainer *container,
410                             char *component_goad_id)
411 {
412         GnomeObjectClient *object_server;
413
414         /*
415          * Launch the component.
416          */
417         object_server = gnome_object_activate_with_goad_id (
418                 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
419
420         if (object_server == NULL)
421                 return NULL;
422
423         /*
424          * Bind it to the local ClientSite.  Every embedded component
425          * has a local GnomeClientSite object which serves as a
426          * container-side point of contact for the embeddable.  The
427          * container talks to the embeddable through its ClientSite
428          */
429         if (!gnome_client_site_bind_embeddable (client_site, object_server)) {
430                 gnome_object_unref (GNOME_OBJECT (object_server));
431                 return NULL;
432         }
433
434         /*
435          * The GnomeContainer object maintains a list of the
436          * ClientSites which it manages.  Here we add the new
437          * ClientSite to that list.
438          */
439         gnome_container_add (container, GNOME_OBJECT (client_site));
440
441         return object_server;
442 }
443
444 /*
445  * Use query_interface to see if `obj' has `interface'.
446  */
447 static gboolean
448 gnome_object_has_interface (GnomeObject *obj, char *interface)
449 {
450         CORBA_Environment ev;
451         CORBA_Object requested_interface;
452
453         CORBA_exception_init (&ev);
454
455         requested_interface = GNOME_Unknown_query_interface (
456                 gnome_object_corba_objref (obj), interface, &ev);
457
458         CORBA_exception_free (&ev);
459
460         if (!CORBA_Object_is_nil(requested_interface, &ev) &&
461             ev._major == CORBA_NO_EXCEPTION)
462         {
463                 /* Get rid of the interface we've been passed */
464                 CORBA_Object_release (requested_interface, &ev);
465                 return TRUE;
466         }
467
468         return FALSE;
469 }
470
471 extern "C" {
472   static Component *
473   container_activate_component (Container *container, char *component_goad_id)
474   {
475     Component *component;
476     GnomeClientSite *client_site;
477     GnomeObjectClient *server;
478     
479     /*
480      * The ClientSite is the container-side point of contact for
481      * the Embeddable.  So there is a one-to-one correspondence
482      * between GnomeClientSites and GnomeEmbeddables.  */
483     client_site = gnome_client_site_new (container->container);
484     
485     /*
486      * A GnomeObjectClient is a simple wrapper for a remote
487      * GnomeObject (a server supporting GNOME::Unknown).
488      */
489     server = container_launch_component (client_site, container->container,
490                                          component_goad_id);
491     if (server == NULL) {
492       char *error_msg;
493       
494       error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
495                                    component_goad_id);
496       gnome_warning_dialog (error_msg);
497       g_free (error_msg);
498       
499       return NULL;
500     }
501     
502     /*
503      * Create the internal data structure which we will use to
504      * keep track of this component.
505      */
506     component = g_new0 (Component, 1);
507     component->container = container;
508     component->client_site = client_site;
509     component->server = server;
510     
511     container_set_view (container, component);
512
513     return component;
514   }
515   
516   static void
517   filenames_dropped (GtkWidget * widget,
518                      GdkDragContext   *context,
519                      gint              x,
520                      gint              y,
521                      GtkSelectionData *selection_data,
522                      guint             info,
523                      guint             time,
524                      Container        *container)
525   {
526     GList *names, *tmp_list;
527     
528     names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
529     tmp_list = names;
530     
531     while (tmp_list) {
532       const char *fname = (const char *)tmp_list->data;
533
534       if (fname) {
535         if (container->view_widget)
536           container = container_new (fname);
537         else
538           open_pdf (container, fname);
539       }
540
541       tmp_list = g_list_next (tmp_list);
542     }
543   }
544 }
545
546 static void
547 container_create_menus (Container *container)
548 {
549         GnomeUIHandlerMenuItem *menu_list;
550
551         gnome_ui_handler_create_menubar (container->uih);
552
553         /*
554          * Create the basic menus out of UIInfo structures.
555          */
556         menu_list = gnome_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
557         gnome_ui_handler_menu_add_list (container->uih, "/", menu_list);
558         gnome_ui_handler_menu_free_list (menu_list);
559 }
560
561 static void
562 container_create_toolbar (Container *container)
563 {
564         GnomeUIHandlerToolbarItem *toolbar;
565
566         gnome_ui_handler_create_toolbar (container->uih, "pdf");
567         toolbar = gnome_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
568         gnome_ui_handler_toolbar_add_list (container->uih, "/", toolbar);
569         gnome_ui_handler_toolbar_free_list (toolbar);
570 }
571
572 static Container *
573 container_new (const char *fname)
574 {
575         Container *container;
576         static GtkTargetEntry drag_types[] =
577         {
578           { "text/uri-list", 0, 0 },
579         };
580         static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
581         
582         container = g_new0 (Container, 1);
583
584         container->app  = gnome_app_new ("pdf-viewer",
585                                          "GNOME PDF viewer");
586         container->zoom = 86.0;
587
588         gtk_drag_dest_set (container->app,
589                            GTK_DEST_DEFAULT_ALL,
590                            drag_types, n_drag_types,
591                            GDK_ACTION_COPY);
592
593         gtk_signal_connect (GTK_OBJECT(container->app),
594                             "drag_data_received",
595                             GTK_SIGNAL_FUNC(filenames_dropped), (gpointer)container);
596
597         gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
598         gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
599
600         container->container   = gnome_container_new ();
601         container->view_widget = NULL;
602         container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
603         gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_AUTOMATIC,
604                                         GTK_POLICY_AUTOMATIC);
605         gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
606
607         /*
608          * Create the GnomeUIHandler object which will be used to
609          * create the container's menus and toolbars.  The UIHandler
610          * also creates a CORBA server which embedded components use
611          * to do menu/toolbar merging.
612          */
613         container->uih = gnome_ui_handler_new ();
614         gnome_ui_handler_set_app (container->uih, GNOME_APP (container->app));
615
616         container_create_menus   (container);
617         container_create_toolbar (container);
618
619         gtk_widget_show_all (container->app);
620
621         if (fname)
622           if (!open_pdf (container, fname)) {
623             container_destroy (container);
624             return NULL;
625           }
626
627         containers = g_list_append (containers, container);
628
629         gtk_widget_show_all (container->app);
630
631         return container;
632 }
633
634 int
635 main (int argc, char **argv)
636 {
637   CORBA_Environment ev;
638   CORBA_ORB         orb;
639   char            **view_files = NULL;
640   gboolean          loaded;
641   int               i;
642   
643   CORBA_exception_init (&ev);
644   
645   gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
646                                     &argc, argv,
647                                     gpdf_popt_options, 0, &ctx,
648                                     GNORBA_INIT_SERVER_FUNC, &ev);
649
650   CORBA_exception_free (&ev);
651
652   orb = gnome_CORBA_ORB ();
653
654   if (bonobo_init (orb, NULL, NULL) == FALSE)
655     g_error (_("Could not initialize Bonobo!\n"));
656   bonobo_activate ();
657
658   view_files = poptGetArgs (ctx);
659
660   /* Load files */
661   i = 0;
662   loaded = FALSE;
663   if (view_files) {
664     for (i = 0; view_files[i]; i++)
665       if (container_new (view_files[i]))
666         loaded = TRUE;
667   }
668   if ((i == 0) || !loaded)
669     container_new (NULL);
670   
671   poptFreeContext (ctx);
672
673   gtk_main ();
674         
675   return 0;
676 }