commit 343bdbbc46b955f26e280b009c9156ecc92ec2af Author: Justin Frankel Date: Thu Apr 27 13:23:31 2017 -0400 swell-generic: retain menus while open diff --git a/WDL/swell/swell-internal.h b/WDL/swell/swell-internal.h index d36c32e4b3..40aa3508b2 100644 --- a/WDL/swell/swell-internal.h +++ b/WDL/swell/swell-internal.h @@ -665,15 +665,20 @@ struct HWND__ struct HMENU__ { - HMENU__() { sel_vis = -1; } - ~HMENU__() { items.Empty(true,freeMenuItem); } + HMENU__() { m_refcnt=1; sel_vis = -1; } + + void Retain() { m_refcnt++; } + void Release() { if (!--m_refcnt) delete this; } WDL_PtrList items; int sel_vis; // for mouse/keyboard nav + int m_refcnt; HMENU__ *Duplicate(); static void freeMenuItem(void *p); +private: + ~HMENU__() { items.Empty(true,freeMenuItem); } }; diff --git a/WDL/swell/swell-menu-generic.cpp b/WDL/swell/swell-menu-generic.cpp index 4697c8c5f2..19847e9415 100644 --- a/WDL/swell/swell-menu-generic.cpp +++ b/WDL/swell/swell-menu-generic.cpp @@ -54,7 +54,7 @@ void HMENU__::freeMenuItem(void *p) { MENUITEMINFO *inf = (MENUITEMINFO *)p; if (!inf) return; - delete inf->hSubMenu; + if (inf->hSubMenu) inf->hSubMenu->Release(); if (inf->fType == MFT_STRING) free(inf->dwTypeData); free(inf); } @@ -157,7 +157,7 @@ HMENU CreatePopupMenuEx(const char *title) void DestroyMenu(HMENU hMenu) { - delete hMenu; + if (hMenu) hMenu->Release(); } int AddMenuItem(HMENU hMenu, int pos, const char *name, int tagid) @@ -215,7 +215,7 @@ BOOL SetMenuItemInfo(HMENU hMenu, int pos, BOOL byPos, MENUITEMINFO *mi) if ((mi->fMask & MIIM_SUBMENU) && mi->hSubMenu != item->hSubMenu) { - delete item->hSubMenu; + if (item->hSubMenu) item->hSubMenu->Release(); item->hSubMenu = mi->hSubMenu; } if (mi->fMask & MIIM_TYPE) @@ -1000,6 +1000,8 @@ int TrackPopupMenu(HMENU hMenu, int flags, int xpos, int ypos, int resvd, HWND h if (!hMenu || m_trackingMenus.GetSize()) return 0; ReleaseCapture(); + + hMenu->Retain(); m_trackingPar=hwnd; m_trackingFlags=flags; m_trackingRet=-1; @@ -1045,6 +1047,8 @@ int TrackPopupMenu(HMENU hMenu, int flags, int xpos, int ypos, int resvd, HWND h if (hwnd) hwnd->Release(); + hMenu->Release(); + return m_trackingRet>0?m_trackingRet:0; } diff --git a/WDL/swell/swell-wnd-generic.cpp b/WDL/swell/swell-wnd-generic.cpp index 874a7e66cc..819dce4dd3 100644 --- a/WDL/swell/swell-wnd-generic.cpp +++ b/WDL/swell/swell-wnd-generic.cpp @@ -7155,6 +7155,7 @@ int menuBarNavigate(int dir) // -1 if no menu bar active, 0 if did nothing, 1 if RECT g_trackpopup_yroot; static void runMenuBar(HWND hwnd, HMENU__ *menu, int x, const RECT *use_r) { + menu->Retain(); MENUITEMINFO *inf = menu->items.Get(x); RECT r = *use_r; g_trackpopup_yroot = r; @@ -7182,6 +7183,7 @@ static void runMenuBar(HWND hwnd, HMENU__ *menu, int x, const RECT *use_r) InvalidateRect(hwnd,&mbr,FALSE); g_menubar_active = NULL; g_trackpopup_yroot.top = g_trackpopup_yroot.bottom = 0; + menu->Release(); } LRESULT DefWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)