From 7b01339e510f53f5427f774103891e3fac366889 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Wed, 17 Jun 2015 15:52:29 -0400 Subject: [PATCH] Future subclasses of PrefsDialog may choose the preferred page by other means. --- src/AudacityApp.cpp | 4 +-- src/DirManager.cpp | 1 - src/Menus.cpp | 2 +- src/prefs/PrefsDialog.cpp | 61 +++++++++++++++++++++++++++------------ src/prefs/PrefsDialog.h | 28 ++++++++++++++++-- 5 files changed, 71 insertions(+), 25 deletions(-) diff --git a/src/AudacityApp.cpp b/src/AudacityApp.cpp index 7f7df24dd..90ea7c794 100644 --- a/src/AudacityApp.cpp +++ b/src/AudacityApp.cpp @@ -1605,7 +1605,7 @@ bool AudacityApp::InitTempDir() // Failed wxMessageBox(_("Audacity could not find a place to store temporary files.\nPlease enter an appropriate directory in the preferences dialog.")); - PrefsDialog dialog(NULL); + GlobalPrefsDialog dialog(NULL); dialog.ShowTempDirPage(); dialog.ShowModal(); @@ -2132,7 +2132,7 @@ void AudacityApp::OnMenuPreferences(wxCommandEvent & event) // all platforms. if(gAudacityProjects.GetCount() == 0) { - PrefsDialog dialog(NULL /* parent */ ); + GlobalPrefsDialog dialog(NULL /* parent */ ); dialog.ShowModal(); } else diff --git a/src/DirManager.cpp b/src/DirManager.cpp index 8104f56b1..691915c5a 100644 --- a/src/DirManager.cpp +++ b/src/DirManager.cpp @@ -101,7 +101,6 @@ #include "widgets/Warning.h" #include "widgets/MultiDialog.h" -#include "prefs/PrefsDialog.h" #include "ondemand/ODManager.h" #if defined(__WXMAC__) diff --git a/src/Menus.cpp b/src/Menus.cpp index 5ecfcce68..92c00da52 100644 --- a/src/Menus.cpp +++ b/src/Menus.cpp @@ -3586,7 +3586,7 @@ void AudacityProject::OnExportMultiple() void AudacityProject::OnPreferences() { - PrefsDialog dialog(this /* parent */ ); + GlobalPrefsDialog dialog(this /* parent */ ); if (!dialog.ShowModal()) { // Canceled diff --git a/src/prefs/PrefsDialog.cpp b/src/prefs/PrefsDialog.cpp index 9852d2f95..32da41f8f 100644 --- a/src/prefs/PrefsDialog.cpp +++ b/src/prefs/PrefsDialog.cpp @@ -233,23 +233,6 @@ PrefsDialog::PrefsDialog S.AddStandardButtons(eOkButton | eCancelButton); - /* long is signed, size_t is unsigned. On some platforms they are different - * lengths as well. So we must check that the stored category is both > 0 - * and within the possible range of categories, making the first check on the - * _signed_ value to avoid issues when converting an unsigned one. - */ - size_t selected; - long prefscat = gPrefs->Read(wxT("/Prefs/PrefsCategory"), 0L); - if (prefscat > 0L ) - selected = prefscat; // only assign if number will fit - else - selected = 0; // use 0 if value can't be assigned - - if (selected >= mCategories->GetPageCount()) - selected = 0; // clamp to available range of tabs - - mCategories->SetSelection(selected); - #if defined(__WXGTK__) mCategories->GetTreeCtrl()->EnsureVisible(mCategories->GetTreeCtrl()->GetRootItem()); #endif @@ -297,6 +280,21 @@ PrefsDialog::~PrefsDialog() { } +int PrefsDialog::ShowModal() +{ + /* long is signed, size_t is unsigned. On some platforms they are different + * lengths as well. So we must check that the stored category is both > 0 + * and within the possible range of categories, making the first check on the + * _signed_ value to avoid issues when converting an unsigned one. + */ + long selected = GetPreferredPage(); + if (selected < 0 || size_t(selected) >= mCategories->GetPageCount()) + selected = 0; // clamp to available range of tabs + mCategories->SetSelection(selected); + + return wxDialog::ShowModal(); +} + void PrefsDialog::OnCancel(wxCommandEvent & WXUNUSED(event)) { RecordExpansionState(); @@ -338,8 +336,7 @@ void PrefsDialog::OnOK(wxCommandEvent & WXUNUSED(event)) panel->Apply(); } - gPrefs->Write(wxT("/Prefs/PrefsCategory"), (long)mCategories->GetSelection()); - gPrefs->Flush(); + SavePreferredPage(); #if USE_PORTMIXER if (gAudioIO) { @@ -394,6 +391,32 @@ void PrefsDialog::ShowTempDirPage() SelectPageByName(_("Directories")); } +int PrefsDialog::GetSelectedPage() const +{ + return mCategories->GetSelection(); +} + +GlobalPrefsDialog::GlobalPrefsDialog(wxWindow * parent) + : PrefsDialog(parent, _("Preferences: "), DefaultFactories()) +{ +} + +GlobalPrefsDialog::~GlobalPrefsDialog() +{ +} + +long GlobalPrefsDialog::GetPreferredPage() +{ + long prefscat = gPrefs->Read(wxT("/Prefs/PrefsCategory"), 0L); + return prefscat; +} + +void GlobalPrefsDialog::SavePreferredPage() +{ + gPrefs->Write(wxT("/Prefs/PrefsCategory"), (long)GetSelectedPage()); + gPrefs->Flush(); +} + void PrefsDialog::RecordExpansionState() { // Remember expansion state of the tree control diff --git a/src/prefs/PrefsDialog.h b/src/prefs/PrefsDialog.h index 190207030..239eb4d8a 100644 --- a/src/prefs/PrefsDialog.h +++ b/src/prefs/PrefsDialog.h @@ -49,6 +49,9 @@ class PrefsDialog:public wxDialog Factories &factories = DefaultFactories()); virtual ~PrefsDialog(); + // Defined this so a protected virtual can be invoked after the constructor + virtual int ShowModal(); + void OnCategoryChange(wxCommandEvent & e); void OnOK(wxCommandEvent & e); void OnCancel(wxCommandEvent & e); @@ -57,9 +60,19 @@ class PrefsDialog:public wxDialog void SelectPageByName(wxString pageName); void ShowTempDirPage(); - private: - void RecordExpansionState(); + // Accessor to help implementations of SavePreferredPage(), + // such as by saving a preference after DoModal() returns + int GetSelectedPage() const; + protected: + // Decide which page to open first; return -1 for undecided + virtual long GetPreferredPage() = 0; + + // Called after OK is clicked and all pages validate + virtual void SavePreferredPage() = 0; + +private: + void RecordExpansionState(); wxTreebook *mCategories; Factories &mFactories; const wxString mTitlePrefix; @@ -67,4 +80,15 @@ class PrefsDialog:public wxDialog DECLARE_EVENT_TABLE() }; +// This adds code appropriate only to the original use of PrefsDialog for +// global settings -- not its reuses elsewhere as in View Settings +class GlobalPrefsDialog : public PrefsDialog +{ +public: + GlobalPrefsDialog(wxWindow * parent); + virtual ~GlobalPrefsDialog(); + virtual long GetPreferredPage(); + virtual void SavePreferredPage(); +}; + #endif