]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormSpellchecker.C
Remove redundant files.
[lyx.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"))
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::update()
81 {
82         // clear input fields
83         fl_set_input(dialog_->input_replacement, "");
84         fl_set_object_label(dialog_->text_unknown, "");
85         fl_clear_browser(dialog_->browser_suggestions);
86
87         // reset dialog and buttons into start condition
88         input(0, 0);
89
90         // reset slider to zero count
91         fl_set_slider_value(dialog_->slider_progress, 0.0);
92         fl_set_object_label(dialog_->slider_progress, "0 %");
93 }
94
95
96 ButtonPolicy::SMInput FormSpellchecker::input(FL_OBJECT * ob, long ob_value)
97 {
98         if (!ob || ob == dialog_->button_start) {
99                 static bool running = false;
100
101                 // update running status of spellingchecker
102                 running = !running && ob == dialog_->button_start;
103
104                 // modify text of Start/Stop button according to running status
105                 string const labeltext = running ? _("Stop") : _("Start");
106                 fl_set_object_label(dialog_->button_start, labeltext.c_str());  
107                 fl_set_button_shortcut(dialog_->button_start, "#S", 1);
108                 fl_show_object(dialog_->button_start);
109
110                 // adjust tooltips to modified Start/Stop button
111                 string const str = (running ? _("Stop the spellingchecker.") :
112                                         _("Start the spellingchecker."));
113                 tooltips().init(dialog_->button_start, str);
114
115                 // enable buttons according to running status
116                 setEnabled(dialog_->button_replace, running);
117                 setEnabled(dialog_->button_ignore, running);
118                 setEnabled(dialog_->button_accept, running);
119                 setEnabled(dialog_->button_add, running);
120                 setEnabled(dialog_->browser_suggestions, running);
121                 setEnabled(dialog_->input_replacement, running);
122
123                 // call controller if Start/Stop button is pressed
124                 if (ob) {
125                         if (running)
126                                 controller().check();
127                         else
128                                 controller().stop();
129                 }
130
131         } else if (ob == dialog_->button_replace) {
132                 string const tmp = getString(dialog_->input_replacement);
133                 controller().replace(tmp);
134
135         } else if (ob == dialog_->button_ignore) {
136                 controller().check();
137
138         } else if (ob == dialog_->button_accept) {
139                 controller().ignoreAll();
140
141         } else if (ob == dialog_->button_add) {
142                 controller().insert();
143
144         } else if (ob == dialog_->browser_suggestions) {
145                 string const tmp = getString(dialog_->browser_suggestions);
146                 if (tmp.empty())
147                         return ButtonPolicy::SMI_NOOP;
148
149                 if (ob_value != 2) {
150                         // single-click
151                         // place the chosen string in the input as feedback
152                         fl_set_input(dialog_->input_replacement, tmp.c_str());
153
154                 } else {
155                         // double-click
156                         controller().replace(tmp);
157                         // reset the browser so that the following
158                         // single-click callback doesn't do anything
159                         fl_deselect_browser(dialog_->browser_suggestions);
160                 }
161         }
162
163         // update slider with word count and progress
164         int const progress = controller().getProgress();
165         if (progress > 0) {
166                 double const wordcount = controller().getCount();
167                 double const total = 100.0 * wordcount / progress;
168                 string const label = tostr(progress) + " %";
169                 fl_set_slider_bounds(dialog_->slider_progress, 0.0, total);
170                 fl_set_slider_value(dialog_->slider_progress, wordcount);
171                 fl_set_object_label(dialog_->slider_progress, label.c_str());
172         }
173
174         return ButtonPolicy::SMI_VALID;
175 }
176
177
178 void FormSpellchecker::partialUpdate(int id)
179 {
180         switch (id) {
181         case 1: // set suggestions
182         {
183                 string w = controller().getWord();
184                 fl_set_input(dialog_->input_replacement, w.c_str());
185                 fl_set_object_label(dialog_->text_unknown, w.c_str());
186                 fl_clear_browser(dialog_->browser_suggestions);
187                 while (!(w = controller().getSuggestion()).empty()) {
188                         fl_add_browser_line(dialog_->browser_suggestions, w.c_str());
189                 }
190         }
191         break;
192         case 2: // end of spell checking
193
194                 // reset dialog and buttons into start condition
195                 input(0, 0);
196
197                 // set slider 'finished' status
198                 fl_set_slider_bounds(dialog_->slider_progress, 0.0, controller().getCount());
199                 fl_set_slider_value(dialog_->slider_progress, controller().getCount());
200                 fl_set_object_label(dialog_->slider_progress, "100 %");
201
202                 break;
203         }
204 }