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 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
89 if ( PMPrinterGetOutputResolution
!= NULL
)
92 m_err
= PMSessionGetCurrentPrinter(native
->GetPrintSession(), &printer
);
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
= PMGetResolution((PMPageFormat
) (native
->GetPageFormat()), &res
);
111 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
114 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
118 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
120 return new wxMacCarbonPrinterDC(data
) ;
123 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& message
)
128 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
129 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
131 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
132 if ( PMPrintSettingsSetJobName
!= NULL
)
133 PMPrintSettingsSetJobName(native
->GetPrintSettings(), wxCFStringRef(message
));
136 m_err
= PMSessionBeginCGDocumentNoDialog(native
->GetPrintSession(),
137 native
->GetPrintSettings(),
138 native
->GetPageFormat());
139 if ( m_err
!= noErr
)
143 m_err
= PMGetAdjustedPageRect(native
->GetPageFormat(), &rPage
);
144 if ( m_err
!= noErr
)
147 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
148 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
151 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
152 if ( PMPrinterGetOutputResolution
!= NULL
)
155 m_err
= PMSessionGetCurrentPrinter(native
->GetPrintSession(), &printer
);
156 if ( m_err
== noErr
)
158 m_err
= PMPrinterGetOutputResolution( printer
, native
->GetPrintSettings(), &res
) ;
159 if ( m_err
== -9589 /* kPMKeyNotFound */ )
162 res
.hRes
= res
.vRes
= 300;
169 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
170 m_err
= PMGetResolution((PMPageFormat
) (native
->GetPageFormat()), &res
);
174 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
178 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
183 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
184 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
186 m_err
= PMSessionEndDocumentNoDialog(native
->GetPrintSession());
189 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
194 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
195 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
197 m_err
= PMSessionBeginPageNoDialog(native
->GetPrintSession(),
198 native
->GetPageFormat(),
201 CGContextRef pageContext
;
203 if ( m_err
== noErr
)
205 m_err
= PMSessionGetCGGraphicsContext(native
->GetPrintSession(),
209 if ( m_err
!= noErr
)
211 PMSessionEndPageNoDialog(native
->GetPrintSession());
212 PMSessionEndDocumentNoDialog(native
->GetPrintSession());
218 m_err
= PMGetAdjustedPageRect(native
->GetPageFormat(), &rPage
);
222 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
225 CGContextTranslateCTM( pageContext
, (CGFloat
) -paperRect
.left
, (CGFloat
) paperRect
.bottom
) ;
226 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
228 // since this is a non-critical error, we set the flag back
231 impl
->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext
) );
234 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
239 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
240 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
242 m_err
= PMSessionEndPageNoDialog(native
->GetPrintSession());
243 if ( m_err
!= noErr
)
245 PMSessionEndDocumentNoDialog(native
->GetPrintSession());
247 // the cg context we got when starting the page isn't valid anymore, so replace it
248 impl
->SetGraphicsContext( wxGraphicsContext::Create() );
251 void wxMacCarbonPrinterDC::GetSize( int *w
, int *h
) const
259 wxSize
wxMacCarbonPrinterDC::GetPPI() const
268 wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC
*owner
, const wxPrintData
& printdata
)
269 : wxGCDCImpl( owner
)
272 m_printData
= printdata
;
273 m_printData
.ConvertToNative() ;
274 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
275 if ( m_nativePrinterDC
)
277 m_ok
= m_nativePrinterDC
->Ok() ;
281 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
282 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
287 wxSize sz
= GetPPI();
288 m_mm_to_pix_x
= mm2inches
* sz
.x
;
289 m_mm_to_pix_y
= mm2inches
* sz
.y
;
291 // we need at least a measuring context because people start measuring before a page
292 // gets printed at all
293 SetGraphicsContext( wxGraphicsContext::Create() );
297 wxSize
wxPrinterDCImpl::GetPPI() const
299 return m_nativePrinterDC
->GetPPI() ;
302 wxPrinterDCImpl::~wxPrinterDCImpl()
304 delete m_nativePrinterDC
;
307 bool wxPrinterDCImpl::StartDoc( const wxString
& message
)
309 wxASSERT_MSG( IsOk() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
314 if ( m_nativePrinterDC
->StartDoc( (wxPrinterDC
*) GetOwner(), message
) )
316 // in case we have to do additional things when successful
318 m_ok
= m_nativePrinterDC
->Ok() ;
322 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
323 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
330 void wxPrinterDCImpl::EndDoc(void)
335 m_nativePrinterDC
->EndDoc( (wxPrinterDC
*) GetOwner() ) ;
336 m_ok
= m_nativePrinterDC
->Ok() ;
341 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
342 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
347 wxRect
wxPrinterDCImpl::GetPaperRect() const
350 GetOwner()->GetSize(&w
, &h
);
351 wxRect
pageRect(0, 0, w
, h
);
352 wxOSXPrintData
*native
= (wxOSXPrintData
*) m_printData
.GetNativeData() ;
353 OSStatus err
= noErr
;
355 err
= PMGetAdjustedPaperRect(native
->GetPageFormat(), &rPaper
);
358 return wxRect(wxCoord(rPaper
.left
), wxCoord(rPaper
.top
),
359 wxCoord(rPaper
.right
- rPaper
.left
), wxCoord(rPaper
.bottom
- rPaper
.top
));
362 void wxPrinterDCImpl::StartPage()
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
;
378 m_nativePrinterDC
->StartPage( (wxPrinterDC
*) GetOwner() ) ;
379 m_ok
= m_nativePrinterDC
->Ok() ;
383 void wxPrinterDCImpl::EndPage()
388 m_nativePrinterDC
->EndPage( (wxPrinterDC
*) GetOwner() );
389 m_ok
= m_nativePrinterDC
->Ok() ;
392 void wxPrinterDCImpl::DoGetSize(int *width
, int *height
) const
394 wxCHECK_RET( m_ok
, wxT("GetSize() doesn't work without a valid wxPrinterDC") );
395 m_nativePrinterDC
->GetSize(width
, height
) ;