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