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