]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcprint.cpp
add wxTLW::UseNativeDecorations[ByDefault]() to allow the programmer to control wheth...
[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
SC
64private :
65 GrafPtr m_macPrintFormerPort ;
66 wxCoord m_maxX ;
67 wxCoord m_maxY ;
db49000e 68 wxSize m_ppi ;
746d7582
SC
69 OSStatus m_err ;
70} ;
71
72wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
72e7876b 73{
746d7582
SC
74 ::GetPort( & m_macPrintFormerPort ) ;
75
76 m_err = noErr ;
dc7ccb9c 77 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ;
eb7f8ac5 78
746d7582
SC
79 PMRect rPage;
80 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
81 if ( m_err != noErr )
82 return;
a689a4d0 83
746d7582
SC
84 m_maxX = wxCoord(rPage.right - rPage.left) ;
85 m_maxY = wxCoord(rPage.bottom - rPage.top);
db49000e
SC
86
87 PMResolution res;
88 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
89 m_ppi = wxSize(int(res.hRes), int(res.vRes));
746d7582 90}
75411508 91
746d7582
SC
92wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
93{
94 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
95 ::SetPort( m_macPrintFormerPort ) ;
96}
97
98wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
99{
100 return new wxMacCarbonPrinterDC(data) ;
101}
102
eb7f8ac5 103bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
746d7582
SC
104{
105 if ( m_err )
106 return false ;
107
dc7ccb9c 108 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582 109
20b69855
SC
110#if wxMAC_USE_CORE_GRAPHICS
111 {
112 CFStringRef s[1] = { kPMGraphicsContextCoreGraphics };
113 CFArrayRef graphicsContextsArray = CFArrayCreate(NULL, (const void**)s, 1, &kCFTypeArrayCallBacks);
114 PMSessionSetDocumentFormatGeneration(native->m_macPrintSession, kPMDocumentFormatPDF, graphicsContextsArray, NULL);
115 CFRelease(graphicsContextsArray);
116 }
117#endif
118
746d7582 119 m_err = PMSessionBeginDocument(native->m_macPrintSession,
eb7f8ac5 120 native->m_macPrintSettings,
746d7582
SC
121 native->m_macPageFormat);
122 if ( m_err != noErr )
123 return false;
124
125 PMRect rPage;
126 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
127 if ( m_err != noErr )
9fff7273 128 return false ;
db49000e
SC
129
130 m_maxX = wxCoord(rPage.right - rPage.left) ;
131 m_maxY = wxCoord(rPage.bottom - rPage.top);
746d7582 132
db49000e
SC
133 PMResolution res;
134 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
135 m_ppi = wxSize(int(res.hRes), int(res.vRes));
746d7582
SC
136 return true ;
137}
138
eb7f8ac5 139void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
746d7582
SC
140{
141 if ( m_err )
142 return ;
143
dc7ccb9c 144 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582
SC
145
146 m_err = PMSessionEndDocument(native->m_macPrintSession);
147}
148
eb7f8ac5 149void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
746d7582
SC
150{
151 if ( m_err )
152 return ;
153
dc7ccb9c 154 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582
SC
155
156 m_err = PMSessionBeginPage(native->m_macPrintSession,
157 native->m_macPageFormat,
158 nil);
eb7f8ac5 159
20b69855
SC
160#if wxMAC_USE_CORE_GRAPHICS
161 CGContextRef pageContext;
162#endif
746d7582 163 if ( m_err == noErr )
e40298d5 164 {
20b69855 165#if wxMAC_USE_CORE_GRAPHICS
eb7f8ac5 166 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
20b69855
SC
167 kPMGraphicsContextCoreGraphics,
168 (void**) &pageContext );
169 dc->MacSetCGContext(pageContext) ;
170#else
171 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
172 kPMGraphicsContextQuickdraw,
eb7f8ac5 173 (void**) &dc->m_macPort );
20b69855 174#endif
e40298d5 175 }
eb7f8ac5 176
746d7582 177 if ( m_err != noErr )
e40298d5 178 {
746d7582
SC
179 PMSessionEndPage(native->m_macPrintSession);
180 PMSessionEndDocument(native->m_macPrintSession);
e40298d5 181 }
746d7582 182 else
e40298d5 183 {
746d7582 184 PMRect rPage;
eb7f8ac5 185
746d7582
SC
186 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
187 if ( !m_err )
188 {
20b69855 189#if wxMAC_USE_CORE_GRAPHICS
a06e389f
SC
190 PMRect paperRect ;
191 PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ;
192 CGContextTranslateCTM( pageContext , -paperRect.left , -paperRect.top + ( rPage.bottom - rPage.top ) ) ;
20b69855 193 CGContextScaleCTM( pageContext , 1 , -1 ) ;
c725df80 194 CGContextSaveGState( pageContext ) ;
20b69855 195#else
eb7f8ac5
VZ
196 dc->m_macLocalOrigin.x = (int) rPage.left;
197 dc->m_macLocalOrigin.y = (int) rPage.top;
20b69855 198#endif
746d7582
SC
199 }
200 // since this is a non-critical error, we set the flag back
201 m_err = noErr ;
e40298d5 202 }
746d7582
SC
203}
204
eb7f8ac5 205void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
746d7582
SC
206{
207 if ( m_err )
208 return ;
209
dc7ccb9c 210 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582
SC
211
212 m_err = PMSessionEndPage(native->m_macPrintSession);
213 if ( m_err != noErr )
f520d381 214 {
746d7582 215 PMSessionEndDocument(native->m_macPrintSession);
f520d381 216 }
746d7582
SC
217}
218
db49000e
SC
219void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const
220{
221 if ( w )
222 *w = m_maxX ;
223 if ( h )
224 *h = m_maxY ;
225}
226
227wxSize wxMacCarbonPrinterDC::GetPPI() const
228{
229 return m_ppi ;
230};
231
232//
233//
234//
235
746d7582
SC
236wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
237{
6d50343d 238 m_ok = false ;
746d7582
SC
239 m_printData = printdata ;
240 m_printData.ConvertToNative() ;
241 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
eb7f8ac5 242 if ( m_nativePrinterDC )
75411508 243 {
746d7582 244 m_ok = m_nativePrinterDC->Ok() ;
746d7582
SC
245 if ( !m_ok )
246 {
247 wxString message ;
eb7f8ac5 248 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
249 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
250 dialog.ShowModal();
251 }
db49000e
SC
252 else
253 {
254 wxSize sz = GetPPI();
255 m_mm_to_pix_x = mm2inches * sz.x;
256 m_mm_to_pix_y = mm2inches * sz.y;
257 }
20b69855
SC
258#if wxMAC_USE_CORE_GRAPHICS
259 // the cgContext will only be handed over page by page
260 m_graphicContext = new wxMacCGContext() ;
261#endif
75411508 262 }
746d7582
SC
263}
264
db49000e
SC
265wxSize wxPrinterDC::GetPPI() const
266{
267 return m_nativePrinterDC->GetPPI() ;
268}
269
746d7582
SC
270wxPrinterDC::~wxPrinterDC(void)
271{
c725df80
SC
272#if wxMAC_USE_CORE_GRAPHICS
273 // this context was borrowed
274 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ;
275#endif
746d7582
SC
276 delete m_nativePrinterDC ;
277}
278
20b69855 279#if wxMAC_USE_CORE_GRAPHICS
6d50343d 280void wxPrinterDC::MacSetCGContext( void * cg )
20b69855 281{
626fd619 282 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( (CGContextRef) cg ) ;
20b69855
SC
283 m_graphicContext->SetPen( m_pen ) ;
284 m_graphicContext->SetBrush( m_brush ) ;
285}
286#endif
eb7f8ac5 287bool wxPrinterDC::StartDoc( const wxString& message )
746d7582
SC
288{
289 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
eb7f8ac5 290
746d7582
SC
291 if ( !m_ok )
292 return false ;
293
294 if ( m_nativePrinterDC->StartDoc(this, message ) )
295 {
296 // in case we have to do additional things when successful
297 }
298 m_ok = m_nativePrinterDC->Ok() ;
299 if ( !m_ok )
75411508 300 {
746d7582 301 wxString message ;
eb7f8ac5 302 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
427ff662 303 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
75411508 304 dialog.ShowModal();
75411508 305 }
746d7582 306
e40298d5 307 return m_ok ;
72e7876b
SC
308}
309
eb7f8ac5 310void wxPrinterDC::EndDoc(void)
72e7876b 311{
746d7582
SC
312 if ( !m_ok )
313 return ;
314
315 m_nativePrinterDC->EndDoc( this ) ;
316 m_ok = m_nativePrinterDC->Ok() ;
317
318 if ( !m_ok )
75411508 319 {
746d7582 320 wxString message ;
eb7f8ac5 321 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
322 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
323 dialog.ShowModal();
75411508 324 }
72e7876b
SC
325}
326
eb7f8ac5 327void wxPrinterDC::StartPage(void)
72e7876b 328{
e40298d5
JS
329 if ( !m_ok )
330 return ;
2f1ae414 331
746d7582
SC
332 m_logicalFunction = wxCOPY;
333 // m_textAlignment = wxALIGN_TOP_LEFT;
334 m_backgroundMode = wxTRANSPARENT;
335
336 m_textForegroundColour = *wxBLACK;
337 m_textBackgroundColour = *wxWHITE;
338 m_pen = *wxBLACK_PEN;
339 m_font = *wxNORMAL_FONT;
340 m_brush = *wxTRANSPARENT_BRUSH;
341 m_backgroundBrush = *wxWHITE_BRUSH;
20b69855 342#if !wxMAC_USE_CORE_GRAPHICS
e40298d5
JS
343 m_macFontInstalled = false ;
344 m_macBrushInstalled = false ;
345 m_macPenInstalled = false ;
20b69855 346#endif
2f1ae414 347
746d7582
SC
348 m_nativePrinterDC->StartPage(this) ;
349 m_ok = m_nativePrinterDC->Ok() ;
eb7f8ac5 350
72e7876b
SC
351}
352
eb7f8ac5 353void wxPrinterDC::EndPage(void)
72e7876b 354{
e40298d5
JS
355 if ( !m_ok )
356 return ;
72e7876b 357
746d7582
SC
358 m_nativePrinterDC->EndPage(this) ;
359 m_ok = m_nativePrinterDC->Ok() ;
360}
72e7876b 361
746d7582
SC
362void wxPrinterDC::DoGetSize(int *width, int *height) const
363{
364 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
db49000e 365 m_nativePrinterDC->GetSize(width, height ) ;
72e7876b 366}
746d7582 367
179e085f 368#endif