Some more work on getting wxWidgets to stop stealing textEntry keys.

Fixes https://gitlab.com/kicad/code/kicad/issues/1941
This commit is contained in:
Jeff Young 2020-05-30 12:31:19 +01:00
parent 5d52378b80
commit 449c90a0ae
9 changed files with 204 additions and 14 deletions

View File

@ -337,6 +337,7 @@ set( COMMON_SRCS
status_popup.cpp status_popup.cpp
systemdirsappend.cpp systemdirsappend.cpp
template_fieldnames.cpp template_fieldnames.cpp
textentry_tricks.cpp
trace_helpers.cpp trace_helpers.cpp
undo_redo_container.cpp undo_redo_container.cpp
utf8.cpp utf8.cpp

View File

@ -58,14 +58,14 @@ SCINTILLA_TRICKS::SCINTILLA_TRICKS( wxStyledTextCtrl* aScintilla, const wxString
} }
bool IsCtrl( int aChar, const wxKeyEvent& e ) bool SCINTILLA_TRICKS::isCtrl( int aChar, const wxKeyEvent& e )
{ {
return e.GetKeyCode() == aChar && e.ControlDown() && !e.AltDown() && return e.GetKeyCode() == aChar && e.ControlDown() && !e.AltDown() &&
!e.ShiftDown() && !e.MetaDown(); !e.ShiftDown() && !e.MetaDown();
} }
bool IsShiftCtrl( int aChar, const wxKeyEvent& e ) bool SCINTILLA_TRICKS::isShiftCtrl( int aChar, const wxKeyEvent& e )
{ {
return e.GetKeyCode() == aChar && e.ControlDown() && !e.AltDown() && return e.GetKeyCode() == aChar && e.ControlDown() && !e.AltDown() &&
e.ShiftDown() && !e.MetaDown(); e.ShiftDown() && !e.MetaDown();
@ -96,26 +96,35 @@ void SCINTILLA_TRICKS::onCharHook( wxKeyEvent& aEvent )
m_te->Tab(); m_te->Tab();
} }
} }
else if( m_te->IsShown() && IsCtrl( 'Z', aEvent ) ) else if( isCtrl( 'Z', aEvent ) )
{ {
m_te->Undo(); m_te->Undo();
} }
else if( m_te->IsShown() && ( IsShiftCtrl( 'Z', aEvent ) || IsCtrl( 'Y', aEvent ) ) ) else if( isShiftCtrl( 'Z', aEvent ) || isCtrl( 'Y', aEvent ) )
{ {
m_te->Redo(); m_te->Redo();
} }
else if( IsCtrl( 'X', aEvent ) ) else if( isCtrl( 'X', aEvent ) )
{ {
m_te->Cut(); m_te->Cut();
} }
else if( IsCtrl( 'C', aEvent ) ) else if( isCtrl( 'C', aEvent ) )
{ {
m_te->Copy(); m_te->Copy();
} }
else if( IsCtrl( 'V', aEvent ) ) else if( isCtrl( 'V', aEvent ) )
{ {
m_te->Paste(); m_te->Paste();
} }
else if( aEvent.GetKeyCode() == WXK_BACK )
{
m_te->DeleteBack();
}
else if( aEvent.GetKeyCode() == WXK_DELETE )
{
if( m_te->GetSelectionEnd() > m_te->GetSelectionStart() )
m_te->DeleteBack();
}
else else
{ {
aEvent.Skip(); aEvent.Skip();

View File

@ -0,0 +1,90 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2020 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <fctsys.h>
#include <textentry_tricks.h>
#include <dialog_shim.h>
bool TEXTENTRY_TRICKS::isCtrl( int aChar, const wxKeyEvent& e )
{
return e.GetKeyCode() == aChar && e.ControlDown() && !e.AltDown() &&
!e.ShiftDown() && !e.MetaDown();
}
bool TEXTENTRY_TRICKS::isShiftCtrl( int aChar, const wxKeyEvent& e )
{
return e.GetKeyCode() == aChar && e.ControlDown() && !e.AltDown() &&
e.ShiftDown() && !e.MetaDown();
}
void TEXTENTRY_TRICKS::OnCharHook( wxTextEntry* aTextEntry, wxKeyEvent& aEvent )
{
if( isCtrl( 'X', aEvent ) )
{
aTextEntry->Cut();
}
else if( isCtrl( 'C', aEvent ) )
{
aTextEntry->Copy();
}
else if( isCtrl( 'V', aEvent ) )
{
aTextEntry->Paste();
}
else if( aEvent.GetKeyCode() == WXK_BACK )
{
long start, end;
aTextEntry->GetSelection( &start, &end );
if( start > end )
{
aTextEntry->Remove( start, end );
aTextEntry->SetInsertionPoint( start );
}
else if (start == end && start > 0 )
{
aTextEntry->Remove( start-1, start );
aTextEntry->SetInsertionPoint( start-1 );
}
}
else if( aEvent.GetKeyCode() == WXK_DELETE )
{
long start, end;
aTextEntry->GetSelection( &start, &end );
if( start > end )
{
aTextEntry->Remove( start, end );
aTextEntry->SetInsertionPoint( start );
}
}
else
{
aEvent.Skip();
}
}

View File

@ -35,7 +35,8 @@
#include <tool/tool_manager.h> #include <tool/tool_manager.h>
#include <trace_helpers.h> #include <trace_helpers.h>
#include <wx/log.h> #include <wx/log.h>
#include <wx/stc/stc.h>
#include <textentry_tricks.h>
using namespace std::placeholders; using namespace std::placeholders;
@ -357,6 +358,7 @@ void ACTION_MENU::OnMenuEvent( wxMenuEvent& aEvent )
OPT_TOOL_EVENT evt; OPT_TOOL_EVENT evt;
wxString menuText; wxString menuText;
wxEventType type = aEvent.GetEventType(); wxEventType type = aEvent.GetEventType();
wxWindow* focus = wxWindow::FindFocus();
if( type == wxEVT_MENU_OPEN ) if( type == wxEVT_MENU_OPEN )
{ {
@ -380,6 +382,40 @@ void ACTION_MENU::OnMenuEvent( wxMenuEvent& aEvent )
} }
else if( type == wxEVT_COMMAND_MENU_SELECTED ) else if( type == wxEVT_COMMAND_MENU_SELECTED )
{ {
// Despite our attempts to catch the theft of text editor CHAR_HOOK and CHAR events
// in TOOL_DISPATCHER::DispatchWxEvent, wxWidgets sometimes converts those it knows
// about into menu commands without ever generating the appropriate CHAR_HOOK and CHAR
// events first.
if( dynamic_cast<wxStyledTextCtrl*>( focus ) || dynamic_cast<wxTextEntry*>( focus ) )
{
// Original key event has been lost, so we have to re-create it from the menu's
// wxAcceleratorEntry.
wxMenuItem* menuItem = FindItem( aEvent.GetId() );
wxAcceleratorEntry* acceleratorKey = menuItem ? menuItem->GetAccel() : nullptr;
if( acceleratorKey )
{
wxKeyEvent keyEvent( wxEVT_CHAR_HOOK );
keyEvent.m_keyCode = acceleratorKey->GetKeyCode();
keyEvent.m_controlDown = ( acceleratorKey->GetFlags() & wxMOD_CONTROL ) > 0;
keyEvent.m_shiftDown = ( acceleratorKey->GetFlags() & wxMOD_SHIFT ) > 0;
keyEvent.m_altDown = ( acceleratorKey->GetFlags() & wxMOD_ALT ) > 0;
if( dynamic_cast<wxStyledTextCtrl*>( focus ) )
focus->HandleWindowEvent( keyEvent );
else if( dynamic_cast<wxTextEntry*>( focus ) )
TEXTENTRY_TRICKS::OnCharHook( dynamic_cast<wxTextEntry*>( focus ), keyEvent );
if( keyEvent.GetSkipped() )
{
keyEvent.SetEventType( wxEVT_CHAR );
focus->HandleWindowEvent( keyEvent );
}
}
return;
}
// Store the selected position, so it can be checked by the tools // Store the selected position, so it can be checked by the tools
m_selected = aEvent.GetId(); m_selected = aEvent.GetId();

View File

@ -92,7 +92,7 @@ int COMMON_CONTROL::ConfigurePaths( const TOOL_EVENT& aEvent )
// Do nothing here. // Do nothing here.
// A error message is displayed after trying to load _pcbnew.kiface. // A error message is displayed after trying to load _pcbnew.kiface.
} }
} }
else else
{ {
DIALOG_CONFIGURE_PATHS dlg( m_frame, nullptr ); DIALOG_CONFIGURE_PATHS dlg( m_frame, nullptr );

View File

@ -37,7 +37,7 @@
#include <eda_draw_frame.h> #include <eda_draw_frame.h>
#include <core/optional.h> #include <core/optional.h>
#include <wx/stc/stc.h>
///> Stores information about a mouse button state ///> Stores information about a mouse button state
struct TOOL_DISPATCHER::BUTTON_STATE struct TOOL_DISPATCHER::BUTTON_STATE
@ -413,6 +413,7 @@ void TOOL_DISPATCHER::DispatchWxEvent( wxEvent& aEvent )
OPT<TOOL_EVENT> evt; OPT<TOOL_EVENT> evt;
bool keyIsEscape = false; // True if the keypress was the escape key bool keyIsEscape = false; // True if the keypress was the escape key
bool keyIsSpecial = false; // True if the key is a special key code bool keyIsSpecial = false; // True if the key is a special key code
wxWindow* focus = wxWindow::FindFocus();
int type = aEvent.GetEventType(); int type = aEvent.GetEventType();
@ -420,7 +421,7 @@ void TOOL_DISPATCHER::DispatchWxEvent( wxEvent& aEvent )
// is opened and is iconized on Windows). // is opened and is iconized on Windows).
// In this case, give the focus to the parent frame (GAL canvas itself does not accept the // In this case, give the focus to the parent frame (GAL canvas itself does not accept the
// focus when iconized for some obscure reason) // focus when iconized for some obscure reason)
if( wxWindow::FindFocus() == nullptr ) if( focus == nullptr )
{ {
wxWindow* window = dynamic_cast<wxWindow*>( m_toolMgr->GetToolHolder() ); wxWindow* window = dynamic_cast<wxWindow*>( m_toolMgr->GetToolHolder() );
@ -484,9 +485,14 @@ void TOOL_DISPATCHER::DispatchWxEvent( wxEvent& aEvent )
keyIsEscape = ( ke->GetKeyCode() == WXK_ESCAPE ); keyIsEscape = ( ke->GetKeyCode() == WXK_ESCAPE );
// Never process key events for tools when a text entry has focus if( dynamic_cast<wxTextEntry*>( focus ) || dynamic_cast<wxStyledTextCtrl*>( focus ) )
if( !dynamic_cast<wxTextEntry*>( wxWindow::FindFocus() ) ) {
evt = GetToolEvent( ke, &keyIsSpecial ); // Never process key events for tools when a text entry has focus
aEvent.Skip();
return;
}
evt = GetToolEvent( ke, &keyIsSpecial );
} }
else if( type == wxEVT_MENU_OPEN || type == wxEVT_MENU_CLOSE || type == wxEVT_MENU_HIGHLIGHT ) else if( type == wxEVT_MENU_OPEN || type == wxEVT_MENU_CLOSE || type == wxEVT_MENU_HIGHLIGHT )
{ {

View File

@ -41,6 +41,9 @@ public:
void DoAutocomplete( const wxString& aPartial, wxArrayString aTokens ); void DoAutocomplete( const wxString& aPartial, wxArrayString aTokens );
protected: protected:
bool isCtrl( int aChar, const wxKeyEvent& e );
bool isShiftCtrl( int aChar, const wxKeyEvent& e );
void onCharHook( wxKeyEvent& aEvent ); void onCharHook( wxKeyEvent& aEvent );
void onScintillaUpdateUI( wxStyledTextEvent& aEvent ); void onScintillaUpdateUI( wxStyledTextEvent& aEvent );

View File

@ -0,0 +1,44 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2020 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef TEXTENTRY_TRICKS_H
#define TEXTENTRY_TRICKS_H
#include <wx/textentry.h>
/**
* TEXTENTRY_TRICKS
* is used to add cut/copy/paste to a wxTextEntry instance. While these are normally handled
* witout our intervention, this is not always the case.
*/
struct TEXTENTRY_TRICKS
{
static void OnCharHook( wxTextEntry* aTextEntry, wxKeyEvent& aEvent );
protected:
static bool isCtrl( int aChar, const wxKeyEvent& e );
static bool isShiftCtrl( int aChar, const wxKeyEvent& e );
};
#endif // TEXTENTRY_TRICKS_H

View File

@ -31,6 +31,7 @@
#include <functional> #include <functional>
#include <wx/menu.h> #include <wx/menu.h>
#include <wx/textentry.h>
#include <tool/tool_action.h> #include <tool/tool_action.h>
class TOOL_INTERACTIVE; class TOOL_INTERACTIVE;