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