1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
18 #pragma implementation
21 #include "kbsequence.h"
27 // The only modifiers that we handle. We want to throw away things
29 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
32 // === static functions ===================================================
35 /* ---F+------------------------------------------------------------------ *\
36 Function : printKeysym
37 Called by : kb_sequence::print and printKeyMap. RVDK_PATCH_5
38 Purpose : prints a keysym, including modifiers.
39 Parameters: key - keysym
41 buf - string where the result goes
42 maxlen - length of string (including '\0')
43 Returns : length of printed string if ok, 0 otherwise.
44 \* ---F------------------------------------------------------------------- */
46 void printKeysym(unsigned int key, unsigned int mod, string & buf);
49 // === kb_sequence methods ================================================
51 /* ---F+------------------------------------------------------------------ *\
52 Function : kb_sequence::addkey
54 Purpose : add a key to the sequence, look up in map and return action
55 Parameters: key - keysym of key
57 nmod - modifier veto mask (unused now)
58 Returns : action or -1 if error (no map defined or key not found)
59 \* ---F------------------------------------------------------------------- */
61 int kb_sequence::addkey(unsigned int key,
62 unsigned int mod, unsigned int nmod /*= 0*/)
64 //lyxerr << "kb_sequence::addkey: length is [" << length << "]\n"
65 // << "kb_sequence::addkey::key == [" << key << "]\n"
66 // << "kb_sequence::addkey::mod == [" << mod << "]" << endl;
74 modifiers.push_back(mod + (nmod << 16));
75 sequence.push_back(key);
79 return curmap->lookup(key, mod, this);
85 /* ---F+------------------------------------------------------------------ *\
86 Function : kb_sequence::parse
88 Purpose : parse a string that holds a key sequence and add the keys
89 Parameters: s - string holding the key sequence
90 Returns : 0 - if ok, error pos if error
91 Note : Keys must be separated with whitespace;
92 Use the keysym names used by XStringToKeysym
93 Prefixes are S-, C-, M- for shift, control, meta
94 \* ---F------------------------------------------------------------------- */
96 int kb_sequence::parse(string const & s)
98 if (s.empty()) return 1;
100 string::size_type i = 0;
101 unsigned int mod = 0, nmod = 0;
102 while (i < s.length()) {
103 if (s[i] && (s[i]) <= ' ') ++i;
104 if (i >= s.length()) break;
106 if (i + 1 < s.length() && s[i + 1] == '-') {
123 } else if (i + 2 < s.length() && s[i] == '~'
124 && s[i + 2] == '-') {
143 string::size_type j = i;
144 for (; j < s.length() && s[j] > ' '; ++j)
145 tbuf += s[j]; // (!!!check bounds :-)
147 KeySym key = XStringToKeysym(tbuf.c_str());
148 if (key == NoSymbol) {
150 << "kbmap.C: No such keysym: "
156 addkey(key, mod, nmod);
165 /* ---F+------------------------------------------------------------------ *\
166 Function : kb_sequence::print
168 Purpose : print the currently defined sequence into a string
169 Parameters: buf - string where the result goes
170 when_defined - only print when sequence is real: length > 0.
171 Returns : 0, if ok, -1 if string too long
172 \* ---F------------------------------------------------------------------- */
174 int kb_sequence::print(string & buf, bool when_defined) const
176 //lyxerr << "kb_sequence::print: length is [" << length << "]" << endl;
181 if (l < 0 && !when_defined ) l = -l;
183 for (int i = 0; i < l; ++i) {
185 mod = modifiers[i] & 0xffff;
186 //lyxerr << "kb_sequence::sequence[" << i << "] == [" << key << "]\n"
187 // << "kb_sequence::modifiers[" << i << "] == [" << mod << "]"
190 printKeysym(key, mod, buf); // RVDK_PATCH_5
192 if (i + 1 < l) { // append a blank
200 /* ---F+------------------------------------------------------------------ *\
201 Function : kb_sequence::printOptions
203 Purpose : print the available key options from the current state in the
204 sequence. RVDK_PATCH_5
205 Parameters: buf - string where the result goes
206 maxlen - length of string (including '\0')
207 Returns : 0, if ok, -1 if string too long
208 \* ---F------------------------------------------------------------------- */
210 int kb_sequence::printOptions(string & buf) const
214 if (!curmap) return -1;
215 buf += _(" options: ");
221 /* ---F+------------------------------------------------------------------ *\
222 Function : kb_sequence::delseq
224 Purpose : mark the sequence as deleted
227 \* ---F------------------------------------------------------------------- */
229 void kb_sequence::delseq()
231 // negative length marks sequence as deleted, but we can still
232 // print() it or retrieve the last char using getiso()
237 /* ---F+------------------------------------------------------------------ *\
238 Function : kb_sequence::getsym
239 Called by : [user], getiso
240 Purpose : get the keysym of the last key in sequence
243 \* ---F------------------------------------------------------------------- */
245 unsigned int kb_sequence::getsym() const
248 if (l == 0) return NoSymbol;
250 return sequence[l - 1];
254 /* ---F+------------------------------------------------------------------ *\
255 Function : kb_sequence::getiso
257 Purpose : return iso character code of last key, if any
259 Returns : iso code or 0 if none
260 \* ---F------------------------------------------------------------------- */
262 char kb_sequence::getiso() const
264 unsigned int const c = getsym();
266 lyxerr[Debug::KBMAP] << "Raw keysym: "
267 << std::hex << c << std::dec << endl;
268 lyxerr[Debug::KBMAP] << "byte 3: "
269 << std::hex << (c & 0x0000FF00) << std::dec
272 switch (c & 0x0000FF00) {
273 // latin 1 byte 3 = 0
276 // latin 2 byte 3 = 1
278 // latin 3 byte 3 = 2
280 // latin 4 byte 3 = 3
282 // latin 8 byte 3 = 18 (0x12)
284 // latin 9 byte 3 = 19 (0x13)
286 return c & 0x000000FF;
291 // not a latin char we know of
292 // Yes but this is already handled above (JMarc)
297 /* ---F+------------------------------------------------------------------ *\
298 Function : kb_sequence::reset
300 Purpose : reset sequence to initial state. RVDK_PATCH_5
303 \* ---F------------------------------------------------------------------- */
305 void kb_sequence::reset()
309 if (length > 0) length = -length;
312 /* === End of File: kbmap.C ============================================== */