]>
Commit | Line | Data |
---|---|---|
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 | #if wxMAC_USE_CORE_GRAPHICS | |
116 | m_release = false; | |
117 | #endif | |
118 | } | |
119 | ||
120 | wxWindowDC::wxWindowDC(wxWindow *window) | |
121 | { | |
122 | m_window = window ; | |
123 | wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ; | |
124 | if (!rootwindow) | |
125 | return; | |
126 | ||
127 | m_ok = true ; | |
128 | ||
129 | #if wxMAC_USE_CORE_GRAPHICS | |
130 | m_window->GetSize( &m_width , &m_height); | |
131 | CGContextRef cg = (CGContextRef) window->MacGetCGContextRef(); | |
132 | m_release = false; | |
133 | if ( cg == NULL ) | |
134 | { | |
135 | SetGraphicsContext( wxGraphicsContext::Create( window ) ) ; | |
136 | } | |
137 | else | |
138 | { | |
139 | CGContextSaveGState( cg ); | |
140 | m_release = true ; | |
141 | // make sure the context is having its origin at the wx-window coordinates of the | |
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 | } | |
148 | SetClippingRegion( 0 , 0 , m_width , m_height ) ; | |
149 | #else | |
150 | int x , y ; | |
151 | x = y = 0 ; | |
152 | window->MacWindowToRootWindow( &x , &y ) ; | |
153 | m_macLocalOrigin.x = x ; | |
154 | m_macLocalOrigin.y = y ; | |
155 | m_macPort = UMAGetWindowPort( (WindowRef) rootwindow->MacGetWindowRef() ) ; | |
156 | ||
157 | CopyRgn( (RgnHandle) window->MacGetVisibleRegion(true).GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ; | |
158 | OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ; | |
159 | CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ; | |
160 | #endif | |
161 | SetBackground(MacGetBackgroundBrush(window)); | |
162 | ||
163 | SetFont( window->GetFont() ) ; | |
164 | } | |
165 | ||
166 | wxWindowDC::~wxWindowDC() | |
167 | { | |
168 | #if wxMAC_USE_CORE_GRAPHICS | |
169 | if ( m_release ) | |
170 | { | |
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) | |
173 | CGContextRef cg = (CGContextRef) m_window->MacGetCGContextRef(); | |
174 | CGContextRestoreGState(cg); | |
175 | } | |
176 | #endif | |
177 | } | |
178 | ||
179 | void wxWindowDC::DoGetSize( int* width, int* height ) const | |
180 | { | |
181 | #if wxMAC_USE_CORE_GRAPHICS | |
182 | if ( width ) | |
183 | *width = m_width; | |
184 | if ( height ) | |
185 | *height = m_height; | |
186 | #else | |
187 | wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") ); | |
188 | m_window->GetSize(width, height); | |
189 | #endif | |
190 | } | |
191 | ||
192 | wxBitmap wxWindowDC::DoGetAsBitmap(const wxRect *subrect) const | |
193 | { | |
194 | ControlRef handle = (ControlRef) m_window->GetHandle(); | |
195 | if ( !handle ) | |
196 | return wxNullBitmap; | |
197 | ||
198 | HIRect rect; | |
199 | CGImageRef image; | |
200 | CGContextRef context; | |
201 | void* data; | |
202 | ||
203 | size_t bytesPerRow; | |
204 | ||
205 | HIViewCreateOffscreenImage( handle, 0, &rect, &image); | |
206 | ||
207 | ||
208 | int width = subrect != NULL ? subrect->width : (int)rect.size.width; | |
209 | int height = subrect != NULL ? subrect->height : (int)rect.size.height ; | |
210 | ||
211 | bytesPerRow = ( ( width * 8 * 4 + 7 ) / 8 ); | |
212 | ||
213 | data = calloc( 1, bytesPerRow * height ); | |
214 | context = CGBitmapContextCreate( data, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst ); | |
215 | ||
216 | if ( subrect ) | |
217 | rect = CGRectOffset( rect, -subrect->x, -subrect->y ) ; | |
218 | CGContextDrawImage( context, rect, image ); | |
219 | ||
220 | unsigned char* buffer = (unsigned char*) data; | |
221 | wxBitmap bmp = wxBitmap(width, height, 32); | |
222 | wxAlphaPixelData pixData(bmp, wxPoint(0,0), wxSize(width, height)); | |
223 | ||
224 | pixData.UseAlpha(); | |
225 | wxAlphaPixelData::Iterator p(pixData); | |
226 | for (int y=0; y<height; y++) { | |
227 | wxAlphaPixelData::Iterator rowStart = p; | |
228 | for (int x=0; x<width; x++) { | |
229 | unsigned char a = buffer[3]; | |
230 | p.Red() = a; buffer++; | |
231 | p.Green() = a; buffer++; | |
232 | p.Blue() = a; buffer++; | |
233 | p.Alpha() = a; buffer++; | |
234 | ++p; | |
235 | } | |
236 | p = rowStart; | |
237 | p.OffsetY(pixData, 1); | |
238 | } | |
239 | ||
240 | return bmp; | |
241 | } | |
242 | ||
243 | /* | |
244 | * wxClientDC | |
245 | */ | |
246 | ||
247 | wxClientDC::wxClientDC() | |
248 | { | |
249 | m_window = NULL ; | |
250 | } | |
251 | ||
252 | #if wxMAC_USE_CORE_GRAPHICS | |
253 | wxClientDC::wxClientDC(wxWindow *window) : | |
254 | wxWindowDC( window ) | |
255 | { | |
256 | wxPoint origin = window->GetClientAreaOrigin() ; | |
257 | m_window->GetClientSize( &m_width , &m_height); | |
258 | SetDeviceOrigin( origin.x, origin.y ); | |
259 | SetClippingRegion( 0 , 0 , m_width , m_height ) ; | |
260 | } | |
261 | #else | |
262 | wxClientDC::wxClientDC(wxWindow *window) | |
263 | { | |
264 | m_window = window ; | |
265 | wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ; | |
266 | if (!rootwindow) | |
267 | return; | |
268 | ||
269 | WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ; | |
270 | wxPoint origin = window->GetClientAreaOrigin() ; | |
271 | wxSize size = window->GetClientSize() ; | |
272 | int x , y ; | |
273 | x = origin.x ; | |
274 | y = origin.y ; | |
275 | window->MacWindowToRootWindow( &x , &y ) ; | |
276 | m_macPort = UMAGetWindowPort( windowref ) ; | |
277 | m_ok = true ; | |
278 | ||
279 | m_macLocalOrigin.x = x ; | |
280 | m_macLocalOrigin.y = y ; | |
281 | SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ; | |
282 | SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ; | |
283 | OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ; | |
284 | OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ; | |
285 | CopyRgn( (RgnHandle) m_macBoundaryClipRgn ,(RgnHandle) m_macCurrentClipRgn ) ; | |
286 | ||
287 | SetBackground(MacGetBackgroundBrush(window)); | |
288 | SetFont( window->GetFont() ) ; | |
289 | } | |
290 | #endif | |
291 | ||
292 | wxClientDC::~wxClientDC() | |
293 | { | |
294 | } | |
295 | ||
296 | #if !wxMAC_USE_CORE_GRAPHICS | |
297 | void wxClientDC::DoGetSize(int *width, int *height) const | |
298 | { | |
299 | wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") ); | |
300 | ||
301 | m_window->GetClientSize( width, height ); | |
302 | } | |
303 | #endif | |
304 | ||
305 | /* | |
306 | * wxPaintDC | |
307 | */ | |
308 | ||
309 | wxPaintDC::wxPaintDC() | |
310 | { | |
311 | m_window = NULL ; | |
312 | } | |
313 | ||
314 | #if wxMAC_USE_CORE_GRAPHICS | |
315 | wxPaintDC::wxPaintDC(wxWindow *window) : | |
316 | wxWindowDC( window ) | |
317 | { | |
318 | wxPoint origin = window->GetClientAreaOrigin() ; | |
319 | m_window->GetClientSize( &m_width , &m_height); | |
320 | SetDeviceOrigin( origin.x, origin.y ); | |
321 | SetClippingRegion( 0 , 0 , m_width , m_height ) ; | |
322 | } | |
323 | #else | |
324 | wxPaintDC::wxPaintDC(wxWindow *window) | |
325 | { | |
326 | m_window = window ; | |
327 | wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ; | |
328 | WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ; | |
329 | wxPoint origin = window->GetClientAreaOrigin() ; | |
330 | wxSize size = window->GetClientSize() ; | |
331 | int x , y ; | |
332 | x = origin.x ; | |
333 | y = origin.y ; | |
334 | window->MacWindowToRootWindow( &x , &y ) ; | |
335 | m_macPort = UMAGetWindowPort( windowref ) ; | |
336 | m_ok = true ; | |
337 | ||
338 | #if wxMAC_USE_CORE_GRAPHICS | |
339 | if ( window->MacGetCGContextRef() ) | |
340 | { | |
341 | m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ; | |
342 | m_graphicContext->SetPen( m_pen ) ; | |
343 | m_graphicContext->SetBrush( m_brush ) ; | |
344 | SetClippingRegion( 0 , 0 , size.x , size.y ) ; | |
345 | SetBackground(MacGetBackgroundBrush(window)); | |
346 | } | |
347 | else | |
348 | { | |
349 | wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ; | |
350 | m_graphicContext = NULL ; | |
351 | } | |
352 | // there is no out-of-order drawing on OSX | |
353 | #else | |
354 | m_macLocalOrigin.x = x ; | |
355 | m_macLocalOrigin.y = y ; | |
356 | SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ; | |
357 | SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ; | |
358 | OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ; | |
359 | SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->GetUpdateRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ; | |
360 | OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ; | |
361 | CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ; | |
362 | SetBackground(MacGetBackgroundBrush(window)); | |
363 | #endif | |
364 | ||
365 | SetFont( window->GetFont() ) ; | |
366 | } | |
367 | #endif | |
368 | ||
369 | wxPaintDC::~wxPaintDC() | |
370 | { | |
371 | } | |
372 | ||
373 | #if !wxMAC_USE_CORE_GRAPHICS | |
374 | void wxPaintDC::DoGetSize(int *width, int *height) const | |
375 | { | |
376 | wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") ); | |
377 | ||
378 | m_window->GetClientSize( width, height ); | |
379 | } | |
380 | #endif |