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);
150 wxBIND_OR_CONNECT_HACK(this, wxEVT_DESTROY
, wxWindowDestroyEventHandler
,
151 wxNonOwnedWindow::OnWindowDestroy
, this);
156 wxNonOwnedWindow::~wxNonOwnedWindow()
160 wxRemoveWXWindowAssociation( this ) ;
167 // avoid dangling refs
168 if ( s_macDeactivateWindow
== this )
169 s_macDeactivateWindow
= NULL
;
172 void wxNonOwnedWindow::OnWindowDestroy( wxWindowDestroyEvent
&event
)
177 m_nowpeer
->WillBeDestroyed();
180 // ----------------------------------------------------------------------------
181 // wxNonOwnedWindow misc
182 // ----------------------------------------------------------------------------
184 bool wxNonOwnedWindow::OSXShowWithEffect(bool show
,
188 // Cocoa code needs to manage window visibility on its own and so calls
189 // wxWindow::Show() as needed but if we already changed the internal
190 // visibility flag here, Show() would do nothing, so avoid doing it
192 if ( !wxWindow::Show(show
) )
196 if ( effect
== wxSHOW_EFFECT_NONE
||
197 !m_nowpeer
|| !m_nowpeer
->ShowWithEffect(show
, effect
, timeout
) )
202 // as apps expect a size event to occur when the window is shown,
203 // generate one when it is shown with effect too
204 wxSizeEvent
event(GetSize(), m_windowId
);
205 event
.SetEventObject(this);
206 HandleWindowEvent(event
);
212 wxPoint
wxNonOwnedWindow::GetClientAreaOrigin() const
214 int left
, top
, width
, height
;
215 m_nowpeer
->GetContentArea(left
, top
, width
, height
);
216 return wxPoint(left
, top
);
219 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour
& c
)
221 if ( !wxWindow::SetBackgroundColour(c
) && m_hasBgCol
)
224 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM
)
227 return m_nowpeer
->SetBackgroundColour(c
);
232 void wxNonOwnedWindow::SetWindowStyleFlag(long flags
)
234 if (flags
== GetWindowStyleFlag())
237 wxWindow::SetWindowStyleFlag(flags
);
240 m_nowpeer
->SetWindowStyleFlag(flags
);
243 // Raise the window to the top of the Z order
244 void wxNonOwnedWindow::Raise()
249 // Lower the window to the bottom of the Z order
250 void wxNonOwnedWindow::Lower()
255 void wxNonOwnedWindow::HandleActivated( double timestampsec
, bool didActivate
)
257 MacActivate( (int) (timestampsec
* 1000) , didActivate
);
258 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, didActivate
, GetId());
259 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
260 wxevent
.SetEventObject(this);
261 HandleWindowEvent(wxevent
);
264 void wxNonOwnedWindow::HandleResized( double timestampsec
)
266 wxSizeEvent
wxevent( GetSize() , GetId());
267 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
268 wxevent
.SetEventObject( this );
269 HandleWindowEvent(wxevent
);
270 // we have to inform some controls that have to reset things
271 // relative to the toplevel window (e.g. OpenGL buffers)
272 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
275 void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec
), wxRect
* rect
)
279 // this is a EVT_SIZING not a EVT_SIZE type !
280 wxSizeEvent
wxevent( r
, GetId() ) ;
281 wxevent
.SetEventObject( this ) ;
282 if ( HandleWindowEvent(wxevent
) )
283 r
= wxevent
.GetRect() ;
285 if ( GetMaxWidth() != -1 && r
.GetWidth() > GetMaxWidth() )
286 r
.SetWidth( GetMaxWidth() ) ;
287 if ( GetMaxHeight() != -1 && r
.GetHeight() > GetMaxHeight() )
288 r
.SetHeight( GetMaxHeight() ) ;
289 if ( GetMinWidth() != -1 && r
.GetWidth() < GetMinWidth() )
290 r
.SetWidth( GetMinWidth() ) ;
291 if ( GetMinHeight() != -1 && r
.GetHeight() < GetMinHeight() )
292 r
.SetHeight( GetMinHeight() ) ;
295 // TODO actuall this is too early, in case the window extents are adjusted
296 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
299 void wxNonOwnedWindow::HandleMoved( double timestampsec
)
301 wxMoveEvent
wxevent( GetPosition() , GetId());
302 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
303 wxevent
.SetEventObject( this );
304 HandleWindowEvent(wxevent
);
307 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp
)
309 if (s_macDeactivateWindow
)
311 wxLogTrace(TRACE_ACTIVATE
,
312 wxT("Doing delayed deactivation of %p"),
313 s_macDeactivateWindow
);
315 s_macDeactivateWindow
->MacActivate(timestamp
, false);
319 void wxNonOwnedWindow::MacActivate( long timestamp
, bool WXUNUSED(inIsActivating
) )
321 wxLogTrace(TRACE_ACTIVATE
, wxT("TopLevel=%p::MacActivate"), this);
323 if (s_macDeactivateWindow
== this)
324 s_macDeactivateWindow
= NULL
;
326 MacDelayedDeactivation(timestamp
);
329 bool wxNonOwnedWindow::Show(bool show
)
331 if ( !wxWindow::Show(show
) )
335 m_nowpeer
->Show(show
);
339 // because apps expect a size event to occur at this moment
340 wxSizeEvent
event(GetSize() , m_windowId
);
341 event
.SetEventObject(this);
342 HandleWindowEvent(event
);
348 bool wxNonOwnedWindow::SetTransparent(wxByte alpha
)
350 return m_nowpeer
->SetTransparent(alpha
);
354 bool wxNonOwnedWindow::CanSetTransparent()
356 return m_nowpeer
->CanSetTransparent();
360 void wxNonOwnedWindow::SetExtraStyle(long exStyle
)
362 if ( GetExtraStyle() == exStyle
)
365 wxWindow::SetExtraStyle( exStyle
) ;
368 m_nowpeer
->SetExtraStyle(exStyle
);
371 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style
)
373 if ( !wxWindow::SetBackgroundStyle(style
) )
376 return m_nowpeer
? m_nowpeer
->SetBackgroundStyle(style
) : true;
379 void wxNonOwnedWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
381 if ( m_nowpeer
== NULL
)
384 m_cachedClippedRectValid
= false ;
386 m_nowpeer
->MoveWindow(x
, y
, width
, height
);
387 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
390 void wxNonOwnedWindow::DoGetPosition( int *x
, int *y
) const
392 if ( m_nowpeer
== NULL
)
396 m_nowpeer
->GetPosition(x1
, y1
);
404 void wxNonOwnedWindow::DoGetSize( int *width
, int *height
) const
406 if ( m_nowpeer
== NULL
)
411 m_nowpeer
->GetSize(w
, h
);
419 void wxNonOwnedWindow::DoGetClientSize( int *width
, int *height
) const
421 if ( m_nowpeer
== NULL
)
425 // perhaps we should do this for all ?
426 #ifdef __WXOSX_IPHONE__
427 m_peer
->GetContentArea(left
, top
, w
, h
);
429 m_nowpeer
->GetContentArea(left
, top
, w
, h
);
439 void wxNonOwnedWindow::Update()
444 WXWindow
wxNonOwnedWindow::GetWXWindow() const
446 return m_nowpeer
? m_nowpeer
->GetWXWindow() : NULL
;
449 // ---------------------------------------------------------------------------
450 // Shape implementation
451 // ---------------------------------------------------------------------------
454 bool wxNonOwnedWindow::DoSetShape(const wxRegion
& region
)
456 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED
), false,
457 wxT("Shaped windows must be created with the wxFRAME_SHAPED style."));
459 // The empty region signifies that the shape
460 // should be removed from the window.
461 if ( region
.IsEmpty() )
463 wxSize sz
= GetClientSize();
464 wxRegion
rgn(0, 0, sz
.x
, sz
.y
);
468 return DoSetShape(rgn
);
471 return m_nowpeer
->SetShape(region
);