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