1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMacPrinter framework
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "printwin.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if wxUSE_PRINTING_ARCHITECTURE
29 #include "wx/msgdlg.h"
33 #include "wx/mac/uma.h"
35 #include "wx/mac/printmac.h"
36 #include "wx/mac/private/print.h"
38 #include "wx/dcprint.h"
39 #include "wx/printdlg.h"
40 #include "wx/mac/printdlg.h"
44 #if !USE_SHARED_LIBRARY
45 IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData
, wxPrintNativeDataBase
)
46 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter
, wxPrinterBase
)
47 IMPLEMENT_CLASS(wxMacPrintPreview
, wxPrintPreviewBase
)
50 bool wxMacCarbonPrintData::Ok() const
52 return (m_macPageFormat
!= kPMNoPageFormat
) && (m_macPrintSettings
!= kPMNoPrintSettings
) && (m_macPrintSession
!= kPMNoReference
);
54 wxMacCarbonPrintData::wxMacCarbonPrintData()
56 m_macPageFormat
= kPMNoPageFormat
;
57 m_macPrintSettings
= kPMNoPrintSettings
;
58 m_macPrintSession
= kPMNoReference
;
62 wxMacCarbonPrintData::~wxMacCarbonPrintData()
64 if (m_macPageFormat
!= kPMNoPageFormat
)
66 (void)PMRelease(m_macPageFormat
);
67 m_macPageFormat
= kPMNoPageFormat
;
70 if (m_macPrintSettings
!= kPMNoPrintSettings
)
72 (void)PMRelease(m_macPrintSettings
);
73 m_macPrintSettings
= kPMNoPrintSettings
;
76 if ( m_macPrintSession
!= kPMNoReference
)
78 (void)PMRelease(m_macPrintSession
);
79 m_macPrintSession
= kPMNoReference
;
83 void wxMacCarbonPrintData::ValidateOrCreate()
85 OSStatus err
= noErr
;
86 if ( m_macPrintSession
== kPMNoReference
)
88 err
= PMCreateSession( (PMPrintSession
*) &m_macPrintSession
) ;
90 // Set up a valid PageFormat object.
91 if ( m_macPageFormat
== kPMNoPageFormat
)
93 err
= PMCreatePageFormat((PMPageFormat
*) &m_macPageFormat
);
95 // Note that PMPageFormat is not session-specific, but calling
96 // PMSessionDefaultPageFormat assigns values specific to the printer
97 // associated with the current printing session.
99 ( m_macPageFormat
!= kPMNoPageFormat
))
101 err
= PMSessionDefaultPageFormat((PMPrintSession
) m_macPrintSession
,
102 (PMPageFormat
) m_macPageFormat
);
107 err
= PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
108 (PMPageFormat
) m_macPageFormat
,
112 // Set up a valid PrintSettings object.
113 if ( m_macPrintSettings
== kPMNoPrintSettings
)
115 err
= PMCreatePrintSettings((PMPrintSettings
*) &m_macPrintSettings
);
117 // Note that PMPrintSettings is not session-specific, but calling
118 // PMSessionDefaultPrintSettings assigns values specific to the printer
119 // associated with the current printing session.
120 if ((err
== noErr
) &&
121 ( m_macPrintSettings
!= kPMNoPrintSettings
))
123 err
= PMSessionDefaultPrintSettings((PMPrintSession
) m_macPrintSession
,
124 (PMPrintSettings
) m_macPrintSettings
);
129 err
= PMSessionValidatePrintSettings((PMPrintSession
) m_macPrintSession
,
130 (PMPrintSettings
) m_macPrintSettings
,
135 bool wxMacCarbonPrintData::TransferFrom( const wxPrintData
&data
)
138 PMSetCopies( (PMPrintSettings
) m_macPrintSettings
, data
.GetNoCopies() , false ) ;
139 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
140 kPMLandscape
: kPMPortrait
, false ) ;
141 // collate cannot be set
142 #if 0 // not yet tested
143 if ( m_printerName
.Length() > 0 )
144 PMSessionSetCurrentPrinter( (PMPrintSession
) m_macPrintSession
, wxMacCFStringHolder( 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
) ;
156 // PMDuplexMode not yet accessible via API
157 // PMQualityMode not yet accessible via API
162 bool wxMacCarbonPrintData::TransferTo( wxPrintData
&data
)
164 OSStatus err
= noErr
;
167 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
169 data
.SetNoCopies( copies
) ;
171 PMOrientation orientation
;
172 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
175 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
176 data
.SetOrientation( wxPORTRAIT
);
178 data
.SetOrientation( wxLANDSCAPE
);
181 // collate cannot be set
184 wxMacCFStringHolder name
;
186 PMSessionGetCurrentPrinter( m_macPrintSession
,
188 m_printerName
= name
.AsString() ;
193 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
195 data
.SetColour( !(color
== kPMBlackAndWhite
) ) ;
197 // PMDuplexMode not yet accessible via API
198 // PMQualityMode not yet accessible via API
201 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
204 data
.SetPaperSize( wxSize (
205 (int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
206 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) ) );
211 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*data
)
213 // should we setup the page rect here ?
214 // since MacOS sometimes has two same paper rects with different
215 // page rects we could make it roundtrip safe perhaps
218 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
221 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
225 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
228 data
->SetMinMarginTopLeft( wxPoint (
229 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
230 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
232 data
->SetMinMarginBottomRight( wxPoint (
233 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
234 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
236 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
237 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
238 data
->GetMarginTopLeft().y
) ) ;
240 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
241 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
242 data
->GetMarginBottomRight().y
) );
244 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
245 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
247 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
248 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
249 data
->GetMinMarginBottomRight().y
) );
255 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
257 UInt32 minPage
, maxPage
;
258 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
259 data
->SetMinPage( minPage
) ;
260 data
->SetMaxPage( maxPage
) ;
262 PMGetCopies( m_macPrintSettings
, &copies
) ;
263 data
->SetNoCopies( copies
) ;
265 PMGetFirstPage( m_macPrintSettings
, &from
) ;
266 PMGetLastPage( m_macPrintSettings
, &to
) ;
267 data
->SetFromPage( from
) ;
268 data
->SetToPage( to
) ;
271 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
273 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
274 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
275 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
277 int toPage
= data
->GetToPage();
279 toPage
= data
->GetFromPage();
280 PMSetLastPage( m_macPrintSettings
, toPage
, false ) ;
287 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
292 wxMacPrinter::~wxMacPrinter(void)
296 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
299 sm_abortWindow
= NULL
;
304 printout
->SetIsPreview(FALSE
);
305 if (m_printDialogData
.GetMinPage() < 1)
306 m_printDialogData
.SetMinPage(1);
307 if (m_printDialogData
.GetMaxPage() < 1)
308 m_printDialogData
.SetMaxPage(9999);
310 // Create a suitable device context
314 wxPrintDialog
dialog(parent
, & m_printDialogData
);
315 if (dialog
.ShowModal() == wxID_OK
)
317 dc
= dialog
.GetPrintDC();
318 m_printDialogData
= dialog
.GetPrintDialogData();
323 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
327 // May have pressed cancel.
328 if (!dc
|| !dc
->Ok())
334 // on the mac we have always pixels as addressing mode with 72 dpi
336 printout
->SetPPIScreen(72, 72);
337 printout
->SetPPIPrinter(72, 72);
339 // Set printout parameters
345 printout
->SetPageSizePixels((int)w
, (int)h
);
346 dc
->GetSizeMM(&ww
, &hh
);
347 printout
->SetPageSizeMM((int)ww
, (int)hh
);
349 // Create an abort window
352 printout
->OnPreparePrinting();
354 // Get some parameters from the printout, if defined
355 int fromPage
, toPage
;
356 int minPage
, maxPage
;
357 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
365 // Only set min and max, because from and to have been
367 m_printDialogData
.SetMinPage(minPage
);
368 m_printDialogData
.SetMaxPage(maxPage
);
370 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
371 wxSafeYield(win
,true);
376 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
380 sm_abortWindow
= win
;
381 sm_abortWindow
->Show(TRUE
);
382 wxSafeYield(win
,true);
384 printout
->OnBeginPrinting();
386 bool keepGoing
= TRUE
;
389 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
391 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
394 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
401 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
412 if ( UMAGetSystemVersion() >= 0x1000 )
416 GetPort( &thePort
) ;
417 wxSafeYield(win
,true);
421 keepGoing
= printout
->OnPrintPage(pn
);
425 printout
->OnEndDocument();
428 printout
->OnEndPrinting();
432 sm_abortWindow
->Show(FALSE
);
433 delete sm_abortWindow
;
434 sm_abortWindow
= NULL
;
444 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
446 wxDC
* dc
= (wxDC
*) NULL
;
448 wxPrintDialog
dialog(parent
, & m_printDialogData
);
449 int ret
= dialog
.ShowModal();
453 dc
= dialog
.GetPrintDC();
454 m_printDialogData
= dialog
.GetPrintDialogData();
460 bool wxMacPrinter::Setup(wxWindow
*parent
)
463 wxPrintDialog
dialog(parent
, & m_printDialogData
);
464 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
466 int ret
= dialog
.ShowModal();
470 m_printDialogData
= dialog
.GetPrintDialogData();
473 return (ret
== wxID_OK
);
482 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
483 wxPrintout
*printoutForPrinting
,
484 wxPrintDialogData
*data
)
485 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
490 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
491 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
496 wxMacPrintPreview::~wxMacPrintPreview(void)
500 bool wxMacPrintPreview::Print(bool interactive
)
502 if (!m_printPrintout
)
504 wxMacPrinter
printer(&m_printDialogData
);
505 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
508 void wxMacPrintPreview::DetermineScaling(void)
510 int screenWidth
, screenHeight
;
511 wxDisplaySize( &screenWidth
, &screenHeight
) ;
513 m_previewPrintout
->SetPPIScreen( 72 , 72 ) ;
514 m_previewPrintout
->SetPPIPrinter( 72 , 72 ) ;
515 m_previewPrintout
->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
516 m_previewPrintout
->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
517 m_pageWidth
= 8 * 72 ;
518 m_pageHeight
= 11 * 72 ;
521 // Get a device context for the currently selected printer
522 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
527 printerDC
.GetSizeMM(&ww
, &hh
);
528 printerDC
.GetSize( &x
, &y
) ;
529 m_previewPrintout
->SetPageSizeMM((int)ww
, (int)hh
);
530 m_previewPrintout
->SetPageSizePixels( x
, y
) ;
539 // At 100%, the page should look about page-size on the screen.
540 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
541 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);