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