]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcprint.cpp
Common code for the same handling of wxSL_INVERSE.
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dcprint.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #endif
25
26 #include "wx/dcprint.h"
27 #include "wx/msgdlg.h"
28 #include "wx/math.h"
29 #include "wx/mac/uma.h"
30 #include "wx/mac/private/print.h"
31
32 #if !USE_SHARED_LIBRARY
33 IMPLEMENT_CLASS(wxPrinterDC, wxDC)
34 #endif
35
36 class wxNativePrinterDC
37 {
38 public :
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 ; }
50
51 static wxNativePrinterDC* Create(wxPrintData* data) ;
52 } ;
53
54 #if TARGET_CARBON
55
56 class wxMacCarbonPrinterDC : public wxNativePrinterDC
57 {
58 public :
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 ; }
68 private :
69 GrafPtr m_macPrintFormerPort ;
70 wxCoord m_maxX ;
71 wxCoord m_maxY ;
72 OSStatus m_err ;
73 } ;
74
75 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
76 {
77 ::GetPort( & m_macPrintFormerPort ) ;
78
79 m_err = noErr ;
80 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->m_nativePrintData ;
81
82 PMRect rPage;
83 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
84 if ( m_err != noErr )
85 return;
86
87 m_maxX = wxCoord(rPage.right - rPage.left) ;
88 m_maxY = wxCoord(rPage.bottom - rPage.top);
89 }
90
91 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
92 {
93 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
94 ::SetPort( m_macPrintFormerPort ) ;
95 }
96
97 wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
98 {
99 return new wxMacCarbonPrinterDC(data) ;
100 }
101
102 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
103 {
104 if ( m_err )
105 return false ;
106
107 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().m_nativePrintData ;
108
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
118 m_err = PMSessionBeginDocument(native->m_macPrintSession,
119 native->m_macPrintSettings,
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
129 m_maxX = (wxCoord)(rPage.right - rPage.left);
130 m_maxY = (wxCoord)(rPage.bottom - rPage.top);
131 return true ;
132 }
133
134 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
135 {
136 if ( m_err )
137 return ;
138
139 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().m_nativePrintData ;
140
141 m_err = PMSessionEndDocument(native->m_macPrintSession);
142 }
143
144 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
145 {
146 if ( m_err )
147 return ;
148
149 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().m_nativePrintData ;
150
151 m_err = PMSessionBeginPage(native->m_macPrintSession,
152 native->m_macPageFormat,
153 nil);
154
155 #if wxMAC_USE_CORE_GRAPHICS
156 CGContextRef pageContext;
157 #endif
158 if ( m_err == noErr )
159 {
160 #if wxMAC_USE_CORE_GRAPHICS
161 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
162 kPMGraphicsContextCoreGraphics,
163 (void**) &pageContext );
164 dc->MacSetCGContext(pageContext) ;
165 #else
166 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
167 kPMGraphicsContextQuickdraw,
168 (void**) &dc->m_macPort );
169 #endif
170 }
171
172 if ( m_err != noErr )
173 {
174 PMSessionEndPage(native->m_macPrintSession);
175 PMSessionEndDocument(native->m_macPrintSession);
176 }
177 else
178 {
179 PMRect rPage;
180
181 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
182 if ( !m_err )
183 {
184 #if wxMAC_USE_CORE_GRAPHICS
185 CGContextTranslateCTM( pageContext , 0 , rPage.bottom - rPage.top ) ;
186 CGContextScaleCTM( pageContext , 1 , -1 ) ;
187 #else
188 dc->m_macLocalOrigin.x = (int) rPage.left;
189 dc->m_macLocalOrigin.y = (int) rPage.top;
190 #endif
191 }
192 // since this is a non-critical error, we set the flag back
193 m_err = noErr ;
194 }
195 }
196
197 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
198 {
199 if ( m_err )
200 return ;
201
202 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().m_nativePrintData ;
203
204 m_err = PMSessionEndPage(native->m_macPrintSession);
205 if ( m_err != noErr )
206 {
207 PMSessionEndDocument(native->m_macPrintSession);
208 }
209 }
210
211 #else
212
213 class wxMacClassicPrinterDC : public wxNativePrinterDC
214 {
215 public :
216 wxMacClassicPrinterDC( wxPrintData* data ) ;
217 ~wxMacClassicPrinterDC() ;
218 virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) ;
219 virtual void EndDoc( wxPrinterDC* dc ) ;
220 virtual void StartPage( wxPrinterDC* dc ) ;
221 virtual void EndPage( wxPrinterDC* dc ) ;
222 virtual wxCoord GetMaxX() const { return m_maxX ; }
223 virtual wxCoord GetMaxY() const { return m_maxY ; }
224 virtual wxUint32 GetStatus() const { return m_err ; }
225 private :
226 GrafPtr m_macPrintFormerPort ;
227 TPPrPort m_macPrintingPort ;
228 OSErr m_err ;
229 long m_maxX ;
230 long m_maxY ;
231 } ;
232
233 wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
234 {
235 return new wxMacClassicPrinterDC(data) ;
236 }
237
238 wxMacClassicPrinterDC::wxMacClassicPrinterDC(wxPrintData* data)
239 {
240 ::GetPort( &m_macPrintFormerPort ) ;
241 m_err = noErr ;
242 ::UMAPrOpen() ;
243 m_err = PrError() ;
244 if ( m_err != noErr )
245 return;
246
247 wxMacClassicPrintData *native = (wxMacClassicPrintData*) data->m_nativePrintData ;
248
249 if ( ::PrValidate( native->m_macPrintSettings ) )
250 {
251 // the driver has changed in the mean time, should we pop up a page setup dialog ?
252 if ( !::PrStlDialog( native->m_macPrintSettings ) )
253 {
254 m_err = -1 ;
255 return;
256 }
257 }
258 m_err = PrError() ;
259
260 if ( m_err == noErr )
261 {
262 m_maxX = (**native->m_macPrintSettings).prInfo.rPage.right - (**native->m_macPrintSettings).prInfo.rPage.left ;
263 m_maxY = (**native->m_macPrintSettings).prInfo.rPage.bottom - (**native->m_macPrintSettings).prInfo.rPage.top ;
264 }
265 }
266
267 wxMacClassicPrinterDC::~wxMacClassicPrinterDC()
268 {
269 ::UMAPrClose() ;
270 ::SetPort( LMGetWMgrPort() ) ;
271 }
272
273 bool wxMacClassicPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
274 {
275 if ( m_err )
276 return false ;
277
278 wxMacClassicPrintData *native = (wxMacClassicPrintData*) dc->GetPrintData().m_nativePrintData ;
279 m_macPrintingPort = ::PrOpenDoc( native->m_macPrintSettings , NULL , NULL ) ;
280 m_err = PrError() ;
281 if ( m_err )
282 return false ;
283
284 // sets current port
285 dc->m_macPort = (GrafPtr ) m_macPrintingPort ;
286 m_maxX = (**native->m_macPrintSettings).prInfo.rPage.right - (**native->m_macPrintSettings).prInfo.rPage.left ;
287 m_maxY = (**native->m_macPrintSettings).prInfo.rPage.bottom - (**native->m_macPrintSettings).prInfo.rPage.top ;
288 return true ;
289 }
290
291 void wxMacClassicPrinterDC::EndDoc( wxPrinterDC* dc )
292 {
293 if ( m_err )
294 return ;
295
296 PrCloseDoc( m_macPrintingPort ) ;
297 m_err = PrError() ;
298 }
299
300 void wxMacClassicPrinterDC::StartPage( wxPrinterDC* dc )
301 {
302 if ( m_err )
303 return ;
304
305 wxMacClassicPrintData *native = (wxMacClassicPrintData*) dc->GetPrintData().m_nativePrintData ;
306
307 PrOpenPage( m_macPrintingPort , NULL ) ;
308 dc->m_macLocalOrigin.x = (**native->m_macPrintSettings).rPaper.left ;
309 dc->m_macLocalOrigin.y = (**native->m_macPrintSettings).rPaper.top ;
310 // m_macPrintingPort is now the current port
311 Rect clip = { -32000 , -32000 , 32000 , 32000 } ;
312 ::ClipRect( &clip ) ;
313 m_err = PrError() ;
314 if ( m_err != noErr )
315 ::PrCloseDoc( m_macPrintingPort ) ;
316 }
317
318 void wxMacClassicPrinterDC::EndPage( wxPrinterDC* dc )
319 {
320 if ( m_err )
321 return ;
322
323 PrClosePage( m_macPrintingPort ) ;
324 m_err = PrError() ;
325 if ( m_err != noErr )
326 ::PrCloseDoc( m_macPrintingPort ) ;
327 }
328
329 #endif
330
331 wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
332 {
333 m_ok = FALSE ;
334 m_printData = printdata ;
335 m_printData.ConvertToNative() ;
336 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
337 if ( m_nativePrinterDC )
338 {
339 m_ok = m_nativePrinterDC->Ok() ;
340 if ( !m_ok )
341 {
342 wxString message ;
343 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
344 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
345 dialog.ShowModal();
346 }
347 #if wxMAC_USE_CORE_GRAPHICS
348 // the cgContext will only be handed over page by page
349 m_graphicContext = new wxMacCGContext() ;
350 #endif
351 }
352 }
353
354 wxPrinterDC::~wxPrinterDC(void)
355 {
356 delete m_nativePrinterDC ;
357 }
358
359 #if wxMAC_USE_CORE_GRAPHICS
360 void wxPrinterDC::MacSetCGContext( void * cg )
361 {
362 dynamic_cast<wxMacCGContext*>(m_graphicContext)->SetNativeContext( (CGContextRef) cg ) ;
363 m_graphicContext->SetPen( m_pen ) ;
364 m_graphicContext->SetBrush( m_brush ) ;
365 }
366 #endif
367 bool wxPrinterDC::StartDoc( const wxString& message )
368 {
369 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
370
371 if ( !m_ok )
372 return false ;
373
374 if ( m_nativePrinterDC->StartDoc(this, message ) )
375 {
376 // in case we have to do additional things when successful
377 }
378 m_ok = m_nativePrinterDC->Ok() ;
379 if ( !m_ok )
380 {
381 wxString message ;
382 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
383 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
384 dialog.ShowModal();
385 }
386
387 return m_ok ;
388 }
389
390 void wxPrinterDC::EndDoc(void)
391 {
392 if ( !m_ok )
393 return ;
394
395 m_nativePrinterDC->EndDoc( this ) ;
396 m_ok = m_nativePrinterDC->Ok() ;
397
398 if ( !m_ok )
399 {
400 wxString message ;
401 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
402 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
403 dialog.ShowModal();
404 }
405 }
406
407 void wxPrinterDC::StartPage(void)
408 {
409 if ( !m_ok )
410 return ;
411
412 m_logicalFunction = wxCOPY;
413 // m_textAlignment = wxALIGN_TOP_LEFT;
414 m_backgroundMode = wxTRANSPARENT;
415
416 m_textForegroundColour = *wxBLACK;
417 m_textBackgroundColour = *wxWHITE;
418 m_pen = *wxBLACK_PEN;
419 m_font = *wxNORMAL_FONT;
420 m_brush = *wxTRANSPARENT_BRUSH;
421 m_backgroundBrush = *wxWHITE_BRUSH;
422 #if !wxMAC_USE_CORE_GRAPHICS
423 m_macFontInstalled = false ;
424 m_macBrushInstalled = false ;
425 m_macPenInstalled = false ;
426 #endif
427
428 m_nativePrinterDC->StartPage(this) ;
429 m_ok = m_nativePrinterDC->Ok() ;
430
431 }
432
433 void wxPrinterDC::EndPage(void)
434 {
435 if ( !m_ok )
436 return ;
437
438 m_nativePrinterDC->EndPage(this) ;
439 m_ok = m_nativePrinterDC->Ok() ;
440 }
441
442 void wxPrinterDC::DoGetSize(int *width, int *height) const
443 {
444 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
445
446 if ( width )
447 * width = m_nativePrinterDC->GetMaxX() ;
448 if ( height )
449 * height = m_nativePrinterDC->GetMaxY() ;
450 }
451
452