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 if (m_printDialogData
.GetMinPage() < 1)
553 m_printDialogData
.SetMinPage(1);
554 if (m_printDialogData
.GetMaxPage() < 1)
555 m_printDialogData
.SetMaxPage(9999);
557 // Create a suitable device context
558 wxPrinterDC
*dc
= NULL
;
561 wxMacPrintDialog
dialog(parent
, & m_printDialogData
);
562 if (dialog
.ShowModal() == wxID_OK
)
564 dc
= wxDynamicCast(dialog
.GetPrintDC(), wxPrinterDC
);
566 m_printDialogData
= dialog
.GetPrintDialogData();
571 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
574 // May have pressed cancel.
575 if (!dc
|| !dc
->IsOk())
581 // on the mac we have always pixels as addressing mode with 72 dpi
582 printout
->SetPPIScreen(72, 72);
586 wxOSXPrintData
* nativeData
= (wxOSXPrintData
*)
587 (m_printDialogData
.GetPrintData().GetNativeData());
589 if (PMSessionGetCurrentPrinter(nativeData
->GetPrintSession(), &printer
) == noErr
)
591 if (PMPrinterGetOutputResolution( printer
, nativeData
->GetPrintSettings(), &res
) == -9589 /* kPMKeyNotFound */ )
593 res
.hRes
= res
.vRes
= 300;
599 res
.hRes
= res
.vRes
= 300;
601 printout
->SetPPIPrinter(int(res
.hRes
), int(res
.vRes
));
603 // Set printout parameters
608 printout
->SetPageSizePixels((int)w
, (int)h
);
609 printout
->SetPaperRectPixels(dc
->GetPaperRect());
611 dc
->GetSizeMM(&mw
, &mh
);
612 printout
->SetPageSizeMM((int)mw
, (int)mh
);
614 // Create an abort window
617 printout
->OnPreparePrinting();
619 // Get some parameters from the printout, if defined
620 int fromPage
, toPage
;
621 int minPage
, maxPage
;
622 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
626 sm_lastError
= wxPRINTER_ERROR
;
630 // Only set min and max, because from and to will be
632 m_printDialogData
.SetMinPage(minPage
);
633 m_printDialogData
.SetMaxPage(maxPage
);
635 printout
->OnBeginPrinting();
637 bool keepGoing
= true;
639 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
642 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
646 for (pn
= m_printDialogData
.GetFromPage();
647 keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
657 keepGoing
= printout
->OnPrintPage(pn
);
661 printout
->OnEndDocument();
663 printout
->OnEndPrinting();
667 sm_abortWindow
->Show(false);
668 wxDELETE(sm_abortWindow
);
678 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
682 wxPrintDialog
dialog(parent
, & m_printDialogData
);
683 int ret
= dialog
.ShowModal();
687 dc
= dialog
.GetPrintDC();
688 m_printDialogData
= dialog
.GetPrintDialogData();
694 bool wxMacPrinter::Setup(wxWindow
*WXUNUSED(parent
))
697 wxPrintDialog
dialog(parent
, & m_printDialogData
);
698 dialog
.GetPrintDialogData().SetSetupDialog(true);
700 int ret
= dialog
.ShowModal();
703 m_printDialogData
= dialog
.GetPrintDialogData();
705 return (ret
== wxID_OK
);
715 IMPLEMENT_CLASS(wxMacPrintPreview
, wxPrintPreviewBase
)
717 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
718 wxPrintout
*printoutForPrinting
,
719 wxPrintDialogData
*data
)
720 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
725 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
726 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
731 wxMacPrintPreview::~wxMacPrintPreview(void)
735 bool wxMacPrintPreview::Print(bool interactive
)
737 if (!m_printPrintout
)
740 wxMacPrinter
printer(&m_printDialogData
);
741 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
744 void wxMacPrintPreview::DetermineScaling(void)
746 int screenWidth
, screenHeight
;
747 wxDisplaySize( &screenWidth
, &screenHeight
) ;
749 wxSize
ppiScreen( 72 , 72 ) ;
750 wxSize
ppiPrinter( 72 , 72 ) ;
752 // Note that with Leopard, screen dpi=72 is no longer a given
753 m_previewPrintout
->SetPPIScreen( ppiScreen
.x
, ppiScreen
.y
) ;
759 // Get a device context for the currently selected printer
760 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
761 if (printerDC
.IsOk())
763 printerDC
.GetSizeMM(&ww
, &hh
);
764 printerDC
.GetSize( &w
, &h
) ;
765 ppiPrinter
= printerDC
.GetPPI() ;
766 paperRect
= printerDC
.GetPaperRect();
774 ww
= (wxCoord
) (w
* 25.4 / ppiPrinter
.x
) ;
775 hh
= (wxCoord
) (h
* 25.4 / ppiPrinter
.y
) ;
776 paperRect
= wxRect(0, 0, w
, h
);
782 m_previewPrintout
->SetPageSizePixels(w
, h
) ;
783 m_previewPrintout
->SetPageSizeMM(ww
, hh
);
784 m_previewPrintout
->SetPaperRectPixels(paperRect
);
785 m_previewPrintout
->SetPPIPrinter( ppiPrinter
.x
, ppiPrinter
.y
) ;
787 m_previewScaleX
= float(ppiScreen
.x
) / ppiPrinter
.x
;
788 m_previewScaleY
= float(ppiScreen
.y
) / ppiPrinter
.y
;
792 // end of print_osx.cpp
797 IMPLEMENT_DYNAMIC_CLASS(wxOSXCarbonPrintData
, wxOSXPrintData
)
799 wxOSXCarbonPrintData::wxOSXCarbonPrintData()
801 if ( PMCreateSession( &m_macPrintSession
) == noErr
)
803 if ( PMCreatePageFormat(&m_macPageFormat
) == noErr
)
805 PMSessionDefaultPageFormat(m_macPrintSession
,
807 PMGetPageFormatPaper(m_macPageFormat
, &m_macPaper
);
810 if ( PMCreatePrintSettings(&m_macPrintSettings
) == noErr
)
812 PMSessionDefaultPrintSettings(m_macPrintSession
,
818 wxOSXCarbonPrintData::~wxOSXCarbonPrintData()
820 (void)PMRelease(m_macPageFormat
);
821 (void)PMRelease(m_macPrintSettings
);
822 (void)PMRelease(m_macPrintSession
);