]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcprint.cpp
27a2734309dfb4a3988db57e73d456fc212e90a1
[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 #endif
25
26 #include "wx/msgdlg.h"
27 #include "wx/math.h"
28 #include "wx/mac/uma.h"
29 #include "wx/mac/private/print.h"
30
31 IMPLEMENT_CLASS(wxPrinterDC, wxDC)
32
33 class wxNativePrinterDC
34 {
35 public :
36 wxNativePrinterDC() {}
37 virtual ~wxNativePrinterDC() {}
38 virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) = 0;
39 virtual void EndDoc( wxPrinterDC* dc ) = 0;
40 virtual void StartPage( wxPrinterDC* dc ) = 0;
41 virtual void EndPage( wxPrinterDC* dc ) = 0;
42 virtual wxCoord GetMaxX() const = 0 ;
43 virtual wxCoord GetMaxY() const = 0 ;
44 // returns 0 in case of no Error, otherwise platform specific error codes
45 virtual wxUint32 GetStatus() const = 0 ;
46 bool Ok() { return GetStatus() == 0 ; }
47
48 static wxNativePrinterDC* Create(wxPrintData* data) ;
49 } ;
50
51 class wxMacCarbonPrinterDC : public wxNativePrinterDC
52 {
53 public :
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 ; }
63 private :
64 GrafPtr m_macPrintFormerPort ;
65 wxCoord m_maxX ;
66 wxCoord m_maxY ;
67 OSStatus m_err ;
68 } ;
69
70 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
71 {
72 ::GetPort( & m_macPrintFormerPort ) ;
73
74 m_err = noErr ;
75 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ;
76
77 PMRect rPage;
78 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
79 if ( m_err != noErr )
80 return;
81
82 m_maxX = wxCoord(rPage.right - rPage.left) ;
83 m_maxY = wxCoord(rPage.bottom - rPage.top);
84 }
85
86 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
87 {
88 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
89 ::SetPort( m_macPrintFormerPort ) ;
90 }
91
92 wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
93 {
94 return new wxMacCarbonPrinterDC(data) ;
95 }
96
97 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
98 {
99 if ( m_err )
100 return false ;
101
102 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
103
104 #if wxMAC_USE_CORE_GRAPHICS
105 {
106 CFStringRef s[1] = { kPMGraphicsContextCoreGraphics };
107 CFArrayRef graphicsContextsArray = CFArrayCreate(NULL, (const void**)s, 1, &kCFTypeArrayCallBacks);
108 PMSessionSetDocumentFormatGeneration(native->m_macPrintSession, kPMDocumentFormatPDF, graphicsContextsArray, NULL);
109 CFRelease(graphicsContextsArray);
110 }
111 #endif
112
113 m_err = PMSessionBeginDocument(native->m_macPrintSession,
114 native->m_macPrintSettings,
115 native->m_macPageFormat);
116 if ( m_err != noErr )
117 return false;
118
119 PMRect rPage;
120 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
121 if ( m_err != noErr )
122 return false;
123
124 m_maxX = (wxCoord)(rPage.right - rPage.left);
125 m_maxY = (wxCoord)(rPage.bottom - rPage.top);
126 return true ;
127 }
128
129 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
130 {
131 if ( m_err )
132 return ;
133
134 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
135
136 m_err = PMSessionEndDocument(native->m_macPrintSession);
137 }
138
139 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
140 {
141 if ( m_err )
142 return ;
143
144 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
145
146 m_err = PMSessionBeginPage(native->m_macPrintSession,
147 native->m_macPageFormat,
148 nil);
149
150 #if wxMAC_USE_CORE_GRAPHICS
151 CGContextRef pageContext;
152 #endif
153 if ( m_err == noErr )
154 {
155 #if wxMAC_USE_CORE_GRAPHICS
156 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
157 kPMGraphicsContextCoreGraphics,
158 (void**) &pageContext );
159 dc->MacSetCGContext(pageContext) ;
160 #else
161 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
162 kPMGraphicsContextQuickdraw,
163 (void**) &dc->m_macPort );
164 #endif
165 }
166
167 if ( m_err != noErr )
168 {
169 PMSessionEndPage(native->m_macPrintSession);
170 PMSessionEndDocument(native->m_macPrintSession);
171 }
172 else
173 {
174 PMRect rPage;
175
176 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
177 if ( !m_err )
178 {
179 #if wxMAC_USE_CORE_GRAPHICS
180 PMRect paperRect ;
181 PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ;
182 CGContextTranslateCTM( pageContext , -paperRect.left , -paperRect.top + ( rPage.bottom - rPage.top ) ) ;
183 CGContextScaleCTM( pageContext , 1 , -1 ) ;
184 CGContextSaveGState( pageContext ) ;
185 #else
186 dc->m_macLocalOrigin.x = (int) rPage.left;
187 dc->m_macLocalOrigin.y = (int) rPage.top;
188 #endif
189 }
190 // since this is a non-critical error, we set the flag back
191 m_err = noErr ;
192 }
193 }
194
195 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
196 {
197 if ( m_err )
198 return ;
199
200 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
201
202 m_err = PMSessionEndPage(native->m_macPrintSession);
203 if ( m_err != noErr )
204 {
205 PMSessionEndDocument(native->m_macPrintSession);
206 }
207 }
208
209 wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
210 {
211 m_ok = false ;
212 m_printData = printdata ;
213 m_printData.ConvertToNative() ;
214 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
215 if ( m_nativePrinterDC )
216 {
217 m_ok = m_nativePrinterDC->Ok() ;
218 if ( !m_ok )
219 {
220 wxString message ;
221 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
222 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
223 dialog.ShowModal();
224 }
225 #if wxMAC_USE_CORE_GRAPHICS
226 // the cgContext will only be handed over page by page
227 m_graphicContext = new wxMacCGContext() ;
228 #endif
229 }
230 }
231
232 wxPrinterDC::~wxPrinterDC(void)
233 {
234 #if wxMAC_USE_CORE_GRAPHICS
235 // this context was borrowed
236 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ;
237 #endif
238 delete m_nativePrinterDC ;
239 }
240
241 #if wxMAC_USE_CORE_GRAPHICS
242 void wxPrinterDC::MacSetCGContext( void * cg )
243 {
244 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( (CGContextRef) cg ) ;
245 m_graphicContext->SetPen( m_pen ) ;
246 m_graphicContext->SetBrush( m_brush ) ;
247 }
248 #endif
249 bool wxPrinterDC::StartDoc( const wxString& message )
250 {
251 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
252
253 if ( !m_ok )
254 return false ;
255
256 if ( m_nativePrinterDC->StartDoc(this, message ) )
257 {
258 // in case we have to do additional things when successful
259 }
260 m_ok = m_nativePrinterDC->Ok() ;
261 if ( !m_ok )
262 {
263 wxString message ;
264 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
265 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
266 dialog.ShowModal();
267 }
268
269 return m_ok ;
270 }
271
272 void wxPrinterDC::EndDoc(void)
273 {
274 if ( !m_ok )
275 return ;
276
277 m_nativePrinterDC->EndDoc( this ) ;
278 m_ok = m_nativePrinterDC->Ok() ;
279
280 if ( !m_ok )
281 {
282 wxString message ;
283 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
284 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
285 dialog.ShowModal();
286 }
287 }
288
289 void wxPrinterDC::StartPage(void)
290 {
291 if ( !m_ok )
292 return ;
293
294 m_logicalFunction = wxCOPY;
295 // m_textAlignment = wxALIGN_TOP_LEFT;
296 m_backgroundMode = wxTRANSPARENT;
297
298 m_textForegroundColour = *wxBLACK;
299 m_textBackgroundColour = *wxWHITE;
300 m_pen = *wxBLACK_PEN;
301 m_font = *wxNORMAL_FONT;
302 m_brush = *wxTRANSPARENT_BRUSH;
303 m_backgroundBrush = *wxWHITE_BRUSH;
304 #if !wxMAC_USE_CORE_GRAPHICS
305 m_macFontInstalled = false ;
306 m_macBrushInstalled = false ;
307 m_macPenInstalled = false ;
308 #endif
309
310 m_nativePrinterDC->StartPage(this) ;
311 m_ok = m_nativePrinterDC->Ok() ;
312
313 }
314
315 void wxPrinterDC::EndPage(void)
316 {
317 if ( !m_ok )
318 return ;
319
320 m_nativePrinterDC->EndPage(this) ;
321 m_ok = m_nativePrinterDC->Ok() ;
322 }
323
324 void wxPrinterDC::DoGetSize(int *width, int *height) const
325 {
326 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
327
328 if ( width )
329 * width = m_nativePrinterDC->GetMaxX() ;
330 if ( height )
331 * height = m_nativePrinterDC->GetMaxY() ;
332 }
333
334 #endif