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 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
134 kPMLandscape
: kPMPortrait
, false ) ;
135 // collate cannot be set
136 #if 0 // not yet tested
137 if ( !m_printerName
.empty() )
138 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
) ;
152 // PMDuplexMode not yet accessible via API
153 // PMQualityMode not yet accessible via API
158 PMSessionGetCurrentPrinter(m_macPrintSession
, &printer
);
159 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
160 PMPrinterGetOutputResolution( printer
,
161 (PMPrintSettings
) m_macPrintSettings
, &res
) ;
162 // TODO transfer ? into page format ?
164 PMTag tag
= kPMMaxSquareResolution
;
165 PMPrinterGetPrinterResolution(printer
, tag
, &res
);
166 PMSetResolution((PMPageFormat
) m_macPageFormat
, &res
);
168 // after setting the new resolution the format has to be updated, otherwise the page rect remains
169 // at the 'old' scaling
170 PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
171 (PMPageFormat
) m_macPageFormat
,
172 kPMDontWantBoolean
) ;
177 bool wxMacCarbonPrintData::TransferTo( wxPrintData
&data
)
179 OSStatus err
= noErr
;
182 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
184 data
.SetNoCopies( copies
) ;
186 PMOrientation orientation
;
187 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
190 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
191 data
.SetOrientation( wxPORTRAIT
);
193 data
.SetOrientation( wxLANDSCAPE
);
196 // collate cannot be set
199 wxMacCFStringHolder name
;
201 PMSessionGetCurrentPrinter( m_macPrintSession
,
203 m_printerName
= name
.AsString() ;
209 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
211 data
.SetColour( !(color
== kPMBlackAndWhite
) ) ;
213 // PMDuplexMode not yet accessible via API
214 // PMQualityMode not yet accessible via API
217 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
220 data
.SetPaperSize( wxSize (
221 (int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
222 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) ) );
227 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*data
)
229 // should we setup the page rect here ?
230 // since MacOS sometimes has two same paper rects with different
231 // page rects we could make it roundtrip safe perhaps
234 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
237 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
241 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
244 data
->SetMinMarginTopLeft( wxPoint (
245 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
246 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
248 data
->SetMinMarginBottomRight( wxPoint (
249 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
250 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
252 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
253 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
254 data
->GetMarginTopLeft().y
) ) ;
256 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
257 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
258 data
->GetMarginBottomRight().y
) );
260 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
261 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
263 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
264 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
265 data
->GetMinMarginBottomRight().y
) );
270 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
272 UInt32 minPage
, maxPage
;
273 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
274 data
->SetMinPage( minPage
) ;
275 data
->SetMaxPage( maxPage
) ;
277 PMGetCopies( m_macPrintSettings
, &copies
) ;
278 data
->SetNoCopies( copies
) ;
280 PMGetFirstPage( m_macPrintSettings
, &from
) ;
281 PMGetLastPage( m_macPrintSettings
, &to
) ;
282 if ( to
>= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
284 data
->SetAllPages( true ) ;
285 // This means all pages, more or less
286 data
->SetFromPage(1);
287 data
->SetToPage(32000);
291 data
->SetFromPage( from
) ;
292 data
->SetToPage( to
) ;
293 data
->SetAllPages( false );
297 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
299 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
300 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
301 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
303 if (data
->GetAllPages() || data
->GetFromPage() == 0)
304 PMSetLastPage( m_macPrintSettings
, (UInt32
) kPMPrintAllPages
, true ) ;
306 PMSetLastPage( m_macPrintSettings
, (UInt32
) data
->GetToPage() , false ) ;
313 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
318 wxMacPrinter::~wxMacPrinter(void)
322 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
325 sm_abortWindow
= NULL
;
330 printout
->SetIsPreview(false);
331 if (m_printDialogData
.GetMinPage() < 1)
332 m_printDialogData
.SetMinPage(1);
333 if (m_printDialogData
.GetMaxPage() < 1)
334 m_printDialogData
.SetMaxPage(9999);
336 // Create a suitable device context
337 wxPrinterDC
*dc
= NULL
;
340 wxMacPrintDialog
dialog(parent
, & m_printDialogData
);
341 if (dialog
.ShowModal() == wxID_OK
)
343 dc
= wxDynamicCast(dialog
.GetPrintDC(), wxPrinterDC
);
345 m_printDialogData
= dialog
.GetPrintDialogData();
350 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
353 // May have pressed cancel.
354 if (!dc
|| !dc
->Ok())
361 // on the mac we have always pixels as addressing mode with 72 dpi
362 printout
->SetPPIScreen(72, 72);
364 wxMacCarbonPrintData
* nativeData
= (wxMacCarbonPrintData
*)
365 (m_printDialogData
.GetPrintData().GetNativeData());
366 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
368 PMSessionGetCurrentPrinter(nativeData
->m_macPrintSession
, &printer
);
369 PMPrinterGetOutputResolution( printer
, nativeData
->m_macPrintSettings
, &res
) ;
371 PMGetResolution((PMPageFormat
) (nativeData
->m_macPageFormat
), &res
);
373 printout
->SetPPIPrinter(int(res
.hRes
), int(res
.vRes
));
375 // Set printout parameters
380 printout
->SetPageSizePixels((int)w
, (int)h
);
381 printout
->SetPaperRectPixels(dc
->GetPaperRect());
383 dc
->GetSizeMM(&mw
, &mh
);
384 printout
->SetPageSizeMM((int)mw
, (int)mh
);
386 // Create an abort window
389 printout
->OnPreparePrinting();
391 // Get some parameters from the printout, if defined
392 int fromPage
, toPage
;
393 int minPage
, maxPage
;
394 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
402 // Only set min and max, because from and to have been
404 m_printDialogData
.SetMinPage(minPage
);
405 m_printDialogData
.SetMaxPage(maxPage
);
407 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
408 wxSafeYield(win
,true);
413 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
419 sm_abortWindow
= win
;
420 sm_abortWindow
->Show(true);
421 wxSafeYield(win
,true);
423 printout
->OnBeginPrinting();
425 bool keepGoing
= true;
428 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
430 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
433 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
440 for (pn
= m_printDialogData
.GetFromPage();
441 keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
452 if ( UMAGetSystemVersion() >= 0x1000 )
455 #if !wxMAC_USE_CORE_GRAPHICS
457 GetPort( &thePort
) ;
459 wxSafeYield(win
,true);
460 #if !wxMAC_USE_CORE_GRAPHICS
465 keepGoing
= printout
->OnPrintPage(pn
);
469 printout
->OnEndDocument();
472 printout
->OnEndPrinting();
476 sm_abortWindow
->Show(false);
477 delete sm_abortWindow
;
478 sm_abortWindow
= NULL
;
488 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
490 wxDC
* dc
= (wxDC
*) NULL
;
492 wxPrintDialog
dialog(parent
, & m_printDialogData
);
493 int ret
= dialog
.ShowModal();
497 dc
= dialog
.GetPrintDC();
498 m_printDialogData
= dialog
.GetPrintDialogData();
504 bool wxMacPrinter::Setup(wxWindow
*parent
)
507 wxPrintDialog
dialog(parent
, & m_printDialogData
);
508 dialog
.GetPrintDialogData().SetSetupDialog(true);
510 int ret
= dialog
.ShowModal();
513 m_printDialogData
= dialog
.GetPrintDialogData();
515 return (ret
== wxID_OK
);
525 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
526 wxPrintout
*printoutForPrinting
,
527 wxPrintDialogData
*data
)
528 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
533 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
534 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
539 wxMacPrintPreview::~wxMacPrintPreview(void)
543 bool wxMacPrintPreview::Print(bool interactive
)
545 if (!m_printPrintout
)
548 wxMacPrinter
printer(&m_printDialogData
);
549 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
552 void wxMacPrintPreview::DetermineScaling(void)
554 int screenWidth
, screenHeight
;
555 wxDisplaySize( &screenWidth
, &screenHeight
) ;
557 wxSize
ppiScreen( 72 , 72 ) ;
558 wxSize
ppiPrinter( 72 , 72 ) ;
560 // Note that with Leopard, screen dpi=72 is no longer a given
561 m_previewPrintout
->SetPPIScreen( ppiScreen
.x
, ppiScreen
.y
) ;
567 // Get a device context for the currently selected printer
568 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
571 printerDC
.GetSizeMM(&ww
, &hh
);
572 printerDC
.GetSize( &w
, &h
) ;
573 ppiPrinter
= printerDC
.GetPPI() ;
574 paperRect
= printerDC
.GetPaperRect();
582 ww
= (wxCoord
) (w
* 25.4 / ppiPrinter
.x
) ;
583 hh
= (wxCoord
) (h
* 25.4 / ppiPrinter
.y
) ;
584 paperRect
= wxRect(0, 0, w
, h
);
590 m_previewPrintout
->SetPageSizePixels(w
, h
) ;
591 m_previewPrintout
->SetPageSizeMM(ww
, hh
);
592 m_previewPrintout
->SetPaperRectPixels(paperRect
);
593 m_previewPrintout
->SetPPIPrinter( ppiPrinter
.x
, ppiPrinter
.y
) ;
595 m_previewScaleX
= float(ppiScreen
.x
) / ppiPrinter
.x
;
596 m_previewScaleY
= float(ppiScreen
.y
) / ppiPrinter
.y
;