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"
36 #include "wx/mac/printdlg.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData
, wxPrintNativeDataBase
)
41 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter
, wxPrinterBase
)
42 IMPLEMENT_CLASS(wxMacPrintPreview
, wxPrintPreviewBase
)
44 bool wxMacCarbonPrintData::IsOk() const
46 return (m_macPageFormat
!= kPMNoPageFormat
) && (m_macPrintSettings
!= kPMNoPrintSettings
) && (m_macPrintSession
!= kPMNoReference
);
48 wxMacCarbonPrintData::wxMacCarbonPrintData()
50 m_macPageFormat
= kPMNoPageFormat
;
51 m_macPrintSettings
= kPMNoPrintSettings
;
52 m_macPrintSession
= kPMNoReference
;
56 wxMacCarbonPrintData::~wxMacCarbonPrintData()
58 if (m_macPageFormat
!= kPMNoPageFormat
)
60 (void)PMRelease(m_macPageFormat
);
61 m_macPageFormat
= kPMNoPageFormat
;
64 if (m_macPrintSettings
!= kPMNoPrintSettings
)
66 (void)PMRelease(m_macPrintSettings
);
67 m_macPrintSettings
= kPMNoPrintSettings
;
70 if ( m_macPrintSession
!= kPMNoReference
)
72 (void)PMRelease(m_macPrintSession
);
73 m_macPrintSession
= kPMNoReference
;
77 void wxMacCarbonPrintData::ValidateOrCreate()
79 OSStatus err
= noErr
;
80 if ( m_macPrintSession
== kPMNoReference
)
82 err
= PMCreateSession( (PMPrintSession
*) &m_macPrintSession
) ;
84 // Set up a valid PageFormat object.
85 if ( m_macPageFormat
== kPMNoPageFormat
)
87 err
= PMCreatePageFormat((PMPageFormat
*) &m_macPageFormat
);
89 // Note that PMPageFormat is not session-specific, but calling
90 // PMSessionDefaultPageFormat assigns values specific to the printer
91 // associated with the current printing session.
93 ( m_macPageFormat
!= kPMNoPageFormat
))
95 err
= PMSessionDefaultPageFormat((PMPrintSession
) m_macPrintSession
,
96 (PMPageFormat
) m_macPageFormat
);
101 err
= PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
102 (PMPageFormat
) m_macPageFormat
,
106 // Set up a valid PrintSettings object.
107 if ( m_macPrintSettings
== kPMNoPrintSettings
)
109 err
= PMCreatePrintSettings((PMPrintSettings
*) &m_macPrintSettings
);
111 // Note that PMPrintSettings is not session-specific, but calling
112 // PMSessionDefaultPrintSettings assigns values specific to the printer
113 // associated with the current printing session.
114 if ((err
== noErr
) &&
115 ( m_macPrintSettings
!= kPMNoPrintSettings
))
117 err
= PMSessionDefaultPrintSettings((PMPrintSession
) m_macPrintSession
,
118 (PMPrintSettings
) m_macPrintSettings
);
123 err
= PMSessionValidatePrintSettings((PMPrintSession
) m_macPrintSession
,
124 (PMPrintSettings
) m_macPrintSettings
,
129 bool wxMacCarbonPrintData::TransferFrom( const wxPrintData
&data
)
132 PMSetCopies( (PMPrintSettings
) m_macPrintSettings
, data
.GetNoCopies() , false ) ;
133 if ( data
.IsOrientationReversed() )
134 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
135 kPMReverseLandscape
: kPMReversePortrait
, false ) ;
137 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
138 kPMLandscape
: kPMPortrait
, false ) ;
139 // collate cannot be set
140 #if 0 // not yet tested
141 if ( !m_printerName
.empty() )
142 PMSessionSetCurrentPrinter( (PMPrintSession
) m_macPrintSession
, wxMacCFStringHolder( m_printerName
, wxFont::GetDefaultEncoding() ) ) ;
146 PMGetColorMode( (PMPrintSettings
) m_macPrintSettings
, &color
) ;
147 if ( data
.GetColour() )
149 if ( color
== kPMBlackAndWhite
)
150 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMColor
) ;
153 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMBlackAndWhite
) ;
156 // PMDuplexMode not yet accessible via API
157 // PMQualityMode not yet accessible via API
162 PMSessionGetCurrentPrinter(m_macPrintSession
, &printer
);
163 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
164 PMPrinterGetOutputResolution( printer
,
165 (PMPrintSettings
) m_macPrintSettings
, &res
) ;
166 // TODO transfer ? into page format ?
168 PMTag tag
= kPMMaxSquareResolution
;
169 PMPrinterGetPrinterResolution(printer
, tag
, &res
);
170 PMSetResolution((PMPageFormat
) m_macPageFormat
, &res
);
172 // after setting the new resolution the format has to be updated, otherwise the page rect remains
173 // at the 'old' scaling
174 PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
175 (PMPageFormat
) m_macPageFormat
,
176 kPMDontWantBoolean
) ;
181 bool wxMacCarbonPrintData::TransferTo( wxPrintData
&data
)
183 OSStatus err
= noErr
;
186 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
188 data
.SetNoCopies( copies
) ;
190 PMOrientation orientation
;
191 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
194 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
196 data
.SetOrientation( wxPORTRAIT
);
197 data
.SetOrientationReversed( orientation
== kPMReversePortrait
);
201 data
.SetOrientation( wxLANDSCAPE
);
202 data
.SetOrientationReversed( orientation
== kPMReverseLandscape
);
206 // collate cannot be set
209 wxMacCFStringHolder name
;
211 PMSessionGetCurrentPrinter( m_macPrintSession
,
213 m_printerName
= name
.AsString() ;
219 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
221 data
.SetColour( !(color
== kPMBlackAndWhite
) ) ;
223 // PMDuplexMode not yet accessible via API
224 // PMQualityMode not yet accessible via API
227 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
230 data
.SetPaperSize( wxSize (
231 (int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
232 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) ) );
237 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*data
)
239 // should we setup the page rect here ?
240 // since MacOS sometimes has two same paper rects with different
241 // page rects we could make it roundtrip safe perhaps
244 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
247 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
251 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
254 data
->SetMinMarginTopLeft( wxPoint (
255 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
256 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
258 data
->SetMinMarginBottomRight( wxPoint (
259 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
260 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
262 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
263 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
264 data
->GetMarginTopLeft().y
) ) ;
266 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
267 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
268 data
->GetMarginBottomRight().y
) );
270 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
271 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
273 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
274 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
275 data
->GetMinMarginBottomRight().y
) );
280 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
282 UInt32 minPage
, maxPage
;
283 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
284 data
->SetMinPage( minPage
) ;
285 data
->SetMaxPage( maxPage
) ;
287 PMGetCopies( m_macPrintSettings
, &copies
) ;
288 data
->SetNoCopies( copies
) ;
290 PMGetFirstPage( m_macPrintSettings
, &from
) ;
291 PMGetLastPage( m_macPrintSettings
, &to
) ;
292 if ( to
>= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
294 data
->SetAllPages( true ) ;
295 // This means all pages, more or less
296 data
->SetFromPage(1);
297 data
->SetToPage(32000);
301 data
->SetFromPage( from
) ;
302 data
->SetToPage( to
) ;
303 data
->SetAllPages( false );
307 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
309 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
310 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
311 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
313 if (data
->GetAllPages() || data
->GetFromPage() == 0)
314 PMSetLastPage( m_macPrintSettings
, (UInt32
) kPMPrintAllPages
, true ) ;
316 PMSetLastPage( m_macPrintSettings
, (UInt32
) data
->GetToPage() , false ) ;
323 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
328 wxMacPrinter::~wxMacPrinter(void)
332 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
335 sm_abortWindow
= NULL
;
340 printout
->SetIsPreview(false);
341 if (m_printDialogData
.GetMinPage() < 1)
342 m_printDialogData
.SetMinPage(1);
343 if (m_printDialogData
.GetMaxPage() < 1)
344 m_printDialogData
.SetMaxPage(9999);
346 // Create a suitable device context
347 wxPrinterDC
*dc
= NULL
;
350 wxMacPrintDialog
dialog(parent
, & m_printDialogData
);
351 if (dialog
.ShowModal() == wxID_OK
)
353 dc
= wxDynamicCast(dialog
.GetPrintDC(), wxPrinterDC
);
355 m_printDialogData
= dialog
.GetPrintDialogData();
360 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
363 // May have pressed cancel.
364 if (!dc
|| !dc
->Ok())
371 // on the mac we have always pixels as addressing mode with 72 dpi
372 printout
->SetPPIScreen(72, 72);
374 wxMacCarbonPrintData
* nativeData
= (wxMacCarbonPrintData
*)
375 (m_printDialogData
.GetPrintData().GetNativeData());
376 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
378 PMSessionGetCurrentPrinter(nativeData
->m_macPrintSession
, &printer
);
379 PMPrinterGetOutputResolution( printer
, nativeData
->m_macPrintSettings
, &res
) ;
381 PMGetResolution((PMPageFormat
) (nativeData
->m_macPageFormat
), &res
);
383 printout
->SetPPIPrinter(int(res
.hRes
), int(res
.vRes
));
385 // Set printout parameters
390 printout
->SetPageSizePixels((int)w
, (int)h
);
391 printout
->SetPaperRectPixels(dc
->GetPaperRect());
393 dc
->GetSizeMM(&mw
, &mh
);
394 printout
->SetPageSizeMM((int)mw
, (int)mh
);
396 // Create an abort window
399 printout
->OnPreparePrinting();
401 // Get some parameters from the printout, if defined
402 int fromPage
, toPage
;
403 int minPage
, maxPage
;
404 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
412 // Only set min and max, because from and to have been
414 m_printDialogData
.SetMinPage(minPage
);
415 m_printDialogData
.SetMaxPage(maxPage
);
417 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
418 wxSafeYield(win
,true);
423 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
429 sm_abortWindow
= win
;
430 sm_abortWindow
->Show(true);
431 wxSafeYield(win
,true);
433 printout
->OnBeginPrinting();
435 bool keepGoing
= true;
438 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
440 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
443 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
450 for (pn
= m_printDialogData
.GetFromPage();
451 keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
462 if ( UMAGetSystemVersion() >= 0x1000 )
465 #if !wxMAC_USE_CORE_GRAPHICS
467 GetPort( &thePort
) ;
469 wxSafeYield(win
,true);
470 #if !wxMAC_USE_CORE_GRAPHICS
475 keepGoing
= printout
->OnPrintPage(pn
);
479 printout
->OnEndDocument();
482 printout
->OnEndPrinting();
486 sm_abortWindow
->Show(false);
487 delete sm_abortWindow
;
488 sm_abortWindow
= NULL
;
498 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
500 wxDC
* dc
= (wxDC
*) NULL
;
502 wxPrintDialog
dialog(parent
, & m_printDialogData
);
503 int ret
= dialog
.ShowModal();
507 dc
= dialog
.GetPrintDC();
508 m_printDialogData
= dialog
.GetPrintDialogData();
514 bool wxMacPrinter::Setup(wxWindow
*parent
)
517 wxPrintDialog
dialog(parent
, & m_printDialogData
);
518 dialog
.GetPrintDialogData().SetSetupDialog(true);
520 int ret
= dialog
.ShowModal();
523 m_printDialogData
= dialog
.GetPrintDialogData();
525 return (ret
== wxID_OK
);
535 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
536 wxPrintout
*printoutForPrinting
,
537 wxPrintDialogData
*data
)
538 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
543 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
544 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
549 wxMacPrintPreview::~wxMacPrintPreview(void)
553 bool wxMacPrintPreview::Print(bool interactive
)
555 if (!m_printPrintout
)
558 wxMacPrinter
printer(&m_printDialogData
);
559 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
562 void wxMacPrintPreview::DetermineScaling(void)
564 int screenWidth
, screenHeight
;
565 wxDisplaySize( &screenWidth
, &screenHeight
) ;
567 wxSize
ppiScreen( 72 , 72 ) ;
568 wxSize
ppiPrinter( 72 , 72 ) ;
570 // Note that with Leopard, screen dpi=72 is no longer a given
571 m_previewPrintout
->SetPPIScreen( ppiScreen
.x
, ppiScreen
.y
) ;
577 // Get a device context for the currently selected printer
578 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
581 printerDC
.GetSizeMM(&ww
, &hh
);
582 printerDC
.GetSize( &w
, &h
) ;
583 ppiPrinter
= printerDC
.GetPPI() ;
584 paperRect
= printerDC
.GetPaperRect();
592 ww
= (wxCoord
) (w
* 25.4 / ppiPrinter
.x
) ;
593 hh
= (wxCoord
) (h
* 25.4 / ppiPrinter
.y
) ;
594 paperRect
= wxRect(0, 0, w
, h
);
600 m_previewPrintout
->SetPageSizePixels(w
, h
) ;
601 m_previewPrintout
->SetPageSizeMM(ww
, hh
);
602 m_previewPrintout
->SetPaperRectPixels(paperRect
);
603 m_previewPrintout
->SetPPIPrinter( ppiPrinter
.x
, ppiPrinter
.y
) ;
605 m_previewScaleX
= float(ppiScreen
.x
) / ppiPrinter
.x
;
606 m_previewScaleY
= float(ppiScreen
.y
) / ppiPrinter
.y
;