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"
52 wxWindowDC::wxWindowDC()
58 wxWindowDC::wxWindowDC(wxWindow
*window
)
61 wxTopLevelWindowMac
* rootwindow
= window
->MacGetTopLevelWindow() ;
67 m_window
->GetSize( &m_width
, &m_height
);
68 CGContextRef cg
= (CGContextRef
) window
->MacGetCGContextRef();
72 SetGraphicsContext( wxGraphicsContext::Create( window
) ) ;
76 CGContextSaveGState( cg
);
78 // make sure the context is having its origin at the wx-window coordinates of the
79 // view (read at the top of window.cpp about the differences)
80 if ( window
->MacGetLeftBorderSize() != 0 || window
->MacGetTopBorderSize() != 0 )
81 CGContextTranslateCTM( cg
, -window
->MacGetLeftBorderSize() , -window
->MacGetTopBorderSize() );
83 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg
) );
85 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
87 SetBackground(wxBrush(window
->GetBackgroundColour(),wxSOLID
));
89 SetFont( window
->GetFont() ) ;
92 wxWindowDC::~wxWindowDC()
96 // this must not necessarily be the current context, we must restore the state of the
97 // cg we started with above (before the CGContextTranslateCTM call)
98 CGContextRef cg
= (CGContextRef
) m_window
->MacGetCGContextRef();
99 CGContextRestoreGState(cg
);
103 void wxWindowDC::DoGetSize( int* width
, int* height
) const
111 wxBitmap
wxWindowDC::DoGetAsBitmap(const wxRect
*subrect
) const
113 // wxScreenDC is derived from wxWindowDC, so a screen dc will
114 // call this method when a Blit is performed with it as a source.
118 ControlRef handle
= (ControlRef
) m_window
->GetHandle();
124 CGContextRef context
;
129 HIViewCreateOffscreenImage( handle
, 0, &rect
, &image
);
132 int width
= subrect
!= NULL
? subrect
->width
: (int)rect
.size
.width
;
133 int height
= subrect
!= NULL
? subrect
->height
: (int)rect
.size
.height
;
135 bytesPerRow
= ( ( width
* 8 * 4 + 7 ) / 8 );
137 data
= calloc( 1, bytesPerRow
* height
);
138 context
= CGBitmapContextCreate( data
, width
, height
, 8, bytesPerRow
, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst
);
141 rect
= CGRectOffset( rect
, -subrect
->x
, -subrect
->y
) ;
142 CGContextDrawImage( context
, rect
, image
);
144 unsigned char* buffer
= (unsigned char*) data
;
145 wxBitmap bmp
= wxBitmap(width
, height
, 32);
146 wxAlphaPixelData
pixData(bmp
, wxPoint(0,0), wxSize(width
, height
));
148 wxAlphaPixelData::Iterator
p(pixData
);
149 for (int y
=0; y
<height
; y
++) {
150 wxAlphaPixelData::Iterator rowStart
= p
;
151 for (int x
=0; x
<width
; x
++) {
152 unsigned char a
= buffer
[3];
153 p
.Red() = a
; buffer
++;
154 p
.Green() = a
; buffer
++;
155 p
.Blue() = a
; buffer
++;
156 p
.Alpha() = a
; buffer
++;
160 p
.OffsetY(pixData
, 1);
170 wxClientDC::wxClientDC()
175 wxClientDC::wxClientDC(wxWindow
*window
) :
178 wxCHECK_RET( window
, _T("invalid window in wxClientDC") );
179 wxPoint origin
= window
->GetClientAreaOrigin() ;
180 m_window
->GetClientSize( &m_width
, &m_height
);
181 SetDeviceOrigin( origin
.x
, origin
.y
);
182 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
185 wxClientDC::~wxClientDC()
193 wxPaintDC::wxPaintDC()
198 wxPaintDC::wxPaintDC(wxWindow
*window
) :
201 wxPoint origin
= window
->GetClientAreaOrigin() ;
202 m_window
->GetClientSize( &m_width
, &m_height
);
203 SetDeviceOrigin( origin
.x
, origin
.y
);
204 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
207 wxPaintDC::~wxPaintDC()