]> git.saurik.com Git - wxWidgets.git/blob - src/mac/dcprint.cpp
Fixed DDE memory leaks.
[wxWidgets.git] / src / mac / 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 #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"
28 #include <math.h>
29 #include "wx/mac/uma.h"
30 #include "wx/mac/private/print.h"
31
32 #if !USE_SHARED_LIBRARY
33 IMPLEMENT_CLASS(wxPrinterDC, wxDC)
34 #endif
35
36 class wxNativePrinterDC
37 {
38 public :
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 ; }
50
51 static wxNativePrinterDC* Create(wxPrintData* data) ;
52 } ;
53
54 #if TARGET_CARBON
55
56 class wxMacCarbonPrinterDC : public wxNativePrinterDC
57 {
58 public :
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 ; }
68 private :
69 GrafPtr m_macPrintFormerPort ;
70 wxCoord m_maxX ;
71 wxCoord m_maxY ;
72 OSStatus m_err ;
73 } ;
74
75 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
76 {
77 ::GetPort( & m_macPrintFormerPort ) ;
78
79 m_err = noErr ;
80 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->m_nativePrintData ;
81
82 PMRect rPage;
83 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
84 if ( m_err != noErr )
85 return;
86
87 m_maxX = wxCoord(rPage.right - rPage.left) ;
88 m_maxY = wxCoord(rPage.bottom - rPage.top);
89 }
90
91 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
92 {
93 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
94 ::SetPort( m_macPrintFormerPort ) ;
95 }
96
97 wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
98 {
99 return new wxMacCarbonPrinterDC(data) ;
100 }
101
102 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
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,
110 native->m_macPrintSettings,
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
120 m_maxX = (wxCoord)(rPage.right - rPage.left);
121 m_maxY = (wxCoord)(rPage.bottom - rPage.top);
122 return true ;
123 }
124
125 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
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
135 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
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);
145
146 if ( m_err == noErr )
147 {
148 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
149 nil,
150 (void**) &dc->m_macPort );
151 }
152
153 if ( m_err != noErr )
154 {
155 PMSessionEndPage(native->m_macPrintSession);
156 PMSessionEndDocument(native->m_macPrintSession);
157 }
158 else
159 {
160 PMRect rPage;
161
162 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
163 if ( !m_err )
164 {
165 dc->m_macLocalOrigin.x = (int) rPage.left;
166 dc->m_macLocalOrigin.y = (int) rPage.top;
167 }
168 // since this is a non-critical error, we set the flag back
169 m_err = noErr ;
170 }
171 }
172
173 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
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 )
182 {
183 PMSessionEndDocument(native->m_macPrintSession);
184 }
185 }
186
187 #else
188
189 class wxMacClassicPrinterDC : public wxNativePrinterDC
190 {
191 public :
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 ; }
201 private :
202 GrafPtr m_macPrintFormerPort ;
203 TPPrPort m_macPrintingPort ;
204 OSErr m_err ;
205 long m_maxX ;
206 long m_maxY ;
207 } ;
208
209 wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
210 {
211 return new wxMacClassicPrinterDC(data) ;
212 }
213
214 wxMacClassicPrinterDC::wxMacClassicPrinterDC(wxPrintData* data)
215 {
216 ::GetPort( &m_macPrintFormerPort ) ;
217 m_err = noErr ;
218 ::UMAPrOpen() ;
219 m_err = PrError() ;
220 if ( m_err != noErr )
221 return;
222
223 wxMacClassicPrintData *native = (wxMacClassicPrintData*) data->m_nativePrintData ;
224
225 if ( ::PrValidate( native->m_macPrintSettings ) )
226 {
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 }
233 }
234 m_err = PrError() ;
235
236 if ( m_err == noErr )
237 {
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 ;
240 }
241 }
242
243 wxMacClassicPrinterDC::~wxMacClassicPrinterDC()
244 {
245 ::UMAPrClose() ;
246 ::SetPort( LMGetWMgrPort() ) ;
247 }
248
249 bool wxMacClassicPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
250 {
251 if ( m_err )
252 return false ;
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
260 // sets current port
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
267 void wxMacClassicPrinterDC::EndDoc( wxPrinterDC* dc )
268 {
269 if ( m_err )
270 return ;
271
272 PrCloseDoc( m_macPrintingPort ) ;
273 m_err = PrError() ;
274 }
275
276 void wxMacClassicPrinterDC::StartPage( wxPrinterDC* dc )
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
294 void wxMacClassicPrinterDC::EndPage( wxPrinterDC* dc )
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
307 wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
308 {
309 m_ok = FALSE ;
310 m_printData = printdata ;
311 m_printData.ConvertToNative() ;
312 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
313 if ( m_nativePrinterDC )
314 {
315 m_ok = m_nativePrinterDC->Ok() ;
316
317 if ( !m_ok )
318 {
319 wxString message ;
320 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
321 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
322 dialog.ShowModal();
323 }
324 }
325 }
326
327 wxPrinterDC::~wxPrinterDC(void)
328 {
329 delete m_nativePrinterDC ;
330 }
331
332 bool wxPrinterDC::StartDoc( const wxString& message )
333 {
334 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
335
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 )
345 {
346 wxString message ;
347 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
348 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
349 dialog.ShowModal();
350 }
351
352 return m_ok ;
353 }
354
355 void wxPrinterDC::EndDoc(void)
356 {
357 if ( !m_ok )
358 return ;
359
360 m_nativePrinterDC->EndDoc( this ) ;
361 m_ok = m_nativePrinterDC->Ok() ;
362
363 if ( !m_ok )
364 {
365 wxString message ;
366 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
367 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
368 dialog.ShowModal();
369 }
370 }
371
372 void wxPrinterDC::StartPage(void)
373 {
374 if ( !m_ok )
375 return ;
376
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
388 m_macFontInstalled = false ;
389 m_macBrushInstalled = false ;
390 m_macPenInstalled = false ;
391
392 m_nativePrinterDC->StartPage(this) ;
393 m_ok = m_nativePrinterDC->Ok() ;
394
395 }
396
397 void wxPrinterDC::EndPage(void)
398 {
399 if ( !m_ok )
400 return ;
401
402 m_nativePrinterDC->EndPage(this) ;
403 m_ok = m_nativePrinterDC->Ok() ;
404 }
405
406 void wxPrinterDC::DoGetSize(int *width, int *height) const
407 {
408 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
409
410 if ( width )
411 * width = m_nativePrinterDC->GetMaxX() ;
412 if ( height )
413 * height = m_nativePrinterDC->GetMaxY() ;
414 }
415
416