]>
Commit | Line | Data |
---|---|---|
72e7876b SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dcprint.cpp | |
3 | // Purpose: wxPrinterDC class | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
6aa89a22 | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
72e7876b SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
3d1a4878 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
72e7876b SC |
13 | #pragma implementation "dcprint.h" |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
179e085f RN |
19 | #if wxUSE_PRINTING_ARCHITECTURE |
20 | ||
72e7876b SC |
21 | #ifdef __BORLANDC__ |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | #endif | |
27 | ||
28 | #include "wx/dcprint.h" | |
03e11df5 | 29 | #include "wx/msgdlg.h" |
463c4d71 | 30 | #include "wx/math.h" |
2f1ae414 | 31 | #include "wx/mac/uma.h" |
746d7582 | 32 | #include "wx/mac/private/print.h" |
a3d3d3bf | 33 | |
72e7876b | 34 | IMPLEMENT_CLASS(wxPrinterDC, wxDC) |
72e7876b | 35 | |
746d7582 SC |
36 | class wxNativePrinterDC |
37 | { | |
38 | public : | |
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 ; } | |
eb7f8ac5 | 50 | |
746d7582 SC |
51 | static wxNativePrinterDC* Create(wxPrintData* data) ; |
52 | } ; | |
72e7876b | 53 | |
746d7582 SC |
54 | class wxMacCarbonPrinterDC : public wxNativePrinterDC |
55 | { | |
56 | public : | |
57 | wxMacCarbonPrinterDC( wxPrintData* data ) ; | |
58 | ~wxMacCarbonPrinterDC() ; | |
59 | virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) ; | |
60 | virtual void EndDoc( wxPrinterDC* dc ) ; | |
61 | virtual void StartPage( wxPrinterDC* dc ) ; | |
62 | virtual void EndPage( wxPrinterDC* dc ) ; | |
63 | virtual wxCoord GetMaxX() const { return m_maxX ; } | |
64 | virtual wxCoord GetMaxY() const { return m_maxY ; } | |
65 | virtual wxUint32 GetStatus() const { return m_err ; } | |
66 | private : | |
67 | GrafPtr m_macPrintFormerPort ; | |
68 | wxCoord m_maxX ; | |
69 | wxCoord m_maxY ; | |
70 | OSStatus m_err ; | |
71 | } ; | |
72 | ||
73 | wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data ) | |
72e7876b | 74 | { |
746d7582 SC |
75 | ::GetPort( & m_macPrintFormerPort ) ; |
76 | ||
77 | m_err = noErr ; | |
dc7ccb9c | 78 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ; |
eb7f8ac5 | 79 | |
746d7582 SC |
80 | PMRect rPage; |
81 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); | |
82 | if ( m_err != noErr ) | |
83 | return; | |
a689a4d0 | 84 | |
746d7582 SC |
85 | m_maxX = wxCoord(rPage.right - rPage.left) ; |
86 | m_maxY = wxCoord(rPage.bottom - rPage.top); | |
87 | } | |
75411508 | 88 | |
746d7582 SC |
89 | wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC() |
90 | { | |
91 | // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member | |
92 | ::SetPort( m_macPrintFormerPort ) ; | |
93 | } | |
94 | ||
95 | wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data) | |
96 | { | |
97 | return new wxMacCarbonPrinterDC(data) ; | |
98 | } | |
99 | ||
eb7f8ac5 | 100 | bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) ) |
746d7582 SC |
101 | { |
102 | if ( m_err ) | |
103 | return false ; | |
104 | ||
dc7ccb9c | 105 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 | 106 | |
20b69855 SC |
107 | #if wxMAC_USE_CORE_GRAPHICS |
108 | { | |
109 | CFStringRef s[1] = { kPMGraphicsContextCoreGraphics }; | |
110 | CFArrayRef graphicsContextsArray = CFArrayCreate(NULL, (const void**)s, 1, &kCFTypeArrayCallBacks); | |
111 | PMSessionSetDocumentFormatGeneration(native->m_macPrintSession, kPMDocumentFormatPDF, graphicsContextsArray, NULL); | |
112 | CFRelease(graphicsContextsArray); | |
113 | } | |
114 | #endif | |
115 | ||
746d7582 | 116 | m_err = PMSessionBeginDocument(native->m_macPrintSession, |
eb7f8ac5 | 117 | native->m_macPrintSettings, |
746d7582 SC |
118 | native->m_macPageFormat); |
119 | if ( m_err != noErr ) | |
120 | return false; | |
121 | ||
122 | PMRect rPage; | |
123 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); | |
124 | if ( m_err != noErr ) | |
125 | return false; | |
126 | ||
eb7f8ac5 VZ |
127 | m_maxX = (wxCoord)(rPage.right - rPage.left); |
128 | m_maxY = (wxCoord)(rPage.bottom - rPage.top); | |
746d7582 SC |
129 | return true ; |
130 | } | |
131 | ||
eb7f8ac5 | 132 | void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc ) |
746d7582 SC |
133 | { |
134 | if ( m_err ) | |
135 | return ; | |
136 | ||
dc7ccb9c | 137 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 SC |
138 | |
139 | m_err = PMSessionEndDocument(native->m_macPrintSession); | |
140 | } | |
141 | ||
eb7f8ac5 | 142 | void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc ) |
746d7582 SC |
143 | { |
144 | if ( m_err ) | |
145 | return ; | |
146 | ||
dc7ccb9c | 147 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 SC |
148 | |
149 | m_err = PMSessionBeginPage(native->m_macPrintSession, | |
150 | native->m_macPageFormat, | |
151 | nil); | |
eb7f8ac5 | 152 | |
20b69855 SC |
153 | #if wxMAC_USE_CORE_GRAPHICS |
154 | CGContextRef pageContext; | |
155 | #endif | |
746d7582 | 156 | if ( m_err == noErr ) |
e40298d5 | 157 | { |
20b69855 | 158 | #if wxMAC_USE_CORE_GRAPHICS |
eb7f8ac5 | 159 | m_err = PMSessionGetGraphicsContext(native->m_macPrintSession, |
20b69855 SC |
160 | kPMGraphicsContextCoreGraphics, |
161 | (void**) &pageContext ); | |
162 | dc->MacSetCGContext(pageContext) ; | |
163 | #else | |
164 | m_err = PMSessionGetGraphicsContext(native->m_macPrintSession, | |
165 | kPMGraphicsContextQuickdraw, | |
eb7f8ac5 | 166 | (void**) &dc->m_macPort ); |
20b69855 | 167 | #endif |
e40298d5 | 168 | } |
eb7f8ac5 | 169 | |
746d7582 | 170 | if ( m_err != noErr ) |
e40298d5 | 171 | { |
746d7582 SC |
172 | PMSessionEndPage(native->m_macPrintSession); |
173 | PMSessionEndDocument(native->m_macPrintSession); | |
e40298d5 | 174 | } |
746d7582 | 175 | else |
e40298d5 | 176 | { |
746d7582 | 177 | PMRect rPage; |
eb7f8ac5 | 178 | |
746d7582 SC |
179 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); |
180 | if ( !m_err ) | |
181 | { | |
20b69855 | 182 | #if wxMAC_USE_CORE_GRAPHICS |
a06e389f SC |
183 | PMRect paperRect ; |
184 | PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ; | |
185 | CGContextTranslateCTM( pageContext , -paperRect.left , -paperRect.top + ( rPage.bottom - rPage.top ) ) ; | |
20b69855 | 186 | CGContextScaleCTM( pageContext , 1 , -1 ) ; |
c725df80 | 187 | CGContextSaveGState( pageContext ) ; |
20b69855 | 188 | #else |
eb7f8ac5 VZ |
189 | dc->m_macLocalOrigin.x = (int) rPage.left; |
190 | dc->m_macLocalOrigin.y = (int) rPage.top; | |
20b69855 | 191 | #endif |
746d7582 SC |
192 | } |
193 | // since this is a non-critical error, we set the flag back | |
194 | m_err = noErr ; | |
e40298d5 | 195 | } |
746d7582 SC |
196 | } |
197 | ||
eb7f8ac5 | 198 | void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc ) |
746d7582 SC |
199 | { |
200 | if ( m_err ) | |
201 | return ; | |
202 | ||
dc7ccb9c | 203 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 SC |
204 | |
205 | m_err = PMSessionEndPage(native->m_macPrintSession); | |
206 | if ( m_err != noErr ) | |
f520d381 | 207 | { |
746d7582 | 208 | PMSessionEndDocument(native->m_macPrintSession); |
f520d381 | 209 | } |
746d7582 SC |
210 | } |
211 | ||
746d7582 SC |
212 | wxPrinterDC::wxPrinterDC(const wxPrintData& printdata) |
213 | { | |
214 | m_ok = FALSE ; | |
215 | m_printData = printdata ; | |
216 | m_printData.ConvertToNative() ; | |
217 | m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ; | |
eb7f8ac5 | 218 | if ( m_nativePrinterDC ) |
75411508 | 219 | { |
746d7582 | 220 | m_ok = m_nativePrinterDC->Ok() ; |
746d7582 SC |
221 | if ( !m_ok ) |
222 | { | |
223 | wxString message ; | |
eb7f8ac5 | 224 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; |
746d7582 SC |
225 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; |
226 | dialog.ShowModal(); | |
227 | } | |
20b69855 SC |
228 | #if wxMAC_USE_CORE_GRAPHICS |
229 | // the cgContext will only be handed over page by page | |
230 | m_graphicContext = new wxMacCGContext() ; | |
231 | #endif | |
75411508 | 232 | } |
746d7582 SC |
233 | } |
234 | ||
235 | wxPrinterDC::~wxPrinterDC(void) | |
236 | { | |
c725df80 SC |
237 | #if wxMAC_USE_CORE_GRAPHICS |
238 | // this context was borrowed | |
239 | ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ; | |
240 | #endif | |
746d7582 SC |
241 | delete m_nativePrinterDC ; |
242 | } | |
243 | ||
20b69855 SC |
244 | #if wxMAC_USE_CORE_GRAPHICS |
245 | void wxPrinterDC::MacSetCGContext( void * cg ) | |
246 | { | |
626fd619 | 247 | ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( (CGContextRef) cg ) ; |
20b69855 SC |
248 | m_graphicContext->SetPen( m_pen ) ; |
249 | m_graphicContext->SetBrush( m_brush ) ; | |
250 | } | |
251 | #endif | |
eb7f8ac5 | 252 | bool wxPrinterDC::StartDoc( const wxString& message ) |
746d7582 SC |
253 | { |
254 | wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ; | |
eb7f8ac5 | 255 | |
746d7582 SC |
256 | if ( !m_ok ) |
257 | return false ; | |
258 | ||
259 | if ( m_nativePrinterDC->StartDoc(this, message ) ) | |
260 | { | |
261 | // in case we have to do additional things when successful | |
262 | } | |
263 | m_ok = m_nativePrinterDC->Ok() ; | |
264 | if ( !m_ok ) | |
75411508 | 265 | { |
746d7582 | 266 | wxString message ; |
eb7f8ac5 | 267 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; |
427ff662 | 268 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; |
75411508 | 269 | dialog.ShowModal(); |
75411508 | 270 | } |
746d7582 | 271 | |
e40298d5 | 272 | return m_ok ; |
72e7876b SC |
273 | } |
274 | ||
eb7f8ac5 | 275 | void wxPrinterDC::EndDoc(void) |
72e7876b | 276 | { |
746d7582 SC |
277 | if ( !m_ok ) |
278 | return ; | |
279 | ||
280 | m_nativePrinterDC->EndDoc( this ) ; | |
281 | m_ok = m_nativePrinterDC->Ok() ; | |
282 | ||
283 | if ( !m_ok ) | |
75411508 | 284 | { |
746d7582 | 285 | wxString message ; |
eb7f8ac5 | 286 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; |
746d7582 SC |
287 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; |
288 | dialog.ShowModal(); | |
75411508 | 289 | } |
72e7876b SC |
290 | } |
291 | ||
eb7f8ac5 | 292 | void wxPrinterDC::StartPage(void) |
72e7876b | 293 | { |
e40298d5 JS |
294 | if ( !m_ok ) |
295 | return ; | |
2f1ae414 | 296 | |
746d7582 SC |
297 | m_logicalFunction = wxCOPY; |
298 | // m_textAlignment = wxALIGN_TOP_LEFT; | |
299 | m_backgroundMode = wxTRANSPARENT; | |
300 | ||
301 | m_textForegroundColour = *wxBLACK; | |
302 | m_textBackgroundColour = *wxWHITE; | |
303 | m_pen = *wxBLACK_PEN; | |
304 | m_font = *wxNORMAL_FONT; | |
305 | m_brush = *wxTRANSPARENT_BRUSH; | |
306 | m_backgroundBrush = *wxWHITE_BRUSH; | |
20b69855 | 307 | #if !wxMAC_USE_CORE_GRAPHICS |
e40298d5 JS |
308 | m_macFontInstalled = false ; |
309 | m_macBrushInstalled = false ; | |
310 | m_macPenInstalled = false ; | |
20b69855 | 311 | #endif |
2f1ae414 | 312 | |
746d7582 SC |
313 | m_nativePrinterDC->StartPage(this) ; |
314 | m_ok = m_nativePrinterDC->Ok() ; | |
eb7f8ac5 | 315 | |
72e7876b SC |
316 | } |
317 | ||
eb7f8ac5 | 318 | void wxPrinterDC::EndPage(void) |
72e7876b | 319 | { |
e40298d5 JS |
320 | if ( !m_ok ) |
321 | return ; | |
72e7876b | 322 | |
746d7582 SC |
323 | m_nativePrinterDC->EndPage(this) ; |
324 | m_ok = m_nativePrinterDC->Ok() ; | |
325 | } | |
72e7876b | 326 | |
746d7582 SC |
327 | void wxPrinterDC::DoGetSize(int *width, int *height) const |
328 | { | |
329 | wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") ); | |
2f1ae414 | 330 | |
746d7582 SC |
331 | if ( width ) |
332 | * width = m_nativePrinterDC->GetMaxX() ; | |
333 | if ( height ) | |
334 | * height = m_nativePrinterDC->GetMaxY() ; | |
72e7876b | 335 | } |
746d7582 | 336 | |
179e085f | 337 | #endif |