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"
45 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter
, wxPrinterBase
)
47 IMPLEMENT_CLASS(wxMacPrintPreview
, wxPrintPreviewBase
)
52 wxNativePrintData
* wxNativePrintData::Create()
54 return new wxMacCarbonPrintData() ;
57 wxMacCarbonPrintData::wxMacCarbonPrintData()
59 m_macPageFormat
= kPMNoPageFormat
;
60 m_macPrintSettings
= kPMNoPrintSettings
;
61 m_macPrintSession
= kPMNoReference
;
65 wxMacCarbonPrintData::~wxMacCarbonPrintData()
67 if (m_macPageFormat
!= kPMNoPageFormat
)
69 (void)PMRelease(m_macPageFormat
);
70 m_macPageFormat
= kPMNoPageFormat
;
73 if (m_macPrintSettings
!= kPMNoPrintSettings
)
75 (void)PMRelease(m_macPrintSettings
);
76 m_macPrintSettings
= kPMNoPrintSettings
;
79 if ( m_macPrintSession
!= kPMNoReference
)
81 (void)PMRelease(m_macPrintSession
);
82 m_macPrintSession
= kPMNoReference
;
86 void wxMacCarbonPrintData::ValidateOrCreate()
88 OSStatus err
= noErr
;
89 if ( m_macPrintSession
== kPMNoReference
)
91 err
= PMCreateSession( (PMPrintSession
*) &m_macPrintSession
) ;
93 // Set up a valid PageFormat object.
94 if ( m_macPageFormat
== kPMNoPageFormat
)
96 err
= PMCreatePageFormat((PMPageFormat
*) &m_macPageFormat
);
98 // Note that PMPageFormat is not session-specific, but calling
99 // PMSessionDefaultPageFormat assigns values specific to the printer
100 // associated with the current printing session.
101 if ((err
== noErr
) &&
102 ( m_macPageFormat
!= kPMNoPageFormat
))
104 err
= PMSessionDefaultPageFormat((PMPrintSession
) m_macPrintSession
,
105 (PMPageFormat
) m_macPageFormat
);
110 err
= PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
111 (PMPageFormat
) m_macPageFormat
,
115 // Set up a valid PrintSettings object.
116 if ( m_macPrintSettings
== kPMNoPrintSettings
)
118 err
= PMCreatePrintSettings((PMPrintSettings
*) &m_macPrintSettings
);
120 // Note that PMPrintSettings is not session-specific, but calling
121 // PMSessionDefaultPrintSettings assigns values specific to the printer
122 // associated with the current printing session.
123 if ((err
== noErr
) &&
124 ( m_macPrintSettings
!= kPMNoPrintSettings
))
126 err
= PMSessionDefaultPrintSettings((PMPrintSession
) m_macPrintSession
,
127 (PMPrintSettings
) m_macPrintSettings
);
132 err
= PMSessionValidatePrintSettings((PMPrintSession
) m_macPrintSession
,
133 (PMPrintSettings
) m_macPrintSettings
,
138 void wxMacCarbonPrintData::TransferFrom( wxPrintData
* data
)
141 PMSetCopies( (PMPrintSettings
) m_macPrintSettings
, data
->GetNoCopies() , false ) ;
142 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
->GetOrientation() == wxLANDSCAPE
) ?
143 kPMLandscape
: kPMPortrait
, false ) ;
144 // collate cannot be set
145 #if 0 // not yet tested
146 if ( m_printerName
.Length() > 0 )
147 PMSessionSetCurrentPrinter( (PMPrintSession
) m_macPrintSession
, wxMacCFStringHolder( m_printerName
, wxFont::GetDefaultEncoding() ) ) ;
150 PMGetColorMode( (PMPrintSettings
) m_macPrintSettings
, &color
) ;
151 if ( data
->GetColour() )
153 if ( color
== kPMBlackAndWhite
)
154 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMColor
) ;
157 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMBlackAndWhite
) ;
159 // PMDuplexMode not yet accessible via API
160 // PMQualityMode not yet accessible via API
164 void wxMacCarbonPrintData::TransferTo( wxPrintData
* data
)
166 OSStatus err
= noErr
;
169 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
171 data
->SetNoCopies( copies
) ;
173 PMOrientation orientation
;
174 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
177 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
178 data
->SetOrientation( wxPORTRAIT
);
180 data
->SetOrientation( wxLANDSCAPE
);
183 // collate cannot be set
186 wxMacCFStringHolder name
;
188 PMSessionGetCurrentPrinter( m_macPrintSession
,
190 m_printerName
= name
.AsString() ;
195 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
197 data
->SetColour( !(color
== kPMBlackAndWhite
) ) ;
199 // PMDuplexMode not yet accessible via API
200 // PMQualityMode not yet accessible via API
203 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
206 data
->SetPaperSize( wxSize (
207 (int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
208 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) ) );
212 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*data
)
214 // should we setup the page rect here ?
215 // since MacOS sometimes has two same paper rects with different
216 // page rects we could make it roundtrip safe perhaps
222 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
225 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
229 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
232 data
->SetMinMarginTopLeft( wxPoint (
233 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
234 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
236 data
->SetMinMarginBottomRight( wxPoint (
237 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
238 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
243 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
245 UInt32 minPage
, maxPage
;
246 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
247 data
->SetMinPage( minPage
) ;
248 data
->SetMaxPage( maxPage
) ;
250 PMGetCopies( m_macPrintSettings
, &copies
) ;
251 data
->SetNoCopies( copies
) ;
253 PMGetFirstPage( m_macPrintSettings
, &from
) ;
254 PMGetLastPage( m_macPrintSettings
, &to
) ;
255 data
->SetFromPage( from
) ;
256 data
->SetToPage( to
) ;
259 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
261 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
262 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
263 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
265 int toPage
= data
->GetToPage();
267 toPage
= data
->GetFromPage();
268 PMSetLastPage( m_macPrintSettings
, toPage
, false ) ;
271 void wxMacCarbonPrintData::CopyFrom( wxNativePrintData
* d
)
273 wxMacCarbonPrintData
*data
= (wxMacCarbonPrintData
*) d
;
274 if ( data
->m_macPrintSession
!= kPMNoReference
)
275 PMRetain( data
->m_macPrintSession
) ;
276 if ( m_macPrintSession
!= kPMNoReference
)
278 PMRelease( m_macPrintSession
) ;
279 m_macPrintSession
= kPMNoReference
;
281 if ( data
->m_macPrintSession
!= kPMNoReference
)
282 m_macPrintSession
= data
->m_macPrintSession
;
284 if ( data
->m_macPrintSettings
!= kPMNoPrintSettings
)
285 PMRetain( data
->m_macPrintSettings
) ;
286 if ( m_macPrintSettings
!= kPMNoPrintSettings
)
288 PMRelease( m_macPrintSettings
) ;
289 m_macPrintSettings
= kPMNoPrintSettings
;
291 if ( data
->m_macPrintSettings
!= kPMNoPrintSettings
)
292 m_macPrintSettings
= data
->m_macPrintSettings
;
294 if ( data
->m_macPageFormat
!= kPMNoPageFormat
)
295 PMRetain( data
->m_macPageFormat
) ;
296 if ( m_macPageFormat
!= kPMNoPageFormat
)
298 PMRelease( m_macPageFormat
) ;
299 m_macPageFormat
= kPMNoPageFormat
;
301 if ( data
->m_macPageFormat
!= kPMNoPageFormat
)
302 m_macPageFormat
= data
->m_macPageFormat
;
305 int wxMacCarbonPrintData::ShowPrintDialog()
307 int result
= wxID_CANCEL
;
314 // Display the Print dialog.
317 err
= PMSessionPrintDialog( m_macPrintSession
,
321 if ((err
== noErr
) && !accepted
)
323 err
= kPMCancel
; // user clicked Cancel button
331 if ((err
!= noErr
) && (err
!= kPMCancel
))
333 message
.Printf( wxT("Print Error %d"), err
) ;
334 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
341 int wxMacCarbonPrintData::ShowPageSetupDialog()
343 int result
= wxID_CANCEL
;
349 // Display the Page Setup dialog.
352 err
= PMSessionPageSetupDialog( m_macPrintSession
,
355 if ((err
== noErr
) && !accepted
)
357 err
= kPMCancel
; // user clicked Cancel button
361 // If the user did not cancel, flatten and save the PageFormat object
362 // with our document.
367 if ((err
!= noErr
) && (err
!= kPMCancel
))
369 message
.Printf( wxT("Print Error %d"), err
) ;
370 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
379 wxNativePrintData
* wxNativePrintData::Create()
381 return new wxMacClassicPrintData() ;
384 wxMacClassicPrintData::wxMacClassicPrintData()
386 m_macPrintSettings
= NULL
;
390 wxMacClassicPrintData::~wxMacClassicPrintData()
392 wxASSERT( m_macPrintSettings
);
393 DisposeHandle( (Handle
) m_macPrintSettings
) ;
396 void wxMacClassicPrintData::ValidateOrCreate()
398 if ( m_macPrintSettings
== NULL
)
400 m_macPrintSettings
= (THPrint
) NewHandleClear( sizeof( TPrint
) );
401 (**m_macPrintSettings
).iPrVersion
= 0; // something invalid
403 (**m_macPrintSettings
).prInfo
.iHRes
= 72;
404 (**m_macPrintSettings
).prInfo
.iVRes
= 72;
405 Rect r1
= { 0, 0, 8*72 - 2 * 18, 11*72 - 2 * 36 };
406 (**m_macPrintSettings
).prInfo
.rPage
= r1
;// must have its top left & (0,0)
408 Rect r2
= { -18, -36, 8*72 - 18, 11*72 - 36 };
409 (**m_macPrintSettings
).rPaper
= r2
;
410 (**m_macPrintSettings
).prStl
.iPageV
= 11 * 120 ; // 11 inches in 120th of an inch
411 (**m_macPrintSettings
).prStl
.iPageH
= 8 * 120 ; // 8 inches in 120th of an inch
418 void wxMacClassicPrintData::TransferFrom( wxPrintData
* data
)
421 (**m_macPrintSettings
).prJob
.iCopies
= data
->GetNoCopies() ;
422 // on mac the paper rect has a negative top left corner, because the page rect (printable area) is at 0,0
423 // if all printing data is consolidated in on structure we will be able to set additional infos about pages
426 void wxMacClassicPrintData::TransferTo( wxPrintData
* data
)
428 data
->SetNoCopies( (**m_macPrintSettings
).prJob
.iCopies
);
429 data
->SetPaperSize( wxSize(
430 ((double) (**m_macPrintSettings
).rPaper
.right
- (**m_macPrintSettings
).rPaper
.left
) * pt2mm
,
431 ((double) (**m_macPrintSettings
).rPaper
.bottom
- (**m_macPrintSettings
).rPaper
.top
) * pt2mm
) ) ;
434 void wxMacClassicPrintData::TransferFrom( wxPageSetupData
*data
)
438 void wxMacClassicPrintData::TransferTo( wxPageSetupData
* data
)
440 data
->SetMinMarginTopLeft( wxPoint(
441 ((double) (**m_macPrintSettings
).prInfo
.rPage
.left
-(**m_macPrintSettings
).rPaper
.left
) * pt2mm
,
442 ((double) (**m_macPrintSettings
).prInfo
.rPage
.top
-(**m_macPrintSettings
).rPaper
.top
) * pt2mm
) ) ;
443 data
->SetMinMarginBottomRight( wxPoint(
444 ((double) (**m_macPrintSettings
).rPaper
.right
- (**m_macPrintSettings
).prInfo
.rPage
.right
) * pt2mm
,
445 ((double)(**m_macPrintSettings
).rPaper
.bottom
- (**m_macPrintSettings
).prInfo
.rPage
.bottom
) * pt2mm
) ) ;
448 void wxMacClassicPrintData::TransferFrom( wxPrintDialogData
* data
)
450 int toPage
= data
->GetToPage();
452 toPage
= data
->GetFromPage();
453 (**m_macPrintSettings
).prJob
.iFstPage
= data
->GetFromPage() ;
454 (**m_macPrintSettings
).prJob
.iLstPage
= toPage
;
457 void wxMacClassicPrintData::TransferTo( wxPrintDialogData
* data
)
459 data
->SetFromPage( (**m_macPrintSettings
).prJob
.iFstPage
) ;
460 data
->SetToPage( (**m_macPrintSettings
).prJob
.iLstPage
) ;
463 void wxMacClassicPrintData::CopyFrom( wxNativePrintData
* data
)
465 DisposeHandle( (Handle
) m_macPrintSettings
) ;
466 m_macPrintSettings
= ((wxMacClassicPrintData
*)data
)->m_macPrintSettings
;
467 HandToHand( (Handle
*) &m_macPrintSettings
);
470 int wxMacClassicPrintData::ShowPrintDialog()
472 int result
= wxID_CANCEL
;
476 err
= ::UMAPrOpen() ;
479 if ( ::PrJobDialog( m_macPrintSettings
) )
487 message
.Printf( wxT("Print Error %d"), err
) ;
488 wxMessageDialog
dialog( NULL
, message
, wxT(""), wxICON_HAND
| wxOK
) ;
496 int wxMacClassicPrintData::ShowPageSetupDialog()
498 int result
= wxID_CANCEL
;
502 err
= ::UMAPrOpen() ;
505 if ( ::PrStlDialog( m_macPrintSettings
) )
513 message
.Printf( wxT("Print Error %d"), err
) ;
514 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
527 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
532 wxMacPrinter::~wxMacPrinter(void)
536 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
539 sm_abortWindow
= NULL
;
544 printout
->SetIsPreview(FALSE
);
545 if (m_printDialogData
.GetMinPage() < 1)
546 m_printDialogData
.SetMinPage(1);
547 if (m_printDialogData
.GetMaxPage() < 1)
548 m_printDialogData
.SetMaxPage(9999);
550 // Create a suitable device context
554 wxPrintDialog
dialog(parent
, & m_printDialogData
);
555 if (dialog
.ShowModal() == wxID_OK
)
557 dc
= dialog
.GetPrintDC();
558 m_printDialogData
= dialog
.GetPrintDialogData();
563 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
567 // May have pressed cancel.
568 if (!dc
|| !dc
->Ok())
574 // on the mac we have always pixels as addressing mode with 72 dpi
576 printout
->SetPPIScreen(72, 72);
577 printout
->SetPPIPrinter(72, 72);
579 // Set printout parameters
585 printout
->SetPageSizePixels((int)w
, (int)h
);
586 dc
->GetSizeMM(&ww
, &hh
);
587 printout
->SetPageSizeMM((int)ww
, (int)hh
);
589 // Create an abort window
592 printout
->OnPreparePrinting();
594 // Get some parameters from the printout, if defined
595 int fromPage
, toPage
;
596 int minPage
, maxPage
;
597 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
605 // Only set min and max, because from and to have been
607 m_printDialogData
.SetMinPage(minPage
);
608 m_printDialogData
.SetMaxPage(maxPage
);
610 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
611 wxSafeYield(win
,true);
616 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
620 sm_abortWindow
= win
;
621 sm_abortWindow
->Show(TRUE
);
622 wxSafeYield(win
,true);
624 printout
->OnBeginPrinting();
626 bool keepGoing
= TRUE
;
629 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
631 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
634 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
641 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
651 // #if !TARGET_CARBON
653 GetPort( &thePort
) ;
654 wxSafeYield(win
,true);
658 keepGoing
= printout
->OnPrintPage(pn
);
662 printout
->OnEndDocument();
665 printout
->OnEndPrinting();
669 sm_abortWindow
->Show(FALSE
);
670 delete sm_abortWindow
;
671 sm_abortWindow
= NULL
;
681 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
683 wxDC
* dc
= (wxDC
*) NULL
;
685 wxPrintDialog
dialog(parent
, & m_printDialogData
);
686 int ret
= dialog
.ShowModal();
690 dc
= dialog
.GetPrintDC();
691 m_printDialogData
= dialog
.GetPrintDialogData();
697 bool wxMacPrinter::Setup(wxWindow
*parent
)
699 wxPrintDialog
dialog(parent
, & m_printDialogData
);
700 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
702 int ret
= dialog
.ShowModal();
706 m_printDialogData
= dialog
.GetPrintDialogData();
709 return (ret
== wxID_OK
);
716 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
717 wxPrintout
*printoutForPrinting
,
718 wxPrintDialogData
*data
)
719 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
724 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
725 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
730 wxMacPrintPreview::~wxMacPrintPreview(void)
734 bool wxMacPrintPreview::Print(bool interactive
)
736 if (!m_printPrintout
)
738 wxMacPrinter
printer(&m_printDialogData
);
739 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
742 void wxMacPrintPreview::DetermineScaling(void)
744 int screenWidth
, screenHeight
;
745 wxDisplaySize( &screenWidth
, &screenHeight
) ;
747 m_previewPrintout
->SetPPIScreen( 72 , 72 ) ;
748 m_previewPrintout
->SetPPIPrinter( 72 , 72 ) ;
749 m_previewPrintout
->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
750 m_previewPrintout
->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
751 m_pageWidth
= 8 * 72 ;
752 m_pageHeight
= 11 * 72 ;
755 // Get a device context for the currently selected printer
756 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
761 printerDC
.GetSizeMM(&ww
, &hh
);
762 printerDC
.GetSize( &x
, &y
) ;
763 m_previewPrintout
->SetPageSizeMM((int)ww
, (int)hh
);
764 m_previewPrintout
->SetPageSizePixels( x
, y
) ;
773 // At 100%, the page should look about page-size on the screen.
774 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
775 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);