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