X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/717a57c2fabb054c8f00dc5dae70df1d03cfe532..270a909e20a2c652fd816ad14407113ad0319c9d:/samples/menu/menu.cpp diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index 2e44aa4747..b4fa149155 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -62,6 +62,7 @@ public: void OnEnableMenuItem(wxCommandEvent& event); void OnGetLabelMenuItem(wxCommandEvent& event); void OnSetLabelMenuItem(wxCommandEvent& event); + void OnGetMenuItemInfo(wxCommandEvent& event); void OnAppendMenu(wxCommandEvent& event); void OnDeleteMenu(wxCommandEvent& event); @@ -107,6 +108,7 @@ enum Menu_Menu_Check, Menu_Menu_GetLabel, Menu_Menu_SetLabel, + Menu_Menu_GetInfo, Menu_Dummy_First = 400, Menu_Dummy_Second, @@ -145,9 +147,10 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Menu_Menu_Insert, MyFrame::OnInsertMenuItem) EVT_MENU(Menu_Menu_Delete, MyFrame::OnDeleteMenuItem) EVT_MENU(Menu_Menu_Enable, MyFrame::OnEnableMenuItem) - EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem) + EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem) EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem) EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem) + EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo) EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy) @@ -234,6 +237,9 @@ MyFrame::MyFrame() "Get the label of the last menu item"); menuMenu->Append(Menu_Menu_SetLabel, "&Set menu item label\tAlt-S", "Change the label of the last menu item"); + menuMenu->AppendSeparator(); + menuMenu->Append(Menu_Menu_GetInfo, "Get menu item in&fo\tAlt-F", + "Show the state of the last menu item"); wxMenu *helpMenu = new wxMenu; helpMenu->Append(Menu_Help_About, "&About\tF1", "About menu sample"); @@ -313,7 +319,7 @@ void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event)) { - static s_count = 0; + static int s_count = 0; wxString title; title.Printf("Dummy menu &%d", ++s_count); @@ -354,7 +360,7 @@ void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event)) size_t count = mbar->GetMenuCount(); wxLogMessage("The label of the last menu item is '%s'", - mbar->GetLabelTop(count - 1)); + mbar->GetLabelTop(count - 1).c_str()); } void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event)) @@ -385,9 +391,10 @@ void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event)) { wxMenuBar *menubar = GetMenuBar(); + wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); - - menu->Append(Menu_Dummy_Last, "Dummy sub menu\tCtrl-F12", + + menu->Append(Menu_Dummy_Last, "Dummy sub menu\tCtrl-F2", CreateDummyMenu()); } @@ -448,7 +455,7 @@ void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) if ( item ) { wxLogMessage("The label of the last menu item is '%s'", - item->GetLabel()); + item->GetLabel().c_str()); } } @@ -462,6 +469,81 @@ void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) } } +void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event)) +{ + wxMenuItem *item = GetLastMenuItem(); + + if ( item ) + { + wxString msg; + msg << "The item is " << (item->IsEnabled() ? "enabled" + : "disabled") + << '\n'; + + if ( item->IsCheckable() ) + { + msg << "It is checkable and " << (item->IsChecked() ? "" : "un") + << "checked\n"; + } + +#if wxUSE_ACCEL + wxAcceleratorEntry *accel = item->GetAccel(); + if ( accel ) + { + msg << "Its accelerator is "; + + int flags = accel->GetFlags(); + if ( flags & wxACCEL_ALT ) + msg << wxT("Alt-"); + if ( flags & wxACCEL_CTRL ) + msg << wxT("Ctrl-"); + if ( flags & wxACCEL_SHIFT ) + msg << wxT("Shift-"); + + int code = accel->GetKeyCode(); + switch ( code ) + { + case WXK_F1: + case WXK_F2: + case WXK_F3: + case WXK_F4: + case WXK_F5: + case WXK_F6: + case WXK_F7: + case WXK_F8: + case WXK_F9: + case WXK_F10: + case WXK_F11: + case WXK_F12: + msg << wxT('F') << code - WXK_F1 + 1; + break; + + // if there are any other keys wxGetAccelFromString() may return, + // we should process them here + + default: + if ( wxIsalnum(code) ) + { + msg << (wxChar)code; + + break; + } + + wxFAIL_MSG( wxT("unknown keyboard accel") ); + } + + delete accel; + } + else + { + msg << "It doesn't have an accelerator"; + } +#endif // wxUSE_ACCEL + + wxLogMessage(msg); + } +} + void MyFrame::OnRightDown(wxMouseEvent &event ) { wxMenu menu("Test popup"); @@ -474,7 +556,7 @@ void MyFrame::OnRightDown(wxMouseEvent &event ) menu.AppendSeparator(); menu.Append(Menu_File_Quit, "E&xit"); - menu.Delete(Menu_Popup_ToBeDeleted); + //menu.Delete(Menu_Popup_ToBeDeleted); menu.Check(Menu_Popup_ToBeChecked, TRUE); menu.Enable(Menu_Popup_ToBeGreyed, FALSE);