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