2 * \file FormThesaurus.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "FormThesaurus.h"
14 #include "ControlThesaurus.h"
15 #include "forms/form_thesaurus.h"
17 #include "xforms_helpers.h"
20 #include "support/lstrings.h"
22 #include "lyx_forms.h"
24 #ifndef CXX_GLOBAL_CSTD
34 using support::lowercase;
36 using support::uppercase;
40 typedef FormController<ControlThesaurus, FormView<FD_thesaurus> > base_class;
43 FormThesaurus::FormThesaurus(Dialog & parent)
44 : base_class(parent, _("Thesaurus"), false),
50 void FormThesaurus::build()
52 dialog_.reset(build_thesaurus(this));
54 fl_set_input_return(dialog_->input_entry, FL_RETURN_CHANGED);
55 fl_set_input_return(dialog_->input_replace, FL_RETURN_CHANGED);
57 setPrehandler(dialog_->input_entry);
58 setPrehandler(dialog_->input_replace);
60 // Manage the ok, apply and cancel/close buttons
61 bcview().setCancel(dialog_->button_close);
62 bcview().addReadOnly(dialog_->input_replace);
64 fl_set_input_return(dialog_->input_entry, FL_RETURN_END_CHANGED);
68 void FormThesaurus::update()
73 string const & str_ = controller().text();
74 setEnabled(dialog_->button_replace, !str_.empty());
75 fl_set_input(dialog_->input_replace, "");
80 void FormThesaurus::updateMeanings(string const & str)
82 fl_clear_browser(dialog_->browser_meanings);
84 fl_set_input(dialog_->input_entry, str.c_str());
86 fl_set_browser_topline(dialog_->browser_meanings, 1);
88 fl_freeze_form(form());
90 Thesaurus::Meanings meanings = controller().getMeanings(str);
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,
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) {
104 fl_add_browser_line(dialog_->browser_meanings,
109 fl_unfreeze_form(form());
110 fl_redraw_form(form());
114 void FormThesaurus::setReplace(string const & templ, string const & nstr)
118 // the following mechanism makes sure we replace "House" with "Home",
119 // "HOUSE" with "HOME" etc.
121 bool all_lower = true;
122 bool all_upper = true;
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) {
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;
142 for (; cit2 != end; ++cit2) {
148 str = lowercase(nstr);
149 str[0] = uppercase(nstr[0]);
153 fl_set_input(dialog_->input_replace, str.c_str());
157 ButtonPolicy::SMInput FormThesaurus::input(FL_OBJECT * obj, long)
159 if (obj == dialog_->input_entry) {
160 string s = trim(fl_get_input(dialog_->input_entry));
165 fl_set_input(dialog_->input_replace, "");
166 return ButtonPolicy::SMI_APPLY;
168 return ButtonPolicy::SMI_NOOP;
170 } else if (obj == dialog_->button_replace) {
171 string rep(fl_get_input(dialog_->input_replace));
173 controller().replace(fl_get_input(dialog_->input_replace));
176 return ButtonPolicy::SMI_APPLY;
177 } else if (obj != dialog_->browser_meanings) {
178 return ButtonPolicy::SMI_NOOP;
181 int const line = fl_get_browser(obj);
183 setReplace(fl_get_input(dialog_->input_entry),
184 trim(fl_get_browser_line(obj, line)));
187 if (clickline_ == fl_get_browser(obj)) {
188 updateMeanings(fl_get_input(dialog_->input_replace));
191 clickline_ = fl_get_browser(obj);
194 return ButtonPolicy::SMI_VALID;
197 } // namespace frontend