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