]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcclient.cpp
use consistent names (Graphic vs. Graphics)
[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 int x , y ;
123 x = y = 0 ;
124 window->MacWindowToRootWindow( &x , &y ) ;
125 m_ok = true ;
126
127 #if wxMAC_USE_CORE_GRAPHICS
128 m_window->GetSize( &m_width , &m_height);
129 CGContextRef cg = (CGContextRef) window->MacGetCGContextRef();
130 m_release = false;
131 if ( cg == NULL )
132 {
133 SetGraphicsContext( wxGraphicsContext::Create( window ) ) ;
134 SetDeviceOrigin( x, y );
135 }
136 else
137 {
138 CGContextSaveGState( cg );
139 m_release = true ;
140 // make sure the context is having its origin at the wx-window coordinates of the
141 // view (read at the top of window.cpp about the differences)
142 if ( window->MacGetLeftBorderSize() != 0 || window->MacGetTopBorderSize() != 0 )
143 CGContextTranslateCTM( cg , -window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize() );
144
145 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg ) );
146 }
147 m_graphicContext->SetPen( m_pen ) ;
148 m_graphicContext->SetBrush( m_brush ) ;
149 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
150 #else
151 m_macLocalOrigin.x = x ;
152 m_macLocalOrigin.y = y ;
153 m_macPort = UMAGetWindowPort( (WindowRef) rootwindow->MacGetWindowRef() ) ;
154
155 CopyRgn( (RgnHandle) window->MacGetVisibleRegion(true).GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
156 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
157 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
158 #endif
159 SetBackground(MacGetBackgroundBrush(window));
160
161 SetFont( window->GetFont() ) ;
162 }
163
164 wxWindowDC::~wxWindowDC()
165 {
166 #if wxMAC_USE_CORE_GRAPHICS
167 if ( m_release && m_graphicContext )
168 {
169 CGContextRef cg = (CGContextRef) m_window->MacGetCGContextRef();
170 CGContextRestoreGState(cg);
171 //CGContextRef cg = (CGContextRef) m_graphicContext->GetNativeContext() ;
172 }
173 #endif
174 }
175
176 void wxWindowDC::DoGetSize( int* width, int* height ) const
177 {
178 #if wxMAC_USE_CORE_GRAPHICS
179 if ( width )
180 *width = m_width;
181 if ( height )
182 *height = m_height;
183 #else
184 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
185 m_window->GetSize(width, height);
186 #endif
187 }
188
189 /*
190 * wxClientDC
191 */
192
193 wxClientDC::wxClientDC()
194 {
195 m_window = NULL ;
196 }
197
198 #if wxMAC_USE_CORE_GRAPHICS
199 wxClientDC::wxClientDC(wxWindow *window) :
200 wxWindowDC( window )
201 {
202 wxPoint origin = window->GetClientAreaOrigin() ;
203 wxSize size = window->GetClientSize() ;
204 int x , y ;
205 x = origin.x ;
206 y = origin.y ;
207 window->MacWindowToRootWindow( &x , &y ) ;
208 m_window->GetClientSize( &m_width , &m_height);
209 SetDeviceOrigin( origin.x, origin.y );
210 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
211 }
212 #else
213 wxClientDC::wxClientDC(wxWindow *window)
214 {
215 m_window = window ;
216 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
217 if (!rootwindow)
218 return;
219
220 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
221 wxPoint origin = window->GetClientAreaOrigin() ;
222 wxSize size = window->GetClientSize() ;
223 int x , y ;
224 x = origin.x ;
225 y = origin.y ;
226 window->MacWindowToRootWindow( &x , &y ) ;
227 m_macPort = UMAGetWindowPort( windowref ) ;
228 m_ok = true ;
229
230 m_macLocalOrigin.x = x ;
231 m_macLocalOrigin.y = y ;
232 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
233 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
234 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
235 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
236 CopyRgn( (RgnHandle) m_macBoundaryClipRgn ,(RgnHandle) m_macCurrentClipRgn ) ;
237
238 SetBackground(MacGetBackgroundBrush(window));
239 SetFont( window->GetFont() ) ;
240 }
241 #endif
242
243 wxClientDC::~wxClientDC()
244 {
245 }
246
247 #if !wxMAC_USE_CORE_GRAPHICS
248 void wxClientDC::DoGetSize(int *width, int *height) const
249 {
250 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
251
252 m_window->GetClientSize( width, height );
253 }
254 #endif
255
256 /*
257 * wxPaintDC
258 */
259
260 wxPaintDC::wxPaintDC()
261 {
262 m_window = NULL ;
263 }
264
265 #if wxMAC_USE_CORE_GRAPHICS
266 wxPaintDC::wxPaintDC(wxWindow *window) :
267 wxWindowDC( window )
268 {
269 wxPoint origin = window->GetClientAreaOrigin() ;
270 wxSize size = window->GetClientSize() ;
271 int x , y ;
272 x = origin.x ;
273 y = origin.y ;
274 window->MacWindowToRootWindow( &x , &y ) ;
275 m_window->GetClientSize( &m_width , &m_height);
276 SetDeviceOrigin( origin.x, origin.y );
277 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
278 }
279 #else
280 wxPaintDC::wxPaintDC(wxWindow *window)
281 {
282 m_window = window ;
283 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
284 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
285 wxPoint origin = window->GetClientAreaOrigin() ;
286 wxSize size = window->GetClientSize() ;
287 int x , y ;
288 x = origin.x ;
289 y = origin.y ;
290 window->MacWindowToRootWindow( &x , &y ) ;
291 m_macPort = UMAGetWindowPort( windowref ) ;
292 m_ok = true ;
293
294 #if wxMAC_USE_CORE_GRAPHICS
295 if ( window->MacGetCGContextRef() )
296 {
297 m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ;
298 m_graphicContext->SetPen( m_pen ) ;
299 m_graphicContext->SetBrush( m_brush ) ;
300 SetClippingRegion( 0 , 0 , size.x , size.y ) ;
301 SetBackground(MacGetBackgroundBrush(window));
302 }
303 else
304 {
305 wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ;
306 m_graphicContext = NULL ;
307 }
308 // there is no out-of-order drawing on OSX
309 #else
310 m_macLocalOrigin.x = x ;
311 m_macLocalOrigin.y = y ;
312 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
313 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
314 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
315 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->GetUpdateRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
316 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
317 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
318 SetBackground(MacGetBackgroundBrush(window));
319 #endif
320
321 SetFont( window->GetFont() ) ;
322 }
323 #endif
324
325 wxPaintDC::~wxPaintDC()
326 {
327 }
328
329 #if !wxMAC_USE_CORE_GRAPHICS
330 void wxPaintDC::DoGetSize(int *width, int *height) const
331 {
332 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
333
334 m_window->GetClientSize( width, height );
335 }
336 #endif