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)
67 if (fgGetBool("/sim/menubar/native", true)) {
68 _menubar.reset(new FGCocoaMenuBar);
71 if (!_menubar.get()) {
72 _menubar.reset(new FGPUIMenuBar);
75 fgTie("/sim/menubar/visibility", this,
76 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
79 SGPath p(globals->get_fg_root(), "gui/dialogs");
81 const std::string aircraft_dir(fgGetString("/sim/aircraft-dir"));
82 readDir( SGPath(aircraft_dir, "gui/dialogs") );
84 // Fix for http://code.google.com/p/flightgear-bugs/issues/detail?id=947
85 fgGetNode("sim/menubar")->setAttribute(SGPropertyNode::PRESERVE, true);
92 fgUntie("/sim/menubar/visibility");
94 _dialog_props.clear();
101 fgSetBool("/sim/signals/reinit-gui", true);
111 NewGUI::reset (bool reload)
113 map<string,FGDialog *>::iterator iter;
114 string_list openDialogs;
115 // close all open dialogs and remember them ...
116 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
117 openDialogs.push_back(iter->first);
119 BOOST_FOREACH(string d, openDialogs)
126 _menubar.reset(new FGPUIMenuBar);
130 _dialog_props.clear();
131 _dialog_names.clear();
139 // open dialogs again
140 BOOST_FOREACH(string d, openDialogs)
155 NewGUI::update (double delta_time_sec)
157 SG_UNUSED(delta_time_sec);
158 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
159 for(/**/; iter != _active_dialogs.end(); iter++)
160 iter->second->update();
164 NewGUI::showDialog (const string &name)
166 // first, check if it's already shown
167 if (_active_dialogs.find(name) != _active_dialogs.end())
170 // check we know about the dialog by name
171 if (_dialog_names.find(name) == _dialog_names.end()) {
172 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
176 _active_dialogs[name] = new FGPUIDialog(getDialogProperties(name));
181 NewGUI::closeActiveDialog ()
183 if (_active_dialog == 0)
186 // Kill any entries in _active_dialogs... Is there an STL
187 // algorithm to do (delete map entries by value, not key)? I hate
189 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
190 for(/**/; iter != _active_dialogs.end(); iter++) {
191 if(iter->second == _active_dialog) {
192 _active_dialogs.erase(iter);
193 // iter is no longer valid
198 delete _active_dialog;
204 NewGUI::closeDialog (const string& name)
206 if(_active_dialogs.find(name) != _active_dialogs.end()) {
207 if(_active_dialog == _active_dialogs[name])
209 delete _active_dialogs[name];
210 _active_dialogs.erase(name);
213 return false; // dialog wasn't open...
217 NewGUI::getDialogProperties (const string &name)
219 if (_dialog_names.find(name) == _dialog_names.end()) {
220 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
224 NameDialogDict::iterator it = _dialog_props.find(name);
225 if (it == _dialog_props.end()) {
227 SGPath path = _dialog_names[name];
228 SGPropertyNode_ptr props = new SGPropertyNode;
230 readProperties(path.str(), props);
231 } catch (const sg_exception &) {
232 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << path);
236 it = _dialog_props.insert(it, std::make_pair(name, props));
243 NewGUI::getDialog (const string &name)
245 if(_active_dialogs.find(name) != _active_dialogs.end())
246 return _active_dialogs[name];
248 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
253 NewGUI::setActiveDialog (FGDialog * dialog)
255 _active_dialog = dialog;
259 NewGUI::getActiveDialog ()
261 return _active_dialog;
265 NewGUI::getMenuBar ()
267 return _menubar.get();
271 NewGUI::getMenuBarVisible () const
273 return _menubar->isVisible();
277 NewGUI::setMenuBarVisible (bool visible)
286 NewGUI::newDialog (SGPropertyNode* props)
288 const char* cname = props->getStringValue("name");
290 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
295 if(_active_dialogs.find(name) == _active_dialogs.end()) {
296 _dialog_props[name] = props;
297 // add a dummy path entry, so we believe the dialog exists
298 _dialog_names[name] = SGPath();
303 NewGUI::readDir (const SGPath& path)
305 simgear::Dir dir(path);
308 SG_LOG(SG_INPUT, SG_INFO, "directory does not exist: " << path.str());
312 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
313 flightgear::NavDataCache::Transaction txn(cache);
314 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
316 BOOST_FOREACH(SGPath xmlPath, xmls) {
317 if (!cache->isCachedFileModified(xmlPath)) {
319 string name = cache->readStringProperty(xmlPath.str());
320 _dialog_names[name] = xmlPath;
324 // we need to parse the actual XML
325 SGPropertyNode_ptr props = new SGPropertyNode;
327 readProperties(xmlPath.str(), props);
328 } catch (const sg_exception &) {
329 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << xmlPath);
333 SGPropertyNode *nameprop = props->getNode("name");
335 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmlPath << " has no name; skipping.");
339 string name = nameprop->getStringValue();
340 _dialog_names[name] = xmlPath;
341 // update cached values
342 cache->stampCacheFile(xmlPath);
343 cache->writeStringProperty(xmlPath.str(), name);
344 } // of directory children iteration
348 ////////////////////////////////////////////////////////////////////////
350 ////////////////////////////////////////////////////////////////////////
353 NewGUI::setStyle (void)
356 for (it = _colors.begin(); it != _colors.end(); ++it)
360 // set up the traditional colors as default
361 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
362 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
363 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
364 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
365 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
366 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
367 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
369 //puSetDefaultStyle();
371 int which = fgGetInt("/sim/gui/current-style", 0);
372 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
373 SGPropertyNode *n = sim->getChild("style", which);
375 n = sim->getChild("style", 0, true);
377 setupFont(n->getNode("fonts/gui", true));
378 n = n->getNode("colors", true);
380 for (int i = 0; i < n->nChildren(); i++) {
381 SGPropertyNode *child = n->getChild(i);
382 _colors[child->getName()] = new FGColor(child);
385 FGColor *c = _colors["background"];
386 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
391 NewGUI::setupFont (SGPropertyNode *node)
393 _font = globals->get_fontcache()->get(node);
394 puSetDefaultFonts(*_font, *_font);
398 // end of new_gui.cxx