]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcprint.cpp
fix compilation error after r50329: wxMenu doesn't have HandleWindowEvent() as it...
[wxWidgets.git] / src / mac / carbon / dcprint.cpp
CommitLineData
72e7876b 1/////////////////////////////////////////////////////////////////////////////
6d50343d 2// Name: src/mac/carbon/dcprint.cpp
72e7876b
SC
3// Purpose: wxPrinterDC class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
6d50343d 9// Licence: wxWindows licence
72e7876b
SC
10/////////////////////////////////////////////////////////////////////////////
11
72e7876b
SC
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
179e085f
RN
15#if wxUSE_PRINTING_ARCHITECTURE
16
72e7876b 17#ifdef __BORLANDC__
6d50343d 18 #pragma hdrstop
72e7876b
SC
19#endif
20
6d50343d
WS
21#include "wx/dcprint.h"
22
72e7876b 23#ifndef WX_PRECOMP
246c5004 24 #include "wx/msgdlg.h"
18680f86 25 #include "wx/math.h"
72e7876b
SC
26#endif
27
2f1ae414 28#include "wx/mac/uma.h"
746d7582 29#include "wx/mac/private/print.h"
888dde65 30#include "wx/mac/carbon/dcprint.h"
8acd14d1 31#include "wx/graphics.h"
a3d3d3bf 32
888dde65 33IMPLEMENT_ABSTRACT_CLASS(wxPrinterDCImpl, wxGCDCImpl)
72e7876b 34
746d7582
SC
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;
db49000e
SC
44 virtual void GetSize( int *w , int *h) const = 0 ;
45 virtual wxSize GetPPI() const = 0 ;
46
746d7582
SC
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 ; }
eb7f8ac5 50
746d7582
SC
51 static wxNativePrinterDC* Create(wxPrintData* data) ;
52} ;
72e7876b 53
746d7582
SC
54class wxMacCarbonPrinterDC : public wxNativePrinterDC
55{
56public :
57 wxMacCarbonPrinterDC( wxPrintData* data ) ;
d3c7fc99 58 virtual ~wxMacCarbonPrinterDC() ;
746d7582
SC
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 ) ;
746d7582 63 virtual wxUint32 GetStatus() const { return m_err ; }
db49000e
SC
64 virtual void GetSize( int *w , int *h) const ;
65 virtual wxSize GetPPI() const ;
746d7582 66private :
746d7582
SC
67 wxCoord m_maxX ;
68 wxCoord m_maxY ;
db49000e 69 wxSize m_ppi ;
746d7582
SC
70 OSStatus m_err ;
71} ;
72
73wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
72e7876b 74{
746d7582 75 m_err = noErr ;
dc7ccb9c 76 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ;
eb7f8ac5 77
746d7582
SC
78 PMRect rPage;
79 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
80 if ( m_err != noErr )
81 return;
a689a4d0 82
746d7582
SC
83 m_maxX = wxCoord(rPage.right - rPage.left) ;
84 m_maxY = wxCoord(rPage.bottom - rPage.top);
db49000e
SC
85
86 PMResolution res;
4f74e0d1
SC
87#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
88 PMPrinter printer;
89 PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer);
90 PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ;
91#else
db49000e 92 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
4f74e0d1 93#endif
f7862d3e 94 m_ppi = wxSize(int(res.hRes), int(res.vRes));
746d7582 95}
75411508 96
746d7582
SC
97wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
98{
746d7582
SC
99}
100
101wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
102{
103 return new wxMacCarbonPrinterDC(data) ;
104}
105
eb7f8ac5 106bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
746d7582
SC
107{
108 if ( m_err )
109 return false ;
110
888dde65
RR
111 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
112 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ;
746d7582 113
3cd25cff 114 m_err = PMSessionBeginCGDocumentNoDialog(native->m_macPrintSession,
4f74e0d1 115 native->m_macPrintSettings,
3cd25cff 116 native->m_macPageFormat);
746d7582
SC
117 if ( m_err != noErr )
118 return false;
119
120 PMRect rPage;
121 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
122 if ( m_err != noErr )
9fff7273 123 return false ;
db49000e
SC
124
125 m_maxX = wxCoord(rPage.right - rPage.left) ;
126 m_maxY = wxCoord(rPage.bottom - rPage.top);
746d7582 127
db49000e 128 PMResolution res;
3cd25cff
SC
129#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
130 if ( PMPrinterGetOutputResolution != NULL )
131 {
132 PMPrinter printer;
133 m_err = PMSessionGetCurrentPrinter(native->m_macPrintSession, &printer);
134 if ( m_err == noErr )
135 m_err = PMPrinterGetOutputResolution( printer, native->m_macPrintSettings, &res) ;
136 }
137 else
4f74e0d1 138#endif
3cd25cff
SC
139 {
140#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
141 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
142#endif
143 }
144
db49000e 145 m_ppi = wxSize(int(res.hRes), int(res.vRes));
746d7582
SC
146 return true ;
147}
148
eb7f8ac5 149void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
746d7582
SC
150{
151 if ( m_err )
152 return ;
153
888dde65
RR
154 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
155 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ;
746d7582 156
3cd25cff 157 m_err = PMSessionEndDocumentNoDialog(native->m_macPrintSession);
746d7582
SC
158}
159
eb7f8ac5 160void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
746d7582
SC
161{
162 if ( m_err )
163 return ;
164
888dde65
RR
165 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
166 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ;
746d7582 167
3cd25cff 168 m_err = PMSessionBeginPageNoDialog(native->m_macPrintSession,
746d7582
SC
169 native->m_macPageFormat,
170 nil);
eb7f8ac5 171
20b69855 172 CGContextRef pageContext;
e1673e52 173
746d7582 174 if ( m_err == noErr )
e40298d5 175 {
4f74e0d1
SC
176 m_err = PMSessionGetCGGraphicsContext(native->m_macPrintSession,
177 &pageContext );
e40298d5 178 }
eb7f8ac5 179
746d7582 180 if ( m_err != noErr )
e40298d5 181 {
3cd25cff
SC
182 PMSessionEndPageNoDialog(native->m_macPrintSession);
183 PMSessionEndDocumentNoDialog(native->m_macPrintSession);
e40298d5 184 }
746d7582 185 else
e40298d5 186 {
746d7582 187 PMRect rPage;
eb7f8ac5 188
746d7582
SC
189 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
190 if ( !m_err )
191 {
a06e389f
SC
192 PMRect paperRect ;
193 PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ;
3925a44c
SC
194 // make sure (0,0) is at the upper left of the printable area (wx conventions)
195 // Core Graphics initially has the lower left of the paper as 0,0
196 CGContextTranslateCTM( pageContext , -paperRect.left , paperRect.bottom ) ;
20b69855 197 CGContextScaleCTM( pageContext , 1 , -1 ) ;
746d7582
SC
198 }
199 // since this is a non-critical error, we set the flag back
200 m_err = noErr ;
e40298d5 201 }
888dde65 202 impl->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext ) );
746d7582
SC
203}
204
eb7f8ac5 205void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
746d7582
SC
206{
207 if ( m_err )
208 return ;
209
888dde65
RR
210 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
211 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) impl->GetPrintData().GetNativeData() ;
746d7582 212
3cd25cff 213 m_err = PMSessionEndPageNoDialog(native->m_macPrintSession);
746d7582 214 if ( m_err != noErr )
f520d381 215 {
3cd25cff 216 PMSessionEndDocumentNoDialog(native->m_macPrintSession);
f520d381 217 }
ad667945 218 // the cg context we got when starting the page isn't valid anymore, so replace it
888dde65 219 impl->SetGraphicsContext( wxGraphicsContext::Create() );
746d7582
SC
220}
221
db49000e
SC
222void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const
223{
224 if ( w )
225 *w = m_maxX ;
226 if ( h )
227 *h = m_maxY ;
228}
229
230wxSize wxMacCarbonPrinterDC::GetPPI() const
231{
232 return m_ppi ;
233};
234
235//
236//
237//
238
888dde65
RR
239wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC *owner, const wxPrintData& printdata )
240 : wxGCDCImpl( owner )
746d7582 241{
6d50343d 242 m_ok = false ;
746d7582
SC
243 m_printData = printdata ;
244 m_printData.ConvertToNative() ;
245 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
eb7f8ac5 246 if ( m_nativePrinterDC )
75411508 247 {
746d7582 248 m_ok = m_nativePrinterDC->Ok() ;
746d7582
SC
249 if ( !m_ok )
250 {
251 wxString message ;
eb7f8ac5 252 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
253 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
254 dialog.ShowModal();
255 }
db49000e
SC
256 else
257 {
258 wxSize sz = GetPPI();
259 m_mm_to_pix_x = mm2inches * sz.x;
260 m_mm_to_pix_y = mm2inches * sz.y;
261 }
ad667945
SC
262 // we need at least a measuring context because people start measuring before a page
263 // gets printed at all
264 SetGraphicsContext( wxGraphicsContext::Create() );
75411508 265 }
746d7582
SC
266}
267
888dde65 268wxSize wxPrinterDCImpl::GetPPI() const
db49000e
SC
269{
270 return m_nativePrinterDC->GetPPI() ;
271}
272
888dde65 273wxPrinterDCImpl::~wxPrinterDCImpl()
746d7582
SC
274{
275 delete m_nativePrinterDC ;
276}
277
888dde65 278bool wxPrinterDCImpl::StartDoc( const wxString& message )
746d7582 279{
f896f240 280 wxASSERT_MSG( IsOk() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
eb7f8ac5 281
746d7582
SC
282 if ( !m_ok )
283 return false ;
284
888dde65 285 if ( m_nativePrinterDC->StartDoc( (wxPrinterDC*) GetOwner(), message ) )
746d7582
SC
286 {
287 // in case we have to do additional things when successful
288 }
289 m_ok = m_nativePrinterDC->Ok() ;
290 if ( !m_ok )
75411508 291 {
746d7582 292 wxString message ;
eb7f8ac5 293 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
427ff662 294 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
75411508 295 dialog.ShowModal();
75411508 296 }
746d7582 297
e40298d5 298 return m_ok ;
72e7876b
SC
299}
300
888dde65 301void wxPrinterDCImpl::EndDoc(void)
72e7876b 302{
746d7582
SC
303 if ( !m_ok )
304 return ;
305
888dde65 306 m_nativePrinterDC->EndDoc( (wxPrinterDC*) GetOwner() ) ;
746d7582
SC
307 m_ok = m_nativePrinterDC->Ok() ;
308
309 if ( !m_ok )
75411508 310 {
746d7582 311 wxString message ;
eb7f8ac5 312 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
313 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
314 dialog.ShowModal();
75411508 315 }
72e7876b
SC
316}
317
888dde65 318wxRect wxPrinterDCImpl::GetPaperRect()
f415cab9
JS
319{
320 wxCoord w, h;
888dde65 321 GetOwner()->GetSize(&w, &h);
f415cab9
JS
322 wxRect pageRect(0, 0, w, h);
323 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) m_printData.GetNativeData() ;
324 OSStatus err = noErr ;
325 PMRect rPaper;
326 err = PMGetAdjustedPaperRect(native->m_macPageFormat, &rPaper);
327 if ( err != noErr )
328 return pageRect;
329 return wxRect(wxCoord(rPaper.left), wxCoord(rPaper.top),
330 wxCoord(rPaper.right - rPaper.left), wxCoord(rPaper.bottom - rPaper.top));
331}
332
888dde65 333void wxPrinterDCImpl::StartPage()
72e7876b 334{
e40298d5
JS
335 if ( !m_ok )
336 return ;
2f1ae414 337
746d7582
SC
338 m_logicalFunction = wxCOPY;
339 // m_textAlignment = wxALIGN_TOP_LEFT;
340 m_backgroundMode = wxTRANSPARENT;
341
342 m_textForegroundColour = *wxBLACK;
343 m_textBackgroundColour = *wxWHITE;
344 m_pen = *wxBLACK_PEN;
345 m_font = *wxNORMAL_FONT;
346 m_brush = *wxTRANSPARENT_BRUSH;
347 m_backgroundBrush = *wxWHITE_BRUSH;
2f1ae414 348
888dde65 349 m_nativePrinterDC->StartPage( (wxPrinterDC*) GetOwner() ) ;
746d7582 350 m_ok = m_nativePrinterDC->Ok() ;
eb7f8ac5 351
72e7876b
SC
352}
353
888dde65 354void wxPrinterDCImpl::EndPage()
72e7876b 355{
e40298d5
JS
356 if ( !m_ok )
357 return ;
72e7876b 358
888dde65 359 m_nativePrinterDC->EndPage( (wxPrinterDC*) GetOwner() );
746d7582
SC
360 m_ok = m_nativePrinterDC->Ok() ;
361}
72e7876b 362
888dde65 363void wxPrinterDCImpl::DoGetSize(int *width, int *height) const
746d7582
SC
364{
365 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
db49000e 366 m_nativePrinterDC->GetSize(width, height ) ;
72e7876b 367}
746d7582 368
179e085f 369#endif