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