]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcprint.cpp
Compilo.
[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 96 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
4f74e0d1 97#endif
f7862d3e 98 m_ppi = wxSize(int(res.hRes), int(res.vRes));
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 288#if wxMAC_USE_CORE_GRAPHICS
f7862d3e 289/*
20b69855
SC
290 // the cgContext will only be handed over page by page
291 m_graphicContext = new wxMacCGContext() ;
f7862d3e 292 */
20b69855 293#endif
75411508 294 }
746d7582
SC
295}
296
db49000e
SC
297wxSize wxPrinterDC::GetPPI() const
298{
299 return m_nativePrinterDC->GetPPI() ;
300}
301
746d7582
SC
302wxPrinterDC::~wxPrinterDC(void)
303{
c725df80 304#if wxMAC_USE_CORE_GRAPHICS
f7862d3e 305/*
c725df80
SC
306 // this context was borrowed
307 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ;
f7862d3e 308 */
c725df80 309#endif
746d7582
SC
310 delete m_nativePrinterDC ;
311}
312
20b69855 313#if wxMAC_USE_CORE_GRAPHICS
6d50343d 314void wxPrinterDC::MacSetCGContext( void * cg )
20b69855 315{
f7862d3e 316 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg ) );
20b69855
SC
317 m_graphicContext->SetPen( m_pen ) ;
318 m_graphicContext->SetBrush( m_brush ) ;
319}
320#endif
eb7f8ac5 321bool wxPrinterDC::StartDoc( const wxString& message )
746d7582
SC
322{
323 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
eb7f8ac5 324
746d7582
SC
325 if ( !m_ok )
326 return false ;
327
328 if ( m_nativePrinterDC->StartDoc(this, message ) )
329 {
330 // in case we have to do additional things when successful
331 }
332 m_ok = m_nativePrinterDC->Ok() ;
333 if ( !m_ok )
75411508 334 {
746d7582 335 wxString message ;
eb7f8ac5 336 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
427ff662 337 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
75411508 338 dialog.ShowModal();
75411508 339 }
746d7582 340
e40298d5 341 return m_ok ;
72e7876b
SC
342}
343
eb7f8ac5 344void wxPrinterDC::EndDoc(void)
72e7876b 345{
746d7582
SC
346 if ( !m_ok )
347 return ;
348
349 m_nativePrinterDC->EndDoc( this ) ;
350 m_ok = m_nativePrinterDC->Ok() ;
351
352 if ( !m_ok )
75411508 353 {
746d7582 354 wxString message ;
eb7f8ac5 355 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
356 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
357 dialog.ShowModal();
75411508 358 }
72e7876b
SC
359}
360
eb7f8ac5 361void wxPrinterDC::StartPage(void)
72e7876b 362{
e40298d5
JS
363 if ( !m_ok )
364 return ;
2f1ae414 365
746d7582
SC
366 m_logicalFunction = wxCOPY;
367 // m_textAlignment = wxALIGN_TOP_LEFT;
368 m_backgroundMode = wxTRANSPARENT;
369
370 m_textForegroundColour = *wxBLACK;
371 m_textBackgroundColour = *wxWHITE;
372 m_pen = *wxBLACK_PEN;
373 m_font = *wxNORMAL_FONT;
374 m_brush = *wxTRANSPARENT_BRUSH;
375 m_backgroundBrush = *wxWHITE_BRUSH;
20b69855 376#if !wxMAC_USE_CORE_GRAPHICS
e40298d5
JS
377 m_macFontInstalled = false ;
378 m_macBrushInstalled = false ;
379 m_macPenInstalled = false ;
20b69855 380#endif
2f1ae414 381
746d7582
SC
382 m_nativePrinterDC->StartPage(this) ;
383 m_ok = m_nativePrinterDC->Ok() ;
eb7f8ac5 384
72e7876b
SC
385}
386
eb7f8ac5 387void wxPrinterDC::EndPage(void)
72e7876b 388{
e40298d5
JS
389 if ( !m_ok )
390 return ;
72e7876b 391
746d7582
SC
392 m_nativePrinterDC->EndPage(this) ;
393 m_ok = m_nativePrinterDC->Ok() ;
394}
72e7876b 395
746d7582
SC
396void wxPrinterDC::DoGetSize(int *width, int *height) const
397{
398 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
db49000e 399 m_nativePrinterDC->GetSize(width, height ) ;
72e7876b 400}
746d7582 401
179e085f 402#endif