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 IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData
, wxPrintNativeDataBase
)
45 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter
, wxPrinterBase
)
46 IMPLEMENT_CLASS(wxMacPrintPreview
, wxPrintPreviewBase
)
48 bool wxMacCarbonPrintData::Ok() const
50 return (m_macPageFormat
!= kPMNoPageFormat
) && (m_macPrintSettings
!= kPMNoPrintSettings
) && (m_macPrintSession
!= kPMNoReference
);
52 wxMacCarbonPrintData::wxMacCarbonPrintData()
54 m_macPageFormat
= kPMNoPageFormat
;
55 m_macPrintSettings
= kPMNoPrintSettings
;
56 m_macPrintSession
= kPMNoReference
;
60 wxMacCarbonPrintData::~wxMacCarbonPrintData()
62 if (m_macPageFormat
!= kPMNoPageFormat
)
64 (void)PMRelease(m_macPageFormat
);
65 m_macPageFormat
= kPMNoPageFormat
;
68 if (m_macPrintSettings
!= kPMNoPrintSettings
)
70 (void)PMRelease(m_macPrintSettings
);
71 m_macPrintSettings
= kPMNoPrintSettings
;
74 if ( m_macPrintSession
!= kPMNoReference
)
76 (void)PMRelease(m_macPrintSession
);
77 m_macPrintSession
= kPMNoReference
;
81 void wxMacCarbonPrintData::ValidateOrCreate()
83 OSStatus err
= noErr
;
84 if ( m_macPrintSession
== kPMNoReference
)
86 err
= PMCreateSession( (PMPrintSession
*) &m_macPrintSession
) ;
88 // Set up a valid PageFormat object.
89 if ( m_macPageFormat
== kPMNoPageFormat
)
91 err
= PMCreatePageFormat((PMPageFormat
*) &m_macPageFormat
);
93 // Note that PMPageFormat is not session-specific, but calling
94 // PMSessionDefaultPageFormat assigns values specific to the printer
95 // associated with the current printing session.
97 ( m_macPageFormat
!= kPMNoPageFormat
))
99 err
= PMSessionDefaultPageFormat((PMPrintSession
) m_macPrintSession
,
100 (PMPageFormat
) m_macPageFormat
);
105 err
= PMSessionValidatePageFormat((PMPrintSession
) m_macPrintSession
,
106 (PMPageFormat
) m_macPageFormat
,
110 // Set up a valid PrintSettings object.
111 if ( m_macPrintSettings
== kPMNoPrintSettings
)
113 err
= PMCreatePrintSettings((PMPrintSettings
*) &m_macPrintSettings
);
115 // Note that PMPrintSettings is not session-specific, but calling
116 // PMSessionDefaultPrintSettings assigns values specific to the printer
117 // associated with the current printing session.
118 if ((err
== noErr
) &&
119 ( m_macPrintSettings
!= kPMNoPrintSettings
))
121 err
= PMSessionDefaultPrintSettings((PMPrintSession
) m_macPrintSession
,
122 (PMPrintSettings
) m_macPrintSettings
);
127 err
= PMSessionValidatePrintSettings((PMPrintSession
) m_macPrintSession
,
128 (PMPrintSettings
) m_macPrintSettings
,
133 bool wxMacCarbonPrintData::TransferFrom( const wxPrintData
&data
)
136 PMSetCopies( (PMPrintSettings
) m_macPrintSettings
, data
.GetNoCopies() , false ) ;
137 PMSetOrientation( (PMPageFormat
) m_macPageFormat
, ( data
.GetOrientation() == wxLANDSCAPE
) ?
138 kPMLandscape
: kPMPortrait
, false ) ;
139 // collate cannot be set
140 #if 0 // not yet tested
141 if ( m_printerName
.Length() > 0 )
142 PMSessionSetCurrentPrinter( (PMPrintSession
) m_macPrintSession
, wxMacCFStringHolder( m_printerName
, wxFont::GetDefaultEncoding() ) ) ;
145 PMGetColorMode( (PMPrintSettings
) m_macPrintSettings
, &color
) ;
146 if ( data
.GetColour() )
148 if ( color
== kPMBlackAndWhite
)
149 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMColor
) ;
152 PMSetColorMode( (PMPrintSettings
) m_macPrintSettings
, kPMBlackAndWhite
) ;
154 // PMDuplexMode not yet accessible via API
155 // PMQualityMode not yet accessible via API
160 bool wxMacCarbonPrintData::TransferTo( wxPrintData
&data
)
162 OSStatus err
= noErr
;
165 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
167 data
.SetNoCopies( copies
) ;
169 PMOrientation orientation
;
170 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
173 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
174 data
.SetOrientation( wxPORTRAIT
);
176 data
.SetOrientation( wxLANDSCAPE
);
179 // collate cannot be set
182 wxMacCFStringHolder name
;
184 PMSessionGetCurrentPrinter( m_macPrintSession
,
186 m_printerName
= name
.AsString() ;
191 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
193 data
.SetColour( !(color
== kPMBlackAndWhite
) ) ;
195 // PMDuplexMode not yet accessible via API
196 // PMQualityMode not yet accessible via API
199 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
202 data
.SetPaperSize( wxSize (
203 (int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
204 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) ) );
209 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*data
)
211 // should we setup the page rect here ?
212 // since MacOS sometimes has two same paper rects with different
213 // page rects we could make it roundtrip safe perhaps
216 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
219 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
223 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
226 data
->SetMinMarginTopLeft( wxPoint (
227 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
228 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
230 data
->SetMinMarginBottomRight( wxPoint (
231 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
232 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
234 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
235 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
236 data
->GetMarginTopLeft().y
) ) ;
238 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
239 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
240 data
->GetMarginBottomRight().y
) );
242 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
243 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
245 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
246 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
247 data
->GetMinMarginBottomRight().y
) );
253 void wxMacCarbonPrintData::TransferTo( wxPrintDialogData
* data
)
255 UInt32 minPage
, maxPage
;
256 PMGetPageRange( m_macPrintSettings
, &minPage
, &maxPage
) ;
257 data
->SetMinPage( minPage
) ;
258 data
->SetMaxPage( maxPage
) ;
260 PMGetCopies( m_macPrintSettings
, &copies
) ;
261 data
->SetNoCopies( copies
) ;
263 PMGetFirstPage( m_macPrintSettings
, &from
) ;
264 PMGetLastPage( m_macPrintSettings
, &to
) ;
265 if ( to
>= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
267 data
->SetAllPages( true ) ;
268 // This means all pages, more or less
269 data
->SetFromPage(1);
270 data
->SetToPage(32000);
274 data
->SetFromPage( from
) ;
275 data
->SetToPage( to
) ;
276 data
->SetAllPages( false );
280 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
282 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
283 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
284 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
286 if (data
->GetAllPages() || data
->GetFromPage() == 0)
288 PMSetLastPage( m_macPrintSettings
, (UInt32
) kPMPrintAllPages
, true ) ;
292 PMSetLastPage( m_macPrintSettings
, (UInt32
) data
->GetToPage() , false ) ;
300 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
305 wxMacPrinter::~wxMacPrinter(void)
309 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
312 sm_abortWindow
= NULL
;
317 printout
->SetIsPreview(FALSE
);
318 if (m_printDialogData
.GetMinPage() < 1)
319 m_printDialogData
.SetMinPage(1);
320 if (m_printDialogData
.GetMaxPage() < 1)
321 m_printDialogData
.SetMaxPage(9999);
323 // Create a suitable device context
327 wxPrintDialog
dialog(parent
, & m_printDialogData
);
328 if (dialog
.ShowModal() == wxID_OK
)
330 dc
= dialog
.GetPrintDC();
331 m_printDialogData
= dialog
.GetPrintDialogData();
336 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
340 // May have pressed cancel.
341 if (!dc
|| !dc
->Ok())
347 // on the mac we have always pixels as addressing mode with 72 dpi
349 printout
->SetPPIScreen(72, 72);
350 printout
->SetPPIPrinter(72, 72);
352 // Set printout parameters
358 printout
->SetPageSizePixels((int)w
, (int)h
);
359 dc
->GetSizeMM(&ww
, &hh
);
360 printout
->SetPageSizeMM((int)ww
, (int)hh
);
362 // Create an abort window
365 printout
->OnPreparePrinting();
367 // Get some parameters from the printout, if defined
368 int fromPage
, toPage
;
369 int minPage
, maxPage
;
370 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
378 // Only set min and max, because from and to have been
380 m_printDialogData
.SetMinPage(minPage
);
381 m_printDialogData
.SetMaxPage(maxPage
);
383 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
384 wxSafeYield(win
,true);
389 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
393 sm_abortWindow
= win
;
394 sm_abortWindow
->Show(TRUE
);
395 wxSafeYield(win
,true);
397 printout
->OnBeginPrinting();
399 bool keepGoing
= TRUE
;
402 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
404 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
407 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
414 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
425 if ( UMAGetSystemVersion() >= 0x1000 )
429 GetPort( &thePort
) ;
430 wxSafeYield(win
,true);
434 keepGoing
= printout
->OnPrintPage(pn
);
438 printout
->OnEndDocument();
441 printout
->OnEndPrinting();
445 sm_abortWindow
->Show(FALSE
);
446 delete sm_abortWindow
;
447 sm_abortWindow
= NULL
;
457 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
459 wxDC
* dc
= (wxDC
*) NULL
;
461 wxPrintDialog
dialog(parent
, & m_printDialogData
);
462 int ret
= dialog
.ShowModal();
466 dc
= dialog
.GetPrintDC();
467 m_printDialogData
= dialog
.GetPrintDialogData();
473 bool wxMacPrinter::Setup(wxWindow
*parent
)
476 wxPrintDialog
dialog(parent
, & m_printDialogData
);
477 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
479 int ret
= dialog
.ShowModal();
483 m_printDialogData
= dialog
.GetPrintDialogData();
486 return (ret
== wxID_OK
);
495 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
496 wxPrintout
*printoutForPrinting
,
497 wxPrintDialogData
*data
)
498 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
503 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
504 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
509 wxMacPrintPreview::~wxMacPrintPreview(void)
513 bool wxMacPrintPreview::Print(bool interactive
)
515 if (!m_printPrintout
)
517 wxMacPrinter
printer(&m_printDialogData
);
518 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
521 void wxMacPrintPreview::DetermineScaling(void)
523 int screenWidth
, screenHeight
;
524 wxDisplaySize( &screenWidth
, &screenHeight
) ;
526 m_previewPrintout
->SetPPIScreen( 72 , 72 ) ;
527 m_previewPrintout
->SetPPIPrinter( 72 , 72 ) ;
528 m_previewPrintout
->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
529 m_previewPrintout
->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
530 m_pageWidth
= 8 * 72 ;
531 m_pageHeight
= 11 * 72 ;
534 // Get a device context for the currently selected printer
535 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
540 printerDC
.GetSizeMM(&ww
, &hh
);
541 printerDC
.GetSize( &x
, &y
) ;
542 m_previewPrintout
->SetPageSizeMM((int)ww
, (int)hh
);
543 m_previewPrintout
->SetPageSizePixels( x
, y
) ;
552 // At 100%, the page should look about page-size on the screen.
553 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
554 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);