1 // new_gui.cxx: implementation of XML-configurable GUI support.
8 #include <simgear/compiler.h>
9 #include <simgear/structure/exception.hxx>
11 #include <Main/fg_props.hxx>
13 #include "menubar.hxx"
19 ////////////////////////////////////////////////////////////////////////
20 // Implementation of NewGUI.
21 ////////////////////////////////////////////////////////////////////////
25 : _menubar(new FGMenuBar),
40 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
41 ulMakePath(path2, path1, "dialogs");
51 _menubar = new FGMenuBar;
59 fgTie("/sim/menubar/visibility", this,
60 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
66 fgUntie("/sim/menubar/visibility");
70 NewGUI::update (double delta_time_sec)
72 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
73 for(/**/; iter != _active_dialogs.end(); iter++)
74 iter->second->update();
78 NewGUI::showDialog (const string &name)
80 if (_dialog_props.find(name) == _dialog_props.end()) {
81 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
84 if(!_active_dialogs[name])
85 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
91 NewGUI::closeActiveDialog ()
93 if (_active_dialog == 0)
96 // Kill any entries in _active_dialogs... Is there an STL
97 // algorithm to do (delete map entries by value, not key)? I hate
99 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
100 for(/**/; iter != _active_dialogs.end(); iter++) {
101 if(iter->second == _active_dialog) {
102 _active_dialogs.erase(iter);
103 // iter is no longer valid
108 delete _active_dialog;
114 NewGUI::closeDialog (const string& name)
116 if(_active_dialogs.find(name) != _active_dialogs.end()) {
117 if(_active_dialog == _active_dialogs[name])
119 delete _active_dialogs[name];
120 _active_dialogs.erase(name);
123 return false; // dialog wasn't open...
127 NewGUI::setActiveDialog (FGDialog * dialog)
129 _active_dialog = dialog;
133 NewGUI::getActiveDialog ()
135 return _active_dialog;
139 NewGUI::getMenuBar ()
145 NewGUI::getMenuBarVisible () const
147 return _menubar->isVisible();
151 NewGUI::setMenuBarVisible (bool visible)
164 _dialog_props.clear();
168 test_extension (const char * path, const char * ext)
170 int pathlen = strlen(path);
171 int extlen = strlen(ext);
173 for (int i = 1; i <= pathlen && i <= extlen; i++) {
174 if (path[pathlen-i] != ext[extlen-i])
181 NewGUI::newDialog (SGPropertyNode* props)
183 const char* cname = props->getStringValue("name");
185 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
189 if(!_active_dialogs[name])
190 _dialog_props[name] = props;
194 NewGUI::readDir (const char * path)
196 ulDir * dir = ulOpenDir(path);
199 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
204 for (ulDirEnt * dirEnt = ulReadDir(dir);
206 dirEnt = ulReadDir(dir)) {
210 ulMakePath(subpath, path, dirEnt->d_name);
212 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
213 SGPropertyNode * props = new SGPropertyNode;
215 readProperties(subpath, props);
216 } catch (const sg_exception &) {
217 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
222 SGPropertyNode *nameprop = props->getNode("name");
224 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
225 << " has no name; skipping.");
229 string name = nameprop->getStringValue();
230 if (_dialog_props[name])
231 delete (SGPropertyNode *)_dialog_props[name];
233 _dialog_props[name] = props;
239 // end of new_gui.cxx