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");
71 const std::string aircraft_dir(fgGetString("/sim/aircraft-dir"));
72 readDir( SGPath(aircraft_dir, "gui/dialogs") );
80 fgSetBool("/sim/signals/reinit-gui", true);
90 NewGUI::reset (bool reload)
92 map<string,FGDialog *>::iterator iter;
93 std::vector<string> dlg;
94 // close all open dialogs and remember them ...
95 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
96 dlg.push_back(iter->first);
99 for (i = 0; i < dlg.size(); i++)
106 _menubar.reset(new FGPUIMenuBar);
110 _dialog_props.clear();
118 // open dialogs again
119 for (i = 0; i < dlg.size(); i++)
126 fgTie("/sim/menubar/visibility", this,
127 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
133 fgUntie("/sim/menubar/visibility");
137 NewGUI::update (double delta_time_sec)
139 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
140 for(/**/; iter != _active_dialogs.end(); iter++)
141 iter->second->update();
145 NewGUI::showDialog (const string &name)
147 if (_dialog_props.find(name) == _dialog_props.end()) {
148 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
151 if(!_active_dialogs[name])
152 _active_dialogs[name] = new FGPUIDialog(_dialog_props[name]);
158 NewGUI::closeActiveDialog ()
160 if (_active_dialog == 0)
163 // Kill any entries in _active_dialogs... Is there an STL
164 // algorithm to do (delete map entries by value, not key)? I hate
166 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
167 for(/**/; iter != _active_dialogs.end(); iter++) {
168 if(iter->second == _active_dialog) {
169 _active_dialogs.erase(iter);
170 // iter is no longer valid
175 delete _active_dialog;
181 NewGUI::closeDialog (const string& name)
183 if(_active_dialogs.find(name) != _active_dialogs.end()) {
184 if(_active_dialog == _active_dialogs[name])
186 delete _active_dialogs[name];
187 _active_dialogs.erase(name);
190 return false; // dialog wasn't open...
194 NewGUI::getDialogProperties (const string &name)
196 if(_dialog_props.find(name) != _dialog_props.end())
197 return _dialog_props[name];
199 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
204 NewGUI::getDialog (const string &name)
206 if(_active_dialogs.find(name) != _active_dialogs.end())
207 return _active_dialogs[name];
209 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
214 NewGUI::setActiveDialog (FGDialog * dialog)
216 _active_dialog = dialog;
220 NewGUI::getActiveDialog ()
222 return _active_dialog;
226 NewGUI::getMenuBar ()
228 return _menubar.get();
232 NewGUI::getMenuBarVisible () const
234 return _menubar->isVisible();
238 NewGUI::setMenuBarVisible (bool visible)
247 NewGUI::newDialog (SGPropertyNode* props)
249 const char* cname = props->getStringValue("name");
251 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
255 if(_active_dialogs.find(name) == _active_dialogs.end())
256 _dialog_props[name] = props;
260 NewGUI::readDir (const SGPath& path)
262 simgear::Dir dir(path);
263 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
265 for (unsigned int i=0; i<xmls.size(); ++i) {
266 SGPropertyNode * props = new SGPropertyNode;
268 readProperties(xmls[i].str(), props);
269 } catch (const sg_exception &) {
270 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
275 SGPropertyNode *nameprop = props->getNode("name");
277 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmls[i].str()
278 << " has no name; skipping.");
282 string name = nameprop->getStringValue();
283 _dialog_props[name] = props;
289 ////////////////////////////////////////////////////////////////////////
291 ////////////////////////////////////////////////////////////////////////
294 NewGUI::setStyle (void)
297 for (it = _colors.begin(); it != _colors.end(); ++it)
301 // set up the traditional colors as default
302 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
303 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
304 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
305 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
306 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
307 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
308 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
310 //puSetDefaultStyle();
312 int which = fgGetInt("/sim/gui/current-style", 0);
313 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
314 SGPropertyNode *n = sim->getChild("style", which);
316 n = sim->getChild("style", 0, true);
318 setupFont(n->getNode("fonts/gui", true));
319 n = n->getNode("colors", true);
321 for (int i = 0; i < n->nChildren(); i++) {
322 SGPropertyNode *child = n->getChild(i);
323 _colors[child->getName()] = new FGColor(child);
326 FGColor *c = _colors["background"];
327 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
332 NewGUI::setupFont (SGPropertyNode *node)
334 _font = globals->get_fontcache()->get(node);
335 puSetDefaultFonts(*_font, *_font);
339 // end of new_gui.cxx