2 * \file ControlSpellchecker.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
14 #pragma implementation
17 #include "ControlSpellchecker.h"
20 #include "BufferView.h"
31 #include "frontends/Alert.h"
33 #include "support/lstrings.h"
35 #include "BoostFormat.h"
37 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
38 : ControlDialogBD(lv, d),
39 rtl_(false), newval_(0.0), oldval_(0), newvalue_(0), count_(0),
40 stop_(false), speller_(0)
44 void ControlSpellchecker::setParams()
49 // create spell object
52 if (lyxrc.use_pspell) {
53 tmp = (lyxrc.isp_use_alt_lang) ?
54 lyxrc.isp_alt_lang : buffer()->params.language->code();
56 speller_ = new PSpell(buffer()->params, tmp);
59 tmp = (lyxrc.isp_use_alt_lang) ?
60 lyxrc.isp_alt_lang : buffer()->params.language->lang();
62 speller_ = new ISpell(buffer()->params, tmp);
67 if (lyxrc.isp_use_alt_lang) {
68 Language const * lang = languages.getLanguage(tmp);
70 rtl_ = lang->RightToLeft();
72 rtl_ = buffer()->params.language->RightToLeft();
75 if (!speller_->error().empty()) {
76 emergency_exit_ = true;
77 Alert::alert("The spellchecker has failed", speller_->error());
84 void ControlSpellchecker::check()
86 SpellBase::Result res = SpellBase::OK;
89 // clear any old selection
90 LyXText * text = bufferview()->getLyXText();
91 bufferview()->toggleSelection(true);
92 bufferview()->update(text, BufferView::SELECT);
94 while ((res == SpellBase::OK || res == SpellBase::IGNORE) && !stop_) {
95 word_ = bufferview()->nextWord(newval_);
97 if (word_.word().empty()) {
104 // Update slider if and only if value has changed
105 newvalue_ = int(100.0 * newval_);
106 if (newvalue_!= oldval_) {
109 view().partialUpdate(0);
112 if (!speller_ || !speller_->alive()) {
118 res = speller_->check(word_);
121 if (!stop_ && !word_.word().empty())
122 bufferview()->selectLastWord();
125 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
126 view().partialUpdate(1);
131 void ControlSpellchecker::replace(string const & replacement)
133 bufferview()->replaceWord(replacement);
140 void ControlSpellchecker::replaceAll(string const & replacement)
143 replace(replacement);
147 void ControlSpellchecker::insert()
149 speller_->insert(word_);
154 string const ControlSpellchecker::getSuggestion() const
156 string miss(speller_->nextMiss());
159 std::reverse(miss.begin(), miss.end());
165 string const ControlSpellchecker::getWord() const
167 string tmp = word_.word();
169 std::reverse(tmp.begin(), tmp.end());
174 void ControlSpellchecker::ignoreAll()
176 speller_->accept(word_);
181 void ControlSpellchecker::stop()
184 bufferview()->endOfSpellCheck();
188 void ControlSpellchecker::clearParams()
193 if (speller_->alive()) {
196 message_ = string(_("Spellchecking completed!")) + '\n';
200 boost::format fmter("%1$d words checked.");
202 message_ += fmter.str();
204 message_ += _("One word checked.");
208 message_ += tostr(count_) + " words checked";
210 message_ = _("One word checked.");
214 message_ = speller_->error();
216 if (message_.empty())
217 message_ = _("The spell checker has died for some reason.\n"
218 "Maybe it has been killed.");
220 // make sure that the dialog is not launched
221 emergency_exit_ = true;
222 Alert::alert("The spellchecker has failed", message_);
227 bufferview()->endOfSpellCheck();
229 // show closing message if any words were checked.
231 view().partialUpdate(2);
233 // reset values to initial