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 DialogDict::iterator it = _active_dialogs.begin();
92 for (; it != _active_dialogs.end(); ++it) {
95 _active_dialogs.clear();
97 fgUntie("/sim/menubar/visibility");
99 _dialog_props.clear();
106 fgSetBool("/sim/signals/reinit-gui", true);
116 NewGUI::createMenuBarImplementation()
119 if (fgGetBool("/sim/menubar/native", true)) {
120 _menubar.reset(new FGCocoaMenuBar);
123 #if defined(SG_WINDOWS)
124 if (fgGetBool("/sim/menubar/native", true)) {
125 // Windows-native menubar disabled for the moment, fall-through
127 // _menubar.reset(new FGWindowsMenuBar);
130 if (!_menubar.get()) {
131 _menubar.reset(new FGPUIMenuBar);
136 NewGUI::reset (bool reload)
138 DialogDict::iterator iter;
139 string_list openDialogs;
140 // close all open dialogs and remember them ...
141 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
142 openDialogs.push_back(iter->first);
144 BOOST_FOREACH(string d, openDialogs)
152 _dialog_props.clear();
153 _dialog_names.clear();
156 createMenuBarImplementation();
162 // open dialogs again
163 BOOST_FOREACH(string d, openDialogs)
178 NewGUI::update (double delta_time_sec)
180 SG_UNUSED(delta_time_sec);
181 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
182 for(/**/; iter != _active_dialogs.end(); iter++)
183 iter->second->update();
187 NewGUI::showDialog (const string &name)
189 // first, check if it's already shown
190 if (_active_dialogs.find(name) != _active_dialogs.end())
193 // check we know about the dialog by name
194 if (_dialog_names.find(name) == _dialog_names.end()) {
195 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
199 _active_dialogs[name] = new FGPUIDialog(getDialogProperties(name));
204 NewGUI::closeActiveDialog ()
206 if (_active_dialog == 0)
209 // Kill any entries in _active_dialogs... Is there an STL
210 // algorithm to do (delete map entries by value, not key)? I hate
212 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
213 for(/**/; iter != _active_dialogs.end(); iter++) {
214 if(iter->second == _active_dialog) {
215 _active_dialogs.erase(iter);
216 // iter is no longer valid
221 delete _active_dialog;
227 NewGUI::closeDialog (const string& name)
229 if(_active_dialogs.find(name) != _active_dialogs.end()) {
230 if(_active_dialog == _active_dialogs[name])
232 delete _active_dialogs[name];
233 _active_dialogs.erase(name);
236 return false; // dialog wasn't open...
240 NewGUI::getDialogProperties (const string &name)
242 if (_dialog_names.find(name) == _dialog_names.end()) {
243 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
247 NameDialogDict::iterator it = _dialog_props.find(name);
248 if (it == _dialog_props.end()) {
250 SGPath path = _dialog_names[name];
251 SGPropertyNode_ptr props = new SGPropertyNode;
253 readProperties(path.str(), props);
254 } catch (const sg_exception &) {
255 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << path);
259 it = _dialog_props.insert(it, std::make_pair(name, props));
266 NewGUI::getDialog (const string &name)
268 if(_active_dialogs.find(name) != _active_dialogs.end())
269 return _active_dialogs[name];
271 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
276 NewGUI::setActiveDialog (FGDialog * dialog)
278 _active_dialog = dialog;
282 NewGUI::getActiveDialog ()
284 return _active_dialog;
288 NewGUI::getMenuBar ()
290 return _menubar.get();
294 NewGUI::getMenuBarVisible () const
296 return _menubar->isVisible();
300 NewGUI::setMenuBarVisible (bool visible)
309 NewGUI::newDialog (SGPropertyNode* props)
311 const char* cname = props->getStringValue("name");
313 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
318 if(_active_dialogs.find(name) == _active_dialogs.end()) {
319 _dialog_props[name] = props;
320 // add a dummy path entry, so we believe the dialog exists
321 _dialog_names[name] = SGPath();
326 NewGUI::readDir (const SGPath& path)
328 simgear::Dir dir(path);
331 SG_LOG(SG_INPUT, SG_INFO, "directory does not exist: " << path.str());
335 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
336 flightgear::NavDataCache::Transaction txn(cache);
337 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
339 BOOST_FOREACH(SGPath xmlPath, xmls) {
340 if (!cache->isCachedFileModified(xmlPath)) {
342 string name = cache->readStringProperty(xmlPath.str());
343 _dialog_names[name] = xmlPath;
347 // we need to parse the actual XML
348 SGPropertyNode_ptr props = new SGPropertyNode;
350 readProperties(xmlPath.str(), props);
351 } catch (const sg_exception &) {
352 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << xmlPath);
356 SGPropertyNode *nameprop = props->getNode("name");
358 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmlPath << " has no name; skipping.");
362 string name = nameprop->getStringValue();
363 _dialog_names[name] = xmlPath;
364 // update cached values
365 if (!cache->isReadOnly()) {
366 cache->stampCacheFile(xmlPath);
367 cache->writeStringProperty(xmlPath.str(), name);
369 } // of directory children iteration
373 ////////////////////////////////////////////////////////////////////////
375 ////////////////////////////////////////////////////////////////////////
378 NewGUI::setStyle (void)
381 for (it = _colors.begin(); it != _colors.end(); ++it)
385 // set up the traditional colors as default
386 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
387 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
388 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
389 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
390 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
391 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
392 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
394 //puSetDefaultStyle();
396 int which = fgGetInt("/sim/gui/current-style", 0);
397 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
398 SGPropertyNode *n = sim->getChild("style", which);
400 n = sim->getChild("style", 0, true);
402 setupFont(n->getNode("fonts/gui", true));
403 n = n->getNode("colors", true);
405 for (int i = 0; i < n->nChildren(); i++) {
406 SGPropertyNode *child = n->getChild(i);
407 _colors[child->getName()] = new FGColor(child);
410 FGColor *c = _colors["background"];
411 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
416 NewGUI::setupFont (SGPropertyNode *node)
418 _font = globals->get_fontcache()->get(node);
419 puSetDefaultFonts(*_font, *_font);
423 // end of new_gui.cxx