1 // new_gui.cxx: implementation of XML-configurable GUI support.
13 #include <simgear/compiler.h>
14 #include <simgear/structure/exception.hxx>
16 #include <Main/fg_props.hxx>
18 #include "menubar.hxx"
21 extern puFont FONT_HELVETICA_14;
22 extern puFont FONT_SANS_12B;
27 ////////////////////////////////////////////////////////////////////////
28 // Implementation of NewGUI.
29 ////////////////////////////////////////////////////////////////////////
34 : _menubar(new FGMenuBar),
42 _dialog_props.clear();
43 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
53 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
54 ulMakePath(path2, path1, "dialogs");
63 fgSetBool("/sim/signals/reinit-gui", true);
73 NewGUI::reset (bool reload)
75 map<string,FGDialog *>::iterator iter;
77 // close all open dialogs and remember them ...
78 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
79 dlg.push_back(iter->first);
82 for (i = 0; i < dlg.size(); i++)
89 _menubar = new FGMenuBar;
92 _dialog_props.clear();
100 // open dialogs again
101 for (i = 0; i < dlg.size(); i++)
108 fgTie("/sim/menubar/visibility", this,
109 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
115 fgUntie("/sim/menubar/visibility");
119 NewGUI::update (double delta_time_sec)
121 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
122 for(/**/; iter != _active_dialogs.end(); iter++)
123 iter->second->update();
127 NewGUI::showDialog (const string &name)
129 if (_dialog_props.find(name) == _dialog_props.end()) {
130 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
133 if(!_active_dialogs[name])
134 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
140 NewGUI::closeActiveDialog ()
142 if (_active_dialog == 0)
145 // Kill any entries in _active_dialogs... Is there an STL
146 // algorithm to do (delete map entries by value, not key)? I hate
148 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
149 for(/**/; iter != _active_dialogs.end(); iter++) {
150 if(iter->second == _active_dialog) {
151 _active_dialogs.erase(iter);
152 // iter is no longer valid
157 delete _active_dialog;
163 NewGUI::closeDialog (const string& name)
165 if(_active_dialogs.find(name) != _active_dialogs.end()) {
166 if(_active_dialog == _active_dialogs[name])
168 delete _active_dialogs[name];
169 _active_dialogs.erase(name);
172 return false; // dialog wasn't open...
176 NewGUI::getDialogProperties (const string &name)
178 if(_dialog_props.find(name) != _dialog_props.end())
179 return _dialog_props[name];
181 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
186 NewGUI::getDialog (const string &name)
188 if(_active_dialogs.find(name) != _active_dialogs.end())
189 return _active_dialogs[name];
191 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
196 NewGUI::setActiveDialog (FGDialog * dialog)
198 _active_dialog = dialog;
202 NewGUI::getActiveDialog ()
204 return _active_dialog;
208 NewGUI::getMenuBar ()
214 NewGUI::getMenuBarVisible () const
216 return _menubar->isVisible();
220 NewGUI::setMenuBarVisible (bool visible)
229 test_extension (const char * path, const char * ext)
231 int pathlen = strlen(path);
232 int extlen = strlen(ext);
234 for (int i = 1; i <= pathlen && i <= extlen; i++) {
235 if (path[pathlen-i] != ext[extlen-i])
242 NewGUI::newDialog (SGPropertyNode* props)
244 const char* cname = props->getStringValue("name");
246 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
250 if(_active_dialogs.find(name) == _active_dialogs.end())
251 _dialog_props[name] = props;
255 NewGUI::readDir (const char * path)
257 ulDir * dir = ulOpenDir(path);
260 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
265 for (ulDirEnt * dirEnt = ulReadDir(dir);
267 dirEnt = ulReadDir(dir)) {
271 ulMakePath(subpath, path, dirEnt->d_name);
273 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
274 SGPropertyNode * props = new SGPropertyNode;
276 readProperties(subpath, props);
277 } catch (const sg_exception &) {
278 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
283 SGPropertyNode *nameprop = props->getNode("name");
285 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
286 << " has no name; skipping.");
290 string name = nameprop->getStringValue();
291 if (_dialog_props[name])
292 delete (SGPropertyNode *)_dialog_props[name];
294 _dialog_props[name] = props;
302 ////////////////////////////////////////////////////////////////////////
304 ////////////////////////////////////////////////////////////////////////
307 NewGUI::setStyle (void)
310 for (it = _colors.begin(); it != _colors.end(); ++it)
314 // set up the traditional colors as default
315 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
316 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
317 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
318 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
319 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
320 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
321 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
323 //puSetDefaultStyle();
325 int which = fgGetInt("/sim/gui/current-style", 0);
326 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
327 SGPropertyNode *n = sim->getChild("style", which);
329 n = sim->getChild("style", 0, true);
331 setupFont(n->getNode("fonts/gui", true));
332 n = n->getNode("colors", true);
334 for (int i = 0; i < n->nChildren(); i++) {
335 SGPropertyNode *child = n->getChild(i);
336 _colors[child->getName()] = new FGColor(child);
339 FGColor *c = _colors["background"];
340 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
345 NewGUI::setupFont (SGPropertyNode *node)
347 _font = globals->get_fontcache()->get(node);
348 puSetDefaultFonts(*_font, *_font);
355 ////////////////////////////////////////////////////////////////////////
357 ////////////////////////////////////////////////////////////////////////
360 FGColor::print() const {
361 std::cerr << "red=" << _red << ", green=" << _green
362 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
366 FGColor::merge(const SGPropertyNode *node)
372 const SGPropertyNode * n;
373 if ((n = node->getNode("red")))
374 _red = n->getFloatValue(), dirty = true;
375 if ((n = node->getNode("green")))
376 _green = n->getFloatValue(), dirty = true;
377 if ((n = node->getNode("blue")))
378 _blue = n->getFloatValue(), dirty = true;
379 if ((n = node->getNode("alpha")))
380 _alpha = n->getFloatValue(), dirty = true;
385 FGColor::merge(const FGColor *color)
388 if (color && color->_red >= 0.0)
389 _red = color->_red, dirty = true;
390 if (color && color->_green >= 0.0)
391 _green = color->_green, dirty = true;
392 if (color && color->_blue >= 0.0)
393 _blue = color->_blue, dirty = true;
394 if (color && color->_alpha >= 0.0)
395 _alpha = color->_alpha, dirty = true;
402 ////////////////////////////////////////////////////////////////////////
403 // FGFontCache class.
404 ////////////////////////////////////////////////////////////////////////
413 : public std::unary_function<const GuiFont, bool>
415 Predicate(const char* name_) : name(name_) {}
416 bool operator() (const GuiFont& f1) const
418 return std::strcmp(f1.name, name) == 0;
424 const GuiFont guifonts[] = {
425 { "default", &FONT_HELVETICA_14 },
426 { "FIXED_8x13", &PUFONT_8_BY_13 },
427 { "FIXED_9x15", &PUFONT_9_BY_15 },
428 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
429 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
430 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
431 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
432 { "HELVETICA_14", &FONT_HELVETICA_14 },
433 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
434 { "SANS_12B", &FONT_SANS_12B }
437 const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])];
440 FGFontCache::FGFontCache() :
445 FGFontCache::~FGFontCache()
447 PuFontMap::iterator it, end = _puFonts.end();
448 for (it = _puFonts.begin(); it != end; ++it)
452 inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1,
453 const FntParams& f2) const
455 int comp = f1.name.compare(f2.name);
460 if (f1.size < f2.size)
462 else if (f1.size > f2.size)
464 return f1.slant < f2.slant;
467 struct FGFontCache::fnt *
468 FGFontCache::getfnt(const char *name, float size, float slant)
470 string fontName(name);
471 FntParams fntParams(fontName, size, slant);
472 PuFontMap::iterator i = _puFonts.find(fntParams);
473 if (i != _puFonts.end())
475 // fntTexFont s are all preloaded into the _texFonts map
476 TexFontMap::iterator texi = _texFonts.find(fontName);
477 fntTexFont* texfont = 0;
479 if (texi != _texFonts.end()) {
480 texfont = texi->second;
482 const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
483 GuiFont::Predicate(name));
484 if (guifont != guifontsEnd) {
485 pufont = guifont->font;
491 } else if (texfont) {
492 f->texfont = texfont;
493 f->pufont = new puFont;
494 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
496 f->pufont = guifonts[0].font;
498 _puFonts[fntParams] = f;
503 FGFontCache::get(const char *name, float size, float slant)
505 return getfnt(name, size, slant)->pufont;
509 FGFontCache::getTexFont(const char *name, float size, float slant)
511 return getfnt(name, size, slant)->texfont;
515 FGFontCache::get(SGPropertyNode *node)
518 return get("Helvetica.txf", 15.0, 0.0);
520 const char *name = node->getStringValue("name", "Helvetica.txf");
521 float size = node->getFloatValue("size", 15.0);
522 float slant = node->getFloatValue("slant", 0.0);
524 return get(name, size, slant);
527 void FGFontCache::init()
530 char *envp = ::getenv("FG_FONTS");
534 _path.set(globals->get_fg_root());
535 _path.append("Fonts");
542 FGFontCache::getfntpath(const char *name)
546 if (name && std::string(name) != "") {
552 path = SGPath(_path);
553 path.append("Helvetica.txf");
558 bool FGFontCache::initializeFonts()
560 static string fontext("txf");
562 ulDir* fontdir = ulOpenDir(_path.c_str());
565 const ulDirEnt *dirEntry;
566 while ((dirEntry = ulReadDir(fontdir)) != 0) {
568 path.append(dirEntry->d_name);
569 if (path.extension() == fontext) {
570 fntTexFont* f = new fntTexFont;
571 if (f->load((char *)path.c_str()))
572 _texFonts[string(dirEntry->d_name)] = f;
581 // end of new_gui.cxx