1 // new_gui.cxx: implementation of XML-configurable GUI support.
12 #include <sys/types.h>
18 #include <simgear/compiler.h>
19 #include <simgear/structure/exception.hxx>
20 #include <simgear/props/props_io.hxx>
22 #include <boost/algorithm/string/case_conv.hpp>
24 #include <Main/fg_props.hxx>
26 #include "menubar.hxx"
29 extern puFont FONT_HELVETICA_14;
30 extern puFont FONT_SANS_12B;
35 ////////////////////////////////////////////////////////////////////////
36 // Implementation of NewGUI.
37 ////////////////////////////////////////////////////////////////////////
42 : _menubar(new FGMenuBar),
50 _dialog_props.clear();
51 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
61 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
62 ulMakePath(path2, path1, "dialogs");
71 fgSetBool("/sim/signals/reinit-gui", true);
81 NewGUI::reset (bool reload)
83 map<string,FGDialog *>::iterator iter;
85 // close all open dialogs and remember them ...
86 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
87 dlg.push_back(iter->first);
90 for (i = 0; i < dlg.size(); i++)
97 _menubar = new FGMenuBar;
100 _dialog_props.clear();
108 // open dialogs again
109 for (i = 0; i < dlg.size(); i++)
116 fgTie("/sim/menubar/visibility", this,
117 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
123 fgUntie("/sim/menubar/visibility");
127 NewGUI::update (double delta_time_sec)
129 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
130 for(/**/; iter != _active_dialogs.end(); iter++)
131 iter->second->update();
135 NewGUI::showDialog (const string &name)
137 if (_dialog_props.find(name) == _dialog_props.end()) {
138 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
141 if(!_active_dialogs[name])
142 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
148 NewGUI::closeActiveDialog ()
150 if (_active_dialog == 0)
153 // Kill any entries in _active_dialogs... Is there an STL
154 // algorithm to do (delete map entries by value, not key)? I hate
156 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
157 for(/**/; iter != _active_dialogs.end(); iter++) {
158 if(iter->second == _active_dialog) {
159 _active_dialogs.erase(iter);
160 // iter is no longer valid
165 delete _active_dialog;
171 NewGUI::closeDialog (const string& name)
173 if(_active_dialogs.find(name) != _active_dialogs.end()) {
174 if(_active_dialog == _active_dialogs[name])
176 delete _active_dialogs[name];
177 _active_dialogs.erase(name);
180 return false; // dialog wasn't open...
184 NewGUI::getDialogProperties (const string &name)
186 if(_dialog_props.find(name) != _dialog_props.end())
187 return _dialog_props[name];
189 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
194 NewGUI::getDialog (const string &name)
196 if(_active_dialogs.find(name) != _active_dialogs.end())
197 return _active_dialogs[name];
199 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
204 NewGUI::setActiveDialog (FGDialog * dialog)
206 _active_dialog = dialog;
210 NewGUI::getActiveDialog ()
212 return _active_dialog;
216 NewGUI::getMenuBar ()
222 NewGUI::getMenuBarVisible () const
224 return _menubar->isVisible();
228 NewGUI::setMenuBarVisible (bool visible)
237 test_extension (const char * path, const char * ext)
239 int pathlen = strlen(path);
240 int extlen = strlen(ext);
242 for (int i = 1; i <= pathlen && i <= extlen; i++) {
243 if (path[pathlen-i] != ext[extlen-i])
250 NewGUI::newDialog (SGPropertyNode* props)
252 const char* cname = props->getStringValue("name");
254 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
258 if(_active_dialogs.find(name) == _active_dialogs.end())
259 _dialog_props[name] = props;
263 NewGUI::readDir (const char * path)
265 ulDir * dir = ulOpenDir(path);
268 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
273 for (ulDirEnt * dirEnt = ulReadDir(dir);
275 dirEnt = ulReadDir(dir)) {
279 ulMakePath(subpath, path, dirEnt->d_name);
281 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
282 SGPropertyNode * props = new SGPropertyNode;
284 readProperties(subpath, props);
285 } catch (const sg_exception &) {
286 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
291 SGPropertyNode *nameprop = props->getNode("name");
293 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
294 << " has no name; skipping.");
298 string name = nameprop->getStringValue();
299 if (_dialog_props[name])
300 delete (SGPropertyNode *)_dialog_props[name];
302 _dialog_props[name] = props;
310 ////////////////////////////////////////////////////////////////////////
312 ////////////////////////////////////////////////////////////////////////
315 NewGUI::setStyle (void)
318 for (it = _colors.begin(); it != _colors.end(); ++it)
322 // set up the traditional colors as default
323 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
324 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
325 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
326 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
327 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
328 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
329 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
331 //puSetDefaultStyle();
333 int which = fgGetInt("/sim/gui/current-style", 0);
334 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
335 SGPropertyNode *n = sim->getChild("style", which);
337 n = sim->getChild("style", 0, true);
339 setupFont(n->getNode("fonts/gui", true));
340 n = n->getNode("colors", true);
342 for (int i = 0; i < n->nChildren(); i++) {
343 SGPropertyNode *child = n->getChild(i);
344 _colors[child->getName()] = new FGColor(child);
347 FGColor *c = _colors["background"];
348 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
353 NewGUI::setupFont (SGPropertyNode *node)
355 _font = globals->get_fontcache()->get(node);
356 puSetDefaultFonts(*_font, *_font);
363 ////////////////////////////////////////////////////////////////////////
365 ////////////////////////////////////////////////////////////////////////
368 FGColor::print() const {
369 std::cerr << "red=" << _red << ", green=" << _green
370 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
374 FGColor::merge(const SGPropertyNode *node)
380 const SGPropertyNode * n;
381 if ((n = node->getNode("red")))
382 _red = n->getFloatValue(), dirty = true;
383 if ((n = node->getNode("green")))
384 _green = n->getFloatValue(), dirty = true;
385 if ((n = node->getNode("blue")))
386 _blue = n->getFloatValue(), dirty = true;
387 if ((n = node->getNode("alpha")))
388 _alpha = n->getFloatValue(), dirty = true;
393 FGColor::merge(const FGColor *color)
396 if (color && color->_red >= 0.0)
397 _red = color->_red, dirty = true;
398 if (color && color->_green >= 0.0)
399 _green = color->_green, dirty = true;
400 if (color && color->_blue >= 0.0)
401 _blue = color->_blue, dirty = true;
402 if (color && color->_alpha >= 0.0)
403 _alpha = color->_alpha, dirty = true;
410 ////////////////////////////////////////////////////////////////////////
411 // FGFontCache class.
412 ////////////////////////////////////////////////////////////////////////
421 : public std::unary_function<const GuiFont, bool>
423 Predicate(const char* name_) : name(name_) {}
424 bool operator() (const GuiFont& f1) const
426 return std::strcmp(f1.name, name) == 0;
432 const GuiFont guifonts[] = {
433 { "default", &FONT_HELVETICA_14 },
434 { "FIXED_8x13", &PUFONT_8_BY_13 },
435 { "FIXED_9x15", &PUFONT_9_BY_15 },
436 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
437 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
438 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
439 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
440 { "HELVETICA_14", &FONT_HELVETICA_14 },
441 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
442 { "SANS_12B", &FONT_SANS_12B }
445 const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])];
448 FGFontCache::FGFontCache() :
453 FGFontCache::~FGFontCache()
455 PuFontMap::iterator it, end = _puFonts.end();
456 for (it = _puFonts.begin(); it != end; ++it)
460 inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1,
461 const FntParams& f2) const
463 int comp = f1.name.compare(f2.name);
468 if (f1.size < f2.size)
470 else if (f1.size > f2.size)
472 return f1.slant < f2.slant;
475 struct FGFontCache::fnt *
476 FGFontCache::getfnt(const char *name, float size, float slant)
478 string fontName = boost::to_lower_copy(string(name));
479 FntParams fntParams(fontName, size, slant);
480 PuFontMap::iterator i = _puFonts.find(fntParams);
481 if (i != _puFonts.end()) {
482 // found in the puFonts map, all done
486 // fntTexFont s are all preloaded into the _texFonts map
487 TexFontMap::iterator texi = _texFonts.find(fontName);
488 fntTexFont* texfont = NULL;
489 puFont* pufont = NULL;
490 if (texi != _texFonts.end()) {
491 texfont = texi->second;
493 // check the built-in PUI fonts (in guifonts array)
494 const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
495 GuiFont::Predicate(name));
496 if (guifont != guifontsEnd) {
497 pufont = guifont->font;
504 } else if (texfont) {
505 f->texfont = texfont;
506 f->pufont = new puFont;
507 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
509 f->pufont = guifonts[0].font;
511 _puFonts[fntParams] = f;
516 FGFontCache::get(const char *name, float size, float slant)
518 return getfnt(name, size, slant)->pufont;
522 FGFontCache::getTexFont(const char *name, float size, float slant)
524 return getfnt(name, size, slant)->texfont;
528 FGFontCache::get(SGPropertyNode *node)
531 return get("Helvetica.txf", 15.0, 0.0);
533 const char *name = node->getStringValue("name", "Helvetica.txf");
534 float size = node->getFloatValue("size", 15.0);
535 float slant = node->getFloatValue("slant", 0.0);
537 return get(name, size, slant);
540 void FGFontCache::init()
546 char *envp = ::getenv("FG_FONTS");
550 _path.set(globals->get_fg_root());
551 _path.append("Fonts");
557 FGFontCache::getfntpath(const char *name)
561 if (name && std::string(name) != "") {
567 path = SGPath(_path);
568 path.append("Helvetica.txf");
569 SG_LOG(SG_GENERAL, SG_WARN, "Unknown font name '" << name << "', defaulting to Helvetica");
573 bool FGFontCache::initializeFonts()
575 static string fontext("txf");
577 ulDir* fontdir = ulOpenDir(_path.c_str());
580 const ulDirEnt *dirEntry;
581 while ((dirEntry = ulReadDir(fontdir)) != 0) {
583 path.append(dirEntry->d_name);
584 if (path.extension() == fontext) {
585 fntTexFont* f = new fntTexFont;
586 if (f->load((char *)path.c_str())) {
587 // convert font names in the map to lowercase for matching
588 string fontName = boost::to_lower_copy(string(dirEntry->d_name));
589 _texFonts[fontName] = f;
598 // end of new_gui.cxx