cwidget
0.5.16
|
00001 // keybindings.h, -*-c++-*- 00002 // 00003 // Copyright 1999-2001, 2003-2005, 2008 Daniel Burrows 00004 // 00005 // This program is free software; you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation; either version 2 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; see the file COPYING. If not, write to 00017 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 // Boston, MA 02111-1307, USA. 00019 00020 #ifndef KEYBINDINGS_H 00021 #define KEYBINDINGS_H 00022 00023 #include <list> 00024 #include <map> 00025 #include <string> 00026 00027 #include <cwidget/curses++.h> 00028 00034 namespace cwidget 00035 { 00036 namespace config 00037 { 00042 struct key 00043 { 00045 wint_t ch; 00046 00048 bool function_key; 00049 00050 key() 00051 :ch((wint_t) ERR), function_key(true) 00052 { 00053 } 00054 00055 key(wint_t _ch, bool _function_key) 00056 :ch(_ch), function_key(_function_key) 00057 { 00058 } 00059 00061 bool operator<(const key &other) const 00062 { 00063 return ch < other.ch || (ch == other.ch && 00064 !function_key && other.function_key); 00065 } 00066 00067 bool operator==(const key &other) const 00068 { 00069 return ch == other.ch && function_key == other.function_key; 00070 } 00071 }; 00072 00074 typedef std::vector<key> keybinding; 00075 00087 class keybindings 00088 { 00089 std::map<std::string, keybinding> keymap; 00090 00091 keybindings *parent; 00092 00093 // It's way too easy to accidentally invoke the automatic copy 00094 // constructor instead of the real one. 00095 keybindings(const keybindings &_parent); 00096 public: 00101 keybindings(keybindings *_parent=NULL):parent(_parent) {} 00102 00108 std::wstring keyname(const std::string &tag); 00109 00110 00117 std::wstring readable_keyname(const std::string &tag); 00118 00120 keybinding get(std::string tag) 00121 { 00122 std::map<std::string, keybinding>::iterator found=keymap.find(tag); 00123 00124 if(found==keymap.end()) 00125 return keybinding(); 00126 else 00127 return found->second; 00128 } 00129 00139 void set(std::string tag, keybinding strokes); 00140 00149 void set(std::string tag, const key &stroke) 00150 { 00151 keybinding strokes; 00152 strokes.push_back(stroke); 00153 set(tag, strokes); 00154 } 00155 00163 bool key_matches(const key &k, std::string tag); 00164 }; 00165 00172 key parse_key(std::wstring keystr); 00173 00183 std::wstring keyname(const key &k); 00184 00191 std::wstring readable_keyname(const key &k); 00192 00198 extern keybindings global_bindings; 00199 } 00200 } 00201 00202 // Stolen from pinfo. I don't like the looks of it, but presumably it works 00203 // (in some circumstances). This is a FIXME, btw :) 00204 /* adapted from Midnight Commander */ 00205 00206 // Having read a bit more, it appears that the control modifier 00207 // clears bits 5 and 4. I think KEY_ALT is utterly broken. 00208 00215 #define KEY_CTRL(x) key(((x)&~(64|32)), false) 00216 #define KEY_ALT(x) key((0x200 | (x)), false) 00217 00218 00219 #endif