]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcclient.cpp
Override GetPixelSize on OS X as the base impl creates a wxScreenDC each time, which...
[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"
76a5e5d2 27#include "wx/mac/private.h"
e9576ca5
SC
28
29//-----------------------------------------------------------------------------
30// constants
31//-----------------------------------------------------------------------------
32
e9576ca5
SC
33//-----------------------------------------------------------------------------
34// wxPaintDC
35//-----------------------------------------------------------------------------
36
e9576ca5
SC
37IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
38IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
39IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
e9576ca5
SC
40
41/*
42 * wxWindowDC
43 */
44
d497dca4 45#include "wx/mac/uma.h"
be346c26
SC
46#include "wx/notebook.h"
47#include "wx/tabctrl.h"
e4db172a 48
be346c26
SC
49
50static wxBrush MacGetBackgroundBrush( wxWindow* window )
51{
52 wxBrush bkdBrush = window->MacGetBackgroundBrush() ;
9b2eab52 53
be346c26
SC
54#if !TARGET_API_MAC_OSX
55 // transparency cannot be handled by the OS when not using composited windows
56 wxWindow* parent = window->GetParent() ;
9b2eab52 57
be346c26
SC
58 // if we have some 'pseudo' transparency
59 if ( ! bkdBrush.Ok() || bkdBrush.GetStyle() == wxTRANSPARENT || window->GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
60 {
61 // walk up until we find something
9b2eab52 62 while ( parent != NULL )
be346c26
SC
63 {
64 if ( parent->GetBackgroundColour() != wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
65 {
66 // if we have any other colours in the hierarchy
67 bkdBrush.SetColour( parent->GetBackgroundColour() ) ;
68 break ;
69 }
9b2eab52 70
be346c26
SC
71 if ( parent->IsKindOf( CLASSINFO(wxTopLevelWindow) ) )
72 {
73 bkdBrush = parent->MacGetBackgroundBrush() ;
74 break ;
75 }
9b2eab52 76
7301b2b8
WS
77 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) )
78#if wxUSE_TAB_DIALOG
79 || parent->IsKindOf( CLASSINFO( wxTabCtrl ) )
9eddec69 80#endif // wxUSE_TAB_DIALOG
7301b2b8 81 )
be346c26
SC
82 {
83 Rect extent = { 0 , 0 , 0 , 0 } ;
84 int x , y ;
85 x = y = 0 ;
86 wxSize size = parent->GetSize() ;
87 parent->MacClientToRootWindow( &x , &y ) ;
88 extent.left = x ;
89 extent.top = y ;
90 extent.top-- ;
91 extent.right = x + size.x ;
92 extent.bottom = y + size.y ;
e4db172a 93 bkdBrush.MacSetThemeBackground( kThemeBackgroundTabPane , (WXRECTPTR) &extent ) ;
be346c26
SC
94 break ;
95 }
e4db172a
WS
96
97 parent = parent->GetParent() ;
be346c26
SC
98 }
99 }
9b2eab52 100
be346c26
SC
101 if ( !bkdBrush.Ok() || bkdBrush.GetStyle() == wxTRANSPARENT )
102 {
103 // if we did not find something, use a default
104 bkdBrush.MacSetTheme( kThemeBrushDialogBackgroundActive ) ;
105 }
9b2eab52
DS
106#endif
107
be346c26 108 return bkdBrush ;
e4db172a 109}
be346c26 110
e4db172a 111wxWindowDC::wxWindowDC()
e9576ca5 112{
1be0560e 113 m_window = NULL ;
519cb848 114}
e9576ca5 115
e4db172a 116wxWindowDC::wxWindowDC(wxWindow *window)
519cb848 117{
1be0560e 118 m_window = window ;
9f391ae1
SC
119 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
120 if (!rootwindow)
121 return;
9b2eab52 122
9f391ae1
SC
123 int x , y ;
124 x = y = 0 ;
9f391ae1 125 window->MacWindowToRootWindow( &x , &y ) ;
eb1a7cf9
DS
126 m_ok = true ;
127
20b69855 128#if wxMAC_USE_CORE_GRAPHICS
4130e414 129 m_window->GetSize( &m_width , &m_height);
a2d6d210
VZ
130 CGContextRef cg = (CGContextRef) window->MacGetCGContextRef();
131 m_release = false;
4130e414 132 if ( cg == NULL )
a2d6d210
VZ
133 {
134 SetGraphicsContext( wxGraphicsContext::Create( window ) ) ;
135 SetDeviceOrigin( x, y );
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 ) ;
20b69855 151#else
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
519cb848
SC
190/*
191 * wxClientDC
192 */
e9576ca5 193
2f1ae414 194wxClientDC::wxClientDC()
e9576ca5 195{
1be0560e 196 m_window = NULL ;
e9576ca5
SC
197}
198
4130e414
SC
199#if wxMAC_USE_CORE_GRAPHICS
200wxClientDC::wxClientDC(wxWindow *window) :
a2d6d210 201 wxWindowDC( window )
4130e414
SC
202{
203 wxPoint origin = window->GetClientAreaOrigin() ;
204 wxSize size = window->GetClientSize() ;
205 int x , y ;
206 x = origin.x ;
207 y = origin.y ;
208 window->MacWindowToRootWindow( &x , &y ) ;
209 m_window->GetClientSize( &m_width , &m_height);
a2d6d210
VZ
210 SetDeviceOrigin( origin.x, origin.y );
211 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
4130e414
SC
212}
213#else
519cb848 214wxClientDC::wxClientDC(wxWindow *window)
e9576ca5 215{
1be0560e 216 m_window = window ;
9f391ae1
SC
217 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
218 if (!rootwindow)
219 return;
9b2eab52 220
9f391ae1
SC
221 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
222 wxPoint origin = window->GetClientAreaOrigin() ;
223 wxSize size = window->GetClientSize() ;
224 int x , y ;
225 x = origin.x ;
226 y = origin.y ;
227 window->MacWindowToRootWindow( &x , &y ) ;
228 m_macPort = UMAGetWindowPort( windowref ) ;
eb1a7cf9
DS
229 m_ok = true ;
230
e40298d5
JS
231 m_macLocalOrigin.x = x ;
232 m_macLocalOrigin.y = y ;
233 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
234 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
235 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
236 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
237 CopyRgn( (RgnHandle) m_macBoundaryClipRgn ,(RgnHandle) m_macCurrentClipRgn ) ;
9b2eab52 238
be346c26 239 SetBackground(MacGetBackgroundBrush(window));
e40298d5 240 SetFont( window->GetFont() ) ;
e9576ca5 241}
4130e414 242#endif
e9576ca5 243
2f1ae414 244wxClientDC::~wxClientDC()
e9576ca5 245{
e9576ca5
SC
246}
247
4130e414 248#if !wxMAC_USE_CORE_GRAPHICS
1be0560e
SC
249void wxClientDC::DoGetSize(int *width, int *height) const
250{
251 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
252
253 m_window->GetClientSize( width, height );
254}
4130e414 255#endif
1be0560e 256
519cb848
SC
257/*
258 * wxPaintDC
259 */
e9576ca5 260
2f1ae414 261wxPaintDC::wxPaintDC()
e9576ca5 262{
1be0560e 263 m_window = NULL ;
e9576ca5
SC
264}
265
4130e414
SC
266#if wxMAC_USE_CORE_GRAPHICS
267wxPaintDC::wxPaintDC(wxWindow *window) :
a2d6d210 268 wxWindowDC( window )
4130e414
SC
269{
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_window->GetClientSize( &m_width , &m_height);
a2d6d210
VZ
277 SetDeviceOrigin( origin.x, origin.y );
278 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
4130e414
SC
279}
280#else
519cb848 281wxPaintDC::wxPaintDC(wxWindow *window)
e9576ca5 282{
1be0560e 283 m_window = window ;
9f391ae1
SC
284 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
285 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
286 wxPoint origin = window->GetClientAreaOrigin() ;
287 wxSize size = window->GetClientSize() ;
288 int x , y ;
289 x = origin.x ;
290 y = origin.y ;
291 window->MacWindowToRootWindow( &x , &y ) ;
292 m_macPort = UMAGetWindowPort( windowref ) ;
eb1a7cf9 293 m_ok = true ;
9b2eab52 294
20b69855
SC
295#if wxMAC_USE_CORE_GRAPHICS
296 if ( window->MacGetCGContextRef() )
297 {
298 m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ;
299 m_graphicContext->SetPen( m_pen ) ;
300 m_graphicContext->SetBrush( m_brush ) ;
9f391ae1
SC
301 SetClippingRegion( 0 , 0 , size.x , size.y ) ;
302 SetBackground(MacGetBackgroundBrush(window));
20b69855
SC
303 }
304 else
305 {
306 wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ;
307 m_graphicContext = NULL ;
20b69855
SC
308 }
309 // there is no out-of-order drawing on OSX
310#else
e40298d5
JS
311 m_macLocalOrigin.x = x ;
312 m_macLocalOrigin.y = y ;
313 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
314 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
315 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
316 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->GetUpdateRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
317 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
318 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
be346c26 319 SetBackground(MacGetBackgroundBrush(window));
20b69855 320#endif
9b2eab52 321
e40298d5 322 SetFont( window->GetFont() ) ;
e9576ca5 323}
4130e414 324#endif
e9576ca5 325
519cb848 326wxPaintDC::~wxPaintDC()
e9576ca5 327{
519cb848 328}
1be0560e 329
4130e414 330#if !wxMAC_USE_CORE_GRAPHICS
1be0560e
SC
331void wxPaintDC::DoGetSize(int *width, int *height) const
332{
333 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
334
335 m_window->GetClientSize( width, height );
336}
4130e414 337#endif