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()
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 (lyxrc.isp_use_alt_lang) {
63 Language const * lang = languages.getLanguage(tmp);
65 rtl_ = lang->RightToLeft();
67 rtl_ = buffer()->params.language->RightToLeft();
70 if (!speller_->error().empty()) {
71 emergency_exit_ = true;
72 Alert::alert("The spellchecker has failed", speller_->error());
80 void ControlSpellchecker::check()
82 SpellBase::Result res = SpellBase::OK;
85 // clear any old selection
86 LyXText * text = bufferview()->getLyXText();
87 bufferview()->toggleSelection(true);
88 bufferview()->update(text, BufferView::SELECT);
90 while ((res == SpellBase::OK || res == SpellBase::IGNORE) && !stop_) {
91 word_ = bufferview()->nextWord(newval_);
93 if (word_.word().empty()) {
100 // Update slider if and only if value has changed
101 newvalue_ = int(100.0 * newval_);
102 if (newvalue_!= oldval_) {
105 view().partialUpdate(0);
108 if (!speller_->alive()) {
114 res = speller_->check(word_);
117 if (!stop_ && !word_.word().empty())
118 bufferview()->selectLastWord();
121 if (res != SpellBase::OK && res != SpellBase::IGNORE) {
122 view().partialUpdate(1);
127 void ControlSpellchecker::replace(string const & replacement)
129 bufferview()->replaceWord(replacement);
136 void ControlSpellchecker::replaceAll(string const & replacement)
139 replace(replacement);
143 void ControlSpellchecker::insert()
145 speller_->insert(word_);
150 string ControlSpellchecker::getSuggestion()
152 string miss(speller_->nextMiss());
155 std::reverse(miss.begin(), miss.end());
161 string ControlSpellchecker::getWord()
163 string tmp = word_.word();
165 std::reverse(tmp.begin(), tmp.end());
170 void ControlSpellchecker::ignoreAll()
172 speller_->accept(word_);
177 void ControlSpellchecker::stop()
180 bufferview()->endOfSpellCheck();
184 void ControlSpellchecker::clearParams()
189 if (speller_->alive()) {
191 message_ = tostr(count_);
193 message_ += _(" words checked.");
195 message_ += _(" word checked.");
197 message_ = "\n" + message_;
198 message_ = _("Spellchecking completed! ") + message_;
201 message_ = speller_->error();
203 if (message_.empty())
204 message_ = _("The spell checker has died for some reason.\n"
205 "Maybe it has been killed.");
207 // make sure that the dialog is not launched
208 emergency_exit_ = true;
209 Alert::alert("The spellchecker has failed", message_);
214 bufferview()->endOfSpellCheck();
216 // show closing message if any words were checked.
218 view().partialUpdate(2);
220 // reset values to initial