Merge branch 'osx-dataview-native-key'

Implement wxEVT_CHAR generation for wxDataViewCtrl under Mac.

Closes https://github.com/wxWidgets/wxWidgets/pull/2324
This commit is contained in:
Vadim Zeitlin
2021-04-24 00:33:31 +02:00
6 changed files with 64 additions and 26 deletions

View File

@@ -573,6 +573,9 @@ public:
virtual void SetFont(const wxFont& font) wxOVERRIDE; virtual void SetFont(const wxFont& font) wxOVERRIDE;
virtual void keyEvent(WX_NSEvent event, WXWidget slf, void* _cmd) wxOVERRIDE;
virtual bool doCommandBySelector(void* sel, WXWidget slf, void* _cmd) wxOVERRIDE;
private: private:
void InitOutlineView(long style); void InitOutlineView(long style);
int GetDefaultRowHeight() const; int GetDefaultRowHeight() const;

View File

@@ -199,7 +199,8 @@ public :
virtual void cursorUpdate(WX_NSEvent event, WXWidget slf, void* _cmd); virtual void cursorUpdate(WX_NSEvent event, WXWidget slf, void* _cmd);
virtual void keyEvent(WX_NSEvent event, WXWidget slf, void* _cmd); virtual void keyEvent(WX_NSEvent event, WXWidget slf, void* _cmd);
virtual void insertText(NSString* text, WXWidget slf, void* _cmd); virtual void insertText(NSString* text, WXWidget slf, void* _cmd);
virtual void doCommandBySelector(void* sel, WXWidget slf, void* _cmd); // Returns true if the event was processed by a user-defined event handler.
virtual bool doCommandBySelector(void* sel, WXWidget slf, void* _cmd);
virtual bool acceptsFirstResponder(WXWidget slf, void* _cmd); virtual bool acceptsFirstResponder(WXWidget slf, void* _cmd);
virtual bool becomeFirstResponder(WXWidget slf, void* _cmd); virtual bool becomeFirstResponder(WXWidget slf, void* _cmd);
virtual bool resignFirstResponder(WXWidget slf, void* _cmd); virtual bool resignFirstResponder(WXWidget slf, void* _cmd);

View File

@@ -284,7 +284,6 @@ protected:
// event handling // event handling
void OnSize(wxSizeEvent &event); void OnSize(wxSizeEvent &event);
void OnMouse(wxMouseEvent &event);
private: private:
// initializing of local variables: // initializing of local variables:

View File

@@ -1632,6 +1632,16 @@ outlineView:(NSOutlineView*)outlineView
// ============================================================================ // ============================================================================
@implementation wxCocoaOutlineView @implementation wxCocoaOutlineView
+ (void)initialize
{
static BOOL initialized = NO;
if (!initialized)
{
initialized = YES;
wxOSXCocoaClassAddWXMethods( self );
}
}
// //
// initializers / destructor // initializers / destructor
// //
@@ -1723,20 +1733,15 @@ outlineView:(NSOutlineView*)outlineView
// Default enter key behaviour is to begin cell editing. Subclass keyDown to // Default enter key behaviour is to begin cell editing. Subclass keyDown to
// provide a keyboard wxEVT_DATAVIEW_ITEM_ACTIVATED event and allow the NSEvent // provide a keyboard wxEVT_DATAVIEW_ITEM_ACTIVATED event and allow the NSEvent
// to pass if the wxEvent is not processed. // to pass if the wxEvent is not processed.
- (void)keyDown:(NSEvent *)event
// catch events routed here and feed them back before things get routed up the responder chain
// otherwise we lose functionality like arrow keys etc.
- (void)doCommandBySelector:(SEL)aSelector
{ {
if( [[event charactersIgnoringModifiers] wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
characterAtIndex: 0] == NSCarriageReturnCharacter ) if (impl)
{ {
wxDataViewCtrl* const dvc = implementation->GetDataViewCtrl(); impl->doCommandBySelector(aSelector, self, _cmd);
const wxDataViewItem item = wxDataViewItem( [[self itemAtRow:[self selectedRow]] pointer]);
wxDataViewEvent eventDV(wxEVT_DATAVIEW_ITEM_ACTIVATED, dvc, item);
if ( !dvc->GetEventHandler()->ProcessEvent(eventDV) )
[super keyDown:event];
}
else
{
[super keyDown:event]; // all other keys
} }
} }
@@ -2082,7 +2087,8 @@ wxCocoaDataViewControl::wxCocoaDataViewControl(wxWindow* peer,
: wxWidgetCocoaImpl : wxWidgetCocoaImpl
( (
peer, peer,
[[NSScrollView alloc] initWithFrame:wxOSXGetFrameForControl(peer,pos,size)] [[NSScrollView alloc] initWithFrame:wxOSXGetFrameForControl(peer,pos,size)],
wxWidgetImpl::Widget_UserKeyEvents
), ),
m_DataSource(NULL), m_DataSource(NULL),
m_OutlineView([[wxCocoaOutlineView alloc] init]), m_OutlineView([[wxCocoaOutlineView alloc] init]),
@@ -2130,6 +2136,37 @@ wxCocoaDataViewControl::~wxCocoaDataViewControl()
[m_OutlineView release]; [m_OutlineView release];
} }
void wxCocoaDataViewControl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
{
if( [event type] == NSKeyDown && [[event charactersIgnoringModifiers]
characterAtIndex: 0] == NSCarriageReturnCharacter )
{
wxDataViewCtrl* const dvc = GetDataViewCtrl();
const wxDataViewItem item = wxDataViewItem( [[m_OutlineView itemAtRow:[m_OutlineView selectedRow]] pointer]);
wxDataViewEvent eventDV(wxEVT_DATAVIEW_ITEM_ACTIVATED, dvc, item);
if ( !dvc->GetEventHandler()->ProcessEvent(eventDV) )
wxWidgetCocoaImpl::keyEvent(event, slf, _cmd);
}
else
{
wxWidgetCocoaImpl::keyEvent(event, slf, _cmd); // all other keys
}
}
bool wxCocoaDataViewControl::doCommandBySelector(void* sel, WXWidget slf, void* _cmd)
{
bool handled = wxWidgetCocoaImpl::doCommandBySelector(sel, slf, _cmd);
// if this special key has not been handled
if ( !handled && IsInNativeKeyDown() )
{
// send the original key event back to the native implementation to get proper default handling like eg for arrow keys
wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:@selector(keyDown:)];
superimpl(slf, @selector(keyDown:), GetLastNativeKeyDownEvent());
}
return handled;
}
// //
// column related methods (inherited from wxDataViewWidgetImpl) // column related methods (inherited from wxDataViewWidgetImpl)
// //

View File

@@ -2145,7 +2145,7 @@ void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
} }
} }
void wxWidgetCocoaImpl::doCommandBySelector(void* sel, WXWidget slf, void* WXUNUSED(_cmd)) bool wxWidgetCocoaImpl::doCommandBySelector(void* sel, WXWidget slf, void* WXUNUSED(_cmd))
{ {
wxLogTrace(TRACE_KEYS, "Selector %s for %s", wxLogTrace(TRACE_KEYS, "Selector %s for %s",
wxDumpSelector((SEL)sel), wxDumpNSView(slf)); wxDumpSelector((SEL)sel), wxDumpNSView(slf));
@@ -2155,23 +2155,25 @@ void wxWidgetCocoaImpl::doCommandBySelector(void* sel, WXWidget slf, void* WXUNU
// it is also possible to map 1 keystroke to multiple commands, eg Ctrl-O on mac is translated to the bash-equivalent of // it is also possible to map 1 keystroke to multiple commands, eg Ctrl-O on mac is translated to the bash-equivalent of
// execute and move back in history, since this results in two commands, Ctrl-O was sent twice as a wx key down event. // execute and move back in history, since this results in two commands, Ctrl-O was sent twice as a wx key down event.
// we now track the sending of the events to avoid duplicates. // we now track the sending of the events to avoid duplicates.
bool handled = false;
if ( IsInNativeKeyDown() && !WasKeyDownSent()) if ( IsInNativeKeyDown() && !WasKeyDownSent())
{ {
// If we have a corresponding key event, send wxEVT_KEY_DOWN now. // If we have a corresponding key event, send wxEVT_KEY_DOWN now.
// (see also: wxWidgetCocoaImpl::DoHandleKeyEvent) // (see also: wxWidgetCocoaImpl::DoHandleKeyEvent)
wxKeyEvent wxevent(wxEVT_KEY_DOWN); wxKeyEvent wxevent(wxEVT_KEY_DOWN);
SetupKeyEvent( wxevent, GetLastNativeKeyDownEvent() ); SetupKeyEvent( wxevent, GetLastNativeKeyDownEvent() );
bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent); handled = GetWXPeer()->OSXHandleKeyEvent(wxevent);
if (!result) if (!handled)
{ {
// Generate wxEVT_CHAR if wxEVT_KEY_DOWN is not handled. // Generate wxEVT_CHAR if wxEVT_KEY_DOWN is not handled.
wxKeyEvent wxevent2(wxevent) ; wxKeyEvent wxevent2(wxevent) ;
wxevent2.SetEventType(wxEVT_CHAR); wxevent2.SetEventType(wxEVT_CHAR);
SetupKeyEvent( wxevent2, GetLastNativeKeyDownEvent() ); SetupKeyEvent( wxevent2, GetLastNativeKeyDownEvent() );
GetWXPeer()->OSXHandleKeyEvent(wxevent2); handled = GetWXPeer()->OSXHandleKeyEvent(wxevent2);
} }
SetKeyDownSent(); SetKeyDownSent();
} }
@@ -2179,6 +2181,8 @@ void wxWidgetCocoaImpl::doCommandBySelector(void* sel, WXWidget slf, void* WXUNU
{ {
wxLogTrace(TRACE_KEYS, "Doing nothing in doCommandBySelector:"); wxLogTrace(TRACE_KEYS, "Doing nothing in doCommandBySelector:");
} }
return handled;
} }
bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd) bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)

View File

@@ -765,16 +765,10 @@ wxSize wxDataViewCtrl::DoGetBestSize() const
return best; return best;
} }
void wxDataViewCtrl::OnMouse(wxMouseEvent& event)
{
event.Skip();
}
wxIMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl,wxDataViewCtrlBase); wxIMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl,wxDataViewCtrlBase);
wxBEGIN_EVENT_TABLE(wxDataViewCtrl,wxDataViewCtrlBase) wxBEGIN_EVENT_TABLE(wxDataViewCtrl,wxDataViewCtrlBase)
EVT_SIZE(wxDataViewCtrl::OnSize) EVT_SIZE(wxDataViewCtrl::OnSize)
EVT_MOTION(wxDataViewCtrl::OnMouse)
wxEND_EVENT_TABLE() wxEND_EVENT_TABLE()
#endif // !wxHAS_GENERIC_DATAVIEWCTRL #endif // !wxHAS_GENERIC_DATAVIEWCTRL