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"
42 // move to print_osx.cpp
45 static int ResolutionSorter(const void *e1
, const void *e2
)
47 const PMResolution
*res1
= (const PMResolution
*)e1
;
48 const PMResolution
*res2
= (const PMResolution
*)e2
;
49 const double area1
= res1
->hRes
* res1
->vRes
;
50 const double area2
= res2
->hRes
* res2
->vRes
;
54 else if (area1
> area2
)
60 static PMResolution
*GetSupportedResolutions(PMPrinter printer
, UInt32
*count
)
62 PMResolution res
, *resolutions
= NULL
;
63 OSStatus status
= PMPrinterGetPrinterResolutionCount(printer
, count
);
66 resolutions
= (PMResolution
*)malloc(sizeof(PMResolution
) * (*count
));
68 for (UInt32 i
= 0; i
< *count
; i
++)
70 if (PMPrinterGetIndexedPrinterResolution(printer
, i
+ 1, &res
) == noErr
)
71 resolutions
[realCount
++] = res
;
73 qsort(resolutions
, realCount
, sizeof(PMResolution
), ResolutionSorter
);
77 if ((*count
== 0) && (resolutions
))
87 IMPLEMENT_DYNAMIC_CLASS(wxOSXPrintData
, wxPrintNativeDataBase
)
89 bool wxOSXPrintData::IsOk() const
91 return (m_macPageFormat
!= kPMNoPageFormat
) && (m_macPrintSettings
!= kPMNoPrintSettings
) && (m_macPrintSession
!= kPMNoReference
);
94 wxOSXPrintData::wxOSXPrintData()
96 m_macPageFormat
= kPMNoPageFormat
;
97 m_macPrintSettings
= kPMNoPrintSettings
;
98 m_macPrintSession
= kPMNoReference
;
99 m_macPaper
= kPMNoData
;
102 wxOSXPrintData::~wxOSXPrintData()
106 void wxOSXPrintData::UpdateFromPMState()
110 void wxOSXPrintData::UpdateToPMState()
114 void wxOSXPrintData::TransferPrinterNameFrom( const wxPrintData
&data
)
116 CFArrayRef printerList
;
117 CFIndex index
, count
;
120 if (PMServerCreatePrinterList(kPMServerLocal
, &printerList
) == noErr
)
122 PMPrinter printer
= NULL
;
123 count
= CFArrayGetCount(printerList
);
124 for (index
= 0; index
< count
; index
++)
126 printer
= (PMPrinter
)CFArrayGetValueAtIndex(printerList
, index
);
127 if ((data
.GetPrinterName().empty()) && (PMPrinterIsDefault(printer
)))
131 name
= PMPrinterGetName(printer
);
133 if (data
.GetPrinterName() == wxCFStringRef(name
).AsString())
138 PMSessionSetCurrentPMPrinter(m_macPrintSession
, printer
);
139 CFRelease(printerList
);
143 void wxOSXPrintData::TransferPaperInfoFrom( const wxPrintData
&data
)
146 PMSessionGetCurrentPrinter(m_macPrintSession
, &printer
);
148 wxSize papersize
= wxDefaultSize
;
149 const wxPaperSize paperId
= data
.GetPaperId();
150 if ( paperId
!= wxPAPER_NONE
&& wxThePrintPaperDatabase
)
152 papersize
= wxThePrintPaperDatabase
->GetSize(paperId
);
153 if ( papersize
!= wxDefaultSize
)
161 papersize
= data
.GetPaperSize();
164 if ( papersize
!= wxDefaultSize
)
166 papersize
.x
= (wxInt32
) (papersize
.x
* mm2pt
);
167 papersize
.y
= (wxInt32
) (papersize
.y
* mm2pt
);
169 double height
, width
;
170 PMPaperGetHeight(m_macPaper
, &height
);
171 PMPaperGetWidth(m_macPaper
, &width
);
173 if ( fabs( width
- papersize
.x
) >= 5 ||
174 fabs( height
- papersize
.y
) >= 5 )
176 // we have to change the current paper
177 CFArrayRef paperlist
= 0 ;
178 if ( PMPrinterGetPaperList( printer
, &paperlist
) == noErr
)
180 PMPaper bestPaper
= kPMNoData
;
181 CFIndex top
= CFArrayGetCount(paperlist
);
182 for ( CFIndex i
= 0 ; i
< top
; ++ i
)
184 PMPaper paper
= (PMPaper
) CFArrayGetValueAtIndex( paperlist
, i
);
185 PMPaperGetHeight(paper
, &height
);
186 PMPaperGetWidth(paper
, &width
);
187 if ( fabs( width
- papersize
.x
) < 5 &&
188 fabs( height
- papersize
.y
) < 5 )
190 // TODO test for duplicate hits and use additional
191 // criteria for best match
195 PMPaper paper
= kPMNoData
;
196 if ( bestPaper
== kPMNoData
)
198 const PMPaperMargins margins
= { 0.0, 0.0, 0.0, 0.0 };
199 wxString id
, name(wxT("Custom paper"));
200 id
.Printf(wxT("wxPaperCustom%dx%d"), papersize
.x
, papersize
.y
);
202 PMPaperCreateCustom(printer
, wxCFStringRef( id
, wxFont::GetDefaultEncoding() ), wxCFStringRef( name
, wxFont::GetDefaultEncoding() ),
203 papersize
.x
, papersize
.y
, &margins
, &paper
);
205 if ( bestPaper
!= kPMNoData
)
207 PMPageFormat pageFormat
;
208 PMCreatePageFormatWithPMPaper(&pageFormat
, bestPaper
);
209 PMCopyPageFormat( pageFormat
, m_macPageFormat
);
210 PMRelease(pageFormat
);
211 PMGetPageFormatPaper(m_macPageFormat
, &m_macPaper
);
218 PMSetCopies( m_macPrintSettings
, data
.GetNoCopies() , false ) ;
219 PMSetCollate(m_macPrintSettings
, data
.GetCollate());
220 if ( data
.IsOrientationReversed() )
221 PMSetOrientation( m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
222 kPMReverseLandscape
: kPMReversePortrait
, false ) ;
224 PMSetOrientation( m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
225 kPMLandscape
: kPMPortrait
, false ) ;
227 PMDuplexMode mode
= 0 ;
228 switch( data
.GetDuplex() )
230 case wxDUPLEX_HORIZONTAL
:
231 mode
= kPMDuplexNoTumble
;
233 case wxDUPLEX_VERTICAL
:
234 mode
= kPMDuplexTumble
;
236 case wxDUPLEX_SIMPLEX
:
238 mode
= kPMDuplexNone
;
241 PMSetDuplex( m_macPrintSettings
, mode
) ;
244 if ( data
.IsOrientationReversed() )
245 PMSetOrientation( m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
246 kPMReverseLandscape
: kPMReversePortrait
, false ) ;
248 PMSetOrientation( m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
249 kPMLandscape
: kPMPortrait
, false ) ;
252 void wxOSXPrintData::TransferResolutionFrom( const wxPrintData
&data
)
255 PMSessionGetCurrentPrinter(m_macPrintSession
, &printer
);
258 PMResolution
*resolutions
= GetSupportedResolutions(printer
, &resCount
);
261 wxPrintQuality quality
= data
.GetQuality();
263 quality
= wxPRINT_QUALITY_HIGH
;
265 PMResolution res
= resolutions
[((quality
- wxPRINT_QUALITY_DRAFT
) * (resCount
- 1)) / 3];
266 PMPrinterSetOutputResolution(printer
, m_macPrintSettings
, &res
);
272 bool wxOSXPrintData::TransferFrom( const wxPrintData
&data
)
274 TransferPrinterNameFrom(data
);
275 TransferPaperInfoFrom(data
);
276 TransferResolutionFrom(data
);
278 // after setting the new resolution the format has to be updated, otherwise the page rect remains
279 // at the 'old' scaling
281 PMSessionValidatePageFormat(m_macPrintSession
,
282 m_macPageFormat
, kPMDontWantBoolean
);
283 PMSessionValidatePrintSettings(m_macPrintSession
,
284 m_macPrintSettings
, kPMDontWantBoolean
);
293 void wxOSXPrintData::TransferPrinterNameTo( wxPrintData
&data
)
297 PMSessionGetCurrentPrinter( m_macPrintSession
, &printer
);
298 if (PMPrinterIsDefault(printer
))
299 data
.SetPrinterName(wxEmptyString
);
302 name
= PMPrinterGetName(printer
);
304 data
.SetPrinterName(wxCFStringRef(name
).AsString());
308 void wxOSXPrintData::TransferPaperInfoTo( wxPrintData
&data
)
310 PMGetPageFormatPaper(m_macPageFormat
, &m_macPaper
);
313 PMSessionGetCurrentPrinter( m_macPrintSession
, &printer
);
314 OSStatus err
= noErr
;
316 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
318 data
.SetNoCopies( copies
) ;
320 PMOrientation orientation
;
321 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
324 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
326 data
.SetOrientation( wxPORTRAIT
);
327 data
.SetOrientationReversed( orientation
== kPMReversePortrait
);
331 data
.SetOrientation( wxLANDSCAPE
);
332 data
.SetOrientationReversed( orientation
== kPMReverseLandscape
);
337 if (PMGetCollate(m_macPrintSettings
, &collate
) == noErr
)
338 data
.SetCollate(collate
);
341 PMDuplexMode mode
= 0 ;
342 PMGetDuplex( m_macPrintSettings
, &mode
) ;
345 case kPMDuplexNoTumble
:
346 data
.SetDuplex(wxDUPLEX_HORIZONTAL
);
348 case kPMDuplexTumble
:
349 data
.SetDuplex(wxDUPLEX_VERTICAL
);
353 data
.SetDuplex(wxDUPLEX_SIMPLEX
);
357 double height
, width
;
358 PMPaperGetHeight(m_macPaper
, &height
);
359 PMPaperGetWidth(m_macPaper
, &width
);
361 wxSize
sz((int)(width
* pt2mm
+ 0.5 ) ,
362 (int)(height
* pt2mm
+ 0.5 ));
363 data
.SetPaperSize(sz
);
364 wxPaperSize id
= wxThePrintPaperDatabase
->GetSize(wxSize(sz
.x
* 10, sz
.y
* 10));
365 if (id
!= wxPAPER_NONE
)
371 void wxOSXPrintData::TransferResolutionTo( wxPrintData
&data
)
374 PMSessionGetCurrentPrinter( m_macPrintSession
, &printer
);
376 /* assume high quality, will change below if we are able to */
377 data
.SetQuality(wxPRINT_QUALITY_HIGH
);
379 PMResolution
*resolutions
;
381 resolutions
= GetSupportedResolutions(printer
, &resCount
);
386 if ( PMPrinterGetOutputResolution(printer
, m_macPrintSettings
, &res
) == noErr
)
392 for (i
= 0; i
< resCount
; i
++)
394 if ((resolutions
[i
].hRes
== res
.hRes
) && (resolutions
[i
].vRes
= res
.vRes
))
398 data
.SetQuality((((i
+ 1) * 3) / resCount
) + wxPRINT_QUALITY_DRAFT
);
404 bool wxOSXPrintData::TransferTo( wxPrintData
&data
)
410 TransferPrinterNameTo(data
);
411 TransferPaperInfoTo(data
);
412 TransferResolutionTo(data
);
416 void wxOSXPrintData::TransferFrom( wxPageSetupDialogData
*WXUNUSED(data
) )
418 // should we setup the page rect here ?
419 // since MacOS sometimes has two same paper rects with different
420 // page rects we could make it roundtrip safe perhaps
423 void wxOSXPrintData::TransferTo( wxPageSetupDialogData
* data
)
429 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
432 wxSize
sz((int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
433 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ));
434 data
->SetPaperSize(sz
);
437 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
440 data
->SetMinMarginTopLeft( wxPoint (
441 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
442 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
444 data
->SetMinMarginBottomRight( wxPoint (
445 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
446 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
448 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
449 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
450 data
->GetMarginTopLeft().y
) ) ;
452 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
453 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
454 data
->GetMarginBottomRight().y
) );
456 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
457 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
459 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
460 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
461 data
->GetMinMarginBottomRight().y
) );
466 void wxOSXPrintData::TransferTo( wxPrintDialogData
* data
)
471 UInt32 minPage
, maxPage
;
472 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
473 data
->SetMinPage( minPage
) ;
474 data
->SetMaxPage( maxPage
) ;
476 PMGetCopies( m_macPrintSettings
, &copies
) ;
477 data
->SetNoCopies( copies
) ;
479 PMGetFirstPage( m_macPrintSettings
, &from
) ;
480 PMGetLastPage( m_macPrintSettings
, &to
) ;
481 if ( to
>= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
483 data
->SetAllPages( true ) ;
484 // This means all pages, more or less
485 data
->SetFromPage(1);
486 data
->SetToPage(9999);
490 data
->SetFromPage( from
) ;
491 data
->SetToPage( to
) ;
492 data
->SetAllPages( false );
496 void wxOSXPrintData::TransferFrom( wxPrintDialogData
* data
)
498 // Respect the value of m_printAllPages
499 if ( data
->GetAllPages() )
500 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , (UInt32
) kPMPrintAllPages
) ;
502 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
503 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
504 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
506 if (data
->GetAllPages() || data
->GetFromPage() == 0)
507 PMSetLastPage( m_macPrintSettings
, (UInt32
) kPMPrintAllPages
, true ) ;
509 PMSetLastPage( m_macPrintSettings
, (UInt32
) data
->GetToPage() , false ) ;
515 wxPrintNativeDataBase
* wxOSXCreatePrintData()
518 return new wxOSXCocoaPrintData();
519 #elif wxOSX_USE_CARBON
520 return new wxOSXCarbonPrintData();
530 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter
, wxPrinterBase
)
532 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
537 wxMacPrinter::~wxMacPrinter(void)
541 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
544 sm_abortWindow
= NULL
;
548 sm_lastError
= wxPRINTER_ERROR
;
552 printout
->OnPreparePrinting();
554 // Get some parameters from the printout, if defined
555 int fromPage
, toPage
;
556 int minPage
, maxPage
;
557 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
561 sm_lastError
= wxPRINTER_ERROR
;
565 // Only set min and max, because from and to will be
567 m_printDialogData
.SetMinPage(minPage
);
568 m_printDialogData
.SetMaxPage(maxPage
);
570 if (m_printDialogData
.GetMinPage() < 1)
571 m_printDialogData
.SetMinPage(1);
572 if (m_printDialogData
.GetMaxPage() < 1)
573 m_printDialogData
.SetMaxPage(9999);
575 // Create a suitable device context
576 wxPrinterDC
*dc
= NULL
;
579 wxMacPrintDialog
dialog(parent
, & m_printDialogData
);
580 if (dialog
.ShowModal() == wxID_OK
)
582 dc
= wxDynamicCast(dialog
.GetPrintDC(), wxPrinterDC
);
584 m_printDialogData
= dialog
.GetPrintDialogData();
589 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
592 // May have pressed cancel.
593 if (!dc
|| !dc
->IsOk())
599 // on the mac we have always pixels as addressing mode with 72 dpi
600 printout
->SetPPIScreen(72, 72);
604 wxOSXPrintData
* nativeData
= (wxOSXPrintData
*)
605 (m_printDialogData
.GetPrintData().GetNativeData());
607 if (PMSessionGetCurrentPrinter(nativeData
->GetPrintSession(), &printer
) == noErr
)
609 if (PMPrinterGetOutputResolution( printer
, nativeData
->GetPrintSettings(), &res
) == -9589 /* kPMKeyNotFound */ )
611 res
.hRes
= res
.vRes
= 300;
617 res
.hRes
= res
.vRes
= 300;
619 printout
->SetPPIPrinter(int(res
.hRes
), int(res
.vRes
));
621 // Set printout parameters
626 printout
->SetPageSizePixels((int)w
, (int)h
);
627 printout
->SetPaperRectPixels(dc
->GetPaperRect());
629 dc
->GetSizeMM(&mw
, &mh
);
630 printout
->SetPageSizeMM((int)mw
, (int)mh
);
634 printout
->OnBeginPrinting();
636 bool keepGoing
= true;
638 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
641 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
645 for (pn
= m_printDialogData
.GetFromPage();
646 keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
656 keepGoing
= printout
->OnPrintPage(pn
);
660 printout
->OnEndDocument();
662 printout
->OnEndPrinting();
666 sm_abortWindow
->Show(false);
667 wxDELETE(sm_abortWindow
);
677 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
681 wxPrintDialog
dialog(parent
, & m_printDialogData
);
682 int ret
= dialog
.ShowModal();
686 dc
= dialog
.GetPrintDC();
687 m_printDialogData
= dialog
.GetPrintDialogData();
693 bool wxMacPrinter::Setup(wxWindow
*WXUNUSED(parent
))
696 wxPrintDialog
dialog(parent
, & m_printDialogData
);
697 dialog
.GetPrintDialogData().SetSetupDialog(true);
699 int ret
= dialog
.ShowModal();
702 m_printDialogData
= dialog
.GetPrintDialogData();
704 return (ret
== wxID_OK
);
714 IMPLEMENT_CLASS(wxMacPrintPreview
, wxPrintPreviewBase
)
716 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
717 wxPrintout
*printoutForPrinting
,
718 wxPrintDialogData
*data
)
719 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
724 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
725 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
730 wxMacPrintPreview::~wxMacPrintPreview(void)
734 bool wxMacPrintPreview::Print(bool interactive
)
736 if (!m_printPrintout
)
739 wxMacPrinter
printer(&m_printDialogData
);
740 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
743 void wxMacPrintPreview::DetermineScaling(void)
745 int screenWidth
, screenHeight
;
746 wxDisplaySize( &screenWidth
, &screenHeight
) ;
748 wxSize
ppiScreen( 72 , 72 ) ;
749 wxSize
ppiPrinter( 72 , 72 ) ;
751 // Note that with Leopard, screen dpi=72 is no longer a given
752 m_previewPrintout
->SetPPIScreen( ppiScreen
.x
, ppiScreen
.y
) ;
758 // Get a device context for the currently selected printer
759 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
760 if (printerDC
.IsOk())
762 printerDC
.GetSizeMM(&ww
, &hh
);
763 printerDC
.GetSize( &w
, &h
) ;
764 ppiPrinter
= printerDC
.GetPPI() ;
765 paperRect
= printerDC
.GetPaperRect();
773 ww
= (wxCoord
) (w
* 25.4 / ppiPrinter
.x
) ;
774 hh
= (wxCoord
) (h
* 25.4 / ppiPrinter
.y
) ;
775 paperRect
= wxRect(0, 0, w
, h
);
781 m_previewPrintout
->SetPageSizePixels(w
, h
) ;
782 m_previewPrintout
->SetPageSizeMM(ww
, hh
);
783 m_previewPrintout
->SetPaperRectPixels(paperRect
);
784 m_previewPrintout
->SetPPIPrinter( ppiPrinter
.x
, ppiPrinter
.y
) ;
786 m_previewScaleX
= float(ppiScreen
.x
) / ppiPrinter
.x
;
787 m_previewScaleY
= float(ppiScreen
.y
) / ppiPrinter
.y
;
791 // end of print_osx.cpp
796 IMPLEMENT_DYNAMIC_CLASS(wxOSXCarbonPrintData
, wxOSXPrintData
)
798 wxOSXCarbonPrintData::wxOSXCarbonPrintData()
800 if ( PMCreateSession( &m_macPrintSession
) == noErr
)
802 if ( PMCreatePageFormat(&m_macPageFormat
) == noErr
)
804 PMSessionDefaultPageFormat(m_macPrintSession
,
806 PMGetPageFormatPaper(m_macPageFormat
, &m_macPaper
);
809 if ( PMCreatePrintSettings(&m_macPrintSettings
) == noErr
)
811 PMSessionDefaultPrintSettings(m_macPrintSession
,
817 wxOSXCarbonPrintData::~wxOSXCarbonPrintData()
819 (void)PMRelease(m_macPageFormat
);
820 (void)PMRelease(m_macPrintSettings
);
821 (void)PMRelease(m_macPrintSession
);