Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / ui / dialog / print.cpp
index 890e0061c7f68c5b8b7bb4b81c5b537c441fcd28..a56cbfd9d65dd6066d4126c9cd5df143ba52f9dc 100644 (file)
@@ -3,6 +3,7 @@
  */
 /* Authors:
  *   Kees Cook <kees@outflux.net>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2007 Kees Cook
  * Released under GNU GPL.  Read the file 'COPYING' for more information.
@@ -13,6 +14,7 @@
 #endif
 #ifdef WIN32
 #include <io.h>
+#include <windows.h>
 #endif
 
 #include <gtkmm/stock.h>
 
 
 
+static void draw_page(
 #ifdef WIN32
-#include <windows.h>
-#include <commdlg.h>
-#include <cairo.h>
-#include <cairo-win32.h>
-
-
-
-static cairo_surface_t *
-_cairo_win32_printing_surface_create (HDC hdc)
-{
-    int x, y, x_dpi, y_dpi, x_off, y_off, depth;
-    XFORM xform;
-    cairo_surface_t *surface;
-
-    x = GetDeviceCaps (hdc, HORZRES);
-    y = GetDeviceCaps (hdc, VERTRES);
-
-    x_dpi = GetDeviceCaps (hdc, LOGPIXELSX);
-    y_dpi = GetDeviceCaps (hdc, LOGPIXELSY);
-
-    x_off = GetDeviceCaps (hdc, PHYSICALOFFSETX);
-    y_off = GetDeviceCaps (hdc, PHYSICALOFFSETY);
-
-    depth = GetDeviceCaps(hdc, BITSPIXEL);
-
-    SetGraphicsMode (hdc, GM_ADVANCED);
-    xform.eM11 = x_dpi/72.0;
-    xform.eM12 = 0;
-    xform.eM21 = 0;
-    xform.eM22 = y_dpi/72.0;
-    xform.eDx = -x_off;
-    xform.eDy = -y_off;
-    SetWorldTransform (hdc, &xform);
-
-    surface = cairo_win32_printing_surface_create (hdc);
-    
-    /**
-               Read fallback dpi from device capabilities. Was a workaround for a bug patched
-               in cairo 1.5.14. Without this, fallback defaults to 300dpi, which is quite acceptable.
-               Going higher can cause spool size and memory problems.
-       */
-    // cairo_surface_set_fallback_resolution (surface, x_dpi, y_dpi);
-
-    return surface;
-}
+                      GtkPrintOperation *operation,
+#else
+                      GtkPrintOperation *,
 #endif
-
-
-
-static void
-draw_page (GtkPrintOperation */*operation*/,
-           GtkPrintContext   *context,
-           gint               /*page_nr*/,
-           gpointer           user_data)
+                      GtkPrintContext   *context,
+                      gint               /*page_nr*/,
+                      gpointer           user_data)
 {
     struct workaround_gtkmm *junk = (struct workaround_gtkmm*)user_data;
     //printf("%s %d\n",__FUNCTION__, page_nr);
 
     if (junk->_tab->as_bitmap()) {
         // Render as exported PNG
-        gdouble width = sp_document_width(junk->_doc);
-        gdouble height = sp_document_height(junk->_doc);
+        gdouble width = (junk->_doc)->getWidth();
+        gdouble height = (junk->_doc)->getHeight();
         gdouble dpi = junk->_tab->bitmap_dpi();
         std::string tmp_png;
         std::string tmp_base = "inkscape-print-png-XXXXXX";
@@ -154,21 +110,26 @@ draw_page (GtkPrintOperation */*operation*/,
 
         cairo_t *cr = gtk_print_context_get_cairo_context (context);
         cairo_surface_t *surface = cairo_get_target(cr);
-
-
-/**
-       Call cairo_win32_printing_surface directly as a workaround until GTK uses this call.
-       When GTK uses cairo_win32_printing_surface this automatically reverts.
-*/
-#ifdef WIN32
-        if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_WIN32) {
-        HDC dc = cairo_win32_surface_get_dc (surface);
-        surface = _cairo_win32_printing_surface_create (dc);
-        }
-#endif
-
         cairo_matrix_t ctm;
         cairo_get_matrix(cr, &ctm);
+#ifdef WIN32
+        //Gtk+ does not take the non printable area into account
+        //http://bugzilla.gnome.org/show_bug.cgi?id=381371
+        //
+        // This workaround translates the origin from the top left of the
+        // printable area to the top left of the page.
+        GtkPrintSettings *settings = gtk_print_operation_get_print_settings(operation);
+        const gchar *printerName = gtk_print_settings_get_printer(settings);
+        HDC hdc = CreateDC("WINSPOOL", printerName, NULL, NULL);
+        if (hdc) {
+            cairo_matrix_t mat;
+            int x_off = GetDeviceCaps (hdc, PHYSICALOFFSETX);
+            int y_off = GetDeviceCaps (hdc, PHYSICALOFFSETY);
+            cairo_matrix_init_translate(&mat, -x_off, -y_off);
+            cairo_matrix_multiply (&ctm, &ctm, &mat);
+            DeleteDC(hdc);
+        }
+#endif             
         bool ret = ctx->setSurfaceTarget (surface, true, &ctm);
         if (ret) {
             ret = renderer.setupDocument (ctx, junk->_doc, TRUE, NULL);
@@ -221,7 +182,7 @@ Print::Print(SPDocument *doc, SPItem *base) :
     _printop = gtk_print_operation_new ();
 
     // set up dialog title, based on document name
-    gchar *jobname = _doc->name ? _doc->name : _("SVG Document");
+    gchar const *jobname = _doc->getName() ? _doc->getName() : _("SVG Document");
     Glib::ustring title = _("Print");
     title += " ";
     title += jobname;
@@ -230,14 +191,21 @@ Print::Print(SPDocument *doc, SPItem *base) :
     // set up paper size to match the document size
     gtk_print_operation_set_unit (_printop, GTK_UNIT_POINTS);
     GtkPageSetup *page_setup = gtk_page_setup_new();
-    gdouble doc_width = sp_document_width(_doc) * PT_PER_PX;
-    gdouble doc_height = sp_document_height(_doc) * PT_PER_PX;
-    GtkPaperSize *paper_size = gtk_paper_size_new_custom("custom", "custom",
-                                doc_width, doc_height, GTK_UNIT_POINTS);
+    gdouble doc_width = _doc->getWidth() * PT_PER_PX;
+    gdouble doc_height = _doc->getHeight() * PT_PER_PX;
+    GtkPaperSize *paper_size;
+    if (doc_width > doc_height) {
+        gtk_page_setup_set_orientation (page_setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
+        paper_size = gtk_paper_size_new_custom("custom", "custom",
+                                               doc_height, doc_width, GTK_UNIT_POINTS);
+    } else {
+        gtk_page_setup_set_orientation (page_setup, GTK_PAGE_ORIENTATION_PORTRAIT);
+        paper_size = gtk_paper_size_new_custom("custom", "custom",
+                                               doc_width, doc_height, GTK_UNIT_POINTS);
+    }
+
     gtk_page_setup_set_paper_size (page_setup, paper_size);
-#ifndef WIN32
     gtk_print_operation_set_default_page_setup (_printop, page_setup);
-#endif
     gtk_print_operation_set_use_full_page (_printop, TRUE);
 
     // set up signals
@@ -273,4 +241,4 @@ Gtk::PrintOperationResult Print::run(Gtk::PrintOperationAction, Gtk::Window &par
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :