]> git.lyx.org Git - features.git/blob - src/frontends/xforms/FormSpellchecker.C
Use of $? in shell scripts is unnecessary apparently.
[features.git] / src / frontends / xforms / FormSpellchecker.C
1 /**
2  * \file FormSpellchecker.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 "Tooltips.h"
18 #include "xformsBC.h"
19 #include "xforms_helpers.h"
20 #include "ControlSpellchecker.h"
21 #include "FormSpellchecker.h"
22 #include "forms/form_spellchecker.h"
23 #include "support/lstrings.h"
24
25 #include FORMS_H_LOCATION
26
27 typedef FormCB<ControlSpellchecker, FormDB<FD_spellchecker> > base_class;
28
29
30 FormSpellchecker::FormSpellchecker()
31         : base_class(_("Spellchecker")), state_(STOP)
32 {}
33
34
35 void FormSpellchecker::build()
36 {
37         dialog_.reset(build_spellchecker(this));
38
39         // Manage the buttons
40         bc().setCancel(dialog_->button_close);
41
42         // disable for read-only documents
43         bc().addReadOnly(dialog_->button_replace);
44
45         // trigger an input event for cut&paste with middle mouse button.
46         setPrehandler(dialog_->input_replacement);
47
48         fl_set_input_return(dialog_->input_replacement, FL_RETURN_CHANGED);
49
50         // callback for double click in browser
51         fl_set_browser_dblclick_callback(dialog_->browser_suggestions,
52                                          C_FormBaseInputCB, 2);
53
54         // do not allow setting of slider by the mouse
55         fl_deactivate_object(dialog_->slider_progress);
56
57         // set up the tooltips
58         string str = _("Type replacement for unknown word "
59                         " or select from suggestions.");
60         tooltips().init(dialog_->input_replacement, str);
61         str = _("List of replacement suggestions from dictionary.");
62         tooltips().init(dialog_->browser_suggestions, str);
63         // Work-around xforms' bug; enable tooltips for browser widgets.
64         setPrehandler(dialog_->browser_suggestions);
65         str = _("Start the spellingchecker.");
66         tooltips().init(dialog_->button_start, str);
67         str = _("Replace unknown word.");
68         tooltips().init(dialog_->button_replace, str);
69         str = _("Ignore unknown word.");
70         tooltips().init(dialog_->button_ignore, str);
71         str = _("Accept unknown word as known in this session.");
72         tooltips().init(dialog_->button_accept, str);
73         str = _("Add unknown word to personal dictionary.");
74         tooltips().init(dialog_->button_add, str);
75         str = _("Shows word count and progress on spell check.");
76         tooltips().init(dialog_->slider_progress, str);
77 }
78
79
80 void FormSpellchecker::updateState(State state)
81 {
82         switch (state) {
83         case START:
84                 fl_set_slider_value(dialog_->slider_progress, 0.0);
85                 fl_set_object_label(dialog_->slider_progress, "0 %");
86                 break;
87
88         case RUNNING: 
89         {
90                 controller().check();
91
92                 int const progress = controller().getProgress();
93                 if (progress == 0)
94                         break;
95
96                 double const wordcount = controller().getCount();
97                 double const total = 100.0 * wordcount / progress;
98                 string const label = tostr(progress) + " %";
99
100                 fl_set_slider_bounds(dialog_->slider_progress, 0.0, total);
101                 fl_set_slider_value(dialog_->slider_progress, wordcount);
102                 fl_set_object_label(dialog_->slider_progress, label.c_str());
103                 break;
104         }
105
106         case STOP: 
107         {
108                 controller().stop();
109
110                 double const wordcount = controller().getCount();
111
112                 // set slider 'finished' status
113                 fl_set_slider_bounds(dialog_->slider_progress, 0.0, wordcount);
114                 fl_set_slider_value(dialog_->slider_progress, wordcount);
115                 fl_set_object_label(dialog_->slider_progress, "100 %");
116                 break;
117         }
118         }
119
120         bool const state_change = state_ != state;
121         state_ = state;
122
123         if (!state_change)
124                 return;
125
126         bool const set_running = (state == RUNNING);
127         string const label = set_running ? _("Stop") : _("Start");
128         
129         fl_set_object_label(dialog_->button_start, label.c_str());      
130         fl_set_button_shortcut(dialog_->button_start, "#S", 1);
131         fl_redraw_object(dialog_->button_start);
132
133         string const tip = set_running ?
134                 _("Stop the spellingchecker.") :
135                 _("Start the spellingchecker.");
136         tooltips().init(dialog_->button_start, tip);
137         
138         setEnabled(dialog_->button_replace,      set_running);
139         setEnabled(dialog_->button_ignore,       set_running);
140         setEnabled(dialog_->button_accept,       set_running);
141         setEnabled(dialog_->button_add,          set_running);
142         setEnabled(dialog_->browser_suggestions, set_running);
143         setEnabled(dialog_->input_replacement,   set_running);
144 }
145
146
147 void FormSpellchecker::update()
148 {
149         // clear input fields
150         fl_set_input(dialog_->input_replacement, "");
151         fl_set_object_label(dialog_->text_unknown, "");
152         fl_clear_browser(dialog_->browser_suggestions);
153
154         // reset dialog and buttons into start condition
155         updateState(START);
156 }
157
158
159 ButtonPolicy::SMInput FormSpellchecker::input(FL_OBJECT * ob, long ob_value)
160 {
161         if (ob == dialog_->button_start) {
162                 updateState(RUNNING);
163
164         } else if (ob == dialog_->button_replace) {
165                 string const tmp = getString(dialog_->input_replacement);
166                 controller().replace(tmp);
167
168         } else if (ob == dialog_->button_ignore) {
169                 controller().check();
170
171         } else if (ob == dialog_->button_accept) {
172                 controller().ignoreAll();
173
174         } else if (ob == dialog_->button_add) {
175                 controller().insert();
176
177         } else if (ob == dialog_->browser_suggestions) {
178                 string const tmp = getString(dialog_->browser_suggestions);
179                 if (tmp.empty())
180                         return ButtonPolicy::SMI_NOOP;
181
182                 if (ob_value != 2) {
183                         // single-click
184                         // place the chosen string in the input as feedback
185                         fl_set_input(dialog_->input_replacement, tmp.c_str());
186
187                 } else {
188                         // double-click
189                         controller().replace(tmp);
190                         // reset the browser so that the following
191                         // single-click callback doesn't do anything
192                         fl_deselect_browser(dialog_->browser_suggestions);
193                 }
194         }
195
196         return ButtonPolicy::SMI_VALID;
197 }
198
199
200 void FormSpellchecker::partialUpdate(int id)
201 {
202         switch (id) {
203         case 1:
204         {
205                 // Set suggestions.
206                 string w = controller().getWord();
207                 fl_set_input(dialog_->input_replacement, w.c_str());
208                 fl_set_object_label(dialog_->text_unknown, w.c_str());
209                 fl_clear_browser(dialog_->browser_suggestions);
210                 while (!(w = controller().getSuggestion()).empty()) {
211                         fl_add_browser_line(dialog_->browser_suggestions,
212                                             w.c_str());
213                 }
214                 break;
215         }
216         case 2:
217                 // End of spell checking.
218                 updateState(STOP);
219                 break;
220         }
221 }