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 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
146 for(/**/; iter != _active_dialogs.end(); iter++)
147 iter->second->update();
151 NewGUI::showDialog (const string &name)
153 // first, check if it's already shown
154 if (_active_dialogs.find(name) != _active_dialogs.end())
157 // check we know about the dialog by name
158 if (_dialog_names.find(name) == _dialog_names.end()) {
159 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
163 _active_dialogs[name] = new FGPUIDialog(getDialogProperties(name));
168 NewGUI::closeActiveDialog ()
170 if (_active_dialog == 0)
173 // Kill any entries in _active_dialogs... Is there an STL
174 // algorithm to do (delete map entries by value, not key)? I hate
176 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
177 for(/**/; iter != _active_dialogs.end(); iter++) {
178 if(iter->second == _active_dialog) {
179 _active_dialogs.erase(iter);
180 // iter is no longer valid
185 delete _active_dialog;
191 NewGUI::closeDialog (const string& name)
193 if(_active_dialogs.find(name) != _active_dialogs.end()) {
194 if(_active_dialog == _active_dialogs[name])
196 delete _active_dialogs[name];
197 _active_dialogs.erase(name);
200 return false; // dialog wasn't open...
204 NewGUI::getDialogProperties (const string &name)
206 if (_dialog_names.find(name) == _dialog_names.end()) {
207 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
211 NameDialogDict::iterator it = _dialog_props.find(name);
212 if (it == _dialog_props.end()) {
214 SGPath path = _dialog_names[name];
215 SGPropertyNode_ptr props = new SGPropertyNode;
217 readProperties(path.str(), props);
218 } catch (const sg_exception &) {
219 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << path);
223 it = _dialog_props.insert(it, std::make_pair(name, props));
230 NewGUI::getDialog (const string &name)
232 if(_active_dialogs.find(name) != _active_dialogs.end())
233 return _active_dialogs[name];
235 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
240 NewGUI::setActiveDialog (FGDialog * dialog)
242 _active_dialog = dialog;
246 NewGUI::getActiveDialog ()
248 return _active_dialog;
252 NewGUI::getMenuBar ()
254 return _menubar.get();
258 NewGUI::getMenuBarVisible () const
260 return _menubar->isVisible();
264 NewGUI::setMenuBarVisible (bool visible)
273 NewGUI::newDialog (SGPropertyNode* props)
275 const char* cname = props->getStringValue("name");
277 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
282 if(_active_dialogs.find(name) == _active_dialogs.end()) {
283 _dialog_props[name] = props;
284 // add a dummy path entry, so we believe the dialog exists
285 _dialog_names[name] = SGPath();
290 NewGUI::readDir (const SGPath& path)
292 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
293 flightgear::NavDataCache::Transaction txn(cache);
294 simgear::Dir dir(path);
295 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
297 BOOST_FOREACH(SGPath xmlPath, xmls) {
298 if (!cache->isCachedFileModified(xmlPath)) {
300 string name = cache->readStringProperty(xmlPath.str());
301 _dialog_names[name] = xmlPath;
305 // we need to parse the actual XML
306 SGPropertyNode_ptr props = new SGPropertyNode;
308 readProperties(xmlPath.str(), props);
309 } catch (const sg_exception &) {
310 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << xmlPath);
314 SGPropertyNode *nameprop = props->getNode("name");
316 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmlPath << " has no name; skipping.");
320 string name = nameprop->getStringValue();
321 _dialog_names[name] = xmlPath;
322 // update cached values
323 cache->stampCacheFile(xmlPath);
324 cache->writeStringProperty(xmlPath.str(), name);
325 } // of directory children iteration
329 ////////////////////////////////////////////////////////////////////////
331 ////////////////////////////////////////////////////////////////////////
334 NewGUI::setStyle (void)
337 for (it = _colors.begin(); it != _colors.end(); ++it)
341 // set up the traditional colors as default
342 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
343 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
344 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
345 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
346 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
347 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
348 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
350 //puSetDefaultStyle();
352 int which = fgGetInt("/sim/gui/current-style", 0);
353 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
354 SGPropertyNode *n = sim->getChild("style", which);
356 n = sim->getChild("style", 0, true);
358 setupFont(n->getNode("fonts/gui", true));
359 n = n->getNode("colors", true);
361 for (int i = 0; i < n->nChildren(); i++) {
362 SGPropertyNode *child = n->getChild(i);
363 _colors[child->getName()] = new FGColor(child);
366 FGColor *c = _colors["background"];
367 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
372 NewGUI::setupFont (SGPropertyNode *node)
374 _font = globals->get_fontcache()->get(node);
375 puSetDefaultFonts(*_font, *_font);
379 // end of new_gui.cxx