Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

KeyBindingsMap.h

Go to the documentation of this file.
00001 #ifndef Impala_Visualization_KeyBindingsMap_h
00002 #define Impala_Visualization_KeyBindingsMap_h
00003 
00004 /*
00005  *******************************************************************************
00006     KeyBindingsMap.h:   Simple Keybindings using std::map<String,String>
00007 
00008     typedef std::map<String,String> KeyBindingsMap
00009 
00010     KeyBindings can be programmed and/or placed inside a
00011     Impala::Basis::CmdOptions based .ini file.
00012 
00013     EXAMPLES
00014         goNextFile    n
00015         goNextFile    ctrl#n
00016         goNextFile    pagedown
00017         goNextFile    ctrl#down
00018         goNextFile    ctrl-shift#left
00019         goNextFile    ctrl#f
00020         goNextFile    fn2
00021         goNextFile    ctrl#fn2
00022 
00023     MULTIPLE KEYBINDINGS
00024     Support is present to allow for the use of multiple KeyBindings:
00025         goNextFile        n
00026         goNextFile-1      N
00027         goNextFile-2      right
00028         goNextFile-3      ctrl#left
00029 
00030         goPrevFile        p
00031         goPrevFile-1      P
00032         goPrevFile-2      left
00033         goPrevFile-3      ctrl#left
00034 
00035         Meaning that the user can go to the next file either by typing:
00036             n, N, right arrow, or ctrl left arrow
00037         or go to the previous file by typing:
00038             p, P, left arrow, or ctrl right arrow
00039 
00040         Alternatives are counted from 1.
00041         So there is a basic form:
00042             goNextFile
00043         with:
00044             goNextFile-1 as first alternative 
00045             goNextFile-2 as second alternative 
00046             goNextFile-3 as third alternative 
00047 
00048     With:
00049         CheckKeyBinding(myKB, "goNextFile", c, state)
00050     or:
00051         CheckKeyBinding(myKB, "goNextFile-2", c, state)
00052     you can check for 1 exact KeyBinding.
00053 
00054     With:
00055         CheckKeyBindingExt(myKB, "goNextFile", c, state)
00056     you can check for a basic keyBinding plus 6 alternatives.
00057 
00058     MORE THAN 6 ALTERNATIVES:
00059     Provided you have done something like:
00060         InitKeyBinding(myKB, "goNextFile", "n", "N", "right", "ctrl#left", "d");
00061         TryInitKeyBinding(myKB, "goNextFile", "pagedown", 7);
00062         TryInitKeyBinding(myKB, "goNextFile", "ctrl#pagedown", 8);
00063     then with
00064         CheckKeyBindingExt(myKB, "goNextFile", c, state, 8)
00065     you can check for a basic keyBinding plus 8 alternatives.
00066 
00067     NOTE:
00068     In this case in an .ini file the user could:
00069       - add alternatives for the undefined alternatives 5 and/or 6:
00070             goNextFile-5    down
00071     or
00072       - overrule any of the bindings:
00073             goNextFile-3    shift#right
00074             goNextFile-8    shift#pagedown
00075 
00076     LIMITING NUMBER OF ALTERNATIVES:
00077     To allow for only a base and 1 alternative KeyBinding, which both can be
00078     overruled in a .ini file, in your code do NOT use
00079         InitKeyBinding(...)
00080     but instead use:
00081         TryInitKeyBinding(myKB, "goNextFile", "right");
00082         TryInitKeyBinding(myKB, "goNextFile", "shift-right", 1);
00083 
00084     EXAMPLE OF USE:
00085     In code the use of KeyBindingsMap would look something like this:
00086     ----------------------------------------------------------------------------
00087         void MyInit()
00088         {
00089             InitKeyBinding(myKB, "goNextFile", "n", "N", "right");
00090             InitKeyBinding(myKB, "goPrevFile", "p", "P", "left");
00091               // Below only if you want to support more than 6 alternatives
00092               // TryInitKeyBinding(myKb, "goPrevFile", "shift#left", 7);
00093               // TryInitKeyBinding(myKb, "goPrevFile", "ctrl-shift#left", 8);
00094               // Now upto 8 alternatives can be checked:
00095               // if (CheckKeyBindingExt(myKB, "goPrevFile", c, state, 8))
00096         }
00097 
00098         void HandleKey(int c, int state)
00099         {
00100             if (CheckKeyBindingExt(myKB, "goNextFile", c, state))
00101                 GoNextFile();
00102             else if (CheckKeyBindingExt(myKB, "goPrevFile", c, state))
00103                 GoPrevFile();
00104         }
00105 
00106         Impala::Visualization::KeyBindingsMap myKB;
00107     ----------------------------------------------------------------------------
00108 
00109     Autor: Richard van Balen
00110 ********************************************************************************
00111 */
00112 
00113 #include <map>
00114 
00115 #ifndef Impala_Basis_String_h
00116 #include "Basis/String.h"
00117 #endif
00118 
00119 #ifndef Impala_Basis_CmdOptions_h
00120 #include "Basis/CmdOptions.h"
00121 #endif
00122 
00123 namespace Impala {
00124 namespace Visualization {
00125 
00126 typedef std::map<String,String> KeyBindingsMap;
00127 
00128 void
00129 ParseKeyBindingStr(String& str, int& c, int& state)
00130 {
00131     int     modState = 0;
00132     int     modIdx;
00133     String  modStr;
00134 
00135     state = c = 0;
00136     if ((modIdx=str.find('#'))!=String::npos)
00137     {
00138         modStr = str.substr(0,modIdx);
00139         if (modStr == "ctrl")
00140             modState |= oglControl;
00141         else if (modStr == "shift")
00142             modState |= oglShift;
00143         else if (modStr == "ctrl-shift" || modStr == "shift-ctrl")
00144             modState |= oglControl | oglShift;
00145     }
00146 
00147     state = modState;
00148 
00149     int     charIdx = modIdx ? modIdx+1 : 0;
00150     String  charStr = str.substr(charIdx);
00151     if (charStr.length()==1)
00152     {
00153         c = charStr[0];
00154         if (modState & oglControl)
00155         {
00156             c = toupper(c);
00157             c -= 'A'-1;
00158         }
00159     }
00160     else if (charStr[0] == 'f' && charStr[1] == 'n')
00161     {
00162         int fnNr = atoi(charStr.substr(2));
00163         c = oglFUNC(fnNr);
00164     }
00165     else if (charStr == "pageup")
00166         c = oglPAGEUP;
00167     else if (charStr == "pagedown")
00168         c = oglPAGEDOWN;
00169     else if (charStr == "up")
00170         c = oglUP;
00171     else if (charStr == "down")
00172         c = oglDOWN;
00173     else if (charStr == "right")
00174         c = oglRIGHT;
00175     else if (charStr == "left")
00176         c = oglLEFT;
00177     else if (charStr == "home")
00178         c = oglHOME;
00179     else if (charStr == "end")
00180         c = oglEND;
00181     else if (charStr == "insert")
00182         c = oglINSERT;
00183     else if (charStr == "delete")
00184         c = oglDELETE;
00185     else if (charStr == "esc")
00186         c = oglESC;
00187     else if (charStr == "space")
00188         c = ' ';
00189     else if (charStr == "\\t")
00190         c = '\t';
00191     else if (charStr == "\\n")
00192         c = '\n';
00193 }
00194 
00195 String
00196 GetKeyBinding(String name, String dflt)
00197 {
00198     CmdOptions& options = CmdOptions::GetInstance();
00199     String str = options.GetString(name, dflt);
00200     return str;
00201 }
00202 
00203 bool
00204 CheckKeyBinding(KeyBindingsMap& kb, String keyStr, int c, int state)
00205 {
00206     int     cVal, stateVal;
00207     String  str = kb[keyStr];
00208     ParseKeyBindingStr(str, cVal, stateVal);
00209     if (!stateVal && isupper(c))
00210         stateVal = oglShift;
00211     return (c==cVal && (state==stateVal));
00212 }
00213 
00214 bool
00215 CheckKeyBindingExt(KeyBindingsMap& kb, String keyStr, int c, int state, int n=6)
00216 {
00217     if (CheckKeyBinding(kb, keyStr, c, state))
00218         return true;
00219     for (int i=1; i<n; i++)
00220         if (CheckKeyBinding(kb, keyStr + "-" + MakeString(i), c, state))
00221             return true;
00222     return false;
00223 }
00224 
00225 void
00226 TryInitKeyBinding(KeyBindingsMap& kb, String keyStr, String dfltStr, int n=0)
00227 {
00228     if (keyStr.empty())
00229         return;
00230 
00231     String k = keyStr;
00232     if (n>0)
00233         k += "-" + MakeString(n);
00234     String bindStr = GetKeyBinding(k, dfltStr);
00235     if (!bindStr.empty())
00236         kb[k] = bindStr;
00237 }
00238 
00239 void
00240 InitKeyBinding(KeyBindingsMap& kb, String keyStr, String dfltStr0,
00241                String dfltStr1="", String dfltStr2="",
00242                String dfltStr3="", String dfltStr4="",
00243                String dfltStr5="", String dfltStr6="")
00244 {
00245     TryInitKeyBinding(kb, keyStr, dfltStr0, 0);
00246     TryInitKeyBinding(kb, keyStr, dfltStr1, 1);
00247     TryInitKeyBinding(kb, keyStr, dfltStr2, 2);
00248     TryInitKeyBinding(kb, keyStr, dfltStr3, 3);
00249     TryInitKeyBinding(kb, keyStr, dfltStr4, 4);
00250     TryInitKeyBinding(kb, keyStr, dfltStr5, 5);
00251     TryInitKeyBinding(kb, keyStr, dfltStr6, 6);
00252 }
00253 
00254 } // namespace Visualization
00255 } // namespace Impala
00256 
00257 #endif // Impala_Visualization_KeyBindingsMap_h

Generated on Fri Mar 19 09:31:50 2010 for ImpalaSrc by  doxygen 1.5.1