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") );
80 // Fix for http://code.google.com/p/flightgear-bugs/issues/detail?id=947
81 fgGetNode("sim/menubar")->setAttribute(SGPropertyNode::PRESERVE, true);
89 fgSetBool("/sim/signals/reinit-gui", true);
99 NewGUI::reset (bool reload)
101 map<string,FGDialog *>::iterator iter;
102 string_list openDialogs;
103 // close all open dialogs and remember them ...
104 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
105 openDialogs.push_back(iter->first);
107 BOOST_FOREACH(string d, openDialogs)
114 _menubar.reset(new FGPUIMenuBar);
118 _dialog_props.clear();
119 _dialog_names.clear();
127 // open dialogs again
128 BOOST_FOREACH(string d, openDialogs)
135 fgTie("/sim/menubar/visibility", this,
136 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
142 fgUntie("/sim/menubar/visibility");
146 NewGUI::update (double delta_time_sec)
148 SG_UNUSED(delta_time_sec);
149 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
150 for(/**/; iter != _active_dialogs.end(); iter++)
151 iter->second->update();
155 NewGUI::showDialog (const string &name)
157 // first, check if it's already shown
158 if (_active_dialogs.find(name) != _active_dialogs.end())
161 // check we know about the dialog by name
162 if (_dialog_names.find(name) == _dialog_names.end()) {
163 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
167 _active_dialogs[name] = new FGPUIDialog(getDialogProperties(name));
172 NewGUI::closeActiveDialog ()
174 if (_active_dialog == 0)
177 // Kill any entries in _active_dialogs... Is there an STL
178 // algorithm to do (delete map entries by value, not key)? I hate
180 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
181 for(/**/; iter != _active_dialogs.end(); iter++) {
182 if(iter->second == _active_dialog) {
183 _active_dialogs.erase(iter);
184 // iter is no longer valid
189 delete _active_dialog;
195 NewGUI::closeDialog (const string& name)
197 if(_active_dialogs.find(name) != _active_dialogs.end()) {
198 if(_active_dialog == _active_dialogs[name])
200 delete _active_dialogs[name];
201 _active_dialogs.erase(name);
204 return false; // dialog wasn't open...
208 NewGUI::getDialogProperties (const string &name)
210 if (_dialog_names.find(name) == _dialog_names.end()) {
211 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
215 NameDialogDict::iterator it = _dialog_props.find(name);
216 if (it == _dialog_props.end()) {
218 SGPath path = _dialog_names[name];
219 SGPropertyNode_ptr props = new SGPropertyNode;
221 readProperties(path.str(), props);
222 } catch (const sg_exception &) {
223 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << path);
227 it = _dialog_props.insert(it, std::make_pair(name, props));
234 NewGUI::getDialog (const string &name)
236 if(_active_dialogs.find(name) != _active_dialogs.end())
237 return _active_dialogs[name];
239 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
244 NewGUI::setActiveDialog (FGDialog * dialog)
246 _active_dialog = dialog;
250 NewGUI::getActiveDialog ()
252 return _active_dialog;
256 NewGUI::getMenuBar ()
258 return _menubar.get();
262 NewGUI::getMenuBarVisible () const
264 return _menubar->isVisible();
268 NewGUI::setMenuBarVisible (bool visible)
277 NewGUI::newDialog (SGPropertyNode* props)
279 const char* cname = props->getStringValue("name");
281 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
286 if(_active_dialogs.find(name) == _active_dialogs.end()) {
287 _dialog_props[name] = props;
288 // add a dummy path entry, so we believe the dialog exists
289 _dialog_names[name] = SGPath();
294 NewGUI::readDir (const SGPath& path)
296 simgear::Dir dir(path);
299 SG_LOG(SG_INPUT, SG_INFO, "directory does not exist: " << path.str());
303 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
304 flightgear::NavDataCache::Transaction txn(cache);
305 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
307 BOOST_FOREACH(SGPath xmlPath, xmls) {
308 if (!cache->isCachedFileModified(xmlPath)) {
310 string name = cache->readStringProperty(xmlPath.str());
311 _dialog_names[name] = xmlPath;
315 // we need to parse the actual XML
316 SGPropertyNode_ptr props = new SGPropertyNode;
318 readProperties(xmlPath.str(), props);
319 } catch (const sg_exception &) {
320 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << xmlPath);
324 SGPropertyNode *nameprop = props->getNode("name");
326 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmlPath << " has no name; skipping.");
330 string name = nameprop->getStringValue();
331 _dialog_names[name] = xmlPath;
332 // update cached values
333 cache->stampCacheFile(xmlPath);
334 cache->writeStringProperty(xmlPath.str(), name);
335 } // of directory children iteration
339 ////////////////////////////////////////////////////////////////////////
341 ////////////////////////////////////////////////////////////////////////
344 NewGUI::setStyle (void)
347 for (it = _colors.begin(); it != _colors.end(); ++it)
351 // set up the traditional colors as default
352 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
353 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
354 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
355 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
356 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
357 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
358 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
360 //puSetDefaultStyle();
362 int which = fgGetInt("/sim/gui/current-style", 0);
363 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
364 SGPropertyNode *n = sim->getChild("style", which);
366 n = sim->getChild("style", 0, true);
368 setupFont(n->getNode("fonts/gui", true));
369 n = n->getNode("colors", true);
371 for (int i = 0; i < n->nChildren(); i++) {
372 SGPropertyNode *child = n->getChild(i);
373 _colors[child->getName()] = new FGColor(child);
376 FGColor *c = _colors["background"];
377 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
382 NewGUI::setupFont (SGPropertyNode *node)
384 _font = globals->get_fontcache()->get(node);
385 puSetDefaultFonts(*_font, *_font);
389 // end of new_gui.cxx