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