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
14 #pragma implementation
17 #include "ControlThesaurus.h"
18 #include "FormThesaurus.h"
19 #include "forms/form_thesaurus.h"
21 #include "xforms_helpers.h"
23 #include "support/lstrings.h"
24 #include FORMS_H_LOCATION
27 #ifndef CXX_GLOBAL_CSTD
35 typedef FormCB<ControlThesaurus, FormDB<FD_thesaurus> > base_class;
38 FormThesaurus::FormThesaurus()
39 : base_class(_("Thesaurus"), false),
45 void FormThesaurus::build()
47 dialog_.reset(build_thesaurus(this));
49 fl_set_input_return(dialog_->input_entry, FL_RETURN_CHANGED);
50 fl_set_input_return(dialog_->input_replace, FL_RETURN_CHANGED);
52 setPrehandler(dialog_->input_entry);
53 setPrehandler(dialog_->input_replace);
55 // Manage the ok, apply and cancel/close buttons
56 bc().setCancel(dialog_->button_close);
57 bc().addReadOnly(dialog_->input_replace);
59 fl_set_input_return(dialog_->input_entry, FL_RETURN_END_CHANGED);
63 void FormThesaurus::update()
68 string const & str_ = controller().text();
69 setEnabled(dialog_->button_replace, !str_.empty());
70 fl_set_input(dialog_->input_replace, "");
75 void FormThesaurus::updateMeanings(string const & str)
77 fl_clear_browser(dialog_->browser_meanings);
79 fl_set_input(dialog_->input_entry, str.c_str());
81 fl_set_browser_topline(dialog_->browser_meanings, 1);
83 fl_freeze_form(form());
85 Thesaurus::Meanings meanings = controller().getMeanings(str);
87 Thesaurus::Meanings::const_iterator cit = meanings.begin();
88 Thesaurus::Meanings::const_iterator end = meanings.end();
89 for (; cit != end; ++cit) {
90 fl_add_browser_line(dialog_->browser_meanings,
93 vector<string> const & tmpvec = cit->second;
94 vector<string>::const_iterator cit2 = tmpvec.begin();
95 vector<string>::const_iterator end2 = tmpvec.end();
96 for (; cit2 != end2; ++cit2) {
99 fl_add_browser_line(dialog_->browser_meanings,
104 fl_unfreeze_form(form());
105 fl_redraw_form(form());
109 void FormThesaurus::setReplace(string const & templ, string const & nstr)
113 // the following mechanism makes sure we replace "House" with "Home",
114 // "HOUSE" with "HOME" etc.
116 bool all_lower = true;
117 bool all_upper = true;
119 string::const_iterator beg = templ.begin();
120 string::const_iterator end = templ.end();
121 string::const_iterator cit = beg;
122 for (; cit != end; ++cit) {
130 str = lowercase(nstr);
131 } else if (all_upper) {
132 str = uppercase(nstr);
133 } else if (templ.size() > 0 && isupper(templ[0])) {
134 bool rest_lower = true;
135 string::const_iterator cit2 = beg + 1;
137 for (; cit2 != end; ++cit2) {
143 str = lowercase(nstr);
144 str[0] = uppercase(nstr[0]);
148 fl_set_input(dialog_->input_replace, str.c_str());
152 ButtonPolicy::SMInput FormThesaurus::input(FL_OBJECT * obj, long)
154 if (obj == dialog_->input_entry) {
155 string s = trim(fl_get_input(dialog_->input_entry));
160 fl_set_input(dialog_->input_replace, "");
161 return ButtonPolicy::SMI_APPLY;
163 return ButtonPolicy::SMI_NOOP;
165 } else if (obj == dialog_->button_replace) {
166 string rep(fl_get_input(dialog_->input_replace));
168 controller().replace(fl_get_input(dialog_->input_replace));
171 return ButtonPolicy::SMI_APPLY;
172 } else if (obj != dialog_->browser_meanings) {
173 return ButtonPolicy::SMI_NOOP;
176 int const line = fl_get_browser(obj);
178 setReplace(fl_get_input(dialog_->input_entry),
179 trim(fl_get_browser_line(obj, line)));
182 if (clickline_ == fl_get_browser(obj)) {
183 updateMeanings(fl_get_input(dialog_->input_replace));
186 clickline_ = fl_get_browser(obj);
189 return ButtonPolicy::SMI_VALID;