]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcclient.cpp
3fab6f98e2eba9430d4b4f8436e37f505e39ca3e
[wxWidgets.git] / src / mac / carbon / dcclient.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dcclient.cpp
3 // Purpose: wxClientDC class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/dcclient.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #include "wx/window.h"
19 #include "wx/dcmemory.h"
20 #include "wx/settings.h"
21 #include "wx/toplevel.h"
22 #include "wx/math.h"
23 #include "wx/region.h"
24 #endif
25
26 #include "wx/graphics.h"
27 #include "wx/rawbmp.h"
28 #include "wx/mac/private.h"
29
30 //-----------------------------------------------------------------------------
31 // constants
32 //-----------------------------------------------------------------------------
33
34 //-----------------------------------------------------------------------------
35 // wxPaintDC
36 //-----------------------------------------------------------------------------
37
38 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
39 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
40 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
41
42 /*
43 * wxWindowDC
44 */
45
46 #include "wx/mac/uma.h"
47 #include "wx/notebook.h"
48 #include "wx/tabctrl.h"
49
50
51 static wxBrush MacGetBackgroundBrush( wxWindow* window )
52 {
53 wxBrush bkdBrush = window->MacGetBackgroundBrush() ;
54
55 #if !TARGET_API_MAC_OSX
56 // transparency cannot be handled by the OS when not using composited windows
57 wxWindow* parent = window->GetParent() ;
58
59 // if we have some 'pseudo' transparency
60 if ( ! bkdBrush.Ok() || bkdBrush.GetStyle() == wxTRANSPARENT || window->GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
61 {
62 // walk up until we find something
63 while ( parent != NULL )
64 {
65 if ( parent->GetBackgroundColour() != wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
66 {
67 // if we have any other colours in the hierarchy
68 bkdBrush.SetColour( parent->GetBackgroundColour() ) ;
69 break ;
70 }
71
72 if ( parent->IsKindOf( CLASSINFO(wxTopLevelWindow) ) )
73 {
74 bkdBrush = parent->MacGetBackgroundBrush() ;
75 break ;
76 }
77
78 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) )
79 #if wxUSE_TAB_DIALOG
80 || parent->IsKindOf( CLASSINFO( wxTabCtrl ) )
81 #endif // wxUSE_TAB_DIALOG
82 )
83 {
84 Rect extent = { 0 , 0 , 0 , 0 } ;
85 int x , y ;
86 x = y = 0 ;
87 wxSize size = parent->GetSize() ;
88 parent->MacClientToRootWindow( &x , &y ) ;
89 extent.left = x ;
90 extent.top = y ;
91 extent.top-- ;
92 extent.right = x + size.x ;
93 extent.bottom = y + size.y ;
94 bkdBrush.MacSetThemeBackground( kThemeBackgroundTabPane , (WXRECTPTR) &extent ) ;
95 break ;
96 }
97
98 parent = parent->GetParent() ;
99 }
100 }
101
102 if ( !bkdBrush.Ok() || bkdBrush.GetStyle() == wxTRANSPARENT )
103 {
104 // if we did not find something, use a default
105 bkdBrush.MacSetTheme( kThemeBrushDialogBackgroundActive ) ;
106 }
107 #endif
108
109 return bkdBrush ;
110 }
111
112 wxWindowDC::wxWindowDC()
113 {
114 m_window = NULL ;
115 }
116
117 wxWindowDC::wxWindowDC(wxWindow *window)
118 {
119 m_window = window ;
120 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
121 if (!rootwindow)
122 return;
123
124 m_ok = true ;
125
126 #if wxMAC_USE_CORE_GRAPHICS
127 m_window->GetSize( &m_width , &m_height);
128 CGContextRef cg = (CGContextRef) window->MacGetCGContextRef();
129 m_release = false;
130 if ( cg == NULL )
131 {
132 SetGraphicsContext( wxGraphicsContext::Create( window ) ) ;
133 }
134 else
135 {
136 CGContextSaveGState( cg );
137 m_release = true ;
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() );
142
143 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg ) );
144 }
145 m_graphicContext->SetPen( m_pen ) ;
146 m_graphicContext->SetBrush( m_brush ) ;
147 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
148 #else
149 int x , y ;
150 x = y = 0 ;
151 window->MacWindowToRootWindow( &x , &y ) ;
152 m_macLocalOrigin.x = x ;
153 m_macLocalOrigin.y = y ;
154 m_macPort = UMAGetWindowPort( (WindowRef) rootwindow->MacGetWindowRef() ) ;
155
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 ) ;
159 #endif
160 SetBackground(MacGetBackgroundBrush(window));
161
162 SetFont( window->GetFont() ) ;
163 }
164
165 wxWindowDC::~wxWindowDC()
166 {
167 #if wxMAC_USE_CORE_GRAPHICS
168 if ( m_release )
169 {
170 // this must not necessarily be the current context, we must restore the state of the
171 // cg we started with above (before the CGContextTranslateCTM call)
172 CGContextRef cg = (CGContextRef) m_window->MacGetCGContextRef();
173 CGContextRestoreGState(cg);
174 }
175 #endif
176 }
177
178 void wxWindowDC::DoGetSize( int* width, int* height ) const
179 {
180 #if wxMAC_USE_CORE_GRAPHICS
181 if ( width )
182 *width = m_width;
183 if ( height )
184 *height = m_height;
185 #else
186 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
187 m_window->GetSize(width, height);
188 #endif
189 }
190
191 wxBitmap wxWindowDC::DoGetAsBitmap(const wxRect *subrect) const
192 {
193 ControlRef handle = (ControlRef) m_window->GetHandle();
194 if ( !handle )
195 return wxNullBitmap;
196
197 HIRect rect;
198 CGImageRef image;
199 CGContextRef context;
200 void* data;
201
202 size_t bytesPerRow;
203
204 HIViewCreateOffscreenImage( handle, 0, &rect, &image);
205
206
207 int width = subrect != NULL ? subrect->width : (int)rect.size.width;
208 int height = subrect != NULL ? subrect->height : (int)rect.size.height ;
209
210 bytesPerRow = ( ( width * 8 * 4 + 7 ) / 8 );
211
212 data = calloc( 1, bytesPerRow * height );
213 context = CGBitmapContextCreate( data, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst );
214
215 if ( subrect )
216 rect = CGRectOffset( rect, -subrect->x, -subrect->y ) ;
217 CGContextDrawImage( context, rect, image );
218
219 unsigned char* buffer = (unsigned char*) data;
220 wxBitmap bmp = wxBitmap(width, height, 32);
221 wxAlphaPixelData pixData(bmp, wxPoint(0,0), wxSize(width, height));
222
223 pixData.UseAlpha();
224 wxAlphaPixelData::Iterator p(pixData);
225 for (int y=0; y<height; y++) {
226 wxAlphaPixelData::Iterator rowStart = p;
227 for (int x=0; x<width; x++) {
228 unsigned char a = buffer[3];
229 p.Red() = a; buffer++;
230 p.Green() = a; buffer++;
231 p.Blue() = a; buffer++;
232 p.Alpha() = a; buffer++;
233 ++p;
234 }
235 p = rowStart;
236 p.OffsetY(pixData, 1);
237 }
238
239 return bmp;
240 }
241
242 /*
243 * wxClientDC
244 */
245
246 wxClientDC::wxClientDC()
247 {
248 m_window = NULL ;
249 }
250
251 #if wxMAC_USE_CORE_GRAPHICS
252 wxClientDC::wxClientDC(wxWindow *window) :
253 wxWindowDC( window )
254 {
255 wxPoint origin = window->GetClientAreaOrigin() ;
256 m_window->GetClientSize( &m_width , &m_height);
257 SetDeviceOrigin( origin.x, origin.y );
258 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
259 }
260 #else
261 wxClientDC::wxClientDC(wxWindow *window)
262 {
263 m_window = window ;
264 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
265 if (!rootwindow)
266 return;
267
268 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
269 wxPoint origin = window->GetClientAreaOrigin() ;
270 wxSize size = window->GetClientSize() ;
271 int x , y ;
272 x = origin.x ;
273 y = origin.y ;
274 window->MacWindowToRootWindow( &x , &y ) ;
275 m_macPort = UMAGetWindowPort( windowref ) ;
276 m_ok = true ;
277
278 m_macLocalOrigin.x = x ;
279 m_macLocalOrigin.y = y ;
280 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
281 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
282 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
283 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
284 CopyRgn( (RgnHandle) m_macBoundaryClipRgn ,(RgnHandle) m_macCurrentClipRgn ) ;
285
286 SetBackground(MacGetBackgroundBrush(window));
287 SetFont( window->GetFont() ) ;
288 }
289 #endif
290
291 wxClientDC::~wxClientDC()
292 {
293 }
294
295 #if !wxMAC_USE_CORE_GRAPHICS
296 void wxClientDC::DoGetSize(int *width, int *height) const
297 {
298 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
299
300 m_window->GetClientSize( width, height );
301 }
302 #endif
303
304 /*
305 * wxPaintDC
306 */
307
308 wxPaintDC::wxPaintDC()
309 {
310 m_window = NULL ;
311 }
312
313 #if wxMAC_USE_CORE_GRAPHICS
314 wxPaintDC::wxPaintDC(wxWindow *window) :
315 wxWindowDC( window )
316 {
317 wxPoint origin = window->GetClientAreaOrigin() ;
318 m_window->GetClientSize( &m_width , &m_height);
319 SetDeviceOrigin( origin.x, origin.y );
320 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
321 }
322 #else
323 wxPaintDC::wxPaintDC(wxWindow *window)
324 {
325 m_window = window ;
326 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
327 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
328 wxPoint origin = window->GetClientAreaOrigin() ;
329 wxSize size = window->GetClientSize() ;
330 int x , y ;
331 x = origin.x ;
332 y = origin.y ;
333 window->MacWindowToRootWindow( &x , &y ) ;
334 m_macPort = UMAGetWindowPort( windowref ) ;
335 m_ok = true ;
336
337 #if wxMAC_USE_CORE_GRAPHICS
338 if ( window->MacGetCGContextRef() )
339 {
340 m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ;
341 m_graphicContext->SetPen( m_pen ) ;
342 m_graphicContext->SetBrush( m_brush ) ;
343 SetClippingRegion( 0 , 0 , size.x , size.y ) ;
344 SetBackground(MacGetBackgroundBrush(window));
345 }
346 else
347 {
348 wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ;
349 m_graphicContext = NULL ;
350 }
351 // there is no out-of-order drawing on OSX
352 #else
353 m_macLocalOrigin.x = x ;
354 m_macLocalOrigin.y = y ;
355 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
356 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
357 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
358 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->GetUpdateRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
359 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
360 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
361 SetBackground(MacGetBackgroundBrush(window));
362 #endif
363
364 SetFont( window->GetFont() ) ;
365 }
366 #endif
367
368 wxPaintDC::~wxPaintDC()
369 {
370 }
371
372 #if !wxMAC_USE_CORE_GRAPHICS
373 void wxPaintDC::DoGetSize(int *width, int *height) const
374 {
375 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
376
377 m_window->GetClientSize( width, height );
378 }
379 #endif