]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/dcprint.cpp
Also accept VT_R4 (float) VARIANTs in wxMSW OLE code.
[wxWidgets.git] / src / osx / carbon / dcprint.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/dcprint.cpp
489468fe
SC
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
524c47aa 28#include "wx/osx/private.h"
1f0c8f31 29#include "wx/osx/private/print.h"
e158da36 30#include "wx/osx/dcprint.h"
489468fe
SC
31#include "wx/graphics.h"
32
33IMPLEMENT_ABSTRACT_CLASS(wxPrinterDCImpl, wxGCDCImpl)
34
35class wxNativePrinterDC
36{
37public :
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 ;
a1b806b9 49 bool IsOk() { return GetStatus() == 0 ; }
489468fe
SC
50
51 static wxNativePrinterDC* Create(wxPrintData* data) ;
52} ;
53
54class wxMacCarbonPrinterDC : public wxNativePrinterDC
55{
56public :
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 ;
66private :
67 wxCoord m_maxX ;
68 wxCoord m_maxY ;
69 wxSize m_ppi ;
70 OSStatus m_err ;
71} ;
72
73wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
74{
75 m_err = noErr ;
c347101b 76 wxOSXPrintData *native = (wxOSXPrintData*) data->GetNativeData() ;
ce00f59b 77
489468fe 78 PMRect rPage;
c347101b 79 m_err = PMGetAdjustedPageRect(native->GetPageFormat(), &rPage);
489468fe
SC
80 if ( m_err != noErr )
81 return;
ce00f59b 82
489468fe
SC
83 m_maxX = wxCoord(rPage.right - rPage.left) ;
84 m_maxY = wxCoord(rPage.bottom - rPage.top);
ce00f59b 85
489468fe 86 PMResolution res;
884dad83
SC
87 PMPrinter printer;
88 m_err = PMSessionGetCurrentPrinter(native->GetPrintSession(), &printer);
89 if ( m_err == noErr )
ce00f59b 90 {
6d52ca53 91#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
884dad83 92 if ( PMPrinterGetOutputResolution != NULL )
489468fe 93 {
489468fe 94 {
884dad83
SC
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 }
489468fe
SC
101 }
102 }
884dad83 103 else
489468fe 104#endif
884dad83 105 {
6d52ca53 106#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
884dad83 107 m_err = PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &res);
4b77e661
SC
108 if ( m_err != noErr )
109 {
110 m_err = PMGetResolution((PMPageFormat) (native->GetPageFormat()), &res);
111 }
489468fe 112#endif
884dad83 113 }
489468fe 114 }
dcc5dc59
SC
115 else
116 {
117 res.hRes = res.vRes = 300;
118 }
ce00f59b 119
884dad83
SC
120 m_maxX = wxCoord((double)m_maxX * res.hRes / 72.0);
121 m_maxY = wxCoord((double)m_maxY * res.vRes / 72.0);
6d52ca53 122
489468fe
SC
123 m_ppi = wxSize(int(res.hRes), int(res.vRes));
124}
125
126wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
127{
128}
129
130wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
131{
132 return new wxMacCarbonPrinterDC(data) ;
133}
134
e9e767f1 135bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& message )
489468fe
SC
136{
137 if ( m_err )
138 return false ;
139
140 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
c347101b 141 wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ;
489468fe 142
884dad83 143 PMPrintSettingsSetJobName(native->GetPrintSettings(), wxCFStringRef(message));
e9e767f1 144
c347101b
SC
145 m_err = PMSessionBeginCGDocumentNoDialog(native->GetPrintSession(),
146 native->GetPrintSettings(),
147 native->GetPageFormat());
489468fe
SC
148 if ( m_err != noErr )
149 return false;
150
151 PMRect rPage;
c347101b 152 m_err = PMGetAdjustedPageRect(native->GetPageFormat(), &rPage);
489468fe
SC
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;
884dad83
SC
160 PMPrinter printer;
161
162 m_err = PMSessionGetCurrentPrinter(native->GetPrintSession(), &printer);
163 if (m_err == noErr)
489468fe 164 {
884dad83
SC
165#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
166 if ( PMPrinterGetOutputResolution != NULL )
489468fe 167 {
c347101b 168 m_err = PMPrinterGetOutputResolution( printer, native->GetPrintSettings(), &res) ;
489468fe
SC
169 if ( m_err == -9589 /* kPMKeyNotFound */ )
170 {
171 m_err = noErr ;
172 res.hRes = res.vRes = 300;
173 }
174 }
884dad83 175 else
489468fe 176#endif
884dad83 177 {
6d52ca53 178#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
4b77e661
SC
179 if ( PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &res) != noErr )
180 {
181 res.hRes = res.vRes = 300;
182 }
489468fe 183#endif
884dad83 184 }
489468fe 185 }
ce00f59b 186
884dad83
SC
187 m_maxX = wxCoord((double)m_maxX * res.hRes / 72.0);
188 m_maxY = wxCoord((double)m_maxY * res.vRes / 72.0);
489468fe
SC
189
190 m_ppi = wxSize(int(res.hRes), int(res.vRes));
191 return true ;
192}
193
194void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
195{
196 if ( m_err )
197 return ;
198
199 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
c347101b 200 wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ;
489468fe 201
c347101b 202 m_err = PMSessionEndDocumentNoDialog(native->GetPrintSession());
489468fe
SC
203}
204
205void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
206{
207 if ( m_err )
208 return ;
209
210 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
c347101b 211 wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ;
489468fe 212
c347101b
SC
213 m_err = PMSessionBeginPageNoDialog(native->GetPrintSession(),
214 native->GetPageFormat(),
884dad83 215 NULL);
489468fe
SC
216
217 CGContextRef pageContext;
218
219 if ( m_err == noErr )
220 {
c347101b 221 m_err = PMSessionGetCGGraphicsContext(native->GetPrintSession(),
489468fe
SC
222 &pageContext );
223 }
224
225 if ( m_err != noErr )
226 {
c347101b
SC
227 PMSessionEndPageNoDialog(native->GetPrintSession());
228 PMSessionEndDocumentNoDialog(native->GetPrintSession());
489468fe
SC
229 }
230 else
231 {
884dad83
SC
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
489468fe 236 if ( !m_err )
489468fe 237 CGContextTranslateCTM( pageContext , (CGFloat) -paperRect.left , (CGFloat) paperRect.bottom ) ;
ce00f59b 238
489468fe
SC
239 // since this is a non-critical error, we set the flag back
240 m_err = noErr ;
884dad83
SC
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 ) );
489468fe 249 }
489468fe
SC
250}
251
252void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
253{
254 if ( m_err )
255 return ;
256
257 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
c347101b 258 wxOSXPrintData *native = (wxOSXPrintData*) impl->GetPrintData().GetNativeData() ;
489468fe 259
c347101b 260 m_err = PMSessionEndPageNoDialog(native->GetPrintSession());
489468fe
SC
261 if ( m_err != noErr )
262 {
c347101b 263 PMSessionEndDocumentNoDialog(native->GetPrintSession());
489468fe
SC
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
269void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const
270{
271 if ( w )
272 *w = m_maxX ;
273 if ( h )
274 *h = m_maxY ;
275}
276
6d52ca53 277wxSize wxMacCarbonPrinterDC::GetPPI() const
489468fe
SC
278{
279 return m_ppi ;
280};
281
282//
283//
284//
285
286wxPrinterDCImpl::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 {
a1b806b9 295 m_ok = m_nativePrinterDC->IsOk() ;
489468fe
SC
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;
6d52ca53 307 m_mm_to_pix_y = mm2inches * sz.y;
489468fe
SC
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
315wxSize wxPrinterDCImpl::GetPPI() const
316{
317 return m_nativePrinterDC->GetPPI() ;
318}
319
320wxPrinterDCImpl::~wxPrinterDCImpl()
321{
322 delete m_nativePrinterDC ;
323}
324
325bool 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 }
a1b806b9 336 m_ok = m_nativePrinterDC->IsOk() ;
489468fe
SC
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
348void wxPrinterDCImpl::EndDoc(void)
349{
350 if ( !m_ok )
351 return ;
352
353 m_nativePrinterDC->EndDoc( (wxPrinterDC*) GetOwner() ) ;
a1b806b9 354 m_ok = m_nativePrinterDC->IsOk() ;
489468fe
SC
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
6d52ca53 365wxRect wxPrinterDCImpl::GetPaperRect() const
489468fe
SC
366{
367 wxCoord w, h;
368 GetOwner()->GetSize(&w, &h);
369 wxRect pageRect(0, 0, w, h);
c347101b 370 wxOSXPrintData *native = (wxOSXPrintData*) m_printData.GetNativeData() ;
489468fe
SC
371 OSStatus err = noErr ;
372 PMRect rPaper;
c347101b 373 err = PMGetAdjustedPaperRect(native->GetPageFormat(), &rPaper);
489468fe
SC
374 if ( err != noErr )
375 return pageRect;
884dad83
SC
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
489468fe
SC
383 return wxRect(wxCoord(rPaper.left), wxCoord(rPaper.top),
384 wxCoord(rPaper.right - rPaper.left), wxCoord(rPaper.bottom - rPaper.top));
385}
386
387void 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() ) ;
a1b806b9 404 m_ok = m_nativePrinterDC->IsOk() ;
489468fe
SC
405
406}
407
408void wxPrinterDCImpl::EndPage()
409{
410 if ( !m_ok )
411 return ;
412
413 m_nativePrinterDC->EndPage( (wxPrinterDC*) GetOwner() );
a1b806b9 414 m_ok = m_nativePrinterDC->IsOk() ;
489468fe
SC
415}
416
417void wxPrinterDCImpl::DoGetSize(int *width, int *height) const
418{
9a83f860 419 wxCHECK_RET( m_ok , wxT("GetSize() doesn't work without a valid wxPrinterDC") );
489468fe
SC
420 m_nativePrinterDC->GetSize(width, height ) ;
421}
422
423#endif