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
) ) ;
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 ) ;
264 PMSetLastPage( m_macPrintSettings
, data
->GetToPage() , false ) ;
267 void wxMacCarbonPrintData::CopyFrom( wxNativePrintData
* d
)
269 wxMacCarbonPrintData
*data
= (wxMacCarbonPrintData
*) d
;
270 if ( data
->m_macPrintSession
!= kPMNoReference
)
271 PMRetain( data
->m_macPrintSession
) ;
272 if ( m_macPrintSession
!= kPMNoReference
)
274 PMRelease( m_macPrintSession
) ;
275 m_macPrintSession
= kPMNoReference
;
277 if ( data
->m_macPrintSession
!= kPMNoReference
)
278 m_macPrintSession
= data
->m_macPrintSession
;
280 if ( data
->m_macPrintSettings
!= kPMNoPrintSettings
)
281 PMRetain( data
->m_macPrintSettings
) ;
282 if ( m_macPrintSettings
!= kPMNoPrintSettings
)
284 PMRelease( m_macPrintSettings
) ;
285 m_macPrintSettings
= kPMNoPrintSettings
;
287 if ( data
->m_macPrintSettings
!= kPMNoPrintSettings
)
288 m_macPrintSettings
= data
->m_macPrintSettings
;
290 if ( data
->m_macPageFormat
!= kPMNoPageFormat
)
291 PMRetain( data
->m_macPageFormat
) ;
292 if ( m_macPageFormat
!= kPMNoPageFormat
)
294 PMRelease( m_macPageFormat
) ;
295 m_macPageFormat
= kPMNoPageFormat
;
297 if ( data
->m_macPageFormat
!= kPMNoPageFormat
)
298 m_macPageFormat
= data
->m_macPageFormat
;
301 int wxMacCarbonPrintData::ShowPrintDialog()
303 int result
= wxID_CANCEL
;
310 // Display the Print dialog.
313 err
= PMSessionPrintDialog( m_macPrintSession
,
317 if ((err
== noErr
) && !accepted
)
319 err
= kPMCancel
; // user clicked Cancel button
327 if ((err
!= noErr
) && (err
!= kPMCancel
))
329 message
.Printf( wxT("Print Error %d"), err
) ;
330 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
337 int wxMacCarbonPrintData::ShowPageSetupDialog()
339 int result
= wxID_CANCEL
;
345 // Display the Page Setup dialog.
348 err
= PMSessionPageSetupDialog( m_macPrintSession
,
351 if ((err
== noErr
) && !accepted
)
353 err
= kPMCancel
; // user clicked Cancel button
357 // If the user did not cancel, flatten and save the PageFormat object
358 // with our document.
363 if ((err
!= noErr
) && (err
!= kPMCancel
))
365 message
.Printf( wxT("Print Error %d"), err
) ;
366 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
375 wxNativePrintData
* wxNativePrintData::Create()
377 return new wxMacClassicPrintData() ;
380 wxMacClassicPrintData::wxMacClassicPrintData()
382 m_macPrintSettings
= NULL
;
386 wxMacClassicPrintData::~wxMacClassicPrintData()
388 wxASSERT( m_macPrintSettings
);
389 DisposeHandle( (Handle
) m_macPrintSettings
) ;
392 void wxMacClassicPrintData::ValidateOrCreate()
394 if ( m_macPrintSettings
== NULL
)
396 m_macPrintSettings
= (THPrint
) NewHandleClear( sizeof( TPrint
) );
397 (**m_macPrintSettings
).iPrVersion
= 0; // something invalid
399 (**m_macPrintSettings
).prInfo
.iHRes
= 72;
400 (**m_macPrintSettings
).prInfo
.iVRes
= 72;
401 Rect r1
= { 0, 0, 8*72 - 2 * 18, 11*72 - 2 * 36 };
402 (**m_macPrintSettings
).prInfo
.rPage
= r1
;// must have its top left & (0,0)
404 Rect r2
= { -18, -36, 8*72 - 18, 11*72 - 36 };
405 (**m_macPrintSettings
).rPaper
= r2
;
406 (**m_macPrintSettings
).prStl
.iPageV
= 11 * 120 ; // 11 inches in 120th of an inch
407 (**m_macPrintSettings
).prStl
.iPageH
= 8 * 120 ; // 8 inches in 120th of an inch
414 void wxMacClassicPrintData::TransferFrom( wxPrintData
* data
)
417 (**m_macPrintSettings
).prJob
.iCopies
= data
->GetNoCopies() ;
418 // on mac the paper rect has a negative top left corner, because the page rect (printable area) is at 0,0
419 // if all printing data is consolidated in on structure we will be able to set additional infos about pages
422 void wxMacClassicPrintData::TransferTo( wxPrintData
* data
)
424 data
->SetNoCopies( (**m_macPrintSettings
).prJob
.iCopies
);
425 data
->SetPaperSize( wxSize(
426 ((double) (**m_macPrintSettings
).rPaper
.right
- (**m_macPrintSettings
).rPaper
.left
) * pt2mm
,
427 ((double) (**m_macPrintSettings
).rPaper
.bottom
- (**m_macPrintSettings
).rPaper
.top
) * pt2mm
) ) ;
430 void wxMacClassicPrintData::TransferFrom( wxPageSetupData
*data
)
434 void wxMacClassicPrintData::TransferTo( wxPageSetupData
* data
)
436 data
->SetMinMarginTopLeft( wxPoint(
437 ((double) (**m_macPrintSettings
).prInfo
.rPage
.left
-(**m_macPrintSettings
).rPaper
.left
) * pt2mm
,
438 ((double) (**m_macPrintSettings
).prInfo
.rPage
.top
-(**m_macPrintSettings
).rPaper
.top
) * pt2mm
) ) ;
439 data
->SetMinMarginBottomRight( wxPoint(
440 ((double) (**m_macPrintSettings
).rPaper
.right
- (**m_macPrintSettings
).prInfo
.rPage
.right
) * pt2mm
,
441 ((double)(**m_macPrintSettings
).rPaper
.bottom
- (**m_macPrintSettings
).prInfo
.rPage
.bottom
) * pt2mm
) ) ;
444 void wxMacClassicPrintData::TransferFrom( wxPrintDialogData
* data
)
446 (**m_macPrintSettings
).prJob
.iFstPage
= data
->GetFromPage() ;
447 (**m_macPrintSettings
).prJob
.iLstPage
= data
->GetToPage() ;
450 void wxMacClassicPrintData::TransferTo( wxPrintDialogData
* data
)
452 data
->SetFromPage( (**m_macPrintSettings
).prJob
.iFstPage
) ;
453 data
->SetToPage( (**m_macPrintSettings
).prJob
.iLstPage
) ;
456 void wxMacClassicPrintData::CopyFrom( wxNativePrintData
* data
)
458 DisposeHandle( (Handle
) m_macPrintSettings
) ;
459 m_macPrintSettings
= ((wxMacClassicPrintData
*)data
)->m_macPrintSettings
;
460 HandToHand( (Handle
*) &m_macPrintSettings
);
463 int wxMacClassicPrintData::ShowPrintDialog()
465 int result
= wxID_CANCEL
;
469 err
= ::UMAPrOpen() ;
472 if ( ::PrJobDialog( m_macPrintSettings
) )
480 message
.Printf( wxT("Print Error %d"), err
) ;
481 wxMessageDialog
dialog( NULL
, message
, wxT(""), wxICON_HAND
| wxOK
) ;
489 int wxMacClassicPrintData::ShowPageSetupDialog()
491 int result
= wxID_CANCEL
;
495 err
= ::UMAPrOpen() ;
498 if ( ::PrStlDialog( m_macPrintSettings
) )
506 message
.Printf( wxT("Print Error %d"), err
) ;
507 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
520 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
525 wxMacPrinter::~wxMacPrinter(void)
529 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
532 sm_abortWindow
= NULL
;
537 printout
->SetIsPreview(FALSE
);
538 printout
->OnPreparePrinting();
540 // Get some parameters from the printout, if defined
541 int fromPage
, toPage
;
542 int minPage
, maxPage
;
543 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
548 m_printDialogData
.SetMinPage(minPage
);
549 m_printDialogData
.SetMaxPage(maxPage
);
551 m_printDialogData
.SetFromPage(fromPage
);
553 m_printDialogData
.SetToPage(toPage
);
557 m_printDialogData
.EnablePageNumbers(TRUE
);
558 if (m_printDialogData
.GetFromPage() < m_printDialogData
.GetMinPage())
559 m_printDialogData
.SetFromPage(m_printDialogData
.GetMinPage());
560 else if (m_printDialogData
.GetFromPage() > m_printDialogData
.GetMaxPage())
561 m_printDialogData
.SetFromPage(m_printDialogData
.GetMaxPage());
562 if (m_printDialogData
.GetToPage() > m_printDialogData
.GetMaxPage())
563 m_printDialogData
.SetToPage(m_printDialogData
.GetMaxPage());
564 else if (m_printDialogData
.GetToPage() < m_printDialogData
.GetMinPage())
565 m_printDialogData
.SetToPage(m_printDialogData
.GetMinPage());
568 m_printDialogData
.EnablePageNumbers(FALSE
);
570 // Create a suitable device context
574 wxPrintDialog
dialog(parent
, & m_printDialogData
);
575 if (dialog
.ShowModal() == wxID_OK
)
577 dc
= dialog
.GetPrintDC();
578 m_printDialogData
= dialog
.GetPrintData();
583 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
587 // May have pressed cancel.
588 if (!dc
|| !dc
->Ok())
594 // on the mac we have always pixels as addressing mode with 72 dpi
596 printout
->SetPPIScreen(72, 72);
597 printout
->SetPPIPrinter(72, 72);
599 // Set printout parameters
605 printout
->SetPageSizePixels((int)w
, (int)h
);
606 dc
->GetSizeMM(&ww
, &hh
);
607 printout
->SetPageSizeMM((int)ww
, (int)hh
);
609 // Create an abort window
612 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
613 wxSafeYield(win
,true);
618 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
622 sm_abortWindow
= win
;
623 sm_abortWindow
->Show(TRUE
);
624 wxSafeYield(win
,true);
626 printout
->OnBeginPrinting();
628 bool keepGoing
= TRUE
;
631 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
633 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
636 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
643 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
653 // #if !TARGET_CARBON
655 GetPort( &thePort
) ;
656 wxSafeYield(win
,true);
660 keepGoing
= printout
->OnPrintPage(pn
);
664 printout
->OnEndDocument();
667 printout
->OnEndPrinting();
671 sm_abortWindow
->Show(FALSE
);
672 delete sm_abortWindow
;
673 sm_abortWindow
= NULL
;
683 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
685 wxDC
* dc
= (wxDC
*) NULL
;
687 wxPrintDialog
dialog(parent
, & m_printDialogData
);
688 int ret
= dialog
.ShowModal();
692 dc
= dialog
.GetPrintDC();
693 m_printDialogData
= dialog
.GetPrintDialogData();
699 bool wxMacPrinter::Setup(wxWindow
*parent
)
701 wxPrintDialog
dialog(parent
, & m_printDialogData
);
702 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
704 int ret
= dialog
.ShowModal();
708 m_printDialogData
= dialog
.GetPrintDialogData();
711 return (ret
== wxID_OK
);
718 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
719 wxPrintout
*printoutForPrinting
,
720 wxPrintDialogData
*data
)
721 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
726 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
727 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
732 wxMacPrintPreview::~wxMacPrintPreview(void)
736 bool wxMacPrintPreview::Print(bool interactive
)
738 if (!m_printPrintout
)
740 wxMacPrinter
printer(&m_printDialogData
);
741 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
744 void wxMacPrintPreview::DetermineScaling(void)
746 int screenWidth
, screenHeight
;
747 wxDisplaySize( &screenWidth
, &screenHeight
) ;
749 m_previewPrintout
->SetPPIScreen( 72 , 72 ) ;
750 m_previewPrintout
->SetPPIPrinter( 72 , 72 ) ;
751 m_previewPrintout
->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
752 m_previewPrintout
->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
753 m_pageWidth
= 8 * 72 ;
754 m_pageHeight
= 11 * 72 ;
757 // Get a device context for the currently selected printer
758 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
763 printerDC
.GetSizeMM(&ww
, &hh
);
764 printerDC
.GetSize( &x
, &y
) ;
765 m_previewPrintout
->SetPageSizeMM((int)ww
, (int)hh
);
766 m_previewPrintout
->SetPageSizePixels( x
, y
) ;
775 // At 100%, the page should look about page-size on the screen.
776 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
777 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);