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