]> git.lyx.org Git - lyx.git/blob - src/trans_mgr.C
- Link against qt-mt333.lib which is what the current qt3 cvs produces
[lyx.git] / src / trans_mgr.C
1 /**
2  * \file trans_mgr.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Matthias Ettrich
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "trans_mgr.h"
15
16 #include "BufferView.h"
17 #include "cursor.h"
18 #include "debug.h"
19 #include "lyxrc.h"
20 #include "lyxtext.h"
21 #include "trans.h"
22
23 #include "insets/insetlatexaccent.h"
24
25 #include "support/lstrings.h"
26
27 using lyx::support::split;
28
29 using std::endl;
30 using std::string;
31 using std::pair;
32
33
34 extern string const DoAccent(string const &, tex_accent);
35 extern string const DoAccent(char, tex_accent);
36
37
38 // TransFSMData
39 TransFSMData::TransFSMData()
40 {
41         deadkey_ = deadkey2_ = 0;
42         deadkey_info_.accent = deadkey2_info_.accent = TEX_NOACCENT;
43 }
44
45
46 // TransState
47 char const TransState::TOKEN_SEP = 4;
48
49
50 // TransInitState
51 TransInitState::TransInitState()
52 {
53         init_state_ = this;
54 }
55
56
57 string const TransInitState::normalkey(char c)
58 {
59         string res;
60         res = c;
61         return res;
62 }
63
64
65 string const TransInitState::deadkey(char c, KmodInfo d)
66 {
67         deadkey_ = c;
68         deadkey_info_ = d;
69         currentState = deadkey_state_;
70         return string();
71 }
72
73
74 // TransDeadkeyState
75 TransDeadkeyState::TransDeadkeyState()
76 {
77         deadkey_state_ = this;
78 }
79
80
81 string const TransDeadkeyState::normalkey(char c)
82 {
83         string res;
84
85         KmodException::iterator it = deadkey_info_.exception_list.begin();
86         KmodException::iterator end = deadkey_info_.exception_list.end();
87
88         for (; it != end; ++it) {
89                 if (it->c == c) {
90                         res = it->data;
91                         break;
92                 }
93         }
94         if (it == end) {
95                 res = DoAccent(c, deadkey_info_.accent);
96         }
97         currentState = init_state_;
98         return res;
99 }
100
101
102 string const TransDeadkeyState::deadkey(char c, KmodInfo d)
103 {
104         string res;
105
106         // Check if the same deadkey was typed twice
107         if (deadkey_ == c) {
108                 res = deadkey_;
109                 deadkey_ = 0;
110                 deadkey_info_.accent = TEX_NOACCENT;
111                 currentState = init_state_;
112                 return res;
113         }
114
115         // Check if it is a combination or an exception
116         KmodException::const_iterator cit = deadkey_info_.exception_list.begin();
117         KmodException::const_iterator end = deadkey_info_.exception_list.end();
118         for (; cit != end; ++cit) {
119                 if (cit->combined == true && cit->accent == d.accent) {
120                         deadkey2_ = c;
121                         deadkey2_info_ = d;
122                         comb_info_ = (*cit);
123                         currentState = combined_state_;
124                         return string();
125                 }
126                 if (cit->c == c) {
127                         res = cit->data;
128                         deadkey_ = 0;
129                         deadkey_info_.accent = TEX_NOACCENT;
130                         currentState = init_state_;
131                         return res;
132                 }
133         }
134
135         // Not a combination or an exception.
136         // Output deadkey1 and keep deadkey2
137
138         if (deadkey_!= 0)
139                 res = deadkey_;
140         deadkey_ = c;
141         deadkey_info_ = d;
142         currentState = deadkey_state_;
143         return res;
144 }
145
146
147 TransCombinedState::TransCombinedState()
148 {
149         combined_state_ = this;
150 }
151
152
153 string const TransCombinedState::normalkey(char c)
154 {
155         string const temp = DoAccent(c, deadkey2_info_.accent);
156         string const res = DoAccent(temp, deadkey_info_.accent);
157         currentState = init_state_;
158         return res;
159 }
160
161
162 string const TransCombinedState::deadkey(char c, KmodInfo d)
163 {
164         // Third key in a row. Output the first one and
165         // reenter with shifted deadkeys
166         string res;
167         if (deadkey_ != 0)
168                 res = deadkey_;
169         res += TOKEN_SEP;
170         deadkey_ = deadkey2_;
171         deadkey_info_ = deadkey2_info_;
172         res += deadkey_state_->deadkey(c, d);
173         return res;
174 }
175
176
177 // TransFSM
178 TransFSM::TransFSM():
179         TransFSMData(),
180         TransInitState(),
181         TransDeadkeyState(),
182         TransCombinedState()
183 {
184         currentState = init_state_;
185 }
186
187
188 // TransManager
189
190 // Initialize static member.
191 Trans TransManager::default_;
192
193
194 TransManager::TransManager()
195         : active_(0), t1_(new Trans), t2_(new Trans)
196 {}
197
198
199 // For the sake of boost::scoped_ptr.
200 TransManager::~TransManager()
201 {}
202
203
204 int TransManager::SetPrimary(string const & language)
205 {
206         if (t1_->GetName() == language)
207                 return 0;
208
209         return t1_->Load(language);
210 }
211
212
213 int TransManager::SetSecondary(string const & language)
214 {
215         if (t2_->GetName() == language)
216                 return 0;
217
218         return t2_->Load(language);
219 }
220
221
222 bool TransManager::setCharset(string const & str)
223 {
224         return chset_.loadFile(str);
225 }
226
227
228 void TransManager::EnablePrimary()
229 {
230         if (t1_->IsDefined())
231                 active_ = t1_.get();
232
233         lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
234 }
235
236
237 void TransManager::EnableSecondary()
238 {
239         if (t2_->IsDefined())
240                 active_ = t2_.get();
241         lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
242 }
243
244
245 void TransManager::DisableKeymap()
246 {
247         active_ = &default_;
248         lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
249 }
250
251
252 void  TransManager::TranslateAndInsert(char c, LyXText * text)
253 {
254         string res = active_->process(c, *this);
255
256         // Process with tokens
257         string temp;
258
259         while (res.length() > 0) {
260                 res = split(res, temp, TransState::TOKEN_SEP);
261                 insert(temp, text);
262         }
263 }
264
265
266 void TransManager::insertVerbatim(string const & str, LyXText * text)
267 {
268         for (string::size_type i = 0, n = str.size(); i < n; ++i)
269                 text->insertChar(text->bv()->cursor(), str[i]);
270 }
271
272
273 void TransManager::insert(string const & str, LyXText * text)
274 {
275         // Go through the character encoding only if the current
276         // encoding (chset_->name()) matches the current font_norm
277         // (lyrxc->font_norm)
278
279         // Is false to speak about "only if" the current encoding will
280         // almost always be equal to font_norm.
281         pair<bool, int> enc = chset_.encodeString(str);
282         if (chset_.getName() != lyxrc.font_norm ||
283             !enc.first) {
284                 // Could not find an encoding
285                 InsetLatexAccent ins(str);
286                 if (ins.canDisplay()) {
287                         text->bv()->cursor().insert(new InsetLatexAccent(ins));
288                 } else {
289                         insertVerbatim(str, text);
290                 }
291                 return;
292         }
293         string const tmp(1, static_cast<char>(enc.second));
294         insertVerbatim(tmp, text);
295 }
296
297
298 void TransManager::deadkey(char c, tex_accent accent, LyXText * t)
299 {
300         if (c == 0 && active_ != &default_) {
301                 // A deadkey was pressed that cannot be printed
302                 // or a accent command was typed in the minibuffer
303                 KmodInfo i;
304                 if (active_->isAccentDefined(accent, i) == true) {
305                         string const res = trans_fsm_
306                                 .currentState->deadkey(c, i);
307                         insert(res, t);
308                         return;
309                 }
310         }
311
312         if (active_ == &default_ || c == 0) {
313                 KmodInfo i;
314                 i.accent = accent;
315                 i.data.erase();
316                 string res = trans_fsm_.currentState->deadkey(c, i);
317                 insert(res, t);
318         } else {
319                 // Go through the translation
320                 TranslateAndInsert(c, t);
321         }
322 }