]> git.lyx.org Git - lyx.git/blob - src/kbmap.h
added a regex to be used when the system is missing one, use noinst instead of pkglib...
[lyx.git] / src / kbmap.h
1 // -*- C++ -*-
2 /* ======================================================================= *\
3    File   : kbmap.h, kbmap.h,v 1.3 1996/12/10 04:35:57 larsbj Exp
4    Author : chb, 30.Oct.1995
5    Docu   : see kbmap.C
6    Purpose: class definitions for XKeyEvent keymap handling
7    \* ==================================================================== */
8
9 #ifndef KBMAP_H
10 #define KBMAP_H
11
12 #ifdef __GNUG__
13 #pragma interface
14 #endif
15
16 #include <X11/Xlib.h>
17
18 #include "LString.h"
19
20 #define NO_HASH 1
21
22 #define KB_PREALLOC  16
23 #ifndef NO_HASH
24 #define KB_HASHSIZE 128   // yes, yes - I know. 128 is not exactly prime :-)
25 // ... but we are dealing with ASCII chars mostly.
26 #endif
27 class kb_keymap;
28 class kb_sequence;
29
30 ///
31 struct kb_key {
32         /// Keysym
33         unsigned int code;
34         
35         /// Modifier masks
36         unsigned int mod;
37         
38         /// Keymap for prefix keys
39         kb_keymap * table;
40         
41         /// Action for !prefix keys
42         int action;
43 };
44
45
46 /// Defines key maps and actions for key sequences
47 class kb_keymap {
48 public:
49         ///
50         kb_keymap() {
51                 size = 0; 
52                 table = 0;
53         }
54         ///
55         ~kb_keymap();
56         
57         /// Bind a key-sequence to an action
58         /** Returns 0 on success. Otherwise, position in string where
59             error occured. */
60         int bind(char const * seq, int action);
61
62         ///
63         void print(string & buf) const;
64         
65         /// Look up a key in the keymap
66         int lookup(KeySym key, unsigned mod, kb_sequence * seq);
67
68         /// Given an action, find all keybindings.
69         string findbinding(int action) const;
70 private:
71         /// Define a new key sequence
72         int defkey(kb_sequence * seq, int action, int idx = 0);
73         
74         /// Size of the table (<0: hashtab)
75         int size;
76         
77         /// Holds the defined keys
78         /** Both kinds of tables ends with NoSymbol */
79 #ifndef NO_HASH
80         union
81         {
82 #endif
83                 /// Table for linear array
84                 kb_key * table;
85 #ifndef NO_HASH 
86                 /// Hash table holding key lists
87                 kb_key ** htable;
88         };
89 #endif
90 };
91
92
93 /// Holds a key sequence and the current and standard keymaps
94 class kb_sequence {
95 public:
96         ///
97         kb_sequence() {
98                 stdmap = curmap = 0;
99                 sequence = staticseq;
100                 modifiers = staticmod;
101                 length = 0; 
102                 size = KB_PREALLOC;
103         }
104         
105         ///
106         
107         
108         ///
109         ~kb_sequence()
110                 {
111                         if (sequence != staticseq) {
112                                 delete sequence;
113                                 delete modifiers;
114                         }
115                 }
116         
117         /// Add a key to the key sequence and look it up in the curmap
118         /** Add a key to the key sequence and look it up in the curmap
119             if the latter is defined. */
120         int addkey(KeySym key, unsigned mod, unsigned nmod = 0);
121
122         ///
123         int print(string & buf, bool when_defined = false) const;
124         
125         ///
126         int printOptions(string & buf) const;
127         
128         /// Make length negative to mark the sequence as deleted
129         void delseq();
130         
131         ///
132         char getiso();
133         
134         ///
135         KeySym getsym();
136         
137         ///
138         void reset();
139         
140         ///
141         int parse(char const * s);
142         
143         /// Keymap to use if a new sequence is starting
144         kb_keymap * stdmap;
145         
146         /// Keymap to use for the next key
147         kb_keymap * curmap;
148         
149         /// Array holding the current key sequence
150         /** If sequence[length-1] < 0xff it can be used as ISO8859 char */
151         unsigned int * sequence;
152         
153         ///
154         unsigned int * modifiers;
155         
156         /// Current length of key sequence
157         int length;
158         
159 private:
160         /// Static array preallocated for sequence
161         unsigned int staticseq[KB_PREALLOC];
162         
163         ///
164         unsigned int staticmod[KB_PREALLOC];
165         
166         /// Physically allocated storage size
167         int size;
168 };
169
170 #endif