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