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