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 ;
72 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
75 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->GetNativeData() ;
78 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
82 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
83 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
86 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
88 PMSessionGetCurrentPrinter(native
->m_macPrintSession
, &printer
);
89 PMPrinterGetOutputResolution( printer
, native
->m_macPrintSettings
, &res
) ;
91 m_err
= PMGetResolution((PMPageFormat
) (native
->m_macPageFormat
), &res
);
93 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
96 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
100 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
102 return new wxMacCarbonPrinterDC(data
) ;
105 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
110 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
112 m_err
= PMSessionBeginCGDocumentNoDialog(native
->m_macPrintSession
,
113 native
->m_macPrintSettings
,
114 native
->m_macPageFormat
);
115 if ( m_err
!= noErr
)
119 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
120 if ( m_err
!= noErr
)
123 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
124 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
127 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
128 if ( PMPrinterGetOutputResolution
!= NULL
)
131 m_err
= PMSessionGetCurrentPrinter(native
->m_macPrintSession
, &printer
);
132 if ( m_err
== noErr
)
133 m_err
= PMPrinterGetOutputResolution( printer
, native
->m_macPrintSettings
, &res
) ;
138 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
139 m_err
= PMGetResolution((PMPageFormat
) (native
->m_macPageFormat
), &res
);
143 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
147 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
152 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
154 m_err
= PMSessionEndDocumentNoDialog(native
->m_macPrintSession
);
157 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
162 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
164 m_err
= PMSessionBeginPageNoDialog(native
->m_macPrintSession
,
165 native
->m_macPageFormat
,
168 CGContextRef pageContext
;
170 if ( m_err
== noErr
)
172 m_err
= PMSessionGetCGGraphicsContext(native
->m_macPrintSession
,
176 if ( m_err
!= noErr
)
178 PMSessionEndPageNoDialog(native
->m_macPrintSession
);
179 PMSessionEndDocumentNoDialog(native
->m_macPrintSession
);
185 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
189 PMGetAdjustedPaperRect( native
->m_macPageFormat
, &paperRect
) ;
190 // make sure (0,0) is at the upper left of the printable area (wx conventions)
191 // Core Graphics initially has the lower left of the paper as 0,0
192 CGContextTranslateCTM( pageContext
, -paperRect
.left
, paperRect
.bottom
) ;
193 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
195 // since this is a non-critical error, we set the flag back
198 dc
->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext
) );
201 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
206 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
208 m_err
= PMSessionEndPageNoDialog(native
->m_macPrintSession
);
209 if ( m_err
!= noErr
)
211 PMSessionEndDocumentNoDialog(native
->m_macPrintSession
);
213 // the cg context we got when starting the page isn't valid anymore, so replace it
214 dc
->SetGraphicsContext( wxGraphicsContext::Create() );
217 void wxMacCarbonPrinterDC::GetSize( int *w
, int *h
) const
225 wxSize
wxMacCarbonPrinterDC::GetPPI() const
234 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
237 m_printData
= printdata
;
238 m_printData
.ConvertToNative() ;
239 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
240 if ( m_nativePrinterDC
)
242 m_ok
= m_nativePrinterDC
->Ok() ;
246 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
247 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
252 wxSize sz
= GetPPI();
253 m_mm_to_pix_x
= mm2inches
* sz
.x
;
254 m_mm_to_pix_y
= mm2inches
* sz
.y
;
256 // we need at least a measuring context because people start measuring before a page
257 // gets printed at all
258 SetGraphicsContext( wxGraphicsContext::Create() );
262 wxSize
wxPrinterDC::GetPPI() const
264 return m_nativePrinterDC
->GetPPI() ;
267 wxPrinterDC::~wxPrinterDC(void)
269 delete m_nativePrinterDC
;
272 bool wxPrinterDC::StartDoc( const wxString
& message
)
274 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
279 if ( m_nativePrinterDC
->StartDoc(this, message
) )
281 // in case we have to do additional things when successful
283 m_ok
= m_nativePrinterDC
->Ok() ;
287 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
288 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
295 void wxPrinterDC::EndDoc(void)
300 m_nativePrinterDC
->EndDoc( this ) ;
301 m_ok
= m_nativePrinterDC
->Ok() ;
306 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
307 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
312 wxRect
wxPrinterDC::GetPaperRect()
316 wxRect
pageRect(0, 0, w
, h
);
317 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) m_printData
.GetNativeData() ;
318 OSStatus err
= noErr
;
320 err
= PMGetAdjustedPaperRect(native
->m_macPageFormat
, &rPaper
);
323 return wxRect(wxCoord(rPaper
.left
), wxCoord(rPaper
.top
),
324 wxCoord(rPaper
.right
- rPaper
.left
), wxCoord(rPaper
.bottom
- rPaper
.top
));
327 void wxPrinterDC::StartPage(void)
332 m_logicalFunction
= wxCOPY
;
333 // m_textAlignment = wxALIGN_TOP_LEFT;
334 m_backgroundMode
= wxTRANSPARENT
;
336 m_textForegroundColour
= *wxBLACK
;
337 m_textBackgroundColour
= *wxWHITE
;
338 m_pen
= *wxBLACK_PEN
;
339 m_font
= *wxNORMAL_FONT
;
340 m_brush
= *wxTRANSPARENT_BRUSH
;
341 m_backgroundBrush
= *wxWHITE_BRUSH
;
343 m_nativePrinterDC
->StartPage(this) ;
344 m_ok
= m_nativePrinterDC
->Ok() ;
348 void wxPrinterDC::EndPage(void)
353 m_nativePrinterDC
->EndPage(this) ;
354 m_ok
= m_nativePrinterDC
->Ok() ;
357 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
359 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
360 m_nativePrinterDC
->GetSize(width
, height
) ;