]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormSpellchecker.C
Yet more dialog tweaking from Rob.
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "Tooltips.h"
18 #include "xformsBC.h"
19 #include "xforms_helpers.h"
20 #include "ControlSpellchecker.h"
21 #include "FormSpellchecker.h"
22 #include "forms/form_spellchecker.h"
23 #include FORMS_H_LOCATION
24
25 typedef FormCB<ControlSpellchecker, FormDB<FD_spellchecker> > base_class;
26
27 FormSpellchecker::FormSpellchecker()
28         : base_class(_("Spellchecker"))
29 {}
30
31
32 void FormSpellchecker::build()
33 {
34         dialog_.reset(build_spellchecker(this));
35
36         fl_set_browser_dblclick_callback(dialog_->browser,
37                                          C_FormBaseInputCB, 2);
38
39         fl_set_input_return(dialog_->input, FL_RETURN_CHANGED);
40         setPrehandler(dialog_->input);
41
42         // Manage the buttons
43         bc().setCancel(dialog_->button_close);
44         bc().addReadOnly(dialog_->button_replace);
45         bc().addReadOnly(dialog_->button_accept);
46         bc().addReadOnly(dialog_->button_add);
47         bc().addReadOnly(dialog_->button_ignore);
48         bc().addReadOnly(dialog_->button_start);
49         bc().addReadOnly(dialog_->browser);
50
51         // set up the tooltips
52         string str = _("Type replacement for unknown word "
53                         " or select from suggestions.");
54         tooltips().init(dialog_->input, str);
55         str = _("List of replacement suggestions from dictionary.");
56         tooltips().init(dialog_->browser, str);
57         str = _("Start the spellingchecker.");
58         tooltips().init(dialog_->button_start, str);
59         str = _("Replace unknown word.");
60         tooltips().init(dialog_->button_replace, str);
61         str = _("Ignore unknown word.");
62         tooltips().init(dialog_->button_ignore, str);
63         str = _("Accept unknown word as known in this session.");
64         tooltips().init(dialog_->button_accept, str);
65         str = _("Add unknown word to personal dictionary.");
66         tooltips().init(dialog_->button_add, str);
67 }
68
69 void FormSpellchecker::update()
70 {
71         fl_set_input(dialog_->input, "");
72         fl_set_object_label(dialog_->text, "");
73         fl_clear_browser(dialog_->browser);
74         fl_set_slider_value(dialog_->slider, 0);
75         start(true);
76 }
77
78 ButtonPolicy::SMInput FormSpellchecker::input(FL_OBJECT * obj, long val)
79 {
80         if (obj == dialog_->button_replace) {
81                 string const tmp = getString(dialog_->input);
82                 controller().replace(tmp);
83
84         } else if (obj == dialog_->button_start) {
85                 if (start())
86                         controller().check();
87                 else
88                         controller().stop();
89
90         } else if (obj == dialog_->button_ignore) {
91                 controller().check();
92
93         } else if (obj == dialog_->button_accept) {
94                 controller().ignoreAll();
95
96         } else if (obj == dialog_->button_add) {
97                 controller().insert();
98
99         } else if (obj == dialog_->browser) {
100                 int const line = fl_get_browser(dialog_->browser);
101                 string const tmp =
102                         getString(dialog_->browser, line);
103                 if (tmp.empty())
104                         return ButtonPolicy::SMI_NOOP;
105
106                 if (val != 2) {
107                         // single-click
108                         // place the chosen string in the input as feedback
109                         fl_set_input(dialog_->input, tmp.c_str());
110
111                 } else {
112                         // double-click
113                         controller().replace(tmp);
114                         // reset the browser so that the following
115                         // single-click callback doesn't do anything
116                         fl_deselect_browser_line(dialog_->browser, line);
117                 }
118         }
119
120         return ButtonPolicy::SMI_VALID;
121 }
122
123 void FormSpellchecker::partialUpdate(int id)
124 {
125         switch (id) {
126         case 0:
127                 // set progress bar
128                 fl_set_slider_value(dialog_->slider,
129                                     controller().getProgress());
130                 break;
131         case 1:
132         {
133                 // set suggestions
134                 string w = controller().getWord();
135                 fl_set_input(dialog_->input, w.c_str());
136                 fl_set_object_label(dialog_->text, w.c_str());
137                 fl_clear_browser(dialog_->browser);
138                 while (!(w = controller().getSuggestion()).empty()) {
139                         fl_add_browser_line(dialog_->browser, w.c_str());
140                 }
141         }
142         break;
143         case 2:
144                 // show exit message
145                 fl_show_messages(controller().getMessage().c_str());
146                 hide();
147         }
148 }
149
150
151 void FormSpellchecker::showMessage(const char * msg)
152 {
153         fl_show_message(msg, "", "");
154 }
155
156 bool FormSpellchecker::start(bool init)
157 {
158         static bool running = false;
159
160         if (init) {
161                 running = false;
162         } else {
163                 running = !running;
164         }
165
166         fl_set_object_label(dialog_->button_start,
167                         (running ? _("Stop") : _("Start")));    
168         fl_set_button_shortcut(dialog_->button_start, "#S", 1);
169         fl_show_object(dialog_->button_start);
170
171         string const str = (running ? _("Stop the spellingchecker.") :
172                                         _("Start the spellingchecker."));
173         tooltips().init(dialog_->button_start, str);
174
175         setEnabled(dialog_->button_replace, running);
176         setEnabled(dialog_->button_ignore, running);
177         setEnabled(dialog_->button_accept, running);
178         setEnabled(dialog_->button_add, running);
179         setEnabled(dialog_->browser, running);
180         setEnabled(dialog_->input, running);
181         
182         return running;
183 }