1 /////////////////////////////////////////////////////////////////////////////
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"
22 #include "wx/dcprint.h"
23 #include "wx/msgdlg.h"
25 #include "wx/mac/uma.h"
26 #include "wx/mac/private/print.h"
28 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
30 class wxNativePrinterDC
33 wxNativePrinterDC() {}
34 virtual ~wxNativePrinterDC() {}
35 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) = 0;
36 virtual void EndDoc( wxPrinterDC
* dc
) = 0;
37 virtual void StartPage( wxPrinterDC
* dc
) = 0;
38 virtual void EndPage( wxPrinterDC
* dc
) = 0;
39 virtual wxCoord
GetMaxX() const = 0 ;
40 virtual wxCoord
GetMaxY() const = 0 ;
41 // returns 0 in case of no Error, otherwise platform specific error codes
42 virtual wxUint32
GetStatus() const = 0 ;
43 bool Ok() { return GetStatus() == 0 ; }
45 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
50 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
53 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
54 ~wxMacCarbonPrinterDC() ;
55 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
56 virtual void EndDoc( wxPrinterDC
* dc
) ;
57 virtual void StartPage( wxPrinterDC
* dc
) ;
58 virtual void EndPage( wxPrinterDC
* dc
) ;
59 virtual wxCoord
GetMaxX() const { return m_maxX
; }
60 virtual wxCoord
GetMaxY() const { return m_maxY
; }
61 virtual wxUint32
GetStatus() const { return m_err
; }
63 GrafPtr m_macPrintFormerPort
;
69 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
71 ::GetPort( & m_macPrintFormerPort
) ;
74 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->m_nativePrintData
;
77 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
81 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
82 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
85 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
87 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
88 ::SetPort( m_macPrintFormerPort
) ;
91 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
93 return new wxMacCarbonPrinterDC(data
) ;
96 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
101 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
103 m_err
= PMSessionBeginDocument(native
->m_macPrintSession
,
104 native
->m_macPrintSettings
,
105 native
->m_macPageFormat
);
106 if ( m_err
!= noErr
)
110 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
111 if ( m_err
!= noErr
)
114 m_maxX
= (wxCoord
)(rPage
.right
- rPage
.left
);
115 m_maxY
= (wxCoord
)(rPage
.bottom
- rPage
.top
);
119 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
124 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
126 m_err
= PMSessionEndDocument(native
->m_macPrintSession
);
129 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
134 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
136 m_err
= PMSessionBeginPage(native
->m_macPrintSession
,
137 native
->m_macPageFormat
,
140 if ( m_err
== noErr
)
142 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
144 (void**) &dc
->m_macPort
);
147 if ( m_err
!= noErr
)
149 PMSessionEndPage(native
->m_macPrintSession
);
150 PMSessionEndDocument(native
->m_macPrintSession
);
156 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
159 dc
->m_macLocalOrigin
.x
= (int) rPage
.left
;
160 dc
->m_macLocalOrigin
.y
= (int) rPage
.top
;
162 // since this is a non-critical error, we set the flag back
167 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
172 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
174 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
175 if ( m_err
!= noErr
)
177 PMSessionEndDocument(native
->m_macPrintSession
);
183 class wxMacClassicPrinterDC
: public wxNativePrinterDC
186 wxMacClassicPrinterDC( wxPrintData
* data
) ;
187 ~wxMacClassicPrinterDC() ;
188 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
189 virtual void EndDoc( wxPrinterDC
* dc
) ;
190 virtual void StartPage( wxPrinterDC
* dc
) ;
191 virtual void EndPage( wxPrinterDC
* dc
) ;
192 virtual wxCoord
GetMaxX() const { return m_maxX
; }
193 virtual wxCoord
GetMaxY() const { return m_maxY
; }
194 virtual wxUint32
GetStatus() const { return m_err
; }
196 GrafPtr m_macPrintFormerPort
;
197 TPPrPort m_macPrintingPort
;
203 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
205 return new wxMacClassicPrinterDC(data
) ;
208 wxMacClassicPrinterDC::wxMacClassicPrinterDC(wxPrintData
* data
)
210 ::GetPort( &m_macPrintFormerPort
) ;
214 if ( m_err
!= noErr
)
217 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) data
->m_nativePrintData
;
219 if ( ::PrValidate( native
->m_macPrintSettings
) )
221 // the driver has changed in the mean time, should we pop up a page setup dialog ?
222 if ( !::PrStlDialog( native
->m_macPrintSettings
) )
230 if ( m_err
== noErr
)
232 m_maxX
= (**native
->m_macPrintSettings
).prInfo
.rPage
.right
- (**native
->m_macPrintSettings
).prInfo
.rPage
.left
;
233 m_maxY
= (**native
->m_macPrintSettings
).prInfo
.rPage
.bottom
- (**native
->m_macPrintSettings
).prInfo
.rPage
.top
;
237 wxMacClassicPrinterDC::~wxMacClassicPrinterDC()
240 ::SetPort( LMGetWMgrPort() ) ;
243 bool wxMacClassicPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
248 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) dc
->GetPrintData().m_nativePrintData
;
249 m_macPrintingPort
= ::PrOpenDoc( native
->m_macPrintSettings
, NULL
, NULL
) ;
255 dc
->m_macPort
= (GrafPtr
) m_macPrintingPort
;
256 m_maxX
= (**native
->m_macPrintSettings
).prInfo
.rPage
.right
- (**native
->m_macPrintSettings
).prInfo
.rPage
.left
;
257 m_maxY
= (**native
->m_macPrintSettings
).prInfo
.rPage
.bottom
- (**native
->m_macPrintSettings
).prInfo
.rPage
.top
;
261 void wxMacClassicPrinterDC::EndDoc( wxPrinterDC
* dc
)
266 PrCloseDoc( m_macPrintingPort
) ;
270 void wxMacClassicPrinterDC::StartPage( wxPrinterDC
* dc
)
275 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) dc
->GetPrintData().m_nativePrintData
;
277 PrOpenPage( m_macPrintingPort
, NULL
) ;
278 dc
->m_macLocalOrigin
.x
= (**native
->m_macPrintSettings
).rPaper
.left
;
279 dc
->m_macLocalOrigin
.y
= (**native
->m_macPrintSettings
).rPaper
.top
;
280 // m_macPrintingPort is now the current port
281 Rect clip
= { -32000 , -32000 , 32000 , 32000 } ;
282 ::ClipRect( &clip
) ;
284 if ( m_err
!= noErr
)
285 ::PrCloseDoc( m_macPrintingPort
) ;
288 void wxMacClassicPrinterDC::EndPage( wxPrinterDC
* dc
)
293 PrClosePage( m_macPrintingPort
) ;
295 if ( m_err
!= noErr
)
296 ::PrCloseDoc( m_macPrintingPort
) ;
301 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
304 m_printData
= printdata
;
305 m_printData
.ConvertToNative() ;
306 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
307 if ( m_nativePrinterDC
)
309 m_ok
= m_nativePrinterDC
->Ok() ;
314 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
315 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
321 wxPrinterDC::~wxPrinterDC(void)
323 delete m_nativePrinterDC
;
326 bool wxPrinterDC::StartDoc( const wxString
& message
)
328 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
333 if ( m_nativePrinterDC
->StartDoc(this, message
) )
335 // in case we have to do additional things when successful
337 m_ok
= m_nativePrinterDC
->Ok() ;
341 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
342 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
349 void wxPrinterDC::EndDoc(void)
354 m_nativePrinterDC
->EndDoc( this ) ;
355 m_ok
= m_nativePrinterDC
->Ok() ;
360 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
361 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
366 void wxPrinterDC::StartPage(void)
371 m_logicalFunction
= wxCOPY
;
372 // m_textAlignment = wxALIGN_TOP_LEFT;
373 m_backgroundMode
= wxTRANSPARENT
;
375 m_textForegroundColour
= *wxBLACK
;
376 m_textBackgroundColour
= *wxWHITE
;
377 m_pen
= *wxBLACK_PEN
;
378 m_font
= *wxNORMAL_FONT
;
379 m_brush
= *wxTRANSPARENT_BRUSH
;
380 m_backgroundBrush
= *wxWHITE_BRUSH
;
382 m_macFontInstalled
= false ;
383 m_macBrushInstalled
= false ;
384 m_macPenInstalled
= false ;
386 m_nativePrinterDC
->StartPage(this) ;
387 m_ok
= m_nativePrinterDC
->Ok() ;
391 void wxPrinterDC::EndPage(void)
396 m_nativePrinterDC
->EndPage(this) ;
397 m_ok
= m_nativePrinterDC
->Ok() ;
400 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
402 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
405 * width
= m_nativePrinterDC
->GetMaxX() ;
407 * height
= m_nativePrinterDC
->GetMaxY() ;