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
16 #include "support/lstrings.h"
18 #include "xforms_helpers.h"
19 #include "ControlThesaurus.h"
20 #include "FormThesaurus.h"
21 #include "form_thesaurus.h"
28 typedef FormCB<ControlThesaurus, FormDB<FD_form_thesaurus> > base_class;
30 FormThesaurus::FormThesaurus(ControlThesaurus & c)
31 : base_class(c, _("LyX: Thesaurus"), false),
37 void FormThesaurus::build()
39 dialog_.reset(build_thesaurus());
41 fl_set_input_return(dialog_->input_entry, FL_RETURN_CHANGED);
42 fl_set_input_return(dialog_->input_replace, FL_RETURN_CHANGED);
44 setPrehandler(dialog_->input_entry);
45 setPrehandler(dialog_->input_replace);
47 // Manage the ok, apply and cancel/close buttons
48 bc().setCancel(dialog_->button_close);
49 bc().addReadOnly(dialog_->input_replace);
51 fl_set_input_return(dialog_->input_entry, FL_RETURN_END_CHANGED);
55 void FormThesaurus::update()
60 string const & str_ = controller().text();
61 setEnabled(dialog_->button_replace, !str_.empty());
62 fl_set_input(dialog_->input_replace, "");
67 void FormThesaurus::updateMeanings(string const & str)
69 fl_clear_browser(dialog_->browser_meanings);
71 fl_set_input(dialog_->input_entry, str.c_str());
73 fl_set_browser_topline(dialog_->browser_meanings, 1);
75 fl_freeze_form(form());
77 Thesaurus::Meanings meanings = controller().getMeanings(str);
79 Thesaurus::Meanings::const_iterator cit = meanings.begin();
80 Thesaurus::Meanings::const_iterator end = meanings.end();
81 for (; cit != end; ++cit) {
82 fl_add_browser_line(dialog_->browser_meanings,
85 vector<string> const & tmpvec = cit->second;
86 vector<string>::const_iterator cit2 = tmpvec.begin();
87 vector<string>::const_iterator end2 = tmpvec.end();
88 for (; cit2 != end2; ++cit2) {
91 fl_add_browser_line(dialog_->browser_meanings,
96 fl_unfreeze_form(form());
97 fl_redraw_form(form());
101 void FormThesaurus::setReplace(string const & templ, string const & nstr)
105 // the following mechanism makes sure we replace "House" with "Home",
106 // "HOUSE" with "HOME" etc.
108 bool all_lower = true;
109 bool all_upper = true;
111 string::const_iterator beg = templ.begin();
112 string::const_iterator end = templ.end();
113 string::const_iterator cit = beg;
114 for (; cit != end; ++cit) {
122 str = lowercase(nstr);
123 } else if (all_upper) {
124 str = uppercase(nstr);
125 } else if (templ.size() > 0 && isupper(templ[0])) {
126 bool rest_lower = true;
127 string::const_iterator cit2 = beg + 1;
129 for (; cit2 != end; ++cit2) {
135 str = lowercase(nstr);
136 str[0] = uppercase(nstr[0]);
140 fl_set_input(dialog_->input_replace, str.c_str());
144 ButtonPolicy::SMInput FormThesaurus::input(FL_OBJECT * obj, long)
146 if (obj == dialog_->input_entry) {
147 string s = strip(frontStrip(fl_get_input(dialog_->input_entry)));
152 fl_set_input(dialog_->input_replace, "");
153 return ButtonPolicy::SMI_APPLY;
155 return ButtonPolicy::SMI_NOOP;
157 } else if (obj == dialog_->button_replace) {
158 string rep(fl_get_input(dialog_->input_replace));
160 controller().replace(fl_get_input(dialog_->input_replace));
163 return ButtonPolicy::SMI_APPLY;
164 } else if (obj != dialog_->browser_meanings) {
165 return ButtonPolicy::SMI_NOOP;
168 int const line = fl_get_browser(obj);
170 setReplace(fl_get_input(dialog_->input_entry),
171 strip(frontStrip(fl_get_browser_line(obj, line))));
174 if (clickline_ == fl_get_browser(obj)) {
175 updateMeanings(fl_get_input(dialog_->input_replace));
178 clickline_ = fl_get_browser(obj);
181 return ButtonPolicy::SMI_VALID;