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
);
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 // make sure (0,0) is at the upper left of the printable area (wx conventions)
223 // Core Graphics initially has the lower left of the paper as 0,0
224 CGContextTranslateCTM( pageContext
, -paperRect
.left
, paperRect
.bottom
) ;
225 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
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
234 #if wxMAC_USE_CORE_GRAPHICS
235 dc
->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext
) );
239 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
244 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
246 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
247 if ( m_err
!= noErr
)
249 PMSessionEndDocument(native
->m_macPrintSession
);
251 #if wxMAC_USE_CORE_GRAPHICS
252 // the cg context we got when starting the page isn't valid anymore, so replace it
253 dc
->SetGraphicsContext( wxGraphicsContext::Create() );
257 void wxMacCarbonPrinterDC::GetSize( int *w
, int *h
) const
265 wxSize
wxMacCarbonPrinterDC::GetPPI() const
274 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
277 m_printData
= printdata
;
278 m_printData
.ConvertToNative() ;
279 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
280 if ( m_nativePrinterDC
)
282 m_ok
= m_nativePrinterDC
->Ok() ;
286 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
287 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
292 wxSize sz
= GetPPI();
293 m_mm_to_pix_x
= mm2inches
* sz
.x
;
294 m_mm_to_pix_y
= mm2inches
* sz
.y
;
296 #if wxMAC_USE_CORE_GRAPHICS
297 // we need at least a measuring context because people start measuring before a page
298 // gets printed at all
299 SetGraphicsContext( wxGraphicsContext::Create() );
304 wxSize
wxPrinterDC::GetPPI() const
306 return m_nativePrinterDC
->GetPPI() ;
309 wxPrinterDC::~wxPrinterDC(void)
311 delete m_nativePrinterDC
;
314 bool wxPrinterDC::StartDoc( const wxString
& message
)
316 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
321 if ( m_nativePrinterDC
->StartDoc(this, message
) )
323 // in case we have to do additional things when successful
325 m_ok
= m_nativePrinterDC
->Ok() ;
329 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
330 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
337 void wxPrinterDC::EndDoc(void)
342 m_nativePrinterDC
->EndDoc( this ) ;
343 m_ok
= m_nativePrinterDC
->Ok() ;
348 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
349 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
354 wxRect
wxPrinterDC::GetPaperRect()
358 wxRect
pageRect(0, 0, w
, h
);
359 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) m_printData
.GetNativeData() ;
360 OSStatus err
= noErr
;
362 err
= PMGetAdjustedPaperRect(native
->m_macPageFormat
, &rPaper
);
365 return wxRect(wxCoord(rPaper
.left
), wxCoord(rPaper
.top
),
366 wxCoord(rPaper
.right
- rPaper
.left
), wxCoord(rPaper
.bottom
- rPaper
.top
));
369 void wxPrinterDC::StartPage(void)
374 m_logicalFunction
= wxCOPY
;
375 // m_textAlignment = wxALIGN_TOP_LEFT;
376 m_backgroundMode
= wxTRANSPARENT
;
378 m_textForegroundColour
= *wxBLACK
;
379 m_textBackgroundColour
= *wxWHITE
;
380 m_pen
= *wxBLACK_PEN
;
381 m_font
= *wxNORMAL_FONT
;
382 m_brush
= *wxTRANSPARENT_BRUSH
;
383 m_backgroundBrush
= *wxWHITE_BRUSH
;
384 #if !wxMAC_USE_CORE_GRAPHICS
385 m_macFontInstalled
= false ;
386 m_macBrushInstalled
= false ;
387 m_macPenInstalled
= false ;
390 m_nativePrinterDC
->StartPage(this) ;
391 m_ok
= m_nativePrinterDC
->Ok() ;
395 void wxPrinterDC::EndPage(void)
400 m_nativePrinterDC
->EndPage(this) ;
401 m_ok
= m_nativePrinterDC
->Ok() ;
404 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
406 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
407 m_nativePrinterDC
->GetSize(width
, height
) ;