]> git.lyx.org Git - lyx.git/blob - src/trans_mgr.h
* Painter.h:
[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 LyXText;
26 class Trans;
27
28 /// Translation state
29 class TransState {
30 public:
31         ///
32         virtual ~TransState() {}
33         ///
34         virtual std::string const normalkey(char) = 0;
35         ///
36         virtual bool backspace() = 0;
37         ///
38         virtual std::string const deadkey(char, KmodInfo) = 0;
39         ///
40         static char const TOKEN_SEP;
41 };
42
43
44 /// Translation FSM
45 class TransFSMData {
46 protected:
47         ///
48         virtual ~TransFSMData() {}
49         ///
50         char deadkey_;
51         ///
52         KmodInfo deadkey_info_;
53         ///
54         char deadkey2_;
55         ///
56         KmodInfo deadkey2_info_;
57         ///
58         Keyexc comb_info_;
59         ///
60         TransState * init_state_;
61         ///
62         TransState * deadkey_state_;
63         ///
64         TransState * combined_state_;
65         ///
66 public:
67         ///
68         TransFSMData();
69         ///
70         TransState * currentState;
71 };
72
73
74 /// Init State
75 class TransInitState :virtual public TransFSMData, public TransState {
76 public:
77         ///
78         TransInitState();
79         ///
80         virtual std::string const normalkey(char);
81         ///
82         virtual bool backspace() { return true; }
83         ///
84         virtual std::string const deadkey(char, KmodInfo);
85 };
86
87
88 /// Deadkey State
89 class TransDeadkeyState : virtual public TransFSMData, public TransState {
90 public:
91         ///
92         TransDeadkeyState();
93         ///
94         virtual std::string const normalkey(char);
95         ///
96         virtual bool backspace() {
97                 currentState = init_state_;
98                 return false;
99         }
100         ///
101         virtual std::string const deadkey(char, KmodInfo);
102 };
103
104
105 /// Combined State
106 class TransCombinedState : virtual public TransFSMData, public TransState {
107 public:
108         ///
109         TransCombinedState();
110         ///
111         virtual std::string const normalkey(char);
112         ///
113         virtual bool backspace() {
114                 // cancel the second deadkey
115                 deadkey2_ = 0;
116                 deadkey2_info_.accent = TEX_NOACCENT;
117                 currentState = deadkey_state_;
118
119                 return false;
120         }
121         ///
122         virtual std::string const deadkey(char, KmodInfo);
123 };
124
125
126 ///
127 class TransFSM : virtual public TransFSMData,
128                  public TransInitState,
129                  public TransDeadkeyState,
130                  public TransCombinedState {
131 public:
132         ///
133         TransFSM();
134 };
135
136
137 ///
138 class TransManager {
139 private:
140         ///
141         TransFSM trans_fsm_;
142         ///
143         Trans * active_;
144         ///
145         boost::scoped_ptr<Trans> t1_;
146         ///
147         boost::scoped_ptr<Trans> t2_;
148         ///
149         static Trans default_;
150         ///
151         CharacterSet chset_;
152         ///
153         void insert(std::string const &, LyXText *);
154         ///
155         void insertVerbatim(std::string const &, LyXText *);
156 public:
157         ///
158         TransManager();
159         ///
160         ~TransManager();
161         ///
162         int setPrimary(std::string const &);
163         ///
164         int setSecondary(std::string const &);
165         ///
166         void enablePrimary();
167         ///
168         void enableSecondary();
169         ///
170         void disableKeymap();
171         ///
172         bool setCharset(std::string const &);
173         ///
174         bool backspace() {
175                 return trans_fsm_.currentState->backspace();
176         }
177         ///
178         void translateAndInsert(char, LyXText *);
179         ///
180         std::string const deadkey(char, KmodInfo);
181         ///
182         std::string const normalkey(char);
183         ///
184         void deadkey(char, tex_accent, LyXText *);
185 };
186
187
188 inline
189 std::string const TransManager::normalkey(char c)
190 {
191         return trans_fsm_.currentState->normalkey(c);
192 }
193
194
195 inline
196 std::string const TransManager::deadkey(char c, KmodInfo t)
197 {
198         return trans_fsm_.currentState->deadkey(c, t);
199 }
200
201
202 } // namespace lyx
203
204 #endif // TRANS_MANAGER_H