]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcprint.cpp
cleanup, fixing exports
[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 wxCoord m_maxX ;
67 wxCoord m_maxY ;
68 wxSize m_ppi ;
69 OSStatus m_err ;
70 } ;
71
72 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
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 PMResolution res;
86 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
87 PMPrinter printer;
88 PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer);
89 PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ;
90 #else
91 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
92 #endif
93 m_ppi = wxSize(int(res.hRes), int(res.vRes));
94 }
95
96 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
97 {
98 }
99
100 wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
101 {
102 return new wxMacCarbonPrinterDC(data) ;
103 }
104
105 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
106 {
107 if ( m_err )
108 return false ;
109
110 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
111
112 m_err = PMSessionBeginCGDocumentNoDialog(native->m_macPrintSession,
113 native->m_macPrintSettings,
114 native->m_macPageFormat);
115 if ( m_err != noErr )
116 return false;
117
118 PMRect rPage;
119 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
120 if ( m_err != noErr )
121 return false ;
122
123 m_maxX = wxCoord(rPage.right - rPage.left) ;
124 m_maxY = wxCoord(rPage.bottom - rPage.top);
125
126 PMResolution res;
127 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
128 if ( PMPrinterGetOutputResolution != NULL )
129 {
130 PMPrinter printer;
131 m_err = PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer);
132 if ( m_err == noErr )
133 m_err = PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ;
134 }
135 else
136 #endif
137 {
138 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
139 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
140 #endif
141 }
142
143 m_ppi = wxSize(int(res.hRes), int(res.vRes));
144 return true ;
145 }
146
147 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
148 {
149 if ( m_err )
150 return ;
151
152 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
153
154 m_err = PMSessionEndDocumentNoDialog(native->m_macPrintSession);
155 }
156
157 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
158 {
159 if ( m_err )
160 return ;
161
162 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
163
164 m_err = PMSessionBeginPageNoDialog(native->m_macPrintSession,
165 native->m_macPageFormat,
166 nil);
167
168 CGContextRef pageContext;
169
170 if ( m_err == noErr )
171 {
172 m_err = PMSessionGetCGGraphicsContext(native->m_macPrintSession,
173 &pageContext );
174 }
175
176 if ( m_err != noErr )
177 {
178 PMSessionEndPageNoDialog(native->m_macPrintSession);
179 PMSessionEndDocumentNoDialog(native->m_macPrintSession);
180 }
181 else
182 {
183 PMRect rPage;
184
185 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
186 if ( !m_err )
187 {
188 PMRect paperRect ;
189 PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ;
190 // make sure (0,0) is at the upper left of the printable area (wx conventions)
191 // Core Graphics initially has the lower left of the paper as 0,0
192 CGContextTranslateCTM( pageContext , -paperRect.left , paperRect.bottom ) ;
193 CGContextScaleCTM( pageContext , 1 , -1 ) ;
194 }
195 // since this is a non-critical error, we set the flag back
196 m_err = noErr ;
197 }
198 dc->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext ) );
199 }
200
201 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
202 {
203 if ( m_err )
204 return ;
205
206 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
207
208 m_err = PMSessionEndPageNoDialog(native->m_macPrintSession);
209 if ( m_err != noErr )
210 {
211 PMSessionEndDocumentNoDialog(native->m_macPrintSession);
212 }
213 // the cg context we got when starting the page isn't valid anymore, so replace it
214 dc->SetGraphicsContext( wxGraphicsContext::Create() );
215 }
216
217 void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const
218 {
219 if ( w )
220 *w = m_maxX ;
221 if ( h )
222 *h = m_maxY ;
223 }
224
225 wxSize wxMacCarbonPrinterDC::GetPPI() const
226 {
227 return m_ppi ;
228 };
229
230 //
231 //
232 //
233
234 wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
235 {
236 m_ok = false ;
237 m_printData = printdata ;
238 m_printData.ConvertToNative() ;
239 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
240 if ( m_nativePrinterDC )
241 {
242 m_ok = m_nativePrinterDC->Ok() ;
243 if ( !m_ok )
244 {
245 wxString message ;
246 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
247 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
248 dialog.ShowModal();
249 }
250 else
251 {
252 wxSize sz = GetPPI();
253 m_mm_to_pix_x = mm2inches * sz.x;
254 m_mm_to_pix_y = mm2inches * sz.y;
255 }
256 // we need at least a measuring context because people start measuring before a page
257 // gets printed at all
258 SetGraphicsContext( wxGraphicsContext::Create() );
259 }
260 }
261
262 wxSize wxPrinterDC::GetPPI() const
263 {
264 return m_nativePrinterDC->GetPPI() ;
265 }
266
267 wxPrinterDC::~wxPrinterDC(void)
268 {
269 delete m_nativePrinterDC ;
270 }
271
272 bool wxPrinterDC::StartDoc( const wxString& message )
273 {
274 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
275
276 if ( !m_ok )
277 return false ;
278
279 if ( m_nativePrinterDC->StartDoc(this, message ) )
280 {
281 // in case we have to do additional things when successful
282 }
283 m_ok = m_nativePrinterDC->Ok() ;
284 if ( !m_ok )
285 {
286 wxString message ;
287 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
288 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
289 dialog.ShowModal();
290 }
291
292 return m_ok ;
293 }
294
295 void wxPrinterDC::EndDoc(void)
296 {
297 if ( !m_ok )
298 return ;
299
300 m_nativePrinterDC->EndDoc( this ) ;
301 m_ok = m_nativePrinterDC->Ok() ;
302
303 if ( !m_ok )
304 {
305 wxString message ;
306 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
307 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
308 dialog.ShowModal();
309 }
310 }
311
312 wxRect wxPrinterDC::GetPaperRect()
313 {
314 wxCoord w, h;
315 GetSize(&w, &h);
316 wxRect pageRect(0, 0, w, h);
317 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) m_printData.GetNativeData() ;
318 OSStatus err = noErr ;
319 PMRect rPaper;
320 err = PMGetAdjustedPaperRect(native->m_macPageFormat, &rPaper);
321 if ( err != noErr )
322 return pageRect;
323 return wxRect(wxCoord(rPaper.left), wxCoord(rPaper.top),
324 wxCoord(rPaper.right - rPaper.left), wxCoord(rPaper.bottom - rPaper.top));
325 }
326
327 void wxPrinterDC::StartPage(void)
328 {
329 if ( !m_ok )
330 return ;
331
332 m_logicalFunction = wxCOPY;
333 // m_textAlignment = wxALIGN_TOP_LEFT;
334 m_backgroundMode = wxTRANSPARENT;
335
336 m_textForegroundColour = *wxBLACK;
337 m_textBackgroundColour = *wxWHITE;
338 m_pen = *wxBLACK_PEN;
339 m_font = *wxNORMAL_FONT;
340 m_brush = *wxTRANSPARENT_BRUSH;
341 m_backgroundBrush = *wxWHITE_BRUSH;
342
343 m_nativePrinterDC->StartPage(this) ;
344 m_ok = m_nativePrinterDC->Ok() ;
345
346 }
347
348 void wxPrinterDC::EndPage(void)
349 {
350 if ( !m_ok )
351 return ;
352
353 m_nativePrinterDC->EndPage(this) ;
354 m_ok = m_nativePrinterDC->Ok() ;
355 }
356
357 void wxPrinterDC::DoGetSize(int *width, int *height) const
358 {
359 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
360 m_nativePrinterDC->GetSize(width, height ) ;
361 }
362
363 #endif