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