]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcprint.cpp
WinCE fixes
[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 PMRect paperRect ;
184 PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ;
185 CGContextTranslateCTM( pageContext , -paperRect.left , -paperRect.top + ( rPage.bottom - rPage.top ) ) ;
186 CGContextScaleCTM( pageContext , 1 , -1 ) ;
187 CGContextSaveGState( pageContext ) ;
188 #else
189 dc->m_macLocalOrigin.x = (int) rPage.left;
190 dc->m_macLocalOrigin.y = (int) rPage.top;
191 #endif
192 }
193 // since this is a non-critical error, we set the flag back
194 m_err = noErr ;
195 }
196 }
197
198 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
199 {
200 if ( m_err )
201 return ;
202
203 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
204
205 m_err = PMSessionEndPage(native->m_macPrintSession);
206 if ( m_err != noErr )
207 {
208 PMSessionEndDocument(native->m_macPrintSession);
209 }
210 }
211
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 ) ;
218 if ( m_nativePrinterDC )
219 {
220 m_ok = m_nativePrinterDC->Ok() ;
221 if ( !m_ok )
222 {
223 wxString message ;
224 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
225 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
226 dialog.ShowModal();
227 }
228 #if wxMAC_USE_CORE_GRAPHICS
229 // the cgContext will only be handed over page by page
230 m_graphicContext = new wxMacCGContext() ;
231 #endif
232 }
233 }
234
235 wxPrinterDC::~wxPrinterDC(void)
236 {
237 #if wxMAC_USE_CORE_GRAPHICS
238 // this context was borrowed
239 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ;
240 #endif
241 delete m_nativePrinterDC ;
242 }
243
244 #if wxMAC_USE_CORE_GRAPHICS
245 void wxPrinterDC::MacSetCGContext( void * cg )
246 {
247 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( (CGContextRef) cg ) ;
248 m_graphicContext->SetPen( m_pen ) ;
249 m_graphicContext->SetBrush( m_brush ) ;
250 }
251 #endif
252 bool wxPrinterDC::StartDoc( const wxString& message )
253 {
254 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
255
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 )
265 {
266 wxString message ;
267 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
268 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
269 dialog.ShowModal();
270 }
271
272 return m_ok ;
273 }
274
275 void wxPrinterDC::EndDoc(void)
276 {
277 if ( !m_ok )
278 return ;
279
280 m_nativePrinterDC->EndDoc( this ) ;
281 m_ok = m_nativePrinterDC->Ok() ;
282
283 if ( !m_ok )
284 {
285 wxString message ;
286 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
287 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
288 dialog.ShowModal();
289 }
290 }
291
292 void wxPrinterDC::StartPage(void)
293 {
294 if ( !m_ok )
295 return ;
296
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;
307 #if !wxMAC_USE_CORE_GRAPHICS
308 m_macFontInstalled = false ;
309 m_macBrushInstalled = false ;
310 m_macPenInstalled = false ;
311 #endif
312
313 m_nativePrinterDC->StartPage(this) ;
314 m_ok = m_nativePrinterDC->Ok() ;
315
316 }
317
318 void wxPrinterDC::EndPage(void)
319 {
320 if ( !m_ok )
321 return ;
322
323 m_nativePrinterDC->EndPage(this) ;
324 m_ok = m_nativePrinterDC->Ok() ;
325 }
326
327 void wxPrinterDC::DoGetSize(int *width, int *height) const
328 {
329 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
330
331 if ( width )
332 * width = m_nativePrinterDC->GetMaxX() ;
333 if ( height )
334 * height = m_nativePrinterDC->GetMaxY() ;
335 }
336
337 #endif