1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/nonownedwnd_osx.cpp
3 // Purpose: implementation of wxNonOwnedWindow
4 // Author: Stefan Csomor
6 // Copyright: (c) Stefan Csomor 2008
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/dcmemory.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 clock_t wxNonOwnedWindow::s_lastFlush
= 0;
43 // unified title and toolbar constant - not in Tiger headers, so we duplicate it here
44 #define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
46 // ---------------------------------------------------------------------------
47 // wxWindowMac utility functions
48 // ---------------------------------------------------------------------------
50 WX_DECLARE_HASH_MAP(WXWindow
, wxNonOwnedWindowImpl
*, wxPointerHash
, wxPointerEqual
, MacWindowMap
);
52 static MacWindowMap wxWinMacWindowList
;
54 wxNonOwnedWindow
* wxNonOwnedWindow::GetFromWXWindow( WXWindow win
)
56 wxNonOwnedWindowImpl
* impl
= wxNonOwnedWindowImpl::FindFromWXWindow(win
);
58 return ( impl
!= NULL
? impl
->GetWXPeer() : NULL
) ;
61 wxNonOwnedWindowImpl
* wxNonOwnedWindowImpl::FindFromWXWindow (WXWindow window
)
63 MacWindowMap::iterator node
= wxWinMacWindowList
.find(window
);
65 return (node
== wxWinMacWindowList
.end()) ? NULL
: node
->second
;
68 void wxNonOwnedWindowImpl::RemoveAssociations( wxNonOwnedWindowImpl
* impl
)
70 MacWindowMap::iterator it
;
71 for ( it
= wxWinMacWindowList
.begin(); it
!= wxWinMacWindowList
.end(); ++it
)
73 if ( it
->second
== impl
)
75 wxWinMacWindowList
.erase(it
);
81 void wxNonOwnedWindowImpl::Associate( WXWindow window
, wxNonOwnedWindowImpl
*impl
)
83 // adding NULL WindowRef is (first) surely a result of an error and
85 wxCHECK_RET( window
!= (WXWindow
) NULL
, wxT("attempt to add a NULL WindowRef to window list") );
87 wxWinMacWindowList
[window
] = impl
;
90 // ----------------------------------------------------------------------------
91 // wxNonOwnedWindow creation
92 // ----------------------------------------------------------------------------
94 IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl
, wxObject
)
96 wxNonOwnedWindow
*wxNonOwnedWindow::s_macDeactivateWindow
= NULL
;
98 void wxNonOwnedWindow::Init()
101 m_isNativeWindowWrapper
= false;
104 bool wxNonOwnedWindow::Create(wxWindow
*parent
,
106 const wxPoint
& posOrig
,
107 const wxSize
& sizeOrig
,
109 const wxString
& name
)
111 m_windowStyle
= style
;
115 m_windowId
= id
== -1 ? NewControlId() : id
;
116 m_windowStyle
= style
;
119 // use the appropriate defaults for the position and size if necessary
120 wxSize
size(sizeOrig
);
121 if ( !size
.IsFullySpecified() )
122 size
.SetDefaults(wxTopLevelWindow::GetDefaultSize());
124 wxPoint
pos(posOrig
);
125 if ( !pos
.IsFullySpecified() )
127 wxRect rectWin
= wxRect(size
).CentreIn(wxGetClientDisplayRect());
129 // The size of the window is often not really known yet, TLWs are often
130 // created with some small initial size and later are fitted to contain
131 // their children so centering the window will show it too much to the
132 // right and bottom, adjust for it by putting it more to the left and
137 pos
.SetDefaults(rectWin
.GetPosition());
141 m_nowpeer
= wxNonOwnedWindowImpl::CreateNonOwnedWindow
145 style
, GetExtraStyle(),
148 wxNonOwnedWindowImpl::Associate( m_nowpeer
->GetWXWindow() , m_nowpeer
) ;
149 SetPeer(wxWidgetImpl::CreateContentView(this));
151 DoSetWindowVariant( m_windowVariant
) ;
153 wxWindowCreateEvent
event(this);
154 HandleWindowEvent(event
);
156 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
));
159 parent
->AddChild(this);
164 bool wxNonOwnedWindow::Create(wxWindow
*parent
, WXWindow nativeWindow
)
167 parent
->AddChild(this);
169 SubclassWin(nativeWindow
);
174 void wxNonOwnedWindow::SubclassWin(WXWindow nativeWindow
)
176 wxASSERT_MSG( !m_isNativeWindowWrapper
, wxT("subclassing window twice?") );
177 wxASSERT_MSG( m_nowpeer
== NULL
, wxT("window already was created") );
179 m_nowpeer
= wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, GetParent(), nativeWindow
);
180 m_isNativeWindowWrapper
= true;
181 wxNonOwnedWindowImpl::Associate( m_nowpeer
->GetWXWindow() , m_nowpeer
) ;
182 SetPeer(wxWidgetImpl::CreateContentView(this));
185 void wxNonOwnedWindow::UnsubclassWin()
187 wxASSERT_MSG( m_isNativeWindowWrapper
, wxT("window was not subclassed") );
190 GetParent()->RemoveChild(this);
192 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer
) ;
195 m_isNativeWindowWrapper
= false;
199 wxNonOwnedWindow::~wxNonOwnedWindow()
203 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer
) ;
209 // avoid dangling refs
210 if ( s_macDeactivateWindow
== this )
211 s_macDeactivateWindow
= NULL
;
214 bool wxNonOwnedWindow::Destroy()
218 return wxWindow::Destroy();
221 void wxNonOwnedWindow::WillBeDestroyed()
224 m_nowpeer
->WillBeDestroyed();
227 // ----------------------------------------------------------------------------
228 // wxNonOwnedWindow misc
229 // ----------------------------------------------------------------------------
231 bool wxNonOwnedWindow::OSXShowWithEffect(bool show
,
235 // Cocoa code needs to manage window visibility on its own and so calls
236 // wxWindow::Show() as needed but if we already changed the internal
237 // visibility flag here, Show() would do nothing, so avoid doing it
239 if ( !wxWindow::Show(show
) )
243 if ( effect
== wxSHOW_EFFECT_NONE
||
244 !m_nowpeer
|| !m_nowpeer
->ShowWithEffect(show
, effect
, timeout
) )
249 // as apps expect a size event to occur when the window is shown,
250 // generate one when it is shown with effect too
257 wxPoint
wxNonOwnedWindow::GetClientAreaOrigin() const
259 int left
, top
, width
, height
;
260 m_nowpeer
->GetContentArea(left
, top
, width
, height
);
261 return wxPoint(left
, top
);
264 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour
& c
)
266 if ( !wxWindow::SetBackgroundColour(c
) && m_hasBgCol
)
269 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM
)
272 return m_nowpeer
->SetBackgroundColour(c
);
277 void wxNonOwnedWindow::SetWindowStyleFlag(long flags
)
279 if (flags
== GetWindowStyleFlag())
282 wxWindow::SetWindowStyleFlag(flags
);
285 m_nowpeer
->SetWindowStyleFlag(flags
);
288 // Raise the window to the top of the Z order
289 void wxNonOwnedWindow::Raise()
294 // Lower the window to the bottom of the Z order
295 void wxNonOwnedWindow::Lower()
300 void wxNonOwnedWindow::HandleActivated( double timestampsec
, bool didActivate
)
302 MacActivate( (int) (timestampsec
* 1000) , didActivate
);
303 wxActivateEvent
wxevent(wxEVT_ACTIVATE
, didActivate
, GetId());
304 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
305 wxevent
.SetEventObject(this);
306 HandleWindowEvent(wxevent
);
309 void wxNonOwnedWindow::HandleResized( double WXUNUSED(timestampsec
) )
312 // we have to inform some controls that have to reset things
313 // relative to the toplevel window (e.g. OpenGL buffers)
314 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
317 void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec
), wxRect
* rect
)
321 // this is a EVT_SIZING not a EVT_SIZE type !
322 wxSizeEvent
wxevent( r
, GetId() ) ;
323 wxevent
.SetEventObject( this ) ;
324 if ( HandleWindowEvent(wxevent
) )
325 r
= wxevent
.GetRect() ;
327 if ( GetMaxWidth() != -1 && r
.GetWidth() > GetMaxWidth() )
328 r
.SetWidth( GetMaxWidth() ) ;
329 if ( GetMaxHeight() != -1 && r
.GetHeight() > GetMaxHeight() )
330 r
.SetHeight( GetMaxHeight() ) ;
331 if ( GetMinWidth() != -1 && r
.GetWidth() < GetMinWidth() )
332 r
.SetWidth( GetMinWidth() ) ;
333 if ( GetMinHeight() != -1 && r
.GetHeight() < GetMinHeight() )
334 r
.SetHeight( GetMinHeight() ) ;
337 // TODO actuall this is too early, in case the window extents are adjusted
338 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
341 void wxNonOwnedWindow::HandleMoved( double timestampsec
)
343 wxMoveEvent
wxevent( GetPosition() , GetId());
344 wxevent
.SetTimestamp( (int) (timestampsec
* 1000) );
345 wxevent
.SetEventObject( this );
346 HandleWindowEvent(wxevent
);
349 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp
)
351 if (s_macDeactivateWindow
)
353 wxLogTrace(TRACE_ACTIVATE
,
354 wxT("Doing delayed deactivation of %p"),
355 s_macDeactivateWindow
);
357 s_macDeactivateWindow
->MacActivate(timestamp
, false);
361 void wxNonOwnedWindow::MacActivate( long timestamp
, bool WXUNUSED(inIsActivating
) )
363 wxLogTrace(TRACE_ACTIVATE
, wxT("TopLevel=%p::MacActivate"), this);
365 if (s_macDeactivateWindow
== this)
366 s_macDeactivateWindow
= NULL
;
368 MacDelayedDeactivation(timestamp
);
371 bool wxNonOwnedWindow::Show(bool show
)
373 if ( !wxWindow::Show(show
) )
377 m_nowpeer
->Show(show
);
381 // because apps expect a size event to occur at this moment
388 bool wxNonOwnedWindow::SetTransparent(wxByte alpha
)
390 return m_nowpeer
->SetTransparent(alpha
);
394 bool wxNonOwnedWindow::CanSetTransparent()
396 return m_nowpeer
->CanSetTransparent();
400 void wxNonOwnedWindow::SetExtraStyle(long exStyle
)
402 if ( GetExtraStyle() == exStyle
)
405 wxWindow::SetExtraStyle( exStyle
) ;
408 m_nowpeer
->SetExtraStyle(exStyle
);
411 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style
)
413 if ( !wxWindow::SetBackgroundStyle(style
) )
416 return m_nowpeer
? m_nowpeer
->SetBackgroundStyle(style
) : true;
419 void wxNonOwnedWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
421 if ( m_nowpeer
== NULL
)
424 m_cachedClippedRectValid
= false ;
426 m_nowpeer
->MoveWindow(x
, y
, width
, height
);
427 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
430 void wxNonOwnedWindow::DoGetPosition( int *x
, int *y
) const
432 if ( m_nowpeer
== NULL
)
436 m_nowpeer
->GetPosition(x1
, y1
);
444 void wxNonOwnedWindow::DoGetSize( int *width
, int *height
) const
446 if ( m_nowpeer
== NULL
)
451 m_nowpeer
->GetSize(w
, h
);
459 void wxNonOwnedWindow::DoGetClientSize( int *width
, int *height
) const
461 if ( m_nowpeer
== NULL
)
465 // under iphone with a translucent status bar the m_nowpeer returns the
466 // inner area, while the content area extends under the translucent
467 // status bar, therefore we use the content view's area
468 #ifdef __WXOSX_IPHONE__
469 GetPeer()->GetContentArea(left
, top
, w
, h
);
471 m_nowpeer
->GetContentArea(left
, top
, w
, h
);
480 void wxNonOwnedWindow::WindowWasPainted()
482 s_lastFlush
= clock();
485 void wxNonOwnedWindow::Update()
487 if ( clock() - s_lastFlush
> CLOCKS_PER_SEC
/ 30 )
489 s_lastFlush
= clock();
494 WXWindow
wxNonOwnedWindow::GetWXWindow() const
496 return m_nowpeer
? m_nowpeer
->GetWXWindow() : NULL
;
499 void *wxNonOwnedWindow::OSXGetViewOrWindow() const
501 return GetWXWindow();
504 // ---------------------------------------------------------------------------
505 // Shape implementation
506 // ---------------------------------------------------------------------------
508 bool wxNonOwnedWindow::DoClearShape()
512 wxSize sz
= GetClientSize();
513 wxRegion
region(0, 0, sz
.x
, sz
.y
);
515 return m_nowpeer
->SetShape(region
);
518 bool wxNonOwnedWindow::DoSetRegionShape(const wxRegion
& region
)
522 return m_nowpeer
->SetShape(region
);
525 #if wxUSE_GRAPHICS_CONTEXT
527 #include "wx/scopedptr.h"
529 bool wxNonOwnedWindow::DoSetPathShape(const wxGraphicsPath
& path
)
533 // Convert the path to wxRegion by rendering the path on a window-sized
534 // bitmap, creating a mask from it and finally creating the region from
536 wxBitmap
bmp(GetSize());
540 dc
.SetBackground(*wxBLACK
);
543 wxScopedPtr
<wxGraphicsContext
> context(wxGraphicsContext::Create(dc
));
544 context
->SetBrush(*wxWHITE
);
545 context
->FillPath(m_shapePath
);
548 bmp
.SetMask(new wxMask(bmp
, *wxBLACK
));
550 return DoSetRegionShape(wxRegion(bmp
));
553 #endif // wxUSE_GRAPHICS_CONTEXT