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