Ensure that there is always at least one style catalog existing and selected so that the style manager doesn't have to check if there is one

Originally committed to SVN as r6543.
This commit is contained in:
Thomas Goyne 2012-03-08 04:17:17 +00:00
parent a0b4da90f7
commit fa06033762
3 changed files with 44 additions and 62 deletions

View file

@ -68,6 +68,7 @@ void AssStyleStorage::Load(wxString const& name) {
if (name.empty()) return; if (name.empty()) return;
Clear(); Clear();
try {
TextFileReader file(StandardPaths::DecodePath("?user/catalog/" + name + ".sty"), "UTF-8"); TextFileReader file(StandardPaths::DecodePath("?user/catalog/" + name + ".sty"), "UTF-8");
while (file.HasMoreLines()) { while (file.HasMoreLines()) {
@ -80,6 +81,10 @@ void AssStyleStorage::Load(wxString const& name) {
} }
} }
} }
}
catch (agi::FileNotAccessibleError const&) {
// Just treat a missing file as an empty file
}
} }
void AssStyleStorage::Clear () { void AssStyleStorage::Clear () {

View file

@ -117,7 +117,7 @@ DialogStyleManager::DialogStyleManager(agi::Context *context)
wxSizer *CatalogBox = new wxStaticBoxSizer(wxHORIZONTAL,this,_("Catalog of available storages")); wxSizer *CatalogBox = new wxStaticBoxSizer(wxHORIZONTAL,this,_("Catalog of available storages"));
CatalogList = new wxComboBox(this,-1, "", wxDefaultPosition, wxSize(-1,-1), 0, NULL, wxCB_READONLY | wxCB_READONLY, wxDefaultValidator, "Catalog List"); CatalogList = new wxComboBox(this,-1, "", wxDefaultPosition, wxSize(-1,-1), 0, NULL, wxCB_READONLY | wxCB_READONLY, wxDefaultValidator, "Catalog List");
wxButton *CatalogNew = new wxButton(this, -1, _("New")); wxButton *CatalogNew = new wxButton(this, -1, _("New"));
wxButton *CatalogDelete = new wxButton(this, -1, _("Delete")); CatalogDelete = new wxButton(this, -1, _("Delete"));
CatalogBox->Add(CatalogList,1,wxEXPAND | wxRIGHT | wxALIGN_RIGHT,5); CatalogBox->Add(CatalogList,1,wxEXPAND | wxRIGHT | wxALIGN_RIGHT,5);
CatalogBox->Add(CatalogNew,0,wxRIGHT,5); CatalogBox->Add(CatalogNew,0,wxRIGHT,5);
CatalogBox->Add(CatalogDelete,0,0,0); CatalogBox->Add(CatalogDelete,0,0,0);
@ -230,9 +230,7 @@ DialogStyleManager::DialogStyleManager(agi::Context *context)
} }
DialogStyleManager::~DialogStyleManager() { DialogStyleManager::~DialogStyleManager() {
int sel = CatalogList->GetSelection(); c->ass->SetScriptInfo("Last Style Storage", CatalogList->GetStringSelection());
if (sel != wxNOT_FOUND)
c->ass->SetScriptInfo("Last Style Storage", CatalogList->GetString(sel));
} }
void DialogStyleManager::LoadCatalog() { void DialogStyleManager::LoadCatalog() {
@ -244,24 +242,21 @@ void DialogStyleManager::LoadCatalog() {
CatalogList->Append(wxFileName(curfile).GetName()); CatalogList->Append(wxFileName(curfile).GetName());
// Create a default storage if there are none // Create a default storage if there are none
if (CatalogList->IsListEmpty()) { if (CatalogList->IsListEmpty())
Store.Clear();
Store.style.push_back(new AssStyle);
Store.Save("Default");
CatalogList->Append("Default"); CatalogList->Append("Default");
}
// Set to default if available // Set to default if available
StorageActions(false);
wxString pickStyle = c->ass->GetScriptInfo("Last Style Storage"); wxString pickStyle = c->ass->GetScriptInfo("Last Style Storage");
if (pickStyle.empty()) if (pickStyle.empty())
pickStyle = "Default"; pickStyle = "Default";
int opt = CatalogList->FindString(pickStyle, false); int opt = CatalogList->FindString(pickStyle, false);
if (opt != wxNOT_FOUND) { if (opt != wxNOT_FOUND)
CatalogList->SetSelection(opt); CatalogList->SetSelection(opt);
else
CatalogList->SetSelection(0);
OnChangeCatalog(); OnChangeCatalog();
}
} }
void DialogStyleManager::LoadCurrentStyles(AssFile *subs) { void DialogStyleManager::LoadCurrentStyles(AssFile *subs) {
@ -290,28 +285,9 @@ void DialogStyleManager::LoadStorageStyles() {
UpdateButtons(); UpdateButtons();
} }
void DialogStyleManager::StorageActions(bool state) {
StorageList->Enable(state);
MoveToLocal->Enable(state);
StorageNew->Enable(state);
StorageCopy->Enable(state);
StorageDelete->Enable(state);
UpdateButtons();
}
void DialogStyleManager::OnChangeCatalog() { void DialogStyleManager::OnChangeCatalog() {
int sel = CatalogList->GetSelection(); Store.Load(CatalogList->GetStringSelection());
if (sel != wxNOT_FOUND) {
StorageActions(true);
Store.Load(CatalogList->GetString(sel));
LoadStorageStyles(); LoadStorageStyles();
}
else {
StorageActions(false);
Store.Clear();
LoadStorageStyles();
}
UpdateButtons(); UpdateButtons();
} }
@ -346,7 +322,6 @@ void DialogStyleManager::OnCatalogNew() {
StorageList->Clear(); StorageList->Clear();
CatalogList->Append(name); CatalogList->Append(name);
CatalogList->SetStringSelection(name); CatalogList->SetStringSelection(name);
StorageActions(true);
Store.Save(name); Store.Save(name);
} }
@ -354,19 +329,17 @@ void DialogStyleManager::OnCatalogNew() {
} }
void DialogStyleManager::OnCatalogDelete() { void DialogStyleManager::OnCatalogDelete() {
int sel = CatalogList->GetSelection(); if (CatalogList->GetCount() == 1) return;
if (sel != wxNOT_FOUND) {
wxString name = CatalogList->GetString(sel); wxString name = CatalogList->GetStringSelection();
wxString message = wxString::Format(_("Are you sure you want to delete the storage \"%s\" from the catalog?"), name); wxString message = wxString::Format(_("Are you sure you want to delete the storage \"%s\" from the catalog?"), name);
int option = wxMessageBox(message, _("Confirm delete"), wxYES_NO | wxICON_EXCLAMATION , this); int option = wxMessageBox(message, _("Confirm delete"), wxYES_NO | wxICON_EXCLAMATION , this);
if (option == wxYES) { if (option == wxYES) {
wxRemoveFile(StandardPaths::DecodePath("?user/catalog/" + name + ".sty")); wxRemoveFile(StandardPaths::DecodePath("?user/catalog/" + name + ".sty"));
CatalogList->Delete(sel); CatalogList->Delete(CatalogList->GetSelection());
StorageList->Clear(); CatalogList->SetSelection(0);
StorageActions(false); OnChangeCatalog();
} }
}
UpdateButtons();
} }
void DialogStyleManager::OnStorageEdit() { void DialogStyleManager::OnStorageEdit() {
@ -376,7 +349,7 @@ void DialogStyleManager::OnStorageEdit() {
AssStyle *selStyle = styleStorageMap[selections[0]]; AssStyle *selStyle = styleStorageMap[selections[0]];
DialogStyleEditor(this, selStyle, c, &Store).ShowModal(); DialogStyleEditor(this, selStyle, c, &Store).ShowModal();
StorageList->SetString(selections[0],selStyle->name); StorageList->SetString(selections[0],selStyle->name);
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetStringSelection());
} }
UpdateButtons(); UpdateButtons();
} }
@ -415,7 +388,7 @@ void DialogStyleManager::OnCopyToStorage() {
copied.push_back(styleName); copied.push_back(styleName);
} }
} }
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetStringSelection());
LoadStorageStyles(); LoadStorageStyles();
for (std::list<wxString>::iterator name = copied.begin(); name != copied.end(); ++name) { for (std::list<wxString>::iterator name = copied.begin(); name != copied.end(); ++name) {
StorageList->SetStringSelection(*name, true); StorageList->SetStringSelection(*name, true);
@ -475,7 +448,7 @@ void DialogStyleManager::OnStorageCopy() {
unique_name(bind(&AssStyleStorage::GetStyle, &Store, _1), s->name)); unique_name(bind(&AssStyleStorage::GetStyle, &Store, _1), s->name));
if (editor.ShowModal()) { if (editor.ShowModal()) {
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetStringSelection());
LoadStorageStyles(); LoadStorageStyles();
StorageList->SetStringSelection(editor.GetStyleName()); StorageList->SetStringSelection(editor.GetStyleName());
UpdateButtons(); UpdateButtons();
@ -558,14 +531,14 @@ void DialogStyleManager::PasteToStorage() {
bind(&AssStyleStorage::GetStyle, &Store, _1), bind(&AssStyleStorage::GetStyle, &Store, _1),
bind(&std::list<AssStyle*>::push_back, &Store.style, _1)); bind(&std::list<AssStyle*>::push_back, &Store.style, _1));
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetStringSelection());
LoadStorageStyles(); LoadStorageStyles();
StorageList->SetStringSelection(Store.style.back()->name); StorageList->SetStringSelection(Store.style.back()->name);
} }
void DialogStyleManager::OnStorageNew() { void DialogStyleManager::OnStorageNew() {
DialogStyleEditor(this, 0, c, &Store).ShowModal(); DialogStyleEditor(this, 0, c, &Store).ShowModal();
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetStringSelection());
LoadStorageStyles(); LoadStorageStyles();
UpdateButtons(); UpdateButtons();
} }
@ -593,7 +566,7 @@ void DialogStyleManager::OnStorageDelete() {
Store.style.remove(temp); Store.style.remove(temp);
delete temp; delete temp;
} }
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetStringSelection());
LoadStorageStyles(); LoadStorageStyles();
} }
UpdateButtons(); UpdateButtons();
@ -680,6 +653,8 @@ void DialogStyleManager::OnCurrentImport() {
} }
void DialogStyleManager::UpdateButtons() { void DialogStyleManager::UpdateButtons() {
CatalogDelete->Enable(CatalogList->GetCount() > 1);
// Get storage selection // Get storage selection
wxArrayInt sels; wxArrayInt sels;
int n = StorageList->GetSelections(sels); int n = StorageList->GetSelections(sels);
@ -813,7 +788,7 @@ void DialogStyleManager::MoveStyles(bool storage, int type) {
if (storage) { if (storage) {
Store.style.clear(); Store.style.clear();
copy(styls.begin(), styls.end(), back_inserter(Store.style)); copy(styls.begin(), styls.end(), back_inserter(Store.style));
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetStringSelection());
} }
// Current // Current
else { else {

View file

@ -73,6 +73,8 @@ class DialogStyleManager : public wxDialog {
wxListBox *StorageList; wxListBox *StorageList;
wxListBox *CurrentList; wxListBox *CurrentList;
wxButton *CatalogDelete;
wxButton *MoveToLocal; wxButton *MoveToLocal;
wxButton *MoveToStorage; wxButton *MoveToStorage;