]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormSpellchecker.C
Convert the spellchecker dialog to the Dialog-based scheme.
[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 #include "FormSpellchecker.h"
14 #include "forms/form_spellchecker.h"
15
16 #include "controllers/ControlSpellchecker.h"
17
18 #include "Tooltips.h"
19 #include "xforms_helpers.h"
20 #include "xformsBC.h"
21
22 #include "support/tostr.h"
23
24 #include "lyx_forms.h"
25
26
27 using std::string;
28
29
30 typedef FormController<ControlSpellchecker, FormView<FD_spellchecker> > base_class;
31
32 FormSpellchecker::FormSpellchecker(Dialog & parent)
33         : base_class(parent, _("Spell-check document"))
34 {}
35
36
37 void FormSpellchecker::build()
38 {
39         dialog_.reset(build_spellchecker(this));
40
41         // Manage the buttons
42         bcview().setCancel(dialog_->button_close);
43
44         // disable for read-only documents
45         bcview().addReadOnly(dialog_->button_replace);
46
47         // trigger an input event for cut&paste with middle mouse button.
48         setPrehandler(dialog_->input_replacement);
49
50         fl_set_input_return(dialog_->input_replacement, FL_RETURN_CHANGED);
51
52         // callback for double click in browser
53         fl_set_browser_dblclick_callback(dialog_->browser_suggestions,
54                                          C_FormDialogView_InputCB, 2);
55
56         // do not allow setting of slider by the mouse
57         fl_deactivate_object(dialog_->slider_progress);
58
59         // set up the tooltips
60         string str = _("Type replacement for unknown word "
61                         "or select from suggestions.");
62         tooltips().init(dialog_->input_replacement, str);
63         str = _("List of replacement suggestions from dictionary.");
64         tooltips().init(dialog_->browser_suggestions, str);
65         // Work-around xforms' bug; enable tooltips for browser widgets.
66         setPrehandler(dialog_->browser_suggestions);
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         controller().check();
83 }
84
85
86 void FormSpellchecker::partialUpdate(int s)
87 {
88         ControlSpellchecker::State const state =
89                 static_cast<ControlSpellchecker::State>(s);
90
91         switch (state) {
92
93         case ControlSpellchecker::SPELL_FOUND_WORD: {
94                 // Set suggestions.
95                 string w = controller().getWord();
96                 fl_set_input(dialog_->input_replacement, w.c_str());
97                 fl_set_object_label(dialog_->text_unknown, w.c_str());
98                 fl_clear_browser(dialog_->browser_suggestions);
99                 while (!(w = controller().getSuggestion()).empty()) {
100                         fl_add_browser_line(dialog_->browser_suggestions,
101                                             w.c_str());
102                 }
103                 // Fall through...
104         }
105
106         case ControlSpellchecker::SPELL_PROGRESSED: {
107                 int const progress = controller().getProgress();
108                 if (progress == 0)
109                         break;
110
111                 double const wordcount = controller().getCount();
112                 double const total = 100.0 * wordcount / progress;
113                 string const label = tostr(progress) + " %";
114
115                 fl_set_slider_bounds(dialog_->slider_progress, 0.0, total);
116                 fl_set_slider_value(dialog_->slider_progress, wordcount);
117                 fl_set_object_label(dialog_->slider_progress, label.c_str());
118                 fl_redraw_object(dialog_->slider_progress);
119                 break;
120         }
121
122         }
123 }
124
125
126 ButtonPolicy::SMInput FormSpellchecker::input(FL_OBJECT * ob, long ob_value)
127 {
128         if (ob == dialog_->button_replace) {
129                 string const tmp = getString(dialog_->input_replacement);
130                 controller().replace(tmp);
131
132         } else if (ob == dialog_->button_ignore) {
133                 controller().check();
134
135         } else if (ob == dialog_->button_accept) {
136                 controller().ignoreAll();
137
138         } else if (ob == dialog_->button_add) {
139                 controller().insert();
140
141         } else if (ob == dialog_->browser_suggestions) {
142                 string const tmp = getString(dialog_->browser_suggestions);
143                 if (tmp.empty())
144                         return ButtonPolicy::SMI_NOOP;
145
146                 if (ob_value != 2) {
147                         // single-click
148                         // place the chosen string in the input as feedback
149                         fl_set_input(dialog_->input_replacement, tmp.c_str());
150
151                 } else {
152                         // double-click
153                         controller().replace(tmp);
154                         // reset the browser so that the following
155                         // single-click callback doesn't do anything
156                         fl_deselect_browser(dialog_->browser_suggestions);
157                 }
158         }
159
160         return ButtonPolicy::SMI_VALID;
161 }