From 73f6bf7b6f12d98616f94234704cb0b9d250492f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= Date: Wed, 19 Oct 2016 17:07:13 +0200 Subject: [PATCH] Allow wxDataViewCtrl to gain focus in wxOSX Fix numerous focus-handling bugs in wxOSX in combination with NSScrollView (which cannot get focus by itself and which was already treated specially in many, but not all, places), including - inability to set the focus - loss of wxEVT_SET_FOCUS events - loss of wxWindow<->NSView association after clearing wxDVC columns --- src/osx/cocoa/dataview.mm | 15 ++++++++++++--- src/osx/cocoa/window.mm | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/osx/cocoa/dataview.mm b/src/osx/cocoa/dataview.mm index 3f49769f10..9c59947b94 100644 --- a/src/osx/cocoa/dataview.mm +++ b/src/osx/cocoa/dataview.mm @@ -1915,6 +1915,14 @@ outlineView:(NSOutlineView*)outlineView } } +-(BOOL) becomeFirstResponder +{ + BOOL r = [super becomeFirstResponder]; + if ( r ) + implementation->DoNotifyFocusSet(); + return r; +} + @end // ============================================================================ @@ -1942,15 +1950,16 @@ wxCocoaDataViewControl::wxCocoaDataViewControl(wxWindow* peer, [scrollview setAutohidesScrollers:YES]; [scrollview setDocumentView:m_OutlineView]; - // we cannot call InstallHandler(m_OutlineView) here, because we are handling - // our action:s ourselves, only associate the view with this impl - Associate(m_OutlineView,this); // initialize the native control itself too InitOutlineView(style); } void wxCocoaDataViewControl::InitOutlineView(long style) { + // we cannot call InstallHandler(m_OutlineView) here, because we are handling + // our action:s ourselves, only associate the view with this impl + Associate(m_OutlineView,this); + [m_OutlineView setImplementation:this]; [m_OutlineView setFocusRingType:NSFocusRingTypeNone]; [m_OutlineView setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle]; diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index cdd9c59961..062bdedffb 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -2328,12 +2328,18 @@ bool wxWidgetCocoaImpl::GetNeedsDisplay() const bool wxWidgetCocoaImpl::CanFocus() const { - return [m_osxView canBecomeKeyView] == YES; + NSView* targetView = m_osxView; + if ( [m_osxView isKindOfClass:[NSScrollView class] ] ) + targetView = [(NSScrollView*) m_osxView documentView]; + return [targetView canBecomeKeyView] == YES; } bool wxWidgetCocoaImpl::HasFocus() const { - return ( FindFocus() == m_osxView ); + NSView* targetView = m_osxView; + if ( [m_osxView isKindOfClass:[NSScrollView class] ] ) + targetView = [(NSScrollView*) m_osxView documentView]; + return ( FindFocus() == targetView ); } bool wxWidgetCocoaImpl::SetFocus() @@ -2341,6 +2347,10 @@ bool wxWidgetCocoaImpl::SetFocus() if ( !CanFocus() ) return false; + NSView* targetView = m_osxView; + if ( [m_osxView isKindOfClass:[NSScrollView class] ] ) + targetView = [(NSScrollView*) m_osxView documentView]; + // TODO remove if no issues arise: should not raise the window, only assign focus //[[m_osxView window] makeKeyAndOrderFront:nil] ; [[m_osxView window] makeFirstResponder: m_osxView] ;