]> git.lyx.org Git - lyx.git/blob - src/KeyMap.h
Fix bug #12772
[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          * @param i18n whether to search in localized folders
86          */
87         bool read(std::string const & bind_file, KeyMap * unbind_map = 0,
88                         BindReadType rt = Default, bool i18n = true);
89
90         /** write to a bind file.
91          * @param append append to the bind_file instead of overwrite it
92          * @param unbind use \unbind instead of \bind, indicating this KeyMap
93          *        actually record unbind maps.
94          */
95         void write(std::string const & bind_file, bool append, bool unbind = false) const;
96
97         /**
98          * print all available keysyms
99          * @param forgui true if the string should use translations and
100          *   special characters.
101          */
102         docstring const print(bool forgui) const;
103
104         /**
105          * Look up a key press in the keymap.
106          * @param key the keysym
107          * @param mod the modifiers
108          * @param seq the current key sequence so far
109          * @return the action / LFUN_COMMAND_PREFIX / LFUN_UNKNOWN_ACTION
110          */
111         FuncRequest const &
112                 lookup(KeySymbol const & key, KeyModifier mod, KeySequence * seq) const;
113
114         ///
115         typedef std::vector<KeySequence> Bindings;
116
117         /// Given an action, find all keybindings.
118         Bindings findBindings(FuncRequest const & func) const;
119
120         /// Given an action, print the keybindings.
121         docstring printBindings(FuncRequest const & func,
122                                 KeySequence::outputFormat format,
123                                 bool const untranslated = false) const;
124
125         struct Binding {
126                 Binding(FuncRequest const & r, KeySequence const & s, ItemType t)
127                         : request(r), sequence(s), tag(t) {}
128                 FuncRequest request;
129                 KeySequence sequence;
130                 KeyMap::ItemType tag;
131         };
132         typedef std::vector<Binding> BindingList;
133         /**
134          * Return all lfun and their associated bindings.
135          * @param unbound list unbound (func without any keybinding) as well
136          * @param tag an optional tag to indicate the source of the bindinglist
137          */
138         BindingList listBindings(bool unbound, ItemType tag = System) const;
139
140         /**
141          *  Given an action, find the first 1-key binding (if it exists).
142          *  The KeySymbol pointer is 0 is no key is found.
143          *  [only used by the Qt/Mac frontend]
144          */
145         std::pair<KeySymbol, KeyModifier>
146                 find1keybinding(FuncRequest const & func) const;
147
148         /**
149          * Returns a string of the given keysym, with modifiers.
150          * @param key the key as a keysym
151          * @param mod the modifiers
152          */
153         static std::string const printKeySym(KeySymbol const & key,
154                                              KeyModifier mod);
155
156 private:
157         ///
158         typedef std::pair<KeyModifier, KeyModifier> ModifierPair;
159
160         ///
161         struct Key {
162                 /// Keysym
163                 KeySymbol code;
164                 /// Modifier masks
165                 ModifierPair mod;
166                 /// Keymap for prefix keys
167                 std::shared_ptr<KeyMap> prefixes;
168                 /// Action for !prefix keys
169                 FuncRequest func;
170         };
171
172         enum ReturnValues {
173                 ReadOK,
174                 ReadError,
175                 FileError,
176                 FormatMismatch
177         };
178         ///
179         bool read(support::FileName const & bind_file, KeyMap * unbind_map = 0);
180         ///
181         ReturnValues readWithoutConv(support::FileName const & bind_file, KeyMap * unbind_map = 0);
182
183         /**
184          * Given an action, find all keybindings
185          * @param func the action
186          * @param prefix a sequence to prepend the results
187          */
188         Bindings findBindings(FuncRequest const & func,
189                               KeySequence const & prefix) const;
190
191         void listBindings(BindingList & list, KeySequence const & prefix,
192                           ItemType tag) const;
193
194         /// is the table empty ?
195         bool empty() const { return table.empty(); }
196         ///
197         typedef std::vector<Key> Table;
198         ///
199         Table table;
200 };
201
202 /// Implementation is in LyX.cpp
203 extern KeyMap & theTopLevelKeymap();
204
205
206 } // namespace lyx
207
208 #endif // KEYMAP_H