]> git.lyx.org Git - lyx.git/blob - src/KeyMap.h
saner borderline between buffer and exporter
[lyx.git] / src / KeyMap.h
1 // -*- C++ -*-
2 /**
3  * \file KeyMap.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 KEYMAP_H
15 #define KEYMAP_H
16
17 #include "FuncRequest.h"
18
19 #include "frontends/KeyModifier.h"
20 #include "frontends/KeySymbol.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 KeySequence;
33
34 /// Defines key maps and actions for key sequences
35 class 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 KeySequence::parse for the syntax of the seq string
42          */
43         size_t bind(std::string const & seq, FuncRequest const & func);
44
45         // Parse a bind file
46         bool read(std::string const & bind_file);
47
48         /**
49          * print all available keysyms
50          * @param forgui true if the string should use translations and
51          *   special characters.
52          */
53         docstring const print(bool forgui) const;
54
55         /**
56          * Look up a key press in the keymap.
57          * @param key the keysym
58          * @param mod the modifiers
59          * @param seq the current key sequence so far
60          * @return the action / LFUN_COMMAND_PREFIX / LFUN_UNKNOWN_ACTION
61          */
62         FuncRequest const &
63         lookup(KeySymbol const & key, KeyModifier mod, KeySequence * seq) const;
64
65         ///
66         typedef std::deque<KeySequence> Bindings;
67
68         /// Given an action, find all keybindings.
69         Bindings findbindings(FuncRequest const & func) const;
70
71         /// Given an action, print the keybindings.
72         docstring const printbindings(FuncRequest const & func) const;
73
74         typedef std::pair<FuncRequest, KeySequence> Binding; 
75         typedef std::vector<Binding> BindingList;
76         /**
77          * Return all lfun and their associated bindings.
78          * @param unbound list unbound (func without any keybinding) as well
79          */
80         BindingList const listBindings(bool unbound) const;
81
82         /**
83          *  Given an action, find the first 1-key binding (if it exists).
84          *  The KeySymbol pointer is 0 is no key is found.
85          *  [only used by the Qt/Mac frontend]
86          */
87         std::pair<KeySymbol, KeyModifier>
88         find1keybinding(FuncRequest const & func) const;
89
90
91         /**
92          * Returns a string of the given keysym, with modifiers.
93          * @param key the key as a keysym
94          * @param mod the modifiers
95          */
96         static std::string const printKeySym(KeySymbol const & key,
97                                              KeyModifier mod);
98
99         typedef std::pair<KeyModifier, KeyModifier> ModifierPair;
100
101
102 private:
103         ///
104         struct Key {
105                 /// Keysym
106                 KeySymbol code;
107
108                 /// Modifier masks
109                 ModifierPair mod;
110
111                 /// Keymap for prefix keys
112                 boost::shared_ptr<KeyMap> table;
113
114                 /// Action for !prefix keys
115                 FuncRequest func;
116         };
117
118         /**
119          * Define an action for a key sequence.
120          * @param r internal recursion level
121          */
122         void defkey(KeySequence * seq, FuncRequest const & func,
123                     unsigned int r = 0);
124
125         /**
126          * Given an action, find all keybindings
127          * @param func the action
128          * @param prefix a sequence to prepend the results
129          */
130         Bindings findbindings(FuncRequest const & func,
131                               KeySequence const & prefix) const;
132         
133         void listBindings(BindingList & list,
134                                   KeySequence const & prefix) const;
135
136         /// is the table empty ?
137         bool empty() const { return table.empty(); }
138         ///
139         typedef std::vector<Key> Table;
140         ///
141         Table table;
142 };
143
144 /// Implementation is in LyX.cpp
145 extern KeyMap & theTopLevelKeymap();
146
147
148 } // namespace lyx
149
150 #endif // KEYMAP_H