]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcprint.cpp
moving rounded rect and ellipse to path class
[wxWidgets.git] / src / mac / carbon / dcprint.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dcprint.cpp
3 // Purpose: wxPrinterDC class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_PRINTING_ARCHITECTURE
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #include "wx/dcprint.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/msgdlg.h"
25 #include "wx/math.h"
26 #endif
27
28 #include "wx/mac/uma.h"
29 #include "wx/mac/private/print.h"
30 #include "wx/graphics.h"
31
32 IMPLEMENT_CLASS(wxPrinterDC, wxDC)
33
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;
43 virtual void GetSize( int *w , int *h) const = 0 ;
44 virtual wxSize GetPPI() const = 0 ;
45
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 ; }
49
50 static wxNativePrinterDC* Create(wxPrintData* data) ;
51 } ;
52
53 class wxMacCarbonPrinterDC : public wxNativePrinterDC
54 {
55 public :
56 wxMacCarbonPrinterDC( wxPrintData* data ) ;
57 virtual ~wxMacCarbonPrinterDC() ;
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 ) ;
62 virtual wxUint32 GetStatus() const { return m_err ; }
63 virtual void GetSize( int *w , int *h) const ;
64 virtual wxSize GetPPI() const ;
65 private :
66 #if !wxMAC_USE_CORE_GRAPHICS
67 GrafPtr m_macPrintFormerPort ;
68 #endif
69 wxCoord m_maxX ;
70 wxCoord m_maxY ;
71 wxSize m_ppi ;
72 OSStatus m_err ;
73 } ;
74
75 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
76 {
77 #if !wxMAC_USE_CORE_GRAPHICS
78 ::GetPort( & m_macPrintFormerPort ) ;
79 #endif
80 m_err = noErr ;
81 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ;
82
83 PMRect rPage;
84 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
85 if ( m_err != noErr )
86 return;
87
88 m_maxX = wxCoord(rPage.right - rPage.left) ;
89 m_maxY = wxCoord(rPage.bottom - rPage.top);
90
91 PMResolution res;
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
97 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
98 #endif
99 m_ppi = wxSize(int(res.hRes), int(res.vRes));
100 }
101
102 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
103 {
104 #if !wxMAC_USE_CORE_GRAPHICS
105 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
106 ::SetPort( m_macPrintFormerPort ) ;
107 #endif
108 }
109
110 wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
111 {
112 return new wxMacCarbonPrinterDC(data) ;
113 }
114
115 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
116 {
117 if ( m_err )
118 return false ;
119
120 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
121
122 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4 && wxMAC_USE_CORE_GRAPHICS
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
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
135 m_err = PMSessionBeginDocument(native->m_macPrintSession,
136 native->m_macPrintSettings,
137 native->m_macPageFormat);
138
139 #endif
140
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 )
147 return false ;
148
149 m_maxX = wxCoord(rPage.right - rPage.left) ;
150 m_maxY = wxCoord(rPage.bottom - rPage.top);
151
152 PMResolution res;
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
158 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
159 #endif
160 m_ppi = wxSize(int(res.hRes), int(res.vRes));
161 return true ;
162 }
163
164 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
165 {
166 if ( m_err )
167 return ;
168
169 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
170
171 m_err = PMSessionEndDocument(native->m_macPrintSession);
172 }
173
174 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
175 {
176 if ( m_err )
177 return ;
178
179 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
180
181 m_err = PMSessionBeginPage(native->m_macPrintSession,
182 native->m_macPageFormat,
183 nil);
184
185 #if wxMAC_USE_CORE_GRAPHICS
186 CGContextRef pageContext;
187 #endif
188 if ( m_err == noErr )
189 {
190 #if wxMAC_USE_CORE_GRAPHICS
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
196 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
197 kPMGraphicsContextCoreGraphics,
198 (void**) &pageContext );
199 #endif
200 dc->MacSetCGContext(pageContext) ;
201 #else
202 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
203 kPMGraphicsContextQuickdraw,
204 (void**) &dc->m_macPort );
205 #endif
206 }
207
208 if ( m_err != noErr )
209 {
210 PMSessionEndPage(native->m_macPrintSession);
211 PMSessionEndDocument(native->m_macPrintSession);
212 }
213 else
214 {
215 PMRect rPage;
216
217 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
218 if ( !m_err )
219 {
220 #if wxMAC_USE_CORE_GRAPHICS
221 PMRect paperRect ;
222 PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ;
223 CGContextTranslateCTM( pageContext , -paperRect.left , -paperRect.top + ( rPage.bottom - rPage.top ) ) ;
224 CGContextScaleCTM( pageContext , 1 , -1 ) ;
225 CGContextSaveGState( pageContext ) ;
226 #else
227 dc->m_macLocalOrigin.x = (int) rPage.left;
228 dc->m_macLocalOrigin.y = (int) rPage.top;
229 #endif
230 }
231 // since this is a non-critical error, we set the flag back
232 m_err = noErr ;
233 }
234 }
235
236 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
237 {
238 if ( m_err )
239 return ;
240
241 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
242
243 m_err = PMSessionEndPage(native->m_macPrintSession);
244 if ( m_err != noErr )
245 {
246 PMSessionEndDocument(native->m_macPrintSession);
247 }
248 }
249
250 void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const
251 {
252 if ( w )
253 *w = m_maxX ;
254 if ( h )
255 *h = m_maxY ;
256 }
257
258 wxSize wxMacCarbonPrinterDC::GetPPI() const
259 {
260 return m_ppi ;
261 };
262
263 //
264 //
265 //
266
267 wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
268 {
269 m_ok = false ;
270 m_printData = printdata ;
271 m_printData.ConvertToNative() ;
272 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
273 if ( m_nativePrinterDC )
274 {
275 m_ok = m_nativePrinterDC->Ok() ;
276 if ( !m_ok )
277 {
278 wxString message ;
279 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
280 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
281 dialog.ShowModal();
282 }
283 else
284 {
285 wxSize sz = GetPPI();
286 m_mm_to_pix_x = mm2inches * sz.x;
287 m_mm_to_pix_y = mm2inches * sz.y;
288 }
289 #if wxMAC_USE_CORE_GRAPHICS
290 /*
291 // the cgContext will only be handed over page by page
292 m_graphicContext = new wxMacCGContext() ;
293 */
294 #endif
295 }
296 }
297
298 wxSize wxPrinterDC::GetPPI() const
299 {
300 return m_nativePrinterDC->GetPPI() ;
301 }
302
303 wxPrinterDC::~wxPrinterDC(void)
304 {
305 #if wxMAC_USE_CORE_GRAPHICS
306 /*
307 // this context was borrowed
308 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ;
309 */
310 #endif
311 delete m_nativePrinterDC ;
312 }
313
314 #if wxMAC_USE_CORE_GRAPHICS
315 void wxPrinterDC::MacSetCGContext( void * cg )
316 {
317 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg ) );
318 m_graphicContext->SetPen( m_pen ) ;
319 m_graphicContext->SetBrush( m_brush ) ;
320 }
321 #endif
322 bool wxPrinterDC::StartDoc( const wxString& message )
323 {
324 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
325
326 if ( !m_ok )
327 return false ;
328
329 if ( m_nativePrinterDC->StartDoc(this, message ) )
330 {
331 // in case we have to do additional things when successful
332 }
333 m_ok = m_nativePrinterDC->Ok() ;
334 if ( !m_ok )
335 {
336 wxString message ;
337 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
338 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
339 dialog.ShowModal();
340 }
341
342 return m_ok ;
343 }
344
345 void wxPrinterDC::EndDoc(void)
346 {
347 if ( !m_ok )
348 return ;
349
350 m_nativePrinterDC->EndDoc( this ) ;
351 m_ok = m_nativePrinterDC->Ok() ;
352
353 if ( !m_ok )
354 {
355 wxString message ;
356 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
357 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
358 dialog.ShowModal();
359 }
360 }
361
362 void wxPrinterDC::StartPage(void)
363 {
364 if ( !m_ok )
365 return ;
366
367 m_logicalFunction = wxCOPY;
368 // m_textAlignment = wxALIGN_TOP_LEFT;
369 m_backgroundMode = wxTRANSPARENT;
370
371 m_textForegroundColour = *wxBLACK;
372 m_textBackgroundColour = *wxWHITE;
373 m_pen = *wxBLACK_PEN;
374 m_font = *wxNORMAL_FONT;
375 m_brush = *wxTRANSPARENT_BRUSH;
376 m_backgroundBrush = *wxWHITE_BRUSH;
377 #if !wxMAC_USE_CORE_GRAPHICS
378 m_macFontInstalled = false ;
379 m_macBrushInstalled = false ;
380 m_macPenInstalled = false ;
381 #endif
382
383 m_nativePrinterDC->StartPage(this) ;
384 m_ok = m_nativePrinterDC->Ok() ;
385
386 }
387
388 void wxPrinterDC::EndPage(void)
389 {
390 if ( !m_ok )
391 return ;
392
393 m_nativePrinterDC->EndPage(this) ;
394 m_ok = m_nativePrinterDC->Ok() ;
395 }
396
397 void wxPrinterDC::DoGetSize(int *width, int *height) const
398 {
399 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
400 m_nativePrinterDC->GetSize(width, height ) ;
401 }
402
403 #endif