From 52b15ce03fcef4b870a3fa834a618565ea3dba95 Mon Sep 17 00:00:00 2001 From: Leland Lucius Date: Sat, 8 Aug 2020 21:17:14 -0500 Subject: [PATCH] Bug 2296 - There is no Import or Export for Macros --- src/BatchCommands.cpp | 71 +++++++++++++++++++++++++++++++++----- src/BatchCommands.h | 4 +-- src/BatchProcessDialog.cpp | 54 +++++++++++++++++++++++------ src/BatchProcessDialog.h | 4 +++ 4 files changed, 113 insertions(+), 20 deletions(-) diff --git a/src/BatchCommands.cpp b/src/BatchCommands.cpp index 25411c44a..a6d0bf645 100644 --- a/src/BatchCommands.cpp +++ b/src/BatchCommands.cpp @@ -164,7 +164,7 @@ int MacroCommands::GetCount() return (int)mCommandMacro.size(); } -bool MacroCommands::ReadMacro(const wxString & macro) +wxString MacroCommands::ReadMacro(const wxString & macro, wxWindow *parent) { // Clear any previous macro ResetMacro(); @@ -172,6 +172,38 @@ bool MacroCommands::ReadMacro(const wxString & macro) // Build the filename wxFileName name(FileNames::MacroDir(), macro, wxT("txt")); + // But, ask the user for the real name if we're importing + if (parent) { + FilePath fn = FileNames::SelectFile(FileNames::Operation::_None, + XO("Import Macro"), + wxEmptyString, + name.GetName(), + wxT("txt"), + { FileNames::TextFiles }, + wxFD_OPEN | wxRESIZE_BORDER, + parent); + + // User canceled... + if (fn.empty()) { + return wxEmptyString; + } + + wxFileName check(fn); + check.SetPath(name.GetPath()); + if (check.FileExists()) + { + int id = AudacityMessageBox( + XO("Macro %s already exists. Would you like to replace it?").Format(check.GetName()), + XO("Import Macro"), + wxYES_NO); + if (id == wxNO) { + return wxEmptyString; + } + } + + name.Assign(fn); + } + // Set the file name wxTextFile tf(name.GetFullPath()); @@ -179,7 +211,7 @@ bool MacroCommands::ReadMacro(const wxString & macro) tf.Open(); if (!tf.IsOpened()) { // wxTextFile will display any errors - return false; + return wxEmptyString; } // Load commands from the file @@ -206,15 +238,38 @@ bool MacroCommands::ReadMacro(const wxString & macro) // Done with the file tf.Close(); - return true; + // Write to macro directory if importing + if (parent) { + return WriteMacro(name.GetName()); + } + + return name.GetName(); } - -bool MacroCommands::WriteMacro(const wxString & macro) +wxString MacroCommands::WriteMacro(const wxString & macro, wxWindow *parent) { - // Build the filename + // Build the default filename wxFileName name(FileNames::MacroDir(), macro, wxT("txt")); + // But, ask the user for the real name if we're exporting + if (parent) { + FilePath fn = FileNames::SelectFile(FileNames::Operation::_None, + XO("Export Macro"), + wxEmptyString, + name.GetName(), + wxT("txt"), + { FileNames::TextFiles }, + wxFD_SAVE | wxFD_OVERWRITE_PROMPT | wxRESIZE_BORDER, + parent); + + // User canceled... + if (fn.empty()) { + return wxEmptyString; + } + + name.Assign(fn); + } + // Set the file name wxTextFile tf(name.GetFullPath()); @@ -228,7 +283,7 @@ bool MacroCommands::WriteMacro(const wxString & macro) if (!tf.IsOpened()) { // wxTextFile will display any errors - return false; + return wxEmptyString; } // Start with a clean slate @@ -247,7 +302,7 @@ bool MacroCommands::WriteMacro(const wxString & macro) // Done with the file tf.Close(); - return true; + return name.GetName(); } bool MacroCommands::AddMacro(const wxString & macro) diff --git a/src/BatchCommands.h b/src/BatchCommands.h index 80fa12674..21a7c4eee 100644 --- a/src/BatchCommands.h +++ b/src/BatchCommands.h @@ -103,8 +103,8 @@ class MacroCommands final { void ResetMacro(); void RestoreMacro(const wxString & name); - bool ReadMacro(const wxString & macro); - bool WriteMacro(const wxString & macro); + wxString ReadMacro(const wxString & macro, wxWindow *parent = nullptr); + wxString WriteMacro(const wxString & macro, wxWindow *parent = nullptr); bool AddMacro(const wxString & macro); bool DeleteMacro(const wxString & name); bool RenameMacro(const wxString & oldmacro, const wxString & newmacro); diff --git a/src/BatchProcessDialog.cpp b/src/BatchProcessDialog.cpp index befe54c38..5ba22a00e 100644 --- a/src/BatchProcessDialog.cpp +++ b/src/BatchProcessDialog.cpp @@ -500,6 +500,8 @@ BEGIN_EVENT_TABLE(MacrosWindow, ApplyMacroDialog) EVT_BUTTON(RemoveButtonID, MacrosWindow::OnRemove) EVT_BUTTON(RenameButtonID, MacrosWindow::OnRename) EVT_BUTTON(RestoreButtonID, MacrosWindow::OnRestore) + EVT_BUTTON(ImportButtonID, MacrosWindow::OnImport) + EVT_BUTTON(ExportButtonID, MacrosWindow::OnExport) EVT_BUTTON(ExpandID, MacrosWindow::OnExpand) EVT_BUTTON(ShrinkID, MacrosWindow::OnShrink) @@ -603,15 +605,8 @@ void MacrosWindow::PopulateOrExchange(ShuttleGui & S) mRemove = S.Id(RemoveButtonID).AddButton(XXO("Remo&ve")); mRename = S.Id(RenameButtonID).AddButton(XXO("&Rename...")); mRestore = S.Id(RestoreButtonID).AddButton(XXO("Re&store")); -// Not yet ready for prime time. -#if 0 - S.Id(ImportButtonID) - .Disable() - .AddButton(XO("I&mport...")); - S.Id(ExportButtonID) - .Disable() - .AddButton(XO("E&xport...")); -#endif + mImport = S.Id(ImportButtonID).AddButton(XO("I&mport...")); + mExport = S.Id(ExportButtonID).AddButton(XO("E&xport...")); } S.EndVerticalLay(); } @@ -623,7 +618,6 @@ void MacrosWindow::PopulateOrExchange(ShuttleGui & S) { S.StartHorizontalLay(wxEXPAND,1); { - mList = S.Id(CommandsListID) .Style(wxSUNKEN_BORDER | wxLC_REPORT | wxLC_HRULES | wxLC_VRULES | wxLC_SINGLE_SEL) @@ -1052,6 +1046,46 @@ void MacrosWindow::OnRestore(wxCommandEvent & WXUNUSED(event)) PopulateList(); } +/// +void MacrosWindow::OnImport(wxCommandEvent & WXUNUSED(event)) +{ + if (!ChangeOK()) { + return; + } + + long item = mMacros->GetNextItem(-1, + wxLIST_NEXT_ALL, + wxLIST_STATE_SELECTED); + if (item == -1) { + return; + } + + wxString name = mMacros->GetItemText(item); + + name = mMacroCommands.ReadMacro({}, this); + if (name == wxEmptyString) { + return; + } + + mActiveMacro = name; + + PopulateMacros(); + UpdateMenus(); +} + +/// +void MacrosWindow::OnExport(wxCommandEvent & WXUNUSED(event)) +{ + long item = mMacros->GetNextItem(-1, + wxLIST_NEXT_ALL, + wxLIST_STATE_SELECTED); + if (item == -1) { + return; + } + + mMacroCommands.WriteMacro(mMacros->GetItemText(item), this); +} + /// An item in the list has been selected. /// Bring up a dialog to allow its parameters to be edited. void MacrosWindow::OnCommandActivated(wxListEvent & WXUNUSED(event)) diff --git a/src/BatchProcessDialog.h b/src/BatchProcessDialog.h index f1a4fb81f..826ca2df5 100644 --- a/src/BatchProcessDialog.h +++ b/src/BatchProcessDialog.h @@ -103,6 +103,8 @@ private: void OnRemove(wxCommandEvent &event); void OnRename(wxCommandEvent &event); void OnRestore(wxCommandEvent &event); + void OnImport(wxCommandEvent &event); + void OnExport(wxCommandEvent &event); void OnExpand(wxCommandEvent &event); void OnShrink(wxCommandEvent &event); void OnSize(wxSizeEvent &event); @@ -127,6 +129,8 @@ private: wxButton *mRemove; wxButton *mRename; wxButton *mRestore; + wxButton *mImport; + wxButton *mExport; int mSelectedCommand; bool mChanged;