]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/dcclient.cpp
new graphics context implementation
[wxWidgets.git] / src / mac / carbon / dcclient.cpp
... / ...
CommitLineData
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
36IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
37IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
38IMPLEMENT_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
49static 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
110wxWindowDC::wxWindowDC()
111{
112 m_window = NULL ;
113}
114
115wxWindowDC::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 SetGraphicsContext( wxGraphicsContext::Create( window ) ) ;
134 else
135 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg ) );
136 m_graphicContext->SetPen( m_pen ) ;
137 m_graphicContext->SetBrush( m_brush ) ;
138 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
139#else
140 m_macLocalOrigin.x = x ;
141 m_macLocalOrigin.y = y ;
142 m_macPort = UMAGetWindowPort( windowref ) ;
143
144 CopyRgn( (RgnHandle) window->MacGetVisibleRegion(true).GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
145 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
146 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
147#endif
148 SetBackground(MacGetBackgroundBrush(window));
149
150 SetFont( window->GetFont() ) ;
151}
152
153wxWindowDC::~wxWindowDC()
154{
155#if wxMAC_USE_CORE_GRAPHICS
156 if ( m_release && m_graphicContext )
157 {
158// CGContextRef cg = (CGContextRef) m_graphicContext->GetNativeContext() ;
159 }
160#endif
161}
162
163void wxWindowDC::DoGetSize( int* width, int* height ) const
164{
165 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
166
167#if wxMAC_USE_CORE_GRAPHICS
168 if ( width )
169 *width = m_width;
170 if ( height )
171 *height = m_height;
172#else
173 m_window->GetSize(width, height);
174#endif
175}
176
177/*
178 * wxClientDC
179 */
180
181wxClientDC::wxClientDC()
182{
183 m_window = NULL ;
184}
185
186#if wxMAC_USE_CORE_GRAPHICS
187wxClientDC::wxClientDC(wxWindow *window) :
188 wxWindowDC( window )
189{
190 wxPoint origin = window->GetClientAreaOrigin() ;
191 wxSize size = window->GetClientSize() ;
192 int x , y ;
193 x = origin.x ;
194 y = origin.y ;
195 window->MacWindowToRootWindow( &x , &y ) ;
196 m_window->GetClientSize( &m_width , &m_height);
197 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
198}
199#else
200wxClientDC::wxClientDC(wxWindow *window)
201{
202 m_window = window ;
203 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
204 if (!rootwindow)
205 return;
206
207 WindowRef windowref = (WindowRef) rootwindow->MacGetWindowRef() ;
208 wxPoint origin = window->GetClientAreaOrigin() ;
209 wxSize size = window->GetClientSize() ;
210 int x , y ;
211 x = origin.x ;
212 y = origin.y ;
213 window->MacWindowToRootWindow( &x , &y ) ;
214 m_macPort = UMAGetWindowPort( windowref ) ;
215 m_ok = true ;
216
217 m_macLocalOrigin.x = x ;
218 m_macLocalOrigin.y = y ;
219 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
220 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
221 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
222 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
223 CopyRgn( (RgnHandle) m_macBoundaryClipRgn ,(RgnHandle) m_macCurrentClipRgn ) ;
224
225 SetBackground(MacGetBackgroundBrush(window));
226 SetFont( window->GetFont() ) ;
227}
228#endif
229
230wxClientDC::~wxClientDC()
231{
232}
233
234#if !wxMAC_USE_CORE_GRAPHICS
235void wxClientDC::DoGetSize(int *width, int *height) const
236{
237 wxCHECK_RET( m_window, _T("GetSize() doesn't work without window") );
238
239 m_window->GetClientSize( width, height );
240}
241#endif
242
243/*
244 * wxPaintDC
245 */
246
247wxPaintDC::wxPaintDC()
248{
249 m_window = NULL ;
250}
251
252#if wxMAC_USE_CORE_GRAPHICS
253wxPaintDC::wxPaintDC(wxWindow *window) :
254 wxWindowDC( window )
255{
256 wxPoint origin = window->GetClientAreaOrigin() ;
257 wxSize size = window->GetClientSize() ;
258 int x , y ;
259 x = origin.x ;
260 y = origin.y ;
261 window->MacWindowToRootWindow( &x , &y ) ;
262 m_window->GetClientSize( &m_width , &m_height);
263 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
264}
265#else
266wxPaintDC::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 if ( window->MacGetCGContextRef() )
282 {
283 m_graphicContext = new wxMacCGContext( (CGContextRef) window->MacGetCGContextRef() ) ;
284 m_graphicContext->SetPen( m_pen ) ;
285 m_graphicContext->SetBrush( m_brush ) ;
286 SetClippingRegion( 0 , 0 , size.x , size.y ) ;
287 SetBackground(MacGetBackgroundBrush(window));
288 }
289 else
290 {
291 wxLogDebug(wxT("You cannot create a wxPaintDC outside an OS-draw event") ) ;
292 m_graphicContext = NULL ;
293 }
294 // there is no out-of-order drawing on OSX
295#else
296 m_macLocalOrigin.x = x ;
297 m_macLocalOrigin.y = y ;
298 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , origin.x , origin.y , origin.x + size.x , origin.y + size.y ) ;
299 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->MacGetVisibleRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
300 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , -origin.x , -origin.y ) ;
301 SectRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) window->GetUpdateRegion().GetWXHRGN() , (RgnHandle) m_macBoundaryClipRgn ) ;
302 OffsetRgn( (RgnHandle) m_macBoundaryClipRgn , m_macLocalOrigin.x , m_macLocalOrigin.y ) ;
303 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
304 SetBackground(MacGetBackgroundBrush(window));
305#endif
306
307 SetFont( window->GetFont() ) ;
308}
309#endif
310
311wxPaintDC::~wxPaintDC()
312{
313}
314
315#if !wxMAC_USE_CORE_GRAPHICS
316void 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}
322#endif