]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/gpdf.cc
045209977e9668f44f18e54cbc8d8c8789dc5027
[evince.git] / pdf / xpdf / gpdf.cc
1 /*
2  * PDF viewer Bonobo container.
3  *
4  * Author:
5  *   Michael Meeks <michael@imaginator.com>
6  */
7 #include <aconf.h>
8 #include "config.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stddef.h>
12 #include <string.h>
13 extern "C" {
14 #define GString G_String
15 #include <gnome.h>
16
17 #include <liboaf/liboaf.h>
18
19 #include <gdk/gdkprivate.h>
20 #include <gdk/gdkx.h>
21 #include <bonobo.h>
22 #undef  GString 
23 }
24 #include "bonobo-application-x-pdf.h"
25
26 poptContext ctx;
27 gint  gpdf_debug=0;
28
29 const struct poptOption gpdf_popt_options [] = {
30   { "debug", '\0', POPT_ARG_INT, &gpdf_debug, 0,
31     N_("Enables some debugging functions"), N_("LEVEL") },
32   { NULL, '\0', 0, NULL, 0 }
33 };
34
35 typedef struct _Component Component;
36 typedef struct _Container Container;
37 /* NB. there is a 1 to 1 Container -> Component mapping, this
38    is due to how much MDI sucks; unutterably */
39 struct _Container {
40         BonoboItemContainer *container;
41         BonoboUIComponent   *ui_component;
42   
43         GtkWidget           *app;
44         GtkWidget           *slot;
45         GtkWidget           *view_widget;
46         Component           *component;
47 };
48
49 struct  _Component {
50         Container         *container;
51
52         BonoboClientSite   *client_site;
53         BonoboViewFrame    *view_frame;
54         BonoboObjectClient *server;
55 };
56
57 GList *containers = NULL;
58 /*
59  * Static prototypes.
60  */
61 extern "C" {
62   static Container *container_new       (const char *fname);
63   static void       container_destroy   (Container *cont);
64   static void       container_open_cmd  (GtkWidget *widget, Container *container);
65   static void       container_close_cmd (GtkWidget *widget, Container *container);
66   static void       container_exit_cmd  (void);
67   static void       container_about_cmd (GtkWidget *widget, Container *container);
68   static void       container_dump_cmd  (GtkWidget *widget, Container *container);
69   static Component *container_activate_component (Container *container, char *component_goad_id);
70 }
71
72 /*
73  * The menus.
74  */
75 BonoboUIVerb verbs [] = {
76         BONOBO_UI_UNSAFE_VERB ("FileOpen",  container_open_cmd),
77         BONOBO_UI_UNSAFE_VERB ("FileClose", container_close_cmd),
78         BONOBO_UI_UNSAFE_VERB ("FileExit",  container_exit_cmd),
79
80         BONOBO_UI_UNSAFE_VERB ("HelpAbout", container_about_cmd),
81
82         BONOBO_UI_UNSAFE_VERB ("DebugDumpXml", container_dump_cmd),
83
84         BONOBO_UI_VERB_END
85 };
86
87
88 extern "C" {
89   static gboolean
90   open_pdf (Container *container, const char *name)
91   {
92     BonoboObjectClient *object;
93     BonoboStream *stream;
94     Bonobo_PersistStream persist;
95     Component *comp;
96     CORBA_Environment ev;
97
98     g_return_val_if_fail (container != NULL, FALSE);
99     g_return_val_if_fail (container->view_widget == NULL, FALSE);
100
101     comp = container_activate_component (
102             container, "OAFIID:GNOME_XPDF_Embeddable");
103
104     if (!comp || !(object = comp->server)) {
105       gnome_error_dialog (_("Could not launch bonobo object."));
106       return FALSE;
107     }
108     
109     CORBA_exception_init (&ev);
110     persist = Bonobo_Unknown_queryInterface (
111       bonobo_object_corba_objref (BONOBO_OBJECT (object)),
112       "IDL:Bonobo/PersistStream:1.0", &ev);
113     
114     if (ev._major != CORBA_NO_EXCEPTION ||
115         persist == CORBA_OBJECT_NIL) {
116       gnome_error_dialog ("Panic: component doesn't implement PersistStream.");
117       return FALSE;
118     }
119     
120     stream = bonobo_stream_open (BONOBO_IO_DRIVER_FS, name, Bonobo_Storage_READ, 0);
121     
122     if (stream == NULL) {
123       char *err = g_strdup_printf (_("Could not open %s"), name);
124       gnome_error_dialog_parented (err, GTK_WINDOW(container->app));
125       g_free (err);
126       return FALSE;
127     }
128     
129     Bonobo_PersistStream_load (persist,
130                               (Bonobo_Stream) bonobo_object_corba_objref (BONOBO_OBJECT (stream)),
131                                "application/pdf",
132                                &ev);
133
134     Bonobo_Unknown_unref (persist, &ev);
135     CORBA_Object_release (persist, &ev);
136     CORBA_exception_free (&ev);
137
138 /*    bonobo_view_frame_view_do_verb (comp->view_frame, "ZoomFit"); */
139     return TRUE;
140   }
141   
142   static void
143   set_ok (GtkWidget *widget, gboolean *dialog_result)
144   {
145     *dialog_result = TRUE;
146     gtk_main_quit ();
147   }
148   
149   static guint
150   file_dialog_delete_event (GtkWidget *widget, GdkEventAny *event)
151   {
152     gtk_main_quit ();
153     return TRUE;
154   }
155   
156   static void
157   container_open_cmd (GtkWidget *widget, Container *container)
158   {
159     GtkFileSelection *fsel;
160     gboolean accepted = FALSE;
161     
162     fsel = GTK_FILE_SELECTION (gtk_file_selection_new (_("Load file")));
163     gtk_window_set_modal (GTK_WINDOW (fsel), TRUE);
164     
165     gtk_window_set_transient_for (GTK_WINDOW (fsel),
166                                   GTK_WINDOW (container->app));
167     
168     /* Connect the signals for Ok and Cancel */
169     gtk_signal_connect (GTK_OBJECT (fsel->ok_button), "clicked",
170                         GTK_SIGNAL_FUNC (set_ok), &accepted);
171     gtk_signal_connect (GTK_OBJECT (fsel->cancel_button), "clicked",
172                         GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
173     gtk_window_set_position (GTK_WINDOW (fsel), GTK_WIN_POS_MOUSE);
174     
175     /*
176      * Make sure that we quit the main loop if the window is destroyed 
177      */
178     gtk_signal_connect (GTK_OBJECT (fsel), "delete_event",
179                         GTK_SIGNAL_FUNC (file_dialog_delete_event), NULL);
180     
181     /* Run the dialog */
182     gtk_widget_show (GTK_WIDGET (fsel));
183     gtk_grab_add (GTK_WIDGET (fsel));
184     gtk_main ();
185     
186     if (accepted) {
187       char *name = gtk_file_selection_get_filename (fsel);
188       
189       if (name [strlen (name)-1] != '/') {
190         char *fname = g_strdup (name);
191         if (container->view_widget) /* any sort of MDI sucks :-] */
192           container = container_new (fname);
193         else {
194           if (!open_pdf (container, fname))
195             container_destroy (container);
196         }
197         g_free (fname);
198       } else {
199         GtkWidget *dialog;
200         dialog = gnome_message_box_new (_("Can't open a directory"),
201                                         GNOME_MESSAGE_BOX_ERROR,
202                                         GNOME_STOCK_BUTTON_OK, NULL);
203         gnome_dialog_set_parent (GNOME_DIALOG (dialog),
204                                  GTK_WINDOW (container->app));
205         gnome_dialog_run (GNOME_DIALOG (dialog));
206       }
207     }
208     
209     gtk_widget_destroy (GTK_WIDGET (fsel));
210   }
211
212   static void 
213   component_destroy (Component *component)
214   {
215     CORBA_Environment ev;
216     Container *container;
217     g_return_if_fail (component != NULL);
218
219     CORBA_exception_init (&ev);
220
221     /* Kill merged menus et al. */
222     bonobo_view_frame_view_deactivate (component->view_frame);
223
224     container = component->container;
225     gtk_widget_destroy (container->view_widget);
226     container->view_widget = NULL;
227
228     if (component->server)
229       Bonobo_Unknown_unref (
230         bonobo_object_corba_objref (BONOBO_OBJECT (component->server)), &ev);
231     component->server = NULL;
232
233     CORBA_exception_free (&ev);
234
235     g_free (component);
236   }
237
238   static void
239   container_destroy (Container *cont)
240   {
241     g_return_if_fail (g_list_find (containers, cont) != NULL);
242
243     containers = g_list_remove (containers, cont);
244     if (cont->app)
245       gtk_widget_destroy (cont->app);
246     cont->app = NULL;
247     
248     if (cont->component)
249       component_destroy (cont->component);
250     cont->component = NULL;
251     
252     g_free (cont);
253
254     if (!containers)
255       gtk_main_quit ();
256   }
257
258   static void
259   container_close (Container *cont)
260   {
261     g_return_if_fail (g_list_find (containers, cont) != NULL);
262     
263     if (cont->component) {
264       component_destroy (cont->component);
265       cont->component = NULL;
266     } else
267       container_destroy (cont);
268   }
269
270   
271   static void
272   container_close_cmd (GtkWidget *widget, Container *cont)
273   {
274     container_close (cont);
275   }
276   
277   static int
278   container_destroy_cb (GtkWidget *widget, GdkEvent *event, Container *cont)
279   {
280     container_destroy (cont);
281     return 1;
282   }
283   
284   static void
285   container_exit_cmd (void)
286   {
287     while (containers)
288       container_destroy ((Container *)containers->data);
289   }
290
291 static void
292 container_dump_cmd (GtkWidget *widget, Container *container)
293 {
294         bonobo_window_dump (BONOBO_WINDOW(container->app), "on demand");
295 }
296
297 static void
298 container_about_cmd (GtkWidget *widget, Container *container)
299 {
300   GtkWidget *about;
301
302   const gchar *authors[] = {
303     N_("Derek B. Noonburg, main author"),
304     N_("Michael Meeks, GNOME port maintainer."),
305     N_("Miguel de Icaza."),
306     N_("Nat Friedman."),
307     NULL
308   };
309   
310 #ifdef ENABLE_NLS
311   int i;
312
313   for (i = 0; authors[i] != NULL; i++)
314     authors [i] = _(authors [i]);
315 #endif
316   
317   about = gnome_about_new (_("GPDF"), xpdfVersion,
318                            _("(C) 1996-1999 Derek B. Noonburg."),
319                            authors, NULL, NULL);
320   
321   gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
322   gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
323   gtk_widget_show (about);
324 }
325 }
326
327 static void
328 container_set_view (Container *container, Component *component)
329 {
330         BonoboViewFrame *view_frame;
331         GtkWidget *view_widget;
332
333         /*
334          * Create the remote view and the local ViewFrame.
335          */
336         view_frame = bonobo_client_site_new_view (
337                 component->client_site,
338                 bonobo_ui_component_get_container (container->ui_component));
339
340         component->view_frame = view_frame;
341
342         /*
343          * Embed the view frame into the application.
344          */
345         view_widget = bonobo_view_frame_get_wrapper (view_frame);
346         bonobo_wrapper_set_visibility (BONOBO_WRAPPER (view_widget), FALSE);
347         container->view_widget = view_widget;
348         container->component   = component;
349
350         gtk_container_add (GTK_CONTAINER (container->slot), view_widget);
351
352         /*
353          * Activate it ( get it to merge menus etc. )
354          */
355         bonobo_view_frame_view_activate (view_frame);
356         bonobo_view_frame_set_covered   (view_frame, FALSE);
357
358         gtk_widget_show_all (GTK_WIDGET (container->slot));
359 }
360
361 static BonoboObjectClient *
362 container_launch_component (BonoboClientSite    *client_site,
363                             BonoboItemContainer *container,
364                             char                *component_goad_id)
365 {
366         BonoboObjectClient *object_server;
367
368         /*
369          * Launch the component.
370          */
371         object_server = bonobo_object_activate (component_goad_id, 0);
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 BonoboClientSite 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 (!bonobo_client_site_bind_embeddable (client_site, object_server)) {
383                 bonobo_object_unref (BONOBO_OBJECT (object_server));
384                 return NULL;
385         }
386
387         return object_server;
388 }
389
390 extern "C" {
391   static Component *
392   container_activate_component (Container *container, char *component_goad_id)
393   {
394     Component *component;
395     BonoboClientSite *client_site;
396     BonoboObjectClient *server;
397     
398     /*
399      * The ClientSite is the container-side point of contact for
400      * the Embeddable.  So there is a one-to-one correspondence
401      * between BonoboClientSites and BonoboEmbeddables.  */
402     client_site = bonobo_client_site_new (container->container);
403     
404     /*
405      * A BonoboObjectClient is a simple wrapper for a remote
406      * BonoboObject (a server supporting Bonobo::Unknown).
407      */
408     server = container_launch_component (client_site, container->container,
409                                          component_goad_id);
410     if (server == NULL) {
411       char *error_msg;
412       
413       error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
414                                    component_goad_id);
415       gnome_warning_dialog (error_msg);
416       g_free (error_msg);
417       
418       return NULL;
419     }
420     
421     /*
422      * Create the internal data structure which we will use to
423      * keep track of this component.
424      */
425     component = g_new0 (Component, 1);
426     component->container = container;
427     component->client_site = client_site;
428     component->server = server;
429     
430     container_set_view (container, component);
431
432     return component;
433   }
434   
435   static void
436   filenames_dropped (GtkWidget * widget,
437                      GdkDragContext   *context,
438                      gint              x,
439                      gint              y,
440                      GtkSelectionData *selection_data,
441                      guint             info,
442                      guint             time,
443                      Container        *container)
444   {
445     GList *names, *tmp_list;
446     
447     names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
448     tmp_list = names;
449     
450     while (tmp_list) {
451       const char *fname = (const char *)tmp_list->data;
452
453       if (fname) {
454         if (container->view_widget)
455           container = container_new (fname);
456         else
457           open_pdf (container, fname);
458       }
459
460       tmp_list = g_list_next (tmp_list);
461     }
462   }
463 }
464
465 static Container *
466 container_new (const char *fname)
467 {
468         Container *container;
469         static GtkTargetEntry drag_types[] =
470         {
471           { "text/uri-list", 0, 0 },
472         };
473         static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
474         BonoboUIContainer *ui_container;
475         
476         container = g_new0 (Container, 1);
477
478         container->app = bonobo_window_new ("pdf-viewer",
479                                          _("GNOME PDF viewer"));
480
481         gtk_drag_dest_set (container->app,
482                            GTK_DEST_DEFAULT_ALL,
483                            drag_types, n_drag_types,
484                            GDK_ACTION_COPY);
485
486         gtk_signal_connect (GTK_OBJECT(container->app),
487                             "drag_data_received",
488                             GTK_SIGNAL_FUNC(filenames_dropped),
489                             (gpointer)container);
490
491         gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
492         gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
493
494         container->container   = bonobo_item_container_new ();
495         container->view_widget = NULL;
496         container->slot = gtk_event_box_new ();
497         gtk_widget_show (container->slot);
498
499         bonobo_window_set_contents (BONOBO_WINDOW(container->app),
500                                  GTK_WIDGET (container->slot));
501         gtk_widget_show_all (container->slot);
502
503         gtk_object_set_data (GTK_OBJECT (container->app), "container_data", container);
504         gtk_signal_connect  (GTK_OBJECT (container->app), "delete_event",
505                              GTK_SIGNAL_FUNC (container_destroy_cb), container);
506
507         ui_container = bonobo_ui_container_new ();
508         bonobo_ui_container_set_win (ui_container, BONOBO_WINDOW(container->app));
509
510         container->ui_component = bonobo_ui_component_new ("gpdf");
511         bonobo_ui_component_set_container (
512                 container->ui_component,
513                 bonobo_object_corba_objref (BONOBO_OBJECT (ui_container)));
514
515         bonobo_ui_component_add_verb_list_with_data (
516                 container->ui_component, verbs, container);
517
518         bonobo_ui_util_set_ui (container->ui_component, DATADIR, "gpdf-ui.xml", "gpdf");
519
520         gtk_widget_show (container->app);
521
522         containers = g_list_append (containers, container);
523
524         if (fname)
525           if (!open_pdf (container, fname)) {
526             container_destroy (container);
527             return NULL;
528           }
529
530         gtk_widget_show (container->app);
531
532         return container;
533 }
534
535 int
536 main (int argc, char **argv)
537 {
538         const char      **view_files = NULL;
539         gboolean          loaded;
540         int               i;
541
542         bindtextdomain (PACKAGE, GNOMELOCALEDIR);
543         textdomain (PACKAGE);
544         
545         gnomelib_register_popt_table (oaf_popt_options, "OAF");
546         gnome_init_with_popt_table("PDFViewer", "0." VERSION,
547                                    argc, argv,
548                                    gpdf_popt_options, 0, &ctx); 
549         
550         oaf_init (argc, argv);
551         
552         if (!bonobo_init (CORBA_OBJECT_NIL, 
553                           CORBA_OBJECT_NIL, 
554                           CORBA_OBJECT_NIL))
555                 g_error (_("Could not initialize Bonobo!\n"));
556
557         bonobo_activate ();
558         
559         view_files = poptGetArgs (ctx);
560         
561         /* Load files */
562         i = 0;
563         loaded = FALSE;
564         if (view_files) {
565                 for (i = 0; view_files[i]; i++)
566                         if (container_new (view_files[i])) {
567                                 loaded = TRUE;
568                                 while (gtk_events_pending ())
569                                         gtk_main_iteration ();
570                         }
571         }
572         if ((i == 0) || !loaded)
573                 container_new (NULL);
574         
575         poptFreeContext (ctx);
576         
577         gtk_widget_push_visual (gdk_rgb_get_visual ());
578         gtk_widget_push_colormap (gdk_rgb_get_cmap ());
579
580         gtk_main ();
581         
582         return 0;
583 }