1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-05-04 17:49:45 +02:00

Use weak pointers to simplify MenuCreator

This commit is contained in:
Paul Licameli 2018-10-18 14:50:06 -04:00
parent c22c7b6fce
commit 9ad88e091c
4 changed files with 43 additions and 41 deletions

View File

@ -170,10 +170,6 @@ MenuCreator::MenuCreator(){
MenuCreator::~MenuCreator()
{
if (wxGetApp().GetRecentFiles())
{
wxGetApp().GetRecentFiles()->RemoveMenu(mRecentFilesMenu);
}
}
enum {
@ -1833,15 +1829,6 @@ void MenuCreator::CreateMenusAndCommands(AudacityProject &project)
project.SetMenuBar(menubar.release());
// Bug 143 workaround.
// The bug is in wxWidgets. For a menu that has scrollers, the
// scrollers have an ID of 0 (not wxID_NONE which is -3).
// Therefore wxWidgets attempts to find a help string. See
// wxFrameBase::ShowMenuHelp(int menuId)
// It finds a bogus automatic help string of "Recent &Files"
// from that submenu.
// So we set the help string for command with Id 0 to empty.
mRecentFilesMenu->GetParent()->SetHelpString( 0, "" );
}
@ -2188,19 +2175,35 @@ void MenuCreator::CreateRecentFilesMenu(CommandManager *c)
{
// Recent Files and Recent Projects menus
wxWeakRef<wxMenu> recentFilesMenu = c->BeginSubMenu(
#ifdef __WXMAC__
/* i18n-hint: This is the name of the menu item on Mac OS X only */
mRecentFilesMenu = c->BeginSubMenu(_("Open Recent"));
/* i18n-hint: This is the name of the menu item on Mac OS X only */
_("Open Recent")
#else
/* i18n-hint: This is the name of the menu item on Windows and Linux */
mRecentFilesMenu = c->BeginSubMenu(_("Recent &Files"));
/* i18n-hint: This is the name of the menu item on Windows and Linux */
_("Recent &Files")
#endif
wxGetApp().GetRecentFiles()->UseMenu(mRecentFilesMenu);
wxGetApp().GetRecentFiles()->AddFilesToMenu(mRecentFilesMenu);
);
wxGetApp().GetRecentFiles()->UseMenu(recentFilesMenu);
wxGetApp().GetRecentFiles()->AddFilesToMenu(recentFilesMenu);
c->EndSubMenu();
wxTheApp->CallAfter( [=] {
// Bug 143 workaround.
// The bug is in wxWidgets. For a menu that has scrollers, the
// scrollers have an ID of 0 (not wxID_NONE which is -3).
// Therefore wxWidgets attempts to find a help string. See
// wxFrameBase::ShowMenuHelp(int menuId)
// It finds a bogus automatic help string of "Recent &Files"
// from that submenu.
// So we set the help string for command with Id 0 to empty.
if ( recentFilesMenu )
recentFilesMenu->GetParent()->SetHelpString( 0, "" );
});
}
// TODO: This surely belongs in CommandManager?
@ -2250,9 +2253,6 @@ void MenuCreator::RebuildMenuBar(AudacityProject &project)
}
#endif
// Allow FileHistory to remove its own menu
wxGetApp().GetRecentFiles()->RemoveMenu(mRecentFilesMenu);
// Delete the menus, since we will soon recreate them.
// Rather oddly, the menus don't vanish as a result of doing this.
{

View File

@ -617,9 +617,6 @@ public:
void CreateRecentFilesMenu(CommandManager *c);
public:
// Recent files
wxMenu *mRecentFilesMenu;
CommandFlag mLastFlags;
// Last effect applied to this project

View File

@ -101,6 +101,8 @@ size_t FileHistory::GetCount()
void FileHistory::UseMenu(wxMenu *menu)
{
Compress();
auto end = mMenus.end();
auto iter = std::find(mMenus.begin(), end, menu);
auto found = (iter != end);
@ -112,19 +114,6 @@ void FileHistory::UseMenu(wxMenu *menu)
}
}
void FileHistory::RemoveMenu(wxMenu *menu)
{
auto end = mMenus.end();
auto iter = std::find(mMenus.begin(), end, menu);
auto found = (iter != end);
if (found)
mMenus.erase(iter);
else {
wxASSERT(false);
}
}
void FileHistory::Load(wxConfigBase & config, const wxString & group)
{
mHistory.Clear();
@ -161,8 +150,10 @@ void FileHistory::Save(wxConfigBase & config, const wxString & group)
void FileHistory::AddFilesToMenu()
{
Compress();
for (auto pMenu : mMenus)
AddFilesToMenu(pMenu);
if (pMenu)
AddFilesToMenu(pMenu);
}
void FileHistory::AddFilesToMenu(wxMenu *menu)
@ -183,3 +174,15 @@ void FileHistory::AddFilesToMenu(wxMenu *menu)
menu->Append(mIDBase, _("&Clear"));
menu->Enable(mIDBase, mHistory.GetCount() > 0);
}
void FileHistory::Compress()
{
// Clear up expired weak pointers
auto end = mMenus.end();
mMenus.erase(
std::remove_if( mMenus.begin(), end,
[](wxWeakRef<wxMenu> &pMenu){ return !pMenu; } ),
end
);
}

View File

@ -19,6 +19,7 @@
#include <wx/grid.h>
#include <wx/string.h>
#include <wx/window.h>
#include <wx/weakref.h>
class AUDACITY_DLL_API FileHistory
{
@ -30,7 +31,6 @@ class AUDACITY_DLL_API FileHistory
void RemoveFileFromHistory(size_t i, bool update = true);
void Clear();
void UseMenu(wxMenu *menu);
void RemoveMenu(wxMenu *menu);
void Load(wxConfigBase& config, const wxString & group);
void Save(wxConfigBase& config, const wxString & group);
@ -41,10 +41,12 @@ class AUDACITY_DLL_API FileHistory
const wxString &GetHistoryFile(size_t i) const;
private:
void Compress();
size_t mMaxFiles;
wxWindowID mIDBase;
std::vector<wxMenu*> mMenus;
std::vector< wxWeakRef< wxMenu > > mMenus;
wxArrayString mHistory;
};