Fix problem with inserting menus in wxMDIParentFrame menu bar.

Account for the "Window" menu (and any other foreign windows lurking in our
menu bar) correctly in wxMenuBar::Insert() instead of the old code which
apparently tried to do something similar but didn't make much sense and didn't
work when trying to insert the menu at the position actually occupied by the
"Window" menu in the menu bar.

Closes #15662, #1732.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@75241 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2013-11-18 14:19:40 +00:00
parent 622f1081b8
commit fec6f3bea1

View File

@@ -1367,10 +1367,6 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
(GetHmenu() != 0); (GetHmenu() != 0);
#endif #endif
int mswpos = (!isAttached || (pos == m_menus.GetCount()))
? -1 // append the menu
: MSWPositionForWxMenu(GetMenu(pos),pos);
if ( !wxMenuBarBase::Insert(pos, menu, title) ) if ( !wxMenuBarBase::Insert(pos, menu, title) )
return false; return false;
@@ -1398,9 +1394,33 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
wxLogLastError(wxT("TB_INSERTBUTTON")); wxLogLastError(wxT("TB_INSERTBUTTON"));
return false; return false;
} }
wxUnusedVar(mswpos);
#else #else
if ( !::InsertMenu(GetHmenu(), mswpos, // We have a problem with the index if there is an extra "Window" menu
// in this menu bar, which is added by wxMDIParentFrame to it directly
// using Windows API (so that it remains invisible to the user code),
// but which does affect the indices of the items we insert after it.
// So we check if any of the menus before the insertion position is a
// foreign one and adjust the insertion index accordingly.
int mswExtra = 0;
// Skip all this if the total number of menus matches (notice that the
// internal menu count has already been incremented by wxMenuBarBase::
// Insert() call above, hence -1).
int mswCount = ::GetMenuItemCount(GetHmenu());
if ( mswCount != -1 &&
static_cast<unsigned>(mswCount) != GetMenuCount() - 1 )
{
wxMenuList::compatibility_iterator node = m_menus.GetFirst();
for ( size_t n = 0; n < pos; n++ )
{
if ( ::GetSubMenu(GetHmenu(), n) != GetHmenuOf(node->GetData()) )
mswExtra++;
else
node = node->GetNext();
}
}
if ( !::InsertMenu(GetHmenu(), pos + mswExtra,
MF_BYPOSITION | MF_POPUP | MF_STRING, MF_BYPOSITION | MF_POPUP | MF_STRING,
(UINT_PTR)GetHmenuOf(menu), title.t_str()) ) (UINT_PTR)GetHmenuOf(menu), title.t_str()) )
{ {