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

Where FileSelector was used, default to Documents not cwd; save prefs

This commit is contained in:
Paul Licameli 2017-08-02 12:41:29 -04:00
parent a9d4c2c05b
commit 5d48e96942
20 changed files with 168 additions and 101 deletions

View File

@ -16,9 +16,10 @@ Provides thread-safe logging based on the wxWidgets log facility.
#include "Audacity.h" // This should always be included first
#include "AudacityLogger.h"
#include "FileDialog.h"
#include "FileNames.h"
#include "ShuttleGui.h"
#include <wx/filedlg.h>
#include <wx/log.h>
#include <wx/frame.h>
#include <wx/icon.h>
@ -292,7 +293,8 @@ void AudacityLogger::OnSave(wxCommandEvent & WXUNUSED(e))
{
wxString fName = _("log.txt");
fName = FileSelector(_("Save log to:"),
fName = FileNames::SelectFile(FileNames::Operation::Export,
_("Save log to:"),
wxEmptyString,
fName,
wxT("txt"),

View File

@ -41,7 +41,7 @@ of the BlockFile system.
#include "Sequence.h"
#include "Prefs.h"
#include "FileDialog.h"
#include "FileNames.h"
class BenchmarkDialog final : public wxDialogWrapper
{
@ -248,7 +248,8 @@ void BenchmarkDialog::OnSave( wxCommandEvent & WXUNUSED(event))
{
wxString fName = wxT("benchmark.txt");
fName = FileSelector(wxT("Export Benchmark Data As:"),
fName = FileNames::SelectFile(FileNames::Operation::Export,
wxT("Export Benchmark Data As:"),
wxEmptyString,
fName,
wxT("txt"),

View File

@ -24,6 +24,7 @@ License: GPL v2. See License.txt.
#include "widgets/HelpSystem.h"
#include <wx/file.h>
#include <wx/filedlg.h>
#include "Experimental.h"
@ -518,7 +519,8 @@ public:
"Where would I find the file '%s'?" instead if you want. */
question.Printf(_("Where is '%s'?"), mName.c_str());
wxString path = FileSelector(question,
wxString path = FileNames::SelectFile(FileNames::Operation::None,
question,
mLibPath.GetPath(),
mLibPath.GetName(),
wxT(""),

View File

@ -140,7 +140,6 @@ extern "C" {
#include <wx/msgdlg.h> // for wxMessageBox
#include <wx/utils.h>
#include "widgets/LinkingHtmlWindow.h"
#include "FileDialog.h"
#include "ShuttleGui.h"
#include "Prefs.h"
#include <wx/checkbox.h>

View File

@ -31,6 +31,7 @@ used throughout Audacity into this one place.
#include "Internat.h"
#include "PlatformCompatibility.h"
#include "wxFileNameWrapper.h"
#include "../lib-src/FileDialog/FileDialog.h"
#if defined(__WXMAC__) || defined(__WXGTK__)
#include <dlfcn.h>
@ -336,3 +337,57 @@ wxFileNameWrapper FileNames::DefaultToDocumentsFolder
return result;
}
namespace {
wxString PreferenceKey(FileNames::Operation op)
{
wxString key;
switch (op) {
case FileNames::Operation::Open:
key = wxT("/DefaultOpenPath"); break;
case FileNames::Operation::Export:
key = wxT("/DefaultExportPath"); break;
case FileNames::Operation::None:
default:
break;
}
return key;
}
}
wxString FileNames::FindDefaultPath(Operation op)
{
auto key = PreferenceKey(op);
if (key.empty())
return wxString{};
else
return DefaultToDocumentsFolder(key).GetPath();
}
void FileNames::UpdateDefaultPath(Operation op, const wxString &path)
{
if (path.empty())
return;
auto key = PreferenceKey(op);
if (!key.empty()) {
gPrefs->Write(key, ::wxPathOnly(path));
gPrefs->Flush();
}
}
wxString
FileNames::SelectFile(Operation op,
const wxString& message,
const wxString& default_path,
const wxString& default_filename,
const wxString& default_extension,
const wxString& wildcard,
int flags,
wxWindow *parent)
{
return WithDefaultPath(op, default_path, [&](const wxString &path) {
return FileSelector(
message, path, default_filename, default_extension,
wildcard, flags, parent, wxDefaultCoord, wxDefaultCoord);
});
}

View File

@ -69,6 +69,40 @@ public:
static wxFileNameWrapper DefaultToDocumentsFolder
(const wxString &preference);
// If not None, determines a preference key (for the default path string) to
// be read and updated
enum class Operation {
None,
Open,
Export
};
static wxString FindDefaultPath(Operation op);
static void UpdateDefaultPath(Operation op, const wxString &path);
// F is a function taking a wxString, returning wxString
template<typename F>
static wxString WithDefaultPath
(Operation op, const wxString &defaultPath, F function)
{
auto path = defaultPath;
if (path.empty())
path = FileNames::FindDefaultPath(op);
auto result = function(path);
FileNames::UpdateDefaultPath(op, result);
return result;
}
static wxString
SelectFile(Operation op, // op matters only when default_path is empty
const wxString& message,
const wxString& default_path,
const wxString& default_filename,
const wxString& default_extension,
const wxString& wildcard,
int flags,
wxWindow *parent);
private:
// Private constructors: No one is ever going to instantiate it.
//

View File

@ -76,7 +76,7 @@ and in the spectrogram spectral selection.
#include "Theme.h"
#include "AllThemeResources.h"
#include "FileDialog.h"
#include "FileNames.h"
#include "WaveTrack.h"
@ -1027,7 +1027,8 @@ void FreqWindow::OnExport(wxCommandEvent & WXUNUSED(event))
{
wxString fName = _("spectrum.txt");
fName = FileSelector(_("Export Spectral Data As:"),
fName = FileNames::SelectFile(FileNames::Operation::Export,
_("Export Spectral Data As:"),
wxEmptyString, fName, wxT("txt"), wxT("*.txt"), wxFD_SAVE | wxRESIZE_BORDER, this);
if (fName == wxT(""))

View File

@ -29,7 +29,6 @@ and on Mac OS X for the filesystem.
#include <math.h> // for pow()
#include "Internat.h"
#include "FileDialog.h"
#include "Experimental.h"
// in order for the static member variables to exist, they must appear here
@ -246,7 +245,8 @@ char *Internat::VerifyFilename(const wxString &s, bool input)
wxMessageBox(_("The specified filename could not be converted due to Unicode character use."));
ext = ff.GetExt();
name = FileSelector(_("Specify New Filename:"),
name = FileNames::SelectFile(FileNames::Operation::None,
_("Specify New Filename:"),
wxEmptyString,
name,
ext,

View File

@ -38,7 +38,7 @@
#include "ViewInfo.h"
#include "widgets/NumericTextCtrl.h"
#include "FileDialog.h"
#include "FileNames.h"
#include <limits>
enum Column
@ -585,12 +585,11 @@ void LabelDialog::OnRemove(wxCommandEvent & WXUNUSED(event))
void LabelDialog::OnImport(wxCommandEvent & WXUNUSED(event))
{
wxString path = gPrefs->Read(wxT("/DefaultOpenPath"),::wxGetCwd());
// Ask user for a filename
wxString fileName =
FileSelector(_("Select a text file containing labels"),
path, // Path
FileNames::SelectFile(FileNames::Operation::Open,
_("Select a text file containing labels"),
wxEmptyString, // Path
wxT(""), // Name
wxT(".txt"), // Extension
_("Text files (*.txt)|*.txt|All files|*"),
@ -599,10 +598,6 @@ void LabelDialog::OnImport(wxCommandEvent & WXUNUSED(event))
// They gave us one...
if (fileName != wxT("")) {
path =::wxPathOnly(fileName);
gPrefs->Write(wxT("/DefaultOpenPath"), path);
gPrefs->Flush();
wxTextFile f;
// Get at the data
@ -640,7 +635,8 @@ void LabelDialog::OnExport(wxCommandEvent & WXUNUSED(event))
// Extract the actual name.
wxString fName = mTrackNames[mTrackNames.GetCount() - 1].AfterFirst(wxT('-')).Mid(1);
fName = FileSelector(_("Export Labels As:"),
fName = FileNames::SelectFile(FileNames::Operation::Export,
_("Export Labels As:"),
wxEmptyString,
fName.c_str(),
wxT("txt"),

View File

@ -117,7 +117,6 @@ simplifies construction of menu items.
#include "SoundActivatedRecord.h"
#include "LabelDialog.h"
#include "FileDialog.h"
#include "SplashDialog.h"
#include "widgets/HelpSystem.h"
#include "DeviceManager.h"
@ -4424,7 +4423,8 @@ void AudacityProject::OnExportLabels()
return;
}
fName = FileSelector(_("Export Labels As:"),
fName = FileNames::SelectFile(FileNames::Operation::Export,
_("Export Labels As:"),
wxEmptyString,
fName,
wxT("txt"),
@ -4505,7 +4505,8 @@ void AudacityProject::OnExportMIDI(){
wxString fName = wxT("");
fName = FileSelector(_("Export MIDI As:"),
fName = FileNames::SelectFile(FileNames::Operation::Export,
_("Export MIDI As:"),
wxEmptyString,
fName,
wxT(".mid|.gro"),
@ -6741,11 +6742,10 @@ void AudacityProject::OnImport()
void AudacityProject::OnImportLabels()
{
wxString path = gPrefs->Read(wxT("/DefaultOpenPath"),::wxGetCwd());
wxString fileName =
FileSelector(_("Select a text file containing labels"),
path, // Path
FileNames::SelectFile(FileNames::Operation::Open,
_("Select a text file containing labels"),
wxEmptyString, // Path
wxT(""), // Name
wxT(".txt"), // Extension
_("Text files (*.txt)|*.txt|All files|*"),
@ -6753,10 +6753,6 @@ void AudacityProject::OnImportLabels()
this); // Parent
if (fileName != wxT("")) {
path =::wxPathOnly(fileName);
gPrefs->Write(wxT("/DefaultOpenPath"), path);
gPrefs->Flush();
wxTextFile f;
f.Open(fileName);
@ -6787,23 +6783,17 @@ void AudacityProject::OnImportLabels()
#ifdef USE_MIDI
void AudacityProject::OnImportMIDI()
{
wxString path = gPrefs->Read(wxT("/DefaultOpenPath"),::wxGetCwd());
wxString fileName = FileSelector(_("Select a MIDI file"),
path, // Path
wxString fileName = FileNames::SelectFile(FileNames::Operation::Open,
_("Select a MIDI file"),
wxEmptyString, // Path
wxT(""), // Name
wxT(""), // Extension
_("MIDI and Allegro files (*.mid;*.midi;*.gro)|*.mid;*.midi;*.gro|MIDI files (*.mid;*.midi)|*.mid;*.midi|Allegro files (*.gro)|*.gro|All files|*"),
wxRESIZE_BORDER, // Flags
this); // Parent
if (fileName != wxT("")) {
path =::wxPathOnly(fileName);
gPrefs->Write(wxT("/DefaultOpenPath"), path);
gPrefs->Flush();
if (fileName != wxT(""))
AudacityProject::DoImportMIDI(this, fileName);
}
}
AudacityProject *AudacityProject::DoImportMIDI(
@ -6839,11 +6829,10 @@ AudacityProject *AudacityProject::DoImportMIDI(
void AudacityProject::OnImportRaw()
{
wxString path = gPrefs->Read(wxT("/DefaultOpenPath"),::wxGetCwd());
wxString fileName =
FileSelector(_("Select any uncompressed audio file"),
path, // Path
FileNames::SelectFile(FileNames::Operation::Open,
_("Select any uncompressed audio file"),
wxEmptyString, // Path
wxT(""), // Name
wxT(""), // Extension
_("All files|*"),
@ -6853,10 +6842,6 @@ void AudacityProject::OnImportRaw()
if (fileName == wxT(""))
return;
path =::wxPathOnly(fileName);
gPrefs->Write(wxT("/DefaultOpenPath"), path);
gPrefs->Flush();
TrackHolders newTracks;
::ImportRaw(this, fileName, GetTrackFactory(), newTracks);
@ -8231,7 +8216,8 @@ void AudacityProject::OnAudioDeviceInfo()
if (dlg.ShowModal() == wxID_OK)
{
wxString fName = FileSelector(_("Save Device Info"),
wxString fName = FileNames::SelectFile(FileNames::Operation::Export,
_("Save Device Info"),
wxEmptyString,
wxT("deviceinfo.txt"),
wxT("txt"),
@ -8271,7 +8257,8 @@ void AudacityProject::OnMidiDeviceInfo()
if (dlg.ShowModal() == wxID_OK)
{
wxString fName = FileSelector(_("Save MIDI Device Info"),
wxString fName = FileNames::SelectFile(FileNames::Operation::Export,
_("Save MIDI Device Info"),
wxEmptyString,
wxT("midideviceinfo.txt"),
wxT("txt"),

View File

@ -4352,7 +4352,8 @@ For an audio file that will open in other apps, use 'Export'.\n"),
// for overwrite ourselves later, and we disallow it.
// We disallow overwrite because we would have to DELETE the many
// smaller files too, or prompt to move them.
wxString fName = FileSelector(sDialogTitle,
wxString fName = FileNames::SelectFile(FileNames::Operation::Export,
sDialogTitle,
filename.GetPath(),
filename.GetFullName(),
wxT("aup"),

View File

@ -41,7 +41,6 @@
#include <wx/window.h>
#endif
#include "FileDialog.h"
#include "FileNames.h"
#include "Internat.h"
#include "Prefs.h"
@ -51,6 +50,7 @@
#include <wx/button.h>
#include <wx/choice.h>
#include <wx/filedlg.h>
#include <wx/filename.h>
#include <wx/intl.h>
#include <wx/listctrl.h>
@ -1152,7 +1152,8 @@ void TagsEditor::OnLoad(wxCommandEvent & WXUNUSED(event))
wxString fn;
// Ask the user for the real name
fn = FileSelector(_("Load Metadata As:"),
fn = FileNames::SelectFile(FileNames::Operation::None,
_("Load Metadata As:"),
FileNames::DataDir(),
wxT("Tags.xml"),
wxT("xml"),
@ -1206,7 +1207,8 @@ void TagsEditor::OnSave(wxCommandEvent & WXUNUSED(event))
TransferDataFromWindow();
// Ask the user for the real name
fn = FileSelector(_("Save Metadata As:"),
fn = FileNames::SelectFile(FileNames::Operation::None,
_("Save Metadata As:"),
FileNames::DataDir(),
wxT("Tags.xml"),
wxT("xml"),

View File

@ -25,6 +25,7 @@
#include <wx/defs.h>
#include <wx/dir.h>
#include <wx/datetime.h>
#include <wx/filedlg.h>
#include <wx/intl.h>
#include <wx/progdlg.h>
#include <wx/sizer.h>
@ -291,7 +292,8 @@ void TimerRecordDialog::OnTimeText_Duration(wxCommandEvent& WXUNUSED(event))
// New events for timer recording automation
void TimerRecordDialog::OnAutoSavePathButton_Click(wxCommandEvent& WXUNUSED(event))
{
wxString fName = FileSelector(_("Save Timer Recording As"),
wxString fName = FileNames::SelectFile(FileNames::Operation::Export,
_("Save Timer Recording As"),
m_fnAutoSaveFile.GetPath(),
m_fnAutoSaveFile.GetFullName(),
wxT("aup"),

View File

@ -20,7 +20,6 @@
#include "../widgets/LinkingHtmlWindow.h"
#include "../widgets/HelpSystem.h"
#include "../widgets/NumericTextCtrl.h"
#include "../lib-src/FileDialog/FileDialog.h"
#include <cmath>
#include <limits>
@ -30,6 +29,7 @@
#define finite(x) _finite(x)
#endif
#include <wx/filedlg.h>
#include <wx/valtext.h>
#include <wx/log.h>
@ -489,7 +489,8 @@ void ContrastDialog::OnExport(wxCommandEvent & WXUNUSED(event))
AudacityProject * project = GetActiveProject();
wxString fName = wxT("contrast.txt");
fName = FileSelector(_("Export Contrast Result As:"),
fName = FileNames::SelectFile(FileNames::Operation::Export,
_("Export Contrast Result As:"),
wxEmptyString,
fName,
wxT("txt"),

View File

@ -78,7 +78,6 @@
// TODO: Unfortunately we have some dependencies on Audacity provided
// dialogs, widgets and other stuff. This will need to be cleaned up.
#include "FileDialog.h"
#include "../../FileNames.h"
#include "../../Internat.h"
#include "../../PlatformCompatibility.h"
@ -1798,8 +1797,9 @@ void VSTEffect::ExportPresets()
// Ask the user for the real name
//
// Passing a valid parent will cause some effects dialogs to malfunction
// upon returning from the FileSelector().
path = FileSelector(_("Save VST Preset As:"),
// upon returning from the FileNames::SelectFile().
path = FileNames::SelectFile(FileNames::Operation::None,
_("Save VST Preset As:"),
FileNames::DataDir(),
wxEmptyString,
wxT("xml"),
@ -1850,7 +1850,8 @@ void VSTEffect::ImportPresets()
wxString path;
// Ask the user for the real name
path = FileSelector(_("Load VST Preset:"),
path = FileNames::SelectFile(FileNames::Operation::None,
_("Load VST Preset:"),
FileNames::DataDir(),
wxEmptyString,
wxT("xml"),

View File

@ -21,8 +21,8 @@
#include "../SampleFormat.h"
#include "../widgets/wxPanelWrapper.h"
#include "FileDialog.h"
class FileDialog;
class wxFileCtrlEvent;
class wxMemoryDC;
class wxStaticText;
class AudacityProject;

View File

@ -18,12 +18,13 @@
#include <wx/app.h>
#include <wx/button.h>
#include <wx/combobox.h>
#include <wx/filedlg.h>
#include <wx/log.h>
#include <wx/msgdlg.h>
#include <wx/process.h>
#include <wx/sizer.h>
#include <wx/textctrl.h>
#include <FileDialog.h>
#include "FileNames.h"
#include "Export.h"
#include "../Mix.h"
@ -169,7 +170,8 @@ void ExportCLOptions::OnBrowse(wxCommandEvent& WXUNUSED(event))
ext = wxT(".exe");
#endif
path = FileSelector(_("Find path to command"),
path = FileNames::SelectFile(FileNames::Operation::Open,
_("Find path to command"),
wxEmptyString,
wxEmptyString,
ext,

View File

@ -53,6 +53,7 @@
#include <wx/spinctrl.h>
#include <wx/combobox.h>
#include <wx/listimpl.cpp>
#include <FileDialog.h>
#include "../FileFormats.h"
#include "../Internat.h"

View File

@ -69,6 +69,7 @@
#include <wx/checkbox.h>
#include <wx/dynlib.h>
#include <wx/ffile.h>
#include <wx/filedlg.h>
#include <wx/intl.h>
#include <wx/log.h>
#include <wx/mimetype.h>
@ -91,8 +92,6 @@
#include "../Track.h"
#include "../widgets/LinkingHtmlWindow.h"
#include "FileDialog.h"
#include "Export.h"
#include <lame/lame.h>
@ -661,7 +660,8 @@ public:
* "Where would I find the file %s" instead if you want. */
question.Printf(_("Where is %s?"), mName.c_str());
wxString path = FileSelector(question,
wxString path = FileNames::SelectFile(FileNames::Operation::None,
question,
mLibPath.GetPath(),
mLibPath.GetName(),
wxT(""),

View File

@ -39,7 +39,7 @@ KeyConfigPrefs and MousePrefs use.
#include "../Internat.h"
#include "../ShuttleGui.h"
#include "FileDialog.h"
#include "FileNames.h"
#if defined(EXPERIMENTAL_KEY_VIEW)
@ -333,11 +333,10 @@ void KeyConfigPrefs::RefreshBindings(bool bSort)
void KeyConfigPrefs::OnImport(wxCommandEvent & WXUNUSED(event))
{
wxString file = wxT("Audacity-keys.xml");
wxString path = gPrefs->Read(wxT("/DefaultOpenPath"),
::wxGetCwd());
file = FileSelector(_("Select an XML file containing Audacity keyboard shortcuts..."),
path,
file = FileNames::SelectFile(FileNames::Operation::Open,
_("Select an XML file containing Audacity keyboard shortcuts..."),
wxEmptyString,
file,
wxT(""),
_("XML files (*.xml)|*.xml|All files|*"),
@ -348,10 +347,6 @@ void KeyConfigPrefs::OnImport(wxCommandEvent & WXUNUSED(event))
return;
}
path = wxPathOnly(file);
gPrefs->Write(wxT("/DefaultOpenPath"), path);
gPrefs->Flush();
XMLFileReader reader;
if (!reader.Parse(mManager, file)) {
wxMessageBox(reader.GetErrorStr(),
@ -365,11 +360,10 @@ void KeyConfigPrefs::OnImport(wxCommandEvent & WXUNUSED(event))
void KeyConfigPrefs::OnExport(wxCommandEvent & WXUNUSED(event))
{
wxString file = wxT("Audacity-keys.xml");
wxString path = gPrefs->Read(wxT("/DefaultExportPath"),
::wxGetCwd());
file = FileSelector(_("Export Keyboard Shortcuts As:"),
path,
file = FileNames::SelectFile(FileNames::Operation::Export,
_("Export Keyboard Shortcuts As:"),
wxEmptyString,
file,
wxT("xml"),
_("XML files (*.xml)|*.xml|All files|*"),
@ -380,10 +374,6 @@ void KeyConfigPrefs::OnExport(wxCommandEvent & WXUNUSED(event))
return;
}
path = wxPathOnly(file);
gPrefs->Write(wxT("/DefaultExportPath"), path);
gPrefs->Flush();
GuardedCall< void >( [&] {
XMLFileWriter prefFile{ file, _("Error Exporting Keyboard Shortcuts") };
mManager->WriteXML(prefFile);
@ -993,11 +983,10 @@ void KeyConfigPrefs::RepopulateBindingsList()
void KeyConfigPrefs::OnImport(wxCommandEvent & WXUNUSED(event))
{
wxString file = wxT("Audacity-keys.xml");
wxString path = gPrefs->Read(wxT("/DefaultOpenPath"),
::wxGetCwd());
file = FileSelector(_("Select an XML file containing Audacity keyboard shortcuts..."),
path,
file = FileNames::SelectFile(FileNames::Operation::Open,
_("Select an XML file containing Audacity keyboard shortcuts..."),
wxEmptyString,
file,
wxT(""),
_("XML files (*.xml)|*.xml|All files|*"),
@ -1008,10 +997,6 @@ void KeyConfigPrefs::OnImport(wxCommandEvent & WXUNUSED(event))
return;
}
path = wxPathOnly(file);
gPrefs->Write(wxT("/DefaultOpenPath"), path);
gPrefs->Flush();
XMLFileReader reader;
if (!reader.Parse(mManager, file)) {
wxMessageBox(reader.GetErrorStr(),
@ -1025,10 +1010,9 @@ void KeyConfigPrefs::OnImport(wxCommandEvent & WXUNUSED(event))
void KeyConfigPrefs::OnExport(wxCommandEvent & WXUNUSED(event))
{
wxString file = wxT("Audacity-keys.xml");
wxString path = gPrefs->Read(wxT("/DefaultExportPath"),
::wxGetCwd());
file = FileSelector(_("Export Keyboard Shortcuts As:"),
file = FileNames::SelectFile(FileNames::Operation::Export,
_("Export Keyboard Shortcuts As:"),
path,
file,
wxT("xml"),
@ -1040,10 +1024,6 @@ void KeyConfigPrefs::OnExport(wxCommandEvent & WXUNUSED(event))
return;
}
path = wxPathOnly(file);
gPrefs->Write(wxT("/DefaultExportPath"), path);
gPrefs->Flush();
GuardedCall< void >( [&] {
XMLFileWriter prefFile{ file, _("Error Exporting Keyboard Shortcuts") };
mManager->WriteXML(prefFile);