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.
121 ControlRef handle
= (ControlRef
) m_window
->GetHandle();
127 CGContextRef context
;
132 HIViewCreateOffscreenImage( handle
, 0, &rect
, &image
);
135 int width
= subrect
!= NULL
? subrect
->width
: (int)rect
.size
.width
;
136 int height
= subrect
!= NULL
? subrect
->height
: (int)rect
.size
.height
;
138 bytesPerRow
= ( ( width
* 8 * 4 + 7 ) / 8 );
140 data
= calloc( 1, bytesPerRow
* height
);
141 context
= CGBitmapContextCreate( data
, width
, height
, 8, bytesPerRow
, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst
);
144 rect
= CGRectOffset( rect
, -subrect
->x
, -subrect
->y
) ;
145 CGContextDrawImage( context
, rect
, image
);
147 unsigned char* buffer
= (unsigned char*) data
;
148 wxBitmap bmp
= wxBitmap(width
, height
, 32);
149 wxAlphaPixelData
pixData(bmp
, wxPoint(0,0), wxSize(width
, height
));
151 wxAlphaPixelData::Iterator
p(pixData
);
152 for (int y
=0; y
<height
; y
++) {
153 wxAlphaPixelData::Iterator rowStart
= p
;
154 for (int x
=0; x
<width
; x
++) {
155 unsigned char a
= buffer
[3];
156 p
.Red() = a
; buffer
++;
157 p
.Green() = a
; buffer
++;
158 p
.Blue() = a
; buffer
++;
159 p
.Alpha() = a
; buffer
++;
163 p
.OffsetY(pixData
, 1);
174 wxClientDC::wxClientDC()
179 wxClientDC::wxClientDC(wxWindow
*window
) :
182 wxCHECK_RET( window
, _T("invalid window in wxClientDC") );
183 wxPoint origin
= window
->GetClientAreaOrigin() ;
184 m_window
->GetClientSize( &m_width
, &m_height
);
185 SetDeviceOrigin( origin
.x
, origin
.y
);
186 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
189 wxClientDC::~wxClientDC()
197 wxPaintDC::wxPaintDC()
202 wxPaintDC::wxPaintDC(wxWindow
*window
) :
205 wxPoint origin
= window
->GetClientAreaOrigin() ;
206 m_window
->GetClientSize( &m_width
, &m_height
);
207 SetDeviceOrigin( origin
.x
, origin
.y
);
208 SetClippingRegion( 0 , 0 , m_width
, m_height
) ;
211 wxPaintDC::~wxPaintDC()