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 ////////////////////////////////////////////////////////////////////////
59 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
66 createMenuBarImplementation();
67 fgTie("/sim/menubar/visibility", this,
68 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
71 SGPath p(globals->get_fg_root(), "gui/dialogs");
74 SGPath aircraftDialogDir(string(fgGetString("/sim/aircraft-dir")), "gui/dialogs");
75 if (aircraftDialogDir.exists()) {
76 readDir(aircraftDialogDir);
79 // Fix for http://code.google.com/p/flightgear-bugs/issues/detail?id=947
80 fgGetNode("sim/menubar")->setAttribute(SGPropertyNode::PRESERVE, true);
87 fgUntie("/sim/menubar/visibility");
89 _dialog_props.clear();
96 fgSetBool("/sim/signals/reinit-gui", true);
106 NewGUI::createMenuBarImplementation()
109 if (fgGetBool("/sim/menubar/native", true)) {
110 _menubar.reset(new FGCocoaMenuBar);
113 if (!_menubar.get()) {
114 _menubar.reset(new FGPUIMenuBar);
119 NewGUI::reset (bool reload)
121 map<string,FGDialog *>::iterator iter;
122 string_list openDialogs;
123 // close all open dialogs and remember them ...
124 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
125 openDialogs.push_back(iter->first);
127 BOOST_FOREACH(string d, openDialogs)
135 _dialog_props.clear();
136 _dialog_names.clear();
139 createMenuBarImplementation();
145 // open dialogs again
146 BOOST_FOREACH(string d, openDialogs)
161 NewGUI::update (double delta_time_sec)
163 SG_UNUSED(delta_time_sec);
164 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
165 for(/**/; iter != _active_dialogs.end(); iter++)
166 iter->second->update();
170 NewGUI::showDialog (const string &name)
172 // first, check if it's already shown
173 if (_active_dialogs.find(name) != _active_dialogs.end())
176 // check we know about the dialog by name
177 if (_dialog_names.find(name) == _dialog_names.end()) {
178 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
182 _active_dialogs[name] = new FGPUIDialog(getDialogProperties(name));
187 NewGUI::closeActiveDialog ()
189 if (_active_dialog == 0)
192 // Kill any entries in _active_dialogs... Is there an STL
193 // algorithm to do (delete map entries by value, not key)? I hate
195 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
196 for(/**/; iter != _active_dialogs.end(); iter++) {
197 if(iter->second == _active_dialog) {
198 _active_dialogs.erase(iter);
199 // iter is no longer valid
204 delete _active_dialog;
210 NewGUI::closeDialog (const string& name)
212 if(_active_dialogs.find(name) != _active_dialogs.end()) {
213 if(_active_dialog == _active_dialogs[name])
215 delete _active_dialogs[name];
216 _active_dialogs.erase(name);
219 return false; // dialog wasn't open...
223 NewGUI::getDialogProperties (const string &name)
225 if (_dialog_names.find(name) == _dialog_names.end()) {
226 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
230 NameDialogDict::iterator it = _dialog_props.find(name);
231 if (it == _dialog_props.end()) {
233 SGPath path = _dialog_names[name];
234 SGPropertyNode_ptr props = new SGPropertyNode;
236 readProperties(path.str(), props);
237 } catch (const sg_exception &) {
238 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << path);
242 it = _dialog_props.insert(it, std::make_pair(name, props));
249 NewGUI::getDialog (const string &name)
251 if(_active_dialogs.find(name) != _active_dialogs.end())
252 return _active_dialogs[name];
254 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
259 NewGUI::setActiveDialog (FGDialog * dialog)
261 _active_dialog = dialog;
265 NewGUI::getActiveDialog ()
267 return _active_dialog;
271 NewGUI::getMenuBar ()
273 return _menubar.get();
277 NewGUI::getMenuBarVisible () const
279 return _menubar->isVisible();
283 NewGUI::setMenuBarVisible (bool visible)
292 NewGUI::newDialog (SGPropertyNode* props)
294 const char* cname = props->getStringValue("name");
296 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
301 if(_active_dialogs.find(name) == _active_dialogs.end()) {
302 _dialog_props[name] = props;
303 // add a dummy path entry, so we believe the dialog exists
304 _dialog_names[name] = SGPath();
309 NewGUI::readDir (const SGPath& path)
311 simgear::Dir dir(path);
314 SG_LOG(SG_INPUT, SG_INFO, "directory does not exist: " << path.str());
318 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
319 flightgear::NavDataCache::Transaction txn(cache);
320 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
322 BOOST_FOREACH(SGPath xmlPath, xmls) {
323 if (!cache->isCachedFileModified(xmlPath)) {
325 string name = cache->readStringProperty(xmlPath.str());
326 _dialog_names[name] = xmlPath;
330 // we need to parse the actual XML
331 SGPropertyNode_ptr props = new SGPropertyNode;
333 readProperties(xmlPath.str(), props);
334 } catch (const sg_exception &) {
335 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << xmlPath);
339 SGPropertyNode *nameprop = props->getNode("name");
341 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmlPath << " has no name; skipping.");
345 string name = nameprop->getStringValue();
346 _dialog_names[name] = xmlPath;
347 // update cached values
348 if (!cache->isReadOnly()) {
349 cache->stampCacheFile(xmlPath);
350 cache->writeStringProperty(xmlPath.str(), name);
352 } // of directory children iteration
356 ////////////////////////////////////////////////////////////////////////
358 ////////////////////////////////////////////////////////////////////////
361 NewGUI::setStyle (void)
364 for (it = _colors.begin(); it != _colors.end(); ++it)
368 // set up the traditional colors as default
369 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
370 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
371 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
372 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
373 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
374 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
375 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
377 //puSetDefaultStyle();
379 int which = fgGetInt("/sim/gui/current-style", 0);
380 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
381 SGPropertyNode *n = sim->getChild("style", which);
383 n = sim->getChild("style", 0, true);
385 setupFont(n->getNode("fonts/gui", true));
386 n = n->getNode("colors", true);
388 for (int i = 0; i < n->nChildren(); i++) {
389 SGPropertyNode *child = n->getChild(i);
390 _colors[child->getName()] = new FGColor(child);
393 FGColor *c = _colors["background"];
394 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
399 NewGUI::setupFont (SGPropertyNode *node)
401 _font = globals->get_fontcache()->get(node);
402 puSetDefaultFonts(*_font, *_font);
406 // end of new_gui.cxx