+// trace mask for activation tracing messages
+#define TRACE_ACTIVATE "activation"
+
+wxWindow* g_MacLastWindow = NULL ;
+
+// unified title and toolbar constant - not in Tiger headers, so we duplicate it here
+#define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
+
+// ---------------------------------------------------------------------------
+// wxWindowMac utility functions
+// ---------------------------------------------------------------------------
+
+// Find an item given the Macintosh Window Reference
+
+WX_DECLARE_HASH_MAP(WXWindow, wxNonOwnedWindow*, wxPointerHash, wxPointerEqual, MacWindowMap);
+
+static MacWindowMap wxWinMacWindowList;
+
+wxNonOwnedWindow *wxFindWindowFromWXWindow(WXWindow inWindowRef)
+{
+ MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
+
+ return (node == wxWinMacWindowList.end()) ? NULL : node->second;
+}
+
+void wxAssociateWindowWithWXWindow(WXWindow inWindowRef, wxNonOwnedWindow *win) ;
+void wxAssociateWindowWithWXWindow(WXWindow inWindowRef, wxNonOwnedWindow *win)
+{
+ // adding NULL WindowRef is (first) surely a result of an error and
+ // nothing else :-)
+ wxCHECK_RET( inWindowRef != (WXWindow) NULL, wxT("attempt to add a NULL WindowRef to window list") );
+
+ wxWinMacWindowList[inWindowRef] = win;
+}
+
+void wxRemoveWXWindowAssociation(wxNonOwnedWindow *win) ;
+void wxRemoveWXWindowAssociation(wxNonOwnedWindow *win)
+{
+ MacWindowMap::iterator it;
+ for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
+ {
+ if ( it->second == win )
+ {
+ wxWinMacWindowList.erase(it);
+ break;
+ }
+ }
+}
+
+wxNonOwnedWindow* wxNonOwnedWindow::GetFromWXWindow( WXWindow win )
+{
+ return wxFindWindowFromWXWindow( win );
+}
+
+// ----------------------------------------------------------------------------
+// wxNonOwnedWindow creation
+// ----------------------------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl , wxObject )
+
+wxNonOwnedWindow *wxNonOwnedWindow::s_macDeactivateWindow = NULL;
+
+void wxNonOwnedWindow::Init()
+{
+ m_nowpeer = NULL;
+}
+
+bool wxNonOwnedWindow::Create(wxWindow *parent,
+ wxWindowID id,
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxString& name)
+{
+ // init our fields
+ Init();
+
+ m_windowStyle = style;
+
+ SetName( name );
+
+ m_windowId = id == -1 ? NewControlId() : id;
+ m_windowStyle = style;
+ m_isShown = false;
+
+ // create frame.
+ int x = (int)pos.x;
+ int y = (int)pos.y;
+
+ wxRect display = wxGetClientDisplayRect() ;
+
+ if ( x == wxDefaultPosition.x )
+ x = display.x ;
+
+ if ( y == wxDefaultPosition.y )
+ y = display.y ;
+
+ int w = WidthDefault(size.x);
+ int h = HeightDefault(size.y);
+
+ // temporary define, TODO
+#if wxOSX_USE_CARBON
+ m_nowpeer = new wxNonOwnedWindowCarbonImpl( this );
+#elif wxOSX_USE_COCOA
+ m_nowpeer = new wxNonOwnedWindowCocoaImpl( this );
+#elif wxOSX_USE_IPHONE
+ m_nowpeer = new wxNonOwnedWindowIPhoneImpl( this );
+#endif
+
+ m_nowpeer->Create( parent, wxPoint(x,y) , wxSize(w,h) , style , GetExtraStyle(), name ) ;
+ wxAssociateWindowWithWXWindow( m_nowpeer->GetWXWindow() , this ) ;
+#if wxOSX_USE_CARBON
+ // temporary cast, TODO
+ m_peer = (wxMacControl*) wxWidgetImpl::CreateContentView(this);
+#else
+ m_peer = wxWidgetImpl::CreateContentView(this);
+#endif
+
+ DoSetWindowVariant( m_windowVariant ) ;
+
+ wxWindowCreateEvent event(this);
+ HandleWindowEvent(event);
+
+ SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ));
+
+ if ( parent )
+ parent->AddChild(this);
+
+ return true;
+}
+
+wxNonOwnedWindow::~wxNonOwnedWindow()
+{
+ wxRemoveWXWindowAssociation( this ) ;
+ if ( m_nowpeer )
+ m_nowpeer->Destroy();
+
+ // avoid dangling refs
+ if ( s_macDeactivateWindow == this )
+ s_macDeactivateWindow = NULL;
+}
+
+// ----------------------------------------------------------------------------
+// wxNonOwnedWindow misc
+// ----------------------------------------------------------------------------
+
+bool wxNonOwnedWindow::ShowWithEffect(wxShowEffect effect,
+ unsigned timeout )
+{
+ if ( !wxWindow::Show(true) )
+ return false;
+
+ // because apps expect a size event to occur at this moment
+ wxSizeEvent event(GetSize() , m_windowId);
+ event.SetEventObject(this);
+ HandleWindowEvent(event);
+
+
+ return m_nowpeer->ShowWithEffect(true, effect, timeout);
+}
+
+bool wxNonOwnedWindow::HideWithEffect(wxShowEffect effect,
+ unsigned timeout )
+{
+ if ( !wxWindow::Show(false) )
+ return false;
+
+ return m_nowpeer->ShowWithEffect(false, effect, timeout);
+}
+
+wxPoint wxNonOwnedWindow::GetClientAreaOrigin() const
+{
+ int left, top, width, height;
+ m_nowpeer->GetContentArea(left, top, width, height);
+ return wxPoint(left, top);
+}
+
+bool wxNonOwnedWindow::SetBackgroundColour(const wxColour& c )
+{
+ if ( !wxWindow::SetBackgroundColour(c) && m_hasBgCol )
+ return false ;
+
+ if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM )
+ {
+ return m_nowpeer->SetBackgroundColour(c);
+ }
+ return true;
+}
+
+// Raise the window to the top of the Z order
+void wxNonOwnedWindow::Raise()
+{
+ m_nowpeer->Raise();
+}
+
+// Lower the window to the bottom of the Z order
+void wxNonOwnedWindow::Lower()
+{
+ m_nowpeer->Lower();
+}
+
+void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp)
+{
+ if (s_macDeactivateWindow)
+ {
+ wxLogTrace(TRACE_ACTIVATE,
+ wxT("Doing delayed deactivation of %p"),
+ s_macDeactivateWindow);
+
+ s_macDeactivateWindow->MacActivate(timestamp, false);
+ }
+}
+
+void wxNonOwnedWindow::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
+{
+ wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
+
+ if (s_macDeactivateWindow == this)
+ s_macDeactivateWindow = NULL;
+
+ MacDelayedDeactivation(timestamp);
+}
+
+bool wxNonOwnedWindow::Show(bool show)
+{
+ if ( !wxWindow::Show(show) )
+ return false;
+
+ if ( m_nowpeer )
+ m_nowpeer->Show(show);
+
+ if ( show )
+ {
+ // because apps expect a size event to occur at this moment
+ wxSizeEvent event(GetSize() , m_windowId);
+ event.SetEventObject(this);
+ HandleWindowEvent(event);
+ }
+
+ return true ;
+}
+
+bool wxNonOwnedWindow::SetTransparent(wxByte alpha)
+{
+ return m_nowpeer->SetTransparent(alpha);
+}
+
+
+bool wxNonOwnedWindow::CanSetTransparent()
+{
+ return m_nowpeer->CanSetTransparent();
+}
+
+
+void wxNonOwnedWindow::SetExtraStyle(long exStyle)
+{
+ if ( GetExtraStyle() == exStyle )
+ return ;
+
+ wxWindow::SetExtraStyle( exStyle ) ;
+
+ if ( m_nowpeer )
+ m_nowpeer->SetExtraStyle(exStyle);
+}
+
+bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style)
+{
+ if ( !wxWindow::SetBackgroundStyle(style) )
+ return false ;
+
+ return m_nowpeer->SetBackgroundStyle(style);
+}
+
+void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height)
+{
+ m_cachedClippedRectValid = false ;
+
+ m_nowpeer->MoveWindow(x, y, width, height);
+ wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
+}
+
+void wxNonOwnedWindow::DoGetPosition( int *x, int *y ) const
+{
+ int x1,y1 ;
+ m_nowpeer->GetPosition(x1, y1);
+
+ if (x)
+ *x = x1 ;
+ if (y)
+ *y = y1 ;
+}
+
+void wxNonOwnedWindow::DoGetSize( int *width, int *height ) const
+{
+ int w,h;
+
+ m_nowpeer->GetSize(w, h);
+
+ if (width)
+ *width = w ;
+ if (height)
+ *height = h ;
+}
+
+void wxNonOwnedWindow::DoGetClientSize( int *width, int *height ) const
+{
+ int left, top, w, h;
+ m_nowpeer->GetContentArea(left, top, w, h);
+
+ if (width)
+ *width = w ;
+ if (height)
+ *height = h ;
+}
+
+
+void wxNonOwnedWindow::Update()
+{
+ m_nowpeer->Update();
+}
+
+WXWindow wxNonOwnedWindow::GetWXWindow() const
+{
+ return m_nowpeer ? m_nowpeer->GetWXWindow() : NULL;
+}
+
+// ---------------------------------------------------------------------------
+// Shape implementation
+// ---------------------------------------------------------------------------
+
+
+bool wxNonOwnedWindow::SetShape(const wxRegion& region)
+{
+ wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
+ _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
+
+ // The empty region signifies that the shape
+ // should be removed from the window.
+ if ( region.IsEmpty() )
+ {
+ wxSize sz = GetClientSize();
+ wxRegion rgn(0, 0, sz.x, sz.y);
+ if ( rgn.IsEmpty() )
+ return false ;
+ else
+ return SetShape(rgn);
+ }
+
+ return m_nowpeer->SetShape(region);
+}
+
+//
+// TODO END move to nonowned_osx.cpp
+//
+
+#if wxOSX_USE_CARBON
+
+IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCarbonImpl , wxNonOwnedWindowImpl )
+
+
+WXWindow wxNonOwnedWindowCarbonImpl::GetWXWindow() const
+{
+ return (WXWindow) m_macWindow;
+}
+void wxNonOwnedWindowCarbonImpl::Raise()
+{
+ ::SelectWindow( m_macWindow ) ;
+}
+
+void wxNonOwnedWindowCarbonImpl::Lower()
+{
+ ::SendBehind( m_macWindow , NULL ) ;
+}
+
+bool wxNonOwnedWindowCarbonImpl::Show(bool show)
+{
+ bool plainTransition = true;
+
+#if wxUSE_SYSTEM_OPTIONS
+ if ( wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) )
+ plainTransition = ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1 ) ;
+#endif
+
+ if (show)
+ {
+#if wxOSX_USE_CARBON
+ if ( plainTransition )
+ ::ShowWindow( (WindowRef)m_macWindow );
+ else
+ ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, NULL );
+
+ ::SelectWindow( (WindowRef)m_macWindow ) ;
+#endif
+ }
+ else
+ {
+#if wxOSX_USE_CARBON
+ if ( plainTransition )
+ ::HideWindow( (WindowRef)m_macWindow );
+ else
+ ::TransitionWindow( (WindowRef)m_macWindow, kWindowZoomTransitionEffect, kWindowHideTransitionAction, NULL );
+#endif
+ }
+ return true;
+}
+
+void wxNonOwnedWindowCarbonImpl::Update()
+{
+ HIWindowFlush(m_macWindow) ;
+}
+
+bool wxNonOwnedWindowCarbonImpl::SetTransparent(wxByte alpha)
+{
+ OSStatus result = SetWindowAlpha((WindowRef)m_macWindow, (CGFloat)((alpha)/255.0));
+ return result == noErr;
+}
+
+bool wxNonOwnedWindowCarbonImpl::SetBackgroundColour(const wxColour& col )
+{
+ if ( col == wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDocumentWindowBackground)) )
+ {
+ SetThemeWindowBackground( (WindowRef) m_macWindow, kThemeBrushDocumentWindowBackground, false ) ;
+ SetBackgroundStyle(wxBG_STYLE_SYSTEM);
+ }
+ else if ( col == wxColour(wxMacCreateCGColorFromHITheme(kThemeBrushDialogBackgroundActive)) )
+ {
+ SetThemeWindowBackground( (WindowRef) m_macWindow, kThemeBrushDialogBackgroundActive, false ) ;
+ SetBackgroundStyle(wxBG_STYLE_SYSTEM);
+ }
+ return true;
+}
+
+void wxNonOwnedWindowCarbonImpl::SetExtraStyle( long exStyle )
+{
+ if ( m_macWindow != NULL )
+ {
+ bool metal = exStyle & wxFRAME_EX_METAL ;
+
+ if ( MacGetMetalAppearance() != metal )
+ {
+ if ( MacGetUnifiedAppearance() )
+ MacSetUnifiedAppearance( !metal ) ;
+
+ MacSetMetalAppearance( metal ) ;
+ }
+ }
+}
+
+bool wxNonOwnedWindowCarbonImpl::SetBackgroundStyle(wxBackgroundStyle style)
+{
+ if ( style == wxBG_STYLE_TRANSPARENT )
+ {
+ OSStatus err = HIWindowChangeFeatures( m_macWindow, 0, kWindowIsOpaque );
+ verify_noerr( err );
+ err = ReshapeCustomWindow( m_macWindow );
+ verify_noerr( err );
+ }
+
+ return true ;
+}
+
+bool wxNonOwnedWindowCarbonImpl::CanSetTransparent()
+{
+ return true;
+}
+
+void wxNonOwnedWindowCarbonImpl::GetContentArea( int &left , int &top , int &width , int &height ) const
+{
+ Rect content, structure ;
+
+ GetWindowBounds( m_macWindow, kWindowStructureRgn , &structure ) ;
+ GetWindowBounds( m_macWindow, kWindowContentRgn , &content ) ;
+
+ left = content.left - structure.left ;
+ top = content.top - structure.top ;
+ width = content.right - content.left ;
+ height = content.bottom - content.top ;
+}
+
+void wxNonOwnedWindowCarbonImpl::MoveWindow(int x, int y, int width, int height)
+{
+ Rect bounds = { y , x , y + height , x + width } ;
+ verify_noerr(SetWindowBounds( (WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
+}
+
+void wxNonOwnedWindowCarbonImpl::GetPosition( int &x, int &y ) const
+{
+ Rect bounds ;
+
+ verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
+
+ x = bounds.left ;
+ y = bounds.top ;
+}
+
+void wxNonOwnedWindowCarbonImpl::GetSize( int &width, int &height ) const
+{
+ Rect bounds ;
+
+ verify_noerr(GetWindowBounds((WindowRef) m_macWindow, kWindowStructureRgn , &bounds )) ;
+
+ width = bounds.right - bounds.left ;
+ height = bounds.bottom - bounds.top ;
+}
+
+bool wxNonOwnedWindowCarbonImpl::MacGetUnifiedAppearance() const
+{
+ return MacGetWindowAttributes() & kWindowUnifiedTitleAndToolbarAttribute ;
+}
+
+void wxNonOwnedWindowCarbonImpl::MacChangeWindowAttributes( wxUint32 attributesToSet , wxUint32 attributesToClear )
+{
+ ChangeWindowAttributes( m_macWindow, attributesToSet, attributesToClear ) ;
+}
+
+wxUint32 wxNonOwnedWindowCarbonImpl::MacGetWindowAttributes() const
+{
+ UInt32 attr = 0 ;
+ GetWindowAttributes( m_macWindow, &attr ) ;
+ return attr ;
+}
+
+void wxNonOwnedWindowCarbonImpl::MacSetMetalAppearance( bool set )
+{
+ if ( MacGetUnifiedAppearance() )
+ MacSetUnifiedAppearance( false ) ;
+
+ MacChangeWindowAttributes( set ? kWindowMetalAttribute : kWindowNoAttributes ,
+ set ? kWindowNoAttributes : kWindowMetalAttribute ) ;
+}
+
+bool wxNonOwnedWindowCarbonImpl::MacGetMetalAppearance() const
+{
+ return MacGetWindowAttributes() & kWindowMetalAttribute ;
+}
+
+void wxNonOwnedWindowCarbonImpl::MacSetUnifiedAppearance( bool set )
+{
+ if ( MacGetMetalAppearance() )
+ MacSetMetalAppearance( false ) ;
+
+ MacChangeWindowAttributes( set ? kWindowUnifiedTitleAndToolbarAttribute : kWindowNoAttributes ,
+ set ? kWindowNoAttributes : kWindowUnifiedTitleAndToolbarAttribute) ;
+
+ // For some reason, Tiger uses white as the background color for this appearance,
+ // while most apps using it use the typical striped background. Restore that behavior
+ // for wx.
+ // TODO: Determine if we need this on Leopard as well. (should be harmless either way,
+ // though)
+ m_wxPeer->SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW) ) ;
+}