1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/dcprint.cpp
3 // Purpose: wxPrinterDC class
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"
19 #include "wx/dcprint.h"
22 #include "wx/msgdlg.h"
26 #include "wx/mac/uma.h"
27 #include "wx/mac/private/print.h"
29 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
31 class wxNativePrinterDC
34 wxNativePrinterDC() {}
35 virtual ~wxNativePrinterDC() {}
36 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) = 0;
37 virtual void EndDoc( wxPrinterDC
* dc
) = 0;
38 virtual void StartPage( wxPrinterDC
* dc
) = 0;
39 virtual void EndPage( wxPrinterDC
* dc
) = 0;
40 virtual wxCoord
GetMaxX() const = 0 ;
41 virtual wxCoord
GetMaxY() const = 0 ;
42 // returns 0 in case of no Error, otherwise platform specific error codes
43 virtual wxUint32
GetStatus() const = 0 ;
44 bool Ok() { return GetStatus() == 0 ; }
46 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
51 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
54 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
55 ~wxMacCarbonPrinterDC() ;
56 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
57 virtual void EndDoc( wxPrinterDC
* dc
) ;
58 virtual void StartPage( wxPrinterDC
* dc
) ;
59 virtual void EndPage( wxPrinterDC
* dc
) ;
60 virtual wxCoord
GetMaxX() const { return m_maxX
; }
61 virtual wxCoord
GetMaxY() const { return m_maxY
; }
62 virtual wxUint32
GetStatus() const { return m_err
; }
64 GrafPtr m_macPrintFormerPort
;
70 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
72 ::GetPort( & m_macPrintFormerPort
) ;
75 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->m_nativePrintData
;
78 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
82 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
83 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
86 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
88 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
89 ::SetPort( m_macPrintFormerPort
) ;
92 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
94 return new wxMacCarbonPrinterDC(data
) ;
97 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
102 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
104 m_err
= PMSessionBeginDocument(native
->m_macPrintSession
,
105 native
->m_macPrintSettings
,
106 native
->m_macPageFormat
);
107 if ( m_err
!= noErr
)
111 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
112 if ( m_err
!= noErr
)
115 m_maxX
= (wxCoord
)(rPage
.right
- rPage
.left
);
116 m_maxY
= (wxCoord
)(rPage
.bottom
- rPage
.top
);
120 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
125 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
127 m_err
= PMSessionEndDocument(native
->m_macPrintSession
);
130 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
135 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
137 m_err
= PMSessionBeginPage(native
->m_macPrintSession
,
138 native
->m_macPageFormat
,
141 if ( m_err
== noErr
)
143 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
145 (void**) &dc
->m_macPort
);
148 if ( m_err
!= noErr
)
150 PMSessionEndPage(native
->m_macPrintSession
);
151 PMSessionEndDocument(native
->m_macPrintSession
);
157 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
160 dc
->m_macLocalOrigin
.x
= (int) rPage
.left
;
161 dc
->m_macLocalOrigin
.y
= (int) rPage
.top
;
163 // since this is a non-critical error, we set the flag back
168 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
173 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
175 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
176 if ( m_err
!= noErr
)
178 PMSessionEndDocument(native
->m_macPrintSession
);
184 class wxMacClassicPrinterDC
: public wxNativePrinterDC
187 wxMacClassicPrinterDC( wxPrintData
* data
) ;
188 ~wxMacClassicPrinterDC() ;
189 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
190 virtual void EndDoc( wxPrinterDC
* dc
) ;
191 virtual void StartPage( wxPrinterDC
* dc
) ;
192 virtual void EndPage( wxPrinterDC
* dc
) ;
193 virtual wxCoord
GetMaxX() const { return m_maxX
; }
194 virtual wxCoord
GetMaxY() const { return m_maxY
; }
195 virtual wxUint32
GetStatus() const { return m_err
; }
197 GrafPtr m_macPrintFormerPort
;
198 TPPrPort m_macPrintingPort
;
204 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
206 return new wxMacClassicPrinterDC(data
) ;
209 wxMacClassicPrinterDC::wxMacClassicPrinterDC(wxPrintData
* data
)
211 ::GetPort( &m_macPrintFormerPort
) ;
215 if ( m_err
!= noErr
)
218 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) data
->m_nativePrintData
;
220 if ( ::PrValidate( native
->m_macPrintSettings
) )
222 // the driver has changed in the mean time, should we pop up a page setup dialog ?
223 if ( !::PrStlDialog( native
->m_macPrintSettings
) )
231 if ( m_err
== noErr
)
233 m_maxX
= (**native
->m_macPrintSettings
).prInfo
.rPage
.right
- (**native
->m_macPrintSettings
).prInfo
.rPage
.left
;
234 m_maxY
= (**native
->m_macPrintSettings
).prInfo
.rPage
.bottom
- (**native
->m_macPrintSettings
).prInfo
.rPage
.top
;
238 wxMacClassicPrinterDC::~wxMacClassicPrinterDC()
241 ::SetPort( LMGetWMgrPort() ) ;
244 bool wxMacClassicPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
249 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) dc
->GetPrintData().m_nativePrintData
;
250 m_macPrintingPort
= ::PrOpenDoc( native
->m_macPrintSettings
, NULL
, NULL
) ;
256 dc
->m_macPort
= (GrafPtr
) m_macPrintingPort
;
257 m_maxX
= (**native
->m_macPrintSettings
).prInfo
.rPage
.right
- (**native
->m_macPrintSettings
).prInfo
.rPage
.left
;
258 m_maxY
= (**native
->m_macPrintSettings
).prInfo
.rPage
.bottom
- (**native
->m_macPrintSettings
).prInfo
.rPage
.top
;
262 void wxMacClassicPrinterDC::EndDoc( wxPrinterDC
* dc
)
267 PrCloseDoc( m_macPrintingPort
) ;
271 void wxMacClassicPrinterDC::StartPage( wxPrinterDC
* dc
)
276 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) dc
->GetPrintData().m_nativePrintData
;
278 PrOpenPage( m_macPrintingPort
, NULL
) ;
279 dc
->m_macLocalOrigin
.x
= (**native
->m_macPrintSettings
).rPaper
.left
;
280 dc
->m_macLocalOrigin
.y
= (**native
->m_macPrintSettings
).rPaper
.top
;
281 // m_macPrintingPort is now the current port
282 Rect clip
= { -32000 , -32000 , 32000 , 32000 } ;
283 ::ClipRect( &clip
) ;
285 if ( m_err
!= noErr
)
286 ::PrCloseDoc( m_macPrintingPort
) ;
289 void wxMacClassicPrinterDC::EndPage( wxPrinterDC
* dc
)
294 PrClosePage( m_macPrintingPort
) ;
296 if ( m_err
!= noErr
)
297 ::PrCloseDoc( m_macPrintingPort
) ;
302 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
305 m_printData
= printdata
;
306 m_printData
.ConvertToNative() ;
307 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
308 if ( m_nativePrinterDC
)
310 m_ok
= m_nativePrinterDC
->Ok() ;
315 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
316 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
322 wxPrinterDC::~wxPrinterDC(void)
324 delete m_nativePrinterDC
;
327 bool wxPrinterDC::StartDoc( const wxString
& message
)
329 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
334 if ( m_nativePrinterDC
->StartDoc(this, message
) )
336 // in case we have to do additional things when successful
338 m_ok
= m_nativePrinterDC
->Ok() ;
342 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
343 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
350 void wxPrinterDC::EndDoc(void)
355 m_nativePrinterDC
->EndDoc( this ) ;
356 m_ok
= m_nativePrinterDC
->Ok() ;
361 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
362 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
367 void wxPrinterDC::StartPage(void)
372 m_logicalFunction
= wxCOPY
;
373 // m_textAlignment = wxALIGN_TOP_LEFT;
374 m_backgroundMode
= wxTRANSPARENT
;
376 m_textForegroundColour
= *wxBLACK
;
377 m_textBackgroundColour
= *wxWHITE
;
378 m_pen
= *wxBLACK_PEN
;
379 m_font
= *wxNORMAL_FONT
;
380 m_brush
= *wxTRANSPARENT_BRUSH
;
381 m_backgroundBrush
= *wxWHITE_BRUSH
;
383 m_macFontInstalled
= false ;
384 m_macBrushInstalled
= false ;
385 m_macPenInstalled
= false ;
387 m_nativePrinterDC
->StartPage(this) ;
388 m_ok
= m_nativePrinterDC
->Ok() ;
392 void wxPrinterDC::EndPage(void)
397 m_nativePrinterDC
->EndPage(this) ;
398 m_ok
= m_nativePrinterDC
->Ok() ;
401 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
403 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
406 * width
= m_nativePrinterDC
->GetMaxX() ;
408 * height
= m_nativePrinterDC
->GetMaxY() ;