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