]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcprint.cpp
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"
30 #include "wx/graphics.h"
32 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
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 Ok() { 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 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) data
->GetNativeData() ;
78 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
82 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
83 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
86 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
88 PMSessionGetCurrentPrinter(native
->m_macPrintSession
, &printer
);
89 PMPrinterGetOutputResolution( printer
, native
->m_macPrintSettings
, &res
) ;
91 m_err
= PMGetResolution((PMPageFormat
) (native
->m_macPageFormat
), &res
);
93 m_ppi
= wxSize(int(res
.hRes
), int(res
.vRes
));
96 wxMacCarbonPrinterDC::~wxMacCarbonPrinterDC()
100 wxNativePrinterDC
* wxNativePrinterDC::Create(wxPrintData
* data
)
102 return new wxMacCarbonPrinterDC(data
) ;
105 bool wxMacCarbonPrinterDC::StartDoc( wxPrinterDC
* dc
, const wxString
& WXUNUSED(message
) )
110 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
112 m_err
= PMSessionBeginCGDocument(native
->m_macPrintSession
,
113 native
->m_macPrintSettings
,
114 native
->m_macPageFormat
);
116 if ( m_err
!= noErr
)
120 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
121 if ( m_err
!= noErr
)
124 m_maxX
= wxCoord(rPage
.right
- rPage
.left
) ;
125 m_maxY
= wxCoord(rPage
.bottom
- rPage
.top
);
128 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
130 PMSessionGetCurrentPrinter(native
->m_macPrintSession
, &printer
);
131 PMPrinterGetOutputResolution( printer
, native
->m_macPrintSettings
, &res
) ;
133 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 CGContextRef pageContext
;
162 if ( m_err
== noErr
)
164 m_err
= PMSessionGetCGGraphicsContext(native
->m_macPrintSession
,
168 if ( m_err
!= noErr
)
170 PMSessionEndPage(native
->m_macPrintSession
);
171 PMSessionEndDocument(native
->m_macPrintSession
);
177 m_err
= PMGetAdjustedPageRect(native
->m_macPageFormat
, &rPage
);
181 PMGetAdjustedPaperRect( native
->m_macPageFormat
, &paperRect
) ;
182 // make sure (0,0) is at the upper left of the printable area (wx conventions)
183 // Core Graphics initially has the lower left of the paper as 0,0
184 CGContextTranslateCTM( pageContext
, -paperRect
.left
, paperRect
.bottom
) ;
185 CGContextScaleCTM( pageContext
, 1 , -1 ) ;
187 // since this is a non-critical error, we set the flag back
190 dc
->SetGraphicsContext( wxGraphicsContext::CreateFromNative( pageContext
) );
193 void wxMacCarbonPrinterDC::EndPage( wxPrinterDC
* dc
)
198 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) dc
->GetPrintData().GetNativeData() ;
200 m_err
= PMSessionEndPage(native
->m_macPrintSession
);
201 if ( m_err
!= noErr
)
203 PMSessionEndDocument(native
->m_macPrintSession
);
205 // the cg context we got when starting the page isn't valid anymore, so replace it
206 dc
->SetGraphicsContext( wxGraphicsContext::Create() );
209 void wxMacCarbonPrinterDC::GetSize( int *w
, int *h
) const
217 wxSize
wxMacCarbonPrinterDC::GetPPI() const
226 wxPrinterDC::wxPrinterDC(const wxPrintData
& printdata
)
229 m_printData
= printdata
;
230 m_printData
.ConvertToNative() ;
231 m_nativePrinterDC
= wxNativePrinterDC::Create( &m_printData
) ;
232 if ( m_nativePrinterDC
)
234 m_ok
= m_nativePrinterDC
->Ok() ;
238 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
239 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
244 wxSize sz
= GetPPI();
245 m_mm_to_pix_x
= mm2inches
* sz
.x
;
246 m_mm_to_pix_y
= mm2inches
* sz
.y
;
248 // we need at least a measuring context because people start measuring before a page
249 // gets printed at all
250 SetGraphicsContext( wxGraphicsContext::Create() );
254 wxSize
wxPrinterDC::GetPPI() const
256 return m_nativePrinterDC
->GetPPI() ;
259 wxPrinterDC::~wxPrinterDC(void)
261 delete m_nativePrinterDC
;
264 bool wxPrinterDC::StartDoc( const wxString
& message
)
266 wxASSERT_MSG( Ok() , wxT("Called wxPrinterDC::StartDoc from an invalid object") ) ;
271 if ( m_nativePrinterDC
->StartDoc(this, message
) )
273 // in case we have to do additional things when successful
275 m_ok
= m_nativePrinterDC
->Ok() ;
279 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
280 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
287 void wxPrinterDC::EndDoc(void)
292 m_nativePrinterDC
->EndDoc( this ) ;
293 m_ok
= m_nativePrinterDC
->Ok() ;
298 message
.Printf( wxT("Print Error %u"), m_nativePrinterDC
->GetStatus() ) ;
299 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
) ;
304 wxRect
wxPrinterDC::GetPaperRect()
308 wxRect
pageRect(0, 0, w
, h
);
309 wxMacCarbonPrintData
*native
= (wxMacCarbonPrintData
*) m_printData
.GetNativeData() ;
310 OSStatus err
= noErr
;
312 err
= PMGetAdjustedPaperRect(native
->m_macPageFormat
, &rPaper
);
315 return wxRect(wxCoord(rPaper
.left
), wxCoord(rPaper
.top
),
316 wxCoord(rPaper
.right
- rPaper
.left
), wxCoord(rPaper
.bottom
- rPaper
.top
));
319 void wxPrinterDC::StartPage(void)
324 m_logicalFunction
= wxCOPY
;
325 // m_textAlignment = wxALIGN_TOP_LEFT;
326 m_backgroundMode
= wxTRANSPARENT
;
328 m_textForegroundColour
= *wxBLACK
;
329 m_textBackgroundColour
= *wxWHITE
;
330 m_pen
= *wxBLACK_PEN
;
331 m_font
= *wxNORMAL_FONT
;
332 m_brush
= *wxTRANSPARENT_BRUSH
;
333 m_backgroundBrush
= *wxWHITE_BRUSH
;
335 m_nativePrinterDC
->StartPage(this) ;
336 m_ok
= m_nativePrinterDC
->Ok() ;
340 void wxPrinterDC::EndPage(void)
345 m_nativePrinterDC
->EndPage(this) ;
346 m_ok
= m_nativePrinterDC
->Ok() ;
349 void wxPrinterDC::DoGetSize(int *width
, int *height
) const
351 wxCHECK_RET( m_ok
, _T("GetSize() doesn't work without a valid wxPrinterDC") );
352 m_nativePrinterDC
->GetSize(width
, height
) ;