1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/nonownedwnd_osx.cpp
3 // Purpose: implementation of wxNonOwnedWindow
4 // Author: Stefan Csomor
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
,
104 const wxPoint
& posOrig
,
105 const wxSize
& sizeOrig
,
107 const wxString
& name
)
109 m_windowStyle
= style
;
113 m_windowId
= id
== -1 ? NewControlId() : id
;
114 m_windowStyle
= style
;
117 // use the appropriate defaults for the position and size if necessary
118 wxPoint
pos(posOrig
);
119 if ( !pos
.IsFullySpecified() )
120 pos
.SetDefaults(wxGetClientDisplayRect().GetPosition());
122 wxSize
size(sizeOrig
);
123 if ( !size
.IsFullySpecified() )
124 size
.SetDefaults(wxTopLevelWindow::GetDefaultSize());
127 m_nowpeer
= wxNonOwnedWindowImpl::CreateNonOwnedWindow
131 style
, GetExtraStyle(),
134 wxNonOwnedWindowImpl::Associate( m_nowpeer
->GetWXWindow() , m_nowpeer
) ;
135 m_peer
= wxWidgetImpl::CreateContentView(this);
137 DoSetWindowVariant( m_windowVariant
) ;
139 wxWindowCreateEvent
event(this);
140 HandleWindowEvent(event
);
142 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
));
145 parent
->AddChild(this);
150 bool wxNonOwnedWindow::Create(wxWindow
*parent
, WXWindow nativeWindow
)
153 parent
->AddChild(this);
155 SubclassWin(nativeWindow
);
160 void wxNonOwnedWindow::SubclassWin(WXWindow nativeWindow
)
162 wxASSERT_MSG( !m_isNativeWindowWrapper
, wxT("subclassing window twice?") );
163 wxASSERT_MSG( m_nowpeer
== NULL
, wxT("window already was created") );
165 m_nowpeer
= wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, GetParent(), nativeWindow
);
166 m_isNativeWindowWrapper
= true;
167 wxNonOwnedWindowImpl::Associate( m_nowpeer
->GetWXWindow() , m_nowpeer
) ;
168 m_peer
= wxWidgetImpl::CreateContentView(this);
171 void wxNonOwnedWindow::UnsubclassWin()
173 wxASSERT_MSG( m_isNativeWindowWrapper
, wxT("window was not subclassed") );
176 GetParent()->RemoveChild(this);
178 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer
) ;
181 m_isNativeWindowWrapper
= false;
185 wxNonOwnedWindow::~wxNonOwnedWindow()
189 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer
) ;
195 // avoid dangling refs
196 if ( s_macDeactivateWindow
== this )
197 s_macDeactivateWindow
= NULL
;
200 bool wxNonOwnedWindow::Destroy()
204 return wxWindow::Destroy();
207 void wxNonOwnedWindow::WillBeDestroyed()
210 m_nowpeer
->WillBeDestroyed();
213 // ----------------------------------------------------------------------------
214 // wxNonOwnedWindow misc
215 // ----------------------------------------------------------------------------
217 bool wxNonOwnedWindow::OSXShowWithEffect(bool show
,
221 // Cocoa code needs to manage window visibility on its own and so calls
222 // wxWindow::Show() as needed but if we already changed the internal
223 // visibility flag here, Show() would do nothing, so avoid doing it
225 if ( !wxWindow::Show(show
) )
229 if ( effect
== wxSHOW_EFFECT_NONE
||
230 !m_nowpeer
|| !m_nowpeer
->ShowWithEffect(show
, effect
, timeout
) )
235 // as apps expect a size event to occur when the window is shown,
236 // generate one when it is shown with effect too
237 wxSizeEvent
event(GetSize(), m_windowId
);
238 event
.SetEventObject(this);
239 HandleWindowEvent(event
);
245 wxPoint
wxNonOwnedWindow::GetClientAreaOrigin() const
247 int left
, top
, width
, height
;
248 m_nowpeer
->GetContentArea(left
, top
, width
, height
);
249 return wxPoint(left
, top
);
252 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour
& c
)
254 if ( !wxWindow::SetBackgroundColour(c
) && m_hasBgCol
)
257 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM
)
260 return m_nowpeer
->SetBackgroundColour(c
);
265 void wxNonOwnedWindow::SetWindowStyleFlag(long flags
)
267 if (flags
== GetWindowStyleFlag())
270 wxWindow::SetWindowStyleFlag(flags
);
273 m_nowpeer
->SetWindowStyleFlag(flags
);
276 // Raise the window to the top of the Z order
277 void wxNonOwnedWindow::Raise()
282 // Lower the window to the bottom of the Z order
283 void wxNonOwnedWindow::Lower()
288 void wxNonOwnedWindow::HandleActivated( double timestampsec
, bool didActivate
)
290 MacActivate( (int) (timestampsec
* 1000) , didActivate
);
291 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, didActivate
, GetId());
292 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
293 wxevent
.SetEventObject(this);
294 HandleWindowEvent(wxevent
);
297 void wxNonOwnedWindow::HandleResized( double timestampsec
)
299 wxSizeEvent
wxevent( GetSize() , GetId());
300 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
301 wxevent
.SetEventObject( this );
302 HandleWindowEvent(wxevent
);
303 // we have to inform some controls that have to reset things
304 // relative to the toplevel window (e.g. OpenGL buffers)
305 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
308 void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec
), wxRect
* rect
)
312 // this is a EVT_SIZING not a EVT_SIZE type !
313 wxSizeEvent
wxevent( r
, GetId() ) ;
314 wxevent
.SetEventObject( this ) ;
315 if ( HandleWindowEvent(wxevent
) )
316 r
= wxevent
.GetRect() ;
318 if ( GetMaxWidth() != -1 && r
.GetWidth() > GetMaxWidth() )
319 r
.SetWidth( GetMaxWidth() ) ;
320 if ( GetMaxHeight() != -1 && r
.GetHeight() > GetMaxHeight() )
321 r
.SetHeight( GetMaxHeight() ) ;
322 if ( GetMinWidth() != -1 && r
.GetWidth() < GetMinWidth() )
323 r
.SetWidth( GetMinWidth() ) ;
324 if ( GetMinHeight() != -1 && r
.GetHeight() < GetMinHeight() )
325 r
.SetHeight( GetMinHeight() ) ;
328 // TODO actuall this is too early, in case the window extents are adjusted
329 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
332 void wxNonOwnedWindow::HandleMoved( double timestampsec
)
334 wxMoveEvent
wxevent( GetPosition() , GetId());
335 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
336 wxevent
.SetEventObject( this );
337 HandleWindowEvent(wxevent
);
340 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp
)
342 if (s_macDeactivateWindow
)
344 wxLogTrace(TRACE_ACTIVATE
,
345 wxT("Doing delayed deactivation of %p"),
346 s_macDeactivateWindow
);
348 s_macDeactivateWindow
->MacActivate(timestamp
, false);
352 void wxNonOwnedWindow::MacActivate( long timestamp
, bool WXUNUSED(inIsActivating
) )
354 wxLogTrace(TRACE_ACTIVATE
, wxT("TopLevel=%p::MacActivate"), this);
356 if (s_macDeactivateWindow
== this)
357 s_macDeactivateWindow
= NULL
;
359 MacDelayedDeactivation(timestamp
);
362 bool wxNonOwnedWindow::Show(bool show
)
364 if ( !wxWindow::Show(show
) )
368 m_nowpeer
->Show(show
);
372 // because apps expect a size event to occur at this moment
373 wxSizeEvent
event(GetSize() , m_windowId
);
374 event
.SetEventObject(this);
375 HandleWindowEvent(event
);
381 bool wxNonOwnedWindow::SetTransparent(wxByte alpha
)
383 return m_nowpeer
->SetTransparent(alpha
);
387 bool wxNonOwnedWindow::CanSetTransparent()
389 return m_nowpeer
->CanSetTransparent();
393 void wxNonOwnedWindow::SetExtraStyle(long exStyle
)
395 if ( GetExtraStyle() == exStyle
)
398 wxWindow::SetExtraStyle( exStyle
) ;
401 m_nowpeer
->SetExtraStyle(exStyle
);
404 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style
)
406 if ( !wxWindow::SetBackgroundStyle(style
) )
409 return m_nowpeer
? m_nowpeer
->SetBackgroundStyle(style
) : true;
412 void wxNonOwnedWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
414 if ( m_nowpeer
== NULL
)
417 m_cachedClippedRectValid
= false ;
419 m_nowpeer
->MoveWindow(x
, y
, width
, height
);
420 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
423 void wxNonOwnedWindow::DoGetPosition( int *x
, int *y
) const
425 if ( m_nowpeer
== NULL
)
429 m_nowpeer
->GetPosition(x1
, y1
);
437 void wxNonOwnedWindow::DoGetSize( int *width
, int *height
) const
439 if ( m_nowpeer
== NULL
)
444 m_nowpeer
->GetSize(w
, h
);
452 void wxNonOwnedWindow::DoGetClientSize( int *width
, int *height
) const
454 if ( m_nowpeer
== NULL
)
458 m_nowpeer
->GetContentArea(left
, top
, w
, h
);
467 void wxNonOwnedWindow::Update()
472 WXWindow
wxNonOwnedWindow::GetWXWindow() const
474 return m_nowpeer
? m_nowpeer
->GetWXWindow() : NULL
;
477 // ---------------------------------------------------------------------------
478 // Shape implementation
479 // ---------------------------------------------------------------------------
482 bool wxNonOwnedWindow::DoSetShape(const wxRegion
& region
)
484 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
485 wxT("Shaped windows must be created with the wxFRAME_SHAPED style."));
489 // The empty region signifies that the shape
490 // should be removed from the window.
491 if ( region
.IsEmpty() )
493 wxSize sz
= GetClientSize();
494 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
498 return DoSetShape(rgn
);
501 return m_nowpeer
->SetShape(region
);