1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-16 08:09:32 +02:00

Bug 2296 - There is no Import or Export for Macros

This commit is contained in:
Leland Lucius 2020-08-08 21:17:14 -05:00
parent c13a074cb2
commit 52b15ce03f
4 changed files with 113 additions and 20 deletions

View File

@ -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)

View File

@ -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);

View File

@ -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))

View File

@ -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;