]> git.lyx.org Git - lyx.git/blob - src/kbsequence.C
Use paragraph iterators in CutAndPaste::SwitchLayoutsBetweenClasses
[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 kb_action kb_sequence::addkey(unsigned int key, unsigned int mod)
33 {
34         // adding a key to a deleted sequence
35         // starts a new sequence
36         if (deleted_) {
37                 deleted_ = false;
38                 length_ = 0;
39                 sequence.clear();
40                 modifiers.clear();
41         }
42
43         modifiers.push_back(mod);
44         sequence.push_back(key);
45         ++length_;
46
47         if (curmap) {
48                 return curmap->lookup(key, mod, this);
49         }
50         
51         return LFUN_UNKNOWN_ACTION;
52 }
53
54
55 string::size_type kb_sequence::parse(string const & s)
56 {
57         if (s.empty()) return 1;
58
59         string::size_type i = 0;
60         unsigned int mod = 0;
61         while (i < s.length()) {
62                 if (s[i] == ' ')
63                         ++i;
64                 if (i >= s.length())
65                         break;
66                 
67                 if (i + 1 < s.length() && s[i + 1] == '-') {
68                         switch (s[i]) {
69                         case 's': case 'S':
70                                 mod |= ShiftMask;
71                                 i += 2;
72                                 continue;
73                         case 'c': case 'C':
74                                 mod |= ControlMask;
75                                 i += 2;
76                                 continue;
77                         case 'm': case 'M':
78                                 mod |= Mod1Mask;
79                                 i += 2;
80                                 continue;
81                         default:
82                                 return i + 1;
83                         }
84                 } else if (i + 2 < s.length() && s[i] == '~'
85                            && s[i + 2] == '-') {
86                         switch (s[i + 1]) {
87                         case 's': case 'S':
88                                 i += 3;
89                                 continue;
90                         case 'c': case 'C':
91                                 i += 3;
92                                 continue;
93                         case 'm': case 'M':
94                                 i += 3;
95                                 continue;
96                         default:
97                                 return i + 2;
98                         }
99                 } else {
100                         string tbuf;
101                         string::size_type j = i;
102                         for (; j < s.length() && s[j] != ' '; ++j)
103                                 tbuf += s[j];    // (!!!check bounds :-)
104                         
105                         KeySym key = XStringToKeysym(tbuf.c_str());
106                         if (key == NoSymbol) {
107                                 lyxerr[Debug::KBMAP]
108                                         << "kbmap.C: No such keysym: "
109                                         << tbuf << endl;
110                                 return j;
111                         }
112                         i = j;
113                         
114                         addkey(key, mod);
115                         mod = 0;
116                 }
117         }
118         
119         // empty sequence?
120         if (!length_)
121                 return 0;
122
123         // everything is fine
124         return string::npos;
125 }
126
127
128 string const kb_sequence::print() const
129 {
130         string buf;
131
132         if (deleted_)
133                 return buf;
134         
135         for (std::vector<unsigned int>::size_type i = 0; i < length_; ++i) {
136                 buf += kb_keymap::printKeysym(sequence[i], modifiers[i] & 0xffff);
137
138                 // append a blank
139                 if (i + 1 < length_) {
140                         buf += ' ';
141                 }
142         }
143         return buf;
144 }
145
146
147 string const kb_sequence::printOptions() const
148 {
149         string buf;
150
151         buf += print();
152         
153         if (!curmap)
154                 return buf;
155
156         buf += _("   options: ");
157         buf += curmap->print();
158         return buf;
159 }
160
161
162 void kb_sequence::mark_deleted()
163 {
164         deleted_ = true;
165 }
166
167
168 unsigned int kb_sequence::getsym() const
169 {
170         if (length_ == 0) return NoSymbol;
171         return sequence[length_ - 1];
172 }
173
174
175 char kb_sequence::getiso() const
176 {
177         unsigned int const c = getsym();
178
179         lyxerr[Debug::KBMAP] << "Raw keysym: "
180                              << std::hex << c << std::dec << endl;
181         lyxerr[Debug::KBMAP] << "byte 3: "
182                              << std::hex << (c & 0xff00) << std::dec
183                              << endl;
184         return kb_keymap::getiso(c);
185 }
186
187
188 void kb_sequence::reset()
189 {
190         mark_deleted();
191         curmap = stdmap;
192 }
193
194 void kb_sequence::clear()
195 {
196         length_ = 0;
197         reset();
198 }