]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/print.cpp
wxDC::GetHandle support for wxMSW
[wxWidgets.git] / src / gtk / print.cpp
index 32b2c08b1eba236af1181859a587bb0a8ee1af45..6ead61516852ce7da46708f6daf03e925dabf18d 100644 (file)
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        wx/gtk/print.cpp
+// Name:        src/gtk/print.cpp
 // Author:      Anthony Bretaudeau
 // Purpose:     GTK printing support
 // Created:     2007-08-25
 #endif
 
 #include "wx/fontutil.h"
-#include "wx/gtk/private.h"
 #include "wx/dynlib.h"
 #include "wx/paper.h"
 
 #include <gtk/gtk.h>
 
+#if GTK_CHECK_VERSION(2,14,0)
+#include <gtk/gtkunixprint.h>
+#else
+#include <gtk/gtkpagesetupunixdialog.h>
+#endif
+
+
 #if wxUSE_GRAPHICS_CONTEXT
 #include "wx/graphics.h"
 #endif
@@ -48,7 +54,9 @@ wxFORCE_LINK_THIS_MODULE(gtk_print)
 #include "wx/gtk/gnome/gprint.h"
 #endif
 
-// Usefull to convert angles from/to Rad to/from Deg.
+#include "wx/gtk/private/object.h"
+
+// Useful to convert angles from/to Rad to/from Deg.
 static const double RAD2DEG  = 180.0 / M_PI;
 static const double DEG2RAD  = M_PI / 180.0;
 
@@ -66,7 +74,7 @@ public:
     {
 #if wxUSE_LIBGNOMEPRINT
         // This module must be initialized AFTER gnomeprint's one
-        AddDependency(CLASSINFO(wxGnomePrintModule));
+        AddDependency(wxCLASSINFO(wxGnomePrintModule));
 #endif
     }
     bool OnInit();
@@ -78,8 +86,12 @@ private:
 
 bool wxGtkPrintModule::OnInit()
 {
+#ifndef __WXGTK3__
     if (gtk_check_version(2,10,0) == NULL)
+#endif
+    {
         wxPrintFactory::SetPrintFactory( new wxGtkPrintFactory );
+    }
     return true;
 }
 
@@ -222,11 +234,14 @@ IMPLEMENT_CLASS(wxGtkPrintNativeData, wxPrintNativeDataBase)
 wxGtkPrintNativeData::wxGtkPrintNativeData()
 {
     m_config = gtk_print_settings_new();
+    m_job = gtk_print_operation_new();
+    m_context = NULL;
 }
 
 wxGtkPrintNativeData::~wxGtkPrintNativeData()
 {
-    g_object_unref (m_config);
+    g_object_unref(m_job);
+    g_object_unref(m_config);
 }
 
 // Convert datas stored in m_config to a wxPrintData.
@@ -386,6 +401,9 @@ bool wxGtkPrintNativeData::TransferTo( wxPrintData &data )
         data.SetPaperId( wxPAPER_FANFOLD_LGL_GERMAN);
     else
         data.SetPaperId(wxPAPER_NONE);
+
+    data.SetPrinterName(gtk_print_settings_get_printer(m_config));
+
     return true;
 }
 
@@ -534,6 +552,8 @@ bool wxGtkPrintNativeData::TransferFrom( const wxPrintData &data )
         default:                break;
     }
 
+    gtk_print_settings_set_printer(m_config, data.GetPrinterName().utf8_str());
+
     return true;
 }
 
@@ -603,8 +623,6 @@ wxGtkPrintDialog::~wxGtkPrintDialog()
 // This is called even if we actually don't want the dialog to appear.
 int wxGtkPrintDialog::ShowModal()
 {
-    GtkPrintOperationResult response;
-
     // We need to restore the settings given in the constructor.
     wxPrintData data = m_printDialogData.GetPrintData();
     wxGtkPrintNativeData *native =
@@ -629,17 +647,26 @@ int wxGtkPrintDialog::ShowModal()
         gtk_print_settings_set_page_ranges (settings, range, 1);
     }
 
+    GtkPrintOperation * const printOp = native->GetPrintJob();
+
     // If the settings are OK, we restore it.
     if (settings != NULL)
-        gtk_print_operation_set_print_settings (native->GetPrintJob(), settings);
-    gtk_print_operation_set_default_page_setup (native->GetPrintJob(), native->GetPageSetupFromSettings(settings));
+        gtk_print_operation_set_print_settings (printOp, settings);
+    gtk_print_operation_set_default_page_setup (printOp, native->GetPageSetupFromSettings(settings));
 
     // Show the dialog if needed.
     GError* gError = NULL;
-    if (GetShowDialog())
-        response = gtk_print_operation_run (native->GetPrintJob(), GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW(gtk_widget_get_toplevel(m_parent->m_widget) ), &gError);
-    else
-        response = gtk_print_operation_run (native->GetPrintJob(), GTK_PRINT_OPERATION_ACTION_PRINT, GTK_WINDOW(gtk_widget_get_toplevel(m_parent->m_widget)), &gError);
+    GtkPrintOperationResult response = gtk_print_operation_run
+                                       (
+                                           printOp,
+                                           GetShowDialog()
+                                            ? GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
+                                            : GTK_PRINT_OPERATION_ACTION_PRINT,
+                                           m_parent
+                                            ? GTK_WINDOW(gtk_widget_get_toplevel(m_parent->m_widget))
+                                            : NULL,
+                                           &gError
+                                       );
 
     // Does everything went well?
     if (response == GTK_PRINT_OPERATION_RESULT_CANCEL)
@@ -648,16 +675,22 @@ int wxGtkPrintDialog::ShowModal()
     }
     else if (response == GTK_PRINT_OPERATION_RESULT_ERROR)
     {
+        wxLogError(_("Error while printing: ") + wxString(gError ? gError->message : "???"));
         g_error_free (gError);
-        wxLogError(_("Error while printing: ") + wxString::Format(_("%s"), gError->message));
         return wxID_NO; // We use wxID_NO because there is no wxID_ERROR available
     }
 
     // Now get the settings and save it.
-    GtkPrintSettings* newSettings = gtk_print_operation_get_print_settings (native->GetPrintJob());
+    GtkPrintSettings* newSettings = gtk_print_operation_get_print_settings(printOp);
     native->SetPrintConfig(newSettings);
     data.ConvertFromNative();
 
+    // Set PrintDialogData variables
+    m_printDialogData.SetPrintData(data);
+    m_printDialogData.SetCollate(data.GetCollate());
+    m_printDialogData.SetNoCopies(data.GetNoCopies());
+    m_printDialogData.SetPrintToFile(data.GetPrinterName() == "Print to File");
+
     // Same problem as a few lines before.
     switch (gtk_print_settings_get_print_pages(newSettings))
     {
@@ -735,43 +768,82 @@ int wxGtkPageSetupDialog::ShowModal()
         }
     }
 
-    // Now show the dialog.
-    GtkPageSetup* newPageSetup = gtk_print_run_page_setup_dialog (GTK_WINDOW(m_parent->m_widget),
-                                                           oldPageSetup,
-                                                           nativeData);
 
-    int ret;
-    if (newPageSetup != oldPageSetup)
-    {
-        native->SetPageSetupToSettings(nativeData, newPageSetup);
-        m_pageDialogData.GetPrintData().ConvertFromNative();
+    // Set selected printer
+    gtk_print_settings_set(nativeData, "format-for-printer",
+                           gtk_print_settings_get_printer(nativeData));
 
-        // Store custom paper format if any.
-        if (m_pageDialogData.GetPrintData().GetPaperId() == wxPAPER_NONE)
-        {
-            gdouble ml,mr,mt,mb,pw,ph;
-            ml = gtk_page_setup_get_left_margin (newPageSetup, GTK_UNIT_MM);
-            mr = gtk_page_setup_get_right_margin (newPageSetup, GTK_UNIT_MM);
-            mt = gtk_page_setup_get_top_margin (newPageSetup, GTK_UNIT_MM);
-            mb = gtk_page_setup_get_bottom_margin (newPageSetup, GTK_UNIT_MM);
+    // Create custom dialog
+    wxString title(GetTitle());
+    if ( title.empty() )
+        title = _("Page Setup");
+    GtkWidget *
+        dlg = gtk_page_setup_unix_dialog_new(title.utf8_str(),
+                                             m_parent
+                                                ? GTK_WINDOW(m_parent->m_widget)
+                                                : NULL);
 
-            pw = gtk_page_setup_get_paper_width (newPageSetup, GTK_UNIT_MM);
-            ph = gtk_page_setup_get_paper_height (newPageSetup, GTK_UNIT_MM);
+    gtk_page_setup_unix_dialog_set_print_settings(
+        GTK_PAGE_SETUP_UNIX_DIALOG(dlg), nativeData);
+    gtk_page_setup_unix_dialog_set_page_setup(
+        GTK_PAGE_SETUP_UNIX_DIALOG(dlg), oldPageSetup);
 
-            m_pageDialogData.SetMarginTopLeft( wxPoint( (int)(ml+0.5), (int)(mt+0.5)) );
-            m_pageDialogData.SetMarginBottomRight( wxPoint( (int)(mr+0.5), (int)(mb+0.5)) );
+    int result = gtk_dialog_run(GTK_DIALOG(dlg));
+    gtk_widget_hide(dlg);
 
-            m_pageDialogData.SetPaperSize( wxSize( (int)(pw+0.5), (int)(ph+0.5) ) );
-        }
-
-        ret = wxID_OK;
-    }
-    else
+    switch ( result )
     {
-        ret = wxID_CANCEL;
+        case GTK_RESPONSE_OK:
+        case GTK_RESPONSE_APPLY:
+            {
+                // Store Selected Printer Name
+                gtk_print_settings_set_printer
+                (
+                    nativeData,
+                    gtk_print_settings_get(nativeData, "format-for-printer")
+                );
+
+                wxGtkObject<GtkPageSetup>
+                    newPageSetup(gtk_page_setup_unix_dialog_get_page_setup(
+                                        GTK_PAGE_SETUP_UNIX_DIALOG(dlg)));
+                native->SetPageSetupToSettings(nativeData, newPageSetup);
+
+                m_pageDialogData.GetPrintData().ConvertFromNative();
+
+                // Store custom paper format if any.
+                if ( m_pageDialogData.GetPrintData().GetPaperId() == wxPAPER_NONE )
+                {
+                    gdouble ml,mr,mt,mb,pw,ph;
+                    ml = gtk_page_setup_get_left_margin (newPageSetup, GTK_UNIT_MM);
+                    mr = gtk_page_setup_get_right_margin (newPageSetup, GTK_UNIT_MM);
+                    mt = gtk_page_setup_get_top_margin (newPageSetup, GTK_UNIT_MM);
+                    mb = gtk_page_setup_get_bottom_margin (newPageSetup, GTK_UNIT_MM);
+
+                    pw = gtk_page_setup_get_paper_width (newPageSetup, GTK_UNIT_MM);
+                    ph = gtk_page_setup_get_paper_height (newPageSetup, GTK_UNIT_MM);
+
+                    m_pageDialogData.SetMarginTopLeft(wxPoint((int)(ml+0.5),
+                                                              (int)(mt+0.5)));
+                    m_pageDialogData.SetMarginBottomRight(wxPoint((int)(mr+0.5),
+                                                                  (int)(mb+0.5)));
+
+                    m_pageDialogData.SetPaperSize(wxSize((int)(pw+0.5),
+                                                         (int)(ph+0.5)));
+                }
+
+                result = wxID_OK;
+            }
+            break;
+
+        default:
+        case GTK_RESPONSE_CANCEL:
+            result = wxID_CANCEL;
+            break;
     }
 
-    return ret;
+    gtk_widget_destroy(dlg);
+
+    return result;
 }
 
 //----------------------------------------------------------------------------
@@ -832,9 +904,7 @@ bool wxGtkPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt )
     wxPrintData printdata = GetPrintDialogData().GetPrintData();
     wxGtkPrintNativeData *native = (wxGtkPrintNativeData*) printdata.GetNativeData();
 
-    GtkPrintOperation *printOp = gtk_print_operation_new ();
-
-    native->SetPrintJob( printOp );
+    GtkPrintOperation * const printOp = native->GetPrintJob();
 
     wxPrinterToGtkData dataToSend;
     dataToSend.printer = this;
@@ -863,8 +933,6 @@ bool wxGtkPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt )
         wxFAIL_MSG(_("The print dialog returned an error."));
     }
 
-    g_object_unref (printOp);
-
     return (sm_lastError == wxPRINTER_NO_ERROR);
 }
 
@@ -1266,7 +1334,8 @@ bool wxGtkPrinterDCImpl::DoGetPixel(wxCoord WXUNUSED(x1),
 
 void wxGtkPrinterDCImpl::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
 {
-    if  (m_pen.GetStyle() == wxPENSTYLE_TRANSPARENT) return;
+    if ( m_pen.IsTransparent() )
+        return;
 
     SetPen( m_pen );
     cairo_move_to ( m_cairo, XLOG2DEV(x1), YLOG2DEV(y1) );
@@ -1374,7 +1443,8 @@ void wxGtkPrinterDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord
 
 void wxGtkPrinterDCImpl::DoDrawPoint(wxCoord x, wxCoord y)
 {
-    if  (m_pen.GetStyle() == wxPENSTYLE_TRANSPARENT) return;
+    if ( m_pen.IsTransparent() )
+        return;
 
     SetPen( m_pen );
 
@@ -1387,7 +1457,9 @@ void wxGtkPrinterDCImpl::DoDrawPoint(wxCoord x, wxCoord y)
 
 void wxGtkPrinterDCImpl::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
 {
-    if (m_pen.GetStyle() == wxPENSTYLE_TRANSPARENT) return;
+    if ( m_pen.IsTransparent() )
+        return;
+
 
     if (n <= 0) return;
 
@@ -1637,9 +1709,11 @@ void wxGtkPrinterDCImpl::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoor
     y = wxCoord(YLOG2DEV(y));
     int bw = bitmap.GetWidth();
     int bh = bitmap.GetHeight();
+#ifndef __WXGTK3__
     wxBitmap bmpSource = bitmap;  // we need a non-const instance.
     if (!useMask && !bitmap.HasPixbuf() && bitmap.GetMask())
         bmpSource.SetMask(NULL);
+#endif
 
     cairo_save(m_cairo);
 
@@ -1651,12 +1725,16 @@ void wxGtkPrinterDCImpl::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoor
     wxDouble scaleY = (wxDouble) YLOG2DEVREL(bh) / (wxDouble) bh;
     cairo_scale(m_cairo, scaleX, scaleY);
 
+#ifdef __WXGTK3__
+    bitmap.Draw(m_cairo, 0, 0, useMask, &m_textForegroundColour, &m_textBackgroundColour);
+#else
     gdk_cairo_set_source_pixbuf(m_cairo, bmpSource.GetPixbuf(), 0, 0);
     cairo_pattern_set_filter(cairo_get_source(m_cairo), CAIRO_FILTER_NEAREST);
     // Use the original size here since the context is scaled already.
     cairo_rectangle(m_cairo, 0, 0, bw, bh);
     // Fill the rectangle using the pattern.
     cairo_fill(m_cairo);
+#endif
 
     CalcBoundingBox(0,0);
     CalcBoundingBox(bw,bh);
@@ -1676,25 +1754,12 @@ void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString& text, wxCoord x, wxCo
 
     angle = -angle;
 
-    bool underlined = m_font.Ok() && m_font.GetUnderlined();
-
     const wxScopedCharBuffer data = text.utf8_str();
 
-    size_t datalen = strlen(data);
-    pango_layout_set_text( m_layout, data, datalen);
+    pango_layout_set_text(m_layout, data, data.length());
 
-    if (underlined)
-    {
-        PangoAttrList *attrs = pango_attr_list_new();
-        PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
-        a->start_index = 0;
-        a->end_index = datalen;
-        pango_attr_list_insert(attrs, a);
-        pango_layout_set_attributes(m_layout, attrs);
-        pango_attr_list_unref(attrs);
-    }
-
-    if (m_textForegroundColour.Ok())
+    const bool setAttrs = m_font.GTKSetPangoAttrs(m_layout);
+    if (m_textForegroundColour.IsOk())
     {
         unsigned char red = m_textForegroundColour.Red();
         unsigned char blue = m_textForegroundColour.Blue();
@@ -1754,7 +1819,7 @@ void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString& text, wxCoord x, wxCo
 
     cairo_restore( m_cairo );
 
-    if (underlined)
+    if (setAttrs)
     {
         // Undo underline attributes setting
         pango_layout_set_attributes(m_layout, NULL);
@@ -1782,7 +1847,7 @@ void wxGtkPrinterDCImpl::SetFont( const wxFont& font )
 {
     m_font = font;
 
-    if (m_font.Ok())
+    if (m_font.IsOk())
     {
         if (m_fontdesc)
             pango_font_description_free( m_fontdesc );
@@ -1799,7 +1864,7 @@ void wxGtkPrinterDCImpl::SetFont( const wxFont& font )
 
 void wxGtkPrinterDCImpl::SetPen( const wxPen& pen )
 {
-    if (!pen.Ok()) return;
+    if (!pen.IsOk()) return;
 
     m_pen = pen;
 
@@ -1878,7 +1943,7 @@ void wxGtkPrinterDCImpl::SetPen( const wxPen& pen )
 
 void wxGtkPrinterDCImpl::SetBrush( const wxBrush& brush )
 {
-    if (!brush.Ok()) return;
+    if (!brush.IsOk()) return;
 
     m_brush = brush;
 
@@ -2087,8 +2152,8 @@ void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString& string, wxCoord *width,
     {
         // scale the font and apply it
         PangoFontDescription *desc = theFont->GetNativeFontInfo()->description;
-        float size = pango_font_description_get_size(desc);
-        size = size * GetFontPointSizeAdjustment(72.0);
+        oldSize = pango_font_description_get_size(desc);
+        const float size = oldSize * GetFontPointSizeAdjustment(72.0);
         pango_font_description_set_size(desc, (gint)size);
 
         pango_layout_set_font_description(m_layout, desc);
@@ -2151,7 +2216,7 @@ void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData& data)
     m_printData = data;
 }
 
-// overriden for wxPrinterDC Impl
+// overridden for wxPrinterDC Impl
 
 wxRect wxGtkPrinterDCImpl::GetPaperRect() const
 {
@@ -2252,8 +2317,14 @@ void wxGtkPrintPreview::DetermineScaling()
 
     if (paper)
     {
-        m_previewPrintout->SetPPIScreen(wxGetDisplayPPI());
-        m_previewPrintout->SetPPIPrinter( m_resolution, m_resolution );
+        const wxSize screenPPI = wxGetDisplayPPI();
+        int logPPIScreenX = screenPPI.GetWidth();
+        int logPPIScreenY = screenPPI.GetHeight();
+        int logPPIPrinterX = m_resolution;
+        int logPPIPrinterY = m_resolution;
+
+        m_previewPrintout->SetPPIScreen( logPPIScreenX, logPPIScreenY );
+        m_previewPrintout->SetPPIPrinter( logPPIPrinterX, logPPIPrinterY );
 
         // Get width and height in points (1/72th of an inch)
         wxSize sizeDevUnits(paper->GetSizeDeviceUnits());
@@ -2280,8 +2351,8 @@ void wxGtkPrintPreview::DetermineScaling()
         m_previewPrintout->SetPaperRectPixels(wxRect(0, 0, m_pageWidth, m_pageHeight));
 
         // At 100%, the page should look about page-size on the screen.
-        m_previewScaleX = 0.8 * 72.0 / (double)m_resolution;
-        m_previewScaleY = m_previewScaleX;
+        m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX;
+        m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY;
     }
 }