Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

key_menu.C

Go to the documentation of this file.
00001 /*!
00002  *  \file key_menu.C
00003  *  \brief Contains the implementation of the KeyMenu class.
00004  *
00005  *  \sa key_menu.H
00006  *
00007  */
00008 
00009 #include <iostream>
00010 #include <vector>
00011 #include <string>
00012 
00013 using namespace std;
00014 
00015 #include "geom/fsa.H"
00016 
00017 #include "key_menu.H"
00018 
00019 /*!
00020  *  \brief Holds all information needed about an entry in a KeyMenu.
00021  *
00022  */
00023 struct KeyMenuItem {
00024    
00025    KeyMenuItem(char k, string d, KeyMenu::key_callback_t cb)
00026       : key(k), desc(d), callback(cb) { }
00027    
00028    char key;   
00029    string desc;   
00030    KeyMenu::key_callback_t callback;
00031    
00032 };
00033 
00034 /*!
00035  *  \param[in] start_in The FSA State where keyboard events will originate from.
00036  *
00037  */
00038 KeyMenu::KeyMenu(State *start_in)
00039    : start(start_in)
00040 {
00041    
00042 }
00043       
00044 /*!
00045  *  \param[in] key The character representing the key on the keyboard that will
00046  *  be mapped to the given operation.
00047  *  \param[in] desc The description of the operation that will be mapped to the
00048  *  supplied key.
00049  *  \param[in] cb A callback function that will be called when the supplied key
00050  *  is pressed.
00051  *
00052  *  \note If this function is called with the same key multiple times, only the
00053  *  most recent calling will have any effect.
00054  *
00055  */
00056 void
00057 KeyMenu::add_menu_item(char key, const string &desc, key_callback_t cb)
00058 {
00059    
00060    // Remove the key if it is already in the menu:
00061    for(unsigned i = 0; i < menu_items.size(); ++i){
00062       
00063       if(menu_items[i].key == key){
00064          
00065          remove_menu_item(key);
00066          break;
00067          
00068       }
00069       
00070    }
00071    
00072    // Add the key to the menu:
00073    
00074    // Add the FSA arc:
00075    *start += Arc(Event(NULL, Evd(key,KEYD)), new CallFunc_t<Event>(cb, start));
00076    
00077    // Add the KeyMenuItem to the list:
00078    menu_items.push_back(KeyMenuItem(key, desc, cb));
00079    
00080 }
00081 
00082 /*!
00083  *  \param[in] keys A string of characters representing the keys on the keyboard
00084  *  that will be mapped to the given operation.
00085  *  \param[in] desc The description of the operation that will be mapped to the
00086  *  supplied keys.
00087  *  \param[in] cb A callback function that will be called when any of the
00088  *  supplied keys is pressed.
00089  *
00090  *  \note If the same character occurs multiple times in \p keys, it will still
00091  *  only be added to the menu once.
00092  *
00093  */
00094 void
00095 KeyMenu::add_menu_item(const char *keys, const string &desc, key_callback_t cb)
00096 {
00097    
00098    // Loop over all characters in the string and add each one the menu:
00099    for(; *keys; ++keys){
00100       
00101       add_menu_item(*keys, desc, cb);
00102       
00103    }
00104    
00105 }
00106 
00107 /*!
00108  *  \param[in] key Character representing the key to remove from the menu.
00109  *
00110  *  \note This function does nothing if \p key is not already in the menu.
00111  *
00112  */
00113 void
00114 KeyMenu::remove_menu_item(char key)
00115 {
00116    
00117    unsigned key_index = (uint)-1;
00118    
00119    // Find the key:
00120    for(unsigned i = 0; i < menu_items.size(); ++i){
00121       
00122       if(menu_items[i].key == key){
00123          
00124          key_index = i;
00125          break;
00126          
00127       }
00128       
00129    }
00130    
00131    // Do nothing if key is not found:
00132    if (key_index == (uint)-1)
00133       return;
00134    
00135    // Remove the FSA arc:
00136    *start -= Arc(Event(NULL, Evd(menu_items[key_index].key,KEYD)),
00137                  new CallFunc_t<Event>(menu_items[key_index].callback, start));
00138    
00139    // Remove the KeyMenuItem from the list:
00140    menu_items.erase(menu_items.begin() + key_index);
00141    
00142 }
00143 
00144 /*!
00145  *  \param[in] keys String of characters representing the keys to remove from
00146  *  the menu.
00147  *
00148  */
00149 void
00150 KeyMenu::remove_menu_item(const char *keys)
00151 {
00152    
00153    // Loop over all characters in the string and remove each one from the menu:
00154    for(; *keys; ++keys){
00155       
00156       remove_menu_item(*keys);
00157       
00158    }
00159    
00160 }
00161 
00162 /*!
00163  *  \param[in] key Character representing the key to get the description for.
00164  *
00165  *  \return The description of the supplied key if it is in the menu and the
00166  *  empty string otherwise.
00167  *
00168  */
00169 std::string
00170 KeyMenu::get_item_desc(char key)
00171 {
00172    
00173    string desc;
00174    
00175    // Find the key:
00176    for(unsigned i = 0; i < menu_items.size(); ++i){
00177       
00178       if(menu_items[i].key == key){
00179          
00180          desc = menu_items[i].desc;
00181          break;
00182          
00183       }
00184       
00185    }
00186    
00187    return desc;
00188    
00189 }
00190 
00191 /*!
00192  *  \param[inout] The stream to output menu listing to.
00193  *
00194  */
00195 void
00196 KeyMenu::display_menu(ostream &out)
00197 {
00198    
00199    for (unsigned i = 0; i < menu_items.size(); ++i){
00200       
00201       out << menu_items[i].key << "\t" << menu_items[i].desc << endl;
00202       
00203    }
00204    
00205 }

Generated on Mon Sep 18 11:39:31 2006 for jot by  doxygen 1.4.4