]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/overlay.cpp
Fix crash in docview code if view initialization failed.
[wxWidgets.git] / src / osx / carbon / overlay.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/overlay.cpp
3 // Purpose: common wxOverlay code
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 2006-10-20
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/overlay.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/dcclient.h"
31 #endif
32
33 #include "wx/private/overlay.h"
34
35 #ifdef wxHAS_NATIVE_OVERLAY
36
37 // ============================================================================
38 // implementation
39 // ============================================================================
40
41 wxOverlayImpl::wxOverlayImpl()
42 {
43 m_window = NULL ;
44 m_overlayContext = NULL ;
45 m_overlayWindow = NULL ;
46 }
47
48 wxOverlayImpl::~wxOverlayImpl()
49 {
50 Reset();
51 }
52
53 bool wxOverlayImpl::IsOk()
54 {
55 return m_overlayWindow != NULL ;
56 }
57
58 void wxOverlayImpl::MacGetBounds( Rect *bounds )
59 {
60 int x, y;
61 x=y=0;
62 m_window->MacWindowToRootWindow( &x , &y ) ;
63 wxNonOwnedWindow* tlw = m_window->MacGetTopLevelWindow();
64 tlw->GetNonOwnedPeer()->WindowToScreen( &x, &y );
65
66 bounds->top = y+m_y;
67 bounds->left = x+m_x;
68 bounds->bottom = y+m_y+m_height;
69 bounds->right = x+m_x+m_width;
70 }
71
72 OSStatus wxOverlayImpl::CreateOverlayWindow()
73 {
74 OSStatus err;
75
76 WindowAttributes overlayAttributes = kWindowIgnoreClicksAttribute;
77
78 if ( m_window )
79 {
80 m_overlayParentWindow =(WindowRef) m_window->MacGetTopLevelWindowRef();
81
82 Rect bounds ;
83 MacGetBounds(&bounds);
84 err = CreateNewWindow( kOverlayWindowClass, overlayAttributes, &bounds, &m_overlayWindow );
85 if ( err == noErr )
86 {
87 SetWindowGroup( m_overlayWindow, GetWindowGroup(m_overlayParentWindow)); // Put them in the same group so that their window layers are consistent
88 }
89 }
90 else
91 {
92 m_overlayParentWindow = NULL ;
93 CGRect cgbounds ;
94 cgbounds = CGDisplayBounds(CGMainDisplayID());
95 Rect bounds;
96 bounds.top = (short)cgbounds.origin.y;
97 bounds.left = (short)cgbounds.origin.x;
98 bounds.bottom = (short)(bounds.top + cgbounds.size.height);
99 bounds.right = (short)(bounds.left + cgbounds.size.width);
100 err = CreateNewWindow( kOverlayWindowClass, overlayAttributes, &bounds, &m_overlayWindow );
101 }
102 ShowWindow(m_overlayWindow);
103 return err;
104 }
105
106 void wxOverlayImpl::Init( wxDC* dc, int x , int y , int width , int height )
107 {
108 wxASSERT_MSG( !IsOk() , _("You cannot Init an overlay twice") );
109
110 m_window = dc->GetWindow();
111 m_x = x ;
112 m_y = y ;
113 if ( dc->IsKindOf( CLASSINFO( wxClientDC ) ))
114 {
115 wxPoint origin = m_window->GetClientAreaOrigin();
116 m_x += origin.x;
117 m_y += origin.y;
118 }
119 m_width = width ;
120 m_height = height ;
121
122 OSStatus err = CreateOverlayWindow();
123 wxASSERT_MSG( err == noErr , _("Couldn't create the overlay window") );
124 #ifndef __LP64__
125 err = QDBeginCGContext(GetWindowPort(m_overlayWindow), &m_overlayContext);
126 #endif
127 CGContextTranslateCTM( m_overlayContext, 0, m_height );
128 CGContextScaleCTM( m_overlayContext, 1, -1 );
129 CGContextTranslateCTM( m_overlayContext, -m_x , -m_y );
130 wxASSERT_MSG( err == noErr , _("Couldn't init the context on the overlay window") );
131 }
132
133 void wxOverlayImpl::BeginDrawing( wxDC* dc)
134 {
135 wxDCImpl *impl = dc->GetImpl();
136 wxGCDCImpl *win_impl = wxDynamicCast(impl,wxGCDCImpl);
137 if (win_impl)
138 {
139 win_impl->SetGraphicsContext( wxGraphicsContext::CreateFromNative( m_overlayContext ) );
140 dc->SetClippingRegion( m_x , m_y , m_width , m_height ) ;
141 }
142 }
143
144 void wxOverlayImpl::EndDrawing( wxDC* dc)
145 {
146 wxDCImpl *impl = dc->GetImpl();
147 wxGCDCImpl *win_impl = wxDynamicCast(impl,wxGCDCImpl);
148 if (win_impl)
149 win_impl->SetGraphicsContext(NULL);
150
151 CGContextFlush( m_overlayContext );
152 }
153
154 void wxOverlayImpl::Clear(wxDC* WXUNUSED(dc))
155 {
156 wxASSERT_MSG( IsOk() , _("You cannot Clear an overlay that is not inited") );
157 CGRect box = CGRectMake( m_x - 1, m_y - 1 , m_width + 2 , m_height + 2 );
158 CGContextClearRect( m_overlayContext, box );
159 }
160
161 void wxOverlayImpl::Reset()
162 {
163 if ( m_overlayContext )
164 {
165 #ifndef __LP64__
166 OSStatus err = QDEndCGContext(GetWindowPort(m_overlayWindow), &m_overlayContext);
167 if ( err != noErr )
168 {
169 wxFAIL_MSG("Couldn't end the context on the overlay window");
170 }
171 #endif
172 m_overlayContext = NULL ;
173 }
174
175 // todo : don't dispose, only hide and reposition on next run
176 if (m_overlayWindow)
177 {
178 DisposeWindow(m_overlayWindow);
179 m_overlayWindow = NULL ;
180 }
181 }
182
183 #endif // wxHAS_NATIVE_OVERLAY