]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/gpdf.cc
da85e0fc4616514c73b426f4bf28baa05122c799
[evince.git] / pdf / xpdf / gpdf.cc
1 //========================================================================
2 //
3 // gpdf.cc
4 //
5 // Copyright 1996 Derek B. Noonburg
6 // Copyright 1999 Michael Meeks.
7 // Copyright 1999 Miguel de Icaza
8 //
9 //========================================================================
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stddef.h>
14 #include <string.h>
15 #define GString G_String
16 #include <gnome.h>
17 #include <glade/glade.h>
18 #undef  GString 
19 #include "gtypes.h"
20 #include "GString.h"
21 #include "parseargs.h"
22 #include "gfile.h"
23 #include "gmem.h"
24 #include "Object.h"
25 #include "Stream.h"
26 #include "Array.h"
27 #include "Dict.h"
28 #include "XRef.h"
29 #include "Catalog.h"
30 #include "Page.h"
31 #include "Link.h"
32 #include "PDFDoc.h"
33 #include "GOutputDev.h"
34 #include "PSOutputDev.h"
35 #include "TextOutputDev.h"
36 #include "Params.h"
37 #include "Error.h"
38 #include "config.h"
39
40 GBool printCommands = gFalse;
41 gint  gpdf_debug=1;
42 poptContext ctx;
43
44 #define DEV_DEBUG 0
45
46 #define DOC_ROOT_TAG "xpdf_doc_root"
47 struct DOC_ROOT {
48   GString        *title;
49   PDFDoc         *pdf;
50   Catalog        *cat;
51   GtkDrawingArea *area;
52   GtkPixmap      *pixmap;
53   OutputDev      *out;
54   GdkColor        paper;
55   GtkScrolledWindow *scroll;
56   GtkWidget      *mainframe;
57   GladeXML       *gui;
58
59   double          zoom;
60   gint            page;
61 };
62
63 GList *documents = NULL;
64
65 const struct poptOption gpdf_popt_options [] = {
66   { "debug", '\0', POPT_ARG_INT, &gpdf_debug, 0,
67     N_("Enables some debugging functions"), N_("LEVEL") },
68   { NULL, '\0', 0, NULL, 0 }
69 };
70
71 extern "C" {
72   static void connect_signals (DOC_ROOT *doc);
73 }
74
75 //------------------------------------------------------------------------
76 // loadFile / displayPage
77 //------------------------------------------------------------------------
78
79 static GtkPixmap *
80 setup_pixmap (DOC_ROOT *doc, GdkWindow *window)
81 {
82   GdkGCValues  gcValues;
83   GdkGC       *strokeGC;
84   PDFDoc      *pdf = doc->pdf;
85   int          w, h;
86   GdkPixmap   *pixmap = NULL;
87
88   w = (int)((pdf->getPageWidth  (doc->page) * doc->zoom) / 72.0);
89   h = (int)((pdf->getPageHeight (doc->page) * doc->zoom) / 72.0);
90
91   pixmap = gdk_pixmap_new (window, w, h, -1);
92
93   gdk_color_white (gtk_widget_get_default_colormap(), &doc->paper);
94   doc->out    = new GOutputDev (pixmap, doc->paper, window);
95
96   gdk_color_white (gtk_widget_get_default_colormap (), &gcValues.foreground);
97   gdk_color_black (gtk_widget_get_default_colormap (), &gcValues.background);
98   gcValues.line_width = 1;
99   gcValues.line_style = GDK_LINE_SOLID;
100   strokeGC = gdk_gc_new_with_values (
101     pixmap, &gcValues, 
102     (enum GdkGCValuesMask)(GDK_GC_FOREGROUND | GDK_GC_BACKGROUND | GDK_GC_LINE_WIDTH | GDK_GC_LINE_STYLE));
103   
104   gdk_draw_rectangle (pixmap, strokeGC,
105                       TRUE, 0, 0,
106                       w, h);
107
108   return GTK_PIXMAP (gtk_pixmap_new (pixmap, NULL));
109 }
110
111 static void
112 render_page (DOC_ROOT *doc)
113 {
114   doc->pdf->displayPage(doc->out, doc->page, doc->zoom, 0, gTrue);
115 }
116
117 /*static void displayPage(int page1, int zoom1, int rotate1) {
118   char s[20];
119
120   // check for document
121   if (!doc)
122     return;
123
124   // busy cursor
125   if (win)
126     win->setBusyCursor(gTrue);
127
128   // new page/zoom/rotate values
129   page = page1;
130   zoom = zoom1;
131   rotate = rotate1;
132
133   // initialize mouse-related stuff
134   linkAction = NULL;
135   win->setDefaultCursor();
136   linkLabel->setText(NULL);
137   selectXMin = selectXMax = 0;
138   selectYMin = selectYMax = 0;
139   lastDragLeft = lastDragTop = gTrue;
140
141   // draw the page
142   doc->displayPage(out, page, zoomDPI[zoom - minZoom], rotate, gTrue);
143   layoutCbk(win);
144
145   // update page number display
146   sprintf(s, "%d", page);
147   pageNumText->setText(s);
148
149   // back to regular cursor
150   win->setBusyCursor(gFalse);
151   }*/
152
153 static PDFDoc *
154 getPDF (GString *fname)
155 {
156   PDFDoc *pdf;
157   pdf = new PDFDoc(fname);
158   if (!pdf->isOk()) {
159     delete pdf;
160     return NULL;
161   }
162   g_return_val_if_fail (pdf->getCatalog(), NULL);
163   return pdf;
164 }
165
166 static DOC_ROOT *
167 doc_root_new (GString *fileName)
168 {
169   DOC_ROOT *doc = new DOC_ROOT();
170   GtkVBox  *pane;
171
172   // open PDF file
173   doc->pdf = getPDF (fileName);
174   if (!doc->pdf) {
175     delete doc;
176     return NULL;
177   }
178
179   doc->gui = glade_xml_new (GPDF_GLADE_DIR "/gpdf.glade", NULL);
180   if (!doc->gui ||
181       !(doc->mainframe = glade_xml_get_widget (doc->gui, "gpdf")) ||
182       !(pane = GTK_VBOX (glade_xml_get_widget (doc->gui, "pane")))) {
183     printf ("Couldn't find " GPDF_GLADE_DIR "/gpdf.glade\n");
184     delete doc->pdf;
185     delete doc;
186     return NULL;
187   }
188
189   doc->zoom = 86.0;
190   doc->page = 1;
191
192   connect_signals (doc);
193
194   gtk_object_set_data (GTK_OBJECT (doc->mainframe), DOC_ROOT_TAG, doc);
195
196   doc->pixmap = setup_pixmap (doc, gtk_widget_get_parent_window (GTK_WIDGET (pane)));
197   
198   doc->scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
199   gtk_scrolled_window_set_policy (doc->scroll, GTK_POLICY_AUTOMATIC,
200                                   GTK_POLICY_AUTOMATIC);
201   render_page (doc);
202   gtk_scrolled_window_add_with_viewport (doc->scroll, GTK_WIDGET (doc->pixmap));
203   gtk_box_pack_start (GTK_BOX (pane), GTK_WIDGET (doc->scroll), TRUE, TRUE, 0);
204
205   gtk_widget_show_all (doc->mainframe);
206
207   documents = g_list_append (documents, doc);
208
209   return doc;
210 }
211
212 static void
213 doc_root_destroy (DOC_ROOT *doc)
214 {
215     gtk_widget_destroy (doc->mainframe);
216     gtk_object_destroy (GTK_OBJECT (doc->gui));
217     
218     documents = g_list_remove (documents, doc);
219     if (g_list_length (documents) == 0)
220       gtk_main_quit ();
221     delete (doc);
222 }
223
224 //------------------------------------------------------------------------
225 //                          Signal handlers
226 //------------------------------------------------------------------------
227
228 extern "C" {
229   void
230   do_close (GtkWidget *menuitem, DOC_ROOT *doc)
231   {
232     doc_root_destroy (doc);
233   }
234
235   void
236   do_exit (GtkWidget *menuitem, DOC_ROOT *doc)
237   {
238     GList *l;
239     while ((l=documents))
240       doc_root_destroy ((DOC_ROOT *)l->data);
241   }
242
243   static void
244   do_about_box (GtkWidget *w, DOC_ROOT *doc)
245   {
246     GladeXML *gui = glade_xml_new (GPDF_GLADE_DIR "/about.glade", NULL);
247     g_return_if_fail (gui);
248     GtkWidget *wi = glade_xml_get_widget (gui, "about_box");
249     g_return_if_fail (wi);
250     gtk_widget_show  (wi);
251     gtk_object_destroy (GTK_OBJECT (gui));
252   }
253
254   static void
255   do_forward_button (GtkWidget *w, DOC_ROOT *doc)
256   {
257     if (doc->page < doc->pdf->getNumPages()) {
258       doc->page++;
259       render_page (doc);
260       gtk_widget_queue_draw (GTK_WIDGET (doc->scroll));
261     }
262   }
263
264   static void
265   do_back_button (GtkWidget *w, DOC_ROOT *doc)
266   {
267     if (doc->page > 1) {
268       doc->page--;
269       render_page (doc);
270       gtk_widget_queue_draw (GTK_WIDGET (doc->pixmap));
271     }
272   }
273
274   static void
275   do_first_button (GtkWidget *w, DOC_ROOT *doc)
276   {
277     if (doc->page != 1) {
278       doc->page = 1;
279       render_page (doc);
280       gtk_widget_queue_draw (GTK_WIDGET (doc->pixmap));
281     }
282   }
283
284   static void
285   do_last_button (GtkWidget *w, DOC_ROOT *doc)
286   {
287     if (doc->page != doc->pdf->getNumPages()) {
288       doc->page = doc->pdf->getNumPages();
289       render_page (doc);
290       gtk_widget_queue_draw (GTK_WIDGET (doc->pixmap));
291     }
292   }
293
294   static void
295   do_larger_button (GtkWidget *w, DOC_ROOT *doc)
296   {
297     if (doc->zoom < 200) {
298       doc->zoom *= 1.2;
299       render_page (doc);
300       gtk_widget_queue_draw (GTK_WIDGET (doc->pixmap));
301     }
302   }
303
304   static void
305   do_smaller_button (GtkWidget *w, DOC_ROOT *doc)
306   {
307     if (doc->zoom < 200) {
308       doc->zoom /= 1.2;
309       render_page (doc);
310       gtk_widget_queue_draw (GTK_WIDGET (doc->pixmap));
311     }
312   }
313   
314   static void
315   simple_menu_connect (DOC_ROOT *doc, const char *name, GtkSignalFunc func)
316   {
317     GtkWidget *w;
318     w = glade_xml_get_widget (doc->gui, name);
319     g_return_if_fail (w);
320     gtk_signal_connect (GTK_OBJECT (w), "activate", func, doc);
321   }
322   
323   static void
324   simple_button_connect (DOC_ROOT *doc, const char *name, GtkSignalFunc func)
325   {
326     GtkWidget *w;
327     w = glade_xml_get_widget (doc->gui, name);
328     g_return_if_fail (w);
329     gtk_signal_connect (GTK_OBJECT (w), "clicked", func, doc);
330   }
331
332   static void
333   connect_signals (DOC_ROOT *doc)
334   {
335     GtkWidget *w;
336
337     simple_menu_connect (doc, "about_menu", GTK_SIGNAL_FUNC (do_about_box)); 
338     simple_menu_connect (doc, "close_menu", GTK_SIGNAL_FUNC (do_close));
339     simple_menu_connect (doc, "exit_menu",  GTK_SIGNAL_FUNC (do_exit));
340
341     simple_button_connect (doc, "forward", GTK_SIGNAL_FUNC (do_forward_button));
342     simple_button_connect (doc, "back",    GTK_SIGNAL_FUNC (do_back_button));
343     simple_button_connect (doc, "first",   GTK_SIGNAL_FUNC (do_first_button));
344     simple_button_connect (doc, "last",    GTK_SIGNAL_FUNC (do_last_button));
345 /*    simple_button_connect (doc, "larger",  GTK_SIGNAL_FUNC (do_larger_button)); need to resize the gtkpixmap...
346       simple_button_connect (doc, "smaller", GTK_SIGNAL_FUNC (do_smaller_button)); but bed first. */
347
348     gtk_signal_connect (GTK_OBJECT (doc->mainframe), "destroy",
349                         GTK_SIGNAL_FUNC (do_close), doc);
350  }
351 }
352
353 int
354 main (int argc, char *argv [])
355 {
356   char **view_files = NULL;
357   int    i;
358   
359   gnome_init_with_popt_table (
360     "gpdf", "0.1", argc, argv,
361     gpdf_popt_options, 0, &ctx);
362
363   errorInit();
364   
365   initParams (xpdfConfigFile); /* Init font path */
366
367   glade_gnome_init ();
368
369   view_files = poptGetArgs (ctx);
370   /* Load files */
371   if (view_files) {
372     for (i = 0; view_files[i]; i++) {
373       GString *name = new GString (view_files[i]);
374       if (!name || !doc_root_new (name))
375         printf ("Error loading '%s'\n", view_files[i]);
376     }
377   } else {
378     printf ("Need filenames...\n");
379     exit (0);
380   }
381
382   poptFreeContext (ctx);
383
384   gtk_main ();
385
386   freeParams();
387
388   /* Destroy files */
389 }