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>
23 #include <Main/fg_props.hxx>
25 #if defined(SG_UNIX) && !defined(SG_MAC)
29 #include "menubar.hxx"
31 #include "FGFontCache.hxx"
32 #include "FGColor.hxx"
37 ////////////////////////////////////////////////////////////////////////
38 // Implementation of NewGUI.
39 ////////////////////////////////////////////////////////////////////////
44 : _menubar(new FGMenuBar),
52 _dialog_props.clear();
53 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
61 SGPath p(globals->get_fg_root(), "gui/dialogs");
70 fgSetBool("/sim/signals/reinit-gui", true);
80 NewGUI::reset (bool reload)
82 map<string,FGDialog *>::iterator iter;
84 // close all open dialogs and remember them ...
85 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
86 dlg.push_back(iter->first);
89 for (i = 0; i < dlg.size(); i++)
96 _menubar = new FGMenuBar;
99 _dialog_props.clear();
107 // open dialogs again
108 for (i = 0; i < dlg.size(); i++)
115 fgTie("/sim/menubar/visibility", this,
116 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
122 fgUntie("/sim/menubar/visibility");
126 NewGUI::update (double delta_time_sec)
128 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
129 for(/**/; iter != _active_dialogs.end(); iter++)
130 iter->second->update();
134 NewGUI::showDialog (const string &name)
136 if (_dialog_props.find(name) == _dialog_props.end()) {
137 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
140 if(!_active_dialogs[name])
141 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
147 NewGUI::closeActiveDialog ()
149 if (_active_dialog == 0)
152 // Kill any entries in _active_dialogs... Is there an STL
153 // algorithm to do (delete map entries by value, not key)? I hate
155 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
156 for(/**/; iter != _active_dialogs.end(); iter++) {
157 if(iter->second == _active_dialog) {
158 _active_dialogs.erase(iter);
159 // iter is no longer valid
164 delete _active_dialog;
170 NewGUI::closeDialog (const string& name)
172 if(_active_dialogs.find(name) != _active_dialogs.end()) {
173 if(_active_dialog == _active_dialogs[name])
175 delete _active_dialogs[name];
176 _active_dialogs.erase(name);
179 return false; // dialog wasn't open...
183 NewGUI::getDialogProperties (const string &name)
185 if(_dialog_props.find(name) != _dialog_props.end())
186 return _dialog_props[name];
188 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
193 NewGUI::getDialog (const string &name)
195 if(_active_dialogs.find(name) != _active_dialogs.end())
196 return _active_dialogs[name];
198 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
203 NewGUI::setActiveDialog (FGDialog * dialog)
205 _active_dialog = dialog;
209 NewGUI::getActiveDialog ()
211 return _active_dialog;
215 NewGUI::getMenuBar ()
221 NewGUI::getMenuBarVisible () const
223 return _menubar->isVisible();
227 NewGUI::setMenuBarVisible (bool visible)
236 NewGUI::newDialog (SGPropertyNode* props)
238 const char* cname = props->getStringValue("name");
240 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
244 if(_active_dialogs.find(name) == _active_dialogs.end())
245 _dialog_props[name] = props;
249 NewGUI::readDir (const SGPath& path)
251 simgear::Dir dir(path);
252 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
254 for (unsigned int i=0; i<xmls.size(); ++i) {
255 SGPropertyNode * props = new SGPropertyNode;
257 readProperties(xmls[i].str(), props);
258 } catch (const sg_exception &) {
259 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
264 SGPropertyNode *nameprop = props->getNode("name");
266 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmls[i].str()
267 << " has no name; skipping.");
271 string name = nameprop->getStringValue();
272 _dialog_props[name] = props;
278 ////////////////////////////////////////////////////////////////////////
280 ////////////////////////////////////////////////////////////////////////
283 NewGUI::setStyle (void)
286 for (it = _colors.begin(); it != _colors.end(); ++it)
290 // set up the traditional colors as default
291 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
292 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
293 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
294 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
295 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
296 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
297 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
299 //puSetDefaultStyle();
301 int which = fgGetInt("/sim/gui/current-style", 0);
302 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
303 SGPropertyNode *n = sim->getChild("style", which);
305 n = sim->getChild("style", 0, true);
307 setupFont(n->getNode("fonts/gui", true));
308 n = n->getNode("colors", true);
310 for (int i = 0; i < n->nChildren(); i++) {
311 SGPropertyNode *child = n->getChild(i);
312 _colors[child->getName()] = new FGColor(child);
315 FGColor *c = _colors["background"];
316 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
321 NewGUI::setupFont (SGPropertyNode *node)
323 _font = globals->get_fontcache()->get(node);
324 puSetDefaultFonts(*_font, *_font);
328 // end of new_gui.cxx