]>
Commit | Line | Data |
---|---|---|
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 | 33 | IMPLEMENT_CLASS(wxPrinterDC, wxDC) |
2f1ae414 | 34 | #endif |
72e7876b | 35 | |
746d7582 SC |
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 | } ; | |
72e7876b | 53 | |
746d7582 SC |
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 ) | |
72e7876b | 76 | { |
746d7582 SC |
77 | ::GetPort( & m_macPrintFormerPort ) ; |
78 | ||
79 | m_err = noErr ; | |
80 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->m_nativePrintData ; | |
e40298d5 | 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 |
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 = rPage.right - rPage.left ; | |
121 | m_maxY = 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 ) | |
e40298d5 | 147 | { |
746d7582 SC |
148 | m_err = PMSessionGetGraphicsContext(native->m_macPrintSession, |
149 | nil, | |
150 | (void**) &dc->m_macPort ); | |
e40298d5 JS |
151 | } |
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 SC |
160 | PMRect rPage; |
161 | ||
162 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); | |
163 | if ( !m_err ) | |
164 | { | |
165 | dc->m_macLocalOrigin.x = rPage.left ; | |
166 | dc->m_macLocalOrigin.y = rPage.top ; | |
167 | } | |
168 | // since this is a non-critical error, we set the flag back | |
169 | m_err = noErr ; | |
e40298d5 | 170 | } |
746d7582 SC |
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 ) | |
f520d381 | 182 | { |
746d7582 | 183 | PMSessionEndDocument(native->m_macPrintSession); |
f520d381 | 184 | } |
746d7582 SC |
185 | } |
186 | ||
5b781a67 | 187 | #else |
746d7582 SC |
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) ; | |
72e7876b SC |
212 | } |
213 | ||
746d7582 | 214 | wxMacClassicPrinterDC::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; | |
222 | ||
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 | 243 | wxMacClassicPrinterDC::~wxMacClassicPrinterDC() |
72e7876b | 244 | { |
746d7582 SC |
245 | ::UMAPrClose() ; |
246 | ::SetPort( LMGetWMgrPort() ) ; | |
247 | } | |
248 | ||
249 | bool wxMacClassicPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) ) | |
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 | ||
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 ) | |
75411508 | 314 | { |
746d7582 SC |
315 | m_ok = m_nativePrinterDC->Ok() ; |
316 | ||
317 | if ( !m_ok ) | |
318 | { | |
319 | wxString message ; | |
320 | message.Printf( wxT("Print Error %ld"), m_nativePrinterDC->GetStatus() ) ; | |
321 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; | |
322 | dialog.ShowModal(); | |
323 | } | |
75411508 | 324 | } |
746d7582 SC |
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") ) ; | |
75411508 | 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 SC |
346 | wxString message ; |
347 | message.Printf( wxT("Print Error %ld"), 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 | ||
355 | void wxPrinterDC::EndDoc(void) | |
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 SC |
365 | wxString message ; |
366 | message.Printf( wxT("Print Error %ld"), m_nativePrinterDC->GetStatus() ) ; | |
367 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; | |
368 | dialog.ShowModal(); | |
75411508 | 369 | } |
72e7876b SC |
370 | } |
371 | ||
372 | void wxPrinterDC::StartPage(void) | |
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() ; | |
e40298d5 | 394 | |
72e7876b SC |
395 | } |
396 | ||
397 | void wxPrinterDC::EndPage(void) | |
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 |
406 | void 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 |