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++)
105 _menubar.reset(new FGCocoaMenuBar);
108 _menubar.reset(new FGPUIMenuBar);
112 _dialog_props.clear();
120 // open dialogs again
121 for (i = 0; i < dlg.size(); i++)
128 fgTie("/sim/menubar/visibility", this,
129 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
135 fgUntie("/sim/menubar/visibility");
139 NewGUI::update (double delta_time_sec)
141 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
142 for(/**/; iter != _active_dialogs.end(); iter++)
143 iter->second->update();
147 NewGUI::showDialog (const string &name)
149 if (_dialog_props.find(name) == _dialog_props.end()) {
150 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
153 if(!_active_dialogs[name])
154 _active_dialogs[name] = new FGPUIDialog(_dialog_props[name]);
160 NewGUI::closeActiveDialog ()
162 if (_active_dialog == 0)
165 // Kill any entries in _active_dialogs... Is there an STL
166 // algorithm to do (delete map entries by value, not key)? I hate
168 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
169 for(/**/; iter != _active_dialogs.end(); iter++) {
170 if(iter->second == _active_dialog) {
171 _active_dialogs.erase(iter);
172 // iter is no longer valid
177 delete _active_dialog;
183 NewGUI::closeDialog (const string& name)
185 if(_active_dialogs.find(name) != _active_dialogs.end()) {
186 if(_active_dialog == _active_dialogs[name])
188 delete _active_dialogs[name];
189 _active_dialogs.erase(name);
192 return false; // dialog wasn't open...
196 NewGUI::getDialogProperties (const string &name)
198 if(_dialog_props.find(name) != _dialog_props.end())
199 return _dialog_props[name];
201 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
206 NewGUI::getDialog (const string &name)
208 if(_active_dialogs.find(name) != _active_dialogs.end())
209 return _active_dialogs[name];
211 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
216 NewGUI::setActiveDialog (FGDialog * dialog)
218 _active_dialog = dialog;
222 NewGUI::getActiveDialog ()
224 return _active_dialog;
228 NewGUI::getMenuBar ()
230 return _menubar.get();
234 NewGUI::getMenuBarVisible () const
236 return _menubar->isVisible();
240 NewGUI::setMenuBarVisible (bool visible)
249 NewGUI::newDialog (SGPropertyNode* props)
251 const char* cname = props->getStringValue("name");
253 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
257 if(_active_dialogs.find(name) == _active_dialogs.end())
258 _dialog_props[name] = props;
262 NewGUI::readDir (const SGPath& path)
264 simgear::Dir dir(path);
265 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
267 for (unsigned int i=0; i<xmls.size(); ++i) {
268 SGPropertyNode * props = new SGPropertyNode;
270 readProperties(xmls[i].str(), props);
271 } catch (const sg_exception &) {
272 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
277 SGPropertyNode *nameprop = props->getNode("name");
279 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmls[i].str()
280 << " has no name; skipping.");
284 string name = nameprop->getStringValue();
285 _dialog_props[name] = props;
291 ////////////////////////////////////////////////////////////////////////
293 ////////////////////////////////////////////////////////////////////////
296 NewGUI::setStyle (void)
299 for (it = _colors.begin(); it != _colors.end(); ++it)
303 // set up the traditional colors as default
304 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
305 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
306 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
307 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
308 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
309 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
310 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
312 //puSetDefaultStyle();
314 int which = fgGetInt("/sim/gui/current-style", 0);
315 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
316 SGPropertyNode *n = sim->getChild("style", which);
318 n = sim->getChild("style", 0, true);
320 setupFont(n->getNode("fonts/gui", true));
321 n = n->getNode("colors", true);
323 for (int i = 0; i < n->nChildren(); i++) {
324 SGPropertyNode *child = n->getChild(i);
325 _colors[child->getName()] = new FGColor(child);
328 FGColor *c = _colors["background"];
329 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
334 NewGUI::setupFont (SGPropertyNode *node)
336 _font = globals->get_fontcache()->get(node);
337 puSetDefaultFonts(*_font, *_font);
341 // end of new_gui.cxx