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() ;
57 wxWindowDC::wxWindowDC()
63 wxWindowDC::wxWindowDC(wxWindow
*window
)
66 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
72 m_window
->GetSize( &m_width
, &m_height
);
73 CGContextRef cg
= (CGContextRef
) window
->MacGetCGContextRef();
77 SetGraphicsContext( wxGraphicsContext::Create( window
) ) ;
81 CGContextSaveGState( cg
);
83 // make sure the context is having its origin at the wx-window coordinates of the
84 // view (read at the top of window.cpp about the differences)
85 if ( window
->MacGetLeftBorderSize() != 0 || window
->MacGetTopBorderSize() != 0 )
86 CGContextTranslateCTM( cg
, -window
->MacGetLeftBorderSize() , -window
->MacGetTopBorderSize() );
88 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg
) );
90 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
92 SetBackground(MacGetBackgroundBrush(window
));
94 SetFont( window
->GetFont() ) ;
97 wxWindowDC::~wxWindowDC()
101 // this must not necessarily be the current context, we must restore the state of the
102 // cg we started with above (before the CGContextTranslateCTM call)
103 CGContextRef cg
= (CGContextRef
) m_window
->MacGetCGContextRef();
104 CGContextRestoreGState(cg
);
108 void wxWindowDC::DoGetSize( int* width
, int* height
) const
116 wxBitmap
wxWindowDC::DoGetAsBitmap(const wxRect
*subrect
) const
118 // wxScreenDC is derived from wxWindowDC, so a screen dc will
119 // call this method when a Blit is performed with it as a source.
123 ControlRef handle
= (ControlRef
) m_window
->GetHandle();
129 CGContextRef context
;
134 HIViewCreateOffscreenImage( handle
, 0, &rect
, &image
);
137 int width
= subrect
!= NULL
? subrect
->width
: (int)rect
.size
.width
;
138 int height
= subrect
!= NULL
? subrect
->height
: (int)rect
.size
.height
;
140 bytesPerRow
= ( ( width
* 8 * 4 + 7 ) / 8 );
142 data
= calloc( 1, bytesPerRow
* height
);
143 context
= CGBitmapContextCreate( data
, width
, height
, 8, bytesPerRow
, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst
);
146 rect
= CGRectOffset( rect
, -subrect
->x
, -subrect
->y
) ;
147 CGContextDrawImage( context
, rect
, image
);
149 unsigned char* buffer
= (unsigned char*) data
;
150 wxBitmap bmp
= wxBitmap(width
, height
, 32);
151 wxAlphaPixelData
pixData(bmp
, wxPoint(0,0), wxSize(width
, height
));
153 wxAlphaPixelData::Iterator
p(pixData
);
154 for (int y
=0; y
<height
; y
++) {
155 wxAlphaPixelData::Iterator rowStart
= p
;
156 for (int x
=0; x
<width
; x
++) {
157 unsigned char a
= buffer
[3];
158 p
.Red() = a
; buffer
++;
159 p
.Green() = a
; buffer
++;
160 p
.Blue() = a
; buffer
++;
161 p
.Alpha() = a
; buffer
++;
165 p
.OffsetY(pixData
, 1);
175 wxClientDC::wxClientDC()
180 wxClientDC::wxClientDC(wxWindow
*window
) :
183 wxCHECK_RET( window
, _T("invalid window in wxClientDC") );
184 wxPoint origin
= window
->GetClientAreaOrigin() ;
185 m_window
->GetClientSize( &m_width
, &m_height
);
186 SetDeviceOrigin( origin
.x
, origin
.y
);
187 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
190 wxClientDC::~wxClientDC()
198 wxPaintDC::wxPaintDC()
203 wxPaintDC::wxPaintDC(wxWindow
*window
) :
206 wxPoint origin
= window
->GetClientAreaOrigin() ;
207 m_window
->GetClientSize( &m_width
, &m_height
);
208 SetDeviceOrigin( origin
.x
, origin
.y
);
209 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
212 wxPaintDC::~wxPaintDC()