1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/printmac.cpp
3 // Purpose: wxMacPrinter framework
4 // Author: Julian Smart, Stefan Csomor
8 // Copyright: (c) Julian Smart, Stefan Csomor
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/osx/private.h"
32 #include "wx/osx/printmac.h"
33 #include "wx/osx/private/print.h"
35 #include "wx/printdlg.h"
37 #include "wx/osx/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( &m_macPrintSession
) ;
85 // Set up a valid PageFormat object.
86 if ( m_macPageFormat
== kPMNoPageFormat
)
88 err
= PMCreatePageFormat(&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(m_macPrintSession
,
102 err
= PMSessionValidatePageFormat(m_macPrintSession
,
107 // Set up a valid PrintSettings object.
108 if ( m_macPrintSettings
== kPMNoPrintSettings
)
110 err
= PMCreatePrintSettings( &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(m_macPrintSession
,
124 err
= PMSessionValidatePrintSettings( m_macPrintSession
,
130 bool wxMacCarbonPrintData::TransferFrom( const wxPrintData
&data
)
132 CFArrayRef printerList
;
133 CFIndex index
, count
;
139 if (PMServerCreatePrinterList(kPMServerLocal
, &printerList
) == noErr
)
141 count
= CFArrayGetCount(printerList
);
142 for (index
= 0; index
< count
; index
++)
144 printer
= (PMPrinter
)CFArrayGetValueAtIndex(printerList
, index
);
145 if ((data
.GetPrinterName().empty()) && (PMPrinterIsDefault(printer
)))
149 name
= PMPrinterGetName(printer
);
151 if (data
.GetPrinterName() == wxCFStringRef(name
).AsString())
156 PMSessionSetCurrentPMPrinter((PMPrintSession
)m_macPrintSession
, printer
);
157 CFRelease(printerList
);
160 PMSetCopies( m_macPrintSettings
, data
.GetNoCopies() , false ) ;
161 PMSetCollate(m_macPrintSettings
, data
.GetCollate());
165 PMGetColorMode( (PMPrintSettings
) m_macPrintSettings
, &color
) ;
166 if ( data
.GetColour() )
168 if ( color
== kPMBlackAndWhite
)
169 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMColor
) ;
172 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMBlackAndWhite
) ;
175 PMDuplexMode mode
= 0 ;
176 switch( data
.GetDuplex() )
178 case wxDUPLEX_HORIZONTAL
:
179 mode
= kPMDuplexNoTumble
;
181 case wxDUPLEX_VERTICAL
:
182 mode
= kPMDuplexTumble
;
184 case wxDUPLEX_SIMPLEX
:
186 mode
= kPMDuplexNone
;
189 PMSetDuplex( (PMPrintSettings
) m_macPrintSettings
, mode
) ;
191 // PMQualityMode not yet accessible via API
194 PMSessionGetCurrentPrinter(m_macPrintSession
, &printer
);
197 CFArrayRef formatList
;
198 PMSessionCreatePageFormatList(m_macPrintSession
, printer
, &formatList
);
201 count
= CFArrayGetCount(formatList
);
202 for (index
= 0; index
< count
; index
++)
204 PMPageFormat temp
= (PMPageFormat
)CFArrayGetValueAtIndex(formatList
, index
);
206 PMGetUnadjustedPaperRect(temp
, &rPaper
);
207 wxSize
sz((int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
208 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ));
209 wxPaperSize id
= wxThePrintPaperDatabase
->GetSize(wxSize(sz
.x
* 10, sz
.y
* 10));
210 if (((data
.GetPaperId() != wxPAPER_NONE
) && (id
== data
.GetPaperId())) ||
211 ((data
.GetPaperId() == wxPAPER_NONE
) && (sz
== data
.GetPaperSize())))
213 PMCopyPageFormat(temp
, m_macPageFormat
);
218 CFRelease(formatList
);
223 const PMPaperMargins margins
= { 0.0, 0.0, 0.0, 0.0 };
224 wxString id
, name(_T("Custom paper"));
226 double width
, height
;
228 id
.Printf(_T("wxPaperCustom%dx%d"), sz
.x
, sz
.y
);
229 if (data
.GetPaperId() == wxPAPER_NONE
)
230 sz
= data
.GetPaperSize();
232 sz
= wxThePrintPaperDatabase
->GetSize(data
.GetPaperId());
233 width
= ((double)sz
.x
/ 10.0) * mm2pt
;
234 height
= ((double)sz
.y
/ 10.0) * mm2pt
;
236 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
237 PMPaperCreate(printer
, wxCFStringRef( id
, wxFont::GetDefaultEncoding() ), wxCFStringRef( name
, wxFont::GetDefaultEncoding() ), width
, height
, &margins
, &paper
);
239 if (PMPaperCreateCustom(printer
, wxCFStringRef( id
, wxFont::GetDefaultEncoding() ), wxCFStringRef( name
, wxFont::GetDefaultEncoding() ), width
, height
, &margins
, &paper
) != noErr
)
245 if (PMCreatePageFormatWithPMPaper(&temp
, paper
) == noErr
) {
246 PMCopyPageFormat(temp
, m_macPageFormat
);
253 if ( data
.IsOrientationReversed() )
254 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
255 kPMReverseLandscape
: kPMReversePortrait
, false ) ;
257 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
258 kPMLandscape
: kPMPortrait
, false ) ;
261 PMTag tag
= kPMMaxSquareResolution
;
262 PMPrinterGetPrinterResolution(printer
, tag
, &res
);
263 PMSetResolution((PMPageFormat
) m_macPageFormat
, &res
);
265 PMPrinterGetOutputResolution( printer
,
266 (PMPrintSettings
) m_macPrintSettings
, &res
) ;
267 // TODO transfer ? into page format ?
270 // after setting the new resolution the format has to be updated, otherwise the page rect remains
271 // at the 'old' scaling
272 PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
273 (PMPageFormat
) m_macPageFormat
,
274 kPMDontWantBoolean
) ;
279 bool wxMacCarbonPrintData::TransferTo( wxPrintData
&data
)
281 OSStatus err
= noErr
;
284 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
286 data
.SetNoCopies( copies
) ;
288 PMOrientation orientation
;
289 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
292 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
294 data
.SetOrientation( wxPORTRAIT
);
295 data
.SetOrientationReversed( orientation
== kPMReversePortrait
);
299 data
.SetOrientation( wxLANDSCAPE
);
300 data
.SetOrientationReversed( orientation
== kPMReverseLandscape
);
305 if (PMGetCollate(m_macPrintSettings
, &collate
) == noErr
)
306 data
.SetCollate(collate
);
310 PMSessionGetCurrentPrinter( m_macPrintSession
, &printer
);
311 if (PMPrinterIsDefault(printer
))
312 data
.SetPrinterName(wxEmptyString
);
315 name
= PMPrinterGetName(printer
);
317 data
.SetPrinterName(wxCFStringRef(name
).AsString());
322 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
324 data
.SetColour( !(color
== kPMBlackAndWhite
) ) ;
326 PMDuplexMode mode
= 0 ;
327 PMGetDuplex( (PMPrintSettings
) m_macPrintSettings
, &mode
) ;
330 case kPMDuplexNoTumble
:
331 data
.SetDuplex(wxDUPLEX_HORIZONTAL
);
333 case kPMDuplexTumble
:
334 data
.SetDuplex(wxDUPLEX_VERTICAL
);
338 data
.SetDuplex(wxDUPLEX_SIMPLEX
);
341 // PMQualityMode not yet accessible via API
344 PMGetPageFormatPaper( m_macPageFormat
, &paper
);
347 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
350 wxSize
sz((int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) * 10,
351 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) * 10);
352 data
.SetPaperSize(sz
);
353 wxPaperSize id
= wxThePrintPaperDatabase
->GetSize(sz
);
359 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*WXUNUSED(data
) )
361 // should we setup the page rect here ?
362 // since MacOS sometimes has two same paper rects with different
363 // page rects we could make it roundtrip safe perhaps
366 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
369 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
372 wxSize
sz((int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
373 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ));
374 data
->SetPaperSize(sz
);
377 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
380 data
->SetMinMarginTopLeft( wxPoint (
381 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
382 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
384 data
->SetMinMarginBottomRight( wxPoint (
385 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
386 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
388 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
389 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
390 data
->GetMarginTopLeft().y
) ) ;
392 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
393 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
394 data
->GetMarginBottomRight().y
) );
396 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
397 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
399 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
400 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
401 data
->GetMinMarginBottomRight().y
) );
406 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
408 UInt32 minPage
, maxPage
;
409 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
410 data
->SetMinPage( minPage
) ;
411 data
->SetMaxPage( maxPage
) ;
413 PMGetCopies( m_macPrintSettings
, &copies
) ;
414 data
->SetNoCopies( copies
) ;
416 PMGetFirstPage( m_macPrintSettings
, &from
) ;
417 PMGetLastPage( m_macPrintSettings
, &to
) ;
418 if ( to
>= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
420 data
->SetAllPages( true ) ;
421 // This means all pages, more or less
422 data
->SetFromPage(1);
423 data
->SetToPage(32000);
427 data
->SetFromPage( from
) ;
428 data
->SetToPage( to
) ;
429 data
->SetAllPages( false );
433 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
435 // Respect the value of m_printAllPages
436 if ( data
->GetAllPages() )
437 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , (UInt32
) kPMPrintAllPages
) ;
439 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
440 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
441 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
443 if (data
->GetAllPages() || data
->GetFromPage() == 0)
444 PMSetLastPage( m_macPrintSettings
, (UInt32
) kPMPrintAllPages
, true ) ;
446 PMSetLastPage( m_macPrintSettings
, (UInt32
) data
->GetToPage() , false ) ;
453 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
458 wxMacPrinter::~wxMacPrinter(void)
462 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
465 sm_abortWindow
= NULL
;
470 printout
->SetIsPreview(false);
471 if (m_printDialogData
.GetMinPage() < 1)
472 m_printDialogData
.SetMinPage(1);
473 if (m_printDialogData
.GetMaxPage() < 1)
474 m_printDialogData
.SetMaxPage(9999);
476 // Create a suitable device context
477 wxPrinterDC
*dc
= NULL
;
480 wxMacPrintDialog
dialog(parent
, & m_printDialogData
);
481 if (dialog
.ShowModal() == wxID_OK
)
483 dc
= wxDynamicCast(dialog
.GetPrintDC(), wxPrinterDC
);
485 m_printDialogData
= dialog
.GetPrintDialogData();
490 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
493 // May have pressed cancel.
494 if (!dc
|| !dc
->IsOk())
500 // on the mac we have always pixels as addressing mode with 72 dpi
501 printout
->SetPPIScreen(72, 72);
503 wxMacCarbonPrintData
* nativeData
= (wxMacCarbonPrintData
*)
504 (m_printDialogData
.GetPrintData().GetNativeData());
506 PMGetResolution((PMPageFormat
) (nativeData
->m_macPageFormat
), &res
);
509 PMSessionGetCurrentPrinter(nativeData
->m_macPrintSession
, &printer
);
510 PMPrinterGetOutputResolution( printer
, nativeData
->m_macPrintSettings
, &res
) ;
512 printout
->SetPPIPrinter(int(res
.hRes
), int(res
.vRes
));
514 // Set printout parameters
519 printout
->SetPageSizePixels((int)w
, (int)h
);
520 printout
->SetPaperRectPixels(dc
->GetPaperRect());
522 dc
->GetSizeMM(&mw
, &mh
);
523 printout
->SetPageSizeMM((int)mw
, (int)mh
);
525 // Create an abort window
528 printout
->OnPreparePrinting();
530 // Get some parameters from the printout, if defined
531 int fromPage
, toPage
;
532 int minPage
, maxPage
;
533 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
541 // Only set min and max, because from and to have been
543 m_printDialogData
.SetMinPage(minPage
);
544 m_printDialogData
.SetMaxPage(maxPage
);
546 printout
->OnBeginPrinting();
548 bool keepGoing
= true;
550 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
553 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
557 for (pn
= m_printDialogData
.GetFromPage();
558 keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
569 keepGoing
= printout
->OnPrintPage(pn
);
573 printout
->OnEndDocument();
575 printout
->OnEndPrinting();
579 sm_abortWindow
->Show(false);
580 delete sm_abortWindow
;
581 sm_abortWindow
= NULL
;
591 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
593 wxDC
* dc
= (wxDC
*) NULL
;
595 wxPrintDialog
dialog(parent
, & m_printDialogData
);
596 int ret
= dialog
.ShowModal();
600 dc
= dialog
.GetPrintDC();
601 m_printDialogData
= dialog
.GetPrintDialogData();
607 bool wxMacPrinter::Setup(wxWindow
*WXUNUSED(parent
))
610 wxPrintDialog
dialog(parent
, & m_printDialogData
);
611 dialog
.GetPrintDialogData().SetSetupDialog(true);
613 int ret
= dialog
.ShowModal();
616 m_printDialogData
= dialog
.GetPrintDialogData();
618 return (ret
== wxID_OK
);
628 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
629 wxPrintout
*printoutForPrinting
,
630 wxPrintDialogData
*data
)
631 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
636 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
637 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
642 wxMacPrintPreview::~wxMacPrintPreview(void)
646 bool wxMacPrintPreview::Print(bool interactive
)
648 if (!m_printPrintout
)
651 wxMacPrinter
printer(&m_printDialogData
);
652 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
655 void wxMacPrintPreview::DetermineScaling(void)
657 int screenWidth
, screenHeight
;
658 wxDisplaySize( &screenWidth
, &screenHeight
) ;
660 wxSize
ppiScreen( 72 , 72 ) ;
661 wxSize
ppiPrinter( 72 , 72 ) ;
663 // Note that with Leopard, screen dpi=72 is no longer a given
664 m_previewPrintout
->SetPPIScreen( ppiScreen
.x
, ppiScreen
.y
) ;
670 // Get a device context for the currently selected printer
671 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
672 if (printerDC
.IsOk())
674 printerDC
.GetSizeMM(&ww
, &hh
);
675 printerDC
.GetSize( &w
, &h
) ;
676 ppiPrinter
= printerDC
.GetPPI() ;
677 paperRect
= printerDC
.GetPaperRect();
685 ww
= (wxCoord
) (w
* 25.4 / ppiPrinter
.x
) ;
686 hh
= (wxCoord
) (h
* 25.4 / ppiPrinter
.y
) ;
687 paperRect
= wxRect(0, 0, w
, h
);
693 m_previewPrintout
->SetPageSizePixels(w
, h
) ;
694 m_previewPrintout
->SetPageSizeMM(ww
, hh
);
695 m_previewPrintout
->SetPaperRectPixels(paperRect
);
696 m_previewPrintout
->SetPPIPrinter( ppiPrinter
.x
, ppiPrinter
.y
) ;
698 m_previewScaleX
= float(ppiScreen
.x
) / ppiPrinter
.x
;
699 m_previewScaleY
= float(ppiScreen
.y
) / ppiPrinter
.y
;