From 1fb42c5e433a36f79cefa453959f6dcfbf348a8f Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Mon, 5 Jul 2021 19:47:03 -0400 Subject: [PATCH] Try and cleanup the input focus/key event a little --- common/tool/tool_dispatcher.cpp | 12 ++--------- common/widgets/ui_common.cpp | 37 ++++++++++++++++++++++++++------- include/widgets/ui_common.h | 13 +++++++++++- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/common/tool/tool_dispatcher.cpp b/common/tool/tool_dispatcher.cpp index fa68174bf8..2382e81ffd 100644 --- a/common/tool/tool_dispatcher.cpp +++ b/common/tool/tool_dispatcher.cpp @@ -520,17 +520,9 @@ void TOOL_DISPATCHER::DispatchWxEvent( wxEvent& aEvent ) keyIsEscape = ( ke->GetKeyCode() == WXK_ESCAPE ); - wxTextEntry* textEntry = dynamic_cast( focus ); - wxStyledTextCtrl* styledText = dynamic_cast( focus ); - - if( KIUI::IsInputControlFocused() ) + if( KIUI::IsInputControlFocused( focus ) ) { - bool enabled = true; - - if( textEntry ) - enabled = textEntry->IsEditable(); - else if( styledText ) - enabled = styledText->IsEditable(); + bool enabled = KIUI::IsInputControlEditable( focus ); // Never process key events for tools when a text entry has focus if( enabled ) diff --git a/common/widgets/ui_common.cpp b/common/widgets/ui_common.cpp index 6918bda9f1..ca4f38c693 100644 --- a/common/widgets/ui_common.cpp +++ b/common/widgets/ui_common.cpp @@ -168,26 +168,29 @@ void KIUI::SelectReferenceNumber( wxTextEntry* aTextEntry ) } -bool KIUI::IsInputControlFocused() +bool KIUI::IsInputControlFocused( wxWindow* aFocus ) { - wxWindow* focus = wxWindow::FindFocus(); + if( aFocus == nullptr ) + { + aFocus = wxWindow::FindFocus(); + } - if( !focus ) + if( !aFocus ) { return false; } - wxTextEntry* textEntry = dynamic_cast( focus ); - wxStyledTextCtrl* styledText = dynamic_cast( focus ); - wxListBox* listBox = dynamic_cast( focus ); - wxSearchCtrl* searchCtrl = dynamic_cast( focus ); + wxTextEntry* textEntry = dynamic_cast( aFocus ); + wxStyledTextCtrl* styledText = dynamic_cast( aFocus ); + wxListBox* listBox = dynamic_cast( aFocus ); + wxSearchCtrl* searchCtrl = dynamic_cast( aFocus ); // Data view control is annoying, the focus is on a "wxDataViewCtrlMainWindow" // class that is not formerly exported via the header. // However, we can test the parent is wxDataViewCtrl instead wxDataViewCtrl* dataViewCtrl = nullptr; - wxWindow* parent = focus->GetParent(); + wxWindow* parent = aFocus->GetParent(); if( parent ) { @@ -198,6 +201,24 @@ bool KIUI::IsInputControlFocused() } +bool KIUI::IsInputControlEditable( wxWindow* aFocus ) +{ + wxTextEntry* textEntry = dynamic_cast( aFocus ); + wxStyledTextCtrl* styledText = dynamic_cast( aFocus ); + wxSearchCtrl* searchCtrl = dynamic_cast( aFocus ); + wxListBox* listBox = dynamic_cast( aFocus ); + + if( textEntry ) + return textEntry->IsEditable(); + else if( styledText ) + return styledText->IsEditable(); + else if( searchCtrl ) + return searchCtrl->IsEditable(); + + return true; // Must return true if we can't determine the state, intentionally true for non inputs as well +} + + bool KIUI::IsModalDialogFocused() { return Pgm().m_ModalDialogCount > 0; diff --git a/include/widgets/ui_common.h b/include/widgets/ui_common.h index 1d7cac362a..7a163cc875 100644 --- a/include/widgets/ui_common.h +++ b/include/widgets/ui_common.h @@ -76,8 +76,19 @@ void SelectReferenceNumber( wxTextEntry* aTextEntry ); /** * Checks if a input control has focus + * + * @param aFocus Control that has focus, if null, wxWidgets will be queried */ -bool IsInputControlFocused(); +bool IsInputControlFocused( wxWindow* aFocus = nullptr ); + +/** + * Checks if a input control has focus + * + * @param aFocus Control that test if editable + * + * @return True if control is input and editable OR control is not a input. False if control is input and not editable. + */ +bool IsInputControlEditable( wxWindow* aControl ); bool IsModalDialogFocused();