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