diff --git a/aegisub/src/dialog_spellchecker.cpp b/aegisub/src/dialog_spellchecker.cpp index 1e9d284a6..98396bf99 100644 --- a/aegisub/src/dialog_spellchecker.cpp +++ b/aegisub/src/dialog_spellchecker.cpp @@ -189,7 +189,6 @@ void DialogSpellChecker::OnAdd(wxCommandEvent&) { void DialogSpellChecker::OnChangeLanguage(wxCommandEvent&) { wxString code = dictionary_lang_codes[language->GetSelection()]; - spellchecker->SetLanguage(code); OPT_SET("Tool/Spell Checker/Language")->SetString(STD_STR(code)); FindNext(); diff --git a/aegisub/src/include/aegisub/spellchecker.h b/aegisub/src/include/aegisub/spellchecker.h index 6898ee5c1..4377b7c31 100644 --- a/aegisub/src/include/aegisub/spellchecker.h +++ b/aegisub/src/include/aegisub/spellchecker.h @@ -69,7 +69,6 @@ public: virtual wxArrayString GetSuggestions(wxString word)=0; virtual wxArrayString GetLanguageList()=0; - virtual void SetLanguage(wxString language)=0; }; /// DOCME diff --git a/aegisub/src/spellchecker_hunspell.cpp b/aegisub/src/spellchecker_hunspell.cpp index 04b441613..628835e21 100644 --- a/aegisub/src/spellchecker_hunspell.cpp +++ b/aegisub/src/spellchecker_hunspell.cpp @@ -38,6 +38,8 @@ #ifdef WITH_HUNSPELL +#include "spellchecker_hunspell.h" + #ifndef AGI_PRE #include #include @@ -47,6 +49,8 @@ #include #endif +#include + #include #include #include @@ -54,18 +58,20 @@ #include "charset_conv.h" #include "compat.h" #include "main.h" -#include "spellchecker_hunspell.h" #include "standard_paths.h" -HunspellSpellChecker::HunspellSpellChecker() { - SetLanguage(lagi_wxString(OPT_GET("Tool/Spell Checker/Language")->GetString())); +HunspellSpellChecker::HunspellSpellChecker() +: lang_listener(OPT_SUB("Tool/Spell Checker/Language", &HunspellSpellChecker::OnLanguageChanged, this)) +, dict_path_listener(OPT_SUB("Path/Dictionary", &HunspellSpellChecker::OnPathChanged, this)) +{ + OnLanguageChanged(); } HunspellSpellChecker::~HunspellSpellChecker() { } bool HunspellSpellChecker::CanAddWord(wxString word) { - if (!hunspell.get()) return false; + if (!hunspell) return false; try { conv->Convert(STD_STR(word)); return true; @@ -76,7 +82,7 @@ bool HunspellSpellChecker::CanAddWord(wxString word) { } void HunspellSpellChecker::AddWord(wxString word) { - if (!hunspell.get()) return; + if (!hunspell) return; std::string sword = STD_STR(word); @@ -95,7 +101,7 @@ void HunspellSpellChecker::AddWord(wxString word) { try { agi::scoped_ptr stream(agi::io::Open(STD_STR(userDicPath))); remove_copy_if( - ++agi::line_iterator(*stream.get()), + ++agi::line_iterator(*stream), agi::line_iterator(), back_inserter(words), mem_fun_ref(&std::string::empty)); @@ -116,7 +122,7 @@ void HunspellSpellChecker::AddWord(wxString word) { } bool HunspellSpellChecker::CheckWord(wxString word) { - if (!hunspell.get()) return true; + if (!hunspell) return true; try { return hunspell->spell(conv->Convert(STD_STR(word)).c_str()) == 1; } @@ -127,7 +133,7 @@ bool HunspellSpellChecker::CheckWord(wxString word) { wxArrayString HunspellSpellChecker::GetSuggestions(wxString word) { wxArrayString suggestions; - if (!hunspell.get()) return suggestions; + if (!hunspell) return suggestions; // Grab raw from Hunspell char **results; @@ -193,11 +199,11 @@ wxArrayString HunspellSpellChecker::GetLanguageList() { return languages; } -void HunspellSpellChecker::SetLanguage(wxString language) { - if (language.empty()) { - hunspell.reset(); - return; - } +void HunspellSpellChecker::OnLanguageChanged() { + hunspell.reset(); + + std::string language = OPT_GET("Tool/Spell Checker/Language")->GetString(); + if (language.empty()) return; wxString userDicRoot = StandardPaths::DecodePath(lagi_wxString(OPT_GET("Path/Dictionary")->GetString())); wxString dataDicRoot = StandardPaths::DecodePath("?data/dictionaries"); @@ -221,14 +227,14 @@ void HunspellSpellChecker::SetLanguage(wxString language) { } hunspell.reset(new Hunspell(affPath.mb_str(csConvLocal), dicPath.mb_str(csConvLocal))); - if (!hunspell.get()) return; + if (!hunspell) return; conv.reset(new agi::charset::IconvWrapper("utf-8", hunspell->get_dic_encoding())); rconv.reset(new agi::charset::IconvWrapper(hunspell->get_dic_encoding(), "utf-8")); try { - std::auto_ptr stream(agi::io::Open(STD_STR(userDicPath))); - agi::line_iterator userDic(*stream.get()); + agi::scoped_ptr stream(agi::io::Open(STD_STR(userDicPath))); + agi::line_iterator userDic(*stream); agi::line_iterator end; ++userDic; // skip entry count line for (; userDic != end; ++userDic) { @@ -247,4 +253,8 @@ void HunspellSpellChecker::SetLanguage(wxString language) { } } +void HunspellSpellChecker::OnPathChanged() { + languages.clear(); +} + #endif // WITH_HUNSPELL diff --git a/aegisub/src/spellchecker_hunspell.h b/aegisub/src/spellchecker_hunspell.h index 61e446d2a..aa3cff81f 100644 --- a/aegisub/src/spellchecker_hunspell.h +++ b/aegisub/src/spellchecker_hunspell.h @@ -36,26 +36,28 @@ #ifdef WITH_HUNSPELL -#include -#include - #include "include/aegisub/spellchecker.h" + +#include +#include + namespace agi { namespace charset { class IconvWrapper; } } +class Hunspell; /// @class HunspellSpellChecker /// @brief Hunspell spell checker /// class HunspellSpellChecker : public SpellChecker { /// Hunspell instance - std::auto_ptr hunspell; + agi::scoped_ptr hunspell; /// Conversions between the dictionary charset and utf-8 - std::auto_ptr conv; - std::auto_ptr rconv; + agi::scoped_ptr conv; + agi::scoped_ptr rconv; /// Languages which we have dictionaries for wxArrayString languages; @@ -63,6 +65,16 @@ class HunspellSpellChecker : public SpellChecker { /// Path to user-local dictionary. wxString userDicPath; + /// Dictionary language change connection + agi::signal::Connection lang_listener; + /// Dictionary language change handler + void OnLanguageChanged(); + + /// Dictionary path change connection + agi::signal::Connection dict_path_listener; + /// Dictionary path change handler + void OnPathChanged(); + public: HunspellSpellChecker(); ~HunspellSpellChecker(); @@ -88,9 +100,6 @@ public: /// @brief Get a list of languages which dictionaries are present for wxArrayString GetLanguageList(); - /// @brief Set the spellchecker's language - /// @param language Language code - void SetLanguage(wxString language); }; #endif diff --git a/aegisub/src/subs_edit_ctrl.cpp b/aegisub/src/subs_edit_ctrl.cpp index ca637ce7f..5e0bcc5d2 100644 --- a/aegisub/src/subs_edit_ctrl.cpp +++ b/aegisub/src/subs_edit_ctrl.cpp @@ -908,7 +908,6 @@ void SubsTextEditCtrl::OnSetDicLanguage(wxCommandEvent &event) { if (index >= 0) lang = langs[index]; - spellchecker->SetLanguage(lang); OPT_SET("Tool/Spell Checker/Language")->SetString(STD_STR(lang)); UpdateStyle();