set m_isBeingDeleted to true (only) in SendDestroyEvent(); call it as early as possib...
[wxWidgets.git] / src / osx / nonownedwnd_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/nonownedwnd_osx.cpp
3 // Purpose: implementation of wxNonOwnedWindow
4 // Author: Stefan Csomor
5 // Created: 2008-03-24
6 // RCS-ID: $Id: nonownedwnd.cpp 50329 2007-11-29 17:00:58Z VS $
7 // Copyright: (c) Stefan Csomor 2008
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/app.h"
16 #endif // WX_PRECOMP
17
18 #include "wx/hashmap.h"
19 #include "wx/evtloop.h"
20 #include "wx/tooltip.h"
21 #include "wx/nonownedwnd.h"
22
23 #include "wx/osx/private.h"
24 #include "wx/settings.h"
25 #include "wx/frame.h"
26
27 #if wxUSE_SYSTEM_OPTIONS
28 #include "wx/sysopt.h"
29 #endif
30
31 // ----------------------------------------------------------------------------
32 // constants
33 // ----------------------------------------------------------------------------
34
35 // trace mask for activation tracing messages
36 #define TRACE_ACTIVATE "activation"
37
38 wxWindow* g_MacLastWindow = NULL ;
39
40 // unified title and toolbar constant - not in Tiger headers, so we duplicate it here
41 #define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
42
43 // ---------------------------------------------------------------------------
44 // wxWindowMac utility functions
45 // ---------------------------------------------------------------------------
46
47 // Find an item given the Macintosh Window Reference
48
49 WX_DECLARE_HASH_MAP(WXWindow, wxNonOwnedWindow*, wxPointerHash, wxPointerEqual, MacWindowMap);
50
51 static MacWindowMap wxWinMacWindowList;
52
53 wxNonOwnedWindow *wxFindWindowFromWXWindow(WXWindow inWindowRef)
54 {
55 MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
56
57 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
58 }
59
60 void wxAssociateWindowWithWXWindow(WXWindow inWindowRef, wxNonOwnedWindow *win) ;
61 void wxAssociateWindowWithWXWindow(WXWindow inWindowRef, wxNonOwnedWindow *win)
62 {
63 // adding NULL WindowRef is (first) surely a result of an error and
64 // nothing else :-)
65 wxCHECK_RET( inWindowRef != (WXWindow) NULL, wxT("attempt to add a NULL WindowRef to window list") );
66
67 wxWinMacWindowList[inWindowRef] = win;
68 }
69
70 void wxRemoveWXWindowAssociation(wxNonOwnedWindow *win) ;
71 void wxRemoveWXWindowAssociation(wxNonOwnedWindow *win)
72 {
73 MacWindowMap::iterator it;
74 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
75 {
76 if ( it->second == win )
77 {
78 wxWinMacWindowList.erase(it);
79 break;
80 }
81 }
82 }
83
84 wxNonOwnedWindow* wxNonOwnedWindow::GetFromWXWindow( WXWindow win )
85 {
86 return wxFindWindowFromWXWindow( win );
87 }
88
89 // ----------------------------------------------------------------------------
90 // wxNonOwnedWindow creation
91 // ----------------------------------------------------------------------------
92
93 IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl , wxObject )
94
95 wxNonOwnedWindow *wxNonOwnedWindow::s_macDeactivateWindow = NULL;
96
97 void wxNonOwnedWindow::Init()
98 {
99 m_nowpeer = NULL;
100 }
101
102 bool wxNonOwnedWindow::Create(wxWindow *parent,
103 wxWindowID id,
104 const wxPoint& pos,
105 const wxSize& size,
106 long style,
107 const wxString& name)
108 {
109 // init our fields
110 Init();
111
112 m_windowStyle = style;
113
114 SetName( name );
115
116 m_windowId = id == -1 ? NewControlId() : id;
117 m_windowStyle = style;
118 m_isShown = false;
119
120 // create frame.
121 int x = (int)pos.x;
122 int y = (int)pos.y;
123
124 wxRect display = wxGetClientDisplayRect() ;
125
126 if ( x == wxDefaultPosition.x )
127 x = display.x ;
128
129 if ( y == wxDefaultPosition.y )
130 y = display.y ;
131
132 int w = WidthDefault(size.x);
133 int h = HeightDefault(size.y);
134
135 m_nowpeer = wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, parent, wxPoint(x,y) , wxSize(w,h) , style , GetExtraStyle(), name );
136 wxAssociateWindowWithWXWindow( m_nowpeer->GetWXWindow() , this ) ;
137 m_peer = wxWidgetImpl::CreateContentView(this);
138
139 DoSetWindowVariant( m_windowVariant ) ;
140
141 wxWindowCreateEvent event(this);
142 HandleWindowEvent(event);
143
144 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ));
145
146 if ( parent )
147 parent->AddChild(this);
148
149 return true;
150 }
151
152 wxNonOwnedWindow::~wxNonOwnedWindow()
153 {
154 SendDestroyEvent();
155
156 wxRemoveWXWindowAssociation( this ) ;
157
158 DestroyChildren();
159
160 delete m_nowpeer;
161
162 // avoid dangling refs
163 if ( s_macDeactivateWindow == this )
164 s_macDeactivateWindow = NULL;
165 }
166
167 // ----------------------------------------------------------------------------
168 // wxNonOwnedWindow misc
169 // ----------------------------------------------------------------------------
170
171 bool wxNonOwnedWindow::ShowWithEffect(wxShowEffect effect,
172 unsigned timeout )
173 {
174 if ( !wxWindow::Show(true) )
175 return false;
176
177 // because apps expect a size event to occur at this moment
178 wxSizeEvent event(GetSize() , m_windowId);
179 event.SetEventObject(this);
180 HandleWindowEvent(event);
181
182
183 return m_nowpeer->ShowWithEffect(true, effect, timeout);
184 }
185
186 bool wxNonOwnedWindow::HideWithEffect(wxShowEffect effect,
187 unsigned timeout )
188 {
189 if ( !wxWindow::Show(false) )
190 return false;
191
192 return m_nowpeer->ShowWithEffect(false, effect, timeout);
193 }
194
195 wxPoint wxNonOwnedWindow::GetClientAreaOrigin() const
196 {
197 int left, top, width, height;
198 m_nowpeer->GetContentArea(left, top, width, height);
199 return wxPoint(left, top);
200 }
201
202 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour& c )
203 {
204 if ( !wxWindow::SetBackgroundColour(c) && m_hasBgCol )
205 return false ;
206
207 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM )
208 {
209 if ( m_nowpeer )
210 return m_nowpeer->SetBackgroundColour(c);
211 }
212 return true;
213 }
214
215 // Raise the window to the top of the Z order
216 void wxNonOwnedWindow::Raise()
217 {
218 m_nowpeer->Raise();
219 }
220
221 // Lower the window to the bottom of the Z order
222 void wxNonOwnedWindow::Lower()
223 {
224 m_nowpeer->Lower();
225 }
226
227 void wxNonOwnedWindow::HandleActivated( double timestampsec, bool didActivate )
228 {
229 MacActivate( (int) (timestampsec * 1000) , didActivate);
230 wxActivateEvent wxevent(wxEVT_ACTIVATE, didActivate , GetId());
231 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
232 wxevent.SetEventObject(this);
233 HandleWindowEvent(wxevent);
234 }
235
236 void wxNonOwnedWindow::HandleResized( double timestampsec )
237 {
238 wxSizeEvent wxevent( GetSize() , GetId());
239 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
240 wxevent.SetEventObject( this );
241 HandleWindowEvent(wxevent);
242 // we have to inform some controls that have to reset things
243 // relative to the toplevel window (e.g. OpenGL buffers)
244 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
245 }
246
247 void wxNonOwnedWindow::HandleResizing( double timestampsec, wxRect* rect )
248 {
249 wxRect r = *rect ;
250
251 // this is a EVT_SIZING not a EVT_SIZE type !
252 wxSizeEvent wxevent( r , GetId() ) ;
253 wxevent.SetEventObject( this ) ;
254 if ( HandleWindowEvent(wxevent) )
255 r = wxevent.GetRect() ;
256
257 if ( GetMaxWidth() != -1 && r.GetWidth() > GetMaxWidth() )
258 r.SetWidth( GetMaxWidth() ) ;
259 if ( GetMaxHeight() != -1 && r.GetHeight() > GetMaxHeight() )
260 r.SetHeight( GetMaxHeight() ) ;
261 if ( GetMinWidth() != -1 && r.GetWidth() < GetMinWidth() )
262 r.SetWidth( GetMinWidth() ) ;
263 if ( GetMinHeight() != -1 && r.GetHeight() < GetMinHeight() )
264 r.SetHeight( GetMinHeight() ) ;
265
266 *rect = r;
267 // TODO actuall this is too early, in case the window extents are adjusted
268 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
269 }
270
271 void wxNonOwnedWindow::HandleMoved( double timestampsec )
272 {
273 wxMoveEvent wxevent( GetPosition() , GetId());
274 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
275 wxevent.SetEventObject( this );
276 HandleWindowEvent(wxevent);
277 }
278
279 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp)
280 {
281 if (s_macDeactivateWindow)
282 {
283 wxLogTrace(TRACE_ACTIVATE,
284 wxT("Doing delayed deactivation of %p"),
285 s_macDeactivateWindow);
286
287 s_macDeactivateWindow->MacActivate(timestamp, false);
288 }
289 }
290
291 void wxNonOwnedWindow::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
292 {
293 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
294
295 if (s_macDeactivateWindow == this)
296 s_macDeactivateWindow = NULL;
297
298 MacDelayedDeactivation(timestamp);
299 }
300
301 bool wxNonOwnedWindow::Show(bool show)
302 {
303 if ( !wxWindow::Show(show) )
304 return false;
305
306 if ( m_nowpeer )
307 m_nowpeer->Show(show);
308
309 if ( show )
310 {
311 // because apps expect a size event to occur at this moment
312 wxSizeEvent event(GetSize() , m_windowId);
313 event.SetEventObject(this);
314 HandleWindowEvent(event);
315 }
316
317 return true ;
318 }
319
320 bool wxNonOwnedWindow::SetTransparent(wxByte alpha)
321 {
322 return m_nowpeer->SetTransparent(alpha);
323 }
324
325
326 bool wxNonOwnedWindow::CanSetTransparent()
327 {
328 return m_nowpeer->CanSetTransparent();
329 }
330
331
332 void wxNonOwnedWindow::SetExtraStyle(long exStyle)
333 {
334 if ( GetExtraStyle() == exStyle )
335 return ;
336
337 wxWindow::SetExtraStyle( exStyle ) ;
338
339 if ( m_nowpeer )
340 m_nowpeer->SetExtraStyle(exStyle);
341 }
342
343 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style)
344 {
345 if ( !wxWindow::SetBackgroundStyle(style) )
346 return false ;
347
348 return m_nowpeer->SetBackgroundStyle(style);
349 }
350
351 void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height)
352 {
353 if ( m_nowpeer == NULL )
354 return;
355
356 m_cachedClippedRectValid = false ;
357
358 m_nowpeer->MoveWindow(x, y, width, height);
359 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
360 }
361
362 void wxNonOwnedWindow::DoGetPosition( int *x, int *y ) const
363 {
364 if ( m_nowpeer == NULL )
365 return;
366
367 int x1,y1 ;
368 m_nowpeer->GetPosition(x1, y1);
369
370 if (x)
371 *x = x1 ;
372 if (y)
373 *y = y1 ;
374 }
375
376 void wxNonOwnedWindow::DoGetSize( int *width, int *height ) const
377 {
378 if ( m_nowpeer == NULL )
379 return;
380
381 int w,h;
382
383 m_nowpeer->GetSize(w, h);
384
385 if (width)
386 *width = w ;
387 if (height)
388 *height = h ;
389 }
390
391 void wxNonOwnedWindow::DoGetClientSize( int *width, int *height ) const
392 {
393 if ( m_nowpeer == NULL )
394 return;
395
396 int left, top, w, h;
397 m_nowpeer->GetContentArea(left, top, w, h);
398
399 if (width)
400 *width = w ;
401 if (height)
402 *height = h ;
403 }
404
405
406 void wxNonOwnedWindow::Update()
407 {
408 m_nowpeer->Update();
409 }
410
411 WXWindow wxNonOwnedWindow::GetWXWindow() const
412 {
413 return m_nowpeer ? m_nowpeer->GetWXWindow() : NULL;
414 }
415
416 // ---------------------------------------------------------------------------
417 // Shape implementation
418 // ---------------------------------------------------------------------------
419
420
421 bool wxNonOwnedWindow::SetShape(const wxRegion& region)
422 {
423 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
424 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
425
426 // The empty region signifies that the shape
427 // should be removed from the window.
428 if ( region.IsEmpty() )
429 {
430 wxSize sz = GetClientSize();
431 wxRegion rgn(0, 0, sz.x, sz.y);
432 if ( rgn.IsEmpty() )
433 return false ;
434 else
435 return SetShape(rgn);
436 }
437
438 return m_nowpeer->SetShape(region);
439 }