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/rawbmp.h"
28 #include "wx/mac/private.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
39 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
40 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxWindowDC
)
46 #include "wx/mac/uma.h"
47 #include "wx/notebook.h"
48 #include "wx/tabctrl.h"
51 static wxBrush
MacGetBackgroundBrush( wxWindow
* window
)
53 wxBrush bkdBrush
= window
->MacGetBackgroundBrush() ;
55 #if !TARGET_API_MAC_OSX
56 // transparency cannot be handled by the OS when not using composited windows
57 wxWindow
* parent
= window
->GetParent() ;
59 // if we have some 'pseudo' transparency
60 if ( ! bkdBrush
.Ok() || bkdBrush
.GetStyle() == wxTRANSPARENT
|| window
->GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
) )
62 // walk up until we find something
63 while ( parent
!= NULL
)
65 if ( parent
->GetBackgroundColour() != wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
) )
67 // if we have any other colours in the hierarchy
68 bkdBrush
.SetColour( parent
->GetBackgroundColour() ) ;
72 if ( parent
->IsKindOf( CLASSINFO(wxTopLevelWindow
) ) )
74 bkdBrush
= parent
->MacGetBackgroundBrush() ;
78 if ( parent
->IsKindOf( CLASSINFO( wxNotebook
) )
80 || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) )
81 #endif // wxUSE_TAB_DIALOG
84 Rect extent
= { 0 , 0 , 0 , 0 } ;
87 wxSize size
= parent
->GetSize() ;
88 parent
->MacClientToRootWindow( &x
, &y
) ;
92 extent
.right
= x
+ size
.x
;
93 extent
.bottom
= y
+ size
.y
;
94 bkdBrush
.MacSetThemeBackground( kThemeBackgroundTabPane
, (WXRECTPTR
) &extent
) ;
98 parent
= parent
->GetParent() ;
102 if ( !bkdBrush
.Ok() || bkdBrush
.GetStyle() == wxTRANSPARENT
)
104 // if we did not find something, use a default
105 bkdBrush
.MacSetTheme( kThemeBrushDialogBackgroundActive
) ;
112 wxWindowDC::wxWindowDC()
117 wxWindowDC::wxWindowDC(wxWindow
*window
)
120 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
126 #if wxMAC_USE_CORE_GRAPHICS
127 m_window
->GetSize( &m_width
, &m_height
);
128 CGContextRef cg
= (CGContextRef
) window
->MacGetCGContextRef();
132 SetGraphicsContext( wxGraphicsContext::Create( window
) ) ;
136 CGContextSaveGState( cg
);
138 // make sure the context is having its origin at the wx-window coordinates of the
139 // view (read at the top of window.cpp about the differences)
140 if ( window
->MacGetLeftBorderSize() != 0 || window
->MacGetTopBorderSize() != 0 )
141 CGContextTranslateCTM( cg
, -window
->MacGetLeftBorderSize() , -window
->MacGetTopBorderSize() );
143 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg
) );
145 m_graphicContext
->SetPen( m_pen
) ;
146 m_graphicContext
->SetBrush( m_brush
) ;
147 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
151 window
->MacWindowToRootWindow( &x
, &y
) ;
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
);
190 wxBitmap
wxWindowDC::DoGetAsBitmap(const wxRect
*subrect
) const
192 ControlRef handle
= (ControlRef
) m_window
->GetHandle();
198 CGContextRef context
;
203 HIViewCreateOffscreenImage( handle
, 0, &rect
, &image
);
206 int width
= subrect
!= NULL
? subrect
->width
: rect
.size
.width
;
207 int height
= subrect
!= NULL
? subrect
->height
: rect
.size
.height
;
209 bytesPerRow
= ( ( width
* 8 * 4 + 7 ) / 8 );
211 data
= calloc( 1, bytesPerRow
* height
);
212 context
= CGBitmapContextCreate( data
, width
, height
, 8, bytesPerRow
, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst
);
215 rect
= CGRectOffset( rect
, -subrect
->x
, -subrect
->y
) ;
216 CGContextDrawImage( context
, rect
, image
);
218 unsigned char* buffer
= (unsigned char*) data
;
219 wxBitmap bmp
= wxBitmap(width
, height
, 32);
220 wxAlphaPixelData
pixData(bmp
, wxPoint(0,0), wxSize(width
, height
));
223 wxAlphaPixelData::Iterator
p(pixData
);
224 for (int y
=0; y
<height
; y
++) {
225 wxAlphaPixelData::Iterator rowStart
= p
;
226 for (int x
=0; x
<width
; x
++) {
227 unsigned char a
= buffer
[3];
228 p
.Red() = a
; buffer
++;
229 p
.Green() = a
; buffer
++;
230 p
.Blue() = a
; buffer
++;
231 p
.Alpha() = a
; buffer
++;
235 p
.OffsetY(pixData
, 1);
245 wxClientDC::wxClientDC()
250 #if wxMAC_USE_CORE_GRAPHICS
251 wxClientDC::wxClientDC(wxWindow
*window
) :
254 wxPoint origin
= window
->GetClientAreaOrigin() ;
255 m_window
->GetClientSize( &m_width
, &m_height
);
256 SetDeviceOrigin( origin
.x
, origin
.y
);
257 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
260 wxClientDC::wxClientDC(wxWindow
*window
)
263 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
267 WindowRef windowref
= (WindowRef
) rootwindow
->MacGetWindowRef() ;
268 wxPoint origin
= window
->GetClientAreaOrigin() ;
269 wxSize size
= window
->GetClientSize() ;
273 window
->MacWindowToRootWindow( &x
, &y
) ;
274 m_macPort
= UMAGetWindowPort( windowref
) ;
277 m_macLocalOrigin
.x
= x
;
278 m_macLocalOrigin
.y
= y
;
279 SetRectRgn( (RgnHandle
) m_macBoundaryClipRgn
, origin
.x
, origin
.y
, origin
.x
+ size
.x
, origin
.y
+ size
.y
) ;
280 SectRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) window
->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle
) m_macBoundaryClipRgn
) ;
281 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, -origin
.x
, -origin
.y
) ;
282 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, m_macLocalOrigin
.x
, m_macLocalOrigin
.y
) ;
283 CopyRgn( (RgnHandle
) m_macBoundaryClipRgn
,(RgnHandle
) m_macCurrentClipRgn
) ;
285 SetBackground(MacGetBackgroundBrush(window
));
286 SetFont( window
->GetFont() ) ;
290 wxClientDC::~wxClientDC()
294 #if !wxMAC_USE_CORE_GRAPHICS
295 void wxClientDC::DoGetSize(int *width
, int *height
) const
297 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
299 m_window
->GetClientSize( width
, height
);
307 wxPaintDC::wxPaintDC()
312 #if wxMAC_USE_CORE_GRAPHICS
313 wxPaintDC::wxPaintDC(wxWindow
*window
) :
316 wxPoint origin
= window
->GetClientAreaOrigin() ;
317 m_window
->GetClientSize( &m_width
, &m_height
);
318 SetDeviceOrigin( origin
.x
, origin
.y
);
319 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
322 wxPaintDC::wxPaintDC(wxWindow
*window
)
325 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
326 WindowRef windowref
= (WindowRef
) rootwindow
->MacGetWindowRef() ;
327 wxPoint origin
= window
->GetClientAreaOrigin() ;
328 wxSize size
= window
->GetClientSize() ;
332 window
->MacWindowToRootWindow( &x
, &y
) ;
333 m_macPort
= UMAGetWindowPort( windowref
) ;
336 #if wxMAC_USE_CORE_GRAPHICS
337 if ( window
->MacGetCGContextRef() )
339 m_graphicContext
= new wxMacCGContext( (CGContextRef
) window
->MacGetCGContextRef() ) ;
340 m_graphicContext
->SetPen( m_pen
) ;
341 m_graphicContext
->SetBrush( m_brush
) ;
342 SetClippingRegion( 0 , 0 , size
.x
, size
.y
) ;
343 SetBackground(MacGetBackgroundBrush(window
));
347 wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ;
348 m_graphicContext
= NULL
;
350 // there is no out-of-order drawing on OSX
352 m_macLocalOrigin
.x
= x
;
353 m_macLocalOrigin
.y
= y
;
354 SetRectRgn( (RgnHandle
) m_macBoundaryClipRgn
, origin
.x
, origin
.y
, origin
.x
+ size
.x
, origin
.y
+ size
.y
) ;
355 SectRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) window
->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle
) m_macBoundaryClipRgn
) ;
356 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, -origin
.x
, -origin
.y
) ;
357 SectRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) window
->GetUpdateRegion().GetWXHRGN() , (RgnHandle
) m_macBoundaryClipRgn
) ;
358 OffsetRgn( (RgnHandle
) m_macBoundaryClipRgn
, m_macLocalOrigin
.x
, m_macLocalOrigin
.y
) ;
359 CopyRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) m_macCurrentClipRgn
) ;
360 SetBackground(MacGetBackgroundBrush(window
));
363 SetFont( window
->GetFont() ) ;
367 wxPaintDC::~wxPaintDC()
371 #if !wxMAC_USE_CORE_GRAPHICS
372 void wxPaintDC::DoGetSize(int *width
, int *height
) const
374 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
376 m_window
->GetClientSize( width
, height
);