1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMacPrinter framework
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "printwin.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/msgdlg.h"
31 #include "wx/mac/uma.h"
33 #include "wx/mac/printmac.h"
34 #include "wx/mac/private/print.h"
36 #include "wx/dcprint.h"
37 #include "wx/printdlg.h"
38 #include "wx/mac/printdlg.h"
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter
, wxPrinterBase
)
44 IMPLEMENT_CLASS(wxMacPrintPreview
, wxPrintPreviewBase
)
49 wxNativePrintData
* wxNativePrintData::Create()
51 return new wxMacCarbonPrintData() ;
54 wxMacCarbonPrintData::wxMacCarbonPrintData()
56 m_macPageFormat
= kPMNoPageFormat
;
57 m_macPrintSettings
= kPMNoPrintSettings
;
58 m_macPrintSession
= kPMNoReference
;
62 wxMacCarbonPrintData::~wxMacCarbonPrintData()
64 if (m_macPageFormat
!= kPMNoPageFormat
)
66 (void)PMRelease(m_macPageFormat
);
67 m_macPageFormat
= kPMNoPageFormat
;
70 if (m_macPrintSettings
!= kPMNoPrintSettings
)
72 (void)PMRelease(m_macPrintSettings
);
73 m_macPrintSettings
= kPMNoPrintSettings
;
76 if ( m_macPrintSession
!= kPMNoReference
)
78 (void)PMRelease(m_macPrintSession
);
79 m_macPrintSession
= kPMNoReference
;
83 void wxMacCarbonPrintData::ValidateOrCreate()
85 OSStatus err
= noErr
;
86 if ( m_macPrintSession
== kPMNoReference
)
88 err
= PMCreateSession( (PMPrintSession
*) &m_macPrintSession
) ;
90 // Set up a valid PageFormat object.
91 if ( m_macPageFormat
== kPMNoPageFormat
)
93 err
= PMCreatePageFormat((PMPageFormat
*) &m_macPageFormat
);
95 // Note that PMPageFormat is not session-specific, but calling
96 // PMSessionDefaultPageFormat assigns values specific to the printer
97 // associated with the current printing session.
99 ( m_macPageFormat
!= kPMNoPageFormat
))
101 err
= PMSessionDefaultPageFormat((PMPrintSession
) m_macPrintSession
,
102 (PMPageFormat
) m_macPageFormat
);
107 err
= PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
108 (PMPageFormat
) m_macPageFormat
,
112 // Set up a valid PrintSettings object.
113 if ( m_macPrintSettings
== kPMNoPrintSettings
)
115 err
= PMCreatePrintSettings((PMPrintSettings
*) &m_macPrintSettings
);
117 // Note that PMPrintSettings is not session-specific, but calling
118 // PMSessionDefaultPrintSettings assigns values specific to the printer
119 // associated with the current printing session.
120 if ((err
== noErr
) &&
121 ( m_macPrintSettings
!= kPMNoPrintSettings
))
123 err
= PMSessionDefaultPrintSettings((PMPrintSession
) m_macPrintSession
,
124 (PMPrintSettings
) m_macPrintSettings
);
129 err
= PMSessionValidatePrintSettings((PMPrintSession
) m_macPrintSession
,
130 (PMPrintSettings
) m_macPrintSettings
,
135 void wxMacCarbonPrintData::TransferFrom( wxPrintData
* data
)
138 PMSetCopies( (PMPrintSettings
) m_macPrintSettings
, data
->GetNoCopies() , false ) ;
139 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
->GetOrientation() == wxLANDSCAPE
) ?
140 kPMLandscape
: kPMPortrait
, false ) ;
141 // collate cannot be set
142 #if 0 // not yet tested
143 if ( m_printerName
.Length() > 0 )
144 PMSessionSetCurrentPrinter( (PMPrintSession
) m_macPrintSession
, wxMacCFStringHolder( m_printerName
, wxFont::GetDefaultEncoding() ) ) ;
147 PMGetColorMode( (PMPrintSettings
) m_macPrintSettings
, &color
) ;
148 if ( data
->GetColour() )
150 if ( color
== kPMBlackAndWhite
)
151 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMColor
) ;
154 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMBlackAndWhite
) ;
156 // PMDuplexMode not yet accessible via API
157 // PMQualityMode not yet accessible via API
161 void wxMacCarbonPrintData::TransferTo( wxPrintData
* data
)
163 OSStatus err
= noErr
;
166 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
168 data
->SetNoCopies( copies
) ;
170 PMOrientation orientation
;
171 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
174 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
175 data
->SetOrientation( wxPORTRAIT
);
177 data
->SetOrientation( wxLANDSCAPE
);
180 // collate cannot be set
183 wxMacCFStringHolder name
;
185 PMSessionGetCurrentPrinter( m_macPrintSession
,
187 m_printerName
= name
.AsString() ;
192 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
194 data
->SetColour( !(color
== kPMBlackAndWhite
) ) ;
196 // PMDuplexMode not yet accessible via API
197 // PMQualityMode not yet accessible via API
200 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
203 data
->SetPaperSize( wxSize (
204 (int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
205 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) ) );
209 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*data
)
211 // should we setup the page rect here ?
212 // since MacOS sometimes has two same paper rects with different
213 // page rects we could make it roundtrip safe perhaps
219 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
222 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
226 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
229 data
->SetMinMarginTopLeft( wxPoint (
230 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
231 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
233 data
->SetMinMarginBottomRight( wxPoint (
234 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
235 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
240 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
242 UInt32 minPage
, maxPage
;
243 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
244 data
->SetMinPage( minPage
) ;
245 data
->SetMaxPage( maxPage
) ;
247 PMGetCopies( m_macPrintSettings
, &copies
) ;
248 data
->SetNoCopies( copies
) ;
250 PMGetFirstPage( m_macPrintSettings
, &from
) ;
251 PMGetLastPage( m_macPrintSettings
, &to
) ;
252 data
->SetFromPage( from
) ;
253 data
->SetToPage( to
) ;
256 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
258 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
259 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
260 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
262 int toPage
= data
->GetToPage();
264 toPage
= data
->GetFromPage();
265 PMSetLastPage( m_macPrintSettings
, toPage
, false ) ;
268 void wxMacCarbonPrintData::CopyFrom( wxNativePrintData
* d
)
270 wxMacCarbonPrintData
*data
= (wxMacCarbonPrintData
*) d
;
271 if ( data
->m_macPrintSession
!= kPMNoReference
)
272 PMRetain( data
->m_macPrintSession
) ;
273 if ( m_macPrintSession
!= kPMNoReference
)
275 PMRelease( m_macPrintSession
) ;
276 m_macPrintSession
= kPMNoReference
;
278 if ( data
->m_macPrintSession
!= kPMNoReference
)
279 m_macPrintSession
= data
->m_macPrintSession
;
281 if ( data
->m_macPrintSettings
!= kPMNoPrintSettings
)
282 PMRetain( data
->m_macPrintSettings
) ;
283 if ( m_macPrintSettings
!= kPMNoPrintSettings
)
285 PMRelease( m_macPrintSettings
) ;
286 m_macPrintSettings
= kPMNoPrintSettings
;
288 if ( data
->m_macPrintSettings
!= kPMNoPrintSettings
)
289 m_macPrintSettings
= data
->m_macPrintSettings
;
291 if ( data
->m_macPageFormat
!= kPMNoPageFormat
)
292 PMRetain( data
->m_macPageFormat
) ;
293 if ( m_macPageFormat
!= kPMNoPageFormat
)
295 PMRelease( m_macPageFormat
) ;
296 m_macPageFormat
= kPMNoPageFormat
;
298 if ( data
->m_macPageFormat
!= kPMNoPageFormat
)
299 m_macPageFormat
= data
->m_macPageFormat
;
302 int wxMacCarbonPrintData::ShowPrintDialog()
304 int result
= wxID_CANCEL
;
311 // Display the Print dialog.
314 err
= PMSessionPrintDialog( m_macPrintSession
,
318 if ((err
== noErr
) && !accepted
)
320 err
= kPMCancel
; // user clicked Cancel button
328 if ((err
!= noErr
) && (err
!= kPMCancel
))
330 message
.Printf( wxT("Print Error %d"), err
) ;
331 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
338 int wxMacCarbonPrintData::ShowPageSetupDialog()
340 int result
= wxID_CANCEL
;
346 // Display the Page Setup dialog.
349 err
= PMSessionPageSetupDialog( m_macPrintSession
,
352 if ((err
== noErr
) && !accepted
)
354 err
= kPMCancel
; // user clicked Cancel button
358 // If the user did not cancel, flatten and save the PageFormat object
359 // with our document.
364 if ((err
!= noErr
) && (err
!= kPMCancel
))
366 message
.Printf( wxT("Print Error %d"), err
) ;
367 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
376 wxNativePrintData
* wxNativePrintData::Create()
378 return new wxMacClassicPrintData() ;
381 wxMacClassicPrintData::wxMacClassicPrintData()
383 m_macPrintSettings
= NULL
;
387 wxMacClassicPrintData::~wxMacClassicPrintData()
389 wxASSERT( m_macPrintSettings
);
390 DisposeHandle( (Handle
) m_macPrintSettings
) ;
393 void wxMacClassicPrintData::ValidateOrCreate()
395 if ( m_macPrintSettings
== NULL
)
397 m_macPrintSettings
= (THPrint
) NewHandleClear( sizeof( TPrint
) );
398 (**m_macPrintSettings
).iPrVersion
= 0; // something invalid
400 (**m_macPrintSettings
).prInfo
.iHRes
= 72;
401 (**m_macPrintSettings
).prInfo
.iVRes
= 72;
402 Rect r1
= { 0, 0, 8*72 - 2 * 18, 11*72 - 2 * 36 };
403 (**m_macPrintSettings
).prInfo
.rPage
= r1
;// must have its top left & (0,0)
405 Rect r2
= { -18, -36, 8*72 - 18, 11*72 - 36 };
406 (**m_macPrintSettings
).rPaper
= r2
;
407 (**m_macPrintSettings
).prStl
.iPageV
= 11 * 120 ; // 11 inches in 120th of an inch
408 (**m_macPrintSettings
).prStl
.iPageH
= 8 * 120 ; // 8 inches in 120th of an inch
415 void wxMacClassicPrintData::TransferFrom( wxPrintData
* data
)
418 (**m_macPrintSettings
).prJob
.iCopies
= data
->GetNoCopies() ;
419 // on mac the paper rect has a negative top left corner, because the page rect (printable area) is at 0,0
420 // if all printing data is consolidated in on structure we will be able to set additional infos about pages
423 void wxMacClassicPrintData::TransferTo( wxPrintData
* data
)
425 data
->SetNoCopies( (**m_macPrintSettings
).prJob
.iCopies
);
426 data
->SetPaperSize( wxSize(
427 ((double) (**m_macPrintSettings
).rPaper
.right
- (**m_macPrintSettings
).rPaper
.left
) * pt2mm
,
428 ((double) (**m_macPrintSettings
).rPaper
.bottom
- (**m_macPrintSettings
).rPaper
.top
) * pt2mm
) ) ;
431 void wxMacClassicPrintData::TransferFrom( wxPageSetupData
*data
)
435 void wxMacClassicPrintData::TransferTo( wxPageSetupData
* data
)
437 data
->SetMinMarginTopLeft( wxPoint(
438 ((double) (**m_macPrintSettings
).prInfo
.rPage
.left
-(**m_macPrintSettings
).rPaper
.left
) * pt2mm
,
439 ((double) (**m_macPrintSettings
).prInfo
.rPage
.top
-(**m_macPrintSettings
).rPaper
.top
) * pt2mm
) ) ;
440 data
->SetMinMarginBottomRight( wxPoint(
441 ((double) (**m_macPrintSettings
).rPaper
.right
- (**m_macPrintSettings
).prInfo
.rPage
.right
) * pt2mm
,
442 ((double)(**m_macPrintSettings
).rPaper
.bottom
- (**m_macPrintSettings
).prInfo
.rPage
.bottom
) * pt2mm
) ) ;
445 void wxMacClassicPrintData::TransferFrom( wxPrintDialogData
* data
)
447 int toPage
= data
->GetToPage();
449 toPage
= data
->GetFromPage();
450 (**m_macPrintSettings
).prJob
.iFstPage
= data
->GetFromPage() ;
451 (**m_macPrintSettings
).prJob
.iLstPage
= toPage
;
454 void wxMacClassicPrintData::TransferTo( wxPrintDialogData
* data
)
456 data
->SetFromPage( (**m_macPrintSettings
).prJob
.iFstPage
) ;
457 data
->SetToPage( (**m_macPrintSettings
).prJob
.iLstPage
) ;
460 void wxMacClassicPrintData::CopyFrom( wxNativePrintData
* data
)
462 DisposeHandle( (Handle
) m_macPrintSettings
) ;
463 m_macPrintSettings
= ((wxMacClassicPrintData
*)data
)->m_macPrintSettings
;
464 HandToHand( (Handle
*) &m_macPrintSettings
);
467 int wxMacClassicPrintData::ShowPrintDialog()
469 int result
= wxID_CANCEL
;
473 err
= ::UMAPrOpen() ;
476 if ( ::PrJobDialog( m_macPrintSettings
) )
484 message
.Printf( wxT("Print Error %d"), err
) ;
485 wxMessageDialog
dialog( NULL
, message
, wxT(""), wxICON_HAND
| wxOK
) ;
493 int wxMacClassicPrintData::ShowPageSetupDialog()
495 int result
= wxID_CANCEL
;
499 err
= ::UMAPrOpen() ;
502 if ( ::PrStlDialog( m_macPrintSettings
) )
510 message
.Printf( wxT("Print Error %d"), err
) ;
511 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
524 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
529 wxMacPrinter::~wxMacPrinter(void)
533 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
536 sm_abortWindow
= NULL
;
541 printout
->SetIsPreview(FALSE
);
542 if (m_printDialogData
.GetMinPage() < 1)
543 m_printDialogData
.SetMinPage(1);
544 if (m_printDialogData
.GetMaxPage() < 1)
545 m_printDialogData
.SetMaxPage(9999);
547 // Create a suitable device context
551 wxPrintDialog
dialog(parent
, & m_printDialogData
);
552 if (dialog
.ShowModal() == wxID_OK
)
554 dc
= dialog
.GetPrintDC();
555 m_printDialogData
= dialog
.GetPrintDialogData();
560 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
564 // May have pressed cancel.
565 if (!dc
|| !dc
->Ok())
571 // on the mac we have always pixels as addressing mode with 72 dpi
573 printout
->SetPPIScreen(72, 72);
574 printout
->SetPPIPrinter(72, 72);
576 // Set printout parameters
582 printout
->SetPageSizePixels((int)w
, (int)h
);
583 dc
->GetSizeMM(&ww
, &hh
);
584 printout
->SetPageSizeMM((int)ww
, (int)hh
);
586 // Create an abort window
589 printout
->OnPreparePrinting();
591 // Get some parameters from the printout, if defined
592 int fromPage
, toPage
;
593 int minPage
, maxPage
;
594 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
602 // Only set min and max, because from and to have been
604 m_printDialogData
.SetMinPage(minPage
);
605 m_printDialogData
.SetMaxPage(maxPage
);
607 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
608 wxSafeYield(win
,true);
613 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
617 sm_abortWindow
= win
;
618 sm_abortWindow
->Show(TRUE
);
619 wxSafeYield(win
,true);
621 printout
->OnBeginPrinting();
623 bool keepGoing
= TRUE
;
626 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
628 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
631 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
638 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
649 if ( UMAGetSystemVersion() >= 0x1000 )
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
)
700 wxPrintDialog
dialog(parent
, & m_printDialogData
);
701 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
703 int ret
= dialog
.ShowModal();
707 m_printDialogData
= dialog
.GetPrintDialogData();
710 return (ret
== wxID_OK
);
719 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
720 wxPrintout
*printoutForPrinting
,
721 wxPrintDialogData
*data
)
722 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
727 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
728 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
733 wxMacPrintPreview::~wxMacPrintPreview(void)
737 bool wxMacPrintPreview::Print(bool interactive
)
739 if (!m_printPrintout
)
741 wxMacPrinter
printer(&m_printDialogData
);
742 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
745 void wxMacPrintPreview::DetermineScaling(void)
747 int screenWidth
, screenHeight
;
748 wxDisplaySize( &screenWidth
, &screenHeight
) ;
750 m_previewPrintout
->SetPPIScreen( 72 , 72 ) ;
751 m_previewPrintout
->SetPPIPrinter( 72 , 72 ) ;
752 m_previewPrintout
->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
753 m_previewPrintout
->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
754 m_pageWidth
= 8 * 72 ;
755 m_pageHeight
= 11 * 72 ;
758 // Get a device context for the currently selected printer
759 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
764 printerDC
.GetSizeMM(&ww
, &hh
);
765 printerDC
.GetSize( &x
, &y
) ;
766 m_previewPrintout
->SetPageSizeMM((int)ww
, (int)hh
);
767 m_previewPrintout
->SetPageSizePixels( x
, y
) ;
776 // At 100%, the page should look about page-size on the screen.
777 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
778 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);