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 #include "ControlThesaurus.h"
15 #include "FormThesaurus.h"
16 #include "forms/form_thesaurus.h"
18 #include "xforms_helpers.h"
20 #include "support/lstrings.h"
21 #include FORMS_H_LOCATION
24 #ifndef CXX_GLOBAL_CSTD
32 typedef FormController<ControlThesaurus, FormView<FD_thesaurus> > base_class;
35 FormThesaurus::FormThesaurus(Dialog & parent)
36 : base_class(parent, _("Thesaurus"), false),
42 void FormThesaurus::build()
44 dialog_.reset(build_thesaurus(this));
46 fl_set_input_return(dialog_->input_entry, FL_RETURN_CHANGED);
47 fl_set_input_return(dialog_->input_replace, FL_RETURN_CHANGED);
49 setPrehandler(dialog_->input_entry);
50 setPrehandler(dialog_->input_replace);
52 // Manage the ok, apply and cancel/close buttons
53 bcview().setCancel(dialog_->button_close);
54 bcview().addReadOnly(dialog_->input_replace);
56 fl_set_input_return(dialog_->input_entry, FL_RETURN_END_CHANGED);
60 void FormThesaurus::update()
65 string const & str_ = controller().text();
66 setEnabled(dialog_->button_replace, !str_.empty());
67 fl_set_input(dialog_->input_replace, "");
72 void FormThesaurus::updateMeanings(string const & str)
74 fl_clear_browser(dialog_->browser_meanings);
76 fl_set_input(dialog_->input_entry, str.c_str());
78 fl_set_browser_topline(dialog_->browser_meanings, 1);
80 fl_freeze_form(form());
82 Thesaurus::Meanings meanings = controller().getMeanings(str);
84 Thesaurus::Meanings::const_iterator cit = meanings.begin();
85 Thesaurus::Meanings::const_iterator end = meanings.end();
86 for (; cit != end; ++cit) {
87 fl_add_browser_line(dialog_->browser_meanings,
90 vector<string> const & tmpvec = cit->second;
91 vector<string>::const_iterator cit2 = tmpvec.begin();
92 vector<string>::const_iterator end2 = tmpvec.end();
93 for (; cit2 != end2; ++cit2) {
96 fl_add_browser_line(dialog_->browser_meanings,
101 fl_unfreeze_form(form());
102 fl_redraw_form(form());
106 void FormThesaurus::setReplace(string const & templ, string const & nstr)
110 // the following mechanism makes sure we replace "House" with "Home",
111 // "HOUSE" with "HOME" etc.
113 bool all_lower = true;
114 bool all_upper = true;
116 string::const_iterator beg = templ.begin();
117 string::const_iterator end = templ.end();
118 string::const_iterator cit = beg;
119 for (; cit != end; ++cit) {
127 str = lowercase(nstr);
128 } else if (all_upper) {
129 str = uppercase(nstr);
130 } else if (templ.size() > 0 && isupper(templ[0])) {
131 bool rest_lower = true;
132 string::const_iterator cit2 = beg + 1;
134 for (; cit2 != end; ++cit2) {
140 str = lowercase(nstr);
141 str[0] = uppercase(nstr[0]);
145 fl_set_input(dialog_->input_replace, str.c_str());
149 ButtonPolicy::SMInput FormThesaurus::input(FL_OBJECT * obj, long)
151 if (obj == dialog_->input_entry) {
152 string s = trim(fl_get_input(dialog_->input_entry));
157 fl_set_input(dialog_->input_replace, "");
158 return ButtonPolicy::SMI_APPLY;
160 return ButtonPolicy::SMI_NOOP;
162 } else if (obj == dialog_->button_replace) {
163 string rep(fl_get_input(dialog_->input_replace));
165 controller().replace(fl_get_input(dialog_->input_replace));
168 return ButtonPolicy::SMI_APPLY;
169 } else if (obj != dialog_->browser_meanings) {
170 return ButtonPolicy::SMI_NOOP;
173 int const line = fl_get_browser(obj);
175 setReplace(fl_get_input(dialog_->input_entry),
176 trim(fl_get_browser_line(obj, line)));
179 if (clickline_ == fl_get_browser(obj)) {
180 updateMeanings(fl_get_input(dialog_->input_replace));
183 clickline_ = fl_get_browser(obj);
186 return ButtonPolicy::SMI_VALID;