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:
parent
c22c7b6fce
commit
9ad88e091c
@ -170,10 +170,6 @@ MenuCreator::MenuCreator(){
|
|||||||
|
|
||||||
MenuCreator::~MenuCreator()
|
MenuCreator::~MenuCreator()
|
||||||
{
|
{
|
||||||
if (wxGetApp().GetRecentFiles())
|
|
||||||
{
|
|
||||||
wxGetApp().GetRecentFiles()->RemoveMenu(mRecentFilesMenu);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
@ -1833,15 +1829,6 @@ void MenuCreator::CreateMenusAndCommands(AudacityProject &project)
|
|||||||
|
|
||||||
|
|
||||||
project.SetMenuBar(menubar.release());
|
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
|
// Recent Files and Recent Projects menus
|
||||||
|
|
||||||
|
wxWeakRef<wxMenu> recentFilesMenu = c->BeginSubMenu(
|
||||||
|
|
||||||
#ifdef __WXMAC__
|
#ifdef __WXMAC__
|
||||||
/* i18n-hint: This is the name of the menu item on Mac OS X only */
|
/* i18n-hint: This is the name of the menu item on Mac OS X only */
|
||||||
mRecentFilesMenu = c->BeginSubMenu(_("Open Recent"));
|
_("Open Recent")
|
||||||
#else
|
#else
|
||||||
/* i18n-hint: This is the name of the menu item on Windows and Linux */
|
/* i18n-hint: This is the name of the menu item on Windows and Linux */
|
||||||
mRecentFilesMenu = c->BeginSubMenu(_("Recent &Files"));
|
_("Recent &Files")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
wxGetApp().GetRecentFiles()->UseMenu(mRecentFilesMenu);
|
);
|
||||||
wxGetApp().GetRecentFiles()->AddFilesToMenu(mRecentFilesMenu);
|
|
||||||
|
wxGetApp().GetRecentFiles()->UseMenu(recentFilesMenu);
|
||||||
|
wxGetApp().GetRecentFiles()->AddFilesToMenu(recentFilesMenu);
|
||||||
|
|
||||||
c->EndSubMenu();
|
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?
|
// TODO: This surely belongs in CommandManager?
|
||||||
@ -2250,9 +2253,6 @@ void MenuCreator::RebuildMenuBar(AudacityProject &project)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Allow FileHistory to remove its own menu
|
|
||||||
wxGetApp().GetRecentFiles()->RemoveMenu(mRecentFilesMenu);
|
|
||||||
|
|
||||||
// Delete the menus, since we will soon recreate them.
|
// Delete the menus, since we will soon recreate them.
|
||||||
// Rather oddly, the menus don't vanish as a result of doing this.
|
// Rather oddly, the menus don't vanish as a result of doing this.
|
||||||
{
|
{
|
||||||
|
@ -617,9 +617,6 @@ public:
|
|||||||
void CreateRecentFilesMenu(CommandManager *c);
|
void CreateRecentFilesMenu(CommandManager *c);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Recent files
|
|
||||||
wxMenu *mRecentFilesMenu;
|
|
||||||
|
|
||||||
CommandFlag mLastFlags;
|
CommandFlag mLastFlags;
|
||||||
|
|
||||||
// Last effect applied to this project
|
// Last effect applied to this project
|
||||||
|
@ -101,6 +101,8 @@ size_t FileHistory::GetCount()
|
|||||||
|
|
||||||
void FileHistory::UseMenu(wxMenu *menu)
|
void FileHistory::UseMenu(wxMenu *menu)
|
||||||
{
|
{
|
||||||
|
Compress();
|
||||||
|
|
||||||
auto end = mMenus.end();
|
auto end = mMenus.end();
|
||||||
auto iter = std::find(mMenus.begin(), end, menu);
|
auto iter = std::find(mMenus.begin(), end, menu);
|
||||||
auto found = (iter != end);
|
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)
|
void FileHistory::Load(wxConfigBase & config, const wxString & group)
|
||||||
{
|
{
|
||||||
mHistory.Clear();
|
mHistory.Clear();
|
||||||
@ -161,8 +150,10 @@ void FileHistory::Save(wxConfigBase & config, const wxString & group)
|
|||||||
|
|
||||||
void FileHistory::AddFilesToMenu()
|
void FileHistory::AddFilesToMenu()
|
||||||
{
|
{
|
||||||
|
Compress();
|
||||||
for (auto pMenu : mMenus)
|
for (auto pMenu : mMenus)
|
||||||
AddFilesToMenu(pMenu);
|
if (pMenu)
|
||||||
|
AddFilesToMenu(pMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileHistory::AddFilesToMenu(wxMenu *menu)
|
void FileHistory::AddFilesToMenu(wxMenu *menu)
|
||||||
@ -183,3 +174,15 @@ void FileHistory::AddFilesToMenu(wxMenu *menu)
|
|||||||
menu->Append(mIDBase, _("&Clear"));
|
menu->Append(mIDBase, _("&Clear"));
|
||||||
menu->Enable(mIDBase, mHistory.GetCount() > 0);
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
#include <wx/grid.h>
|
#include <wx/grid.h>
|
||||||
#include <wx/string.h>
|
#include <wx/string.h>
|
||||||
#include <wx/window.h>
|
#include <wx/window.h>
|
||||||
|
#include <wx/weakref.h>
|
||||||
|
|
||||||
class AUDACITY_DLL_API FileHistory
|
class AUDACITY_DLL_API FileHistory
|
||||||
{
|
{
|
||||||
@ -30,7 +31,6 @@ class AUDACITY_DLL_API FileHistory
|
|||||||
void RemoveFileFromHistory(size_t i, bool update = true);
|
void RemoveFileFromHistory(size_t i, bool update = true);
|
||||||
void Clear();
|
void Clear();
|
||||||
void UseMenu(wxMenu *menu);
|
void UseMenu(wxMenu *menu);
|
||||||
void RemoveMenu(wxMenu *menu);
|
|
||||||
void Load(wxConfigBase& config, const wxString & group);
|
void Load(wxConfigBase& config, const wxString & group);
|
||||||
void Save(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;
|
const wxString &GetHistoryFile(size_t i) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void Compress();
|
||||||
|
|
||||||
size_t mMaxFiles;
|
size_t mMaxFiles;
|
||||||
wxWindowID mIDBase;
|
wxWindowID mIDBase;
|
||||||
|
|
||||||
std::vector<wxMenu*> mMenus;
|
std::vector< wxWeakRef< wxMenu > > mMenus;
|
||||||
wxArrayString mHistory;
|
wxArrayString mHistory;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user