1 // new_gui.cxx: implementation of XML-configurable GUI support.
8 #include <simgear/compiler.h>
9 #include <simgear/structure/exception.hxx>
11 #include <Main/fg_props.hxx>
13 #include "menubar.hxx"
16 extern puFont FONT_HELVETICA_14;
17 extern puFont FONT_SANS_12B;
22 ////////////////////////////////////////////////////////////////////////
23 // Implementation of NewGUI.
24 ////////////////////////////////////////////////////////////////////////
29 : _fontcache(new FGFontCache),
30 _menubar(new FGMenuBar),
38 _dialog_props.clear();
47 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
48 ulMakePath(path2, path1, "dialogs");
66 NewGUI::reset (bool reload)
68 map<string,FGDialog *>::iterator iter;
70 // close all open dialogs and remember them ...
71 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
72 dlg.push_back(iter->first);
75 for (i = 0; i < dlg.size(); i++)
82 _menubar = new FGMenuBar;
85 _dialog_props.clear();
94 for (i = 0; i < dlg.size(); i++)
101 fgTie("/sim/menubar/visibility", this,
102 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
108 fgUntie("/sim/menubar/visibility");
112 NewGUI::update (double delta_time_sec)
114 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
115 for(/**/; iter != _active_dialogs.end(); iter++)
116 iter->second->update();
120 NewGUI::showDialog (const string &name)
122 if (_dialog_props.find(name) == _dialog_props.end()) {
123 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
126 if(!_active_dialogs[name])
127 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
133 NewGUI::closeActiveDialog ()
135 if (_active_dialog == 0)
138 // Kill any entries in _active_dialogs... Is there an STL
139 // algorithm to do (delete map entries by value, not key)? I hate
141 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
142 for(/**/; iter != _active_dialogs.end(); iter++) {
143 if(iter->second == _active_dialog) {
144 _active_dialogs.erase(iter);
145 // iter is no longer valid
150 delete _active_dialog;
156 NewGUI::closeDialog (const string& name)
158 if(_active_dialogs.find(name) != _active_dialogs.end()) {
159 if(_active_dialog == _active_dialogs[name])
161 delete _active_dialogs[name];
162 _active_dialogs.erase(name);
165 return false; // dialog wasn't open...
169 NewGUI::getDialog (const string &name)
171 if(_dialog_props.find(name) != _dialog_props.end())
172 return _dialog_props[name];
174 SG_LOG(SG_GENERAL, SG_ALERT, "dialog '" << name << "' missing");
179 NewGUI::setActiveDialog (FGDialog * dialog)
181 _active_dialog = dialog;
185 NewGUI::getActiveDialog ()
187 return _active_dialog;
191 NewGUI::getMenuBar ()
197 NewGUI::getMenuBarVisible () const
199 return _menubar->isVisible();
203 NewGUI::setMenuBarVisible (bool visible)
212 test_extension (const char * path, const char * ext)
214 int pathlen = strlen(path);
215 int extlen = strlen(ext);
217 for (int i = 1; i <= pathlen && i <= extlen; i++) {
218 if (path[pathlen-i] != ext[extlen-i])
225 NewGUI::newDialog (SGPropertyNode* props)
227 const char* cname = props->getStringValue("name");
229 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
233 if(_active_dialogs.find(name) == _active_dialogs.end())
234 _dialog_props[name] = props;
238 NewGUI::readDir (const char * path)
240 ulDir * dir = ulOpenDir(path);
243 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
248 for (ulDirEnt * dirEnt = ulReadDir(dir);
250 dirEnt = ulReadDir(dir)) {
254 ulMakePath(subpath, path, dirEnt->d_name);
256 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
257 SGPropertyNode * props = new SGPropertyNode;
259 readProperties(subpath, props);
260 } catch (const sg_exception &) {
261 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
266 SGPropertyNode *nameprop = props->getNode("name");
268 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
269 << " has no name; skipping.");
273 string name = nameprop->getStringValue();
274 if (_dialog_props[name])
275 delete (SGPropertyNode *)_dialog_props[name];
277 _dialog_props[name] = props;
285 ////////////////////////////////////////////////////////////////////////
287 ////////////////////////////////////////////////////////////////////////
290 NewGUI::setStyle (void)
293 for (it = _colors.begin(); it != _colors.end(); ++it)
297 // set up the traditional colors as default
298 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
299 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
300 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
301 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
302 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
303 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
304 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
306 //puSetDefaultStyle();
308 int which = fgGetInt("/sim/current-gui", 0);
309 SGPropertyNode *sim = globals->get_props()->getNode("sim");
310 SGPropertyNode *n = sim->getChild("gui", which);
312 n = sim->getChild("gui", 0, true);
314 setupFont(n->getNode("fonts/gui", true));
315 n = n->getNode("colors", true);
317 for (int i = 0; i < n->nChildren(); i++) {
318 SGPropertyNode *child = n->getChild(i);
319 _colors[child->getName()] = new FGColor(child);
322 FGColor *c = _colors["background"];
323 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
329 static const struct {
333 { "default", &FONT_HELVETICA_14 },
334 { "FIXED_8x13", &PUFONT_8_BY_13 },
335 { "FIXED_9x15", &PUFONT_9_BY_15 },
336 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
337 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
338 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
339 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
340 { "HELVETICA_14", &FONT_HELVETICA_14 },
341 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
342 { "SANS_12B", &FONT_SANS_12B },
347 NewGUI::setupFont (SGPropertyNode *node)
349 _font = _fontcache->get(node);
350 puSetDefaultFonts(*_font, *_font);
357 ////////////////////////////////////////////////////////////////////////
359 ////////////////////////////////////////////////////////////////////////
362 FGColor::merge(const SGPropertyNode *node)
368 const SGPropertyNode * n;
369 if ((n = node->getNode("red")))
370 _red = n->getFloatValue(), dirty = true;
371 if ((n = node->getNode("green")))
372 _green = n->getFloatValue(), dirty = true;
373 if ((n = node->getNode("blue")))
374 _blue = n->getFloatValue(), dirty = true;
375 if ((n = node->getNode("alpha")))
376 _alpha = n->getFloatValue(), dirty = true;
381 FGColor::merge(const FGColor *color)
384 if (color && color->_red >= 0.0)
385 _red = color->_red, dirty = true;
386 if (color && color->_green >= 0.0)
387 _green = color->_green, dirty = true;
388 if (color && color->_blue >= 0.0)
389 _blue = color->_blue, dirty = true;
390 if (color && color->_alpha >= 0.0)
391 _alpha = color->_alpha, dirty = true;
397 FGFontCache::FGFontCache()
399 char *envp = ::getenv("FG_FONTS");
403 _path.set(globals->get_fg_root());
404 _path.append("Fonts");
407 for (int i=0; guifonts[i].name; i++)
408 _fonts[guifonts[i].name] = new fnt(guifonts[i].font);
411 FGFontCache::~FGFontCache()
417 FGFontCache::get(const char *name, float size, float slant)
421 if ((it = _fonts.find(name)) != _fonts.end())
422 return it->second->pufont;
428 f->texfont = new fntTexFont;
430 if (f->texfont->load((char *)path.c_str())) {
431 f->pufont = new puFont;
432 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
438 return _fonts["default"]->pufont;
442 FGFontCache::get(SGPropertyNode *node)
444 const char *name = node->getStringValue("name", "Helvetica.txf");
445 float size = node->getFloatValue("size", 15.0);
446 float slant = node->getFloatValue("slant", 0.0);
448 return get(name, size, slant);
451 // end of new_gui.cxx