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"
25 // The only modifiers that we handle. We want to throw away things
27 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
30 // === static functions ===================================================
33 /* ---F+------------------------------------------------------------------ *\
34 Function : printKeysym
35 Called by : kb_sequence::print and printKeyMap. RVDK_PATCH_5
36 Purpose : prints a keysym, including modifiers.
37 Parameters: key - keysym
39 buf - string where the result goes
40 maxlen - length of string (including '\0')
41 Returns : length of printed string if ok, 0 otherwise.
42 \* ---F------------------------------------------------------------------- */
44 void printKeysym(unsigned int key, unsigned int mod, string & buf);
47 // === kb_sequence methods ================================================
49 /* ---F+------------------------------------------------------------------ *\
50 Function : kb_sequence::addkey
52 Purpose : add a key to the sequence, look up in map and return action
53 Parameters: key - keysym of key
55 nmod - modifier veto mask (unused now)
56 Returns : action or -1 if error (no map defined or key not found)
57 \* ---F------------------------------------------------------------------- */
59 int kb_sequence::addkey(unsigned int key,
60 unsigned int mod, unsigned int nmod /*= 0*/)
62 if(length < 0) length = 0;
64 if(length + 1 >= size) {
65 unsigned int * nseq = new unsigned int[size + KB_PREALLOC];
67 memcpy(nseq, sequence, length * sizeof(unsigned int));
68 if(sequence != staticseq) delete sequence;
70 nseq = new unsigned int[size];
71 memcpy(nseq, modifiers, length * sizeof(unsigned int));
72 if(modifiers != staticmod) delete modifiers;
76 modifiers[length] = mod + (nmod << 16);
77 sequence[length++] = key;
80 return curmap->lookup(key, mod, this);
86 /* ---F+------------------------------------------------------------------ *\
87 Function : kb_sequence::parse
89 Purpose : parse a string that holds a key sequence and add the keys
90 Parameters: s - string holding the key sequence
91 Returns : 0 - if ok, error pos if error
92 Note : Keys must be separated with whitespace;
93 Use the keysym names used by XStringToKeysym
94 Prefixes are S-, C-, M- for shift, control, meta
95 \* ---F------------------------------------------------------------------- */
97 int kb_sequence::parse(char const * s)
102 unsigned int mod = 0, nmod = 0;
104 if(s[i] && (s[i]) <= ' ') ++i;
107 if(s[i + 1] == '-') { // is implicit that s[i] == true
124 } else if(s[i] == '~' && s[i + 1] && s[i + 2] == '-') {
144 for(; s[j] && 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 maxlen - length of string (including '\0')
171 when_defined - only print when sequence is real: length > 0.
172 Returns : 0, if ok, -1 if string too long
173 \* ---F------------------------------------------------------------------- */
175 int kb_sequence::print(string & buf, bool when_defined) const
180 if ( l < 0 && !when_defined ) l = -l;
182 for(int i = 0; i < l; ++i) {
184 mod = modifiers[i] & 0xffff;
186 printKeysym(key, mod, buf); // RVDK_PATCH_5
188 if(i + 1 < l) { // append a blank
196 /* ---F+------------------------------------------------------------------ *\
197 Function : kb_sequence::printOptions
199 Purpose : print the available key options from the current state in the
200 sequence. RVDK_PATCH_5
201 Parameters: buf - string where the result goes
202 maxlen - length of string (including '\0')
203 Returns : 0, if ok, -1 if string too long
204 \* ---F------------------------------------------------------------------- */
206 int kb_sequence::printOptions(string & buf) const
210 if (!curmap) return -1;
211 buf += _(" options: ");
217 /* ---F+------------------------------------------------------------------ *\
218 Function : kb_sequence::delseq
220 Purpose : mark the sequence as deleted
223 \* ---F------------------------------------------------------------------- */
225 void kb_sequence::delseq()
227 // negative length marks sequence as deleted, but we can still
228 // print() it or retrieve the last char using getiso()
233 /* ---F+------------------------------------------------------------------ *\
234 Function : kb_sequence::getsym
235 Called by : [user], getiso
236 Purpose : get the keysym of the last key in sequence
239 \* ---F------------------------------------------------------------------- */
241 unsigned int kb_sequence::getsym() const
244 if(l == 0) return NoSymbol;
246 return sequence[l - 1];
250 /* ---F+------------------------------------------------------------------ *\
251 Function : kb_sequence::getiso
253 Purpose : return iso character code of last key, if any
255 Returns : iso code or 0 if none
256 \* ---F------------------------------------------------------------------- */
258 char kb_sequence::getiso() const
268 /* ---F+------------------------------------------------------------------ *\
269 Function : kb_sequence::reset
271 Purpose : reset sequence to initial state. RVDK_PATCH_5
274 \* ---F------------------------------------------------------------------- */
276 void kb_sequence::reset()
280 if (length > 0) length = -length;
283 /* === End of File: kbmap.C ============================================== */