1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/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"
15 #if wxUSE_PRINTING_ARCHITECTURE
25 #include "wx/msgdlg.h"
26 #include "wx/dcprint.h"
30 #include "wx/mac/uma.h"
32 #include "wx/mac/printmac.h"
33 #include "wx/mac/private/print.h"
35 #include "wx/printdlg.h"
37 #include "wx/mac/printdlg.h"
41 IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData
, wxPrintNativeDataBase
)
42 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter
, wxPrinterBase
)
43 IMPLEMENT_CLASS(wxMacPrintPreview
, wxPrintPreviewBase
)
45 bool wxMacCarbonPrintData::IsOk() const
47 return (m_macPageFormat
!= kPMNoPageFormat
) && (m_macPrintSettings
!= kPMNoPrintSettings
) && (m_macPrintSession
!= kPMNoReference
);
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 bool wxMacCarbonPrintData::TransferFrom( const wxPrintData
&data
)
133 PMSetCopies( (PMPrintSettings
) m_macPrintSettings
, data
.GetNoCopies() , false ) ;
134 if ( data
.IsOrientationReversed() )
135 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
136 kPMReverseLandscape
: kPMReversePortrait
, false ) ;
138 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
139 kPMLandscape
: kPMPortrait
, false ) ;
140 // collate cannot be set
141 #if 0 // not yet tested
142 if ( !m_printerName
.empty() )
143 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
) ;
157 PMDuplexMode mode
= 0 ;
158 switch( data
.GetDuplex() )
160 case wxDUPLEX_HORIZONTAL
:
161 mode
= kPMDuplexNoTumble
;
163 case wxDUPLEX_VERTICAL
:
164 mode
= kPMDuplexTumble
;
166 case wxDUPLEX_SIMPLEX
:
168 mode
= kPMDuplexNone
;
171 PMSetDuplex( (PMPrintSettings
) m_macPrintSettings
, mode
) ;
173 // PMQualityMode not yet accessible via API
178 PMSessionGetCurrentPrinter(m_macPrintSession
, &printer
);
179 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
180 PMPrinterGetOutputResolution( printer
,
181 (PMPrintSettings
) m_macPrintSettings
, &res
) ;
182 // TODO transfer ? into page format ?
184 PMTag tag
= kPMMaxSquareResolution
;
185 PMPrinterGetPrinterResolution(printer
, tag
, &res
);
186 PMSetResolution((PMPageFormat
) m_macPageFormat
, &res
);
188 // after setting the new resolution the format has to be updated, otherwise the page rect remains
189 // at the 'old' scaling
190 PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
191 (PMPageFormat
) m_macPageFormat
,
192 kPMDontWantBoolean
) ;
197 bool wxMacCarbonPrintData::TransferTo( wxPrintData
&data
)
199 OSStatus err
= noErr
;
202 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
204 data
.SetNoCopies( copies
) ;
206 PMOrientation orientation
;
207 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
210 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
212 data
.SetOrientation( wxPORTRAIT
);
213 data
.SetOrientationReversed( orientation
== kPMReversePortrait
);
217 data
.SetOrientation( wxLANDSCAPE
);
218 data
.SetOrientationReversed( orientation
== kPMReverseLandscape
);
222 // collate cannot be set
225 wxMacCFStringHolder name
;
227 PMSessionGetCurrentPrinter( m_macPrintSession
,
229 m_printerName
= name
.AsString() ;
235 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
237 data
.SetColour( !(color
== kPMBlackAndWhite
) ) ;
239 PMDuplexMode mode
= 0 ;
240 PMGetDuplex( (PMPrintSettings
) m_macPrintSettings
, &mode
) ;
243 case kPMDuplexNoTumble
:
244 data
.SetDuplex(wxDUPLEX_HORIZONTAL
);
246 case kPMDuplexTumble
:
247 data
.SetDuplex(wxDUPLEX_VERTICAL
);
251 data
.SetDuplex(wxDUPLEX_SIMPLEX
);
254 // PMQualityMode not yet accessible via API
257 PMGetPageFormatPaper( m_macPageFormat
, &paper
);
260 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
263 wxSize
sz((int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
264 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ));
265 data
.SetPaperSize(sz
);
266 wxPaperSize id
= wxThePrintPaperDatabase
->GetSize(wxSize(sz
.x
* 10, sz
.y
* 10));
267 if (id
!= wxPAPER_NONE
)
275 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*WXUNUSED(data
) )
277 // should we setup the page rect here ?
278 // since MacOS sometimes has two same paper rects with different
279 // page rects we could make it roundtrip safe perhaps
282 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
285 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
288 wxSize
sz((int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
289 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ));
290 data
->SetPaperSize(sz
);
293 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
296 data
->SetMinMarginTopLeft( wxPoint (
297 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
298 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
300 data
->SetMinMarginBottomRight( wxPoint (
301 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
302 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
304 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
305 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
306 data
->GetMarginTopLeft().y
) ) ;
308 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
309 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
310 data
->GetMarginBottomRight().y
) );
312 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
313 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
315 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
316 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
317 data
->GetMinMarginBottomRight().y
) );
322 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
324 UInt32 minPage
, maxPage
;
325 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
326 data
->SetMinPage( minPage
) ;
327 data
->SetMaxPage( maxPage
) ;
329 PMGetCopies( m_macPrintSettings
, &copies
) ;
330 data
->SetNoCopies( copies
) ;
332 PMGetFirstPage( m_macPrintSettings
, &from
) ;
333 PMGetLastPage( m_macPrintSettings
, &to
) ;
334 if ( to
>= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
336 data
->SetAllPages( true ) ;
337 // This means all pages, more or less
338 data
->SetFromPage(1);
339 data
->SetToPage(32000);
343 data
->SetFromPage( from
) ;
344 data
->SetToPage( to
) ;
345 data
->SetAllPages( false );
349 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
351 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
352 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
353 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
355 if (data
->GetAllPages() || data
->GetFromPage() == 0)
356 PMSetLastPage( m_macPrintSettings
, (UInt32
) kPMPrintAllPages
, true ) ;
358 PMSetLastPage( m_macPrintSettings
, (UInt32
) data
->GetToPage() , false ) ;
365 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
370 wxMacPrinter::~wxMacPrinter(void)
374 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
377 sm_abortWindow
= NULL
;
382 printout
->SetIsPreview(false);
383 if (m_printDialogData
.GetMinPage() < 1)
384 m_printDialogData
.SetMinPage(1);
385 if (m_printDialogData
.GetMaxPage() < 1)
386 m_printDialogData
.SetMaxPage(9999);
388 // Create a suitable device context
389 wxPrinterDC
*dc
= NULL
;
392 wxMacPrintDialog
dialog(parent
, & m_printDialogData
);
393 if (dialog
.ShowModal() == wxID_OK
)
395 dc
= wxDynamicCast(dialog
.GetPrintDC(), wxPrinterDC
);
397 m_printDialogData
= dialog
.GetPrintDialogData();
402 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
405 // May have pressed cancel.
406 if (!dc
|| !dc
->Ok())
413 // on the mac we have always pixels as addressing mode with 72 dpi
414 printout
->SetPPIScreen(72, 72);
416 wxMacCarbonPrintData
* nativeData
= (wxMacCarbonPrintData
*)
417 (m_printDialogData
.GetPrintData().GetNativeData());
418 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
420 PMSessionGetCurrentPrinter(nativeData
->m_macPrintSession
, &printer
);
421 PMPrinterGetOutputResolution( printer
, nativeData
->m_macPrintSettings
, &res
) ;
423 PMGetResolution((PMPageFormat
) (nativeData
->m_macPageFormat
), &res
);
425 printout
->SetPPIPrinter(int(res
.hRes
), int(res
.vRes
));
427 // Set printout parameters
432 printout
->SetPageSizePixels((int)w
, (int)h
);
433 printout
->SetPaperRectPixels(dc
->GetPaperRect());
435 dc
->GetSizeMM(&mw
, &mh
);
436 printout
->SetPageSizeMM((int)mw
, (int)mh
);
438 // Create an abort window
441 printout
->OnPreparePrinting();
443 // Get some parameters from the printout, if defined
444 int fromPage
, toPage
;
445 int minPage
, maxPage
;
446 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
454 // Only set min and max, because from and to have been
456 m_printDialogData
.SetMinPage(minPage
);
457 m_printDialogData
.SetMaxPage(maxPage
);
459 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
460 wxSafeYield(win
,true);
465 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
471 sm_abortWindow
= win
;
472 sm_abortWindow
->Show(true);
473 wxSafeYield(win
,true);
475 printout
->OnBeginPrinting();
477 bool keepGoing
= true;
480 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
482 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
485 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
492 for (pn
= m_printDialogData
.GetFromPage();
493 keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
503 wxSafeYield(win
,true);
505 keepGoing
= printout
->OnPrintPage(pn
);
509 printout
->OnEndDocument();
512 printout
->OnEndPrinting();
516 sm_abortWindow
->Show(false);
517 delete sm_abortWindow
;
518 sm_abortWindow
= NULL
;
528 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
530 wxDC
* dc
= (wxDC
*) NULL
;
532 wxPrintDialog
dialog(parent
, & m_printDialogData
);
533 int ret
= dialog
.ShowModal();
537 dc
= dialog
.GetPrintDC();
538 m_printDialogData
= dialog
.GetPrintDialogData();
544 bool wxMacPrinter::Setup(wxWindow
*WXUNUSED(parent
))
547 wxPrintDialog
dialog(parent
, & m_printDialogData
);
548 dialog
.GetPrintDialogData().SetSetupDialog(true);
550 int ret
= dialog
.ShowModal();
553 m_printDialogData
= dialog
.GetPrintDialogData();
555 return (ret
== wxID_OK
);
565 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
566 wxPrintout
*printoutForPrinting
,
567 wxPrintDialogData
*data
)
568 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
573 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
574 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
579 wxMacPrintPreview::~wxMacPrintPreview(void)
583 bool wxMacPrintPreview::Print(bool interactive
)
585 if (!m_printPrintout
)
588 wxMacPrinter
printer(&m_printDialogData
);
589 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
592 void wxMacPrintPreview::DetermineScaling(void)
594 int screenWidth
, screenHeight
;
595 wxDisplaySize( &screenWidth
, &screenHeight
) ;
597 wxSize
ppiScreen( 72 , 72 ) ;
598 wxSize
ppiPrinter( 72 , 72 ) ;
600 // Note that with Leopard, screen dpi=72 is no longer a given
601 m_previewPrintout
->SetPPIScreen( ppiScreen
.x
, ppiScreen
.y
) ;
607 // Get a device context for the currently selected printer
608 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
611 printerDC
.GetSizeMM(&ww
, &hh
);
612 printerDC
.GetSize( &w
, &h
) ;
613 ppiPrinter
= printerDC
.GetPPI() ;
614 paperRect
= printerDC
.GetPaperRect();
622 ww
= (wxCoord
) (w
* 25.4 / ppiPrinter
.x
) ;
623 hh
= (wxCoord
) (h
* 25.4 / ppiPrinter
.y
) ;
624 paperRect
= wxRect(0, 0, w
, h
);
630 m_previewPrintout
->SetPageSizePixels(w
, h
) ;
631 m_previewPrintout
->SetPageSizeMM(ww
, hh
);
632 m_previewPrintout
->SetPaperRectPixels(paperRect
);
633 m_previewPrintout
->SetPPIPrinter( ppiPrinter
.x
, ppiPrinter
.y
) ;
635 m_previewScaleX
= float(ppiScreen
.x
) / ppiPrinter
.x
;
636 m_previewScaleY
= float(ppiScreen
.y
) / ppiPrinter
.y
;