]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcclient.cpp
501e99022689fa24fcf7395fda50d4a2acd01f69
[wxWidgets.git] / src / mac / carbon / dcclient.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dcclient.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #include "wx/dcclient.h"
19 #include "wx/dcmemory.h"
20 #include "wx/region.h"
21 #include "wx/window.h"
22 #include "wx/toplevel.h"
23 #include "wx/settings.h"
24 #include "wx/math.h"
25 #include "wx/mac/private.h"
26 #include "wx/log.h"
27
28 //-----------------------------------------------------------------------------
29 // constants
30 //-----------------------------------------------------------------------------
31
32 #define RAD2DEG 57.2957795131
33
34 //-----------------------------------------------------------------------------
35 // wxPaintDC
36 //-----------------------------------------------------------------------------
37
38 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
39 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
40 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
41
42 /*
43 * wxWindowDC
44 */
45
46 #include "wx/mac/uma.h"
47 #include "wx/notebook.h"
48 #include "wx/tabctrl.h"
49
50
51 static wxBrush MacGetBackgroundBrush( wxWindow* window )
52 {
53 wxBrush bkdBrush = window->MacGetBackgroundBrush() ;
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 // 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 if ( parent->IsKindOf( CLASSINFO(wxTopLevelWindow) ) )
70 {
71 bkdBrush = parent->MacGetBackgroundBrush() ;
72 break ;
73 }
74 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ) )
75 {
76 Rect extent = { 0 , 0 , 0 , 0 } ;
77 int x , y ;
78 x = y = 0 ;
79 wxSize size = parent->GetSize() ;
80 parent->MacClientToRootWindow( &x , &y ) ;
81 extent.left = x ;
82 extent.top = y ;
83 extent.top-- ;
84 extent.right = x + size.x ;
85 extent.bottom = y + size.y ;
86 bkdBrush.MacSetThemeBackground( kThemeBackgroundTabPane , (WXRECTPTR) &extent ) ;
87 break ;
88 }
89
90 parent = parent->GetParent() ;
91 }
92 }
93 if ( !bkdBrush.Ok() || bkdBrush.GetStyle() == wxTRANSPARENT )
94 {
95 // if we did not find something, use a default
96 bkdBrush.MacSetTheme( kThemeBrushDialogBackgroundActive ) ;
97 }
98 #endif
99 return bkdBrush ;
100 }
101
102
103 wxWindowDC::wxWindowDC()
104 {
105 m_window = NULL ;
106 }
107
108 wxWindowDC::wxWindowDC(wxWindow *window)
109 {
110 m_window = window ;
111 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
112 if (!rootwindow)
113 return;
114 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
115 int x , y ;
116 x = y = 0 ;
117 wxSize size = window->GetSize() ;
118 window->MacWindowToRootWindow( &x , &y ) ;
119 m_macPort = UMAGetWindowPort( windowref ) ;
120
121 #if wxMAC_USE_CORE_GRAPHICS
122 m_macLocalOriginInPort.x = x ;
123 m_macLocalOriginInPort.y = y ;
124
125 if ( window->MacGetCGContextRef() )
126 {
127 m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ;
128 m_graphicContext->SetPen( m_pen ) ;
129 m_graphicContext->SetBrush( m_brush ) ;
130 SetBackground(MacGetBackgroundBrush(window));
131 }
132 else
133 {
134 // as out of order redraw is not supported under CQ, we have to create a qd port for these
135 // situations
136 m_macLocalOrigin.x = x ;
137 m_macLocalOrigin.y = y ;
138
139 m_graphicContext = new wxMacCGContext( (CGrafPtr) m_macPort ) ;
140 m_graphicContext->SetPen( m_pen ) ;
141 m_graphicContext->SetBrush( m_brush ) ;
142 SetBackground(MacGetBackgroundBrush(window));
143 }
144 // there is no out-of-order drawing on OSX
145 #else
146 m_macLocalOrigin.x = x ;
147 m_macLocalOrigin.y = y ;
148 CopyRgn( (RgnHandle) window->MacGetVisibleRegion(true).GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
149 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
150 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
151 SetBackground(MacGetBackgroundBrush(window));
152 #endif
153 m_ok = TRUE ;
154 SetFont( window->GetFont() ) ;
155 }
156
157 wxWindowDC::~wxWindowDC()
158 {
159 }
160
161 void wxWindowDC::DoGetSize( int* width, int* height ) const
162 {
163 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
164
165 m_window->GetSize(width, height);
166 }
167
168 /*
169 * wxClientDC
170 */
171
172 wxClientDC::wxClientDC()
173 {
174 m_window = NULL ;
175 }
176
177 wxClientDC::wxClientDC(wxWindow *window)
178 {
179 m_window = window ;
180 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
181 if (!rootwindow)
182 return;
183 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
184 wxPoint origin = window->GetClientAreaOrigin() ;
185 wxSize size = window->GetClientSize() ;
186 int x , y ;
187 x = origin.x ;
188 y = origin.y ;
189 window->MacWindowToRootWindow( &x , &y ) ;
190 m_macPort = UMAGetWindowPort( windowref ) ;
191
192 #if wxMAC_USE_CORE_GRAPHICS
193 m_macLocalOriginInPort.x = x ;
194 m_macLocalOriginInPort.y = y ;
195 if ( window->MacGetCGContextRef() )
196 {
197 m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ;
198 m_graphicContext->SetPen( m_pen ) ;
199 m_graphicContext->SetBrush( m_brush ) ;
200 m_ok = TRUE ;
201 SetClippingRegion( 0 , 0 , size.x , size.y ) ;
202 SetBackground(MacGetBackgroundBrush(window));
203 }
204 else
205 {
206 // as out of order redraw is not supported under CQ, we have to create a qd port for these
207 // situations
208 m_macLocalOrigin.x = x ;
209 m_macLocalOrigin.y = y ;
210 m_graphicContext = new wxMacCGContext( (CGrafPtr) m_macPort ) ;
211 m_graphicContext->SetPen( m_pen ) ;
212 m_graphicContext->SetBrush( m_brush ) ;
213 m_ok = TRUE ;
214 }
215 #else
216 m_macLocalOrigin.x = x ;
217 m_macLocalOrigin.y = y ;
218 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
219 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
220 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
221 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
222 CopyRgn( (RgnHandle) m_macBoundaryClipRgn ,(RgnHandle) m_macCurrentClipRgn ) ;
223 m_ok = TRUE ;
224 #endif
225 SetBackground(MacGetBackgroundBrush(window));
226 SetFont( window->GetFont() ) ;
227 }
228
229 wxClientDC::~wxClientDC()
230 {
231 #if wxMAC_USE_CORE_GRAPHICS
232 /*
233 if ( m_window->MacGetCGContextRef() == 0)
234 {
235 CGContextRef cgContext = (wxMacCGContext*)(m_graphicContext)->GetNativeContext() ;
236 CGContextFlush( cgContext ) ;
237 }
238 */
239 #endif
240 }
241
242 void wxClientDC::DoGetSize(int *width, int *height) const
243 {
244 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
245
246 m_window->GetClientSize( width, height );
247 }
248
249
250 /*
251 * wxPaintDC
252 */
253
254 wxPaintDC::wxPaintDC()
255 {
256 m_window = NULL ;
257 }
258
259 wxPaintDC::wxPaintDC(wxWindow *window)
260 {
261 m_window = window ;
262 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
263 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
264 wxPoint origin = window->GetClientAreaOrigin() ;
265 wxSize size = window->GetClientSize() ;
266 int x , y ;
267 x = origin.x ;
268 y = origin.y ;
269 window->MacWindowToRootWindow( &x , &y ) ;
270 m_macPort = UMAGetWindowPort( windowref ) ;
271 #if wxMAC_USE_CORE_GRAPHICS
272 m_macLocalOriginInPort.x = x ;
273 m_macLocalOriginInPort.y = y ;
274 if ( window->MacGetCGContextRef() )
275 {
276 m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ;
277 m_graphicContext->SetPen( m_pen ) ;
278 m_graphicContext->SetBrush( m_brush ) ;
279 m_ok = TRUE ;
280 SetClippingRegion( 0 , 0 , size.x , size.y ) ;
281 SetBackground(MacGetBackgroundBrush(window));
282 }
283 else
284 {
285 wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ;
286 m_graphicContext = NULL ;
287 m_ok = TRUE ;
288 }
289 // there is no out-of-order drawing on OSX
290 #else
291 m_macLocalOrigin.x = x ;
292 m_macLocalOrigin.y = y ;
293 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
294 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
295 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
296 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->GetUpdateRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
297 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
298 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
299 SetBackground(MacGetBackgroundBrush(window));
300 m_ok = TRUE ;
301 #endif
302 SetFont( window->GetFont() ) ;
303 }
304
305 wxPaintDC::~wxPaintDC()
306 {
307 }
308
309 void wxPaintDC::DoGetSize(int *width, int *height) const
310 {
311 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
312
313 m_window->GetClientSize( width, height );
314 }
315
316