]> git.lyx.org Git - lyx.git/blob - src/kbsequence.C
More very minor syncing ..
[lyx.git] / src / kbsequence.C
1 /**
2  * \file kbsequence.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <moz@compsoc.man.ac.uk>
7  */
8
9 #include <config.h>
10
11 #include "gettext.h"
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "frontends/mouse_state.h"
18 #include "kbsequence.h"
19 #include "kbmap.h"
20 #include "commandtags.h"
21 #include "debug.h"
22
23 #include <X11/Xlib.h>
24
25 using std::make_pair;
26 using std::vector;
27 using std::endl;
28 using std::hex;
29 using std::dec;
30
31
32 // The only modifiers that we handle. We want to throw away things
33 // like NumLock.
34 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
35
36
37 int kb_sequence::addkey(unsigned int key, key_modifier::state mod, key_modifier::state nmod)
38 {
39         // adding a key to a deleted sequence
40         // starts a new sequence
41         if (deleted_) {
42                 deleted_ = false;
43                 length_ = 0;
44                 sequence.clear();
45                 modifiers.clear();
46         }
47
48         modifiers.push_back(make_pair(mod, nmod));
49         sequence.push_back(key);
50         ++length_;
51
52         if (curmap) {
53                 return curmap->lookup(key, mod, this);
54         }
55
56         return LFUN_UNKNOWN_ACTION;
57 }
58
59
60 string::size_type kb_sequence::parse(string const & s)
61 {
62         if (s.empty()) return 1;
63
64         string::size_type i = 0;
65         key_modifier::state mod = key_modifier::none;
66         key_modifier::state nmod = key_modifier::none;
67  
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 |= key_modifier::shift;
78                                 i += 2;
79                                 continue;
80                         case 'c': case 'C':
81                                 mod |= key_modifier::ctrl;
82                                 i += 2;
83                                 continue;
84                         case 'm': case 'M':
85                                 mod |= key_modifier::alt;
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 |= key_modifier::shift;
96                                 i += 3;
97                                 continue;
98                         case 'c': case 'C':
99                                 nmod |= key_modifier::ctrl;
100                                 i += 3;
101                                 continue;
102                         case 'm': case 'M':
103                                 nmod |= key_modifier::alt;
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 = key_modifier::none;
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].first);
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 }