Reuse wxMenu::SendEvent() from wxFrameBase::ProcessCommand().
Not only this avoids code duplication but it also fixes wrong code in wxFrameBase version as it set the event object incorrectly to the frame itself instead of setting it to the menu. Added event object check to the menu events unit test. Closes #1595. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71114 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -251,8 +251,7 @@ bool wxFrameBase::ProcessCommand(int id)
|
|||||||
|
|
||||||
bool wxFrameBase::ProcessCommand(wxMenuItem *item)
|
bool wxFrameBase::ProcessCommand(wxMenuItem *item)
|
||||||
{
|
{
|
||||||
wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
|
wxCHECK_MSG( item, false, wxS("Menu item can't be NULL") );
|
||||||
commandEvent.SetEventObject(this);
|
|
||||||
|
|
||||||
if (!item->IsEnabled())
|
if (!item->IsEnabled())
|
||||||
return true;
|
return true;
|
||||||
@@ -260,19 +259,23 @@ bool wxFrameBase::ProcessCommand(wxMenuItem *item)
|
|||||||
if ((item->GetKind() == wxITEM_RADIO) && item->IsChecked() )
|
if ((item->GetKind() == wxITEM_RADIO) && item->IsChecked() )
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
int checked;
|
||||||
if (item->IsCheckable())
|
if (item->IsCheckable())
|
||||||
{
|
{
|
||||||
item->Toggle();
|
item->Toggle();
|
||||||
|
|
||||||
// use the new value
|
// use the new value
|
||||||
commandEvent.SetInt(item->IsChecked());
|
checked = item->IsChecked();
|
||||||
}
|
}
|
||||||
else // Uncheckable item.
|
else // Uncheckable item.
|
||||||
{
|
{
|
||||||
commandEvent.SetInt(-1);
|
checked = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return HandleWindowEvent(commandEvent);
|
wxMenu* const menu = item->GetMenu();
|
||||||
|
wxCHECK_MSG( menu, false, wxS("Menu item should be attached to a menu") );
|
||||||
|
|
||||||
|
return menu->SendEvent(item->GetId(), checked);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // wxUSE_MENUS
|
#endif // wxUSE_MENUS
|
||||||
|
@@ -116,6 +116,9 @@ private:
|
|||||||
|
|
||||||
wxArrayString m_menuLabels;
|
wxArrayString m_menuLabels;
|
||||||
|
|
||||||
|
// The menu containing the item with MenuTestCase_Bar id.
|
||||||
|
wxMenu* m_menuWithBar;
|
||||||
|
|
||||||
DECLARE_NO_COPY_CLASS(MenuTestCase)
|
DECLARE_NO_COPY_CLASS(MenuTestCase)
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -155,6 +158,7 @@ void MenuTestCase::CreateFrame()
|
|||||||
|
|
||||||
PopulateMenu(helpMenu, "Helpmenu item ", itemcount);
|
PopulateMenu(helpMenu, "Helpmenu item ", itemcount);
|
||||||
helpMenu->Append(MenuTestCase_Bar, "Bar\tF1");
|
helpMenu->Append(MenuTestCase_Bar, "Bar\tF1");
|
||||||
|
m_menuWithBar = helpMenu;
|
||||||
helpMenu->AppendSubMenu(subMenu, "Sub&menu", "Test a submenu");
|
helpMenu->AppendSubMenu(subMenu, "Sub&menu", "Test a submenu");
|
||||||
|
|
||||||
// +2 for "Foo" and "Bar", +2 for the 2 submenus
|
// +2 for "Foo" and "Bar", +2 for the 2 submenus
|
||||||
@@ -464,5 +468,13 @@ void MenuTestCase::Events()
|
|||||||
|
|
||||||
const wxCommandEvent& ev = handler.GetEvent();
|
const wxCommandEvent& ev = handler.GetEvent();
|
||||||
CPPUNIT_ASSERT_EQUAL( static_cast<int>(MenuTestCase_Bar), ev.GetId() );
|
CPPUNIT_ASSERT_EQUAL( static_cast<int>(MenuTestCase_Bar), ev.GetId() );
|
||||||
|
|
||||||
|
wxObject* const src = ev.GetEventObject();
|
||||||
|
CPPUNIT_ASSERT( src );
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT_EQUAL( "wxMenu",
|
||||||
|
wxString(src->GetClassInfo()->GetClassName()) );
|
||||||
|
CPPUNIT_ASSERT_EQUAL( static_cast<wxObject*>(m_menuWithBar),
|
||||||
|
src );
|
||||||
#endif // wxUSE_UIACTIONSIMULATOR
|
#endif // wxUSE_UIACTIONSIMULATOR
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user