2 * \file FormThesaurus.C
3 * Copyright 2001 The LyX Team.
4 * See the file COPYING.
6 * \author Edwin Leuven, leuven@fee.uva.nl
12 #pragma implementation
15 #include "ControlThesaurus.h"
16 #include "FormThesaurus.h"
17 #include "forms/form_thesaurus.h"
19 #include "xforms_helpers.h"
21 #include "support/lstrings.h"
22 #include FORMS_H_LOCATION
25 #ifndef CXX_GLOBAL_CSTD
33 typedef FormCB<ControlThesaurus, FormDB<FD_thesaurus> > base_class;
36 FormThesaurus::FormThesaurus(ControlThesaurus & c, Dialogs & d)
37 : base_class(c, d, _("LyX: Thesaurus"), false),
43 void FormThesaurus::build()
45 dialog_.reset(build_thesaurus(this));
47 fl_set_input_return(dialog_->input_entry, FL_RETURN_CHANGED);
48 fl_set_input_return(dialog_->input_replace, FL_RETURN_CHANGED);
50 setPrehandler(dialog_->input_entry);
51 setPrehandler(dialog_->input_replace);
53 // Manage the ok, apply and cancel/close buttons
54 bc().setCancel(dialog_->button_close);
55 bc().addReadOnly(dialog_->input_replace);
57 fl_set_input_return(dialog_->input_entry, FL_RETURN_END_CHANGED);
61 void FormThesaurus::update()
66 string const & str_ = controller().text();
67 setEnabled(dialog_->button_replace, !str_.empty());
68 fl_set_input(dialog_->input_replace, "");
73 void FormThesaurus::updateMeanings(string const & str)
75 fl_clear_browser(dialog_->browser_meanings);
77 fl_set_input(dialog_->input_entry, str.c_str());
79 fl_set_browser_topline(dialog_->browser_meanings, 1);
81 fl_freeze_form(form());
83 Thesaurus::Meanings meanings = controller().getMeanings(str);
85 Thesaurus::Meanings::const_iterator cit = meanings.begin();
86 Thesaurus::Meanings::const_iterator end = meanings.end();
87 for (; cit != end; ++cit) {
88 fl_add_browser_line(dialog_->browser_meanings,
91 vector<string> const & tmpvec = cit->second;
92 vector<string>::const_iterator cit2 = tmpvec.begin();
93 vector<string>::const_iterator end2 = tmpvec.end();
94 for (; cit2 != end2; ++cit2) {
97 fl_add_browser_line(dialog_->browser_meanings,
102 fl_unfreeze_form(form());
103 fl_redraw_form(form());
107 void FormThesaurus::setReplace(string const & templ, string const & nstr)
111 // the following mechanism makes sure we replace "House" with "Home",
112 // "HOUSE" with "HOME" etc.
114 bool all_lower = true;
115 bool all_upper = true;
117 string::const_iterator beg = templ.begin();
118 string::const_iterator end = templ.end();
119 string::const_iterator cit = beg;
120 for (; cit != end; ++cit) {
128 str = lowercase(nstr);
129 } else if (all_upper) {
130 str = uppercase(nstr);
131 } else if (templ.size() > 0 && isupper(templ[0])) {
132 bool rest_lower = true;
133 string::const_iterator cit2 = beg + 1;
135 for (; cit2 != end; ++cit2) {
141 str = lowercase(nstr);
142 str[0] = uppercase(nstr[0]);
146 fl_set_input(dialog_->input_replace, str.c_str());
150 ButtonPolicy::SMInput FormThesaurus::input(FL_OBJECT * obj, long)
152 if (obj == dialog_->input_entry) {
153 string s = strip(frontStrip(fl_get_input(dialog_->input_entry)));
158 fl_set_input(dialog_->input_replace, "");
159 return ButtonPolicy::SMI_APPLY;
161 return ButtonPolicy::SMI_NOOP;
163 } else if (obj == dialog_->button_replace) {
164 string rep(fl_get_input(dialog_->input_replace));
166 controller().replace(fl_get_input(dialog_->input_replace));
169 return ButtonPolicy::SMI_APPLY;
170 } else if (obj != dialog_->browser_meanings) {
171 return ButtonPolicy::SMI_NOOP;
174 int const line = fl_get_browser(obj);
176 setReplace(fl_get_input(dialog_->input_entry),
177 strip(frontStrip(fl_get_browser_line(obj, line))));
180 if (clickline_ == fl_get_browser(obj)) {
181 updateMeanings(fl_get_input(dialog_->input_replace));
184 clickline_ = fl_get_browser(obj);
187 return ButtonPolicy::SMI_VALID;