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