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