1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 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 if(length < 0) length = 0;
66 if(length + 1 >= size) {
67 unsigned int * nseq = new unsigned int[size + KB_PREALLOC];
69 memcpy(nseq, sequence, length * sizeof(unsigned int));
70 if(sequence != staticseq) delete sequence;
72 nseq = new unsigned int[size];
73 memcpy(nseq, modifiers, length * sizeof(unsigned int));
74 if(modifiers != staticmod) delete modifiers;
78 modifiers[length] = mod + (nmod << 16);
79 sequence[length++] = key;
82 return curmap->lookup(key, mod, this);
88 /* ---F+------------------------------------------------------------------ *\
89 Function : kb_sequence::parse
91 Purpose : parse a string that holds a key sequence and add the keys
92 Parameters: s - string holding the key sequence
93 Returns : 0 - if ok, error pos if error
94 Note : Keys must be separated with whitespace;
95 Use the keysym names used by XStringToKeysym
96 Prefixes are S-, C-, M- for shift, control, meta
97 \* ---F------------------------------------------------------------------- */
99 int kb_sequence::parse(char const * s)
104 unsigned int mod = 0, nmod = 0;
106 if(s[i] && (s[i]) <= ' ') ++i;
109 if(s[i + 1] == '-') { // is implicit that s[i] == true
126 } else if(s[i] == '~' && s[i + 1] && s[i + 2] == '-') {
146 for(; s[j] && 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);
167 /* ---F+------------------------------------------------------------------ *\
168 Function : kb_sequence::print
170 Purpose : print the currently defined sequence into a string
171 Parameters: buf - string where the result goes
172 maxlen - length of string (including '\0')
173 when_defined - only print when sequence is real: length > 0.
174 Returns : 0, if ok, -1 if string too long
175 \* ---F------------------------------------------------------------------- */
177 int kb_sequence::print(string & buf, bool when_defined) const
182 if ( l < 0 && !when_defined ) l = -l;
184 for(int i = 0; i < l; ++i) {
186 mod = modifiers[i] & 0xffff;
188 printKeysym(key, mod, buf); // RVDK_PATCH_5
190 if(i + 1 < l) { // append a blank
198 /* ---F+------------------------------------------------------------------ *\
199 Function : kb_sequence::printOptions
201 Purpose : print the available key options from the current state in the
202 sequence. RVDK_PATCH_5
203 Parameters: buf - string where the result goes
204 maxlen - length of string (including '\0')
205 Returns : 0, if ok, -1 if string too long
206 \* ---F------------------------------------------------------------------- */
208 int kb_sequence::printOptions(string & buf) const
212 if (!curmap) return -1;
213 buf += _(" options: ");
219 /* ---F+------------------------------------------------------------------ *\
220 Function : kb_sequence::delseq
222 Purpose : mark the sequence as deleted
225 \* ---F------------------------------------------------------------------- */
227 void kb_sequence::delseq()
229 // negative length marks sequence as deleted, but we can still
230 // print() it or retrieve the last char using getiso()
235 /* ---F+------------------------------------------------------------------ *\
236 Function : kb_sequence::getsym
237 Called by : [user], getiso
238 Purpose : get the keysym of the last key in sequence
241 \* ---F------------------------------------------------------------------- */
243 unsigned int kb_sequence::getsym() const
246 if(l == 0) return NoSymbol;
248 return sequence[l - 1];
252 /* ---F+------------------------------------------------------------------ *\
253 Function : kb_sequence::getiso
255 Purpose : return iso character code of last key, if any
257 Returns : iso code or 0 if none
258 \* ---F------------------------------------------------------------------- */
260 char kb_sequence::getiso() const
270 /* ---F+------------------------------------------------------------------ *\
271 Function : kb_sequence::reset
273 Purpose : reset sequence to initial state. RVDK_PATCH_5
276 \* ---F------------------------------------------------------------------- */
278 void kb_sequence::reset()
282 if (length > 0) length = -length;
285 /* === End of File: kbmap.C ============================================== */