]> git.lyx.org Git - lyx.git/blob - src/trans_mgr.h
A better fix for bug 675:
[lyx.git] / src / trans_mgr.h
1 // -*- C++ -*-
2 /**
3  * \file trans_mgr.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Matthias Ettrich
9  * \author John Levon
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef TRANS_MANAGER_H
15 #define TRANS_MANAGER_H
16
17 #include "chset.h"
18 #include "trans_decl.h"
19
20 #include <boost/scoped_ptr.hpp>
21
22
23 namespace lyx {
24
25 class LCursor;
26 class LyXText;
27 class Trans;
28
29 /// Translation state
30 class TransState {
31 public:
32         ///
33         virtual ~TransState() {}
34         ///
35         virtual std::string const normalkey(char) = 0;
36         ///
37         virtual bool backspace() = 0;
38         ///
39         virtual std::string const deadkey(char, KmodInfo) = 0;
40         ///
41         static char const TOKEN_SEP;
42 };
43
44
45 /// Translation FSM
46 class TransFSMData {
47 protected:
48         ///
49         virtual ~TransFSMData() {}
50         ///
51         char deadkey_;
52         ///
53         KmodInfo deadkey_info_;
54         ///
55         char deadkey2_;
56         ///
57         KmodInfo deadkey2_info_;
58         ///
59         Keyexc comb_info_;
60         ///
61         TransState * init_state_;
62         ///
63         TransState * deadkey_state_;
64         ///
65         TransState * combined_state_;
66         ///
67 public:
68         ///
69         TransFSMData();
70         ///
71         TransState * currentState;
72 };
73
74
75 /// Init State
76 class TransInitState :virtual public TransFSMData, public TransState {
77 public:
78         ///
79         TransInitState();
80         ///
81         virtual std::string const normalkey(char);
82         ///
83         virtual bool backspace() { return true; }
84         ///
85         virtual std::string const deadkey(char, KmodInfo);
86 };
87
88
89 /// Deadkey State
90 class TransDeadkeyState : virtual public TransFSMData, public TransState {
91 public:
92         ///
93         TransDeadkeyState();
94         ///
95         virtual std::string const normalkey(char);
96         ///
97         virtual bool backspace() {
98                 currentState = init_state_;
99                 return false;
100         }
101         ///
102         virtual std::string const deadkey(char, KmodInfo);
103 };
104
105
106 /// Combined State
107 class TransCombinedState : virtual public TransFSMData, public TransState {
108 public:
109         ///
110         TransCombinedState();
111         ///
112         virtual std::string const normalkey(char);
113         ///
114         virtual bool backspace() {
115                 // cancel the second deadkey
116                 deadkey2_ = 0;
117                 deadkey2_info_.accent = TEX_NOACCENT;
118                 currentState = deadkey_state_;
119
120                 return false;
121         }
122         ///
123         virtual std::string const deadkey(char, KmodInfo);
124 };
125
126
127 ///
128 class TransFSM : virtual public TransFSMData,
129                  public TransInitState,
130                  public TransDeadkeyState,
131                  public TransCombinedState {
132 public:
133         ///
134         TransFSM();
135 };
136
137
138 ///
139 class TransManager {
140 private:
141         ///
142         TransFSM trans_fsm_;
143         ///
144         Trans * active_;
145         ///
146         boost::scoped_ptr<Trans> t1_;
147         ///
148         boost::scoped_ptr<Trans> t2_;
149         ///
150         static Trans default_;
151         ///
152         CharacterSet chset_;
153         ///
154         void insert(std::string const &, LyXText *, LCursor & cur);
155         ///
156         void insertVerbatim(std::string const &, LyXText *, LCursor & cur);
157 public:
158         ///
159         TransManager();
160         ///
161         ~TransManager();
162         ///
163         int setPrimary(std::string const &);
164         ///
165         int setSecondary(std::string const &);
166         ///
167         void enablePrimary();
168         ///
169         void enableSecondary();
170         ///
171         void disableKeymap();
172         ///
173         bool setCharset(std::string const &);
174         ///
175         bool backspace() {
176                 return trans_fsm_.currentState->backspace();
177         }
178         ///
179         void translateAndInsert(char, LyXText *, LCursor &);
180         ///
181         std::string const deadkey(char, KmodInfo);
182         ///
183         std::string const normalkey(char);
184         ///
185         void deadkey(char, tex_accent, LyXText *, LCursor &);
186 };
187
188
189 inline
190 std::string const TransManager::normalkey(char c)
191 {
192         return trans_fsm_.currentState->normalkey(c);
193 }
194
195
196 inline
197 std::string const TransManager::deadkey(char c, KmodInfo t)
198 {
199         return trans_fsm_.currentState->deadkey(c, t);
200 }
201
202
203 } // namespace lyx
204
205 #endif // TRANS_MANAGER_H