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"
25 #include "frontends/Alert.h"
26 #include "support/lstrings.h"
34 ControlSpellchecker::ControlSpellchecker(LyXView & lv, Dialogs & d)
35 : ControlDialogBD(lv, d),
36 rtl_(false), newval_(0.0), oldval_(0), newvalue_(0), count_(0),
37 stop_(false), speller_(0)
41 void ControlSpellchecker::setParams()
46 // create spell object
49 if (lyxrc.use_pspell) {
50 tmp = (lyxrc.isp_use_alt_lang) ?
51 lyxrc.isp_alt_lang : buffer()->params.language->code();
53 speller_ = new PSpell(buffer()->params, tmp);
56 tmp = (lyxrc.isp_use_alt_lang) ?
57 lyxrc.isp_alt_lang : buffer()->params.language->lang();
59 speller_ = new ISpell(buffer()->params, tmp);
64 if (lyxrc.isp_use_alt_lang) {
65 Language const * lang = languages.getLanguage(tmp);
67 rtl_ = lang->RightToLeft();
69 rtl_ = buffer()->params.language->RightToLeft();
72 if (!speller_->error().empty()) {
73 emergency_exit_ = true;
74 Alert::alert("The spellchecker has failed", speller_->error());
81 void ControlSpellchecker::check()
83 SpellBase::Result res = SpellBase::OK;
86 // clear any old selection
87 LyXText * text = bufferview()->getLyXText();
88 bufferview()->toggleSelection(true);
89 bufferview()->update(text, BufferView::SELECT);
91 while ((res == SpellBase::OK || res == SpellBase::IGNORE) && !stop_) {
92 word_ = bufferview()->nextWord(newval_);
94 if (word_.word().empty()) {
101 // Update slider if and only if value has changed
102 newvalue_ = int(100.0 * newval_);
103 if (newvalue_!= oldval_) {
106 view().partialUpdate(0);
109 if (!speller_->alive()) {
115 res = speller_->check(word_);
118 if (!stop_ && !word_.word().empty())
119 bufferview()->selectLastWord();
122 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
123 view().partialUpdate(1);
128 void ControlSpellchecker::replace(string const & replacement)
130 bufferview()->replaceWord(replacement);
137 void ControlSpellchecker::replaceAll(string const & replacement)
140 replace(replacement);
144 void ControlSpellchecker::insert()
146 speller_->insert(word_);
151 string ControlSpellchecker::getSuggestion()
153 string miss(speller_->nextMiss());
156 std::reverse(miss.begin(), miss.end());
162 string ControlSpellchecker::getWord()
164 string tmp = word_.word();
166 std::reverse(tmp.begin(), tmp.end());
171 void ControlSpellchecker::ignoreAll()
173 speller_->accept(word_);
178 void ControlSpellchecker::stop()
181 bufferview()->endOfSpellCheck();
185 void ControlSpellchecker::clearParams()
190 if (speller_->alive()) {
192 message_ = tostr(count_);
194 message_ += _(" words checked.");
196 message_ += _(" word checked.");
198 message_ = "\n" + message_;
199 message_ = _("Spellchecking completed! ") + message_;
202 message_ = speller_->error();
204 if (message_.empty())
205 message_ = _("The spell checker has died for some reason.\n"
206 "Maybe it has been killed.");
208 // make sure that the dialog is not launched
209 emergency_exit_ = true;
210 Alert::alert("The spellchecker has failed", message_);
215 bufferview()->endOfSpellCheck();
217 // show closing message if any words were checked.
219 view().partialUpdate(2);
221 // reset values to initial