keybindings.h

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 
00029 namespace cwidget
00030 {
00031   namespace config
00032   {
00037     struct key
00038     {
00040       wint_t ch;
00041 
00043       bool function_key;
00044 
00045       key()
00046         :ch((wint_t) ERR), function_key(true)
00047       {
00048       }
00049 
00050       key(wint_t _ch, bool _function_key)
00051         :ch(_ch), function_key(_function_key)
00052       {
00053       }
00054 
00056       bool operator<(const key &other) const
00057       {
00058         return ch < other.ch || (ch == other.ch &&
00059                                  !function_key && other.function_key);
00060       }
00061 
00062       bool operator==(const key &other) const
00063       {
00064         return ch == other.ch && function_key == other.function_key;
00065       }
00066     };
00067 
00068     typedef std::vector<key> keybinding;
00069 
00070     class keybindings
00071     {
00072       std::map<std::string, keybinding> keymap;
00073 
00074       keybindings *parent;
00075 
00076       // It's way too easy to accidentally invoke the automatic copy
00077       // constructor instead of the real one.
00078       keybindings(const keybindings &_parent);
00079     public:
00080       keybindings(keybindings *_parent=NULL):parent(_parent) {}
00081 
00085       std::wstring keyname(const std::string &tag);
00086 
00087 
00091       std::wstring readable_keyname(const std::string &tag);
00092 
00093       keybinding get(std::string tag)
00094       // Returns the keybinding for the given string.  Almost never needed.
00095       {
00096         std::map<std::string, keybinding>::iterator found=keymap.find(tag);
00097 
00098         if(found==keymap.end())
00099           return keybinding();
00100         else
00101           return found->second;
00102       }
00103 
00104       void set(std::string tag, keybinding strokes);
00105       // Adds a setting for the given binding, clobbering whatever was there
00106       // previously.
00107 
00108       void set(std::string tag, const key &stroke)
00109       {
00110         keybinding strokes;
00111         strokes.push_back(stroke);
00112         set(tag, strokes);
00113       }
00114 
00115       bool key_matches(const key &k, std::string tag);
00116       // Tests whether the given keystroke matches the keybinding with the given
00117       // name.  If no keybinding by that name exists, the match fails.
00118     };
00119 
00120     key parse_key(std::wstring keystr);
00121     // Parses a string to a keycode.  Returns ERR if the parse fails.
00122 
00123     std::wstring keyname(const key &k);
00124     // Returns a string identifying the given keystroke.
00125 
00129     std::wstring readable_keyname(const key &k);
00130 
00131     extern keybindings global_bindings;
00132     // For now, this is where the global bindings are stored (I might want to move
00133     // it in the future, hmmm..)
00134   }
00135 }
00136 
00137 // Stolen from pinfo.  I don't like the looks of it, but presumably it works
00138 // (in some circumstances).  This is a FIXME, btw :)
00139 /* adapted from Midnight Commander */
00140 
00141 // Having read a bit more, it appears that the control modifier
00142 // clears bits 5 and 4.  I think KEY_ALT is utterly broken.
00143 #define KEY_CTRL(x) key(((x)&~(64|32)), false)
00144 #define KEY_ALT(x) key((0x200 | (x)), false)
00145 
00146 
00147 #endif

Generated on Mon Feb 16 01:16:31 2009 for cwidget by  doxygen 1.4.6