1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMacPrinter framework
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "printwin.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/msgdlg.h"
32 #include "wx/mac/uma.h"
34 #include "wx/mac/printmac.h"
35 #include "wx/mac/private/print.h"
37 #define mm2pt 2.83464566929
38 #define pt2mm 0.352777777778
40 #include "wx/dcprint.h"
41 #include "wx/printdlg.h"
42 #include "wx/mac/printdlg.h"
46 #if !USE_SHARED_LIBRARY
47 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter
, wxPrinterBase
)
48 IMPLEMENT_CLASS(wxMacPrintPreview
, wxPrintPreviewBase
)
53 wxNativePrintData
* wxNativePrintData::Create()
55 return new wxMacCarbonPrintData() ;
58 wxMacCarbonPrintData::wxMacCarbonPrintData()
60 m_macPageFormat
= kPMNoPageFormat
;
61 m_macPrintSettings
= kPMNoPrintSettings
;
62 m_macPrintSession
= kPMNoReference
;
66 wxMacCarbonPrintData::~wxMacCarbonPrintData()
68 if (m_macPageFormat
!= kPMNoPageFormat
)
70 (void)PMRelease(m_macPageFormat
);
71 m_macPageFormat
= kPMNoPageFormat
;
74 if (m_macPrintSettings
!= kPMNoPrintSettings
)
76 (void)PMRelease(m_macPrintSettings
);
77 m_macPrintSettings
= kPMNoPrintSettings
;
80 if ( m_macPrintSession
!= kPMNoReference
)
82 (void)PMRelease(m_macPrintSession
);
83 m_macPrintSession
= kPMNoReference
;
87 void wxMacCarbonPrintData::ValidateOrCreate()
89 OSStatus err
= noErr
;
90 if ( m_macPrintSession
== kPMNoReference
)
92 err
= PMCreateSession( (PMPrintSession
*) &m_macPrintSession
) ;
94 // Set up a valid PageFormat object.
95 if ( m_macPageFormat
== kPMNoPageFormat
)
97 err
= PMCreatePageFormat((PMPageFormat
*) &m_macPageFormat
);
99 // Note that PMPageFormat is not session-specific, but calling
100 // PMSessionDefaultPageFormat assigns values specific to the printer
101 // associated with the current printing session.
102 if ((err
== noErr
) &&
103 ( m_macPageFormat
!= kPMNoPageFormat
))
105 err
= PMSessionDefaultPageFormat((PMPrintSession
) m_macPrintSession
,
106 (PMPageFormat
) m_macPageFormat
);
111 err
= PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
112 (PMPageFormat
) m_macPageFormat
,
116 // Set up a valid PrintSettings object.
117 if ( m_macPrintSettings
== kPMNoPrintSettings
)
119 err
= PMCreatePrintSettings((PMPrintSettings
*) &m_macPrintSettings
);
121 // Note that PMPrintSettings is not session-specific, but calling
122 // PMSessionDefaultPrintSettings assigns values specific to the printer
123 // associated with the current printing session.
124 if ((err
== noErr
) &&
125 ( m_macPrintSettings
!= kPMNoPrintSettings
))
127 err
= PMSessionDefaultPrintSettings((PMPrintSession
) m_macPrintSession
,
128 (PMPrintSettings
) m_macPrintSettings
);
133 err
= PMSessionValidatePrintSettings((PMPrintSession
) m_macPrintSession
,
134 (PMPrintSettings
) m_macPrintSettings
,
139 void wxMacCarbonPrintData::TransferFrom( wxPrintData
* data
)
142 PMSetCopies( (PMPrintSettings
) m_macPrintSettings
, data
->GetNoCopies() , false ) ;
143 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
->GetOrientation() == wxLANDSCAPE
) ?
144 kPMLandscape
: kPMPortrait
, false ) ;
145 // collate cannot be set
146 #if 0 // not yet tested
147 if ( m_printerName
.Length() > 0 )
148 PMSessionSetCurrentPrinter( (PMPrintSession
) m_macPrintSession
, wxMacCFStringHolder( m_printerName
, wxFont::GetDefaultEncoding() ) ) ;
151 PMGetColorMode( (PMPrintSettings
) m_macPrintSettings
, &color
) ;
152 if ( data
->GetColour() )
154 if ( color
== kPMBlackAndWhite
)
155 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMColor
) ;
158 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMBlackAndWhite
) ;
160 // PMDuplexMode not yet accessible via API
161 // PMQualityMode not yet accessible via API
165 void wxMacCarbonPrintData::TransferTo( wxPrintData
* data
)
167 OSStatus err
= noErr
;
170 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
172 data
->SetNoCopies( copies
) ;
174 PMOrientation orientation
;
175 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
178 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
179 data
->SetOrientation( wxPORTRAIT
);
181 data
->SetOrientation( wxLANDSCAPE
);
184 // collate cannot be set
187 wxMacCFStringHolder name
;
189 PMSessionGetCurrentPrinter( m_macPrintSession
,
191 m_printerName
= name
.AsString() ;
196 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
198 data
->SetColour( !(color
== kPMBlackAndWhite
) ) ;
200 // PMDuplexMode not yet accessible via API
201 // PMQualityMode not yet accessible via API
204 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
207 data
->SetPaperSize( wxSize (
208 (int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
209 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) ) );
213 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*data
)
215 // should we setup the page rect here ?
216 // since MacOS sometimes has two same paper rects with different
217 // page rects we could make it roundtrip safe perhaps
223 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
226 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
230 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
233 data
->SetMinMarginTopLeft( wxPoint (
234 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
235 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
237 data
->SetMinMarginBottomRight( wxPoint (
238 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
239 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
244 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
246 UInt32 minPage
, maxPage
;
247 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
248 data
->SetMinPage( minPage
) ;
249 data
->SetMaxPage( maxPage
) ;
251 PMGetCopies( m_macPrintSettings
, &copies
) ;
252 data
->SetNoCopies( copies
) ;
254 PMGetFirstPage( m_macPrintSettings
, &from
) ;
255 PMGetLastPage( m_macPrintSettings
, &to
) ;
256 data
->SetFromPage( from
) ;
257 data
->SetToPage( to
) ;
260 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
262 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
263 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
264 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
266 int toPage
= data
->GetToPage();
268 toPage
= data
->GetFromPage();
269 PMSetLastPage( m_macPrintSettings
, toPage
, false ) ;
272 void wxMacCarbonPrintData::CopyFrom( wxNativePrintData
* d
)
274 wxMacCarbonPrintData
*data
= (wxMacCarbonPrintData
*) d
;
275 if ( data
->m_macPrintSession
!= kPMNoReference
)
276 PMRetain( data
->m_macPrintSession
) ;
277 if ( m_macPrintSession
!= kPMNoReference
)
279 PMRelease( m_macPrintSession
) ;
280 m_macPrintSession
= kPMNoReference
;
282 if ( data
->m_macPrintSession
!= kPMNoReference
)
283 m_macPrintSession
= data
->m_macPrintSession
;
285 if ( data
->m_macPrintSettings
!= kPMNoPrintSettings
)
286 PMRetain( data
->m_macPrintSettings
) ;
287 if ( m_macPrintSettings
!= kPMNoPrintSettings
)
289 PMRelease( m_macPrintSettings
) ;
290 m_macPrintSettings
= kPMNoPrintSettings
;
292 if ( data
->m_macPrintSettings
!= kPMNoPrintSettings
)
293 m_macPrintSettings
= data
->m_macPrintSettings
;
295 if ( data
->m_macPageFormat
!= kPMNoPageFormat
)
296 PMRetain( data
->m_macPageFormat
) ;
297 if ( m_macPageFormat
!= kPMNoPageFormat
)
299 PMRelease( m_macPageFormat
) ;
300 m_macPageFormat
= kPMNoPageFormat
;
302 if ( data
->m_macPageFormat
!= kPMNoPageFormat
)
303 m_macPageFormat
= data
->m_macPageFormat
;
306 int wxMacCarbonPrintData::ShowPrintDialog()
308 int result
= wxID_CANCEL
;
315 // Display the Print dialog.
318 err
= PMSessionPrintDialog( m_macPrintSession
,
322 if ((err
== noErr
) && !accepted
)
324 err
= kPMCancel
; // user clicked Cancel button
332 if ((err
!= noErr
) && (err
!= kPMCancel
))
334 message
.Printf( wxT("Print Error %d"), err
) ;
335 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
342 int wxMacCarbonPrintData::ShowPageSetupDialog()
344 int result
= wxID_CANCEL
;
350 // Display the Page Setup dialog.
353 err
= PMSessionPageSetupDialog( m_macPrintSession
,
356 if ((err
== noErr
) && !accepted
)
358 err
= kPMCancel
; // user clicked Cancel button
362 // If the user did not cancel, flatten and save the PageFormat object
363 // with our document.
368 if ((err
!= noErr
) && (err
!= kPMCancel
))
370 message
.Printf( wxT("Print Error %d"), err
) ;
371 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
380 wxNativePrintData
* wxNativePrintData::Create()
382 return new wxMacClassicPrintData() ;
385 wxMacClassicPrintData::wxMacClassicPrintData()
387 m_macPrintSettings
= NULL
;
391 wxMacClassicPrintData::~wxMacClassicPrintData()
393 wxASSERT( m_macPrintSettings
);
394 DisposeHandle( (Handle
) m_macPrintSettings
) ;
397 void wxMacClassicPrintData::ValidateOrCreate()
399 if ( m_macPrintSettings
== NULL
)
401 m_macPrintSettings
= (THPrint
) NewHandleClear( sizeof( TPrint
) );
402 (**m_macPrintSettings
).iPrVersion
= 0; // something invalid
404 (**m_macPrintSettings
).prInfo
.iHRes
= 72;
405 (**m_macPrintSettings
).prInfo
.iVRes
= 72;
406 Rect r1
= { 0, 0, 8*72 - 2 * 18, 11*72 - 2 * 36 };
407 (**m_macPrintSettings
).prInfo
.rPage
= r1
;// must have its top left & (0,0)
409 Rect r2
= { -18, -36, 8*72 - 18, 11*72 - 36 };
410 (**m_macPrintSettings
).rPaper
= r2
;
411 (**m_macPrintSettings
).prStl
.iPageV
= 11 * 120 ; // 11 inches in 120th of an inch
412 (**m_macPrintSettings
).prStl
.iPageH
= 8 * 120 ; // 8 inches in 120th of an inch
419 void wxMacClassicPrintData::TransferFrom( wxPrintData
* data
)
422 (**m_macPrintSettings
).prJob
.iCopies
= data
->GetNoCopies() ;
423 // on mac the paper rect has a negative top left corner, because the page rect (printable area) is at 0,0
424 // if all printing data is consolidated in on structure we will be able to set additional infos about pages
427 void wxMacClassicPrintData::TransferTo( wxPrintData
* data
)
429 data
->SetNoCopies( (**m_macPrintSettings
).prJob
.iCopies
);
430 data
->SetPaperSize( wxSize(
431 ((double) (**m_macPrintSettings
).rPaper
.right
- (**m_macPrintSettings
).rPaper
.left
) * pt2mm
,
432 ((double) (**m_macPrintSettings
).rPaper
.bottom
- (**m_macPrintSettings
).rPaper
.top
) * pt2mm
) ) ;
435 void wxMacClassicPrintData::TransferFrom( wxPageSetupData
*data
)
439 void wxMacClassicPrintData::TransferTo( wxPageSetupData
* data
)
441 data
->SetMinMarginTopLeft( wxPoint(
442 ((double) (**m_macPrintSettings
).prInfo
.rPage
.left
-(**m_macPrintSettings
).rPaper
.left
) * pt2mm
,
443 ((double) (**m_macPrintSettings
).prInfo
.rPage
.top
-(**m_macPrintSettings
).rPaper
.top
) * pt2mm
) ) ;
444 data
->SetMinMarginBottomRight( wxPoint(
445 ((double) (**m_macPrintSettings
).rPaper
.right
- (**m_macPrintSettings
).prInfo
.rPage
.right
) * pt2mm
,
446 ((double)(**m_macPrintSettings
).rPaper
.bottom
- (**m_macPrintSettings
).prInfo
.rPage
.bottom
) * pt2mm
) ) ;
449 void wxMacClassicPrintData::TransferFrom( wxPrintDialogData
* data
)
451 int toPage
= data
->GetToPage();
453 toPage
= data
->GetFromPage();
454 (**m_macPrintSettings
).prJob
.iFstPage
= data
->GetFromPage() ;
455 (**m_macPrintSettings
).prJob
.iLstPage
= toPage
;
458 void wxMacClassicPrintData::TransferTo( wxPrintDialogData
* data
)
460 data
->SetFromPage( (**m_macPrintSettings
).prJob
.iFstPage
) ;
461 data
->SetToPage( (**m_macPrintSettings
).prJob
.iLstPage
) ;
464 void wxMacClassicPrintData::CopyFrom( wxNativePrintData
* data
)
466 DisposeHandle( (Handle
) m_macPrintSettings
) ;
467 m_macPrintSettings
= ((wxMacClassicPrintData
*)data
)->m_macPrintSettings
;
468 HandToHand( (Handle
*) &m_macPrintSettings
);
471 int wxMacClassicPrintData::ShowPrintDialog()
473 int result
= wxID_CANCEL
;
477 err
= ::UMAPrOpen() ;
480 if ( ::PrJobDialog( m_macPrintSettings
) )
488 message
.Printf( wxT("Print Error %d"), err
) ;
489 wxMessageDialog
dialog( NULL
, message
, wxT(""), wxICON_HAND
| wxOK
) ;
497 int wxMacClassicPrintData::ShowPageSetupDialog()
499 int result
= wxID_CANCEL
;
503 err
= ::UMAPrOpen() ;
506 if ( ::PrStlDialog( m_macPrintSettings
) )
514 message
.Printf( wxT("Print Error %d"), err
) ;
515 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
528 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
533 wxMacPrinter::~wxMacPrinter(void)
537 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
540 sm_abortWindow
= NULL
;
545 printout
->SetIsPreview(FALSE
);
546 if (m_printDialogData
.GetMinPage() < 1)
547 m_printDialogData
.SetMinPage(1);
548 if (m_printDialogData
.GetMaxPage() < 1)
549 m_printDialogData
.SetMaxPage(9999);
551 // Create a suitable device context
555 wxPrintDialog
dialog(parent
, & m_printDialogData
);
556 if (dialog
.ShowModal() == wxID_OK
)
558 dc
= dialog
.GetPrintDC();
559 m_printDialogData
= dialog
.GetPrintDialogData();
564 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
568 // May have pressed cancel.
569 if (!dc
|| !dc
->Ok())
575 // on the mac we have always pixels as addressing mode with 72 dpi
577 printout
->SetPPIScreen(72, 72);
578 printout
->SetPPIPrinter(72, 72);
580 // Set printout parameters
586 printout
->SetPageSizePixels((int)w
, (int)h
);
587 dc
->GetSizeMM(&ww
, &hh
);
588 printout
->SetPageSizeMM((int)ww
, (int)hh
);
590 // Create an abort window
593 printout
->OnPreparePrinting();
595 // Get some parameters from the printout, if defined
596 int fromPage
, toPage
;
597 int minPage
, maxPage
;
598 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
606 // Only set min and max, because from and to have been
608 m_printDialogData
.SetMinPage(minPage
);
609 m_printDialogData
.SetMaxPage(maxPage
);
611 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
612 wxSafeYield(win
,true);
617 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
621 sm_abortWindow
= win
;
622 sm_abortWindow
->Show(TRUE
);
623 wxSafeYield(win
,true);
625 printout
->OnBeginPrinting();
627 bool keepGoing
= TRUE
;
630 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
632 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
635 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
642 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
653 if ( UMAGetSystemVersion() >= 0x1000 )
657 GetPort( &thePort
) ;
658 wxSafeYield(win
,true);
662 keepGoing
= printout
->OnPrintPage(pn
);
666 printout
->OnEndDocument();
669 printout
->OnEndPrinting();
673 sm_abortWindow
->Show(FALSE
);
674 delete sm_abortWindow
;
675 sm_abortWindow
= NULL
;
685 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
687 wxDC
* dc
= (wxDC
*) NULL
;
689 wxPrintDialog
dialog(parent
, & m_printDialogData
);
690 int ret
= dialog
.ShowModal();
694 dc
= dialog
.GetPrintDC();
695 m_printDialogData
= dialog
.GetPrintDialogData();
701 bool wxMacPrinter::Setup(wxWindow
*parent
)
704 wxPrintDialog
dialog(parent
, & m_printDialogData
);
705 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
707 int ret
= dialog
.ShowModal();
711 m_printDialogData
= dialog
.GetPrintDialogData();
714 return (ret
== wxID_OK
);
723 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
724 wxPrintout
*printoutForPrinting
,
725 wxPrintDialogData
*data
)
726 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
731 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
732 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
737 wxMacPrintPreview::~wxMacPrintPreview(void)
741 bool wxMacPrintPreview::Print(bool interactive
)
743 if (!m_printPrintout
)
745 wxMacPrinter
printer(&m_printDialogData
);
746 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
749 void wxMacPrintPreview::DetermineScaling(void)
751 int screenWidth
, screenHeight
;
752 wxDisplaySize( &screenWidth
, &screenHeight
) ;
754 m_previewPrintout
->SetPPIScreen( 72 , 72 ) ;
755 m_previewPrintout
->SetPPIPrinter( 72 , 72 ) ;
756 m_previewPrintout
->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
757 m_previewPrintout
->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
758 m_pageWidth
= 8 * 72 ;
759 m_pageHeight
= 11 * 72 ;
762 // Get a device context for the currently selected printer
763 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
768 printerDC
.GetSizeMM(&ww
, &hh
);
769 printerDC
.GetSize( &x
, &y
) ;
770 m_previewPrintout
->SetPageSizeMM((int)ww
, (int)hh
);
771 m_previewPrintout
->SetPageSizePixels( x
, y
) ;
780 // At 100%, the page should look about page-size on the screen.
781 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
782 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);