React better if we can't find bind files. Related to bug 6076.
[lyx.git] / src / KeyMap.h
blob1a37a1fc99772fae1ded88eaaf5c86dee5950e86
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.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author John Levon
11 * Full author contact details are available in file CREDITS.
14 #ifndef KEYMAP_H
15 #define KEYMAP_H
17 #include "FuncRequest.h"
18 #include "KeySequence.h"
20 #include "support/strfwd.h"
22 #include <boost/shared_ptr.hpp>
24 #include <vector>
27 namespace lyx {
29 namespace support {
30 class FileName;
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.
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.
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
56 size_t bind(std::string const & seq, FuncRequest const & func);
57 size_t unbind(std::string const & seq, FuncRequest const & func);
59 /**
60 * Define/Undefine an action for a key sequence.
61 * @param r internal recursion level
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);
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);
75 /// clear all bindings
76 void clear();
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.
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
87 bool read(std::string const & bind_file, KeyMap * unbind_map = 0,
88 BindReadType rt = Default);
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.
95 void write(std::string const & bind_file, bool append, bool unbind = false) const;
97 /**
98 * print all available keysyms
99 * @param forgui true if the string should use translations and
100 * special characters.
102 docstring const print(bool forgui) const;
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
111 FuncRequest const &
112 lookup(KeySymbol const & key, KeyModifier mod, KeySequence * seq) const;
115 typedef std::vector<KeySequence> Bindings;
117 /// Given an action, find all keybindings.
118 Bindings findBindings(FuncRequest const & func) const;
120 /// Given an action, print the keybindings.
121 docstring printBindings(FuncRequest const & func,
122 KeySequence::outputFormat format) const;
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;
131 typedef std::vector<Binding> BindingList;
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
137 BindingList listBindings(bool unbound, ItemType tag = System) const;
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]
144 std::pair<KeySymbol, KeyModifier>
145 find1keybinding(FuncRequest const & func) const;
148 * Returns a string of the given keysym, with modifiers.
149 * @param key the key as a keysym
150 * @param mod the modifiers
152 static std::string const printKeySym(KeySymbol const & key,
153 KeyModifier mod);
155 private:
157 typedef std::pair<KeyModifier, KeyModifier> ModifierPair;
160 struct Key {
161 /// Keysym
162 KeySymbol code;
163 /// Modifier masks
164 ModifierPair mod;
165 /// Keymap for prefix keys
166 boost::shared_ptr<KeyMap> prefixes;
167 /// Action for !prefix keys
168 FuncRequest func;
172 bool read(support::FileName const & bind_file, KeyMap * unbind_map = 0);
175 * Given an action, find all keybindings
176 * @param func the action
177 * @param prefix a sequence to prepend the results
179 Bindings findBindings(FuncRequest const & func,
180 KeySequence const & prefix) const;
182 void listBindings(BindingList & list, KeySequence const & prefix,
183 ItemType tag) const;
185 /// is the table empty ?
186 bool empty() const { return table.empty(); }
188 typedef std::vector<Key> Table;
190 Table table;
193 /// Implementation is in LyX.cpp
194 extern KeyMap & theTopLevelKeymap();
197 } // namespace lyx
199 #endif // KEYMAP_H