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"
18 #include "wx/hashmap.h"
19 #include "wx/evtloop.h"
20 #include "wx/tooltip.h"
21 #include "wx/nonownedwnd.h"
23 #include "wx/osx/private.h"
24 #include "wx/settings.h"
27 #if wxUSE_SYSTEM_OPTIONS
28 #include "wx/sysopt.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // trace mask for activation tracing messages
36 #define TRACE_ACTIVATE "activation"
38 wxWindow
* g_MacLastWindow
= NULL
;
40 // unified title and toolbar constant - not in Tiger headers, so we duplicate it here
41 #define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
43 // ---------------------------------------------------------------------------
44 // wxWindowMac utility functions
45 // ---------------------------------------------------------------------------
47 // Find an item given the Macintosh Window Reference
49 WX_DECLARE_HASH_MAP(WXWindow
, wxNonOwnedWindow
*, wxPointerHash
, wxPointerEqual
, MacWindowMap
);
51 static MacWindowMap wxWinMacWindowList
;
53 wxNonOwnedWindow
*wxFindWindowFromWXWindow(WXWindow inWindowRef
)
55 MacWindowMap::iterator node
= wxWinMacWindowList
.find(inWindowRef
);
57 return (node
== wxWinMacWindowList
.end()) ? NULL
: node
->second
;
60 void wxAssociateWindowWithWXWindow(WXWindow inWindowRef
, wxNonOwnedWindow
*win
) ;
61 void wxAssociateWindowWithWXWindow(WXWindow inWindowRef
, wxNonOwnedWindow
*win
)
63 // adding NULL WindowRef is (first) surely a result of an error and
65 wxCHECK_RET( inWindowRef
!= (WXWindow
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
67 wxWinMacWindowList
[inWindowRef
] = win
;
70 void wxRemoveWXWindowAssociation(wxNonOwnedWindow
*win
) ;
71 void wxRemoveWXWindowAssociation(wxNonOwnedWindow
*win
)
73 MacWindowMap::iterator it
;
74 for ( it
= wxWinMacWindowList
.begin(); it
!= wxWinMacWindowList
.end(); ++it
)
76 if ( it
->second
== win
)
78 wxWinMacWindowList
.erase(it
);
84 wxNonOwnedWindow
* wxNonOwnedWindow::GetFromWXWindow( WXWindow win
)
86 return wxFindWindowFromWXWindow( win
);
89 // ----------------------------------------------------------------------------
90 // wxNonOwnedWindow creation
91 // ----------------------------------------------------------------------------
93 IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl
, wxObject
)
95 wxNonOwnedWindow
*wxNonOwnedWindow::s_macDeactivateWindow
= NULL
;
97 void wxNonOwnedWindow::Init()
102 bool wxNonOwnedWindow::Create(wxWindow
*parent
,
107 const wxString
& name
)
112 m_windowStyle
= style
;
116 m_windowId
= id
== -1 ? NewControlId() : id
;
117 m_windowStyle
= style
;
124 wxRect display
= wxGetClientDisplayRect() ;
126 if ( x
== wxDefaultPosition
.x
)
129 if ( y
== wxDefaultPosition
.y
)
132 int w
= WidthDefault(size
.x
);
133 int h
= HeightDefault(size
.y
);
135 m_nowpeer
= wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, parent
, wxPoint(x
,y
) , wxSize(w
,h
) , style
, GetExtraStyle(), name
);
136 wxAssociateWindowWithWXWindow( m_nowpeer
->GetWXWindow() , this ) ;
137 m_peer
= wxWidgetImpl::CreateContentView(this);
139 DoSetWindowVariant( m_windowVariant
) ;
141 wxWindowCreateEvent
event(this);
142 HandleWindowEvent(event
);
144 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
));
147 parent
->AddChild(this);
152 wxNonOwnedWindow::~wxNonOwnedWindow()
156 wxRemoveWXWindowAssociation( this ) ;
162 // avoid dangling refs
163 if ( s_macDeactivateWindow
== this )
164 s_macDeactivateWindow
= NULL
;
167 // ----------------------------------------------------------------------------
168 // wxNonOwnedWindow misc
169 // ----------------------------------------------------------------------------
171 bool wxNonOwnedWindow::ShowWithEffect(wxShowEffect effect
,
174 if ( !wxWindow::Show(true) )
177 // because apps expect a size event to occur at this moment
178 wxSizeEvent
event(GetSize() , m_windowId
);
179 event
.SetEventObject(this);
180 HandleWindowEvent(event
);
183 return m_nowpeer
->ShowWithEffect(true, effect
, timeout
);
186 bool wxNonOwnedWindow::HideWithEffect(wxShowEffect effect
,
189 if ( !wxWindow::Show(false) )
192 return m_nowpeer
->ShowWithEffect(false, effect
, timeout
);
195 wxPoint
wxNonOwnedWindow::GetClientAreaOrigin() const
197 int left
, top
, width
, height
;
198 m_nowpeer
->GetContentArea(left
, top
, width
, height
);
199 return wxPoint(left
, top
);
202 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour
& c
)
204 if ( !wxWindow::SetBackgroundColour(c
) && m_hasBgCol
)
207 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM
)
210 return m_nowpeer
->SetBackgroundColour(c
);
215 // Raise the window to the top of the Z order
216 void wxNonOwnedWindow::Raise()
221 // Lower the window to the bottom of the Z order
222 void wxNonOwnedWindow::Lower()
227 void wxNonOwnedWindow::HandleActivated( double timestampsec
, bool didActivate
)
229 MacActivate( (int) (timestampsec
* 1000) , didActivate
);
230 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, didActivate
, GetId());
231 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
232 wxevent
.SetEventObject(this);
233 HandleWindowEvent(wxevent
);
236 void wxNonOwnedWindow::HandleResized( double timestampsec
)
238 wxSizeEvent
wxevent( GetSize() , GetId());
239 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
240 wxevent
.SetEventObject( this );
241 HandleWindowEvent(wxevent
);
242 // we have to inform some controls that have to reset things
243 // relative to the toplevel window (e.g. OpenGL buffers)
244 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
247 void wxNonOwnedWindow::HandleResizing( double timestampsec
, wxRect
* rect
)
251 // this is a EVT_SIZING not a EVT_SIZE type !
252 wxSizeEvent
wxevent( r
, GetId() ) ;
253 wxevent
.SetEventObject( this ) ;
254 if ( HandleWindowEvent(wxevent
) )
255 r
= wxevent
.GetRect() ;
257 if ( GetMaxWidth() != -1 && r
.GetWidth() > GetMaxWidth() )
258 r
.SetWidth( GetMaxWidth() ) ;
259 if ( GetMaxHeight() != -1 && r
.GetHeight() > GetMaxHeight() )
260 r
.SetHeight( GetMaxHeight() ) ;
261 if ( GetMinWidth() != -1 && r
.GetWidth() < GetMinWidth() )
262 r
.SetWidth( GetMinWidth() ) ;
263 if ( GetMinHeight() != -1 && r
.GetHeight() < GetMinHeight() )
264 r
.SetHeight( GetMinHeight() ) ;
267 // TODO actuall this is too early, in case the window extents are adjusted
268 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
271 void wxNonOwnedWindow::HandleMoved( double timestampsec
)
273 wxMoveEvent
wxevent( GetPosition() , GetId());
274 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
275 wxevent
.SetEventObject( this );
276 HandleWindowEvent(wxevent
);
279 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp
)
281 if (s_macDeactivateWindow
)
283 wxLogTrace(TRACE_ACTIVATE
,
284 wxT("Doing delayed deactivation of %p"),
285 s_macDeactivateWindow
);
287 s_macDeactivateWindow
->MacActivate(timestamp
, false);
291 void wxNonOwnedWindow::MacActivate( long timestamp
, bool WXUNUSED(inIsActivating
) )
293 wxLogTrace(TRACE_ACTIVATE
, wxT("TopLevel=%p::MacActivate"), this);
295 if (s_macDeactivateWindow
== this)
296 s_macDeactivateWindow
= NULL
;
298 MacDelayedDeactivation(timestamp
);
301 bool wxNonOwnedWindow::Show(bool show
)
303 if ( !wxWindow::Show(show
) )
307 m_nowpeer
->Show(show
);
311 // because apps expect a size event to occur at this moment
312 wxSizeEvent
event(GetSize() , m_windowId
);
313 event
.SetEventObject(this);
314 HandleWindowEvent(event
);
320 bool wxNonOwnedWindow::SetTransparent(wxByte alpha
)
322 return m_nowpeer
->SetTransparent(alpha
);
326 bool wxNonOwnedWindow::CanSetTransparent()
328 return m_nowpeer
->CanSetTransparent();
332 void wxNonOwnedWindow::SetExtraStyle(long exStyle
)
334 if ( GetExtraStyle() == exStyle
)
337 wxWindow::SetExtraStyle( exStyle
) ;
340 m_nowpeer
->SetExtraStyle(exStyle
);
343 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style
)
345 if ( !wxWindow::SetBackgroundStyle(style
) )
348 return m_nowpeer
->SetBackgroundStyle(style
);
351 void wxNonOwnedWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
353 if ( m_nowpeer
== NULL
)
356 m_cachedClippedRectValid
= false ;
358 m_nowpeer
->MoveWindow(x
, y
, width
, height
);
359 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
362 void wxNonOwnedWindow::DoGetPosition( int *x
, int *y
) const
364 if ( m_nowpeer
== NULL
)
368 m_nowpeer
->GetPosition(x1
, y1
);
376 void wxNonOwnedWindow::DoGetSize( int *width
, int *height
) const
378 if ( m_nowpeer
== NULL
)
383 m_nowpeer
->GetSize(w
, h
);
391 void wxNonOwnedWindow::DoGetClientSize( int *width
, int *height
) const
393 if ( m_nowpeer
== NULL
)
397 m_nowpeer
->GetContentArea(left
, top
, w
, h
);
406 void wxNonOwnedWindow::Update()
411 WXWindow
wxNonOwnedWindow::GetWXWindow() const
413 return m_nowpeer
? m_nowpeer
->GetWXWindow() : NULL
;
416 // ---------------------------------------------------------------------------
417 // Shape implementation
418 // ---------------------------------------------------------------------------
421 bool wxNonOwnedWindow::SetShape(const wxRegion
& region
)
423 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
424 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
426 // The empty region signifies that the shape
427 // should be removed from the window.
428 if ( region
.IsEmpty() )
430 wxSize sz
= GetClientSize();
431 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
435 return SetShape(rgn
);
438 return m_nowpeer
->SetShape(region
);