From 70b1f69bbe77fa8d00b1384fbbd28b1ff8cdf819 Mon Sep 17 00:00:00 2001 From: James Crook Date: Sun, 4 Mar 2018 19:02:22 +0000 Subject: [PATCH] Use names not numbers in MacroIdOfName() Previously the ID for a macro was, e.g. Macro003. However this would not work if macros were added or deleted, since chains containing the old macro references would now refer to a different macro. So changed to using names. --- src/BatchProcessDialog.cpp | 21 +++++++++++++++++++++ src/BatchProcessDialog.h | 2 ++ src/Menus.cpp | 10 +++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/BatchProcessDialog.cpp b/src/BatchProcessDialog.cpp index a958b8163..2b66d5371 100644 --- a/src/BatchProcessDialog.cpp +++ b/src/BatchProcessDialog.cpp @@ -192,6 +192,27 @@ void ApplyMacroDialog::OnApplyToProject(wxCommandEvent & WXUNUSED(event)) ApplyMacroToProject( item ); } +wxString ApplyMacroDialog::MacroIdOfName( const wxString & MacroName ) +{ + wxString Temp = MacroName; + Temp.Replace(" ",""); + Temp = wxString( "Macro_" ) + Temp; + return Temp; +} + + +// Does nothing if not found, rather than returning an error. +void ApplyMacroDialog::ApplyMacroToProject( const wxString & MacroID, bool bHasGui ) +{ + for( size_t i=0;iGetItemCount();i++){ + wxString name = mMacros->GetItemText(i); + if( MacroIdOfName( name ) == MacroID ){ + ApplyMacroToProject( i, bHasGui ); + return; + } + } +} + void ApplyMacroDialog::ApplyMacroToProject( int iMacro, bool bHasGui ) { wxString name = mMacros->GetItemText(iMacro); diff --git a/src/BatchProcessDialog.h b/src/BatchProcessDialog.h index 32568936d..29e4af050 100644 --- a/src/BatchProcessDialog.h +++ b/src/BatchProcessDialog.h @@ -56,7 +56,9 @@ class ApplyMacroDialog : public wxDialogWrapper { virtual wxString GetHelpPageName() {return "Tools_Menu#macros_compact_dialog";}; void PopulateMacros(); + static wxString MacroIdOfName( const wxString & MacroName ); void ApplyMacroToProject( int iMacro, bool bHasGui=true ); + void ApplyMacroToProject( const wxString & MacroID, bool bHasGui=true ); // These will be reused in the derived class... diff --git a/src/Menus.cpp b/src/Menus.cpp index a41be23f2..b592cc3a1 100644 --- a/src/Menus.cpp +++ b/src/Menus.cpp @@ -1713,7 +1713,8 @@ void AudacityProject::PopulateMacrosMenu( CommandManager* c, CommandFlag flags int i; for (i = 0; i < (int)names.GetCount(); i++) { - c->AddItem(wxString::Format("Macro%03i", i ), names[i], FN(OnApplyMacroDirectly), + wxString MacroID = ApplyMacroDialog::MacroIdOfName( names[i] ); + c->AddItem(MacroID, names[i], FN(OnApplyMacroDirectly), flags, flags); } @@ -6860,10 +6861,17 @@ void AudacityProject::OnApplyMacroDirectly(const CommandContext &context ) //wxLogDebug( "Macro was: %s", context.parameter); ApplyMacroDialog dlg(this); wxString Name = context.parameter; + +// We used numbers previously, but macros could get renumbered, making +// macros containing macros unpredictable. +#ifdef MACROS_BY_NUMBERS long item=0; // Take last three letters (of e.g. Macro007) and convert to a number. Name.Mid( Name.Length() - 3 ).ToLong( &item, 10 ); dlg.ApplyMacroToProject( item, false ); +#else + dlg.ApplyMacroToProject( Name, false ); +#endif ModifyUndoMenuItems(); }