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