1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/printmac.cpp
3 // Purpose: wxMacPrinter framework
4 // Author: Julian Smart, Stefan Csomor
7 // Copyright: (c) Julian Smart, Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #if wxUSE_PRINTING_ARCHITECTURE
24 #include "wx/msgdlg.h"
25 #include "wx/dcprint.h"
29 #include "wx/osx/private.h"
31 #include "wx/osx/printmac.h"
32 #include "wx/osx/private/print.h"
34 #include "wx/printdlg.h"
36 #include "wx/osx/printdlg.h"
41 // move to print_osx.cpp
44 static int ResolutionSorter(const void *e1
, const void *e2
)
46 const PMResolution
*res1
= (const PMResolution
*)e1
;
47 const PMResolution
*res2
= (const PMResolution
*)e2
;
48 const double area1
= res1
->hRes
* res1
->vRes
;
49 const double area2
= res2
->hRes
* res2
->vRes
;
53 else if (area1
> area2
)
59 static PMResolution
*GetSupportedResolutions(PMPrinter printer
, UInt32
*count
)
61 PMResolution res
, *resolutions
= NULL
;
62 OSStatus status
= PMPrinterGetPrinterResolutionCount(printer
, count
);
65 resolutions
= (PMResolution
*)malloc(sizeof(PMResolution
) * (*count
));
67 for (UInt32 i
= 0; i
< *count
; i
++)
69 if (PMPrinterGetIndexedPrinterResolution(printer
, i
+ 1, &res
) == noErr
)
70 resolutions
[realCount
++] = res
;
72 qsort(resolutions
, realCount
, sizeof(PMResolution
), ResolutionSorter
);
76 if ((*count
== 0) && (resolutions
))
86 IMPLEMENT_DYNAMIC_CLASS(wxOSXPrintData
, wxPrintNativeDataBase
)
88 bool wxOSXPrintData::IsOk() const
90 return (m_macPageFormat
!= kPMNoPageFormat
) && (m_macPrintSettings
!= kPMNoPrintSettings
) && (m_macPrintSession
!= kPMNoReference
);
93 wxOSXPrintData::wxOSXPrintData()
95 m_macPageFormat
= kPMNoPageFormat
;
96 m_macPrintSettings
= kPMNoPrintSettings
;
97 m_macPrintSession
= kPMNoReference
;
98 m_macPaper
= kPMNoData
;
101 wxOSXPrintData::~wxOSXPrintData()
105 void wxOSXPrintData::UpdateFromPMState()
109 void wxOSXPrintData::UpdateToPMState()
113 void wxOSXPrintData::TransferPrinterNameFrom( const wxPrintData
&data
)
115 CFArrayRef printerList
;
116 CFIndex index
, count
;
119 if (PMServerCreatePrinterList(kPMServerLocal
, &printerList
) == noErr
)
121 PMPrinter printer
= NULL
;
122 count
= CFArrayGetCount(printerList
);
123 for (index
= 0; index
< count
; index
++)
125 printer
= (PMPrinter
)CFArrayGetValueAtIndex(printerList
, index
);
126 if ((data
.GetPrinterName().empty()) && (PMPrinterIsDefault(printer
)))
130 name
= PMPrinterGetName(printer
);
132 if (data
.GetPrinterName() == wxCFStringRef(name
).AsString())
137 PMSessionSetCurrentPMPrinter(m_macPrintSession
, printer
);
138 CFRelease(printerList
);
142 void wxOSXPrintData::TransferPaperInfoFrom( const wxPrintData
&data
)
145 PMSessionGetCurrentPrinter(m_macPrintSession
, &printer
);
147 wxSize papersize
= wxDefaultSize
;
148 const wxPaperSize paperId
= data
.GetPaperId();
149 if ( paperId
!= wxPAPER_NONE
&& wxThePrintPaperDatabase
)
151 papersize
= wxThePrintPaperDatabase
->GetSize(paperId
);
152 if ( papersize
!= wxDefaultSize
)
160 papersize
= data
.GetPaperSize();
163 if ( papersize
!= wxDefaultSize
)
165 papersize
.x
= (wxInt32
) (papersize
.x
* mm2pt
);
166 papersize
.y
= (wxInt32
) (papersize
.y
* mm2pt
);
168 double height
, width
;
169 PMPaperGetHeight(m_macPaper
, &height
);
170 PMPaperGetWidth(m_macPaper
, &width
);
172 if ( fabs( width
- papersize
.x
) >= 5 ||
173 fabs( height
- papersize
.y
) >= 5 )
175 // we have to change the current paper
176 CFArrayRef paperlist
= 0 ;
177 if ( PMPrinterGetPaperList( printer
, &paperlist
) == noErr
)
179 PMPaper bestPaper
= kPMNoData
;
180 CFIndex top
= CFArrayGetCount(paperlist
);
181 for ( CFIndex i
= 0 ; i
< top
; ++ i
)
183 PMPaper paper
= (PMPaper
) CFArrayGetValueAtIndex( paperlist
, i
);
184 PMPaperGetHeight(paper
, &height
);
185 PMPaperGetWidth(paper
, &width
);
186 if ( fabs( width
- papersize
.x
) < 5 &&
187 fabs( height
- papersize
.y
) < 5 )
189 // TODO test for duplicate hits and use additional
190 // criteria for best match
194 PMPaper paper
= kPMNoData
;
195 if ( bestPaper
== kPMNoData
)
197 const PMPaperMargins margins
= { 0.0, 0.0, 0.0, 0.0 };
198 wxString id
, name(wxT("Custom paper"));
199 id
.Printf(wxT("wxPaperCustom%dx%d"), papersize
.x
, papersize
.y
);
201 PMPaperCreateCustom(printer
, wxCFStringRef( id
, wxFont::GetDefaultEncoding() ), wxCFStringRef( name
, wxFont::GetDefaultEncoding() ),
202 papersize
.x
, papersize
.y
, &margins
, &paper
);
204 if ( bestPaper
!= kPMNoData
)
206 PMPageFormat pageFormat
;
207 PMCreatePageFormatWithPMPaper(&pageFormat
, bestPaper
);
208 PMCopyPageFormat( pageFormat
, m_macPageFormat
);
209 PMRelease(pageFormat
);
210 PMGetPageFormatPaper(m_macPageFormat
, &m_macPaper
);
217 PMSetCopies( m_macPrintSettings
, data
.GetNoCopies() , false ) ;
218 PMSetCollate(m_macPrintSettings
, data
.GetCollate());
219 if ( data
.IsOrientationReversed() )
220 PMSetOrientation( m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
221 kPMReverseLandscape
: kPMReversePortrait
, false ) ;
223 PMSetOrientation( m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
224 kPMLandscape
: kPMPortrait
, false ) ;
226 PMDuplexMode mode
= 0 ;
227 switch( data
.GetDuplex() )
229 case wxDUPLEX_HORIZONTAL
:
230 mode
= kPMDuplexNoTumble
;
232 case wxDUPLEX_VERTICAL
:
233 mode
= kPMDuplexTumble
;
235 case wxDUPLEX_SIMPLEX
:
237 mode
= kPMDuplexNone
;
240 PMSetDuplex( m_macPrintSettings
, mode
) ;
243 if ( data
.IsOrientationReversed() )
244 PMSetOrientation( m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
245 kPMReverseLandscape
: kPMReversePortrait
, false ) ;
247 PMSetOrientation( m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
248 kPMLandscape
: kPMPortrait
, false ) ;
251 void wxOSXPrintData::TransferResolutionFrom( const wxPrintData
&data
)
254 PMSessionGetCurrentPrinter(m_macPrintSession
, &printer
);
257 PMResolution
*resolutions
= GetSupportedResolutions(printer
, &resCount
);
260 wxPrintQuality quality
= data
.GetQuality();
262 quality
= wxPRINT_QUALITY_HIGH
;
264 PMResolution res
= resolutions
[((quality
- wxPRINT_QUALITY_DRAFT
) * (resCount
- 1)) / 3];
265 PMPrinterSetOutputResolution(printer
, m_macPrintSettings
, &res
);
271 bool wxOSXPrintData::TransferFrom( const wxPrintData
&data
)
273 TransferPrinterNameFrom(data
);
274 TransferPaperInfoFrom(data
);
275 TransferResolutionFrom(data
);
277 // after setting the new resolution the format has to be updated, otherwise the page rect remains
278 // at the 'old' scaling
280 PMSessionValidatePageFormat(m_macPrintSession
,
281 m_macPageFormat
, kPMDontWantBoolean
);
282 PMSessionValidatePrintSettings(m_macPrintSession
,
283 m_macPrintSettings
, kPMDontWantBoolean
);
292 void wxOSXPrintData::TransferPrinterNameTo( wxPrintData
&data
)
296 PMSessionGetCurrentPrinter( m_macPrintSession
, &printer
);
297 if (PMPrinterIsDefault(printer
))
298 data
.SetPrinterName(wxEmptyString
);
301 name
= PMPrinterGetName(printer
);
303 data
.SetPrinterName(wxCFStringRef(name
).AsString());
307 void wxOSXPrintData::TransferPaperInfoTo( wxPrintData
&data
)
309 PMGetPageFormatPaper(m_macPageFormat
, &m_macPaper
);
312 PMSessionGetCurrentPrinter( m_macPrintSession
, &printer
);
313 OSStatus err
= noErr
;
315 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
317 data
.SetNoCopies( copies
) ;
319 PMOrientation orientation
;
320 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
323 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
325 data
.SetOrientation( wxPORTRAIT
);
326 data
.SetOrientationReversed( orientation
== kPMReversePortrait
);
330 data
.SetOrientation( wxLANDSCAPE
);
331 data
.SetOrientationReversed( orientation
== kPMReverseLandscape
);
336 if (PMGetCollate(m_macPrintSettings
, &collate
) == noErr
)
337 data
.SetCollate(collate
);
340 PMDuplexMode mode
= 0 ;
341 PMGetDuplex( m_macPrintSettings
, &mode
) ;
344 case kPMDuplexNoTumble
:
345 data
.SetDuplex(wxDUPLEX_HORIZONTAL
);
347 case kPMDuplexTumble
:
348 data
.SetDuplex(wxDUPLEX_VERTICAL
);
352 data
.SetDuplex(wxDUPLEX_SIMPLEX
);
356 double height
, width
;
357 PMPaperGetHeight(m_macPaper
, &height
);
358 PMPaperGetWidth(m_macPaper
, &width
);
360 wxSize
sz((int)(width
* pt2mm
+ 0.5 ) ,
361 (int)(height
* pt2mm
+ 0.5 ));
362 data
.SetPaperSize(sz
);
363 wxPaperSize id
= wxThePrintPaperDatabase
->GetSize(wxSize(sz
.x
* 10, sz
.y
* 10));
364 if (id
!= wxPAPER_NONE
)
370 void wxOSXPrintData::TransferResolutionTo( wxPrintData
&data
)
373 PMSessionGetCurrentPrinter( m_macPrintSession
, &printer
);
375 /* assume high quality, will change below if we are able to */
376 data
.SetQuality(wxPRINT_QUALITY_HIGH
);
378 PMResolution
*resolutions
;
380 resolutions
= GetSupportedResolutions(printer
, &resCount
);
385 if ( PMPrinterGetOutputResolution(printer
, m_macPrintSettings
, &res
) == noErr
)
391 for (i
= 0; i
< resCount
; i
++)
393 if ((resolutions
[i
].hRes
== res
.hRes
) && (resolutions
[i
].vRes
= res
.vRes
))
397 data
.SetQuality((((i
+ 1) * 3) / resCount
) + wxPRINT_QUALITY_DRAFT
);
403 bool wxOSXPrintData::TransferTo( wxPrintData
&data
)
409 TransferPrinterNameTo(data
);
410 TransferPaperInfoTo(data
);
411 TransferResolutionTo(data
);
415 void wxOSXPrintData::TransferFrom( wxPageSetupDialogData
*WXUNUSED(data
) )
417 // should we setup the page rect here ?
418 // since MacOS sometimes has two same paper rects with different
419 // page rects we could make it roundtrip safe perhaps
422 void wxOSXPrintData::TransferTo( wxPageSetupDialogData
* data
)
428 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
431 wxSize
sz((int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
432 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ));
433 data
->SetPaperSize(sz
);
436 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
439 data
->SetMinMarginTopLeft( wxPoint (
440 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
441 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
443 data
->SetMinMarginBottomRight( wxPoint (
444 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
445 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
447 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
448 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
449 data
->GetMarginTopLeft().y
) ) ;
451 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
452 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
453 data
->GetMarginBottomRight().y
) );
455 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
456 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
458 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
459 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
460 data
->GetMinMarginBottomRight().y
) );
465 void wxOSXPrintData::TransferTo( wxPrintDialogData
* data
)
470 UInt32 minPage
, maxPage
;
471 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
472 data
->SetMinPage( minPage
) ;
473 data
->SetMaxPage( maxPage
) ;
475 PMGetCopies( m_macPrintSettings
, &copies
) ;
476 data
->SetNoCopies( copies
) ;
478 PMGetFirstPage( m_macPrintSettings
, &from
) ;
479 PMGetLastPage( m_macPrintSettings
, &to
) ;
480 if ( to
>= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
482 data
->SetAllPages( true ) ;
483 // This means all pages, more or less
484 data
->SetFromPage(1);
485 data
->SetToPage(9999);
489 data
->SetFromPage( from
) ;
490 data
->SetToPage( to
) ;
491 data
->SetAllPages( false );
495 void wxOSXPrintData::TransferFrom( wxPrintDialogData
* data
)
497 // Respect the value of m_printAllPages
498 if ( data
->GetAllPages() )
499 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , (UInt32
) kPMPrintAllPages
) ;
501 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
502 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
503 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
505 if (data
->GetAllPages() || data
->GetFromPage() == 0)
506 PMSetLastPage( m_macPrintSettings
, (UInt32
) kPMPrintAllPages
, true ) ;
508 PMSetLastPage( m_macPrintSettings
, (UInt32
) data
->GetToPage() , false ) ;
514 wxPrintNativeDataBase
* wxOSXCreatePrintData()
517 return new wxOSXCocoaPrintData();
518 #elif wxOSX_USE_CARBON
519 return new wxOSXCarbonPrintData();
529 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter
, wxPrinterBase
)
531 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
536 wxMacPrinter::~wxMacPrinter(void)
540 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
543 sm_abortWindow
= NULL
;
547 sm_lastError
= wxPRINTER_ERROR
;
551 if (m_printDialogData
.GetMinPage() < 1)
552 m_printDialogData
.SetMinPage(1);
553 if (m_printDialogData
.GetMaxPage() < 1)
554 m_printDialogData
.SetMaxPage(9999);
556 // Create a suitable device context
557 wxPrinterDC
*dc
= NULL
;
560 wxMacPrintDialog
dialog(parent
, & m_printDialogData
);
561 if (dialog
.ShowModal() == wxID_OK
)
563 dc
= wxDynamicCast(dialog
.GetPrintDC(), wxPrinterDC
);
565 m_printDialogData
= dialog
.GetPrintDialogData();
570 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
573 // May have pressed cancel.
574 if (!dc
|| !dc
->IsOk())
580 // on the mac we have always pixels as addressing mode with 72 dpi
581 printout
->SetPPIScreen(72, 72);
585 wxOSXPrintData
* nativeData
= (wxOSXPrintData
*)
586 (m_printDialogData
.GetPrintData().GetNativeData());
588 if (PMSessionGetCurrentPrinter(nativeData
->GetPrintSession(), &printer
) == noErr
)
590 if (PMPrinterGetOutputResolution( printer
, nativeData
->GetPrintSettings(), &res
) == -9589 /* kPMKeyNotFound */ )
592 res
.hRes
= res
.vRes
= 300;
598 res
.hRes
= res
.vRes
= 300;
600 printout
->SetPPIPrinter(int(res
.hRes
), int(res
.vRes
));
602 // Set printout parameters
607 printout
->SetPageSizePixels((int)w
, (int)h
);
608 printout
->SetPaperRectPixels(dc
->GetPaperRect());
610 dc
->GetSizeMM(&mw
, &mh
);
611 printout
->SetPageSizeMM((int)mw
, (int)mh
);
613 // Create an abort window
616 printout
->OnPreparePrinting();
618 // Get some parameters from the printout, if defined
619 int fromPage
, toPage
;
620 int minPage
, maxPage
;
621 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
625 sm_lastError
= wxPRINTER_ERROR
;
629 // Only set min and max, because from and to will be
631 m_printDialogData
.SetMinPage(minPage
);
632 m_printDialogData
.SetMaxPage(maxPage
);
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
);