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