From f6d90fb9ce285df92a22124bad344f48f1603c95 Mon Sep 17 00:00:00 2001 From: Gilles Depeyrot Date: Sat, 14 Dec 2002 21:12:47 +0000 Subject: [PATCH] added commands to search for menus and menu items by label git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@18232 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/menu/menu.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index ef1ee21bf5..456932c45f 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -79,6 +79,7 @@ protected: void OnGetLabelMenuItem(wxCommandEvent& event); void OnSetLabelMenuItem(wxCommandEvent& event); void OnGetMenuItemInfo(wxCommandEvent& event); + void OnFindMenuItem(wxCommandEvent& event); void OnAppendMenu(wxCommandEvent& event); void OnInsertMenu(wxCommandEvent& event); @@ -87,6 +88,7 @@ protected: void OnEnableMenu(wxCommandEvent& event); void OnGetLabelMenu(wxCommandEvent& event); void OnSetLabelMenu(wxCommandEvent& event); + void OnFindMenu(wxCommandEvent& event); void OnTestNormal(wxCommandEvent& event); void OnTestCheck(wxCommandEvent& event); @@ -167,6 +169,7 @@ enum Menu_MenuBar_Enable, Menu_MenuBar_GetLabel, Menu_MenuBar_SetLabel, + Menu_MenuBar_FindMenu, Menu_Menu_Append = 300, Menu_Menu_AppendSub, @@ -177,6 +180,7 @@ enum Menu_Menu_GetLabel, Menu_Menu_SetLabel, Menu_Menu_GetInfo, + Menu_Menu_FindItem, Menu_Test_Normal = 400, Menu_Test_Check, @@ -217,6 +221,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu) EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu) EVT_MENU(Menu_MenuBar_SetLabel, MyFrame::OnSetLabelMenu) + EVT_MENU(Menu_MenuBar_FindMenu, MyFrame::OnFindMenu) EVT_MENU(Menu_Menu_Append, MyFrame::OnAppendMenuItem) EVT_MENU(Menu_Menu_AppendSub, MyFrame::OnAppendSubMenu) @@ -227,6 +232,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem) EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem) EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo) + EVT_MENU(Menu_Menu_FindItem, MyFrame::OnFindMenuItem) EVT_MENU(Menu_Test_Normal, MyFrame::OnTestNormal) EVT_MENU(Menu_Test_Check, MyFrame::OnTestCheck) @@ -327,6 +333,9 @@ MyFrame::MyFrame() "Get the label of the last menu"); menubarMenu->Append(Menu_MenuBar_SetLabel, "&Set menu label\tCtrl-S", "Change the label of the last menu"); + menubarMenu->AppendSeparator(); + menubarMenu->Append(Menu_MenuBar_FindMenu, "&Find menu from label\tCtrl-F", + "Find a menu by searching for its label"); wxMenu *menuMenu = new wxMenu; menuMenu->Append(Menu_Menu_Append, "&Append menu item\tAlt-A", @@ -350,6 +359,9 @@ MyFrame::MyFrame() menuMenu->AppendSeparator(); menuMenu->Append(Menu_Menu_GetInfo, "Get menu item in&fo\tAlt-F", "Show the state of the last menu item"); + menuMenu->AppendSeparator(); + menuMenu->Append(Menu_Menu_FindItem, "Find menu item from label", + "Find a menu item by searching for its label"); wxMenu *testMenu = new wxMenu; testMenu->Append(Menu_Test_Normal, "&Normal item"); @@ -566,6 +578,36 @@ void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event)) } } +void MyFrame::OnFindMenu(wxCommandEvent& WXUNUSED(event)) +{ + wxMenuBar *mbar = GetMenuBar(); + size_t count = mbar->GetMenuCount(); + + wxCHECK_RET( count, _T("no last menu?") ); + + wxString label = wxGetTextFromUser + ( + _T("Enter label to search for: "), + _T("Find menu"), + "", + this + ); + + if ( !label.empty() ) + { + int index = mbar->FindMenu(label); + + if (index == wxNOT_FOUND) + { + wxLogWarning(wxT("No menu with label '%s'"), label.c_str()); + } + else + { + wxLogMessage(wxT("Menu %d has label '%s'"), index, label.c_str()); + } + } +} + void MyFrame::OnDummy(wxCommandEvent& event) { wxLogMessage(wxT("Dummy item #%d"), event.GetId() - Menu_Dummy_First + 1); @@ -750,6 +792,42 @@ void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event)) } } +void MyFrame::OnFindMenuItem(wxCommandEvent& WXUNUSED(event)) +{ + wxMenuBar *mbar = GetMenuBar(); + size_t count = mbar->GetMenuCount(); + + wxCHECK_RET( count, _T("no last menu?") ); + + wxString label = wxGetTextFromUser + ( + _T("Enter label to search for: "), + _T("Find menu item"), + "", + this + ); + + if ( !label.empty() ) + { + size_t menuindex = 0; + int index = wxNOT_FOUND; + + for (menuindex = 0; (menuindex < count) && (index == wxNOT_FOUND); ++menuindex) + { + index = mbar->FindMenuItem(mbar->GetMenu(menuindex)->GetTitle(), label); + } + if (index == wxNOT_FOUND) + { + wxLogWarning(wxT("No menu item with label '%s'"), label.c_str()); + } + else + { + wxLogMessage(wxT("Menu item %d in menu %d has label '%s'"), + index, menuindex, label.c_str()); + } + } +} + void MyFrame::ShowContextMenu(const wxPoint& pos) { wxMenu menu("Test popup"); -- 2.45.2