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
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() ) ;
559 // May have pressed cancel.
560 if (!dc
|| !dc
->Ok())
566 // on the mac we have always pixels as addressing mode with 72 dpi
568 printout
->SetPPIScreen(72, 72);
569 printout
->SetPPIPrinter(72, 72);
571 // Set printout parameters
577 printout
->SetPageSizePixels((int)w
, (int)h
);
578 dc
->GetSizeMM(&ww
, &hh
);
579 printout
->SetPageSizeMM((int)ww
, (int)hh
);
581 // Create an abort window
584 printout
->OnPreparePrinting();
586 // Get some parameters from the printout, if defined
587 int fromPage
, toPage
;
588 int minPage
, maxPage
;
589 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
597 // Only set min and max, because from and to have been
599 m_printDialogData
.SetMinPage(minPage
);
600 m_printDialogData
.SetMaxPage(maxPage
);
602 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
603 wxSafeYield(win
,true);
608 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
612 sm_abortWindow
= win
;
613 sm_abortWindow
->Show(true);
614 wxSafeYield(win
,true);
616 printout
->OnBeginPrinting();
618 bool keepGoing
= true;
621 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
623 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
626 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
633 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
644 if ( UMAGetSystemVersion() >= 0x1000 )
648 GetPort( &thePort
) ;
649 wxSafeYield(win
,true);
653 keepGoing
= printout
->OnPrintPage(pn
);
657 printout
->OnEndDocument();
660 printout
->OnEndPrinting();
664 sm_abortWindow
->Show(false);
665 delete sm_abortWindow
;
666 sm_abortWindow
= NULL
;
676 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
678 wxDC
* dc
= (wxDC
*) NULL
;
680 wxPrintDialog
dialog(parent
, & m_printDialogData
);
681 int ret
= dialog
.ShowModal();
685 dc
= dialog
.GetPrintDC();
686 m_printDialogData
= dialog
.GetPrintDialogData();
692 bool wxMacPrinter::Setup(wxWindow
*parent
)
695 wxPrintDialog
dialog(parent
, & m_printDialogData
);
696 dialog
.GetPrintDialogData().SetSetupDialog(true);
698 int ret
= dialog
.ShowModal();
702 m_printDialogData
= dialog
.GetPrintDialogData();
705 return (ret
== wxID_OK
);
714 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
715 wxPrintout
*printoutForPrinting
,
716 wxPrintDialogData
*data
)
717 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
722 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
723 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
728 wxMacPrintPreview::~wxMacPrintPreview(void)
732 bool wxMacPrintPreview::Print(bool interactive
)
734 if (!m_printPrintout
)
736 wxMacPrinter
printer(&m_printDialogData
);
737 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
740 void wxMacPrintPreview::DetermineScaling(void)
742 int screenWidth
, screenHeight
;
743 wxDisplaySize( &screenWidth
, &screenHeight
) ;
745 m_previewPrintout
->SetPPIScreen( 72 , 72 ) ;
746 m_previewPrintout
->SetPPIPrinter( 72 , 72 ) ;
747 m_previewPrintout
->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
748 m_previewPrintout
->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
749 m_pageWidth
= 8 * 72 ;
750 m_pageHeight
= 11 * 72 ;
753 // Get a device context for the currently selected printer
754 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
759 printerDC
.GetSizeMM(&ww
, &hh
);
760 printerDC
.GetSize( &x
, &y
) ;
761 m_previewPrintout
->SetPageSizeMM((int)ww
, (int)hh
);
762 m_previewPrintout
->SetPageSizePixels( x
, y
) ;
771 // At 100%, the page should look about page-size on the screen.
772 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
773 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);