menu positions to the ones that Windows is using.  This mainly accounts
for the system menu in maximized MDI child frames.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@25171 
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
+    // Return the MSW position for a wxMenu which is sometimes different from
+    // the wxWindows position.
+    int MSWPositionForWxMenu(wxMenu *menu, int wxpos);
 #if wxUSE_ACCEL
     // the accelerator table for all accelerators in all our menus
     wxAcceleratorTable m_accelTable;
 #if wxUSE_ACCEL
     // the accelerator table for all accelerators in all our menus
     wxAcceleratorTable m_accelTable;
 
+int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos)
+{
+    wxASSERT(menu);
+    wxASSERT(menu->GetHMenu());
+    wxASSERT(m_hMenu);
+    int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu);
+    int i; // For old C++ compatibility
+    for(i=wxpos; i<totalMSWItems; i++)
+    {
+        if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
+            return i;
+    }
+    for(i=0; i<wxpos; i++)
+    {
+        if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
+            return i;
+    }
+    wxFAIL;
+    return -1;
+}
+
 // ---------------------------------------------------------------------------
 // wxMenuBar functions to work with the top level submenus
 // ---------------------------------------------------------------------------
 // ---------------------------------------------------------------------------
 // wxMenuBar functions to work with the top level submenus
 // ---------------------------------------------------------------------------
 void wxMenuBar::EnableTop(size_t pos, bool enable)
 {
     wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
 void wxMenuBar::EnableTop(size_t pos, bool enable)
 {
     wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
+    wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
 
     int flag = enable ? MF_ENABLED : MF_GRAYED;
 
 
     int flag = enable ? MF_ENABLED : MF_GRAYED;
 
-    EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
+    EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag);
     }
     //else: have to modify the existing menu
 
     }
     //else: have to modify the existing menu
 
+    int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
+
-    UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
+    UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
     if ( flagsOld == 0xFFFFFFFF )
     {
         wxLogLastError(wxT("GetMenuState"));
     if ( flagsOld == 0xFFFFFFFF )
     {
         wxLogLastError(wxT("GetMenuState"));
     {
         // HIBYTE contains the number of items in the submenu in this case
         flagsOld &= 0xff;
     {
         // HIBYTE contains the number of items in the submenu in this case
         flagsOld &= 0xff;
-        id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos);
+        id = (UINT)::GetSubMenu((HMENU)m_hMenu, mswpos);
-    if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
+    if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
         id, label) == (int)0xFFFFFFFF )
     {
         wxLogLastError(wxT("ModifyMenu"));
         id, label) == (int)0xFFFFFFFF )
     {
         wxLogLastError(wxT("ModifyMenu"));
+        int mswpos = MSWPositionForWxMenu(menuOld,pos);
+
         // can't use ModifyMenu() because it deletes the submenu it replaces
         // can't use ModifyMenu() because it deletes the submenu it replaces
-        if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
+        if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
         {
             wxLogLastError(wxT("RemoveMenu"));
         }
 
         {
             wxLogLastError(wxT("RemoveMenu"));
         }
 
-        if ( !::InsertMenu(GetHmenu(), (UINT)pos,
+        if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
                            MF_BYPOSITION | MF_POPUP | MF_STRING,
                            (UINT)GetHmenuOf(menu), title) )
         {
                            MF_BYPOSITION | MF_POPUP | MF_STRING,
                            (UINT)GetHmenuOf(menu), title) )
         {
 
 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
 {
 
 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
 {
+    // Find out which MSW item before which we'll be inserting before
+    // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
+    int mswpos = (pos == m_menus.GetCount())
+        ?   -1 // append the menu
+        :   MSWPositionForWxMenu(GetMenu(pos),pos);
+
     if ( !wxMenuBarBase::Insert(pos, menu, title) )
         return FALSE;
 
     if ( !wxMenuBarBase::Insert(pos, menu, title) )
         return FALSE;
 
-        if ( !::InsertMenu(GetHmenu(), pos,
+        if ( !::InsertMenu(GetHmenu(), mswpos,
                            MF_BYPOSITION | MF_POPUP | MF_STRING,
                            (UINT)GetHmenuOf(menu), title) )
         {
                            MF_BYPOSITION | MF_POPUP | MF_STRING,
                            (UINT)GetHmenuOf(menu), title) )
         {
-        if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
+        if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
         {
             wxLogLastError(wxT("RemoveMenu"));
         }
         {
             wxLogLastError(wxT("RemoveMenu"));
         }