2007-09-25 [holger] 3.0.1cvs27
[claws.git] / src / printing.c
index f4ab09d031917ad3cc13d4dbb50a0642205bda6d..6f45d127f496c4b2d480348051dfc7122c01e2bd 100644 (file)
@@ -1,6 +1,6 @@
 /* Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2007 Holger Berndt <hb@claws-mail.org> 
- * and the Claws Mail team
+ * Copyright (C) 2007 Holger Berndt <hb@claws-mail.org>,
+ * Colin Leroy <colin@colino.net>, and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -27,6 +27,7 @@
 #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT)
 
 #include "gtkutils.h"
+#include "toolbar.h"
 #include "prefs_common.h"
 
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 #include <pango/pango.h>
 #include <string.h>
+#include <math.h>
 
 typedef struct {
   PangoLayout *layout;
   PangoContext *pango_context;
   char *text;
   GList *page_breaks;
+  guint npages;
   GtkTextBuffer *buffer;
   gint sel_start;
   gint sel_end;
   GHashTable *images;
   gint img_cnt;
-  gboolean print_started;
-  gchar *old_print_preview;
+  gdouble zoom;
 } PrintData;
 
+typedef struct {
+  GtkPrintOperation *op;
+  GtkPrintOperationPreview *preview;
+  GtkWidget         *dialog;
+  GtkWidget         *scrolled_window;
+  GtkWidget         *area;
+  PrintData         *print_data;
+  GtkWidget         *page_nr_label;
+  GList             *pages_to_print;
+  GList             *current_page;
+  GtkWidget *first;
+  GtkWidget *next;
+  GtkWidget *previous;
+  GtkWidget *last;
+  GtkWidget *close;
+  GtkWidget *zoom_100;
+  GtkWidget *zoom_fit;
+  GtkWidget *zoom_in;
+  GtkWidget *zoom_out;
+  gboolean rendering;
+  gint page_width;
+  gint page_height;
+} PreviewData;
 
 /* callbacks */
-static void cb_begin_print(GtkPrintOperation*, GtkPrintContext*, gpointer);
-static void cb_draw_page(GtkPrintOperation*, GtkPrintContext*, gint, gpointer);
+static void     cb_begin_print(GtkPrintOperation*, GtkPrintContext*, gpointer);
+static void     cb_draw_page(GtkPrintOperation*, GtkPrintContext*, gint,
+                            gpointer);
+static gboolean cb_preview(GtkPrintOperation*, GtkPrintOperationPreview*,
+                          GtkPrintContext*, GtkWindow*, gpointer);
+static void     cb_preview_destroy(GtkWindow*, gpointer);
+static gboolean cb_preview_close(GtkWidget*, GdkEventAny*, gpointer);
+static void     cb_preview_size_allocate(GtkWidget*, GtkAllocation*);
+static void     cb_preview_ready(GtkPrintOperationPreview*,
+                                GtkPrintContext*, gpointer);
+static gboolean cb_preview_expose(GtkWidget*, GdkEventExpose*, gpointer);
+static void     cb_preview_got_page_size(GtkPrintOperationPreview*,
+                                        GtkPrintContext*,
+                                        GtkPageSetup*, gpointer);
+static void     cb_preview_go_first(GtkButton*, gpointer);
+static void     cb_preview_go_previous(GtkButton*, gpointer);
+static void     cb_preview_go_next(GtkButton*, gpointer);
+static void     cb_preview_go_last(GtkButton*, gpointer);
+static void     cb_preview_btn_close(GtkButton*, gpointer);
+static void     cb_preview_zoom_100(GtkButton*, gpointer);
+static void     cb_preview_zoom_fit(GtkButton*, gpointer);
+static void     cb_preview_zoom_in(GtkButton*, gpointer);
+static void     cb_preview_zoom_out(GtkButton*, gpointer);
+static void     cb_preview_request_page_setup(GtkPrintOperation*,
+                                             GtkPrintContext*,
+                                             gint,GtkPageSetup*,gpointer);
+
+static void     printing_preview_update_zoom_sensitivity(PreviewData*);
 
 /* variables */
 static GtkPrintSettings *settings   = NULL;
@@ -63,136 +114,16 @@ static void     printing_layout_set_text_attributes(PrintData*, GtkPrintContext
 static gboolean printing_is_pango_gdk_color_equal(PangoColor*, GdkColor*); 
 static gint     printing_text_iter_get_offset_bytes(PrintData *, const GtkTextIter*);
 
-static gboolean claws_draw_page(GtkPrintOperation *op, GtkPrintContext *context, gint page_nr, gpointer user_data);
-
 #define PREVIEW_SCALE 72
-static void preview_destroy (GtkWindow *window, GtkPrintOperationPreview *preview)
-{
-  gtk_print_operation_preview_end_preview (preview);
-}
-
-static gboolean preview_close(GtkWidget *widget, GdkEventAny *event,
-                                gpointer data)
-{
-       if (event->type == GDK_KEY_PRESS)
-               if (((GdkEventKey *)event)->keyval != GDK_Escape)
-                       return FALSE;
-
-       gtk_widget_destroy(widget);
-       return FALSE;
-}
+#define PREVIEW_SHADOW_OFFSET 3
+#define PREVIEW_ZOOM_FAC 1.41
+#define PREVIEW_ZOOM_MAX 10.
+#define PREVIEW_ZOOM_MIN 0.2
 
-static gboolean cb_preview (GtkPrintOperation        *operation,
-                                     GtkPrintOperationPreview *preview,
-                                     GtkPrintContext          *context,
-                                     GtkWindow                *parent,
-                                     PrintData                *print_data)
+static void free_pixbuf(gpointer key, gpointer value, gpointer data)
 {
-  GtkPageSetup    *page_setup = gtk_print_context_get_page_setup (context);
-  GtkPaperSize    *paper_size;
-  gdouble          paper_width;
-  gdouble          paper_height;
-  gdouble          top_margin;
-  gdouble          bottom_margin;
-  gdouble          left_margin;
-  gdouble          right_margin;
-  gint             preview_width;
-  gint             preview_height;
-  gint num_pages = 0, i = 0;
-  cairo_t         *cr;
-  cairo_surface_t *surface;
-  GtkPageOrientation orientation;
-  cairo_status_t   status;
-  gchar           *fname;
-  GtkWidget *dialog = NULL;
-  GtkWidget *image, *notebook;
-  GSList *pages = NULL, *cur;
-
-  paper_size      = gtk_page_setup_get_paper_size    (page_setup);
-  paper_width     = gtk_paper_size_get_width         (paper_size, GTK_UNIT_INCH);
-  paper_height    = gtk_paper_size_get_height        (paper_size,  GTK_UNIT_INCH);
-  top_margin      = gtk_page_setup_get_top_margin    (page_setup, GTK_UNIT_INCH);
-  bottom_margin   = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_INCH);
-  left_margin     = gtk_page_setup_get_left_margin   (page_setup, GTK_UNIT_INCH);
-  right_margin    = gtk_page_setup_get_right_margin  (page_setup, GTK_UNIT_INCH);
-
-  /* the print context does not have the page orientation, it is transformed */
-  orientation     = gtk_page_setup_get_orientation (page_setup);
-
-  if (orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
-      orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
-    {
-      preview_width  = PREVIEW_SCALE * paper_width;
-      preview_height = PREVIEW_SCALE * paper_height;
-    }
-  else
-    {
-      preview_width  = PREVIEW_SCALE * paper_height;
-      preview_height = PREVIEW_SCALE * paper_width;
-    }
-
-
-  num_pages = 1;  
-  for (i = 0; i < num_pages; i++) {
-    surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
-                                              preview_width, preview_height);
-
-    if (CAIRO_STATUS_SUCCESS != cairo_surface_status (surface)) {
-      g_message ("Unable to create preview (not enough memory?)");
-      return TRUE;
-    }
-
-    cr = cairo_create (surface);
-    gtk_print_context_set_cairo_context (context, cr, PREVIEW_SCALE, PREVIEW_SCALE);
-
-    /* fill page with white */
-    cairo_set_source_rgb (cr, 1, 1, 1);
-    cairo_new_path (cr);
-    cairo_rectangle (cr, 0, 0, preview_width, preview_height);
-    cairo_fill (cr);
-
-    cairo_translate (cr, left_margin * PREVIEW_SCALE, right_margin * PREVIEW_SCALE);
-
-    claws_draw_page (operation, context, i, print_data);
-    num_pages = g_list_length(print_data->page_breaks) + 1;
-
-    fname = get_tmp_file();
-    status = cairo_surface_write_to_png (surface, fname);
-    cairo_destroy (cr);
-    cairo_surface_destroy (surface);
-    if (status == CAIRO_STATUS_SUCCESS) {
-       image = gtk_image_new_from_file (fname);
-       g_unlink (fname);
-       g_free (fname);
-       pages = g_slist_prepend(pages, image);
-       debug_print("added one page\n");
-    }
-  }
-  pages = g_slist_reverse(pages);
-  
-  dialog = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "print_preview");
-  gtk_window_set_title(GTK_WINDOW(dialog), _("Print preview"));
-  notebook = gtk_notebook_new();
-  gtk_container_add(GTK_CONTAINER(dialog), notebook);
-  i = 0;
-  for (cur = pages; cur; cur = cur->next) {
-    image = (GtkImage *)cur->data;
-    if (gtk_print_operation_preview_is_selected(preview, i)) {
-      debug_print("page %d sel\n", i);
-      gtk_notebook_append_page(GTK_NOTEBOOK(notebook), image, NULL);
-    }
-    i++;
-  }
-  g_slist_free(pages);
-  
-  gtk_widget_show_all(dialog);
-  
-  g_signal_connect (dialog, "destroy",
-                    G_CALLBACK (preview_destroy), preview);
-  g_signal_connect (dialog, "key_press_event",
-                    G_CALLBACK (preview_close), preview);
-  
-  return TRUE;
+  PangoAttrShape *attr = (PangoAttrShape *) value;
+  g_object_unref(G_OBJECT(attr->data));
 }
 
 void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, gint sel_end)
@@ -207,6 +138,8 @@ void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, g
 
   print_data = g_new0(PrintData,1);
 
+  print_data->zoom = 1.;
+
   print_data->images = g_hash_table_new(g_direct_hash, g_direct_equal);
   print_data->pango_context=gtk_widget_get_pango_context(GTK_WIDGET(text_view));
 
@@ -233,10 +166,12 @@ void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, g
     gtk_print_settings_set_duplex(settings, prefs_common.print_use_duplex);
   }
   if (page_setup == NULL) {
-    GtkPaperSize *paper = gtk_paper_size_new(prefs_common.print_paper_type);
     page_setup = gtk_page_setup_new();
-    gtk_page_setup_set_paper_size(page_setup, paper);
-    gtk_paper_size_free(paper);
+    if (prefs_common.print_paper_type && *prefs_common.print_paper_type) {
+      GtkPaperSize *paper = gtk_paper_size_new(prefs_common.print_paper_type);
+      gtk_page_setup_set_paper_size(page_setup, paper);
+      gtk_paper_size_free(paper);
+    }
     gtk_page_setup_set_orientation(page_setup, prefs_common.print_paper_orientation);
   }
   
@@ -270,6 +205,7 @@ void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, g
     prefs_common.print_use_duplex = gtk_print_settings_get_duplex(settings);
   }
 
+  g_hash_table_foreach(print_data->images, free_pixbuf, NULL);
   g_hash_table_destroy(print_data->images);
   if(print_data->text)
     g_free(print_data->text);
@@ -277,13 +213,6 @@ void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, g
   if(print_data->layout)
     g_object_unref(print_data->layout);
 
-  if (print_data->old_print_preview) {
-    g_object_set(gtk_settings_get_default(),
-                 "gtk-print-preview-command", print_data->old_print_preview, NULL);
-    g_free(print_data->old_print_preview);
-    print_data->old_print_preview = NULL;
-  }
-
   g_free(print_data);
 
   g_object_unref(op);
@@ -302,10 +231,12 @@ void printing_page_setup(GtkWindow *parent)
     gtk_print_settings_set_duplex(settings, prefs_common.print_use_duplex);
   }
   if (page_setup == NULL) {
-    GtkPaperSize *paper = gtk_paper_size_new(prefs_common.print_paper_type);
     page_setup = gtk_page_setup_new();
-    gtk_page_setup_set_paper_size(page_setup, paper);
-    gtk_paper_size_free(paper);
+    if (prefs_common.print_paper_type && *prefs_common.print_paper_type) {
+      GtkPaperSize *paper = gtk_paper_size_new(prefs_common.print_paper_type);
+      gtk_page_setup_set_paper_size(page_setup, paper);
+      gtk_paper_size_free(paper);
+    }
     gtk_page_setup_set_orientation(page_setup, prefs_common.print_paper_orientation);
   }
 
@@ -322,6 +253,450 @@ void printing_page_setup(GtkWindow *parent)
   prefs_common.print_paper_orientation = gtk_page_setup_get_orientation(page_setup);
 }
 
+static gboolean cb_preview(GtkPrintOperation        *operation,
+                          GtkPrintOperationPreview *preview,
+                          GtkPrintContext          *context,
+                          GtkWindow                *parent,
+                          gpointer                 data)
+{
+  PrintData *print_data;
+  cairo_t *cr;
+  PreviewData *preview_data;
+  GtkWidget *vbox;
+  GtkWidget *toolbar;
+  GtkWidget *da;
+  GtkWidget *sw;
+  GtkWidget *page;
+  GtkToolItem *separator;
+  static GdkGeometry geometry;
+  GtkWidget *dialog = NULL;
+  GtkTooltips *toolbar_tips = gtk_tooltips_new();
+  GtkWidget *statusbar = gtk_hbox_new(2, FALSE);
+  debug_print("Creating internal print preview\n");
+
+  print_data = (PrintData*) data;
+
+  preview_data = g_new0(PreviewData,1);
+  preview_data->print_data = print_data;
+  preview_data->op = g_object_ref(operation);
+  preview_data->preview = preview;
+
+  /* Window */
+  dialog = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "print_preview");
+  preview_data->dialog = dialog;
+  if(!geometry.min_height) {
+    geometry.min_width = 600;
+    geometry.min_height = 400;
+  }
+  gtk_window_set_geometry_hints(GTK_WINDOW(dialog), NULL, &geometry,
+                               GDK_HINT_MIN_SIZE);
+  gtk_widget_set_size_request(dialog, prefs_common.print_previewwin_width,
+                             prefs_common.print_previewwin_height);
+  gtk_window_set_title(GTK_WINDOW(dialog), _("Print preview"));
+
+  /* vbox */
+  vbox = gtk_vbox_new(FALSE, 0);
+  gtk_container_add(GTK_CONTAINER(dialog), vbox);
+  
+  /* toolbar */
+  toolbar = gtk_toolbar_new();
+  gtk_toolbar_set_orientation(GTK_TOOLBAR(toolbar), GTK_ORIENTATION_HORIZONTAL);
+  switch (prefs_common.toolbar_style) {
+  case TOOLBAR_ICON:
+    gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
+    break;
+  case TOOLBAR_TEXT:
+    gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_TEXT);
+    break;
+  case TOOLBAR_BOTH_HORIZ:
+    gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_BOTH_HORIZ);
+    break;
+  case TOOLBAR_BOTH:
+  default:       
+    gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_BOTH);
+  }
+  gtk_toolbar_set_show_arrow(GTK_TOOLBAR(toolbar), TRUE);
+       
+  gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);
+
+#define TOOLBAR_ITEM(item,text,tooltip,cb,cbdata) {                                                            \
+       item = GTK_WIDGET(gtk_tool_button_new_from_stock(text));                                        \
+       gtk_tool_item_set_homogeneous(GTK_TOOL_ITEM(item), FALSE);                                      \
+       gtk_tool_item_set_is_important(GTK_TOOL_ITEM(item), TRUE);                                      \
+       gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(item), -1);                              \
+       g_signal_connect (G_OBJECT(item), "clicked", G_CALLBACK(cb), cbdata);                           \
+       gtk_tool_item_set_tooltip(GTK_TOOL_ITEM(item), GTK_TOOLTIPS(toolbar_tips),                      \
+                       tooltip, NULL);                                                                 \
+}
+
+  TOOLBAR_ITEM(preview_data->first, GTK_STOCK_GOTO_FIRST, _("First page"), cb_preview_go_first, preview_data);
+  TOOLBAR_ITEM(preview_data->previous, GTK_STOCK_GO_BACK, _("Previous page"), cb_preview_go_previous, preview_data);
+  
+  page = gtk_label_new("");
+  gtk_widget_set_size_request(page, 100, -1);
+  preview_data->page_nr_label = page;
+  
+  TOOLBAR_ITEM(preview_data->next, GTK_STOCK_GO_FORWARD, _("Next page"), cb_preview_go_next, preview_data);
+  TOOLBAR_ITEM(preview_data->last, GTK_STOCK_GOTO_LAST, _("Last page"), cb_preview_go_last, preview_data);
+  
+  separator = gtk_separator_tool_item_new();
+  gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(separator), -1);
+
+  TOOLBAR_ITEM(preview_data->zoom_100, GTK_STOCK_ZOOM_100, _("Zoom 100%"), cb_preview_zoom_100, preview_data);
+  TOOLBAR_ITEM(preview_data->zoom_fit, GTK_STOCK_ZOOM_FIT, _("Zoom fit"), cb_preview_zoom_fit, preview_data);
+  TOOLBAR_ITEM(preview_data->zoom_in, GTK_STOCK_ZOOM_IN, _("Zoom in"), cb_preview_zoom_in, preview_data);
+  TOOLBAR_ITEM(preview_data->zoom_out, GTK_STOCK_ZOOM_OUT, _("Zoom out"), cb_preview_zoom_out, preview_data);
+
+  separator = gtk_separator_tool_item_new();
+  gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(separator), -1);
+
+  /* tooltip has to be NULL else it triggers an expose_event */
+  TOOLBAR_ITEM(preview_data->close, GTK_STOCK_CLOSE, NULL, cb_preview_btn_close, preview_data);
+
+  gtk_widget_show(statusbar);
+  gtk_box_pack_start(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
+  gtk_box_pack_start(GTK_BOX(statusbar), page, FALSE, FALSE, 0);
+  /* Drawing area */
+  sw = gtk_scrolled_window_new(NULL, NULL);
+  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
+                                GTK_POLICY_AUTOMATIC,
+                                GTK_POLICY_AUTOMATIC);
+  gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0);
+  da = gtk_drawing_area_new();
+  gtk_widget_set_double_buffered(da, FALSE);
+  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw),
+                                       da);
+  gtk_widget_realize(da);
+  preview_data->scrolled_window = sw;
+  preview_data->area = da;
+
+  /* cairo context */
+  cr = gdk_cairo_create(da->window);
+  gtk_print_context_set_cairo_context(context, cr, PREVIEW_SCALE, PREVIEW_SCALE);
+  cairo_destroy(cr);
+
+  /* signals */
+  g_signal_connect(dialog, "key_press_event",
+                  G_CALLBACK(cb_preview_close), preview_data);
+  g_signal_connect(dialog, "size_allocate",
+                  G_CALLBACK(cb_preview_size_allocate), NULL);
+  g_signal_connect(dialog, "destroy",G_CALLBACK(cb_preview_destroy),
+                  preview_data);
+  g_signal_connect(preview, "ready", G_CALLBACK(cb_preview_ready),
+                  preview_data);
+  g_signal_connect(preview, "got-page-size",
+                  G_CALLBACK(cb_preview_got_page_size), preview_data);
+
+  g_signal_connect(operation, "request-page-setup",
+                  G_CALLBACK(cb_preview_request_page_setup), preview_data);
+
+  gtk_widget_show_all(dialog);
+  return TRUE;
+}
+
+static void cb_preview_destroy(GtkWindow *window, gpointer data)
+{
+  PreviewData *preview_data;
+  preview_data = (PreviewData*) data;
+
+  if (preview_data->rendering)
+    return;
+  debug_print("Preview window destroyed\n");
+
+  gtk_print_operation_preview_end_preview(preview_data->preview);
+  g_object_unref(preview_data->op);
+  g_list_free(preview_data->pages_to_print);
+
+  g_free(preview_data);
+}
+
+static gboolean cb_preview_close(GtkWidget *widget, GdkEventAny *event,
+                                gpointer data)
+{
+  PreviewData *preview_data = (PreviewData *)data;
+  if(event->type == GDK_KEY_PRESS)
+    if(((GdkEventKey *)event)->keyval != GDK_Escape)
+      return FALSE;
+  if (preview_data->rendering)
+    return FALSE; 
+  gtk_widget_destroy(widget);
+  return FALSE;
+}
+
+static void cb_preview_size_allocate(GtkWidget *widget,
+                                    GtkAllocation *allocation)
+{
+  g_return_if_fail(allocation != NULL);
+
+  prefs_common.print_previewwin_width = allocation->width;
+  prefs_common.print_previewwin_height = allocation->height;
+}
+
+static void cb_preview_ready(GtkPrintOperationPreview *preview,
+                            GtkPrintContext          *context,
+                            gpointer                  data)
+{
+  PreviewData *preview_data;
+  gint iPage;
+  preview_data = (PreviewData*) data;
+  debug_print("preview_ready %d\n", preview_data->print_data->npages);
+  
+  for(iPage = 0; iPage < (preview_data->print_data->npages); iPage++) {
+    if(gtk_print_operation_preview_is_selected(preview_data->preview, iPage)) {
+      preview_data->pages_to_print = 
+       g_list_prepend(preview_data->pages_to_print, GINT_TO_POINTER(iPage));
+      debug_print("want to print page %d\n",iPage+1);
+    }
+  }
+
+  preview_data->pages_to_print = g_list_reverse(preview_data->pages_to_print);
+  preview_data->current_page = preview_data->pages_to_print;
+
+  g_signal_connect(preview_data->area, "expose_event",
+                  G_CALLBACK(cb_preview_expose),
+                  preview_data);
+
+  gtk_widget_queue_draw(preview_data->area);
+}
+
+static void cb_preview_got_page_size(GtkPrintOperationPreview *preview,
+                                    GtkPrintContext          *context,
+                                    GtkPageSetup             *page_setup,
+                                    gpointer                  data)
+{
+  PreviewData *preview_data;
+  GtkPageOrientation orientation;
+  GtkPaperSize *paper_size;
+  gint paper_width;
+  gint paper_height;
+
+  preview_data = (PreviewData*) data;
+  debug_print("got_page_size\n");
+  orientation  = gtk_page_setup_get_orientation(page_setup);
+  paper_size   = gtk_page_setup_get_paper_size(page_setup);
+  paper_width  = (gint)(gtk_paper_size_get_width(paper_size, GTK_UNIT_INCH)  
+                       * PREVIEW_SCALE);
+  paper_height = (gint)(gtk_paper_size_get_height(paper_size,  GTK_UNIT_INCH)
+                       * PREVIEW_SCALE);
+
+  preview_data->page_width  = paper_width;
+  preview_data->page_height = paper_height;
+
+  debug_print("w/h %d/%d\n", paper_width, paper_height);
+  gtk_widget_set_size_request(GTK_WIDGET(preview_data->area), 
+                             paper_width, paper_height);
+}
+
+static gboolean cb_preview_expose(GtkWidget *widget, GdkEventExpose *event,
+                                 gpointer data)
+{
+  PreviewData *preview_data = data;
+  GdkGC *gc;
+  GdkColor white;
+  GdkColor black;
+  GdkColor gray;
+
+  debug_print("preview_expose (current %p)\n", preview_data->current_page);
+  gdk_window_clear(preview_data->area->window);
+
+  white.red   = 65535;
+  white.green = 65535;
+  white.blue  = 65535;
+  black.red   = 0;
+  black.green = 0;
+  black.blue  = 0;
+  gray.red   = 32700;
+  gray.green = 32700;
+  gray.blue  = 32700;
+
+  gc = gdk_gc_new(GDK_DRAWABLE(preview_data->area->window));
+
+  /* background */
+  gdk_gc_set_rgb_fg_color(gc, &gray);
+  gdk_draw_rectangle(preview_data->area->window, gc, TRUE, 0, 0,
+                    preview_data->area->allocation.width,
+                    preview_data->area->allocation.height);
+
+  /* shadow */
+  gdk_gc_set_rgb_fg_color(gc, &black);
+  gdk_draw_rectangle(preview_data->area->window, gc, TRUE,
+                    PREVIEW_SHADOW_OFFSET, PREVIEW_SHADOW_OFFSET,
+                    preview_data->page_width+PREVIEW_SHADOW_OFFSET,
+                    preview_data->page_height+PREVIEW_SHADOW_OFFSET);
+
+  /* paper */
+  gdk_gc_set_rgb_fg_color(gc, &white);
+  gdk_draw_rectangle(preview_data->area->window, gc, TRUE, 0, 0,
+                    preview_data->page_width,
+                    preview_data->page_height);
+
+  g_object_unref(gc);
+
+  if(preview_data->current_page) {
+    preview_data->rendering = TRUE;
+    gtk_widget_set_sensitive(preview_data->close, FALSE);
+    int cur = GPOINTER_TO_INT(preview_data->current_page->data);
+    gchar *str;
+    str = g_strdup_printf(_("Page %d"), cur+1);
+    gtk_label_set_text(GTK_LABEL(preview_data->page_nr_label), str);
+    g_free(str);
+    gtk_print_operation_preview_render_page(preview_data->preview,
+                                           GPOINTER_TO_INT
+                                           (preview_data->current_page->data));
+
+    gtk_widget_set_sensitive(preview_data->first, preview_data->current_page->prev != NULL);
+    gtk_widget_set_sensitive(preview_data->previous, preview_data->current_page->prev != NULL);
+    gtk_widget_set_sensitive(preview_data->next, preview_data->current_page->next != NULL);
+    gtk_widget_set_sensitive(preview_data->last, preview_data->current_page->next != NULL);
+    gtk_widget_set_sensitive(preview_data->close, TRUE);
+    preview_data->rendering = FALSE;
+
+  }
+  return TRUE;
+}
+
+static void cb_preview_go_first(GtkButton *button, gpointer data)
+{
+  PreviewData *preview_data = (PreviewData*) data;
+  preview_data->current_page = preview_data->pages_to_print;
+  gtk_widget_queue_draw(preview_data->area);
+}
+
+static void cb_preview_go_previous(GtkButton *button, gpointer data)
+{
+  GList *next;
+  PreviewData *preview_data = (PreviewData*) data;
+  next = g_list_previous(preview_data->current_page);
+  if(next)
+    preview_data->current_page = next;
+  gtk_widget_queue_draw(preview_data->area);
+}
+
+static void cb_preview_go_next(GtkButton *button, gpointer data)
+{
+  GList *next;
+  PreviewData *preview_data = (PreviewData*) data;
+  next = g_list_next(preview_data->current_page);
+  if(next)
+    preview_data->current_page = next;
+  gtk_widget_queue_draw(preview_data->area);
+}
+
+static void cb_preview_go_last(GtkButton *button, gpointer data)
+{
+  PreviewData *preview_data = (PreviewData*) data;
+  preview_data->current_page = g_list_last(preview_data->current_page);
+  gtk_widget_queue_draw(preview_data->area);
+}
+
+static void cb_preview_btn_close(GtkButton *button, gpointer data)
+{
+  PreviewData *preview_data = (PreviewData *)data;
+  if (preview_data->rendering)
+    return; 
+  gtk_widget_destroy(preview_data->dialog);
+}
+
+static void cb_preview_zoom_100(GtkButton *button, gpointer data)
+{
+  PreviewData *preview_data = (PreviewData*) data;
+  if(preview_data->print_data->zoom != 1.) {
+    preview_data->print_data->zoom = 1.;
+    gtk_widget_queue_draw(preview_data->area);
+    printing_preview_update_zoom_sensitivity(preview_data);
+  }
+}
+
+static void cb_preview_zoom_fit(GtkButton *button, gpointer data)
+{
+  PreviewData *preview_data = (PreviewData*) data;
+  gdouble zoom_w;
+  gdouble zoom_h;
+  
+  zoom_w = ((gdouble)preview_data->scrolled_window->allocation.width) /
+    ((gdouble)preview_data->page_width/preview_data->print_data->zoom +
+     PREVIEW_SHADOW_OFFSET);
+  zoom_h = ((gdouble)preview_data->scrolled_window->allocation.height) /
+    ((gdouble)preview_data->page_height/preview_data->print_data->zoom +
+     PREVIEW_SHADOW_OFFSET);
+
+  preview_data->print_data->zoom = MIN(zoom_w,zoom_h) - 0.01;
+
+  if(preview_data->print_data->zoom > PREVIEW_ZOOM_MAX)
+    preview_data->print_data->zoom = PREVIEW_ZOOM_MAX;
+  else if(preview_data->print_data->zoom < PREVIEW_ZOOM_MIN)
+    preview_data->print_data->zoom = PREVIEW_ZOOM_MIN;
+
+  printing_preview_update_zoom_sensitivity(preview_data);
+  gtk_widget_queue_draw(preview_data->area);
+}
+
+static void cb_preview_zoom_in(GtkButton *button, gpointer data)
+{
+  PreviewData *preview_data = (PreviewData*) data;
+  gdouble new_zoom;
+  new_zoom =  preview_data->print_data->zoom * PREVIEW_ZOOM_FAC;
+  if(new_zoom <= PREVIEW_ZOOM_MAX) {
+    preview_data->print_data->zoom = new_zoom;
+    printing_preview_update_zoom_sensitivity(preview_data);
+    gtk_widget_queue_draw(preview_data->area);
+  }
+}
+
+static void cb_preview_zoom_out(GtkButton *button, gpointer data)
+{
+  PreviewData *preview_data = (PreviewData*) data;
+  gdouble new_zoom;
+  new_zoom =  preview_data->print_data->zoom / PREVIEW_ZOOM_FAC;
+  if(new_zoom >= PREVIEW_ZOOM_MIN) {
+    preview_data->print_data->zoom = new_zoom;
+    printing_preview_update_zoom_sensitivity(preview_data);
+    gtk_widget_queue_draw(preview_data->area);
+  }
+}
+
+static void cb_preview_request_page_setup(GtkPrintOperation *op,
+                                         GtkPrintContext *context,
+                                         gint page_nr,
+                                         GtkPageSetup *setup,gpointer data)
+{
+  GtkPaperSize *paper_size;
+  GtkPaperSize *old_size;
+  gdouble width;
+  gdouble height;
+  gdouble top_margin;
+  gdouble bottom_margin;
+  gdouble left_margin;
+  gdouble right_margin;
+
+  PreviewData *preview_data = (PreviewData*) data;
+
+  old_size = gtk_page_setup_get_paper_size(setup);
+  width  = gtk_paper_size_get_width(old_size,GTK_UNIT_INCH);
+  height = gtk_paper_size_get_height(old_size,GTK_UNIT_INCH);
+
+  top_margin    = gtk_page_setup_get_top_margin(setup,GTK_UNIT_INCH);
+  bottom_margin = gtk_page_setup_get_bottom_margin(setup,GTK_UNIT_INCH);
+  left_margin   = gtk_page_setup_get_left_margin(setup,GTK_UNIT_INCH);
+  right_margin  = gtk_page_setup_get_right_margin(setup,GTK_UNIT_INCH);
+
+  paper_size = gtk_paper_size_new_custom("preview paper", "preview_paper",
+                                        width*preview_data->print_data->zoom,
+                                        height*preview_data->print_data->zoom,
+                                        GTK_UNIT_INCH);
+  gtk_page_setup_set_paper_size(setup, paper_size);
+  gtk_paper_size_free(paper_size);
+
+  gtk_page_setup_set_top_margin(setup,top_margin*preview_data->print_data->zoom,
+                               GTK_UNIT_INCH);
+  gtk_page_setup_set_bottom_margin(setup,bottom_margin*preview_data->print_data->zoom,
+                                  GTK_UNIT_INCH);
+  gtk_page_setup_set_left_margin(setup,left_margin*preview_data->print_data->zoom,
+                                GTK_UNIT_INCH);
+  gtk_page_setup_set_right_margin(setup,right_margin*preview_data->print_data->zoom,
+                                 GTK_UNIT_INCH);
+}
+
 static void cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context,
                           gpointer user_data)
 {
@@ -338,8 +713,6 @@ static void cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context,
 
   print_data = (PrintData*) user_data;
 
-  if (print_data->print_started)
-    return;
   debug_print("Preparing print job...\n");
        
   width  = gtk_print_context_get_width(context);
@@ -399,12 +772,12 @@ static void cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context,
   pango_layout_iter_free(iter);
 
   page_breaks = g_list_reverse(page_breaks);
-  gtk_print_operation_set_n_pages(op, g_list_length(page_breaks) + 1);
-       
+  print_data->npages = g_list_length(page_breaks) + 1; 
   print_data->page_breaks = page_breaks;
 
+  gtk_print_operation_set_n_pages(op, print_data->npages);
+
   debug_print("Starting print job...\n");
-  print_data->print_started = TRUE;
 }
 
 static cairo_surface_t *pixbuf_to_surface(GdkPixbuf *pixbuf)
@@ -485,7 +858,8 @@ static cairo_surface_t *pixbuf_to_surface(GdkPixbuf *pixbuf)
        return surface;
 }
 
-static gboolean claws_draw_page(GtkPrintOperation *op, GtkPrintContext *context, gint page_nr, gpointer user_data)
+static void cb_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
+                        int page_nr, gpointer user_data)
 {
   cairo_t *cr;
   PrintData *print_data;
@@ -494,10 +868,9 @@ static gboolean claws_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
   PangoLayoutIter *iter;
   double start_pos;
   gboolean notlast = TRUE;
+
   print_data = (PrintData*) user_data;
 
-  if (print_data->print_started == FALSE)
-    cb_begin_print(op, context, print_data);
   if (page_nr == 0) {
     start = 0;
   } else {
@@ -512,6 +885,7 @@ static gboolean claws_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
     end = GPOINTER_TO_INT(pagebreak->data);
 
   cr = gtk_print_context_get_cairo_context(context);
+  cairo_scale(cr, print_data->zoom, print_data->zoom);
   cairo_set_source_rgb(cr, 0., 0., 0.);
 
   ii = 0;
@@ -545,7 +919,6 @@ static gboolean claws_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
                ((double)baseline) / PANGO_SCALE - start_pos);
          cairo_paint (cr);
          cairo_surface_destroy (surface);
-         g_object_unref(GDK_PIXBUF(attr->data));
       }        else {
         pango_cairo_show_layout_line(cr, line);
       }
@@ -553,13 +926,6 @@ static gboolean claws_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
     ii++;
   } while(ii < end && (notlast = pango_layout_iter_next_line(iter)));
   pango_layout_iter_free(iter);
-  return TRUE;
-}
-
-static void cb_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
-                        int page_nr, gpointer user_data)
-{
-  claws_draw_page(op, context, page_nr, user_data);
   debug_print("Sent page %d to printer\n", page_nr+1);
 }
 
@@ -642,6 +1008,8 @@ static void printing_layout_set_text_attributes(PrintData *print_data, GtkPrintC
                open_attrs = g_slist_delete_link(open_attrs, attr_walk);
                break;
              }
+              if (color)
+                gdk_color_free(color);
            }
          }
          if(!found)
@@ -662,6 +1030,8 @@ static void printing_layout_set_text_attributes(PrintData *print_data, GtkPrintC
                open_attrs = g_slist_delete_link(open_attrs, attr_walk);
                break;
              }
+              if (color)
+                gdk_color_free(color);
            }
          }
          if(!found)
@@ -791,4 +1161,17 @@ static gint printing_text_iter_get_offset_bytes(PrintData *print_data, const Gtk
   return off_bytes;
 }
 
+static void printing_preview_update_zoom_sensitivity(PreviewData *preview_data)
+{
+  if((preview_data->print_data->zoom * PREVIEW_ZOOM_FAC) > PREVIEW_ZOOM_MAX)
+    gtk_widget_set_sensitive(preview_data->zoom_in, FALSE);
+  else
+    gtk_widget_set_sensitive(preview_data->zoom_in, TRUE);
+
+  if((preview_data->print_data->zoom / PREVIEW_ZOOM_FAC) < PREVIEW_ZOOM_MIN)
+    gtk_widget_set_sensitive(preview_data->zoom_out, FALSE);
+  else
+    gtk_widget_set_sensitive(preview_data->zoom_out, TRUE);
+}
+
 #endif /* GTK+ >= 2.10.0 */