1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxPrinterDC class 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart 
   9 // Licence:       wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) 
  13 #pragma implementation "dcprint.h" 
  16 // For compilers that support precompilation, includes "wx.h". 
  17 #include "wx/wxprec.h" 
  19 #if wxUSE_PRINTING_ARCHITECTURE 
  28 #include "wx/dcprint.h" 
  29 #include "wx/msgdlg.h" 
  31 #include "wx/mac/uma.h" 
  32 #include "wx/mac/private/print.h" 
  34 #if !USE_SHARED_LIBRARY 
  35 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
) 
  38 class wxNativePrinterDC
 
  41     wxNativePrinterDC() {} 
  42     virtual ~wxNativePrinterDC() {} 
  43     virtual bool StartDoc(  wxPrinterDC
* dc 
, const wxString
& message 
) = 0; 
  44     virtual void EndDoc( wxPrinterDC
* dc 
) = 0; 
  45     virtual void StartPage( wxPrinterDC
* dc 
) = 0; 
  46     virtual void EndPage( wxPrinterDC
* dc 
) = 0; 
  47     virtual wxCoord 
GetMaxX() const = 0 ; 
  48     virtual wxCoord 
GetMaxY() const = 0 ; 
  49     // returns 0 in case of no Error, otherwise platform specific error codes 
  50     virtual wxUint32 
GetStatus() const = 0 ; 
  51     bool Ok() { return GetStatus() == 0 ; } 
  53     static wxNativePrinterDC
* Create(wxPrintData
* data
) ; 
  58 class wxMacCarbonPrinterDC 
: public wxNativePrinterDC
 
  61     wxMacCarbonPrinterDC( wxPrintData
* data 
) ; 
  62     ~wxMacCarbonPrinterDC() ; 
  63     virtual bool StartDoc(  wxPrinterDC
* dc 
, const wxString
& message 
) ; 
  64     virtual void EndDoc( wxPrinterDC
* dc 
) ; 
  65     virtual void StartPage( wxPrinterDC
* dc 
) ; 
  66     virtual void EndPage( wxPrinterDC
* dc 
) ; 
  67     virtual wxCoord 
GetMaxX() const { return m_maxX 
; } 
  68     virtual wxCoord 
GetMaxY() const { return m_maxY 
; } 
  69     virtual wxUint32 
GetStatus() const { return m_err 
; } 
  71     GrafPtr m_macPrintFormerPort 
; 
  77 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data 
) 
  79     ::GetPort( & m_macPrintFormerPort 
) ; 
  82     wxMacCarbonPrintData 
*native 
= (wxMacCarbonPrintData
*) data
->m_nativePrintData 
; 
  85     m_err 
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
); 
  89     m_maxX 
= wxCoord(rPage
.right 
- rPage
.left
) ; 
  90     m_maxY 
= wxCoord(rPage
.bottom 
- rPage
.top
); 
  93 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC() 
  95     // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member 
  96     ::SetPort( m_macPrintFormerPort 
) ; 
  99 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
) 
 101     return new wxMacCarbonPrinterDC(data
) ; 
 104 bool wxMacCarbonPrinterDC::StartDoc(  wxPrinterDC
* dc 
, const wxString
& WXUNUSED(message
)  ) 
 109     wxMacCarbonPrintData 
*native 
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData 
; 
 111 #if wxMAC_USE_CORE_GRAPHICS 
 113         CFStringRef s
[1] = { kPMGraphicsContextCoreGraphics 
}; 
 114         CFArrayRef  graphicsContextsArray 
= CFArrayCreate(NULL
, (const void**)s
, 1, &kCFTypeArrayCallBacks
); 
 115         PMSessionSetDocumentFormatGeneration(native
->m_macPrintSession
, kPMDocumentFormatPDF
, graphicsContextsArray
, NULL
); 
 116         CFRelease(graphicsContextsArray
); 
 120     m_err 
= PMSessionBeginDocument(native
->m_macPrintSession
, 
 121               native
->m_macPrintSettings
, 
 122               native
->m_macPageFormat
); 
 123     if ( m_err 
!= noErr 
) 
 127     m_err 
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
); 
 128     if ( m_err 
!= noErr 
) 
 131     m_maxX 
= (wxCoord
)(rPage
.right 
- rPage
.left
); 
 132     m_maxY 
= (wxCoord
)(rPage
.bottom 
- rPage
.top
); 
 136 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc 
) 
 141     wxMacCarbonPrintData 
*native 
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData 
; 
 143     m_err 
= PMSessionEndDocument(native
->m_macPrintSession
); 
 146 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc 
) 
 151     wxMacCarbonPrintData 
*native 
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData 
; 
 153     m_err 
= PMSessionBeginPage(native
->m_macPrintSession
, 
 154                  native
->m_macPageFormat
, 
 157 #if wxMAC_USE_CORE_GRAPHICS 
 158     CGContextRef pageContext
; 
 160     if ( m_err 
== noErr 
) 
 162 #if wxMAC_USE_CORE_GRAPHICS 
 163         m_err 
= PMSessionGetGraphicsContext(native
->m_macPrintSession
, 
 164                                             kPMGraphicsContextCoreGraphics
, 
 165                                             (void**) &pageContext 
); 
 166         dc
->MacSetCGContext(pageContext
) ; 
 168         m_err 
= PMSessionGetGraphicsContext(native
->m_macPrintSession
, 
 169                                             kPMGraphicsContextQuickdraw
, 
 170                                             (void**) &dc
->m_macPort 
); 
 174     if ( m_err 
!= noErr 
) 
 176         PMSessionEndPage(native
->m_macPrintSession
); 
 177         PMSessionEndDocument(native
->m_macPrintSession
); 
 183         m_err 
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
); 
 186 #if wxMAC_USE_CORE_GRAPHICS 
 187             CGContextTranslateCTM( pageContext 
, 0 , rPage
.bottom 
- rPage
.top 
) ; 
 188             CGContextScaleCTM( pageContext 
, 1 , -1 ) ; 
 190             dc
->m_macLocalOrigin
.x 
= (int) rPage
.left
; 
 191             dc
->m_macLocalOrigin
.y 
= (int) rPage
.top
; 
 194         // since this is a non-critical error, we set the flag back 
 199 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc 
) 
 204     wxMacCarbonPrintData 
*native 
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData 
; 
 206     m_err 
= PMSessionEndPage(native
->m_macPrintSession
); 
 207     if ( m_err 
!= noErr 
) 
 209         PMSessionEndDocument(native
->m_macPrintSession
); 
 215 class wxMacClassicPrinterDC 
: public wxNativePrinterDC
 
 218     wxMacClassicPrinterDC( wxPrintData
* data 
) ; 
 219     ~wxMacClassicPrinterDC() ; 
 220     virtual bool StartDoc(  wxPrinterDC
* dc 
, const wxString
& message 
) ; 
 221     virtual void EndDoc( wxPrinterDC
* dc 
) ; 
 222     virtual void StartPage( wxPrinterDC
* dc 
) ; 
 223     virtual void EndPage( wxPrinterDC
* dc 
) ; 
 224     virtual wxCoord 
GetMaxX() const { return m_maxX 
; } 
 225     virtual wxCoord 
GetMaxY() const { return m_maxY 
; } 
 226     virtual wxUint32 
GetStatus() const { return m_err 
; } 
 228     GrafPtr m_macPrintFormerPort 
; 
 229     TPPrPort m_macPrintingPort 
; 
 235 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
) 
 237     return new wxMacClassicPrinterDC(data
) ; 
 240 wxMacClassicPrinterDC::wxMacClassicPrinterDC(wxPrintData
* data
) 
 242     ::GetPort( &m_macPrintFormerPort 
) ; 
 246     if ( m_err 
!= noErr 
) 
 249     wxMacClassicPrintData 
*native 
= (wxMacClassicPrintData
*) data
->m_nativePrintData 
; 
 251     if ( ::PrValidate( native
->m_macPrintSettings 
) ) 
 253         // the driver has changed in the mean time, should we pop up a page setup dialog ? 
 254         if ( !::PrStlDialog( native
->m_macPrintSettings 
) ) 
 262     if ( m_err 
== noErr 
) 
 264         m_maxX 
= (**native
->m_macPrintSettings
).prInfo
.rPage
.right 
- (**native
->m_macPrintSettings
).prInfo
.rPage
.left 
; 
 265         m_maxY 
= (**native
->m_macPrintSettings
).prInfo
.rPage
.bottom 
- (**native
->m_macPrintSettings
).prInfo
.rPage
.top 
; 
 269 wxMacClassicPrinterDC::~wxMacClassicPrinterDC() 
 272     ::SetPort( LMGetWMgrPort() ) ; 
 275 bool wxMacClassicPrinterDC::StartDoc(  wxPrinterDC
* dc 
, const wxString
& WXUNUSED(message
)  ) 
 280     wxMacClassicPrintData 
*native 
= (wxMacClassicPrintData
*) dc
->GetPrintData().m_nativePrintData 
; 
 281     m_macPrintingPort 
= ::PrOpenDoc( native
->m_macPrintSettings 
, NULL 
, NULL 
) ; 
 287     dc
->m_macPort 
= (GrafPtr 
) m_macPrintingPort 
; 
 288     m_maxX 
= (**native
->m_macPrintSettings
).prInfo
.rPage
.right 
- (**native
->m_macPrintSettings
).prInfo
.rPage
.left 
; 
 289     m_maxY 
= (**native
->m_macPrintSettings
).prInfo
.rPage
.bottom 
- (**native
->m_macPrintSettings
).prInfo
.rPage
.top 
; 
 293 void wxMacClassicPrinterDC::EndDoc( wxPrinterDC
* dc 
) 
 298     PrCloseDoc( m_macPrintingPort 
) ; 
 302 void wxMacClassicPrinterDC::StartPage( wxPrinterDC
* dc 
) 
 307     wxMacClassicPrintData 
*native 
= (wxMacClassicPrintData
*) dc
->GetPrintData().m_nativePrintData 
; 
 309     PrOpenPage( m_macPrintingPort 
, NULL 
) ; 
 310     dc
->m_macLocalOrigin
.x 
=  (**native
->m_macPrintSettings
).rPaper
.left 
; 
 311     dc
->m_macLocalOrigin
.y 
=  (**native
->m_macPrintSettings
).rPaper
.top 
; 
 312     // m_macPrintingPort is now the current port 
 313     Rect clip 
= { -32000 , -32000 , 32000 , 32000 } ; 
 314     ::ClipRect( &clip 
) ; 
 316     if ( m_err 
!= noErr 
) 
 317         ::PrCloseDoc( m_macPrintingPort 
) ; 
 320 void wxMacClassicPrinterDC::EndPage( wxPrinterDC
* dc 
) 
 325     PrClosePage( m_macPrintingPort 
) ; 
 327     if ( m_err 
!= noErr 
) 
 328         ::PrCloseDoc( m_macPrintingPort  
) ; 
 333 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
) 
 336     m_printData 
= printdata 
; 
 337     m_printData
.ConvertToNative() ; 
 338     m_nativePrinterDC 
= wxNativePrinterDC::Create( &m_printData 
) ; 
 339     if ( m_nativePrinterDC 
) 
 341         m_ok 
= m_nativePrinterDC
->Ok() ; 
 345             message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ; 
 346             wxMessageDialog 
dialog( NULL 
, message 
, wxEmptyString
, wxICON_HAND 
| wxOK
) ; 
 349 #if wxMAC_USE_CORE_GRAPHICS 
 350         // the cgContext will only be handed over page by page 
 351         m_graphicContext 
= new wxMacCGContext() ; 
 356 wxPrinterDC::~wxPrinterDC(void) 
 358     delete m_nativePrinterDC 
; 
 361 #if wxMAC_USE_CORE_GRAPHICS 
 362 void wxPrinterDC::MacSetCGContext( void * cg 
)  
 364     ((wxMacCGContext
*)(m_graphicContext
))->SetNativeContext( (CGContextRef
) cg 
) ; 
 365     m_graphicContext
->SetPen( m_pen 
) ; 
 366     m_graphicContext
->SetBrush( m_brush 
) ; 
 369 bool wxPrinterDC::StartDoc( const wxString
& message 
) 
 371     wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ; 
 376     if ( m_nativePrinterDC
->StartDoc(this, message 
) ) 
 378         // in case we have to do additional things when successful 
 380     m_ok 
= m_nativePrinterDC
->Ok() ; 
 384         message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ; 
 385         wxMessageDialog 
dialog( NULL 
, message 
, wxEmptyString
, wxICON_HAND 
| wxOK
) ; 
 392 void wxPrinterDC::EndDoc(void) 
 397     m_nativePrinterDC
->EndDoc( this ) ; 
 398     m_ok 
= m_nativePrinterDC
->Ok() ; 
 403         message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ; 
 404         wxMessageDialog 
dialog( NULL 
, message 
, wxEmptyString
, wxICON_HAND 
| wxOK
) ; 
 409 void wxPrinterDC::StartPage(void) 
 414     m_logicalFunction 
= wxCOPY
; 
 415     //  m_textAlignment = wxALIGN_TOP_LEFT; 
 416     m_backgroundMode 
= wxTRANSPARENT
; 
 418     m_textForegroundColour 
= *wxBLACK
; 
 419     m_textBackgroundColour 
= *wxWHITE
; 
 420     m_pen 
= *wxBLACK_PEN
; 
 421     m_font 
= *wxNORMAL_FONT
; 
 422     m_brush 
= *wxTRANSPARENT_BRUSH
; 
 423     m_backgroundBrush 
= *wxWHITE_BRUSH
; 
 424 #if !wxMAC_USE_CORE_GRAPHICS 
 425     m_macFontInstalled 
= false ; 
 426     m_macBrushInstalled 
= false ; 
 427     m_macPenInstalled 
= false ; 
 430     m_nativePrinterDC
->StartPage(this) ; 
 431     m_ok 
= m_nativePrinterDC
->Ok() ; 
 435 void wxPrinterDC::EndPage(void) 
 440     m_nativePrinterDC
->EndPage(this) ; 
 441     m_ok 
= m_nativePrinterDC
->Ok() ; 
 444 void wxPrinterDC::DoGetSize(int *width
, int *height
) const 
 446     wxCHECK_RET( m_ok 
, _T("GetSize() doesn't work without a valid wxPrinterDC") ); 
 449         * width 
= m_nativePrinterDC
->GetMaxX() ; 
 451         * height 
= m_nativePrinterDC
->GetMaxY() ;