]> git.saurik.com Git - wxWidgets.git/blame - src/osx/nonownedwnd_osx.cpp
fixing reentrancy problem under iOS 5
[wxWidgets.git] / src / osx / nonownedwnd_osx.cpp
CommitLineData
3ccc5735
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/nonownedwnd_osx.cpp
3// Purpose: implementation of wxNonOwnedWindow
4// Author: Stefan Csomor
5// Created: 2008-03-24
b5b208a1 6// RCS-ID: $Id$
3ccc5735
SC
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"
83ed867e 16 #include "wx/dcmemory.h"
e1f833da 17 #include "wx/log.h"
3ccc5735
SC
18#endif // WX_PRECOMP
19
20#include "wx/hashmap.h"
21#include "wx/evtloop.h"
22#include "wx/tooltip.h"
23#include "wx/nonownedwnd.h"
24
25#include "wx/osx/private.h"
26#include "wx/settings.h"
27#include "wx/frame.h"
28
29#if wxUSE_SYSTEM_OPTIONS
30 #include "wx/sysopt.h"
31#endif
32
33// ----------------------------------------------------------------------------
34// constants
35// ----------------------------------------------------------------------------
36
37// trace mask for activation tracing messages
38#define TRACE_ACTIVATE "activation"
39
40wxWindow* g_MacLastWindow = NULL ;
41
42// unified title and toolbar constant - not in Tiger headers, so we duplicate it here
43#define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
44
45// ---------------------------------------------------------------------------
46// wxWindowMac utility functions
47// ---------------------------------------------------------------------------
48
638b3cd7 49WX_DECLARE_HASH_MAP(WXWindow, wxNonOwnedWindowImpl*, wxPointerHash, wxPointerEqual, MacWindowMap);
3ccc5735
SC
50
51static MacWindowMap wxWinMacWindowList;
52
638b3cd7 53wxNonOwnedWindow* wxNonOwnedWindow::GetFromWXWindow( WXWindow win )
3ccc5735 54{
638b3cd7 55 wxNonOwnedWindowImpl* impl = wxNonOwnedWindowImpl::FindFromWXWindow(win);
ce00f59b 56
638b3cd7 57 return ( impl != NULL ? impl->GetWXPeer() : NULL ) ;
3ccc5735
SC
58}
59
638b3cd7 60wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::FindFromWXWindow (WXWindow window)
3ccc5735 61{
638b3cd7 62 MacWindowMap::iterator node = wxWinMacWindowList.find(window);
ce00f59b 63
638b3cd7 64 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
3ccc5735
SC
65}
66
638b3cd7 67void wxNonOwnedWindowImpl::RemoveAssociations( wxNonOwnedWindowImpl* impl)
3ccc5735
SC
68{
69 MacWindowMap::iterator it;
70 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
71 {
638b3cd7 72 if ( it->second == impl )
3ccc5735
SC
73 {
74 wxWinMacWindowList.erase(it);
75 break;
76 }
77 }
78}
79
638b3cd7 80void wxNonOwnedWindowImpl::Associate( WXWindow window, wxNonOwnedWindowImpl *impl )
3ccc5735 81{
638b3cd7
SC
82 // adding NULL WindowRef is (first) surely a result of an error and
83 // nothing else :-)
84 wxCHECK_RET( window != (WXWindow) NULL, wxT("attempt to add a NULL WindowRef to window list") );
ce00f59b 85
638b3cd7 86 wxWinMacWindowList[window] = impl;
3ccc5735
SC
87}
88
89// ----------------------------------------------------------------------------
90// wxNonOwnedWindow creation
91// ----------------------------------------------------------------------------
92
93IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl , wxObject )
94
95wxNonOwnedWindow *wxNonOwnedWindow::s_macDeactivateWindow = NULL;
96
97void wxNonOwnedWindow::Init()
98{
99 m_nowpeer = NULL;
17e2694c 100 m_isNativeWindowWrapper = false;
3ccc5735
SC
101}
102
103bool wxNonOwnedWindow::Create(wxWindow *parent,
104 wxWindowID id,
e08b4823
VZ
105 const wxPoint& posOrig,
106 const wxSize& sizeOrig,
3ccc5735
SC
107 long style,
108 const wxString& name)
109{
3ccc5735
SC
110 m_windowStyle = style;
111
112 SetName( name );
113
114 m_windowId = id == -1 ? NewControlId() : id;
115 m_windowStyle = style;
116 m_isShown = false;
117
e08b4823 118 // use the appropriate defaults for the position and size if necessary
e08b4823
VZ
119 wxSize size(sizeOrig);
120 if ( !size.IsFullySpecified() )
121 size.SetDefaults(wxTopLevelWindow::GetDefaultSize());
3ccc5735 122
0d65f8d2
VZ
123 wxPoint pos(posOrig);
124 if ( !pos.IsFullySpecified() )
125 {
126 wxRect rectWin = wxRect(size).CentreIn(wxGetClientDisplayRect());
127
128 // The size of the window is often not really known yet, TLWs are often
129 // created with some small initial size and later are fitted to contain
130 // their children so centering the window will show it too much to the
131 // right and bottom, adjust for it by putting it more to the left and
132 // center.
133 rectWin.x /= 2;
134 rectWin.y /= 2;
135
136 pos.SetDefaults(rectWin.GetPosition());
137 }
138
e08b4823
VZ
139 // create frame.
140 m_nowpeer = wxNonOwnedWindowImpl::CreateNonOwnedWindow
141 (
142 this, parent,
143 pos , size,
144 style, GetExtraStyle(),
145 name
146 );
638b3cd7 147 wxNonOwnedWindowImpl::Associate( m_nowpeer->GetWXWindow() , m_nowpeer ) ;
22756322 148 SetPeer(wxWidgetImpl::CreateContentView(this));
3ccc5735
SC
149
150 DoSetWindowVariant( m_windowVariant ) ;
151
152 wxWindowCreateEvent event(this);
153 HandleWindowEvent(event);
154
155 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ));
156
157 if ( parent )
158 parent->AddChild(this);
159
160 return true;
161}
162
17e2694c
SC
163bool wxNonOwnedWindow::Create(wxWindow *parent, WXWindow nativeWindow)
164{
d623e8b1
SC
165 if ( parent )
166 parent->AddChild(this);
ce00f59b 167
d623e8b1 168 SubclassWin(nativeWindow);
ce00f59b 169
d623e8b1
SC
170 return true;
171}
172
173void wxNonOwnedWindow::SubclassWin(WXWindow nativeWindow)
174{
175 wxASSERT_MSG( !m_isNativeWindowWrapper, wxT("subclassing window twice?") );
176 wxASSERT_MSG( m_nowpeer == NULL, wxT("window already was created") );
177
178 m_nowpeer = wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, GetParent(), nativeWindow );
17e2694c 179 m_isNativeWindowWrapper = true;
638b3cd7 180 wxNonOwnedWindowImpl::Associate( m_nowpeer->GetWXWindow() , m_nowpeer ) ;
22756322 181 SetPeer(wxWidgetImpl::CreateContentView(this));
d623e8b1 182}
17e2694c 183
d623e8b1
SC
184void wxNonOwnedWindow::UnsubclassWin()
185{
186 wxASSERT_MSG( m_isNativeWindowWrapper, wxT("window was not subclassed") );
187
188 if ( GetParent() )
189 GetParent()->RemoveChild(this);
ce00f59b
VZ
190
191 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer) ;
d623e8b1 192 wxDELETE(m_nowpeer);
22756322 193 SetPeer(NULL);
77eb08cc 194 m_isNativeWindowWrapper = false;
17e2694c
SC
195}
196
d623e8b1 197
3ccc5735
SC
198wxNonOwnedWindow::~wxNonOwnedWindow()
199{
c6212a0c 200 SendDestroyEvent();
03647350 201
638b3cd7 202 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer) ;
03647350 203
524c47aa 204 DestroyChildren();
03647350 205
5276b0a5 206 wxDELETE(m_nowpeer);
3ccc5735
SC
207
208 // avoid dangling refs
209 if ( s_macDeactivateWindow == this )
210 s_macDeactivateWindow = NULL;
211}
212
e2758e21 213bool wxNonOwnedWindow::Destroy()
0aaa6ace 214{
e2758e21 215 WillBeDestroyed();
ce00f59b 216
e2758e21
KO
217 return wxWindow::Destroy();
218}
219
220void wxNonOwnedWindow::WillBeDestroyed()
221{
0aaa6ace
SC
222 if ( m_nowpeer )
223 m_nowpeer->WillBeDestroyed();
224}
225
3ccc5735
SC
226// ----------------------------------------------------------------------------
227// wxNonOwnedWindow misc
228// ----------------------------------------------------------------------------
229
ab9a0b84
VZ
230bool wxNonOwnedWindow::OSXShowWithEffect(bool show,
231 wxShowEffect effect,
232 unsigned timeout)
03647350 233{
ab9a0b84
VZ
234 // Cocoa code needs to manage window visibility on its own and so calls
235 // wxWindow::Show() as needed but if we already changed the internal
236 // visibility flag here, Show() would do nothing, so avoid doing it
237#if wxOSX_USE_CARBON
238 if ( !wxWindow::Show(show) )
3ccc5735 239 return false;
ab9a0b84 240#endif // Carbon
3ccc5735 241
ab9a0b84
VZ
242 if ( effect == wxSHOW_EFFECT_NONE ||
243 !m_nowpeer || !m_nowpeer->ShowWithEffect(show, effect, timeout) )
244 return Show(show);
3ccc5735 245
ab9a0b84
VZ
246 if ( show )
247 {
248 // as apps expect a size event to occur when the window is shown,
249 // generate one when it is shown with effect too
250 wxSizeEvent event(GetSize(), m_windowId);
251 event.SetEventObject(this);
252 HandleWindowEvent(event);
253 }
3ccc5735 254
ab9a0b84 255 return true;
3ccc5735
SC
256}
257
258wxPoint wxNonOwnedWindow::GetClientAreaOrigin() const
259{
260 int left, top, width, height;
261 m_nowpeer->GetContentArea(left, top, width, height);
262 return wxPoint(left, top);
263}
264
265bool wxNonOwnedWindow::SetBackgroundColour(const wxColour& c )
03647350 266{
3ccc5735
SC
267 if ( !wxWindow::SetBackgroundColour(c) && m_hasBgCol )
268 return false ;
03647350 269
3ccc5735
SC
270 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM )
271 {
524c47aa
SC
272 if ( m_nowpeer )
273 return m_nowpeer->SetBackgroundColour(c);
3ccc5735
SC
274 }
275 return true;
03647350 276}
3ccc5735 277
b6dc21e7
KO
278void wxNonOwnedWindow::SetWindowStyleFlag(long flags)
279{
280 if (flags == GetWindowStyleFlag())
281 return;
ce00f59b 282
b6dc21e7 283 wxWindow::SetWindowStyleFlag(flags);
ce00f59b 284
b6dc21e7
KO
285 if (m_nowpeer)
286 m_nowpeer->SetWindowStyleFlag(flags);
287}
288
3ccc5735
SC
289// Raise the window to the top of the Z order
290void wxNonOwnedWindow::Raise()
291{
292 m_nowpeer->Raise();
293}
294
295// Lower the window to the bottom of the Z order
296void wxNonOwnedWindow::Lower()
297{
298 m_nowpeer->Lower();
299}
300
301void wxNonOwnedWindow::HandleActivated( double timestampsec, bool didActivate )
302{
303 MacActivate( (int) (timestampsec * 1000) , didActivate);
304 wxActivateEvent wxevent(wxEVT_ACTIVATE, didActivate , GetId());
305 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
306 wxevent.SetEventObject(this);
307 HandleWindowEvent(wxevent);
308}
309
310void wxNonOwnedWindow::HandleResized( double timestampsec )
311{
312 wxSizeEvent wxevent( GetSize() , GetId());
313 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
314 wxevent.SetEventObject( this );
315 HandleWindowEvent(wxevent);
316 // we have to inform some controls that have to reset things
317 // relative to the toplevel window (e.g. OpenGL buffers)
318 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
319}
320
0faf03bf 321void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec), wxRect* rect )
3ccc5735
SC
322{
323 wxRect r = *rect ;
324
325 // this is a EVT_SIZING not a EVT_SIZE type !
326 wxSizeEvent wxevent( r , GetId() ) ;
327 wxevent.SetEventObject( this ) ;
328 if ( HandleWindowEvent(wxevent) )
329 r = wxevent.GetRect() ;
330
331 if ( GetMaxWidth() != -1 && r.GetWidth() > GetMaxWidth() )
332 r.SetWidth( GetMaxWidth() ) ;
333 if ( GetMaxHeight() != -1 && r.GetHeight() > GetMaxHeight() )
334 r.SetHeight( GetMaxHeight() ) ;
335 if ( GetMinWidth() != -1 && r.GetWidth() < GetMinWidth() )
336 r.SetWidth( GetMinWidth() ) ;
337 if ( GetMinHeight() != -1 && r.GetHeight() < GetMinHeight() )
338 r.SetHeight( GetMinHeight() ) ;
339
340 *rect = r;
341 // TODO actuall this is too early, in case the window extents are adjusted
342 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
343}
344
345void wxNonOwnedWindow::HandleMoved( double timestampsec )
346{
347 wxMoveEvent wxevent( GetPosition() , GetId());
348 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
349 wxevent.SetEventObject( this );
350 HandleWindowEvent(wxevent);
351}
352
353void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp)
354{
355 if (s_macDeactivateWindow)
356 {
357 wxLogTrace(TRACE_ACTIVATE,
358 wxT("Doing delayed deactivation of %p"),
359 s_macDeactivateWindow);
360
361 s_macDeactivateWindow->MacActivate(timestamp, false);
362 }
363}
364
365void wxNonOwnedWindow::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
366{
367 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
368
369 if (s_macDeactivateWindow == this)
370 s_macDeactivateWindow = NULL;
371
372 MacDelayedDeactivation(timestamp);
373}
374
375bool wxNonOwnedWindow::Show(bool show)
376{
377 if ( !wxWindow::Show(show) )
378 return false;
379
380 if ( m_nowpeer )
381 m_nowpeer->Show(show);
03647350 382
3ccc5735
SC
383 if ( show )
384 {
385 // because apps expect a size event to occur at this moment
386 wxSizeEvent event(GetSize() , m_windowId);
387 event.SetEventObject(this);
388 HandleWindowEvent(event);
389 }
03647350 390
3ccc5735
SC
391 return true ;
392}
393
394bool wxNonOwnedWindow::SetTransparent(wxByte alpha)
395{
396 return m_nowpeer->SetTransparent(alpha);
397}
398
399
400bool wxNonOwnedWindow::CanSetTransparent()
401{
402 return m_nowpeer->CanSetTransparent();
403}
404
405
406void wxNonOwnedWindow::SetExtraStyle(long exStyle)
407{
408 if ( GetExtraStyle() == exStyle )
409 return ;
410
411 wxWindow::SetExtraStyle( exStyle ) ;
412
413 if ( m_nowpeer )
414 m_nowpeer->SetExtraStyle(exStyle);
415}
416
417bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style)
418{
419 if ( !wxWindow::SetBackgroundStyle(style) )
420 return false ;
03647350 421
4d949e7f 422 return m_nowpeer ? m_nowpeer->SetBackgroundStyle(style) : true;
3ccc5735
SC
423}
424
425void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height)
426{
b8afff01
SC
427 if ( m_nowpeer == NULL )
428 return;
429
3ccc5735
SC
430 m_cachedClippedRectValid = false ;
431
432 m_nowpeer->MoveWindow(x, y, width, height);
433 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
434}
435
436void wxNonOwnedWindow::DoGetPosition( int *x, int *y ) const
437{
b8afff01
SC
438 if ( m_nowpeer == NULL )
439 return;
440
3ccc5735
SC
441 int x1,y1 ;
442 m_nowpeer->GetPosition(x1, y1);
443
444 if (x)
445 *x = x1 ;
446 if (y)
447 *y = y1 ;
448}
449
450void wxNonOwnedWindow::DoGetSize( int *width, int *height ) const
451{
b8afff01
SC
452 if ( m_nowpeer == NULL )
453 return;
03647350 454
3ccc5735 455 int w,h;
03647350 456
3ccc5735
SC
457 m_nowpeer->GetSize(w, h);
458
459 if (width)
460 *width = w ;
461 if (height)
462 *height = h ;
463}
464
465void wxNonOwnedWindow::DoGetClientSize( int *width, int *height ) const
466{
b8afff01
SC
467 if ( m_nowpeer == NULL )
468 return;
469
3ccc5735 470 int left, top, w, h;
bacea497
SC
471 // under iphone with a translucent status bar the m_nowpeer returns the
472 // inner area, while the content area extends under the translucent
473 // status bar, therefore we use the content view's area
5397ea53 474#ifdef __WXOSX_IPHONE__
22756322 475 GetPeer()->GetContentArea(left, top, w, h);
5397ea53 476#else
3ccc5735 477 m_nowpeer->GetContentArea(left, top, w, h);
5397ea53
SC
478#endif
479
3ccc5735
SC
480 if (width)
481 *width = w ;
482 if (height)
483 *height = h ;
484}
485
486
487void wxNonOwnedWindow::Update()
488{
489 m_nowpeer->Update();
490}
491
492WXWindow wxNonOwnedWindow::GetWXWindow() const
493{
494 return m_nowpeer ? m_nowpeer->GetWXWindow() : NULL;
495}
496
497// ---------------------------------------------------------------------------
498// Shape implementation
499// ---------------------------------------------------------------------------
500
46ea442c 501bool wxNonOwnedWindow::DoClearShape()
3ccc5735 502{
46ea442c
VZ
503 m_shape.Clear();
504
505 wxSize sz = GetClientSize();
506 wxRegion region(0, 0, sz.x, sz.y);
507
508 return m_nowpeer->SetShape(region);
509}
3ccc5735 510
46ea442c
VZ
511bool wxNonOwnedWindow::DoSetRegionShape(const wxRegion& region)
512{
b61b8371 513 m_shape = region;
ce00f59b 514
46ea442c
VZ
515 return m_nowpeer->SetShape(region);
516}
517
518#if wxUSE_GRAPHICS_CONTEXT
519
520#include "wx/scopedptr.h"
521
522bool wxNonOwnedWindow::DoSetPathShape(const wxGraphicsPath& path)
523{
524 m_shapePath = path;
525
526 // Convert the path to wxRegion by rendering the path on a window-sized
527 // bitmap, creating a mask from it and finally creating the region from
528 // this mask.
529 wxBitmap bmp(GetSize());
530
3ccc5735 531 {
46ea442c
VZ
532 wxMemoryDC dc(bmp);
533 dc.SetBackground(*wxBLACK);
534 dc.Clear();
535
536 wxScopedPtr<wxGraphicsContext> context(wxGraphicsContext::Create(dc));
537 context->SetBrush(*wxWHITE);
538 context->FillPath(m_shapePath);
3ccc5735
SC
539 }
540
46ea442c
VZ
541 bmp.SetMask(new wxMask(bmp, *wxBLACK));
542
543 return DoSetRegionShape(wxRegion(bmp));
3ccc5735 544}
46ea442c
VZ
545
546#endif // wxUSE_GRAPHICS_CONTEXT