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