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