1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/nonownedwnd_osx.cpp
3 // Purpose: implementation of wxNonOwnedWindow
4 // Author: Stefan Csomor
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 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
19 #include "wx/hashmap.h"
20 #include "wx/evtloop.h"
21 #include "wx/tooltip.h"
22 #include "wx/nonownedwnd.h"
24 #include "wx/osx/private.h"
25 #include "wx/settings.h"
28 #if wxUSE_SYSTEM_OPTIONS
29 #include "wx/sysopt.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // trace mask for activation tracing messages
37 #define TRACE_ACTIVATE "activation"
39 wxWindow
* g_MacLastWindow
= NULL
;
41 // unified title and toolbar constant - not in Tiger headers, so we duplicate it here
42 #define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
44 // ---------------------------------------------------------------------------
45 // wxWindowMac utility functions
46 // ---------------------------------------------------------------------------
48 WX_DECLARE_HASH_MAP(WXWindow
, wxNonOwnedWindowImpl
*, wxPointerHash
, wxPointerEqual
, MacWindowMap
);
50 static MacWindowMap wxWinMacWindowList
;
52 wxNonOwnedWindow
* wxNonOwnedWindow::GetFromWXWindow( WXWindow win
)
54 wxNonOwnedWindowImpl
* impl
= wxNonOwnedWindowImpl::FindFromWXWindow(win
);
56 return ( impl
!= NULL
? impl
->GetWXPeer() : NULL
) ;
59 wxNonOwnedWindowImpl
* wxNonOwnedWindowImpl::FindFromWXWindow (WXWindow window
)
61 MacWindowMap::iterator node
= wxWinMacWindowList
.find(window
);
63 return (node
== wxWinMacWindowList
.end()) ? NULL
: node
->second
;
66 void wxNonOwnedWindowImpl::RemoveAssociations( wxNonOwnedWindowImpl
* impl
)
68 MacWindowMap::iterator it
;
69 for ( it
= wxWinMacWindowList
.begin(); it
!= wxWinMacWindowList
.end(); ++it
)
71 if ( it
->second
== impl
)
73 wxWinMacWindowList
.erase(it
);
79 void wxNonOwnedWindowImpl::Associate( WXWindow window
, wxNonOwnedWindowImpl
*impl
)
81 // adding NULL WindowRef is (first) surely a result of an error and
83 wxCHECK_RET( window
!= (WXWindow
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
85 wxWinMacWindowList
[window
] = impl
;
88 // ----------------------------------------------------------------------------
89 // wxNonOwnedWindow creation
90 // ----------------------------------------------------------------------------
92 IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl
, wxObject
)
94 wxNonOwnedWindow
*wxNonOwnedWindow::s_macDeactivateWindow
= NULL
;
96 void wxNonOwnedWindow::Init()
99 m_isNativeWindowWrapper
= false;
102 bool wxNonOwnedWindow::Create(wxWindow
*parent
,
107 const wxString
& name
)
109 m_windowStyle
= style
;
113 m_windowId
= id
== -1 ? NewControlId() : id
;
114 m_windowStyle
= style
;
121 wxRect display
= wxGetClientDisplayRect() ;
123 if ( x
== wxDefaultPosition
.x
)
126 if ( y
== wxDefaultPosition
.y
)
129 int w
= WidthDefault(size
.x
);
130 int h
= HeightDefault(size
.y
);
132 m_nowpeer
= wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, parent
, wxPoint(x
,y
) , wxSize(w
,h
) , style
, GetExtraStyle(), name
);
133 wxNonOwnedWindowImpl::Associate( m_nowpeer
->GetWXWindow() , m_nowpeer
) ;
134 m_peer
= wxWidgetImpl::CreateContentView(this);
136 DoSetWindowVariant( m_windowVariant
) ;
138 wxWindowCreateEvent
event(this);
139 HandleWindowEvent(event
);
141 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
));
144 parent
->AddChild(this);
149 bool wxNonOwnedWindow::Create(wxWindow
*parent
, WXWindow nativeWindow
)
151 m_nowpeer
= wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, parent
, nativeWindow
);
152 m_isNativeWindowWrapper
= true;
153 wxNonOwnedWindowImpl::Associate( m_nowpeer
->GetWXWindow() , m_nowpeer
) ;
154 m_peer
= wxWidgetImpl::CreateContentView(this);
157 parent
->AddChild(this);
162 wxNonOwnedWindow::~wxNonOwnedWindow()
166 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer
) ;
173 // avoid dangling refs
174 if ( s_macDeactivateWindow
== this )
175 s_macDeactivateWindow
= NULL
;
178 bool wxNonOwnedWindow::Destroy()
182 return wxWindow::Destroy();
185 void wxNonOwnedWindow::WillBeDestroyed()
188 m_nowpeer
->WillBeDestroyed();
191 // ----------------------------------------------------------------------------
192 // wxNonOwnedWindow misc
193 // ----------------------------------------------------------------------------
195 bool wxNonOwnedWindow::OSXShowWithEffect(bool show
,
199 // Cocoa code needs to manage window visibility on its own and so calls
200 // wxWindow::Show() as needed but if we already changed the internal
201 // visibility flag here, Show() would do nothing, so avoid doing it
203 if ( !wxWindow::Show(show
) )
207 if ( effect
== wxSHOW_EFFECT_NONE
||
208 !m_nowpeer
|| !m_nowpeer
->ShowWithEffect(show
, effect
, timeout
) )
213 // as apps expect a size event to occur when the window is shown,
214 // generate one when it is shown with effect too
215 wxSizeEvent
event(GetSize(), m_windowId
);
216 event
.SetEventObject(this);
217 HandleWindowEvent(event
);
223 wxPoint
wxNonOwnedWindow::GetClientAreaOrigin() const
225 int left
, top
, width
, height
;
226 m_nowpeer
->GetContentArea(left
, top
, width
, height
);
227 return wxPoint(left
, top
);
230 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour
& c
)
232 if ( !wxWindow::SetBackgroundColour(c
) && m_hasBgCol
)
235 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM
)
238 return m_nowpeer
->SetBackgroundColour(c
);
243 void wxNonOwnedWindow::SetWindowStyleFlag(long flags
)
245 if (flags
== GetWindowStyleFlag())
248 wxWindow::SetWindowStyleFlag(flags
);
251 m_nowpeer
->SetWindowStyleFlag(flags
);
254 // Raise the window to the top of the Z order
255 void wxNonOwnedWindow::Raise()
260 // Lower the window to the bottom of the Z order
261 void wxNonOwnedWindow::Lower()
266 void wxNonOwnedWindow::HandleActivated( double timestampsec
, bool didActivate
)
268 MacActivate( (int) (timestampsec
* 1000) , didActivate
);
269 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, didActivate
, GetId());
270 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
271 wxevent
.SetEventObject(this);
272 HandleWindowEvent(wxevent
);
275 void wxNonOwnedWindow::HandleResized( double timestampsec
)
277 wxSizeEvent
wxevent( GetSize() , GetId());
278 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
279 wxevent
.SetEventObject( this );
280 HandleWindowEvent(wxevent
);
281 // we have to inform some controls that have to reset things
282 // relative to the toplevel window (e.g. OpenGL buffers)
283 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
286 void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec
), wxRect
* rect
)
290 // this is a EVT_SIZING not a EVT_SIZE type !
291 wxSizeEvent
wxevent( r
, GetId() ) ;
292 wxevent
.SetEventObject( this ) ;
293 if ( HandleWindowEvent(wxevent
) )
294 r
= wxevent
.GetRect() ;
296 if ( GetMaxWidth() != -1 && r
.GetWidth() > GetMaxWidth() )
297 r
.SetWidth( GetMaxWidth() ) ;
298 if ( GetMaxHeight() != -1 && r
.GetHeight() > GetMaxHeight() )
299 r
.SetHeight( GetMaxHeight() ) ;
300 if ( GetMinWidth() != -1 && r
.GetWidth() < GetMinWidth() )
301 r
.SetWidth( GetMinWidth() ) ;
302 if ( GetMinHeight() != -1 && r
.GetHeight() < GetMinHeight() )
303 r
.SetHeight( GetMinHeight() ) ;
306 // TODO actuall this is too early, in case the window extents are adjusted
307 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
310 void wxNonOwnedWindow::HandleMoved( double timestampsec
)
312 wxMoveEvent
wxevent( GetPosition() , GetId());
313 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
314 wxevent
.SetEventObject( this );
315 HandleWindowEvent(wxevent
);
318 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp
)
320 if (s_macDeactivateWindow
)
322 wxLogTrace(TRACE_ACTIVATE
,
323 wxT("Doing delayed deactivation of %p"),
324 s_macDeactivateWindow
);
326 s_macDeactivateWindow
->MacActivate(timestamp
, false);
330 void wxNonOwnedWindow::MacActivate( long timestamp
, bool WXUNUSED(inIsActivating
) )
332 wxLogTrace(TRACE_ACTIVATE
, wxT("TopLevel=%p::MacActivate"), this);
334 if (s_macDeactivateWindow
== this)
335 s_macDeactivateWindow
= NULL
;
337 MacDelayedDeactivation(timestamp
);
340 bool wxNonOwnedWindow::Show(bool show
)
342 if ( !wxWindow::Show(show
) )
346 m_nowpeer
->Show(show
);
350 // because apps expect a size event to occur at this moment
351 wxSizeEvent
event(GetSize() , m_windowId
);
352 event
.SetEventObject(this);
353 HandleWindowEvent(event
);
359 bool wxNonOwnedWindow::SetTransparent(wxByte alpha
)
361 return m_nowpeer
->SetTransparent(alpha
);
365 bool wxNonOwnedWindow::CanSetTransparent()
367 return m_nowpeer
->CanSetTransparent();
371 void wxNonOwnedWindow::SetExtraStyle(long exStyle
)
373 if ( GetExtraStyle() == exStyle
)
376 wxWindow::SetExtraStyle( exStyle
) ;
379 m_nowpeer
->SetExtraStyle(exStyle
);
382 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style
)
384 if ( !wxWindow::SetBackgroundStyle(style
) )
387 return m_nowpeer
? m_nowpeer
->SetBackgroundStyle(style
) : true;
390 void wxNonOwnedWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
392 if ( m_nowpeer
== NULL
)
395 m_cachedClippedRectValid
= false ;
397 m_nowpeer
->MoveWindow(x
, y
, width
, height
);
398 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
401 void wxNonOwnedWindow::DoGetPosition( int *x
, int *y
) const
403 if ( m_nowpeer
== NULL
)
407 m_nowpeer
->GetPosition(x1
, y1
);
415 void wxNonOwnedWindow::DoGetSize( int *width
, int *height
) const
417 if ( m_nowpeer
== NULL
)
422 m_nowpeer
->GetSize(w
, h
);
430 void wxNonOwnedWindow::DoGetClientSize( int *width
, int *height
) const
432 if ( m_nowpeer
== NULL
)
436 m_nowpeer
->GetContentArea(left
, top
, w
, h
);
445 void wxNonOwnedWindow::Update()
450 WXWindow
wxNonOwnedWindow::GetWXWindow() const
452 return m_nowpeer
? m_nowpeer
->GetWXWindow() : NULL
;
455 // ---------------------------------------------------------------------------
456 // Shape implementation
457 // ---------------------------------------------------------------------------
460 bool wxNonOwnedWindow::DoSetShape(const wxRegion
& region
)
462 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
463 wxT("Shaped windows must be created with the wxFRAME_SHAPED style."));
467 // The empty region signifies that the shape
468 // should be removed from the window.
469 if ( region
.IsEmpty() )
471 wxSize sz
= GetClientSize();
472 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
476 return DoSetShape(rgn
);
479 return m_nowpeer
->SetShape(region
);