]> git.lyx.org Git - lyx.git/blob - src/kbmap.h
fd320bf97987e9fe28a254c05d5b101f1942160b
[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 "LString.h"
17
18 #define KB_PREALLOC  16
19
20 class kb_keymap;
21 class kb_sequence;
22
23 ///
24 struct kb_key {
25         /// Keysym
26         unsigned int code;
27         
28         /// Modifier masks
29         unsigned int mod;
30         
31         /// Keymap for prefix keys
32         kb_keymap * table;
33         
34         /// Action for !prefix keys
35         int action;
36 };
37
38
39 /// Defines key maps and actions for key sequences
40 class kb_keymap {
41 public:
42         ///
43         kb_keymap() {
44                 size = 0; 
45                 table = 0;
46         }
47         ///
48         ~kb_keymap();
49         
50         /// Bind a key-sequence to an action
51         /** Returns 0 on success. Otherwise, position in string where
52             error occured. */
53         int bind(char const * seq, int action);
54
55         ///
56         void print(string & buf) const;
57         
58         /// Look up a key in the keymap
59         int lookup(unsigned long key, unsigned mod, kb_sequence * seq);
60
61         /// Given an action, find all keybindings.
62         string findbinding(int action) const;
63 private:
64         /// Define a new key sequence
65         int defkey(kb_sequence * seq, int action, int idx = 0);
66         
67         /// Size of the table (<0: hashtab)
68         int size;
69         
70         /// Holds the defined keys
71         /// Table for linear array, table ends with NoSymbol.
72         kb_key * table;
73 };
74
75
76 /// Holds a key sequence and the current and standard keymaps
77 class kb_sequence {
78 public:
79         ///
80         kb_sequence() {
81                 stdmap = curmap = 0;
82                 sequence = staticseq;
83                 modifiers = staticmod;
84                 length = 0; 
85                 size = KB_PREALLOC;
86         }
87         
88         ///
89         
90         
91         ///
92         ~kb_sequence() {
93                 if (sequence != staticseq) {
94                         delete sequence;
95                         delete modifiers;
96                 }
97         }
98         
99         /// Add a key to the key sequence and look it up in the curmap
100         /** Add a key to the key sequence and look it up in the curmap
101             if the latter is defined. */
102         int addkey(unsigned long key, unsigned mod, unsigned nmod = 0);
103
104         ///
105         int print(string & buf, bool when_defined = false) const;
106         
107         ///
108         int printOptions(string & buf) const;
109         
110         /// Make length negative to mark the sequence as deleted
111         void delseq();
112         
113         ///
114         char getiso();
115         
116         ///
117         unsigned long getsym();
118         
119         ///
120         void reset();
121         
122         ///
123         int parse(char const * s);
124         
125         /// Keymap to use if a new sequence is starting
126         kb_keymap * stdmap;
127         
128         /// Keymap to use for the next key
129         kb_keymap * curmap;
130         
131         /// Array holding the current key sequence
132         /** If sequence[length-1] < 0xff it can be used as ISO8859 char */
133         unsigned int * sequence;
134         
135         ///
136         unsigned int * modifiers;
137         
138         /// Current length of key sequence
139         int length;
140         
141 private:
142         /// Static array preallocated for sequence
143         unsigned int staticseq[KB_PREALLOC];
144         
145         ///
146         unsigned int staticmod[KB_PREALLOC];
147         
148         /// Physically allocated storage size
149         int size;
150 };
151
152 #endif