]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcprint.cpp
use our own mbstowcs() and wcstombs() implementations with Metrowerks as MSL CRT...
[wxWidgets.git] / src / mac / carbon / dcprint.cpp
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$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_PRINTING_ARCHITECTURE
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #endif
23
24 #include "wx/dcprint.h"
25 #include "wx/msgdlg.h"
26 #include "wx/math.h"
27 #include "wx/mac/uma.h"
28 #include "wx/mac/private/print.h"
29
30 IMPLEMENT_CLASS(wxPrinterDC, wxDC)
31
32 class wxNativePrinterDC
33 {
34 public :
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 ; }
46
47 static wxNativePrinterDC* Create(wxPrintData* data) ;
48 } ;
49
50 class wxMacCarbonPrinterDC : public wxNativePrinterDC
51 {
52 public :
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 ; }
62 private :
63 GrafPtr m_macPrintFormerPort ;
64 wxCoord m_maxX ;
65 wxCoord m_maxY ;
66 OSStatus m_err ;
67 } ;
68
69 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
70 {
71 ::GetPort( & m_macPrintFormerPort ) ;
72
73 m_err = noErr ;
74 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ;
75
76 PMRect rPage;
77 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
78 if ( m_err != noErr )
79 return;
80
81 m_maxX = wxCoord(rPage.right - rPage.left) ;
82 m_maxY = wxCoord(rPage.bottom - rPage.top);
83 }
84
85 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
86 {
87 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
88 ::SetPort( m_macPrintFormerPort ) ;
89 }
90
91 wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
92 {
93 return new wxMacCarbonPrinterDC(data) ;
94 }
95
96 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
97 {
98 if ( m_err )
99 return false ;
100
101 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
102
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
112 m_err = PMSessionBeginDocument(native->m_macPrintSession,
113 native->m_macPrintSettings,
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
123 m_maxX = (wxCoord)(rPage.right - rPage.left);
124 m_maxY = (wxCoord)(rPage.bottom - rPage.top);
125 return true ;
126 }
127
128 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
129 {
130 if ( m_err )
131 return ;
132
133 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
134
135 m_err = PMSessionEndDocument(native->m_macPrintSession);
136 }
137
138 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
139 {
140 if ( m_err )
141 return ;
142
143 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
144
145 m_err = PMSessionBeginPage(native->m_macPrintSession,
146 native->m_macPageFormat,
147 nil);
148
149 #if wxMAC_USE_CORE_GRAPHICS
150 CGContextRef pageContext;
151 #endif
152 if ( m_err == noErr )
153 {
154 #if wxMAC_USE_CORE_GRAPHICS
155 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
156 kPMGraphicsContextCoreGraphics,
157 (void**) &pageContext );
158 dc->MacSetCGContext(pageContext) ;
159 #else
160 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
161 kPMGraphicsContextQuickdraw,
162 (void**) &dc->m_macPort );
163 #endif
164 }
165
166 if ( m_err != noErr )
167 {
168 PMSessionEndPage(native->m_macPrintSession);
169 PMSessionEndDocument(native->m_macPrintSession);
170 }
171 else
172 {
173 PMRect rPage;
174
175 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
176 if ( !m_err )
177 {
178 #if wxMAC_USE_CORE_GRAPHICS
179 PMRect paperRect ;
180 PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ;
181 CGContextTranslateCTM( pageContext , -paperRect.left , -paperRect.top + ( rPage.bottom - rPage.top ) ) ;
182 CGContextScaleCTM( pageContext , 1 , -1 ) ;
183 CGContextSaveGState( pageContext ) ;
184 #else
185 dc->m_macLocalOrigin.x = (int) rPage.left;
186 dc->m_macLocalOrigin.y = (int) rPage.top;
187 #endif
188 }
189 // since this is a non-critical error, we set the flag back
190 m_err = noErr ;
191 }
192 }
193
194 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
195 {
196 if ( m_err )
197 return ;
198
199 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
200
201 m_err = PMSessionEndPage(native->m_macPrintSession);
202 if ( m_err != noErr )
203 {
204 PMSessionEndDocument(native->m_macPrintSession);
205 }
206 }
207
208 wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
209 {
210 m_ok = FALSE ;
211 m_printData = printdata ;
212 m_printData.ConvertToNative() ;
213 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
214 if ( m_nativePrinterDC )
215 {
216 m_ok = m_nativePrinterDC->Ok() ;
217 if ( !m_ok )
218 {
219 wxString message ;
220 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
221 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
222 dialog.ShowModal();
223 }
224 #if wxMAC_USE_CORE_GRAPHICS
225 // the cgContext will only be handed over page by page
226 m_graphicContext = new wxMacCGContext() ;
227 #endif
228 }
229 }
230
231 wxPrinterDC::~wxPrinterDC(void)
232 {
233 #if wxMAC_USE_CORE_GRAPHICS
234 // this context was borrowed
235 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ;
236 #endif
237 delete m_nativePrinterDC ;
238 }
239
240 #if wxMAC_USE_CORE_GRAPHICS
241 void wxPrinterDC::MacSetCGContext( void * cg )
242 {
243 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( (CGContextRef) cg ) ;
244 m_graphicContext->SetPen( m_pen ) ;
245 m_graphicContext->SetBrush( m_brush ) ;
246 }
247 #endif
248 bool wxPrinterDC::StartDoc( const wxString& message )
249 {
250 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
251
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 )
261 {
262 wxString message ;
263 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
264 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
265 dialog.ShowModal();
266 }
267
268 return m_ok ;
269 }
270
271 void wxPrinterDC::EndDoc(void)
272 {
273 if ( !m_ok )
274 return ;
275
276 m_nativePrinterDC->EndDoc( this ) ;
277 m_ok = m_nativePrinterDC->Ok() ;
278
279 if ( !m_ok )
280 {
281 wxString message ;
282 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
283 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
284 dialog.ShowModal();
285 }
286 }
287
288 void wxPrinterDC::StartPage(void)
289 {
290 if ( !m_ok )
291 return ;
292
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;
303 #if !wxMAC_USE_CORE_GRAPHICS
304 m_macFontInstalled = false ;
305 m_macBrushInstalled = false ;
306 m_macPenInstalled = false ;
307 #endif
308
309 m_nativePrinterDC->StartPage(this) ;
310 m_ok = m_nativePrinterDC->Ok() ;
311
312 }
313
314 void wxPrinterDC::EndPage(void)
315 {
316 if ( !m_ok )
317 return ;
318
319 m_nativePrinterDC->EndPage(this) ;
320 m_ok = m_nativePrinterDC->Ok() ;
321 }
322
323 void wxPrinterDC::DoGetSize(int *width, int *height) const
324 {
325 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
326
327 if ( width )
328 * width = m_nativePrinterDC->GetMaxX() ;
329 if ( height )
330 * height = m_nativePrinterDC->GetMaxY() ;
331 }
332
333 #endif