From: Vadim Zeitlin Date: Mon, 8 Aug 2011 09:32:39 +0000 (+0000) Subject: Added private wxMenu::MSWNewFromHMENU() method. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/960493053b4923a9c882fb54c195ea530de72da1 Added private wxMenu::MSWNewFromHMENU() method. Add a method allowing creation of a wxMenu object from a native menu handle. This will be used to implement access to the system menu in an upcoming commit but could also be useful for other purposes. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@68595 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/msw/menu.h b/include/wx/msw/menu.h index 4c5e95f5ad..3ea8404309 100644 --- a/include/wx/msw/menu.h +++ b/include/wx/msw/menu.h @@ -61,6 +61,14 @@ public: virtual void SetTitle(const wxString& title); + // MSW-only methods + // ---------------- + + // Create a new menu from the given native HMENU. Takes ownership of the + // menu handle and will delete it when this object is destroyed. + static wxMenu *MSWNewFromHMENU(WXHMENU hMenu) { return new wxMenu(hMenu); } + + // implementation only from now on // ------------------------------- @@ -120,7 +128,14 @@ protected: virtual wxMenuItem* DoRemove(wxMenuItem *item); private: - // common part of all ctors + // This constructor is private, use MSWNewFromHMENU() to use it. + wxMenu(WXHMENU hMenu); + + // Common part of all ctors, it doesn't create a new HMENU. + void InitNoCreate(); + + // Common part of all ctors except of the one above taking a native menu + // handler: calls InitNoCreate() and also creates a new menu. void Init(); // common part of Append/Insert (behaves as Append is pos == (size_t)-1) diff --git a/src/msw/menu.cpp b/src/msw/menu.cpp index c1accd90e2..fe8b4a51dd 100644 --- a/src/msw/menu.cpp +++ b/src/msw/menu.cpp @@ -260,7 +260,7 @@ inline bool IsGreaterThanStdSize(const wxBitmap& bmp) // --------------------------------------------------------------------------- // Construct a menu with optional title (then use append) -void wxMenu::Init() +void wxMenu::InitNoCreate() { m_radioData = NULL; m_doBreak = false; @@ -270,6 +270,11 @@ void wxMenu::Init() m_maxBitmapWidth = 0; m_maxAccelWidth = -1; #endif // wxUSE_OWNER_DRAWN +} + +void wxMenu::Init() +{ + InitNoCreate(); // create the menu m_hMenu = (WXHMENU)CreatePopupMenu(); @@ -287,6 +292,24 @@ void wxMenu::Init() } } +wxMenu::wxMenu(WXHMENU hMenu) +{ + InitNoCreate(); + + m_hMenu = hMenu; + + // Ensure that our internal idea of how many items we have corresponds to + // the real number of items in the menu. + // + // We could also retrieve the real labels of the items here but it doesn't + // seem to be worth the trouble. + const int numExistingItems = ::GetMenuItemCount(m_hMenu); + for ( int n = 0; n < numExistingItems; n++ ) + { + wxMenuBase::DoAppend(wxMenuItem::New(this, wxID_SEPARATOR)); + } +} + // The wxWindow destructor will take care of deleting the submenus. wxMenu::~wxMenu() {