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