]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcclient.cpp
Prevent assert and crash if an attempt is made to make a bitmap with
[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 #endif
22
23 #include "wx/region.h"
24 #include "wx/toplevel.h"
25 #include "wx/math.h"
26 #include "wx/mac/private.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
55 #if !TARGET_API_MAC_OSX
56 // transparency cannot be handled by the OS when not using composited windows
57 wxWindow* parent = window->GetParent() ;
58
59 // if we have some 'pseudo' transparency
60 if ( ! bkdBrush.Ok() || bkdBrush.GetStyle() == wxTRANSPARENT || window->GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
61 {
62 // walk up until we find something
63 while ( parent != NULL )
64 {
65 if ( parent->GetBackgroundColour() != wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
66 {
67 // if we have any other colours in the hierarchy
68 bkdBrush.SetColour( parent->GetBackgroundColour() ) ;
69 break ;
70 }
71
72 if ( parent->IsKindOf( CLASSINFO(wxTopLevelWindow) ) )
73 {
74 bkdBrush = parent->MacGetBackgroundBrush() ;
75 break ;
76 }
77
78 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) )
79 #if wxUSE_TAB_DIALOG
80 || parent->IsKindOf( CLASSINFO( wxTabCtrl ) )
81 #endif // wxUSE_TAB_DIALOG
82 )
83 {
84 Rect extent = { 0 , 0 , 0 , 0 } ;
85 int x , y ;
86 x = y = 0 ;
87 wxSize size = parent->GetSize() ;
88 parent->MacClientToRootWindow( &x , &y ) ;
89 extent.left = x ;
90 extent.top = y ;
91 extent.top-- ;
92 extent.right = x + size.x ;
93 extent.bottom = y + size.y ;
94 bkdBrush.MacSetThemeBackground( kThemeBackgroundTabPane , (WXRECTPTR) &extent ) ;
95 break ;
96 }
97
98 parent = parent->GetParent() ;
99 }
100 }
101
102 if ( !bkdBrush.Ok() || bkdBrush.GetStyle() == wxTRANSPARENT )
103 {
104 // if we did not find something, use a default
105 bkdBrush.MacSetTheme( kThemeBrushDialogBackgroundActive ) ;
106 }
107 #endif
108
109 return bkdBrush ;
110 }
111
112 wxWindowDC::wxWindowDC()
113 {
114 m_window = NULL ;
115 }
116
117 wxWindowDC::wxWindowDC(wxWindow *window)
118 {
119 m_window = window ;
120 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
121 if (!rootwindow)
122 return;
123
124 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
125 int x , y ;
126 x = y = 0 ;
127 wxSize size = window->GetSize() ;
128 window->MacWindowToRootWindow( &x , &y ) ;
129 m_macPort = UMAGetWindowPort( windowref ) ;
130 m_ok = true ;
131
132 #if wxMAC_USE_CORE_GRAPHICS
133 m_macLocalOriginInPort.x = x ;
134 m_macLocalOriginInPort.y = y ;
135
136 if ( window->MacGetCGContextRef() )
137 {
138 m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ;
139 m_graphicContext->SetPen( m_pen ) ;
140 m_graphicContext->SetBrush( m_brush ) ;
141 }
142 else
143 {
144 // as out of order redraw is not supported under CQ, we have to create a qd port for these
145 // situations
146 m_macLocalOrigin.x = x ;
147 m_macLocalOrigin.y = y ;
148
149 m_graphicContext = new wxMacCGContext( (CGrafPtr) m_macPort ) ;
150 m_graphicContext->SetPen( m_pen ) ;
151 m_graphicContext->SetBrush( m_brush ) ;
152 }
153 // there is no out-of-order drawing on OSX
154 #else
155 m_macLocalOrigin.x = x ;
156 m_macLocalOrigin.y = y ;
157 CopyRgn( (RgnHandle) window->MacGetVisibleRegion(true).GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
158 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
159 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
160 #endif
161 SetBackground(MacGetBackgroundBrush(window));
162
163 SetFont( window->GetFont() ) ;
164 }
165
166 wxWindowDC::~wxWindowDC()
167 {
168 }
169
170 void wxWindowDC::DoGetSize( int* width, int* height ) const
171 {
172 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
173
174 m_window->GetSize(width, height);
175 }
176
177 /*
178 * wxClientDC
179 */
180
181 wxClientDC::wxClientDC()
182 {
183 m_window = NULL ;
184 }
185
186 wxClientDC::wxClientDC(wxWindow *window)
187 {
188 m_window = window ;
189 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
190 if (!rootwindow)
191 return;
192
193 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
194 wxPoint origin = window->GetClientAreaOrigin() ;
195 wxSize size = window->GetClientSize() ;
196 int x , y ;
197 x = origin.x ;
198 y = origin.y ;
199 window->MacWindowToRootWindow( &x , &y ) ;
200 m_macPort = UMAGetWindowPort( windowref ) ;
201 m_ok = true ;
202
203 #if wxMAC_USE_CORE_GRAPHICS
204 m_macLocalOriginInPort.x = x ;
205 m_macLocalOriginInPort.y = y ;
206 if ( window->MacGetCGContextRef() )
207 {
208 m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ;
209 m_graphicContext->SetPen( m_pen ) ;
210 m_graphicContext->SetBrush( m_brush ) ;
211 SetClippingRegion( 0 , 0 , size.x , size.y ) ;
212 }
213 else
214 {
215 // as out of order redraw is not supported under CQ,
216 // we have to create a QD port for these situations
217 m_macLocalOrigin.x = x ;
218 m_macLocalOrigin.y = y ;
219 m_graphicContext = new wxMacCGContext( (CGrafPtr) m_macPort ) ;
220 m_graphicContext->SetPen( m_pen ) ;
221 m_graphicContext->SetBrush( m_brush ) ;
222 }
223 #else
224 m_macLocalOrigin.x = x ;
225 m_macLocalOrigin.y = y ;
226 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
227 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
228 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
229 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
230 CopyRgn( (RgnHandle) m_macBoundaryClipRgn ,(RgnHandle) m_macCurrentClipRgn ) ;
231 #endif
232
233 SetBackground(MacGetBackgroundBrush(window));
234 SetFont( window->GetFont() ) ;
235 }
236
237 wxClientDC::~wxClientDC()
238 {
239 #if wxMAC_USE_CORE_GRAPHICS
240 /*
241 if ( m_window->MacGetCGContextRef() == 0)
242 {
243 CGContextRef cgContext = (wxMacCGContext*)(m_graphicContext)->GetNativeContext() ;
244 CGContextFlush( cgContext ) ;
245 }
246 */
247 #endif
248 }
249
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
257 /*
258 * wxPaintDC
259 */
260
261 wxPaintDC::wxPaintDC()
262 {
263 m_window = NULL ;
264 }
265
266 wxPaintDC::wxPaintDC(wxWindow *window)
267 {
268 m_window = window ;
269 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
270 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
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_macPort = UMAGetWindowPort( windowref ) ;
278 m_ok = true ;
279
280 #if wxMAC_USE_CORE_GRAPHICS
281 m_macLocalOriginInPort.x = x ;
282 m_macLocalOriginInPort.y = y ;
283 if ( window->MacGetCGContextRef() )
284 {
285 m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ;
286 m_graphicContext->SetPen( m_pen ) ;
287 m_graphicContext->SetBrush( m_brush ) ;
288 SetClippingRegion( 0 , 0 , size.x , size.y ) ;
289 SetBackground(MacGetBackgroundBrush(window));
290 }
291 else
292 {
293 wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ;
294 m_graphicContext = NULL ;
295 }
296 // there is no out-of-order drawing on OSX
297 #else
298 m_macLocalOrigin.x = x ;
299 m_macLocalOrigin.y = y ;
300 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
301 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
302 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
303 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->GetUpdateRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
304 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
305 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
306 SetBackground(MacGetBackgroundBrush(window));
307 #endif
308
309 SetFont( window->GetFont() ) ;
310 }
311
312 wxPaintDC::~wxPaintDC()
313 {
314 }
315
316 void wxPaintDC::DoGetSize(int *width, int *height) const
317 {
318 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
319
320 m_window->GetClientSize( width, height );
321 }