1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/printwin.cpp
3 // Purpose: wxMacPrinter framework
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/msgdlg.h"
24 #include "wx/dcprint.h"
27 #include "wx/mac/uma.h"
29 #include "wx/mac/printmac.h"
30 #include "wx/mac/private/print.h"
32 #define mm2pt 2.83464566929
33 #define pt2mm 0.352777777778
35 #include "wx/printdlg.h"
39 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter
, wxPrinterBase
)
40 IMPLEMENT_CLASS(wxMacPrintPreview
, wxPrintPreviewBase
)
44 wxNativePrintData
* wxNativePrintData::Create()
46 return new wxMacCarbonPrintData() ;
49 wxMacCarbonPrintData::wxMacCarbonPrintData()
51 m_macPageFormat
= kPMNoPageFormat
;
52 m_macPrintSettings
= kPMNoPrintSettings
;
53 m_macPrintSession
= kPMNoReference
;
57 wxMacCarbonPrintData::~wxMacCarbonPrintData()
59 if (m_macPageFormat
!= kPMNoPageFormat
)
61 (void)PMRelease(m_macPageFormat
);
62 m_macPageFormat
= kPMNoPageFormat
;
65 if (m_macPrintSettings
!= kPMNoPrintSettings
)
67 (void)PMRelease(m_macPrintSettings
);
68 m_macPrintSettings
= kPMNoPrintSettings
;
71 if ( m_macPrintSession
!= kPMNoReference
)
73 (void)PMRelease(m_macPrintSession
);
74 m_macPrintSession
= kPMNoReference
;
78 void wxMacCarbonPrintData::ValidateOrCreate()
80 OSStatus err
= noErr
;
81 if ( m_macPrintSession
== kPMNoReference
)
83 err
= PMCreateSession( (PMPrintSession
*) &m_macPrintSession
) ;
85 // Set up a valid PageFormat object.
86 if ( m_macPageFormat
== kPMNoPageFormat
)
88 err
= PMCreatePageFormat((PMPageFormat
*) &m_macPageFormat
);
90 // Note that PMPageFormat is not session-specific, but calling
91 // PMSessionDefaultPageFormat assigns values specific to the printer
92 // associated with the current printing session.
94 ( m_macPageFormat
!= kPMNoPageFormat
))
96 err
= PMSessionDefaultPageFormat((PMPrintSession
) m_macPrintSession
,
97 (PMPageFormat
) m_macPageFormat
);
102 err
= PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
103 (PMPageFormat
) m_macPageFormat
,
107 // Set up a valid PrintSettings object.
108 if ( m_macPrintSettings
== kPMNoPrintSettings
)
110 err
= PMCreatePrintSettings((PMPrintSettings
*) &m_macPrintSettings
);
112 // Note that PMPrintSettings is not session-specific, but calling
113 // PMSessionDefaultPrintSettings assigns values specific to the printer
114 // associated with the current printing session.
115 if ((err
== noErr
) &&
116 ( m_macPrintSettings
!= kPMNoPrintSettings
))
118 err
= PMSessionDefaultPrintSettings((PMPrintSession
) m_macPrintSession
,
119 (PMPrintSettings
) m_macPrintSettings
);
124 err
= PMSessionValidatePrintSettings((PMPrintSession
) m_macPrintSession
,
125 (PMPrintSettings
) m_macPrintSettings
,
130 void wxMacCarbonPrintData::TransferFrom( wxPrintData
* data
)
133 PMSetCopies( (PMPrintSettings
) m_macPrintSettings
, data
->GetNoCopies() , false ) ;
134 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
->GetOrientation() == wxLANDSCAPE
) ?
135 kPMLandscape
: kPMPortrait
, false ) ;
136 // collate cannot be set
137 #if 0 // not yet tested
138 if ( !m_printerName
.empty() )
139 PMSessionSetCurrentPrinter( (PMPrintSession
) m_macPrintSession
, wxMacCFStringHolder( m_printerName
, wxFont::GetDefaultEncoding() ) ) ;
142 PMGetColorMode( (PMPrintSettings
) m_macPrintSettings
, &color
) ;
143 if ( data
->GetColour() )
145 if ( color
== kPMBlackAndWhite
)
146 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMColor
) ;
149 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMBlackAndWhite
) ;
151 // PMDuplexMode not yet accessible via API
152 // PMQualityMode not yet accessible via API
156 void wxMacCarbonPrintData::TransferTo( wxPrintData
* data
)
158 OSStatus err
= noErr
;
161 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
163 data
->SetNoCopies( copies
) ;
165 PMOrientation orientation
;
166 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
169 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
170 data
->SetOrientation( wxPORTRAIT
);
172 data
->SetOrientation( wxLANDSCAPE
);
175 // collate cannot be set
178 wxMacCFStringHolder name
;
180 PMSessionGetCurrentPrinter( m_macPrintSession
,
182 m_printerName
= name
.AsString() ;
187 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
189 data
->SetColour( !(color
== kPMBlackAndWhite
) ) ;
191 // PMDuplexMode not yet accessible via API
192 // PMQualityMode not yet accessible via API
195 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
198 data
->SetPaperSize( wxSize (
199 (int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
200 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) ) );
204 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*data
)
206 // should we setup the page rect here ?
207 // since MacOS sometimes has two same paper rects with different
208 // page rects we could make it roundtrip safe perhaps
214 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
217 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
221 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
224 data
->SetMinMarginTopLeft( wxPoint (
225 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
226 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
228 data
->SetMinMarginBottomRight( wxPoint (
229 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
230 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
235 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
237 UInt32 minPage
, maxPage
;
238 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
239 data
->SetMinPage( minPage
) ;
240 data
->SetMaxPage( maxPage
) ;
242 PMGetCopies( m_macPrintSettings
, &copies
) ;
243 data
->SetNoCopies( copies
) ;
245 PMGetFirstPage( m_macPrintSettings
, &from
) ;
246 PMGetLastPage( m_macPrintSettings
, &to
) ;
247 data
->SetFromPage( from
) ;
248 data
->SetToPage( to
) ;
251 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
253 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
254 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
255 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
257 int toPage
= data
->GetToPage();
259 toPage
= data
->GetFromPage();
260 PMSetLastPage( m_macPrintSettings
, toPage
, false ) ;
263 void wxMacCarbonPrintData::CopyFrom( wxNativePrintData
* d
)
265 wxMacCarbonPrintData
*data
= (wxMacCarbonPrintData
*) d
;
266 if ( data
->m_macPrintSession
!= kPMNoReference
)
267 PMRetain( data
->m_macPrintSession
) ;
268 if ( m_macPrintSession
!= kPMNoReference
)
270 PMRelease( m_macPrintSession
) ;
271 m_macPrintSession
= kPMNoReference
;
273 if ( data
->m_macPrintSession
!= kPMNoReference
)
274 m_macPrintSession
= data
->m_macPrintSession
;
276 if ( data
->m_macPrintSettings
!= kPMNoPrintSettings
)
277 PMRetain( data
->m_macPrintSettings
) ;
278 if ( m_macPrintSettings
!= kPMNoPrintSettings
)
280 PMRelease( m_macPrintSettings
) ;
281 m_macPrintSettings
= kPMNoPrintSettings
;
283 if ( data
->m_macPrintSettings
!= kPMNoPrintSettings
)
284 m_macPrintSettings
= data
->m_macPrintSettings
;
286 if ( data
->m_macPageFormat
!= kPMNoPageFormat
)
287 PMRetain( data
->m_macPageFormat
) ;
288 if ( m_macPageFormat
!= kPMNoPageFormat
)
290 PMRelease( m_macPageFormat
) ;
291 m_macPageFormat
= kPMNoPageFormat
;
293 if ( data
->m_macPageFormat
!= kPMNoPageFormat
)
294 m_macPageFormat
= data
->m_macPageFormat
;
297 int wxMacCarbonPrintData::ShowPrintDialog()
299 int result
= wxID_CANCEL
;
306 // Display the Print dialog.
309 err
= PMSessionPrintDialog( m_macPrintSession
,
313 if ((err
== noErr
) && !accepted
)
315 err
= kPMCancel
; // user clicked Cancel button
323 if ((err
!= noErr
) && (err
!= kPMCancel
))
325 message
.Printf( wxT("Print Error %d"), err
) ;
326 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
333 int wxMacCarbonPrintData::ShowPageSetupDialog()
335 int result
= wxID_CANCEL
;
341 // Display the Page Setup dialog.
344 err
= PMSessionPageSetupDialog( m_macPrintSession
,
347 if ((err
== noErr
) && !accepted
)
349 err
= kPMCancel
; // user clicked Cancel button
353 // If the user did not cancel, flatten and save the PageFormat object
354 // with our document.
359 if ((err
!= noErr
) && (err
!= kPMCancel
))
361 message
.Printf( wxT("Print Error %d"), err
) ;
362 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
371 wxNativePrintData
* wxNativePrintData::Create()
373 return new wxMacClassicPrintData() ;
376 wxMacClassicPrintData::wxMacClassicPrintData()
378 m_macPrintSettings
= NULL
;
382 wxMacClassicPrintData::~wxMacClassicPrintData()
384 wxASSERT( m_macPrintSettings
);
385 DisposeHandle( (Handle
) m_macPrintSettings
) ;
388 void wxMacClassicPrintData::ValidateOrCreate()
390 if ( m_macPrintSettings
== NULL
)
392 m_macPrintSettings
= (THPrint
) NewHandleClear( sizeof( TPrint
) );
393 (**m_macPrintSettings
).iPrVersion
= 0; // something invalid
395 (**m_macPrintSettings
).prInfo
.iHRes
= 72;
396 (**m_macPrintSettings
).prInfo
.iVRes
= 72;
397 Rect r1
= { 0, 0, 8*72 - 2 * 18, 11*72 - 2 * 36 };
398 (**m_macPrintSettings
).prInfo
.rPage
= r1
;// must have its top left & (0,0)
400 Rect r2
= { -18, -36, 8*72 - 18, 11*72 - 36 };
401 (**m_macPrintSettings
).rPaper
= r2
;
402 (**m_macPrintSettings
).prStl
.iPageV
= 11 * 120 ; // 11 inches in 120th of an inch
403 (**m_macPrintSettings
).prStl
.iPageH
= 8 * 120 ; // 8 inches in 120th of an inch
410 void wxMacClassicPrintData::TransferFrom( wxPrintData
* data
)
413 (**m_macPrintSettings
).prJob
.iCopies
= data
->GetNoCopies() ;
414 // on mac the paper rect has a negative top left corner, because the page rect (printable area) is at 0,0
415 // if all printing data is consolidated in on structure we will be able to set additional infos about pages
418 void wxMacClassicPrintData::TransferTo( wxPrintData
* data
)
420 data
->SetNoCopies( (**m_macPrintSettings
).prJob
.iCopies
);
421 data
->SetPaperSize( wxSize(
422 ((double) (**m_macPrintSettings
).rPaper
.right
- (**m_macPrintSettings
).rPaper
.left
) * pt2mm
,
423 ((double) (**m_macPrintSettings
).rPaper
.bottom
- (**m_macPrintSettings
).rPaper
.top
) * pt2mm
) ) ;
426 void wxMacClassicPrintData::TransferFrom( wxPageSetupData
*data
)
430 void wxMacClassicPrintData::TransferTo( wxPageSetupData
* data
)
432 data
->SetMinMarginTopLeft( wxPoint(
433 ((double) (**m_macPrintSettings
).prInfo
.rPage
.left
-(**m_macPrintSettings
).rPaper
.left
) * pt2mm
,
434 ((double) (**m_macPrintSettings
).prInfo
.rPage
.top
-(**m_macPrintSettings
).rPaper
.top
) * pt2mm
) ) ;
435 data
->SetMinMarginBottomRight( wxPoint(
436 ((double) (**m_macPrintSettings
).rPaper
.right
- (**m_macPrintSettings
).prInfo
.rPage
.right
) * pt2mm
,
437 ((double)(**m_macPrintSettings
).rPaper
.bottom
- (**m_macPrintSettings
).prInfo
.rPage
.bottom
) * pt2mm
) ) ;
440 void wxMacClassicPrintData::TransferFrom( wxPrintDialogData
* data
)
442 int toPage
= data
->GetToPage();
444 toPage
= data
->GetFromPage();
445 (**m_macPrintSettings
).prJob
.iFstPage
= data
->GetFromPage() ;
446 (**m_macPrintSettings
).prJob
.iLstPage
= toPage
;
449 void wxMacClassicPrintData::TransferTo( wxPrintDialogData
* data
)
451 data
->SetFromPage( (**m_macPrintSettings
).prJob
.iFstPage
) ;
452 data
->SetToPage( (**m_macPrintSettings
).prJob
.iLstPage
) ;
455 void wxMacClassicPrintData::CopyFrom( wxNativePrintData
* data
)
457 DisposeHandle( (Handle
) m_macPrintSettings
) ;
458 m_macPrintSettings
= ((wxMacClassicPrintData
*)data
)->m_macPrintSettings
;
459 HandToHand( (Handle
*) &m_macPrintSettings
);
462 int wxMacClassicPrintData::ShowPrintDialog()
464 int result
= wxID_CANCEL
;
468 err
= ::UMAPrOpen() ;
471 if ( ::PrJobDialog( m_macPrintSettings
) )
479 message
.Printf( wxT("Print Error %d"), err
) ;
480 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
488 int wxMacClassicPrintData::ShowPageSetupDialog()
490 int result
= wxID_CANCEL
;
494 err
= ::UMAPrOpen() ;
497 if ( ::PrStlDialog( m_macPrintSettings
) )
505 message
.Printf( wxT("Print Error %d"), err
) ;
506 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
519 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
524 wxMacPrinter::~wxMacPrinter(void)
528 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
531 sm_abortWindow
= NULL
;
536 printout
->SetIsPreview(false);
537 if (m_printDialogData
.GetMinPage() < 1)
538 m_printDialogData
.SetMinPage(1);
539 if (m_printDialogData
.GetMaxPage() < 1)
540 m_printDialogData
.SetMaxPage(9999);
542 // Create a suitable device context
543 wxPrinterDC
*dc
= NULL
;
546 wxPrintDialog
dialog(parent
, & m_printDialogData
);
547 if (dialog
.ShowModal() == wxID_OK
)
549 dc
= dialog
.GetPrintDC();
550 m_printDialogData
= dialog
.GetPrintDialogData();
555 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
558 // May have pressed cancel.
559 if (!dc
|| !dc
->Ok())
565 // on the mac we have always pixels as addressing mode with 72 dpi
566 printout
->SetPPIScreen(72, 72);
567 printout
->SetPPIPrinter(72, 72);
569 // Set printout parameters
575 printout
->SetPageSizePixels((int)w
, (int)h
);
576 printout
->SetPaperRectPixels(dc
->GetPaperRect());
577 dc
->GetSizeMM(&ww
, &hh
);
578 printout
->SetPageSizeMM((int)ww
, (int)hh
);
580 // Create an abort window
583 printout
->OnPreparePrinting();
585 // Get some parameters from the printout, if defined
586 int fromPage
, toPage
;
587 int minPage
, maxPage
;
588 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
596 // Only set min and max, because from and to have been
598 m_printDialogData
.SetMinPage(minPage
);
599 m_printDialogData
.SetMaxPage(maxPage
);
601 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
602 wxSafeYield(win
,true);
607 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
611 sm_abortWindow
= win
;
612 sm_abortWindow
->Show(true);
613 wxSafeYield(win
,true);
615 printout
->OnBeginPrinting();
617 bool keepGoing
= true;
620 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
622 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
625 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
632 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
643 if ( UMAGetSystemVersion() >= 0x1000 )
647 GetPort( &thePort
) ;
648 wxSafeYield(win
,true);
652 keepGoing
= printout
->OnPrintPage(pn
);
656 printout
->OnEndDocument();
659 printout
->OnEndPrinting();
663 sm_abortWindow
->Show(false);
664 delete sm_abortWindow
;
665 sm_abortWindow
= NULL
;
675 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
677 wxDC
* dc
= (wxDC
*) NULL
;
679 wxPrintDialog
dialog(parent
, & m_printDialogData
);
680 int ret
= dialog
.ShowModal();
684 dc
= dialog
.GetPrintDC();
685 m_printDialogData
= dialog
.GetPrintDialogData();
691 bool wxMacPrinter::Setup(wxWindow
*parent
)
694 wxPrintDialog
dialog(parent
, & m_printDialogData
);
695 dialog
.GetPrintDialogData().SetSetupDialog(true);
697 int ret
= dialog
.ShowModal();
701 m_printDialogData
= dialog
.GetPrintDialogData();
704 return (ret
== wxID_OK
);
713 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
714 wxPrintout
*printoutForPrinting
,
715 wxPrintDialogData
*data
)
716 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
721 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
722 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
727 wxMacPrintPreview::~wxMacPrintPreview(void)
731 bool wxMacPrintPreview::Print(bool interactive
)
733 if (!m_printPrintout
)
735 wxMacPrinter
printer(&m_printDialogData
);
736 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
739 void wxMacPrintPreview::DetermineScaling(void)
741 int screenWidth
, screenHeight
;
742 wxDisplaySize( &screenWidth
, &screenHeight
) ;
744 wxSize
ppiScreen( 72 , 72 ) ;
745 wxSize
ppiPrinter( 72 , 72 ) ;
747 m_previewPrintout
->SetPPIScreen( ppiScreen
.x
, ppiScreen
.y
) ;
753 // Get a device context for the currently selected printer
754 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
757 printerDC
.GetSizeMM(&ww
, &hh
);
758 printerDC
.GetSize( &w
, &h
) ;
759 ppiPrinter
= printerDC
.GetPPI() ;
760 paperRect
= printerDC
.GetPaperRect();
768 ww
= (wxCoord
) (w
* 25.4 / ppiPrinter
.x
) ;
769 hh
= (wxCoord
) (h
* 25.4 / ppiPrinter
.y
) ;
770 paperRect
= wxRect(0, 0, w
, h
);
773 m_previewPrintout
->SetPageSizeMM(ww
, hh
);
774 m_previewPrintout
->SetPageSizePixels(w
, h
) ;
775 m_previewPrintout
->SetPaperRectPixels(paperRect
);
778 m_previewPrintout
->SetPPIPrinter( ppiPrinter
.x
, ppiPrinter
.y
) ;
780 m_previewScaleX
= float(ppiScreen
.x
) / ppiPrinter
.x
;
781 m_previewScaleY
= float(ppiScreen
.y
) / ppiPrinter
.y
;