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 #include "FGPUIDialog.hxx"
37 #include "FGFontCache.hxx"
38 #include "FGColor.hxx"
40 // ignore the word Navaid here, it's a DataCache
41 #include <Navaids/NavDataCache.hxx>
46 ////////////////////////////////////////////////////////////////////////
47 // Implementation of NewGUI.
48 ////////////////////////////////////////////////////////////////////////
56 if (fgGetBool("/sim/menubar/native", true)) {
57 _menubar.reset(new FGCocoaMenuBar);
61 _menubar.reset(new FGPUIMenuBar);
66 _dialog_props.clear();
67 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
75 SGPath p(globals->get_fg_root(), "gui/dialogs");
77 const std::string aircraft_dir(fgGetString("/sim/aircraft-dir"));
78 readDir( SGPath(aircraft_dir, "gui/dialogs") );
86 fgSetBool("/sim/signals/reinit-gui", true);
96 NewGUI::reset (bool reload)
98 map<string,FGDialog *>::iterator iter;
99 string_list openDialogs;
100 // close all open dialogs and remember them ...
101 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
102 openDialogs.push_back(iter->first);
104 BOOST_FOREACH(string d, openDialogs)
111 _menubar.reset(new FGPUIMenuBar);
115 _dialog_props.clear();
116 _dialog_names.clear();
124 // open dialogs again
125 BOOST_FOREACH(string d, openDialogs)
132 fgTie("/sim/menubar/visibility", this,
133 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
139 fgUntie("/sim/menubar/visibility");
143 NewGUI::update (double delta_time_sec)
145 SG_UNUSED(delta_time_sec);
146 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
147 for(/**/; iter != _active_dialogs.end(); iter++)
148 iter->second->update();
152 NewGUI::showDialog (const string &name)
154 // first, check if it's already shown
155 if (_active_dialogs.find(name) != _active_dialogs.end())
158 // check we know about the dialog by name
159 if (_dialog_names.find(name) == _dialog_names.end()) {
160 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
164 _active_dialogs[name] = new FGPUIDialog(getDialogProperties(name));
169 NewGUI::closeActiveDialog ()
171 if (_active_dialog == 0)
174 // Kill any entries in _active_dialogs... Is there an STL
175 // algorithm to do (delete map entries by value, not key)? I hate
177 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
178 for(/**/; iter != _active_dialogs.end(); iter++) {
179 if(iter->second == _active_dialog) {
180 _active_dialogs.erase(iter);
181 // iter is no longer valid
186 delete _active_dialog;
192 NewGUI::closeDialog (const string& name)
194 if(_active_dialogs.find(name) != _active_dialogs.end()) {
195 if(_active_dialog == _active_dialogs[name])
197 delete _active_dialogs[name];
198 _active_dialogs.erase(name);
201 return false; // dialog wasn't open...
205 NewGUI::getDialogProperties (const string &name)
207 if (_dialog_names.find(name) == _dialog_names.end()) {
208 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
212 NameDialogDict::iterator it = _dialog_props.find(name);
213 if (it == _dialog_props.end()) {
215 SGPath path = _dialog_names[name];
216 SGPropertyNode_ptr props = new SGPropertyNode;
218 readProperties(path.str(), props);
219 } catch (const sg_exception &) {
220 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << path);
224 it = _dialog_props.insert(it, std::make_pair(name, props));
231 NewGUI::getDialog (const string &name)
233 if(_active_dialogs.find(name) != _active_dialogs.end())
234 return _active_dialogs[name];
236 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
241 NewGUI::setActiveDialog (FGDialog * dialog)
243 _active_dialog = dialog;
247 NewGUI::getActiveDialog ()
249 return _active_dialog;
253 NewGUI::getMenuBar ()
255 return _menubar.get();
259 NewGUI::getMenuBarVisible () const
261 return _menubar->isVisible();
265 NewGUI::setMenuBarVisible (bool visible)
274 NewGUI::newDialog (SGPropertyNode* props)
276 const char* cname = props->getStringValue("name");
278 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
283 if(_active_dialogs.find(name) == _active_dialogs.end()) {
284 _dialog_props[name] = props;
285 // add a dummy path entry, so we believe the dialog exists
286 _dialog_names[name] = SGPath();
291 NewGUI::readDir (const SGPath& path)
293 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
294 flightgear::NavDataCache::Transaction txn(cache);
295 simgear::Dir dir(path);
296 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
298 BOOST_FOREACH(SGPath xmlPath, xmls) {
299 if (!cache->isCachedFileModified(xmlPath)) {
301 string name = cache->readStringProperty(xmlPath.str());
302 _dialog_names[name] = xmlPath;
306 // we need to parse the actual XML
307 SGPropertyNode_ptr props = new SGPropertyNode;
309 readProperties(xmlPath.str(), props);
310 } catch (const sg_exception &) {
311 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << xmlPath);
315 SGPropertyNode *nameprop = props->getNode("name");
317 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmlPath << " has no name; skipping.");
321 string name = nameprop->getStringValue();
322 _dialog_names[name] = xmlPath;
323 // update cached values
324 cache->stampCacheFile(xmlPath);
325 cache->writeStringProperty(xmlPath.str(), name);
326 } // of directory children iteration
330 ////////////////////////////////////////////////////////////////////////
332 ////////////////////////////////////////////////////////////////////////
335 NewGUI::setStyle (void)
338 for (it = _colors.begin(); it != _colors.end(); ++it)
342 // set up the traditional colors as default
343 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
344 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
345 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
346 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
347 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
348 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
349 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
351 //puSetDefaultStyle();
353 int which = fgGetInt("/sim/gui/current-style", 0);
354 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
355 SGPropertyNode *n = sim->getChild("style", which);
357 n = sim->getChild("style", 0, true);
359 setupFont(n->getNode("fonts/gui", true));
360 n = n->getNode("colors", true);
362 for (int i = 0; i < n->nChildren(); i++) {
363 SGPropertyNode *child = n->getChild(i);
364 _colors[child->getName()] = new FGColor(child);
367 FGColor *c = _colors["background"];
368 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
373 NewGUI::setupFont (SGPropertyNode *node)
375 _font = globals->get_fontcache()->get(node);
376 puSetDefaultFonts(*_font, *_font);
380 // end of new_gui.cxx