]>
Commit | Line | Data |
---|---|---|
2646f485 | 1 | ///////////////////////////////////////////////////////////////////////////// |
18f3decb | 2 | // Name: src/mac/classic/printwin.cpp |
2646f485 SC |
3 | // Purpose: wxMacPrinter framework |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
18f3decb | 9 | // Licence: wxWindows licence |
2646f485 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
2646f485 SC |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
18f3decb | 16 | #pragma hdrstop |
2646f485 SC |
17 | #endif |
18 | ||
2646f485 | 19 | #ifndef WX_PRECOMP |
18f3decb WS |
20 | #include "wx/utils.h" |
21 | #include "wx/dc.h" | |
22 | #include "wx/app.h" | |
23 | #include "wx/msgdlg.h" | |
6d50343d | 24 | #include "wx/dcprint.h" |
2646f485 SC |
25 | #endif |
26 | ||
27 | #include "wx/mac/uma.h" | |
28 | ||
29 | #include "wx/mac/printmac.h" | |
30 | #include "wx/mac/private/print.h" | |
31 | ||
32 | #define mm2pt 2.83464566929 | |
33 | #define pt2mm 0.352777777778 | |
34 | ||
2646f485 SC |
35 | #include "wx/printdlg.h" |
36 | ||
37 | #include <stdlib.h> | |
38 | ||
2646f485 SC |
39 | IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase) |
40 | IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase) | |
2646f485 SC |
41 | |
42 | #if TARGET_CARBON | |
43 | ||
18f3decb | 44 | wxNativePrintData* wxNativePrintData::Create() |
2646f485 SC |
45 | { |
46 | return new wxMacCarbonPrintData() ; | |
47 | } | |
48 | ||
49 | wxMacCarbonPrintData::wxMacCarbonPrintData() | |
50 | { | |
51 | m_macPageFormat = kPMNoPageFormat; | |
52 | m_macPrintSettings = kPMNoPrintSettings; | |
53 | m_macPrintSession = kPMNoReference ; | |
54 | ValidateOrCreate() ; | |
55 | } | |
56 | ||
57 | wxMacCarbonPrintData::~wxMacCarbonPrintData() | |
58 | { | |
59 | if (m_macPageFormat != kPMNoPageFormat) | |
60 | { | |
61 | (void)PMRelease(m_macPageFormat); | |
62 | m_macPageFormat = kPMNoPageFormat; | |
63 | } | |
64 | ||
65 | if (m_macPrintSettings != kPMNoPrintSettings) | |
66 | { | |
67 | (void)PMRelease(m_macPrintSettings); | |
68 | m_macPrintSettings = kPMNoPrintSettings; | |
69 | } | |
18f3decb | 70 | |
2646f485 SC |
71 | if ( m_macPrintSession != kPMNoReference ) |
72 | { | |
73 | (void)PMRelease(m_macPrintSession); | |
74 | m_macPrintSession = kPMNoReference; | |
75 | } | |
76 | } | |
77 | ||
18f3decb | 78 | void wxMacCarbonPrintData::ValidateOrCreate() |
2646f485 SC |
79 | { |
80 | OSStatus err = noErr ; | |
81 | if ( m_macPrintSession == kPMNoReference ) | |
82 | { | |
83 | err = PMCreateSession( (PMPrintSession *) &m_macPrintSession ) ; | |
84 | } | |
85 | // Set up a valid PageFormat object. | |
86 | if ( m_macPageFormat == kPMNoPageFormat) | |
87 | { | |
88 | err = PMCreatePageFormat((PMPageFormat *) &m_macPageFormat); | |
18f3decb | 89 | |
2646f485 SC |
90 | // Note that PMPageFormat is not session-specific, but calling |
91 | // PMSessionDefaultPageFormat assigns values specific to the printer | |
92 | // associated with the current printing session. | |
93 | if ((err == noErr) && | |
94 | ( m_macPageFormat != kPMNoPageFormat)) | |
95 | { | |
96 | err = PMSessionDefaultPageFormat((PMPrintSession) m_macPrintSession, | |
97 | (PMPageFormat) m_macPageFormat); | |
98 | } | |
99 | } | |
100 | else | |
101 | { | |
102 | err = PMSessionValidatePageFormat((PMPrintSession) m_macPrintSession, | |
103 | (PMPageFormat) m_macPageFormat, | |
104 | kPMDontWantBoolean); | |
105 | } | |
18f3decb | 106 | |
2646f485 SC |
107 | // Set up a valid PrintSettings object. |
108 | if ( m_macPrintSettings == kPMNoPrintSettings) | |
109 | { | |
110 | err = PMCreatePrintSettings((PMPrintSettings *) &m_macPrintSettings); | |
18f3decb | 111 | |
2646f485 SC |
112 | // Note that PMPrintSettings is not session-specific, but calling |
113 | // PMSessionDefaultPrintSettings assigns values specific to the printer | |
114 | // associated with the current printing session. | |
115 | if ((err == noErr) && | |
116 | ( m_macPrintSettings != kPMNoPrintSettings)) | |
117 | { | |
118 | err = PMSessionDefaultPrintSettings((PMPrintSession) m_macPrintSession, | |
119 | (PMPrintSettings) m_macPrintSettings); | |
120 | } | |
121 | } | |
122 | else | |
123 | { | |
124 | err = PMSessionValidatePrintSettings((PMPrintSession) m_macPrintSession, | |
125 | (PMPrintSettings) m_macPrintSettings, | |
126 | kPMDontWantBoolean); | |
127 | } | |
128 | } | |
129 | ||
130 | void wxMacCarbonPrintData::TransferFrom( wxPrintData* data ) | |
131 | { | |
132 | ValidateOrCreate() ; | |
133 | PMSetCopies( (PMPrintSettings) m_macPrintSettings , data->GetNoCopies() , false ) ; | |
134 | PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data->GetOrientation() == wxLANDSCAPE ) ? | |
135 | kPMLandscape : kPMPortrait , false ) ; | |
136 | // collate cannot be set | |
137 | #if 0 // not yet tested | |
6d50343d | 138 | if ( !m_printerName.empty() ) |
2646f485 SC |
139 | PMSessionSetCurrentPrinter( (PMPrintSession) m_macPrintSession , wxMacCFStringHolder( m_printerName , wxFont::GetDefaultEncoding() ) ) ; |
140 | #endif | |
141 | PMColorMode color ; | |
142 | PMGetColorMode( (PMPrintSettings) m_macPrintSettings, &color ) ; | |
143 | if ( data->GetColour() ) | |
144 | { | |
145 | if ( color == kPMBlackAndWhite ) | |
146 | PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMColor ) ; | |
147 | } | |
148 | else | |
149 | PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMBlackAndWhite ) ; | |
18f3decb | 150 | |
2646f485 SC |
151 | // PMDuplexMode not yet accessible via API |
152 | // PMQualityMode not yet accessible via API | |
153 | // todo paperSize | |
154 | } | |
155 | ||
156 | void wxMacCarbonPrintData::TransferTo( wxPrintData* data ) | |
157 | { | |
158 | OSStatus err = noErr ; | |
18f3decb | 159 | |
2646f485 SC |
160 | UInt32 copies ; |
161 | err = PMGetCopies( m_macPrintSettings , &copies ) ; | |
162 | if ( err == noErr ) | |
18f3decb WS |
163 | data->SetNoCopies( copies ) ; |
164 | ||
2646f485 SC |
165 | PMOrientation orientation ; |
166 | err = PMGetOrientation( m_macPageFormat , &orientation ) ; | |
167 | if ( err == noErr ) | |
168 | { | |
169 | if ( orientation == kPMPortrait || orientation == kPMReversePortrait ) | |
170 | data->SetOrientation( wxPORTRAIT ); | |
171 | else | |
172 | data->SetOrientation( wxLANDSCAPE ); | |
173 | } | |
174 | ||
175 | // collate cannot be set | |
176 | #if 0 | |
177 | { | |
178 | wxMacCFStringHolder name ; | |
179 | PMPrinter printer ; | |
180 | PMSessionGetCurrentPrinter( m_macPrintSession , | |
181 | &printer ) ; | |
182 | m_printerName = name.AsString() ; | |
183 | } | |
184 | #endif | |
18f3decb | 185 | |
2646f485 SC |
186 | PMColorMode color ; |
187 | err = PMGetColorMode( m_macPrintSettings, &color ) ; | |
188 | if ( err == noErr ) | |
189 | data->SetColour( !(color == kPMBlackAndWhite) ) ; | |
18f3decb | 190 | |
2646f485 SC |
191 | // PMDuplexMode not yet accessible via API |
192 | // PMQualityMode not yet accessible via API | |
193 | // todo paperSize | |
18f3decb | 194 | PMRect rPaper; |
2646f485 SC |
195 | err = PMGetUnadjustedPaperRect( m_macPageFormat, &rPaper); |
196 | if ( err == noErr ) | |
197 | { | |
198 | data->SetPaperSize( wxSize ( | |
199 | (int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) , | |
200 | (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ) ) ); | |
201 | } | |
202 | } | |
203 | ||
204 | void wxMacCarbonPrintData::TransferFrom( wxPageSetupData *data ) | |
205 | { | |
206 | // should we setup the page rect here ? | |
207 | // since MacOS sometimes has two same paper rects with different | |
208 | // page rects we could make it roundtrip safe perhaps | |
209 | #if TARGET_CARBON | |
210 | #else | |
211 | #endif | |
212 | } | |
213 | ||
214 | void wxMacCarbonPrintData::TransferTo( wxPageSetupData* data ) | |
215 | { | |
18f3decb | 216 | PMRect rPaper; |
2646f485 SC |
217 | OSStatus err = PMGetUnadjustedPaperRect(m_macPageFormat, &rPaper); |
218 | if ( err == noErr ) | |
219 | { | |
220 | PMRect rPage ; | |
221 | err = PMGetUnadjustedPageRect(m_macPageFormat , &rPage ) ; | |
222 | if ( err == noErr ) | |
223 | { | |
224 | data->SetMinMarginTopLeft( wxPoint ( | |
18f3decb WS |
225 | (int)(((double) rPage.left - rPaper.left ) * pt2mm) , |
226 | (int)(((double) rPage.top - rPaper.top ) * pt2mm) ) ) ; | |
227 | ||
228 | data->SetMinMarginBottomRight( wxPoint ( | |
2646f485 SC |
229 | (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm), |
230 | (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ; | |
18f3decb WS |
231 | } |
232 | } | |
2646f485 SC |
233 | } |
234 | ||
235 | void wxMacCarbonPrintData::TransferTo( wxPrintDialogData* data ) | |
236 | { | |
237 | UInt32 minPage , maxPage ; | |
238 | PMGetPageRange( m_macPrintSettings , &minPage , &maxPage ) ; | |
239 | data->SetMinPage( minPage ) ; | |
240 | data->SetMaxPage( maxPage ) ; | |
241 | UInt32 copies ; | |
242 | PMGetCopies( m_macPrintSettings , &copies ) ; | |
243 | data->SetNoCopies( copies ) ; | |
244 | UInt32 from , to ; | |
245 | PMGetFirstPage( m_macPrintSettings , &from ) ; | |
246 | PMGetLastPage( m_macPrintSettings , &to ) ; | |
247 | data->SetFromPage( from ) ; | |
248 | data->SetToPage( to ) ; | |
249 | } | |
250 | ||
251 | void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData* data ) | |
252 | { | |
253 | PMSetPageRange( m_macPrintSettings , data->GetMinPage() , data->GetMaxPage() ) ; | |
254 | PMSetCopies( m_macPrintSettings , data->GetNoCopies() , false ) ; | |
255 | PMSetFirstPage( m_macPrintSettings , data->GetFromPage() , false ) ; | |
256 | ||
257 | int toPage = data->GetToPage(); | |
258 | if (toPage < 1) | |
259 | toPage = data->GetFromPage(); | |
260 | PMSetLastPage( m_macPrintSettings , toPage , false ) ; | |
261 | } | |
262 | ||
263 | void wxMacCarbonPrintData::CopyFrom( wxNativePrintData* d ) | |
264 | { | |
265 | wxMacCarbonPrintData *data = (wxMacCarbonPrintData*) d ; | |
266 | if ( data->m_macPrintSession != kPMNoReference ) | |
18f3decb WS |
267 | PMRetain( data->m_macPrintSession ) ; |
268 | if ( m_macPrintSession != kPMNoReference ) | |
269 | { | |
270 | PMRelease( m_macPrintSession ) ; | |
271 | m_macPrintSession = kPMNoReference ; | |
272 | } | |
273 | if ( data->m_macPrintSession != kPMNoReference ) | |
274 | m_macPrintSession = data->m_macPrintSession ; | |
2646f485 SC |
275 | |
276 | if ( data->m_macPrintSettings != kPMNoPrintSettings ) | |
18f3decb WS |
277 | PMRetain( data->m_macPrintSettings ) ; |
278 | if ( m_macPrintSettings != kPMNoPrintSettings ) | |
279 | { | |
280 | PMRelease( m_macPrintSettings ) ; | |
281 | m_macPrintSettings = kPMNoPrintSettings ; | |
282 | } | |
283 | if ( data->m_macPrintSettings != kPMNoPrintSettings ) | |
284 | m_macPrintSettings = data->m_macPrintSettings ; | |
285 | ||
286 | if ( data->m_macPageFormat != kPMNoPageFormat ) | |
287 | PMRetain( data->m_macPageFormat ) ; | |
288 | if ( m_macPageFormat != kPMNoPageFormat ) | |
289 | { | |
290 | PMRelease( m_macPageFormat ) ; | |
291 | m_macPageFormat = kPMNoPageFormat ; | |
292 | } | |
293 | if ( data->m_macPageFormat != kPMNoPageFormat ) | |
294 | m_macPageFormat = data->m_macPageFormat ; | |
2646f485 SC |
295 | } |
296 | ||
297 | int wxMacCarbonPrintData::ShowPrintDialog() | |
298 | { | |
299 | int result = wxID_CANCEL ; | |
300 | OSErr err = noErr ; | |
301 | wxString message ; | |
18f3decb | 302 | |
2646f485 | 303 | Boolean accepted; |
18f3decb | 304 | |
2646f485 SC |
305 | { |
306 | // Display the Print dialog. | |
307 | if (err == noErr) | |
308 | { | |
309 | err = PMSessionPrintDialog( m_macPrintSession, | |
310 | m_macPrintSettings, | |
311 | m_macPageFormat, | |
312 | &accepted); | |
313 | if ((err == noErr) && !accepted) | |
314 | { | |
315 | err = kPMCancel; // user clicked Cancel button | |
316 | } | |
317 | } | |
318 | if ( err == noErr ) | |
319 | { | |
320 | result = wxID_OK ; | |
321 | } | |
322 | } | |
323 | if ((err != noErr) && (err != kPMCancel)) | |
324 | { | |
325 | message.Printf( wxT("Print Error %d"), err ) ; | |
326 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; | |
327 | dialog.ShowModal(); | |
328 | } | |
329 | ||
330 | return result ; | |
331 | } | |
332 | ||
333 | int wxMacCarbonPrintData::ShowPageSetupDialog() | |
334 | { | |
335 | int result = wxID_CANCEL ; | |
336 | OSErr err = noErr ; | |
337 | wxString message ; | |
18f3decb | 338 | |
2646f485 SC |
339 | Boolean accepted; |
340 | { | |
341 | // Display the Page Setup dialog. | |
342 | if (err == noErr) | |
343 | { | |
344 | err = PMSessionPageSetupDialog( m_macPrintSession, | |
345 | m_macPageFormat, | |
346 | &accepted); | |
347 | if ((err == noErr) && !accepted) | |
348 | { | |
349 | err = kPMCancel; // user clicked Cancel button | |
350 | } | |
18f3decb WS |
351 | } |
352 | ||
2646f485 SC |
353 | // If the user did not cancel, flatten and save the PageFormat object |
354 | // with our document. | |
355 | if (err == noErr) { | |
356 | result = wxID_OK ; | |
357 | } | |
358 | } | |
359 | if ((err != noErr) && (err != kPMCancel)) | |
360 | { | |
361 | message.Printf( wxT("Print Error %d"), err ) ; | |
362 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; | |
363 | dialog.ShowModal(); | |
364 | } | |
365 | ||
366 | return result ; | |
367 | } | |
368 | ||
369 | #else | |
370 | ||
18f3decb | 371 | wxNativePrintData* wxNativePrintData::Create() |
2646f485 SC |
372 | { |
373 | return new wxMacClassicPrintData() ; | |
374 | } | |
375 | ||
376 | wxMacClassicPrintData::wxMacClassicPrintData() | |
377 | { | |
378 | m_macPrintSettings = NULL ; | |
379 | ValidateOrCreate() ; | |
380 | } | |
381 | ||
382 | wxMacClassicPrintData::~wxMacClassicPrintData() | |
383 | { | |
384 | wxASSERT( m_macPrintSettings ); | |
385 | DisposeHandle( (Handle) m_macPrintSettings ) ; | |
386 | } | |
387 | ||
18f3decb | 388 | void wxMacClassicPrintData::ValidateOrCreate() |
2646f485 SC |
389 | { |
390 | if ( m_macPrintSettings == NULL ) | |
391 | { | |
392 | m_macPrintSettings = (THPrint) NewHandleClear( sizeof( TPrint ) ); | |
393 | (**m_macPrintSettings).iPrVersion = 0; // something invalid | |
394 | ||
395 | (**m_macPrintSettings).prInfo.iHRes = 72; | |
396 | (**m_macPrintSettings).prInfo.iVRes = 72; | |
397 | Rect r1 = { 0, 0, 8*72 - 2 * 18, 11*72 - 2 * 36 }; | |
398 | (**m_macPrintSettings).prInfo.rPage = r1;// must have its top left & (0,0) | |
399 | ||
400 | Rect r2 = { -18, -36, 8*72 - 18, 11*72 - 36 }; | |
401 | (**m_macPrintSettings).rPaper = r2; | |
402 | (**m_macPrintSettings).prStl.iPageV = 11 * 120 ; // 11 inches in 120th of an inch | |
403 | (**m_macPrintSettings).prStl.iPageH = 8 * 120 ; // 8 inches in 120th of an inch | |
404 | } | |
405 | else | |
406 | { | |
407 | } | |
408 | } | |
409 | ||
410 | void wxMacClassicPrintData::TransferFrom( wxPrintData* data ) | |
411 | { | |
412 | ValidateOrCreate() ; | |
413 | (**m_macPrintSettings).prJob.iCopies = data->GetNoCopies() ; | |
414 | // on mac the paper rect has a negative top left corner, because the page rect (printable area) is at 0,0 | |
415 | // if all printing data is consolidated in on structure we will be able to set additional infos about pages | |
416 | } | |
417 | ||
418 | void wxMacClassicPrintData::TransferTo( wxPrintData* data ) | |
419 | { | |
420 | data->SetNoCopies( (**m_macPrintSettings).prJob.iCopies ); | |
18f3decb | 421 | data->SetPaperSize( wxSize( |
2646f485 SC |
422 | ((double) (**m_macPrintSettings).rPaper.right - (**m_macPrintSettings).rPaper.left ) * pt2mm , |
423 | ((double) (**m_macPrintSettings).rPaper.bottom - (**m_macPrintSettings).rPaper.top ) * pt2mm ) ) ; | |
424 | } | |
425 | ||
426 | void wxMacClassicPrintData::TransferFrom( wxPageSetupData *data ) | |
427 | { | |
428 | } | |
429 | ||
430 | void wxMacClassicPrintData::TransferTo( wxPageSetupData* data ) | |
431 | { | |
432 | data->SetMinMarginTopLeft( wxPoint( | |
433 | ((double) (**m_macPrintSettings).prInfo.rPage.left -(**m_macPrintSettings).rPaper.left ) * pt2mm , | |
434 | ((double) (**m_macPrintSettings).prInfo.rPage.top -(**m_macPrintSettings).rPaper.top ) * pt2mm ) ) ; | |
435 | data->SetMinMarginBottomRight( wxPoint( | |
436 | ((double) (**m_macPrintSettings).rPaper.right - (**m_macPrintSettings).prInfo.rPage.right ) * pt2mm , | |
437 | ((double)(**m_macPrintSettings).rPaper.bottom - (**m_macPrintSettings).prInfo.rPage.bottom ) * pt2mm ) ) ; | |
438 | } | |
439 | ||
440 | void wxMacClassicPrintData::TransferFrom( wxPrintDialogData* data ) | |
441 | { | |
442 | int toPage = data->GetToPage(); | |
443 | if (toPage < 1) | |
444 | toPage = data->GetFromPage(); | |
445 | (**m_macPrintSettings).prJob.iFstPage = data->GetFromPage() ; | |
446 | (**m_macPrintSettings).prJob.iLstPage = toPage; | |
447 | } | |
448 | ||
449 | void wxMacClassicPrintData::TransferTo( wxPrintDialogData* data ) | |
450 | { | |
451 | data->SetFromPage( (**m_macPrintSettings).prJob.iFstPage ) ; | |
452 | data->SetToPage( (**m_macPrintSettings).prJob.iLstPage ) ; | |
453 | } | |
454 | ||
455 | void wxMacClassicPrintData::CopyFrom( wxNativePrintData* data ) | |
456 | { | |
457 | DisposeHandle( (Handle) m_macPrintSettings ) ; | |
458 | m_macPrintSettings = ((wxMacClassicPrintData*)data)->m_macPrintSettings; | |
459 | HandToHand( (Handle*) &m_macPrintSettings ); | |
460 | } | |
461 | ||
462 | int wxMacClassicPrintData::ShowPrintDialog() | |
463 | { | |
464 | int result = wxID_CANCEL ; | |
465 | OSErr err = noErr ; | |
466 | wxString message ; | |
18f3decb | 467 | |
2646f485 SC |
468 | err = ::UMAPrOpen() ; |
469 | if ( err == noErr ) | |
470 | { | |
471 | if ( ::PrJobDialog( m_macPrintSettings ) ) | |
472 | { | |
473 | result = wxID_OK ; | |
474 | } | |
18f3decb | 475 | |
2646f485 SC |
476 | } |
477 | else | |
478 | { | |
479 | message.Printf( wxT("Print Error %d"), err ) ; | |
18f3decb | 480 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; |
2646f485 SC |
481 | dialog.ShowModal(); |
482 | } | |
483 | ::UMAPrClose() ; | |
484 | ||
485 | return result ; | |
486 | } | |
487 | ||
488 | int wxMacClassicPrintData::ShowPageSetupDialog() | |
489 | { | |
490 | int result = wxID_CANCEL ; | |
491 | OSErr err = noErr ; | |
492 | wxString message ; | |
18f3decb | 493 | |
2646f485 SC |
494 | err = ::UMAPrOpen() ; |
495 | if ( err == noErr ) | |
496 | { | |
497 | if ( ::PrStlDialog( m_macPrintSettings ) ) | |
498 | { | |
499 | result = wxID_OK ; | |
500 | } | |
18f3decb | 501 | |
2646f485 SC |
502 | } |
503 | else | |
504 | { | |
505 | message.Printf( wxT("Print Error %d"), err ) ; | |
506 | wxMessageDialog dialog( NULL , message , wxEmptyString , wxICON_HAND | wxOK) ; | |
507 | dialog.ShowModal(); | |
508 | } | |
509 | ::UMAPrClose() ; | |
510 | return result ; | |
511 | } | |
512 | ||
513 | #endif | |
514 | ||
515 | /* | |
516 | * Printer | |
517 | */ | |
518 | ||
519 | wxMacPrinter::wxMacPrinter(wxPrintDialogData *data): | |
520 | wxPrinterBase(data) | |
521 | { | |
522 | } | |
523 | ||
524 | wxMacPrinter::~wxMacPrinter(void) | |
525 | { | |
526 | } | |
527 | ||
528 | bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) | |
529 | { | |
18f3decb | 530 | sm_abortIt = false; |
2646f485 | 531 | sm_abortWindow = NULL; |
18f3decb | 532 | |
2646f485 | 533 | if (!printout) |
18f3decb WS |
534 | return false; |
535 | ||
536 | printout->SetIsPreview(false); | |
2646f485 SC |
537 | if (m_printDialogData.GetMinPage() < 1) |
538 | m_printDialogData.SetMinPage(1); | |
539 | if (m_printDialogData.GetMaxPage() < 1) | |
540 | m_printDialogData.SetMaxPage(9999); | |
541 | ||
18f3decb | 542 | // Create a suitable device context |
2646f485 SC |
543 | wxDC *dc = NULL; |
544 | if (prompt) | |
545 | { | |
546 | wxPrintDialog dialog(parent, & m_printDialogData); | |
547 | if (dialog.ShowModal() == wxID_OK) | |
548 | { | |
549 | dc = dialog.GetPrintDC(); | |
550 | m_printDialogData = dialog.GetPrintDialogData(); | |
551 | } | |
552 | } | |
553 | else | |
554 | { | |
555 | dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ; | |
556 | } | |
18f3decb WS |
557 | |
558 | ||
2646f485 SC |
559 | // May have pressed cancel. |
560 | if (!dc || !dc->Ok()) | |
561 | { | |
562 | if (dc) delete dc; | |
18f3decb | 563 | return false; |
2646f485 | 564 | } |
18f3decb | 565 | |
2646f485 | 566 | // on the mac we have always pixels as addressing mode with 72 dpi |
18f3decb | 567 | |
2646f485 SC |
568 | printout->SetPPIScreen(72, 72); |
569 | printout->SetPPIPrinter(72, 72); | |
18f3decb WS |
570 | |
571 | // Set printout parameters | |
2646f485 | 572 | printout->SetDC(dc); |
18f3decb | 573 | |
2646f485 SC |
574 | int w, h; |
575 | wxCoord ww, hh; | |
576 | dc->GetSize(&w, &h); | |
577 | printout->SetPageSizePixels((int)w, (int)h); | |
578 | dc->GetSizeMM(&ww, &hh); | |
579 | printout->SetPageSizeMM((int)ww, (int)hh); | |
18f3decb | 580 | |
2646f485 SC |
581 | // Create an abort window |
582 | wxBeginBusyCursor(); | |
18f3decb | 583 | |
2646f485 | 584 | printout->OnPreparePrinting(); |
18f3decb | 585 | |
2646f485 SC |
586 | // Get some parameters from the printout, if defined |
587 | int fromPage, toPage; | |
588 | int minPage, maxPage; | |
589 | printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); | |
18f3decb | 590 | |
2646f485 SC |
591 | if (maxPage == 0) |
592 | { | |
593 | wxEndBusyCursor(); | |
18f3decb | 594 | return false; |
2646f485 | 595 | } |
18f3decb | 596 | |
2646f485 SC |
597 | // Only set min and max, because from and to have been |
598 | // set by the user | |
599 | m_printDialogData.SetMinPage(minPage); | |
600 | m_printDialogData.SetMaxPage(maxPage); | |
18f3decb | 601 | |
2646f485 SC |
602 | wxWindow *win = CreateAbortWindow(parent, printout); |
603 | wxSafeYield(win,true); | |
18f3decb | 604 | |
2646f485 SC |
605 | if (!win) |
606 | { | |
607 | wxEndBusyCursor(); | |
608 | wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent); | |
609 | delete dc; | |
18f3decb | 610 | return false; |
2646f485 SC |
611 | } |
612 | sm_abortWindow = win; | |
18f3decb | 613 | sm_abortWindow->Show(true); |
2646f485 | 614 | wxSafeYield(win,true); |
18f3decb | 615 | |
2646f485 | 616 | printout->OnBeginPrinting(); |
18f3decb WS |
617 | |
618 | bool keepGoing = true; | |
619 | ||
2646f485 SC |
620 | int copyCount; |
621 | for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++) | |
622 | { | |
623 | if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) | |
624 | { | |
625 | wxEndBusyCursor(); | |
626 | wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent); | |
627 | break; | |
628 | } | |
629 | if (sm_abortIt) | |
630 | break; | |
18f3decb | 631 | |
2646f485 SC |
632 | int pn; |
633 | for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn); | |
634 | pn++) | |
635 | { | |
636 | if (sm_abortIt) | |
637 | { | |
18f3decb | 638 | keepGoing = false; |
2646f485 SC |
639 | break; |
640 | } | |
641 | else | |
642 | { | |
643 | #if TARGET_CARBON | |
644 | if ( UMAGetSystemVersion() >= 0x1000 ) | |
645 | #endif | |
646 | { | |
647 | GrafPtr thePort ; | |
648 | GetPort( &thePort ) ; | |
649 | wxSafeYield(win,true); | |
650 | SetPort( thePort ) ; | |
651 | } | |
652 | dc->StartPage(); | |
653 | keepGoing = printout->OnPrintPage(pn); | |
654 | dc->EndPage(); | |
655 | } | |
656 | } | |
657 | printout->OnEndDocument(); | |
658 | } | |
18f3decb | 659 | |
2646f485 | 660 | printout->OnEndPrinting(); |
18f3decb | 661 | |
2646f485 SC |
662 | if (sm_abortWindow) |
663 | { | |
18f3decb | 664 | sm_abortWindow->Show(false); |
2646f485 SC |
665 | delete sm_abortWindow; |
666 | sm_abortWindow = NULL; | |
667 | } | |
18f3decb | 668 | |
2646f485 | 669 | wxEndBusyCursor(); |
18f3decb | 670 | |
2646f485 | 671 | delete dc; |
18f3decb WS |
672 | |
673 | return true; | |
2646f485 SC |
674 | } |
675 | ||
676 | wxDC* wxMacPrinter::PrintDialog(wxWindow *parent) | |
677 | { | |
678 | wxDC* dc = (wxDC*) NULL; | |
18f3decb | 679 | |
2646f485 SC |
680 | wxPrintDialog dialog(parent, & m_printDialogData); |
681 | int ret = dialog.ShowModal(); | |
18f3decb | 682 | |
2646f485 SC |
683 | if (ret == wxID_OK) |
684 | { | |
685 | dc = dialog.GetPrintDC(); | |
686 | m_printDialogData = dialog.GetPrintDialogData(); | |
687 | } | |
18f3decb | 688 | |
2646f485 SC |
689 | return dc; |
690 | } | |
691 | ||
692 | bool wxMacPrinter::Setup(wxWindow *parent) | |
693 | { | |
04a014a5 | 694 | #if 0 |
2646f485 | 695 | wxPrintDialog dialog(parent, & m_printDialogData); |
18f3decb WS |
696 | dialog.GetPrintDialogData().SetSetupDialog(true); |
697 | ||
2646f485 | 698 | int ret = dialog.ShowModal(); |
18f3decb | 699 | |
2646f485 SC |
700 | if (ret == wxID_OK) |
701 | { | |
702 | m_printDialogData = dialog.GetPrintDialogData(); | |
703 | } | |
18f3decb | 704 | |
2646f485 | 705 | return (ret == wxID_OK); |
04a014a5 RR |
706 | #endif |
707 | return wxID_CANCEL; | |
2646f485 SC |
708 | } |
709 | ||
710 | /* | |
711 | * Print preview | |
712 | */ | |
713 | ||
714 | wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, | |
715 | wxPrintout *printoutForPrinting, | |
716 | wxPrintDialogData *data) | |
717 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
718 | { | |
719 | DetermineScaling(); | |
720 | } | |
721 | ||
722 | wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data): | |
723 | wxPrintPreviewBase(printout, printoutForPrinting, data) | |
724 | { | |
725 | DetermineScaling(); | |
726 | } | |
727 | ||
728 | wxMacPrintPreview::~wxMacPrintPreview(void) | |
729 | { | |
730 | } | |
731 | ||
732 | bool wxMacPrintPreview::Print(bool interactive) | |
733 | { | |
734 | if (!m_printPrintout) | |
18f3decb | 735 | return false; |
2646f485 SC |
736 | wxMacPrinter printer(&m_printDialogData); |
737 | return printer.Print(m_previewFrame, m_printPrintout, interactive); | |
738 | } | |
739 | ||
740 | void wxMacPrintPreview::DetermineScaling(void) | |
741 | { | |
742 | int screenWidth , screenHeight ; | |
743 | wxDisplaySize( &screenWidth , &screenHeight ) ; | |
18f3decb | 744 | |
2646f485 SC |
745 | m_previewPrintout->SetPPIScreen( 72 , 72 ) ; |
746 | m_previewPrintout->SetPPIPrinter( 72 , 72 ) ; | |
747 | m_previewPrintout->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) ); | |
748 | m_previewPrintout->SetPageSizePixels( 8 * 72 , 11 * 72 ) ; | |
749 | m_pageWidth = 8 * 72 ; | |
750 | m_pageHeight = 11 * 72 ; | |
751 | m_previewScale = 1 ; | |
18f3decb | 752 | |
2646f485 SC |
753 | // Get a device context for the currently selected printer |
754 | wxPrinterDC printerDC(m_printDialogData.GetPrintData()); | |
755 | if (printerDC.Ok()) | |
756 | { | |
757 | int x , y ; | |
758 | wxCoord ww, hh; | |
759 | printerDC.GetSizeMM(&ww, &hh); | |
760 | printerDC.GetSize( &x , &y ) ; | |
761 | m_previewPrintout->SetPageSizeMM((int)ww, (int)hh); | |
762 | m_previewPrintout->SetPageSizePixels( x , y) ; | |
763 | m_pageWidth = x ; | |
764 | m_pageHeight = y ; | |
765 | m_isOk = true ; | |
766 | } | |
767 | else | |
768 | { | |
769 | m_isOk = false ; | |
770 | } | |
771 | // At 100%, the page should look about page-size on the screen. | |
772 | // m_previewScale = (float)((float)screenWidth/(float)printerWidth); | |
773 | // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes); | |
18f3decb | 774 | |
2646f485 SC |
775 | m_previewScale = 1 ; |
776 | } |