]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcprint.cpp
Include wx/dcmemory.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / mac / carbon / dcprint.cpp
CommitLineData
72e7876b
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: dcprint.cpp
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
65571936 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
SC
17#ifdef __BORLANDC__
18#pragma hdrstop
19#endif
20
21#ifndef WX_PRECOMP
22#endif
23
24#include "wx/dcprint.h"
03e11df5 25#include "wx/msgdlg.h"
463c4d71 26#include "wx/math.h"
2f1ae414 27#include "wx/mac/uma.h"
746d7582 28#include "wx/mac/private/print.h"
a3d3d3bf 29
72e7876b 30IMPLEMENT_CLASS(wxPrinterDC, wxDC)
72e7876b 31
746d7582
SC
32class wxNativePrinterDC
33{
34public :
35 wxNativePrinterDC() {}
36 virtual ~wxNativePrinterDC() {}
37 virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) = 0;
38 virtual void EndDoc( wxPrinterDC* dc ) = 0;
39 virtual void StartPage( wxPrinterDC* dc ) = 0;
40 virtual void EndPage( wxPrinterDC* dc ) = 0;
41 virtual wxCoord GetMaxX() const = 0 ;
42 virtual wxCoord GetMaxY() const = 0 ;
43 // returns 0 in case of no Error, otherwise platform specific error codes
44 virtual wxUint32 GetStatus() const = 0 ;
45 bool Ok() { return GetStatus() == 0 ; }
eb7f8ac5 46
746d7582
SC
47 static wxNativePrinterDC* Create(wxPrintData* data) ;
48} ;
72e7876b 49
746d7582
SC
50class wxMacCarbonPrinterDC : public wxNativePrinterDC
51{
52public :
53 wxMacCarbonPrinterDC( wxPrintData* data ) ;
54 ~wxMacCarbonPrinterDC() ;
55 virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) ;
56 virtual void EndDoc( wxPrinterDC* dc ) ;
57 virtual void StartPage( wxPrinterDC* dc ) ;
58 virtual void EndPage( wxPrinterDC* dc ) ;
59 virtual wxCoord GetMaxX() const { return m_maxX ; }
60 virtual wxCoord GetMaxY() const { return m_maxY ; }
61 virtual wxUint32 GetStatus() const { return m_err ; }
62private :
63 GrafPtr m_macPrintFormerPort ;
64 wxCoord m_maxX ;
65 wxCoord m_maxY ;
66 OSStatus m_err ;
67} ;
68
69wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
72e7876b 70{
746d7582
SC
71 ::GetPort( & m_macPrintFormerPort ) ;
72
73 m_err = noErr ;
dc7ccb9c 74 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ;
eb7f8ac5 75
746d7582
SC
76 PMRect rPage;
77 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
78 if ( m_err != noErr )
79 return;
a689a4d0 80
746d7582
SC
81 m_maxX = wxCoord(rPage.right - rPage.left) ;
82 m_maxY = wxCoord(rPage.bottom - rPage.top);
83}
75411508 84
746d7582
SC
85wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
86{
87 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
88 ::SetPort( m_macPrintFormerPort ) ;
89}
90
91wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
92{
93 return new wxMacCarbonPrinterDC(data) ;
94}
95
eb7f8ac5 96bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
746d7582
SC
97{
98 if ( m_err )
99 return false ;
100
dc7ccb9c 101 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582 102
20b69855
SC
103#if wxMAC_USE_CORE_GRAPHICS
104 {
105 CFStringRef s[1] = { kPMGraphicsContextCoreGraphics };
106 CFArrayRef graphicsContextsArray = CFArrayCreate(NULL, (const void**)s, 1, &kCFTypeArrayCallBacks);
107 PMSessionSetDocumentFormatGeneration(native->m_macPrintSession, kPMDocumentFormatPDF, graphicsContextsArray, NULL);
108 CFRelease(graphicsContextsArray);
109 }
110#endif
111
746d7582 112 m_err = PMSessionBeginDocument(native->m_macPrintSession,
eb7f8ac5 113 native->m_macPrintSettings,
746d7582
SC
114 native->m_macPageFormat);
115 if ( m_err != noErr )
116 return false;
117
118 PMRect rPage;
119 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
120 if ( m_err != noErr )
121 return false;
122
eb7f8ac5
VZ
123 m_maxX = (wxCoord)(rPage.right - rPage.left);
124 m_maxY = (wxCoord)(rPage.bottom - rPage.top);
746d7582
SC
125 return true ;
126}
127
eb7f8ac5 128void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
746d7582
SC
129{
130 if ( m_err )
131 return ;
132
dc7ccb9c 133 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582
SC
134
135 m_err = PMSessionEndDocument(native->m_macPrintSession);
136}
137
eb7f8ac5 138void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
746d7582
SC
139{
140 if ( m_err )
141 return ;
142
dc7ccb9c 143 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582
SC
144
145 m_err = PMSessionBeginPage(native->m_macPrintSession,
146 native->m_macPageFormat,
147 nil);
eb7f8ac5 148
20b69855
SC
149#if wxMAC_USE_CORE_GRAPHICS
150 CGContextRef pageContext;
151#endif
746d7582 152 if ( m_err == noErr )
e40298d5 153 {
20b69855 154#if wxMAC_USE_CORE_GRAPHICS
eb7f8ac5 155 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
20b69855
SC
156 kPMGraphicsContextCoreGraphics,
157 (void**) &pageContext );
158 dc->MacSetCGContext(pageContext) ;
159#else
160 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
161 kPMGraphicsContextQuickdraw,
eb7f8ac5 162 (void**) &dc->m_macPort );
20b69855 163#endif
e40298d5 164 }
eb7f8ac5 165
746d7582 166 if ( m_err != noErr )
e40298d5 167 {
746d7582
SC
168 PMSessionEndPage(native->m_macPrintSession);
169 PMSessionEndDocument(native->m_macPrintSession);
e40298d5 170 }
746d7582 171 else
e40298d5 172 {
746d7582 173 PMRect rPage;
eb7f8ac5 174
746d7582
SC
175 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
176 if ( !m_err )
177 {
20b69855 178#if wxMAC_USE_CORE_GRAPHICS
a06e389f
SC
179 PMRect paperRect ;
180 PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ;
181 CGContextTranslateCTM( pageContext , -paperRect.left , -paperRect.top + ( rPage.bottom - rPage.top ) ) ;
20b69855 182 CGContextScaleCTM( pageContext , 1 , -1 ) ;
c725df80 183 CGContextSaveGState( pageContext ) ;
20b69855 184#else
eb7f8ac5
VZ
185 dc->m_macLocalOrigin.x = (int) rPage.left;
186 dc->m_macLocalOrigin.y = (int) rPage.top;
20b69855 187#endif
746d7582
SC
188 }
189 // since this is a non-critical error, we set the flag back
190 m_err = noErr ;
e40298d5 191 }
746d7582
SC
192}
193
eb7f8ac5 194void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
746d7582
SC
195{
196 if ( m_err )
197 return ;
198
dc7ccb9c 199 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582
SC
200
201 m_err = PMSessionEndPage(native->m_macPrintSession);
202 if ( m_err != noErr )
f520d381 203 {
746d7582 204 PMSessionEndDocument(native->m_macPrintSession);
f520d381 205 }
746d7582
SC
206}
207
746d7582
SC
208wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
209{
210 m_ok = FALSE ;
211 m_printData = printdata ;
212 m_printData.ConvertToNative() ;
213 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
eb7f8ac5 214 if ( m_nativePrinterDC )
75411508 215 {
746d7582 216 m_ok = m_nativePrinterDC->Ok() ;
746d7582
SC
217 if ( !m_ok )
218 {
219 wxString message ;
eb7f8ac5 220 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
221 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
222 dialog.ShowModal();
223 }
20b69855
SC
224#if wxMAC_USE_CORE_GRAPHICS
225 // the cgContext will only be handed over page by page
226 m_graphicContext = new wxMacCGContext() ;
227#endif
75411508 228 }
746d7582
SC
229}
230
231wxPrinterDC::~wxPrinterDC(void)
232{
c725df80
SC
233#if wxMAC_USE_CORE_GRAPHICS
234 // this context was borrowed
235 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ;
236#endif
746d7582
SC
237 delete m_nativePrinterDC ;
238}
239
20b69855
SC
240#if wxMAC_USE_CORE_GRAPHICS
241void wxPrinterDC::MacSetCGContext( void * cg )
242{
626fd619 243 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( (CGContextRef) cg ) ;
20b69855
SC
244 m_graphicContext->SetPen( m_pen ) ;
245 m_graphicContext->SetBrush( m_brush ) ;
246}
247#endif
eb7f8ac5 248bool wxPrinterDC::StartDoc( const wxString& message )
746d7582
SC
249{
250 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
eb7f8ac5 251
746d7582
SC
252 if ( !m_ok )
253 return false ;
254
255 if ( m_nativePrinterDC->StartDoc(this, message ) )
256 {
257 // in case we have to do additional things when successful
258 }
259 m_ok = m_nativePrinterDC->Ok() ;
260 if ( !m_ok )
75411508 261 {
746d7582 262 wxString message ;
eb7f8ac5 263 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
427ff662 264 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
75411508 265 dialog.ShowModal();
75411508 266 }
746d7582 267
e40298d5 268 return m_ok ;
72e7876b
SC
269}
270
eb7f8ac5 271void wxPrinterDC::EndDoc(void)
72e7876b 272{
746d7582
SC
273 if ( !m_ok )
274 return ;
275
276 m_nativePrinterDC->EndDoc( this ) ;
277 m_ok = m_nativePrinterDC->Ok() ;
278
279 if ( !m_ok )
75411508 280 {
746d7582 281 wxString message ;
eb7f8ac5 282 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
283 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
284 dialog.ShowModal();
75411508 285 }
72e7876b
SC
286}
287
eb7f8ac5 288void wxPrinterDC::StartPage(void)
72e7876b 289{
e40298d5
JS
290 if ( !m_ok )
291 return ;
2f1ae414 292
746d7582
SC
293 m_logicalFunction = wxCOPY;
294 // m_textAlignment = wxALIGN_TOP_LEFT;
295 m_backgroundMode = wxTRANSPARENT;
296
297 m_textForegroundColour = *wxBLACK;
298 m_textBackgroundColour = *wxWHITE;
299 m_pen = *wxBLACK_PEN;
300 m_font = *wxNORMAL_FONT;
301 m_brush = *wxTRANSPARENT_BRUSH;
302 m_backgroundBrush = *wxWHITE_BRUSH;
20b69855 303#if !wxMAC_USE_CORE_GRAPHICS
e40298d5
JS
304 m_macFontInstalled = false ;
305 m_macBrushInstalled = false ;
306 m_macPenInstalled = false ;
20b69855 307#endif
2f1ae414 308
746d7582
SC
309 m_nativePrinterDC->StartPage(this) ;
310 m_ok = m_nativePrinterDC->Ok() ;
eb7f8ac5 311
72e7876b
SC
312}
313
eb7f8ac5 314void wxPrinterDC::EndPage(void)
72e7876b 315{
e40298d5
JS
316 if ( !m_ok )
317 return ;
72e7876b 318
746d7582
SC
319 m_nativePrinterDC->EndPage(this) ;
320 m_ok = m_nativePrinterDC->Ok() ;
321}
72e7876b 322
746d7582
SC
323void wxPrinterDC::DoGetSize(int *width, int *height) const
324{
325 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
2f1ae414 326
746d7582
SC
327 if ( width )
328 * width = m_nativePrinterDC->GetMaxX() ;
329 if ( height )
330 * height = m_nativePrinterDC->GetMaxY() ;
72e7876b 331}
746d7582 332
179e085f 333#endif