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>
22 #include <boost/foreach.hpp>
24 #include <Main/fg_props.hxx>
26 #if defined(SG_UNIX) && !defined(SG_MAC)
30 #include "FGPUIMenuBar.hxx"
33 #include "FGCocoaMenuBar.hxx"
36 #if defined(SG_WINDOWS)
37 #include "FGWindowsMenuBar.hxx"
40 #include "FGPUIDialog.hxx"
41 #include "FGFontCache.hxx"
42 #include "FGColor.hxx"
44 // ignore the word Navaid here, it's a DataCache
45 #include <Navaids/NavDataCache.hxx>
50 ////////////////////////////////////////////////////////////////////////
51 // Implementation of NewGUI.
52 ////////////////////////////////////////////////////////////////////////
63 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
70 createMenuBarImplementation();
71 fgTie("/sim/menubar/visibility", this,
72 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
75 SGPath p(globals->get_fg_root(), "gui/dialogs");
78 SGPath aircraftDialogDir(string(fgGetString("/sim/aircraft-dir")), "gui/dialogs");
79 if (aircraftDialogDir.exists()) {
80 readDir(aircraftDialogDir);
83 // Fix for http://code.google.com/p/flightgear-bugs/issues/detail?id=947
84 fgGetNode("sim/menubar")->setAttribute(SGPropertyNode::PRESERVE, true);
91 fgUntie("/sim/menubar/visibility");
93 _dialog_props.clear();
100 fgSetBool("/sim/signals/reinit-gui", true);
110 NewGUI::createMenuBarImplementation()
113 if (fgGetBool("/sim/menubar/native", true)) {
114 _menubar.reset(new FGCocoaMenuBar);
117 #if defined(SG_WINDOWS)
118 if (fgGetBool("/sim/menubar/native", true)) {
119 // Windows-native menubar disabled for the moment, fall-through
121 // _menubar.reset(new FGWindowsMenuBar);
124 if (!_menubar.get()) {
125 _menubar.reset(new FGPUIMenuBar);
130 NewGUI::reset (bool reload)
132 map<string,FGDialog *>::iterator iter;
133 string_list openDialogs;
134 // close all open dialogs and remember them ...
135 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
136 openDialogs.push_back(iter->first);
138 BOOST_FOREACH(string d, openDialogs)
146 _dialog_props.clear();
147 _dialog_names.clear();
150 createMenuBarImplementation();
156 // open dialogs again
157 BOOST_FOREACH(string d, openDialogs)
172 NewGUI::update (double delta_time_sec)
174 SG_UNUSED(delta_time_sec);
175 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
176 for(/**/; iter != _active_dialogs.end(); iter++)
177 iter->second->update();
181 NewGUI::showDialog (const string &name)
183 // first, check if it's already shown
184 if (_active_dialogs.find(name) != _active_dialogs.end())
187 // check we know about the dialog by name
188 if (_dialog_names.find(name) == _dialog_names.end()) {
189 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
193 _active_dialogs[name] = new FGPUIDialog(getDialogProperties(name));
198 NewGUI::closeActiveDialog ()
200 if (_active_dialog == 0)
203 // Kill any entries in _active_dialogs... Is there an STL
204 // algorithm to do (delete map entries by value, not key)? I hate
206 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
207 for(/**/; iter != _active_dialogs.end(); iter++) {
208 if(iter->second == _active_dialog) {
209 _active_dialogs.erase(iter);
210 // iter is no longer valid
215 delete _active_dialog;
221 NewGUI::closeDialog (const string& name)
223 if(_active_dialogs.find(name) != _active_dialogs.end()) {
224 if(_active_dialog == _active_dialogs[name])
226 delete _active_dialogs[name];
227 _active_dialogs.erase(name);
230 return false; // dialog wasn't open...
234 NewGUI::getDialogProperties (const string &name)
236 if (_dialog_names.find(name) == _dialog_names.end()) {
237 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
241 NameDialogDict::iterator it = _dialog_props.find(name);
242 if (it == _dialog_props.end()) {
244 SGPath path = _dialog_names[name];
245 SGPropertyNode_ptr props = new SGPropertyNode;
247 readProperties(path.str(), props);
248 } catch (const sg_exception &) {
249 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << path);
253 it = _dialog_props.insert(it, std::make_pair(name, props));
260 NewGUI::getDialog (const string &name)
262 if(_active_dialogs.find(name) != _active_dialogs.end())
263 return _active_dialogs[name];
265 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
270 NewGUI::setActiveDialog (FGDialog * dialog)
272 _active_dialog = dialog;
276 NewGUI::getActiveDialog ()
278 return _active_dialog;
282 NewGUI::getMenuBar ()
284 return _menubar.get();
288 NewGUI::getMenuBarVisible () const
290 return _menubar->isVisible();
294 NewGUI::setMenuBarVisible (bool visible)
303 NewGUI::newDialog (SGPropertyNode* props)
305 const char* cname = props->getStringValue("name");
307 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
312 if(_active_dialogs.find(name) == _active_dialogs.end()) {
313 _dialog_props[name] = props;
314 // add a dummy path entry, so we believe the dialog exists
315 _dialog_names[name] = SGPath();
320 NewGUI::readDir (const SGPath& path)
322 simgear::Dir dir(path);
325 SG_LOG(SG_INPUT, SG_INFO, "directory does not exist: " << path.str());
329 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
330 flightgear::NavDataCache::Transaction txn(cache);
331 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
333 BOOST_FOREACH(SGPath xmlPath, xmls) {
334 if (!cache->isCachedFileModified(xmlPath)) {
336 string name = cache->readStringProperty(xmlPath.str());
337 _dialog_names[name] = xmlPath;
341 // we need to parse the actual XML
342 SGPropertyNode_ptr props = new SGPropertyNode;
344 readProperties(xmlPath.str(), props);
345 } catch (const sg_exception &) {
346 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << xmlPath);
350 SGPropertyNode *nameprop = props->getNode("name");
352 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmlPath << " has no name; skipping.");
356 string name = nameprop->getStringValue();
357 _dialog_names[name] = xmlPath;
358 // update cached values
359 if (!cache->isReadOnly()) {
360 cache->stampCacheFile(xmlPath);
361 cache->writeStringProperty(xmlPath.str(), name);
363 } // of directory children iteration
367 ////////////////////////////////////////////////////////////////////////
369 ////////////////////////////////////////////////////////////////////////
372 NewGUI::setStyle (void)
375 for (it = _colors.begin(); it != _colors.end(); ++it)
379 // set up the traditional colors as default
380 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
381 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
382 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
383 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
384 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
385 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
386 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
388 //puSetDefaultStyle();
390 int which = fgGetInt("/sim/gui/current-style", 0);
391 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
392 SGPropertyNode *n = sim->getChild("style", which);
394 n = sim->getChild("style", 0, true);
396 setupFont(n->getNode("fonts/gui", true));
397 n = n->getNode("colors", true);
399 for (int i = 0; i < n->nChildren(); i++) {
400 SGPropertyNode *child = n->getChild(i);
401 _colors[child->getName()] = new FGColor(child);
404 FGColor *c = _colors["background"];
405 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
410 NewGUI::setupFont (SGPropertyNode *node)
412 _font = globals->get_fontcache()->get(node);
413 puSetDefaultFonts(*_font, *_font);
417 // end of new_gui.cxx