Make the style editor commit its own changes. Fixes some issues where changes made in the style editor would fail to update the rest of the program.

Originally committed to SVN as r5754.
This commit is contained in:
Thomas Goyne 2011-10-17 22:00:58 +00:00
parent a75aa7a633
commit c7d0ce60ee
3 changed files with 64 additions and 94 deletions

View file

@ -78,15 +78,25 @@ static wxTextCtrl *num_text_ctrl(wxWindow *parent, double value, wxSize size = w
return new wxTextCtrl(parent, -1, "", wxDefaultPosition, size, 0, NumValidator(wxString::Format("%0.3g", value), true, false)); return new wxTextCtrl(parent, -1, "", wxDefaultPosition, size, 0, NumValidator(wxString::Format("%0.3g", value), true, false));
} }
DialogStyleEditor::DialogStyleEditor (wxWindow *parent, AssStyle *style, agi::Context *c, bool local, AssStyleStorage *store, bool newStyle) DialogStyleEditor::DialogStyleEditor(wxWindow *parent, AssStyle *style, agi::Context *c, AssStyleStorage *store, bool copy_style)
: wxDialog (parent, -1, _("Style Editor"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER, "DialogStyleEditor") : wxDialog (parent, -1, _("Style Editor"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER, "DialogStyleEditor")
, c(c) , c(c)
, isLocal(local) , is_new(false)
, isNew(newStyle)
, style(style) , style(style)
, work(new AssStyle(*style))
, store(store) , store(store)
{ {
if (copy_style) {
is_new = true;
style = this->style = new AssStyle(*style);
style->name += _(" - Copy");
}
else if (!style) {
is_new = true;
style = this->style = new AssStyle;
}
work.reset(new AssStyle(*style));
SetIcon(BitmapToIcon(GETIMAGE(style_toolbutton_24))); SetIcon(BitmapToIcon(GETIMAGE(style_toolbutton_24)));
// Prepare control values // Prepare control values
@ -332,30 +342,29 @@ DialogStyleEditor::DialogStyleEditor (wxWindow *parent, AssStyle *style, agi::Co
} }
DialogStyleEditor::~DialogStyleEditor() { DialogStyleEditor::~DialogStyleEditor() {
if (is_new)
delete style;
} }
/// @brief Update appearances of a renamed style in \r tags /// @brief Update appearances of a renamed style in \r tags
void ReplaceStyle(wxString tag, int, AssOverrideParameter* param, void *userData) { static void ReplaceStyle(wxString tag, int, AssOverrideParameter* param, void *userData) {
wxArrayString strings = *((wxArrayString*)userData); wxArrayString strings = *((wxArrayString*)userData);
if (tag == "\\r" && param->GetType() == VARDATA_TEXT && param->Get<wxString>() == strings[0]) { if (tag == "\\r" && param->GetType() == VARDATA_TEXT && param->Get<wxString>() == strings[0]) {
param->Set(strings[1]); param->Set(strings[1]);
} }
} }
/// @brief Maybe apply changs and maybe close the dialog
/// @param apply Should changes be applied?
/// @param close Should the dialog be closed?
void DialogStyleEditor::Apply(bool apply, bool close) { void DialogStyleEditor::Apply(bool apply, bool close) {
if (apply) { if (apply) {
wxString newStyleName = StyleName->GetValue(); wxString newStyleName = StyleName->GetValue();
// Get list of existing styles // Get list of existing styles
wxArrayString styles = isLocal ? c->ass->GetStyles() : store->GetNames(); wxArrayString styles = store ? store->GetNames() : c->ass->GetStyles();
// Check if style name is unique // Check if style name is unique
for (unsigned int i=0;i<styles.Count();i++) { for (unsigned int i=0;i<styles.Count();i++) {
if (newStyleName.CmpNoCase(styles[i]) == 0) { if (newStyleName.CmpNoCase(styles[i]) == 0) {
if ((isLocal && (c->ass->GetStyle(styles[i]) != style)) || (!isLocal && (store->GetStyle(styles[i]) != style))) { if ((store && store->GetStyle(styles[i]) != style) || c->ass->GetStyle(styles[i]) != style) {
wxMessageBox("There is already a style with this name. Please choose another name.", "Style name conflict.", wxICON_ERROR|wxOK); wxMessageBox("There is already a style with this name. Please choose another name.", "Style name conflict.", wxICON_ERROR|wxOK);
return; return;
} }
@ -365,11 +374,9 @@ void DialogStyleEditor::Apply (bool apply, bool close) {
// Style name change // Style name change
bool did_rename = false; bool did_rename = false;
if (work->name != newStyleName) { if (work->name != newStyleName) {
if (!isNew && isLocal) { if (!store && !is_new && work->name != "Default") {
// See if user wants to update style name through script // See if user wants to update style name through script
int answer = wxNO; int answer = wxMessageBox(_("Do you want to change all instances of this style in the script to this new name?"), _("Update script?"), wxYES_NO | wxCANCEL);
if (work->name != "Default")
answer = wxMessageBox(_("Do you want to change all instances of this style in the script to this new name?"), _("Update script?"), wxYES_NO | wxCANCEL);
if (answer == wxCANCEL) return; if (answer == wxCANCEL) return;
@ -397,7 +404,14 @@ void DialogStyleEditor::Apply (bool apply, bool close) {
*style = *work; *style = *work;
style->UpdateData(); style->UpdateData();
if (isLocal) if (is_new) {
if (store)
store->style.push_back(style);
else
c->ass->InsertStyle(style);
is_new = false;
}
if (!store)
c->ass->Commit(_("style change"), AssFile::COMMIT_STYLES | (did_rename ? AssFile::COMMIT_DIAG_FULL : 0)); c->ass->Commit(_("style change"), AssFile::COMMIT_STYLES | (did_rename ? AssFile::COMMIT_DIAG_FULL : 0));
// Update preview // Update preview

View file

@ -60,13 +60,10 @@ class DialogStyleEditor : public wxDialog {
agi::Context *c; agi::Context *c;
agi::scoped_ptr<PersistLocation> persist; agi::scoped_ptr<PersistLocation> persist;
/// DOCME
bool isLocal;
/// If true, the style was just created and so the user should not be /// If true, the style was just created and so the user should not be
/// asked if they want to change any existing lines should they rename /// asked if they want to change any existing lines should they rename
/// the style /// the style
bool isNew; bool is_new;
/// DOCME /// DOCME
AssStyle *style; AssStyle *style;
@ -151,10 +148,13 @@ class DialogStyleEditor : public wxDialog {
void OnPreviewTextChange(wxCommandEvent &event); void OnPreviewTextChange(wxCommandEvent &event);
void OnPreviewColourChange(wxCommandEvent &event); void OnPreviewColourChange(wxCommandEvent &event);
/// @brief Maybe apply changes and maybe close the dialog
/// @param apply Should changes be applied?
/// @param close Should the dialog be closed?
void Apply(bool apply,bool close); void Apply(bool apply,bool close);
void OnSetColor(int n, wxCommandEvent& evt); void OnSetColor(int n, wxCommandEvent& evt);
public: public:
DialogStyleEditor(wxWindow *parent,AssStyle *style, agi::Context *c,bool local,AssStyleStorage *store,bool newStyle=false); DialogStyleEditor(wxWindow *parent, AssStyle *style, agi::Context *c, AssStyleStorage *store, bool copy_style);
~DialogStyleEditor(); ~DialogStyleEditor();
}; };

View file

@ -481,12 +481,10 @@ void DialogStyleManager::OnStorageEdit (wxCommandEvent &) {
int n = StorageList->GetSelections(selections); int n = StorageList->GetSelections(selections);
if (n == 1) { if (n == 1) {
AssStyle *selStyle = styleStorageMap[selections[0]]; AssStyle *selStyle = styleStorageMap[selections[0]];
DialogStyleEditor editor(this,selStyle,c,false,&Store); DialogStyleEditor(this, selStyle, c, &Store, false).ShowModal();
if (editor.ShowModal()) {
StorageList->SetString(selections[0],selStyle->name); StorageList->SetString(selections[0],selStyle->name);
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetString(CatalogList->GetSelection()));
} }
}
UpdateMoveButtons(); UpdateMoveButtons();
} }
@ -496,11 +494,9 @@ void DialogStyleManager::OnCurrentEdit (wxCommandEvent &) {
int n = CurrentList->GetSelections(selections); int n = CurrentList->GetSelections(selections);
if (n == 1) { if (n == 1) {
AssStyle *selStyle = styleMap[selections[0]]; AssStyle *selStyle = styleMap[selections[0]];
DialogStyleEditor editor(this,selStyle,c,true,&Store); DialogStyleEditor(this, selStyle, c, 0, false).ShowModal();
if (editor.ShowModal()) {
CurrentList->SetString(selections[0],selStyle->name); CurrentList->SetString(selections[0],selStyle->name);
} }
}
UpdateMoveButtons(); UpdateMoveButtons();
} }
@ -606,21 +602,10 @@ void DialogStyleManager::OnStorageCopy (wxCommandEvent &) {
wxArrayInt selections; wxArrayInt selections;
StorageList->GetSelections(selections); StorageList->GetSelections(selections);
if (selections.size() == 0) return; if (selections.size() == 0) return;
AssStyle *temp = new AssStyle(*(styleStorageMap.at(selections[0])));
wxString newName = _("Copy of "); DialogStyleEditor(this, styleStorageMap[selections[0]], c, &Store, true).ShowModal();
newName += temp->name;
temp->name = newName;
DialogStyleEditor editor(this,temp,c,false,&Store,true);
int modified = editor.ShowModal();
if (modified) {
Store.style.push_back(temp);
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetString(CatalogList->GetSelection()));
LoadStorageStyles(); LoadStorageStyles();
StorageList->SetStringSelection(temp->name); // the copy/delete/copy-to-local buttons stay disabled after this?
}
else delete temp;
UpdateMoveButtons(); UpdateMoveButtons();
} }
@ -630,21 +615,8 @@ void DialogStyleManager::OnCurrentCopy (wxCommandEvent &) {
CurrentList->GetSelections(selections); CurrentList->GetSelections(selections);
if (selections.size() == 0) return; if (selections.size() == 0) return;
AssStyle *temp = new AssStyle(styleMap.at(selections[0])->GetEntryData()); DialogStyleEditor(this, styleMap[selections[0]], c, 0, true).ShowModal();
wxString newName = _("Copy of ");
newName += temp->name;
temp->name = newName;
DialogStyleEditor editor(this,temp,c,true,&Store,true);
int modified = editor.ShowModal();
if (modified) {
c->ass->InsertStyle(temp);
LoadCurrentStyles(c->ass); LoadCurrentStyles(c->ass);
CurrentList->SetStringSelection(temp->name); // but even without this, the copy/delete/copy-to-storage buttons stay enabled?
}
else delete temp;
c->ass->Commit(_("style copy"), AssFile::COMMIT_STYLES);
UpdateMoveButtons(); UpdateMoveButtons();
} }
@ -750,32 +722,16 @@ void DialogStyleManager::PasteToStorage() {
/// @brief Storage new /// @brief Storage new
void DialogStyleManager::OnStorageNew (wxCommandEvent &) { void DialogStyleManager::OnStorageNew (wxCommandEvent &) {
AssStyle *temp = new AssStyle; DialogStyleEditor(this, 0, c, &Store, false).ShowModal();
DialogStyleEditor editor(this,temp,c,false,&Store,true);
int modified = editor.ShowModal();
if (modified) {
Store.style.push_back(temp);
Store.Save(CatalogList->GetString(CatalogList->GetSelection())); Store.Save(CatalogList->GetString(CatalogList->GetSelection()));
LoadStorageStyles(); LoadStorageStyles();
StorageList->SetStringSelection(temp->name);
}
else delete temp;
UpdateMoveButtons(); UpdateMoveButtons();
} }
/// @brief Current new /// @brief Current new
void DialogStyleManager::OnCurrentNew (wxCommandEvent &) { void DialogStyleManager::OnCurrentNew (wxCommandEvent &) {
AssStyle *temp = new AssStyle; DialogStyleEditor(this,0, c, 0, false).ShowModal();
DialogStyleEditor editor(this,temp,c,true,&Store,true);
int modified = editor.ShowModal();
if (modified) {
c->ass->InsertStyle(temp);
LoadCurrentStyles(c->ass); LoadCurrentStyles(c->ass);
CurrentList->SetStringSelection(temp->name);
}
else delete temp;
UpdateMoveButtons(); UpdateMoveButtons();
} }