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