From 3d476369ece57365c1f055d793532772d6de0a9b Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Thu, 28 Jan 2016 22:32:27 +0100 Subject: [PATCH] Fix inserting owner drawn menu items in wxMSW after menu creation When menu item is inserted to the menu using wxMenu::DoInsert(), it is first added to the list of all menu items in wxMenuBase::DoAppend() and then it is added to the physical menu in wxMenu::DoInsertOrAppend(). Prior to adding the menu item to the menu all already existing menu items are set as owner drawn, so the list of items is iterated and SetOwnerDrawnMenuItem() is called for each item. Because the new item is on the list but doesn't yet physically exist, it has to be skipped in the iterations to avoid assigning wrong data (like address of handler) to the wrong item. Closes #17350. --- src/msw/menu.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/msw/menu.cpp b/src/msw/menu.cpp index b079933db3..95d9585722 100644 --- a/src/msw/menu.cpp +++ b/src/msw/menu.cpp @@ -632,7 +632,11 @@ bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) { wxMenuItem* item = node->GetData(); - if ( !item->IsOwnerDrawn()) + // Current item is already added to the list of items + // but is not yet physically attached to the menu + // so we have to skip setting it as an owner drawn. + // It will be done later on when the item will be created. + if ( !item->IsOwnerDrawn() && item != pItem ) { item->SetOwnerDrawn(true); SetOwnerDrawnMenuItem(GetHmenu(), position, @@ -642,7 +646,12 @@ bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) item->SetMarginWidth(m_maxBitmapWidth); node = node->GetNext(); - position++; + // Current item is already added to the list of items + // but is not yet physically attached to the menu + // so it cannot be counted while determining position + // in the menu. + if ( item != pItem ) + position++; } // set menu as ownerdrawn