]> git.lyx.org Git - lyx.git/blob - src/KeyMap.h
Change the "empty layout" to the "plain layout", to try to avoid confusion.
[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 #include "KeySequence.h"
19
20 #include "support/strfwd.h"
21
22 #include <boost/shared_ptr.hpp>
23
24 #include <vector>
25
26
27 namespace lyx {
28
29 /// Defines key maps and actions for key sequences
30 class KeyMap {
31 public:
32         /**
33          * Bind/Unbind a key sequence to an action.
34          * @return 0 on success, or position in string seq where error
35          * occurs.
36          * See KeySequence::parse for the syntax of the seq string
37          */
38         size_t bind(std::string const & seq, FuncRequest const & func);
39         size_t unbind(std::string const & seq, FuncRequest const & func);
40
41         /**
42          * Define/Undefine an action for a key sequence.
43          * @param r internal recursion level
44          */
45         void bind(KeySequence * seq, FuncRequest const & func,
46                     unsigned int r = 0);
47         void unbind(KeySequence * seq, FuncRequest const & func,
48                     unsigned int r = 0);
49
50
51         // if a keybinding has been defined.
52         bool hasBinding(KeySequence const & seq, FuncRequest const & func,
53                         unsigned int r = 0);
54
55         // clear all bindings
56         void clear();
57
58         /** Parse a bind file. If a valid unbind_map is given, put \unbind 
59          * bindings to a separate KeyMap. This is used in the Shortcut preference
60          * dialog where main and user bind files are loaded separately so \unbind
61          * in user.bind can not nullify \bind in the master bind file.
62          *
63          * @param bind_file bind file
64          * @param unbind_map pointer to a KeyMap that holds \unbind bindings
65          */
66         bool read(std::string const & bind_file, KeyMap * unbind_map = 0);
67
68         /** write to a bind file.
69          * @param append append to the bind_file instead of overwrite it
70          * @param unbind use \unbind instead of \bind, indicating this KeyMap
71          *        actually record unbind maps.
72          */
73         void write(std::string const & bind_file, bool append, bool unbind=false) const;
74
75         /**
76          * print all available keysyms
77          * @param forgui true if the string should use translations and
78          *   special characters.
79          */
80         docstring const print(bool forgui) const;
81
82         /**
83          * Look up a key press in the keymap.
84          * @param key the keysym
85          * @param mod the modifiers
86          * @param seq the current key sequence so far
87          * @return the action / LFUN_COMMAND_PREFIX / LFUN_UNKNOWN_ACTION
88          */
89         FuncRequest const &
90         lookup(KeySymbol const & key, KeyModifier mod, KeySequence * seq) const;
91
92         ///
93         typedef std::vector<KeySequence> Bindings;
94
95         /// Given an action, find all keybindings.
96         Bindings findBindings(FuncRequest const & func) const;
97
98         /// Given an action, print the keybindings.
99         docstring printBindings(FuncRequest const & func) const;
100
101         struct Binding {
102                 Binding(FuncRequest const & r, KeySequence const & s, int t)
103                         : request(r), sequence(s), tag(t) {}
104                 FuncRequest request;
105                 KeySequence sequence;
106                 int tag;
107         }; 
108         typedef std::vector<Binding> BindingList;
109         /**
110          * Return all lfun and their associated bindings.
111          * @param unbound list unbound (func without any keybinding) as well
112          * @param tag an optional tag to indicate the source of the bindinglist
113          */
114         BindingList listBindings(bool unbound, int tag = 0) const;
115
116         /**
117          *  Given an action, find the first 1-key binding (if it exists).
118          *  The KeySymbol pointer is 0 is no key is found.
119          *  [only used by the Qt/Mac frontend]
120          */
121         std::pair<KeySymbol, KeyModifier>
122         find1keybinding(FuncRequest const & func) const;
123
124         /**
125          * Returns a string of the given keysym, with modifiers.
126          * @param key the key as a keysym
127          * @param mod the modifiers
128          */
129         static std::string const printKeySym(KeySymbol const & key,
130                                              KeyModifier mod);
131
132 private:
133         ///
134         typedef std::pair<KeyModifier, KeyModifier> ModifierPair;
135
136         ///
137         struct Key {
138                 /// Keysym
139                 KeySymbol code;
140                 /// Modifier masks
141                 ModifierPair mod;
142                 /// Keymap for prefix keys
143                 boost::shared_ptr<KeyMap> table;
144                 /// Action for !prefix keys
145                 FuncRequest func;
146         };
147
148         /**
149          * Given an action, find all keybindings
150          * @param func the action
151          * @param prefix a sequence to prepend the results
152          */
153         Bindings findBindings(FuncRequest const & func,
154                               KeySequence const & prefix) const;
155         
156         void listBindings(BindingList & list, KeySequence const & prefix,
157                                   int tag) const;
158
159         /// is the table empty ?
160         bool empty() const { return table.empty(); }
161         ///
162         typedef std::vector<Key> Table;
163         ///
164         Table table;
165 };
166
167 /// Implementation is in LyX.cpp
168 extern KeyMap & theTopLevelKeymap();
169
170
171 } // namespace lyx
172
173 #endif // KEYMAP_H