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