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