]> git.lyx.org Git - features.git/blob - src/frontends/xforms/FormSpellchecker.C
80469dcb8c3593fc8296e1991ff91427bb205078
[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
14 #include "xformsBC.h"
15 #include "FormSpellchecker.h"
16 #include "ControlSpellchecker.h"
17 #include "forms/form_spellchecker.h"
18
19 #include "forms_gettext.h"
20 #include "Tooltips.h"
21 #include "xforms_helpers.h"
22 #include "support/lstrings.h"
23
24 #include FORMS_H_LOCATION
25
26 using std::endl;
27
28 typedef FormCB<ControlSpellchecker, FormDB<FD_spellchecker> > base_class;
29
30 FormSpellchecker::FormSpellchecker()
31         : base_class(_("Spell-check Document"))
32 {}
33
34
35 void FormSpellchecker::build()
36 {
37         dialog_.reset(build_spellchecker(this));
38
39         // Manage the buttons
40         bcview().setCancel(dialog_->button_close);
41
42         // disable for read-only documents
43         bcview().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 = _("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);
75 }
76
77
78 void FormSpellchecker::partialUpdate(int s)
79 {
80         ControlSpellchecker::State const state =
81                 static_cast<ControlSpellchecker::State>(s);
82
83         switch (state) {
84
85         case ControlSpellchecker::SPELL_FOUND_WORD: {
86                 // Set suggestions.
87                 string w = controller().getWord();
88                 fl_set_input(dialog_->input_replacement, w.c_str());
89                 fl_set_object_label(dialog_->text_unknown, w.c_str());
90                 fl_clear_browser(dialog_->browser_suggestions);
91                 while (!(w = controller().getSuggestion()).empty()) {
92                         fl_add_browser_line(dialog_->browser_suggestions,
93                                             w.c_str());
94                 }
95                 // Fall through...
96         }
97
98         case ControlSpellchecker::SPELL_PROGRESSED: {
99                 int const progress = controller().getProgress();
100                 if (progress == 0)
101                         break;
102
103                 double const wordcount = controller().getCount();
104                 double const total = 100.0 * wordcount / progress;
105                 string const label = tostr(progress) + " %";
106
107                 fl_set_slider_bounds(dialog_->slider_progress, 0.0, total);
108                 fl_set_slider_value(dialog_->slider_progress, wordcount);
109                 fl_set_object_label(dialog_->slider_progress, label.c_str());
110                 fl_redraw_object(dialog_->slider_progress);
111                 break;
112         }
113
114         }
115 }
116
117
118 ButtonPolicy::SMInput FormSpellchecker::input(FL_OBJECT * ob, long ob_value)
119 {
120         if (ob == dialog_->button_replace) {
121                 string const tmp = getString(dialog_->input_replacement);
122                 controller().replace(tmp);
123
124         } else if (ob == dialog_->button_ignore) {
125                 controller().check();
126
127         } else if (ob == dialog_->button_accept) {
128                 controller().ignoreAll();
129
130         } else if (ob == dialog_->button_add) {
131                 controller().insert();
132
133         } else if (ob == dialog_->browser_suggestions) {
134                 string const tmp = getString(dialog_->browser_suggestions);
135                 if (tmp.empty())
136                         return ButtonPolicy::SMI_NOOP;
137
138                 if (ob_value != 2) {
139                         // single-click
140                         // place the chosen string in the input as feedback
141                         fl_set_input(dialog_->input_replacement, tmp.c_str());
142
143                 } else {
144                         // double-click
145                         controller().replace(tmp);
146                         // reset the browser so that the following
147                         // single-click callback doesn't do anything
148                         fl_deselect_browser(dialog_->browser_suggestions);
149                 }
150         }
151
152         return ButtonPolicy::SMI_VALID;
153 }