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 // Find an item given the Macintosh Window Reference
50 WX_DECLARE_HASH_MAP(WXWindow
, wxNonOwnedWindow
*, wxPointerHash
, wxPointerEqual
, MacWindowMap
);
52 static MacWindowMap wxWinMacWindowList
;
54 wxNonOwnedWindow
*wxFindWindowFromWXWindow(WXWindow inWindowRef
)
56 MacWindowMap::iterator node
= wxWinMacWindowList
.find(inWindowRef
);
58 return (node
== wxWinMacWindowList
.end()) ? NULL
: node
->second
;
61 void wxAssociateWindowWithWXWindow(WXWindow inWindowRef
, wxNonOwnedWindow
*win
) ;
62 void wxAssociateWindowWithWXWindow(WXWindow inWindowRef
, wxNonOwnedWindow
*win
)
64 // adding NULL WindowRef is (first) surely a result of an error and
66 wxCHECK_RET( inWindowRef
!= (WXWindow
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
68 wxWinMacWindowList
[inWindowRef
] = win
;
71 void wxRemoveWXWindowAssociation(wxNonOwnedWindow
*win
) ;
72 void wxRemoveWXWindowAssociation(wxNonOwnedWindow
*win
)
74 MacWindowMap::iterator it
;
75 for ( it
= wxWinMacWindowList
.begin(); it
!= wxWinMacWindowList
.end(); ++it
)
77 if ( it
->second
== win
)
79 wxWinMacWindowList
.erase(it
);
85 wxNonOwnedWindow
* wxNonOwnedWindow::GetFromWXWindow( WXWindow win
)
87 return wxFindWindowFromWXWindow( win
);
90 // ----------------------------------------------------------------------------
91 // wxNonOwnedWindow creation
92 // ----------------------------------------------------------------------------
94 IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl
, wxObject
)
96 wxNonOwnedWindow
*wxNonOwnedWindow::s_macDeactivateWindow
= NULL
;
98 void wxNonOwnedWindow::Init()
103 bool wxNonOwnedWindow::Create(wxWindow
*parent
,
108 const wxString
& name
)
113 m_windowStyle
= style
;
117 m_windowId
= id
== -1 ? NewControlId() : id
;
118 m_windowStyle
= style
;
125 wxRect display
= wxGetClientDisplayRect() ;
127 if ( x
== wxDefaultPosition
.x
)
130 if ( y
== wxDefaultPosition
.y
)
133 int w
= WidthDefault(size
.x
);
134 int h
= HeightDefault(size
.y
);
136 m_nowpeer
= wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, parent
, wxPoint(x
,y
) , wxSize(w
,h
) , style
, GetExtraStyle(), name
);
137 wxAssociateWindowWithWXWindow( m_nowpeer
->GetWXWindow() , this ) ;
138 m_peer
= wxWidgetImpl::CreateContentView(this);
140 DoSetWindowVariant( m_windowVariant
) ;
142 wxWindowCreateEvent
event(this);
143 HandleWindowEvent(event
);
145 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
));
148 parent
->AddChild(this);
153 wxNonOwnedWindow::~wxNonOwnedWindow()
157 wxRemoveWXWindowAssociation( this ) ;
163 // avoid dangling refs
164 if ( s_macDeactivateWindow
== this )
165 s_macDeactivateWindow
= NULL
;
168 // ----------------------------------------------------------------------------
169 // wxNonOwnedWindow misc
170 // ----------------------------------------------------------------------------
172 bool wxNonOwnedWindow::OSXShowWithEffect(bool show
,
176 // Cocoa code needs to manage window visibility on its own and so calls
177 // wxWindow::Show() as needed but if we already changed the internal
178 // visibility flag here, Show() would do nothing, so avoid doing it
180 if ( !wxWindow::Show(show
) )
184 if ( effect
== wxSHOW_EFFECT_NONE
||
185 !m_nowpeer
|| !m_nowpeer
->ShowWithEffect(show
, effect
, timeout
) )
190 // as apps expect a size event to occur when the window is shown,
191 // generate one when it is shown with effect too
192 wxSizeEvent
event(GetSize(), m_windowId
);
193 event
.SetEventObject(this);
194 HandleWindowEvent(event
);
200 wxPoint
wxNonOwnedWindow::GetClientAreaOrigin() const
202 int left
, top
, width
, height
;
203 m_nowpeer
->GetContentArea(left
, top
, width
, height
);
204 return wxPoint(left
, top
);
207 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour
& c
)
209 if ( !wxWindow::SetBackgroundColour(c
) && m_hasBgCol
)
212 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM
)
215 return m_nowpeer
->SetBackgroundColour(c
);
220 // Raise the window to the top of the Z order
221 void wxNonOwnedWindow::Raise()
226 // Lower the window to the bottom of the Z order
227 void wxNonOwnedWindow::Lower()
232 void wxNonOwnedWindow::HandleActivated( double timestampsec
, bool didActivate
)
234 MacActivate( (int) (timestampsec
* 1000) , didActivate
);
235 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, didActivate
, GetId());
236 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
237 wxevent
.SetEventObject(this);
238 HandleWindowEvent(wxevent
);
241 void wxNonOwnedWindow::HandleResized( double timestampsec
)
243 wxSizeEvent
wxevent( GetSize() , GetId());
244 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
245 wxevent
.SetEventObject( this );
246 HandleWindowEvent(wxevent
);
247 // we have to inform some controls that have to reset things
248 // relative to the toplevel window (e.g. OpenGL buffers)
249 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
252 void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec
), wxRect
* rect
)
256 // this is a EVT_SIZING not a EVT_SIZE type !
257 wxSizeEvent
wxevent( r
, GetId() ) ;
258 wxevent
.SetEventObject( this ) ;
259 if ( HandleWindowEvent(wxevent
) )
260 r
= wxevent
.GetRect() ;
262 if ( GetMaxWidth() != -1 && r
.GetWidth() > GetMaxWidth() )
263 r
.SetWidth( GetMaxWidth() ) ;
264 if ( GetMaxHeight() != -1 && r
.GetHeight() > GetMaxHeight() )
265 r
.SetHeight( GetMaxHeight() ) ;
266 if ( GetMinWidth() != -1 && r
.GetWidth() < GetMinWidth() )
267 r
.SetWidth( GetMinWidth() ) ;
268 if ( GetMinHeight() != -1 && r
.GetHeight() < GetMinHeight() )
269 r
.SetHeight( GetMinHeight() ) ;
272 // TODO actuall this is too early, in case the window extents are adjusted
273 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
276 void wxNonOwnedWindow::HandleMoved( double timestampsec
)
278 wxMoveEvent
wxevent( GetPosition() , GetId());
279 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
280 wxevent
.SetEventObject( this );
281 HandleWindowEvent(wxevent
);
284 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp
)
286 if (s_macDeactivateWindow
)
288 wxLogTrace(TRACE_ACTIVATE
,
289 wxT("Doing delayed deactivation of %p"),
290 s_macDeactivateWindow
);
292 s_macDeactivateWindow
->MacActivate(timestamp
, false);
296 void wxNonOwnedWindow::MacActivate( long timestamp
, bool WXUNUSED(inIsActivating
) )
298 wxLogTrace(TRACE_ACTIVATE
, wxT("TopLevel=%p::MacActivate"), this);
300 if (s_macDeactivateWindow
== this)
301 s_macDeactivateWindow
= NULL
;
303 MacDelayedDeactivation(timestamp
);
306 bool wxNonOwnedWindow::Show(bool show
)
308 if ( !wxWindow::Show(show
) )
312 m_nowpeer
->Show(show
);
316 // because apps expect a size event to occur at this moment
317 wxSizeEvent
event(GetSize() , m_windowId
);
318 event
.SetEventObject(this);
319 HandleWindowEvent(event
);
325 bool wxNonOwnedWindow::SetTransparent(wxByte alpha
)
327 return m_nowpeer
->SetTransparent(alpha
);
331 bool wxNonOwnedWindow::CanSetTransparent()
333 return m_nowpeer
->CanSetTransparent();
337 void wxNonOwnedWindow::SetExtraStyle(long exStyle
)
339 if ( GetExtraStyle() == exStyle
)
342 wxWindow::SetExtraStyle( exStyle
) ;
345 m_nowpeer
->SetExtraStyle(exStyle
);
348 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style
)
350 if ( !wxWindow::SetBackgroundStyle(style
) )
353 return m_nowpeer
? m_nowpeer
->SetBackgroundStyle(style
) : true;
356 void wxNonOwnedWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
358 if ( m_nowpeer
== NULL
)
361 m_cachedClippedRectValid
= false ;
363 m_nowpeer
->MoveWindow(x
, y
, width
, height
);
364 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
367 void wxNonOwnedWindow::DoGetPosition( int *x
, int *y
) const
369 if ( m_nowpeer
== NULL
)
373 m_nowpeer
->GetPosition(x1
, y1
);
381 void wxNonOwnedWindow::DoGetSize( int *width
, int *height
) const
383 if ( m_nowpeer
== NULL
)
388 m_nowpeer
->GetSize(w
, h
);
396 void wxNonOwnedWindow::DoGetClientSize( int *width
, int *height
) const
398 if ( m_nowpeer
== NULL
)
402 m_nowpeer
->GetContentArea(left
, top
, w
, h
);
411 void wxNonOwnedWindow::Update()
416 WXWindow
wxNonOwnedWindow::GetWXWindow() const
418 return m_nowpeer
? m_nowpeer
->GetWXWindow() : NULL
;
421 // ---------------------------------------------------------------------------
422 // Shape implementation
423 // ---------------------------------------------------------------------------
426 bool wxNonOwnedWindow::DoSetShape(const wxRegion
& region
)
428 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
429 wxT("Shaped windows must be created with the wxFRAME_SHAPED style."));
431 // The empty region signifies that the shape
432 // should be removed from the window.
433 if ( region
.IsEmpty() )
435 wxSize sz
= GetClientSize();
436 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
440 return DoSetShape(rgn
);
443 return m_nowpeer
->SetShape(region
);