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"
24 typedef FormCB<ControlThesaurus, FormDB<FD_form_thesaurus> > base_class;
26 FormThesaurus::FormThesaurus(ControlThesaurus & c)
27 : base_class(c, _("LyX: Thesaurus"), false),
33 void FormThesaurus::build()
35 dialog_.reset(build_thesaurus());
37 // Manage the ok, apply and cancel/close buttons
38 bc().setCancel(dialog_->button_close);
39 bc().addReadOnly(dialog_->input_replace);
41 fl_set_input_return(dialog_->input_entry, FL_RETURN_END_CHANGED);
45 void FormThesaurus::update()
50 string const & str_ = controller().text();
51 setEnabled(dialog_->button_replace, !str_.empty());
52 fl_set_input(dialog_->input_replace, "");
57 void FormThesaurus::updateMeanings(string const & str)
59 fl_clear_browser(dialog_->browser_meanings);
61 fl_set_input(dialog_->input_entry, str.c_str());
63 fl_set_browser_topline(dialog_->browser_meanings, 1);
65 fl_freeze_form(form());
67 Thesaurus::Meanings meanings = controller().getMeanings(str);
69 for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
70 cit != meanings.end(); ++cit) {
71 fl_add_browser_line(dialog_->browser_meanings, cit->first.c_str());
72 for (std::vector<string>::const_iterator cit2 = cit->second.begin();
73 cit2 != cit->second.end(); ++cit2) {
76 fl_add_browser_line(dialog_->browser_meanings, ent.c_str());
80 fl_unfreeze_form(form());
81 fl_redraw_form(form());
85 void FormThesaurus::setReplace(string const & templ, string const & nstr)
89 // the following mechanism makes sure we replace "House" with "Home",
90 // "HOUSE" with "HOME" etc.
92 bool all_lower = true;
93 bool all_upper = true;
95 for (string::const_iterator it = templ.begin(); it != templ.end(); ++it) {
103 str = lowercase(nstr);
104 } else if (all_upper) {
105 str = uppercase(nstr);
106 } else if (templ.size() > 0 && isupper(templ[0])) {
107 bool rest_lower = true;
108 for (string::const_iterator it = templ.begin() + 1;
109 it != templ.end(); ++it) {
115 str = lowercase(nstr);
116 str[0] = uppercase(nstr[0]);
120 fl_set_input(dialog_->input_replace, str.c_str());
124 ButtonPolicy::SMInput FormThesaurus::input(FL_OBJECT * obj, long)
126 if (obj == dialog_->input_entry) {
127 string s = strip(frontStrip(fl_get_input(dialog_->input_entry)));
132 fl_set_input(dialog_->input_replace, "");
133 return ButtonPolicy::SMI_APPLY;
135 return ButtonPolicy::SMI_NOOP;
137 } else if (obj == dialog_->button_replace) {
138 string rep(fl_get_input(dialog_->input_replace));
140 controller().replace(fl_get_input(dialog_->input_replace));
143 return ButtonPolicy::SMI_APPLY;
144 } else if (obj != dialog_->browser_meanings) {
145 return ButtonPolicy::SMI_NOOP;
148 int const line = fl_get_browser(obj);
150 setReplace(fl_get_input(dialog_->input_entry),
151 strip(frontStrip(fl_get_browser_line(obj, line))));
154 if (clickline_ == fl_get_browser(obj)) {
155 updateMeanings(fl_get_input(dialog_->input_replace));
158 clickline_ = fl_get_browser(obj);
161 return ButtonPolicy::SMI_VALID;