]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcprint.cpp
carbon cfm fixes
[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
56#if TARGET_CARBON
57
58class wxMacCarbonPrinterDC : public wxNativePrinterDC
59{
60public :
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 ; }
70private :
71 GrafPtr m_macPrintFormerPort ;
72 wxCoord m_maxX ;
73 wxCoord m_maxY ;
74 OSStatus m_err ;
75} ;
76
77wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
72e7876b 78{
746d7582
SC
79 ::GetPort( & m_macPrintFormerPort ) ;
80
81 m_err = noErr ;
82 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->m_nativePrintData ;
eb7f8ac5 83
746d7582
SC
84 PMRect rPage;
85 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
86 if ( m_err != noErr )
87 return;
a689a4d0 88
746d7582
SC
89 m_maxX = wxCoord(rPage.right - rPage.left) ;
90 m_maxY = wxCoord(rPage.bottom - rPage.top);
91}
75411508 92
746d7582
SC
93wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
94{
95 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
96 ::SetPort( m_macPrintFormerPort ) ;
97}
98
99wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
100{
101 return new wxMacCarbonPrinterDC(data) ;
102}
103
eb7f8ac5 104bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
746d7582
SC
105{
106 if ( m_err )
107 return false ;
108
109 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().m_nativePrintData ;
110
20b69855
SC
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
746d7582 120 m_err = PMSessionBeginDocument(native->m_macPrintSession,
eb7f8ac5 121 native->m_macPrintSettings,
746d7582
SC
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
eb7f8ac5
VZ
131 m_maxX = (wxCoord)(rPage.right - rPage.left);
132 m_maxY = (wxCoord)(rPage.bottom - rPage.top);
746d7582
SC
133 return true ;
134}
135
eb7f8ac5 136void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
746d7582
SC
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
eb7f8ac5 146void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
746d7582
SC
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);
eb7f8ac5 156
20b69855
SC
157#if wxMAC_USE_CORE_GRAPHICS
158 CGContextRef pageContext;
159#endif
746d7582 160 if ( m_err == noErr )
e40298d5 161 {
20b69855 162#if wxMAC_USE_CORE_GRAPHICS
eb7f8ac5 163 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
20b69855
SC
164 kPMGraphicsContextCoreGraphics,
165 (void**) &pageContext );
166 dc->MacSetCGContext(pageContext) ;
167#else
168 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
169 kPMGraphicsContextQuickdraw,
eb7f8ac5 170 (void**) &dc->m_macPort );
20b69855 171#endif
e40298d5 172 }
eb7f8ac5 173
746d7582 174 if ( m_err != noErr )
e40298d5 175 {
746d7582
SC
176 PMSessionEndPage(native->m_macPrintSession);
177 PMSessionEndDocument(native->m_macPrintSession);
e40298d5 178 }
746d7582 179 else
e40298d5 180 {
746d7582 181 PMRect rPage;
eb7f8ac5 182
746d7582
SC
183 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
184 if ( !m_err )
185 {
20b69855
SC
186#if wxMAC_USE_CORE_GRAPHICS
187 CGContextTranslateCTM( pageContext , 0 , rPage.bottom - rPage.top ) ;
188 CGContextScaleCTM( pageContext , 1 , -1 ) ;
189#else
eb7f8ac5
VZ
190 dc->m_macLocalOrigin.x = (int) rPage.left;
191 dc->m_macLocalOrigin.y = (int) rPage.top;
20b69855 192#endif
746d7582
SC
193 }
194 // since this is a non-critical error, we set the flag back
195 m_err = noErr ;
e40298d5 196 }
746d7582
SC
197}
198
eb7f8ac5 199void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
746d7582
SC
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 )
f520d381 208 {
746d7582 209 PMSessionEndDocument(native->m_macPrintSession);
f520d381 210 }
746d7582
SC
211}
212
5b781a67 213#else
746d7582
SC
214
215class wxMacClassicPrinterDC : public wxNativePrinterDC
216{
217public :
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 ; }
227private :
228 GrafPtr m_macPrintFormerPort ;
229 TPPrPort m_macPrintingPort ;
230 OSErr m_err ;
231 long m_maxX ;
232 long m_maxY ;
233} ;
234
235wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
236{
237 return new wxMacClassicPrinterDC(data) ;
72e7876b
SC
238}
239
746d7582 240wxMacClassicPrinterDC::wxMacClassicPrinterDC(wxPrintData* data)
72e7876b 241{
746d7582
SC
242 ::GetPort( &m_macPrintFormerPort ) ;
243 m_err = noErr ;
244 ::UMAPrOpen() ;
245 m_err = PrError() ;
246 if ( m_err != noErr )
247 return;
eb7f8ac5 248
746d7582
SC
249 wxMacClassicPrintData *native = (wxMacClassicPrintData*) data->m_nativePrintData ;
250
251 if ( ::PrValidate( native->m_macPrintSettings ) )
e40298d5 252 {
746d7582
SC
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 }
e40298d5 259 }
746d7582
SC
260 m_err = PrError() ;
261
262 if ( m_err == noErr )
e40298d5 263 {
746d7582
SC
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 ;
e40298d5 266 }
72e7876b
SC
267}
268
746d7582 269wxMacClassicPrinterDC::~wxMacClassicPrinterDC()
72e7876b 270{
746d7582
SC
271 ::UMAPrClose() ;
272 ::SetPort( LMGetWMgrPort() ) ;
273}
274
eb7f8ac5 275bool wxMacClassicPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
746d7582
SC
276{
277 if ( m_err )
75411508 278 return false ;
746d7582
SC
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
75411508 286 // sets current port
746d7582
SC
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
eb7f8ac5 293void wxMacClassicPrinterDC::EndDoc( wxPrinterDC* dc )
746d7582
SC
294{
295 if ( m_err )
296 return ;
297
298 PrCloseDoc( m_macPrintingPort ) ;
299 m_err = PrError() ;
300}
301
eb7f8ac5 302void wxMacClassicPrinterDC::StartPage( wxPrinterDC* dc )
746d7582
SC
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
eb7f8ac5 320void wxMacClassicPrinterDC::EndPage( wxPrinterDC* dc )
746d7582
SC
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
333wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
334{
335 m_ok = FALSE ;
336 m_printData = printdata ;
337 m_printData.ConvertToNative() ;
338 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
eb7f8ac5 339 if ( m_nativePrinterDC )
75411508 340 {
746d7582 341 m_ok = m_nativePrinterDC->Ok() ;
746d7582
SC
342 if ( !m_ok )
343 {
344 wxString message ;
eb7f8ac5 345 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
346 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
347 dialog.ShowModal();
348 }
20b69855
SC
349#if wxMAC_USE_CORE_GRAPHICS
350 // the cgContext will only be handed over page by page
351 m_graphicContext = new wxMacCGContext() ;
352#endif
75411508 353 }
746d7582
SC
354}
355
356wxPrinterDC::~wxPrinterDC(void)
357{
358 delete m_nativePrinterDC ;
359}
360
20b69855
SC
361#if wxMAC_USE_CORE_GRAPHICS
362void wxPrinterDC::MacSetCGContext( void * cg )
363{
364 dynamic_cast<wxMacCGContext*>(m_graphicContext)->SetNativeContext( (CGContextRef) cg ) ;
365 m_graphicContext->SetPen( m_pen ) ;
366 m_graphicContext->SetBrush( m_brush ) ;
367}
368#endif
eb7f8ac5 369bool wxPrinterDC::StartDoc( const wxString& message )
746d7582
SC
370{
371 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
eb7f8ac5 372
746d7582
SC
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 )
75411508 382 {
746d7582 383 wxString message ;
eb7f8ac5 384 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
427ff662 385 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
75411508 386 dialog.ShowModal();
75411508 387 }
746d7582 388
e40298d5 389 return m_ok ;
72e7876b
SC
390}
391
eb7f8ac5 392void wxPrinterDC::EndDoc(void)
72e7876b 393{
746d7582
SC
394 if ( !m_ok )
395 return ;
396
397 m_nativePrinterDC->EndDoc( this ) ;
398 m_ok = m_nativePrinterDC->Ok() ;
399
400 if ( !m_ok )
75411508 401 {
746d7582 402 wxString message ;
eb7f8ac5 403 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
404 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
405 dialog.ShowModal();
75411508 406 }
72e7876b
SC
407}
408
eb7f8ac5 409void wxPrinterDC::StartPage(void)
72e7876b 410{
e40298d5
JS
411 if ( !m_ok )
412 return ;
2f1ae414 413
746d7582
SC
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;
20b69855 424#if !wxMAC_USE_CORE_GRAPHICS
e40298d5
JS
425 m_macFontInstalled = false ;
426 m_macBrushInstalled = false ;
427 m_macPenInstalled = false ;
20b69855 428#endif
2f1ae414 429
746d7582
SC
430 m_nativePrinterDC->StartPage(this) ;
431 m_ok = m_nativePrinterDC->Ok() ;
eb7f8ac5 432
72e7876b
SC
433}
434
eb7f8ac5 435void wxPrinterDC::EndPage(void)
72e7876b 436{
e40298d5
JS
437 if ( !m_ok )
438 return ;
72e7876b 439
746d7582
SC
440 m_nativePrinterDC->EndPage(this) ;
441 m_ok = m_nativePrinterDC->Ok() ;
442}
72e7876b 443
746d7582
SC
444void wxPrinterDC::DoGetSize(int *width, int *height) const
445{
446 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
2f1ae414 447
746d7582
SC
448 if ( width )
449 * width = m_nativePrinterDC->GetMaxX() ;
450 if ( height )
451 * height = m_nativePrinterDC->GetMaxY() ;
72e7876b 452}
746d7582 453
179e085f 454#endif