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"
24 #include "wx/msgdlg.h"
28 #include "wx/mac/uma.h"
29 #include "wx/mac/private/print.h"
30 #include "wx/graphics.h"
32 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
34 class wxNativePrinterDC
37 wxNativePrinterDC() {}
38 virtual ~wxNativePrinterDC() {}
39 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) = 0;
40 virtual void EndDoc( wxPrinterDC
* dc
) = 0;
41 virtual void StartPage( wxPrinterDC
* dc
) = 0;
42 virtual void EndPage( wxPrinterDC
* dc
) = 0;
43 virtual void GetSize( int *w
, int *h
) const = 0 ;
44 virtual wxSize
GetPPI() const = 0 ;
46 // returns 0 in case of no Error, otherwise platform specific error codes
47 virtual wxUint32
GetStatus() const = 0 ;
48 bool Ok() { return GetStatus() == 0 ; }
50 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
53 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
56 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
57 virtual ~wxMacCarbonPrinterDC() ;
58 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
59 virtual void EndDoc( wxPrinterDC
* dc
) ;
60 virtual void StartPage( wxPrinterDC
* dc
) ;
61 virtual void EndPage( wxPrinterDC
* dc
) ;
62 virtual wxUint32
GetStatus() const { return m_err
; }
63 virtual void GetSize( int *w
, int *h
) const ;
64 virtual wxSize
GetPPI() const ;
66 #if !wxMAC_USE_CORE_GRAPHICS
67 GrafPtr m_macPrintFormerPort
;
75 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
77 #if !wxMAC_USE_CORE_GRAPHICS
78 ::GetPort( & m_macPrintFormerPort
) ;
81 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->GetNativeData() ;
84 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
88 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
89 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
92 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
94 PMSessionGetCurrentPrinter(native
->m_macPrintSession
, &printer
);
95 PMPrinterGetOutputResolution( printer
, native
->m_macPrintSettings
, &res
) ;
97 m_err
= PMGetResolution((PMPageFormat
) (native
->m_macPageFormat
), &res
);
99 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
102 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
104 #if !wxMAC_USE_CORE_GRAPHICS
105 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
106 ::SetPort( m_macPrintFormerPort
) ;
110 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
112 return new wxMacCarbonPrinterDC(data
) ;
115 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
120 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
122 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4 && wxMAC_USE_CORE_GRAPHICS
124 CFStringRef s
[1] = { kPMGraphicsContextCoreGraphics
};
125 CFArrayRef graphicsContextsArray
= CFArrayCreate(NULL
, (const void**)s
, 1, &kCFTypeArrayCallBacks
);
126 PMSessionSetDocumentFormatGeneration(native
->m_macPrintSession
, kPMDocumentFormatPDF
, graphicsContextsArray
, NULL
);
127 CFRelease(graphicsContextsArray
);
130 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4 && wxMAC_USE_CORE_GRAPHICS
131 m_err
= PMSessionBeginCGDocument(native
->m_macPrintSession
,
132 native
->m_macPrintSettings
,
133 native
->m_macPageFormat
);
135 m_err
= PMSessionBeginDocument(native
->m_macPrintSession
,
136 native
->m_macPrintSettings
,
137 native
->m_macPageFormat
);
141 if ( m_err
!= noErr
)
145 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
146 if ( m_err
!= noErr
)
149 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
150 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
153 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
155 PMSessionGetCurrentPrinter(native
->m_macPrintSession
, &printer
);
156 PMPrinterGetOutputResolution( printer
, native
->m_macPrintSettings
, &res
) ;
158 m_err
= PMGetResolution((PMPageFormat
) (native
->m_macPageFormat
), &res
);
160 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
164 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
169 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
171 m_err
= PMSessionEndDocument(native
->m_macPrintSession
);
174 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
179 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
181 m_err
= PMSessionBeginPage(native
->m_macPrintSession
,
182 native
->m_macPageFormat
,
185 #if wxMAC_USE_CORE_GRAPHICS
186 CGContextRef pageContext
;
188 if ( m_err
== noErr
)
190 #if wxMAC_USE_CORE_GRAPHICS
191 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
192 m_err
= PMSessionGetCGGraphicsContext(native
->m_macPrintSession
,
196 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
197 kPMGraphicsContextCoreGraphics
,
198 (void**) &pageContext
);
200 dc
->MacSetCGContext(pageContext
) ;
202 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
203 kPMGraphicsContextQuickdraw
,
204 (void**) &dc
->m_macPort
);
208 if ( m_err
!= noErr
)
210 PMSessionEndPage(native
->m_macPrintSession
);
211 PMSessionEndDocument(native
->m_macPrintSession
);
217 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
220 #if wxMAC_USE_CORE_GRAPHICS
222 PMGetAdjustedPaperRect( native
->m_macPageFormat
, &paperRect
) ;
223 CGContextTranslateCTM( pageContext
, -paperRect
.left
, -paperRect
.top
+ ( rPage
.bottom
- rPage
.top
) ) ;
224 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
225 CGContextSaveGState( pageContext
) ;
227 dc
->m_macLocalOrigin
.x
= (int) rPage
.left
;
228 dc
->m_macLocalOrigin
.y
= (int) rPage
.top
;
231 // since this is a non-critical error, we set the flag back
236 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
241 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
243 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
244 if ( m_err
!= noErr
)
246 PMSessionEndDocument(native
->m_macPrintSession
);
250 void wxMacCarbonPrinterDC::GetSize( int *w
, int *h
) const
258 wxSize
wxMacCarbonPrinterDC::GetPPI() const
267 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
270 m_printData
= printdata
;
271 m_printData
.ConvertToNative() ;
272 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
273 if ( m_nativePrinterDC
)
275 m_ok
= m_nativePrinterDC
->Ok() ;
279 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
280 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
285 wxSize sz
= GetPPI();
286 m_mm_to_pix_x
= mm2inches
* sz
.x
;
287 m_mm_to_pix_y
= mm2inches
* sz
.y
;
289 #if wxMAC_USE_CORE_GRAPHICS
291 // the cgContext will only be handed over page by page
292 m_graphicContext = new wxMacCGContext() ;
298 wxSize
wxPrinterDC::GetPPI() const
300 return m_nativePrinterDC
->GetPPI() ;
303 wxPrinterDC::~wxPrinterDC(void)
305 #if wxMAC_USE_CORE_GRAPHICS
307 // this context was borrowed
308 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ;
311 delete m_nativePrinterDC
;
314 #if wxMAC_USE_CORE_GRAPHICS
315 void wxPrinterDC::MacSetCGContext( void * cg
)
317 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg
) );
318 m_graphicContext
->SetPen( m_pen
) ;
319 m_graphicContext
->SetBrush( m_brush
) ;
322 bool wxPrinterDC::StartDoc( const wxString
& message
)
324 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
329 if ( m_nativePrinterDC
->StartDoc(this, message
) )
331 // in case we have to do additional things when successful
333 m_ok
= m_nativePrinterDC
->Ok() ;
337 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
338 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
345 void wxPrinterDC::EndDoc(void)
350 m_nativePrinterDC
->EndDoc( this ) ;
351 m_ok
= m_nativePrinterDC
->Ok() ;
356 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
357 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
362 void wxPrinterDC::StartPage(void)
367 m_logicalFunction
= wxCOPY
;
368 // m_textAlignment = wxALIGN_TOP_LEFT;
369 m_backgroundMode
= wxTRANSPARENT
;
371 m_textForegroundColour
= *wxBLACK
;
372 m_textBackgroundColour
= *wxWHITE
;
373 m_pen
= *wxBLACK_PEN
;
374 m_font
= *wxNORMAL_FONT
;
375 m_brush
= *wxTRANSPARENT_BRUSH
;
376 m_backgroundBrush
= *wxWHITE_BRUSH
;
377 #if !wxMAC_USE_CORE_GRAPHICS
378 m_macFontInstalled
= false ;
379 m_macBrushInstalled
= false ;
380 m_macPenInstalled
= false ;
383 m_nativePrinterDC
->StartPage(this) ;
384 m_ok
= m_nativePrinterDC
->Ok() ;
388 void wxPrinterDC::EndPage(void)
393 m_nativePrinterDC
->EndPage(this) ;
394 m_ok
= m_nativePrinterDC
->Ok() ;
397 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
399 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
400 m_nativePrinterDC
->GetSize(width
, height
) ;