]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcclient.cpp
on demand creation of native CGContexts , so that pure text measuring contexts don...
[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/mac/private.h"
28
29 //-----------------------------------------------------------------------------
30 // constants
31 //-----------------------------------------------------------------------------
32
33 //-----------------------------------------------------------------------------
34 // wxPaintDC
35 //-----------------------------------------------------------------------------
36
37 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
38 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
39 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
40
41 /*
42 * wxWindowDC
43 */
44
45 #include "wx/mac/uma.h"
46 #include "wx/notebook.h"
47 #include "wx/tabctrl.h"
48
49
50 static wxBrush MacGetBackgroundBrush( wxWindow* window )
51 {
52 wxBrush bkdBrush = window->MacGetBackgroundBrush() ;
53
54 #if !TARGET_API_MAC_OSX
55 // transparency cannot be handled by the OS when not using composited windows
56 wxWindow* parent = window->GetParent() ;
57
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
62 while ( parent != NULL )
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 }
70
71 if ( parent->IsKindOf( CLASSINFO(wxTopLevelWindow) ) )
72 {
73 bkdBrush = parent->MacGetBackgroundBrush() ;
74 break ;
75 }
76
77 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) )
78 #if wxUSE_TAB_DIALOG
79 || parent->IsKindOf( CLASSINFO( wxTabCtrl ) )
80 #endif // wxUSE_TAB_DIALOG
81 )
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 ;
93 bkdBrush.MacSetThemeBackground( kThemeBackgroundTabPane , (WXRECTPTR) &extent ) ;
94 break ;
95 }
96
97 parent = parent->GetParent() ;
98 }
99 }
100
101 if ( !bkdBrush.Ok() || bkdBrush.GetStyle() == wxTRANSPARENT )
102 {
103 // if we did not find something, use a default
104 bkdBrush.MacSetTheme( kThemeBrushDialogBackgroundActive ) ;
105 }
106 #endif
107
108 return bkdBrush ;
109 }
110
111 wxWindowDC::wxWindowDC()
112 {
113 m_window = NULL ;
114 }
115
116 wxWindowDC::wxWindowDC(wxWindow *window)
117 {
118 m_window = window ;
119 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
120 if (!rootwindow)
121 return;
122
123 int x , y ;
124 x = y = 0 ;
125 window->MacWindowToRootWindow( &x , &y ) ;
126 m_ok = true ;
127
128 #if wxMAC_USE_CORE_GRAPHICS
129 m_window->GetSize( &m_width , &m_height);
130 CGContextRef cg = (CGContextRef) window->MacGetCGContextRef();
131 m_release = false;
132 if ( cg == NULL )
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 ) ;
151 #else
152 m_macLocalOrigin.x = x ;
153 m_macLocalOrigin.y = y ;
154 m_macPort = UMAGetWindowPort( (WindowRef) rootwindow->MacGetWindowRef() ) ;
155
156 CopyRgn( (RgnHandle) window->MacGetVisibleRegion(true).GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
157 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
158 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
159 #endif
160 SetBackground(MacGetBackgroundBrush(window));
161
162 SetFont( window->GetFont() ) ;
163 }
164
165 wxWindowDC::~wxWindowDC()
166 {
167 #if wxMAC_USE_CORE_GRAPHICS
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 }
174 #endif
175 }
176
177 void wxWindowDC::DoGetSize( int* width, int* height ) const
178 {
179 #if wxMAC_USE_CORE_GRAPHICS
180 if ( width )
181 *width = m_width;
182 if ( height )
183 *height = m_height;
184 #else
185 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
186 m_window->GetSize(width, height);
187 #endif
188 }
189
190 /*
191 * wxClientDC
192 */
193
194 wxClientDC::wxClientDC()
195 {
196 m_window = NULL ;
197 }
198
199 #if wxMAC_USE_CORE_GRAPHICS
200 wxClientDC::wxClientDC(wxWindow *window) :
201 wxWindowDC( window )
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);
210 SetDeviceOrigin( origin.x, origin.y );
211 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
212 }
213 #else
214 wxClientDC::wxClientDC(wxWindow *window)
215 {
216 m_window = window ;
217 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
218 if (!rootwindow)
219 return;
220
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 ) ;
229 m_ok = true ;
230
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 ) ;
238
239 SetBackground(MacGetBackgroundBrush(window));
240 SetFont( window->GetFont() ) ;
241 }
242 #endif
243
244 wxClientDC::~wxClientDC()
245 {
246 }
247
248 #if !wxMAC_USE_CORE_GRAPHICS
249 void 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 }
255 #endif
256
257 /*
258 * wxPaintDC
259 */
260
261 wxPaintDC::wxPaintDC()
262 {
263 m_window = NULL ;
264 }
265
266 #if wxMAC_USE_CORE_GRAPHICS
267 wxPaintDC::wxPaintDC(wxWindow *window) :
268 wxWindowDC( window )
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);
277 SetDeviceOrigin( origin.x, origin.y );
278 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
279 }
280 #else
281 wxPaintDC::wxPaintDC(wxWindow *window)
282 {
283 m_window = window ;
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 ) ;
293 m_ok = true ;
294
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 ) ;
301 SetClippingRegion( 0 , 0 , size.x , size.y ) ;
302 SetBackground(MacGetBackgroundBrush(window));
303 }
304 else
305 {
306 wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ;
307 m_graphicContext = NULL ;
308 }
309 // there is no out-of-order drawing on OSX
310 #else
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 ) ;
319 SetBackground(MacGetBackgroundBrush(window));
320 #endif
321
322 SetFont( window->GetFont() ) ;
323 }
324 #endif
325
326 wxPaintDC::~wxPaintDC()
327 {
328 }
329
330 #if !wxMAC_USE_CORE_GRAPHICS
331 void 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 }
337 #endif