2 * \file FormSpellchecker.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
19 #include "xforms_helpers.h"
20 #include "ControlSpellchecker.h"
21 #include "FormSpellchecker.h"
22 #include "forms/form_spellchecker.h"
23 #include "support/lstrings.h"
25 #include FORMS_H_LOCATION
27 typedef FormCB<ControlSpellchecker, FormDB<FD_spellchecker> > base_class;
30 FormSpellchecker::FormSpellchecker()
31 : base_class(_("Spellchecker"))
35 void FormSpellchecker::build()
37 dialog_.reset(build_spellchecker(this));
40 bc().setCancel(dialog_->button_close);
42 // disable for read-only documents
43 bc().addReadOnly(dialog_->button_replace);
45 // trigger an input event for cut&paste with middle mouse button.
46 setPrehandler(dialog_->input_replacement);
48 fl_set_input_return(dialog_->input_replacement, FL_RETURN_CHANGED);
50 // callback for double click in browser
51 fl_set_browser_dblclick_callback(dialog_->browser_suggestions,
52 C_FormBaseInputCB, 2);
54 // do not allow setting of slider by the mouse
55 fl_deactivate_object(dialog_->slider_progress);
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 str = _("Start the spellingchecker.");
64 tooltips().init(dialog_->button_start, str);
65 str = _("Replace unknown word.");
66 tooltips().init(dialog_->button_replace, str);
67 str = _("Ignore unknown word.");
68 tooltips().init(dialog_->button_ignore, str);
69 str = _("Accept unknown word as known in this session.");
70 tooltips().init(dialog_->button_accept, str);
71 str = _("Add unknown word to personal dictionary.");
72 tooltips().init(dialog_->button_add, str);
73 str = _("Shows word count and progress on spell check.");
74 tooltips().init(dialog_->slider_progress, str);
78 void FormSpellchecker::update()
81 fl_set_input(dialog_->input_replacement, "");
82 fl_set_object_label(dialog_->text_unknown, "");
83 fl_clear_browser(dialog_->browser_suggestions);
85 // reset dialog and buttons into start condition
88 // reset slider to zero count
89 fl_set_slider_value(dialog_->slider_progress, 0.0);
90 fl_set_object_label(dialog_->slider_progress, "0 %");
94 ButtonPolicy::SMInput FormSpellchecker::input(FL_OBJECT * ob, long ob_value)
96 if (!ob || ob == dialog_->button_start) {
97 static bool running = false;
99 // update running status of spellingchecker
100 running = !running && ob == dialog_->button_start;
102 // modify text of Start/Stop button according to running status
103 string const labeltext = running ? _("Stop") : _("Start");
104 fl_set_object_label(dialog_->button_start, labeltext.c_str());
105 fl_set_button_shortcut(dialog_->button_start, "#S", 1);
106 fl_show_object(dialog_->button_start);
108 // adjust tooltips to modified Start/Stop button
109 string const str = (running ? _("Stop the spellingchecker.") :
110 _("Start the spellingchecker."));
111 tooltips().init(dialog_->button_start, str);
113 // enable buttons according to running status
114 setEnabled(dialog_->button_replace, running);
115 setEnabled(dialog_->button_ignore, running);
116 setEnabled(dialog_->button_accept, running);
117 setEnabled(dialog_->button_add, running);
118 setEnabled(dialog_->browser_suggestions, running);
119 setEnabled(dialog_->input_replacement, running);
121 // call controller if Start/Stop button is pressed
124 controller().check();
129 } else if (ob == dialog_->button_replace) {
130 string const tmp = getString(dialog_->input_replacement);
131 controller().replace(tmp);
133 } else if (ob == dialog_->button_ignore) {
134 controller().check();
136 } else if (ob == dialog_->button_accept) {
137 controller().ignoreAll();
139 } else if (ob == dialog_->button_add) {
140 controller().insert();
142 } else if (ob == dialog_->browser_suggestions) {
143 string const tmp = getString(dialog_->browser_suggestions);
145 return ButtonPolicy::SMI_NOOP;
149 // place the chosen string in the input as feedback
150 fl_set_input(dialog_->input_replacement, tmp.c_str());
154 controller().replace(tmp);
155 // reset the browser so that the following
156 // single-click callback doesn't do anything
157 fl_deselect_browser(dialog_->browser_suggestions);
161 // update slider with word count and progress
162 double const wordcount = controller().getCount();
163 if (wordcount > 0.0) {
164 int const progress = controller().getProgress();
165 double const total = 100.0 * wordcount / progress;
167 fl_set_slider_bounds(dialog_->slider_progress, 0.0, total);
168 fl_set_slider_value(dialog_->slider_progress, wordcount);
169 fl_set_object_label(dialog_->slider_progress,
170 (tostr(progress) + " %").c_str());
173 return ButtonPolicy::SMI_VALID;
177 void FormSpellchecker::partialUpdate(int id)
180 case 1: // set suggestions
182 string w = controller().getWord();
183 fl_set_input(dialog_->input_replacement, w.c_str());
184 fl_set_object_label(dialog_->text_unknown, w.c_str());
185 fl_clear_browser(dialog_->browser_suggestions);
186 while (!(w = controller().getSuggestion()).empty()) {
187 fl_add_browser_line(dialog_->browser_suggestions, w.c_str());
191 case 2: // end of spell checking
193 // reset dialog and buttons into start condition
196 // set slider 'finished' status
197 fl_set_slider_bounds(dialog_->slider_progress, 0.0, controller().getCount());
198 fl_set_slider_value(dialog_->slider_progress, controller().getCount());
199 fl_set_object_label(dialog_->slider_progress, "100 %");