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/mac/carbon/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 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->GetNativeData() ;
79 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
83 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
84 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
87 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
89 PMSessionGetCurrentPrinter(native
->m_macPrintSession
, &printer
);
90 PMPrinterGetOutputResolution( printer
, native
->m_macPrintSettings
, &res
) ;
92 m_err
= PMGetResolution((PMPageFormat
) (native
->m_macPageFormat
), &res
);
94 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
97 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
101 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
103 return new wxMacCarbonPrinterDC(data
) ;
106 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
111 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
112 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) impl
->GetPrintData().GetNativeData() ;
114 m_err
= PMSessionBeginCGDocumentNoDialog(native
->m_macPrintSession
,
115 native
->m_macPrintSettings
,
116 native
->m_macPageFormat
);
117 if ( m_err
!= noErr
)
121 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
122 if ( m_err
!= noErr
)
125 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
126 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
129 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
130 if ( PMPrinterGetOutputResolution
!= NULL
)
133 m_err
= PMSessionGetCurrentPrinter(native
->m_macPrintSession
, &printer
);
134 if ( m_err
== noErr
)
135 m_err
= PMPrinterGetOutputResolution( printer
, native
->m_macPrintSettings
, &res
) ;
140 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
141 m_err
= PMGetResolution((PMPageFormat
) (native
->m_macPageFormat
), &res
);
145 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
149 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
154 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
155 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) impl
->GetPrintData().GetNativeData() ;
157 m_err
= PMSessionEndDocumentNoDialog(native
->m_macPrintSession
);
160 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
165 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
166 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) impl
->GetPrintData().GetNativeData() ;
168 m_err
= PMSessionBeginPageNoDialog(native
->m_macPrintSession
,
169 native
->m_macPageFormat
,
172 CGContextRef pageContext
;
174 if ( m_err
== noErr
)
176 m_err
= PMSessionGetCGGraphicsContext(native
->m_macPrintSession
,
180 if ( m_err
!= noErr
)
182 PMSessionEndPageNoDialog(native
->m_macPrintSession
);
183 PMSessionEndDocumentNoDialog(native
->m_macPrintSession
);
189 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
193 PMGetAdjustedPaperRect( native
->m_macPageFormat
, &paperRect
) ;
194 // make sure (0,0) is at the upper left of the printable area (wx conventions)
195 // Core Graphics initially has the lower left of the paper as 0,0
196 CGContextTranslateCTM( pageContext
, -paperRect
.left
, paperRect
.bottom
) ;
197 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
199 // since this is a non-critical error, we set the flag back
202 impl
->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext
) );
205 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
210 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
211 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) impl
->GetPrintData().GetNativeData() ;
213 m_err
= PMSessionEndPageNoDialog(native
->m_macPrintSession
);
214 if ( m_err
!= noErr
)
216 PMSessionEndDocumentNoDialog(native
->m_macPrintSession
);
218 // the cg context we got when starting the page isn't valid anymore, so replace it
219 impl
->SetGraphicsContext( wxGraphicsContext::Create() );
222 void wxMacCarbonPrinterDC::GetSize( int *w
, int *h
) const
230 wxSize
wxMacCarbonPrinterDC::GetPPI() const
239 wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC
*owner
, const wxPrintData
& printdata
)
240 : wxGCDCImpl( owner
)
243 m_printData
= printdata
;
244 m_printData
.ConvertToNative() ;
245 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
246 if ( m_nativePrinterDC
)
248 m_ok
= m_nativePrinterDC
->Ok() ;
252 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
253 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
258 wxSize sz
= GetPPI();
259 m_mm_to_pix_x
= mm2inches
* sz
.x
;
260 m_mm_to_pix_y
= mm2inches
* sz
.y
;
262 // we need at least a measuring context because people start measuring before a page
263 // gets printed at all
264 SetGraphicsContext( wxGraphicsContext::Create() );
268 wxSize
wxPrinterDCImpl::GetPPI() const
270 return m_nativePrinterDC
->GetPPI() ;
273 wxPrinterDCImpl::~wxPrinterDCImpl()
275 delete m_nativePrinterDC
;
278 bool wxPrinterDCImpl::StartDoc( const wxString
& message
)
280 wxASSERT_MSG( IsOk() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
285 if ( m_nativePrinterDC
->StartDoc( (wxPrinterDC
*) GetOwner(), message
) )
287 // in case we have to do additional things when successful
289 m_ok
= m_nativePrinterDC
->Ok() ;
293 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
294 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
301 void wxPrinterDCImpl::EndDoc(void)
306 m_nativePrinterDC
->EndDoc( (wxPrinterDC
*) GetOwner() ) ;
307 m_ok
= m_nativePrinterDC
->Ok() ;
312 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
313 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
318 wxRect
wxPrinterDCImpl::GetPaperRect()
321 GetOwner()->GetSize(&w
, &h
);
322 wxRect
pageRect(0, 0, w
, h
);
323 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) m_printData
.GetNativeData() ;
324 OSStatus err
= noErr
;
326 err
= PMGetAdjustedPaperRect(native
->m_macPageFormat
, &rPaper
);
329 return wxRect(wxCoord(rPaper
.left
), wxCoord(rPaper
.top
),
330 wxCoord(rPaper
.right
- rPaper
.left
), wxCoord(rPaper
.bottom
- rPaper
.top
));
333 void wxPrinterDCImpl::StartPage()
338 m_logicalFunction
= wxCOPY
;
339 // m_textAlignment = wxALIGN_TOP_LEFT;
340 m_backgroundMode
= wxTRANSPARENT
;
342 m_textForegroundColour
= *wxBLACK
;
343 m_textBackgroundColour
= *wxWHITE
;
344 m_pen
= *wxBLACK_PEN
;
345 m_font
= *wxNORMAL_FONT
;
346 m_brush
= *wxTRANSPARENT_BRUSH
;
347 m_backgroundBrush
= *wxWHITE_BRUSH
;
349 m_nativePrinterDC
->StartPage( (wxPrinterDC
*) GetOwner() ) ;
350 m_ok
= m_nativePrinterDC
->Ok() ;
354 void wxPrinterDCImpl::EndPage()
359 m_nativePrinterDC
->EndPage( (wxPrinterDC
*) GetOwner() );
360 m_ok
= m_nativePrinterDC
->Ok() ;
363 void wxPrinterDCImpl::DoGetSize(int *width
, int *height
) const
365 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
366 m_nativePrinterDC
->GetSize(width
, height
) ;