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