2 * \file FormThesaurus.C
3 * Copyright 2001 The LyX Team.
4 * See the file COPYING.
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 // Manage the ok, apply and cancel/close buttons
42 bc().setCancel(dialog_->button_close);
43 bc().addReadOnly(dialog_->input_replace);
45 fl_set_input_return(dialog_->input_entry, FL_RETURN_END_CHANGED);
49 void FormThesaurus::update()
54 string const & str_ = controller().text();
55 setEnabled(dialog_->button_replace, !str_.empty());
56 fl_set_input(dialog_->input_replace, "");
61 void FormThesaurus::updateMeanings(string const & str)
63 fl_clear_browser(dialog_->browser_meanings);
65 fl_set_input(dialog_->input_entry, str.c_str());
67 fl_set_browser_topline(dialog_->browser_meanings, 1);
69 fl_freeze_form(form());
71 Thesaurus::Meanings meanings = controller().getMeanings(str);
73 Thesaurus::Meanings::const_iterator cit = meanings.begin();
74 Thesaurus::Meanings::const_iterator end = meanings.end();
75 for (; cit != end; ++cit) {
76 fl_add_browser_line(dialog_->browser_meanings,
79 vector<string> const & tmpvec = cit->second;
80 vector<string>::const_iterator cit2 = tmpvec.begin();
81 vector<string>::const_iterator end2 = tmpvec.end();
82 for (; cit2 != end2; ++cit2) {
85 fl_add_browser_line(dialog_->browser_meanings,
90 fl_unfreeze_form(form());
91 fl_redraw_form(form());
95 void FormThesaurus::setReplace(string const & templ, string const & nstr)
99 // the following mechanism makes sure we replace "House" with "Home",
100 // "HOUSE" with "HOME" etc.
102 bool all_lower = true;
103 bool all_upper = true;
105 string::const_iterator beg = templ.begin();
106 string::const_iterator end = templ.end();
107 string::const_iterator cit = beg;
108 for (; cit != end; ++cit) {
116 str = lowercase(nstr);
117 } else if (all_upper) {
118 str = uppercase(nstr);
119 } else if (templ.size() > 0 && isupper(templ[0])) {
120 bool rest_lower = true;
121 string::const_iterator cit2 = beg + 1;
123 for (; cit2 != end; ++cit2) {
129 str = lowercase(nstr);
130 str[0] = uppercase(nstr[0]);
134 fl_set_input(dialog_->input_replace, str.c_str());
138 ButtonPolicy::SMInput FormThesaurus::input(FL_OBJECT * obj, long)
140 if (obj == dialog_->input_entry) {
141 string s = strip(frontStrip(fl_get_input(dialog_->input_entry)));
146 fl_set_input(dialog_->input_replace, "");
147 return ButtonPolicy::SMI_APPLY;
149 return ButtonPolicy::SMI_NOOP;
151 } else if (obj == dialog_->button_replace) {
152 string rep(fl_get_input(dialog_->input_replace));
154 controller().replace(fl_get_input(dialog_->input_replace));
157 return ButtonPolicy::SMI_APPLY;
158 } else if (obj != dialog_->browser_meanings) {
159 return ButtonPolicy::SMI_NOOP;
162 int const line = fl_get_browser(obj);
164 setReplace(fl_get_input(dialog_->input_entry),
165 strip(frontStrip(fl_get_browser_line(obj, line))));
168 if (clickline_ == fl_get_browser(obj)) {
169 updateMeanings(fl_get_input(dialog_->input_replace));
172 clickline_ = fl_get_browser(obj);
175 return ButtonPolicy::SMI_VALID;