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