From 2b99f9287294754ee7d24aae2a9c3330791b55f9 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Sun, 18 May 2014 10:44:24 +0000 Subject: [PATCH] refactoring to common code for focus set and lost events, so that changes can be made a single place, see #14269 git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76575 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/cocoa/private.h | 2 ++ src/osx/cocoa/window.mm | 52 ++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index 0a8f766a41..865a4c6395 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -131,6 +131,8 @@ public : virtual bool DoHandleMouseEvent(NSEvent *event); virtual bool DoHandleKeyEvent(NSEvent *event); virtual bool DoHandleCharEvent(NSEvent *event, NSString *text); + virtual void DoNotifyFocusSet(); + virtual void DoNotifyFocusLost(); virtual void DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow); virtual void SetupKeyEvent(wxKeyEvent &wxevent, NSEvent * nsEvent, NSString* charString = NULL); diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index 7015088eea..c6f0756b4c 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -1479,15 +1479,9 @@ bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd) bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd) { wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; - // get the current focus before running becomeFirstResponder - NSView* otherView = FindFocus(); - - wxWidgetImpl* otherWindow = FindFromWXWidget(otherView); BOOL r = superimpl(slf, (SEL)_cmd); if ( r ) - { - DoNotifyFocusEvent( true, otherWindow ); - } + DoNotifyFocusSet(); return r; } @@ -1496,23 +1490,13 @@ bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd) { wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; BOOL r = superimpl(slf, (SEL)_cmd); - - NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder(); - NSView* otherView = wxOSXGetViewFromResponder(responder); - - wxWidgetImpl* otherWindow = FindBestFromWXWidget(otherView); - // It doesn't make sense to notify about the loss of focus if it's the same - // control in the end, and just a different subview - if ( otherWindow == this ) - return r; - - // NSTextViews have an editor as true responder, therefore the might get the - // resign notification if their editor takes over, don't trigger any event then + // wxNSTextFields and wxNSComboBoxes have an editor as real responder, therefore they get + // a resign notification when their editor takes over, don't trigger event here, the control + // gets a controlTextDidEndEditing notification which will send a focus kill. if ( r && !m_hasEditor) - { - DoNotifyFocusEvent( false, otherWindow ); - } + DoNotifyFocusLost(); + return r; } @@ -2785,6 +2769,30 @@ bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event) return result; } +void wxWidgetCocoaImpl::DoNotifyFocusSet() +{ + NSResponder* responder = wxNonOwnedWindowCocoaImpl::GetFormerFirstResponder(); + NSView* otherView = wxOSXGetViewFromResponder(responder); + wxWidgetImpl* otherWindow = FindFromWXWidget(otherView); + + // It doesn't make sense to notify about the focus set if it's the same + // control in the end, and just a different subview + if ( otherWindow != this ) + DoNotifyFocusEvent(true, otherWindow); +} + +void wxWidgetCocoaImpl::DoNotifyFocusLost() +{ + NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder(); + NSView* otherView = wxOSXGetViewFromResponder(responder); + wxWidgetImpl* otherWindow = FindBestFromWXWidget(otherView); + + // It doesn't make sense to notify about the loss of focus if it's the same + // control in the end, and just a different subview + if ( otherWindow != this ) + DoNotifyFocusEvent( false, otherWindow ); +} + void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow) { wxWindow* thisWindow = GetWXPeer();