X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cfbf301d7cd079bf5d25b3c10a8f0980c397724c..b2b0bee6d822b05fea419e8bf24ab4009cb17ac4:/samples/menu/menu.cpp diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index be55ff793c..10e6268e48 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -256,6 +256,8 @@ enum Menu_Popup_ToBeChecked, Menu_Popup_Submenu, + Menu_PopupChoice, + Menu_Max }; @@ -344,6 +346,9 @@ IMPLEMENT_APP(MyApp) // main frame bool MyApp::OnInit() { + if ( !wxApp::OnInit() ) + return false; + // Create the main frame window MyFrame* frame = new MyFrame; @@ -543,7 +548,7 @@ MyFrame::MyFrame() wxTE_MULTILINE); m_textctrl->SetEditable(false); - wxLog::SetTimestamp(NULL); + wxLog::DisableTimestamp(); m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl)); wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ") @@ -716,7 +721,7 @@ void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event)) wxCHECK_RET( count, _T("no last menu?") ); wxLogMessage(_T("The label of the last menu item is '%s'"), - mbar->GetLabelTop(count - 1).c_str()); + mbar->GetMenuLabel(count - 1).c_str()); } #if wxUSE_TEXTDLG @@ -731,13 +736,13 @@ void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event)) ( _T("Enter new label: "), _T("Change last menu text"), - mbar->GetLabelTop(count - 1), + mbar->GetMenuLabel(count - 1), this ); if ( !label.empty() ) { - mbar->SetLabelTop(count - 1, label); + mbar->SetMenuLabel(count - 1, label); } } @@ -855,8 +860,9 @@ void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) if ( item ) { + wxString label = item->GetItemLabel(); wxLogMessage(_T("The label of the last menu item is '%s'"), - item->GetLabel().c_str()); + label.c_str()); } } @@ -871,14 +877,14 @@ void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) ( _T("Enter new label: "), _T("Change last menu item text"), - item->GetLabel(), + item->GetItemLabel(), this ); label.Replace( _T("\\t"), _T("\t") ); if ( !label.empty() ) { - item->SetText(label); + item->SetItemLabel(label); } } } @@ -1001,27 +1007,50 @@ void MyFrame::ShowContextMenu(const wxPoint& pos) { wxMenu menu; - menu.Append(Menu_Help_About, _T("&About")); - menu.Append(Menu_Popup_Submenu, _T("&Submenu"), CreateDummyMenu(NULL)); - menu.Append(Menu_Popup_ToBeDeleted, _T("To be &deleted")); - menu.AppendCheckItem(Menu_Popup_ToBeChecked, _T("To be &checked")); - menu.Append(Menu_Popup_ToBeGreyed, _T("To be &greyed"), - _T("This menu item should be initially greyed out")); - menu.AppendSeparator(); - menu.Append(Menu_File_Quit, _T("E&xit")); - - menu.Delete(Menu_Popup_ToBeDeleted); - menu.Check(Menu_Popup_ToBeChecked, true); - menu.Enable(Menu_Popup_ToBeGreyed, false); - - PopupMenu(&menu, pos.x, pos.y); - - // test for destroying items in popup menus + if ( wxGetKeyState(WXK_SHIFT) ) + { + // when Shift is pressed, demonstrate the use of a simple function + // returning the id of the item selected in the popup menu + menu.SetTitle("Choose one of:"); + static const char *choices[] = { "Apple", "Banana", "Cherry" }; + for ( size_t n = 0; n < WXSIZEOF(choices); n++ ) + menu.Append(Menu_PopupChoice + n, choices[n]); + + const int rc = GetPopupMenuSelectionFromUser(menu, pos); + if ( rc == wxID_NONE ) + { + wxLogMessage("No selection"); + } + else + { + wxLogMessage("You have selected \"%s\"", + choices[rc - Menu_PopupChoice]); + } + } + else // normal case, shift not pressed + { + menu.Append(Menu_Help_About, _T("&About")); + menu.Append(Menu_Popup_Submenu, _T("&Submenu"), CreateDummyMenu(NULL)); + menu.Append(Menu_Popup_ToBeDeleted, _T("To be &deleted")); + menu.AppendCheckItem(Menu_Popup_ToBeChecked, _T("To be &checked")); + menu.Append(Menu_Popup_ToBeGreyed, _T("To be &greyed"), + _T("This menu item should be initially greyed out")); + menu.AppendSeparator(); + menu.Append(Menu_File_Quit, _T("E&xit")); + + menu.Delete(Menu_Popup_ToBeDeleted); + menu.Check(Menu_Popup_ToBeChecked, true); + menu.Enable(Menu_Popup_ToBeGreyed, false); + + PopupMenu(&menu, pos); + + // test for destroying items in popup menus #if 0 // doesn't work in wxGTK! - menu.Destroy(Menu_Popup_Submenu); + menu.Destroy(Menu_Popup_Submenu); - PopupMenu( &menu, event.GetX(), event.GetY() ); + PopupMenu( &menu, event.GetX(), event.GetY() ); #endif // 0 + } } void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event))