1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-07-06 07:29:07 +02:00

Prefs dialog, when constructed with a single page, does not show the tree

This commit is contained in:
Paul Licameli 2015-08-18 23:36:16 -04:00
parent 4868b6afeb
commit 28d552c39b
2 changed files with 102 additions and 55 deletions

View File

@ -196,46 +196,61 @@ PrefsDialog::PrefsDialog
wxDefaultSize, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
, mFactories(factories) , mFactories(factories)
, mCategories(NULL)
, mUniquePage(NULL)
, mTitlePrefix(titlePrefix) , mTitlePrefix(titlePrefix)
{ {
wxASSERT(factories.size() > 0);
const bool uniquePage = (factories.size() == 1);
ShuttleGui S(this, eIsCreating); ShuttleGui S(this, eIsCreating);
S.StartVerticalLay(true); S.StartVerticalLay(true);
{ {
S.StartHorizontalLay(wxALIGN_LEFT | wxEXPAND, true); wxASSERT(factories.size() > 0);
{ if (!uniquePage) {
mCategories = new wxTreebookExt(this, wxID_ANY, mTitlePrefix); mCategories = new wxTreebookExt(this, wxID_ANY, mTitlePrefix);
S.Prop(1); S.StartHorizontalLay(wxALIGN_LEFT | wxEXPAND, true);
S.AddWindow(mCategories, wxEXPAND);
{ {
typedef std::pair<int, int> IntPair; S.Prop(1);
std::vector<IntPair> stack; S.AddWindow(mCategories, wxEXPAND);
int iPage = 0;
for (Factories::const_iterator it = factories.begin(), end = factories.end();
it != end; ++it, ++iPage)
{ {
const PrefsNode &node = *it; typedef std::pair<int, int> IntPair;
PrefsPanelFactory &factory = *node.pFactory; std::vector<IntPair> stack;
wxWindow *const w = factory.Create(mCategories); int iPage = 0;
if (stack.empty()) for (Factories::const_iterator it = factories.begin(), end = factories.end();
// Parameters are: AddPage(page, name, IsSelected, imageId). it != end; ++it, ++iPage)
mCategories->AddPage(w, w->GetName(), false, 0); {
else { const PrefsNode &node = *it;
IntPair &top = *stack.rbegin(); PrefsPanelFactory &factory = *node.pFactory;
mCategories->InsertSubPage(top.first, w, w->GetName(), false, 0); wxWindow *const w = factory.Create(mCategories);
if (--top.second == 0) { if (stack.empty())
// Expand all nodes before the layout calculation // Parameters are: AddPage(page, name, IsSelected, imageId).
mCategories->ExpandNode(top.first, true); mCategories->AddPage(w, w->GetName(), false, 0);
stack.pop_back(); else {
IntPair &top = *stack.rbegin();
mCategories->InsertSubPage(top.first, w, w->GetName(), false, 0);
if (--top.second == 0) {
// Expand all nodes before the layout calculation
mCategories->ExpandNode(top.first, true);
stack.pop_back();
}
} }
if (node.nChildren > 0)
stack.push_back(IntPair(iPage, node.nChildren));
} }
if (node.nChildren > 0)
stack.push_back(IntPair(iPage, node.nChildren));
} }
} }
S.EndHorizontalLay();
}
else {
// Unique page, don't show the factory
const PrefsNode &node = factories[0];
PrefsPanelFactory &factory = *node.pFactory;
mUniquePage = factory.Create(this);
S.AddWindow(mUniquePage, wxEXPAND);
} }
S.EndHorizontalLay();
} }
S.EndVerticalLay(); S.EndVerticalLay();
@ -251,6 +266,7 @@ PrefsDialog::PrefsDialog
wxSize sz = GetSize(); wxSize sz = GetSize();
// Collapse nodes only after layout so the tree is wide enough // Collapse nodes only after layout so the tree is wide enough
if (mCategories)
{ {
int iPage = 0; int iPage = 0;
for (Factories::const_iterator it = factories.begin(), end = factories.end(); for (Factories::const_iterator it = factories.begin(), end = factories.end();
@ -290,15 +306,22 @@ PrefsDialog::~PrefsDialog()
int PrefsDialog::ShowModal() int PrefsDialog::ShowModal()
{ {
/* long is signed, size_t is unsigned. On some platforms they are different if (mCategories) {
* lengths as well. So we must check that the stored category is both > 0 /* long is signed, size_t is unsigned. On some platforms they are different
* and within the possible range of categories, making the first check on the * lengths as well. So we must check that the stored category is both > 0
* _signed_ value to avoid issues when converting an unsigned one. * 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()) long selected = GetPreferredPage();
selected = 0; // clamp to available range of tabs if (selected < 0 || size_t(selected) >= mCategories->GetPageCount())
mCategories->SetSelection(selected); selected = 0; // clamp to available range of tabs
mCategories->SetSelection(selected);
}
else {
wxString Temp = mTitlePrefix + mUniquePage->GetLabel();
SetTitle(Temp);
SetName(Temp);
}
return wxDialog::ShowModal(); return wxDialog::ShowModal();
} }
@ -307,9 +330,13 @@ void PrefsDialog::OnCancel(wxCommandEvent & WXUNUSED(event))
{ {
RecordExpansionState(); RecordExpansionState();
for (size_t i = 0; i < mCategories->GetPageCount(); i++) { if (mCategories) {
((PrefsPanel *) mCategories->GetPage(i))->Cancel(); for (size_t i = 0; i < mCategories->GetPageCount(); i++) {
((PrefsPanel *)mCategories->GetPage(i))->Cancel();
}
} }
else
mUniquePage->Cancel();
EndModal(false); EndModal(false);
} }
@ -327,23 +354,33 @@ void PrefsDialog::OnOK(wxCommandEvent & WXUNUSED(event))
RecordExpansionState(); RecordExpansionState();
// Validate all pages first // Validate all pages first
for (size_t i = 0; i < mCategories->GetPageCount(); i++) { if (mCategories) {
PrefsPanel *panel = (PrefsPanel *) mCategories->GetPage(i); for (size_t i = 0; i < mCategories->GetPageCount(); i++) {
PrefsPanel *panel = (PrefsPanel *)mCategories->GetPage(i);
// The dialog doesn't end until all the input is valid // The dialog doesn't end until all the input is valid
if (!panel->Validate()) { if (!panel->Validate()) {
mCategories->SetSelection(i); mCategories->SetSelection(i);
return; return;
}
} }
} }
else {
// Now apply the changes if (!mUniquePage->Validate())
for (size_t i = 0; i < mCategories->GetPageCount(); i++) { return;
PrefsPanel *panel = (PrefsPanel *) mCategories->GetPage(i);
panel->Apply();
} }
if (mCategories) {
// Now apply the changes
for (size_t i = 0; i < mCategories->GetPageCount(); i++) {
PrefsPanel *panel = (PrefsPanel *)mCategories->GetPage(i);
panel->Apply();
}
}
else
mUniquePage->Apply();
SavePreferredPage(); SavePreferredPage();
#if USE_PORTMIXER #if USE_PORTMIXER
@ -386,19 +423,24 @@ void PrefsDialog::OnOK(wxCommandEvent & WXUNUSED(event))
void PrefsDialog::SelectPageByName(wxString pageName) void PrefsDialog::SelectPageByName(wxString pageName)
{ {
size_t n = mCategories->GetPageCount(); if (mCategories) {
size_t n = mCategories->GetPageCount();
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
if (mCategories->GetPageText(i) == pageName) { if (mCategories->GetPageText(i) == pageName) {
mCategories->SetSelection(i); mCategories->SetSelection(i);
return; return;
}
} }
} }
} }
int PrefsDialog::GetSelectedPage() const int PrefsDialog::GetSelectedPage() const
{ {
return mCategories->GetSelection(); if (mCategories)
return mCategories->GetSelection();
else
return 0;
} }
GlobalPrefsDialog::GlobalPrefsDialog(wxWindow * parent, Factories &factories) GlobalPrefsDialog::GlobalPrefsDialog(wxWindow * parent, Factories &factories)
@ -425,10 +467,13 @@ void GlobalPrefsDialog::SavePreferredPage()
void PrefsDialog::RecordExpansionState() void PrefsDialog::RecordExpansionState()
{ {
// Remember expansion state of the tree control // Remember expansion state of the tree control
if (mCategories)
{ {
int iPage = 0; int iPage = 0;
for (Factories::iterator it = mFactories.begin(), end = mFactories.end(); for (Factories::iterator it = mFactories.begin(), end = mFactories.end();
it != end; ++it, ++iPage) it != end; ++it, ++iPage)
it->expanded = mCategories->IsNodeExpanded(iPage); it->expanded = mCategories->IsNodeExpanded(iPage);
} }
else
mFactories[0].expanded = true;
} }

View File

@ -20,6 +20,7 @@
#include <wx/treebook.h> #include <wx/treebook.h>
#include <wx/window.h> #include <wx/window.h>
class PrefsPanel;
class PrefsPanelFactory; class PrefsPanelFactory;
#ifdef __GNUC__ #ifdef __GNUC__
@ -75,6 +76,7 @@ class PrefsDialog:public wxDialog
private: private:
void RecordExpansionState(); void RecordExpansionState();
wxTreebook *mCategories; wxTreebook *mCategories;
PrefsPanel *mUniquePage;
Factories &mFactories; Factories &mFactories;
const wxString mTitlePrefix; const wxString mTitlePrefix;