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 #include "ControlSpellchecker.h"
17 #include "BufferView.h"
28 #include "frontends/Alert.h"
30 #include "BoostFormat.h"
32 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
33 : ControlDialogBD(lv, d),
34 newval_(0.0), oldval_(0), newvalue_(0), count_(0),
35 stop_(false), speller_(0)
39 void ControlSpellchecker::setParams()
44 // create spell object
47 if (lyxrc.use_pspell) {
48 tmp = (lyxrc.isp_use_alt_lang) ?
49 lyxrc.isp_alt_lang : buffer()->params.language->code();
51 speller_ = new PSpell(buffer()->params, tmp);
54 tmp = (lyxrc.isp_use_alt_lang) ?
55 lyxrc.isp_alt_lang : buffer()->params.language->lang();
57 speller_ = new ISpell(buffer()->params, tmp);
62 if (!speller_->error().empty()) {
63 emergency_exit_ = true;
64 Alert::alert("The spellchecker has failed", speller_->error());
71 void ControlSpellchecker::check()
73 SpellBase::Result res = SpellBase::OK;
76 // clear any old selection
77 LyXText * text = bufferview()->getLyXText();
78 bufferview()->toggleSelection(true);
79 bufferview()->update(text, BufferView::SELECT);
81 while ((res == SpellBase::OK || res == SpellBase::IGNORE) && !stop_) {
82 word_ = bufferview()->nextWord(newval_);
84 if (word_.word().empty()) {
91 // Update slider if and only if value has changed
92 newvalue_ = int(100.0 * newval_);
93 if (newvalue_!= oldval_) {
96 view().partialUpdate(0);
99 if (!speller_ || !speller_->alive()) {
105 res = speller_->check(word_);
108 if (!stop_ && !word_.word().empty())
109 bufferview()->selectLastWord();
112 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
113 view().partialUpdate(1);
118 void ControlSpellchecker::replace(string const & replacement)
120 bufferview()->replaceWord(replacement);
127 void ControlSpellchecker::replaceAll(string const & replacement)
130 replace(replacement);
134 void ControlSpellchecker::insert()
136 speller_->insert(word_);
141 string const ControlSpellchecker::getSuggestion() const
143 return speller_->nextMiss();
147 string const ControlSpellchecker::getWord() const
153 void ControlSpellchecker::ignoreAll()
155 speller_->accept(word_);
160 void ControlSpellchecker::stop()
163 bufferview()->endOfSpellCheck();
167 void ControlSpellchecker::clearParams()
172 if (speller_->alive()) {
175 message_ = string(_("Spellchecking completed!")) + '\n';
179 boost::format fmter("%1$d words checked.");
181 message_ += fmter.str();
183 message_ += _("One word checked.");
187 message_ += tostr(count_) + " words checked";
189 message_ = _("One word checked.");
193 message_ = speller_->error();
195 if (message_.empty())
196 message_ = _("The spell checker has died for some reason.\n"
197 "Maybe it has been killed.");
199 // make sure that the dialog is not launched
200 emergency_exit_ = true;
201 Alert::alert("The spellchecker has failed", message_);
206 bufferview()->endOfSpellCheck();
208 // show closing message if any words were checked.
210 view().partialUpdate(2);
212 // reset values to initial