]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormSpellchecker.C
Change glob() API to accept a dir parameter.
[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 using std::string;
27
28 namespace lyx {
29 namespace frontend {
30
31 typedef FormController<ControlSpellchecker, FormView<FD_spellchecker> > base_class;
32
33 FormSpellchecker::FormSpellchecker(Dialog & parent)
34         : base_class(parent, _("Spell-check document"))
35 {}
36
37
38 void FormSpellchecker::build()
39 {
40         dialog_.reset(build_spellchecker(this));
41
42         // Manage the buttons
43         bcview().setCancel(dialog_->button_close);
44
45         // disable for read-only documents
46         bcview().addReadOnly(dialog_->button_replace);
47
48         // trigger an input event for cut&paste with middle mouse button.
49         setPrehandler(dialog_->input_replacement);
50
51         fl_set_input_return(dialog_->input_replacement, FL_RETURN_CHANGED);
52
53         // callback for double click in browser
54         fl_set_browser_dblclick_callback(dialog_->browser_suggestions,
55                                          C_FormDialogView_InputCB, 2);
56
57         // do not allow setting of slider by the mouse
58         fl_deactivate_object(dialog_->slider_progress);
59
60         // set up the tooltips
61         string str = _("Type replacement for unknown word "
62                         "or select from suggestions.");
63         tooltips().init(dialog_->input_replacement, str);
64         str = _("List of replacement suggestions from dictionary.");
65         tooltips().init(dialog_->browser_suggestions, str);
66         // Work-around xforms' bug; enable tooltips for browser widgets.
67         setPrehandler(dialog_->browser_suggestions);
68         str = _("Replace unknown word.");
69         tooltips().init(dialog_->button_replace, str);
70         str = _("Ignore unknown word.");
71         tooltips().init(dialog_->button_ignore, str);
72         str = _("Accept unknown word as known in this session.");
73         tooltips().init(dialog_->button_accept, str);
74         str = _("Add unknown word to personal dictionary.");
75         tooltips().init(dialog_->button_add, str);
76         str = _("Shows word count and progress on spell check.");
77         tooltips().init(dialog_->slider_progress, str);
78 }
79
80
81 void FormSpellchecker::update()
82 {
83         controller().check();
84 }
85
86
87 void FormSpellchecker::partialUpdate(int s)
88 {
89         ControlSpellchecker::State const state =
90                 static_cast<ControlSpellchecker::State>(s);
91
92         switch (state) {
93
94         case ControlSpellchecker::SPELL_FOUND_WORD: {
95                 // Set suggestions.
96                 string w = controller().getWord();
97                 fl_set_input(dialog_->input_replacement, w.c_str());
98                 fl_set_object_label(dialog_->text_unknown, w.c_str());
99                 fl_clear_browser(dialog_->browser_suggestions);
100                 while (!(w = controller().getSuggestion()).empty()) {
101                         fl_add_browser_line(dialog_->browser_suggestions,
102                                             w.c_str());
103                 }
104                 // Fall through...
105         }
106
107         case ControlSpellchecker::SPELL_PROGRESSED: {
108                 int const progress = controller().getProgress();
109                 if (progress == 0)
110                         break;
111
112                 double const wordcount = controller().getCount();
113                 double const total = 100.0 * wordcount / progress;
114                 string const label = tostr(progress) + " %";
115
116                 fl_set_slider_bounds(dialog_->slider_progress, 0.0, total);
117                 fl_set_slider_value(dialog_->slider_progress, wordcount);
118                 fl_set_object_label(dialog_->slider_progress, label.c_str());
119                 fl_redraw_object(dialog_->slider_progress);
120                 break;
121         }
122
123         }
124 }
125
126
127 ButtonPolicy::SMInput FormSpellchecker::input(FL_OBJECT * ob, long ob_value)
128 {
129         if (ob == dialog_->button_replace) {
130                 string const tmp = getString(dialog_->input_replacement);
131                 controller().replace(tmp);
132
133         } else if (ob == dialog_->button_ignore) {
134                 controller().check();
135
136         } else if (ob == dialog_->button_accept) {
137                 controller().ignoreAll();
138
139         } else if (ob == dialog_->button_add) {
140                 controller().insert();
141
142         } else if (ob == dialog_->browser_suggestions) {
143                 string const tmp = getString(dialog_->browser_suggestions);
144                 if (tmp.empty())
145                         return ButtonPolicy::SMI_NOOP;
146
147                 if (ob_value != 2) {
148                         // single-click
149                         // place the chosen string in the input as feedback
150                         fl_set_input(dialog_->input_replacement, tmp.c_str());
151
152                 } else {
153                         // double-click
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);
158                 }
159         }
160
161         return ButtonPolicy::SMI_VALID;
162 }
163
164 } // namespace frontend
165 } // namespace lyx