]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/mac/carbon/dcprint.cpp | |
3 | // Purpose: wxPrinterDC class | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #if wxUSE_PRINTING_ARCHITECTURE | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
21 | #include "wx/dcprint.h" | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/msgdlg.h" | |
25 | #include "wx/math.h" | |
26 | #endif | |
27 | ||
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" | |
32 | ||
33 | IMPLEMENT_ABSTRACT_CLASS(wxPrinterDCImpl, wxGCDCImpl) | |
34 | ||
35 | class wxNativePrinterDC | |
36 | { | |
37 | public : | |
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 ; | |
46 | ||
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 ; } | |
50 | ||
51 | static wxNativePrinterDC* Create(wxPrintData* data) ; | |
52 | } ; | |
53 | ||
54 | class wxMacCarbonPrinterDC : public wxNativePrinterDC | |
55 | { | |
56 | public : | |
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 ; | |
66 | private : | |
67 | wxCoord m_maxX ; | |
68 | wxCoord m_maxY ; | |
69 | wxSize m_ppi ; | |
70 | OSStatus m_err ; | |
71 | } ; | |
72 | ||
73 | wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data ) | |
74 | { | |
75 | m_err = noErr ; | |
76 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ; | |
77 | ||
78 | PMRect rPage; | |
79 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); | |
80 | if ( m_err != noErr ) | |
81 | return; | |
82 | ||
83 | m_maxX = wxCoord(rPage.right - rPage.left) ; | |
84 | m_maxY = wxCoord(rPage.bottom - rPage.top); | |
85 | ||
86 | PMResolution res; | |
87 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 | |
88 | PMPrinter printer; | |
89 | PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer); | |
90 | PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ; | |
91 | #else | |
92 | m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res); | |
93 | #endif | |
94 | m_ppi = wxSize(int(res.hRes), int(res.vRes)); | |
95 | } | |
96 | ||
97 | wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC() | |
98 | { | |
99 | } | |
100 | ||
101 | wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data) | |
102 | { | |
103 | return new wxMacCarbonPrinterDC(data) ; | |
104 | } | |
105 | ||
106 | bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) ) | |
107 | { | |
108 | if ( m_err ) | |
109 | return false ; | |
110 | ||
111 | wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); | |
112 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ; | |
113 | ||
114 | m_err = PMSessionBeginCGDocumentNoDialog(native->m_macPrintSession, | |
115 | native->m_macPrintSettings, | |
116 | native->m_macPageFormat); | |
117 | if ( m_err != noErr ) | |
118 | return false; | |
119 | ||
120 | PMRect rPage; | |
121 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); | |
122 | if ( m_err != noErr ) | |
123 | return false ; | |
124 | ||
125 | m_maxX = wxCoord(rPage.right - rPage.left) ; | |
126 | m_maxY = wxCoord(rPage.bottom - rPage.top); | |
127 | ||
128 | PMResolution res; | |
129 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 | |
130 | if ( PMPrinterGetOutputResolution != NULL ) | |
131 | { | |
132 | PMPrinter printer; | |
133 | m_err = PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer); | |
134 | if ( m_err == noErr ) | |
135 | m_err = PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ; | |
136 | } | |
137 | else | |
138 | #endif | |
139 | { | |
140 | #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | |
141 | m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res); | |
142 | #endif | |
143 | } | |
144 | ||
145 | m_ppi = wxSize(int(res.hRes), int(res.vRes)); | |
146 | return true ; | |
147 | } | |
148 | ||
149 | void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc ) | |
150 | { | |
151 | if ( m_err ) | |
152 | return ; | |
153 | ||
154 | wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); | |
155 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ; | |
156 | ||
157 | m_err = PMSessionEndDocumentNoDialog(native->m_macPrintSession); | |
158 | } | |
159 | ||
160 | void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc ) | |
161 | { | |
162 | if ( m_err ) | |
163 | return ; | |
164 | ||
165 | wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); | |
166 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ; | |
167 | ||
168 | m_err = PMSessionBeginPageNoDialog(native->m_macPrintSession, | |
169 | native->m_macPageFormat, | |
170 | nil); | |
171 | ||
172 | CGContextRef pageContext; | |
173 | ||
174 | if ( m_err == noErr ) | |
175 | { | |
176 | m_err = PMSessionGetCGGraphicsContext(native->m_macPrintSession, | |
177 | &pageContext ); | |
178 | } | |
179 | ||
180 | if ( m_err != noErr ) | |
181 | { | |
182 | PMSessionEndPageNoDialog(native->m_macPrintSession); | |
183 | PMSessionEndDocumentNoDialog(native->m_macPrintSession); | |
184 | } | |
185 | else | |
186 | { | |
187 | PMRect rPage; | |
188 | ||
189 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); | |
190 | if ( !m_err ) | |
191 | { | |
192 | PMRect paperRect ; | |
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 ) ; | |
198 | } | |
199 | // since this is a non-critical error, we set the flag back | |
200 | m_err = noErr ; | |
201 | } | |
202 | impl->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext ) ); | |
203 | } | |
204 | ||
205 | void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc ) | |
206 | { | |
207 | if ( m_err ) | |
208 | return ; | |
209 | ||
210 | wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); | |
211 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ; | |
212 | ||
213 | m_err = PMSessionEndPageNoDialog(native->m_macPrintSession); | |
214 | if ( m_err != noErr ) | |
215 | { | |
216 | PMSessionEndDocumentNoDialog(native->m_macPrintSession); | |
217 | } | |
218 | // the cg context we got when starting the page isn't valid anymore, so replace it | |
219 | impl->SetGraphicsContext( wxGraphicsContext::Create() ); | |
220 | } | |
221 | ||
222 | void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const | |
223 | { | |
224 | if ( w ) | |
225 | *w = m_maxX ; | |
226 | if ( h ) | |
227 | *h = m_maxY ; | |
228 | } | |
229 | ||
230 | wxSize wxMacCarbonPrinterDC::GetPPI() const | |
231 | { | |
232 | return m_ppi ; | |
233 | }; | |
234 | ||
235 | // | |
236 | // | |
237 | // | |
238 | ||
239 | wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC *owner, const wxPrintData& printdata ) | |
240 | : wxGCDCImpl( owner ) | |
241 | { | |
242 | m_ok = false ; | |
243 | m_printData = printdata ; | |
244 | m_printData.ConvertToNative() ; | |
245 | m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ; | |
246 | if ( m_nativePrinterDC ) | |
247 | { | |
248 | m_ok = m_nativePrinterDC->Ok() ; | |
249 | if ( !m_ok ) | |
250 | { | |
251 | wxString message ; | |
252 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; | |
253 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; | |
254 | dialog.ShowModal(); | |
255 | } | |
256 | else | |
257 | { | |
258 | wxSize sz = GetPPI(); | |
259 | m_mm_to_pix_x = mm2inches * sz.x; | |
260 | m_mm_to_pix_y = mm2inches * sz.y; | |
261 | } | |
262 | // we need at least a measuring context because people start measuring before a page | |
263 | // gets printed at all | |
264 | SetGraphicsContext( wxGraphicsContext::Create() ); | |
265 | } | |
266 | } | |
267 | ||
268 | wxSize wxPrinterDCImpl::GetPPI() const | |
269 | { | |
270 | return m_nativePrinterDC->GetPPI() ; | |
271 | } | |
272 | ||
273 | wxPrinterDCImpl::~wxPrinterDCImpl() | |
274 | { | |
275 | delete m_nativePrinterDC ; | |
276 | } | |
277 | ||
278 | bool wxPrinterDCImpl::StartDoc( const wxString& message ) | |
279 | { | |
280 | wxASSERT_MSG( IsOk() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ; | |
281 | ||
282 | if ( !m_ok ) | |
283 | return false ; | |
284 | ||
285 | if ( m_nativePrinterDC->StartDoc( (wxPrinterDC*) GetOwner(), message ) ) | |
286 | { | |
287 | // in case we have to do additional things when successful | |
288 | } | |
289 | m_ok = m_nativePrinterDC->Ok() ; | |
290 | if ( !m_ok ) | |
291 | { | |
292 | wxString message ; | |
293 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; | |
294 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; | |
295 | dialog.ShowModal(); | |
296 | } | |
297 | ||
298 | return m_ok ; | |
299 | } | |
300 | ||
301 | void wxPrinterDCImpl::EndDoc(void) | |
302 | { | |
303 | if ( !m_ok ) | |
304 | return ; | |
305 | ||
306 | m_nativePrinterDC->EndDoc( (wxPrinterDC*) GetOwner() ) ; | |
307 | m_ok = m_nativePrinterDC->Ok() ; | |
308 | ||
309 | if ( !m_ok ) | |
310 | { | |
311 | wxString message ; | |
312 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; | |
313 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; | |
314 | dialog.ShowModal(); | |
315 | } | |
316 | } | |
317 | ||
318 | wxRect wxPrinterDCImpl::GetPaperRect() | |
319 | { | |
320 | wxCoord w, h; | |
321 | GetOwner()->GetSize(&w, &h); | |
322 | wxRect pageRect(0, 0, w, h); | |
323 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) m_printData.GetNativeData() ; | |
324 | OSStatus err = noErr ; | |
325 | PMRect rPaper; | |
326 | err = PMGetAdjustedPaperRect(native->m_macPageFormat, &rPaper); | |
327 | if ( err != noErr ) | |
328 | return pageRect; | |
329 | return wxRect(wxCoord(rPaper.left), wxCoord(rPaper.top), | |
330 | wxCoord(rPaper.right - rPaper.left), wxCoord(rPaper.bottom - rPaper.top)); | |
331 | } | |
332 | ||
333 | void wxPrinterDCImpl::StartPage() | |
334 | { | |
335 | if ( !m_ok ) | |
336 | return ; | |
337 | ||
338 | m_logicalFunction = wxCOPY; | |
339 | // m_textAlignment = wxALIGN_TOP_LEFT; | |
340 | m_backgroundMode = wxTRANSPARENT; | |
341 | ||
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; | |
348 | ||
349 | m_nativePrinterDC->StartPage( (wxPrinterDC*) GetOwner() ) ; | |
350 | m_ok = m_nativePrinterDC->Ok() ; | |
351 | ||
352 | } | |
353 | ||
354 | void wxPrinterDCImpl::EndPage() | |
355 | { | |
356 | if ( !m_ok ) | |
357 | return ; | |
358 | ||
359 | m_nativePrinterDC->EndPage( (wxPrinterDC*) GetOwner() ); | |
360 | m_ok = m_nativePrinterDC->Ok() ; | |
361 | } | |
362 | ||
363 | void wxPrinterDCImpl::DoGetSize(int *width, int *height) const | |
364 | { | |
365 | wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") ); | |
366 | m_nativePrinterDC->GetSize(width, height ) ; | |
367 | } | |
368 | ||
369 | #endif |