+ }
+
+ m_ppi = wxSize(int(res.hRes), int(res.vRes));
+}
+
+wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
+{
+}
+
+wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
+{
+ return new wxMacCarbonPrinterDC(data) ;
+}
+
+bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
+{
+ if ( m_err )
+ return false ;
+
+ wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
+ wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ;
+
+ m_err = PMSessionBeginCGDocumentNoDialog(native->m_macPrintSession,
+ native->m_macPrintSettings,
+ native->m_macPageFormat);
+ if ( m_err != noErr )
+ return false;
+
+ PMRect rPage;
+ m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
+ if ( m_err != noErr )
+ return false ;
+
+ m_maxX = wxCoord(rPage.right - rPage.left) ;
+ 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->m_macPrintSession, &printer);
+ if ( m_err == noErr )
+ {
+ m_err = PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ;
+ if ( m_err == -9589 /* kPMKeyNotFound */ )
+ {
+ 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_ppi = wxSize(int(res.hRes), int(res.vRes));
+ return true ;
+}
+
+void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
+{
+ if ( m_err )
+ return ;
+
+ wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
+ wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ;
+
+ m_err = PMSessionEndDocumentNoDialog(native->m_macPrintSession);
+}
+
+void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
+{
+ if ( m_err )
+ return ;
+
+ wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
+ wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ;
+
+ m_err = PMSessionBeginPageNoDialog(native->m_macPrintSession,
+ native->m_macPageFormat,
+ nil);
+
+ CGContextRef pageContext;
+
+ if ( m_err == noErr )
+ {
+ m_err = PMSessionGetCGGraphicsContext(native->m_macPrintSession,
+ &pageContext );
+ }
+
+ if ( m_err != noErr )
+ {
+ PMSessionEndPageNoDialog(native->m_macPrintSession);
+ PMSessionEndDocumentNoDialog(native->m_macPrintSession);
+ }
+ else
+ {
+ PMRect rPage;
+
+ m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
+ 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 ;
+ }
+ impl->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext ) );
+}
+
+void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
+{
+ if ( m_err )
+ return ;
+
+ wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
+ wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ;
+
+ m_err = PMSessionEndPageNoDialog(native->m_macPrintSession);
+ if ( m_err != noErr )
+ {
+ PMSessionEndDocumentNoDialog(native->m_macPrintSession);
+ }
+ // the cg context we got when starting the page isn't valid anymore, so replace it
+ impl->SetGraphicsContext( wxGraphicsContext::Create() );
+}
+
+void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const
+{
+ if ( w )
+ *w = m_maxX ;
+ if ( h )
+ *h = m_maxY ;
+}
+
+wxSize wxMacCarbonPrinterDC::GetPPI() const
+{
+ return m_ppi ;
+};
+
+//
+//
+//
+
+wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC *owner, const wxPrintData& printdata )
+ : wxGCDCImpl( owner )
+{
+ m_ok = false ;
+ m_printData = printdata ;
+ m_printData.ConvertToNative() ;
+ m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
+ if ( m_nativePrinterDC )
+ {
+ m_ok = m_nativePrinterDC->Ok() ;
+ if ( !m_ok )
+ {
+ wxString message ;
+ message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
+ wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
+ dialog.ShowModal();
+ }
+ else
+ {
+ wxSize sz = GetPPI();
+ m_mm_to_pix_x = mm2inches * sz.x;
+ 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
+ SetGraphicsContext( wxGraphicsContext::Create() );
+ }
+}
+
+wxSize wxPrinterDCImpl::GetPPI() const
+{
+ return m_nativePrinterDC->GetPPI() ;
+}
+
+wxPrinterDCImpl::~wxPrinterDCImpl()
+{
+ delete m_nativePrinterDC ;
+}
+
+bool wxPrinterDCImpl::StartDoc( const wxString& message )
+{
+ wxASSERT_MSG( IsOk() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
+
+ if ( !m_ok )
+ return false ;
+
+ if ( m_nativePrinterDC->StartDoc( (wxPrinterDC*) GetOwner(), message ) )
+ {
+ // in case we have to do additional things when successful
+ }
+ m_ok = m_nativePrinterDC->Ok() ;
+ if ( !m_ok )
+ {
+ wxString message ;
+ message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
+ wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
+ dialog.ShowModal();
+ }