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