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