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/graphics.h"
27 #include "wx/mac/private.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
38 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
39 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxWindowDC
)
45 #include "wx/mac/uma.h"
46 #include "wx/notebook.h"
47 #include "wx/tabctrl.h"
50 static wxBrush
MacGetBackgroundBrush( wxWindow
* window
)
52 wxBrush bkdBrush
= window
->MacGetBackgroundBrush() ;
54 #if !TARGET_API_MAC_OSX
55 // transparency cannot be handled by the OS when not using composited windows
56 wxWindow
* parent
= window
->GetParent() ;
58 // if we have some 'pseudo' transparency
59 if ( ! bkdBrush
.Ok() || bkdBrush
.GetStyle() == wxTRANSPARENT
|| window
->GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
) )
61 // walk up until we find something
62 while ( parent
!= NULL
)
64 if ( parent
->GetBackgroundColour() != wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
) )
66 // if we have any other colours in the hierarchy
67 bkdBrush
.SetColour( parent
->GetBackgroundColour() ) ;
71 if ( parent
->IsKindOf( CLASSINFO(wxTopLevelWindow
) ) )
73 bkdBrush
= parent
->MacGetBackgroundBrush() ;
77 if ( parent
->IsKindOf( CLASSINFO( wxNotebook
) )
79 || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) )
80 #endif // wxUSE_TAB_DIALOG
83 Rect extent
= { 0 , 0 , 0 , 0 } ;
86 wxSize size
= parent
->GetSize() ;
87 parent
->MacClientToRootWindow( &x
, &y
) ;
91 extent
.right
= x
+ size
.x
;
92 extent
.bottom
= y
+ size
.y
;
93 bkdBrush
.MacSetThemeBackground( kThemeBackgroundTabPane
, (WXRECTPTR
) &extent
) ;
97 parent
= parent
->GetParent() ;
101 if ( !bkdBrush
.Ok() || bkdBrush
.GetStyle() == wxTRANSPARENT
)
103 // if we did not find something, use a default
104 bkdBrush
.MacSetTheme( kThemeBrushDialogBackgroundActive
) ;
111 wxWindowDC::wxWindowDC()
116 wxWindowDC::wxWindowDC(wxWindow
*window
)
119 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
125 window
->MacWindowToRootWindow( &x
, &y
) ;
128 #if wxMAC_USE_CORE_GRAPHICS
129 m_window
->GetSize( &m_width
, &m_height
);
130 CGContextRef cg
= (CGContextRef
) window
->MacGetCGContextRef();
134 SetGraphicsContext( wxGraphicsContext::Create( window
) ) ;
135 SetDeviceOrigin( x
, y
);
139 CGContextSaveGState( cg
);
141 // make sure the context is having its origin at the wx-window coordinates of the
142 // view (read at the top of window.cpp about the differences)
143 if ( window
->MacGetLeftBorderSize() != 0 || window
->MacGetTopBorderSize() != 0 )
144 CGContextTranslateCTM( cg
, -window
->MacGetLeftBorderSize() , -window
->MacGetTopBorderSize() );
146 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg
) );
148 m_graphicContext
->SetPen( m_pen
) ;
149 m_graphicContext
->SetBrush( m_brush
) ;
150 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
152 m_macLocalOrigin
.x
= x
;
153 m_macLocalOrigin
.y
= y
;
154 m_macPort
= UMAGetWindowPort( (WindowRef
) rootwindow
->MacGetWindowRef() ) ;
156 CopyRgn( (RgnHandle
) window
->MacGetVisibleRegion(true).GetWXHRGN() , (RgnHandle
) m_macBoundaryClipRgn
) ;
157 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, m_macLocalOrigin
.x
, m_macLocalOrigin
.y
) ;
158 CopyRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) m_macCurrentClipRgn
) ;
160 SetBackground(MacGetBackgroundBrush(window
));
162 SetFont( window
->GetFont() ) ;
165 wxWindowDC::~wxWindowDC()
167 #if wxMAC_USE_CORE_GRAPHICS
168 if ( m_release
&& m_graphicContext
)
170 CGContextRef cg
= (CGContextRef
) m_window
->MacGetCGContextRef();
171 CGContextRestoreGState(cg
);
172 //CGContextRef cg = (CGContextRef) m_graphicContext->GetNativeContext() ;
177 void wxWindowDC::DoGetSize( int* width
, int* height
) const
179 #if wxMAC_USE_CORE_GRAPHICS
185 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
186 m_window
->GetSize(width
, height
);
194 wxClientDC::wxClientDC()
199 #if wxMAC_USE_CORE_GRAPHICS
200 wxClientDC::wxClientDC(wxWindow
*window
) :
203 wxPoint origin
= window
->GetClientAreaOrigin() ;
204 wxSize size
= window
->GetClientSize() ;
208 window
->MacWindowToRootWindow( &x
, &y
) ;
209 m_window
->GetClientSize( &m_width
, &m_height
);
210 SetDeviceOrigin( origin
.x
, origin
.y
);
211 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
214 wxClientDC::wxClientDC(wxWindow
*window
)
217 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
221 WindowRef windowref
= (WindowRef
) rootwindow
->MacGetWindowRef() ;
222 wxPoint origin
= window
->GetClientAreaOrigin() ;
223 wxSize size
= window
->GetClientSize() ;
227 window
->MacWindowToRootWindow( &x
, &y
) ;
228 m_macPort
= UMAGetWindowPort( windowref
) ;
231 m_macLocalOrigin
.x
= x
;
232 m_macLocalOrigin
.y
= y
;
233 SetRectRgn( (RgnHandle
) m_macBoundaryClipRgn
, origin
.x
, origin
.y
, origin
.x
+ size
.x
, origin
.y
+ size
.y
) ;
234 SectRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) window
->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle
) m_macBoundaryClipRgn
) ;
235 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, -origin
.x
, -origin
.y
) ;
236 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, m_macLocalOrigin
.x
, m_macLocalOrigin
.y
) ;
237 CopyRgn( (RgnHandle
) m_macBoundaryClipRgn
,(RgnHandle
) m_macCurrentClipRgn
) ;
239 SetBackground(MacGetBackgroundBrush(window
));
240 SetFont( window
->GetFont() ) ;
244 wxClientDC::~wxClientDC()
248 #if !wxMAC_USE_CORE_GRAPHICS
249 void wxClientDC::DoGetSize(int *width
, int *height
) const
251 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
253 m_window
->GetClientSize( width
, height
);
261 wxPaintDC::wxPaintDC()
266 #if wxMAC_USE_CORE_GRAPHICS
267 wxPaintDC::wxPaintDC(wxWindow
*window
) :
270 wxPoint origin
= window
->GetClientAreaOrigin() ;
271 wxSize size
= window
->GetClientSize() ;
275 window
->MacWindowToRootWindow( &x
, &y
) ;
276 m_window
->GetClientSize( &m_width
, &m_height
);
277 SetDeviceOrigin( origin
.x
, origin
.y
);
278 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
281 wxPaintDC::wxPaintDC(wxWindow
*window
)
284 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
285 WindowRef windowref
= (WindowRef
) rootwindow
->MacGetWindowRef() ;
286 wxPoint origin
= window
->GetClientAreaOrigin() ;
287 wxSize size
= window
->GetClientSize() ;
291 window
->MacWindowToRootWindow( &x
, &y
) ;
292 m_macPort
= UMAGetWindowPort( windowref
) ;
295 #if wxMAC_USE_CORE_GRAPHICS
296 if ( window
->MacGetCGContextRef() )
298 m_graphicContext
= new wxMacCGContext( (CGContextRef
) window
->MacGetCGContextRef() ) ;
299 m_graphicContext
->SetPen( m_pen
) ;
300 m_graphicContext
->SetBrush( m_brush
) ;
301 SetClippingRegion( 0 , 0 , size
.x
, size
.y
) ;
302 SetBackground(MacGetBackgroundBrush(window
));
306 wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ;
307 m_graphicContext
= NULL
;
309 // there is no out-of-order drawing on OSX
311 m_macLocalOrigin
.x
= x
;
312 m_macLocalOrigin
.y
= y
;
313 SetRectRgn( (RgnHandle
) m_macBoundaryClipRgn
, origin
.x
, origin
.y
, origin
.x
+ size
.x
, origin
.y
+ size
.y
) ;
314 SectRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) window
->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle
) m_macBoundaryClipRgn
) ;
315 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, -origin
.x
, -origin
.y
) ;
316 SectRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) window
->GetUpdateRegion().GetWXHRGN() , (RgnHandle
) m_macBoundaryClipRgn
) ;
317 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, m_macLocalOrigin
.x
, m_macLocalOrigin
.y
) ;
318 CopyRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) m_macCurrentClipRgn
) ;
319 SetBackground(MacGetBackgroundBrush(window
));
322 SetFont( window
->GetFont() ) ;
326 wxPaintDC::~wxPaintDC()
330 #if !wxMAC_USE_CORE_GRAPHICS
331 void wxPaintDC::DoGetSize(int *width
, int *height
) const
333 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
335 m_window
->GetClientSize( width
, height
);