From 7f3f059ac5f8e01052f9fb0854e31cb2303e193d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 28 Dec 2011 13:51:17 +0000 Subject: [PATCH] Complete wxEVT_MENU_{OPEN,CLOSE} implementation in wxMSW and wxOSX. Set the wxMenu correctly for wxEVT_MENU_CLOSE events in wxMSW. Set the menu id correctly to allow wxMenuEvent::IsPopup() to work for both wxEVT_MENU_OPEN and wxEVT_MENU_CLOSE in wxOSX. Closes #11313. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70151 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/msw/frame.h | 5 ++--- include/wx/osx/menu.h | 4 ++++ interface/wx/event.h | 9 ++++++--- src/msw/frame.cpp | 27 +++++++++++++-------------- src/osx/menu_osx.cpp | 17 +++++++++++++---- 6 files changed, 39 insertions(+), 24 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index a4cc9f2f3a..9d420c3750 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -468,6 +468,7 @@ MSW: - Fixed regression with initial focus in the dialogs in 2.9.3. - Added support for wxEXEC_MAKE_GROUP_LEADER to wxExecute (tteras). +- Set wxMenu being closed in wxEVT_MENU_CLOSE events (Marcin Malich). OSX: diff --git a/include/wx/msw/frame.h b/include/wx/msw/frame.h index d528a882dd..b7404bddc2 100644 --- a/include/wx/msw/frame.h +++ b/include/wx/msw/frame.h @@ -79,7 +79,6 @@ public: bool HandleSize(int x, int y, WXUINT flag); bool HandleCommand(WXWORD id, WXWORD cmd, WXHWND control); bool HandleMenuSelect(WXWORD nItem, WXWORD nFlags, WXHMENU hMenu); - bool HandleMenuLoop(const wxEventType& evtType, WXWORD isPopup); // tooltip management #if wxUSE_TOOLTIPS @@ -133,8 +132,8 @@ protected: // wxMDIChildFrame bool MSWDoTranslateMessage(wxFrame *frame, WXMSG *msg); - // handle WM_INITMENUPOPUP message to generate wxEVT_MENU_OPEN - bool HandleInitMenuPopup(WXHMENU hMenu); + // handle WM_(UN)INITMENUPOPUP message to generate wxEVT_MENU_OPEN/CLOSE + bool HandleMenuPopup(wxEventType evtType, WXHMENU hMenu); virtual bool IsMDIChild() const { return false; } diff --git a/include/wx/osx/menu.h b/include/wx/osx/menu.h index 55796749a1..b3d742defd 100644 --- a/include/wx/osx/menu.h +++ b/include/wx/osx/menu.h @@ -83,6 +83,10 @@ private: // terminate the current radio group, if any void EndRadioGroup(); + // Common part of HandleMenu{Opened,Closed}(). + void DoHandleMenuOpenedOrClosed(wxEventType evtType); + + // if TRUE, insert a breal before appending the next item bool m_doBreak; diff --git a/interface/wx/event.h b/interface/wx/event.h index 5cb9c497aa..7cf8ec67f4 100644 --- a/interface/wx/event.h +++ b/interface/wx/event.h @@ -3870,9 +3870,12 @@ public: wxMenuEvent(wxEventType type = wxEVT_NULL, int id = 0, wxMenu* menu = NULL); /** - Returns the menu which is being opened or closed. This method should only be - used with the @c OPEN and @c CLOSE events and even for them the - returned pointer may be @NULL in some ports. + Returns the menu which is being opened or closed. + + This method can only be used with the @c OPEN and @c CLOSE events. + + The returned value is never @NULL in the ports implementing this + function, which currently includes all the major ones. */ wxMenu* GetMenu() const; diff --git a/src/msw/frame.cpp b/src/msw/frame.cpp index 9126afb4d9..ed3369c64c 100644 --- a/src/msw/frame.cpp +++ b/src/msw/frame.cpp @@ -61,9 +61,9 @@ // globals // ---------------------------------------------------------------------------- -#if wxUSE_MENUS_NATIVE +#if wxUSE_MENUS || wxUSE_MENUS_NATIVE extern wxMenu *wxCurrentPopupMenu; -#endif // wxUSE_MENUS_NATIVE +#endif // wxUSE_MENUS || wxUSE_MENUS_NATIVE // ---------------------------------------------------------------------------- // event tables @@ -849,25 +849,24 @@ wxFrame::HandleMenuSelect(WXWORD nItem, WXWORD flags, WXHMENU WXUNUSED(hMenu)) return false; } -bool wxFrame::HandleMenuLoop(const wxEventType& evtType, WXWORD isPopup) +bool wxFrame::HandleMenuPopup(wxEventType evtType, WXHMENU hMenu) { // we don't have the menu id here, so we use the id to specify if the event // was from a popup menu or a normal one - wxMenuEvent event(evtType, isPopup ? -1 : 0); - event.SetEventObject(this); - return HandleWindowEvent(event); -} - -bool wxFrame::HandleInitMenuPopup(WXHMENU hMenu) -{ + int menuid = 0; wxMenu* menu = NULL; if (GetMenuBar()) { menu = GetMenuBar()->MSWGetMenu(hMenu); } + else if ( wxCurrentPopupMenu && wxCurrentPopupMenu->GetHMenu() == hMenu ) + { + menu = wxCurrentPopupMenu; + menuid = wxID_ANY; + } - wxMenuEvent event(wxEVT_MENU_OPEN, 0, menu); + wxMenuEvent event(evtType, menuid, menu); event.SetEventObject(this); return HandleWindowEvent(event); @@ -917,7 +916,7 @@ WXLRESULT wxFrame::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lPara #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__) #if wxUSE_MENUS case WM_INITMENUPOPUP: - processed = HandleInitMenuPopup((WXHMENU) wParam); + processed = HandleMenuPopup(wxEVT_MENU_OPEN, (WXHMENU)wParam); break; case WM_MENUSELECT: @@ -930,8 +929,8 @@ WXLRESULT wxFrame::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lPara } break; - case WM_EXITMENULOOP: - processed = HandleMenuLoop(wxEVT_MENU_CLOSE, (WXWORD)wParam); + case WM_UNINITMENUPOPUP: + processed = HandleMenuPopup(wxEVT_MENU_CLOSE, (WXHMENU)wParam); break; #endif // wxUSE_MENUS diff --git a/src/osx/menu_osx.cpp b/src/osx/menu_osx.cpp index ee18135ab7..da7d66545a 100644 --- a/src/osx/menu_osx.cpp +++ b/src/osx/menu_osx.cpp @@ -452,16 +452,25 @@ void wxMenu::HandleMenuItemHighlighted( wxMenuItem* item ) DoHandleMenuEvent( wxevent ); } -void wxMenu::HandleMenuOpened() +void wxMenu::DoHandleMenuOpenedOrClosed(wxEventType evtType) { - wxMenuEvent wxevent(wxEVT_MENU_OPEN, 0, this); + // Popup menu being currently shown or NULL, defined in wincmn.cpp. + extern wxMenu *wxCurrentPopupMenu; + + // Set the id to allow wxMenuEvent::IsPopup() to work correctly. + int menuid = this == wxCurrentPopupMenu ? wxID_ANY : 0; + wxMenuEvent wxevent(evtType, menuid, this); DoHandleMenuEvent( wxevent ); } +void wxMenu::HandleMenuOpened() +{ + DoHandleMenuOpenedOrClosed(wxEVT_MENU_OPEN); +} + void wxMenu::HandleMenuClosed() { - wxMenuEvent wxevent(wxEVT_MENU_CLOSE, 0, this); - DoHandleMenuEvent( wxevent ); + DoHandleMenuOpenedOrClosed(wxEVT_MENU_CLOSE); } bool wxMenu::DoHandleMenuEvent(wxEvent& wxevent) -- 2.45.2