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"
26 #include "wx/dcprint.h"
27 #include "wx/msgdlg.h"
29 #include "wx/mac/uma.h"
30 #include "wx/mac/private/print.h"
32 #if !USE_SHARED_LIBRARY
33 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
) ;
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
->m_nativePrintData
;
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().m_nativePrintData
;
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().m_nativePrintData
;
141 m_err
= PMSessionEndDocument(native
->m_macPrintSession
);
144 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
149 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().m_nativePrintData
;
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().m_nativePrintData
;
204 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
205 if ( m_err
!= noErr
)
207 PMSessionEndDocument(native
->m_macPrintSession
);
213 class wxMacClassicPrinterDC
: public wxNativePrinterDC
216 wxMacClassicPrinterDC( wxPrintData
* data
) ;
217 ~wxMacClassicPrinterDC() ;
218 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
219 virtual void EndDoc( wxPrinterDC
* dc
) ;
220 virtual void StartPage( wxPrinterDC
* dc
) ;
221 virtual void EndPage( wxPrinterDC
* dc
) ;
222 virtual wxCoord
GetMaxX() const { return m_maxX
; }
223 virtual wxCoord
GetMaxY() const { return m_maxY
; }
224 virtual wxUint32
GetStatus() const { return m_err
; }
226 GrafPtr m_macPrintFormerPort
;
227 TPPrPort m_macPrintingPort
;
233 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
235 return new wxMacClassicPrinterDC(data
) ;
238 wxMacClassicPrinterDC::wxMacClassicPrinterDC(wxPrintData
* data
)
240 ::GetPort( &m_macPrintFormerPort
) ;
244 if ( m_err
!= noErr
)
247 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) data
->m_nativePrintData
;
249 if ( ::PrValidate( native
->m_macPrintSettings
) )
251 // the driver has changed in the mean time, should we pop up a page setup dialog ?
252 if ( !::PrStlDialog( native
->m_macPrintSettings
) )
260 if ( m_err
== noErr
)
262 m_maxX
= (**native
->m_macPrintSettings
).prInfo
.rPage
.right
- (**native
->m_macPrintSettings
).prInfo
.rPage
.left
;
263 m_maxY
= (**native
->m_macPrintSettings
).prInfo
.rPage
.bottom
- (**native
->m_macPrintSettings
).prInfo
.rPage
.top
;
267 wxMacClassicPrinterDC::~wxMacClassicPrinterDC()
270 ::SetPort( LMGetWMgrPort() ) ;
273 bool wxMacClassicPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
278 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) dc
->GetPrintData().m_nativePrintData
;
279 m_macPrintingPort
= ::PrOpenDoc( native
->m_macPrintSettings
, NULL
, NULL
) ;
285 dc
->m_macPort
= (GrafPtr
) m_macPrintingPort
;
286 m_maxX
= (**native
->m_macPrintSettings
).prInfo
.rPage
.right
- (**native
->m_macPrintSettings
).prInfo
.rPage
.left
;
287 m_maxY
= (**native
->m_macPrintSettings
).prInfo
.rPage
.bottom
- (**native
->m_macPrintSettings
).prInfo
.rPage
.top
;
291 void wxMacClassicPrinterDC::EndDoc( wxPrinterDC
* dc
)
296 PrCloseDoc( m_macPrintingPort
) ;
300 void wxMacClassicPrinterDC::StartPage( wxPrinterDC
* dc
)
305 wxMacClassicPrintData
*native
= (wxMacClassicPrintData
*) dc
->GetPrintData().m_nativePrintData
;
307 PrOpenPage( m_macPrintingPort
, NULL
) ;
308 dc
->m_macLocalOrigin
.x
= (**native
->m_macPrintSettings
).rPaper
.left
;
309 dc
->m_macLocalOrigin
.y
= (**native
->m_macPrintSettings
).rPaper
.top
;
310 // m_macPrintingPort is now the current port
311 Rect clip
= { -32000 , -32000 , 32000 , 32000 } ;
312 ::ClipRect( &clip
) ;
314 if ( m_err
!= noErr
)
315 ::PrCloseDoc( m_macPrintingPort
) ;
318 void wxMacClassicPrinterDC::EndPage( wxPrinterDC
* dc
)
323 PrClosePage( m_macPrintingPort
) ;
325 if ( m_err
!= noErr
)
326 ::PrCloseDoc( m_macPrintingPort
) ;
331 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
334 m_printData
= printdata
;
335 m_printData
.ConvertToNative() ;
336 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
337 if ( m_nativePrinterDC
)
339 m_ok
= m_nativePrinterDC
->Ok() ;
343 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
344 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
347 #if wxMAC_USE_CORE_GRAPHICS
348 // the cgContext will only be handed over page by page
349 m_graphicContext
= new wxMacCGContext() ;
354 wxPrinterDC::~wxPrinterDC(void)
356 delete m_nativePrinterDC
;
359 #if wxMAC_USE_CORE_GRAPHICS
360 void wxPrinterDC::MacSetCGContext( void * cg
)
362 dynamic_cast<wxMacCGContext
*>(m_graphicContext
)->SetNativeContext( (CGContextRef
) cg
) ;
363 m_graphicContext
->SetPen( m_pen
) ;
364 m_graphicContext
->SetBrush( m_brush
) ;
367 bool wxPrinterDC::StartDoc( const wxString
& message
)
369 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
374 if ( m_nativePrinterDC
->StartDoc(this, message
) )
376 // in case we have to do additional things when successful
378 m_ok
= m_nativePrinterDC
->Ok() ;
382 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
383 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
390 void wxPrinterDC::EndDoc(void)
395 m_nativePrinterDC
->EndDoc( this ) ;
396 m_ok
= m_nativePrinterDC
->Ok() ;
401 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
402 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
407 void wxPrinterDC::StartPage(void)
412 m_logicalFunction
= wxCOPY
;
413 // m_textAlignment = wxALIGN_TOP_LEFT;
414 m_backgroundMode
= wxTRANSPARENT
;
416 m_textForegroundColour
= *wxBLACK
;
417 m_textBackgroundColour
= *wxWHITE
;
418 m_pen
= *wxBLACK_PEN
;
419 m_font
= *wxNORMAL_FONT
;
420 m_brush
= *wxTRANSPARENT_BRUSH
;
421 m_backgroundBrush
= *wxWHITE_BRUSH
;
422 #if !wxMAC_USE_CORE_GRAPHICS
423 m_macFontInstalled
= false ;
424 m_macBrushInstalled
= false ;
425 m_macPenInstalled
= false ;
428 m_nativePrinterDC
->StartPage(this) ;
429 m_ok
= m_nativePrinterDC
->Ok() ;
433 void wxPrinterDC::EndPage(void)
438 m_nativePrinterDC
->EndPage(this) ;
439 m_ok
= m_nativePrinterDC
->Ok() ;
442 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
444 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
447 * width
= m_nativePrinterDC
->GetMaxX() ;
449 * height
= m_nativePrinterDC
->GetMaxY() ;