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