]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/osx/core/printmac.cpp | |
3 | // Purpose: wxMacPrinter framework | |
4 | // Author: Julian Smart, Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // Copyright: (c) Julian Smart, Stefan Csomor | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #if wxUSE_PRINTING_ARCHITECTURE | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
21 | #include "wx/utils.h" | |
22 | #include "wx/dc.h" | |
23 | #include "wx/app.h" | |
24 | #include "wx/msgdlg.h" | |
25 | #include "wx/dcprint.h" | |
26 | #include "wx/math.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/osx/private.h" | |
30 | ||
31 | #include "wx/osx/printmac.h" | |
32 | #include "wx/osx/private/print.h" | |
33 | ||
34 | #include "wx/printdlg.h" | |
35 | #include "wx/paper.h" | |
36 | #include "wx/osx/printdlg.h" | |
37 | ||
38 | #include <stdlib.h> | |
39 | ||
40 | // | |
41 | // move to print_osx.cpp | |
42 | // | |
43 | ||
44 | static int ResolutionSorter(const void *e1, const void *e2) | |
45 | { | |
46 | const PMResolution *res1 = (const PMResolution *)e1; | |
47 | const PMResolution *res2 = (const PMResolution *)e2; | |
48 | const double area1 = res1->hRes * res1->vRes; | |
49 | const double area2 = res2->hRes * res2->vRes; | |
50 | ||
51 | if (area1 < area2) | |
52 | return -1; | |
53 | else if (area1 > area2) | |
54 | return 1; | |
55 | else | |
56 | return 0; | |
57 | } | |
58 | ||
59 | static PMResolution *GetSupportedResolutions(PMPrinter printer, UInt32 *count) | |
60 | { | |
61 | PMResolution res, *resolutions = NULL; | |
62 | OSStatus status = PMPrinterGetPrinterResolutionCount(printer, count); | |
63 | if (status == noErr) | |
64 | { | |
65 | resolutions = (PMResolution *)malloc(sizeof(PMResolution) * (*count)); | |
66 | UInt32 realCount = 0; | |
67 | for (UInt32 i = 0; i < *count; i++) | |
68 | { | |
69 | if (PMPrinterGetIndexedPrinterResolution(printer, i + 1, &res) == noErr) | |
70 | resolutions[realCount++] = res; | |
71 | } | |
72 | qsort(resolutions, realCount, sizeof(PMResolution), ResolutionSorter); | |
73 | ||
74 | *count = realCount; | |
75 | } | |
76 | if ((*count == 0) && (resolutions)) | |
77 | { | |
78 | free(resolutions); | |
79 | resolutions = NULL; | |
80 | } | |
81 | return resolutions; | |
82 | } | |
83 | ||
84 | ||
85 | ||
86 | IMPLEMENT_DYNAMIC_CLASS(wxOSXPrintData, wxPrintNativeDataBase) | |
87 | ||
88 | bool wxOSXPrintData::IsOk() const | |
89 | { | |
90 | return (m_macPageFormat != kPMNoPageFormat) && (m_macPrintSettings != kPMNoPrintSettings) && (m_macPrintSession != kPMNoReference); | |
91 | } | |
92 | ||
93 | wxOSXPrintData::wxOSXPrintData() | |
94 | { | |
95 | m_macPageFormat = kPMNoPageFormat; | |
96 | m_macPrintSettings = kPMNoPrintSettings; | |
97 | m_macPrintSession = kPMNoReference ; | |
98 | m_macPaper = kPMNoData; | |
99 | } | |
100 | ||
101 | wxOSXPrintData::~wxOSXPrintData() | |
102 | { | |
103 | } | |
104 | ||
105 | void wxOSXPrintData::UpdateFromPMState() | |
106 | { | |
107 | } | |
108 | ||
109 | void wxOSXPrintData::UpdateToPMState() | |
110 | { | |
111 | } | |
112 | ||
113 | void wxOSXPrintData::TransferPrinterNameFrom( const wxPrintData &data ) | |
114 | { | |
115 | CFArrayRef printerList; | |
116 | CFIndex index, count; | |
117 | CFStringRef name; | |
118 | ||
119 | if (PMServerCreatePrinterList(kPMServerLocal, &printerList) == noErr) | |
120 | { | |
121 | PMPrinter printer = NULL; | |
122 | count = CFArrayGetCount(printerList); | |
123 | for (index = 0; index < count; index++) | |
124 | { | |
125 | printer = (PMPrinter)CFArrayGetValueAtIndex(printerList, index); | |
126 | if ((data.GetPrinterName().empty()) && (PMPrinterIsDefault(printer))) | |
127 | break; | |
128 | else | |
129 | { | |
130 | name = PMPrinterGetName(printer); | |
131 | CFRetain(name); | |
132 | if (data.GetPrinterName() == wxCFStringRef(name).AsString()) | |
133 | break; | |
134 | } | |
135 | } | |
136 | if (index < count) | |
137 | PMSessionSetCurrentPMPrinter(m_macPrintSession, printer); | |
138 | CFRelease(printerList); | |
139 | } | |
140 | } | |
141 | ||
142 | void wxOSXPrintData::TransferPaperInfoFrom( const wxPrintData &data ) | |
143 | { | |
144 | PMPrinter printer; | |
145 | PMSessionGetCurrentPrinter(m_macPrintSession, &printer); | |
146 | ||
147 | wxSize papersize = wxDefaultSize; | |
148 | const wxPaperSize paperId = data.GetPaperId(); | |
149 | if ( paperId != wxPAPER_NONE && wxThePrintPaperDatabase ) | |
150 | { | |
151 | papersize = wxThePrintPaperDatabase->GetSize(paperId); | |
152 | if ( papersize != wxDefaultSize ) | |
153 | { | |
154 | papersize.x /= 10; | |
155 | papersize.y /= 10; | |
156 | } | |
157 | } | |
158 | else | |
159 | { | |
160 | papersize = data.GetPaperSize(); | |
161 | } | |
162 | ||
163 | if ( papersize != wxDefaultSize ) | |
164 | { | |
165 | papersize.x = (wxInt32) (papersize.x * mm2pt); | |
166 | papersize.y = (wxInt32) (papersize.y * mm2pt); | |
167 | ||
168 | double height, width; | |
169 | PMPaperGetHeight(m_macPaper, &height); | |
170 | PMPaperGetWidth(m_macPaper, &width); | |
171 | ||
172 | if ( fabs( width - papersize.x ) >= 5 || | |
173 | fabs( height - papersize.y ) >= 5 ) | |
174 | { | |
175 | // we have to change the current paper | |
176 | CFArrayRef paperlist = 0 ; | |
177 | if ( PMPrinterGetPaperList( printer, &paperlist ) == noErr ) | |
178 | { | |
179 | PMPaper bestPaper = kPMNoData ; | |
180 | CFIndex top = CFArrayGetCount(paperlist); | |
181 | for ( CFIndex i = 0 ; i < top ; ++ i ) | |
182 | { | |
183 | PMPaper paper = (PMPaper) CFArrayGetValueAtIndex( paperlist, i ); | |
184 | PMPaperGetHeight(paper, &height); | |
185 | PMPaperGetWidth(paper, &width); | |
186 | if ( fabs( width - papersize.x ) < 5 && | |
187 | fabs( height - papersize.y ) < 5 ) | |
188 | { | |
189 | // TODO test for duplicate hits and use additional | |
190 | // criteria for best match | |
191 | bestPaper = paper; | |
192 | } | |
193 | } | |
194 | PMPaper paper = kPMNoData; | |
195 | if ( bestPaper == kPMNoData ) | |
196 | { | |
197 | const PMPaperMargins margins = { 0.0, 0.0, 0.0, 0.0 }; | |
198 | wxString id, name(wxT("Custom paper")); | |
199 | id.Printf(wxT("wxPaperCustom%dx%d"), papersize.x, papersize.y); | |
200 | ||
201 | PMPaperCreateCustom(printer, wxCFStringRef( id, wxFont::GetDefaultEncoding() ), wxCFStringRef( name, wxFont::GetDefaultEncoding() ), | |
202 | papersize.x, papersize.y, &margins, &paper); | |
203 | } | |
204 | if ( bestPaper != kPMNoData ) | |
205 | { | |
206 | PMPageFormat pageFormat; | |
207 | PMCreatePageFormatWithPMPaper(&pageFormat, bestPaper); | |
208 | PMCopyPageFormat( pageFormat, m_macPageFormat ); | |
209 | PMRelease(pageFormat); | |
210 | PMGetPageFormatPaper(m_macPageFormat, &m_macPaper); | |
211 | } | |
212 | PMRelease(paper); | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | PMSetCopies( m_macPrintSettings , data.GetNoCopies() , false ) ; | |
218 | PMSetCollate(m_macPrintSettings, data.GetCollate()); | |
219 | if ( data.IsOrientationReversed() ) | |
220 | PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? | |
221 | kPMReverseLandscape : kPMReversePortrait , false ) ; | |
222 | else | |
223 | PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? | |
224 | kPMLandscape : kPMPortrait , false ) ; | |
225 | ||
226 | PMDuplexMode mode = 0 ; | |
227 | switch( data.GetDuplex() ) | |
228 | { | |
229 | case wxDUPLEX_HORIZONTAL : | |
230 | mode = kPMDuplexNoTumble ; | |
231 | break ; | |
232 | case wxDUPLEX_VERTICAL : | |
233 | mode = kPMDuplexTumble ; | |
234 | break ; | |
235 | case wxDUPLEX_SIMPLEX : | |
236 | default : | |
237 | mode = kPMDuplexNone ; | |
238 | break ; | |
239 | } | |
240 | PMSetDuplex( m_macPrintSettings, mode ) ; | |
241 | ||
242 | ||
243 | if ( data.IsOrientationReversed() ) | |
244 | PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? | |
245 | kPMReverseLandscape : kPMReversePortrait , false ) ; | |
246 | else | |
247 | PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? | |
248 | kPMLandscape : kPMPortrait , false ) ; | |
249 | } | |
250 | ||
251 | void wxOSXPrintData::TransferResolutionFrom( const wxPrintData &data ) | |
252 | { | |
253 | PMPrinter printer; | |
254 | PMSessionGetCurrentPrinter(m_macPrintSession, &printer); | |
255 | ||
256 | UInt32 resCount; | |
257 | PMResolution *resolutions = GetSupportedResolutions(printer, &resCount); | |
258 | if (resolutions) | |
259 | { | |
260 | wxPrintQuality quality = data.GetQuality(); | |
261 | if (quality >= 0) | |
262 | quality = wxPRINT_QUALITY_HIGH; | |
263 | ||
264 | PMResolution res = resolutions[((quality - wxPRINT_QUALITY_DRAFT) * (resCount - 1)) / 3]; | |
265 | PMPrinterSetOutputResolution(printer, m_macPrintSettings, &res); | |
266 | ||
267 | free(resolutions); | |
268 | } | |
269 | } | |
270 | ||
271 | bool wxOSXPrintData::TransferFrom( const wxPrintData &data ) | |
272 | { | |
273 | TransferPrinterNameFrom(data); | |
274 | TransferPaperInfoFrom(data); | |
275 | TransferResolutionFrom(data); | |
276 | ||
277 | // after setting the new resolution the format has to be updated, otherwise the page rect remains | |
278 | // at the 'old' scaling | |
279 | ||
280 | PMSessionValidatePageFormat(m_macPrintSession, | |
281 | m_macPageFormat, kPMDontWantBoolean); | |
282 | PMSessionValidatePrintSettings(m_macPrintSession, | |
283 | m_macPrintSettings, kPMDontWantBoolean); | |
284 | ||
285 | #if wxOSX_USE_COCOA | |
286 | UpdateFromPMState(); | |
287 | #endif | |
288 | ||
289 | return true ; | |
290 | } | |
291 | ||
292 | void wxOSXPrintData::TransferPrinterNameTo( wxPrintData &data ) | |
293 | { | |
294 | CFStringRef name; | |
295 | PMPrinter printer ; | |
296 | PMSessionGetCurrentPrinter( m_macPrintSession, &printer ); | |
297 | if (PMPrinterIsDefault(printer)) | |
298 | data.SetPrinterName(wxEmptyString); | |
299 | else | |
300 | { | |
301 | name = PMPrinterGetName(printer); | |
302 | CFRetain(name); | |
303 | data.SetPrinterName(wxCFStringRef(name).AsString()); | |
304 | } | |
305 | } | |
306 | ||
307 | void wxOSXPrintData::TransferPaperInfoTo( wxPrintData &data ) | |
308 | { | |
309 | PMGetPageFormatPaper(m_macPageFormat, &m_macPaper); | |
310 | ||
311 | PMPrinter printer ; | |
312 | PMSessionGetCurrentPrinter( m_macPrintSession, &printer ); | |
313 | OSStatus err = noErr ; | |
314 | UInt32 copies ; | |
315 | err = PMGetCopies( m_macPrintSettings , &copies ) ; | |
316 | if ( err == noErr ) | |
317 | data.SetNoCopies( copies ) ; | |
318 | ||
319 | PMOrientation orientation ; | |
320 | err = PMGetOrientation( m_macPageFormat , &orientation ) ; | |
321 | if ( err == noErr ) | |
322 | { | |
323 | if ( orientation == kPMPortrait || orientation == kPMReversePortrait ) | |
324 | { | |
325 | data.SetOrientation( wxPORTRAIT ); | |
326 | data.SetOrientationReversed( orientation == kPMReversePortrait ); | |
327 | } | |
328 | else | |
329 | { | |
330 | data.SetOrientation( wxLANDSCAPE ); | |
331 | data.SetOrientationReversed( orientation == kPMReverseLandscape ); | |
332 | } | |
333 | } | |
334 | ||
335 | Boolean collate; | |
336 | if (PMGetCollate(m_macPrintSettings, &collate) == noErr) | |
337 | data.SetCollate(collate); | |
338 | ||
339 | ||
340 | PMDuplexMode mode = 0 ; | |
341 | PMGetDuplex( m_macPrintSettings, &mode ) ; | |
342 | switch( mode ) | |
343 | { | |
344 | case kPMDuplexNoTumble : | |
345 | data.SetDuplex(wxDUPLEX_HORIZONTAL); | |
346 | break ; | |
347 | case kPMDuplexTumble : | |
348 | data.SetDuplex(wxDUPLEX_VERTICAL); | |
349 | break ; | |
350 | case kPMDuplexNone : | |
351 | default : | |
352 | data.SetDuplex(wxDUPLEX_SIMPLEX); | |
353 | break ; | |
354 | } | |
355 | ||
356 | double height, width; | |
357 | PMPaperGetHeight(m_macPaper, &height); | |
358 | PMPaperGetWidth(m_macPaper, &width); | |
359 | ||
360 | wxSize sz((int)(width * pt2mm + 0.5 ) , | |
361 | (int)(height * pt2mm + 0.5 )); | |
362 | data.SetPaperSize(sz); | |
363 | wxPaperSize id = wxThePrintPaperDatabase->GetSize(wxSize(sz.x* 10, sz.y * 10)); | |
364 | if (id != wxPAPER_NONE) | |
365 | { | |
366 | data.SetPaperId(id); | |
367 | } | |
368 | } | |
369 | ||
370 | void wxOSXPrintData::TransferResolutionTo( wxPrintData &data ) | |
371 | { | |
372 | PMPrinter printer ; | |
373 | PMSessionGetCurrentPrinter( m_macPrintSession, &printer ); | |
374 | ||
375 | /* assume high quality, will change below if we are able to */ | |
376 | data.SetQuality(wxPRINT_QUALITY_HIGH); | |
377 | ||
378 | PMResolution *resolutions; | |
379 | UInt32 resCount; | |
380 | resolutions = GetSupportedResolutions(printer, &resCount); | |
381 | if (resolutions) | |
382 | { | |
383 | bool valid = false; | |
384 | PMResolution res; | |
385 | if ( PMPrinterGetOutputResolution(printer, m_macPrintSettings, &res) == noErr ) | |
386 | valid = true; | |
387 | ||
388 | if ( valid ) | |
389 | { | |
390 | UInt32 i; | |
391 | for (i = 0; i < resCount; i++) | |
392 | { | |
393 | if ((resolutions[i].hRes == res.hRes) && (resolutions[i].vRes = res.vRes)) | |
394 | break; | |
395 | } | |
396 | if (i < resCount) | |
397 | data.SetQuality((((i + 1) * 3) / resCount) + wxPRINT_QUALITY_DRAFT); | |
398 | } | |
399 | free(resolutions); | |
400 | } | |
401 | } | |
402 | ||
403 | bool wxOSXPrintData::TransferTo( wxPrintData &data ) | |
404 | { | |
405 | #if wxOSX_USE_COCOA | |
406 | UpdateToPMState(); | |
407 | #endif | |
408 | ||
409 | TransferPrinterNameTo(data); | |
410 | TransferPaperInfoTo(data); | |
411 | TransferResolutionTo(data); | |
412 | return true ; | |
413 | } | |
414 | ||
415 | void wxOSXPrintData::TransferFrom( wxPageSetupDialogData *WXUNUSED(data) ) | |
416 | { | |
417 | // should we setup the page rect here ? | |
418 | // since MacOS sometimes has two same paper rects with different | |
419 | // page rects we could make it roundtrip safe perhaps | |
420 | } | |
421 | ||
422 | void wxOSXPrintData::TransferTo( wxPageSetupDialogData* data ) | |
423 | { | |
424 | #if wxOSX_USE_COCOA | |
425 | UpdateToPMState(); | |
426 | #endif | |
427 | PMRect rPaper; | |
428 | OSStatus err = PMGetUnadjustedPaperRect(m_macPageFormat, &rPaper); | |
429 | if ( err == noErr ) | |
430 | { | |
431 | wxSize sz((int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) , | |
432 | (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 )); | |
433 | data->SetPaperSize(sz); | |
434 | ||
435 | PMRect rPage ; | |
436 | err = PMGetUnadjustedPageRect(m_macPageFormat , &rPage ) ; | |
437 | if ( err == noErr ) | |
438 | { | |
439 | data->SetMinMarginTopLeft( wxPoint ( | |
440 | (int)(((double) rPage.left - rPaper.left ) * pt2mm) , | |
441 | (int)(((double) rPage.top - rPaper.top ) * pt2mm) ) ) ; | |
442 | ||
443 | data->SetMinMarginBottomRight( wxPoint ( | |
444 | (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm), | |
445 | (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ; | |
446 | ||
447 | if ( data->GetMarginTopLeft().x < data->GetMinMarginTopLeft().x ) | |
448 | data->SetMarginTopLeft( wxPoint( data->GetMinMarginTopLeft().x , | |
449 | data->GetMarginTopLeft().y ) ) ; | |
450 | ||
451 | if ( data->GetMarginBottomRight().x < data->GetMinMarginBottomRight().x ) | |
452 | data->SetMarginBottomRight( wxPoint( data->GetMinMarginBottomRight().x , | |
453 | data->GetMarginBottomRight().y ) ); | |
454 | ||
455 | if ( data->GetMarginTopLeft().y < data->GetMinMarginTopLeft().y ) | |
456 | data->SetMarginTopLeft( wxPoint( data->GetMarginTopLeft().x , data->GetMinMarginTopLeft().y ) ); | |
457 | ||
458 | if ( data->GetMarginBottomRight().y < data->GetMinMarginBottomRight().y ) | |
459 | data->SetMarginBottomRight( wxPoint( data->GetMarginBottomRight().x , | |
460 | data->GetMinMarginBottomRight().y) ); | |
461 | } | |
462 | } | |
463 | } | |
464 | ||
465 | void wxOSXPrintData::TransferTo( wxPrintDialogData* data ) | |
466 | { | |
467 | #if wxOSX_USE_COCOA | |
468 | UpdateToPMState(); | |
469 | #endif | |
470 | UInt32 minPage , maxPage ; | |
471 | PMGetPageRange( m_macPrintSettings , &minPage , &maxPage ) ; | |
472 | data->SetMinPage( minPage ) ; | |
473 | data->SetMaxPage( maxPage ) ; | |
474 | UInt32 copies ; | |
475 | PMGetCopies( m_macPrintSettings , &copies ) ; | |
476 | data->SetNoCopies( copies ) ; | |
477 | UInt32 from , to ; | |
478 | PMGetFirstPage( m_macPrintSettings , &from ) ; | |
479 | PMGetLastPage( m_macPrintSettings , &to ) ; | |
480 | if ( to >= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages | |
481 | { | |
482 | data->SetAllPages( true ) ; | |
483 | // This means all pages, more or less | |
484 | data->SetFromPage(1); | |
485 | data->SetToPage(9999); | |
486 | } | |
487 | else | |
488 | { | |
489 | data->SetFromPage( from ) ; | |
490 | data->SetToPage( to ) ; | |
491 | data->SetAllPages( false ); | |
492 | } | |
493 | } | |
494 | ||
495 | void wxOSXPrintData::TransferFrom( wxPrintDialogData* data ) | |
496 | { | |
497 | // Respect the value of m_printAllPages | |
498 | if ( data->GetAllPages() ) | |
499 | PMSetPageRange( m_macPrintSettings , data->GetMinPage() , (UInt32) kPMPrintAllPages ) ; | |
500 | else | |
501 | PMSetPageRange( m_macPrintSettings , data->GetMinPage() , data->GetMaxPage() ) ; | |
502 | PMSetCopies( m_macPrintSettings , data->GetNoCopies() , false ) ; | |
503 | PMSetFirstPage( m_macPrintSettings , data->GetFromPage() , false ) ; | |
504 | ||
505 | if (data->GetAllPages() || data->GetFromPage() == 0) | |
506 | PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ; | |
507 | else | |
508 | PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ; | |
509 | #if wxOSX_USE_COCOA | |
510 | UpdateFromPMState(); | |
511 | #endif | |
512 | } | |
513 | ||
514 | wxPrintNativeDataBase* wxOSXCreatePrintData() | |
515 | { | |
516 | #if wxOSX_USE_COCOA | |
517 | return new wxOSXCocoaPrintData(); | |
518 | #elif wxOSX_USE_CARBON | |
519 | return new wxOSXCarbonPrintData(); | |
520 | #else | |
521 | return NULL; | |
522 | #endif | |
523 | } | |
524 | ||
525 | /* | |
526 | * Printer | |
527 | */ | |
528 | ||
529 | IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase) | |
530 | ||
531 | wxMacPrinter::wxMacPrinter(wxPrintDialogData *data): | |
532 | wxPrinterBase(data) | |
533 | { | |
534 | } | |
535 | ||
536 | wxMacPrinter::~wxMacPrinter(void) | |
537 | { | |
538 | } | |
539 | ||
540 | bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) | |
541 | { | |
542 | sm_abortIt = false; | |
543 | sm_abortWindow = NULL; | |
544 | ||
545 | if (!printout) | |
546 | { | |
547 | sm_lastError = wxPRINTER_ERROR; | |
548 | return false; | |
549 | } | |
550 | ||
551 | if (m_printDialogData.GetMinPage() < 1) | |
552 | m_printDialogData.SetMinPage(1); | |
553 | if (m_printDialogData.GetMaxPage() < 1) | |
554 | m_printDialogData.SetMaxPage(9999); | |
555 | ||
556 | // Create a suitable device context | |
557 | wxPrinterDC *dc = NULL; | |
558 | if (prompt) | |
559 | { | |
560 | wxMacPrintDialog dialog(parent, & m_printDialogData); | |
561 | if (dialog.ShowModal() == wxID_OK) | |
562 | { | |
563 | dc = wxDynamicCast(dialog.GetPrintDC(), wxPrinterDC); | |
564 | wxASSERT(dc); | |
565 | m_printDialogData = dialog.GetPrintDialogData(); | |
566 | } | |
567 | } | |
568 | else | |
569 | { | |
570 | dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ; | |
571 | } | |
572 | ||
573 | // May have pressed cancel. | |
574 | if (!dc || !dc->IsOk()) | |
575 | { | |
576 | delete dc; | |
577 | return false; | |
578 | } | |
579 | ||
580 | // on the mac we have always pixels as addressing mode with 72 dpi | |
581 | printout->SetPPIScreen(72, 72); | |
582 | ||
583 | PMResolution res; | |
584 | PMPrinter printer; | |
585 | wxOSXPrintData* nativeData = (wxOSXPrintData*) | |
586 | (m_printDialogData.GetPrintData().GetNativeData()); | |
587 | ||
588 | if (PMSessionGetCurrentPrinter(nativeData->GetPrintSession(), &printer) == noErr) | |
589 | { | |
590 | if (PMPrinterGetOutputResolution( printer, nativeData->GetPrintSettings(), &res) == -9589 /* kPMKeyNotFound */ ) | |
591 | { | |
592 | res.hRes = res.vRes = 300; | |
593 | } | |
594 | } | |
595 | else | |
596 | { | |
597 | // fallback | |
598 | res.hRes = res.vRes = 300; | |
599 | } | |
600 | printout->SetPPIPrinter(int(res.hRes), int(res.vRes)); | |
601 | ||
602 | // Set printout parameters | |
603 | printout->SetDC(dc); | |
604 | ||
605 | int w, h; | |
606 | dc->GetSize(&w, &h); | |
607 | printout->SetPageSizePixels((int)w, (int)h); | |
608 | printout->SetPaperRectPixels(dc->GetPaperRect()); | |
609 | wxCoord mw, mh; | |
610 | dc->GetSizeMM(&mw, &mh); | |
611 | printout->SetPageSizeMM((int)mw, (int)mh); | |
612 | ||
613 | // Create an abort window | |
614 | wxBeginBusyCursor(); | |
615 | ||
616 | printout->OnPreparePrinting(); | |
617 | ||
618 | // Get some parameters from the printout, if defined | |
619 | int fromPage, toPage; | |
620 | int minPage, maxPage; | |
621 | printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); | |
622 | ||
623 | if (maxPage == 0) | |
624 | { | |
625 | sm_lastError = wxPRINTER_ERROR; | |
626 | return false; | |
627 | } | |
628 | ||
629 | // Only set min and max, because from and to will be | |
630 | // set by the user | |
631 | m_printDialogData.SetMinPage(minPage); | |
632 | m_printDialogData.SetMaxPage(maxPage); | |
633 | ||
634 | printout->OnBeginPrinting(); | |
635 | ||
636 | bool keepGoing = true; | |
637 | ||
638 | if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) | |
639 | { | |
640 | wxEndBusyCursor(); | |
641 | wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent); | |
642 | } | |
643 | ||
644 | int pn; | |
645 | for (pn = m_printDialogData.GetFromPage(); | |
646 | keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn); | |
647 | pn++) | |
648 | { | |
649 | if (sm_abortIt) | |
650 | { | |
651 | break; | |
652 | } | |
653 | else | |
654 | { | |
655 | dc->StartPage(); | |
656 | keepGoing = printout->OnPrintPage(pn); | |
657 | dc->EndPage(); | |
658 | } | |
659 | } | |
660 | printout->OnEndDocument(); | |
661 | ||
662 | printout->OnEndPrinting(); | |
663 | ||
664 | if (sm_abortWindow) | |
665 | { | |
666 | sm_abortWindow->Show(false); | |
667 | wxDELETE(sm_abortWindow); | |
668 | } | |
669 | ||
670 | wxEndBusyCursor(); | |
671 | ||
672 | delete dc; | |
673 | ||
674 | return true; | |
675 | } | |
676 | ||
677 | wxDC* wxMacPrinter::PrintDialog(wxWindow *parent) | |
678 | { | |
679 | wxDC* dc = NULL; | |
680 | ||
681 | wxPrintDialog dialog(parent, & m_printDialogData); | |
682 | int ret = dialog.ShowModal(); | |
683 | ||
684 | if (ret == wxID_OK) | |
685 | { | |
686 | dc = dialog.GetPrintDC(); | |
687 | m_printDialogData = dialog.GetPrintDialogData(); | |
688 | } | |
689 | ||
690 | return dc; | |
691 | } | |
692 | ||
693 | bool wxMacPrinter::Setup(wxWindow *WXUNUSED(parent)) | |
694 | { | |
695 | #if 0 | |
696 | wxPrintDialog dialog(parent, & m_printDialogData); | |
697 | dialog.GetPrintDialogData().SetSetupDialog(true); | |
698 | ||
699 | int ret = dialog.ShowModal(); | |
700 | ||
701 | if (ret == wxID_OK) | |
702 | m_printDialogData = dialog.GetPrintDialogData(); | |
703 | ||
704 | return (ret == wxID_OK); | |
705 | #endif | |
706 | ||
707 | return false; | |
708 | } | |
709 | ||
710 | /* | |
711 | * Print preview | |
712 | */ | |
713 | ||
714 | IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase) | |
715 | ||
716 | wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, | |
717 | wxPrintout *printoutForPrinting, | |
718 | wxPrintDialogData *data) | |
719 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
720 | { | |
721 | DetermineScaling(); | |
722 | } | |
723 | ||
724 | wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data): | |
725 | wxPrintPreviewBase(printout, printoutForPrinting, data) | |
726 | { | |
727 | DetermineScaling(); | |
728 | } | |
729 | ||
730 | wxMacPrintPreview::~wxMacPrintPreview(void) | |
731 | { | |
732 | } | |
733 | ||
734 | bool wxMacPrintPreview::Print(bool interactive) | |
735 | { | |
736 | if (!m_printPrintout) | |
737 | return false; | |
738 | ||
739 | wxMacPrinter printer(&m_printDialogData); | |
740 | return printer.Print(m_previewFrame, m_printPrintout, interactive); | |
741 | } | |
742 | ||
743 | void wxMacPrintPreview::DetermineScaling(void) | |
744 | { | |
745 | int screenWidth , screenHeight ; | |
746 | wxDisplaySize( &screenWidth , &screenHeight ) ; | |
747 | ||
748 | wxSize ppiScreen( 72 , 72 ) ; | |
749 | wxSize ppiPrinter( 72 , 72 ) ; | |
750 | ||
751 | // Note that with Leopard, screen dpi=72 is no longer a given | |
752 | m_previewPrintout->SetPPIScreen( ppiScreen.x , ppiScreen.y ) ; | |
753 | ||
754 | wxCoord w , h ; | |
755 | wxCoord ww, hh; | |
756 | wxRect paperRect; | |
757 | ||
758 | // Get a device context for the currently selected printer | |
759 | wxPrinterDC printerDC(m_printDialogData.GetPrintData()); | |
760 | if (printerDC.IsOk()) | |
761 | { | |
762 | printerDC.GetSizeMM(&ww, &hh); | |
763 | printerDC.GetSize( &w , &h ) ; | |
764 | ppiPrinter = printerDC.GetPPI() ; | |
765 | paperRect = printerDC.GetPaperRect(); | |
766 | m_isOk = true ; | |
767 | } | |
768 | else | |
769 | { | |
770 | // use some defaults | |
771 | w = 8 * 72 ; | |
772 | h = 11 * 72 ; | |
773 | ww = (wxCoord) (w * 25.4 / ppiPrinter.x) ; | |
774 | hh = (wxCoord) (h * 25.4 / ppiPrinter.y) ; | |
775 | paperRect = wxRect(0, 0, w, h); | |
776 | m_isOk = false ; | |
777 | } | |
778 | m_pageWidth = w; | |
779 | m_pageHeight = h; | |
780 | ||
781 | m_previewPrintout->SetPageSizePixels(w , h) ; | |
782 | m_previewPrintout->SetPageSizeMM(ww, hh); | |
783 | m_previewPrintout->SetPaperRectPixels(paperRect); | |
784 | m_previewPrintout->SetPPIPrinter( ppiPrinter.x , ppiPrinter.y ) ; | |
785 | ||
786 | m_previewScaleX = float(ppiScreen.x) / ppiPrinter.x; | |
787 | m_previewScaleY = float(ppiScreen.y) / ppiPrinter.y; | |
788 | } | |
789 | ||
790 | // | |
791 | // end of print_osx.cpp | |
792 | // | |
793 | ||
794 | #if wxOSX_USE_CARBON | |
795 | ||
796 | IMPLEMENT_DYNAMIC_CLASS(wxOSXCarbonPrintData, wxOSXPrintData) | |
797 | ||
798 | wxOSXCarbonPrintData::wxOSXCarbonPrintData() | |
799 | { | |
800 | if ( PMCreateSession( &m_macPrintSession ) == noErr ) | |
801 | { | |
802 | if ( PMCreatePageFormat(&m_macPageFormat) == noErr ) | |
803 | { | |
804 | PMSessionDefaultPageFormat(m_macPrintSession, | |
805 | m_macPageFormat); | |
806 | PMGetPageFormatPaper(m_macPageFormat, &m_macPaper); | |
807 | } | |
808 | ||
809 | if ( PMCreatePrintSettings(&m_macPrintSettings) == noErr ) | |
810 | { | |
811 | PMSessionDefaultPrintSettings(m_macPrintSession, | |
812 | m_macPrintSettings); | |
813 | } | |
814 | } | |
815 | } | |
816 | ||
817 | wxOSXCarbonPrintData::~wxOSXCarbonPrintData() | |
818 | { | |
819 | (void)PMRelease(m_macPageFormat); | |
820 | (void)PMRelease(m_macPrintSettings); | |
821 | (void)PMRelease(m_macPrintSession); | |
822 | } | |
823 | #endif | |
824 | ||
825 | #endif |