]> git.saurik.com Git - wxWidgets.git/blame - src/mac/dcprint.cpp
made wxFFile a bit more safe: don't crash when Tell() and Length() are called on...
[wxWidgets.git] / src / mac / 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
e40298d5 9// Licence: wxWindows licence
72e7876b
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
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
GD
27#include "wx/msgdlg.h"
28#include <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
109 m_err = PMSessionBeginDocument(native->m_macPrintSession,
eb7f8ac5 110 native->m_macPrintSettings,
746d7582
SC
111 native->m_macPageFormat);
112 if ( m_err != noErr )
113 return false;
114
115 PMRect rPage;
116 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
117 if ( m_err != noErr )
118 return false;
119
eb7f8ac5
VZ
120 m_maxX = (wxCoord)(rPage.right - rPage.left);
121 m_maxY = (wxCoord)(rPage.bottom - rPage.top);
746d7582
SC
122 return true ;
123}
124
eb7f8ac5 125void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
746d7582
SC
126{
127 if ( m_err )
128 return ;
129
130 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().m_nativePrintData ;
131
132 m_err = PMSessionEndDocument(native->m_macPrintSession);
133}
134
eb7f8ac5 135void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
746d7582
SC
136{
137 if ( m_err )
138 return ;
139
140 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().m_nativePrintData ;
141
142 m_err = PMSessionBeginPage(native->m_macPrintSession,
143 native->m_macPageFormat,
144 nil);
eb7f8ac5 145
746d7582 146 if ( m_err == noErr )
e40298d5 147 {
eb7f8ac5
VZ
148 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
149 nil,
150 (void**) &dc->m_macPort );
e40298d5 151 }
eb7f8ac5 152
746d7582 153 if ( m_err != noErr )
e40298d5 154 {
746d7582
SC
155 PMSessionEndPage(native->m_macPrintSession);
156 PMSessionEndDocument(native->m_macPrintSession);
e40298d5 157 }
746d7582 158 else
e40298d5 159 {
746d7582 160 PMRect rPage;
eb7f8ac5 161
746d7582
SC
162 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
163 if ( !m_err )
164 {
eb7f8ac5
VZ
165 dc->m_macLocalOrigin.x = (int) rPage.left;
166 dc->m_macLocalOrigin.y = (int) rPage.top;
746d7582
SC
167 }
168 // since this is a non-critical error, we set the flag back
169 m_err = noErr ;
e40298d5 170 }
746d7582
SC
171}
172
eb7f8ac5 173void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
746d7582
SC
174{
175 if ( m_err )
176 return ;
177
178 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().m_nativePrintData ;
179
180 m_err = PMSessionEndPage(native->m_macPrintSession);
181 if ( m_err != noErr )
f520d381 182 {
746d7582 183 PMSessionEndDocument(native->m_macPrintSession);
f520d381 184 }
746d7582
SC
185}
186
5b781a67 187#else
746d7582
SC
188
189class wxMacClassicPrinterDC : public wxNativePrinterDC
190{
191public :
192 wxMacClassicPrinterDC( wxPrintData* data ) ;
193 ~wxMacClassicPrinterDC() ;
194 virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) ;
195 virtual void EndDoc( wxPrinterDC* dc ) ;
196 virtual void StartPage( wxPrinterDC* dc ) ;
197 virtual void EndPage( wxPrinterDC* dc ) ;
198 virtual wxCoord GetMaxX() const { return m_maxX ; }
199 virtual wxCoord GetMaxY() const { return m_maxY ; }
200 virtual wxUint32 GetStatus() const { return m_err ; }
201private :
202 GrafPtr m_macPrintFormerPort ;
203 TPPrPort m_macPrintingPort ;
204 OSErr m_err ;
205 long m_maxX ;
206 long m_maxY ;
207} ;
208
209wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
210{
211 return new wxMacClassicPrinterDC(data) ;
72e7876b
SC
212}
213
746d7582 214wxMacClassicPrinterDC::wxMacClassicPrinterDC(wxPrintData* data)
72e7876b 215{
746d7582
SC
216 ::GetPort( &m_macPrintFormerPort ) ;
217 m_err = noErr ;
218 ::UMAPrOpen() ;
219 m_err = PrError() ;
220 if ( m_err != noErr )
221 return;
eb7f8ac5 222
746d7582
SC
223 wxMacClassicPrintData *native = (wxMacClassicPrintData*) data->m_nativePrintData ;
224
225 if ( ::PrValidate( native->m_macPrintSettings ) )
e40298d5 226 {
746d7582
SC
227 // the driver has changed in the mean time, should we pop up a page setup dialog ?
228 if ( !::PrStlDialog( native->m_macPrintSettings ) )
229 {
230 m_err = -1 ;
231 return;
232 }
e40298d5 233 }
746d7582
SC
234 m_err = PrError() ;
235
236 if ( m_err == noErr )
e40298d5 237 {
746d7582
SC
238 m_maxX = (**native->m_macPrintSettings).prInfo.rPage.right - (**native->m_macPrintSettings).prInfo.rPage.left ;
239 m_maxY = (**native->m_macPrintSettings).prInfo.rPage.bottom - (**native->m_macPrintSettings).prInfo.rPage.top ;
e40298d5 240 }
72e7876b
SC
241}
242
746d7582 243wxMacClassicPrinterDC::~wxMacClassicPrinterDC()
72e7876b 244{
746d7582
SC
245 ::UMAPrClose() ;
246 ::SetPort( LMGetWMgrPort() ) ;
247}
248
eb7f8ac5 249bool wxMacClassicPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
746d7582
SC
250{
251 if ( m_err )
75411508 252 return false ;
746d7582
SC
253
254 wxMacClassicPrintData *native = (wxMacClassicPrintData*) dc->GetPrintData().m_nativePrintData ;
255 m_macPrintingPort = ::PrOpenDoc( native->m_macPrintSettings , NULL , NULL ) ;
256 m_err = PrError() ;
257 if ( m_err )
258 return false ;
259
75411508 260 // sets current port
746d7582
SC
261 dc->m_macPort = (GrafPtr ) m_macPrintingPort ;
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 return true ;
265}
266
eb7f8ac5 267void wxMacClassicPrinterDC::EndDoc( wxPrinterDC* dc )
746d7582
SC
268{
269 if ( m_err )
270 return ;
271
272 PrCloseDoc( m_macPrintingPort ) ;
273 m_err = PrError() ;
274}
275
eb7f8ac5 276void wxMacClassicPrinterDC::StartPage( wxPrinterDC* dc )
746d7582
SC
277{
278 if ( m_err )
279 return ;
280
281 wxMacClassicPrintData *native = (wxMacClassicPrintData*) dc->GetPrintData().m_nativePrintData ;
282
283 PrOpenPage( m_macPrintingPort , NULL ) ;
284 dc->m_macLocalOrigin.x = (**native->m_macPrintSettings).rPaper.left ;
285 dc->m_macLocalOrigin.y = (**native->m_macPrintSettings).rPaper.top ;
286 // m_macPrintingPort is now the current port
287 Rect clip = { -32000 , -32000 , 32000 , 32000 } ;
288 ::ClipRect( &clip ) ;
289 m_err = PrError() ;
290 if ( m_err != noErr )
291 ::PrCloseDoc( m_macPrintingPort ) ;
292}
293
eb7f8ac5 294void wxMacClassicPrinterDC::EndPage( wxPrinterDC* dc )
746d7582
SC
295{
296 if ( m_err )
297 return ;
298
299 PrClosePage( m_macPrintingPort ) ;
300 m_err = PrError() ;
301 if ( m_err != noErr )
302 ::PrCloseDoc( m_macPrintingPort ) ;
303}
304
305#endif
306
307wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
308{
309 m_ok = FALSE ;
310 m_printData = printdata ;
311 m_printData.ConvertToNative() ;
312 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
eb7f8ac5 313 if ( m_nativePrinterDC )
75411508 314 {
746d7582 315 m_ok = m_nativePrinterDC->Ok() ;
eb7f8ac5 316
746d7582
SC
317 if ( !m_ok )
318 {
319 wxString message ;
eb7f8ac5 320 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
321 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
322 dialog.ShowModal();
323 }
75411508 324 }
746d7582
SC
325}
326
327wxPrinterDC::~wxPrinterDC(void)
328{
329 delete m_nativePrinterDC ;
330}
331
eb7f8ac5 332bool wxPrinterDC::StartDoc( const wxString& message )
746d7582
SC
333{
334 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
eb7f8ac5 335
746d7582
SC
336 if ( !m_ok )
337 return false ;
338
339 if ( m_nativePrinterDC->StartDoc(this, message ) )
340 {
341 // in case we have to do additional things when successful
342 }
343 m_ok = m_nativePrinterDC->Ok() ;
344 if ( !m_ok )
75411508 345 {
746d7582 346 wxString message ;
eb7f8ac5 347 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
427ff662 348 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
75411508 349 dialog.ShowModal();
75411508 350 }
746d7582 351
e40298d5 352 return m_ok ;
72e7876b
SC
353}
354
eb7f8ac5 355void wxPrinterDC::EndDoc(void)
72e7876b 356{
746d7582
SC
357 if ( !m_ok )
358 return ;
359
360 m_nativePrinterDC->EndDoc( this ) ;
361 m_ok = m_nativePrinterDC->Ok() ;
362
363 if ( !m_ok )
75411508 364 {
746d7582 365 wxString message ;
eb7f8ac5 366 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
367 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
368 dialog.ShowModal();
75411508 369 }
72e7876b
SC
370}
371
eb7f8ac5 372void wxPrinterDC::StartPage(void)
72e7876b 373{
e40298d5
JS
374 if ( !m_ok )
375 return ;
2f1ae414 376
746d7582
SC
377 m_logicalFunction = wxCOPY;
378 // m_textAlignment = wxALIGN_TOP_LEFT;
379 m_backgroundMode = wxTRANSPARENT;
380
381 m_textForegroundColour = *wxBLACK;
382 m_textBackgroundColour = *wxWHITE;
383 m_pen = *wxBLACK_PEN;
384 m_font = *wxNORMAL_FONT;
385 m_brush = *wxTRANSPARENT_BRUSH;
386 m_backgroundBrush = *wxWHITE_BRUSH;
387
e40298d5
JS
388 m_macFontInstalled = false ;
389 m_macBrushInstalled = false ;
390 m_macPenInstalled = false ;
2f1ae414 391
746d7582
SC
392 m_nativePrinterDC->StartPage(this) ;
393 m_ok = m_nativePrinterDC->Ok() ;
eb7f8ac5 394
72e7876b
SC
395}
396
eb7f8ac5 397void wxPrinterDC::EndPage(void)
72e7876b 398{
e40298d5
JS
399 if ( !m_ok )
400 return ;
72e7876b 401
746d7582
SC
402 m_nativePrinterDC->EndPage(this) ;
403 m_ok = m_nativePrinterDC->Ok() ;
404}
72e7876b 405
746d7582
SC
406void wxPrinterDC::DoGetSize(int *width, int *height) const
407{
408 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
2f1ae414 409
746d7582
SC
410 if ( width )
411 * width = m_nativePrinterDC->GetMaxX() ;
412 if ( height )
413 * height = m_nativePrinterDC->GetMaxY() ;
72e7876b 414}
746d7582
SC
415
416