Always use the same language for all spellchecker instances as having different languages in different places is weird

Originally committed to SVN as r6395.
This commit is contained in:
Thomas Goyne 2012-01-31 00:43:49 +00:00
parent e205f9d51b
commit 57fc4145d9
5 changed files with 44 additions and 28 deletions

View File

@ -189,7 +189,6 @@ void DialogSpellChecker::OnAdd(wxCommandEvent&) {
void DialogSpellChecker::OnChangeLanguage(wxCommandEvent&) { void DialogSpellChecker::OnChangeLanguage(wxCommandEvent&) {
wxString code = dictionary_lang_codes[language->GetSelection()]; wxString code = dictionary_lang_codes[language->GetSelection()];
spellchecker->SetLanguage(code);
OPT_SET("Tool/Spell Checker/Language")->SetString(STD_STR(code)); OPT_SET("Tool/Spell Checker/Language")->SetString(STD_STR(code));
FindNext(); FindNext();

View File

@ -69,7 +69,6 @@ public:
virtual wxArrayString GetSuggestions(wxString word)=0; virtual wxArrayString GetSuggestions(wxString word)=0;
virtual wxArrayString GetLanguageList()=0; virtual wxArrayString GetLanguageList()=0;
virtual void SetLanguage(wxString language)=0;
}; };
/// DOCME /// DOCME

View File

@ -38,6 +38,8 @@
#ifdef WITH_HUNSPELL #ifdef WITH_HUNSPELL
#include "spellchecker_hunspell.h"
#ifndef AGI_PRE #ifndef AGI_PRE
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
@ -47,6 +49,8 @@
#include <wx/filename.h> #include <wx/filename.h>
#endif #endif
#include <hunspell/hunspell.hxx>
#include <libaegisub/io.h> #include <libaegisub/io.h>
#include <libaegisub/line_iterator.h> #include <libaegisub/line_iterator.h>
#include <libaegisub/log.h> #include <libaegisub/log.h>
@ -54,18 +58,20 @@
#include "charset_conv.h" #include "charset_conv.h"
#include "compat.h" #include "compat.h"
#include "main.h" #include "main.h"
#include "spellchecker_hunspell.h"
#include "standard_paths.h" #include "standard_paths.h"
HunspellSpellChecker::HunspellSpellChecker() { HunspellSpellChecker::HunspellSpellChecker()
SetLanguage(lagi_wxString(OPT_GET("Tool/Spell Checker/Language")->GetString())); : lang_listener(OPT_SUB("Tool/Spell Checker/Language", &HunspellSpellChecker::OnLanguageChanged, this))
, dict_path_listener(OPT_SUB("Path/Dictionary", &HunspellSpellChecker::OnPathChanged, this))
{
OnLanguageChanged();
} }
HunspellSpellChecker::~HunspellSpellChecker() { HunspellSpellChecker::~HunspellSpellChecker() {
} }
bool HunspellSpellChecker::CanAddWord(wxString word) { bool HunspellSpellChecker::CanAddWord(wxString word) {
if (!hunspell.get()) return false; if (!hunspell) return false;
try { try {
conv->Convert(STD_STR(word)); conv->Convert(STD_STR(word));
return true; return true;
@ -76,7 +82,7 @@ bool HunspellSpellChecker::CanAddWord(wxString word) {
} }
void HunspellSpellChecker::AddWord(wxString word) { void HunspellSpellChecker::AddWord(wxString word) {
if (!hunspell.get()) return; if (!hunspell) return;
std::string sword = STD_STR(word); std::string sword = STD_STR(word);
@ -95,7 +101,7 @@ void HunspellSpellChecker::AddWord(wxString word) {
try { try {
agi::scoped_ptr<std::istream> stream(agi::io::Open(STD_STR(userDicPath))); agi::scoped_ptr<std::istream> stream(agi::io::Open(STD_STR(userDicPath)));
remove_copy_if( remove_copy_if(
++agi::line_iterator<std::string>(*stream.get()), ++agi::line_iterator<std::string>(*stream),
agi::line_iterator<std::string>(), agi::line_iterator<std::string>(),
back_inserter(words), back_inserter(words),
mem_fun_ref(&std::string::empty)); mem_fun_ref(&std::string::empty));
@ -116,7 +122,7 @@ void HunspellSpellChecker::AddWord(wxString word) {
} }
bool HunspellSpellChecker::CheckWord(wxString word) { bool HunspellSpellChecker::CheckWord(wxString word) {
if (!hunspell.get()) return true; if (!hunspell) return true;
try { try {
return hunspell->spell(conv->Convert(STD_STR(word)).c_str()) == 1; 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 HunspellSpellChecker::GetSuggestions(wxString word) {
wxArrayString suggestions; wxArrayString suggestions;
if (!hunspell.get()) return suggestions; if (!hunspell) return suggestions;
// Grab raw from Hunspell // Grab raw from Hunspell
char **results; char **results;
@ -193,11 +199,11 @@ wxArrayString HunspellSpellChecker::GetLanguageList() {
return languages; return languages;
} }
void HunspellSpellChecker::SetLanguage(wxString language) { void HunspellSpellChecker::OnLanguageChanged() {
if (language.empty()) { hunspell.reset();
hunspell.reset();
return; 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 userDicRoot = StandardPaths::DecodePath(lagi_wxString(OPT_GET("Path/Dictionary")->GetString()));
wxString dataDicRoot = StandardPaths::DecodePath("?data/dictionaries"); 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))); 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())); conv.reset(new agi::charset::IconvWrapper("utf-8", hunspell->get_dic_encoding()));
rconv.reset(new agi::charset::IconvWrapper(hunspell->get_dic_encoding(), "utf-8")); rconv.reset(new agi::charset::IconvWrapper(hunspell->get_dic_encoding(), "utf-8"));
try { try {
std::auto_ptr<std::istream> stream(agi::io::Open(STD_STR(userDicPath))); agi::scoped_ptr<std::istream> stream(agi::io::Open(STD_STR(userDicPath)));
agi::line_iterator<std::string> userDic(*stream.get()); agi::line_iterator<std::string> userDic(*stream);
agi::line_iterator<std::string> end; agi::line_iterator<std::string> end;
++userDic; // skip entry count line ++userDic; // skip entry count line
for (; userDic != end; ++userDic) { for (; userDic != end; ++userDic) {
@ -247,4 +253,8 @@ void HunspellSpellChecker::SetLanguage(wxString language) {
} }
} }
void HunspellSpellChecker::OnPathChanged() {
languages.clear();
}
#endif // WITH_HUNSPELL #endif // WITH_HUNSPELL

View File

@ -36,26 +36,28 @@
#ifdef WITH_HUNSPELL #ifdef WITH_HUNSPELL
#include <memory>
#include <hunspell/hunspell.hxx>
#include "include/aegisub/spellchecker.h" #include "include/aegisub/spellchecker.h"
#include <libaegisub/scoped_ptr.h>
#include <libaegisub/signal.h>
namespace agi { namespace agi {
namespace charset { namespace charset {
class IconvWrapper; class IconvWrapper;
} }
} }
class Hunspell;
/// @class HunspellSpellChecker /// @class HunspellSpellChecker
/// @brief Hunspell spell checker /// @brief Hunspell spell checker
/// ///
class HunspellSpellChecker : public SpellChecker { class HunspellSpellChecker : public SpellChecker {
/// Hunspell instance /// Hunspell instance
std::auto_ptr<Hunspell> hunspell; agi::scoped_ptr<Hunspell> hunspell;
/// Conversions between the dictionary charset and utf-8 /// Conversions between the dictionary charset and utf-8
std::auto_ptr<agi::charset::IconvWrapper> conv; agi::scoped_ptr<agi::charset::IconvWrapper> conv;
std::auto_ptr<agi::charset::IconvWrapper> rconv; agi::scoped_ptr<agi::charset::IconvWrapper> rconv;
/// Languages which we have dictionaries for /// Languages which we have dictionaries for
wxArrayString languages; wxArrayString languages;
@ -63,6 +65,16 @@ class HunspellSpellChecker : public SpellChecker {
/// Path to user-local dictionary. /// Path to user-local dictionary.
wxString userDicPath; 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: public:
HunspellSpellChecker(); HunspellSpellChecker();
~HunspellSpellChecker(); ~HunspellSpellChecker();
@ -88,9 +100,6 @@ public:
/// @brief Get a list of languages which dictionaries are present for /// @brief Get a list of languages which dictionaries are present for
wxArrayString GetLanguageList(); wxArrayString GetLanguageList();
/// @brief Set the spellchecker's language
/// @param language Language code
void SetLanguage(wxString language);
}; };
#endif #endif

View File

@ -908,7 +908,6 @@ void SubsTextEditCtrl::OnSetDicLanguage(wxCommandEvent &event) {
if (index >= 0) if (index >= 0)
lang = langs[index]; lang = langs[index];
spellchecker->SetLanguage(lang);
OPT_SET("Tool/Spell Checker/Language")->SetString(STD_STR(lang)); OPT_SET("Tool/Spell Checker/Language")->SetString(STD_STR(lang));
UpdateStyle(); UpdateStyle();