]>
git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/dcprint.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/dcprint.cpp
3 // Purpose: wxPrinterDC class
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #if wxUSE_PRINTING_ARCHITECTURE
20 #include "wx/dcprint.h"
23 #include "wx/msgdlg.h"
27 #include "wx/osx/private.h"
28 #include "wx/osx/private/print.h"
29 #include "wx/osx/dcprint.h"
30 #include "wx/graphics.h"
32 IMPLEMENT_ABSTRACT_CLASS(wxPrinterDCImpl
, wxGCDCImpl
)
34 class wxNativePrinterDC
37 wxNativePrinterDC() {}
38 virtual ~wxNativePrinterDC() {}
39 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) = 0;
40 virtual void EndDoc( wxPrinterDC
* dc
) = 0;
41 virtual void StartPage( wxPrinterDC
* dc
) = 0;
42 virtual void EndPage( wxPrinterDC
* dc
) = 0;
43 virtual void GetSize( int *w
, int *h
) const = 0 ;
44 virtual wxSize
GetPPI() const = 0 ;
46 // returns 0 in case of no Error, otherwise platform specific error codes
47 virtual wxUint32
GetStatus() const = 0 ;
48 bool IsOk() { return GetStatus() == 0 ; }
50 static wxNativePrinterDC
* Create(wxPrintData
* data
) ;
53 class wxMacCarbonPrinterDC
: public wxNativePrinterDC
56 wxMacCarbonPrinterDC( wxPrintData
* data
) ;
57 virtual ~wxMacCarbonPrinterDC() ;
58 virtual bool StartDoc( wxPrinterDC
* dc
, const wxString
& message
) ;
59 virtual void EndDoc( wxPrinterDC
* dc
) ;
60 virtual void StartPage( wxPrinterDC
* dc
) ;
61 virtual void EndPage( wxPrinterDC
* dc
) ;
62 virtual wxUint32
GetStatus() const { return m_err
; }
63 virtual void GetSize( int *w
, int *h
) const ;
64 virtual wxSize
GetPPI() const ;
72 wxMacCarbonPrinterDC::wxMacCarbonPrinterDC( wxPrintData
* data
)
75 wxOSXPrintData
*native
= (wxOSXPrintData
*) data
->GetNativeData() ;
78 m_err
= PMGetAdjustedPageRect(native
->GetPageFormat(), &rPage
);
82 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
83 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
87 m_err
= PMSessionGetCurrentPrinter(native
->GetPrintSession(), &printer
);
90 m_err
= PMPrinterGetOutputResolution( printer
, native
->GetPrintSettings(), &res
) ;
91 if ( m_err
== -9589 /* kPMKeyNotFound */ )
94 res
.hRes
= res
.vRes
= 300;
99 res
.hRes
= res
.vRes
= 300;
102 m_maxX
= wxCoord((double)m_maxX
* res
.hRes
/ 72.0);
103 m_maxY
= wxCoord((double)m_maxY
* res
.vRes
/ 72.0);
105 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
108 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
112 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
114 return new wxMacCarbonPrinterDC(data
) ;
117 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& message
)
122 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
123 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
125 PMPrintSettingsSetJobName(native
->GetPrintSettings(), wxCFStringRef(message
));
127 m_err
= PMSessionBeginCGDocumentNoDialog(native
->GetPrintSession(),
128 native
->GetPrintSettings(),
129 native
->GetPageFormat());
130 if ( m_err
!= noErr
)
134 m_err
= PMGetAdjustedPageRect(native
->GetPageFormat(), &rPage
);
135 if ( m_err
!= noErr
)
138 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
139 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
144 m_err
= PMSessionGetCurrentPrinter(native
->GetPrintSession(), &printer
);
147 m_err
= PMPrinterGetOutputResolution( printer
, native
->GetPrintSettings(), &res
) ;
148 if ( m_err
== -9589 /* kPMKeyNotFound */ )
151 res
.hRes
= res
.vRes
= 300;
155 m_maxX
= wxCoord((double)m_maxX
* res
.hRes
/ 72.0);
156 m_maxY
= wxCoord((double)m_maxY
* res
.vRes
/ 72.0);
158 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
162 void wxMacCarbonPrinterDC::EndDoc( wxPrinterDC
* dc
)
167 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
168 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
170 m_err
= PMSessionEndDocumentNoDialog(native
->GetPrintSession());
173 void wxMacCarbonPrinterDC::StartPage( wxPrinterDC
* dc
)
178 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
179 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
181 m_err
= PMSessionBeginPageNoDialog(native
->GetPrintSession(),
182 native
->GetPageFormat(),
185 CGContextRef pageContext
;
187 if ( m_err
== noErr
)
189 m_err
= PMSessionGetCGGraphicsContext(native
->GetPrintSession(),
193 if ( m_err
!= noErr
)
195 PMSessionEndPageNoDialog(native
->GetPrintSession());
196 PMSessionEndDocumentNoDialog(native
->GetPrintSession());
201 m_err
= PMGetAdjustedPaperRect( native
->GetPageFormat() , &paperRect
) ;
202 // make sure (0,0) is at the upper left of the printable area (wx conventions)
203 // Core Graphics initially has the lower left of the paper as 0,0
205 CGContextTranslateCTM( pageContext
, (CGFloat
) -paperRect
.left
, (CGFloat
) paperRect
.bottom
) ;
207 // since this is a non-critical error, we set the flag back
210 // Leopard deprecated PMSetResolution() which will not be available in 64 bit mode, so we avoid using it.
211 // To set the proper drawing resolution, the docs suggest the use of CGContextScaleCTM(), so here we go; as a
212 // consequence though, PMGetAdjustedPaperRect() and PMGetAdjustedPageRect() return unscaled rects, so we
213 // have to manually scale them later.
214 CGContextScaleCTM( pageContext
, 72.0 / (double)m_ppi
.x
, -72.0 / (double)m_ppi
.y
);
216 impl
->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext
) );
220 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
225 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
226 wxOSXPrintData
*native
= (wxOSXPrintData
*) impl
->GetPrintData().GetNativeData() ;
228 m_err
= PMSessionEndPageNoDialog(native
->GetPrintSession());
229 if ( m_err
!= noErr
)
231 PMSessionEndDocumentNoDialog(native
->GetPrintSession());
233 // the cg context we got when starting the page isn't valid anymore, so replace it
234 impl
->SetGraphicsContext( wxGraphicsContext::Create() );
237 void wxMacCarbonPrinterDC::GetSize( int *w
, int *h
) const
245 wxSize
wxMacCarbonPrinterDC::GetPPI() const
254 wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC
*owner
, const wxPrintData
& printdata
)
255 : wxGCDCImpl( owner
)
258 m_printData
= printdata
;
259 m_printData
.ConvertToNative() ;
260 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
261 if ( m_nativePrinterDC
)
263 m_ok
= m_nativePrinterDC
->IsOk() ;
267 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
268 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
273 wxSize sz
= GetPPI();
274 m_mm_to_pix_x
= mm2inches
* sz
.x
;
275 m_mm_to_pix_y
= mm2inches
* sz
.y
;
277 // we need at least a measuring context because people start measuring before a page
278 // gets printed at all
279 SetGraphicsContext( wxGraphicsContext::Create() );
283 wxSize
wxPrinterDCImpl::GetPPI() const
285 return m_nativePrinterDC
->GetPPI() ;
288 wxPrinterDCImpl::~wxPrinterDCImpl()
290 delete m_nativePrinterDC
;
293 bool wxPrinterDCImpl::StartDoc( const wxString
& message
)
295 wxASSERT_MSG( IsOk() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
300 if ( m_nativePrinterDC
->StartDoc( (wxPrinterDC
*) GetOwner(), message
) )
302 // in case we have to do additional things when successful
304 m_ok
= m_nativePrinterDC
->IsOk() ;
308 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
309 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
316 void wxPrinterDCImpl::EndDoc(void)
321 m_nativePrinterDC
->EndDoc( (wxPrinterDC
*) GetOwner() ) ;
322 m_ok
= m_nativePrinterDC
->IsOk() ;
327 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
328 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
333 wxRect
wxPrinterDCImpl::GetPaperRect() const
336 GetOwner()->GetSize(&w
, &h
);
337 wxRect
pageRect(0, 0, w
, h
);
338 wxOSXPrintData
*native
= (wxOSXPrintData
*) m_printData
.GetNativeData() ;
339 OSStatus err
= noErr
;
341 err
= PMGetAdjustedPaperRect(native
->GetPageFormat(), &rPaper
);
345 wxSize ppi
= GetOwner()->GetPPI();
346 rPaper
.right
*= (ppi
.x
/ 72.0);
347 rPaper
.bottom
*= (ppi
.y
/ 72.0);
348 rPaper
.left
*= (ppi
.x
/ 72.0);
349 rPaper
.top
*= (ppi
.y
/ 72.0);
351 return wxRect(wxCoord(rPaper
.left
), wxCoord(rPaper
.top
),
352 wxCoord(rPaper
.right
- rPaper
.left
), wxCoord(rPaper
.bottom
- rPaper
.top
));
355 void wxPrinterDCImpl::StartPage()
360 m_logicalFunction
= wxCOPY
;
361 // m_textAlignment = wxALIGN_TOP_LEFT;
362 m_backgroundMode
= wxTRANSPARENT
;
364 m_textForegroundColour
= *wxBLACK
;
365 m_textBackgroundColour
= *wxWHITE
;
366 m_pen
= *wxBLACK_PEN
;
367 m_font
= *wxNORMAL_FONT
;
368 m_brush
= *wxTRANSPARENT_BRUSH
;
369 m_backgroundBrush
= *wxWHITE_BRUSH
;
371 m_nativePrinterDC
->StartPage( (wxPrinterDC
*) GetOwner() ) ;
372 m_ok
= m_nativePrinterDC
->IsOk() ;
376 void wxPrinterDCImpl::EndPage()
381 m_nativePrinterDC
->EndPage( (wxPrinterDC
*) GetOwner() );
382 m_ok
= m_nativePrinterDC
->IsOk() ;
385 void wxPrinterDCImpl::DoGetSize(int *width
, int *height
) const
387 wxCHECK_RET( m_ok
, wxT("GetSize() doesn't work without a valid wxPrinterDC") );
388 m_nativePrinterDC
->GetSize(width
, height
) ;