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 "BoostFormat.h"
35 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
36 : ControlDialogBD(lv, d),
37 newval_(0.0), oldval_(0), newvalue_(0), count_(0),
38 stop_(false), speller_(0)
42 void ControlSpellchecker::setParams()
47 // create spell object
50 if (lyxrc.use_pspell) {
51 tmp = (lyxrc.isp_use_alt_lang) ?
52 lyxrc.isp_alt_lang : buffer()->params.language->code();
54 speller_ = new PSpell(buffer()->params, tmp);
57 tmp = (lyxrc.isp_use_alt_lang) ?
58 lyxrc.isp_alt_lang : buffer()->params.language->lang();
60 speller_ = new ISpell(buffer()->params, tmp);
65 if (!speller_->error().empty()) {
66 emergency_exit_ = true;
67 Alert::alert("The spellchecker has failed", speller_->error());
74 void ControlSpellchecker::check()
76 SpellBase::Result res = SpellBase::OK;
79 // clear any old selection
80 LyXText * text = bufferview()->getLyXText();
81 bufferview()->toggleSelection(true);
82 bufferview()->update(text, BufferView::SELECT);
84 while ((res == SpellBase::OK || res == SpellBase::IGNORE) && !stop_) {
85 word_ = bufferview()->nextWord(newval_);
87 if (word_.word().empty()) {
94 // Update slider if and only if value has changed
95 newvalue_ = int(100.0 * newval_);
96 if (newvalue_!= oldval_) {
99 view().partialUpdate(0);
102 if (!speller_ || !speller_->alive()) {
108 res = speller_->check(word_);
111 if (!stop_ && !word_.word().empty())
112 bufferview()->selectLastWord();
115 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
116 view().partialUpdate(1);
121 void ControlSpellchecker::replace(string const & replacement)
123 bufferview()->replaceWord(replacement);
130 void ControlSpellchecker::replaceAll(string const & replacement)
133 replace(replacement);
137 void ControlSpellchecker::insert()
139 speller_->insert(word_);
144 string const ControlSpellchecker::getSuggestion() const
146 return speller_->nextMiss();
150 string const ControlSpellchecker::getWord() const
156 void ControlSpellchecker::ignoreAll()
158 speller_->accept(word_);
163 void ControlSpellchecker::stop()
166 bufferview()->endOfSpellCheck();
170 void ControlSpellchecker::clearParams()
175 if (speller_->alive()) {
178 message_ = string(_("Spellchecking completed!")) + '\n';
182 boost::format fmter("%1$d words checked.");
184 message_ += fmter.str();
186 message_ += _("One word checked.");
190 message_ += tostr(count_) + " words checked";
192 message_ = _("One word checked.");
196 message_ = speller_->error();
198 if (message_.empty())
199 message_ = _("The spell checker has died for some reason.\n"
200 "Maybe it has been killed.");
202 // make sure that the dialog is not launched
203 emergency_exit_ = true;
204 Alert::alert("The spellchecker has failed", message_);
209 bufferview()->endOfSpellCheck();
211 // show closing message if any words were checked.
213 view().partialUpdate(2);
215 // reset values to initial