1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxPrinterDC class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dcprint.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if wxUSE_PRINTING_ARCHITECTURE
28 #include "wx/dcprint.h"
29 #include "wx/msgdlg.h"
31 #include "wx/mac/uma.h"
32 #include "wx/mac/private/print.h"
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
38 class wxNativePrinterDC
41 wxNativePrinterDC() {}
42 virtual ~wxNativePrinterDC() {}
43 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) = 0;
44 virtual void EndDoc( wxPrinterDC
* dc
) = 0;
45 virtual void StartPage( wxPrinterDC
* dc
) = 0;
46 virtual void EndPage( wxPrinterDC
* dc
) = 0;
47 virtual wxCoord
GetMaxX() const = 0 ;
48 virtual wxCoord
GetMaxY() const = 0 ;
49 // returns 0 in case of no Error, otherwise platform specific error codes
50 virtual wxUint32
GetStatus() const = 0 ;
51 bool Ok() { return GetStatus() == 0 ; }
53 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
56 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
59 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
60 ~wxMacCarbonPrinterDC() ;
61 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
62 virtual void EndDoc( wxPrinterDC
* dc
) ;
63 virtual void StartPage( wxPrinterDC
* dc
) ;
64 virtual void EndPage( wxPrinterDC
* dc
) ;
65 virtual wxCoord
GetMaxX() const { return m_maxX
; }
66 virtual wxCoord
GetMaxY() const { return m_maxY
; }
67 virtual wxUint32
GetStatus() const { return m_err
; }
69 GrafPtr m_macPrintFormerPort
;
75 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
77 ::GetPort( & m_macPrintFormerPort
) ;
80 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->GetNativeData() ;
83 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
87 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
88 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
91 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
93 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
94 ::SetPort( m_macPrintFormerPort
) ;
97 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
99 return new wxMacCarbonPrinterDC(data
) ;
102 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
107 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
109 #if wxMAC_USE_CORE_GRAPHICS
111 CFStringRef s
[1] = { kPMGraphicsContextCoreGraphics
};
112 CFArrayRef graphicsContextsArray
= CFArrayCreate(NULL
, (const void**)s
, 1, &kCFTypeArrayCallBacks
);
113 PMSessionSetDocumentFormatGeneration(native
->m_macPrintSession
, kPMDocumentFormatPDF
, graphicsContextsArray
, NULL
);
114 CFRelease(graphicsContextsArray
);
118 m_err
= PMSessionBeginDocument(native
->m_macPrintSession
,
119 native
->m_macPrintSettings
,
120 native
->m_macPageFormat
);
121 if ( m_err
!= noErr
)
125 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
126 if ( m_err
!= noErr
)
129 m_maxX
= (wxCoord
)(rPage
.right
- rPage
.left
);
130 m_maxY
= (wxCoord
)(rPage
.bottom
- rPage
.top
);
134 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
139 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
141 m_err
= PMSessionEndDocument(native
->m_macPrintSession
);
144 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
149 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
151 m_err
= PMSessionBeginPage(native
->m_macPrintSession
,
152 native
->m_macPageFormat
,
155 #if wxMAC_USE_CORE_GRAPHICS
156 CGContextRef pageContext
;
158 if ( m_err
== noErr
)
160 #if wxMAC_USE_CORE_GRAPHICS
161 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
162 kPMGraphicsContextCoreGraphics
,
163 (void**) &pageContext
);
164 dc
->MacSetCGContext(pageContext
) ;
166 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
167 kPMGraphicsContextQuickdraw
,
168 (void**) &dc
->m_macPort
);
172 if ( m_err
!= noErr
)
174 PMSessionEndPage(native
->m_macPrintSession
);
175 PMSessionEndDocument(native
->m_macPrintSession
);
181 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
184 #if wxMAC_USE_CORE_GRAPHICS
185 CGContextTranslateCTM( pageContext
, 0 , rPage
.bottom
- rPage
.top
) ;
186 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
188 dc
->m_macLocalOrigin
.x
= (int) rPage
.left
;
189 dc
->m_macLocalOrigin
.y
= (int) rPage
.top
;
192 // since this is a non-critical error, we set the flag back
197 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
202 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
204 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
205 if ( m_err
!= noErr
)
207 PMSessionEndDocument(native
->m_macPrintSession
);
211 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
214 m_printData
= printdata
;
215 m_printData
.ConvertToNative() ;
216 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
217 if ( m_nativePrinterDC
)
219 m_ok
= m_nativePrinterDC
->Ok() ;
223 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
224 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
227 #if wxMAC_USE_CORE_GRAPHICS
228 // the cgContext will only be handed over page by page
229 m_graphicContext
= new wxMacCGContext() ;
234 wxPrinterDC::~wxPrinterDC(void)
236 delete m_nativePrinterDC
;
239 #if wxMAC_USE_CORE_GRAPHICS
240 void wxPrinterDC::MacSetCGContext( void * cg
)
242 ((wxMacCGContext
*)(m_graphicContext
))->SetNativeContext( (CGContextRef
) cg
) ;
243 m_graphicContext
->SetPen( m_pen
) ;
244 m_graphicContext
->SetBrush( m_brush
) ;
247 bool wxPrinterDC::StartDoc( const wxString
& message
)
249 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
254 if ( m_nativePrinterDC
->StartDoc(this, message
) )
256 // in case we have to do additional things when successful
258 m_ok
= m_nativePrinterDC
->Ok() ;
262 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
263 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
270 void wxPrinterDC::EndDoc(void)
275 m_nativePrinterDC
->EndDoc( this ) ;
276 m_ok
= m_nativePrinterDC
->Ok() ;
281 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
282 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
287 void wxPrinterDC::StartPage(void)
292 m_logicalFunction
= wxCOPY
;
293 // m_textAlignment = wxALIGN_TOP_LEFT;
294 m_backgroundMode
= wxTRANSPARENT
;
296 m_textForegroundColour
= *wxBLACK
;
297 m_textBackgroundColour
= *wxWHITE
;
298 m_pen
= *wxBLACK_PEN
;
299 m_font
= *wxNORMAL_FONT
;
300 m_brush
= *wxTRANSPARENT_BRUSH
;
301 m_backgroundBrush
= *wxWHITE_BRUSH
;
302 #if !wxMAC_USE_CORE_GRAPHICS
303 m_macFontInstalled
= false ;
304 m_macBrushInstalled
= false ;
305 m_macPenInstalled
= false ;
308 m_nativePrinterDC
->StartPage(this) ;
309 m_ok
= m_nativePrinterDC
->Ok() ;
313 void wxPrinterDC::EndPage(void)
318 m_nativePrinterDC
->EndPage(this) ;
319 m_ok
= m_nativePrinterDC
->Ok() ;
322 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
324 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
327 * width
= m_nativePrinterDC
->GetMaxX() ;
329 * height
= m_nativePrinterDC
->GetMaxY() ;