1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dcclient.cpp
3 // Purpose: wxClientDCImpl 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 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl
, wxGCDCImpl
)
36 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
)
42 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
, wxWindow
*window
)
47 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
53 m_window
->GetSize( &m_width
, &m_height
);
54 CGContextRef cg
= (CGContextRef
) window
->MacGetCGContextRef();
58 SetGraphicsContext( wxGraphicsContext::Create( window
) ) ;
62 CGContextSaveGState( cg
);
64 // make sure the context is having its origin at the wx-window coordinates of the
65 // view (read at the top of window.cpp about the differences)
66 if ( window
->MacGetLeftBorderSize() != 0 || window
->MacGetTopBorderSize() != 0 )
67 CGContextTranslateCTM( cg
, -window
->MacGetLeftBorderSize() , -window
->MacGetTopBorderSize() );
69 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg
) );
71 DoSetClippingRegion( 0 , 0 , m_width
, m_height
) ;
73 SetBackground(wxBrush(window
->GetBackgroundColour(),wxSOLID
));
75 SetFont( window
->GetFont() ) ;
78 wxWindowDCImpl::~wxWindowDCImpl()
82 // this must not necessarily be the current context, we must restore the state of the
83 // cg we started with above (before the CGContextTranslateCTM call)
84 CGContextRef cg
= (CGContextRef
) m_window
->MacGetCGContextRef();
85 CGContextRestoreGState(cg
);
89 void wxWindowDCImpl::DoGetSize( int* width
, int* height
) const
97 wxBitmap
wxWindowDCImpl::DoGetAsBitmap(const wxRect
*subrect
) const
99 // wxScreenDC is derived from wxWindowDC, so a screen dc will
100 // call this method when a Blit is performed with it as a source.
107 ControlRef handle
= (ControlRef
) m_window
->GetHandle();
113 CGContextRef context
;
118 HIViewCreateOffscreenImage( handle
, 0, &rect
, &image
);
121 int width
= subrect
!= NULL
? subrect
->width
: (int)rect
.size
.width
;
122 int height
= subrect
!= NULL
? subrect
->height
: (int)rect
.size
.height
;
124 bytesPerRow
= ( ( width
* 8 * 4 + 7 ) / 8 );
126 data
= calloc( 1, bytesPerRow
* height
);
127 context
= CGBitmapContextCreate( data
, width
, height
, 8, bytesPerRow
, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst
);
130 rect
= CGRectOffset( rect
, -subrect
->x
, -subrect
->y
) ;
131 CGContextDrawImage( context
, rect
, image
);
133 unsigned char* buffer
= (unsigned char*) data
;
134 wxBitmap bmp
= wxBitmap(width
, height
, 32);
135 wxAlphaPixelData
pixData(bmp
, wxPoint(0,0), wxSize(width
, height
));
137 wxAlphaPixelData::Iterator
p(pixData
);
138 for (int y
=0; y
<height
; y
++) {
139 wxAlphaPixelData::Iterator rowStart
= p
;
140 for (int x
=0; x
<width
; x
++) {
141 unsigned char a
= buffer
[3];
142 p
.Red() = a
; buffer
++;
143 p
.Green() = a
; buffer
++;
144 p
.Blue() = a
; buffer
++;
145 p
.Alpha() = a
; buffer
++;
149 p
.OffsetY(pixData
, 1);
160 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl
, wxWindowDCImpl
)
162 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
)
163 : wxWindowDCImpl( owner
)
167 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
, wxWindow
*window
) :
168 wxWindowDCImpl( owner
, window
)
170 wxCHECK_RET( window
, _T("invalid window in wxClientDCImpl") );
171 wxPoint origin
= window
->GetClientAreaOrigin() ;
172 m_window
->GetClientSize( &m_width
, &m_height
);
173 SetDeviceOrigin( origin
.x
, origin
.y
);
174 DoSetClippingRegion( 0 , 0 , m_width
, m_height
) ;
177 wxClientDCImpl::~wxClientDCImpl()
185 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl
, wxWindowDCImpl
)
187 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
)
188 : wxWindowDCImpl( owner
)
192 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
, wxWindow
*window
) :
193 wxWindowDCImpl( owner
, window
)
195 wxPoint origin
= window
->GetClientAreaOrigin() ;
196 m_window
->GetClientSize( &m_width
, &m_height
);
197 SetDeviceOrigin( origin
.x
, origin
.y
);
198 DoSetClippingRegion( 0 , 0 , m_width
, m_height
) ;
201 wxPaintDCImpl::~wxPaintDCImpl()