+ result = noErr ;
+ }
+ }
+
+ if ( cEvent.GetKind() == kEventMouseUp && wxApp::s_captureWindow )
+ {
+ wxApp::s_captureWindow = NULL ;
+ // update cursor ?
+ }
+
+ // update cursor
+
+ wxWindow* cursorTarget = currentMouseWindow ;
+ wxPoint cursorPoint( wxevent.m_x , wxevent.m_y ) ;
+
+ while ( cursorTarget && !cursorTarget->MacSetupCursor( cursorPoint ) )
+ {
+ cursorTarget = cursorTarget->GetParent() ;
+ if ( cursorTarget )
+ cursorPoint += cursorTarget->GetPosition();
+ }
+
+ }
+ else // currentMouseWindow == NULL
+ {
+ // don't mess with controls we don't know about
+ // for some reason returning eventNotHandledErr does not lead to the correct behaviour
+ // so we try sending them the correct control directly
+ if ( cEvent.GetKind() == kEventMouseDown && toplevelWindow && control )
+ {
+ EventModifiers modifiers = cEvent.GetParameter<EventModifiers>(kEventParamKeyModifiers, typeUInt32) ;
+ Point clickLocation = windowMouseLocation ;
+#if TARGET_API_MAC_OSX
+ if ( toplevelWindow->MacUsesCompositing() )
+ {
+ HIPoint hiPoint ;
+ hiPoint.x = clickLocation.h ;
+ hiPoint.y = clickLocation.v ;
+ HIViewConvertPoint( &hiPoint , (ControlRef) toplevelWindow->GetHandle() , control ) ;
+ clickLocation.h = (int)hiPoint.x ;
+ clickLocation.v = (int)hiPoint.y ;
+ }
+#endif // TARGET_API_MAC_OSX
+
+ HandleControlClick( control , clickLocation , modifiers , (ControlActionUPP ) -1 ) ;
+ result = noErr ;
+ }
+ }
+
+ return result ;
+}
+
+static pascal OSStatus wxMacTopLevelWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+
+ wxMacCarbonEvent cEvent( event ) ;
+
+ // WindowRef windowRef = cEvent.GetParameter<WindowRef>(kEventParamDirectObject) ;
+ wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
+
+ switch ( GetEventKind( event ) )
+ {
+ case kEventWindowActivated :
+ {
+ toplevelWindow->MacActivate( cEvent.GetTicks() , true) ;
+ wxActivateEvent wxevent(wxEVT_ACTIVATE, true , toplevelWindow->GetId());
+ wxevent.SetTimestamp( cEvent.GetTicks() ) ;
+ wxevent.SetEventObject(toplevelWindow);
+ toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
+ // we still sending an eventNotHandledErr in order to allow for default processing
+ }
+ break ;
+
+ case kEventWindowDeactivated :
+ {
+ toplevelWindow->MacActivate(cEvent.GetTicks() , false) ;
+ wxActivateEvent wxevent(wxEVT_ACTIVATE, false , toplevelWindow->GetId());
+ wxevent.SetTimestamp( cEvent.GetTicks() ) ;
+ wxevent.SetEventObject(toplevelWindow);
+ toplevelWindow->GetEventHandler()->ProcessEvent(wxevent);
+ // we still sending an eventNotHandledErr in order to allow for default processing
+ }
+ break ;
+
+ case kEventWindowShown :
+ toplevelWindow->Refresh() ;
+ result = noErr ;
+ break ;
+
+ case kEventWindowClose :
+ toplevelWindow->Close() ;
+ result = noErr ;
+ break ;
+
+ case kEventWindowBoundsChanged :
+ {
+ UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes, typeUInt32) ;
+ Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
+ wxRect r( newRect.left , newRect.top , newRect.right - newRect.left , newRect.bottom - newRect.top ) ;
+ if ( attributes & kWindowBoundsChangeSizeChanged )
+ {
+ // according to the other ports we handle this within the OS level
+ // resize event, not within a wxSizeEvent
+ wxFrame *frame = wxDynamicCast( toplevelWindow , wxFrame ) ;
+ if ( frame )
+ {
+ frame->PositionBars();
+ }
+
+ wxSizeEvent event( r.GetSize() , toplevelWindow->GetId() ) ;
+ event.SetEventObject( toplevelWindow ) ;
+
+ toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
+ toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
+ }
+
+ if ( attributes & kWindowBoundsChangeOriginChanged )
+ {
+ wxMoveEvent event( r.GetLeftTop() , toplevelWindow->GetId() ) ;
+ event.SetEventObject( toplevelWindow ) ;
+ toplevelWindow->GetEventHandler()->ProcessEvent(event) ;
+ }
+
+ result = noErr ;
+ }
+ break ;
+
+ case kEventWindowBoundsChanging :
+ {
+ UInt32 attributes = cEvent.GetParameter<UInt32>(kEventParamAttributes,typeUInt32) ;
+ Rect newRect = cEvent.GetParameter<Rect>(kEventParamCurrentBounds) ;
+
+ if ( (attributes & kWindowBoundsChangeSizeChanged) || (attributes & kWindowBoundsChangeOriginChanged) )
+ {
+ // all (Mac) rects are in content area coordinates, all wxRects in structure coordinates
+ int left , top , right , bottom ;
+ toplevelWindow->MacGetContentAreaInset( left , top , right , bottom ) ;
+
+ wxRect r(
+ newRect.left - left,
+ newRect.top - top,
+ newRect.right - newRect.left + left + right,
+ newRect.bottom - newRect.top + top + bottom ) ;
+
+ // this is a EVT_SIZING not a EVT_SIZE type !
+ wxSizeEvent wxevent( r , toplevelWindow->GetId() ) ;
+ wxevent.SetEventObject( toplevelWindow ) ;
+ wxRect adjustR = r ;
+ if ( toplevelWindow->GetEventHandler()->ProcessEvent(wxevent) )
+ adjustR = wxevent.GetRect() ;
+
+ if ( toplevelWindow->GetMaxWidth() != -1 && adjustR.GetWidth() > toplevelWindow->GetMaxWidth() )
+ adjustR.SetWidth( toplevelWindow->GetMaxWidth() ) ;
+ if ( toplevelWindow->GetMaxHeight() != -1 && adjustR.GetHeight() > toplevelWindow->GetMaxHeight() )
+ adjustR.SetHeight( toplevelWindow->GetMaxHeight() ) ;
+ if ( toplevelWindow->GetMinWidth() != -1 && adjustR.GetWidth() < toplevelWindow->GetMinWidth() )
+ adjustR.SetWidth( toplevelWindow->GetMinWidth() ) ;
+ if ( toplevelWindow->GetMinHeight() != -1 && adjustR.GetHeight() < toplevelWindow->GetMinHeight() )
+ adjustR.SetHeight( toplevelWindow->GetMinHeight() ) ;
+ const Rect adjustedRect = { adjustR.y + top , adjustR.x + left , adjustR.y + adjustR.height - bottom , adjustR.x + adjustR.width - right } ;
+ if ( !EqualRect( &newRect , &adjustedRect ) )
+ cEvent.SetParameter<Rect>( kEventParamCurrentBounds , &adjustedRect ) ;
+ toplevelWindow->wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
+ }
+
+ result = noErr ;
+ }
+ break ;
+
+ default :
+ break ;
+ }
+
+ return result ;
+}
+
+// mix this in from window.cpp
+pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
+
+pascal OSStatus wxMacTopLevelEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+
+ switch ( GetEventClass( event ) )
+ {
+ case kEventClassTextInput :
+ result = wxMacUnicodeTextEventHandler( handler, event , data ) ;
+ break ;
+
+ case kEventClassKeyboard :
+ result = KeyboardEventHandler( handler, event , data ) ;
+ break ;
+
+ case kEventClassWindow :
+ result = wxMacTopLevelWindowEventHandler( handler, event , data ) ;
+ break ;
+
+ case kEventClassMouse :
+ result = wxMacTopLevelMouseEventHandler( handler, event , data ) ;
+ break ;
+
+ default :
+ break ;
+ }
+
+ return result ;
+}
+
+DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTopLevelEventHandler )
+
+// ---------------------------------------------------------------------------
+// wxWindowMac utility functions
+// ---------------------------------------------------------------------------
+
+// Find an item given the Macintosh Window Reference
+
+WX_DECLARE_HASH_MAP(WindowRef, wxTopLevelWindowMac*, wxPointerHash, wxPointerEqual, MacWindowMap);
+
+static MacWindowMap wxWinMacWindowList;
+
+wxTopLevelWindowMac *wxFindWinFromMacWindow(WindowRef inWindowRef)
+{
+ MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
+
+ return (node == wxWinMacWindowList.end()) ? NULL : node->second;
+}
+
+void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win) ;
+void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
+{
+ // adding NULL WindowRef is (first) surely a result of an error and
+ // nothing else :-)
+ wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
+
+ wxWinMacWindowList[inWindowRef] = win;
+}
+
+void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win) ;
+void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
+{
+ MacWindowMap::iterator it;
+ for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
+ {
+ if ( it->second == win )
+ {
+ wxWinMacWindowList.erase(it);
+ break;
+ }
+ }
+}
+
+// ----------------------------------------------------------------------------
+// wxTopLevelWindowMac creation
+// ----------------------------------------------------------------------------
+
+wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
+
+typedef struct
+{
+ wxPoint m_position ;
+ wxSize m_size ;
+ bool m_wasResizable ;
+}
+FullScreenData ;
+
+void wxTopLevelWindowMac::Init()
+{
+ m_iconized =
+ m_maximizeOnShow = false;
+ m_macWindow = NULL ;
+
+#if TARGET_API_MAC_OSX
+ m_macUsesCompositing = ( UMAGetSystemVersion() >= 0x1030 );
+#else
+ m_macUsesCompositing = false;
+#endif
+
+ m_macEventHandler = NULL ;
+ m_macFullScreenData = NULL ;
+}
+
+class wxMacDeferredWindowDeleter : public wxObject
+{
+public :
+ wxMacDeferredWindowDeleter( WindowRef windowRef )
+ {
+ m_macWindow = windowRef ;
+ }
+
+ virtual ~wxMacDeferredWindowDeleter()
+ {
+ UMADisposeWindow( (WindowRef) m_macWindow ) ;
+ }
+
+protected :
+ WindowRef m_macWindow ;
+} ;
+
+bool wxTopLevelWindowMac::Create(wxWindow *parent,
+ wxWindowID id,
+ const wxString& title,
+ 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;
+ wxWindow::SetLabel( title ) ;
+
+ MacCreateRealWindow( title, pos , size , MacRemoveBordersFromStyle(style) , name ) ;
+
+ SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
+
+ if (GetExtraStyle() & wxFRAME_EX_METAL)
+ MacSetMetalAppearance(true);
+
+ wxTopLevelWindows.Append(this);
+
+ if ( parent )
+ parent->AddChild(this);
+
+ return true;
+}
+
+wxTopLevelWindowMac::~wxTopLevelWindowMac()
+{
+ if ( m_macWindow )
+ {
+#if wxUSE_TOOLTIPS
+ wxToolTip::NotifyWindowDelete(m_macWindow) ;
+#endif
+ wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
+ }
+
+ if ( m_macEventHandler )
+ {
+ ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
+ m_macEventHandler = NULL ;
+ }
+
+ wxRemoveMacWindowAssociation( this ) ;
+
+ if ( wxModelessWindows.Find(this) )
+ wxModelessWindows.DeleteObject(this);
+
+ FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
+ delete data ;
+ m_macFullScreenData = NULL ;
+}
+
+
+// ----------------------------------------------------------------------------
+// wxTopLevelWindowMac maximize/minimize
+// ----------------------------------------------------------------------------
+
+void wxTopLevelWindowMac::Maximize(bool maximize)
+{
+ // TODO: check if this is still necessary
+#if 0
+ wxMacPortStateHelper help( (GrafPtr)GetWindowPort( (WindowRef)m_macWindow) ) ;
+ wxMacWindowClipper clip( this );
+#endif
+#if 0
+ if ( !IsWindowInStandardState( (WindowRef)m_macWindow, NULL, NULL ) )
+ {
+ Rect rect;
+
+ GetWindowBounds((WindowRef)m_macWindow, kWindowGlobalPortRgn, &rect);
+ SetWindowIdealUserState((WindowRef)m_macWindow, &rect);
+ SetWindowUserState((WindowRef)m_macWindow, &rect);
+ }
+ ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
+#else
+ Point idealSize = { 0 , 0 } ;
+ if ( maximize )
+ {
+ Rect rect ;
+ GetAvailableWindowPositioningBounds(GetMainDevice(),&rect) ;
+ idealSize.h = rect.right - rect.left ;
+ idealSize.v = rect.bottom - rect.top ;
+ }
+ ZoomWindowIdeal( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , &idealSize ) ;
+#endif
+}
+
+bool wxTopLevelWindowMac::IsMaximized() const
+{
+ return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
+}
+
+void wxTopLevelWindowMac::Iconize(bool iconize)
+{
+ if ( IsWindowCollapsable( (WindowRef)m_macWindow) )
+ CollapseWindow( (WindowRef)m_macWindow , iconize ) ;
+}
+
+bool wxTopLevelWindowMac::IsIconized() const
+{
+ return IsWindowCollapsed((WindowRef)m_macWindow ) ;
+}
+
+void wxTopLevelWindowMac::Restore()
+{
+ if ( IsMaximized() )
+ Maximize(false);
+ else if ( IsIconized() )
+ Iconize(false);
+}
+
+// ----------------------------------------------------------------------------
+// wxTopLevelWindowMac misc
+// ----------------------------------------------------------------------------
+
+wxPoint wxTopLevelWindowMac::GetClientAreaOrigin() const
+{
+ return wxPoint(0, 0) ;
+}
+
+void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
+{
+ // this sets m_icon
+ wxTopLevelWindowBase::SetIcon(icon);
+}
+
+void wxTopLevelWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
+{
+ wxTopLevelWindowBase::MacSetBackgroundBrush( brush ) ;
+
+ if ( m_macBackgroundBrush.Ok() && m_macBackgroundBrush.GetStyle() != wxTRANSPARENT && m_macBackgroundBrush.MacGetBrushKind() == kwxMacBrushTheme )
+ {
+ SetThemeWindowBackground( (WindowRef) m_macWindow , m_macBackgroundBrush.MacGetTheme() , false ) ;
+ }
+}
+
+void wxTopLevelWindowMac::MacInstallTopLevelWindowEventHandler()