From b98b2b27d670754d8db7c1d80e0c2687da530bc6 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 18 Jul 2000 07:59:05 +0000 Subject: [PATCH] wxCommandEvent::Checked() was renamed to IsChecked() (keeping the old name too, of course), now works for menu item in wxMSW too and is documented accordingly. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_2_BRANCH@7759 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/event.h | 6 ++++- samples/menu/menu.cpp | 57 +++++++++++++++++++++++++++++++++++++++++-- src/msw/menu.cpp | 8 +++++- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/include/wx/event.h b/include/wx/event.h index 1a594944db..d38c3f642a 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -443,7 +443,7 @@ public: wxString GetString() const { return m_commandString; } // Get checkbox value - bool Checked() const { return (m_commandInt != 0); } + bool IsChecked() const { return m_commandInt != 0; } // TRUE if the listbox event was a selection. bool IsSelection() const { return (m_extraLong != 0); } @@ -456,6 +456,10 @@ public: void CopyObject(wxObject& obj) const; +#ifdef WXWIN_COMPATIBILITY_2 + bool Checked() const { return IsChecked(); } +#endif // WXWIN_COMPATIBILITY_2 + public: wxString m_commandString; // String event argument int m_commandInt; diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index 529577f799..cee944b197 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -47,7 +47,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); @@ -88,6 +90,25 @@ private: 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() +}; + // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- @@ -164,6 +185,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 // ============================================================================ @@ -202,7 +227,7 @@ MyFrame::MyFrame() m_menu = NULL; m_countDummy = 0; - CreateStatusBar(); + CreateStatusBar(2); // create the menubar wxMenu *fileMenu = new wxMenu; @@ -267,6 +292,17 @@ 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(wxString *title) @@ -302,6 +338,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); diff --git a/src/msw/menu.cpp b/src/msw/menu.cpp index bf7b961b17..67d416c466 100644 --- a/src/msw/menu.cpp +++ b/src/msw/menu.cpp @@ -434,7 +434,13 @@ bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id) wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED); event.SetEventObject( this ); event.SetId( id ); - event.SetInt( id ); + + // VZ: previosuly, the command int was set to id too which was quite + // useless anyhow (as it could be retrieved using GetId()) and + // uncompatible with wxGTK, so now we use the command int instead + // to pass the checked status + event.SetInt(::GetMenuState(GetHmenu(), id, MF_BYCOMMAND) & MF_CHECKED); + ProcessCommand(event); }