Fix printed PDF quality
[claws.git] / src / plugins / pdf_viewer / poppler_viewer.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright(C) 1999-2007 the Claws Mail Team
4  * This file Copyright (C) 2007 Salvatore De Paolis 
5  * <iwkse@claws-mail.org> 
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  *(at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #include "claws-features.h"
25 #endif
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29
30 #include "poppler_viewer.h"
31 #include "printing.h"
32 #include "prefs_common.h"
33 #include "gtk/gtkutils.h"
34 #include "mimeview.h"
35 #ifndef POPPLER_WITH_GDK
36 #include "stdbool.h"
37 #endif
38
39 static FileType pdf_viewer_mimepart_get_type(MimeInfo *partinfo);
40 static MimeViewerFactory pdf_viewer_factory;
41
42 static void pdf_viewer_show_mimepart(MimeViewer *_viewer, const gchar *infile,
43                                 MimeInfo *partinfo);
44
45 static MimeViewer *pdf_viewer_create(void);
46 static void pdf_viewer_clear(MimeViewer *_viewer);
47 static void pdf_viewer_destroy(MimeViewer *_viewer);
48 static void pdf_viewer_update(MimeViewer *_viewer, gboolean reload_file, int page_num);
49
50 static GtkWidget *pdf_viewer_get_widget(MimeViewer *_viewer);
51
52 static void pdf_viewer_hide_index_pane(PdfViewer *viewer);
53 static void pdf_viewer_set_index_button_sensitive(PdfViewer *viewer);
54 static void pdf_viewer_scroll_to(PdfViewer *viewer, gfloat x, gfloat y);
55
56 static void search_matches_free(PdfViewer *viewer);
57 static gboolean pdf_viewer_text_search(MimeViewer *_viewer, gboolean backward,
58                                      const gchar *str, gboolean case_sens);
59 static void pdf_viewer_render_selection(PdfViewer *viewer, PopplerRectangle *rect, PageResult *page_results);
60 static void pdf_viewer_render_page(PopplerPage *page, GtkWidget *view, double width, double height, double zoom, gint rotate);
61
62 static char * pdf_viewer_get_document_format_data(GTime utime);
63 static void pdf_viewer_get_document_index(PdfViewer *viewer, PopplerIndexIter *index_iter, GtkTreeIter *parentiter);
64 static void pdf_viewer_index_row_activated(GtkTreeView          *tree_view,
65                                         GtkTreePath             *path,
66                                         GtkTreeViewColumn       *column,
67                                         gpointer                 data);
68
69 static GtkTable * pdf_viewer_fill_info_table(PdfViewer *viewer);
70
71 /* Callbacks */
72 static void pdf_viewer_move_events_cb(GtkWidget *widget, GdkEventMotion *event, PdfViewer *viewer); 
73 static void pdf_viewer_button_press_events_cb(GtkWidget *widget, GdkEventButton *event, PdfViewer *viewer); 
74 static void pdf_viewer_mouse_scroll_destroy_cb(GtkWidget *widget, GdkEventButton *event, PdfViewer *viewer); 
75 static void pdf_viewer_button_first_page_cb(GtkButton *button, PdfViewer *viewer);
76 static void pdf_viewer_button_last_page_cb(GtkButton *button, PdfViewer *viewer);
77 static void pdf_viewer_button_zoom_in_cb(GtkButton *button, PdfViewer *viewer);
78 static void pdf_viewer_button_zoom_out_cb(GtkButton *button, PdfViewer *viewer);
79 static void pdf_viewer_button_zoom_fit_cb(GtkButton *button, PdfViewer *viewer);
80 static void pdf_viewer_button_zoom_width_cb(GtkButton *button, PdfViewer *viewer);
81 static void pdf_viewer_button_rotate_right_cb(GtkButton *button, PdfViewer *viewer);
82 static void pdf_viewer_button_rotate_left_cb(GtkButton *button, PdfViewer *viewer);
83 static void pdf_viewer_spin_change_page_cb(GtkSpinButton *button, PdfViewer *viewer);
84 static void pdf_viewer_spin_zoom_scroll_cb(GtkSpinButton *button, PdfViewer *viewer);
85 /* Show/Hide the index pane */
86 static void pdf_viewer_show_document_index_cb(GtkButton *button, PdfViewer *viewer);
87 static void pdf_viewer_button_document_info_cb(GtkButton *button, PdfViewer *viewer);
88
89 static void pdf_viewer_show_controls(PdfViewer *viewer, gboolean show);
90 static gboolean pdf_viewer_scroll_page(MimeViewer *_viewer, gboolean up);
91 static void pdf_viewer_scroll_one_line(MimeViewer *_viewer, gboolean up);
92
93 /** Claws-Mail Plugin functions*/
94 gint plugin_init(gchar **error);
95 const gchar *plugin_name(void);
96 const gchar *plugin_desc(void);
97 const gchar *plugin_type(void);
98 const gchar *plugin_licence(void);
99 const gchar *plugin_version(void);
100 struct PluginFeature *plugin_provides(void);
101
102 #ifndef POPPLER_WITH_GDK
103 static void
104 copy_cairo_surface_to_pixbuf (cairo_surface_t *surface,
105                               GdkPixbuf       *pixbuf)
106 {
107         int cairo_width, cairo_height, cairo_rowstride;
108         unsigned char *pixbuf_data, *dst, *cairo_data;
109         int pixbuf_rowstride, pixbuf_n_channels;
110         unsigned int *src;
111         int x, y;
112
113         cairo_width = cairo_image_surface_get_width (surface);
114         cairo_height = cairo_image_surface_get_height (surface);
115         cairo_rowstride = cairo_image_surface_get_stride (surface);
116         cairo_data = cairo_image_surface_get_data (surface);
117
118         pixbuf_data = gdk_pixbuf_get_pixels (pixbuf);
119         pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
120         pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
121
122         if (cairo_width > gdk_pixbuf_get_width (pixbuf))
123                 cairo_width = gdk_pixbuf_get_width (pixbuf);
124         if (cairo_height > gdk_pixbuf_get_height (pixbuf))
125                 cairo_height = gdk_pixbuf_get_height (pixbuf);
126         for (y = 0; y < cairo_height; y++) {
127                 src = (unsigned int *) (cairo_data + y * cairo_rowstride);
128                 dst = pixbuf_data + y * pixbuf_rowstride;
129                 for (x = 0; x < cairo_width; x++) {
130                         dst[0] = (*src >> 16) & 0xff;
131                         dst[1] = (*src >> 8) & 0xff; 
132                         dst[2] = (*src >> 0) & 0xff;
133                         if (pixbuf_n_channels == 4)
134                                 dst[3] = (*src >> 24) & 0xff;
135                         dst += pixbuf_n_channels;
136                         src++;
137                 }
138         }
139 }
140 static void
141 _poppler_page_render_to_pixbuf (PopplerPage *page,
142                                 int src_x, int src_y,
143                                 int src_width, int src_height,
144                                 double scale,
145                                 int rotation,
146                                 gboolean printing,
147                                 GdkPixbuf *pixbuf)
148 {
149         cairo_t *cr;
150         cairo_surface_t *surface;
151
152         surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
153                                         src_width, src_height);
154         cr = cairo_create (surface);
155         cairo_save (cr);
156         switch (rotation) {
157         case 90:
158                 cairo_translate (cr, src_x + src_width, -src_y);
159                 break;
160         case 180:
161                 cairo_translate (cr, src_x + src_width, src_y + src_height);
162                 break;
163         case 270:
164                 cairo_translate (cr, -src_x, src_y + src_height);
165                 break;
166         default:
167                 cairo_translate (cr, -src_x, -src_y);
168         }
169
170         if (scale != 1.0)
171                 cairo_scale (cr, scale, scale);
172
173         if (rotation != 0)
174                 cairo_rotate (cr, rotation * G_PI / 180.0);
175
176         if (printing)
177                 poppler_page_render_for_printing (page, cr);
178         else
179                 poppler_page_render (page, cr);
180         cairo_restore (cr);
181
182         cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
183         cairo_set_source_rgb (cr, 1., 1., 1.);
184         cairo_paint (cr);
185
186         cairo_destroy (cr);
187
188         copy_cairo_surface_to_pixbuf (surface, pixbuf);
189         cairo_surface_destroy (surface);
190 }
191
192 /**
193  * poppler_page_render_to_pixbuf:
194  * @page: the page to render from
195  * @src_x: x coordinate of upper left corner  
196  * @src_y: y coordinate of upper left corner  
197  * @src_width: width of rectangle to render  
198  * @src_height: height of rectangle to render
199  * @scale: scale specified as pixels per point
200  * @rotation: rotate the document by the specified degree
201  * @pixbuf: pixbuf to render into
202  *
203  * First scale the document to match the specified pixels per point,
204  * then render the rectangle given by the upper left corner at
205  * (src_x, src_y) and src_width and src_height.
206  * This function is for rendering a page that will be displayed.
207  * If you want to render a page that will be printed use
208  * poppler_page_render_to_pixbuf_for_printing() instead
209  *
210  * Deprecated: 0.16
211  **/
212 static void
213 poppler_page_render_to_pixbuf (PopplerPage *page,
214                                int src_x, int src_y,
215                                int src_width, int src_height,
216                                double scale,
217                                int rotation,
218                                GdkPixbuf *pixbuf)
219 {
220         g_return_if_fail (POPPLER_IS_PAGE (page));
221         g_return_if_fail (scale > 0.0);
222         g_return_if_fail (pixbuf != NULL);
223
224         _poppler_page_render_to_pixbuf (page, src_x, src_y,
225                                   src_width, src_height,
226                                   scale, rotation,
227                                   FALSE,
228                                   pixbuf);
229 }
230 #endif
231 static GtkWidget *pdf_viewer_get_widget(MimeViewer *_viewer)
232 {
233         PdfViewer *viewer = (PdfViewer *) _viewer;
234         debug_print("pdf_viewer_get_widget: %p\n", viewer->vbox);
235
236         return GTK_WIDGET(viewer->vbox);
237 }
238 /** Hide the index panel */
239 static void pdf_viewer_hide_index_pane(PdfViewer *viewer)
240 {
241         if (viewer->pdf_index) {   
242                 poppler_index_iter_free(viewer->pdf_index);
243                 viewer->pdf_index = NULL;
244                 gtk_widget_hide(GTK_WIDGET(viewer->frame_index));
245         }
246 }
247
248 static void search_matches_free(PdfViewer *viewer)
249 {
250         GList *cur; 
251         for(cur = viewer->text_found; cur; cur = cur->next) {
252                 PageResult *res = (PageResult *)cur->data;
253                 g_list_free(res->results);
254                 g_free(res);
255         }
256         g_list_free(viewer->text_found);
257         viewer->text_found = NULL;
258         g_free(viewer->last_search);
259         viewer->last_search = NULL;
260         if (viewer->last_rect && viewer->last_page_result) {
261                 viewer->last_rect = NULL;
262                 viewer->last_page_result = NULL;
263                 pdf_viewer_update((MimeViewer *)viewer, 
264                         FALSE, 
265                         gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page)));
266         }
267 }
268
269 static void pdf_viewer_scroll_to(PdfViewer *viewer, gfloat x, gfloat y)
270 {
271         GtkAdjustment *vadj;
272         GtkAdjustment *hadj;
273         vadj = gtk_scrolled_window_get_vadjustment(
274                 GTK_SCROLLED_WINDOW(viewer->scrollwin));
275         
276         if (y < vadj->value) {
277                 vadj->value = y;
278         }
279         else {
280                 while(y > vadj->value + vadj->page_size) {
281                         vadj->value += vadj->page_size;
282                 }
283         }
284         
285         hadj = gtk_scrolled_window_get_hadjustment(
286                 GTK_SCROLLED_WINDOW(viewer->scrollwin));
287         
288         if (x < hadj->value) {
289                 hadj->value = x;
290         }
291         else {
292                 while(x > hadj->value + hadj->page_size) {
293                         hadj->value += hadj->page_size;
294                 }
295         }
296
297         g_signal_emit_by_name(G_OBJECT(hadj), "value-changed", 0);      
298         g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);      
299 }
300 static void pdf_viewer_render_page(PopplerPage *page, GtkWidget *view, double width, 
301                                    double height, double zoom, gint rotate)
302 {
303         GdkPixbuf *pb;
304         
305         debug_print("width: %f\n", width);
306         pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, 
307                                 FALSE, 8, 
308                                 (int)(width * zoom), 
309                                 (int)(height * zoom));  
310         
311                         poppler_page_render_to_pixbuf(page, 0, 0, 
312                                 (int)(width * zoom), 
313                                 (int)(height * zoom), 
314                                 zoom, rotate, pb);
315                 
316                         gtk_image_set_from_pixbuf(GTK_IMAGE(view), pb);
317                         g_object_unref(G_OBJECT(pb));
318 }
319 static void pdf_viewer_render_selection(PdfViewer *viewer, PopplerRectangle *rect, PageResult *page_results)
320 {
321         gint selw, selh;
322         double width_points, height_points;
323         gint width, height;
324         GdkPixbuf *sel_pb, *page_pb;
325         gfloat x1, x2, y1, y2;  
326         
327
328         gint cur_page_num = 
329                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page));
330         
331         viewer->last_match = viewer->res_cnt;
332         
333         viewer->last_rect = NULL;
334         viewer->last_page_result = NULL;
335         if (cur_page_num != page_results->page_num) {
336                 /* we changed page. update the view */
337                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->cur_page), 
338         (gdouble) page_results->page_num);
339         }
340                         
341         viewer->last_rect = rect;
342         viewer->last_page_result = page_results;
343
344         GTK_EVENTS_FLUSH();
345                                 
346         poppler_page_get_size(POPPLER_PAGE(viewer->pdf_page), &width_points, &height_points);
347         width = (int)((width_points * viewer->zoom) + 0.5);
348         height = (int)((height_points * viewer->zoom) + 0.5);
349
350         if (viewer->rotate == 90) {
351                 x1 = MIN(rect->y1,rect->y2) * viewer->zoom;
352                 x2 = MAX(rect->y1,rect->y2) * viewer->zoom;
353                 y1 = MAX(rect->x1,rect->x2) * viewer->zoom;
354                 y2 = MIN(rect->x1,rect->x2) * viewer->zoom;
355                 selw = (x2 - x1);
356                 selh = (y1 - y2);
357
358         } else if (viewer->rotate == 180) {
359                 x1 = width - rect->x2 * viewer->zoom;
360                 x2 = width - rect->x1 * viewer->zoom;
361                 y1 = height - rect->y2 * viewer->zoom;
362                 y2 = height - rect->y1 * viewer->zoom;
363                 selw = (x2 - x1);
364                 selh = (y2 - y1);
365                 y1 = height - y1;
366                 y2 = height - y2;
367
368         } else if (viewer->rotate == 270) {
369                 x1 = height - MAX(rect->y1,rect->y2) * viewer->zoom;
370                 x2 = height - MIN(rect->y1,rect->y2) * viewer->zoom;
371                 y1 = width - MIN(rect->x1,rect->x2) * viewer->zoom;
372                 y2 = width - MAX(rect->x1,rect->x2) * viewer->zoom;
373                 selw = (x2 - x1);
374                 selh = (y1 - y2);
375         } else {
376                 x1 = rect->x1 * viewer->zoom;
377                 x2 = rect->x2 * viewer->zoom;
378                 y1 = rect->y1 * viewer->zoom;
379                 y2 = rect->y2 * viewer->zoom;
380                 selw = (x2 - x1);
381                 selh = (y2 - y1);
382                 y1 = height - y1;
383                 y2 = height - y2;
384         }
385                                 
386         sel_pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 
387                                                         selw, selh);
388
389         gdk_pixbuf_fill(sel_pb, SELECTION_COLOR);
390                                 
391         page_pb = gtk_image_get_pixbuf(GTK_IMAGE(viewer->pdf_view));
392                                 
393         page_pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, 
394                                         FALSE, 8, 
395                                 (int)(viewer->width * viewer->zoom), 
396                                 (int)(viewer->height * viewer->zoom));  
397         
398         poppler_page_render_to_pixbuf(viewer->pdf_page, 
399                                         0, 
400                                         0, 
401                                 (int)(viewer->width * viewer->zoom), 
402                                 (int)(viewer->height * viewer->zoom), 
403                                         viewer->zoom, 
404                                         viewer->rotate, 
405                                         page_pb);
406                                 
407         gdk_pixbuf_composite(sel_pb, page_pb, 
408                                         x1, y2, selw, selh, 0, 0, 
409                                         viewer->zoom, viewer->zoom, 
410                                         GDK_INTERP_BILINEAR, ALPHA_CHANNEL);
411                                 
412         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->pdf_view), page_pb);
413         
414         pdf_viewer_scroll_to(viewer, MIN(x1,x2), MIN(y1,y2));
415
416         g_object_unref(G_OBJECT(sel_pb));
417         g_object_unref(G_OBJECT(page_pb));
418
419 }
420
421 static gboolean pdf_viewer_text_search(MimeViewer *_viewer, gboolean backward,
422                                      const gchar *str, gboolean case_sens)
423 {
424         PdfViewer *viewer = (PdfViewer *)_viewer;
425         GList *all_pages_results, *cur_page_results;
426         viewer->res_cnt = 0;
427
428         debug_print("pdf_viewer_text_search: %s\n", str);
429         main_window_cursor_wait(mainwindow_get_mainwindow());
430         
431         if (viewer->last_search && strcmp(str, viewer->last_search)) {
432                 search_matches_free(viewer);
433                 viewer->last_match = -1;
434                 viewer->num_matches = 0;
435         } else if (!viewer->last_search) {
436                 viewer->last_match = -1;
437                 viewer->num_matches = 0;
438         }
439         /* It's a new search, build list of matches 
440          * across all pages */
441         if (viewer->last_match == -1) {
442                 gint i; 
443                 
444                 for(i = 1; i <= viewer->num_pages; i++) {
445                         
446                         PopplerPage *pdf_page = poppler_document_get_page(viewer->pdf_doc, i - 1);
447                         viewer->page_results = poppler_page_find_text(pdf_page, str);
448                         
449                         if (viewer->page_results != NULL) {
450                                 debug_print("page_results %p\n", viewer->page_results);
451                                 /* store results for this page */
452                                 gint num_res = 0;
453                                 PageResult *res = g_new0(PageResult, 1);
454                                 res->results = viewer->page_results;
455                                 res->page_num = i;
456                                 /* found text, prepend this page(faster than append) */
457                                 viewer->text_found = g_list_prepend(viewer->text_found, res);
458                                 num_res = g_list_length(viewer->page_results);
459                                 debug_print("%d results on page %d\n", num_res, i);
460                                 viewer->num_matches += num_res;
461                         }
462                         g_object_unref(G_OBJECT(pdf_page));
463                 }
464
465                 if (viewer->text_found == NULL) {
466                         main_window_cursor_normal(mainwindow_get_mainwindow());
467                         return FALSE;
468                 }
469                 /* put back the list in the correct order */
470                 viewer->text_found = g_list_reverse(viewer->text_found);
471         } 
472         
473         if (!viewer->text_found) {
474                 main_window_cursor_normal(mainwindow_get_mainwindow());
475                 return FALSE;
476         } else {
477                 viewer->last_search = g_strdup(str);
478         }
479         
480         if (backward) {
481                 /* if backward, we have to initialize stuff to search 
482                  * from the end */
483                 viewer->res_cnt = viewer->num_matches-1;
484                 if (viewer->last_match == -1) {
485                         viewer->last_match = viewer->num_matches+1;
486                 }
487                 all_pages_results = g_list_last(viewer->text_found);
488         } 
489         else {
490                 all_pages_results = viewer->text_found;
491         }
492         
493         for(; all_pages_results; all_pages_results = (backward?all_pages_results->prev:all_pages_results->next)) {
494                 
495                 PageResult * page_results = (PageResult *)all_pages_results->data;
496
497                 if (backward) {
498                         cur_page_results = g_list_last(page_results->results);
499                 }
500                 else {
501                         cur_page_results = page_results->results;
502                 }
503
504                 for(; cur_page_results; cur_page_results = (backward?cur_page_results->prev:cur_page_results->next)) {
505
506                         gboolean valid = FALSE;
507                         /* first valid result is the last+1 if searching
508                          * forward, last-1 if searching backward */
509                         if (backward) {
510                                 valid = (viewer->res_cnt < viewer->last_match);
511                         }
512                         else {
513                                 valid = (viewer->res_cnt > viewer->last_match);
514                         }
515                         if (valid) {
516                                 pdf_viewer_render_selection(viewer, 
517                                         (PopplerRectangle *)cur_page_results->data,
518                                                 page_results);
519                                 main_window_cursor_normal(mainwindow_get_mainwindow());
520                                 return TRUE;
521                         }
522                         
523                         if (backward) {
524                                 viewer->res_cnt--;
525                         }
526                         else {
527                                 viewer->res_cnt++;
528                         }
529                 }
530         }
531         main_window_cursor_normal(mainwindow_get_mainwindow());
532         search_matches_free(viewer);
533         return FALSE;
534 }
535
536 static void pdf_viewer_get_document_index(PdfViewer *viewer, PopplerIndexIter *index_iter, GtkTreeIter *parentiter)
537 {
538         PopplerAction *action;
539         PopplerIndexIter *child;
540         GtkTreeIter childiter;
541
542         debug_print("get document index\n");
543         do      {
544                 gint page_num = 0;
545                 
546                 action = poppler_index_iter_get_action(index_iter);
547
548                 if (action->type != POPPLER_ACTION_GOTO_DEST) {
549                         poppler_action_free(action);
550                         continue;
551                 }
552
553                 if (action->goto_dest.dest->type == POPPLER_DEST_XYZ || action->goto_dest.dest->type == POPPLER_DEST_FITH) {
554                         page_num = action->goto_dest.dest->page_num;
555                 }
556 #ifdef HAVE_POPPLER_DEST_NAMED
557                 else if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
558                         PopplerDest *dest = poppler_document_find_dest(
559                                         viewer->pdf_doc, action->goto_dest.dest->named_dest);
560                         if (dest->type != POPPLER_DEST_XYZ) {
561                                 g_warning("couldn't figure out link\n");
562                                 poppler_dest_free(dest);
563                                 continue;
564                         }
565                         page_num = dest->page_num;
566                         poppler_dest_free(dest);
567                 } 
568 #endif
569                 else {
570 #ifdef HAVE_POPPLER_DEST_NAMED
571                         g_warning("unhandled link type %d\nplease contact developers\n", action->goto_dest.dest->type);
572 #else
573                         g_warning("unhandled link type %d\nplease upgrade libpoppler-glib to 0.5.4\n", action->goto_dest.dest->type);
574 #endif
575                         continue;
576                 }
577                 gtk_tree_store_append(GTK_TREE_STORE(viewer->index_model), &childiter, parentiter);
578                 gtk_tree_store_set(GTK_TREE_STORE(viewer->index_model), &childiter,
579                                                 INDEX_NAME, action->named.title,
580                                                 INDEX_PAGE, page_num,
581                                                 INDEX_TOP, action->goto_dest.dest->top,
582                                                 -1);
583                 poppler_action_free(action);
584                 child = poppler_index_iter_get_child(index_iter);
585                 if (child) {
586                         pdf_viewer_get_document_index(viewer, child, &childiter);
587                         poppler_index_iter_free(child);
588                 }
589         }
590         while(poppler_index_iter_next(index_iter));
591 }
592
593 static void pdf_viewer_index_row_activated(GtkTreeView          *tree_view,
594                                         GtkTreePath             *path,
595                                         GtkTreeViewColumn       *column,
596                                         gpointer                 data)
597 {
598         GtkTreeIter iter;
599         GtkTreeModel *model = gtk_tree_view_get_model(tree_view);
600         PdfViewer *viewer = (PdfViewer *)data;
601         gint page_num = 0;
602         
603         debug_print("index_row_activated\n");
604         if (!gtk_tree_model_get_iter(model, &iter, path)) return;
605
606         gtk_tree_model_get(model, &iter, 
607                            INDEX_PAGE, &page_num,
608                            -1);
609
610         if (page_num > 0) {
611                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->cur_page),(gdouble)page_num);
612                 debug_print("Page num: %d\n", page_num);
613         }
614         GTK_EVENTS_FLUSH();
615 }
616
617 /** Disable the index button if the document doesn't have an index */
618 static void pdf_viewer_set_index_button_sensitive(PdfViewer *viewer)
619 {
620         viewer->pdf_index  = poppler_index_iter_new(viewer->pdf_doc);
621         if (viewer->pdf_index) {        
622                 if (!gtk_widget_is_sensitive(viewer->doc_index)) {
623                         gtk_widget_set_sensitive(viewer->doc_index, TRUE);
624                 }
625         }
626         else {
627                 gtk_widget_set_sensitive(viewer->doc_index, FALSE);
628         }
629
630     poppler_index_iter_free(viewer->pdf_index);
631     viewer->pdf_index = NULL;
632 }
633
634 static char * pdf_viewer_get_document_format_data(GTime utime) 
635 {
636         time_t time = (time_t) utime;
637         struct tm t;
638         char s[256];
639         const char *fmt_hack = "%c";
640         size_t len;
641
642         if (time == 0 || !localtime_r(&time, &t)) return NULL;
643
644         len = strftime(s, sizeof(s), fmt_hack, &t);
645         
646         if (len == 0 || s[0] == '\0') return NULL;
647
648         return g_locale_to_utf8(s, -1, NULL, NULL, NULL);
649 }
650
651 #define ADD_TO_TABLE(LABEL, VALUE) \
652         label = gtk_label_new(LABEL); \
653         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); \
654         gtk_misc_set_padding(GTK_MISC(label), 4, 0); \
655         gtk_table_attach(viewer->table_doc_info, label, 0, 1, row, row+1, GTK_EXPAND | GTK_FILL, 0, 0, 0); \
656         \
657         label = gtk_label_new(VALUE); \
658         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
659         gtk_misc_set_padding(GTK_MISC(label), 4, 0); \
660         gtk_table_attach(viewer->table_doc_info, label, 1, 2, row, row+1, GTK_EXPAND | GTK_FILL, 0, 0, 0); \
661         row++;
662
663         
664 static GtkTable * pdf_viewer_fill_info_table(PdfViewer *viewer)
665 {
666         GtkWidget *label;
667         const gchar *title, *format, *author, *subject, *keywords, *creator, *producer;
668         gboolean linearized;
669         gchar *tmp;
670         gint row = 0;
671
672         GTime creation_date, mod_date;
673
674         PopplerPageLayout layout;
675         PopplerPageMode mode;
676         PopplerPermissions permissions;
677         PopplerViewerPreferences view_prefs;
678
679         title = format = author = subject = keywords = creator = producer = tmp = 0;
680
681         g_object_get(viewer->pdf_doc,
682                                 "title", &title,
683                                 "format", &format,
684                                 "author", &author,
685                                 "subject", &subject,
686                                 "keywords", &keywords,
687                                 "creation-date", &creation_date,
688                                 "permissions", &permissions,
689                                 "mod-date", &mod_date,
690                                 "creator", &creator,
691                                 "producer", &producer,  
692                                 "linearized", &linearized,
693                                 "page-mode", &mode,
694                                 "page-layout", &layout,
695                                 "viewer-preferences", &view_prefs,
696                                 NULL);
697
698         viewer->table_doc_info = GTK_TABLE(gtk_table_new(13, 2, FALSE));
699
700         ADD_TO_TABLE(_("Filename:"), viewer->target_filename)
701         ADD_TO_TABLE(_("Size:"), to_human_readable(viewer->to_load->length))
702         ADD_TO_TABLE(NULL, NULL)
703         ADD_TO_TABLE(_("Title:"), title)
704         ADD_TO_TABLE(_("Subject:"), subject)
705         ADD_TO_TABLE(_("Author:"), author)
706         ADD_TO_TABLE(_("Keywords:"), keywords)
707         ADD_TO_TABLE(_("Creator:"), creator)
708         ADD_TO_TABLE(_("Producer:"), producer)
709
710         tmp = pdf_viewer_get_document_format_data(creation_date);
711         ADD_TO_TABLE(_("Created:"), tmp)
712         g_free(tmp);
713
714         tmp = pdf_viewer_get_document_format_data(mod_date);
715         ADD_TO_TABLE(_("Modified:"), tmp)
716         g_free(tmp);
717         
718         ADD_TO_TABLE(_("Format:"), format)
719         if (linearized) {
720                 ADD_TO_TABLE(_("Optimized:"), _("Yes"))
721         }
722         else {
723                 ADD_TO_TABLE(_("Optimized:"), _("No"))
724         }
725         //ADD_TO_TABLE(_("Page Mode:"), pdf_viewer_get_document_info_mode(mode)) 
726         //ADD_TO_TABLE(_("Page Layout:"), pdf_viewer_get_document_info_layout(layout))
727
728         return(GtkTable *) viewer->table_doc_info;
729 }
730 #undef ADD_TO_TABLE
731
732 static FileType pdf_viewer_mimepart_get_type(MimeInfo *partinfo)
733 {
734         gchar *content_type = NULL;
735         FileType type = TYPE_UNKNOWN;
736         debug_print("mimepart_get_type\n");
737         if ((partinfo->type == MIMETYPE_APPLICATION) &&
738         (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
739                 
740                 const gchar *filename;
741
742                 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
743                 
744                         if (filename == NULL)
745                                 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
746                         if (filename != NULL)
747                                 content_type = procmime_get_mime_type(filename);
748         } 
749         else {
750                 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
751         }
752
753         if (content_type == NULL) type = TYPE_UNKNOWN;
754         else if (!strcmp(content_type, "application/pdf")) type = TYPE_PDF;
755         else if (!strcmp(content_type, "application/postscript")) type = TYPE_PS;
756         else type = TYPE_UNKNOWN;
757         
758         g_free(content_type);
759         return type;
760 }
761
762 /* Callbacks */
763 static void pdf_viewer_button_first_page_cb(GtkButton *button, PdfViewer *viewer) 
764 {
765         
766         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_HOME, 1);
767 }
768
769 static void pdf_viewer_button_prev_page_cb(GtkButton *button, PdfViewer *viewer) 
770 {
771         
772         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_BACKWARD, 1);
773 }
774
775 static void pdf_viewer_button_next_page_cb(GtkButton *button, PdfViewer *viewer) 
776 {
777         
778         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_FORWARD, 1);
779 }
780
781 static void pdf_viewer_button_last_page_cb(GtkButton *button, PdfViewer *viewer) 
782 {
783         
784         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_END, 1);
785 }
786
787 static void pdf_viewer_spin_change_page_cb(GtkSpinButton *button, PdfViewer *viewer)
788 {
789         pdf_viewer_update((MimeViewer *)viewer, 
790                         FALSE, 
791                         gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page)));
792 }
793
794 static void pdf_viewer_button_zoom_in_cb(GtkButton *button, PdfViewer *viewer) 
795 {
796
797         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->zoom_scroll), GTK_SPIN_STEP_FORWARD, ZOOM_FACTOR);
798 }
799
800 static void pdf_viewer_spin_zoom_scroll_cb(GtkSpinButton *button, PdfViewer *viewer)
801 {
802         viewer->zoom = gtk_spin_button_get_value(GTK_SPIN_BUTTON(viewer->zoom_scroll));
803         pdf_viewer_update((MimeViewer *)viewer,
804                         FALSE,
805                         gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page)));
806 }
807
808 static void pdf_viewer_button_zoom_out_cb(GtkButton *button, PdfViewer *viewer) 
809 {
810
811         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->zoom_scroll), GTK_SPIN_STEP_BACKWARD, ZOOM_FACTOR);
812         
813 }
814
815 static void pdf_viewer_button_press_events_cb(GtkWidget *widget, GdkEventButton *event, PdfViewer *viewer) 
816 {
817         gchar *uri;
818         #ifdef HAVE_POPPLER_DEST_NAMED
819         PopplerDest *dest;
820         #endif
821         static GdkCursor *hand_cur = NULL;
822         
823         if (!hand_cur) hand_cur = gdk_cursor_new(GDK_FLEUR);
824
825         /* Execute Poppler Links */
826         if (event->button == 1 && viewer->in_link) {
827                 switch (viewer->link_action->type) {
828                 case POPPLER_ACTION_UNKNOWN:
829                         debug_print("action unknown\n");
830                         break;
831                 case POPPLER_ACTION_GOTO_DEST:
832                         if (viewer->link_action->goto_dest.dest->type == POPPLER_DEST_XYZ || 
833                                         viewer->link_action->goto_dest.dest->type == POPPLER_DEST_FITH) {
834                                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->cur_page), 
835                                                                           (gdouble)viewer->link_action->goto_dest.dest->page_num);
836                         }
837                 #ifdef HAVE_POPPLER_DEST_NAMED
838                         else if (viewer->link_action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
839                                 dest = poppler_document_find_dest(
840                                         viewer->pdf_doc, viewer->link_action->goto_dest.dest->named_dest);
841                         if (dest->type != POPPLER_DEST_XYZ) {
842                                 g_warning("couldn't figure out link\n");
843                                 poppler_dest_free(dest);
844                                 break;
845                         }
846                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->cur_page), 
847                                                                           (gdouble)dest->page_num);
848                         poppler_dest_free(dest);
849                 } 
850                 #endif
851                         break;
852                 case POPPLER_ACTION_GOTO_REMOTE:
853                         #ifdef HAVE_POPPLER_DEST_NAMED
854                         dest = poppler_document_find_dest(
855                                         viewer->pdf_doc, viewer->link_action->goto_remote.dest->named_dest);
856                         if (dest->type != POPPLER_DEST_XYZ) {
857                                 g_warning ("couldn't figure out link\n");
858                                 poppler_dest_free(dest);
859                                 break;
860                         }
861                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->cur_page),
862                                                                           (gdouble)dest->page_num);
863                         poppler_dest_free(dest);
864                         #endif
865                         break;
866                 case POPPLER_ACTION_LAUNCH:
867                         debug_print("action launch not yet implemented\n");
868                         break;
869                 case POPPLER_ACTION_URI:
870                         uri = g_strdup(viewer->link_action->uri.uri);
871                         if (!g_ascii_strncasecmp(uri, "mailto:", 7)) 
872                                 compose_new(NULL, uri + 7, NULL);
873                         else 
874                                 open_uri(uri, prefs_common_get_uri_cmd());
875                         g_free(uri);
876                         break;
877                 case POPPLER_ACTION_NAMED:
878                         debug_print("action named not yet implemented\n");
879                         break;
880                 case POPPLER_ACTION_MOVIE:
881                         debug_print("yoyoyo ;-) a movie?\n");
882                         break;
883                 }
884                 if (((MimeViewer *)viewer)->mimeview && 
885                         ((MimeViewer *)viewer)->mimeview->messageview && 
886                         ((MimeViewer *)viewer)->mimeview->messageview->window && 
887                         ((MimeViewer *)viewer)->mimeview->messageview->window->window) 
888                         gdk_window_set_cursor (((MimeViewer *)viewer)->mimeview->messageview->window->window, NULL);
889                 else
890                         gdk_window_set_cursor (mainwindow_get_mainwindow()->window->window, NULL);
891         }
892
893         /* Init document to be scrolled with left mouse click */
894         if (event->button == 1 && !viewer->in_link) {
895                 viewer->pdf_view_scroll = TRUE;
896                 if (((MimeViewer *)viewer)->mimeview && 
897                         ((MimeViewer *)viewer)->mimeview->messageview && 
898                         ((MimeViewer *)viewer)->mimeview->messageview->window && 
899                         ((MimeViewer *)viewer)->mimeview->messageview->window->window) 
900                         gdk_window_set_cursor (((MimeViewer *)viewer)->mimeview->messageview->window->window, hand_cur);
901                 else
902                         gdk_window_set_cursor (mainwindow_get_mainwindow()->window->window, hand_cur);
903
904                 viewer->last_x = event->x;
905                 viewer->last_y = event->y;
906                 viewer->last_dir_x = 0;
907                 viewer->last_dir_y = 0;
908         }
909 }
910 /* Set the normal cursor*/
911 static void pdf_viewer_mouse_scroll_destroy_cb(GtkWidget *widget, GdkEventButton *event, PdfViewer *viewer) 
912 {
913         
914         if (event->button == 1) {
915                 viewer->pdf_view_scroll = FALSE;
916                 if (((MimeViewer *)viewer)->mimeview && 
917                         ((MimeViewer *)viewer)->mimeview->messageview && 
918                         ((MimeViewer *)viewer)->mimeview->messageview->window && 
919                         ((MimeViewer *)viewer)->mimeview->messageview->window->window) 
920                         gdk_window_set_cursor (((MimeViewer *)viewer)->mimeview->messageview->window->window, NULL);
921                 else
922                         gdk_window_set_cursor (mainwindow_get_mainwindow()->window->window, NULL);
923         }
924 }
925
926 static void pdf_viewer_move_events_cb(GtkWidget *widget, GdkEventMotion *event, PdfViewer *viewer) 
927 {
928         /* Grab the document and scroll it with mouse */ 
929         if (viewer->pdf_view_scroll) {
930
931                 viewer->pdf_view_vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
932                 viewer->pdf_view_hadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
933                         
934                         if (event->x < viewer->last_x
935                                         && viewer->pdf_view_hadj->value < (viewer->pdf_view_hadj->upper - viewer->pdf_view_hadj->page_size)) {
936                                 if (viewer->last_dir_x == -1) {
937                                         viewer->pdf_view_hadj->value += viewer->last_x - event->x; 
938                                         g_signal_emit_by_name(G_OBJECT(viewer->pdf_view_hadj),
939                                                                 "value_changed", 0);
940                                 }
941                                 viewer->last_dir_x = -1;
942                         }
943                         else if (event->x > viewer->last_x
944                                         && viewer->pdf_view_hadj->value > 0.0)  {
945                                 if (viewer->last_dir_x == +1) {
946                                         viewer->pdf_view_hadj->value += viewer->last_x - event->x; 
947                                         g_signal_emit_by_name(G_OBJECT(viewer->pdf_view_hadj),
948                                                                 "value_changed", 0);
949                                 }
950                                 viewer->last_dir_x = +1;
951                         }
952
953                         if (event->y < viewer->last_y
954                                         && viewer->pdf_view_vadj->value < (viewer->pdf_view_vadj->upper - viewer->pdf_view_vadj->page_size)) {
955                                 if (viewer->last_dir_y == -1) {
956                                         viewer->pdf_view_vadj->value += viewer->last_y - event->y; 
957                                         g_signal_emit_by_name(G_OBJECT(viewer->pdf_view_vadj),
958                                                                 "value_changed", 0);
959                                 }
960                                 viewer->last_dir_y = -1;
961                         }
962                         else if (event->y > viewer->last_y
963                                         && viewer->pdf_view_vadj->value > 0.0)  {
964                                 if (viewer->last_dir_y == +1) {
965                                         viewer->pdf_view_vadj->value += viewer->last_y - event->y; 
966                                         g_signal_emit_by_name(G_OBJECT(viewer->pdf_view_vadj),
967                                                                 "value_changed", 0);
968                                 }
969                                 viewer->last_dir_y = +1;
970                         }
971                         viewer->last_x = event->x;
972                         viewer->last_y = event->y;
973                         GTK_EVENTS_FLUSH();
974                 } 
975         else {  
976         /* Link Mapping */
977         static GList *l;
978         static GdkCursor *link_cur = NULL;
979         static GtkRequisition size;
980         static gdouble x,y, x1, y1, x2, y2;
981         gboolean ccur;
982         
983         viewer->pdf_view_vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
984         viewer->pdf_view_hadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
985         
986         if (!link_cur) link_cur = gdk_cursor_new(GDK_HAND2);
987
988         ccur = FALSE;
989         viewer->in_link = FALSE;        
990         for (l = viewer->link_map; l; l = g_list_next (l)) {
991                 PopplerLinkMapping *lmapping;
992                 lmapping = (PopplerLinkMapping *)l->data;
993
994                 x1 = lmapping->area.x1;
995                 y1 = lmapping->area.y1;
996                 x2 = lmapping->area.x2;
997                 y2 = lmapping->area.y2;
998                 gtk_widget_size_request(viewer->pdf_view, &size);
999                 
1000                 switch (viewer->rotate) {
1001                 case 0:
1002                 case 360:
1003                                 if (size.width != viewer->pdf_view_hadj->upper)
1004                                         x = (event->x - (viewer->pdf_view_hadj->upper - size.width) / 2) / viewer->zoom;
1005                                 else
1006                                         x = event->x / viewer->zoom;
1007
1008                                 y = (viewer->pdf_view_vadj->upper - event->y) / viewer->zoom;
1009                         break;
1010                 case 90:
1011                                 if (size.width != viewer->pdf_view_hadj->upper)
1012                                         y = (event->x - (viewer->pdf_view_hadj->upper - size.width) / 2) / viewer->zoom;
1013                                 else
1014                                         y = event->x / viewer->zoom;
1015
1016                                 x = (event->y) / viewer->zoom;
1017                         break;
1018                 case 180:
1019                                 if (size.width != viewer->pdf_view_hadj->upper)
1020                                         x = ((viewer->pdf_view_hadj->upper -  event->x) - ((viewer->pdf_view_hadj->upper - size.width) / 2)) / viewer->zoom;
1021                                 else
1022                                         x =  ((viewer->pdf_view_hadj->upper -  event->x) - (viewer->pdf_view_hadj->upper - size.width)) / viewer->zoom;
1023
1024                                 y = (event->y) / viewer->zoom;
1025                         break;
1026                 case 270:
1027                                 if (size.width != viewer->pdf_view_hadj->upper)
1028                                         y = ((viewer->pdf_view_hadj->upper -  event->x) - ((viewer->pdf_view_hadj->upper - size.width) / 2)) / viewer->zoom;
1029                                 else
1030                                         y =  ((viewer->pdf_view_hadj->upper -  event->x) - (viewer->pdf_view_hadj->upper - size.width)) / viewer->zoom;
1031
1032                                 x = (viewer->pdf_view_vadj->upper - event->y) / viewer->zoom;
1033                         break;
1034                 }
1035
1036                 if ( (x > x1 && x < x2) && (y > y1 && y < y2) ) {
1037                                 viewer->in_link = TRUE;
1038                         if (((MimeViewer *)viewer)->mimeview && 
1039                                 ((MimeViewer *)viewer)->mimeview->messageview && 
1040                                 ((MimeViewer *)viewer)->mimeview->messageview->window && 
1041                                 ((MimeViewer *)viewer)->mimeview->messageview->window->window) 
1042                                         gdk_window_set_cursor (((MimeViewer *)viewer)->mimeview->messageview->window->window, link_cur);
1043                                 else
1044                                         gdk_window_set_cursor (mainwindow_get_mainwindow()->window->window, link_cur);
1045                                 
1046                                 viewer->link_action = lmapping->action; 
1047                                 ccur = TRUE;
1048                 }
1049                 if (!ccur) {
1050                         if (((MimeViewer *)viewer)->mimeview && 
1051                                 ((MimeViewer *)viewer)->mimeview->messageview && 
1052                                 ((MimeViewer *)viewer)->mimeview->messageview->window && 
1053                                 ((MimeViewer *)viewer)->mimeview->messageview->window->window) 
1054                                 gdk_window_set_cursor (((MimeViewer *)viewer)->mimeview->messageview->window->window, NULL);
1055                         else
1056                                 gdk_window_set_cursor (mainwindow_get_mainwindow()->window->window, NULL);
1057                 }
1058         }
1059         g_free(l);
1060         }
1061 }
1062 static gboolean pdf_viewer_scroll_cb(GtkWidget *widget, GdkEventScroll *event,
1063                                     PdfViewer *viewer)
1064 {
1065         GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
1066         static gboolean in_scroll_cb = FALSE;
1067         gboolean handled = FALSE;
1068         gint cur_p = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page));               
1069
1070         if (in_scroll_cb)
1071                 return FALSE;
1072
1073         in_scroll_cb = TRUE;
1074
1075         if (event->direction == GDK_SCROLL_UP &&
1076             adj->value == adj->lower && 
1077             cur_p > 1) {
1078                 gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_BACKWARD, 1);
1079                 adj->value = adj->upper - adj->page_size;
1080                 handled = TRUE;
1081         } else if (event->direction == GDK_SCROLL_DOWN &&
1082             adj->value + adj->page_size == adj->upper &&
1083             cur_p < viewer->num_pages) {
1084                 gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_FORWARD, 1);
1085                 adj->value = 0.0;
1086                 handled = TRUE;
1087         }
1088         in_scroll_cb = FALSE;
1089         return handled;
1090 }
1091
1092 static void pdf_viewer_button_zoom_fit_cb(GtkButton *button, PdfViewer *viewer)
1093 {
1094         GtkAllocation *allocation;
1095         double xratio, yratio;
1096         allocation = &(viewer->scrollwin->allocation);
1097         debug_print("width: %d\n", allocation->width);
1098         debug_print("height: %d\n", allocation->height);
1099         xratio = allocation->width / viewer->width;
1100         yratio = allocation->height / viewer->height;
1101
1102         if (xratio >= yratio) {
1103                 viewer->zoom = yratio;
1104                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->zoom_scroll),viewer->zoom);
1105         }
1106         else {
1107                 viewer->zoom = xratio;
1108                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->zoom_scroll),viewer->zoom);
1109         }
1110 }
1111
1112 static void pdf_viewer_button_zoom_width_cb(GtkButton *button, PdfViewer *viewer)
1113 {
1114         GtkAllocation *allocation;
1115         double xratio;
1116         allocation = &(viewer->scrollwin->allocation);
1117         debug_print("width: %d\n", allocation->width);
1118         xratio = allocation->width / viewer->width;
1119         gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->zoom_scroll), xratio);
1120 }
1121
1122 static void pdf_viewer_button_rotate_right_cb(GtkButton *button, PdfViewer *viewer)
1123 {
1124         if (viewer->rotate == 360) {
1125                 viewer->rotate = 0;
1126         }
1127         
1128         viewer->rotate += (gint) ROTATION;
1129         pdf_viewer_update((MimeViewer *)viewer, FALSE,
1130                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page)));
1131 }
1132
1133 static void pdf_viewer_button_rotate_left_cb(GtkButton *button, PdfViewer *viewer)
1134 {
1135         if (viewer->rotate == 0) {
1136                 viewer->rotate = 360;
1137         }
1138         
1139         viewer->rotate = abs(viewer->rotate -(gint) ROTATION);
1140         pdf_viewer_update((MimeViewer *)viewer, FALSE,
1141                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page)));
1142 }
1143
1144 /* Show/Hide the index pane */
1145 static void pdf_viewer_show_document_index_cb(GtkButton *button, PdfViewer *viewer)
1146 {
1147         if (!viewer->pdf_index) {
1148                 viewer->pdf_index = poppler_index_iter_new(viewer->pdf_doc);
1149         }
1150         
1151         gtk_tree_store_clear(GTK_TREE_STORE(viewer->index_model));
1152         
1153         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(viewer->doc_index))) {
1154                 pdf_viewer_get_document_index(viewer,(PopplerIndexIter *) viewer->pdf_index, NULL);
1155                 gtk_widget_show(GTK_WIDGET(viewer->frame_index));
1156         }
1157         else {
1158                 pdf_viewer_hide_index_pane(viewer);
1159         }
1160                 
1161 }
1162
1163 static void pdf_viewer_button_document_info_cb(GtkButton *button, PdfViewer *viewer)
1164 {
1165         alertpanel_full(_("PDF properties"), NULL, GTK_STOCK_CLOSE, NULL, NULL,
1166                         FALSE, (GtkWidget *) pdf_viewer_fill_info_table(viewer), 
1167                         ALERT_NOTICE, G_ALERTDEFAULT);
1168 }
1169
1170 /*
1171 static const char * poppler_get_document_info_mode(PopplerPageMode mode)
1172 {
1173         GEnumValue *enum_value;
1174
1175         enum_value = g_enum_get_value((GEnumClass *) g_type_class_peek(POPPLER_TYPE_PAGE_MODE), mode);
1176         return(gchar *) enum_value->value_name;
1177 }
1178 static const char * poppler_get_document_info_layout(PopplerPageLayout layout)
1179 {
1180
1181         GEnumValue *enum_value;
1182
1183         enum_value = g_enum_get_value((GEnumClass *) g_type_class_peek(POPPLER_TYPE_PAGE_LAYOUT), layout);
1184         return(gchar *) enum_value->value_name;
1185 }
1186 */
1187 static void pdf_viewer_show_controls(PdfViewer *viewer, gboolean show)
1188 {
1189         if (show) {
1190                 gtk_widget_show(viewer->first_page);
1191                 gtk_widget_show(viewer->cur_page);
1192                 gtk_widget_show(viewer->prev_page);
1193                 gtk_widget_show(viewer->next_page);
1194                 gtk_widget_show(viewer->last_page);
1195                 gtk_widget_show(viewer->zoom_in);
1196                 gtk_widget_show(viewer->zoom_out);
1197                 gtk_widget_show(viewer->zoom_fit);
1198                 gtk_widget_show(viewer->zoom_width);
1199                 gtk_widget_show(viewer->zoom_scroll);
1200                 gtk_widget_show(viewer->widgets_table);
1201                 gtk_widget_show(viewer->rotate_right);
1202                 gtk_widget_show(viewer->rotate_left);
1203                 gtk_widget_show(viewer->doc_info);
1204                 gtk_widget_show(viewer->doc_index);
1205         } else {
1206                 gtk_widget_hide(viewer->first_page);
1207                 gtk_widget_hide(viewer->cur_page);
1208                 gtk_widget_hide(viewer->prev_page);
1209                 gtk_widget_hide(viewer->next_page);
1210                 gtk_widget_hide(viewer->last_page);
1211                 gtk_widget_hide(viewer->zoom_in);
1212                 gtk_widget_hide(viewer->zoom_out);
1213                 gtk_widget_hide(viewer->zoom_fit);
1214                 gtk_widget_hide(viewer->zoom_width);
1215                 gtk_widget_hide(viewer->widgets_table);
1216                 gtk_widget_hide(viewer->rotate_right);
1217                 gtk_widget_hide(viewer->rotate_left);
1218                 gtk_widget_hide(viewer->doc_info);
1219                 gtk_widget_hide(viewer->doc_index);
1220                 gtk_widget_hide(viewer->zoom_scroll);
1221         }
1222 }
1223 /** Render the current page, page_num on the viewer */
1224 static void pdf_viewer_update(MimeViewer *_viewer, gboolean reload_file, int page_num) 
1225 {
1226
1227         PdfViewer *viewer = (PdfViewer *) _viewer;
1228         GError *error;
1229         gchar *tmpfile = NULL;
1230         gchar *tmp;
1231
1232         debug_print("pdf_viewer_update\n");
1233
1234         error = NULL;
1235         if (reload_file) {
1236                 if (viewer->pdf_doc) {
1237                         g_object_unref(G_OBJECT(viewer->pdf_doc));
1238                         viewer->pdf_doc = NULL;
1239                 }
1240                 
1241                 if (pdf_viewer_mimepart_get_type(viewer->to_load) == TYPE_PS) {
1242                         stock_pixbuf_gdk(viewer->hbox, 
1243                                         STOCK_PIXMAP_MIME_PS, 
1244                                         &viewer->icon_pixbuf);
1245                         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->icon_type),
1246                                                                         viewer->icon_pixbuf);
1247                 } 
1248                 else if (pdf_viewer_mimepart_get_type(viewer->to_load) == TYPE_PDF) {
1249                         stock_pixbuf_gdk(viewer->hbox, 
1250                         STOCK_PIXMAP_MIME_PDF, 
1251                         &viewer->icon_pixbuf);
1252                         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->icon_type), 
1253                                                                         viewer->icon_pixbuf);
1254                 } 
1255                 else {
1256                         stock_pixbuf_gdk(viewer->hbox, 
1257                         STOCK_PIXMAP_MIME_APPLICATION, 
1258                         &viewer->icon_pixbuf);
1259                         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->icon_type), 
1260                                                                         viewer->icon_pixbuf);
1261                 }
1262
1263                 gtk_label_set_text(GTK_LABEL(viewer->doc_label), _("Loading..."));      
1264                 pdf_viewer_show_controls(viewer, FALSE);
1265                 main_window_cursor_wait(mainwindow_get_mainwindow());
1266
1267                 GTK_EVENTS_FLUSH();
1268
1269                 if (pdf_viewer_mimepart_get_type(viewer->to_load) == TYPE_PS) {
1270                         gchar *cmdline = NULL, *tmp = NULL, *gspath = NULL;
1271                         gint result = 0;
1272
1273                         if ((gspath = g_find_program_in_path("gs")) != NULL) {
1274                                 g_free(gspath);
1275                                 /* convert postscript to pdf */
1276                                 tmpfile = get_tmp_file();
1277                                 cmdline = g_strdup_printf(
1278                                         "gs -dSAFER -dCompatibilityLevel=1.2 -q -dNOPAUSE -dBATCH "
1279                                           "-sDEVICE=pdfwrite -sOutputFile=%s -c .setpdfwrite -f \"%s\"",
1280                                         tmpfile, viewer->filename);
1281                                 result = execute_command_line(cmdline, FALSE);
1282                                 if (result == 0) {
1283                                         tmp = g_filename_to_uri(tmpfile, NULL, NULL);
1284                                         viewer->pdf_doc = poppler_document_new_from_file( tmp, NULL, &error);
1285                                         g_free(tmp);
1286                                 } 
1287                                 else {
1288                                         g_warning("gs conversion failed: %s returned %d\n", cmdline, result);
1289                                         tmp = g_strdup_printf("gs: err %d", result);
1290                                         alertpanel_warning("%s", tmp);
1291                                         g_free(tmp);
1292                                 }
1293         
1294                                 g_free(cmdline);
1295                                 claws_unlink(tmpfile);
1296                                 g_free(tmpfile);
1297                         }
1298                         else {
1299                                 g_warning("gs conversion disabled: gs binary was not found\n");
1300                                 alertpanel_warning("PostScript view disabled: required gs program not found");
1301                                 result = 1;
1302                                         
1303                         }
1304                         if (result != 0) {
1305                                 main_window_cursor_normal(mainwindow_get_mainwindow());
1306                                 return;
1307                         }
1308                 }   
1309                 else {
1310                         viewer->pdf_doc = poppler_document_new_from_file( viewer->fsname, NULL, &error);
1311                 }
1312                 
1313                 viewer->num_pages = poppler_document_get_n_pages(viewer->pdf_doc);
1314
1315                 g_signal_handlers_block_by_func(G_OBJECT(viewer->cur_page), pdf_viewer_spin_change_page_cb,(gpointer *)viewer);
1316                 gtk_spin_button_set_range(GTK_SPIN_BUTTON(viewer->cur_page), 
1317                                                                         1, 
1318                                                                 (gdouble)viewer->num_pages );
1319
1320                 g_signal_handlers_unblock_by_func(G_OBJECT(viewer->cur_page), pdf_viewer_spin_change_page_cb,(gpointer *)viewer);
1321                 gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_HOME, 1);
1322                 tmp = g_strdup_printf(_("%s Document"),pdf_viewer_mimepart_get_type(viewer->to_load) == TYPE_PDF ? "PDF":"Postscript");
1323                 CLAWS_SET_TIP(
1324                                 GTK_WIDGET(viewer->icon_type_ebox),
1325                                 tmp);
1326                 g_free(tmp);
1327
1328                 tmp = g_strdup_printf(_("of %d"), viewer->num_pages);
1329                 gtk_label_set_text(GTK_LABEL(viewer->doc_label), tmp);
1330                 g_free(tmp);
1331
1332                 pdf_viewer_show_controls(viewer, TRUE);
1333                 main_window_cursor_normal(mainwindow_get_mainwindow());
1334         } 
1335         if (viewer->pdf_doc == NULL) {
1336                 strretchomp(error->message);
1337                 stock_pixbuf_gdk(viewer->hbox, 
1338                                 STOCK_PIXMAP_MIME_APPLICATION, 
1339                                 &viewer->icon_pixbuf);
1340
1341                 gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->icon_type), viewer->icon_pixbuf);
1342                 alertpanel_error("%s", error->message);
1343         
1344                 pdf_viewer_show_controls(viewer, FALSE);
1345                 g_error_free(error);
1346                 return;
1347         }
1348
1349         if (page_num == 1) { 
1350                 gtk_widget_set_sensitive(viewer->first_page, FALSE);
1351                 gtk_widget_set_sensitive(viewer->prev_page, FALSE);
1352         }
1353         else {
1354                 gtk_widget_set_sensitive(viewer->first_page, TRUE);
1355                 gtk_widget_set_sensitive(viewer->prev_page, TRUE);
1356         }
1357
1358         if (page_num == viewer->num_pages) { 
1359                 gtk_widget_set_sensitive(viewer->last_page, FALSE);
1360                 gtk_widget_set_sensitive(viewer->next_page, FALSE);
1361         }
1362         else {
1363                 gtk_widget_set_sensitive(viewer->last_page, TRUE);
1364                 gtk_widget_set_sensitive(viewer->next_page, TRUE);
1365         }
1366
1367         /* check for the index if exists */
1368         pdf_viewer_set_index_button_sensitive((PdfViewer *) viewer);
1369         
1370         if (page_num > 0 && page_num <= viewer->num_pages) {
1371
1372                 GTK_EVENTS_FLUSH();
1373                 
1374                 if (viewer->pdf_page) {
1375                         g_object_unref(G_OBJECT(viewer->pdf_page));
1376                 }
1377                         
1378                 viewer->pdf_page = poppler_document_get_page(viewer->pdf_doc, page_num - 1);
1379
1380                 if (viewer->pdf_page == NULL) {
1381                         g_warning("Page not found\n");
1382                         return;
1383                 }   
1384         
1385                 if (viewer->rotate == 90 || viewer->rotate == 270) {
1386                         poppler_page_get_size(viewer->pdf_page, &viewer->height, &viewer->width);
1387                 } 
1388                 else {
1389                         poppler_page_get_size(viewer->pdf_page, &viewer->width, &viewer->height);
1390                 }
1391
1392                 if (viewer->last_rect && viewer->last_page_result &&
1393                     viewer->last_page_result->page_num == page_num) {
1394                         pdf_viewer_render_selection(viewer, viewer->last_rect, viewer->last_page_result);
1395                 }
1396                 else {
1397                         pdf_viewer_render_page(viewer->pdf_page, viewer->pdf_view, viewer->width, 
1398                                                                         viewer->height, viewer->zoom, viewer->rotate);
1399
1400                 }
1401         
1402         /* Get Links Mapping */
1403         if (viewer->link_map) {
1404                 poppler_page_free_link_mapping(viewer->link_map);
1405         }
1406         viewer->link_map = poppler_page_get_link_mapping(viewer->pdf_page);
1407         
1408         }
1409 }
1410
1411
1412 static void pdf_viewer_show_mimepart(MimeViewer *_viewer, const gchar *infile,
1413                                 MimeInfo *partinfo)
1414 {
1415         PdfViewer *viewer = (PdfViewer *) _viewer;
1416         gchar buf[4096];
1417         const gchar *charset = NULL;
1418         MessageView *messageview = ((MimeViewer *)viewer)->mimeview 
1419                                         ?((MimeViewer *)viewer)->mimeview->messageview 
1420                                         : NULL;
1421
1422         viewer->rotate = 0;
1423         viewer->to_load = partinfo;
1424
1425         memset(buf, 0, sizeof(buf));
1426         messageview->updating = TRUE;
1427         debug_print("pdf_viewer_show_mimepart\n");
1428
1429         if (viewer->filename != NULL) {
1430                 claws_unlink(viewer->filename);
1431                 g_free(viewer->filename);
1432                 viewer->filename = NULL;
1433         }
1434         
1435         viewer->mimeinfo = NULL;
1436         
1437         if (partinfo) {
1438                 viewer->target_filename = procmime_get_part_file_name(partinfo);
1439                 viewer->filename = procmime_get_tmp_file_name(partinfo);
1440                 viewer->fsname = g_filename_to_uri(viewer->filename, NULL, NULL);
1441         }
1442         
1443         if (partinfo && !(procmime_get_part(viewer->filename, partinfo) < 0)) {
1444
1445                 if (_viewer && _viewer->mimeview && 
1446                                 _viewer->mimeview->messageview->forced_charset) {
1447                         charset = _viewer->mimeview->messageview->forced_charset;
1448                 }
1449                 else {
1450                         charset = procmime_mimeinfo_get_parameter(partinfo, "charset");
1451                 }
1452                 if (charset == NULL) {
1453                         charset = conv_get_locale_charset_str();
1454                 }
1455
1456                 debug_print("using charset %s\n", charset);
1457                 
1458                 viewer->mimeinfo = partinfo;
1459         }
1460         
1461         pdf_viewer_update((MimeViewer *)viewer, TRUE, 1);
1462         
1463         messageview->updating = FALSE;
1464 }
1465
1466 static void pdf_viewer_clear(MimeViewer *_viewer)
1467 {
1468         PdfViewer *viewer = (PdfViewer *) _viewer;
1469         GtkAdjustment *vadj;
1470                 
1471         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(viewer->doc_index), FALSE);
1472         gtk_widget_hide(viewer->frame_index);
1473
1474         debug_print("pdf_viewer_clear\n");
1475         viewer->to_load = NULL;
1476         
1477         if (viewer->pdf_doc) {
1478                 g_object_unref(G_OBJECT(viewer->pdf_doc));
1479                 viewer->pdf_doc = NULL;
1480         }
1481         
1482         vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
1483         vadj->value = 0.0;
1484         g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);
1485         vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin_index));
1486         vadj->value = 0.0;
1487         g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);
1488         gtk_tree_store_clear(GTK_TREE_STORE(viewer->index_model));
1489         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->pdf_view), NULL);
1490 }
1491
1492 static void pdf_viewer_destroy(MimeViewer *_viewer)
1493 {
1494         PdfViewer *viewer = (PdfViewer *) _viewer;
1495
1496         debug_print("pdf_viewer_destroy\n");
1497         
1498         if (viewer->pdf_index) poppler_index_iter_free(viewer->pdf_index);
1499
1500         poppler_page_free_link_mapping (viewer->link_map);
1501         g_object_unref(GTK_WIDGET(viewer->vbox));
1502         g_object_unref(GTK_WIDGET(viewer->pdf_view));
1503         g_object_unref(GTK_WIDGET(viewer->doc_index_pane));
1504         g_object_unref(GTK_WIDGET(viewer->scrollwin));
1505         g_object_unref(GTK_WIDGET(viewer->scrollwin_index));
1506         claws_unlink(viewer->filename);
1507         g_free(viewer->filename);
1508         g_free(viewer);
1509 }
1510
1511 static gboolean pdf_viewer_scroll_page(MimeViewer *_viewer, gboolean up)
1512 {
1513         PdfViewer *viewer = (PdfViewer *)_viewer;
1514         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
1515                                 GTK_SCROLLED_WINDOW(viewer->scrollwin));
1516         
1517         gint cur_p = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page));
1518         
1519         if (viewer->pdf_view == NULL) return FALSE;
1520
1521         if (!gtkutils_scroll_page(GTK_WIDGET(viewer->pdf_view), vadj, up)) {
1522                 if (!up && cur_p != viewer->num_pages) {
1523                         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_FORWARD, 1);
1524                         vadj = gtk_scrolled_window_get_vadjustment(
1525                                         GTK_SCROLLED_WINDOW(viewer->scrollwin));
1526                         vadj->value = 0.0;
1527                         g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);
1528                         return TRUE;
1529                 } 
1530                 else if (up && cur_p != 1) {
1531                         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_BACKWARD, 1);
1532                         vadj = gtk_scrolled_window_get_vadjustment(
1533                                         GTK_SCROLLED_WINDOW(viewer->scrollwin));
1534                         vadj->value = vadj->upper - vadj->page_size;
1535                         g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);
1536                         return TRUE;
1537                 } 
1538                 return FALSE;
1539         } 
1540         else return TRUE;
1541 }
1542
1543 static void pdf_viewer_scroll_one_line(MimeViewer *_viewer, gboolean up)
1544 {
1545         PdfViewer *viewer = (PdfViewer *)_viewer;
1546         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
1547                                 GTK_SCROLLED_WINDOW(viewer->scrollwin));
1548         gint cur_p;
1549         cur_p = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page));
1550
1551         if (viewer->pdf_view == NULL) return; 
1552                 debug_print("up: %d\n", up);    
1553                 if (vadj->value <(vadj->upper - vadj->page_size))  {
1554                         gtkutils_scroll_one_line(GTK_WIDGET(viewer->pdf_view), vadj, up);
1555                 }
1556                 else {
1557                         if (cur_p != viewer->num_pages) {
1558                                 pdf_viewer_scroll_page((MimeViewer *)viewer, up);
1559                         }
1560                 }
1561
1562 }
1563
1564 #define BUTTON_H_PADDING 3
1565 #define ADD_BUTTON_TO_TABLE(widget, stock_image) \
1566         widget = gtk_button_new(); \
1567         img = gtk_image_new_from_stock(stock_image, GTK_ICON_SIZE_MENU); \
1568         gtk_button_set_image(GTK_BUTTON(widget), img); \
1569         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(widget), \
1570                                 col, col+1, 0, 1, 0, 0, BUTTON_H_PADDING, 0); \
1571         col++;
1572
1573 #define ADD_SEP_TO_TABLE \
1574         sep = gtk_label_new(""); \
1575         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(sep), \
1576                                         col, col+1, 0, 1, 0, 0, 0, 0); \
1577         gtk_table_set_col_spacing(GTK_TABLE(viewer->widgets_table), col, 3*BUTTON_H_PADDING); \
1578         col++;
1579
1580 #if GTK_CHECK_VERSION(2,10,0) && POPPLER_HAS_CAIRO && !USE_LIBGNOMEPRINT
1581 static PangoContext *pdf_viewer_get_pango_context(gpointer data)
1582 {
1583         return NULL;
1584 }
1585
1586 static gpointer pdf_viewer_get_data_to_print(gpointer data, gint sel_start, gint sel_end)
1587 {
1588         return NULL; /* we don't need it */
1589 }
1590
1591 static void pdf_viewer_cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context,
1592                            gpointer user_data)
1593 {
1594   PrintData *print_data;
1595   PopplerDocument *pdf_doc;
1596   gint n_pages = 0;
1597   print_data = (PrintData*) user_data;
1598   pdf_doc = (PopplerDocument *)printing_get_renderer_data(print_data);
1599
1600   debug_print("Preparing print job...\n");
1601
1602   n_pages = poppler_document_get_n_pages(pdf_doc);
1603   printing_set_n_pages(print_data, n_pages);
1604   gtk_print_operation_set_n_pages(op, n_pages);
1605
1606   debug_print("Starting print job...\n");
1607 }
1608
1609 static void pdf_viewer_cb_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
1610                          int page_nr, gpointer user_data)
1611 {
1612   cairo_t *cr;
1613   PrintData *print_data;
1614   PopplerDocument *pdf_doc;
1615   PopplerPage *pdf_page;
1616   
1617   print_data = (PrintData*) user_data;
1618   pdf_doc = (PopplerDocument *)printing_get_renderer_data(print_data);
1619   pdf_page = poppler_document_get_page(pdf_doc, page_nr);
1620
1621   cr = gtk_print_context_get_cairo_context(context);
1622   cairo_scale(cr, printing_get_zoom(print_data), printing_get_zoom(print_data));
1623   cairo_set_source_rgb(cr, 0., 0., 0.);
1624
1625   poppler_page_render_for_printing(pdf_page, cr);
1626
1627   g_object_unref(G_OBJECT(pdf_page));
1628
1629   debug_print("Sent page %d to printer\n", page_nr+1);
1630 }
1631
1632 static void pdf_viewer_print(MimeViewer *mviewer)
1633 {
1634         PdfViewer *viewer = (PdfViewer *)mviewer;
1635         PrintRenderer *pdf_renderer = g_new0(PrintRenderer, 1);
1636         MainWindow *mainwin = mainwindow_get_mainwindow();
1637         
1638         pdf_renderer->get_pango_context = pdf_viewer_get_pango_context;
1639         pdf_renderer->get_data_to_print = pdf_viewer_get_data_to_print;
1640         pdf_renderer->cb_begin_print    = pdf_viewer_cb_begin_print;
1641         pdf_renderer->cb_draw_page      = pdf_viewer_cb_draw_page;
1642
1643         printing_print_full(mainwin ? GTK_WINDOW(mainwin->window):NULL,
1644                         pdf_renderer, viewer->pdf_doc, -1, -1);
1645         
1646         g_free(pdf_renderer);
1647 }
1648 #endif
1649
1650 static MimeViewer *pdf_viewer_create(void)
1651 {
1652         PdfViewer *viewer;
1653         GtkTreeViewColumn *column;
1654         GtkCellRenderer *renderer;
1655         GtkTreeStore *tree_store;
1656         GtkWidget *sep;
1657         GtkWidget *img;
1658         gint col = 0;
1659
1660         viewer = g_new0(PdfViewer, 1);
1661         debug_print("pdf_viewer_create\n");
1662     
1663         viewer->last_x = 0;
1664         viewer->last_y = 0;
1665         viewer->mimeviewer.factory = &pdf_viewer_factory;
1666         viewer->mimeviewer.get_widget = pdf_viewer_get_widget;
1667         viewer->mimeviewer.show_mimepart = pdf_viewer_show_mimepart;
1668         viewer->mimeviewer.clear_viewer = pdf_viewer_clear;
1669         viewer->mimeviewer.destroy_viewer = pdf_viewer_destroy;
1670         viewer->mimeviewer.text_search = pdf_viewer_text_search;
1671         viewer->mimeviewer.scroll_page = pdf_viewer_scroll_page;
1672         viewer->mimeviewer.scroll_one_line = pdf_viewer_scroll_one_line;
1673 #if GTK_CHECK_VERSION(2,10,0) && POPPLER_HAS_CAIRO && !USE_LIBGNOMEPRINT
1674         viewer->mimeviewer.print = pdf_viewer_print;
1675 #endif
1676         viewer->scrollwin = gtk_scrolled_window_new(NULL, NULL);
1677         viewer->scrollwin_index = gtk_scrolled_window_new(NULL, NULL);
1678         viewer->pdf_view_ebox = gtk_event_box_new();
1679         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->pdf_view_ebox), FALSE);
1680                                                         
1681         viewer->mimeinfo  = NULL;
1682
1683         viewer->pdf_view = gtk_image_new();
1684         gtk_widget_set_events(viewer->pdf_view_ebox,
1685                                                 GDK_BUTTON_RELEASE_MASK
1686                                                 | GDK_POINTER_MOTION_MASK
1687                                                 | GDK_BUTTON_PRESS_MASK
1688                                                 | GDK_BUTTON_MOTION_MASK
1689                                             );
1690         gtk_container_add (GTK_CONTAINER(viewer->pdf_view_ebox), viewer->pdf_view);
1691         viewer->icon_type = gtk_image_new();
1692         viewer->icon_type_ebox = gtk_event_box_new();
1693
1694         gtk_container_add(GTK_CONTAINER(viewer->icon_type_ebox), viewer->icon_type);
1695
1696         viewer->doc_label = gtk_label_new("");
1697
1698         viewer->widgets_table = gtk_table_new(1, 1, FALSE);
1699
1700         viewer->doc_index_pane = gtk_hpaned_new();
1701
1702         viewer->frame_index = gtk_frame_new(NULL);
1703         gtk_frame_set_shadow_type(GTK_FRAME(viewer->frame_index), GTK_SHADOW_IN);
1704         gtk_widget_set_size_request(viewer->frame_index, 18, -1);
1705         gtk_frame_set_label(GTK_FRAME(viewer->frame_index), _("Document Index"));
1706
1707 #if !(GTK_CHECK_VERSION(2,12,0))
1708         viewer->button_bar_tips = tips;
1709 #endif
1710
1711         ADD_SEP_TO_TABLE
1712         ADD_BUTTON_TO_TABLE(viewer->first_page, GTK_STOCK_GOTO_FIRST)
1713         ADD_BUTTON_TO_TABLE(viewer->prev_page, GTK_STOCK_GO_BACK)
1714         viewer->cur_page = gtk_spin_button_new_with_range(0.0, 0.0, 1.0);
1715         viewer->zoom_scroll = gtk_spin_button_new_with_range(0.20, 8.0, 0.20);
1716         gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->zoom_scroll), 1.0);
1717         viewer->zoom = 1.0;
1718         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(viewer->cur_page), TRUE);
1719         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(viewer->zoom_scroll), TRUE);
1720         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(viewer->cur_page),
1721                                         col, col+1, 
1722                                         0, 1, 0, 0, 
1723                                         BUTTON_H_PADDING, 
1724                                         0);
1725         col++;
1726         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(viewer->doc_label),
1727                                         col, col+1, 
1728                                         0, 1, 0, 0, 
1729                                         BUTTON_H_PADDING, 
1730                                         0);
1731         col++;
1732
1733         ADD_BUTTON_TO_TABLE(viewer->next_page, GTK_STOCK_GO_FORWARD)
1734         ADD_BUTTON_TO_TABLE(viewer->last_page, GTK_STOCK_GOTO_LAST)
1735         ADD_SEP_TO_TABLE
1736         ADD_BUTTON_TO_TABLE(viewer->zoom_fit, GTK_STOCK_ZOOM_FIT)
1737         ADD_BUTTON_TO_TABLE(viewer->zoom_in, GTK_STOCK_ZOOM_IN)
1738         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(viewer->zoom_scroll),
1739                                         col, col+1, 
1740                                         0, 1, 0, 0, 
1741                                         BUTTON_H_PADDING, 
1742                                         0);
1743         col++;
1744         ADD_BUTTON_TO_TABLE(viewer->zoom_out, GTK_STOCK_ZOOM_OUT)
1745         ADD_BUTTON_TO_TABLE(viewer->zoom_width, GTK_STOCK_FULLSCREEN)
1746         ADD_SEP_TO_TABLE
1747         ADD_BUTTON_TO_TABLE(viewer->rotate_left, GTK_STOCK_UNDO)
1748         ADD_BUTTON_TO_TABLE(viewer->rotate_right, GTK_STOCK_REDO)
1749         ADD_SEP_TO_TABLE
1750         ADD_BUTTON_TO_TABLE(viewer->doc_info, GTK_STOCK_INFO)
1751
1752         viewer->doc_index = GTK_WIDGET(gtk_toggle_tool_button_new_from_stock(GTK_STOCK_INDEX));
1753         gtk_widget_set_size_request(GTK_WIDGET(viewer->doc_index), 26, 26);
1754         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(viewer->doc_index),
1755                                         col, col+1, 
1756                                         0, 1, 0, 0, 
1757                                         BUTTON_H_PADDING, 
1758                                         0);
1759         col++;
1760         
1761         gtk_scrolled_window_set_policy(
1762                         GTK_SCROLLED_WINDOW(viewer->scrollwin), 
1763                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1764
1765         gtk_scrolled_window_set_shadow_type(
1766                         GTK_SCROLLED_WINDOW(viewer->scrollwin),
1767                         GTK_SHADOW_IN);
1768
1769         gtk_scrolled_window_add_with_viewport(
1770                         GTK_SCROLLED_WINDOW(viewer->scrollwin),
1771                         viewer->pdf_view_ebox);
1772
1773         viewer->vbox = gtk_vbox_new(FALSE, 4);
1774         viewer->hbox = gtk_hbox_new(FALSE, 4);
1775
1776     /* treeview */
1777         tree_store = gtk_tree_store_new(N_INDEX_COLUMNS,
1778                                         G_TYPE_STRING,
1779                                         G_TYPE_INT,
1780                                         G_TYPE_DOUBLE);
1781
1782         viewer->index_list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));
1783         renderer = gtk_cell_renderer_text_new();
1784         column = gtk_tree_view_column_new_with_attributes(_("Name"),  renderer, "text", 0,  NULL);
1785         gtk_tree_view_append_column(GTK_TREE_VIEW(viewer->index_list), column);         
1786         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(viewer->index_list), FALSE);
1787         
1788         viewer->index_model = GTK_TREE_MODEL(tree_store);
1789
1790         gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(viewer->index_list)), 
1791                                                                 GTK_SELECTION_SINGLE);
1792
1793         g_signal_connect(G_OBJECT(viewer->index_list), "row_activated",
1794                          G_CALLBACK(pdf_viewer_index_row_activated),
1795                                          viewer);
1796
1797         gtk_scrolled_window_set_policy(
1798                         GTK_SCROLLED_WINDOW(viewer->scrollwin_index), 
1799                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1800
1801         gtk_scrolled_window_set_shadow_type(
1802                         GTK_SCROLLED_WINDOW(viewer->scrollwin_index),
1803                         GTK_SHADOW_IN);
1804
1805         gtk_scrolled_window_add_with_viewport(
1806                         GTK_SCROLLED_WINDOW(viewer->scrollwin_index),
1807                         viewer->index_list);
1808
1809         /* end treeview */
1810
1811         stock_pixbuf_gdk(viewer->hbox, 
1812                         STOCK_PIXMAP_MIME_TEXT_PLAIN, 
1813                         &viewer->icon_pixbuf);
1814
1815         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->icon_type), 
1816                                 viewer->icon_pixbuf);
1817
1818         /* pack widgets*/
1819         gtk_box_pack_start(GTK_BOX(viewer->hbox), viewer->icon_type_ebox, FALSE, FALSE, 0);
1820         gtk_box_pack_start(GTK_BOX(viewer->hbox), viewer->widgets_table, FALSE, FALSE, 0);
1821
1822         gtk_container_add(GTK_CONTAINER(viewer->frame_index), viewer->scrollwin_index);
1823
1824         gtk_paned_pack1(GTK_PANED(viewer->doc_index_pane), viewer->frame_index, FALSE, FALSE);
1825         gtk_paned_pack2(GTK_PANED(viewer->doc_index_pane), viewer->scrollwin, FALSE, FALSE);
1826
1827         gtk_box_pack_start(GTK_BOX(viewer->vbox), viewer->hbox, FALSE, FALSE, 0);
1828         gtk_box_pack_start(GTK_BOX(viewer->vbox), viewer->doc_index_pane, TRUE, TRUE, 0);
1829         /* show widgets */
1830         gtk_widget_show(GTK_WIDGET(viewer->doc_index_pane));
1831         g_object_ref(GTK_WIDGET(viewer->doc_index_pane));
1832         gtk_widget_show(GTK_WIDGET(viewer->scrollwin));
1833         g_object_ref(GTK_WIDGET(viewer->scrollwin));
1834         gtk_widget_show(GTK_WIDGET(viewer->icon_type_ebox));
1835         g_object_ref(GTK_WIDGET(viewer->icon_type_ebox));
1836         gtk_widget_show(GTK_WIDGET(viewer->pdf_view_ebox));
1837         g_object_ref(GTK_WIDGET(viewer->pdf_view_ebox));
1838         gtk_widget_show(GTK_WIDGET(viewer->scrollwin_index));
1839         g_object_ref(GTK_WIDGET(viewer->scrollwin_index));
1840         gtk_widget_show(GTK_WIDGET(viewer->hbox));
1841         g_object_ref(GTK_WIDGET(viewer->hbox)); 
1842         gtk_widget_show(GTK_WIDGET(viewer->vbox));
1843         g_object_ref(GTK_WIDGET(viewer->vbox));
1844
1845         gtk_widget_show(GTK_WIDGET(viewer->widgets_table));
1846         g_object_ref(GTK_WIDGET(viewer->widgets_table));
1847
1848         gtk_widget_show(GTK_WIDGET(viewer->cur_page));
1849         g_object_ref(GTK_WIDGET(viewer->cur_page));
1850
1851         gtk_widget_show(GTK_WIDGET(viewer->first_page));
1852         g_object_ref(GTK_WIDGET(viewer->first_page));
1853
1854         gtk_widget_show(GTK_WIDGET(viewer->last_page));
1855         g_object_ref(GTK_WIDGET(viewer->last_page));
1856
1857         gtk_widget_show(GTK_WIDGET(viewer->prev_page));
1858         g_object_ref(GTK_WIDGET(viewer->prev_page));
1859
1860         gtk_widget_show(GTK_WIDGET(viewer->next_page));
1861         g_object_ref(GTK_WIDGET(viewer->next_page));
1862
1863         gtk_widget_show(GTK_WIDGET(viewer->zoom_in));
1864         g_object_ref(GTK_WIDGET(viewer->zoom_in));
1865         gtk_widget_show(GTK_WIDGET(viewer->zoom_out));
1866         g_object_ref(GTK_WIDGET(viewer->zoom_out));
1867         gtk_widget_show(GTK_WIDGET(viewer->zoom_fit));
1868         g_object_ref(GTK_WIDGET(viewer->zoom_fit));
1869         gtk_widget_show(GTK_WIDGET(viewer->zoom_width));
1870         g_object_ref(GTK_WIDGET(viewer->zoom_width));
1871
1872         gtk_widget_show(GTK_WIDGET(viewer->rotate_right));
1873         g_object_ref(GTK_WIDGET(viewer->rotate_right));
1874         gtk_widget_show(GTK_WIDGET(viewer->rotate_left));
1875         g_object_ref(GTK_WIDGET(viewer->rotate_left));
1876         gtk_widget_show(GTK_WIDGET(viewer->doc_info));
1877         g_object_ref(GTK_WIDGET(viewer->doc_info));
1878         gtk_widget_show(GTK_WIDGET(viewer->doc_index));
1879         g_object_ref(GTK_WIDGET(viewer->doc_index));
1880
1881         gtk_widget_show(GTK_WIDGET(viewer->doc_label));
1882         g_object_ref(GTK_WIDGET(viewer->doc_label));
1883         gtk_widget_show(GTK_WIDGET(viewer->icon_type));
1884         g_object_ref(GTK_WIDGET(viewer->icon_type));    
1885         gtk_widget_show(GTK_WIDGET(viewer->pdf_view));
1886         g_object_ref(GTK_WIDGET(viewer->pdf_view));
1887         gtk_widget_show(GTK_WIDGET(viewer->zoom_scroll));
1888         g_object_ref(GTK_WIDGET(viewer->zoom_scroll));
1889
1890         gtk_widget_show(GTK_WIDGET(viewer->index_list));
1891         g_object_ref(GTK_WIDGET(viewer->index_list));
1892
1893         /* Set Tooltips*/
1894         CLAWS_SET_TIP(viewer->first_page,
1895                                 _("First Page"));
1896
1897         CLAWS_SET_TIP(viewer->prev_page,
1898                                 _("Previous Page"));
1899
1900         CLAWS_SET_TIP(viewer->next_page,
1901                                 _("Next Page"));
1902         
1903         CLAWS_SET_TIP(viewer->last_page,
1904                                 _("Last Page"));
1905
1906         CLAWS_SET_TIP(viewer->zoom_in,
1907                                 _("Zoom In"));
1908         CLAWS_SET_TIP(viewer->zoom_out,
1909                                 _("Zoom Out"));
1910
1911         CLAWS_SET_TIP(viewer->zoom_fit,
1912                                 _("Fit Page"));
1913
1914         CLAWS_SET_TIP(viewer->zoom_width,
1915                                 _("Fit Page Width"));
1916
1917         CLAWS_SET_TIP(viewer->rotate_left,
1918                                 _("Rotate Left"));
1919
1920         CLAWS_SET_TIP(viewer->rotate_right,
1921                                 _("Rotate Right"));
1922
1923         CLAWS_SET_TIP(viewer->doc_info,
1924                                 _("Document Info"));
1925
1926         CLAWS_SET_TIP(viewer->doc_index,
1927                                 _("Document Index"));
1928         CLAWS_SET_TIP(viewer->cur_page,
1929                                 _("Page Number"));
1930         CLAWS_SET_TIP(viewer->zoom_scroll,
1931                                 _("Zoom Factor"));
1932         /* Connect Signals */
1933         g_signal_connect(G_OBJECT(viewer->cur_page), 
1934                                     "value-changed", 
1935                                     G_CALLBACK(pdf_viewer_spin_change_page_cb), 
1936                                    (gpointer) viewer);
1937
1938         g_signal_connect(G_OBJECT(viewer->first_page), 
1939                                     "clicked", 
1940                                     G_CALLBACK(pdf_viewer_button_first_page_cb), 
1941                                    (gpointer) viewer);
1942         g_signal_connect(G_OBJECT(viewer->prev_page), 
1943                                     "clicked", 
1944                                     G_CALLBACK(pdf_viewer_button_prev_page_cb), 
1945                                    (gpointer) viewer);
1946         g_signal_connect(G_OBJECT(viewer->next_page), 
1947                                     "clicked", 
1948                                     G_CALLBACK(pdf_viewer_button_next_page_cb), 
1949                                    (gpointer) viewer);
1950         g_signal_connect(G_OBJECT(viewer->last_page), 
1951                                     "clicked", 
1952                                     G_CALLBACK(pdf_viewer_button_last_page_cb), 
1953                                    (gpointer) viewer);
1954         g_signal_connect(G_OBJECT(viewer->zoom_in), 
1955                                     "clicked", 
1956                                     G_CALLBACK(pdf_viewer_button_zoom_in_cb), 
1957                                    (gpointer) viewer);
1958         g_signal_connect(G_OBJECT(viewer->zoom_out), 
1959                                     "clicked", 
1960                                     G_CALLBACK(pdf_viewer_button_zoom_out_cb), 
1961                                    (gpointer) viewer);
1962         g_signal_connect(G_OBJECT(viewer->zoom_scroll), 
1963                                     "value-changed", 
1964                                     G_CALLBACK(pdf_viewer_spin_zoom_scroll_cb), 
1965                                    (gpointer) viewer);
1966
1967         g_signal_connect(G_OBJECT(viewer->zoom_fit), 
1968                                    "clicked", 
1969                                     G_CALLBACK(pdf_viewer_button_zoom_fit_cb), 
1970                                    (gpointer) viewer);
1971
1972         g_signal_connect(G_OBJECT(viewer->zoom_width), 
1973                                     "clicked", 
1974                                     G_CALLBACK(pdf_viewer_button_zoom_width_cb), 
1975                                    (gpointer) viewer);
1976
1977         g_signal_connect(G_OBJECT(viewer->rotate_right), 
1978                                     "clicked", 
1979                                     G_CALLBACK(pdf_viewer_button_rotate_right_cb), 
1980                                    (gpointer) viewer);
1981         
1982         g_signal_connect(G_OBJECT(viewer->rotate_left), 
1983                                     "clicked", 
1984                                     G_CALLBACK(pdf_viewer_button_rotate_left_cb), 
1985                                    (gpointer) viewer);
1986         
1987         g_signal_connect(G_OBJECT(viewer->doc_info), 
1988                                     "clicked", 
1989                                     G_CALLBACK(pdf_viewer_button_document_info_cb), 
1990                                    (gpointer) viewer);  
1991         
1992         g_signal_connect(G_OBJECT(viewer->doc_index), 
1993                                     "clicked", 
1994                                     G_CALLBACK(pdf_viewer_show_document_index_cb), 
1995                                    (gpointer) viewer);
1996         g_signal_connect(G_OBJECT(viewer->scrollwin), 
1997                                     "scroll-event", 
1998                                     G_CALLBACK(pdf_viewer_scroll_cb), 
1999                                    (gpointer) viewer);
2000         g_signal_connect(G_OBJECT(viewer->pdf_view_ebox), 
2001                                     "button_press_event", 
2002                                     G_CALLBACK(pdf_viewer_button_press_events_cb), 
2003                                    (gpointer) viewer);
2004         g_signal_connect(G_OBJECT(viewer->pdf_view_ebox), 
2005                                     "button_release_event", 
2006                                     G_CALLBACK(pdf_viewer_mouse_scroll_destroy_cb), 
2007                                    (gpointer) viewer);
2008         g_signal_connect(G_OBJECT(viewer->pdf_view_ebox), 
2009                                     "motion_notify_event", 
2010                                     G_CALLBACK(pdf_viewer_move_events_cb), 
2011                                    (gpointer) viewer);
2012
2013         viewer->target_filename = NULL;
2014         viewer->filename = NULL;
2015         viewer->fsname = NULL;
2016
2017         return(MimeViewer *) viewer;
2018 }
2019
2020 #undef ADD_BUTTON_TO_TABLE
2021 #undef ADD_SEP_TO_TABLE
2022 #undef BUTTON_H_PADDING
2023 #undef SEP_H_PADDING
2024                 
2025 static MimeViewerFactory pdf_viewer_factory =
2026 {
2027         content_types,
2028         0,
2029         pdf_viewer_create,
2030 };
2031
2032 gint plugin_init(gchar **error)
2033 {
2034         gchar *gspath = NULL;
2035
2036         msg = g_strdup_printf(_("This plugin enables the viewing of PDF and PostScript "
2037                                 "attachments using the Poppler %s Lib and the gs tool.\n\n"
2038                                 "Any feedback is welcome: iwkse@claws-mail.org"
2039                                 ), poppler_get_version());
2040
2041         if (!check_plugin_version(MAKE_NUMERIC_VERSION(3,8,1,46),
2042                     VERSION_NUMERIC, _("PDF Viewer"), error)) return -1;
2043
2044         if ((gspath = g_find_program_in_path("gs")) == NULL) {
2045                 gchar *pmsg = msg;
2046                 msg = g_strdup_printf(_("Warning: could not find ghostscript binary (gs) required "
2047                                         "for %s plugin to process PostScript attachments, only PDF "
2048                                         "attachments will be displayed. To enable PostScript "
2049                                         "support please install gs program.\n\n%s"
2050                                         ), _("PDF Viewer"), pmsg);
2051                 g_free(pmsg);
2052         }
2053         else {
2054                 g_free(gspath);
2055         }
2056         
2057         mimeview_register_viewer_factory(&pdf_viewer_factory);
2058         return 0;
2059 }
2060
2061 gboolean plugin_done(void)
2062 {
2063         g_free(msg);    
2064         mimeview_unregister_viewer_factory(&pdf_viewer_factory);
2065         return TRUE;
2066 }
2067
2068 const gchar *plugin_name(void)
2069 {
2070         return _("PDF Viewer");
2071 }
2072
2073 const gchar *plugin_desc(void)
2074 {
2075         return msg;
2076 }
2077
2078 const gchar *plugin_type(void)
2079 {
2080         return "GTK2";
2081 }
2082
2083 const gchar *plugin_licence(void)
2084 {
2085         return "GPL3+";
2086 }
2087
2088 const gchar *plugin_version(void)
2089 {
2090         return VERSION;
2091 }
2092
2093 struct PluginFeature *plugin_provides(void)
2094 {
2095         static struct PluginFeature features[] = 
2096                 { {PLUGIN_MIMEVIEWER, "application/pdf"},
2097                   {PLUGIN_MIMEVIEWER, "application/postscript"},
2098                   {PLUGIN_NOTHING, NULL} };
2099         return features;
2100 }
2101