]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcprint.cpp
Patch by David Brinegar to fix menubar updating after modal dialog shown
[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
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#endif
25
26#include "wx/dcprint.h"
03e11df5 27#include "wx/msgdlg.h"
463c4d71 28#include "wx/math.h"
2f1ae414 29#include "wx/mac/uma.h"
746d7582 30#include "wx/mac/private/print.h"
a3d3d3bf 31
2f1ae414 32#if !USE_SHARED_LIBRARY
72e7876b 33IMPLEMENT_CLASS(wxPrinterDC, wxDC)
2f1ae414 34#endif
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
54#if TARGET_CARBON
55
56class wxMacCarbonPrinterDC : public wxNativePrinterDC
57{
58public :
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 ; }
68private :
69 GrafPtr m_macPrintFormerPort ;
70 wxCoord m_maxX ;
71 wxCoord m_maxY ;
72 OSStatus m_err ;
73} ;
74
75wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
72e7876b 76{
746d7582
SC
77 ::GetPort( & m_macPrintFormerPort ) ;
78
79 m_err = noErr ;
80 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->m_nativePrintData ;
eb7f8ac5 81
746d7582
SC
82 PMRect rPage;
83 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
84 if ( m_err != noErr )
85 return;
a689a4d0 86
746d7582
SC
87 m_maxX = wxCoord(rPage.right - rPage.left) ;
88 m_maxY = wxCoord(rPage.bottom - rPage.top);
89}
75411508 90
746d7582
SC
91wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
92{
93 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
94 ::SetPort( m_macPrintFormerPort ) ;
95}
96
97wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
98{
99 return new wxMacCarbonPrinterDC(data) ;
100}
101
eb7f8ac5 102bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
746d7582
SC
103{
104 if ( m_err )
105 return false ;
106
107 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().m_nativePrintData ;
108
20b69855
SC
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
746d7582 118 m_err = PMSessionBeginDocument(native->m_macPrintSession,
eb7f8ac5 119 native->m_macPrintSettings,
746d7582
SC
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
eb7f8ac5
VZ
129 m_maxX = (wxCoord)(rPage.right - rPage.left);
130 m_maxY = (wxCoord)(rPage.bottom - rPage.top);
746d7582
SC
131 return true ;
132}
133
eb7f8ac5 134void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
746d7582
SC
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
eb7f8ac5 144void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
746d7582
SC
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);
eb7f8ac5 154
20b69855
SC
155#if wxMAC_USE_CORE_GRAPHICS
156 CGContextRef pageContext;
157#endif
746d7582 158 if ( m_err == noErr )
e40298d5 159 {
20b69855 160#if wxMAC_USE_CORE_GRAPHICS
eb7f8ac5 161 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
20b69855
SC
162 kPMGraphicsContextCoreGraphics,
163 (void**) &pageContext );
164 dc->MacSetCGContext(pageContext) ;
165#else
166 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
167 kPMGraphicsContextQuickdraw,
eb7f8ac5 168 (void**) &dc->m_macPort );
20b69855 169#endif
e40298d5 170 }
eb7f8ac5 171
746d7582 172 if ( m_err != noErr )
e40298d5 173 {
746d7582
SC
174 PMSessionEndPage(native->m_macPrintSession);
175 PMSessionEndDocument(native->m_macPrintSession);
e40298d5 176 }
746d7582 177 else
e40298d5 178 {
746d7582 179 PMRect rPage;
eb7f8ac5 180
746d7582
SC
181 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
182 if ( !m_err )
183 {
20b69855
SC
184#if wxMAC_USE_CORE_GRAPHICS
185 CGContextTranslateCTM( pageContext , 0 , rPage.bottom - rPage.top ) ;
186 CGContextScaleCTM( pageContext , 1 , -1 ) ;
187#else
eb7f8ac5
VZ
188 dc->m_macLocalOrigin.x = (int) rPage.left;
189 dc->m_macLocalOrigin.y = (int) rPage.top;
20b69855 190#endif
746d7582
SC
191 }
192 // since this is a non-critical error, we set the flag back
193 m_err = noErr ;
e40298d5 194 }
746d7582
SC
195}
196
eb7f8ac5 197void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
746d7582
SC
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 )
f520d381 206 {
746d7582 207 PMSessionEndDocument(native->m_macPrintSession);
f520d381 208 }
746d7582
SC
209}
210
5b781a67 211#else
746d7582
SC
212
213class wxMacClassicPrinterDC : public wxNativePrinterDC
214{
215public :
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 ; }
225private :
226 GrafPtr m_macPrintFormerPort ;
227 TPPrPort m_macPrintingPort ;
228 OSErr m_err ;
229 long m_maxX ;
230 long m_maxY ;
231} ;
232
233wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
234{
235 return new wxMacClassicPrinterDC(data) ;
72e7876b
SC
236}
237
746d7582 238wxMacClassicPrinterDC::wxMacClassicPrinterDC(wxPrintData* data)
72e7876b 239{
746d7582
SC
240 ::GetPort( &m_macPrintFormerPort ) ;
241 m_err = noErr ;
242 ::UMAPrOpen() ;
243 m_err = PrError() ;
244 if ( m_err != noErr )
245 return;
eb7f8ac5 246
746d7582
SC
247 wxMacClassicPrintData *native = (wxMacClassicPrintData*) data->m_nativePrintData ;
248
249 if ( ::PrValidate( native->m_macPrintSettings ) )
e40298d5 250 {
746d7582
SC
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 }
e40298d5 257 }
746d7582
SC
258 m_err = PrError() ;
259
260 if ( m_err == noErr )
e40298d5 261 {
746d7582
SC
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 ;
e40298d5 264 }
72e7876b
SC
265}
266
746d7582 267wxMacClassicPrinterDC::~wxMacClassicPrinterDC()
72e7876b 268{
746d7582
SC
269 ::UMAPrClose() ;
270 ::SetPort( LMGetWMgrPort() ) ;
271}
272
eb7f8ac5 273bool wxMacClassicPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
746d7582
SC
274{
275 if ( m_err )
75411508 276 return false ;
746d7582
SC
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
75411508 284 // sets current port
746d7582
SC
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
eb7f8ac5 291void wxMacClassicPrinterDC::EndDoc( wxPrinterDC* dc )
746d7582
SC
292{
293 if ( m_err )
294 return ;
295
296 PrCloseDoc( m_macPrintingPort ) ;
297 m_err = PrError() ;
298}
299
eb7f8ac5 300void wxMacClassicPrinterDC::StartPage( wxPrinterDC* dc )
746d7582
SC
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
eb7f8ac5 318void wxMacClassicPrinterDC::EndPage( wxPrinterDC* dc )
746d7582
SC
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
331wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
332{
333 m_ok = FALSE ;
334 m_printData = printdata ;
335 m_printData.ConvertToNative() ;
336 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
eb7f8ac5 337 if ( m_nativePrinterDC )
75411508 338 {
746d7582 339 m_ok = m_nativePrinterDC->Ok() ;
746d7582
SC
340 if ( !m_ok )
341 {
342 wxString message ;
eb7f8ac5 343 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
344 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
345 dialog.ShowModal();
346 }
20b69855
SC
347#if wxMAC_USE_CORE_GRAPHICS
348 // the cgContext will only be handed over page by page
349 m_graphicContext = new wxMacCGContext() ;
350#endif
75411508 351 }
746d7582
SC
352}
353
354wxPrinterDC::~wxPrinterDC(void)
355{
356 delete m_nativePrinterDC ;
357}
358
20b69855
SC
359#if wxMAC_USE_CORE_GRAPHICS
360void 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
eb7f8ac5 367bool wxPrinterDC::StartDoc( const wxString& message )
746d7582
SC
368{
369 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
eb7f8ac5 370
746d7582
SC
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 )
75411508 380 {
746d7582 381 wxString message ;
eb7f8ac5 382 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
427ff662 383 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
75411508 384 dialog.ShowModal();
75411508 385 }
746d7582 386
e40298d5 387 return m_ok ;
72e7876b
SC
388}
389
eb7f8ac5 390void wxPrinterDC::EndDoc(void)
72e7876b 391{
746d7582
SC
392 if ( !m_ok )
393 return ;
394
395 m_nativePrinterDC->EndDoc( this ) ;
396 m_ok = m_nativePrinterDC->Ok() ;
397
398 if ( !m_ok )
75411508 399 {
746d7582 400 wxString message ;
eb7f8ac5 401 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
402 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
403 dialog.ShowModal();
75411508 404 }
72e7876b
SC
405}
406
eb7f8ac5 407void wxPrinterDC::StartPage(void)
72e7876b 408{
e40298d5
JS
409 if ( !m_ok )
410 return ;
2f1ae414 411
746d7582
SC
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;
20b69855 422#if !wxMAC_USE_CORE_GRAPHICS
e40298d5
JS
423 m_macFontInstalled = false ;
424 m_macBrushInstalled = false ;
425 m_macPenInstalled = false ;
20b69855 426#endif
2f1ae414 427
746d7582
SC
428 m_nativePrinterDC->StartPage(this) ;
429 m_ok = m_nativePrinterDC->Ok() ;
eb7f8ac5 430
72e7876b
SC
431}
432
eb7f8ac5 433void wxPrinterDC::EndPage(void)
72e7876b 434{
e40298d5
JS
435 if ( !m_ok )
436 return ;
72e7876b 437
746d7582
SC
438 m_nativePrinterDC->EndPage(this) ;
439 m_ok = m_nativePrinterDC->Ok() ;
440}
72e7876b 441
746d7582
SC
442void wxPrinterDC::DoGetSize(int *width, int *height) const
443{
444 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
2f1ae414 445
746d7582
SC
446 if ( width )
447 * width = m_nativePrinterDC->GetMaxX() ;
448 if ( height )
449 * height = m_nativePrinterDC->GetMaxY() ;
72e7876b 450}
746d7582
SC
451
452