cwidget  0.5.17
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 <list>
24 #include <map>
25 #include <string>
26 
27 #include <cwidget/curses++.h>
28 
34 namespace 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=NULL):parent(_parent) {}
102 
108  std::wstring keyname(const std::string &tag);
109 
110 
117  std::wstring readable_keyname(const std::string &tag);
118 
120  keybinding get(std::string tag)
121  {
122  std::map<std::string, keybinding>::iterator found=keymap.find(tag);
123 
124  if(found==keymap.end())
125  return keybinding();
126  else
127  return found->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);
164  };
165 
172  key parse_key(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:87
keybindings global_bindings
The global keybindings object.
Definition: keybindings.cc:43
bool function_key
If true, this is a function key.
Definition: keybindings.h:48
The namespace containing everything defined by cwidget.
Definition: columnify.cc:26
keybindings(keybindings *_parent=NULL)
Create a new key-binding scope.
Definition: keybindings.h:101
std::vector< key > keybinding
The type used to store the keybindings of a function.
Definition: keybindings.h:74
Represents a keystroke as seen by curses.
Definition: keybindings.h:42
wstring readable_keyname(const key &k)
Convert a keystroke to a human-readable keyname.
Definition: keybindings.cc:455
wstring keyname(const key &k)
Convert a keystroke to its string definition.
Definition: keybindings.cc:423
bool operator<(const key &other) const
Lexicographic ordering on keys.
Definition: keybindings.h:61
wint_t ch
The key code.
Definition: keybindings.h:45