diff --git a/src/Menus.h b/src/Menus.h index f8dcca3f7..dd7c38d58 100644 --- a/src/Menus.h +++ b/src/Menus.h @@ -104,15 +104,6 @@ public: // Exported helper functions from various menu handling source files -/// Namespace for functions for File menu -namespace FileActions { -// Import into existing project -bool DoImportMIDI( AudacityProject &project, const FilePath &fileName ); -// Import file, creating a project if the first argument is null -AudacityProject *DoImportMIDI( - AudacityProject *pProject, const FilePath &fileName ); -} - /// Namespace for functions for Edit menu namespace EditActions { bool DoEditMetadata( diff --git a/src/ProjectFileManager.cpp b/src/ProjectFileManager.cpp index d570ff2c9..1f0649ce3 100644 --- a/src/ProjectFileManager.cpp +++ b/src/ProjectFileManager.cpp @@ -24,7 +24,6 @@ Paul Licameli split from AudacityProject.cpp #include "DirManager.h" #include "FileNames.h" #include "Legacy.h" -#include "Menus.h" #include "PlatformCompatibility.h" #include "Project.h" #include "ProjectFileIO.h" @@ -47,6 +46,7 @@ Paul Licameli split from AudacityProject.cpp #include "blockfile/ODDecodeBlockFile.h" #include "export/Export.h" #include "import/Import.h" +#include "import/ImportMIDI.h" #include "commands/CommandContext.h" #include "ondemand/ODComputeSummaryTask.h" #include "ondemand/ODDecodeFlacTask.h" @@ -1303,7 +1303,7 @@ void ProjectFileManager::OpenFile(const FilePath &fileNameArg, bool addtohistory { #ifdef USE_MIDI if (FileNames::IsMidi(fileName)) - FileActions::DoImportMIDI( project, fileName ); + DoImportMIDI( project, fileName ); else #endif Import( fileName ); diff --git a/src/ProjectManager.cpp b/src/ProjectManager.cpp index 15eae84da..ea7f74507 100644 --- a/src/ProjectManager.cpp +++ b/src/ProjectManager.cpp @@ -37,6 +37,7 @@ Paul Licameli split from AudacityProject.cpp #include "UndoManager.h" #include "WaveTrack.h" #include "wxFileNameWrapper.h" +#include "import/ImportMIDI.h" #include "ondemand/ODManager.h" #include "prefs/QualityPrefs.h" #include "toolbars/ControlToolBar.h" @@ -326,7 +327,7 @@ public: for (const auto &name : sortednames) { #ifdef USE_MIDI if (FileNames::IsMidi(name)) - FileActions::DoImportMIDI( *mProject, name); + DoImportMIDI( *mProject, name ); else #endif ProjectFileManager::Get( *mProject ).Import(name); diff --git a/src/import/ImportLOF.cpp b/src/import/ImportLOF.cpp index 9b7d4ef33..233e51518 100644 --- a/src/import/ImportLOF.cpp +++ b/src/import/ImportLOF.cpp @@ -84,7 +84,6 @@ #include "../WaveTrack.h" #include "ImportPlugin.h" #include "Import.h" -#include "../Menus.h" #include "../NoteTrack.h" #include "../Project.h" #include "../ProjectHistory.h" @@ -277,6 +276,25 @@ static Importer::RegisteredImportPlugin registered{ std::make_unique< LOFImportPlugin >() }; +// return null on failure; if success, return the given project, or a NEW +// one, if the given was null; create no NEW project if failure +static AudacityProject *DoImportMIDIProject( + AudacityProject *pProject, const FilePath &fileName) +{ + AudacityProject *pNewProject {}; + if ( !pProject ) + pProject = pNewProject = ProjectManager::New(); + auto cleanup = finally( [&] + { if ( pNewProject ) GetProjectFrame( *pNewProject ).Close(true); } ); + + if ( DoImportMIDI( *pProject, fileName ) ) { + pNewProject = nullptr; + return pProject; + } + else + return nullptr; +} + /** @brief Processes a single line from a LOF text file, doing whatever is * indicated on the line. * @@ -382,7 +400,7 @@ void LOFImportFileHandle::lofOpenFiles(wxString* ln) // If file is a midi if (FileNames::IsMidi(targetfile)) { - mProject = FileActions::DoImportMIDI(mProject, targetfile); + mProject = DoImportMIDIProject(mProject, targetfile); } // If not a midi, open audio file diff --git a/src/import/ImportMIDI.cpp b/src/import/ImportMIDI.cpp index 86c1f63cb..0b8f43ff6 100644 --- a/src/import/ImportMIDI.cpp +++ b/src/import/ImportMIDI.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #if defined(USE_MIDI) @@ -22,7 +23,39 @@ //#include "mfmidi.h" #include "../NoteTrack.h" +#include "../Project.h" +#include "../ProjectHistory.h" +#include "../ProjectWindow.h" +#include "../SelectUtilities.h" #include "../widgets/AudacityMessageBox.h" +#include "../widgets/FileHistory.h" + +// Given an existing project, try to import into it, return true on success +bool DoImportMIDI( AudacityProject &project, const FilePath &fileName ) +{ + auto &tracks = TrackList::Get( project ); + auto newTrack = TrackFactory::Get( project ).NewNoteTrack(); + + if (::ImportMIDI(fileName, newTrack.get())) { + + SelectUtilities::SelectNone( project ); + auto pTrack = tracks.Add( newTrack ); + pTrack->SetSelected(true); + + ProjectHistory::Get( project ) + .PushState( + wxString::Format(_("Imported MIDI from '%s'"), + fileName), + _("Import MIDI") + ); + + ProjectWindow::Get( project ).ZoomAfterImport(pTrack); + FileHistory::Global().AddFileToHistory(fileName); + return true; + } + else + return false; +} bool ImportMIDI(const FilePath &fName, NoteTrack * dest) { diff --git a/src/import/ImportMIDI.h b/src/import/ImportMIDI.h index 60819cf95..3acb86c94 100644 --- a/src/import/ImportMIDI.h +++ b/src/import/ImportMIDI.h @@ -24,8 +24,11 @@ into a NoteTrack. #if defined(USE_MIDI) class wxString; +class AudacityProject; class NoteTrack; +bool DoImportMIDI( AudacityProject &project, const FilePath &fileName ); + bool ImportMIDI(const FilePath &fName, NoteTrack * dest); class MIDIParser { diff --git a/src/menus/FileMenus.cpp b/src/menus/FileMenus.cpp index 789de0820..e37cecd6e 100644 --- a/src/menus/FileMenus.cpp +++ b/src/menus/FileMenus.cpp @@ -21,6 +21,7 @@ #include "../commands/CommandContext.h" #include "../commands/CommandManager.h" #include "../export/ExportMultiple.h" +#include "../import/ImportMIDI.h" #include "../widgets/AudacityMessageBox.h" #include "../widgets/FileHistory.h" @@ -107,62 +108,10 @@ void DoExport } } -namespace FileActions { - -// exported helper functions - -#ifdef USE_MIDI -// Given an existing project, try to import into it, return true on success -bool DoImportMIDI( AudacityProject &project, const FilePath &fileName ) -{ - auto &tracks = TrackList::Get( project ); - auto newTrack = TrackFactory::Get( project ).NewNoteTrack(); - - if (::ImportMIDI(fileName, newTrack.get())) { - - SelectUtilities::SelectNone( project ); - auto pTrack = tracks.Add( newTrack ); - pTrack->SetSelected(true); - - ProjectHistory::Get( project ) - .PushState( - wxString::Format(_("Imported MIDI from '%s'"), - fileName), - _("Import MIDI") - ); - - ProjectWindow::Get( project ).ZoomAfterImport(pTrack); - FileHistory::Global().AddFileToHistory(fileName); - return true; - } - else - return false; -} - -// return null on failure; if success, return the given project, or a NEW -// one, if the given was null; create no NEW project if failure -AudacityProject *DoImportMIDI( - AudacityProject *pProject, const FilePath &fileName) -{ - AudacityProject *pNewProject {}; - if ( !pProject ) - pProject = pNewProject = ProjectManager::New(); - auto cleanup = finally( [&] - { if ( pNewProject ) GetProjectFrame( *pNewProject ).Close(true); } ); - - if ( DoImportMIDI( *pProject, fileName ) ) { - pNewProject = nullptr; - return pProject; - } - else - return nullptr; -} -#endif - -#ifdef USE_MIDI - // Menu handler functions +namespace FileActions { + struct Handler : CommandHandlerObject { void OnNew(const CommandContext & ) @@ -507,6 +456,7 @@ void OnImportLabels(const CommandContext &context) } } +#ifdef USE_MIDI void OnImportMIDI(const CommandContext &context) { auto &project = context.project;