1 /////////////////////////////////////////////////////////////////////////////
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"
29 #include "wx/mac/uma.h"
31 #include "wx/mac/printmac.h"
32 #include "wx/mac/private/print.h"
34 #include "wx/dcprint.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
.Length() > 0 )
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
156 bool wxMacCarbonPrintData::TransferTo( wxPrintData
&data
)
158 OSStatus err
= noErr
;
161 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
163 data
.SetNoCopies( copies
) ;
165 PMOrientation orientation
;
166 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
169 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
170 data
.SetOrientation( wxPORTRAIT
);
172 data
.SetOrientation( wxLANDSCAPE
);
175 // collate cannot be set
178 wxMacCFStringHolder name
;
180 PMSessionGetCurrentPrinter( m_macPrintSession
,
182 m_printerName
= name
.AsString() ;
187 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
189 data
.SetColour( !(color
== kPMBlackAndWhite
) ) ;
191 // PMDuplexMode not yet accessible via API
192 // PMQualityMode not yet accessible via API
195 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
198 data
.SetPaperSize( wxSize (
199 (int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
200 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) ) );
205 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*data
)
207 // should we setup the page rect here ?
208 // since MacOS sometimes has two same paper rects with different
209 // page rects we could make it roundtrip safe perhaps
212 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
215 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
219 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
222 data
->SetMinMarginTopLeft( wxPoint (
223 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
224 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
226 data
->SetMinMarginBottomRight( wxPoint (
227 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
228 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
230 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
231 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
232 data
->GetMarginTopLeft().y
) ) ;
234 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
235 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
236 data
->GetMarginBottomRight().y
) );
238 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
239 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
241 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
242 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
243 data
->GetMinMarginBottomRight().y
) );
249 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
251 UInt32 minPage
, maxPage
;
252 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
253 data
->SetMinPage( minPage
) ;
254 data
->SetMaxPage( maxPage
) ;
256 PMGetCopies( m_macPrintSettings
, &copies
) ;
257 data
->SetNoCopies( copies
) ;
259 PMGetFirstPage( m_macPrintSettings
, &from
) ;
260 PMGetLastPage( m_macPrintSettings
, &to
) ;
261 if ( to
>= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
263 data
->SetAllPages( true ) ;
264 // This means all pages, more or less
265 data
->SetFromPage(1);
266 data
->SetToPage(32000);
270 data
->SetFromPage( from
) ;
271 data
->SetToPage( to
) ;
272 data
->SetAllPages( false );
276 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
278 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
279 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
280 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
282 if (data
->GetAllPages() || data
->GetFromPage() == 0)
284 PMSetLastPage( m_macPrintSettings
, (UInt32
) kPMPrintAllPages
, true ) ;
288 PMSetLastPage( m_macPrintSettings
, (UInt32
) data
->GetToPage() , false ) ;
296 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
301 wxMacPrinter::~wxMacPrinter(void)
305 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
308 sm_abortWindow
= NULL
;
313 printout
->SetIsPreview(FALSE
);
314 if (m_printDialogData
.GetMinPage() < 1)
315 m_printDialogData
.SetMinPage(1);
316 if (m_printDialogData
.GetMaxPage() < 1)
317 m_printDialogData
.SetMaxPage(9999);
319 // Create a suitable device context
323 wxPrintDialog
dialog(parent
, & m_printDialogData
);
324 if (dialog
.ShowModal() == wxID_OK
)
326 dc
= dialog
.GetPrintDC();
327 m_printDialogData
= dialog
.GetPrintDialogData();
332 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
336 // May have pressed cancel.
337 if (!dc
|| !dc
->Ok())
343 // on the mac we have always pixels as addressing mode with 72 dpi
345 printout
->SetPPIScreen(72, 72);
346 printout
->SetPPIPrinter(72, 72);
348 // Set printout parameters
354 printout
->SetPageSizePixels((int)w
, (int)h
);
355 dc
->GetSizeMM(&ww
, &hh
);
356 printout
->SetPageSizeMM((int)ww
, (int)hh
);
358 // Create an abort window
361 printout
->OnPreparePrinting();
363 // Get some parameters from the printout, if defined
364 int fromPage
, toPage
;
365 int minPage
, maxPage
;
366 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
374 // Only set min and max, because from and to have been
376 m_printDialogData
.SetMinPage(minPage
);
377 m_printDialogData
.SetMaxPage(maxPage
);
379 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
380 wxSafeYield(win
,true);
385 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
389 sm_abortWindow
= win
;
390 sm_abortWindow
->Show(TRUE
);
391 wxSafeYield(win
,true);
393 printout
->OnBeginPrinting();
395 bool keepGoing
= TRUE
;
398 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
400 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
403 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
410 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
421 if ( UMAGetSystemVersion() >= 0x1000 )
425 GetPort( &thePort
) ;
426 wxSafeYield(win
,true);
430 keepGoing
= printout
->OnPrintPage(pn
);
434 printout
->OnEndDocument();
437 printout
->OnEndPrinting();
441 sm_abortWindow
->Show(FALSE
);
442 delete sm_abortWindow
;
443 sm_abortWindow
= NULL
;
453 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
455 wxDC
* dc
= (wxDC
*) NULL
;
457 wxPrintDialog
dialog(parent
, & m_printDialogData
);
458 int ret
= dialog
.ShowModal();
462 dc
= dialog
.GetPrintDC();
463 m_printDialogData
= dialog
.GetPrintDialogData();
469 bool wxMacPrinter::Setup(wxWindow
*parent
)
472 wxPrintDialog
dialog(parent
, & m_printDialogData
);
473 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
475 int ret
= dialog
.ShowModal();
479 m_printDialogData
= dialog
.GetPrintDialogData();
482 return (ret
== wxID_OK
);
491 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
492 wxPrintout
*printoutForPrinting
,
493 wxPrintDialogData
*data
)
494 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
499 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
500 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
505 wxMacPrintPreview::~wxMacPrintPreview(void)
509 bool wxMacPrintPreview::Print(bool interactive
)
511 if (!m_printPrintout
)
513 wxMacPrinter
printer(&m_printDialogData
);
514 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
517 void wxMacPrintPreview::DetermineScaling(void)
519 int screenWidth
, screenHeight
;
520 wxDisplaySize( &screenWidth
, &screenHeight
) ;
522 m_previewPrintout
->SetPPIScreen( 72 , 72 ) ;
523 m_previewPrintout
->SetPPIPrinter( 72 , 72 ) ;
524 m_previewPrintout
->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
525 m_previewPrintout
->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
526 m_pageWidth
= 8 * 72 ;
527 m_pageHeight
= 11 * 72 ;
530 // Get a device context for the currently selected printer
531 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
536 printerDC
.GetSizeMM(&ww
, &hh
);
537 printerDC
.GetSize( &x
, &y
) ;
538 m_previewPrintout
->SetPageSizeMM((int)ww
, (int)hh
);
539 m_previewPrintout
->SetPageSizePixels( x
, y
) ;
548 // At 100%, the page should look about page-size on the screen.
549 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
550 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);