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
) ;
172 // avoid dangling refs
173 if ( s_macDeactivateWindow
== this )
174 s_macDeactivateWindow
= NULL
;
177 bool wxNonOwnedWindow::Destroy()
181 return wxWindow::Destroy();
184 void wxNonOwnedWindow::WillBeDestroyed()
187 m_nowpeer
->WillBeDestroyed();
190 // ----------------------------------------------------------------------------
191 // wxNonOwnedWindow misc
192 // ----------------------------------------------------------------------------
194 bool wxNonOwnedWindow::OSXShowWithEffect(bool show
,
198 // Cocoa code needs to manage window visibility on its own and so calls
199 // wxWindow::Show() as needed but if we already changed the internal
200 // visibility flag here, Show() would do nothing, so avoid doing it
202 if ( !wxWindow::Show(show
) )
206 if ( effect
== wxSHOW_EFFECT_NONE
||
207 !m_nowpeer
|| !m_nowpeer
->ShowWithEffect(show
, effect
, timeout
) )
212 // as apps expect a size event to occur when the window is shown,
213 // generate one when it is shown with effect too
214 wxSizeEvent
event(GetSize(), m_windowId
);
215 event
.SetEventObject(this);
216 HandleWindowEvent(event
);
222 wxPoint
wxNonOwnedWindow::GetClientAreaOrigin() const
224 int left
, top
, width
, height
;
225 m_nowpeer
->GetContentArea(left
, top
, width
, height
);
226 return wxPoint(left
, top
);
229 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour
& c
)
231 if ( !wxWindow::SetBackgroundColour(c
) && m_hasBgCol
)
234 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM
)
237 return m_nowpeer
->SetBackgroundColour(c
);
242 void wxNonOwnedWindow::SetWindowStyleFlag(long flags
)
244 if (flags
== GetWindowStyleFlag())
247 wxWindow::SetWindowStyleFlag(flags
);
250 m_nowpeer
->SetWindowStyleFlag(flags
);
253 // Raise the window to the top of the Z order
254 void wxNonOwnedWindow::Raise()
259 // Lower the window to the bottom of the Z order
260 void wxNonOwnedWindow::Lower()
265 void wxNonOwnedWindow::HandleActivated( double timestampsec
, bool didActivate
)
267 MacActivate( (int) (timestampsec
* 1000) , didActivate
);
268 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, didActivate
, GetId());
269 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
270 wxevent
.SetEventObject(this);
271 HandleWindowEvent(wxevent
);
274 void wxNonOwnedWindow::HandleResized( double timestampsec
)
276 wxSizeEvent
wxevent( GetSize() , GetId());
277 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
278 wxevent
.SetEventObject( this );
279 HandleWindowEvent(wxevent
);
280 // we have to inform some controls that have to reset things
281 // relative to the toplevel window (e.g. OpenGL buffers)
282 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
285 void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec
), wxRect
* rect
)
289 // this is a EVT_SIZING not a EVT_SIZE type !
290 wxSizeEvent
wxevent( r
, GetId() ) ;
291 wxevent
.SetEventObject( this ) ;
292 if ( HandleWindowEvent(wxevent
) )
293 r
= wxevent
.GetRect() ;
295 if ( GetMaxWidth() != -1 && r
.GetWidth() > GetMaxWidth() )
296 r
.SetWidth( GetMaxWidth() ) ;
297 if ( GetMaxHeight() != -1 && r
.GetHeight() > GetMaxHeight() )
298 r
.SetHeight( GetMaxHeight() ) ;
299 if ( GetMinWidth() != -1 && r
.GetWidth() < GetMinWidth() )
300 r
.SetWidth( GetMinWidth() ) ;
301 if ( GetMinHeight() != -1 && r
.GetHeight() < GetMinHeight() )
302 r
.SetHeight( GetMinHeight() ) ;
305 // TODO actuall this is too early, in case the window extents are adjusted
306 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
309 void wxNonOwnedWindow::HandleMoved( double timestampsec
)
311 wxMoveEvent
wxevent( GetPosition() , GetId());
312 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
313 wxevent
.SetEventObject( this );
314 HandleWindowEvent(wxevent
);
317 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp
)
319 if (s_macDeactivateWindow
)
321 wxLogTrace(TRACE_ACTIVATE
,
322 wxT("Doing delayed deactivation of %p"),
323 s_macDeactivateWindow
);
325 s_macDeactivateWindow
->MacActivate(timestamp
, false);
329 void wxNonOwnedWindow::MacActivate( long timestamp
, bool WXUNUSED(inIsActivating
) )
331 wxLogTrace(TRACE_ACTIVATE
, wxT("TopLevel=%p::MacActivate"), this);
333 if (s_macDeactivateWindow
== this)
334 s_macDeactivateWindow
= NULL
;
336 MacDelayedDeactivation(timestamp
);
339 bool wxNonOwnedWindow::Show(bool show
)
341 if ( !wxWindow::Show(show
) )
345 m_nowpeer
->Show(show
);
349 // because apps expect a size event to occur at this moment
350 wxSizeEvent
event(GetSize() , m_windowId
);
351 event
.SetEventObject(this);
352 HandleWindowEvent(event
);
358 bool wxNonOwnedWindow::SetTransparent(wxByte alpha
)
360 return m_nowpeer
->SetTransparent(alpha
);
364 bool wxNonOwnedWindow::CanSetTransparent()
366 return m_nowpeer
->CanSetTransparent();
370 void wxNonOwnedWindow::SetExtraStyle(long exStyle
)
372 if ( GetExtraStyle() == exStyle
)
375 wxWindow::SetExtraStyle( exStyle
) ;
378 m_nowpeer
->SetExtraStyle(exStyle
);
381 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style
)
383 if ( !wxWindow::SetBackgroundStyle(style
) )
386 return m_nowpeer
? m_nowpeer
->SetBackgroundStyle(style
) : true;
389 void wxNonOwnedWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
391 if ( m_nowpeer
== NULL
)
394 m_cachedClippedRectValid
= false ;
396 m_nowpeer
->MoveWindow(x
, y
, width
, height
);
397 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
400 void wxNonOwnedWindow::DoGetPosition( int *x
, int *y
) const
402 if ( m_nowpeer
== NULL
)
406 m_nowpeer
->GetPosition(x1
, y1
);
414 void wxNonOwnedWindow::DoGetSize( int *width
, int *height
) const
416 if ( m_nowpeer
== NULL
)
421 m_nowpeer
->GetSize(w
, h
);
429 void wxNonOwnedWindow::DoGetClientSize( int *width
, int *height
) const
431 if ( m_nowpeer
== NULL
)
435 m_nowpeer
->GetContentArea(left
, top
, w
, h
);
444 void wxNonOwnedWindow::Update()
449 WXWindow
wxNonOwnedWindow::GetWXWindow() const
451 return m_nowpeer
? m_nowpeer
->GetWXWindow() : NULL
;
454 // ---------------------------------------------------------------------------
455 // Shape implementation
456 // ---------------------------------------------------------------------------
459 bool wxNonOwnedWindow::DoSetShape(const wxRegion
& region
)
461 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
462 wxT("Shaped windows must be created with the wxFRAME_SHAPED style."));
466 // The empty region signifies that the shape
467 // should be removed from the window.
468 if ( region
.IsEmpty() )
470 wxSize sz
= GetClientSize();
471 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
475 return DoSetShape(rgn
);
478 return m_nowpeer
->SetShape(region
);