]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormThesaurus.C
Change glob() API to accept a dir parameter.
[lyx.git] / src / frontends / xforms / FormThesaurus.C
1 /**
2  * \file FormThesaurus.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 "FormThesaurus.h"
14 #include "ControlThesaurus.h"
15 #include "forms/form_thesaurus.h"
16
17 #include "xforms_helpers.h"
18 #include "xformsBC.h"
19
20 #include "support/lstrings.h"
21
22 #include "lyx_forms.h"
23
24 #ifndef CXX_GLOBAL_CSTD
25 using std::islower;
26 using std::isupper;
27 #endif
28
29 using std::vector;
30 using std::string;
31
32 namespace lyx {
33
34 using support::lowercase;
35 using support::trim;
36 using support::uppercase;
37
38 namespace frontend {
39
40 typedef FormController<ControlThesaurus, FormView<FD_thesaurus> > base_class;
41
42
43 FormThesaurus::FormThesaurus(Dialog & parent)
44         : base_class(parent, _("Thesaurus"), false),
45           clickline_(-1)
46 {
47 }
48
49
50 void FormThesaurus::build()
51 {
52         dialog_.reset(build_thesaurus(this));
53
54         fl_set_input_return(dialog_->input_entry,   FL_RETURN_CHANGED);
55         fl_set_input_return(dialog_->input_replace, FL_RETURN_CHANGED);
56
57         setPrehandler(dialog_->input_entry);
58         setPrehandler(dialog_->input_replace);
59
60         // Manage the ok, apply and cancel/close buttons
61         bcview().setCancel(dialog_->button_close);
62         bcview().addReadOnly(dialog_->input_replace);
63
64         fl_set_input_return(dialog_->input_entry, FL_RETURN_END_CHANGED);
65 }
66
67
68 void FormThesaurus::update()
69 {
70         if (!dialog_.get())
71                 return;
72
73         string const & str_ = controller().text();
74         setEnabled(dialog_->button_replace, !str_.empty());
75         fl_set_input(dialog_->input_replace, "");
76         updateMeanings(str_);
77 }
78
79
80 void FormThesaurus::updateMeanings(string const & str)
81 {
82         fl_clear_browser(dialog_->browser_meanings);
83
84         fl_set_input(dialog_->input_entry, str.c_str());
85
86         fl_set_browser_topline(dialog_->browser_meanings, 1);
87
88         fl_freeze_form(form());
89
90         Thesaurus::Meanings meanings = controller().getMeanings(str);
91
92         Thesaurus::Meanings::const_iterator cit = meanings.begin();
93         Thesaurus::Meanings::const_iterator end = meanings.end();
94         for (; cit != end; ++cit) {
95                 fl_add_browser_line(dialog_->browser_meanings,
96                                     cit->first.c_str());
97
98                 vector<string> const & tmpvec = cit->second;
99                 vector<string>::const_iterator cit2 = tmpvec.begin();
100                 vector<string>::const_iterator end2 = tmpvec.end();
101                 for (; cit2 != end2; ++cit2) {
102                         string ent = "   ";
103                         ent += *cit2;
104                         fl_add_browser_line(dialog_->browser_meanings,
105                                             ent.c_str());
106                 }
107         }
108
109         fl_unfreeze_form(form());
110         fl_redraw_form(form());
111 }
112
113
114 void FormThesaurus::setReplace(string const & templ, string const & nstr)
115 {
116         string str(nstr);
117
118         // the following mechanism makes sure we replace "House" with "Home",
119         // "HOUSE" with "HOME" etc.
120
121         bool all_lower = true;
122         bool all_upper = true;
123
124         string::const_iterator beg = templ.begin();
125         string::const_iterator end = templ.end();
126         string::const_iterator cit = beg;
127         for (; cit != end; ++cit) {
128                 if (isupper(*cit))
129                         all_lower = false;
130                 if (islower(*cit))
131                         all_upper = false;
132         }
133
134         if (all_lower) {
135                 str = lowercase(nstr);
136         } else if (all_upper) {
137                 str = uppercase(nstr);
138         } else if (templ.size() > 0 && isupper(templ[0])) {
139                 bool rest_lower = true;
140                 string::const_iterator cit2 = beg + 1;
141
142                 for (; cit2 != end; ++cit2) {
143                         if (isupper(*cit2))
144                                 rest_lower = false;
145                 }
146
147                 if (rest_lower) {
148                         str = lowercase(nstr);
149                         str[0] = uppercase(nstr[0]);
150                 }
151         }
152
153         fl_set_input(dialog_->input_replace, str.c_str());
154 }
155
156
157 ButtonPolicy::SMInput FormThesaurus::input(FL_OBJECT * obj, long)
158 {
159         if (obj == dialog_->input_entry) {
160                 string s = trim(fl_get_input(dialog_->input_entry));
161
162                 updateMeanings(s);
163
164                 if (s.empty()) {
165                         fl_set_input(dialog_->input_replace, "");
166                         return ButtonPolicy::SMI_APPLY;
167                 }
168                 return ButtonPolicy::SMI_NOOP;
169
170         } else if (obj == dialog_->button_replace) {
171                 string rep(fl_get_input(dialog_->input_replace));
172                 if (!rep.empty())
173                         controller().replace(fl_get_input(dialog_->input_replace));
174                 clickline_ = -1;
175                 updateMeanings(rep);
176                 return ButtonPolicy::SMI_APPLY;
177         } else if (obj != dialog_->browser_meanings) {
178                 return ButtonPolicy::SMI_NOOP;
179         }
180
181         int const line = fl_get_browser(obj);
182         if (line > 0) {
183                 setReplace(fl_get_input(dialog_->input_entry),
184                            trim(fl_get_browser_line(obj, line)));
185         }
186
187         if (clickline_ == fl_get_browser(obj)) {
188                 updateMeanings(fl_get_input(dialog_->input_replace));
189                 clickline_ = -1;
190         } else {
191                 clickline_ = fl_get_browser(obj);
192         }
193
194         return ButtonPolicy::SMI_VALID;
195 }
196
197 } // namespace frontend
198 } // namespace lyx