From 9b0c9de5b9503d2b345e407f1a837f86541f5ec5 Mon Sep 17 00:00:00 2001 From: Persune Date: Wed, 8 Jan 2025 22:16:45 +0800 Subject: [PATCH 1/2] Match submenu item instead of hardcoding indices This fixes #317. --- Source/FamiTracker.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Source/FamiTracker.cpp b/Source/FamiTracker.cpp index c7bd5ef4..982c8d52 100644 --- a/Source/FamiTracker.cpp +++ b/Source/FamiTracker.cpp @@ -451,10 +451,23 @@ void CFamiTrackerApp::OnRecentFilesClear() // // // SAFE_RELEASE(m_pRecentFileList); m_pRecentFileList = new CRecentFileList(0, _T("Recent File List"), _T("File%d"), MAX_RECENT_FILES); - auto pMenu = m_pMainWnd->GetMenu()->GetSubMenu(0)->GetSubMenu(14); - for (int i = 0; i < MAX_RECENT_FILES; ++i) - pMenu->RemoveMenu(ID_FILE_MRU_FILE1 + i, MF_BYCOMMAND); - pMenu->AppendMenu(MF_STRING, ID_FILE_MRU_FILE1, _T("(File)")); + // Files menu + auto pFilesMenu = m_pMainWnd->GetMenu()->GetSubMenu(0); + + // Try and find Recent Files submenu + for (int nPos = pFilesMenu->GetMenuItemCount(); nPos > 0; --nPos) { + UINT Status = pFilesMenu->GetMenuState(nPos, MF_BYPOSITION); + // check if valid menu state + if (Status != UINT(-1)) { + // check if it has a submenu and the first item is ID_RECENTFILES_CLEAR + auto pSubMenu = pFilesMenu->GetSubMenu(nPos); + if (pSubMenu != nullptr && pSubMenu->GetMenuItemID(0) == ID_RECENTFILES_CLEAR) { + for (int i = 0; i < MAX_RECENT_FILES; ++i) + pSubMenu->RemoveMenu(ID_FILE_MRU_FILE1 + i, MF_BYCOMMAND); + pSubMenu->AppendMenu(MF_STRING, ID_FILE_MRU_FILE1, _T("(File)")); + } + } + } } void CFamiTrackerApp::OnUpdateRecentFiles(CCmdUI *pCmdUI) // // // From 4de0240f01f16fe9276e99feb3ef2ad1076a6700 Mon Sep 17 00:00:00 2001 From: Persune Date: Thu, 9 Jan 2025 12:58:54 +0800 Subject: [PATCH 2/2] Add loop break and error handling --- Source/FamiTracker.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Source/FamiTracker.cpp b/Source/FamiTracker.cpp index 982c8d52..8d4cc122 100644 --- a/Source/FamiTracker.cpp +++ b/Source/FamiTracker.cpp @@ -455,19 +455,20 @@ void CFamiTrackerApp::OnRecentFilesClear() // // // auto pFilesMenu = m_pMainWnd->GetMenu()->GetSubMenu(0); // Try and find Recent Files submenu - for (int nPos = pFilesMenu->GetMenuItemCount(); nPos > 0; --nPos) { - UINT Status = pFilesMenu->GetMenuState(nPos, MF_BYPOSITION); + for (int nPos = 0; nPos < pFilesMenu->GetMenuItemCount(); nPos++) // check if valid menu state - if (Status != UINT(-1)) { + if (pFilesMenu->GetMenuState(nPos, MF_BYPOSITION) != UINT(-1)) { // check if it has a submenu and the first item is ID_RECENTFILES_CLEAR auto pSubMenu = pFilesMenu->GetSubMenu(nPos); if (pSubMenu != nullptr && pSubMenu->GetMenuItemID(0) == ID_RECENTFILES_CLEAR) { for (int i = 0; i < MAX_RECENT_FILES; ++i) pSubMenu->RemoveMenu(ID_FILE_MRU_FILE1 + i, MF_BYCOMMAND); pSubMenu->AppendMenu(MF_STRING, ID_FILE_MRU_FILE1, _T("(File)")); + return; } } - } + + throw std::runtime_error("Could not find \"ID_RECENTFILES_CLEAR\""); } void CFamiTrackerApp::OnUpdateRecentFiles(CCmdUI *pCmdUI) // // //