1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dcprint.cpp
3 // Purpose: wxPrinterDC class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #if wxUSE_PRINTING_ARCHITECTURE
21 #include "wx/dcprint.h"
24 #include "wx/msgdlg.h"
28 #include "wx/mac/uma.h"
29 #include "wx/mac/private/print.h"
31 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
33 class wxNativePrinterDC
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 void GetSize( int *w
, int *h
) const = 0 ;
43 virtual wxSize
GetPPI() const = 0 ;
45 // returns 0 in case of no Error, otherwise platform specific error codes
46 virtual wxUint32
GetStatus() const = 0 ;
47 bool Ok() { return GetStatus() == 0 ; }
49 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
52 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
55 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
56 ~wxMacCarbonPrinterDC() ;
57 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
58 virtual void EndDoc( wxPrinterDC
* dc
) ;
59 virtual void StartPage( wxPrinterDC
* dc
) ;
60 virtual void EndPage( wxPrinterDC
* dc
) ;
61 virtual wxUint32
GetStatus() const { return m_err
; }
62 virtual void GetSize( int *w
, int *h
) const ;
63 virtual wxSize
GetPPI() const ;
65 GrafPtr m_macPrintFormerPort
;
72 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
74 ::GetPort( & m_macPrintFormerPort
) ;
77 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->GetNativeData() ;
80 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
84 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
85 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
88 m_err
= PMGetResolution((PMPageFormat
) (native
->m_macPageFormat
), &res
);
89 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
92 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
94 // nothing to release from print data, as wxPrinterDC has all data in its wxPrintData member
95 ::SetPort( m_macPrintFormerPort
) ;
98 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
100 return new wxMacCarbonPrinterDC(data
) ;
103 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
108 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
110 #if wxMAC_USE_CORE_GRAPHICS
112 CFStringRef s
[1] = { kPMGraphicsContextCoreGraphics
};
113 CFArrayRef graphicsContextsArray
= CFArrayCreate(NULL
, (const void**)s
, 1, &kCFTypeArrayCallBacks
);
114 PMSessionSetDocumentFormatGeneration(native
->m_macPrintSession
, kPMDocumentFormatPDF
, graphicsContextsArray
, NULL
);
115 CFRelease(graphicsContextsArray
);
119 m_err
= PMSessionBeginDocument(native
->m_macPrintSession
,
120 native
->m_macPrintSettings
,
121 native
->m_macPageFormat
);
122 if ( m_err
!= noErr
)
126 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
127 if ( m_err
!= noErr
)
130 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
131 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
134 m_err
= PMGetResolution((PMPageFormat
) (native
->m_macPageFormat
), &res
);
135 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
139 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
144 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
146 m_err
= PMSessionEndDocument(native
->m_macPrintSession
);
149 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
154 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
156 m_err
= PMSessionBeginPage(native
->m_macPrintSession
,
157 native
->m_macPageFormat
,
160 #if wxMAC_USE_CORE_GRAPHICS
161 CGContextRef pageContext
;
163 if ( m_err
== noErr
)
165 #if wxMAC_USE_CORE_GRAPHICS
166 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
167 kPMGraphicsContextCoreGraphics
,
168 (void**) &pageContext
);
169 dc
->MacSetCGContext(pageContext
) ;
171 m_err
= PMSessionGetGraphicsContext(native
->m_macPrintSession
,
172 kPMGraphicsContextQuickdraw
,
173 (void**) &dc
->m_macPort
);
177 if ( m_err
!= noErr
)
179 PMSessionEndPage(native
->m_macPrintSession
);
180 PMSessionEndDocument(native
->m_macPrintSession
);
186 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
189 #if wxMAC_USE_CORE_GRAPHICS
191 PMGetAdjustedPaperRect( native
->m_macPageFormat
, &paperRect
) ;
192 CGContextTranslateCTM( pageContext
, -paperRect
.left
, -paperRect
.top
+ ( rPage
.bottom
- rPage
.top
) ) ;
193 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
194 CGContextSaveGState( pageContext
) ;
196 dc
->m_macLocalOrigin
.x
= (int) rPage
.left
;
197 dc
->m_macLocalOrigin
.y
= (int) rPage
.top
;
200 // since this is a non-critical error, we set the flag back
205 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
210 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
212 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
213 if ( m_err
!= noErr
)
215 PMSessionEndDocument(native
->m_macPrintSession
);
219 void wxMacCarbonPrinterDC::GetSize( int *w
, int *h
) const
227 wxSize
wxMacCarbonPrinterDC::GetPPI() const
236 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
239 m_printData
= printdata
;
240 m_printData
.ConvertToNative() ;
241 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
242 if ( m_nativePrinterDC
)
244 m_ok
= m_nativePrinterDC
->Ok() ;
248 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
249 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
254 wxSize sz
= GetPPI();
255 m_mm_to_pix_x
= mm2inches
* sz
.x
;
256 m_mm_to_pix_y
= mm2inches
* sz
.y
;
258 #if wxMAC_USE_CORE_GRAPHICS
259 // the cgContext will only be handed over page by page
260 m_graphicContext
= new wxMacCGContext() ;
265 wxSize
wxPrinterDC::GetPPI() const
267 return m_nativePrinterDC
->GetPPI() ;
270 wxPrinterDC::~wxPrinterDC(void)
272 #if wxMAC_USE_CORE_GRAPHICS
273 // this context was borrowed
274 ((wxMacCGContext
*)(m_graphicContext
))->SetNativeContext( NULL
) ;
276 delete m_nativePrinterDC
;
279 #if wxMAC_USE_CORE_GRAPHICS
280 void wxPrinterDC::MacSetCGContext( void * cg
)
282 ((wxMacCGContext
*)(m_graphicContext
))->SetNativeContext( (CGContextRef
) cg
) ;
283 m_graphicContext
->SetPen( m_pen
) ;
284 m_graphicContext
->SetBrush( m_brush
) ;
287 bool wxPrinterDC::StartDoc( const wxString
& message
)
289 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
294 if ( m_nativePrinterDC
->StartDoc(this, message
) )
296 // in case we have to do additional things when successful
298 m_ok
= m_nativePrinterDC
->Ok() ;
302 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
303 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
310 void wxPrinterDC::EndDoc(void)
315 m_nativePrinterDC
->EndDoc( this ) ;
316 m_ok
= m_nativePrinterDC
->Ok() ;
321 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
322 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
327 void wxPrinterDC::StartPage(void)
332 m_logicalFunction
= wxCOPY
;
333 // m_textAlignment = wxALIGN_TOP_LEFT;
334 m_backgroundMode
= wxTRANSPARENT
;
336 m_textForegroundColour
= *wxBLACK
;
337 m_textBackgroundColour
= *wxWHITE
;
338 m_pen
= *wxBLACK_PEN
;
339 m_font
= *wxNORMAL_FONT
;
340 m_brush
= *wxTRANSPARENT_BRUSH
;
341 m_backgroundBrush
= *wxWHITE_BRUSH
;
342 #if !wxMAC_USE_CORE_GRAPHICS
343 m_macFontInstalled
= false ;
344 m_macBrushInstalled
= false ;
345 m_macPenInstalled
= false ;
348 m_nativePrinterDC
->StartPage(this) ;
349 m_ok
= m_nativePrinterDC
->Ok() ;
353 void wxPrinterDC::EndPage(void)
358 m_nativePrinterDC
->EndPage(this) ;
359 m_ok
= m_nativePrinterDC
->Ok() ;
362 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
364 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
365 m_nativePrinterDC
->GetSize(width
, height
) ;