]>
Commit | Line | Data |
---|---|---|
72e7876b | 1 | ///////////////////////////////////////////////////////////////////////////// |
6d50343d | 2 | // Name: src/mac/carbon/dcprint.cpp |
72e7876b SC |
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 |
6d50343d | 9 | // Licence: wxWindows licence |
72e7876b SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
72e7876b SC |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
179e085f RN |
15 | #if wxUSE_PRINTING_ARCHITECTURE |
16 | ||
72e7876b | 17 | #ifdef __BORLANDC__ |
6d50343d | 18 | #pragma hdrstop |
72e7876b SC |
19 | #endif |
20 | ||
6d50343d WS |
21 | #include "wx/dcprint.h" |
22 | ||
72e7876b | 23 | #ifndef WX_PRECOMP |
246c5004 | 24 | #include "wx/msgdlg.h" |
18680f86 | 25 | #include "wx/math.h" |
72e7876b SC |
26 | #endif |
27 | ||
2f1ae414 | 28 | #include "wx/mac/uma.h" |
746d7582 | 29 | #include "wx/mac/private/print.h" |
8acd14d1 | 30 | #include "wx/graphics.h" |
a3d3d3bf | 31 | |
72e7876b | 32 | IMPLEMENT_CLASS(wxPrinterDC, wxDC) |
72e7876b | 33 | |
746d7582 SC |
34 | class wxNativePrinterDC |
35 | { | |
36 | public : | |
37 | wxNativePrinterDC() {} | |
38 | virtual ~wxNativePrinterDC() {} | |
39 | virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) = 0; | |
40 | virtual void EndDoc( wxPrinterDC* dc ) = 0; | |
41 | virtual void StartPage( wxPrinterDC* dc ) = 0; | |
42 | virtual void EndPage( wxPrinterDC* dc ) = 0; | |
db49000e SC |
43 | virtual void GetSize( int *w , int *h) const = 0 ; |
44 | virtual wxSize GetPPI() const = 0 ; | |
45 | ||
746d7582 SC |
46 | // returns 0 in case of no Error, otherwise platform specific error codes |
47 | virtual wxUint32 GetStatus() const = 0 ; | |
48 | bool Ok() { return GetStatus() == 0 ; } | |
eb7f8ac5 | 49 | |
746d7582 SC |
50 | static wxNativePrinterDC* Create(wxPrintData* data) ; |
51 | } ; | |
72e7876b | 52 | |
746d7582 SC |
53 | class wxMacCarbonPrinterDC : public wxNativePrinterDC |
54 | { | |
55 | public : | |
56 | wxMacCarbonPrinterDC( wxPrintData* data ) ; | |
d3c7fc99 | 57 | virtual ~wxMacCarbonPrinterDC() ; |
746d7582 SC |
58 | virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) ; |
59 | virtual void EndDoc( wxPrinterDC* dc ) ; | |
60 | virtual void StartPage( wxPrinterDC* dc ) ; | |
61 | virtual void EndPage( wxPrinterDC* dc ) ; | |
746d7582 | 62 | virtual wxUint32 GetStatus() const { return m_err ; } |
db49000e SC |
63 | virtual void GetSize( int *w , int *h) const ; |
64 | virtual wxSize GetPPI() const ; | |
746d7582 | 65 | private : |
4f74e0d1 | 66 | #if !wxMAC_USE_CORE_GRAPHICS |
746d7582 | 67 | GrafPtr m_macPrintFormerPort ; |
4f74e0d1 | 68 | #endif |
746d7582 SC |
69 | wxCoord m_maxX ; |
70 | wxCoord m_maxY ; | |
db49000e | 71 | wxSize m_ppi ; |
746d7582 SC |
72 | OSStatus m_err ; |
73 | } ; | |
74 | ||
75 | wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data ) | |
72e7876b | 76 | { |
4f74e0d1 | 77 | #if !wxMAC_USE_CORE_GRAPHICS |
746d7582 | 78 | ::GetPort( & m_macPrintFormerPort ) ; |
4f74e0d1 | 79 | #endif |
746d7582 | 80 | m_err = noErr ; |
dc7ccb9c | 81 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ; |
eb7f8ac5 | 82 | |
746d7582 SC |
83 | PMRect rPage; |
84 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); | |
85 | if ( m_err != noErr ) | |
86 | return; | |
a689a4d0 | 87 | |
746d7582 SC |
88 | m_maxX = wxCoord(rPage.right - rPage.left) ; |
89 | m_maxY = wxCoord(rPage.bottom - rPage.top); | |
db49000e SC |
90 | |
91 | PMResolution res; | |
4f74e0d1 SC |
92 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 |
93 | PMPrinter printer; | |
94 | PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer); | |
95 | PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ; | |
96 | #else | |
db49000e | 97 | m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res); |
4f74e0d1 | 98 | #endif |
f7862d3e | 99 | m_ppi = wxSize(int(res.hRes), int(res.vRes)); |
746d7582 | 100 | } |
75411508 | 101 | |
746d7582 SC |
102 | wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC() |
103 | { | |
4f74e0d1 | 104 | #if !wxMAC_USE_CORE_GRAPHICS |
746d7582 SC |
105 | // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member |
106 | ::SetPort( m_macPrintFormerPort ) ; | |
4f74e0d1 | 107 | #endif |
746d7582 SC |
108 | } |
109 | ||
110 | wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data) | |
111 | { | |
112 | return new wxMacCarbonPrinterDC(data) ; | |
113 | } | |
114 | ||
eb7f8ac5 | 115 | bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) ) |
746d7582 SC |
116 | { |
117 | if ( m_err ) | |
118 | return false ; | |
119 | ||
dc7ccb9c | 120 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 | 121 | |
4f74e0d1 | 122 | #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4 && wxMAC_USE_CORE_GRAPHICS |
20b69855 SC |
123 | { |
124 | CFStringRef s[1] = { kPMGraphicsContextCoreGraphics }; | |
125 | CFArrayRef graphicsContextsArray = CFArrayCreate(NULL, (const void**)s, 1, &kCFTypeArrayCallBacks); | |
126 | PMSessionSetDocumentFormatGeneration(native->m_macPrintSession, kPMDocumentFormatPDF, graphicsContextsArray, NULL); | |
127 | CFRelease(graphicsContextsArray); | |
128 | } | |
129 | #endif | |
4f74e0d1 SC |
130 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4 && wxMAC_USE_CORE_GRAPHICS |
131 | m_err = PMSessionBeginCGDocument(native->m_macPrintSession, | |
132 | native->m_macPrintSettings, | |
133 | native->m_macPageFormat); | |
134 | #else | |
746d7582 | 135 | m_err = PMSessionBeginDocument(native->m_macPrintSession, |
eb7f8ac5 | 136 | native->m_macPrintSettings, |
746d7582 | 137 | native->m_macPageFormat); |
4f74e0d1 SC |
138 | |
139 | #endif | |
140 | ||
746d7582 SC |
141 | if ( m_err != noErr ) |
142 | return false; | |
143 | ||
144 | PMRect rPage; | |
145 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); | |
146 | if ( m_err != noErr ) | |
9fff7273 | 147 | return false ; |
db49000e SC |
148 | |
149 | m_maxX = wxCoord(rPage.right - rPage.left) ; | |
150 | m_maxY = wxCoord(rPage.bottom - rPage.top); | |
746d7582 | 151 | |
db49000e | 152 | PMResolution res; |
4f74e0d1 SC |
153 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 |
154 | PMPrinter printer; | |
155 | PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer); | |
156 | PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ; | |
157 | #else | |
db49000e | 158 | m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res); |
4f74e0d1 | 159 | #endif |
db49000e | 160 | m_ppi = wxSize(int(res.hRes), int(res.vRes)); |
746d7582 SC |
161 | return true ; |
162 | } | |
163 | ||
eb7f8ac5 | 164 | void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc ) |
746d7582 SC |
165 | { |
166 | if ( m_err ) | |
167 | return ; | |
168 | ||
dc7ccb9c | 169 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 SC |
170 | |
171 | m_err = PMSessionEndDocument(native->m_macPrintSession); | |
172 | } | |
173 | ||
eb7f8ac5 | 174 | void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc ) |
746d7582 SC |
175 | { |
176 | if ( m_err ) | |
177 | return ; | |
178 | ||
dc7ccb9c | 179 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 SC |
180 | |
181 | m_err = PMSessionBeginPage(native->m_macPrintSession, | |
182 | native->m_macPageFormat, | |
183 | nil); | |
eb7f8ac5 | 184 | |
20b69855 SC |
185 | #if wxMAC_USE_CORE_GRAPHICS |
186 | CGContextRef pageContext; | |
187 | #endif | |
746d7582 | 188 | if ( m_err == noErr ) |
e40298d5 | 189 | { |
20b69855 | 190 | #if wxMAC_USE_CORE_GRAPHICS |
4f74e0d1 SC |
191 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4 |
192 | m_err = PMSessionGetCGGraphicsContext(native->m_macPrintSession, | |
193 | &pageContext ); | |
194 | ||
195 | #else | |
eb7f8ac5 | 196 | m_err = PMSessionGetGraphicsContext(native->m_macPrintSession, |
20b69855 SC |
197 | kPMGraphicsContextCoreGraphics, |
198 | (void**) &pageContext ); | |
4f74e0d1 | 199 | #endif |
20b69855 SC |
200 | #else |
201 | m_err = PMSessionGetGraphicsContext(native->m_macPrintSession, | |
202 | kPMGraphicsContextQuickdraw, | |
eb7f8ac5 | 203 | (void**) &dc->m_macPort ); |
20b69855 | 204 | #endif |
e40298d5 | 205 | } |
eb7f8ac5 | 206 | |
746d7582 | 207 | if ( m_err != noErr ) |
e40298d5 | 208 | { |
746d7582 SC |
209 | PMSessionEndPage(native->m_macPrintSession); |
210 | PMSessionEndDocument(native->m_macPrintSession); | |
e40298d5 | 211 | } |
746d7582 | 212 | else |
e40298d5 | 213 | { |
746d7582 | 214 | PMRect rPage; |
eb7f8ac5 | 215 | |
746d7582 SC |
216 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); |
217 | if ( !m_err ) | |
218 | { | |
20b69855 | 219 | #if wxMAC_USE_CORE_GRAPHICS |
a06e389f SC |
220 | PMRect paperRect ; |
221 | PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ; | |
222 | CGContextTranslateCTM( pageContext , -paperRect.left , -paperRect.top + ( rPage.bottom - rPage.top ) ) ; | |
20b69855 SC |
223 | CGContextScaleCTM( pageContext , 1 , -1 ) ; |
224 | #else | |
eb7f8ac5 VZ |
225 | dc->m_macLocalOrigin.x = (int) rPage.left; |
226 | dc->m_macLocalOrigin.y = (int) rPage.top; | |
20b69855 | 227 | #endif |
746d7582 SC |
228 | } |
229 | // since this is a non-critical error, we set the flag back | |
230 | m_err = noErr ; | |
e40298d5 | 231 | } |
f518afb5 SC |
232 | #if wxMAC_USE_CORE_GRAPHICS |
233 | dc->MacSetCGContext(pageContext) ; | |
234 | #endif | |
746d7582 SC |
235 | } |
236 | ||
eb7f8ac5 | 237 | void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc ) |
746d7582 SC |
238 | { |
239 | if ( m_err ) | |
240 | return ; | |
241 | ||
dc7ccb9c | 242 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 SC |
243 | |
244 | m_err = PMSessionEndPage(native->m_macPrintSession); | |
245 | if ( m_err != noErr ) | |
f520d381 | 246 | { |
746d7582 | 247 | PMSessionEndDocument(native->m_macPrintSession); |
f520d381 | 248 | } |
f518afb5 SC |
249 | #if wxMAC_USE_CORE_GRAPHICS |
250 | dc->MacSetCGContext(NULL) ; | |
251 | #endif | |
746d7582 SC |
252 | } |
253 | ||
db49000e SC |
254 | void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const |
255 | { | |
256 | if ( w ) | |
257 | *w = m_maxX ; | |
258 | if ( h ) | |
259 | *h = m_maxY ; | |
260 | } | |
261 | ||
262 | wxSize wxMacCarbonPrinterDC::GetPPI() const | |
263 | { | |
264 | return m_ppi ; | |
265 | }; | |
266 | ||
267 | // | |
268 | // | |
269 | // | |
270 | ||
746d7582 SC |
271 | wxPrinterDC::wxPrinterDC(const wxPrintData& printdata) |
272 | { | |
6d50343d | 273 | m_ok = false ; |
746d7582 SC |
274 | m_printData = printdata ; |
275 | m_printData.ConvertToNative() ; | |
276 | m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ; | |
eb7f8ac5 | 277 | if ( m_nativePrinterDC ) |
75411508 | 278 | { |
746d7582 | 279 | m_ok = m_nativePrinterDC->Ok() ; |
746d7582 SC |
280 | if ( !m_ok ) |
281 | { | |
282 | wxString message ; | |
eb7f8ac5 | 283 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; |
746d7582 SC |
284 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; |
285 | dialog.ShowModal(); | |
286 | } | |
db49000e SC |
287 | else |
288 | { | |
289 | wxSize sz = GetPPI(); | |
290 | m_mm_to_pix_x = mm2inches * sz.x; | |
291 | m_mm_to_pix_y = mm2inches * sz.y; | |
292 | } | |
20b69855 | 293 | #if wxMAC_USE_CORE_GRAPHICS |
f7862d3e | 294 | /* |
20b69855 SC |
295 | // the cgContext will only be handed over page by page |
296 | m_graphicContext = new wxMacCGContext() ; | |
f7862d3e | 297 | */ |
20b69855 | 298 | #endif |
75411508 | 299 | } |
746d7582 SC |
300 | } |
301 | ||
db49000e SC |
302 | wxSize wxPrinterDC::GetPPI() const |
303 | { | |
304 | return m_nativePrinterDC->GetPPI() ; | |
305 | } | |
306 | ||
746d7582 SC |
307 | wxPrinterDC::~wxPrinterDC(void) |
308 | { | |
c725df80 | 309 | #if wxMAC_USE_CORE_GRAPHICS |
f7862d3e | 310 | /* |
c725df80 SC |
311 | // this context was borrowed |
312 | ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ; | |
f7862d3e | 313 | */ |
c725df80 | 314 | #endif |
746d7582 SC |
315 | delete m_nativePrinterDC ; |
316 | } | |
317 | ||
20b69855 | 318 | #if wxMAC_USE_CORE_GRAPHICS |
6d50343d | 319 | void wxPrinterDC::MacSetCGContext( void * cg ) |
20b69855 | 320 | { |
f7862d3e | 321 | SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg ) ); |
20b69855 SC |
322 | m_graphicContext->SetPen( m_pen ) ; |
323 | m_graphicContext->SetBrush( m_brush ) ; | |
324 | } | |
325 | #endif | |
eb7f8ac5 | 326 | bool wxPrinterDC::StartDoc( const wxString& message ) |
746d7582 SC |
327 | { |
328 | wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ; | |
eb7f8ac5 | 329 | |
746d7582 SC |
330 | if ( !m_ok ) |
331 | return false ; | |
332 | ||
333 | if ( m_nativePrinterDC->StartDoc(this, message ) ) | |
334 | { | |
335 | // in case we have to do additional things when successful | |
336 | } | |
337 | m_ok = m_nativePrinterDC->Ok() ; | |
338 | if ( !m_ok ) | |
75411508 | 339 | { |
746d7582 | 340 | wxString message ; |
eb7f8ac5 | 341 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; |
427ff662 | 342 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; |
75411508 | 343 | dialog.ShowModal(); |
75411508 | 344 | } |
746d7582 | 345 | |
e40298d5 | 346 | return m_ok ; |
72e7876b SC |
347 | } |
348 | ||
eb7f8ac5 | 349 | void wxPrinterDC::EndDoc(void) |
72e7876b | 350 | { |
746d7582 SC |
351 | if ( !m_ok ) |
352 | return ; | |
353 | ||
354 | m_nativePrinterDC->EndDoc( this ) ; | |
355 | m_ok = m_nativePrinterDC->Ok() ; | |
356 | ||
357 | if ( !m_ok ) | |
75411508 | 358 | { |
746d7582 | 359 | wxString message ; |
eb7f8ac5 | 360 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; |
746d7582 SC |
361 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; |
362 | dialog.ShowModal(); | |
75411508 | 363 | } |
72e7876b SC |
364 | } |
365 | ||
f415cab9 JS |
366 | wxRect wxPrinterDC::GetPaperRect() |
367 | { | |
368 | wxCoord w, h; | |
369 | GetSize(&w, &h); | |
370 | wxRect pageRect(0, 0, w, h); | |
371 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) m_printData.GetNativeData() ; | |
372 | OSStatus err = noErr ; | |
373 | PMRect rPaper; | |
374 | err = PMGetAdjustedPaperRect(native->m_macPageFormat, &rPaper); | |
375 | if ( err != noErr ) | |
376 | return pageRect; | |
377 | return wxRect(wxCoord(rPaper.left), wxCoord(rPaper.top), | |
378 | wxCoord(rPaper.right - rPaper.left), wxCoord(rPaper.bottom - rPaper.top)); | |
379 | } | |
380 | ||
eb7f8ac5 | 381 | void wxPrinterDC::StartPage(void) |
72e7876b | 382 | { |
e40298d5 JS |
383 | if ( !m_ok ) |
384 | return ; | |
2f1ae414 | 385 | |
746d7582 SC |
386 | m_logicalFunction = wxCOPY; |
387 | // m_textAlignment = wxALIGN_TOP_LEFT; | |
388 | m_backgroundMode = wxTRANSPARENT; | |
389 | ||
390 | m_textForegroundColour = *wxBLACK; | |
391 | m_textBackgroundColour = *wxWHITE; | |
392 | m_pen = *wxBLACK_PEN; | |
393 | m_font = *wxNORMAL_FONT; | |
394 | m_brush = *wxTRANSPARENT_BRUSH; | |
395 | m_backgroundBrush = *wxWHITE_BRUSH; | |
20b69855 | 396 | #if !wxMAC_USE_CORE_GRAPHICS |
e40298d5 JS |
397 | m_macFontInstalled = false ; |
398 | m_macBrushInstalled = false ; | |
399 | m_macPenInstalled = false ; | |
20b69855 | 400 | #endif |
2f1ae414 | 401 | |
746d7582 SC |
402 | m_nativePrinterDC->StartPage(this) ; |
403 | m_ok = m_nativePrinterDC->Ok() ; | |
eb7f8ac5 | 404 | |
72e7876b SC |
405 | } |
406 | ||
eb7f8ac5 | 407 | void wxPrinterDC::EndPage(void) |
72e7876b | 408 | { |
e40298d5 JS |
409 | if ( !m_ok ) |
410 | return ; | |
72e7876b | 411 | |
746d7582 SC |
412 | m_nativePrinterDC->EndPage(this) ; |
413 | m_ok = m_nativePrinterDC->Ok() ; | |
414 | } | |
72e7876b | 415 | |
746d7582 SC |
416 | void wxPrinterDC::DoGetSize(int *width, int *height) const |
417 | { | |
418 | wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") ); | |
db49000e | 419 | m_nativePrinterDC->GetSize(width, height ) ; |
72e7876b | 420 | } |
746d7582 | 421 | |
179e085f | 422 | #endif |