]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlSpellchecker.C
Remove XForms RTL hacks.
[lyx.git] / src / frontends / controllers / ControlSpellchecker.C
1 /**
2  * \file ControlSpellchecker.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "ControlSpellchecker.h"
18 #include "ViewBase.h"
19 #include "buffer.h"
20 #include "BufferView.h"
21 #include "gettext.h"
22 #include "language.h"
23 #include "lyxrc.h"
24 #include "lyxtext.h"
25
26 #include "ispell.h"
27 #ifdef USE_PSPELL
28 # include "pspell.h"
29 #endif
30
31 #include "frontends/Alert.h"
32
33 #include "BoostFormat.h"
34
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)
39 {}
40
41
42 void ControlSpellchecker::setParams()
43 {
44         if (speller_)
45                 return;
46
47         // create spell object
48         string tmp;
49 #ifdef USE_PSPELL
50         if (lyxrc.use_pspell) {
51                 tmp = (lyxrc.isp_use_alt_lang) ?
52                         lyxrc.isp_alt_lang : buffer()->params.language->code();
53
54                 speller_ = new PSpell(buffer()->params, tmp);
55         } else {
56 #endif
57                 tmp = (lyxrc.isp_use_alt_lang) ?
58                         lyxrc.isp_alt_lang : buffer()->params.language->lang();
59
60                 speller_ = new ISpell(buffer()->params, tmp);
61 #ifdef USE_PSPELL
62         }
63 #endif
64
65         if (!speller_->error().empty()) {
66                 emergency_exit_ = true;
67                 Alert::alert("The spellchecker has failed", speller_->error());
68                 clearParams();
69                 return;
70         }
71 }
72
73
74 void ControlSpellchecker::check()
75 {
76         SpellBase::Result res = SpellBase::OK;
77         stop_ = false;
78
79         // clear any old selection
80         LyXText * text = bufferview()->getLyXText();
81         bufferview()->toggleSelection(true);
82         bufferview()->update(text, BufferView::SELECT);
83
84         while ((res == SpellBase::OK || res == SpellBase::IGNORE) && !stop_) {
85                 word_ = bufferview()->nextWord(newval_);
86
87                 if (word_.word().empty()) {
88                         clearParams();
89                         break;
90                 }
91
92                 ++count_;
93
94                 // Update slider if and only if value has changed
95                 newvalue_ = int(100.0 * newval_);
96                 if (newvalue_!= oldval_) {
97                         oldval_ = newvalue_;
98                         // set progress bar
99                         view().partialUpdate(0);
100                 }
101
102                 if (!speller_ || !speller_->alive()) {
103                         clearParams();
104                         stop();
105                         return;
106                 }
107
108                 res = speller_->check(word_);
109         }
110
111         if (!stop_ && !word_.word().empty())
112                 bufferview()->selectLastWord();
113
114         // set suggestions
115         if (res != SpellBase::OK && res != SpellBase::IGNORE) {
116                 view().partialUpdate(1);
117         }
118 }
119
120
121 void ControlSpellchecker::replace(string const & replacement)
122 {
123         bufferview()->replaceWord(replacement);
124         // fix up the count
125         --count_;
126         check();
127 }
128
129
130 void ControlSpellchecker::replaceAll(string const & replacement)
131 {
132         // TODO: add to list
133         replace(replacement);
134 }
135
136
137 void ControlSpellchecker::insert()
138 {
139         speller_->insert(word_);
140         check();
141 }
142
143
144 string const ControlSpellchecker::getSuggestion() const
145 {
146         return speller_->nextMiss();
147 }
148
149
150 string const ControlSpellchecker::getWord() const
151 {
152         return word_.word();
153 }
154
155
156 void ControlSpellchecker::ignoreAll()
157 {
158         speller_->accept(word_);
159         check();
160 }
161
162
163 void ControlSpellchecker::stop()
164 {
165         stop_ = true;
166         bufferview()->endOfSpellCheck();
167 }
168
169
170 void ControlSpellchecker::clearParams()
171 {
172         if (!speller_)
173                 return;
174
175         if (speller_->alive()) {
176                 speller_->close();
177
178                 message_ = string(_("Spellchecking completed!")) + '\n';
179
180 #if USE_BOOST_FORMAT
181                 if (count_ != 1) {
182                 boost::format fmter("%1$d words checked.");
183                 fmter % count_;
184                 message_ += fmter.str();
185                 } else {
186                         message_ += _("One word checked.");
187                 }
188 #else
189                 if (count_ != 1) {
190                         message_ += tostr(count_) + " words checked";
191                 } else {
192                         message_ = _("One word checked.");
193                 }
194 #endif
195         } else {
196                 message_ = speller_->error();
197                 speller_->cleanUp();
198                 if (message_.empty())
199                     message_ = _("The spell checker has died for some reason.\n"
200                                  "Maybe it has been killed.");
201
202                 // make sure that the dialog is not launched
203                 emergency_exit_ = true;
204                 Alert::alert("The spellchecker has failed", message_);
205         }
206
207         delete speller_;
208
209         bufferview()->endOfSpellCheck();
210
211         // show closing message if any words were checked.
212         if (count_ > 0)
213                 view().partialUpdate(2);
214
215         // reset values to initial
216         newval_ = 0.0;
217         oldval_ = 0;
218         newvalue_ = 0;
219         count_ = 0;
220         message_.erase();
221         stop_ = false;
222         speller_ = 0;
223 }