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 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
36 class wxNativePrinterDC
39 wxNativePrinterDC() {}
40 virtual ~wxNativePrinterDC() {}
41 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) = 0;
42 virtual void EndDoc( wxPrinterDC
* dc
) = 0;
43 virtual void StartPage( wxPrinterDC
* dc
) = 0;
44 virtual void EndPage( wxPrinterDC
* dc
) = 0;
45 virtual wxCoord
GetMaxX() const = 0 ;
46 virtual wxCoord
GetMaxY() const = 0 ;
47 // returns 0 in case of no Error, otherwise platform specific error codes
48 virtual wxUint32
GetStatus() const = 0 ;
49 bool Ok() { return GetStatus() == 0 ; }
51 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
54 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
57 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
58 ~wxMacCarbonPrinterDC() ;
59 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
60 virtual void EndDoc( wxPrinterDC
* dc
) ;
61 virtual void StartPage( wxPrinterDC
* dc
) ;
62 virtual void EndPage( wxPrinterDC
* dc
) ;
63 virtual wxCoord
GetMaxX() const { return m_maxX
; }
64 virtual wxCoord
GetMaxY() const { return m_maxY
; }
65 virtual wxUint32
GetStatus() const { return m_err
; }
67 GrafPtr m_macPrintFormerPort
;
73 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
75 ::GetPort( & m_macPrintFormerPort
) ;
78 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->GetNativeData() ;
81 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
85 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
86 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
89 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
91 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
92 ::SetPort( m_macPrintFormerPort
) ;
95 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
97 return new wxMacCarbonPrinterDC(data
) ;
100 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
105 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
107 #if wxMAC_USE_CORE_GRAPHICS
109 CFStringRef s
[1] = { kPMGraphicsContextCoreGraphics
};
110 CFArrayRef graphicsContextsArray
= CFArrayCreate(NULL
, (const void**)s
, 1, &kCFTypeArrayCallBacks
);
111 PMSessionSetDocumentFormatGeneration(native
->m_macPrintSession
, kPMDocumentFormatPDF
, graphicsContextsArray
, NULL
);
112 CFRelease(graphicsContextsArray
);
116 m_err
= PMSessionBeginDocument(native
->m_macPrintSession
,
117 native
->m_macPrintSettings
,
118 native
->m_macPageFormat
);
119 if ( m_err
!= noErr
)
123 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
124 if ( m_err
!= noErr
)
127 m_maxX
= (wxCoord
)(rPage
.right
- rPage
.left
);
128 m_maxY
= (wxCoord
)(rPage
.bottom
- rPage
.top
);
132 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
137 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
139 m_err
= PMSessionEndDocument(native
->m_macPrintSession
);
142 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
147 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
149 m_err
= PMSessionBeginPage(native
->m_macPrintSession
,
150 native
->m_macPageFormat
,
153 #if wxMAC_USE_CORE_GRAPHICS
154 CGContextRef pageContext
;
156 if ( m_err
== noErr
)
158 #if wxMAC_USE_CORE_GRAPHICS
159 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
160 kPMGraphicsContextCoreGraphics
,
161 (void**) &pageContext
);
162 dc
->MacSetCGContext(pageContext
) ;
164 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
165 kPMGraphicsContextQuickdraw
,
166 (void**) &dc
->m_macPort
);
170 if ( m_err
!= noErr
)
172 PMSessionEndPage(native
->m_macPrintSession
);
173 PMSessionEndDocument(native
->m_macPrintSession
);
179 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
182 #if wxMAC_USE_CORE_GRAPHICS
183 CGContextTranslateCTM( pageContext
, 0 , rPage
.bottom
- rPage
.top
) ;
184 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
186 dc
->m_macLocalOrigin
.x
= (int) rPage
.left
;
187 dc
->m_macLocalOrigin
.y
= (int) rPage
.top
;
190 // since this is a non-critical error, we set the flag back
195 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
200 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
202 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
203 if ( m_err
!= noErr
)
205 PMSessionEndDocument(native
->m_macPrintSession
);
209 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
212 m_printData
= printdata
;
213 m_printData
.ConvertToNative() ;
214 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
215 if ( m_nativePrinterDC
)
217 m_ok
= m_nativePrinterDC
->Ok() ;
221 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
222 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
225 #if wxMAC_USE_CORE_GRAPHICS
226 // the cgContext will only be handed over page by page
227 m_graphicContext
= new wxMacCGContext() ;
232 wxPrinterDC::~wxPrinterDC(void)
234 delete m_nativePrinterDC
;
237 #if wxMAC_USE_CORE_GRAPHICS
238 void wxPrinterDC::MacSetCGContext( void * cg
)
240 ((wxMacCGContext
*)(m_graphicContext
))->SetNativeContext( (CGContextRef
) cg
) ;
241 m_graphicContext
->SetPen( m_pen
) ;
242 m_graphicContext
->SetBrush( m_brush
) ;
245 bool wxPrinterDC::StartDoc( const wxString
& message
)
247 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
252 if ( m_nativePrinterDC
->StartDoc(this, message
) )
254 // in case we have to do additional things when successful
256 m_ok
= m_nativePrinterDC
->Ok() ;
260 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
261 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
268 void wxPrinterDC::EndDoc(void)
273 m_nativePrinterDC
->EndDoc( this ) ;
274 m_ok
= m_nativePrinterDC
->Ok() ;
279 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
280 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
285 void wxPrinterDC::StartPage(void)
290 m_logicalFunction
= wxCOPY
;
291 // m_textAlignment = wxALIGN_TOP_LEFT;
292 m_backgroundMode
= wxTRANSPARENT
;
294 m_textForegroundColour
= *wxBLACK
;
295 m_textBackgroundColour
= *wxWHITE
;
296 m_pen
= *wxBLACK_PEN
;
297 m_font
= *wxNORMAL_FONT
;
298 m_brush
= *wxTRANSPARENT_BRUSH
;
299 m_backgroundBrush
= *wxWHITE_BRUSH
;
300 #if !wxMAC_USE_CORE_GRAPHICS
301 m_macFontInstalled
= false ;
302 m_macBrushInstalled
= false ;
303 m_macPenInstalled
= false ;
306 m_nativePrinterDC
->StartPage(this) ;
307 m_ok
= m_nativePrinterDC
->Ok() ;
311 void wxPrinterDC::EndPage(void)
316 m_nativePrinterDC
->EndPage(this) ;
317 m_ok
= m_nativePrinterDC
->Ok() ;
320 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
322 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
325 * width
= m_nativePrinterDC
->GetMaxX() ;
327 * height
= m_nativePrinterDC
->GetMaxY() ;