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