]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/gpdf.cc
cd8149482ee0da873bc281f6fdea4976cd99cd1e
[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 doesn't implement PersistStream.");
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   int i;
315
316   for (i = 0; authors[i] != NULL; i++)
317     authors [i] = _(authors [i]);
318 #endif
319   
320   about = gnome_about_new (_("GPDF"), xpdfVersion,
321                            _("(C) 1996-1999 Derek B. Noonburg."),
322                            authors, NULL, NULL);
323   
324   gnome_dialog_set_parent (GNOME_DIALOG (about), GTK_WINDOW (container->app));
325   gnome_dialog_set_close (GNOME_DIALOG (about), TRUE);
326   gtk_widget_show (about);
327 }
328 }
329
330 static void
331 container_set_view (Container *container, Component *component)
332 {
333         BonoboViewFrame *view_frame;
334         GtkWidget *view_widget;
335
336         /*
337          * Create the remote view and the local ViewFrame.
338          */
339         view_frame = bonobo_client_site_new_view (component->client_site,
340                                                   bonobo_object_corba_objref (BONOBO_OBJECT (
341                                                           container->uih)));
342         component->view_frame = view_frame;
343
344         /*
345          * Embed the view frame into the application.
346          */
347         view_widget = bonobo_view_frame_get_wrapper (view_frame);
348         bonobo_wrapper_set_visibility (BONOBO_WRAPPER (view_widget), FALSE);
349         container->view_widget = view_widget;
350         container->component   = component;
351
352         /*
353          * Show the component.
354          */
355         gtk_scrolled_window_add_with_viewport (container->scroll, view_widget);
356
357         /*
358          * Activate it ( get it to merge menus etc. )
359          */
360         bonobo_view_frame_view_activate (view_frame);
361         bonobo_view_frame_set_covered   (view_frame, FALSE);
362
363         gtk_widget_show_all (GTK_WIDGET (container->scroll));
364 }
365
366 static BonoboObjectClient *
367 container_launch_component (BonoboClientSite *client_site,
368                             BonoboContainer *container,
369                             char *component_goad_id)
370 {
371         BonoboObjectClient *object_server;
372
373         /*
374          * Launch the component.
375          */
376         object_server = bonobo_object_activate_with_goad_id (
377                 NULL, component_goad_id, GOAD_ACTIVATE_SHLIB, NULL);
378
379         if (object_server == NULL)
380                 return NULL;
381
382         /*
383          * Bind it to the local ClientSite.  Every embedded component
384          * has a local BonoboClientSite object which serves as a
385          * container-side point of contact for the embeddable.  The
386          * container talks to the embeddable through its ClientSite
387          */
388         if (!bonobo_client_site_bind_embeddable (client_site, object_server)) {
389                 bonobo_object_unref (BONOBO_OBJECT (object_server));
390                 return NULL;
391         }
392
393         /*
394          * The BonoboContainer object maintains a list of the
395          * ClientSites which it manages.  Here we add the new
396          * ClientSite to that list.
397          */
398         bonobo_container_add (container, BONOBO_OBJECT (client_site));
399
400         return object_server;
401 }
402
403 extern "C" {
404   static Component *
405   container_activate_component (Container *container, char *component_goad_id)
406   {
407     Component *component;
408     BonoboClientSite *client_site;
409     BonoboObjectClient *server;
410     
411     /*
412      * The ClientSite is the container-side point of contact for
413      * the Embeddable.  So there is a one-to-one correspondence
414      * between BonoboClientSites and BonoboEmbeddables.  */
415     client_site = bonobo_client_site_new (container->container);
416     
417     /*
418      * A BonoboObjectClient is a simple wrapper for a remote
419      * BonoboObject (a server supporting Bonobo::Unknown).
420      */
421     server = container_launch_component (client_site, container->container,
422                                          component_goad_id);
423     if (server == NULL) {
424       char *error_msg;
425       
426       error_msg = g_strdup_printf (_("Could not launch Embeddable %s!"),
427                                    component_goad_id);
428       gnome_warning_dialog (error_msg);
429       g_free (error_msg);
430       
431       return NULL;
432     }
433     
434     /*
435      * Create the internal data structure which we will use to
436      * keep track of this component.
437      */
438     component = g_new0 (Component, 1);
439     component->container = container;
440     component->client_site = client_site;
441     component->server = server;
442     
443     container_set_view (container, component);
444
445     return component;
446   }
447   
448   static void
449   filenames_dropped (GtkWidget * widget,
450                      GdkDragContext   *context,
451                      gint              x,
452                      gint              y,
453                      GtkSelectionData *selection_data,
454                      guint             info,
455                      guint             time,
456                      Container        *container)
457   {
458     GList *names, *tmp_list;
459     
460     names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
461     tmp_list = names;
462     
463     while (tmp_list) {
464       const char *fname = (const char *)tmp_list->data;
465
466       if (fname) {
467         if (container->view_widget)
468           container = container_new (fname);
469         else
470           open_pdf (container, fname);
471       }
472
473       tmp_list = g_list_next (tmp_list);
474     }
475   }
476   
477   /*
478    * GtkWidget key_press method override
479    *
480    * Scrolls the window on keypress
481    */
482   static gint
483   key_press_event_cb (GtkWidget *widget, GdkEventKey *event)
484   {
485     Container *container = (Container *) gtk_object_get_data (GTK_OBJECT (widget), "container_data");
486     Component *component;
487     GtkScrolledWindow *win;
488     float              delta;
489
490     g_return_val_if_fail (container != NULL, FALSE);
491
492     win       = container->scroll;
493     component = container->component;
494     if (component == NULL || win == NULL)
495       return FALSE;
496
497     /*
498      * Scrolling the view.
499      */
500     if (event->keyval == GDK_Up) {
501       GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment (win);
502
503       if (event->state & GDK_CONTROL_MASK)
504         delta = adj->step_increment * 3;
505       else
506         delta = adj->step_increment;
507
508       adj->value = CLAMP (adj->value - delta,
509                           adj->lower, adj->upper - adj->page_size);
510
511       gtk_adjustment_value_changed (adj);
512       return TRUE;
513     } else if (event->keyval == GDK_Down) {
514       GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment (win);
515
516       if (event->state & GDK_CONTROL_MASK)
517         delta = adj->step_increment * 3;
518       else
519         delta = adj->step_increment;
520
521       adj->value = CLAMP (adj->value + delta,
522                           adj->lower, adj->upper - adj->page_size);
523       gtk_adjustment_value_changed (adj);
524       return TRUE;
525     } else if (event->keyval == GDK_Left) {
526       GtkAdjustment *adj = gtk_scrolled_window_get_hadjustment (win);
527
528       if (event->state & GDK_CONTROL_MASK)
529         delta = adj->step_increment * 3;
530       else
531         delta = adj->step_increment;
532
533       adj->value = CLAMP (adj->value - delta,
534                           adj->lower, adj->upper - adj->page_size);
535       gtk_adjustment_value_changed (adj);
536       return TRUE;
537     } else if (event->keyval == GDK_Right) {
538       GtkAdjustment *adj = gtk_scrolled_window_get_hadjustment (win);
539
540       if (event->state & GDK_CONTROL_MASK)
541         delta = adj->step_increment * 3;
542       else
543         delta = adj->step_increment;
544
545       adj->value = CLAMP (adj->value + delta,
546                           adj->lower, adj->upper - adj->page_size);
547       gtk_adjustment_value_changed (adj);
548       return TRUE;
549
550       /*
551        * Various shortcuts mapped to verbs.
552        */
553
554     } else if (event->keyval == GDK_Home) {
555       bonobo_view_frame_view_do_verb (component->view_frame, VERB_FIRST);
556       return TRUE;
557     } else if (event->keyval == GDK_End) {
558       bonobo_view_frame_view_do_verb (component->view_frame, VERB_LAST);
559       return TRUE;
560     } else if (event->keyval == GDK_Page_Down ||
561                event->keyval == GDK_Next) {
562       bonobo_view_frame_view_do_verb (component->view_frame, VERB_NEXT);
563       return TRUE;
564     } else if (event->keyval == GDK_Page_Up ||
565                event->keyval == GDK_Prior) {
566       bonobo_view_frame_view_do_verb (component->view_frame, VERB_PREV);
567       return TRUE;
568     } else if (event->keyval == GDK_plus ||
569                event->keyval == GDK_equal) {
570       bonobo_view_frame_view_do_verb (component->view_frame, VERB_Z_IN);
571     } else if (event->keyval == GDK_underscore ||
572                event->keyval == GDK_minus) {
573       bonobo_view_frame_view_do_verb (component->view_frame, VERB_Z_OUT);
574     }    
575     return FALSE;
576   }
577 }
578
579 static void
580 container_create_menus (Container *container)
581 {
582         BonoboUIHandlerMenuItem *menu_list;
583
584         bonobo_ui_handler_create_menubar (container->uih);
585
586         /*
587          * Create the basic menus out of UIInfo structures.
588          */
589         menu_list = bonobo_ui_handler_menu_parse_uiinfo_list_with_data (container_main_menu, container);
590         bonobo_ui_handler_menu_add_list (container->uih, "/", menu_list);
591         bonobo_ui_handler_menu_free_list (menu_list);
592 }
593
594 static void
595 container_create_toolbar (Container *container)
596 {
597         BonoboUIHandlerToolbarItem *toolbar;
598
599         bonobo_ui_handler_create_toolbar (container->uih, "pdf");
600         toolbar = bonobo_ui_handler_toolbar_parse_uiinfo_list_with_data (container_toolbar, container);
601         bonobo_ui_handler_toolbar_add_list (container->uih, "/pdf/", toolbar);
602         bonobo_ui_handler_toolbar_free_list (toolbar);
603 }
604
605 static Container *
606 container_new (const char *fname)
607 {
608         Container *container;
609         static GtkTargetEntry drag_types[] =
610         {
611           { "text/uri-list", 0, 0 },
612         };
613         static gint n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);
614         
615         container = g_new0 (Container, 1);
616
617         container->app = gnome_app_new ("pdf-viewer",
618                                         "GNOME PDF viewer");
619
620         gtk_drag_dest_set (container->app,
621                            GTK_DEST_DEFAULT_ALL,
622                            drag_types, n_drag_types,
623                            GDK_ACTION_COPY);
624
625         gtk_signal_connect (GTK_OBJECT(container->app),
626                             "drag_data_received",
627                             GTK_SIGNAL_FUNC(filenames_dropped),
628                             (gpointer)container);
629
630         gtk_window_set_default_size (GTK_WINDOW (container->app), 600, 600);
631         gtk_window_set_policy (GTK_WINDOW (container->app), TRUE, TRUE, FALSE);
632
633         container->container   = bonobo_container_new ();
634         container->view_widget = NULL;
635         container->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
636         gtk_scrolled_window_set_policy (container->scroll, GTK_POLICY_ALWAYS,
637                                         GTK_POLICY_ALWAYS);
638         gnome_app_set_contents (GNOME_APP (container->app), GTK_WIDGET (container->scroll));
639
640         gtk_object_set_data (GTK_OBJECT (container->app), "container_data", container);
641         gtk_signal_connect  (GTK_OBJECT (container->app), "key_press_event",
642                              GTK_SIGNAL_FUNC (key_press_event_cb), NULL);
643         gtk_signal_connect  (GTK_OBJECT (container->app), "delete_event",
644                              GTK_SIGNAL_FUNC (container_destroy_cb), container);
645
646         /*
647          * Create the BonoboUIHandler object which will be used to
648          * create the container's menus and toolbars.  The UIHandler
649          * also creates a CORBA server which embedded components use
650          * to do menu/toolbar merging.
651          */
652         container->uih = bonobo_ui_handler_new ();
653         bonobo_ui_handler_set_app (container->uih, GNOME_APP (container->app));
654
655         container_create_menus   (container);
656         container_create_toolbar (container);
657
658         gtk_widget_show_all (container->app);
659
660         containers = g_list_append (containers, container);
661
662         if (fname)
663           if (!open_pdf (container, fname)) {
664             container_destroy (container);
665             return NULL;
666           }
667
668         gtk_widget_show_all (container->app);
669
670         return container;
671 }
672
673 int
674 main (int argc, char **argv)
675 {
676   CORBA_Environment ev;
677   CORBA_ORB         orb;
678   const char      **view_files = NULL;
679   gboolean          loaded;
680   int               i;
681   
682   CORBA_exception_init (&ev);
683   
684   gnome_CORBA_init_with_popt_table ("PDFViewer", "0.0.1",
685                                     &argc, argv,
686                                     gpdf_popt_options, 0, &ctx,
687                                     GNORBA_INIT_SERVER_FUNC, &ev);
688
689   CORBA_exception_free (&ev);
690
691   orb = gnome_CORBA_ORB ();
692
693   if (bonobo_init (orb, NULL, NULL) == FALSE)
694     g_error (_("Could not initialize Bonobo!\n"));
695   bonobo_activate ();
696
697   view_files = poptGetArgs (ctx);
698
699   /* Load files */
700   i = 0;
701   loaded = FALSE;
702   if (view_files) {
703     for (i = 0; view_files[i]; i++)
704       if (container_new (view_files[i])) {
705         loaded = TRUE;
706         while (gtk_events_pending ())
707           gtk_main_iteration ();
708       }
709   }
710   if ((i == 0) || !loaded)
711     container_new (NULL);
712   
713   poptFreeContext (ctx);
714
715   gtk_main ();
716         
717   return 0;
718 }