From 0aaa05ae3adcf50f27049190423827ff1103aaaf Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 29 Sep 2019 23:45:55 +0200 Subject: [PATCH] Override Ctrl-A accelerator when wxTextCtrl has focus in wxMSW This key combination is used for selecting all text, while it's also relatively common to use it as an accelerator for some menu item. Resolve the conflict in favour of wxTextCtrl, i.e. let it have this key when it has focus, while still allowing to use it as an accelerator otherwise. --- src/msw/textctrl.cpp | 1 + tests/menu/menu.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index 9118db73bd..2196918b93 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -2048,6 +2048,7 @@ bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* msg) { switch ( vkey ) { + case 'A': case 'C': case 'V': case 'X': diff --git a/tests/menu/menu.cpp b/tests/menu/menu.cpp index ce33963e3a..99531c5622 100644 --- a/tests/menu/menu.cpp +++ b/tests/menu/menu.cpp @@ -36,6 +36,7 @@ namespace enum { MenuTestCase_Foo = 10000, + MenuTestCase_SelectAll, MenuTestCase_Bar, MenuTestCase_First }; @@ -168,6 +169,9 @@ void MenuTestCase::CreateFrame() fileMenu->Append(MenuTestCase_Foo, "&Foo\tCtrl-F", "Test item to be found"); m_itemCount++; + fileMenu->Append(MenuTestCase_SelectAll, "Select &all\tCtrl-A", + "Accelerator conflicting with wxTextCtrl"); + m_itemCount++; PopulateMenu(helpMenu, "Helpmenu item ", m_itemCount); @@ -548,6 +552,11 @@ public: return *m_event; } + bool GotEvent() const + { + return m_gotEvent; + } + private: void OnMenu(wxCommandEvent& event) { @@ -607,6 +616,24 @@ void MenuTestCase::Events() wxString(src->GetClassInfo()->GetClassName()) ); CPPUNIT_ASSERT_EQUAL( static_cast(m_menuWithBar), src ); + + // Invoke another accelerator, it should also work. + sim.Char('A', wxMOD_CONTROL); + wxYield(); + + const wxCommandEvent& ev2 = handler.GetEvent(); + CHECK( ev2.GetId() == MenuTestCase_SelectAll ); + + // Now create a text control which uses the same accelerator for itself and + // check that when the text control has focus, the accelerator does _not_ + // work. + wxTextCtrl* const text = new wxTextCtrl(m_frame, wxID_ANY, "Testing"); + text->SetFocus(); + + sim.Char('A', wxMOD_CONTROL); + wxYield(); + + CHECK( !handler.GotEvent() ); #endif // wxUSE_UIACTIONSIMULATOR }