keybindings.h

00001 // keybindings.h, -*-c++-*-
00002 //
00003 //  Copyright 1999-2001, 2003-2005 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 <string>
00024 #include <list>
00025 
00026 #include <cwidget-config.h>
00027 
00028 #ifdef CWIDGET_HAVE_HASH_MAP
00029 #include <hash_map>
00030 #else
00031 #ifdef CWIDGET_HAVE_EXT_HASH_MAP
00032 #include <ext/hash_map>
00033 #else
00034 // Fallback to the non-hashing map class
00035 #include <map>
00036 #define hash_map map
00037 #endif
00038 #endif
00039 
00040 #include <cwidget/generic/util/strhash.h>
00041 #include <cwidget/curses++.h>
00042 
00043 namespace cwidget
00044 {
00045   namespace config
00046   {
00051     struct key
00052     {
00054       wint_t ch;
00055 
00057       bool function_key;
00058 
00059       key()
00060         :ch((wint_t) ERR), function_key(true)
00061       {
00062       }
00063 
00064       key(wint_t _ch, bool _function_key)
00065         :ch(_ch), function_key(_function_key)
00066       {
00067       }
00068 
00070       bool operator<(const key &other) const
00071       {
00072         return ch < other.ch || (ch == other.ch &&
00073                                  !function_key && other.function_key);
00074       }
00075 
00076       bool operator==(const key &other) const
00077       {
00078         return ch == other.ch && function_key == other.function_key;
00079       }
00080     };
00081 
00082     typedef std::vector<key> keybinding;
00083 
00084     class keybindings
00085     {
00086       CWIDGET_HASH_NAMESPACE::hash_map<std::string, keybinding> keymap;
00087 
00088       keybindings *parent;
00089 
00090       // It's way too easy to accidentally invoke the automatic copy
00091       // constructor instead of the real one.
00092       keybindings(const keybindings &_parent);
00093     public:
00094       keybindings(keybindings *_parent=NULL):parent(_parent) {}
00095 
00099       std::wstring keyname(const std::string &tag);
00100 
00101 
00105       std::wstring readable_keyname(const std::string &tag);
00106 
00107       keybinding get(std::string tag)
00108       // Returns the keybinding for the given string.  Almost never needed.
00109       {
00110         CWIDGET_HASH_NAMESPACE::hash_map<std::string, keybinding>::iterator found=keymap.find(tag);
00111 
00112         if(found==keymap.end())
00113           return keybinding();
00114         else
00115           return found->second;
00116       }
00117 
00118       void set(std::string tag, keybinding strokes);
00119       // Adds a setting for the given binding, clobbering whatever was there
00120       // previously.
00121 
00122       void set(std::string tag, const key &stroke)
00123       {
00124         keybinding strokes;
00125         strokes.push_back(stroke);
00126         set(tag, strokes);
00127       }
00128 
00129       bool key_matches(const key &k, std::string tag);
00130       // Tests whether the given keystroke matches the keybinding with the given
00131       // name.  If no keybinding by that name exists, the match fails.
00132     };
00133 
00134     key parse_key(std::wstring keystr);
00135     // Parses a string to a keycode.  Returns ERR if the parse fails.
00136 
00137     std::wstring keyname(const key &k);
00138     // Returns a string identifying the given keystroke.
00139 
00143     std::wstring readable_keyname(const key &k);
00144 
00145     extern keybindings global_bindings;
00146     // For now, this is where the global bindings are stored (I might want to move
00147     // it in the future, hmmm..)
00148   }
00149 }
00150 
00151 // Stolen from pinfo.  I don't like the looks of it, but presumably it works
00152 // (in some circumstances).  This is a FIXME, btw :)
00153 /* adapted from Midnight Commander */
00154 
00155 // Having read a bit more, it appears that the control modifier
00156 // clears bits 5 and 4.  I think KEY_ALT is utterly broken.
00157 #define KEY_CTRL(x) key(((x)&~(64|32)), false)
00158 #define KEY_ALT(x) key((0x200 | (x)), false)
00159 
00160 
00161 #endif

Generated on Fri Nov 16 03:33:05 2007 for cwidget by  doxygen 1.5.1