X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7118e711dcf3bdb543d5e20cac2c45d50c691979..3dea8ba7acb49948ccda20029655fd190a306298:/include/wx/menu.h diff --git a/include/wx/menu.h b/include/wx/menu.h index 8914432b71..accb6f1d7e 100644 --- a/include/wx/menu.h +++ b/include/wx/menu.h @@ -256,11 +256,14 @@ public: wxEvtHandler *GetEventHandler() const { return m_eventHandler; } // Invoking window: this is set by wxWindow::PopupMenu() before showing a - // popup menu and reset after it's hidden. Notice that GetInvokingWindow() - // recurses upwards and will return the invoking window for any submenu of - // a popup menu as well as the menu itself. + // popup menu and reset after it's hidden. Notice that you probably want to + // use GetWindow() below instead of GetInvokingWindow() as the latter only + // returns non-NULL for the top level menus + // + // NB: avoid calling SetInvokingWindow() directly if possible, use + // wxMenuInvokingWindowSetter class below instead void SetInvokingWindow(wxWindow *win); - wxWindow *GetInvokingWindow() const; + wxWindow *GetInvokingWindow() const { return m_invokingWindow; } // the window associated with this menu: this is the invoking window for // popup menus or the top level window to which the menu bar is attached @@ -551,7 +554,35 @@ protected: #endif #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY +// ---------------------------------------------------------------------------- +// Helper class used in the implementation only: sets the invoking window of +// the given menu in its ctor and resets it in dtor. +// ---------------------------------------------------------------------------- + +class wxMenuInvokingWindowSetter +{ +public: + // Ctor sets the invoking window for the given menu. + // + // The menu lifetime must be greater than that of this class. + wxMenuInvokingWindowSetter(wxMenu& menu, wxWindow *win) + : m_menu(menu) + { + menu.SetInvokingWindow(win); + } + + // Dtor resets the invoking window. + ~wxMenuInvokingWindowSetter() + { + m_menu.SetInvokingWindow(NULL); + } + +private: + wxMenu& m_menu; + + wxDECLARE_NO_COPY_CLASS(wxMenuInvokingWindowSetter); +}; + #endif // wxUSE_MENUS -#endif - // _WX_MENU_H_BASE_ +#endif // _WX_MENU_H_BASE_