]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/dcprint.cpp
mac cleanup, pure cgcolor
[wxWidgets.git] / src / mac / carbon / dcprint.cpp
... / ...
CommitLineData
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
28#include "wx/mac/uma.h"
29#include "wx/mac/private/print.h"
30#include "wx/graphics.h"
31
32IMPLEMENT_CLASS(wxPrinterDC, wxDC)
33
34class wxNativePrinterDC
35{
36public :
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;
43 virtual void GetSize( int *w , int *h) const = 0 ;
44 virtual wxSize GetPPI() const = 0 ;
45
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 ; }
49
50 static wxNativePrinterDC* Create(wxPrintData* data) ;
51} ;
52
53class wxMacCarbonPrinterDC : public wxNativePrinterDC
54{
55public :
56 wxMacCarbonPrinterDC( wxPrintData* data ) ;
57 virtual ~wxMacCarbonPrinterDC() ;
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 ) ;
62 virtual wxUint32 GetStatus() const { return m_err ; }
63 virtual void GetSize( int *w , int *h) const ;
64 virtual wxSize GetPPI() const ;
65private :
66 wxCoord m_maxX ;
67 wxCoord m_maxY ;
68 wxSize m_ppi ;
69 OSStatus m_err ;
70} ;
71
72wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
73{
74 m_err = noErr ;
75 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) data->GetNativeData() ;
76
77 PMRect rPage;
78 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
79 if ( m_err != noErr )
80 return;
81
82 m_maxX = wxCoord(rPage.right - rPage.left) ;
83 m_maxY = wxCoord(rPage.bottom - rPage.top);
84
85 PMResolution res;
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
91 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
92#endif
93 m_ppi = wxSize(int(res.hRes), int(res.vRes));
94}
95
96wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
97{
98}
99
100wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
101{
102 return new wxMacCarbonPrinterDC(data) ;
103}
104
105bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
106{
107 if ( m_err )
108 return false ;
109
110 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
111
112 m_err = PMSessionBeginCGDocument(native->m_macPrintSession,
113 native->m_macPrintSettings,
114 native->m_macPageFormat);
115
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 )
122 return false ;
123
124 m_maxX = wxCoord(rPage.right - rPage.left) ;
125 m_maxY = wxCoord(rPage.bottom - rPage.top);
126
127 PMResolution res;
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
133 m_err = PMGetResolution((PMPageFormat) (native->m_macPageFormat), &res);
134#endif
135 m_ppi = wxSize(int(res.hRes), int(res.vRes));
136 return true ;
137}
138
139void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
140{
141 if ( m_err )
142 return ;
143
144 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
145
146 m_err = PMSessionEndDocument(native->m_macPrintSession);
147}
148
149void wxMacCarbonPrinterDC::StartPage( wxPrinterDC* dc )
150{
151 if ( m_err )
152 return ;
153
154 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
155
156 m_err = PMSessionBeginPage(native->m_macPrintSession,
157 native->m_macPageFormat,
158 nil);
159
160 CGContextRef pageContext;
161
162 if ( m_err == noErr )
163 {
164 m_err = PMSessionGetCGGraphicsContext(native->m_macPrintSession,
165 &pageContext );
166 }
167
168 if ( m_err != noErr )
169 {
170 PMSessionEndPage(native->m_macPrintSession);
171 PMSessionEndDocument(native->m_macPrintSession);
172 }
173 else
174 {
175 PMRect rPage;
176
177 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
178 if ( !m_err )
179 {
180 PMRect paperRect ;
181 PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ;
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 ) ;
185 CGContextScaleCTM( pageContext , 1 , -1 ) ;
186 }
187 // since this is a non-critical error, we set the flag back
188 m_err = noErr ;
189 }
190 dc->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext ) );
191}
192
193void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
194{
195 if ( m_err )
196 return ;
197
198 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
199
200 m_err = PMSessionEndPage(native->m_macPrintSession);
201 if ( m_err != noErr )
202 {
203 PMSessionEndDocument(native->m_macPrintSession);
204 }
205 // the cg context we got when starting the page isn't valid anymore, so replace it
206 dc->SetGraphicsContext( wxGraphicsContext::Create() );
207}
208
209void wxMacCarbonPrinterDC::GetSize( int *w , int *h) const
210{
211 if ( w )
212 *w = m_maxX ;
213 if ( h )
214 *h = m_maxY ;
215}
216
217wxSize wxMacCarbonPrinterDC::GetPPI() const
218{
219 return m_ppi ;
220};
221
222//
223//
224//
225
226wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
227{
228 m_ok = false ;
229 m_printData = printdata ;
230 m_printData.ConvertToNative() ;
231 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
232 if ( m_nativePrinterDC )
233 {
234 m_ok = m_nativePrinterDC->Ok() ;
235 if ( !m_ok )
236 {
237 wxString message ;
238 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
239 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
240 dialog.ShowModal();
241 }
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 }
248 // we need at least a measuring context because people start measuring before a page
249 // gets printed at all
250 SetGraphicsContext( wxGraphicsContext::Create() );
251 }
252}
253
254wxSize wxPrinterDC::GetPPI() const
255{
256 return m_nativePrinterDC->GetPPI() ;
257}
258
259wxPrinterDC::~wxPrinterDC(void)
260{
261 delete m_nativePrinterDC ;
262}
263
264bool wxPrinterDC::StartDoc( const wxString& message )
265{
266 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
267
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 )
277 {
278 wxString message ;
279 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
280 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
281 dialog.ShowModal();
282 }
283
284 return m_ok ;
285}
286
287void wxPrinterDC::EndDoc(void)
288{
289 if ( !m_ok )
290 return ;
291
292 m_nativePrinterDC->EndDoc( this ) ;
293 m_ok = m_nativePrinterDC->Ok() ;
294
295 if ( !m_ok )
296 {
297 wxString message ;
298 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
299 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
300 dialog.ShowModal();
301 }
302}
303
304wxRect 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
319void wxPrinterDC::StartPage(void)
320{
321 if ( !m_ok )
322 return ;
323
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;
334
335 m_nativePrinterDC->StartPage(this) ;
336 m_ok = m_nativePrinterDC->Ok() ;
337
338}
339
340void wxPrinterDC::EndPage(void)
341{
342 if ( !m_ok )
343 return ;
344
345 m_nativePrinterDC->EndPage(this) ;
346 m_ok = m_nativePrinterDC->Ok() ;
347}
348
349void wxPrinterDC::DoGetSize(int *width, int *height) const
350{
351 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
352 m_nativePrinterDC->GetSize(width, height ) ;
353}
354
355#endif