cwidget 0.5.18
keybindings.h
Go to the documentation of this file.
1// keybindings.h, -*-c++-*-
2//
3// Copyright 1999-2001, 2003-2005, 2008 Daniel Burrows
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; see the file COPYING. If not, write to
17// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18// Boston, MA 02111-1307, USA.
19
20#ifndef KEYBINDINGS_H
21#define KEYBINDINGS_H
22
23#include <map>
24#include <string>
25#include <vector>
26
27#include <cwidget/curses++.h>
28
34namespace cwidget
35{
36 namespace config
37 {
42 struct key
43 {
45 wint_t ch;
46
49
50 key()
51 :ch((wint_t) ERR), function_key(true)
52 {
53 }
54
55 key(wint_t _ch, bool _function_key)
56 :ch(_ch), function_key(_function_key)
57 {
58 }
59
61 bool operator<(const key &other) const
62 {
63 return ch < other.ch || (ch == other.ch &&
64 !function_key && other.function_key);
65 }
66
67 bool operator==(const key &other) const
68 {
69 return ch == other.ch && function_key == other.function_key;
70 }
71 };
72
74 typedef std::vector<key> keybinding;
75
88 {
89 std::map<std::string, keybinding> keymap;
90
91 keybindings *parent;
92
93 // It's way too easy to accidentally invoke the automatic copy
94 // constructor instead of the real one.
95 keybindings(const keybindings &_parent);
96 public:
101 keybindings(keybindings *_parent = nullptr) : parent(_parent) {}
102
108 std::wstring keyname(const std::string &tag) const;
109
110
117 std::wstring readable_keyname(const std::string &tag) const;
118
120 keybinding get(const std::string &tag) const
121 {
122 auto it = keymap.find(tag);
123
124 if (it == keymap.end())
125 return keybinding();
126 else
127 return it->second;
128 }
129
139 void set(std::string tag, keybinding strokes);
140
149 void set(std::string tag, const key &stroke)
150 {
151 keybinding strokes;
152 strokes.push_back(stroke);
153 set(tag, strokes);
154 }
155
163 bool key_matches(const key &k, std::string tag) const;
164 };
165
172 key parse_key(const std::wstring &keystr);
173
183 std::wstring keyname(const key &k);
184
191 std::wstring readable_keyname(const key &k);
192
199 }
200}
201
202// Stolen from pinfo. I don't like the looks of it, but presumably it works
203// (in some circumstances). This is a FIXME, btw :)
204/* adapted from Midnight Commander */
205
206// Having read a bit more, it appears that the control modifier
207// clears bits 5 and 4. I think KEY_ALT is utterly broken.
208
215#define KEY_CTRL(x) key(((x)&~(64|32)), false)
216#define KEY_ALT(x) key((0x200 | (x)), false)
217
218
219#endif
Stores the keys bound to various functions.
Definition: keybindings.h:88
void set(std::string tag, const key &stroke)
Modify a binding in this scope.
Definition: keybindings.h:149
std::wstring keyname(const std::string &tag) const
Definition: keybindings.cc:465
bool key_matches(const key &k, std::string tag) const
Test whether a key is bound to a function.
Definition: keybindings.cc:325
std::wstring readable_keyname(const std::string &tag) const
Definition: keybindings.cc:479
keybindings(keybindings *_parent=nullptr)
Create a new key-binding scope.
Definition: keybindings.h:101
void set(std::string tag, keybinding strokes)
Modify a binding in this scope.
keybinding get(const std::string &tag) const
Retrieve the binding of the given function.
Definition: keybindings.h:120
wstring readable_keyname(const key &k)
Convert a keystroke to a human-readable keyname.
Definition: keybindings.cc:456
wstring keyname(const key &k)
Convert a keystroke to its string definition.
Definition: keybindings.cc:424
keybindings global_bindings
The global keybindings object.
Definition: keybindings.cc:43
std::vector< key > keybinding
The type used to store the keybindings of a function.
Definition: keybindings.h:74
The namespace containing everything defined by cwidget.
Definition: columnify.cc:28
Represents a keystroke as seen by curses.
Definition: keybindings.h:43
bool operator<(const key &other) const
Lexicographic ordering on keys.
Definition: keybindings.h:61
wint_t ch
The key code.
Definition: keybindings.h:45
bool function_key
If true, this is a function key.
Definition: keybindings.h:48