]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormThesaurus.C
Yet more dialog tweaking from Rob.
[lyx.git] / src / frontends / xforms / FormThesaurus.C
1 /**
2  * \file FormThesaurus.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "ControlThesaurus.h"
18 #include "FormThesaurus.h"
19 #include "forms/form_thesaurus.h"
20 #include "xformsBC.h"
21 #include "xforms_helpers.h"
22 #include "debug.h"
23 #include "support/lstrings.h"
24 #include FORMS_H_LOCATION
25 #include <cctype>
26
27 #ifndef CXX_GLOBAL_CSTD
28 using std::isupper;
29 using std::islower;
30 #endif
31
32 using std::vector;
33
34
35 typedef FormCB<ControlThesaurus, FormDB<FD_thesaurus> > base_class;
36
37
38 FormThesaurus::FormThesaurus()
39         : base_class(_("Thesaurus"), false),
40           clickline_(-1)
41 {
42 }
43
44
45 void FormThesaurus::build()
46 {
47         dialog_.reset(build_thesaurus(this));
48
49         fl_set_input_return(dialog_->input_entry,   FL_RETURN_CHANGED);
50         fl_set_input_return(dialog_->input_replace, FL_RETURN_CHANGED);
51
52         setPrehandler(dialog_->input_entry);
53         setPrehandler(dialog_->input_replace);
54
55         // Manage the ok, apply and cancel/close buttons
56         bc().setCancel(dialog_->button_close);
57         bc().addReadOnly(dialog_->input_replace);
58
59         fl_set_input_return(dialog_->input_entry, FL_RETURN_END_CHANGED);
60 }
61
62
63 void FormThesaurus::update()
64 {
65         if (!dialog_.get())
66                 return;
67
68         string const & str_ = controller().text();
69         setEnabled(dialog_->button_replace, !str_.empty());
70         fl_set_input(dialog_->input_replace, "");
71         updateMeanings(str_);
72 }
73
74
75 void FormThesaurus::updateMeanings(string const & str)
76 {
77         fl_clear_browser(dialog_->browser_meanings);
78
79         fl_set_input(dialog_->input_entry, str.c_str());
80
81         fl_set_browser_topline(dialog_->browser_meanings, 1);
82
83         fl_freeze_form(form());
84
85         Thesaurus::Meanings meanings = controller().getMeanings(str);
86
87         Thesaurus::Meanings::const_iterator cit = meanings.begin();
88         Thesaurus::Meanings::const_iterator end = meanings.end();
89         for (; cit != end; ++cit) {
90                 fl_add_browser_line(dialog_->browser_meanings,
91                                     cit->first.c_str());
92
93                 vector<string> const & tmpvec = cit->second;
94                 vector<string>::const_iterator cit2 = tmpvec.begin();
95                 vector<string>::const_iterator end2 = tmpvec.end();
96                 for (; cit2 != end2; ++cit2) {
97                         string ent = "   ";
98                         ent += *cit2;
99                         fl_add_browser_line(dialog_->browser_meanings,
100                                             ent.c_str());
101                 }
102         }
103
104         fl_unfreeze_form(form());
105         fl_redraw_form(form());
106 }
107
108
109 void FormThesaurus::setReplace(string const & templ, string const & nstr)
110 {
111         string str(nstr);
112
113         // the following mechanism makes sure we replace "House" with "Home",
114         // "HOUSE" with "HOME" etc.
115
116         bool all_lower = true;
117         bool all_upper = true;
118
119         string::const_iterator beg = templ.begin();
120         string::const_iterator end = templ.end();
121         string::const_iterator cit = beg;
122         for (; cit != end; ++cit) {
123                 if (isupper(*cit))
124                         all_lower = false;
125                 if (islower(*cit))
126                         all_upper = false;
127         }
128
129         if (all_lower) {
130                 str = lowercase(nstr);
131         } else if (all_upper) {
132                 str = uppercase(nstr);
133         } else if (templ.size() > 0 && isupper(templ[0])) {
134                 bool rest_lower = true;
135                 string::const_iterator cit2 = beg + 1;
136
137                 for (; cit2 != end; ++cit2) {
138                         if (isupper(*cit2))
139                                 rest_lower = false;
140                 }
141
142                 if (rest_lower) {
143                         str = lowercase(nstr);
144                         str[0] = uppercase(nstr[0]);
145                 }
146         }
147
148         fl_set_input(dialog_->input_replace, str.c_str());
149 }
150
151
152 ButtonPolicy::SMInput FormThesaurus::input(FL_OBJECT * obj, long)
153 {
154         if (obj == dialog_->input_entry) {
155                 string s = trim(fl_get_input(dialog_->input_entry));
156
157                 updateMeanings(s);
158
159                 if (s.empty()) {
160                         fl_set_input(dialog_->input_replace, "");
161                         return ButtonPolicy::SMI_APPLY;
162                 }
163                 return ButtonPolicy::SMI_NOOP;
164
165         } else if (obj == dialog_->button_replace) {
166                 string rep(fl_get_input(dialog_->input_replace));
167                 if (!rep.empty())
168                         controller().replace(fl_get_input(dialog_->input_replace));
169                 clickline_ = -1;
170                 updateMeanings(rep);
171                 return ButtonPolicy::SMI_APPLY;
172         } else if (obj != dialog_->browser_meanings) {
173                 return ButtonPolicy::SMI_NOOP;
174         }
175
176         int const line = fl_get_browser(obj);
177         if (line > 0) {
178                 setReplace(fl_get_input(dialog_->input_entry),
179                            trim(fl_get_browser_line(obj, line)));
180         }
181
182         if (clickline_ == fl_get_browser(obj)) {
183                 updateMeanings(fl_get_input(dialog_->input_replace));
184                 clickline_ = -1;
185         } else {
186                 clickline_ = fl_get_browser(obj);
187         }
188
189         return ButtonPolicy::SMI_VALID;
190 }