]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcprint.cpp
Updated list of subprojects.
[wxWidgets.git] / src / mac / carbon / dcprint.cpp
CommitLineData
72e7876b 1/////////////////////////////////////////////////////////////////////////////
6d50343d 2// Name: src/mac/carbon/dcprint.cpp
72e7876b
SC
3// Purpose: wxPrinterDC class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
6d50343d 9// Licence: wxWindows licence
72e7876b
SC
10/////////////////////////////////////////////////////////////////////////////
11
72e7876b
SC
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
179e085f
RN
15#if wxUSE_PRINTING_ARCHITECTURE
16
72e7876b 17#ifdef __BORLANDC__
6d50343d 18 #pragma hdrstop
72e7876b
SC
19#endif
20
6d50343d
WS
21#include "wx/dcprint.h"
22
72e7876b 23#ifndef WX_PRECOMP
246c5004 24 #include "wx/msgdlg.h"
18680f86 25 #include "wx/math.h"
72e7876b
SC
26#endif
27
2f1ae414 28#include "wx/mac/uma.h"
746d7582 29#include "wx/mac/private/print.h"
a3d3d3bf 30
72e7876b 31IMPLEMENT_CLASS(wxPrinterDC, wxDC)
72e7876b 32
746d7582
SC
33class wxNativePrinterDC
34{
35public :
36 wxNativePrinterDC() {}
37 virtual ~wxNativePrinterDC() {}
38 virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) = 0;
39 virtual void EndDoc( wxPrinterDC* dc ) = 0;
40 virtual void StartPage( wxPrinterDC* dc ) = 0;
41 virtual void EndPage( wxPrinterDC* dc ) = 0;
db49000e
SC
42 virtual void GetSize( int *w , int *h) const = 0 ;
43 virtual wxSize GetPPI() const = 0 ;
44
746d7582
SC
45 // returns 0 in case of no Error, otherwise platform specific error codes
46 virtual wxUint32 GetStatus() const = 0 ;
47 bool Ok() { return GetStatus() == 0 ; }
eb7f8ac5 48
746d7582
SC
49 static wxNativePrinterDC* Create(wxPrintData* data) ;
50} ;
72e7876b 51
746d7582
SC
52class wxMacCarbonPrinterDC : public wxNativePrinterDC
53{
54public :
55 wxMacCarbonPrinterDC( wxPrintData* data ) ;
d3c7fc99 56 virtual ~wxMacCarbonPrinterDC() ;
746d7582
SC
57 virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) ;
58 virtual void EndDoc( wxPrinterDC* dc ) ;
59 virtual void StartPage( wxPrinterDC* dc ) ;
60 virtual void EndPage( wxPrinterDC* dc ) ;
746d7582 61 virtual wxUint32 GetStatus() const { return m_err ; }
db49000e
SC
62 virtual void GetSize( int *w , int *h) const ;
63 virtual wxSize GetPPI() const ;
746d7582 64private :
4f74e0d1 65#if !wxMAC_USE_CORE_GRAPHICS
746d7582 66 GrafPtr m_macPrintFormerPort ;
4f74e0d1 67#endif
746d7582
SC
68 wxCoord m_maxX ;
69 wxCoord m_maxY ;
db49000e 70 wxSize m_ppi ;
746d7582
SC
71 OSStatus m_err ;
72} ;
73
74wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
72e7876b 75{
4f74e0d1 76#if !wxMAC_USE_CORE_GRAPHICS
746d7582 77 ::GetPort( & m_macPrintFormerPort ) ;
4f74e0d1 78#endif
746d7582 79 m_err = noErr ;
dc7ccb9c 80 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ;
eb7f8ac5 81
746d7582
SC
82 PMRect rPage;
83 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
84 if ( m_err != noErr )
85 return;
a689a4d0 86
746d7582
SC
87 m_maxX = wxCoord(rPage.right - rPage.left) ;
88 m_maxY = wxCoord(rPage.bottom - rPage.top);
db49000e
SC
89
90 PMResolution res;
4f74e0d1
SC
91#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
92 PMPrinter printer;
93 PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer);
94 PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ;
95#else
db49000e
SC
96 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
97 m_ppi = wxSize(int(res.hRes), int(res.vRes));
4f74e0d1 98#endif
746d7582 99}
75411508 100
746d7582
SC
101wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
102{
4f74e0d1 103#if !wxMAC_USE_CORE_GRAPHICS
746d7582
SC
104 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
105 ::SetPort( m_macPrintFormerPort ) ;
4f74e0d1 106#endif
746d7582
SC
107}
108
109wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
110{
111 return new wxMacCarbonPrinterDC(data) ;
112}
113
eb7f8ac5 114bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
746d7582
SC
115{
116 if ( m_err )
117 return false ;
118
dc7ccb9c 119 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582 120
4f74e0d1 121#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4 && wxMAC_USE_CORE_GRAPHICS
20b69855
SC
122 {
123 CFStringRef s[1] = { kPMGraphicsContextCoreGraphics };
124 CFArrayRef graphicsContextsArray = CFArrayCreate(NULL, (const void**)s, 1, &kCFTypeArrayCallBacks);
125 PMSessionSetDocumentFormatGeneration(native->m_macPrintSession, kPMDocumentFormatPDF, graphicsContextsArray, NULL);
126 CFRelease(graphicsContextsArray);
127 }
128#endif
4f74e0d1
SC
129#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4 && wxMAC_USE_CORE_GRAPHICS
130 m_err = PMSessionBeginCGDocument(native->m_macPrintSession,
131 native->m_macPrintSettings,
132 native->m_macPageFormat);
133#else
746d7582 134 m_err = PMSessionBeginDocument(native->m_macPrintSession,
eb7f8ac5 135 native->m_macPrintSettings,
746d7582 136 native->m_macPageFormat);
4f74e0d1
SC
137
138#endif
139
746d7582
SC
140 if ( m_err != noErr )
141 return false;
142
143 PMRect rPage;
144 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
145 if ( m_err != noErr )
9fff7273 146 return false ;
db49000e
SC
147
148 m_maxX = wxCoord(rPage.right - rPage.left) ;
149 m_maxY = wxCoord(rPage.bottom - rPage.top);
746d7582 150
db49000e 151 PMResolution res;
4f74e0d1
SC
152#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
153 PMPrinter printer;
154 PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer);
155 PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ;
156#else
db49000e 157 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
4f74e0d1 158#endif
db49000e 159 m_ppi = wxSize(int(res.hRes), int(res.vRes));
746d7582
SC
160 return true ;
161}
162
eb7f8ac5 163void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
746d7582
SC
164{
165 if ( m_err )
166 return ;
167
dc7ccb9c 168 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582
SC
169
170 m_err = PMSessionEndDocument(native->m_macPrintSession);
171}
172
eb7f8ac5 173void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
746d7582
SC
174{
175 if ( m_err )
176 return ;
177
dc7ccb9c 178 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582
SC
179
180 m_err = PMSessionBeginPage(native->m_macPrintSession,
181 native->m_macPageFormat,
182 nil);
eb7f8ac5 183
20b69855
SC
184#if wxMAC_USE_CORE_GRAPHICS
185 CGContextRef pageContext;
186#endif
746d7582 187 if ( m_err == noErr )
e40298d5 188 {
20b69855 189#if wxMAC_USE_CORE_GRAPHICS
4f74e0d1
SC
190#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
191 m_err = PMSessionGetCGGraphicsContext(native->m_macPrintSession,
192 &pageContext );
193
194#else
eb7f8ac5 195 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
20b69855
SC
196 kPMGraphicsContextCoreGraphics,
197 (void**) &pageContext );
4f74e0d1 198#endif
20b69855
SC
199 dc->MacSetCGContext(pageContext) ;
200#else
201 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
202 kPMGraphicsContextQuickdraw,
eb7f8ac5 203 (void**) &dc->m_macPort );
20b69855 204#endif
e40298d5 205 }
eb7f8ac5 206
746d7582 207 if ( m_err != noErr )
e40298d5 208 {
746d7582
SC
209 PMSessionEndPage(native->m_macPrintSession);
210 PMSessionEndDocument(native->m_macPrintSession);
e40298d5 211 }
746d7582 212 else
e40298d5 213 {
746d7582 214 PMRect rPage;
eb7f8ac5 215
746d7582
SC
216 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
217 if ( !m_err )
218 {
20b69855 219#if wxMAC_USE_CORE_GRAPHICS
a06e389f
SC
220 PMRect paperRect ;
221 PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ;
222 CGContextTranslateCTM( pageContext , -paperRect.left , -paperRect.top + ( rPage.bottom - rPage.top ) ) ;
20b69855 223 CGContextScaleCTM( pageContext , 1 , -1 ) ;
c725df80 224 CGContextSaveGState( pageContext ) ;
20b69855 225#else
eb7f8ac5
VZ
226 dc->m_macLocalOrigin.x = (int) rPage.left;
227 dc->m_macLocalOrigin.y = (int) rPage.top;
20b69855 228#endif
746d7582
SC
229 }
230 // since this is a non-critical error, we set the flag back
231 m_err = noErr ;
e40298d5 232 }
746d7582
SC
233}
234
eb7f8ac5 235void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
746d7582
SC
236{
237 if ( m_err )
238 return ;
239
dc7ccb9c 240 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582
SC
241
242 m_err = PMSessionEndPage(native->m_macPrintSession);
243 if ( m_err != noErr )
f520d381 244 {
746d7582 245 PMSessionEndDocument(native->m_macPrintSession);
f520d381 246 }
746d7582
SC
247}
248
db49000e
SC
249void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const
250{
251 if ( w )
252 *w = m_maxX ;
253 if ( h )
254 *h = m_maxY ;
255}
256
257wxSize wxMacCarbonPrinterDC::GetPPI() const
258{
259 return m_ppi ;
260};
261
262//
263//
264//
265
746d7582
SC
266wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
267{
6d50343d 268 m_ok = false ;
746d7582
SC
269 m_printData = printdata ;
270 m_printData.ConvertToNative() ;
271 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
eb7f8ac5 272 if ( m_nativePrinterDC )
75411508 273 {
746d7582 274 m_ok = m_nativePrinterDC->Ok() ;
746d7582
SC
275 if ( !m_ok )
276 {
277 wxString message ;
eb7f8ac5 278 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
279 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
280 dialog.ShowModal();
281 }
db49000e
SC
282 else
283 {
284 wxSize sz = GetPPI();
285 m_mm_to_pix_x = mm2inches * sz.x;
286 m_mm_to_pix_y = mm2inches * sz.y;
287 }
20b69855
SC
288#if wxMAC_USE_CORE_GRAPHICS
289 // the cgContext will only be handed over page by page
290 m_graphicContext = new wxMacCGContext() ;
291#endif
75411508 292 }
746d7582
SC
293}
294
db49000e
SC
295wxSize wxPrinterDC::GetPPI() const
296{
297 return m_nativePrinterDC->GetPPI() ;
298}
299
746d7582
SC
300wxPrinterDC::~wxPrinterDC(void)
301{
c725df80
SC
302#if wxMAC_USE_CORE_GRAPHICS
303 // this context was borrowed
304 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ;
305#endif
746d7582
SC
306 delete m_nativePrinterDC ;
307}
308
20b69855 309#if wxMAC_USE_CORE_GRAPHICS
6d50343d 310void wxPrinterDC::MacSetCGContext( void * cg )
20b69855 311{
626fd619 312 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( (CGContextRef) cg ) ;
20b69855
SC
313 m_graphicContext->SetPen( m_pen ) ;
314 m_graphicContext->SetBrush( m_brush ) ;
315}
316#endif
eb7f8ac5 317bool wxPrinterDC::StartDoc( const wxString& message )
746d7582
SC
318{
319 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
eb7f8ac5 320
746d7582
SC
321 if ( !m_ok )
322 return false ;
323
324 if ( m_nativePrinterDC->StartDoc(this, message ) )
325 {
326 // in case we have to do additional things when successful
327 }
328 m_ok = m_nativePrinterDC->Ok() ;
329 if ( !m_ok )
75411508 330 {
746d7582 331 wxString message ;
eb7f8ac5 332 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
427ff662 333 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
75411508 334 dialog.ShowModal();
75411508 335 }
746d7582 336
e40298d5 337 return m_ok ;
72e7876b
SC
338}
339
eb7f8ac5 340void wxPrinterDC::EndDoc(void)
72e7876b 341{
746d7582
SC
342 if ( !m_ok )
343 return ;
344
345 m_nativePrinterDC->EndDoc( this ) ;
346 m_ok = m_nativePrinterDC->Ok() ;
347
348 if ( !m_ok )
75411508 349 {
746d7582 350 wxString message ;
eb7f8ac5 351 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
352 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
353 dialog.ShowModal();
75411508 354 }
72e7876b
SC
355}
356
eb7f8ac5 357void wxPrinterDC::StartPage(void)
72e7876b 358{
e40298d5
JS
359 if ( !m_ok )
360 return ;
2f1ae414 361
746d7582
SC
362 m_logicalFunction = wxCOPY;
363 // m_textAlignment = wxALIGN_TOP_LEFT;
364 m_backgroundMode = wxTRANSPARENT;
365
366 m_textForegroundColour = *wxBLACK;
367 m_textBackgroundColour = *wxWHITE;
368 m_pen = *wxBLACK_PEN;
369 m_font = *wxNORMAL_FONT;
370 m_brush = *wxTRANSPARENT_BRUSH;
371 m_backgroundBrush = *wxWHITE_BRUSH;
20b69855 372#if !wxMAC_USE_CORE_GRAPHICS
e40298d5
JS
373 m_macFontInstalled = false ;
374 m_macBrushInstalled = false ;
375 m_macPenInstalled = false ;
20b69855 376#endif
2f1ae414 377
746d7582
SC
378 m_nativePrinterDC->StartPage(this) ;
379 m_ok = m_nativePrinterDC->Ok() ;
eb7f8ac5 380
72e7876b
SC
381}
382
eb7f8ac5 383void wxPrinterDC::EndPage(void)
72e7876b 384{
e40298d5
JS
385 if ( !m_ok )
386 return ;
72e7876b 387
746d7582
SC
388 m_nativePrinterDC->EndPage(this) ;
389 m_ok = m_nativePrinterDC->Ok() ;
390}
72e7876b 391
746d7582
SC
392void wxPrinterDC::DoGetSize(int *width, int *height) const
393{
394 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
db49000e 395 m_nativePrinterDC->GetSize(width, height ) ;
72e7876b 396}
746d7582 397
179e085f 398#endif