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