]> git.lyx.org Git - lyx.git/blob - src/kbsequence.C
Fix working of the spellchecker dialog with ispell when there are no
[lyx.git] / src / kbsequence.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12 //#include <cstring>
13 #include <X11/Xlib.h>
14
15 #include "gettext.h"
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "kbsequence.h"
22 #include "kbmap.h"
23 #include "debug.h"
24
25 using std::endl;
26
27 // The only modifiers that we handle. We want to throw away things
28 // like NumLock. 
29 enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
30
31
32 // === static functions =================================================== 
33
34
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
40                mod    - modifiers
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------------------------------------------------------------------- */
45 extern
46 void printKeysym(unsigned int key, unsigned int mod, string & buf);
47
48
49 // === kb_sequence methods ================================================ 
50
51 /* ---F+------------------------------------------------------------------ *\
52     Function  : kb_sequence::addkey
53     Called by : [user]
54     Purpose   : add a key to the sequence, look up in map and return action
55     Parameters: key  - keysym of key
56                 mod  - modifier mask
57                 nmod - modifier veto mask (unused now)
58     Returns   : action or -1 if error (no map defined or key not found)
59 \* ---F------------------------------------------------------------------- */
60
61 int kb_sequence::addkey(unsigned int key,
62                         unsigned int mod, unsigned int nmod /*= 0*/)
63 {
64         //lyxerr << "kb_sequence::addkey: length is [" << length << "]\n"
65         //       << "kb_sequence::addkey::key == [" << key << "]\n"
66         //       << "kb_sequence::addkey::mod == [" << mod << "]" << endl;
67         
68         if (length <= 0) {
69                 length = 0;
70                 sequence.clear();
71                 modifiers.clear();
72         }
73
74         modifiers.push_back(mod + (nmod << 16));
75         sequence.push_back(key);
76         ++length;
77
78         if (curmap)
79                 return curmap->lookup(key, mod, this);
80         
81         return -1;
82 }
83
84
85 /* ---F+------------------------------------------------------------------ *\
86     Function  : kb_sequence::parse
87     Called by : [user]
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------------------------------------------------------------------- */
95
96 int kb_sequence::parse(string const & s)
97 {
98         if (s.empty()) return 1;
99
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;
105                 
106                 if (i + 1 < s.length() && s[i + 1] == '-')      {
107                         switch (s[i]) {
108                         case 's': case 'S':
109                                 mod |= ShiftMask;
110                                 i += 2;
111                                 continue;
112                         case 'c': case 'C':
113                                 mod |= ControlMask;
114                                 i += 2;
115                                 continue;
116                         case 'm': case 'M':
117                                 mod |= Mod1Mask;
118                                 i += 2;
119                                 continue;
120                         default:
121                                 return i + 1;
122                         }
123                 } else if (i + 2 < s.length() && s[i] == '~'
124                            && s[i + 2] == '-') {
125                         switch (s[i + 1]) {
126                         case 's': case 'S':
127                                 nmod |= ShiftMask;
128                                 i += 3;
129                                 continue;
130                         case 'c': case 'C':
131                                 nmod |= ControlMask;
132                                 i += 3;
133                                 continue;
134                         case 'm': case 'M':
135                                 nmod |= Mod1Mask;
136                                 i += 3;
137                                 continue;
138                         default:
139                                 return i + 2;
140                         }
141                 } else {
142                         string tbuf;
143                         string::size_type j = i;
144                         for (; j < s.length() && s[j] > ' '; ++j)
145                                 tbuf += s[j];    // (!!!check bounds :-)
146                         
147                         KeySym key = XStringToKeysym(tbuf.c_str());
148                         if (key == NoSymbol) {
149                                 lyxerr[Debug::KBMAP]
150                                         << "kbmap.C: No such keysym: "
151                                         << tbuf << endl;
152                                 return j;
153                         }
154                         i = j;
155                         
156                         addkey(key, mod, nmod);
157                         mod = 0;
158                         nmod = 0;
159                 }
160         }
161         return 0;
162 }
163
164
165 /* ---F+------------------------------------------------------------------ *\
166     Function  : kb_sequence::print
167     Called by : [user]
168     Purpose   : print the currently defined sequence into a string
169     Parameters: buf           - string where the result goes
170                 when_defined  - only  print when sequence is real: length > 0.
171     Returns   : 0, if ok, -1 if string too long
172 \* ---F------------------------------------------------------------------- */
173
174 int kb_sequence::print(string & buf, bool when_defined) const
175 {
176         //lyxerr << "kb_sequence::print: length is [" << length << "]" << endl;
177         
178         KeySym key;
179         unsigned int mod;
180         int l = length;
181         if (l < 0 && !when_defined ) l = -l;
182         
183         for (int i = 0; i < l; ++i) {
184                 key = sequence[i];
185                 mod = modifiers[i] & 0xffff;
186                 //lyxerr << "kb_sequence::sequence[" << i << "] == [" << key << "]\n"
187                 //       << "kb_sequence::modifiers[" << i << "] == [" << mod << "]"
188                 //       << endl;
189
190                 printKeysym(key, mod, buf);  // RVDK_PATCH_5
191
192                 if (i + 1 < l) {  // append a blank
193                         buf += ' ';
194                 }
195         }
196         return 0;
197 }
198
199
200 /* ---F+------------------------------------------------------------------ *\
201     Function  : kb_sequence::printOptions
202     Called by : [user]
203     Purpose   : print the available key options from the current state in the
204                 sequence. RVDK_PATCH_5
205     Parameters: buf    - string where the result goes
206                 maxlen - length of string (including '\0')
207     Returns   : 0, if ok, -1 if string too long
208 \* ---F------------------------------------------------------------------- */
209
210 int kb_sequence::printOptions(string & buf) const
211 {
212         print(buf, true);
213         
214         if (!curmap) return -1;
215         buf += _("   options: ");
216         curmap->print(buf);
217         return 0;
218 }
219
220
221 /* ---F+------------------------------------------------------------------ *\
222     Function  : kb_sequence::delseq
223     Called by : [user]
224     Purpose   : mark the sequence as deleted
225     Parameters: none
226     Returns   : nothing
227 \* ---F------------------------------------------------------------------- */
228
229 void kb_sequence::delseq()
230 {
231         // negative length marks sequence as deleted, but we can still
232         // print() it or retrieve the last char using getiso()
233         length = -length;
234 }
235
236
237 /* ---F+------------------------------------------------------------------ *\
238    Function  : kb_sequence::getsym
239    Called by : [user], getiso
240    Purpose   : get the keysym of the last key in sequence
241    Parameters: none
242    Returns   : keysym
243 \* ---F------------------------------------------------------------------- */
244
245 unsigned int kb_sequence::getsym() const
246 {
247         int l = length;
248         if (l == 0) return NoSymbol;
249         if (l < 0) l = -l;
250         return sequence[l - 1];
251 }
252
253
254 /* ---F+------------------------------------------------------------------ *\
255     Function  : kb_sequence::getiso
256     Called by : [user]
257     Purpose   : return iso character code of last key, if any
258     Parameters: none
259     Returns   : iso code or 0 if none
260 \* ---F------------------------------------------------------------------- */
261
262 char kb_sequence::getiso() const
263 {
264         unsigned int const c = getsym();
265
266         lyxerr[Debug::KBMAP] << "Raw keysym: "
267                              << std::hex << c << std::dec << endl;
268         lyxerr[Debug::KBMAP] << "byte 3: "
269                              << std::hex << (c & 0x0000FF00) << std::dec
270                              << endl;
271         
272         switch (c & 0x0000FF00) {
273                 // latin 1 byte 3 = 0
274         case 0x00000000:
275                 return c;
276                 // latin 2 byte 3 = 1
277         case 0x00000100:
278                 // latin 3 byte 3 = 2
279         case 0x00000200:
280                 // latin 4 byte 3 = 3
281         case 0x00000300:
282                 // latin 8 byte 3 = 18 (0x12)
283         case 0x00001200:
284                 // latin 9 byte 3 = 19 (0x13)
285         case 0x00001300:
286                 return c & 0x000000FF;
287         default:
288                 return '\0';
289         }
290
291         // not a latin char we know of
292         // Yes but this is already handled above (JMarc)
293         //return '\0';
294 }
295
296
297 /* ---F+------------------------------------------------------------------ *\
298     Function  : kb_sequence::reset
299     Called by : [user]
300     Purpose   : reset sequence to initial state. RVDK_PATCH_5
301     Parameters: none
302     Returns   : void
303 \* ---F------------------------------------------------------------------- */
304
305 void kb_sequence::reset()
306 {
307         delseq();
308         curmap = stdmap;
309         if (length > 0) length = -length;
310 }
311
312 /* === End of File: kbmap.C ============================================== */