]>
Commit | Line | Data |
---|---|---|
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" |
8acd14d1 | 30 | #include "wx/graphics.h" |
a3d3d3bf | 31 | |
72e7876b | 32 | IMPLEMENT_CLASS(wxPrinterDC, wxDC) |
72e7876b | 33 | |
746d7582 SC |
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; | |
db49000e SC |
43 | virtual void GetSize( int *w , int *h) const = 0 ; |
44 | virtual wxSize GetPPI() const = 0 ; | |
45 | ||
746d7582 SC |
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 ; } | |
eb7f8ac5 | 49 | |
746d7582 SC |
50 | static wxNativePrinterDC* Create(wxPrintData* data) ; |
51 | } ; | |
72e7876b | 52 | |
746d7582 SC |
53 | class wxMacCarbonPrinterDC : public wxNativePrinterDC |
54 | { | |
55 | public : | |
56 | wxMacCarbonPrinterDC( wxPrintData* data ) ; | |
d3c7fc99 | 57 | virtual ~wxMacCarbonPrinterDC() ; |
746d7582 SC |
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 ) ; | |
746d7582 | 62 | virtual wxUint32 GetStatus() const { return m_err ; } |
db49000e SC |
63 | virtual void GetSize( int *w , int *h) const ; |
64 | virtual wxSize GetPPI() const ; | |
746d7582 | 65 | private : |
746d7582 SC |
66 | wxCoord m_maxX ; |
67 | wxCoord m_maxY ; | |
db49000e | 68 | wxSize m_ppi ; |
746d7582 SC |
69 | OSStatus m_err ; |
70 | } ; | |
71 | ||
72 | wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data ) | |
72e7876b | 73 | { |
746d7582 | 74 | m_err = noErr ; |
dc7ccb9c | 75 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ; |
eb7f8ac5 | 76 | |
746d7582 SC |
77 | PMRect rPage; |
78 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); | |
79 | if ( m_err != noErr ) | |
80 | return; | |
a689a4d0 | 81 | |
746d7582 SC |
82 | m_maxX = wxCoord(rPage.right - rPage.left) ; |
83 | m_maxY = wxCoord(rPage.bottom - rPage.top); | |
db49000e SC |
84 | |
85 | PMResolution res; | |
4f74e0d1 SC |
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 | |
db49000e | 91 | m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res); |
4f74e0d1 | 92 | #endif |
f7862d3e | 93 | m_ppi = wxSize(int(res.hRes), int(res.vRes)); |
746d7582 | 94 | } |
75411508 | 95 | |
746d7582 SC |
96 | wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC() |
97 | { | |
746d7582 SC |
98 | } |
99 | ||
100 | wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data) | |
101 | { | |
102 | return new wxMacCarbonPrinterDC(data) ; | |
103 | } | |
104 | ||
eb7f8ac5 | 105 | bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) ) |
746d7582 SC |
106 | { |
107 | if ( m_err ) | |
108 | return false ; | |
109 | ||
dc7ccb9c | 110 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 | 111 | |
3cd25cff | 112 | m_err = PMSessionBeginCGDocumentNoDialog(native->m_macPrintSession, |
4f74e0d1 | 113 | native->m_macPrintSettings, |
3cd25cff | 114 | native->m_macPageFormat); |
746d7582 SC |
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 ) | |
9fff7273 | 121 | return false ; |
db49000e SC |
122 | |
123 | m_maxX = wxCoord(rPage.right - rPage.left) ; | |
124 | m_maxY = wxCoord(rPage.bottom - rPage.top); | |
746d7582 | 125 | |
db49000e | 126 | PMResolution res; |
3cd25cff SC |
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 | |
4f74e0d1 | 136 | #endif |
3cd25cff SC |
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 | ||
db49000e | 143 | m_ppi = wxSize(int(res.hRes), int(res.vRes)); |
746d7582 SC |
144 | return true ; |
145 | } | |
146 | ||
eb7f8ac5 | 147 | void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc ) |
746d7582 SC |
148 | { |
149 | if ( m_err ) | |
150 | return ; | |
151 | ||
dc7ccb9c | 152 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 | 153 | |
3cd25cff | 154 | m_err = PMSessionEndDocumentNoDialog(native->m_macPrintSession); |
746d7582 SC |
155 | } |
156 | ||
eb7f8ac5 | 157 | void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc ) |
746d7582 SC |
158 | { |
159 | if ( m_err ) | |
160 | return ; | |
161 | ||
dc7ccb9c | 162 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 | 163 | |
3cd25cff | 164 | m_err = PMSessionBeginPageNoDialog(native->m_macPrintSession, |
746d7582 SC |
165 | native->m_macPageFormat, |
166 | nil); | |
eb7f8ac5 | 167 | |
20b69855 | 168 | CGContextRef pageContext; |
e1673e52 | 169 | |
746d7582 | 170 | if ( m_err == noErr ) |
e40298d5 | 171 | { |
4f74e0d1 SC |
172 | m_err = PMSessionGetCGGraphicsContext(native->m_macPrintSession, |
173 | &pageContext ); | |
e40298d5 | 174 | } |
eb7f8ac5 | 175 | |
746d7582 | 176 | if ( m_err != noErr ) |
e40298d5 | 177 | { |
3cd25cff SC |
178 | PMSessionEndPageNoDialog(native->m_macPrintSession); |
179 | PMSessionEndDocumentNoDialog(native->m_macPrintSession); | |
e40298d5 | 180 | } |
746d7582 | 181 | else |
e40298d5 | 182 | { |
746d7582 | 183 | PMRect rPage; |
eb7f8ac5 | 184 | |
746d7582 SC |
185 | m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage); |
186 | if ( !m_err ) | |
187 | { | |
a06e389f SC |
188 | PMRect paperRect ; |
189 | PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ; | |
3925a44c SC |
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 ) ; | |
20b69855 | 193 | CGContextScaleCTM( pageContext , 1 , -1 ) ; |
746d7582 SC |
194 | } |
195 | // since this is a non-critical error, we set the flag back | |
196 | m_err = noErr ; | |
e40298d5 | 197 | } |
ad667945 | 198 | dc->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext ) ); |
746d7582 SC |
199 | } |
200 | ||
eb7f8ac5 | 201 | void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc ) |
746d7582 SC |
202 | { |
203 | if ( m_err ) | |
204 | return ; | |
205 | ||
dc7ccb9c | 206 | wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ; |
746d7582 | 207 | |
3cd25cff | 208 | m_err = PMSessionEndPageNoDialog(native->m_macPrintSession); |
746d7582 | 209 | if ( m_err != noErr ) |
f520d381 | 210 | { |
3cd25cff | 211 | PMSessionEndDocumentNoDialog(native->m_macPrintSession); |
f520d381 | 212 | } |
ad667945 SC |
213 | // the cg context we got when starting the page isn't valid anymore, so replace it |
214 | dc->SetGraphicsContext( wxGraphicsContext::Create() ); | |
746d7582 SC |
215 | } |
216 | ||
db49000e SC |
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 | ||
746d7582 SC |
234 | wxPrinterDC::wxPrinterDC(const wxPrintData& printdata) |
235 | { | |
6d50343d | 236 | m_ok = false ; |
746d7582 SC |
237 | m_printData = printdata ; |
238 | m_printData.ConvertToNative() ; | |
239 | m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ; | |
eb7f8ac5 | 240 | if ( m_nativePrinterDC ) |
75411508 | 241 | { |
746d7582 | 242 | m_ok = m_nativePrinterDC->Ok() ; |
746d7582 SC |
243 | if ( !m_ok ) |
244 | { | |
245 | wxString message ; | |
eb7f8ac5 | 246 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; |
746d7582 SC |
247 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; |
248 | dialog.ShowModal(); | |
249 | } | |
db49000e SC |
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 | } | |
ad667945 SC |
256 | // we need at least a measuring context because people start measuring before a page |
257 | // gets printed at all | |
258 | SetGraphicsContext( wxGraphicsContext::Create() ); | |
75411508 | 259 | } |
746d7582 SC |
260 | } |
261 | ||
db49000e SC |
262 | wxSize wxPrinterDC::GetPPI() const |
263 | { | |
264 | return m_nativePrinterDC->GetPPI() ; | |
265 | } | |
266 | ||
746d7582 SC |
267 | wxPrinterDC::~wxPrinterDC(void) |
268 | { | |
269 | delete m_nativePrinterDC ; | |
270 | } | |
271 | ||
eb7f8ac5 | 272 | bool wxPrinterDC::StartDoc( const wxString& message ) |
746d7582 SC |
273 | { |
274 | wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ; | |
eb7f8ac5 | 275 | |
746d7582 SC |
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 ) | |
75411508 | 285 | { |
746d7582 | 286 | wxString message ; |
eb7f8ac5 | 287 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; |
427ff662 | 288 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; |
75411508 | 289 | dialog.ShowModal(); |
75411508 | 290 | } |
746d7582 | 291 | |
e40298d5 | 292 | return m_ok ; |
72e7876b SC |
293 | } |
294 | ||
eb7f8ac5 | 295 | void wxPrinterDC::EndDoc(void) |
72e7876b | 296 | { |
746d7582 SC |
297 | if ( !m_ok ) |
298 | return ; | |
299 | ||
300 | m_nativePrinterDC->EndDoc( this ) ; | |
301 | m_ok = m_nativePrinterDC->Ok() ; | |
302 | ||
303 | if ( !m_ok ) | |
75411508 | 304 | { |
746d7582 | 305 | wxString message ; |
eb7f8ac5 | 306 | message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ; |
746d7582 SC |
307 | wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ; |
308 | dialog.ShowModal(); | |
75411508 | 309 | } |
72e7876b SC |
310 | } |
311 | ||
f415cab9 JS |
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 | ||
eb7f8ac5 | 327 | void wxPrinterDC::StartPage(void) |
72e7876b | 328 | { |
e40298d5 JS |
329 | if ( !m_ok ) |
330 | return ; | |
2f1ae414 | 331 | |
746d7582 SC |
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; | |
2f1ae414 | 342 | |
746d7582 SC |
343 | m_nativePrinterDC->StartPage(this) ; |
344 | m_ok = m_nativePrinterDC->Ok() ; | |
eb7f8ac5 | 345 | |
72e7876b SC |
346 | } |
347 | ||
eb7f8ac5 | 348 | void wxPrinterDC::EndPage(void) |
72e7876b | 349 | { |
e40298d5 JS |
350 | if ( !m_ok ) |
351 | return ; | |
72e7876b | 352 | |
746d7582 SC |
353 | m_nativePrinterDC->EndPage(this) ; |
354 | m_ok = m_nativePrinterDC->Ok() ; | |
355 | } | |
72e7876b | 356 | |
746d7582 SC |
357 | void wxPrinterDC::DoGetSize(int *width, int *height) const |
358 | { | |
359 | wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") ); | |
db49000e | 360 | m_nativePrinterDC->GetSize(width, height ) ; |
72e7876b | 361 | } |
746d7582 | 362 | |
179e085f | 363 | #endif |