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*/)
70 modifiers.push_back(mod + (nmod << 16));
71 sequence.push_back(key);
75 return curmap->lookup(key, mod, this);
81 /* ---F+------------------------------------------------------------------ *\
82 Function : kb_sequence::parse
84 Purpose : parse a string that holds a key sequence and add the keys
85 Parameters: s - string holding the key sequence
86 Returns : 0 - if ok, error pos if error
87 Note : Keys must be separated with whitespace;
88 Use the keysym names used by XStringToKeysym
89 Prefixes are S-, C-, M- for shift, control, meta
90 \* ---F------------------------------------------------------------------- */
92 int kb_sequence::parse(string const & s)
94 if (s.empty()) return 1;
96 string::size_type i = 0;
97 unsigned int mod = 0, nmod = 0;
98 while (i < s.length()) {
99 if (s[i] && (s[i]) <= ' ') ++i;
100 if (i >= s.length()) break;
102 if (s[i + 1] == '-') { // is implicit that s[i] == true
119 } else if (s[i] == '~' && s[i + 1] && s[i + 2] == '-') {
138 string::size_type j = i;
139 for (; j < s.length() && s[j] > ' '; ++j)
140 tbuf += s[j]; // (!!!check bounds :-)
142 KeySym key = XStringToKeysym(tbuf.c_str());
143 if (key == NoSymbol) {
145 << "kbmap.C: No such keysym: "
151 addkey(key, mod, nmod);
160 /* ---F+------------------------------------------------------------------ *\
161 Function : kb_sequence::print
163 Purpose : print the currently defined sequence into a string
164 Parameters: buf - string where the result goes
165 maxlen - length of string (including '\0')
166 when_defined - only print when sequence is real: length > 0.
167 Returns : 0, if ok, -1 if string too long
168 \* ---F------------------------------------------------------------------- */
170 int kb_sequence::print(string & buf, bool when_defined) const
175 if (l < 0 && !when_defined ) l = -l;
177 for (int i = 0; i < l; ++i) {
179 mod = modifiers[i] & 0xffff;
181 printKeysym(key, mod, buf); // RVDK_PATCH_5
183 if (i + 1 < l) { // append a blank
191 /* ---F+------------------------------------------------------------------ *\
192 Function : kb_sequence::printOptions
194 Purpose : print the available key options from the current state in the
195 sequence. RVDK_PATCH_5
196 Parameters: buf - string where the result goes
197 maxlen - length of string (including '\0')
198 Returns : 0, if ok, -1 if string too long
199 \* ---F------------------------------------------------------------------- */
201 int kb_sequence::printOptions(string & buf) const
205 if (!curmap) return -1;
206 buf += _(" options: ");
212 /* ---F+------------------------------------------------------------------ *\
213 Function : kb_sequence::delseq
215 Purpose : mark the sequence as deleted
218 \* ---F------------------------------------------------------------------- */
220 void kb_sequence::delseq()
222 // negative length marks sequence as deleted, but we can still
223 // print() it or retrieve the last char using getiso()
228 /* ---F+------------------------------------------------------------------ *\
229 Function : kb_sequence::getsym
230 Called by : [user], getiso
231 Purpose : get the keysym of the last key in sequence
234 \* ---F------------------------------------------------------------------- */
236 unsigned int kb_sequence::getsym() const
239 if (l == 0) return NoSymbol;
241 return sequence[l - 1];
245 /* ---F+------------------------------------------------------------------ *\
246 Function : kb_sequence::getiso
248 Purpose : return iso character code of last key, if any
250 Returns : iso code or 0 if none
251 \* ---F------------------------------------------------------------------- */
253 char kb_sequence::getiso() const
255 int const c = getsym();
263 /* ---F+------------------------------------------------------------------ *\
264 Function : kb_sequence::reset
266 Purpose : reset sequence to initial state. RVDK_PATCH_5
269 \* ---F------------------------------------------------------------------- */
271 void kb_sequence::reset()
275 if (length > 0) length = -length;
278 /* === End of File: kbmap.C ============================================== */