Fix handling events from their items in submenu itself

This previously worked in wxGTK, but not in wxMSW and even under wxGTK
it could be surprising that the submenu got the event, but its parent
menu did not.

Make things consistent between the platforms and send the event to the
menu directly containing it first, but then also to its parent menu(s).

Document the new behaviour and verify that it works as intended with a
new unit test.

Closes #18202.
This commit is contained in:
Vadim Zeitlin
2018-08-24 19:03:15 +02:00
parent 5b2c905fb9
commit de5ba70203
5 changed files with 47 additions and 29 deletions

View File

@@ -445,11 +445,14 @@ wxMenu* CreateTestMenu(wxFrame* frame)
// reliable than using wxUIActionSimulator and currently works in all ports as
// they all call wxMenuBase::SendEvent() from their respective menu event
// handlers.
#define ASSERT_MENU_EVENT_RESULT(menu, result) \
g_str.clear(); \
menu->SendEvent(wxID_APPLY); \
#define ASSERT_MENU_EVENT_RESULT_FOR(cmd, menu, result) \
g_str.clear(); \
menu->SendEvent(cmd); \
CHECK( g_str == result )
#define ASSERT_MENU_EVENT_RESULT(menu, result) \
ASSERT_MENU_EVENT_RESULT_FOR(wxID_APPLY, menu, result)
void EventPropagationTestCase::MenuEvent()
{
wxFrame* const frame = static_cast<wxFrame*>(wxTheApp->GetTopWindow());
@@ -472,6 +475,17 @@ void EventPropagationTestCase::MenuEvent()
ASSERT_MENU_EVENT_RESULT( menu, "aomA" );
// Check that a handler can also be attached to a submenu.
wxMenu* const submenu = new wxMenu;
submenu->Append(wxID_ABOUT);
menu->Append(wxID_ANY, "Submenu", submenu);
TestMenuEvtHandler hs('s'); // 's' for "submenu"
submenu->SetNextHandler(&hs);
wxON_BLOCK_EXIT_OBJ1( *submenu,
wxEvtHandler::SetNextHandler, (wxEvtHandler*)NULL );
ASSERT_MENU_EVENT_RESULT_FOR( wxID_ABOUT, submenu, "aosomA" );
// Test that the event handler associated with the menu bar gets the event.
TestMenuEvtHandler hb('b'); // 'b' for "menu Bar"
mb->PushEventHandler(&hb);