]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcclient.cpp
Applied patch 1586499: wxCoordRound function
[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 int x , y ;
125 x = y = 0 ;
126 window->MacWindowToRootWindow( &x , &y ) ;
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 SetDeviceOrigin( x, y );
137 }
138 else
139 {
140 CGContextSaveGState( cg );
141 m_release = true ;
142 // make sure the context is having its origin at the wx-window coordinates of the
143 // view (read at the top of window.cpp about the differences)
144 if ( window->MacGetLeftBorderSize() != 0 || window->MacGetTopBorderSize() != 0 )
145 CGContextTranslateCTM( cg , -window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize() );
146
147 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg ) );
148 }
149 m_graphicContext->SetPen( m_pen ) ;
150 m_graphicContext->SetBrush( m_brush ) ;
151 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
152 #else
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 && m_graphicContext )
170 {
171 CGContextRef cg = (CGContextRef) m_window->MacGetCGContextRef();
172 CGContextRestoreGState(cg);
173 //CGContextRef cg = (CGContextRef) m_graphicContext->GetNativeContext() ;
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
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 int width = rect.size.width;
207 int height = rect.size.height;
208
209 bytesPerRow = ( ( width * 8 * 4 + 7 ) / 8 );
210
211 data = calloc( 1, bytesPerRow * height );
212 context = CGBitmapContextCreate( data, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst );
213
214 CGContextDrawImage( context, rect, image );
215
216 unsigned char* buffer = (unsigned char*) data;
217 wxBitmap bmp = wxBitmap(width, height, 32);
218 wxAlphaPixelData pixData(bmp, wxPoint(0,0), wxSize(width, height));
219
220 pixData.UseAlpha();
221 wxAlphaPixelData::Iterator p(pixData);
222 for (int y=0; y<height; y++) {
223 wxAlphaPixelData::Iterator rowStart = p;
224 for (int x=0; x<width; x++) {
225 unsigned char a = buffer[3];
226 p.Red() = a; buffer++;
227 p.Green() = a; buffer++;
228 p.Blue() = a; buffer++;
229 p.Alpha() = a; buffer++;
230 ++p;
231 }
232 p = rowStart;
233 p.OffsetY(pixData, 1);
234 }
235
236 return bmp;
237 }
238
239 /*
240 * wxClientDC
241 */
242
243 wxClientDC::wxClientDC()
244 {
245 m_window = NULL ;
246 }
247
248 #if wxMAC_USE_CORE_GRAPHICS
249 wxClientDC::wxClientDC(wxWindow *window) :
250 wxWindowDC( window )
251 {
252 wxPoint origin = window->GetClientAreaOrigin() ;
253 wxSize size = window->GetClientSize() ;
254 int x , y ;
255 x = origin.x ;
256 y = origin.y ;
257 window->MacWindowToRootWindow( &x , &y ) ;
258 m_window->GetClientSize( &m_width , &m_height);
259 SetDeviceOrigin( origin.x, origin.y );
260 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
261 }
262 #else
263 wxClientDC::wxClientDC(wxWindow *window)
264 {
265 m_window = window ;
266 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
267 if (!rootwindow)
268 return;
269
270 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
271 wxPoint origin = window->GetClientAreaOrigin() ;
272 wxSize size = window->GetClientSize() ;
273 int x , y ;
274 x = origin.x ;
275 y = origin.y ;
276 window->MacWindowToRootWindow( &x , &y ) ;
277 m_macPort = UMAGetWindowPort( windowref ) ;
278 m_ok = true ;
279
280 m_macLocalOrigin.x = x ;
281 m_macLocalOrigin.y = y ;
282 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
283 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
284 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
285 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
286 CopyRgn( (RgnHandle) m_macBoundaryClipRgn ,(RgnHandle) m_macCurrentClipRgn ) ;
287
288 SetBackground(MacGetBackgroundBrush(window));
289 SetFont( window->GetFont() ) ;
290 }
291 #endif
292
293 wxClientDC::~wxClientDC()
294 {
295 }
296
297 #if !wxMAC_USE_CORE_GRAPHICS
298 void wxClientDC::DoGetSize(int *width, int *height) const
299 {
300 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
301
302 m_window->GetClientSize( width, height );
303 }
304 #endif
305
306 /*
307 * wxPaintDC
308 */
309
310 wxPaintDC::wxPaintDC()
311 {
312 m_window = NULL ;
313 }
314
315 #if wxMAC_USE_CORE_GRAPHICS
316 wxPaintDC::wxPaintDC(wxWindow *window) :
317 wxWindowDC( window )
318 {
319 wxPoint origin = window->GetClientAreaOrigin() ;
320 wxSize size = window->GetClientSize() ;
321 int x , y ;
322 x = origin.x ;
323 y = origin.y ;
324 window->MacWindowToRootWindow( &x , &y ) ;
325 m_window->GetClientSize( &m_width , &m_height);
326 SetDeviceOrigin( origin.x, origin.y );
327 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
328 }
329 #else
330 wxPaintDC::wxPaintDC(wxWindow *window)
331 {
332 m_window = window ;
333 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
334 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
335 wxPoint origin = window->GetClientAreaOrigin() ;
336 wxSize size = window->GetClientSize() ;
337 int x , y ;
338 x = origin.x ;
339 y = origin.y ;
340 window->MacWindowToRootWindow( &x , &y ) ;
341 m_macPort = UMAGetWindowPort( windowref ) ;
342 m_ok = true ;
343
344 #if wxMAC_USE_CORE_GRAPHICS
345 if ( window->MacGetCGContextRef() )
346 {
347 m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ;
348 m_graphicContext->SetPen( m_pen ) ;
349 m_graphicContext->SetBrush( m_brush ) ;
350 SetClippingRegion( 0 , 0 , size.x , size.y ) ;
351 SetBackground(MacGetBackgroundBrush(window));
352 }
353 else
354 {
355 wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ;
356 m_graphicContext = NULL ;
357 }
358 // there is no out-of-order drawing on OSX
359 #else
360 m_macLocalOrigin.x = x ;
361 m_macLocalOrigin.y = y ;
362 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
363 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
364 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
365 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->GetUpdateRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
366 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
367 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
368 SetBackground(MacGetBackgroundBrush(window));
369 #endif
370
371 SetFont( window->GetFont() ) ;
372 }
373 #endif
374
375 wxPaintDC::~wxPaintDC()
376 {
377 }
378
379 #if !wxMAC_USE_CORE_GRAPHICS
380 void wxPaintDC::DoGetSize(int *width, int *height) const
381 {
382 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
383
384 m_window->GetClientSize( width, height );
385 }
386 #endif