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/mac/uma.h"
32 #include "wx/mac/printmac.h"
33 #include "wx/mac/private/print.h"
35 #include "wx/printdlg.h"
36 #include "wx/mac/printdlg.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData
, wxPrintNativeDataBase
)
41 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter
, wxPrinterBase
)
42 IMPLEMENT_CLASS(wxMacPrintPreview
, wxPrintPreviewBase
)
44 bool wxMacCarbonPrintData::Ok() const
46 return (m_macPageFormat
!= kPMNoPageFormat
) && (m_macPrintSettings
!= kPMNoPrintSettings
) && (m_macPrintSession
!= kPMNoReference
);
48 wxMacCarbonPrintData::wxMacCarbonPrintData()
50 m_macPageFormat
= kPMNoPageFormat
;
51 m_macPrintSettings
= kPMNoPrintSettings
;
52 m_macPrintSession
= kPMNoReference
;
56 wxMacCarbonPrintData::~wxMacCarbonPrintData()
58 if (m_macPageFormat
!= kPMNoPageFormat
)
60 (void)PMRelease(m_macPageFormat
);
61 m_macPageFormat
= kPMNoPageFormat
;
64 if (m_macPrintSettings
!= kPMNoPrintSettings
)
66 (void)PMRelease(m_macPrintSettings
);
67 m_macPrintSettings
= kPMNoPrintSettings
;
70 if ( m_macPrintSession
!= kPMNoReference
)
72 (void)PMRelease(m_macPrintSession
);
73 m_macPrintSession
= kPMNoReference
;
77 void wxMacCarbonPrintData::ValidateOrCreate()
79 OSStatus err
= noErr
;
80 if ( m_macPrintSession
== kPMNoReference
)
82 err
= PMCreateSession( (PMPrintSession
*) &m_macPrintSession
) ;
84 // Set up a valid PageFormat object.
85 if ( m_macPageFormat
== kPMNoPageFormat
)
87 err
= PMCreatePageFormat((PMPageFormat
*) &m_macPageFormat
);
89 // Note that PMPageFormat is not session-specific, but calling
90 // PMSessionDefaultPageFormat assigns values specific to the printer
91 // associated with the current printing session.
93 ( m_macPageFormat
!= kPMNoPageFormat
))
95 err
= PMSessionDefaultPageFormat((PMPrintSession
) m_macPrintSession
,
96 (PMPageFormat
) m_macPageFormat
);
101 err
= PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
102 (PMPageFormat
) m_macPageFormat
,
106 // Set up a valid PrintSettings object.
107 if ( m_macPrintSettings
== kPMNoPrintSettings
)
109 err
= PMCreatePrintSettings((PMPrintSettings
*) &m_macPrintSettings
);
111 // Note that PMPrintSettings is not session-specific, but calling
112 // PMSessionDefaultPrintSettings assigns values specific to the printer
113 // associated with the current printing session.
114 if ((err
== noErr
) &&
115 ( m_macPrintSettings
!= kPMNoPrintSettings
))
117 err
= PMSessionDefaultPrintSettings((PMPrintSession
) m_macPrintSession
,
118 (PMPrintSettings
) m_macPrintSettings
);
123 err
= PMSessionValidatePrintSettings((PMPrintSession
) m_macPrintSession
,
124 (PMPrintSettings
) m_macPrintSettings
,
129 bool wxMacCarbonPrintData::TransferFrom( const wxPrintData
&data
)
132 PMSetCopies( (PMPrintSettings
) m_macPrintSettings
, data
.GetNoCopies() , false ) ;
133 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
134 kPMLandscape
: kPMPortrait
, false ) ;
135 // collate cannot be set
136 #if 0 // not yet tested
137 if ( !m_printerName
.empty() )
138 PMSessionSetCurrentPrinter( (PMPrintSession
) m_macPrintSession
, wxMacCFStringHolder( m_printerName
, wxFont::GetDefaultEncoding() ) ) ;
141 PMGetColorMode( (PMPrintSettings
) m_macPrintSettings
, &color
) ;
142 if ( data
.GetColour() )
144 if ( color
== kPMBlackAndWhite
)
145 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMColor
) ;
148 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMBlackAndWhite
) ;
150 // PMDuplexMode not yet accessible via API
151 // PMQualityMode not yet accessible via API
155 PMTag tag
= kPMMaxSquareResolution
;
156 PMSessionGetCurrentPrinter(m_macPrintSession
, &printer
);
157 PMPrinterGetPrinterResolution(printer
, tag
, &res
);
158 PMSetResolution((PMPageFormat
) m_macPageFormat
, &res
);
159 // after setting the new resolution the format has to be updated, otherwise the page rect remains
160 // at the 'old' scaling
161 PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
162 (PMPageFormat
) m_macPageFormat
,
163 kPMDontWantBoolean
) ;
168 bool wxMacCarbonPrintData::TransferTo( wxPrintData
&data
)
170 OSStatus err
= noErr
;
173 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
175 data
.SetNoCopies( copies
) ;
177 PMOrientation orientation
;
178 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
181 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
182 data
.SetOrientation( wxPORTRAIT
);
184 data
.SetOrientation( wxLANDSCAPE
);
187 // collate cannot be set
190 wxMacCFStringHolder name
;
192 PMSessionGetCurrentPrinter( m_macPrintSession
,
194 m_printerName
= name
.AsString() ;
199 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
201 data
.SetColour( !(color
== kPMBlackAndWhite
) ) ;
203 // PMDuplexMode not yet accessible via API
204 // PMQualityMode not yet accessible via API
207 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
210 data
.SetPaperSize( wxSize (
211 (int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
212 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) ) );
217 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*data
)
219 // should we setup the page rect here ?
220 // since MacOS sometimes has two same paper rects with different
221 // page rects we could make it roundtrip safe perhaps
224 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
227 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
231 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
234 data
->SetMinMarginTopLeft( wxPoint (
235 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
236 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
238 data
->SetMinMarginBottomRight( wxPoint (
239 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
240 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
242 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
243 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
244 data
->GetMarginTopLeft().y
) ) ;
246 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
247 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
248 data
->GetMarginBottomRight().y
) );
250 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
251 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
253 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
254 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
255 data
->GetMinMarginBottomRight().y
) );
260 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
262 UInt32 minPage
, maxPage
;
263 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
264 data
->SetMinPage( minPage
) ;
265 data
->SetMaxPage( maxPage
) ;
267 PMGetCopies( m_macPrintSettings
, &copies
) ;
268 data
->SetNoCopies( copies
) ;
270 PMGetFirstPage( m_macPrintSettings
, &from
) ;
271 PMGetLastPage( m_macPrintSettings
, &to
) ;
272 if ( to
>= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
274 data
->SetAllPages( true ) ;
275 // This means all pages, more or less
276 data
->SetFromPage(1);
277 data
->SetToPage(32000);
281 data
->SetFromPage( from
) ;
282 data
->SetToPage( to
) ;
283 data
->SetAllPages( false );
287 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
289 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
290 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
291 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
293 if (data
->GetAllPages() || data
->GetFromPage() == 0)
294 PMSetLastPage( m_macPrintSettings
, (UInt32
) kPMPrintAllPages
, true ) ;
296 PMSetLastPage( m_macPrintSettings
, (UInt32
) data
->GetToPage() , false ) ;
303 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
308 wxMacPrinter::~wxMacPrinter(void)
312 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
315 sm_abortWindow
= NULL
;
320 printout
->SetIsPreview(false);
321 if (m_printDialogData
.GetMinPage() < 1)
322 m_printDialogData
.SetMinPage(1);
323 if (m_printDialogData
.GetMaxPage() < 1)
324 m_printDialogData
.SetMaxPage(9999);
326 // Create a suitable device context
330 wxPrintDialog
dialog(parent
, & m_printDialogData
);
331 if (dialog
.ShowModal() == wxID_OK
)
333 dc
= dialog
.GetPrintDC();
334 m_printDialogData
= dialog
.GetPrintDialogData();
339 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
342 // May have pressed cancel.
343 if (!dc
|| !dc
->Ok())
350 // on the mac we have always pixels as addressing mode with 72 dpi
351 printout
->SetPPIScreen(72, 72);
353 wxMacCarbonPrintData
* nativeData
= (wxMacCarbonPrintData
*)
354 (m_printDialogData
.GetPrintData().GetNativeData());
355 PMGetResolution((PMPageFormat
) (nativeData
->m_macPageFormat
), &res
);
356 printout
->SetPPIPrinter(int(res
.hRes
), int(res
.vRes
));
358 // Set printout parameters
364 printout
->SetPageSizePixels((int)w
, (int)h
);
365 dc
->GetSizeMM(&ww
, &hh
);
366 printout
->SetPageSizeMM((int)ww
, (int)hh
);
368 // Create an abort window
371 printout
->OnPreparePrinting();
373 // Get some parameters from the printout, if defined
374 int fromPage
, toPage
;
375 int minPage
, maxPage
;
376 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
384 // Only set min and max, because from and to have been
386 m_printDialogData
.SetMinPage(minPage
);
387 m_printDialogData
.SetMaxPage(maxPage
);
389 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
390 wxSafeYield(win
,true);
395 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
401 sm_abortWindow
= win
;
402 sm_abortWindow
->Show(true);
403 wxSafeYield(win
,true);
405 printout
->OnBeginPrinting();
407 bool keepGoing
= true;
410 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
412 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
415 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
422 for (pn
= m_printDialogData
.GetFromPage();
423 keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
434 if ( UMAGetSystemVersion() >= 0x1000 )
438 GetPort( &thePort
) ;
439 wxSafeYield(win
,true);
443 keepGoing
= printout
->OnPrintPage(pn
);
447 printout
->OnEndDocument();
450 printout
->OnEndPrinting();
454 sm_abortWindow
->Show(false);
455 delete sm_abortWindow
;
456 sm_abortWindow
= NULL
;
466 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
468 wxDC
* dc
= (wxDC
*) NULL
;
470 wxPrintDialog
dialog(parent
, & m_printDialogData
);
471 int ret
= dialog
.ShowModal();
475 dc
= dialog
.GetPrintDC();
476 m_printDialogData
= dialog
.GetPrintDialogData();
482 bool wxMacPrinter::Setup(wxWindow
*parent
)
485 wxPrintDialog
dialog(parent
, & m_printDialogData
);
486 dialog
.GetPrintDialogData().SetSetupDialog(true);
488 int ret
= dialog
.ShowModal();
491 m_printDialogData
= dialog
.GetPrintDialogData();
493 return (ret
== wxID_OK
);
503 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
504 wxPrintout
*printoutForPrinting
,
505 wxPrintDialogData
*data
)
506 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
511 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
512 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
517 wxMacPrintPreview::~wxMacPrintPreview(void)
521 bool wxMacPrintPreview::Print(bool interactive
)
523 if (!m_printPrintout
)
526 wxMacPrinter
printer(&m_printDialogData
);
527 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
530 void wxMacPrintPreview::DetermineScaling(void)
532 int screenWidth
, screenHeight
;
533 wxDisplaySize( &screenWidth
, &screenHeight
) ;
535 wxSize
ppiScreen( 72 , 72 ) ;
536 wxSize
ppiPrinter( 72 , 72 ) ;
538 m_previewPrintout
->SetPPIScreen( ppiScreen
.x
, ppiScreen
.y
) ;
543 // Get a device context for the currently selected printer
544 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
547 printerDC
.GetSizeMM(&ww
, &hh
);
548 printerDC
.GetSize( &x
, &y
) ;
549 ppiPrinter
= printerDC
.GetPPI() ;
557 ww
= (int) (x
* 25.4 / ppiPrinter
.x
) ;
558 hh
= (int) (y
* 25.4 / ppiPrinter
.y
) ;
561 m_previewPrintout
->SetPageSizeMM((int)ww
, (int)hh
);
562 m_previewPrintout
->SetPageSizePixels( x
, y
) ;
565 m_previewPrintout
->SetPPIPrinter( ppiPrinter
.x
, ppiPrinter
.y
) ;
567 m_previewScale
= (float)((float)ppiScreen
.x
/(float)ppiPrinter
.y
);