]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/dcprint.cpp
Update version to 2.9.4 in version.bkl too and rebake everything.
[wxWidgets.git] / src / osx / carbon / dcprint.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/private.h"
29 #include "wx/osx/private/print.h"
30 #include "wx/osx/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 IsOk() { 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 wxOSXPrintData *native = (wxOSXPrintData*) data->GetNativeData() ;
77
78 PMRect rPage;
79 m_err = PMGetAdjustedPageRect(native->GetPageFormat(), &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 PMPrinter printer;
88 m_err = PMSessionGetCurrentPrinter(native->GetPrintSession(), &printer);
89 if ( m_err == noErr )
90 {
91 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
92 if ( PMPrinterGetOutputResolution != NULL )
93 {
94 {
95 m_err = PMPrinterGetOutputResolution( printer, native->GetPrintSettings(), &res) ;
96 if ( m_err == -9589 /* kPMKeyNotFound */ )
97 {
98 m_err = noErr ;
99 res.hRes = res.vRes = 300;
100 }
101 }
102 }
103 else
104 #endif
105 {
106 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
107 m_err = PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &res);
108 if ( m_err != noErr )
109 {
110 m_err = PMGetResolution((PMPageFormat) (native->GetPageFormat()), &res);
111 }
112 #endif
113 }
114 }
115 else
116 {
117 res.hRes = res.vRes = 300;
118 }
119
120 m_maxX = wxCoord((double)m_maxX * res.hRes / 72.0);
121 m_maxY = wxCoord((double)m_maxY * res.vRes / 72.0);
122
123 m_ppi = wxSize(int(res.hRes), int(res.vRes));
124 }
125
126 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
127 {
128 }
129
130 wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
131 {
132 return new wxMacCarbonPrinterDC(data) ;
133 }
134
135 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& message )
136 {
137 if ( m_err )
138 return false ;
139
140 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
141 wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ;
142
143 PMPrintSettingsSetJobName(native->GetPrintSettings(), wxCFStringRef(message));
144
145 m_err = PMSessionBeginCGDocumentNoDialog(native->GetPrintSession(),
146 native->GetPrintSettings(),
147 native->GetPageFormat());
148 if ( m_err != noErr )
149 return false;
150
151 PMRect rPage;
152 m_err = PMGetAdjustedPageRect(native->GetPageFormat(), &rPage);
153 if ( m_err != noErr )
154 return false ;
155
156 m_maxX = wxCoord(rPage.right - rPage.left) ;
157 m_maxY = wxCoord(rPage.bottom - rPage.top);
158
159 PMResolution res;
160 PMPrinter printer;
161
162 m_err = PMSessionGetCurrentPrinter(native->GetPrintSession(), &printer);
163 if (m_err == noErr)
164 {
165 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
166 if ( PMPrinterGetOutputResolution != NULL )
167 {
168 m_err = PMPrinterGetOutputResolution( printer, native->GetPrintSettings(), &res) ;
169 if ( m_err == -9589 /* kPMKeyNotFound */ )
170 {
171 m_err = noErr ;
172 res.hRes = res.vRes = 300;
173 }
174 }
175 else
176 #endif
177 {
178 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
179 if ( PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &res) != noErr )
180 {
181 res.hRes = res.vRes = 300;
182 }
183 #endif
184 }
185 }
186
187 m_maxX = wxCoord((double)m_maxX * res.hRes / 72.0);
188 m_maxY = wxCoord((double)m_maxY * res.vRes / 72.0);
189
190 m_ppi = wxSize(int(res.hRes), int(res.vRes));
191 return true ;
192 }
193
194 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
195 {
196 if ( m_err )
197 return ;
198
199 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
200 wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ;
201
202 m_err = PMSessionEndDocumentNoDialog(native->GetPrintSession());
203 }
204
205 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
206 {
207 if ( m_err )
208 return ;
209
210 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
211 wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ;
212
213 m_err = PMSessionBeginPageNoDialog(native->GetPrintSession(),
214 native->GetPageFormat(),
215 NULL);
216
217 CGContextRef pageContext;
218
219 if ( m_err == noErr )
220 {
221 m_err = PMSessionGetCGGraphicsContext(native->GetPrintSession(),
222 &pageContext );
223 }
224
225 if ( m_err != noErr )
226 {
227 PMSessionEndPageNoDialog(native->GetPrintSession());
228 PMSessionEndDocumentNoDialog(native->GetPrintSession());
229 }
230 else
231 {
232 PMRect paperRect ;
233 m_err = PMGetAdjustedPaperRect( native->GetPageFormat() , &paperRect ) ;
234 // make sure (0,0) is at the upper left of the printable area (wx conventions)
235 // Core Graphics initially has the lower left of the paper as 0,0
236 if ( !m_err )
237 CGContextTranslateCTM( pageContext , (CGFloat) -paperRect.left , (CGFloat) paperRect.bottom ) ;
238
239 // since this is a non-critical error, we set the flag back
240 m_err = noErr ;
241
242 // Leopard deprecated PMSetResolution() which will not be available in 64 bit mode, so we avoid using it.
243 // To set the proper drawing resolution, the docs suggest the use of CGContextScaleCTM(), so here we go; as a
244 // consequence though, PMGetAdjustedPaperRect() and PMGetAdjustedPageRect() return unscaled rects, so we
245 // have to manually scale them later.
246 CGContextScaleCTM( pageContext, 72.0 / (double)m_ppi.x, -72.0 / (double)m_ppi.y);
247
248 impl->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext ) );
249 }
250 }
251
252 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
253 {
254 if ( m_err )
255 return ;
256
257 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
258 wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ;
259
260 m_err = PMSessionEndPageNoDialog(native->GetPrintSession());
261 if ( m_err != noErr )
262 {
263 PMSessionEndDocumentNoDialog(native->GetPrintSession());
264 }
265 // the cg context we got when starting the page isn't valid anymore, so replace it
266 impl->SetGraphicsContext( wxGraphicsContext::Create() );
267 }
268
269 void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const
270 {
271 if ( w )
272 *w = m_maxX ;
273 if ( h )
274 *h = m_maxY ;
275 }
276
277 wxSize wxMacCarbonPrinterDC::GetPPI() const
278 {
279 return m_ppi ;
280 };
281
282 //
283 //
284 //
285
286 wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC *owner, const wxPrintData& printdata )
287 : wxGCDCImpl( owner )
288 {
289 m_ok = false ;
290 m_printData = printdata ;
291 m_printData.ConvertToNative() ;
292 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
293 if ( m_nativePrinterDC )
294 {
295 m_ok = m_nativePrinterDC->IsOk() ;
296 if ( !m_ok )
297 {
298 wxString message ;
299 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
300 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
301 dialog.ShowModal();
302 }
303 else
304 {
305 wxSize sz = GetPPI();
306 m_mm_to_pix_x = mm2inches * sz.x;
307 m_mm_to_pix_y = mm2inches * sz.y;
308 }
309 // we need at least a measuring context because people start measuring before a page
310 // gets printed at all
311 SetGraphicsContext( wxGraphicsContext::Create() );
312 }
313 }
314
315 wxSize wxPrinterDCImpl::GetPPI() const
316 {
317 return m_nativePrinterDC->GetPPI() ;
318 }
319
320 wxPrinterDCImpl::~wxPrinterDCImpl()
321 {
322 delete m_nativePrinterDC ;
323 }
324
325 bool wxPrinterDCImpl::StartDoc( const wxString& message )
326 {
327 wxASSERT_MSG( IsOk() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
328
329 if ( !m_ok )
330 return false ;
331
332 if ( m_nativePrinterDC->StartDoc( (wxPrinterDC*) GetOwner(), message ) )
333 {
334 // in case we have to do additional things when successful
335 }
336 m_ok = m_nativePrinterDC->IsOk() ;
337 if ( !m_ok )
338 {
339 wxString message ;
340 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
341 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
342 dialog.ShowModal();
343 }
344
345 return m_ok ;
346 }
347
348 void wxPrinterDCImpl::EndDoc(void)
349 {
350 if ( !m_ok )
351 return ;
352
353 m_nativePrinterDC->EndDoc( (wxPrinterDC*) GetOwner() ) ;
354 m_ok = m_nativePrinterDC->IsOk() ;
355
356 if ( !m_ok )
357 {
358 wxString message ;
359 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
360 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
361 dialog.ShowModal();
362 }
363 }
364
365 wxRect wxPrinterDCImpl::GetPaperRect() const
366 {
367 wxCoord w, h;
368 GetOwner()->GetSize(&w, &h);
369 wxRect pageRect(0, 0, w, h);
370 wxOSXPrintData *native = (wxOSXPrintData*) m_printData.GetNativeData() ;
371 OSStatus err = noErr ;
372 PMRect rPaper;
373 err = PMGetAdjustedPaperRect(native->GetPageFormat(), &rPaper);
374 if ( err != noErr )
375 return pageRect;
376
377 wxSize ppi = GetOwner()->GetPPI();
378 rPaper.right *= (ppi.x / 72.0);
379 rPaper.bottom *= (ppi.y / 72.0);
380 rPaper.left *= (ppi.x / 72.0);
381 rPaper.top *= (ppi.y / 72.0);
382
383 return wxRect(wxCoord(rPaper.left), wxCoord(rPaper.top),
384 wxCoord(rPaper.right - rPaper.left), wxCoord(rPaper.bottom - rPaper.top));
385 }
386
387 void wxPrinterDCImpl::StartPage()
388 {
389 if ( !m_ok )
390 return ;
391
392 m_logicalFunction = wxCOPY;
393 // m_textAlignment = wxALIGN_TOP_LEFT;
394 m_backgroundMode = wxTRANSPARENT;
395
396 m_textForegroundColour = *wxBLACK;
397 m_textBackgroundColour = *wxWHITE;
398 m_pen = *wxBLACK_PEN;
399 m_font = *wxNORMAL_FONT;
400 m_brush = *wxTRANSPARENT_BRUSH;
401 m_backgroundBrush = *wxWHITE_BRUSH;
402
403 m_nativePrinterDC->StartPage( (wxPrinterDC*) GetOwner() ) ;
404 m_ok = m_nativePrinterDC->IsOk() ;
405
406 }
407
408 void wxPrinterDCImpl::EndPage()
409 {
410 if ( !m_ok )
411 return ;
412
413 m_nativePrinterDC->EndPage( (wxPrinterDC*) GetOwner() );
414 m_ok = m_nativePrinterDC->IsOk() ;
415 }
416
417 void wxPrinterDCImpl::DoGetSize(int *width, int *height) const
418 {
419 wxCHECK_RET( m_ok , wxT("GetSize() doesn't work without a valid wxPrinterDC") );
420 m_nativePrinterDC->GetSize(width, height ) ;
421 }
422
423 #endif