1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/private.h"
29 #include "wx/osx/private/print.h"
30 #include "wx/osx/dcprint.h"
31 #include "wx/graphics.h"
33 IMPLEMENT_ABSTRACT_CLASS(wxPrinterDCImpl
, wxGCDCImpl
)
35 class wxNativePrinterDC
38 wxNativePrinterDC() {}
39 virtual ~wxNativePrinterDC() {}
40 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) = 0;
41 virtual void EndDoc( wxPrinterDC
* dc
) = 0;
42 virtual void StartPage( wxPrinterDC
* dc
) = 0;
43 virtual void EndPage( wxPrinterDC
* dc
) = 0;
44 virtual void GetSize( int *w
, int *h
) const = 0 ;
45 virtual wxSize
GetPPI() const = 0 ;
47 // returns 0 in case of no Error, otherwise platform specific error codes
48 virtual wxUint32
GetStatus() const = 0 ;
49 bool Ok() { return GetStatus() == 0 ; }
51 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
54 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
57 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
58 virtual ~wxMacCarbonPrinterDC() ;
59 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
60 virtual void EndDoc( wxPrinterDC
* dc
) ;
61 virtual void StartPage( wxPrinterDC
* dc
) ;
62 virtual void EndPage( wxPrinterDC
* dc
) ;
63 virtual wxUint32
GetStatus() const { return m_err
; }
64 virtual void GetSize( int *w
, int *h
) const ;
65 virtual wxSize
GetPPI() const ;
73 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
76 wxOSXPrintData
*native
= (wxOSXPrintData
*) data
->GetNativeData() ;
79 m_err
= PMGetAdjustedPageRect(native
->GetPageFormat(), &rPage
);
83 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
84 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
88 m_err
= PMSessionGetCurrentPrinter(native
->GetPrintSession(), &printer
);
91 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
92 if ( PMPrinterGetOutputResolution
!= NULL
)
95 m_err
= PMPrinterGetOutputResolution( printer
, native
->GetPrintSettings(), &res
) ;
96 if ( m_err
== -9589 /* kPMKeyNotFound */ )
99 res
.hRes
= res
.vRes
= 300;
106 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
107 m_err
= PMPrinterGetPrinterResolution(printer
, kPMCurrentValue
, &res
);
112 m_maxX
= wxCoord((double)m_maxX
* res
.hRes
/ 72.0);
113 m_maxY
= wxCoord((double)m_maxY
* res
.vRes
/ 72.0);
115 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
118 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
122 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
124 return new wxMacCarbonPrinterDC(data
) ;
127 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& message
)
132 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
133 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
135 PMPrintSettingsSetJobName(native
->GetPrintSettings(), wxCFStringRef(message
));
137 m_err
= PMSessionBeginCGDocumentNoDialog(native
->GetPrintSession(),
138 native
->GetPrintSettings(),
139 native
->GetPageFormat());
140 if ( m_err
!= noErr
)
144 m_err
= PMGetAdjustedPageRect(native
->GetPageFormat(), &rPage
);
145 if ( m_err
!= noErr
)
148 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
149 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
154 m_err
= PMSessionGetCurrentPrinter(native
->GetPrintSession(), &printer
);
157 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
158 if ( PMPrinterGetOutputResolution
!= NULL
)
160 m_err
= PMPrinterGetOutputResolution( printer
, native
->GetPrintSettings(), &res
) ;
161 if ( m_err
== -9589 /* kPMKeyNotFound */ )
164 res
.hRes
= res
.vRes
= 300;
170 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
171 m_err
= PMPrinterGetPrinterResolution(printer
, kPMCurrentValue
, &res
);
176 m_maxX
= wxCoord((double)m_maxX
* res
.hRes
/ 72.0);
177 m_maxY
= wxCoord((double)m_maxY
* res
.vRes
/ 72.0);
179 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
183 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
188 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
189 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
191 m_err
= PMSessionEndDocumentNoDialog(native
->GetPrintSession());
194 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
199 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
200 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
202 m_err
= PMSessionBeginPageNoDialog(native
->GetPrintSession(),
203 native
->GetPageFormat(),
206 CGContextRef pageContext
;
208 if ( m_err
== noErr
)
210 m_err
= PMSessionGetCGGraphicsContext(native
->GetPrintSession(),
214 if ( m_err
!= noErr
)
216 PMSessionEndPageNoDialog(native
->GetPrintSession());
217 PMSessionEndDocumentNoDialog(native
->GetPrintSession());
222 m_err
= PMGetAdjustedPaperRect( native
->GetPageFormat() , &paperRect
) ;
223 // make sure (0,0) is at the upper left of the printable area (wx conventions)
224 // Core Graphics initially has the lower left of the paper as 0,0
226 CGContextTranslateCTM( pageContext
, (CGFloat
) -paperRect
.left
, (CGFloat
) paperRect
.bottom
) ;
228 // since this is a non-critical error, we set the flag back
231 // Leopard deprecated PMSetResolution() which will not be available in 64 bit mode, so we avoid using it.
232 // To set the proper drawing resolution, the docs suggest the use of CGContextScaleCTM(), so here we go; as a
233 // consequence though, PMGetAdjustedPaperRect() and PMGetAdjustedPageRect() return unscaled rects, so we
234 // have to manually scale them later.
235 CGContextScaleCTM( pageContext
, 72.0 / (double)m_ppi
.x
, -72.0 / (double)m_ppi
.y
);
237 impl
->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext
) );
241 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
246 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
247 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
249 m_err
= PMSessionEndPageNoDialog(native
->GetPrintSession());
250 if ( m_err
!= noErr
)
252 PMSessionEndDocumentNoDialog(native
->GetPrintSession());
254 // the cg context we got when starting the page isn't valid anymore, so replace it
255 impl
->SetGraphicsContext( wxGraphicsContext::Create() );
258 void wxMacCarbonPrinterDC::GetSize( int *w
, int *h
) const
266 wxSize
wxMacCarbonPrinterDC::GetPPI() const
275 wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC
*owner
, const wxPrintData
& printdata
)
276 : wxGCDCImpl( owner
)
279 m_printData
= printdata
;
280 m_printData
.ConvertToNative() ;
281 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
282 if ( m_nativePrinterDC
)
284 m_ok
= m_nativePrinterDC
->Ok() ;
288 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
289 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
294 wxSize sz
= GetPPI();
295 m_mm_to_pix_x
= mm2inches
* sz
.x
;
296 m_mm_to_pix_y
= mm2inches
* sz
.y
;
298 // we need at least a measuring context because people start measuring before a page
299 // gets printed at all
300 SetGraphicsContext( wxGraphicsContext::Create() );
304 wxSize
wxPrinterDCImpl::GetPPI() const
306 return m_nativePrinterDC
->GetPPI() ;
309 wxPrinterDCImpl::~wxPrinterDCImpl()
311 delete m_nativePrinterDC
;
314 bool wxPrinterDCImpl::StartDoc( const wxString
& message
)
316 wxASSERT_MSG( IsOk() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
321 if ( m_nativePrinterDC
->StartDoc( (wxPrinterDC
*) GetOwner(), 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 wxPrinterDCImpl::EndDoc(void)
342 m_nativePrinterDC
->EndDoc( (wxPrinterDC
*) GetOwner() ) ;
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
wxPrinterDCImpl::GetPaperRect() const
357 GetOwner()->GetSize(&w
, &h
);
358 wxRect
pageRect(0, 0, w
, h
);
359 wxOSXPrintData
*native
= (wxOSXPrintData
*) m_printData
.GetNativeData() ;
360 OSStatus err
= noErr
;
362 err
= PMGetAdjustedPaperRect(native
->GetPageFormat(), &rPaper
);
366 wxSize ppi
= GetOwner()->GetPPI();
367 rPaper
.right
*= (ppi
.x
/ 72.0);
368 rPaper
.bottom
*= (ppi
.y
/ 72.0);
369 rPaper
.left
*= (ppi
.x
/ 72.0);
370 rPaper
.top
*= (ppi
.y
/ 72.0);
372 return wxRect(wxCoord(rPaper
.left
), wxCoord(rPaper
.top
),
373 wxCoord(rPaper
.right
- rPaper
.left
), wxCoord(rPaper
.bottom
- rPaper
.top
));
376 void wxPrinterDCImpl::StartPage()
381 m_logicalFunction
= wxCOPY
;
382 // m_textAlignment = wxALIGN_TOP_LEFT;
383 m_backgroundMode
= wxTRANSPARENT
;
385 m_textForegroundColour
= *wxBLACK
;
386 m_textBackgroundColour
= *wxWHITE
;
387 m_pen
= *wxBLACK_PEN
;
388 m_font
= *wxNORMAL_FONT
;
389 m_brush
= *wxTRANSPARENT_BRUSH
;
390 m_backgroundBrush
= *wxWHITE_BRUSH
;
392 m_nativePrinterDC
->StartPage( (wxPrinterDC
*) GetOwner() ) ;
393 m_ok
= m_nativePrinterDC
->Ok() ;
397 void wxPrinterDCImpl::EndPage()
402 m_nativePrinterDC
->EndPage( (wxPrinterDC
*) GetOwner() );
403 m_ok
= m_nativePrinterDC
->Ok() ;
406 void wxPrinterDCImpl::DoGetSize(int *width
, int *height
) const
408 wxCHECK_RET( m_ok
, wxT("GetSize() doesn't work without a valid wxPrinterDC") );
409 m_nativePrinterDC
->GetSize(width
, height
) ;