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"
16 #include "wx/dcmemory.h"
20 #include "wx/hashmap.h"
21 #include "wx/evtloop.h"
22 #include "wx/tooltip.h"
23 #include "wx/nonownedwnd.h"
25 #include "wx/osx/private.h"
26 #include "wx/settings.h"
29 #if wxUSE_SYSTEM_OPTIONS
30 #include "wx/sysopt.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // trace mask for activation tracing messages
38 #define TRACE_ACTIVATE "activation"
40 wxWindow
* g_MacLastWindow
= NULL
;
42 // unified title and toolbar constant - not in Tiger headers, so we duplicate it here
43 #define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
45 // ---------------------------------------------------------------------------
46 // wxWindowMac utility functions
47 // ---------------------------------------------------------------------------
49 WX_DECLARE_HASH_MAP(WXWindow
, wxNonOwnedWindowImpl
*, wxPointerHash
, wxPointerEqual
, MacWindowMap
);
51 static MacWindowMap wxWinMacWindowList
;
53 wxNonOwnedWindow
* wxNonOwnedWindow::GetFromWXWindow( WXWindow win
)
55 wxNonOwnedWindowImpl
* impl
= wxNonOwnedWindowImpl::FindFromWXWindow(win
);
57 return ( impl
!= NULL
? impl
->GetWXPeer() : NULL
) ;
60 wxNonOwnedWindowImpl
* wxNonOwnedWindowImpl::FindFromWXWindow (WXWindow window
)
62 MacWindowMap::iterator node
= wxWinMacWindowList
.find(window
);
64 return (node
== wxWinMacWindowList
.end()) ? NULL
: node
->second
;
67 void wxNonOwnedWindowImpl::RemoveAssociations( wxNonOwnedWindowImpl
* impl
)
69 MacWindowMap::iterator it
;
70 for ( it
= wxWinMacWindowList
.begin(); it
!= wxWinMacWindowList
.end(); ++it
)
72 if ( it
->second
== impl
)
74 wxWinMacWindowList
.erase(it
);
80 void wxNonOwnedWindowImpl::Associate( WXWindow window
, wxNonOwnedWindowImpl
*impl
)
82 // adding NULL WindowRef is (first) surely a result of an error and
84 wxCHECK_RET( window
!= (WXWindow
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
86 wxWinMacWindowList
[window
] = impl
;
89 // ----------------------------------------------------------------------------
90 // wxNonOwnedWindow creation
91 // ----------------------------------------------------------------------------
93 IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl
, wxObject
)
95 wxNonOwnedWindow
*wxNonOwnedWindow::s_macDeactivateWindow
= NULL
;
97 void wxNonOwnedWindow::Init()
100 m_isNativeWindowWrapper
= false;
103 bool wxNonOwnedWindow::Create(wxWindow
*parent
,
105 const wxPoint
& posOrig
,
106 const wxSize
& sizeOrig
,
108 const wxString
& name
)
110 m_windowStyle
= style
;
114 m_windowId
= id
== -1 ? NewControlId() : id
;
115 m_windowStyle
= style
;
118 // use the appropriate defaults for the position and size if necessary
119 wxSize
size(sizeOrig
);
120 if ( !size
.IsFullySpecified() )
121 size
.SetDefaults(wxTopLevelWindow::GetDefaultSize());
123 wxPoint
pos(posOrig
);
124 if ( !pos
.IsFullySpecified() )
126 wxRect rectWin
= wxRect(size
).CentreIn(wxGetClientDisplayRect());
128 // The size of the window is often not really known yet, TLWs are often
129 // created with some small initial size and later are fitted to contain
130 // their children so centering the window will show it too much to the
131 // right and bottom, adjust for it by putting it more to the left and
136 pos
.SetDefaults(rectWin
.GetPosition());
140 m_nowpeer
= wxNonOwnedWindowImpl::CreateNonOwnedWindow
144 style
, GetExtraStyle(),
147 wxNonOwnedWindowImpl::Associate( m_nowpeer
->GetWXWindow() , m_nowpeer
) ;
148 SetPeer(wxWidgetImpl::CreateContentView(this));
150 DoSetWindowVariant( m_windowVariant
) ;
152 wxWindowCreateEvent
event(this);
153 HandleWindowEvent(event
);
155 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
));
158 parent
->AddChild(this);
163 bool wxNonOwnedWindow::Create(wxWindow
*parent
, WXWindow nativeWindow
)
166 parent
->AddChild(this);
168 SubclassWin(nativeWindow
);
173 void wxNonOwnedWindow::SubclassWin(WXWindow nativeWindow
)
175 wxASSERT_MSG( !m_isNativeWindowWrapper
, wxT("subclassing window twice?") );
176 wxASSERT_MSG( m_nowpeer
== NULL
, wxT("window already was created") );
178 m_nowpeer
= wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, GetParent(), nativeWindow
);
179 m_isNativeWindowWrapper
= true;
180 wxNonOwnedWindowImpl::Associate( m_nowpeer
->GetWXWindow() , m_nowpeer
) ;
181 SetPeer(wxWidgetImpl::CreateContentView(this));
184 void wxNonOwnedWindow::UnsubclassWin()
186 wxASSERT_MSG( m_isNativeWindowWrapper
, wxT("window was not subclassed") );
189 GetParent()->RemoveChild(this);
191 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer
) ;
194 m_isNativeWindowWrapper
= false;
198 wxNonOwnedWindow::~wxNonOwnedWindow()
202 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer
) ;
208 // avoid dangling refs
209 if ( s_macDeactivateWindow
== this )
210 s_macDeactivateWindow
= NULL
;
213 bool wxNonOwnedWindow::Destroy()
217 return wxWindow::Destroy();
220 void wxNonOwnedWindow::WillBeDestroyed()
223 m_nowpeer
->WillBeDestroyed();
226 // ----------------------------------------------------------------------------
227 // wxNonOwnedWindow misc
228 // ----------------------------------------------------------------------------
230 bool wxNonOwnedWindow::OSXShowWithEffect(bool show
,
234 // Cocoa code needs to manage window visibility on its own and so calls
235 // wxWindow::Show() as needed but if we already changed the internal
236 // visibility flag here, Show() would do nothing, so avoid doing it
238 if ( !wxWindow::Show(show
) )
242 if ( effect
== wxSHOW_EFFECT_NONE
||
243 !m_nowpeer
|| !m_nowpeer
->ShowWithEffect(show
, effect
, timeout
) )
248 // as apps expect a size event to occur when the window is shown,
249 // generate one when it is shown with effect too
250 wxSizeEvent
event(GetSize(), m_windowId
);
251 event
.SetEventObject(this);
252 HandleWindowEvent(event
);
258 wxPoint
wxNonOwnedWindow::GetClientAreaOrigin() const
260 int left
, top
, width
, height
;
261 m_nowpeer
->GetContentArea(left
, top
, width
, height
);
262 return wxPoint(left
, top
);
265 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour
& c
)
267 if ( !wxWindow::SetBackgroundColour(c
) && m_hasBgCol
)
270 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM
)
273 return m_nowpeer
->SetBackgroundColour(c
);
278 void wxNonOwnedWindow::SetWindowStyleFlag(long flags
)
280 if (flags
== GetWindowStyleFlag())
283 wxWindow::SetWindowStyleFlag(flags
);
286 m_nowpeer
->SetWindowStyleFlag(flags
);
289 // Raise the window to the top of the Z order
290 void wxNonOwnedWindow::Raise()
295 // Lower the window to the bottom of the Z order
296 void wxNonOwnedWindow::Lower()
301 void wxNonOwnedWindow::HandleActivated( double timestampsec
, bool didActivate
)
303 MacActivate( (int) (timestampsec
* 1000) , didActivate
);
304 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, didActivate
, GetId());
305 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
306 wxevent
.SetEventObject(this);
307 HandleWindowEvent(wxevent
);
310 void wxNonOwnedWindow::HandleResized( double timestampsec
)
312 wxSizeEvent
wxevent( GetSize() , GetId());
313 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
314 wxevent
.SetEventObject( this );
315 HandleWindowEvent(wxevent
);
316 // we have to inform some controls that have to reset things
317 // relative to the toplevel window (e.g. OpenGL buffers)
318 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
321 void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec
), wxRect
* rect
)
325 // this is a EVT_SIZING not a EVT_SIZE type !
326 wxSizeEvent
wxevent( r
, GetId() ) ;
327 wxevent
.SetEventObject( this ) ;
328 if ( HandleWindowEvent(wxevent
) )
329 r
= wxevent
.GetRect() ;
331 if ( GetMaxWidth() != -1 && r
.GetWidth() > GetMaxWidth() )
332 r
.SetWidth( GetMaxWidth() ) ;
333 if ( GetMaxHeight() != -1 && r
.GetHeight() > GetMaxHeight() )
334 r
.SetHeight( GetMaxHeight() ) ;
335 if ( GetMinWidth() != -1 && r
.GetWidth() < GetMinWidth() )
336 r
.SetWidth( GetMinWidth() ) ;
337 if ( GetMinHeight() != -1 && r
.GetHeight() < GetMinHeight() )
338 r
.SetHeight( GetMinHeight() ) ;
341 // TODO actuall this is too early, in case the window extents are adjusted
342 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
345 void wxNonOwnedWindow::HandleMoved( double timestampsec
)
347 wxMoveEvent
wxevent( GetPosition() , GetId());
348 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
349 wxevent
.SetEventObject( this );
350 HandleWindowEvent(wxevent
);
353 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp
)
355 if (s_macDeactivateWindow
)
357 wxLogTrace(TRACE_ACTIVATE
,
358 wxT("Doing delayed deactivation of %p"),
359 s_macDeactivateWindow
);
361 s_macDeactivateWindow
->MacActivate(timestamp
, false);
365 void wxNonOwnedWindow::MacActivate( long timestamp
, bool WXUNUSED(inIsActivating
) )
367 wxLogTrace(TRACE_ACTIVATE
, wxT("TopLevel=%p::MacActivate"), this);
369 if (s_macDeactivateWindow
== this)
370 s_macDeactivateWindow
= NULL
;
372 MacDelayedDeactivation(timestamp
);
375 bool wxNonOwnedWindow::Show(bool show
)
377 if ( !wxWindow::Show(show
) )
381 m_nowpeer
->Show(show
);
385 // because apps expect a size event to occur at this moment
386 wxSizeEvent
event(GetSize() , m_windowId
);
387 event
.SetEventObject(this);
388 HandleWindowEvent(event
);
394 bool wxNonOwnedWindow::SetTransparent(wxByte alpha
)
396 return m_nowpeer
->SetTransparent(alpha
);
400 bool wxNonOwnedWindow::CanSetTransparent()
402 return m_nowpeer
->CanSetTransparent();
406 void wxNonOwnedWindow::SetExtraStyle(long exStyle
)
408 if ( GetExtraStyle() == exStyle
)
411 wxWindow::SetExtraStyle( exStyle
) ;
414 m_nowpeer
->SetExtraStyle(exStyle
);
417 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style
)
419 if ( !wxWindow::SetBackgroundStyle(style
) )
422 return m_nowpeer
? m_nowpeer
->SetBackgroundStyle(style
) : true;
425 void wxNonOwnedWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
427 if ( m_nowpeer
== NULL
)
430 m_cachedClippedRectValid
= false ;
432 m_nowpeer
->MoveWindow(x
, y
, width
, height
);
433 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
436 void wxNonOwnedWindow::DoGetPosition( int *x
, int *y
) const
438 if ( m_nowpeer
== NULL
)
442 m_nowpeer
->GetPosition(x1
, y1
);
450 void wxNonOwnedWindow::DoGetSize( int *width
, int *height
) const
452 if ( m_nowpeer
== NULL
)
457 m_nowpeer
->GetSize(w
, h
);
465 void wxNonOwnedWindow::DoGetClientSize( int *width
, int *height
) const
467 if ( m_nowpeer
== NULL
)
471 // under iphone with a translucent status bar the m_nowpeer returns the
472 // inner area, while the content area extends under the translucent
473 // status bar, therefore we use the content view's area
474 #ifdef __WXOSX_IPHONE__
475 GetPeer()->GetContentArea(left
, top
, w
, h
);
477 m_nowpeer
->GetContentArea(left
, top
, w
, h
);
487 void wxNonOwnedWindow::Update()
492 WXWindow
wxNonOwnedWindow::GetWXWindow() const
494 return m_nowpeer
? m_nowpeer
->GetWXWindow() : NULL
;
497 // ---------------------------------------------------------------------------
498 // Shape implementation
499 // ---------------------------------------------------------------------------
501 bool wxNonOwnedWindow::DoClearShape()
505 wxSize sz
= GetClientSize();
506 wxRegion
region(0, 0, sz
.x
, sz
.y
);
508 return m_nowpeer
->SetShape(region
);
511 bool wxNonOwnedWindow::DoSetRegionShape(const wxRegion
& region
)
515 return m_nowpeer
->SetShape(region
);
518 #if wxUSE_GRAPHICS_CONTEXT
520 #include "wx/scopedptr.h"
522 bool wxNonOwnedWindow::DoSetPathShape(const wxGraphicsPath
& path
)
526 // Convert the path to wxRegion by rendering the path on a window-sized
527 // bitmap, creating a mask from it and finally creating the region from
529 wxBitmap
bmp(GetSize());
533 dc
.SetBackground(*wxBLACK
);
536 wxScopedPtr
<wxGraphicsContext
> context(wxGraphicsContext::Create(dc
));
537 context
->SetBrush(*wxWHITE
);
538 context
->FillPath(m_shapePath
);
541 bmp
.SetMask(new wxMask(bmp
, *wxBLACK
));
543 return DoSetRegionShape(wxRegion(bmp
));
546 #endif // wxUSE_GRAPHICS_CONTEXT