1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dcclient.cpp
3 // Purpose: wxClientDC class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/dcclient.h"
18 #include "wx/window.h"
19 #include "wx/dcmemory.h"
20 #include "wx/settings.h"
21 #include "wx/toplevel.h"
23 #include "wx/region.h"
26 #include "wx/mac/private.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
37 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
38 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxWindowDC
)
44 #include "wx/mac/uma.h"
45 #include "wx/notebook.h"
46 #include "wx/tabctrl.h"
49 static wxBrush
MacGetBackgroundBrush( wxWindow
* window
)
51 wxBrush bkdBrush
= window
->MacGetBackgroundBrush() ;
53 #if !TARGET_API_MAC_OSX
54 // transparency cannot be handled by the OS when not using composited windows
55 wxWindow
* parent
= window
->GetParent() ;
57 // if we have some 'pseudo' transparency
58 if ( ! bkdBrush
.Ok() || bkdBrush
.GetStyle() == wxTRANSPARENT
|| window
->GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
) )
60 // walk up until we find something
61 while ( parent
!= NULL
)
63 if ( parent
->GetBackgroundColour() != wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
) )
65 // if we have any other colours in the hierarchy
66 bkdBrush
.SetColour( parent
->GetBackgroundColour() ) ;
70 if ( parent
->IsKindOf( CLASSINFO(wxTopLevelWindow
) ) )
72 bkdBrush
= parent
->MacGetBackgroundBrush() ;
76 if ( parent
->IsKindOf( CLASSINFO( wxNotebook
) )
78 || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) )
79 #endif // wxUSE_TAB_DIALOG
82 Rect extent
= { 0 , 0 , 0 , 0 } ;
85 wxSize size
= parent
->GetSize() ;
86 parent
->MacClientToRootWindow( &x
, &y
) ;
90 extent
.right
= x
+ size
.x
;
91 extent
.bottom
= y
+ size
.y
;
92 bkdBrush
.MacSetThemeBackground( kThemeBackgroundTabPane
, (WXRECTPTR
) &extent
) ;
96 parent
= parent
->GetParent() ;
100 if ( !bkdBrush
.Ok() || bkdBrush
.GetStyle() == wxTRANSPARENT
)
102 // if we did not find something, use a default
103 bkdBrush
.MacSetTheme( kThemeBrushDialogBackgroundActive
) ;
110 wxWindowDC::wxWindowDC()
115 wxWindowDC::wxWindowDC(wxWindow
*window
)
118 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
124 window
->MacWindowToRootWindow( &x
, &y
) ;
127 #if wxMAC_USE_CORE_GRAPHICS
128 m_window
->GetSize( &m_width
, &m_height
);
129 CGContextRef cg
= (CGContextRef
) window
->MacGetCGContextRef();
133 SetGraphicsContext( wxGraphicsContext::Create( window
) ) ;
134 SetDeviceOrigin( x
, y
);
138 CGContextSaveGState( cg
);
140 // make sure the context is having its origin at the wx-window coordinates of the
141 // view (read at the top of window.cpp about the differences)
142 if ( window
->MacGetLeftBorderSize() != 0 || window
->MacGetTopBorderSize() != 0 )
143 CGContextTranslateCTM( cg
, -window
->MacGetLeftBorderSize() , -window
->MacGetTopBorderSize() );
145 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg
) );
147 m_graphicContext
->SetPen( m_pen
) ;
148 m_graphicContext
->SetBrush( m_brush
) ;
149 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
151 m_macLocalOrigin
.x
= x
;
152 m_macLocalOrigin
.y
= y
;
153 m_macPort
= UMAGetWindowPort( (WindowRef
) rootwindow
->MacGetWindowRef() ) ;
155 CopyRgn( (RgnHandle
) window
->MacGetVisibleRegion(true).GetWXHRGN() , (RgnHandle
) m_macBoundaryClipRgn
) ;
156 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, m_macLocalOrigin
.x
, m_macLocalOrigin
.y
) ;
157 CopyRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) m_macCurrentClipRgn
) ;
159 SetBackground(MacGetBackgroundBrush(window
));
161 SetFont( window
->GetFont() ) ;
164 wxWindowDC::~wxWindowDC()
166 #if wxMAC_USE_CORE_GRAPHICS
167 if ( m_release
&& m_graphicContext
)
169 CGContextRef cg
= (CGContextRef
) m_window
->MacGetCGContextRef();
170 CGContextRestoreGState(cg
);
171 //CGContextRef cg = (CGContextRef) m_graphicContext->GetNativeContext() ;
176 void wxWindowDC::DoGetSize( int* width
, int* height
) const
178 #if wxMAC_USE_CORE_GRAPHICS
184 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
185 m_window
->GetSize(width
, height
);
193 wxClientDC::wxClientDC()
198 #if wxMAC_USE_CORE_GRAPHICS
199 wxClientDC::wxClientDC(wxWindow
*window
) :
202 wxPoint origin
= window
->GetClientAreaOrigin() ;
203 wxSize size
= window
->GetClientSize() ;
207 window
->MacWindowToRootWindow( &x
, &y
) ;
208 m_window
->GetClientSize( &m_width
, &m_height
);
209 SetDeviceOrigin( origin
.x
, origin
.y
);
210 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
213 wxClientDC::wxClientDC(wxWindow
*window
)
216 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
220 WindowRef windowref
= (WindowRef
) rootwindow
->MacGetWindowRef() ;
221 wxPoint origin
= window
->GetClientAreaOrigin() ;
222 wxSize size
= window
->GetClientSize() ;
226 window
->MacWindowToRootWindow( &x
, &y
) ;
227 m_macPort
= UMAGetWindowPort( windowref
) ;
230 m_macLocalOrigin
.x
= x
;
231 m_macLocalOrigin
.y
= y
;
232 SetRectRgn( (RgnHandle
) m_macBoundaryClipRgn
, origin
.x
, origin
.y
, origin
.x
+ size
.x
, origin
.y
+ size
.y
) ;
233 SectRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) window
->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle
) m_macBoundaryClipRgn
) ;
234 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, -origin
.x
, -origin
.y
) ;
235 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, m_macLocalOrigin
.x
, m_macLocalOrigin
.y
) ;
236 CopyRgn( (RgnHandle
) m_macBoundaryClipRgn
,(RgnHandle
) m_macCurrentClipRgn
) ;
238 SetBackground(MacGetBackgroundBrush(window
));
239 SetFont( window
->GetFont() ) ;
243 wxClientDC::~wxClientDC()
247 #if !wxMAC_USE_CORE_GRAPHICS
248 void wxClientDC::DoGetSize(int *width
, int *height
) const
250 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
252 m_window
->GetClientSize( width
, height
);
260 wxPaintDC::wxPaintDC()
265 #if wxMAC_USE_CORE_GRAPHICS
266 wxPaintDC::wxPaintDC(wxWindow
*window
) :
269 wxPoint origin
= window
->GetClientAreaOrigin() ;
270 wxSize size
= window
->GetClientSize() ;
274 window
->MacWindowToRootWindow( &x
, &y
) ;
275 m_window
->GetClientSize( &m_width
, &m_height
);
276 SetDeviceOrigin( origin
.x
, origin
.y
);
277 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
280 wxPaintDC::wxPaintDC(wxWindow
*window
)
283 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
284 WindowRef windowref
= (WindowRef
) rootwindow
->MacGetWindowRef() ;
285 wxPoint origin
= window
->GetClientAreaOrigin() ;
286 wxSize size
= window
->GetClientSize() ;
290 window
->MacWindowToRootWindow( &x
, &y
) ;
291 m_macPort
= UMAGetWindowPort( windowref
) ;
294 #if wxMAC_USE_CORE_GRAPHICS
295 if ( window
->MacGetCGContextRef() )
297 m_graphicContext
= new wxMacCGContext( (CGContextRef
) window
->MacGetCGContextRef() ) ;
298 m_graphicContext
->SetPen( m_pen
) ;
299 m_graphicContext
->SetBrush( m_brush
) ;
300 SetClippingRegion( 0 , 0 , size
.x
, size
.y
) ;
301 SetBackground(MacGetBackgroundBrush(window
));
305 wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ;
306 m_graphicContext
= NULL
;
308 // there is no out-of-order drawing on OSX
310 m_macLocalOrigin
.x
= x
;
311 m_macLocalOrigin
.y
= y
;
312 SetRectRgn( (RgnHandle
) m_macBoundaryClipRgn
, origin
.x
, origin
.y
, origin
.x
+ size
.x
, origin
.y
+ size
.y
) ;
313 SectRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) window
->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle
) m_macBoundaryClipRgn
) ;
314 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, -origin
.x
, -origin
.y
) ;
315 SectRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) window
->GetUpdateRegion().GetWXHRGN() , (RgnHandle
) m_macBoundaryClipRgn
) ;
316 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, m_macLocalOrigin
.x
, m_macLocalOrigin
.y
) ;
317 CopyRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) m_macCurrentClipRgn
) ;
318 SetBackground(MacGetBackgroundBrush(window
));
321 SetFont( window
->GetFont() ) ;
325 wxPaintDC::~wxPaintDC()
329 #if !wxMAC_USE_CORE_GRAPHICS
330 void wxPaintDC::DoGetSize(int *width
, int *height
) const
332 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
334 m_window
->GetClientSize( width
, height
);