00001
00002
00003
00004
00005
00006
00007
00008 #include "gui/SubMenu.h"
00009 #include "gui/SimpleCommand.h"
00010 #include <strstream>
00011 #include <string>
00012 #include <cstdio>
00013
00014 namespace gui {
00015
00016
00017 Menu::Menu(GUI& gui)
00018 :m_gui(gui)
00019 {
00020
00021 m_fileMenu = &subMenu("File");
00022 endMenu();
00023 s_instance = this;
00024 }
00025 Menu::~Menu()
00026 {
00027 std::vector<SubMenu*>::iterator psm = m_sub_menus.begin();
00028 while (psm != m_sub_menus.end())
00029 delete (*psm++);
00030
00031 ClientList::iterator it = m_clients.begin();
00032 while (it != m_clients.end())
00033 delete (*it++);
00034
00035 std::vector<Command*>::iterator pcmd = m_commands.begin();
00036
00037 while( pcmd != m_commands.end()) {
00038 delete (*pcmd++);
00039 }
00040
00041 }
00042
00043 Menu* Menu::s_instance = 0;
00044
00045 Menu* Menu::instance(Menu* m)
00046 {
00047 if(m) s_instance = m;
00048 return s_instance;
00049 }
00050
00051 void Menu::run(bool paused)
00052
00053 {
00054
00055 ClientList::iterator it = m_clients.begin();
00056 while (it != m_clients.end())
00057 (*it++)->finishSetup();
00058
00059
00060 m_fileMenu->addSeparator();
00061 m_fileMenu->addButton("Exit", new SimpleCommand<Menu>(this, &Menu::quit));
00062
00063
00064 if( !paused ) m_gui.start();
00065 else m_gui.run();
00066 }
00067 void Menu::quit()
00068 {
00069
00070 ClientList::iterator it = m_clients.begin();
00071 while (it != m_clients.end()) (*it++)->quit();
00072
00073
00074 m_gui.quit();
00075 }
00076
00077
00078
00079 void Menu::check_messages()
00080 {
00081 m_gui.processMessages();
00082 }
00083
00084
00085 void Menu::add_button(const std::string& title, Command* command)
00086 {
00087 m_gui.addToMenu( title.c_str(), command);
00088 m_commands.push_back(command);
00089
00090 }
00091
00092 void Menu::addCommand(const std::string& title, Command* command)
00093 {
00094 m_gui.addToMenu( title.c_str(), command);
00095
00096 m_commands.push_back(command);
00097 }
00098
00099 GUI::Toggle* Menu::addToggle(const std::string& title, bool state, Command* set, Command* unset)
00100 {
00101 GUI::Toggle* t =m_gui.addToggleToMenu(title.c_str(), state, set, unset);
00102 if( state ) set->execute(); else unset->execute();
00103 m_commands.push_back(set);
00104 m_commands.push_back(unset);
00105 return t;
00106 }
00107
00108 Menu::Node* Menu::beginMenu(const std::string& name, Menu::Node* subnode)
00109 {
00110 return (Menu::Node*)m_gui.beginPullDownMenu(name.c_str(), (GUI::Menu*) subnode);
00111 }
00112
00113 void Menu::setMenu(Node* m)
00114 {
00115 m_gui.restorePullDownMenu( (GUI::Menu*) m);
00116 }
00117
00118 SubMenu& Menu::file_menu()
00119 {
00120 return *m_fileMenu;
00121 }
00122
00123
00124 void Menu::addSeparator()
00125 {
00126 m_gui.menuSeparator();
00127 }
00128
00129 void Menu::endMenu()
00130 {
00131 m_gui.endPullDownMenu();
00132 }
00133
00134
00135 void Menu::register_key(char key, Command* cmd)
00136 {
00137 m_key_map[key]=cmd;
00138 }
00139 bool Menu::strike(char key)
00140 {
00141 KeyCommandMap::const_iterator it = m_key_map.find(key);
00142 if( it != m_key_map.end() ) {
00143 (*it).second->execute();
00144 return true;
00145 }
00146 return false;
00147 }
00148
00149 void Menu::query(const std::string& ask, int* value)
00150 {
00151 char temp[20]; sprintf(temp,"%i", *value);
00152 char* answer = m_gui.askUser(ask.c_str(),temp ) ;
00153 if( answer && answer[0] )
00154 *value =atoi(answer) ;
00155 }
00156
00157 void Menu::query(const std::string& ask, double* value, int n)
00158 {
00159 std::ostrstream temp;
00160 for(int i=0; i<n; ++i) {
00161 temp << value[i] ;
00162 if( i<n-1) temp << ", ";
00163 }
00164 temp << '\0';
00165 std::string answer(m_gui.askUser(ask.c_str(), temp.str() ) );
00166 #ifdef _MSC_VER // not defined otherwise?
00167 temp.rdbuf()->freeze(false);
00168 #endif
00169 if( answer.size()==0 ) return;
00170 answer += ",";
00171
00172 int p = 0;
00173 for(int j=0; j< n; ++j) {
00174 int q = answer.substr(p).find_first_of(",");
00175 if( q <0 ) break;
00176 if( q >0 ) value[j] = atof(answer.substr(p,q).c_str());
00177 p += q+1;
00178 }
00179
00180
00181 }
00182
00183 void Menu::query(const std::string& ask, float* value)
00184 {
00185 char temp[20]; sprintf(temp,"%f", *value);
00186 char* answer = m_gui.askUser(ask.c_str(),temp ) ;
00187 if( answer && answer[0] )
00188 *value =float(atof(answer)) ;
00189 }
00190
00191 void Menu::query(const std::string& ask, std::string* value)
00192 {
00193 std::string answer = m_gui.askUser(ask.c_str(),value->c_str() ) ;
00194 if( answer.length() >0 )
00195 *value =answer ;
00196 }
00197
00198 }