1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxPrinterDC class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #if wxUSE_PRINTING_ARCHITECTURE
24 #include "wx/dcprint.h"
25 #include "wx/msgdlg.h"
27 #include "wx/mac/uma.h"
28 #include "wx/mac/private/print.h"
30 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
32 class wxNativePrinterDC
35 wxNativePrinterDC() {}
36 virtual ~wxNativePrinterDC() {}
37 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) = 0;
38 virtual void EndDoc( wxPrinterDC
* dc
) = 0;
39 virtual void StartPage( wxPrinterDC
* dc
) = 0;
40 virtual void EndPage( wxPrinterDC
* dc
) = 0;
41 virtual wxCoord
GetMaxX() const = 0 ;
42 virtual wxCoord
GetMaxY() const = 0 ;
43 // returns 0 in case of no Error, otherwise platform specific error codes
44 virtual wxUint32
GetStatus() const = 0 ;
45 bool Ok() { return GetStatus() == 0 ; }
47 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
50 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
53 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
54 ~wxMacCarbonPrinterDC() ;
55 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
56 virtual void EndDoc( wxPrinterDC
* dc
) ;
57 virtual void StartPage( wxPrinterDC
* dc
) ;
58 virtual void EndPage( wxPrinterDC
* dc
) ;
59 virtual wxCoord
GetMaxX() const { return m_maxX
; }
60 virtual wxCoord
GetMaxY() const { return m_maxY
; }
61 virtual wxUint32
GetStatus() const { return m_err
; }
63 GrafPtr m_macPrintFormerPort
;
69 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
71 ::GetPort( & m_macPrintFormerPort
) ;
74 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->GetNativeData() ;
77 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
81 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
82 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
85 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
87 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
88 ::SetPort( m_macPrintFormerPort
) ;
91 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
93 return new wxMacCarbonPrinterDC(data
) ;
96 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
101 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
103 #if wxMAC_USE_CORE_GRAPHICS
105 CFStringRef s
[1] = { kPMGraphicsContextCoreGraphics
};
106 CFArrayRef graphicsContextsArray
= CFArrayCreate(NULL
, (const void**)s
, 1, &kCFTypeArrayCallBacks
);
107 PMSessionSetDocumentFormatGeneration(native
->m_macPrintSession
, kPMDocumentFormatPDF
, graphicsContextsArray
, NULL
);
108 CFRelease(graphicsContextsArray
);
112 m_err
= PMSessionBeginDocument(native
->m_macPrintSession
,
113 native
->m_macPrintSettings
,
114 native
->m_macPageFormat
);
115 if ( m_err
!= noErr
)
119 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
120 if ( m_err
!= noErr
)
123 m_maxX
= (wxCoord
)(rPage
.right
- rPage
.left
);
124 m_maxY
= (wxCoord
)(rPage
.bottom
- rPage
.top
);
128 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
133 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
135 m_err
= PMSessionEndDocument(native
->m_macPrintSession
);
138 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
143 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
145 m_err
= PMSessionBeginPage(native
->m_macPrintSession
,
146 native
->m_macPageFormat
,
149 #if wxMAC_USE_CORE_GRAPHICS
150 CGContextRef pageContext
;
152 if ( m_err
== noErr
)
154 #if wxMAC_USE_CORE_GRAPHICS
155 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
156 kPMGraphicsContextCoreGraphics
,
157 (void**) &pageContext
);
158 dc
->MacSetCGContext(pageContext
) ;
160 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
161 kPMGraphicsContextQuickdraw
,
162 (void**) &dc
->m_macPort
);
166 if ( m_err
!= noErr
)
168 PMSessionEndPage(native
->m_macPrintSession
);
169 PMSessionEndDocument(native
->m_macPrintSession
);
175 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
178 #if wxMAC_USE_CORE_GRAPHICS
180 PMGetAdjustedPaperRect( native
->m_macPageFormat
, &paperRect
) ;
181 CGContextTranslateCTM( pageContext
, -paperRect
.left
, -paperRect
.top
+ ( rPage
.bottom
- rPage
.top
) ) ;
182 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
183 CGContextSaveGState( pageContext
) ;
185 dc
->m_macLocalOrigin
.x
= (int) rPage
.left
;
186 dc
->m_macLocalOrigin
.y
= (int) rPage
.top
;
189 // since this is a non-critical error, we set the flag back
194 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
199 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
201 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
202 if ( m_err
!= noErr
)
204 PMSessionEndDocument(native
->m_macPrintSession
);
208 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
211 m_printData
= printdata
;
212 m_printData
.ConvertToNative() ;
213 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
214 if ( m_nativePrinterDC
)
216 m_ok
= m_nativePrinterDC
->Ok() ;
220 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
221 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
224 #if wxMAC_USE_CORE_GRAPHICS
225 // the cgContext will only be handed over page by page
226 m_graphicContext
= new wxMacCGContext() ;
231 wxPrinterDC::~wxPrinterDC(void)
233 #if wxMAC_USE_CORE_GRAPHICS
234 // this context was borrowed
235 ((wxMacCGContext
*)(m_graphicContext
))->SetNativeContext( NULL
) ;
237 delete m_nativePrinterDC
;
240 #if wxMAC_USE_CORE_GRAPHICS
241 void wxPrinterDC::MacSetCGContext( void * cg
)
243 ((wxMacCGContext
*)(m_graphicContext
))->SetNativeContext( (CGContextRef
) cg
) ;
244 m_graphicContext
->SetPen( m_pen
) ;
245 m_graphicContext
->SetBrush( m_brush
) ;
248 bool wxPrinterDC::StartDoc( const wxString
& message
)
250 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
255 if ( m_nativePrinterDC
->StartDoc(this, message
) )
257 // in case we have to do additional things when successful
259 m_ok
= m_nativePrinterDC
->Ok() ;
263 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
264 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
271 void wxPrinterDC::EndDoc(void)
276 m_nativePrinterDC
->EndDoc( this ) ;
277 m_ok
= m_nativePrinterDC
->Ok() ;
282 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
283 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
288 void wxPrinterDC::StartPage(void)
293 m_logicalFunction
= wxCOPY
;
294 // m_textAlignment = wxALIGN_TOP_LEFT;
295 m_backgroundMode
= wxTRANSPARENT
;
297 m_textForegroundColour
= *wxBLACK
;
298 m_textBackgroundColour
= *wxWHITE
;
299 m_pen
= *wxBLACK_PEN
;
300 m_font
= *wxNORMAL_FONT
;
301 m_brush
= *wxTRANSPARENT_BRUSH
;
302 m_backgroundBrush
= *wxWHITE_BRUSH
;
303 #if !wxMAC_USE_CORE_GRAPHICS
304 m_macFontInstalled
= false ;
305 m_macBrushInstalled
= false ;
306 m_macPenInstalled
= false ;
309 m_nativePrinterDC
->StartPage(this) ;
310 m_ok
= m_nativePrinterDC
->Ok() ;
314 void wxPrinterDC::EndPage(void)
319 m_nativePrinterDC
->EndPage(this) ;
320 m_ok
= m_nativePrinterDC
->Ok() ;
323 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
325 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
328 * width
= m_nativePrinterDC
->GetMaxX() ;
330 * height
= m_nativePrinterDC
->GetMaxY() ;