]> git.lyx.org Git - lyx.git/blob - src/kbmap.h
029a4e1b4eafcf406187ddd9b9eb132a77629a3d
[lyx.git] / src / kbmap.h
1 // -*- C++ -*-
2 /**
3  * \file kbmap.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef KBMAP_H
15 #define KBMAP_H
16
17 #include "funcrequest.h"
18
19 #include "frontends/key_state.h"
20 #include "frontends/LyXKeySym.h"
21
22 #include "support/docstream.h"
23
24 #include <boost/shared_ptr.hpp>
25
26 #include <vector>
27 #include <deque>
28
29
30 namespace lyx {
31
32 class kb_sequence;
33
34 /// Defines key maps and actions for key sequences
35 class kb_keymap {
36 public:
37         /**
38          * Bind a key sequence to an action.
39          * @return 0 on success, or position in string seq where error
40          * occurs.
41          * See kb_sequence::parse for the syntax of the seq string
42          */
43         std::string::size_type bind(std::string const & seq, FuncRequest const & func);
44
45         // Parse a bind file
46         bool read(std::string const & bind_file);
47
48         /// print all available keysyms
49         docstring const print() const;
50
51         /**
52          * Look up a key press in the keymap.
53          * @param key the keysym
54          * @param mod the modifiers
55          * @param seq the current key sequence so far
56          * @return the action / LFUN_COMMAND_PREFIX / LFUN_UNKNOWN_ACTION
57          */
58         FuncRequest const &
59         lookup(LyXKeySymPtr key,
60                key_modifier::state mod, kb_sequence * seq) const;
61
62         ///
63         typedef std::deque<kb_sequence> Bindings;
64
65         /// Given an action, find all keybindings.
66         Bindings findbindings(FuncRequest const & func) const;
67
68         /// Given an action, print the keybindings.
69         docstring const printbindings(FuncRequest const & func) const;
70
71         /**
72          *  Given an action, find the first 1-key binding (if it exists).
73          *  The LyXKeySym pointer is 0 is no key is found.
74          *  [only used by the Qt/Mac frontend]
75          */
76         std::pair<LyXKeySym const *, key_modifier::state>
77         find1keybinding(FuncRequest const & func) const;
78
79
80         /**
81          * Returns a string of the given keysym, with modifiers.
82          * @param key the key as a keysym
83          * @param mod the modifiers
84          */
85         static std::string const printKeySym(LyXKeySym const & key,
86                                              key_modifier::state mod);
87
88         typedef std::pair<key_modifier::state, key_modifier::state> modifier_pair;
89
90 private:
91         ///
92         struct kb_key {
93                 /// Keysym
94                 LyXKeySymPtr code;
95
96                 /// Modifier masks
97                 modifier_pair mod;
98
99                 /// Keymap for prefix keys
100                 boost::shared_ptr<kb_keymap> table;
101
102                 /// Action for !prefix keys
103                 FuncRequest func;
104         };
105
106         /**
107          * Define an action for a key sequence.
108          * @param r internal recursion level
109          */
110         void defkey(kb_sequence * seq, FuncRequest const & func,
111                     unsigned int r = 0);
112
113         ///  Returns a string of the given key
114         docstring const printKey(kb_key const & key) const;
115
116         /**
117          * Given an action, find all keybindings
118          * @param func the action
119          * @param prefix a sequence to prepend the results
120          */
121         Bindings findbindings(FuncRequest const & func,
122                               kb_sequence const & prefix) const;
123
124         /// is the table empty ?
125         bool empty() const {
126                 return table.empty();
127         }
128         ///
129         typedef std::vector<kb_key> Table;
130         ///
131         Table table;
132 };
133
134 /// Implementation is in lyx_main.C
135 extern kb_keymap & theTopLevelKeymap();
136
137
138 } // namespace lyx
139
140 #endif // KBMAP_H