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 "FGPUIMenuBar.hxx"
32 #include "FGCocoaMenuBar.hxx"
35 #include "FGPUIDialog.hxx"
36 #include "FGFontCache.hxx"
37 #include "FGColor.hxx"
42 ////////////////////////////////////////////////////////////////////////
43 // Implementation of NewGUI.
44 ////////////////////////////////////////////////////////////////////////
52 _menubar.reset(new FGCocoaMenuBar);
54 _menubar.reset(new FGPUIMenuBar);
60 _dialog_props.clear();
61 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
69 SGPath p(globals->get_fg_root(), "gui/dialogs");
78 fgSetBool("/sim/signals/reinit-gui", true);
88 NewGUI::reset (bool reload)
90 map<string,FGDialog *>::iterator iter;
91 std::vector<string> dlg;
92 // close all open dialogs and remember them ...
93 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
94 dlg.push_back(iter->first);
97 for (i = 0; i < dlg.size(); i++)
104 _menubar.reset(new FGPUIMenuBar);
108 _dialog_props.clear();
116 // open dialogs again
117 for (i = 0; i < dlg.size(); i++)
124 fgTie("/sim/menubar/visibility", this,
125 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
131 fgUntie("/sim/menubar/visibility");
135 NewGUI::update (double delta_time_sec)
137 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
138 for(/**/; iter != _active_dialogs.end(); iter++)
139 iter->second->update();
143 NewGUI::showDialog (const string &name)
145 if (_dialog_props.find(name) == _dialog_props.end()) {
146 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
149 if(!_active_dialogs[name])
150 _active_dialogs[name] = new FGPUIDialog(_dialog_props[name]);
156 NewGUI::closeActiveDialog ()
158 if (_active_dialog == 0)
161 // Kill any entries in _active_dialogs... Is there an STL
162 // algorithm to do (delete map entries by value, not key)? I hate
164 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
165 for(/**/; iter != _active_dialogs.end(); iter++) {
166 if(iter->second == _active_dialog) {
167 _active_dialogs.erase(iter);
168 // iter is no longer valid
173 delete _active_dialog;
179 NewGUI::closeDialog (const string& name)
181 if(_active_dialogs.find(name) != _active_dialogs.end()) {
182 if(_active_dialog == _active_dialogs[name])
184 delete _active_dialogs[name];
185 _active_dialogs.erase(name);
188 return false; // dialog wasn't open...
192 NewGUI::getDialogProperties (const string &name)
194 if(_dialog_props.find(name) != _dialog_props.end())
195 return _dialog_props[name];
197 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
202 NewGUI::getDialog (const string &name)
204 if(_active_dialogs.find(name) != _active_dialogs.end())
205 return _active_dialogs[name];
207 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
212 NewGUI::setActiveDialog (FGDialog * dialog)
214 _active_dialog = dialog;
218 NewGUI::getActiveDialog ()
220 return _active_dialog;
224 NewGUI::getMenuBar ()
226 return _menubar.get();
230 NewGUI::getMenuBarVisible () const
232 return _menubar->isVisible();
236 NewGUI::setMenuBarVisible (bool visible)
245 NewGUI::newDialog (SGPropertyNode* props)
247 const char* cname = props->getStringValue("name");
249 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
253 if(_active_dialogs.find(name) == _active_dialogs.end())
254 _dialog_props[name] = props;
258 NewGUI::readDir (const SGPath& path)
260 simgear::Dir dir(path);
261 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
263 for (unsigned int i=0; i<xmls.size(); ++i) {
264 SGPropertyNode * props = new SGPropertyNode;
266 readProperties(xmls[i].str(), props);
267 } catch (const sg_exception &) {
268 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
273 SGPropertyNode *nameprop = props->getNode("name");
275 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmls[i].str()
276 << " has no name; skipping.");
280 string name = nameprop->getStringValue();
281 _dialog_props[name] = props;
287 ////////////////////////////////////////////////////////////////////////
289 ////////////////////////////////////////////////////////////////////////
292 NewGUI::setStyle (void)
295 for (it = _colors.begin(); it != _colors.end(); ++it)
299 // set up the traditional colors as default
300 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
301 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
302 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
303 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
304 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
305 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
306 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
308 //puSetDefaultStyle();
310 int which = fgGetInt("/sim/gui/current-style", 0);
311 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
312 SGPropertyNode *n = sim->getChild("style", which);
314 n = sim->getChild("style", 0, true);
316 setupFont(n->getNode("fonts/gui", true));
317 n = n->getNode("colors", true);
319 for (int i = 0; i < n->nChildren(); i++) {
320 SGPropertyNode *child = n->getChild(i);
321 _colors[child->getName()] = new FGColor(child);
324 FGColor *c = _colors["background"];
325 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
330 NewGUI::setupFont (SGPropertyNode *node)
332 _font = globals->get_fontcache()->get(node);
333 puSetDefaultFonts(*_font, *_font);
337 // end of new_gui.cxx