]> git.lyx.org Git - lyx.git/blob - src/kbsequence.C
fix typo that put too many include paths for most people
[lyx.git] / src / kbsequence.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12 //#include <cstring>
13 #include <X11/Xlib.h>
14
15 #include "gettext.h"
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "kbsequence.h"
22 #include "kbmap.h"
23 #include "commandtags.h"
24 #include "debug.h"
25
26
27 using std::vector;
28 using std::endl;
29 using std::hex;
30 using std::dec;
31
32
33 // The only modifiers that we handle. We want to throw away things
34 // like NumLock.
35 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
36
37
38 int kb_sequence::addkey(unsigned int key, unsigned int mod, unsigned int nmod)
39 {
40         // adding a key to a deleted sequence
41         // starts a new sequence
42         if (deleted_) {
43                 deleted_ = false;
44                 length_ = 0;
45                 sequence.clear();
46                 modifiers.clear();
47         }
48
49         modifiers.push_back(mod + (nmod << 16));
50         sequence.push_back(key);
51         ++length_;
52
53         if (curmap) {
54                 return curmap->lookup(key, mod, this);
55         }
56
57         return LFUN_UNKNOWN_ACTION;
58 }
59
60
61 string::size_type kb_sequence::parse(string const & s)
62 {
63         if (s.empty()) return 1;
64
65         string::size_type i = 0;
66         unsigned int mod = 0;
67         unsigned int nmod = 0;
68         while (i < s.length()) {
69                 if (s[i] == ' ')
70                         ++i;
71                 if (i >= s.length())
72                         break;
73
74                 if (i + 1 < s.length() && s[i + 1] == '-') {
75                         switch (s[i]) {
76                         case 's': case 'S':
77                                 mod |= ShiftMask;
78                                 i += 2;
79                                 continue;
80                         case 'c': case 'C':
81                                 mod |= ControlMask;
82                                 i += 2;
83                                 continue;
84                         case 'm': case 'M':
85                                 mod |= Mod1Mask;
86                                 i += 2;
87                                 continue;
88                         default:
89                                 return i + 1;
90                         }
91                 } else if (i + 2 < s.length() && s[i] == '~'
92                            && s[i + 2] == '-') {
93                         switch (s[i + 1]) {
94                         case 's': case 'S':
95                                 nmod |= ShiftMask;
96                                 i += 3;
97                                 continue;
98                         case 'c': case 'C':
99                                 nmod |= ControlMask;
100                                 i += 3;
101                                 continue;
102                         case 'm': case 'M':
103                                 nmod |= Mod1Mask;
104                                 i += 3;
105                                 continue;
106                         default:
107                                 return i + 2;
108                         }
109                 } else {
110                         string tbuf;
111                         string::size_type j = i;
112                         for (; j < s.length() && s[j] != ' '; ++j)
113                                 tbuf += s[j];    // (!!!check bounds :-)
114
115                         KeySym key = XStringToKeysym(tbuf.c_str());
116                         if (key == NoSymbol) {
117                                 lyxerr[Debug::KBMAP]
118                                         << "kbmap.C: No such keysym: "
119                                         << tbuf << endl;
120                                 return j;
121                         }
122                         i = j;
123
124                         addkey(key, mod, nmod);
125                         mod = 0;
126                 }
127         }
128
129         // empty sequence?
130         if (!length_)
131                 return 0;
132
133         // everything is fine
134         return string::npos;
135 }
136
137
138 string const kb_sequence::print() const
139 {
140         string buf;
141
142         //if (deleted_)
143         //      return buf;
144
145         for (vector<unsigned int>::size_type i = 0; i < length_; ++i) {
146                 buf += kb_keymap::printKeysym(sequence[i], modifiers[i] & 0xffff);
147
148                 // append a blank
149                 if (i + 1 < length_) {
150                         buf += ' ';
151                 }
152         }
153         return buf;
154 }
155
156
157 string const kb_sequence::printOptions() const
158 {
159         string buf;
160
161         buf += print();
162
163         if (!curmap)
164                 return buf;
165
166         buf += _("   options: ");
167         buf += curmap->print();
168         return buf;
169 }
170
171
172 void kb_sequence::mark_deleted()
173 {
174         deleted_ = true;
175 }
176
177
178 unsigned int kb_sequence::getsym() const
179 {
180         if (length_ == 0) return NoSymbol;
181         return sequence[length_ - 1];
182 }
183
184
185 char kb_sequence::getiso() const
186 {
187         unsigned int const c = getsym();
188
189         lyxerr[Debug::KBMAP] << "Raw keysym: "
190                              << hex << c << dec << endl;
191         lyxerr[Debug::KBMAP] << "byte 3: "
192                              << hex << (c & 0xff00) << dec
193                              << endl;
194         return kb_keymap::getiso(c);
195 }
196
197
198 void kb_sequence::reset()
199 {
200         mark_deleted();
201         curmap = stdmap;
202 }
203
204 void kb_sequence::clear()
205 {
206         length_ = 0;
207         reset();
208 }