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