1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxPrinterDC class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dcprint.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/dcprint.h"
27 #include "wx/msgdlg.h"
29 #include "wx/mac/uma.h"
30 #include "wx/mac/private/print.h"
32 #if !USE_SHARED_LIBRARY
33 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
36 class wxNativePrinterDC
39 wxNativePrinterDC() {}
40 virtual ~wxNativePrinterDC() {}
41 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) = 0;
42 virtual void EndDoc( wxPrinterDC
* dc
) = 0;
43 virtual void StartPage( wxPrinterDC
* dc
) = 0;
44 virtual void EndPage( wxPrinterDC
* dc
) = 0;
45 virtual wxCoord
GetMaxX() const = 0 ;
46 virtual wxCoord
GetMaxY() const = 0 ;
47 // returns 0 in case of no Error, otherwise platform specific error codes
48 virtual wxUint32
GetStatus() const = 0 ;
49 bool Ok() { return GetStatus() == 0 ; }
51 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
56 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
59 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
60 ~wxMacCarbonPrinterDC() ;
61 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
62 virtual void EndDoc( wxPrinterDC
* dc
) ;
63 virtual void StartPage( wxPrinterDC
* dc
) ;
64 virtual void EndPage( wxPrinterDC
* dc
) ;
65 virtual wxCoord
GetMaxX() const { return m_maxX
; }
66 virtual wxCoord
GetMaxY() const { return m_maxY
; }
67 virtual wxUint32
GetStatus() const { return m_err
; }
69 GrafPtr m_macPrintFormerPort
;
75 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
77 ::GetPort( & m_macPrintFormerPort
) ;
80 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->m_nativePrintData
;
83 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
87 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
88 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
91 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
93 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
94 ::SetPort( m_macPrintFormerPort
) ;
97 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
99 return new wxMacCarbonPrinterDC(data
) ;
102 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
107 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
109 m_err
= PMSessionBeginDocument(native
->m_macPrintSession
,
110 native
->m_macPrintSettings
,
111 native
->m_macPageFormat
);
112 if ( m_err
!= noErr
)
116 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
117 if ( m_err
!= noErr
)
120 m_maxX
= (wxCoord
)(rPage
.right
- rPage
.left
);
121 m_maxY
= (wxCoord
)(rPage
.bottom
- rPage
.top
);
125 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
130 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
132 m_err
= PMSessionEndDocument(native
->m_macPrintSession
);
135 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
140 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
142 m_err
= PMSessionBeginPage(native
->m_macPrintSession
,
143 native
->m_macPageFormat
,
146 if ( m_err
== noErr
)
148 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
150 (void**) &dc
->m_macPort
);
153 if ( m_err
!= noErr
)
155 PMSessionEndPage(native
->m_macPrintSession
);
156 PMSessionEndDocument(native
->m_macPrintSession
);
162 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
165 dc
->m_macLocalOrigin
.x
= (int) rPage
.left
;
166 dc
->m_macLocalOrigin
.y
= (int) rPage
.top
;
168 // since this is a non-critical error, we set the flag back
173 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
178 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
180 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
181 if ( m_err
!= noErr
)
183 PMSessionEndDocument(native
->m_macPrintSession
);
189 class wxMacClassicPrinterDC
: public wxNativePrinterDC
192 wxMacClassicPrinterDC( wxPrintData
* data
) ;
193 ~wxMacClassicPrinterDC() ;
194 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
195 virtual void EndDoc( wxPrinterDC
* dc
) ;
196 virtual void StartPage( wxPrinterDC
* dc
) ;
197 virtual void EndPage( wxPrinterDC
* dc
) ;
198 virtual wxCoord
GetMaxX() const { return m_maxX
; }
199 virtual wxCoord
GetMaxY() const { return m_maxY
; }
200 virtual wxUint32
GetStatus() const { return m_err
; }
202 GrafPtr m_macPrintFormerPort
;
203 TPPrPort m_macPrintingPort
;
209 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
211 return new wxMacClassicPrinterDC(data
) ;
214 wxMacClassicPrinterDC::wxMacClassicPrinterDC(wxPrintData
* data
)
216 ::GetPort( &m_macPrintFormerPort
) ;
220 if ( m_err
!= noErr
)
223 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) data
->m_nativePrintData
;
225 if ( ::PrValidate( native
->m_macPrintSettings
) )
227 // the driver has changed in the mean time, should we pop up a page setup dialog ?
228 if ( !::PrStlDialog( native
->m_macPrintSettings
) )
236 if ( m_err
== noErr
)
238 m_maxX
= (**native
->m_macPrintSettings
).prInfo
.rPage
.right
- (**native
->m_macPrintSettings
).prInfo
.rPage
.left
;
239 m_maxY
= (**native
->m_macPrintSettings
).prInfo
.rPage
.bottom
- (**native
->m_macPrintSettings
).prInfo
.rPage
.top
;
243 wxMacClassicPrinterDC::~wxMacClassicPrinterDC()
246 ::SetPort( LMGetWMgrPort() ) ;
249 bool wxMacClassicPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
254 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) dc
->GetPrintData().m_nativePrintData
;
255 m_macPrintingPort
= ::PrOpenDoc( native
->m_macPrintSettings
, NULL
, NULL
) ;
261 dc
->m_macPort
= (GrafPtr
) m_macPrintingPort
;
262 m_maxX
= (**native
->m_macPrintSettings
).prInfo
.rPage
.right
- (**native
->m_macPrintSettings
).prInfo
.rPage
.left
;
263 m_maxY
= (**native
->m_macPrintSettings
).prInfo
.rPage
.bottom
- (**native
->m_macPrintSettings
).prInfo
.rPage
.top
;
267 void wxMacClassicPrinterDC::EndDoc( wxPrinterDC
* dc
)
272 PrCloseDoc( m_macPrintingPort
) ;
276 void wxMacClassicPrinterDC::StartPage( wxPrinterDC
* dc
)
281 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) dc
->GetPrintData().m_nativePrintData
;
283 PrOpenPage( m_macPrintingPort
, NULL
) ;
284 dc
->m_macLocalOrigin
.x
= (**native
->m_macPrintSettings
).rPaper
.left
;
285 dc
->m_macLocalOrigin
.y
= (**native
->m_macPrintSettings
).rPaper
.top
;
286 // m_macPrintingPort is now the current port
287 Rect clip
= { -32000 , -32000 , 32000 , 32000 } ;
288 ::ClipRect( &clip
) ;
290 if ( m_err
!= noErr
)
291 ::PrCloseDoc( m_macPrintingPort
) ;
294 void wxMacClassicPrinterDC::EndPage( wxPrinterDC
* dc
)
299 PrClosePage( m_macPrintingPort
) ;
301 if ( m_err
!= noErr
)
302 ::PrCloseDoc( m_macPrintingPort
) ;
307 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
310 m_printData
= printdata
;
311 m_printData
.ConvertToNative() ;
312 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
313 if ( m_nativePrinterDC
)
315 m_ok
= m_nativePrinterDC
->Ok() ;
320 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
321 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
327 wxPrinterDC::~wxPrinterDC(void)
329 delete m_nativePrinterDC
;
332 bool wxPrinterDC::StartDoc( const wxString
& message
)
334 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
339 if ( m_nativePrinterDC
->StartDoc(this, message
) )
341 // in case we have to do additional things when successful
343 m_ok
= m_nativePrinterDC
->Ok() ;
347 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
348 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
355 void wxPrinterDC::EndDoc(void)
360 m_nativePrinterDC
->EndDoc( this ) ;
361 m_ok
= m_nativePrinterDC
->Ok() ;
366 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
367 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
372 void wxPrinterDC::StartPage(void)
377 m_logicalFunction
= wxCOPY
;
378 // m_textAlignment = wxALIGN_TOP_LEFT;
379 m_backgroundMode
= wxTRANSPARENT
;
381 m_textForegroundColour
= *wxBLACK
;
382 m_textBackgroundColour
= *wxWHITE
;
383 m_pen
= *wxBLACK_PEN
;
384 m_font
= *wxNORMAL_FONT
;
385 m_brush
= *wxTRANSPARENT_BRUSH
;
386 m_backgroundBrush
= *wxWHITE_BRUSH
;
388 m_macFontInstalled
= false ;
389 m_macBrushInstalled
= false ;
390 m_macPenInstalled
= false ;
392 m_nativePrinterDC
->StartPage(this) ;
393 m_ok
= m_nativePrinterDC
->Ok() ;
397 void wxPrinterDC::EndPage(void)
402 m_nativePrinterDC
->EndPage(this) ;
403 m_ok
= m_nativePrinterDC
->Ok() ;
406 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
408 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
411 * width
= m_nativePrinterDC
->GetMaxX() ;
413 * height
= m_nativePrinterDC
->GetMaxY() ;