X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/717a57c2fabb054c8f00dc5dae70df1d03cfe532..191ebf4d459ca672c8d7678293c0ab3b05ba95d2:/samples/menu/menu.cpp?ds=sidebyside diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index 2e44aa4747..3346cf7424 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -26,10 +26,11 @@ #ifndef WX_PRECOMP #include - #include #endif +#include "copy.xpm" + // ---------------------------------------------------------------------------- // classes // ---------------------------------------------------------------------------- @@ -47,7 +48,9 @@ class MyFrame: public wxFrame public: MyFrame(); - virtual ~MyFrame() { delete m_menu; } + virtual ~MyFrame(); + + void LogMenuEvent(const wxCommandEvent& event); void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); @@ -62,8 +65,10 @@ public: void OnEnableMenuItem(wxCommandEvent& event); void OnGetLabelMenuItem(wxCommandEvent& event); void OnSetLabelMenuItem(wxCommandEvent& event); + void OnGetMenuItemInfo(wxCommandEvent& event); void OnAppendMenu(wxCommandEvent& event); + void OnInsertMenu(wxCommandEvent& event); void OnDeleteMenu(wxCommandEvent& event); void OnToggleMenu(wxCommandEvent& event); void OnEnableMenu(wxCommandEvent& event); @@ -75,12 +80,33 @@ public: void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event); private: - wxMenu *CreateDummyMenu(); + wxMenu *CreateDummyMenu(wxString *title); wxMenuItem *GetLastMenuItem() const; wxMenu *m_menu; + size_t m_countDummy; + + DECLARE_EVENT_TABLE() +}; + +// A small helper class which intercepts all menu events and logs them +class MyEvtHandler : public wxEvtHandler +{ +public: + MyEvtHandler(MyFrame *frame) { m_frame = frame; } + + void OnMenuEvent(wxCommandEvent& event) + { + m_frame->LogMenuEvent(event); + + event.Skip(); + } + +private: + MyFrame *m_frame; + DECLARE_EVENT_TABLE() }; @@ -94,6 +120,7 @@ enum Menu_MenuBar_Toggle = 200, Menu_MenuBar_Append, + Menu_MenuBar_Insert, Menu_MenuBar_Delete, Menu_MenuBar_Enable, Menu_MenuBar_GetLabel, @@ -107,6 +134,7 @@ enum Menu_Menu_Check, Menu_Menu_GetLabel, Menu_Menu_SetLabel, + Menu_Menu_GetInfo, Menu_Dummy_First = 400, Menu_Dummy_Second, @@ -135,6 +163,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Menu_MenuBar_Toggle, MyFrame::OnToggleMenu) EVT_MENU(Menu_MenuBar_Append, MyFrame::OnAppendMenu) + EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu) EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu) EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu) EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu) @@ -145,9 +174,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) @@ -156,6 +186,10 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_RIGHT_DOWN(MyFrame::OnRightDown) END_EVENT_TABLE() +BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler) + EVT_MENU(-1, MyEvtHandler::OnMenuEvent) +END_EVENT_TABLE() + // ============================================================================ // implementation // ============================================================================ @@ -192,16 +226,23 @@ MyFrame::MyFrame() wxDefaultPosition, wxSize(300, 200)) { m_menu = NULL; + m_countDummy = 0; - CreateStatusBar(); + CreateStatusBar(2); // create the menubar wxMenu *fileMenu = new wxMenu; fileMenu->Append(Menu_File_Quit, "E&xit\tAlt-X", "Quit toolbar sample" ); + + wxMenuItem *bitmap_menu_item = new wxMenuItem( fileMenu, Menu_File_Quit, "Quit with &bitmap" ); + bitmap_menu_item->SetBitmap( wxBitmap( copy_xpm ) ); + fileMenu->Append( bitmap_menu_item ); wxMenu *menubarMenu = new wxMenu; menubarMenu->Append(Menu_MenuBar_Append, "&Append menu\tCtrl-A", "Append a menu to the menubar"); + menubarMenu->Append(Menu_MenuBar_Insert, "&Insert menu\tCtrl-I", + "Insert a menu into the menubar"); menubarMenu->Append(Menu_MenuBar_Delete, "&Delete menu\tCtrl-D", "Delete the last menu from the menubar"); menubarMenu->Append(Menu_MenuBar_Toggle, "&Toggle menu\tCtrl-T", @@ -234,6 +275,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"); @@ -253,15 +297,31 @@ MyFrame::MyFrame() // associate the menu bar with the frame SetMenuBar(menuBar); + + // intercept all menu events and log them in this custom event handler + PushEventHandler(new MyEvtHandler(this)); +} + +MyFrame::~MyFrame() +{ + delete m_menu; + + // delete the event handler installed in ctor + PopEventHandler(TRUE); } -wxMenu *MyFrame::CreateDummyMenu() +wxMenu *MyFrame::CreateDummyMenu(wxString *title) { wxMenu *menu = new wxMenu; menu->Append(Menu_Dummy_First, "First item\tCtrl-F1"); menu->AppendSeparator(); menu->Append(Menu_Dummy_Second, "Second item\tCtrl-F2", "", TRUE); + if ( title ) + { + title->Printf("Dummy menu &%d", ++m_countDummy); + } + return menu; } @@ -283,6 +343,23 @@ wxMenuItem *MyFrame::GetLastMenuItem() const } } +void MyFrame::LogMenuEvent(const wxCommandEvent& event) +{ + int id = event.GetId(); + wxString msg = wxString::Format("Menu command %d", id); + if ( GetMenuBar()->FindItem(id)->IsCheckable() ) + { + msg += wxString::Format(" (the item is currently %schecked)", + event.IsChecked() ? "" : "not "); + } + + SetStatusText(msg, 1); +} + +// ---------------------------------------------------------------------------- +// menu callbacks +// ---------------------------------------------------------------------------- + void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(TRUE); @@ -311,14 +388,18 @@ void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event)) } } -void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event)) +void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event)) { - static s_count = 0; - wxString title; - title.Printf("Dummy menu &%d", ++s_count); + wxMenu *menu = CreateDummyMenu(&title); + GetMenuBar()->Insert(0, menu, title); +} - GetMenuBar()->Append(CreateDummyMenu(), title); +void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event)) +{ + wxString title; + wxMenu *menu = CreateDummyMenu(&title); + GetMenuBar()->Append(menu, title); } void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event)) @@ -354,7 +435,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,10 +466,11 @@ 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", - CreateDummyMenu()); + menu->Append(Menu_Dummy_Last, "Dummy sub menu", + CreateDummyMenu(NULL), "Dummy sub menu help"); } void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event)) @@ -436,6 +518,8 @@ void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event) { + wxLogNull nolog; + wxMenuItem *item = GetLastMenuItem(); event.Enable(item && item->IsCheckable()); @@ -448,7 +532,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,12 +546,87 @@ 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"); menu.Append(Menu_Help_About, "&About"); - menu.Append(Menu_Popup_Submenu, "Submenu", CreateDummyMenu()); + menu.Append(Menu_Popup_Submenu, "Submenu", CreateDummyMenu(NULL)); menu.Append(Menu_Popup_ToBeDeleted, "To be deleted"); menu.Append(Menu_Popup_ToBeChecked, "To be checked", "", TRUE); menu.Append(Menu_Popup_ToBeGreyed, "To be greyed"); @@ -479,4 +638,11 @@ void MyFrame::OnRightDown(wxMouseEvent &event ) menu.Enable(Menu_Popup_ToBeGreyed, FALSE); PopupMenu( &menu, event.GetX(), event.GetY() ); + + // test for destroying items in popup menus +#if 0 + menu.Destroy(Menu_Popup_Submenu); + + PopupMenu( &menu, event.GetX(), event.GetY() ); +#endif // 0 }