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