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/osx/uma.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
)
133 PMSetCopies( (PMPrintSettings
) m_macPrintSettings
, data
.GetNoCopies() , false ) ;
134 if ( data
.IsOrientationReversed() )
135 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
136 kPMReverseLandscape
: kPMReversePortrait
, false ) ;
138 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
139 kPMLandscape
: kPMPortrait
, false ) ;
140 // collate cannot be set
141 #if 0 // not yet tested
142 if ( !m_printerName
.empty() )
143 PMSessionSetCurrentPrinter( (PMPrintSession
) m_macPrintSession
, wxCFStringRef( m_printerName
, wxFont::GetDefaultEncoding() ) ) ;
147 PMGetColorMode( (PMPrintSettings
) m_macPrintSettings
, &color
) ;
148 if ( data
.GetColour() )
150 if ( color
== kPMBlackAndWhite
)
151 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMColor
) ;
154 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMBlackAndWhite
) ;
157 PMDuplexMode mode
= 0 ;
158 switch( data
.GetDuplex() )
160 case wxDUPLEX_HORIZONTAL
:
161 mode
= kPMDuplexNoTumble
;
163 case wxDUPLEX_VERTICAL
:
164 mode
= kPMDuplexTumble
;
166 case wxDUPLEX_SIMPLEX
:
168 mode
= kPMDuplexNone
;
171 PMSetDuplex( (PMPrintSettings
) m_macPrintSettings
, mode
) ;
173 // PMQualityMode not yet accessible via API
178 PMSessionGetCurrentPrinter(m_macPrintSession
, &printer
);
181 PMTag tag
= kPMMaxSquareResolution
;
182 PMPrinterGetPrinterResolution(printer
, tag
, &res
);
183 PMSetResolution((PMPageFormat
) m_macPageFormat
, &res
);
185 PMPrinterGetOutputResolution( printer
,
186 (PMPrintSettings
) m_macPrintSettings
, &res
) ;
187 // TODO transfer ? into page format ?
190 // after setting the new resolution the format has to be updated, otherwise the page rect remains
191 // at the 'old' scaling
192 PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
193 (PMPageFormat
) m_macPageFormat
,
194 kPMDontWantBoolean
) ;
199 bool wxMacCarbonPrintData::TransferTo( wxPrintData
&data
)
201 OSStatus err
= noErr
;
204 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
206 data
.SetNoCopies( copies
) ;
208 PMOrientation orientation
;
209 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
212 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
214 data
.SetOrientation( wxPORTRAIT
);
215 data
.SetOrientationReversed( orientation
== kPMReversePortrait
);
219 data
.SetOrientation( wxLANDSCAPE
);
220 data
.SetOrientationReversed( orientation
== kPMReverseLandscape
);
224 // collate cannot be set
229 PMSessionGetCurrentPrinter( m_macPrintSession
,
231 m_printerName
= name
.AsString() ;
237 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
239 data
.SetColour( !(color
== kPMBlackAndWhite
) ) ;
241 PMDuplexMode mode
= 0 ;
242 PMGetDuplex( (PMPrintSettings
) m_macPrintSettings
, &mode
) ;
245 case kPMDuplexNoTumble
:
246 data
.SetDuplex(wxDUPLEX_HORIZONTAL
);
248 case kPMDuplexTumble
:
249 data
.SetDuplex(wxDUPLEX_VERTICAL
);
253 data
.SetDuplex(wxDUPLEX_SIMPLEX
);
256 // PMQualityMode not yet accessible via API
259 PMGetPageFormatPaper( m_macPageFormat
, &paper
);
262 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
265 wxSize
sz((int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
266 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ));
267 data
.SetPaperSize(sz
);
268 wxPaperSize id
= wxThePrintPaperDatabase
->GetSize(wxSize(sz
.x
* 10, sz
.y
* 10));
269 if (id
!= wxPAPER_NONE
)
277 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*WXUNUSED(data
) )
279 // should we setup the page rect here ?
280 // since MacOS sometimes has two same paper rects with different
281 // page rects we could make it roundtrip safe perhaps
284 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
287 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
290 wxSize
sz((int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
291 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ));
292 data
->SetPaperSize(sz
);
295 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
298 data
->SetMinMarginTopLeft( wxPoint (
299 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
300 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
302 data
->SetMinMarginBottomRight( wxPoint (
303 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
304 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
306 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
307 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
308 data
->GetMarginTopLeft().y
) ) ;
310 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
311 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
312 data
->GetMarginBottomRight().y
) );
314 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
315 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
317 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
318 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
319 data
->GetMinMarginBottomRight().y
) );
324 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
326 UInt32 minPage
, maxPage
;
327 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
328 data
->SetMinPage( minPage
) ;
329 data
->SetMaxPage( maxPage
) ;
331 PMGetCopies( m_macPrintSettings
, &copies
) ;
332 data
->SetNoCopies( copies
) ;
334 PMGetFirstPage( m_macPrintSettings
, &from
) ;
335 PMGetLastPage( m_macPrintSettings
, &to
) ;
336 if ( to
>= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
338 data
->SetAllPages( true ) ;
339 // This means all pages, more or less
340 data
->SetFromPage(1);
341 data
->SetToPage(32000);
345 data
->SetFromPage( from
) ;
346 data
->SetToPage( to
) ;
347 data
->SetAllPages( false );
351 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
353 // Respect the value of m_printAllPages
354 if ( data
->GetAllPages() )
355 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , (UInt32
) kPMPrintAllPages
) ;
357 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
358 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
359 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
361 if (data
->GetAllPages() || data
->GetFromPage() == 0)
362 PMSetLastPage( m_macPrintSettings
, (UInt32
) kPMPrintAllPages
, true ) ;
364 PMSetLastPage( m_macPrintSettings
, (UInt32
) data
->GetToPage() , false ) ;
371 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
376 wxMacPrinter::~wxMacPrinter(void)
380 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
383 sm_abortWindow
= NULL
;
388 printout
->SetIsPreview(false);
389 if (m_printDialogData
.GetMinPage() < 1)
390 m_printDialogData
.SetMinPage(1);
391 if (m_printDialogData
.GetMaxPage() < 1)
392 m_printDialogData
.SetMaxPage(9999);
394 // Create a suitable device context
395 wxPrinterDC
*dc
= NULL
;
398 wxMacPrintDialog
dialog(parent
, & m_printDialogData
);
399 if (dialog
.ShowModal() == wxID_OK
)
401 dc
= wxDynamicCast(dialog
.GetPrintDC(), wxPrinterDC
);
403 m_printDialogData
= dialog
.GetPrintDialogData();
408 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
411 // May have pressed cancel.
412 if (!dc
|| !dc
->IsOk())
419 // on the mac we have always pixels as addressing mode with 72 dpi
420 printout
->SetPPIScreen(72, 72);
422 wxMacCarbonPrintData
* nativeData
= (wxMacCarbonPrintData
*)
423 (m_printDialogData
.GetPrintData().GetNativeData());
425 PMGetResolution((PMPageFormat
) (nativeData
->m_macPageFormat
), &res
);
428 PMSessionGetCurrentPrinter(nativeData
->m_macPrintSession
, &printer
);
429 PMPrinterGetOutputResolution( printer
, nativeData
->m_macPrintSettings
, &res
) ;
431 printout
->SetPPIPrinter(int(res
.hRes
), int(res
.vRes
));
433 // Set printout parameters
438 printout
->SetPageSizePixels((int)w
, (int)h
);
439 printout
->SetPaperRectPixels(dc
->GetPaperRect());
441 dc
->GetSizeMM(&mw
, &mh
);
442 printout
->SetPageSizeMM((int)mw
, (int)mh
);
444 // Create an abort window
447 printout
->OnPreparePrinting();
449 // Get some parameters from the printout, if defined
450 int fromPage
, toPage
;
451 int minPage
, maxPage
;
452 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
460 // Only set min and max, because from and to have been
462 m_printDialogData
.SetMinPage(minPage
);
463 m_printDialogData
.SetMaxPage(maxPage
);
465 printout
->OnBeginPrinting();
467 bool keepGoing
= true;
469 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
472 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
476 for (pn
= m_printDialogData
.GetFromPage();
477 keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
488 keepGoing
= printout
->OnPrintPage(pn
);
492 printout
->OnEndDocument();
494 printout
->OnEndPrinting();
498 sm_abortWindow
->Show(false);
499 delete sm_abortWindow
;
500 sm_abortWindow
= NULL
;
510 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
512 wxDC
* dc
= (wxDC
*) NULL
;
514 wxPrintDialog
dialog(parent
, & m_printDialogData
);
515 int ret
= dialog
.ShowModal();
519 dc
= dialog
.GetPrintDC();
520 m_printDialogData
= dialog
.GetPrintDialogData();
526 bool wxMacPrinter::Setup(wxWindow
*WXUNUSED(parent
))
529 wxPrintDialog
dialog(parent
, & m_printDialogData
);
530 dialog
.GetPrintDialogData().SetSetupDialog(true);
532 int ret
= dialog
.ShowModal();
535 m_printDialogData
= dialog
.GetPrintDialogData();
537 return (ret
== wxID_OK
);
547 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
548 wxPrintout
*printoutForPrinting
,
549 wxPrintDialogData
*data
)
550 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
555 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
556 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
561 wxMacPrintPreview::~wxMacPrintPreview(void)
565 bool wxMacPrintPreview::Print(bool interactive
)
567 if (!m_printPrintout
)
570 wxMacPrinter
printer(&m_printDialogData
);
571 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
574 void wxMacPrintPreview::DetermineScaling(void)
576 int screenWidth
, screenHeight
;
577 wxDisplaySize( &screenWidth
, &screenHeight
) ;
579 wxSize
ppiScreen( 72 , 72 ) ;
580 wxSize
ppiPrinter( 72 , 72 ) ;
582 // Note that with Leopard, screen dpi=72 is no longer a given
583 m_previewPrintout
->SetPPIScreen( ppiScreen
.x
, ppiScreen
.y
) ;
589 // Get a device context for the currently selected printer
590 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
591 if (printerDC
.IsOk())
593 printerDC
.GetSizeMM(&ww
, &hh
);
594 printerDC
.GetSize( &w
, &h
) ;
595 ppiPrinter
= printerDC
.GetPPI() ;
596 paperRect
= printerDC
.GetPaperRect();
604 ww
= (wxCoord
) (w
* 25.4 / ppiPrinter
.x
) ;
605 hh
= (wxCoord
) (h
* 25.4 / ppiPrinter
.y
) ;
606 paperRect
= wxRect(0, 0, w
, h
);
612 m_previewPrintout
->SetPageSizePixels(w
, h
) ;
613 m_previewPrintout
->SetPageSizeMM(ww
, hh
);
614 m_previewPrintout
->SetPaperRectPixels(paperRect
);
615 m_previewPrintout
->SetPPIPrinter( ppiPrinter
.x
, ppiPrinter
.y
) ;
617 m_previewScaleX
= float(ppiScreen
.x
) / ppiPrinter
.x
;
618 m_previewScaleY
= float(ppiScreen
.y
) / ppiPrinter
.y
;