diff --git a/src/prefs/PrefsDialog.cpp b/src/prefs/PrefsDialog.cpp index 16ef57835..d39952289 100644 --- a/src/prefs/PrefsDialog.cpp +++ b/src/prefs/PrefsDialog.cpp @@ -477,7 +477,36 @@ int wxTreebookExt::SetSelection(size_t n) return i; } +namespace { + struct Entry{ + unsigned sequenceNumber; + PrefsDialog::PrefsNode node; + bool operator < ( const Entry &other ) const + { return sequenceNumber < other.sequenceNumber; } + }; + using Entries = std::vector< Entry >; + Entries &Registry() + { + static Entries result; + return result; + } +} + +PrefsPanel::Registration::Registration( unsigned sequenceNumber, + const Factory &factory, + unsigned nChildren, + bool expanded ) +{ + auto ®istry = Registry(); + Entry entry{ sequenceNumber, { factory, nChildren, expanded } }; + const auto end = registry.end(); + // Find insertion point: + auto iter = std::lower_bound( registry.begin(), end, entry ); + // There should not be duplicate sequence numbers: + wxASSERT( iter == end || entry < *iter ); + registry.insert( iter, entry ); +} PrefsDialog::Factories &PrefsDialog::DefaultFactories() diff --git a/src/prefs/PrefsDialog.h b/src/prefs/PrefsDialog.h index 06187fb61..10def863f 100644 --- a/src/prefs/PrefsDialog.h +++ b/src/prefs/PrefsDialog.h @@ -39,11 +39,11 @@ class PrefsDialog /* not final */ : public wxDialogWrapper std::function< PrefsPanel * ( wxWindow *parent, wxWindowID winid, AudacityProject *) >; Factory factory; - CONST int nChildren; + unsigned nChildren; bool expanded; PrefsNode(const Factory &factory_, - int nChildren_ = 0, + unsigned nChildren_ = 0, bool expanded_ = true) : factory(factory_), nChildren(nChildren_), expanded(expanded_) {} diff --git a/src/prefs/PrefsPanel.h b/src/prefs/PrefsPanel.h index 9ed84312c..c8508ac8f 100644 --- a/src/prefs/PrefsPanel.h +++ b/src/prefs/PrefsPanel.h @@ -49,7 +49,7 @@ class PrefsPanel /* not final */ : public wxPanelWrapper, ComponentInterface { public: // \brief Type alias for factories such as GUIPrefsFactory that produce a - // PrefsPanel. + // PrefsPanel, used by the Preferences dialog in a treebook. // The project pointer may be null. Usually it's not needed because // preferences are global. But sometimes you need a project, such as to // preview the preference changes for spectrograms. @@ -57,8 +57,18 @@ class PrefsPanel /* not final */ : public wxPanelWrapper, ComponentInterface std::function< PrefsPanel * ( wxWindow *parent, wxWindowID winid, AudacityProject *) >; - PrefsPanel( - wxWindow * parent, wxWindowID winid, const TranslatableString &title) + // Typically you make a static object of this type in the .cpp file that + // also implements the PrefsPanel subclass. + struct Registration final + { + Registration( unsigned sequenceNumber, + const Factory &factory, + unsigned nChildren = 0, + bool expanded = true ); + }; + + PrefsPanel(wxWindow * parent, + wxWindowID winid, const TranslatableString &title) : wxPanelWrapper(parent, winid) { SetLabel(title); // Provide visual label