]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcprint.cpp
added wxPowerEvent; moved power functions stubs to common/powercmn.cpp
[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"
72e7876b
SC
25#endif
26
463c4d71 27#include "wx/math.h"
2f1ae414 28#include "wx/mac/uma.h"
746d7582 29#include "wx/mac/private/print.h"
a3d3d3bf 30
72e7876b 31IMPLEMENT_CLASS(wxPrinterDC, wxDC)
72e7876b 32
746d7582
SC
33class wxNativePrinterDC
34{
35public :
36 wxNativePrinterDC() {}
37 virtual ~wxNativePrinterDC() {}
38 virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) = 0;
39 virtual void EndDoc( wxPrinterDC* dc ) = 0;
40 virtual void StartPage( wxPrinterDC* dc ) = 0;
41 virtual void EndPage( wxPrinterDC* dc ) = 0;
42 virtual wxCoord GetMaxX() const = 0 ;
43 virtual wxCoord GetMaxY() const = 0 ;
44 // returns 0 in case of no Error, otherwise platform specific error codes
45 virtual wxUint32 GetStatus() const = 0 ;
46 bool Ok() { return GetStatus() == 0 ; }
eb7f8ac5 47
746d7582
SC
48 static wxNativePrinterDC* Create(wxPrintData* data) ;
49} ;
72e7876b 50
746d7582
SC
51class wxMacCarbonPrinterDC : public wxNativePrinterDC
52{
53public :
54 wxMacCarbonPrinterDC( wxPrintData* data ) ;
55 ~wxMacCarbonPrinterDC() ;
56 virtual bool StartDoc( wxPrinterDC* dc , const wxString& message ) ;
57 virtual void EndDoc( wxPrinterDC* dc ) ;
58 virtual void StartPage( wxPrinterDC* dc ) ;
59 virtual void EndPage( wxPrinterDC* dc ) ;
60 virtual wxCoord GetMaxX() const { return m_maxX ; }
61 virtual wxCoord GetMaxY() const { return m_maxY ; }
62 virtual wxUint32 GetStatus() const { return m_err ; }
63private :
64 GrafPtr m_macPrintFormerPort ;
65 wxCoord m_maxX ;
66 wxCoord m_maxY ;
67 OSStatus m_err ;
68} ;
69
70wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData* data )
72e7876b 71{
746d7582
SC
72 ::GetPort( & m_macPrintFormerPort ) ;
73
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);
84}
75411508 85
746d7582
SC
86wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
87{
88 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
89 ::SetPort( m_macPrintFormerPort ) ;
90}
91
92wxNativePrinterDC* wxNativePrinterDC::Create(wxPrintData* data)
93{
94 return new wxMacCarbonPrinterDC(data) ;
95}
96
eb7f8ac5 97bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC* dc , const wxString& WXUNUSED(message) )
746d7582
SC
98{
99 if ( m_err )
100 return false ;
101
dc7ccb9c 102 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582 103
20b69855
SC
104#if wxMAC_USE_CORE_GRAPHICS
105 {
106 CFStringRef s[1] = { kPMGraphicsContextCoreGraphics };
107 CFArrayRef graphicsContextsArray = CFArrayCreate(NULL, (const void**)s, 1, &kCFTypeArrayCallBacks);
108 PMSessionSetDocumentFormatGeneration(native->m_macPrintSession, kPMDocumentFormatPDF, graphicsContextsArray, NULL);
109 CFRelease(graphicsContextsArray);
110 }
111#endif
112
746d7582 113 m_err = PMSessionBeginDocument(native->m_macPrintSession,
eb7f8ac5 114 native->m_macPrintSettings,
746d7582
SC
115 native->m_macPageFormat);
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
eb7f8ac5
VZ
124 m_maxX = (wxCoord)(rPage.right - rPage.left);
125 m_maxY = (wxCoord)(rPage.bottom - rPage.top);
746d7582
SC
126 return true ;
127}
128
eb7f8ac5 129void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC* dc )
746d7582
SC
130{
131 if ( m_err )
132 return ;
133
dc7ccb9c 134 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582
SC
135
136 m_err = PMSessionEndDocument(native->m_macPrintSession);
137}
138
eb7f8ac5 139void wxMacCarbonPrinterDC::StartPage( 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 = PMSessionBeginPage(native->m_macPrintSession,
147 native->m_macPageFormat,
148 nil);
eb7f8ac5 149
20b69855
SC
150#if wxMAC_USE_CORE_GRAPHICS
151 CGContextRef pageContext;
152#endif
746d7582 153 if ( m_err == noErr )
e40298d5 154 {
20b69855 155#if wxMAC_USE_CORE_GRAPHICS
eb7f8ac5 156 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
20b69855
SC
157 kPMGraphicsContextCoreGraphics,
158 (void**) &pageContext );
159 dc->MacSetCGContext(pageContext) ;
160#else
161 m_err = PMSessionGetGraphicsContext(native->m_macPrintSession,
162 kPMGraphicsContextQuickdraw,
eb7f8ac5 163 (void**) &dc->m_macPort );
20b69855 164#endif
e40298d5 165 }
eb7f8ac5 166
746d7582 167 if ( m_err != noErr )
e40298d5 168 {
746d7582
SC
169 PMSessionEndPage(native->m_macPrintSession);
170 PMSessionEndDocument(native->m_macPrintSession);
e40298d5 171 }
746d7582 172 else
e40298d5 173 {
746d7582 174 PMRect rPage;
eb7f8ac5 175
746d7582
SC
176 m_err = PMGetAdjustedPageRect(native->m_macPageFormat, &rPage);
177 if ( !m_err )
178 {
20b69855 179#if wxMAC_USE_CORE_GRAPHICS
a06e389f
SC
180 PMRect paperRect ;
181 PMGetAdjustedPaperRect( native->m_macPageFormat , &paperRect ) ;
182 CGContextTranslateCTM( pageContext , -paperRect.left , -paperRect.top + ( rPage.bottom - rPage.top ) ) ;
20b69855 183 CGContextScaleCTM( pageContext , 1 , -1 ) ;
c725df80 184 CGContextSaveGState( pageContext ) ;
20b69855 185#else
eb7f8ac5
VZ
186 dc->m_macLocalOrigin.x = (int) rPage.left;
187 dc->m_macLocalOrigin.y = (int) rPage.top;
20b69855 188#endif
746d7582
SC
189 }
190 // since this is a non-critical error, we set the flag back
191 m_err = noErr ;
e40298d5 192 }
746d7582
SC
193}
194
eb7f8ac5 195void wxMacCarbonPrinterDC::EndPage( wxPrinterDC* dc )
746d7582
SC
196{
197 if ( m_err )
198 return ;
199
dc7ccb9c 200 wxMacCarbonPrintData *native = (wxMacCarbonPrintData*) dc->GetPrintData().GetNativeData() ;
746d7582
SC
201
202 m_err = PMSessionEndPage(native->m_macPrintSession);
203 if ( m_err != noErr )
f520d381 204 {
746d7582 205 PMSessionEndDocument(native->m_macPrintSession);
f520d381 206 }
746d7582
SC
207}
208
746d7582
SC
209wxPrinterDC::wxPrinterDC(const wxPrintData& printdata)
210{
6d50343d 211 m_ok = false ;
746d7582
SC
212 m_printData = printdata ;
213 m_printData.ConvertToNative() ;
214 m_nativePrinterDC = wxNativePrinterDC::Create( &m_printData ) ;
eb7f8ac5 215 if ( m_nativePrinterDC )
75411508 216 {
746d7582 217 m_ok = m_nativePrinterDC->Ok() ;
746d7582
SC
218 if ( !m_ok )
219 {
220 wxString message ;
eb7f8ac5 221 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
222 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
223 dialog.ShowModal();
224 }
20b69855
SC
225#if wxMAC_USE_CORE_GRAPHICS
226 // the cgContext will only be handed over page by page
227 m_graphicContext = new wxMacCGContext() ;
228#endif
75411508 229 }
746d7582
SC
230}
231
232wxPrinterDC::~wxPrinterDC(void)
233{
c725df80
SC
234#if wxMAC_USE_CORE_GRAPHICS
235 // this context was borrowed
236 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( NULL ) ;
237#endif
746d7582
SC
238 delete m_nativePrinterDC ;
239}
240
20b69855 241#if wxMAC_USE_CORE_GRAPHICS
6d50343d 242void wxPrinterDC::MacSetCGContext( void * cg )
20b69855 243{
626fd619 244 ((wxMacCGContext*)(m_graphicContext))->SetNativeContext( (CGContextRef) cg ) ;
20b69855
SC
245 m_graphicContext->SetPen( m_pen ) ;
246 m_graphicContext->SetBrush( m_brush ) ;
247}
248#endif
eb7f8ac5 249bool wxPrinterDC::StartDoc( const wxString& message )
746d7582
SC
250{
251 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
eb7f8ac5 252
746d7582
SC
253 if ( !m_ok )
254 return false ;
255
256 if ( m_nativePrinterDC->StartDoc(this, message ) )
257 {
258 // in case we have to do additional things when successful
259 }
260 m_ok = m_nativePrinterDC->Ok() ;
261 if ( !m_ok )
75411508 262 {
746d7582 263 wxString message ;
eb7f8ac5 264 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
427ff662 265 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
75411508 266 dialog.ShowModal();
75411508 267 }
746d7582 268
e40298d5 269 return m_ok ;
72e7876b
SC
270}
271
eb7f8ac5 272void wxPrinterDC::EndDoc(void)
72e7876b 273{
746d7582
SC
274 if ( !m_ok )
275 return ;
276
277 m_nativePrinterDC->EndDoc( this ) ;
278 m_ok = m_nativePrinterDC->Ok() ;
279
280 if ( !m_ok )
75411508 281 {
746d7582 282 wxString message ;
eb7f8ac5 283 message.Printf( wxT("Print Error %u"), m_nativePrinterDC->GetStatus() ) ;
746d7582
SC
284 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
285 dialog.ShowModal();
75411508 286 }
72e7876b
SC
287}
288
eb7f8ac5 289void wxPrinterDC::StartPage(void)
72e7876b 290{
e40298d5
JS
291 if ( !m_ok )
292 return ;
2f1ae414 293
746d7582
SC
294 m_logicalFunction = wxCOPY;
295 // m_textAlignment = wxALIGN_TOP_LEFT;
296 m_backgroundMode = wxTRANSPARENT;
297
298 m_textForegroundColour = *wxBLACK;
299 m_textBackgroundColour = *wxWHITE;
300 m_pen = *wxBLACK_PEN;
301 m_font = *wxNORMAL_FONT;
302 m_brush = *wxTRANSPARENT_BRUSH;
303 m_backgroundBrush = *wxWHITE_BRUSH;
20b69855 304#if !wxMAC_USE_CORE_GRAPHICS
e40298d5
JS
305 m_macFontInstalled = false ;
306 m_macBrushInstalled = false ;
307 m_macPenInstalled = false ;
20b69855 308#endif
2f1ae414 309
746d7582
SC
310 m_nativePrinterDC->StartPage(this) ;
311 m_ok = m_nativePrinterDC->Ok() ;
eb7f8ac5 312
72e7876b
SC
313}
314
eb7f8ac5 315void wxPrinterDC::EndPage(void)
72e7876b 316{
e40298d5
JS
317 if ( !m_ok )
318 return ;
72e7876b 319
746d7582
SC
320 m_nativePrinterDC->EndPage(this) ;
321 m_ok = m_nativePrinterDC->Ok() ;
322}
72e7876b 323
746d7582
SC
324void wxPrinterDC::DoGetSize(int *width, int *height) const
325{
326 wxCHECK_RET( m_ok , _T("GetSize() doesn't work without a valid wxPrinterDC") );
2f1ae414 327
746d7582
SC
328 if ( width )
329 * width = m_nativePrinterDC->GetMaxX() ;
330 if ( height )
331 * height = m_nativePrinterDC->GetMaxY() ;
72e7876b 332}
746d7582 333
179e085f 334#endif