X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f91bf72fb0eb3baa200bb153e57ea8d53b5f5aac..19e30148e18cc99296b26503c155e5cef59045f4:/samples/menu/menu.cpp diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index 63a7454627..bc87eebf62 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -31,6 +31,7 @@ #include "wx/msgdlg.h" #include "wx/log.h" #include "wx/textctrl.h" + #include "wx/textdlg.h" #endif #if !wxUSE_MENUS @@ -87,13 +88,31 @@ protected: void OnGetLabelMenu(wxCommandEvent& event); void OnSetLabelMenu(wxCommandEvent& event); - void OnRightUp(wxMouseEvent& event); + void OnTestNormal(wxCommandEvent& event); + void OnTestCheck(wxCommandEvent& event); + void OnTestRadio(wxCommandEvent& event); + +#ifdef __WXMSW__ + void OnContextMenu(wxContextMenuEvent& event) + { ShowContextMenu(ScreenToClient(event.GetPosition())); } +#else + void OnRightUp(wxMouseEvent& event) + { ShowContextMenu(event.GetPosition()); } +#endif + + void OnMenuOpen(wxMenuEvent& event) + { LogMenuOpenOrClose(event, _T("opened")); } + void OnMenuClose(wxMenuEvent& event) + { LogMenuOpenOrClose(event, _T("closed")); } void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event); void OnSize(wxSizeEvent& event); private: + void LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what); + void ShowContextMenu(const wxPoint& pos); + wxMenu *CreateDummyMenu(wxString *title); wxMenuItem *GetLastMenuItem() const; @@ -159,7 +178,13 @@ enum Menu_Menu_SetLabel, Menu_Menu_GetInfo, - Menu_Dummy_First = 400, + Menu_Test_Normal = 400, + Menu_Test_Check, + Menu_Test_Radio1, + Menu_Test_Radio2, + Menu_Test_Radio3, + + Menu_Dummy_First = 500, Menu_Dummy_Second, Menu_Dummy_Third, Menu_Dummy_Fourth, @@ -203,11 +228,24 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem) EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo) + EVT_MENU(Menu_Test_Normal, MyFrame::OnTestNormal) + EVT_MENU(Menu_Test_Check, MyFrame::OnTestCheck) + EVT_MENU(Menu_Test_Radio1, MyFrame::OnTestRadio) + EVT_MENU(Menu_Test_Radio2, MyFrame::OnTestRadio) + EVT_MENU(Menu_Test_Radio3, MyFrame::OnTestRadio) + EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy) EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI) +#ifdef __WXMSW__ + EVT_CONTEXT_MENU(MyFrame::OnContextMenu) +#else EVT_RIGHT_UP(MyFrame::OnRightUp) +#endif + + EVT_MENU_OPEN(MyFrame::OnMenuOpen) + EVT_MENU_CLOSE(MyFrame::OnMenuClose) EVT_SIZE(MyFrame::OnSize) END_EVENT_TABLE() @@ -251,7 +289,7 @@ bool MyApp::OnInit() // Define my frame constructor MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, -1, "wxWindows menu sample", - wxDefaultPosition, wxSize(300, 200)) + wxDefaultPosition, wxSize(400, 250)) { m_textctrl = NULL; m_menu = NULL; @@ -313,6 +351,15 @@ MyFrame::MyFrame() menuMenu->Append(Menu_Menu_GetInfo, "Get menu item in&fo\tAlt-F", "Show the state of the last menu item"); + wxMenu *testMenu = new wxMenu; + testMenu->Append(Menu_Test_Normal, "&Normal item"); + testMenu->AppendSeparator(); + testMenu->AppendCheckItem(Menu_Test_Check, "&Check item"); + testMenu->AppendSeparator(); + testMenu->AppendRadioItem(Menu_Test_Radio1, "Radio item &1"); + testMenu->AppendRadioItem(Menu_Test_Radio2, "Radio item &2"); + testMenu->AppendRadioItem(Menu_Test_Radio3, "Radio item &3"); + wxMenu *helpMenu = new wxMenu; helpMenu->Append(Menu_Help_About, "&About\tF1", "About menu sample"); @@ -321,6 +368,7 @@ MyFrame::MyFrame() menuBar->Append(fileMenu, "&File"); menuBar->Append(menubarMenu, "Menu&bar"); menuBar->Append(menuMenu, "&Menu"); + menuBar->Append(testMenu, "&Test"); menuBar->Append(helpMenu, "&Help"); // these items should be initially checked @@ -340,12 +388,14 @@ MyFrame::MyFrame() wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE); m_textctrl->SetEditable(FALSE); + + wxLog::SetTimestamp(NULL); m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl)); - wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu\n") + wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ") _T("append/insert/delete items to/from the last menu.\n") - _T("The commands from \"Menubar\" menu work with the\n") - _T("menubar itself.\n") + _T("The commands from \"Menubar\" menu work with the ") + _T("menubar itself.\n\n") _T("Right click the band below to test popup menus.\n")); } @@ -489,6 +539,8 @@ void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event)) wxMenuBar *mbar = GetMenuBar(); size_t count = mbar->GetMenuCount(); + wxCHECK_RET( count, _T("no last menu?") ); + wxLogMessage(wxT("The label of the last menu item is '%s'"), mbar->GetLabelTop(count - 1).c_str()); } @@ -498,7 +550,20 @@ void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event)) wxMenuBar *mbar = GetMenuBar(); size_t count = mbar->GetMenuCount(); - mbar->SetLabelTop(count - 1, "Dummy label &0"); + wxCHECK_RET( count, _T("no last menu?") ); + + wxString label = wxGetTextFromUser + ( + _T("Enter new label: "), + _T("Change last menu text"), + mbar->GetLabelTop(count - 1), + this + ); + + if ( !label.empty() ) + { + mbar->SetLabelTop(count - 1, label); + } } void MyFrame::OnDummy(wxCommandEvent& event) @@ -595,7 +660,18 @@ void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) if ( item ) { - item->SetText("Dummy menu item text"); + wxString label = wxGetTextFromUser + ( + _T("Enter new label: "), + _T("Change last menu item text"), + item->GetLabel(), + this + ); + + if ( !label.empty() ) + { + item->SetText(label); + } } } @@ -674,7 +750,7 @@ void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event)) } } -void MyFrame::OnRightUp(wxMouseEvent &event) +void MyFrame::ShowContextMenu(const wxPoint& pos) { wxMenu menu("Test popup"); @@ -690,7 +766,7 @@ void MyFrame::OnRightUp(wxMouseEvent &event) menu.Check(Menu_Popup_ToBeChecked, TRUE); menu.Enable(Menu_Popup_ToBeGreyed, FALSE); - PopupMenu(&menu, event.GetX(), event.GetY()); + PopupMenu(&menu, pos.x, pos.y); // test for destroying items in popup menus #if 0 // doesn't work in wxGTK! @@ -700,6 +776,29 @@ void MyFrame::OnRightUp(wxMouseEvent &event) #endif // 0 } +void MyFrame::OnTestNormal(wxCommandEvent& event) +{ + wxLogMessage(_T("Normal item selected")); +} + +void MyFrame::OnTestCheck(wxCommandEvent& event) +{ + wxLogMessage(_T("Check item %schecked"), + event.IsChecked() ? _T("") : _T("un")); +} + +void MyFrame::OnTestRadio(wxCommandEvent& event) +{ + wxLogMessage(_T("Radio item %d selected"), + event.GetId() - Menu_Test_Radio1 + 1); +} + +void MyFrame::LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what) +{ + wxLogStatus(this, _T("A %smenu has been %s."), + event.IsPopup() ? _T("popup ") : _T(""), what); +} + void MyFrame::OnSize(wxSizeEvent& event) { if ( !m_textctrl )