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