]> git.saurik.com Git - wxWidgets.git/commitdiff
Added private wxMenu::MSWNewFromHMENU() method.
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 8 Aug 2011 09:32:39 +0000 (09:32 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 8 Aug 2011 09:32:39 +0000 (09:32 +0000)
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

include/wx/msw/menu.h
src/msw/menu.cpp

index 4c5e95f5ad2fece2044ea313e3c04e5f1adb351f..3ea840430974eed2dab1fd0bf6ab557526e197de 100644 (file)
@@ -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)
index c1accd90e2f30272b0801a7bd762f2fcfbc1f1c2..fe8b4a51dde02959b77bbb2ed9edf22c12710158 100644 (file)
@@ -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()
 {