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 #include "menubar.hxx"
28 extern puFont FONT_HELVETICA_14;
29 extern puFont FONT_SANS_12B;
34 ////////////////////////////////////////////////////////////////////////
35 // Implementation of NewGUI.
36 ////////////////////////////////////////////////////////////////////////
41 : _menubar(new FGMenuBar),
49 _dialog_props.clear();
50 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
58 SGPath p(globals->get_fg_root(), "gui/dialogs");
67 fgSetBool("/sim/signals/reinit-gui", true);
77 NewGUI::reset (bool reload)
79 map<string,FGDialog *>::iterator iter;
81 // close all open dialogs and remember them ...
82 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
83 dlg.push_back(iter->first);
86 for (i = 0; i < dlg.size(); i++)
93 _menubar = new FGMenuBar;
96 _dialog_props.clear();
104 // open dialogs again
105 for (i = 0; i < dlg.size(); i++)
112 fgTie("/sim/menubar/visibility", this,
113 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
119 fgUntie("/sim/menubar/visibility");
123 NewGUI::update (double delta_time_sec)
125 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
126 for(/**/; iter != _active_dialogs.end(); iter++)
127 iter->second->update();
131 NewGUI::showDialog (const string &name)
133 if (_dialog_props.find(name) == _dialog_props.end()) {
134 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
137 if(!_active_dialogs[name])
138 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
144 NewGUI::closeActiveDialog ()
146 if (_active_dialog == 0)
149 // Kill any entries in _active_dialogs... Is there an STL
150 // algorithm to do (delete map entries by value, not key)? I hate
152 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
153 for(/**/; iter != _active_dialogs.end(); iter++) {
154 if(iter->second == _active_dialog) {
155 _active_dialogs.erase(iter);
156 // iter is no longer valid
161 delete _active_dialog;
167 NewGUI::closeDialog (const string& name)
169 if(_active_dialogs.find(name) != _active_dialogs.end()) {
170 if(_active_dialog == _active_dialogs[name])
172 delete _active_dialogs[name];
173 _active_dialogs.erase(name);
176 return false; // dialog wasn't open...
180 NewGUI::getDialogProperties (const string &name)
182 if(_dialog_props.find(name) != _dialog_props.end())
183 return _dialog_props[name];
185 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
190 NewGUI::getDialog (const string &name)
192 if(_active_dialogs.find(name) != _active_dialogs.end())
193 return _active_dialogs[name];
195 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
200 NewGUI::setActiveDialog (FGDialog * dialog)
202 _active_dialog = dialog;
206 NewGUI::getActiveDialog ()
208 return _active_dialog;
212 NewGUI::getMenuBar ()
218 NewGUI::getMenuBarVisible () const
220 return _menubar->isVisible();
224 NewGUI::setMenuBarVisible (bool visible)
233 NewGUI::newDialog (SGPropertyNode* props)
235 const char* cname = props->getStringValue("name");
237 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
241 if(_active_dialogs.find(name) == _active_dialogs.end())
242 _dialog_props[name] = props;
246 NewGUI::readDir (const SGPath& path)
248 simgear::Dir dir(path);
249 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
251 for (unsigned int i=0; i<xmls.size(); ++i) {
252 SGPropertyNode * props = new SGPropertyNode;
254 readProperties(xmls[i].str(), props);
255 } catch (const sg_exception &) {
256 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
261 SGPropertyNode *nameprop = props->getNode("name");
263 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmls[i].str()
264 << " has no name; skipping.");
268 string name = nameprop->getStringValue();
269 _dialog_props[name] = props;
275 ////////////////////////////////////////////////////////////////////////
277 ////////////////////////////////////////////////////////////////////////
280 NewGUI::setStyle (void)
283 for (it = _colors.begin(); it != _colors.end(); ++it)
287 // set up the traditional colors as default
288 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
289 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
290 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
291 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
292 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
293 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
294 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
296 //puSetDefaultStyle();
298 int which = fgGetInt("/sim/gui/current-style", 0);
299 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
300 SGPropertyNode *n = sim->getChild("style", which);
302 n = sim->getChild("style", 0, true);
304 setupFont(n->getNode("fonts/gui", true));
305 n = n->getNode("colors", true);
307 for (int i = 0; i < n->nChildren(); i++) {
308 SGPropertyNode *child = n->getChild(i);
309 _colors[child->getName()] = new FGColor(child);
312 FGColor *c = _colors["background"];
313 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
318 NewGUI::setupFont (SGPropertyNode *node)
320 _font = globals->get_fontcache()->get(node);
321 puSetDefaultFonts(*_font, *_font);
328 ////////////////////////////////////////////////////////////////////////
330 ////////////////////////////////////////////////////////////////////////
333 FGColor::print() const {
334 std::cerr << "red=" << _red << ", green=" << _green
335 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
339 FGColor::merge(const SGPropertyNode *node)
345 const SGPropertyNode * n;
346 if ((n = node->getNode("red")))
347 _red = n->getFloatValue(), dirty = true;
348 if ((n = node->getNode("green")))
349 _green = n->getFloatValue(), dirty = true;
350 if ((n = node->getNode("blue")))
351 _blue = n->getFloatValue(), dirty = true;
352 if ((n = node->getNode("alpha")))
353 _alpha = n->getFloatValue(), dirty = true;
358 FGColor::merge(const FGColor *color)
361 if (color && color->_red >= 0.0)
362 _red = color->_red, dirty = true;
363 if (color && color->_green >= 0.0)
364 _green = color->_green, dirty = true;
365 if (color && color->_blue >= 0.0)
366 _blue = color->_blue, dirty = true;
367 if (color && color->_alpha >= 0.0)
368 _alpha = color->_alpha, dirty = true;
375 ////////////////////////////////////////////////////////////////////////
376 // FGFontCache class.
377 ////////////////////////////////////////////////////////////////////////
386 : public std::unary_function<const GuiFont, bool>
388 Predicate(const char* name_) : name(name_) {}
389 bool operator() (const GuiFont& f1) const
391 return std::strcmp(f1.name, name) == 0;
397 const GuiFont guifonts[] = {
398 { "default", &FONT_HELVETICA_14 },
399 { "FIXED_8x13", &PUFONT_8_BY_13 },
400 { "FIXED_9x15", &PUFONT_9_BY_15 },
401 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
402 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
403 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
404 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
405 { "HELVETICA_14", &FONT_HELVETICA_14 },
406 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
407 { "SANS_12B", &FONT_SANS_12B }
410 const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])];
413 FGFontCache::FGFontCache() :
418 FGFontCache::~FGFontCache()
420 PuFontMap::iterator it, end = _puFonts.end();
421 for (it = _puFonts.begin(); it != end; ++it)
425 inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1,
426 const FntParams& f2) const
428 int comp = f1.name.compare(f2.name);
433 if (f1.size < f2.size)
435 else if (f1.size > f2.size)
437 return f1.slant < f2.slant;
440 struct FGFontCache::fnt *
441 FGFontCache::getfnt(const char *name, float size, float slant)
443 string fontName = boost::to_lower_copy(string(name));
444 FntParams fntParams(fontName, size, slant);
445 PuFontMap::iterator i = _puFonts.find(fntParams);
446 if (i != _puFonts.end()) {
447 // found in the puFonts map, all done
451 // fntTexFont s are all preloaded into the _texFonts map
452 TexFontMap::iterator texi = _texFonts.find(fontName);
453 fntTexFont* texfont = NULL;
454 puFont* pufont = NULL;
455 if (texi != _texFonts.end()) {
456 texfont = texi->second;
458 // check the built-in PUI fonts (in guifonts array)
459 const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
460 GuiFont::Predicate(name));
461 if (guifont != guifontsEnd) {
462 pufont = guifont->font;
469 } else if (texfont) {
470 f->texfont = texfont;
471 f->pufont = new puFont;
472 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
474 f->pufont = guifonts[0].font;
476 _puFonts[fntParams] = f;
481 FGFontCache::get(const char *name, float size, float slant)
483 return getfnt(name, size, slant)->pufont;
487 FGFontCache::getTexFont(const char *name, float size, float slant)
489 return getfnt(name, size, slant)->texfont;
493 FGFontCache::get(SGPropertyNode *node)
496 return get("Helvetica.txf", 15.0, 0.0);
498 const char *name = node->getStringValue("name", "Helvetica.txf");
499 float size = node->getFloatValue("size", 15.0);
500 float slant = node->getFloatValue("slant", 0.0);
502 return get(name, size, slant);
505 void FGFontCache::init()
511 char *envp = ::getenv("FG_FONTS");
515 _path.set(globals->get_fg_root());
516 _path.append("Fonts");
522 FGFontCache::getfntpath(const char *name)
526 if (name && std::string(name) != "") {
532 path = SGPath(_path);
533 path.append("Helvetica.txf");
534 SG_LOG(SG_GENERAL, SG_WARN, "Unknown font name '" << name << "', defaulting to Helvetica");
538 bool FGFontCache::initializeFonts()
540 static string fontext("txf");
542 ulDir* fontdir = ulOpenDir(_path.c_str());
545 const ulDirEnt *dirEntry;
546 while ((dirEntry = ulReadDir(fontdir)) != 0) {
548 path.append(dirEntry->d_name);
549 if (path.extension() == fontext) {
550 fntTexFont* f = new fntTexFont;
551 if (f->load((char *)path.c_str())) {
552 // convert font names in the map to lowercase for matching
553 string fontName = boost::to_lower_copy(string(dirEntry->d_name));
554 _texFonts[fontName] = f;
563 // end of new_gui.cxx