#ifndef WX_PRECOMP
#include <wx/wx.h>
-
#include <wx/log.h>
#endif
+#include "copy.xpm"
+
// ----------------------------------------------------------------------------
// classes
// ----------------------------------------------------------------------------
public:
MyFrame();
- virtual ~MyFrame() { delete m_menu; }
+ virtual ~MyFrame();
+
+ void LogMenuEvent(const wxCommandEvent& event);
void OnQuit(wxCommandEvent& event);
void OnAbout(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);
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()
};
Menu_MenuBar_Toggle = 200,
Menu_MenuBar_Append,
+ Menu_MenuBar_Insert,
Menu_MenuBar_Delete,
Menu_MenuBar_Enable,
Menu_MenuBar_GetLabel,
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)
EVT_RIGHT_DOWN(MyFrame::OnRightDown)
END_EVENT_TABLE()
+BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
+ EVT_MENU(-1, MyEvtHandler::OnMenuEvent)
+END_EVENT_TABLE()
+
// ============================================================================
// implementation
// ============================================================================
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\tAlt-Q" );
+ 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",
// 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));
}
-wxMenu *MyFrame::CreateDummyMenu()
+MyFrame::~MyFrame()
+{
+ delete m_menu;
+
+ // delete the event handler installed in ctor
+ PopEventHandler(TRUE);
+}
+
+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;
}
}
}
+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);
}
}
-void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event))
+void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event))
{
- static int 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))
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-F2",
- CreateDummyMenu());
+
+ menu->Append(Menu_Dummy_Last, "Dummy sub menu",
+ CreateDummyMenu(NULL), "Dummy sub menu help");
}
void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event))
void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event)
{
+ wxLogNull nolog;
+
wxMenuItem *item = GetLastMenuItem();
event.Enable(item && item->IsCheckable());
msg << "The item is " << (item->IsEnabled() ? "enabled"
: "disabled")
<< '\n';
-
+
if ( item->IsCheckable() )
{
msg << "It is checkable and " << (item->IsChecked() ? "" : "un")
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");
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);
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
}