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 //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 (s[i + 1] == '-') { // is implicit that s[i] == true
123 } else if (s[i] == '~' && s[i + 1] && s[i + 2] == '-') {
142 string::size_type j = i;
143 for (; j < s.length() && s[j] > ' '; ++j)
144 tbuf += s[j]; // (!!!check bounds :-)
146 KeySym key = XStringToKeysym(tbuf.c_str());
147 if (key == NoSymbol) {
149 << "kbmap.C: No such keysym: "
155 addkey(key, mod, nmod);
164 /* ---F+------------------------------------------------------------------ *\
165 Function : kb_sequence::print
167 Purpose : print the currently defined sequence into a string
168 Parameters: buf - string where the result goes
169 when_defined - only print when sequence is real: length > 0.
170 Returns : 0, if ok, -1 if string too long
171 \* ---F------------------------------------------------------------------- */
173 int kb_sequence::print(string & buf, bool when_defined) const
175 //lyxerr << "kb_sequence::print: length is [" << length << "]" << endl;
180 if (l < 0 && !when_defined ) l = -l;
182 for (int i = 0; i < l; ++i) {
184 mod = modifiers[i] & 0xffff;
185 //lyxerr << "kb_sequence::sequence[" << i << "] == [" << key << "]\n"
186 // << "kb_sequence::modifiers[" << i << "] == [" << mod << "]"
189 printKeysym(key, mod, buf); // RVDK_PATCH_5
191 if (i + 1 < l) { // append a blank
199 /* ---F+------------------------------------------------------------------ *\
200 Function : kb_sequence::printOptions
202 Purpose : print the available key options from the current state in the
203 sequence. RVDK_PATCH_5
204 Parameters: buf - string where the result goes
205 maxlen - length of string (including '\0')
206 Returns : 0, if ok, -1 if string too long
207 \* ---F------------------------------------------------------------------- */
209 int kb_sequence::printOptions(string & buf) const
213 if (!curmap) return -1;
214 buf += _(" options: ");
220 /* ---F+------------------------------------------------------------------ *\
221 Function : kb_sequence::delseq
223 Purpose : mark the sequence as deleted
226 \* ---F------------------------------------------------------------------- */
228 void kb_sequence::delseq()
230 // negative length marks sequence as deleted, but we can still
231 // print() it or retrieve the last char using getiso()
236 /* ---F+------------------------------------------------------------------ *\
237 Function : kb_sequence::getsym
238 Called by : [user], getiso
239 Purpose : get the keysym of the last key in sequence
242 \* ---F------------------------------------------------------------------- */
244 unsigned int kb_sequence::getsym() const
247 if (l == 0) return NoSymbol;
249 return sequence[l - 1];
253 /* ---F+------------------------------------------------------------------ *\
254 Function : kb_sequence::getiso
256 Purpose : return iso character code of last key, if any
258 Returns : iso code or 0 if none
259 \* ---F------------------------------------------------------------------- */
261 char kb_sequence::getiso() const
263 int const c = getsym();
271 /* ---F+------------------------------------------------------------------ *\
272 Function : kb_sequence::reset
274 Purpose : reset sequence to initial state. RVDK_PATCH_5
277 \* ---F------------------------------------------------------------------- */
279 void kb_sequence::reset()
283 if (length > 0) length = -length;
286 /* === End of File: kbmap.C ============================================== */