]> git.lyx.org Git - lyx.git/blob - src/KeyMap.h
Update fr.po
[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 <memory>
23 #include <vector>
24
25
26 namespace lyx {
27
28 namespace support {
29         class FileName;
30 }
31
32 /// Defines key maps and actions for key sequences
33 class KeyMap {
34 public:
35         enum ItemType {
36                 System,         //< loaded from a bind file
37                 UserBind,       //< \bind loaded from user.bind
38                 UserUnbind,     //< \unbind loaded from user.bind, with corresponding
39                                 //<    entry in system bind file
40                 UserExtraUnbind //< \unbind loaded from user.bind, without
41                                 //<    corresponding entry in system bind file.
42         };
43         enum BindReadType {
44                 MissingOK,      //< It's OK if this file is missing.
45                 Fallback,       //< If missing, fallback to default "cua". This should only
46                                 //< be used when attempting to read the user-secified bind file.
47                 Default         //< Report error and return.
48         };
49         /**
50          * Bind/Unbind a key sequence to an action.
51          * @return 0 on success, or position in string seq where error
52          * occurs.
53          * See KeySequence::parse for the syntax of the seq string
54          */
55         size_t bind(std::string const & seq, FuncRequest const & func);
56         size_t unbind(std::string const & seq, FuncRequest const & func);
57
58         /**
59          * Define/Undefine an action for a key sequence.
60          * @param r internal recursion level
61          */
62         void bind(KeySequence * seq, FuncRequest const & func,
63                     unsigned int r = 0);
64         void unbind(KeySequence * seq, FuncRequest const & func,
65                     unsigned int r = 0);
66
67
68         /// returns the function bound to this key sequence, or
69         /// FuncRequest::unknown if no binding exists for it.
70         /// @param r an internal recursion counter
71         // FIXME Surely there's a better way to do that?
72         FuncRequest getBinding(KeySequence const & seq, unsigned int r = 0);
73
74         /// clear all bindings
75         void clear();
76
77         /** Parse a bind file. If a valid unbind_map is given, put \unbind
78          * bindings to a separate KeyMap. This is used in the Shortcut preference
79          * dialog where main and user bind files are loaded separately so \unbind
80          * in user.bind can not nullify \bind in the master bind file.
81          *
82          * @param bind_file bind file
83          * @param unbind_map pointer to a KeyMap that holds \unbind bindings
84          * @param rt how to respond if the file can't be found
85          */
86         bool read(std::string const & bind_file, KeyMap * unbind_map = 0,
87                         BindReadType rt = Default);
88
89         /** write to a bind file.
90          * @param append append to the bind_file instead of overwrite it
91          * @param unbind use \unbind instead of \bind, indicating this KeyMap
92          *        actually record unbind maps.
93          */
94         void write(std::string const & bind_file, bool append, bool unbind = false) const;
95
96         /**
97          * print all available keysyms
98          * @param forgui true if the string should use translations and
99          *   special characters.
100          */
101         docstring const print(bool forgui) const;
102
103         /**
104          * Look up a key press in the keymap.
105          * @param key the keysym
106          * @param mod the modifiers
107          * @param seq the current key sequence so far
108          * @return the action / LFUN_COMMAND_PREFIX / LFUN_UNKNOWN_ACTION
109          */
110         FuncRequest const &
111                 lookup(KeySymbol const & key, KeyModifier mod, KeySequence * seq) const;
112
113         ///
114         typedef std::vector<KeySequence> Bindings;
115
116         /// Given an action, find all keybindings.
117         Bindings findBindings(FuncRequest const & func) const;
118
119         /// Given an action, print the keybindings.
120         docstring printBindings(FuncRequest const & func,
121                                 KeySequence::outputFormat format) const;
122
123         struct Binding {
124                 Binding(FuncRequest const & r, KeySequence const & s, ItemType t)
125                         : request(r), sequence(s), tag(t) {}
126                 FuncRequest request;
127                 KeySequence sequence;
128                 KeyMap::ItemType tag;
129         };
130         typedef std::vector<Binding> BindingList;
131         /**
132          * Return all lfun and their associated bindings.
133          * @param unbound list unbound (func without any keybinding) as well
134          * @param tag an optional tag to indicate the source of the bindinglist
135          */
136         BindingList listBindings(bool unbound, ItemType tag = System) const;
137
138         /**
139          *  Given an action, find the first 1-key binding (if it exists).
140          *  The KeySymbol pointer is 0 is no key is found.
141          *  [only used by the Qt/Mac frontend]
142          */
143         std::pair<KeySymbol, KeyModifier>
144                 find1keybinding(FuncRequest const & func) const;
145
146         /**
147          * Returns a string of the given keysym, with modifiers.
148          * @param key the key as a keysym
149          * @param mod the modifiers
150          */
151         static std::string const printKeySym(KeySymbol const & key,
152                                              KeyModifier mod);
153
154 private:
155         ///
156         typedef std::pair<KeyModifier, KeyModifier> ModifierPair;
157
158         ///
159         struct Key {
160                 /// Keysym
161                 KeySymbol code;
162                 /// Modifier masks
163                 ModifierPair mod;
164                 /// Keymap for prefix keys
165                 std::shared_ptr<KeyMap> prefixes;
166                 /// Action for !prefix keys
167                 FuncRequest func;
168         };
169
170         enum ReturnValues {
171                 ReadOK,
172                 ReadError,
173                 FileError,
174                 FormatMismatch
175         };
176         ///
177         bool read(support::FileName const & bind_file, KeyMap * unbind_map = 0);
178         ///
179         ReturnValues readWithoutConv(support::FileName const & bind_file, KeyMap * unbind_map = 0);
180
181         /**
182          * Given an action, find all keybindings
183          * @param func the action
184          * @param prefix a sequence to prepend the results
185          */
186         Bindings findBindings(FuncRequest const & func,
187                               KeySequence const & prefix) const;
188
189         void listBindings(BindingList & list, KeySequence const & prefix,
190                           ItemType tag) const;
191
192         /// is the table empty ?
193         bool empty() const { return table.empty(); }
194         ///
195         typedef std::vector<Key> Table;
196         ///
197         Table table;
198 };
199
200 /// Implementation is in LyX.cpp
201 extern KeyMap & theTopLevelKeymap();
202
203
204 } // namespace lyx
205
206 #endif // KEYMAP_H