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"
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 void GetSize( int *w
, int *h
) const = 0 ;
43 virtual wxSize
GetPPI() const = 0 ;
45 // returns 0 in case of no Error, otherwise platform specific error codes
46 virtual wxUint32
GetStatus() const = 0 ;
47 bool Ok() { return GetStatus() == 0 ; }
49 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
52 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
55 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
56 virtual ~wxMacCarbonPrinterDC() ;
57 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
58 virtual void EndDoc( wxPrinterDC
* dc
) ;
59 virtual void StartPage( wxPrinterDC
* dc
) ;
60 virtual void EndPage( wxPrinterDC
* dc
) ;
61 virtual wxUint32
GetStatus() const { return m_err
; }
62 virtual void GetSize( int *w
, int *h
) const ;
63 virtual wxSize
GetPPI() const ;
65 #if !wxMAC_USE_CORE_GRAPHICS
66 GrafPtr m_macPrintFormerPort
;
74 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
76 #if !wxMAC_USE_CORE_GRAPHICS
77 ::GetPort( & m_macPrintFormerPort
) ;
80 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->GetNativeData() ;
83 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
87 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
88 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
91 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
93 PMSessionGetCurrentPrinter(native
->m_macPrintSession
, &printer
);
94 PMPrinterGetOutputResolution( printer
, native
->m_macPrintSettings
, &res
) ;
96 m_err
= PMGetResolution((PMPageFormat
) (native
->m_macPageFormat
), &res
);
97 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
101 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
103 #if !wxMAC_USE_CORE_GRAPHICS
104 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
105 ::SetPort( m_macPrintFormerPort
) ;
109 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
111 return new wxMacCarbonPrinterDC(data
) ;
114 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
119 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
121 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4 && wxMAC_USE_CORE_GRAPHICS
123 CFStringRef s
[1] = { kPMGraphicsContextCoreGraphics
};
124 CFArrayRef graphicsContextsArray
= CFArrayCreate(NULL
, (const void**)s
, 1, &kCFTypeArrayCallBacks
);
125 PMSessionSetDocumentFormatGeneration(native
->m_macPrintSession
, kPMDocumentFormatPDF
, graphicsContextsArray
, NULL
);
126 CFRelease(graphicsContextsArray
);
129 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4 && wxMAC_USE_CORE_GRAPHICS
130 m_err
= PMSessionBeginCGDocument(native
->m_macPrintSession
,
131 native
->m_macPrintSettings
,
132 native
->m_macPageFormat
);
134 m_err
= PMSessionBeginDocument(native
->m_macPrintSession
,
135 native
->m_macPrintSettings
,
136 native
->m_macPageFormat
);
140 if ( m_err
!= noErr
)
144 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
145 if ( m_err
!= noErr
)
148 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
149 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
152 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
154 PMSessionGetCurrentPrinter(native
->m_macPrintSession
, &printer
);
155 PMPrinterGetOutputResolution( printer
, native
->m_macPrintSettings
, &res
) ;
157 m_err
= PMGetResolution((PMPageFormat
) (native
->m_macPageFormat
), &res
);
159 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
163 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
168 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
170 m_err
= PMSessionEndDocument(native
->m_macPrintSession
);
173 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
178 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
180 m_err
= PMSessionBeginPage(native
->m_macPrintSession
,
181 native
->m_macPageFormat
,
184 #if wxMAC_USE_CORE_GRAPHICS
185 CGContextRef pageContext
;
187 if ( m_err
== noErr
)
189 #if wxMAC_USE_CORE_GRAPHICS
190 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
191 m_err
= PMSessionGetCGGraphicsContext(native
->m_macPrintSession
,
195 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
196 kPMGraphicsContextCoreGraphics
,
197 (void**) &pageContext
);
199 dc
->MacSetCGContext(pageContext
) ;
201 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
202 kPMGraphicsContextQuickdraw
,
203 (void**) &dc
->m_macPort
);
207 if ( m_err
!= noErr
)
209 PMSessionEndPage(native
->m_macPrintSession
);
210 PMSessionEndDocument(native
->m_macPrintSession
);
216 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
219 #if wxMAC_USE_CORE_GRAPHICS
221 PMGetAdjustedPaperRect( native
->m_macPageFormat
, &paperRect
) ;
222 CGContextTranslateCTM( pageContext
, -paperRect
.left
, -paperRect
.top
+ ( rPage
.bottom
- rPage
.top
) ) ;
223 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
224 CGContextSaveGState( pageContext
) ;
226 dc
->m_macLocalOrigin
.x
= (int) rPage
.left
;
227 dc
->m_macLocalOrigin
.y
= (int) rPage
.top
;
230 // since this is a non-critical error, we set the flag back
235 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
240 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
242 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
243 if ( m_err
!= noErr
)
245 PMSessionEndDocument(native
->m_macPrintSession
);
249 void wxMacCarbonPrinterDC::GetSize( int *w
, int *h
) const
257 wxSize
wxMacCarbonPrinterDC::GetPPI() const
266 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
269 m_printData
= printdata
;
270 m_printData
.ConvertToNative() ;
271 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
272 if ( m_nativePrinterDC
)
274 m_ok
= m_nativePrinterDC
->Ok() ;
278 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
279 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
284 wxSize sz
= GetPPI();
285 m_mm_to_pix_x
= mm2inches
* sz
.x
;
286 m_mm_to_pix_y
= mm2inches
* sz
.y
;
288 #if wxMAC_USE_CORE_GRAPHICS
289 // the cgContext will only be handed over page by page
290 m_graphicContext
= new wxMacCGContext() ;
295 wxSize
wxPrinterDC::GetPPI() const
297 return m_nativePrinterDC
->GetPPI() ;
300 wxPrinterDC::~wxPrinterDC(void)
302 #if wxMAC_USE_CORE_GRAPHICS
303 // this context was borrowed
304 ((wxMacCGContext
*)(m_graphicContext
))->SetNativeContext( NULL
) ;
306 delete m_nativePrinterDC
;
309 #if wxMAC_USE_CORE_GRAPHICS
310 void wxPrinterDC::MacSetCGContext( void * cg
)
312 ((wxMacCGContext
*)(m_graphicContext
))->SetNativeContext( (CGContextRef
) cg
) ;
313 m_graphicContext
->SetPen( m_pen
) ;
314 m_graphicContext
->SetBrush( m_brush
) ;
317 bool wxPrinterDC::StartDoc( const wxString
& message
)
319 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
324 if ( m_nativePrinterDC
->StartDoc(this, message
) )
326 // in case we have to do additional things when successful
328 m_ok
= m_nativePrinterDC
->Ok() ;
332 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
333 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
340 void wxPrinterDC::EndDoc(void)
345 m_nativePrinterDC
->EndDoc( this ) ;
346 m_ok
= m_nativePrinterDC
->Ok() ;
351 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
352 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
357 void wxPrinterDC::StartPage(void)
362 m_logicalFunction
= wxCOPY
;
363 // m_textAlignment = wxALIGN_TOP_LEFT;
364 m_backgroundMode
= wxTRANSPARENT
;
366 m_textForegroundColour
= *wxBLACK
;
367 m_textBackgroundColour
= *wxWHITE
;
368 m_pen
= *wxBLACK_PEN
;
369 m_font
= *wxNORMAL_FONT
;
370 m_brush
= *wxTRANSPARENT_BRUSH
;
371 m_backgroundBrush
= *wxWHITE_BRUSH
;
372 #if !wxMAC_USE_CORE_GRAPHICS
373 m_macFontInstalled
= false ;
374 m_macBrushInstalled
= false ;
375 m_macPenInstalled
= false ;
378 m_nativePrinterDC
->StartPage(this) ;
379 m_ok
= m_nativePrinterDC
->Ok() ;
383 void wxPrinterDC::EndPage(void)
388 m_nativePrinterDC
->EndPage(this) ;
389 m_ok
= m_nativePrinterDC
->Ok() ;
392 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
394 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
395 m_nativePrinterDC
->GetSize(width
, height
) ;