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
);
163 bool wxMacCarbonPrintData::TransferTo( wxPrintData
&data
)
165 OSStatus err
= noErr
;
168 err
= PMGetCopies( m_macPrintSettings
, &copies
) ;
170 data
.SetNoCopies( copies
) ;
172 PMOrientation orientation
;
173 err
= PMGetOrientation( m_macPageFormat
, &orientation
) ;
176 if ( orientation
== kPMPortrait
|| orientation
== kPMReversePortrait
)
177 data
.SetOrientation( wxPORTRAIT
);
179 data
.SetOrientation( wxLANDSCAPE
);
182 // collate cannot be set
185 wxMacCFStringHolder name
;
187 PMSessionGetCurrentPrinter( m_macPrintSession
,
189 m_printerName
= name
.AsString() ;
194 err
= PMGetColorMode( m_macPrintSettings
, &color
) ;
196 data
.SetColour( !(color
== kPMBlackAndWhite
) ) ;
198 // PMDuplexMode not yet accessible via API
199 // PMQualityMode not yet accessible via API
202 err
= PMGetUnadjustedPaperRect( m_macPageFormat
, &rPaper
);
205 data
.SetPaperSize( wxSize (
206 (int)(( rPaper
.right
- rPaper
.left
) * pt2mm
+ 0.5 ) ,
207 (int)(( rPaper
.bottom
- rPaper
.top
) * pt2mm
+ 0.5 ) ) );
212 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData
*data
)
214 // should we setup the page rect here ?
215 // since MacOS sometimes has two same paper rects with different
216 // page rects we could make it roundtrip safe perhaps
219 void wxMacCarbonPrintData::TransferTo( wxPageSetupData
* data
)
222 OSStatus err
= PMGetUnadjustedPaperRect(m_macPageFormat
, &rPaper
);
226 err
= PMGetUnadjustedPageRect(m_macPageFormat
, &rPage
) ;
229 data
->SetMinMarginTopLeft( wxPoint (
230 (int)(((double) rPage
.left
- rPaper
.left
) * pt2mm
) ,
231 (int)(((double) rPage
.top
- rPaper
.top
) * pt2mm
) ) ) ;
233 data
->SetMinMarginBottomRight( wxPoint (
234 (wxCoord
)(((double) rPaper
.right
- rPage
.right
) * pt2mm
),
235 (wxCoord
)(((double) rPaper
.bottom
- rPage
.bottom
) * pt2mm
)) ) ;
237 if ( data
->GetMarginTopLeft().x
< data
->GetMinMarginTopLeft().x
)
238 data
->SetMarginTopLeft( wxPoint( data
->GetMinMarginTopLeft().x
,
239 data
->GetMarginTopLeft().y
) ) ;
241 if ( data
->GetMarginBottomRight().x
< data
->GetMinMarginBottomRight().x
)
242 data
->SetMarginBottomRight( wxPoint( data
->GetMinMarginBottomRight().x
,
243 data
->GetMarginBottomRight().y
) );
245 if ( data
->GetMarginTopLeft().y
< data
->GetMinMarginTopLeft().y
)
246 data
->SetMarginTopLeft( wxPoint( data
->GetMarginTopLeft().x
, data
->GetMinMarginTopLeft().y
) );
248 if ( data
->GetMarginBottomRight().y
< data
->GetMinMarginBottomRight().y
)
249 data
->SetMarginBottomRight( wxPoint( data
->GetMarginBottomRight().x
,
250 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 if ( to
>= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
269 data
->SetAllPages( true ) ;
270 // This means all pages, more or less
271 data
->SetFromPage(1);
272 data
->SetToPage(32000);
276 data
->SetFromPage( from
) ;
277 data
->SetToPage( to
) ;
278 data
->SetAllPages( false );
282 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData
* data
)
284 PMSetPageRange( m_macPrintSettings
, data
->GetMinPage() , data
->GetMaxPage() ) ;
285 PMSetCopies( m_macPrintSettings
, data
->GetNoCopies() , false ) ;
286 PMSetFirstPage( m_macPrintSettings
, data
->GetFromPage() , false ) ;
288 if (data
->GetAllPages() || data
->GetFromPage() == 0)
289 PMSetLastPage( m_macPrintSettings
, (UInt32
) kPMPrintAllPages
, true ) ;
291 PMSetLastPage( m_macPrintSettings
, (UInt32
) data
->GetToPage() , false ) ;
298 wxMacPrinter::wxMacPrinter(wxPrintDialogData
*data
):
303 wxMacPrinter::~wxMacPrinter(void)
307 bool wxMacPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
310 sm_abortWindow
= NULL
;
315 printout
->SetIsPreview(false);
316 if (m_printDialogData
.GetMinPage() < 1)
317 m_printDialogData
.SetMinPage(1);
318 if (m_printDialogData
.GetMaxPage() < 1)
319 m_printDialogData
.SetMaxPage(9999);
321 // Create a suitable device context
325 wxPrintDialog
dialog(parent
, & m_printDialogData
);
326 if (dialog
.ShowModal() == wxID_OK
)
328 dc
= dialog
.GetPrintDC();
329 m_printDialogData
= dialog
.GetPrintDialogData();
334 dc
= new wxPrinterDC( m_printDialogData
.GetPrintData() ) ;
337 // May have pressed cancel.
338 if (!dc
|| !dc
->Ok())
345 // on the mac we have always pixels as addressing mode with 72 dpi
346 printout
->SetPPIScreen(72, 72);
348 wxMacCarbonPrintData
* nativeData
= (wxMacCarbonPrintData
*)
349 (m_printDialogData
.GetPrintData().GetNativeData());
350 PMGetResolution((PMPageFormat
) (nativeData
->m_macPageFormat
), &res
);
351 printout
->SetPPIPrinter(int(res
.hRes
), int(res
.vRes
));
353 // Set printout parameters
359 printout
->SetPageSizePixels((int)w
, (int)h
);
360 dc
->GetSizeMM(&ww
, &hh
);
361 printout
->SetPageSizeMM((int)ww
, (int)hh
);
363 // Create an abort window
366 printout
->OnPreparePrinting();
368 // Get some parameters from the printout, if defined
369 int fromPage
, toPage
;
370 int minPage
, maxPage
;
371 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
379 // Only set min and max, because from and to have been
381 m_printDialogData
.SetMinPage(minPage
);
382 m_printDialogData
.SetMaxPage(maxPage
);
384 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
385 wxSafeYield(win
,true);
390 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK
, parent
);
396 sm_abortWindow
= win
;
397 sm_abortWindow
->Show(true);
398 wxSafeYield(win
,true);
400 printout
->OnBeginPrinting();
402 bool keepGoing
= true;
405 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
407 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
410 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK
, parent
);
417 for (pn
= m_printDialogData
.GetFromPage();
418 keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
429 if ( UMAGetSystemVersion() >= 0x1000 )
433 GetPort( &thePort
) ;
434 wxSafeYield(win
,true);
438 keepGoing
= printout
->OnPrintPage(pn
);
442 printout
->OnEndDocument();
445 printout
->OnEndPrinting();
449 sm_abortWindow
->Show(false);
450 delete sm_abortWindow
;
451 sm_abortWindow
= NULL
;
461 wxDC
* wxMacPrinter::PrintDialog(wxWindow
*parent
)
463 wxDC
* dc
= (wxDC
*) NULL
;
465 wxPrintDialog
dialog(parent
, & m_printDialogData
);
466 int ret
= dialog
.ShowModal();
470 dc
= dialog
.GetPrintDC();
471 m_printDialogData
= dialog
.GetPrintDialogData();
477 bool wxMacPrinter::Setup(wxWindow
*parent
)
480 wxPrintDialog
dialog(parent
, & m_printDialogData
);
481 dialog
.GetPrintDialogData().SetSetupDialog(true);
483 int ret
= dialog
.ShowModal();
486 m_printDialogData
= dialog
.GetPrintDialogData();
488 return (ret
== wxID_OK
);
498 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
,
499 wxPrintout
*printoutForPrinting
,
500 wxPrintDialogData
*data
)
501 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
506 wxMacPrintPreview::wxMacPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
507 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
512 wxMacPrintPreview::~wxMacPrintPreview(void)
516 bool wxMacPrintPreview::Print(bool interactive
)
518 if (!m_printPrintout
)
521 wxMacPrinter
printer(&m_printDialogData
);
522 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
525 void wxMacPrintPreview::DetermineScaling(void)
527 int screenWidth
, screenHeight
;
528 wxDisplaySize( &screenWidth
, &screenHeight
) ;
530 m_previewPrintout
->SetPPIScreen( 72 , 72 ) ;
531 m_previewPrintout
->SetPPIPrinter( 72 , 72 ) ;
532 m_previewPrintout
->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
533 m_previewPrintout
->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
534 m_pageWidth
= 8 * 72 ;
535 m_pageHeight
= 11 * 72 ;
538 // Get a device context for the currently selected printer
539 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
544 printerDC
.GetSizeMM(&ww
, &hh
);
545 printerDC
.GetSize( &x
, &y
) ;
546 m_previewPrintout
->SetPageSizeMM((int)ww
, (int)hh
);
547 m_previewPrintout
->SetPageSizePixels( x
, y
) ;
557 // At 100%, the page should look about page-size on the screen.
558 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
559 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);