1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dcprint.cpp
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
21 #include "wx/dcprint.h"
26 #include "wx/msgdlg.h"
28 #include "wx/mac/uma.h"
29 #include "wx/mac/private/print.h"
31 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
33 class wxNativePrinterDC
36 wxNativePrinterDC() {}
37 virtual ~wxNativePrinterDC() {}
38 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) = 0;
39 virtual void EndDoc( wxPrinterDC
* dc
) = 0;
40 virtual void StartPage( wxPrinterDC
* dc
) = 0;
41 virtual void EndPage( wxPrinterDC
* dc
) = 0;
42 virtual wxCoord
GetMaxX() const = 0 ;
43 virtual wxCoord
GetMaxY() const = 0 ;
44 // returns 0 in case of no Error, otherwise platform specific error codes
45 virtual wxUint32
GetStatus() const = 0 ;
46 bool Ok() { return GetStatus() == 0 ; }
48 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
51 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
54 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
55 ~wxMacCarbonPrinterDC() ;
56 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
57 virtual void EndDoc( wxPrinterDC
* dc
) ;
58 virtual void StartPage( wxPrinterDC
* dc
) ;
59 virtual void EndPage( wxPrinterDC
* dc
) ;
60 virtual wxCoord
GetMaxX() const { return m_maxX
; }
61 virtual wxCoord
GetMaxY() const { return m_maxY
; }
62 virtual wxUint32
GetStatus() const { return m_err
; }
64 GrafPtr m_macPrintFormerPort
;
70 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
72 ::GetPort( & m_macPrintFormerPort
) ;
75 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->GetNativeData() ;
78 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
82 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
83 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
86 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
88 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
89 ::SetPort( m_macPrintFormerPort
) ;
92 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
94 return new wxMacCarbonPrinterDC(data
) ;
97 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
102 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
104 #if wxMAC_USE_CORE_GRAPHICS
106 CFStringRef s
[1] = { kPMGraphicsContextCoreGraphics
};
107 CFArrayRef graphicsContextsArray
= CFArrayCreate(NULL
, (const void**)s
, 1, &kCFTypeArrayCallBacks
);
108 PMSessionSetDocumentFormatGeneration(native
->m_macPrintSession
, kPMDocumentFormatPDF
, graphicsContextsArray
, NULL
);
109 CFRelease(graphicsContextsArray
);
113 m_err
= PMSessionBeginDocument(native
->m_macPrintSession
,
114 native
->m_macPrintSettings
,
115 native
->m_macPageFormat
);
116 if ( m_err
!= noErr
)
120 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
121 if ( m_err
!= noErr
)
124 m_maxX
= (wxCoord
)(rPage
.right
- rPage
.left
);
125 m_maxY
= (wxCoord
)(rPage
.bottom
- rPage
.top
);
129 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
134 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
136 m_err
= PMSessionEndDocument(native
->m_macPrintSession
);
139 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
144 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
146 m_err
= PMSessionBeginPage(native
->m_macPrintSession
,
147 native
->m_macPageFormat
,
150 #if wxMAC_USE_CORE_GRAPHICS
151 CGContextRef pageContext
;
153 if ( m_err
== noErr
)
155 #if wxMAC_USE_CORE_GRAPHICS
156 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
157 kPMGraphicsContextCoreGraphics
,
158 (void**) &pageContext
);
159 dc
->MacSetCGContext(pageContext
) ;
161 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
162 kPMGraphicsContextQuickdraw
,
163 (void**) &dc
->m_macPort
);
167 if ( m_err
!= noErr
)
169 PMSessionEndPage(native
->m_macPrintSession
);
170 PMSessionEndDocument(native
->m_macPrintSession
);
176 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
179 #if wxMAC_USE_CORE_GRAPHICS
181 PMGetAdjustedPaperRect( native
->m_macPageFormat
, &paperRect
) ;
182 CGContextTranslateCTM( pageContext
, -paperRect
.left
, -paperRect
.top
+ ( rPage
.bottom
- rPage
.top
) ) ;
183 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
184 CGContextSaveGState( pageContext
) ;
186 dc
->m_macLocalOrigin
.x
= (int) rPage
.left
;
187 dc
->m_macLocalOrigin
.y
= (int) rPage
.top
;
190 // since this is a non-critical error, we set the flag back
195 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
200 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
202 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
203 if ( m_err
!= noErr
)
205 PMSessionEndDocument(native
->m_macPrintSession
);
209 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
212 m_printData
= printdata
;
213 m_printData
.ConvertToNative() ;
214 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
215 if ( m_nativePrinterDC
)
217 m_ok
= m_nativePrinterDC
->Ok() ;
221 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
222 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
225 #if wxMAC_USE_CORE_GRAPHICS
226 // the cgContext will only be handed over page by page
227 m_graphicContext
= new wxMacCGContext() ;
232 wxPrinterDC::~wxPrinterDC(void)
234 #if wxMAC_USE_CORE_GRAPHICS
235 // this context was borrowed
236 ((wxMacCGContext
*)(m_graphicContext
))->SetNativeContext( NULL
) ;
238 delete m_nativePrinterDC
;
241 #if wxMAC_USE_CORE_GRAPHICS
242 void wxPrinterDC::MacSetCGContext( void * cg
)
244 ((wxMacCGContext
*)(m_graphicContext
))->SetNativeContext( (CGContextRef
) cg
) ;
245 m_graphicContext
->SetPen( m_pen
) ;
246 m_graphicContext
->SetBrush( m_brush
) ;
249 bool wxPrinterDC::StartDoc( const wxString
& message
)
251 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
256 if ( m_nativePrinterDC
->StartDoc(this, message
) )
258 // in case we have to do additional things when successful
260 m_ok
= m_nativePrinterDC
->Ok() ;
264 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
265 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
272 void wxPrinterDC::EndDoc(void)
277 m_nativePrinterDC
->EndDoc( this ) ;
278 m_ok
= m_nativePrinterDC
->Ok() ;
283 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
284 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
289 void wxPrinterDC::StartPage(void)
294 m_logicalFunction
= wxCOPY
;
295 // m_textAlignment = wxALIGN_TOP_LEFT;
296 m_backgroundMode
= wxTRANSPARENT
;
298 m_textForegroundColour
= *wxBLACK
;
299 m_textBackgroundColour
= *wxWHITE
;
300 m_pen
= *wxBLACK_PEN
;
301 m_font
= *wxNORMAL_FONT
;
302 m_brush
= *wxTRANSPARENT_BRUSH
;
303 m_backgroundBrush
= *wxWHITE_BRUSH
;
304 #if !wxMAC_USE_CORE_GRAPHICS
305 m_macFontInstalled
= false ;
306 m_macBrushInstalled
= false ;
307 m_macPenInstalled
= false ;
310 m_nativePrinterDC
->StartPage(this) ;
311 m_ok
= m_nativePrinterDC
->Ok() ;
315 void wxPrinterDC::EndPage(void)
320 m_nativePrinterDC
->EndPage(this) ;
321 m_ok
= m_nativePrinterDC
->Ok() ;
324 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
326 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
329 * width
= m_nativePrinterDC
->GetMaxX() ;
331 * height
= m_nativePrinterDC
->GetMaxY() ;