From f4e1b28c9f1da32cd5ee8eef178a1eccd581372d Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Wed, 7 Mar 2012 22:40:15 +0000 Subject: [PATCH] Rework handling of copied styles in the style manager a bit Generate the new name in the style manager rather than the editor so that the naming scheme of copies is consistent between the Copy buttons and pasting styles. Name the second copy of style X "X - Copy (2)" rather than "X - Copy - Copy", and so on for further copies. (Copies of style "X - Copy" would still be named "X - Copy - Copy"). Select the new style when a copy is created. Originally committed to SVN as r6536. --- aegisub/src/dialog_style_editor.cpp | 12 ++++-- aegisub/src/dialog_style_editor.h | 4 +- aegisub/src/dialog_style_manager.cpp | 58 ++++++++++++++++++++-------- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/aegisub/src/dialog_style_editor.cpp b/aegisub/src/dialog_style_editor.cpp index edd7b7062..75276d051 100644 --- a/aegisub/src/dialog_style_editor.cpp +++ b/aegisub/src/dialog_style_editor.cpp @@ -78,17 +78,17 @@ static wxTextCtrl *num_text_ctrl(wxWindow *parent, double value, wxSize size = w return new wxTextCtrl(parent, -1, "", wxDefaultPosition, size, 0, NumValidator(value)); } -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") +DialogStyleEditor::DialogStyleEditor(wxWindow *parent, AssStyle *style, agi::Context *c, AssStyleStorage *store, wxString const& new_name) +: wxDialog (parent, -1, _("Style Editor"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) , c(c) , is_new(false) , style(style) , store(store) { - if (copy_style) { + if (new_name.size()) { is_new = true; style = this->style = new AssStyle(*style); - style->name = wxString::Format(_("%s - Copy"), style->name); + style->name = new_name; } else if (!style) { is_new = true; @@ -346,6 +346,10 @@ DialogStyleEditor::~DialogStyleEditor() { delete style; } +wxString DialogStyleEditor::GetStyleName() const { + return style->name; +} + /// @brief Update appearances of a renamed style in \r tags static void ReplaceStyle(wxString tag, int, AssOverrideParameter* param, void *userData) { wxArrayString strings = *((wxArrayString*)userData); diff --git a/aegisub/src/dialog_style_editor.h b/aegisub/src/dialog_style_editor.h index 28cddf59f..9ba357b3f 100644 --- a/aegisub/src/dialog_style_editor.h +++ b/aegisub/src/dialog_style_editor.h @@ -155,6 +155,8 @@ class DialogStyleEditor : public wxDialog { void OnSetColor(int n, wxCommandEvent& evt); public: - DialogStyleEditor(wxWindow *parent, AssStyle *style, agi::Context *c, AssStyleStorage *store, bool copy_style); + DialogStyleEditor(wxWindow *parent, AssStyle *style, agi::Context *c, AssStyleStorage *store = 0, wxString const& new_name = ""); ~DialogStyleEditor(); + + wxString GetStyleName() const; }; diff --git a/aegisub/src/dialog_style_manager.cpp b/aegisub/src/dialog_style_manager.cpp index 4226d16d8..2f052ac0c 100644 --- a/aegisub/src/dialog_style_manager.cpp +++ b/aegisub/src/dialog_style_manager.cpp @@ -66,6 +66,8 @@ #include "standard_paths.h" #include "utils.h" +using std::tr1::placeholders::_1; + static wxBitmapButton *add_bitmap_button(wxWindow *parent, wxSizer *sizer, wxBitmap const& img, wxString const& tooltip) { wxBitmapButton *btn = new wxBitmapButton(parent, -1, img); btn->SetToolTip(tooltip); @@ -372,7 +374,7 @@ void DialogStyleManager::OnStorageEdit() { int n = StorageList->GetSelections(selections); if (n == 1) { AssStyle *selStyle = styleStorageMap[selections[0]]; - DialogStyleEditor(this, selStyle, c, &Store, false).ShowModal(); + DialogStyleEditor(this, selStyle, c, &Store).ShowModal(); StorageList->SetString(selections[0],selStyle->name); Store.Save(CatalogList->GetString(CatalogList->GetSelection())); } @@ -384,7 +386,7 @@ void DialogStyleManager::OnCurrentEdit() { int n = CurrentList->GetSelections(selections); if (n == 1) { AssStyle *selStyle = styleMap[selections[0]]; - DialogStyleEditor(this, selStyle, c, 0, false).ShowModal(); + DialogStyleEditor(this, selStyle, c).ShowModal(); CurrentList->SetString(selections[0],selStyle->name); } UpdateButtons(); @@ -452,15 +454,32 @@ void DialogStyleManager::OnCopyToCurrent() { UpdateButtons(); } +template +static wxString unique_name(Func name_checker, wxString const& source_name) { + if (name_checker(source_name)) { + wxString name = wxString::Format(_("%s - Copy"), source_name); + for (int i = 2; name_checker(name); ++i) + name = wxString::Format(_("%s - Copy (%d)"), source_name, i); + return name; + } + return source_name; +} + void DialogStyleManager::OnStorageCopy() { wxArrayInt selections; StorageList->GetSelections(selections); if (selections.empty()) return; - DialogStyleEditor(this, styleStorageMap[selections[0]], c, &Store, true).ShowModal(); - Store.Save(CatalogList->GetString(CatalogList->GetSelection())); - LoadStorageStyles(); - UpdateButtons(); + AssStyle *s = styleStorageMap[selections[0]]; + DialogStyleEditor editor(this, s, c, &Store, + unique_name(bind(&AssStyleStorage::GetStyle, &Store, _1), s->name)); + + if (editor.ShowModal()) { + Store.Save(CatalogList->GetString(CatalogList->GetSelection())); + LoadStorageStyles(); + StorageList->SetStringSelection(editor.GetStyleName()); + UpdateButtons(); + } } void DialogStyleManager::OnCurrentCopy() { @@ -468,9 +487,15 @@ void DialogStyleManager::OnCurrentCopy() { CurrentList->GetSelections(selections); if (selections.empty()) return; - DialogStyleEditor(this, styleMap[selections[0]], c, 0, true).ShowModal(); - LoadCurrentStyles(c->ass); - UpdateButtons(); + AssStyle *s = styleMap[selections[0]]; + DialogStyleEditor editor(this, s, c, 0, + unique_name(bind(&AssFile::GetStyle, c->ass, _1), s->name)); + + if (editor.ShowModal()) { + LoadCurrentStyles(c->ass); + CurrentList->SetStringSelection(editor.GetStyleName()); + UpdateButtons(); + } } void DialogStyleManager::CopyToClipboard(wxListBox *list, std::vector v) { @@ -510,8 +535,7 @@ static void add_styles(Func1 name_checker, Func2 style_adder) { while (st.HasMoreTokens()) { try { AssStyle *s = new AssStyle(st.GetNextToken().Trim(true)); - while (name_checker(s->name)) - s->name = "Copy of " + s->name; + s->name = unique_name(name_checker, s->name); style_adder(s); } catch (...) { @@ -522,8 +546,8 @@ static void add_styles(Func1 name_checker, Func2 style_adder) { void DialogStyleManager::PasteToCurrent() { add_styles( - bind(&AssFile::GetStyle, c->ass, std::tr1::placeholders::_1), - bind(&AssFile::InsertStyle, c->ass, std::tr1::placeholders::_1)); + bind(&AssFile::GetStyle, c->ass, _1), + bind(&AssFile::InsertStyle, c->ass, _1)); LoadCurrentStyles(c->ass); c->ass->Commit(_("style paste"), AssFile::COMMIT_STYLES); @@ -531,8 +555,8 @@ void DialogStyleManager::PasteToCurrent() { void DialogStyleManager::PasteToStorage() { add_styles( - bind(&AssStyleStorage::GetStyle, &Store, std::tr1::placeholders::_1), - bind(&std::list::push_back, &Store.style, std::tr1::placeholders::_1)); + bind(&AssStyleStorage::GetStyle, &Store, _1), + bind(&std::list::push_back, &Store.style, _1)); Store.Save(CatalogList->GetString(CatalogList->GetSelection())); LoadStorageStyles(); @@ -540,14 +564,14 @@ void DialogStyleManager::PasteToStorage() { } void DialogStyleManager::OnStorageNew() { - DialogStyleEditor(this, 0, c, &Store, false).ShowModal(); + DialogStyleEditor(this, 0, c, &Store).ShowModal(); Store.Save(CatalogList->GetString(CatalogList->GetSelection())); LoadStorageStyles(); UpdateButtons(); } void DialogStyleManager::OnCurrentNew() { - DialogStyleEditor(this,0, c, 0, false).ShowModal(); + DialogStyleEditor(this, 0, c, 0).ShowModal(); LoadCurrentStyles(c->ass); UpdateButtons(); }