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 : string::npos 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 string::size_type 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()) {
108 if (i + 1 < s.length() && s[i + 1] == '-') {
125 } else if (i + 2 < s.length() && s[i] == '~'
126 && s[i + 2] == '-') {
145 string::size_type j = i;
146 for (; j < s.length() && s[j] != ' '; ++j)
147 tbuf += s[j]; // (!!!check bounds :-)
149 KeySym key = XStringToKeysym(tbuf.c_str());
150 if (key == NoSymbol) {
152 << "kbmap.C: No such keysym: "
158 addkey(key, mod, nmod);
168 // everything is fine
173 /* ---F+------------------------------------------------------------------ *\
174 Function : kb_sequence::print
176 Purpose : print the currently defined sequence into a string
177 Parameters: buf - string where the result goes
178 when_defined - only print when sequence is real: length > 0.
179 Returns : 0, if ok, -1 if string too long
180 \* ---F------------------------------------------------------------------- */
182 int kb_sequence::print(string & buf, bool when_defined) const
184 //lyxerr << "kb_sequence::print: length is [" << length << "]" << endl;
189 if (l < 0 && !when_defined ) l = -l;
191 for (int i = 0; i < l; ++i) {
193 mod = modifiers[i] & 0xffff;
194 //lyxerr << "kb_sequence::sequence[" << i << "] == [" << key << "]\n"
195 // << "kb_sequence::modifiers[" << i << "] == [" << mod << "]"
198 printKeysym(key, mod, buf); // RVDK_PATCH_5
200 if (i + 1 < l) { // append a blank
208 /* ---F+------------------------------------------------------------------ *\
209 Function : kb_sequence::printOptions
211 Purpose : print the available key options from the current state in the
212 sequence. RVDK_PATCH_5
213 Parameters: buf - string where the result goes
214 maxlen - length of string (including '\0')
215 Returns : 0, if ok, -1 if string too long
216 \* ---F------------------------------------------------------------------- */
218 int kb_sequence::printOptions(string & buf) const
222 if (!curmap) return -1;
223 buf += _(" options: ");
229 /* ---F+------------------------------------------------------------------ *\
230 Function : kb_sequence::delseq
232 Purpose : mark the sequence as deleted
235 \* ---F------------------------------------------------------------------- */
237 void kb_sequence::delseq()
239 // negative length marks sequence as deleted, but we can still
240 // print() it or retrieve the last char using getiso()
245 /* ---F+------------------------------------------------------------------ *\
246 Function : kb_sequence::getsym
247 Called by : [user], getiso
248 Purpose : get the keysym of the last key in sequence
251 \* ---F------------------------------------------------------------------- */
253 unsigned int kb_sequence::getsym() const
256 if (l == 0) return NoSymbol;
258 return sequence[l - 1];
262 /* ---F+------------------------------------------------------------------ *\
263 Function : kb_sequence::getiso
265 Purpose : return iso character code of last key, if any
267 Returns : iso code or 0 if none
268 \* ---F------------------------------------------------------------------- */
270 char kb_sequence::getiso() const
272 unsigned int const c = getsym();
274 lyxerr[Debug::KBMAP] << "Raw keysym: "
275 << std::hex << c << std::dec << endl;
276 lyxerr[Debug::KBMAP] << "byte 3: "
277 << std::hex << (c & 0x0000FF00) << std::dec
280 switch (c & 0x0000FF00) {
281 // latin 1 byte 3 = 0
284 // latin 2 byte 3 = 1
286 // latin 3 byte 3 = 2
288 // latin 4 byte 3 = 3
290 // latin 8 byte 3 = 18 (0x12)
292 // latin 9 byte 3 = 19 (0x13)
294 return c & 0x000000FF;
299 // not a latin char we know of
300 // Yes but this is already handled above (JMarc)
305 /* ---F+------------------------------------------------------------------ *\
306 Function : kb_sequence::reset
308 Purpose : reset sequence to initial state. RVDK_PATCH_5
311 \* ---F------------------------------------------------------------------- */
313 void kb_sequence::reset()
317 if (length > 0) length = -length;
320 /* === End of File: kbmap.C ============================================== */