]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcprint.cpp
As reported by Chris Elliott some releases of Lesstif crash
[wxWidgets.git] / src / mac / carbon / dcprint.cpp
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$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dcprint.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if wxUSE_PRINTING_ARCHITECTURE
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #ifndef WX_PRECOMP
26 #endif
27
28 #include "wx/dcprint.h"
29 #include "wx/msgdlg.h"
30 #include "wx/math.h"
31 #include "wx/mac/uma.h"
32 #include "wx/mac/private/print.h"
33
34 IMPLEMENT_CLASS(wxPrinterDC, wxDC)
35
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 ; }
50
51 static wxNativePrinterDC* Create(wxPrintData* data) ;
52 } ;
53
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 )
74 {
75 ::GetPort( & m_macPrintFormerPort ) ;
76
77 m_err = noErr ;
78 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ;
79
80 PMRect rPage;
81 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
82 if ( m_err != noErr )
83 return;
84
85 m_maxX = wxCoord(rPage.right - rPage.left) ;
86 m_maxY = wxCoord(rPage.bottom - rPage.top);
87 }
88
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
100 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
101 {
102 if ( m_err )
103 return false ;
104
105 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
106
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
116 m_err = PMSessionBeginDocument(native->m_macPrintSession,
117 native->m_macPrintSettings,
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
127 m_maxX = (wxCoord)(rPage.right - rPage.left);
128 m_maxY = (wxCoord)(rPage.bottom - rPage.top);
129 return true ;
130 }
131
132 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
133 {
134 if ( m_err )
135 return ;
136
137 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
138
139 m_err = PMSessionEndDocument(native->m_macPrintSession);
140 }
141
142 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
143 {
144 if ( m_err )
145 return ;
146
147 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
148
149 m_err = PMSessionBeginPage(native->m_macPrintSession,
150 native->m_macPageFormat,
151 nil);
152
153 #if wxMAC_USE_CORE_GRAPHICS
154 CGContextRef pageContext;
155 #endif
156 if ( m_err == noErr )
157 {
158 #if wxMAC_USE_CORE_GRAPHICS
159 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
160 kPMGraphicsContextCoreGraphics,
161 (void**) &pageContext );
162 dc->MacSetCGContext(pageContext) ;
163 #else
164 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
165 kPMGraphicsContextQuickdraw,
166 (void**) &dc->m_macPort );
167 #endif
168 }
169
170 if ( m_err != noErr )
171 {
172 PMSessionEndPage(native->m_macPrintSession);
173 PMSessionEndDocument(native->m_macPrintSession);
174 }
175 else
176 {
177 PMRect rPage;
178
179 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
180 if ( !m_err )
181 {
182 #if wxMAC_USE_CORE_GRAPHICS
183 CGContextTranslateCTM( pageContext , 0 , rPage.bottom - rPage.top ) ;
184 CGContextScaleCTM( pageContext , 1 , -1 ) ;
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 delete m_nativePrinterDC ;
235 }
236
237 #if wxMAC_USE_CORE_GRAPHICS
238 void wxPrinterDC::MacSetCGContext( void * cg )
239 {
240 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( (CGContextRef) cg ) ;
241 m_graphicContext->SetPen( m_pen ) ;
242 m_graphicContext->SetBrush( m_brush ) ;
243 }
244 #endif
245 bool wxPrinterDC::StartDoc( const wxString& message )
246 {
247 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
248
249 if ( !m_ok )
250 return false ;
251
252 if ( m_nativePrinterDC->StartDoc(this, message ) )
253 {
254 // in case we have to do additional things when successful
255 }
256 m_ok = m_nativePrinterDC->Ok() ;
257 if ( !m_ok )
258 {
259 wxString message ;
260 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
261 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
262 dialog.ShowModal();
263 }
264
265 return m_ok ;
266 }
267
268 void wxPrinterDC::EndDoc(void)
269 {
270 if ( !m_ok )
271 return ;
272
273 m_nativePrinterDC->EndDoc( this ) ;
274 m_ok = m_nativePrinterDC->Ok() ;
275
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 }
284
285 void wxPrinterDC::StartPage(void)
286 {
287 if ( !m_ok )
288 return ;
289
290 m_logicalFunction = wxCOPY;
291 // m_textAlignment = wxALIGN_TOP_LEFT;
292 m_backgroundMode = wxTRANSPARENT;
293
294 m_textForegroundColour = *wxBLACK;
295 m_textBackgroundColour = *wxWHITE;
296 m_pen = *wxBLACK_PEN;
297 m_font = *wxNORMAL_FONT;
298 m_brush = *wxTRANSPARENT_BRUSH;
299 m_backgroundBrush = *wxWHITE_BRUSH;
300 #if !wxMAC_USE_CORE_GRAPHICS
301 m_macFontInstalled = false ;
302 m_macBrushInstalled = false ;
303 m_macPenInstalled = false ;
304 #endif
305
306 m_nativePrinterDC->StartPage(this) ;
307 m_ok = m_nativePrinterDC->Ok() ;
308
309 }
310
311 void wxPrinterDC::EndPage(void)
312 {
313 if ( !m_ok )
314 return ;
315
316 m_nativePrinterDC->EndPage(this) ;
317 m_ok = m_nativePrinterDC->Ok() ;
318 }
319
320 void wxPrinterDC::DoGetSize(int *width, int *height) const
321 {
322 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
323
324 if ( width )
325 * width = m_nativePrinterDC->GetMaxX() ;
326 if ( height )
327 * height = m_nativePrinterDC->GetMaxY() ;
328 }
329
330 #endif