X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1f0c8f31f407ecfce909060464c0ea655221cdab..64ea838d8f4d1853b7d850db93ee565e901d099a:/src/osx/carbon/dcprint.cpp diff --git a/src/osx/carbon/dcprint.cpp b/src/osx/carbon/dcprint.cpp index 7bec2b5eaf..e1927125bc 100644 --- a/src/osx/carbon/dcprint.cpp +++ b/src/osx/carbon/dcprint.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: src/mac/carbon/dcprint.cpp +// Name: src/osx/carbon/dcprint.cpp // Purpose: wxPrinterDC class // Author: Julian Smart // Modified by: @@ -25,9 +25,9 @@ #include "wx/math.h" #endif -#include "wx/osx/uma.h" +#include "wx/osx/private.h" #include "wx/osx/private/print.h" -#include "wx/osx/carbon/dcprint.h" +#include "wx/osx/dcprint.h" #include "wx/graphics.h" IMPLEMENT_ABSTRACT_CLASS(wxPrinterDCImpl, wxGCDCImpl) @@ -46,7 +46,7 @@ public : // returns 0 in case of no Error, otherwise platform specific error codes virtual wxUint32 GetStatus() const = 0 ; - bool Ok() { return GetStatus() == 0 ; } + bool IsOk() { return GetStatus() == 0 ; } static wxNativePrinterDC* Create(wxPrintData* data) ; } ; @@ -73,10 +73,10 @@ private : wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data ) { m_err = noErr ; - wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ; + wxOSXPrintData *native = (wxOSXPrintData*) data->GetNativeData() ; PMRect rPage; - m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); + m_err = PMGetAdjustedPageRect(native->GetPageFormat(), &rPage); if ( m_err != noErr ) return; @@ -84,30 +84,25 @@ wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data ) m_maxY = wxCoord(rPage.bottom - rPage.top); PMResolution res; - -#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 - if ( PMPrinterGetOutputResolution != NULL ) + PMPrinter printer; + m_err = PMSessionGetCurrentPrinter(native->GetPrintSession(), &printer); + if ( m_err == noErr ) { - PMPrinter printer; - m_err = PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer); - if ( m_err == noErr ) + m_err = PMPrinterGetOutputResolution( printer, native->GetPrintSettings(), &res) ; + if ( m_err == -9589 /* kPMKeyNotFound */ ) { - m_err = PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ; - if ( m_err == -9589 /* kPMKeyNotFound */ ) - { - m_err = noErr ; - res.hRes = res.vRes = 300; - } + m_err = noErr ; + res.hRes = res.vRes = 300; } } else -#endif { -#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 - m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res); -#endif + res.hRes = res.vRes = 300; } - + + m_maxX = wxCoord((double)m_maxX * res.hRes / 72.0); + m_maxY = wxCoord((double)m_maxY * res.vRes / 72.0); + m_ppi = wxSize(int(res.hRes), int(res.vRes)); } @@ -120,22 +115,24 @@ wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data) return new wxMacCarbonPrinterDC(data) ; } -bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) ) +bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& message ) { if ( m_err ) return false ; wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); - wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ; + wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ; + + PMPrintSettingsSetJobName(native->GetPrintSettings(), wxCFStringRef(message)); - m_err = PMSessionBeginCGDocumentNoDialog(native->m_macPrintSession, - native->m_macPrintSettings, - native->m_macPageFormat); + m_err = PMSessionBeginCGDocumentNoDialog(native->GetPrintSession(), + native->GetPrintSettings(), + native->GetPageFormat()); if ( m_err != noErr ) return false; PMRect rPage; - m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); + m_err = PMGetAdjustedPageRect(native->GetPageFormat(), &rPage); if ( m_err != noErr ) return false ; @@ -143,28 +140,21 @@ bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED m_maxY = wxCoord(rPage.bottom - rPage.top); PMResolution res; -#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 - if ( PMPrinterGetOutputResolution != NULL ) + PMPrinter printer; + + m_err = PMSessionGetCurrentPrinter(native->GetPrintSession(), &printer); + if (m_err == noErr) { - PMPrinter printer; - m_err = PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer); - if ( m_err == noErr ) + m_err = PMPrinterGetOutputResolution( printer, native->GetPrintSettings(), &res) ; + if ( m_err == -9589 /* kPMKeyNotFound */ ) { - m_err = PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ; - if ( m_err == -9589 /* kPMKeyNotFound */ ) - { - m_err = noErr ; - res.hRes = res.vRes = 300; - } + m_err = noErr ; + res.hRes = res.vRes = 300; } } - else -#endif - { -#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 - m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res); -#endif - } + + m_maxX = wxCoord((double)m_maxX * res.hRes / 72.0); + m_maxY = wxCoord((double)m_maxY * res.vRes / 72.0); m_ppi = wxSize(int(res.hRes), int(res.vRes)); return true ; @@ -176,9 +166,9 @@ void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc ) return ; wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); - wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ; + wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ; - m_err = PMSessionEndDocumentNoDialog(native->m_macPrintSession); + m_err = PMSessionEndDocumentNoDialog(native->GetPrintSession()); } void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc ) @@ -187,43 +177,45 @@ void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc ) return ; wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); - wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ; + wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ; - m_err = PMSessionBeginPageNoDialog(native->m_macPrintSession, - native->m_macPageFormat, - nil); + m_err = PMSessionBeginPageNoDialog(native->GetPrintSession(), + native->GetPageFormat(), + NULL); CGContextRef pageContext; if ( m_err == noErr ) { - m_err = PMSessionGetCGGraphicsContext(native->m_macPrintSession, + m_err = PMSessionGetCGGraphicsContext(native->GetPrintSession(), &pageContext ); } if ( m_err != noErr ) { - PMSessionEndPageNoDialog(native->m_macPrintSession); - PMSessionEndDocumentNoDialog(native->m_macPrintSession); + PMSessionEndPageNoDialog(native->GetPrintSession()); + PMSessionEndDocumentNoDialog(native->GetPrintSession()); } else { - PMRect rPage; - - m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); + PMRect paperRect ; + m_err = PMGetAdjustedPaperRect( native->GetPageFormat() , &paperRect ) ; + // make sure (0,0) is at the upper left of the printable area (wx conventions) + // Core Graphics initially has the lower left of the paper as 0,0 if ( !m_err ) - { - PMRect paperRect ; - PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ; - // make sure (0,0) is at the upper left of the printable area (wx conventions) - // Core Graphics initially has the lower left of the paper as 0,0 CGContextTranslateCTM( pageContext , (CGFloat) -paperRect.left , (CGFloat) paperRect.bottom ) ; - CGContextScaleCTM( pageContext , 1 , -1 ) ; - } + // since this is a non-critical error, we set the flag back m_err = noErr ; + + // Leopard deprecated PMSetResolution() which will not be available in 64 bit mode, so we avoid using it. + // To set the proper drawing resolution, the docs suggest the use of CGContextScaleCTM(), so here we go; as a + // consequence though, PMGetAdjustedPaperRect() and PMGetAdjustedPageRect() return unscaled rects, so we + // have to manually scale them later. + CGContextScaleCTM( pageContext, 72.0 / (double)m_ppi.x, -72.0 / (double)m_ppi.y); + + impl->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext ) ); } - impl->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext ) ); } void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc ) @@ -232,12 +224,12 @@ void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc ) return ; wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); - wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ; + wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ; - m_err = PMSessionEndPageNoDialog(native->m_macPrintSession); + m_err = PMSessionEndPageNoDialog(native->GetPrintSession()); if ( m_err != noErr ) { - PMSessionEndDocumentNoDialog(native->m_macPrintSession); + PMSessionEndDocumentNoDialog(native->GetPrintSession()); } // the cg context we got when starting the page isn't valid anymore, so replace it impl->SetGraphicsContext( wxGraphicsContext::Create() ); @@ -251,7 +243,7 @@ void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const *h = m_maxY ; } -wxSize wxMacCarbonPrinterDC::GetPPI() const +wxSize wxMacCarbonPrinterDC::GetPPI() const { return m_ppi ; }; @@ -269,7 +261,7 @@ wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC *owner, const wxPrintData& printda m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ; if ( m_nativePrinterDC ) { - m_ok = m_nativePrinterDC->Ok() ; + m_ok = m_nativePrinterDC->IsOk() ; if ( !m_ok ) { wxString message ; @@ -281,7 +273,7 @@ wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC *owner, const wxPrintData& printda { wxSize sz = GetPPI(); m_mm_to_pix_x = mm2inches * sz.x; - m_mm_to_pix_y = mm2inches * sz.y; + m_mm_to_pix_y = mm2inches * sz.y; } // we need at least a measuring context because people start measuring before a page // gets printed at all @@ -310,7 +302,7 @@ bool wxPrinterDCImpl::StartDoc( const wxString& message ) { // in case we have to do additional things when successful } - m_ok = m_nativePrinterDC->Ok() ; + m_ok = m_nativePrinterDC->IsOk() ; if ( !m_ok ) { wxString message ; @@ -328,7 +320,7 @@ void wxPrinterDCImpl::EndDoc(void) return ; m_nativePrinterDC->EndDoc( (wxPrinterDC*) GetOwner() ) ; - m_ok = m_nativePrinterDC->Ok() ; + m_ok = m_nativePrinterDC->IsOk() ; if ( !m_ok ) { @@ -339,17 +331,24 @@ void wxPrinterDCImpl::EndDoc(void) } } -wxRect wxPrinterDCImpl::GetPaperRect() +wxRect wxPrinterDCImpl::GetPaperRect() const { wxCoord w, h; GetOwner()->GetSize(&w, &h); wxRect pageRect(0, 0, w, h); - wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) m_printData.GetNativeData() ; + wxOSXPrintData *native = (wxOSXPrintData*) m_printData.GetNativeData() ; OSStatus err = noErr ; PMRect rPaper; - err = PMGetAdjustedPaperRect(native->m_macPageFormat, &rPaper); + err = PMGetAdjustedPaperRect(native->GetPageFormat(), &rPaper); if ( err != noErr ) return pageRect; + + wxSize ppi = GetOwner()->GetPPI(); + rPaper.right *= (ppi.x / 72.0); + rPaper.bottom *= (ppi.y / 72.0); + rPaper.left *= (ppi.x / 72.0); + rPaper.top *= (ppi.y / 72.0); + return wxRect(wxCoord(rPaper.left), wxCoord(rPaper.top), wxCoord(rPaper.right - rPaper.left), wxCoord(rPaper.bottom - rPaper.top)); } @@ -371,7 +370,7 @@ void wxPrinterDCImpl::StartPage() m_backgroundBrush = *wxWHITE_BRUSH; m_nativePrinterDC->StartPage( (wxPrinterDC*) GetOwner() ) ; - m_ok = m_nativePrinterDC->Ok() ; + m_ok = m_nativePrinterDC->IsOk() ; } @@ -381,12 +380,12 @@ void wxPrinterDCImpl::EndPage() return ; m_nativePrinterDC->EndPage( (wxPrinterDC*) GetOwner() ); - m_ok = m_nativePrinterDC->Ok() ; + m_ok = m_nativePrinterDC->IsOk() ; } void wxPrinterDCImpl::DoGetSize(int *width, int *height) const { - wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") ); + wxCHECK_RET( m_ok , wxT("GetSize() doesn't work without a valid wxPrinterDC") ); m_nativePrinterDC->GetSize(width, height ) ; }