1 // new_gui.cxx: implementation of XML-configurable GUI support.
13 #include <simgear/compiler.h>
14 #include <simgear/structure/exception.hxx>
15 #include <simgear/props/props_io.hxx>
17 #include <boost/algorithm/string/case_conv.hpp>
19 #include <Main/fg_props.hxx>
21 #include "menubar.hxx"
24 extern puFont FONT_HELVETICA_14;
25 extern puFont FONT_SANS_12B;
30 ////////////////////////////////////////////////////////////////////////
31 // Implementation of NewGUI.
32 ////////////////////////////////////////////////////////////////////////
37 : _menubar(new FGMenuBar),
45 _dialog_props.clear();
46 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
56 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
57 ulMakePath(path2, path1, "dialogs");
66 fgSetBool("/sim/signals/reinit-gui", true);
76 NewGUI::reset (bool reload)
78 map<string,FGDialog *>::iterator iter;
80 // close all open dialogs and remember them ...
81 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
82 dlg.push_back(iter->first);
85 for (i = 0; i < dlg.size(); i++)
92 _menubar = new FGMenuBar;
95 _dialog_props.clear();
103 // open dialogs again
104 for (i = 0; i < dlg.size(); i++)
111 fgTie("/sim/menubar/visibility", this,
112 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
118 fgUntie("/sim/menubar/visibility");
122 NewGUI::update (double delta_time_sec)
124 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
125 for(/**/; iter != _active_dialogs.end(); iter++)
126 iter->second->update();
130 NewGUI::showDialog (const string &name)
132 if (_dialog_props.find(name) == _dialog_props.end()) {
133 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
136 if(!_active_dialogs[name])
137 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
143 NewGUI::closeActiveDialog ()
145 if (_active_dialog == 0)
148 // Kill any entries in _active_dialogs... Is there an STL
149 // algorithm to do (delete map entries by value, not key)? I hate
151 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
152 for(/**/; iter != _active_dialogs.end(); iter++) {
153 if(iter->second == _active_dialog) {
154 _active_dialogs.erase(iter);
155 // iter is no longer valid
160 delete _active_dialog;
166 NewGUI::closeDialog (const string& name)
168 if(_active_dialogs.find(name) != _active_dialogs.end()) {
169 if(_active_dialog == _active_dialogs[name])
171 delete _active_dialogs[name];
172 _active_dialogs.erase(name);
175 return false; // dialog wasn't open...
179 NewGUI::getDialogProperties (const string &name)
181 if(_dialog_props.find(name) != _dialog_props.end())
182 return _dialog_props[name];
184 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
189 NewGUI::getDialog (const string &name)
191 if(_active_dialogs.find(name) != _active_dialogs.end())
192 return _active_dialogs[name];
194 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
199 NewGUI::setActiveDialog (FGDialog * dialog)
201 _active_dialog = dialog;
205 NewGUI::getActiveDialog ()
207 return _active_dialog;
211 NewGUI::getMenuBar ()
217 NewGUI::getMenuBarVisible () const
219 return _menubar->isVisible();
223 NewGUI::setMenuBarVisible (bool visible)
232 test_extension (const char * path, const char * ext)
234 int pathlen = strlen(path);
235 int extlen = strlen(ext);
237 for (int i = 1; i <= pathlen && i <= extlen; i++) {
238 if (path[pathlen-i] != ext[extlen-i])
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 char * path)
260 ulDir * dir = ulOpenDir(path);
263 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
268 for (ulDirEnt * dirEnt = ulReadDir(dir);
270 dirEnt = ulReadDir(dir)) {
274 ulMakePath(subpath, path, dirEnt->d_name);
276 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
277 SGPropertyNode * props = new SGPropertyNode;
279 readProperties(subpath, props);
280 } catch (const sg_exception &) {
281 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
286 SGPropertyNode *nameprop = props->getNode("name");
288 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
289 << " has no name; skipping.");
293 string name = nameprop->getStringValue();
294 if (_dialog_props[name])
295 delete (SGPropertyNode *)_dialog_props[name];
297 _dialog_props[name] = props;
305 ////////////////////////////////////////////////////////////////////////
307 ////////////////////////////////////////////////////////////////////////
310 NewGUI::setStyle (void)
313 for (it = _colors.begin(); it != _colors.end(); ++it)
317 // set up the traditional colors as default
318 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
319 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
320 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
321 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
322 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
323 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
324 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
326 //puSetDefaultStyle();
328 int which = fgGetInt("/sim/gui/current-style", 0);
329 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
330 SGPropertyNode *n = sim->getChild("style", which);
332 n = sim->getChild("style", 0, true);
334 setupFont(n->getNode("fonts/gui", true));
335 n = n->getNode("colors", true);
337 for (int i = 0; i < n->nChildren(); i++) {
338 SGPropertyNode *child = n->getChild(i);
339 _colors[child->getName()] = new FGColor(child);
342 FGColor *c = _colors["background"];
343 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
348 NewGUI::setupFont (SGPropertyNode *node)
350 _font = globals->get_fontcache()->get(node);
351 puSetDefaultFonts(*_font, *_font);
358 ////////////////////////////////////////////////////////////////////////
360 ////////////////////////////////////////////////////////////////////////
363 FGColor::print() const {
364 std::cerr << "red=" << _red << ", green=" << _green
365 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
369 FGColor::merge(const SGPropertyNode *node)
375 const SGPropertyNode * n;
376 if ((n = node->getNode("red")))
377 _red = n->getFloatValue(), dirty = true;
378 if ((n = node->getNode("green")))
379 _green = n->getFloatValue(), dirty = true;
380 if ((n = node->getNode("blue")))
381 _blue = n->getFloatValue(), dirty = true;
382 if ((n = node->getNode("alpha")))
383 _alpha = n->getFloatValue(), dirty = true;
388 FGColor::merge(const FGColor *color)
391 if (color && color->_red >= 0.0)
392 _red = color->_red, dirty = true;
393 if (color && color->_green >= 0.0)
394 _green = color->_green, dirty = true;
395 if (color && color->_blue >= 0.0)
396 _blue = color->_blue, dirty = true;
397 if (color && color->_alpha >= 0.0)
398 _alpha = color->_alpha, dirty = true;
405 ////////////////////////////////////////////////////////////////////////
406 // FGFontCache class.
407 ////////////////////////////////////////////////////////////////////////
416 : public std::unary_function<const GuiFont, bool>
418 Predicate(const char* name_) : name(name_) {}
419 bool operator() (const GuiFont& f1) const
421 return std::strcmp(f1.name, name) == 0;
427 const GuiFont guifonts[] = {
428 { "default", &FONT_HELVETICA_14 },
429 { "FIXED_8x13", &PUFONT_8_BY_13 },
430 { "FIXED_9x15", &PUFONT_9_BY_15 },
431 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
432 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
433 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
434 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
435 { "HELVETICA_14", &FONT_HELVETICA_14 },
436 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
437 { "SANS_12B", &FONT_SANS_12B }
440 const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])];
443 FGFontCache::FGFontCache() :
448 FGFontCache::~FGFontCache()
450 PuFontMap::iterator it, end = _puFonts.end();
451 for (it = _puFonts.begin(); it != end; ++it)
455 inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1,
456 const FntParams& f2) const
458 int comp = f1.name.compare(f2.name);
463 if (f1.size < f2.size)
465 else if (f1.size > f2.size)
467 return f1.slant < f2.slant;
470 struct FGFontCache::fnt *
471 FGFontCache::getfnt(const char *name, float size, float slant)
473 string fontName = boost::to_lower_copy(string(name));
474 FntParams fntParams(fontName, size, slant);
475 PuFontMap::iterator i = _puFonts.find(fntParams);
476 if (i != _puFonts.end()) {
477 // found in the puFonts map, all done
481 // fntTexFont s are all preloaded into the _texFonts map
482 TexFontMap::iterator texi = _texFonts.find(fontName);
483 fntTexFont* texfont = NULL;
484 puFont* pufont = NULL;
485 if (texi != _texFonts.end()) {
486 texfont = texi->second;
488 // check the built-in PUI fonts (in guifonts array)
489 const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
490 GuiFont::Predicate(name));
491 if (guifont != guifontsEnd) {
492 pufont = guifont->font;
499 } else if (texfont) {
500 f->texfont = texfont;
501 f->pufont = new puFont;
502 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
504 f->pufont = guifonts[0].font;
506 _puFonts[fntParams] = f;
511 FGFontCache::get(const char *name, float size, float slant)
513 return getfnt(name, size, slant)->pufont;
517 FGFontCache::getTexFont(const char *name, float size, float slant)
519 return getfnt(name, size, slant)->texfont;
523 FGFontCache::get(SGPropertyNode *node)
526 return get("Helvetica.txf", 15.0, 0.0);
528 const char *name = node->getStringValue("name", "Helvetica.txf");
529 float size = node->getFloatValue("size", 15.0);
530 float slant = node->getFloatValue("slant", 0.0);
532 return get(name, size, slant);
535 void FGFontCache::init()
541 char *envp = ::getenv("FG_FONTS");
545 _path.set(globals->get_fg_root());
546 _path.append("Fonts");
552 FGFontCache::getfntpath(const char *name)
556 if (name && std::string(name) != "") {
562 path = SGPath(_path);
563 path.append("Helvetica.txf");
564 SG_LOG(SG_GENERAL, SG_WARN, "Unknown font name '" << name << "', defaulting to Helvetica");
568 bool FGFontCache::initializeFonts()
570 static string fontext("txf");
572 ulDir* fontdir = ulOpenDir(_path.c_str());
575 const ulDirEnt *dirEntry;
576 while ((dirEntry = ulReadDir(fontdir)) != 0) {
578 path.append(dirEntry->d_name);
579 if (path.extension() == fontext) {
580 fntTexFont* f = new fntTexFont;
581 if (f->load((char *)path.c_str())) {
582 // convert font names in the map to lowercase for matching
583 string fontName = boost::to_lower_copy(string(dirEntry->d_name));
584 _texFonts[fontName] = f;
593 // end of new_gui.cxx