diff --git a/src/BatchCommands.cpp b/src/BatchCommands.cpp index e1be1d88d..a9b5ae004 100644 --- a/src/BatchCommands.cpp +++ b/src/BatchCommands.cpp @@ -87,19 +87,13 @@ static const std::pair SpecialCommands[] = { }; // end CLEANSPEECH remnant -static const wxString MP3Conversion = wxT("MP3 Conversion"); -static const wxString FadeEnds = wxT("Fade Ends"); - MacroCommands::MacroCommands() { mMessage = ""; ResetMacro(); wxArrayString names = GetNames(); - - wxArrayString defaults; - defaults.Add( MP3Conversion ); - defaults.Add( FadeEnds ); + wxArrayString defaults = GetNamesOfDefaultMacros(); for( size_t i = 0;i 1 ) + return true; + + // Restore the reentry counter (to zero) when we exit. + auto cleanup1 = valueRestorer( MacroReentryCount); + MacroReentryCount++; + mFileName = filename; AudacityProject *proj = GetActiveProject(); bool res = false; - auto cleanup = finally( [&] { + auto cleanup2 = finally( [&] { if (!res) { if(proj) { // Macro failed or was cancelled; revert to the previous state @@ -795,7 +812,8 @@ bool MacroCommands::ApplyMacro(const wxString & filename) if (!proj) return false; - proj->PushState(longDesc, shortDesc); + if( MacroReentryCount <= 1 ) + proj->PushState(longDesc, shortDesc); return true; } @@ -877,13 +895,12 @@ wxArrayString MacroCommands::GetNames() bool MacroCommands::IsFixed(const wxString & name) { - if (name == MP3Conversion) + wxArrayString defaults = GetNamesOfDefaultMacros(); + if( defaults.Index( name ) != wxNOT_FOUND ) return true; return false; } - - void MacroCommands::Split(const wxString & str, wxString & command, wxString & param) { int splitAt; diff --git a/src/BatchCommands.h b/src/BatchCommands.h index 4e3245996..3e0ebcec7 100644 --- a/src/BatchCommands.h +++ b/src/BatchCommands.h @@ -42,6 +42,7 @@ class MacroCommands final { // These commands do not depend on the command list. static wxArrayString GetNames(); + static wxArrayString GetNamesOfDefaultMacros(); // A triple of user-visible name, internal string identifier and type/help string. using CommandName = std::tuple; diff --git a/src/BatchProcessDialog.cpp b/src/BatchProcessDialog.cpp index af509b9fe..15482d63f 100644 --- a/src/BatchProcessDialog.cpp +++ b/src/BatchProcessDialog.cpp @@ -123,6 +123,14 @@ void ApplyMacroDialog::PopulateOrExchange(ShuttleGui &S) S.Id(ExpandID).AddButton(_("&Expand")); } S.EndHorizontalLay(); + S.StartHorizontalLay(wxALIGN_RIGHT, false); + { + S.AddSpace( 40 ); + S.AddPrompt( _("Apply Macro to:") ); + S.Id(ApplyToProjectID).AddButton(_("&Project")); + S.Id(ApplyToFilesID).AddButton(_("&Files...")); + } + S.EndHorizontalLay(); S.StartStatic(_("&Select Macro"), true); { @@ -132,14 +140,8 @@ void ApplyMacroDialog::PopulateOrExchange(ShuttleGui &S) mMacros->InsertColumn(0, _("Macro"), wxLIST_FORMAT_LEFT); } S.EndStatic(); - S.StartHorizontalLay(wxALIGN_RIGHT, false); { - S.SetBorder(10); - S.AddPrompt( _("Apply Macro to:") ); - S.Id(ApplyToProjectID).AddButton(_("&Project")); - S.Id(ApplyToFilesID).AddButton(_("&Files...")); - S.AddSpace( 40 ); S.AddStandardButtons( eCancelButton | eHelpButton); } S.EndHorizontalLay(); @@ -190,6 +192,28 @@ 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; +} + +// Apply macro, given its ID. +// 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; + } + } +} + +// Apply macro, given its number in the list. void ApplyMacroDialog::ApplyMacroToProject( int iMacro, bool bHasGui ) { wxString name = mMacros->GetItemText(iMacro); @@ -501,9 +525,9 @@ MacrosWindow::MacrosWindow(wxWindow * parent, bool bExpanded): ApplyMacroDialog(parent, true) { mbExpanded = bExpanded; - SetLabel(_("Macros")); // Provide visual label - SetName(_("Macros")); // Provide audible label - SetTitle(_("Macros")); + SetLabel(_("Edit Macros")); // Provide visual label + SetName(_("Edit Macros")); // Provide audible label + SetTitle(_("Edit Macros")); mChanged = false; mSelectedCommand = 0; @@ -563,10 +587,18 @@ void MacrosWindow::PopulateOrExchange(ShuttleGui & S) { S.Prop(0).StartHorizontalLay(wxALIGN_RIGHT, false); { - S.Id(ShrinkID).AddButton(_("&Shrink")); + S.Id(ShrinkID).AddButton(_("Shrin&k")); } S.EndHorizontalLay(); - S.StartStatic(_("&Macros"),1); + S.StartHorizontalLay(wxALIGN_RIGHT, false); + { + S.AddSpace( 40 ); + S.AddPrompt( _("Apply Macro to:") ); + S.Id(ApplyToProjectID).AddButton(_("&Project")); + S.Id(ApplyToFilesID).AddButton(_("&Files...")); + } + S.EndHorizontalLay(); + S.StartStatic(_("&Select Macros"),1); { S.SetStyle(wxSUNKEN_BORDER | wxLC_REPORT | wxLC_HRULES | wxLC_SINGLE_SEL | wxLC_EDIT_LABELS); @@ -587,7 +619,7 @@ void MacrosWindow::PopulateOrExchange(ShuttleGui & S) S.StartVerticalLay( 1 ); { - S.StartStatic(_("Ma&cro (Double-Click or press SPACE to edit)"), true); + S.StartStatic(_("&Macro (Double-Click or press SPACE to edit)"), true); { S.StartHorizontalLay(wxEXPAND,1); { @@ -620,10 +652,6 @@ void MacrosWindow::PopulateOrExchange(ShuttleGui & S) S.EndStatic(); S.StartHorizontalLay(wxALIGN_RIGHT, false); { - S.AddPrompt( _("Apply Macro to:") ); - S.Id(ApplyToProjectButtonID).AddButton(_("&Project"), wxALIGN_LEFT); - S.Id(ApplyToFilesButtonID).AddButton(_("&Files..."), wxALIGN_LEFT); - S.AddSpace( 40 ); S.AddStandardButtons( eOkButton | eCancelButton | eHelpButton); } S.EndHorizontalLay(); @@ -684,6 +712,10 @@ void MacrosWindow::UpdateDisplay( bool bExpanded ) { if( bExpanded == mbExpanded ) return; + + if( !SaveChanges() ) + return; + mbExpanded = bExpanded; DestroyChildren(); SetSizer( nullptr ); @@ -692,10 +724,22 @@ void MacrosWindow::UpdateDisplay( bool bExpanded ) mSelectedCommand = 0; SetMinSize( wxSize( 200,200 )); + // Get and set position for optical stability. + // Expanded and shrunk dialogs 'stay where they were'. + // That's OK , and what we want, even if we exapnd off-screen. + // We won't shrink to being off-screen, since the shrink button + // was clicked, so must have been on screen. + wxPoint p = GetPosition( ); if( mbExpanded ) Populate(); else ApplyMacroDialog::Populate(); + SetPosition( p ); + + wxString Title = mbExpanded ? _("Edit Macros") : _("Apply Macro"); + SetLabel( Title ); // Provide visual label + SetName( Title ); // Provide audible label + SetTitle( Title ); } void MacrosWindow::OnExpand(wxCommandEvent &WXUNUSED(event)) 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(); }