1 // new_gui.cxx: implementation of XML-configurable GUI support.
12 #include <sys/types.h>
16 #include <simgear/compiler.h>
17 #include <simgear/structure/exception.hxx>
18 #include <simgear/props/props_io.hxx>
19 #include <simgear/misc/sg_dir.hxx>
21 #include <boost/algorithm/string/case_conv.hpp>
23 #include <Main/fg_props.hxx>
25 #if defined(SG_UNIX) && !defined(SG_MAC)
29 #include "FGPUIMenuBar.hxx"
30 #include "FGPUIDialog.hxx"
31 #include "FGFontCache.hxx"
32 #include "FGColor.hxx"
37 ////////////////////////////////////////////////////////////////////////
38 // Implementation of NewGUI.
39 ////////////////////////////////////////////////////////////////////////
44 : _menubar(new FGPUIMenuBar),
51 _dialog_props.clear();
52 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
60 SGPath p(globals->get_fg_root(), "gui/dialogs");
69 fgSetBool("/sim/signals/reinit-gui", true);
79 NewGUI::reset (bool reload)
81 map<string,FGDialog *>::iterator iter;
82 std::vector<string> dlg;
83 // close all open dialogs and remember them ...
84 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
85 dlg.push_back(iter->first);
88 for (i = 0; i < dlg.size(); i++)
94 _menubar.reset(new FGPUIMenuBar);
97 _dialog_props.clear();
105 // open dialogs again
106 for (i = 0; i < dlg.size(); i++)
113 fgTie("/sim/menubar/visibility", this,
114 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
120 fgUntie("/sim/menubar/visibility");
124 NewGUI::update (double delta_time_sec)
126 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
127 for(/**/; iter != _active_dialogs.end(); iter++)
128 iter->second->update();
132 NewGUI::showDialog (const string &name)
134 if (_dialog_props.find(name) == _dialog_props.end()) {
135 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
138 if(!_active_dialogs[name])
139 _active_dialogs[name] = new FGPUIDialog(_dialog_props[name]);
145 NewGUI::closeActiveDialog ()
147 if (_active_dialog == 0)
150 // Kill any entries in _active_dialogs... Is there an STL
151 // algorithm to do (delete map entries by value, not key)? I hate
153 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
154 for(/**/; iter != _active_dialogs.end(); iter++) {
155 if(iter->second == _active_dialog) {
156 _active_dialogs.erase(iter);
157 // iter is no longer valid
162 delete _active_dialog;
168 NewGUI::closeDialog (const string& name)
170 if(_active_dialogs.find(name) != _active_dialogs.end()) {
171 if(_active_dialog == _active_dialogs[name])
173 delete _active_dialogs[name];
174 _active_dialogs.erase(name);
177 return false; // dialog wasn't open...
181 NewGUI::getDialogProperties (const string &name)
183 if(_dialog_props.find(name) != _dialog_props.end())
184 return _dialog_props[name];
186 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
191 NewGUI::getDialog (const string &name)
193 if(_active_dialogs.find(name) != _active_dialogs.end())
194 return _active_dialogs[name];
196 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
201 NewGUI::setActiveDialog (FGDialog * dialog)
203 _active_dialog = dialog;
207 NewGUI::getActiveDialog ()
209 return _active_dialog;
213 NewGUI::getMenuBar ()
215 return _menubar.get();
219 NewGUI::getMenuBarVisible () const
221 return _menubar->isVisible();
225 NewGUI::setMenuBarVisible (bool visible)
234 NewGUI::newDialog (SGPropertyNode* props)
236 const char* cname = props->getStringValue("name");
238 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
242 if(_active_dialogs.find(name) == _active_dialogs.end())
243 _dialog_props[name] = props;
247 NewGUI::readDir (const SGPath& path)
249 simgear::Dir dir(path);
250 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
252 for (unsigned int i=0; i<xmls.size(); ++i) {
253 SGPropertyNode * props = new SGPropertyNode;
255 readProperties(xmls[i].str(), props);
256 } catch (const sg_exception &) {
257 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
262 SGPropertyNode *nameprop = props->getNode("name");
264 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmls[i].str()
265 << " has no name; skipping.");
269 string name = nameprop->getStringValue();
270 _dialog_props[name] = props;
276 ////////////////////////////////////////////////////////////////////////
278 ////////////////////////////////////////////////////////////////////////
281 NewGUI::setStyle (void)
284 for (it = _colors.begin(); it != _colors.end(); ++it)
288 // set up the traditional colors as default
289 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
290 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
291 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
292 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
293 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
294 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
295 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
297 //puSetDefaultStyle();
299 int which = fgGetInt("/sim/gui/current-style", 0);
300 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
301 SGPropertyNode *n = sim->getChild("style", which);
303 n = sim->getChild("style", 0, true);
305 setupFont(n->getNode("fonts/gui", true));
306 n = n->getNode("colors", true);
308 for (int i = 0; i < n->nChildren(); i++) {
309 SGPropertyNode *child = n->getChild(i);
310 _colors[child->getName()] = new FGColor(child);
313 FGColor *c = _colors["background"];
314 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
319 NewGUI::setupFont (SGPropertyNode *node)
321 _font = globals->get_fontcache()->get(node);
322 puSetDefaultFonts(*_font, *_font);
326 // end of new_gui.cxx