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 <Main/fg_props.hxx>
19 #include "menubar.hxx"
22 extern puFont FONT_HELVETICA_14;
23 extern puFont FONT_SANS_12B;
28 ////////////////////////////////////////////////////////////////////////
29 // Implementation of NewGUI.
30 ////////////////////////////////////////////////////////////////////////
35 : _menubar(new FGMenuBar),
43 _dialog_props.clear();
44 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
54 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
55 ulMakePath(path2, path1, "dialogs");
64 fgSetBool("/sim/signals/reinit-gui", true);
74 NewGUI::reset (bool reload)
76 map<string,FGDialog *>::iterator iter;
78 // close all open dialogs and remember them ...
79 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
80 dlg.push_back(iter->first);
83 for (i = 0; i < dlg.size(); i++)
90 _menubar = new FGMenuBar;
93 _dialog_props.clear();
101 // open dialogs again
102 for (i = 0; i < dlg.size(); i++)
109 fgTie("/sim/menubar/visibility", this,
110 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
116 fgUntie("/sim/menubar/visibility");
120 NewGUI::update (double delta_time_sec)
122 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
123 for(/**/; iter != _active_dialogs.end(); iter++)
124 iter->second->update();
128 NewGUI::showDialog (const string &name)
130 if (_dialog_props.find(name) == _dialog_props.end()) {
131 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
134 if(!_active_dialogs[name])
135 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
141 NewGUI::closeActiveDialog ()
143 if (_active_dialog == 0)
146 // Kill any entries in _active_dialogs... Is there an STL
147 // algorithm to do (delete map entries by value, not key)? I hate
149 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
150 for(/**/; iter != _active_dialogs.end(); iter++) {
151 if(iter->second == _active_dialog) {
152 _active_dialogs.erase(iter);
153 // iter is no longer valid
158 delete _active_dialog;
164 NewGUI::closeDialog (const string& name)
166 if(_active_dialogs.find(name) != _active_dialogs.end()) {
167 if(_active_dialog == _active_dialogs[name])
169 delete _active_dialogs[name];
170 _active_dialogs.erase(name);
173 return false; // dialog wasn't open...
177 NewGUI::getDialogProperties (const string &name)
179 if(_dialog_props.find(name) != _dialog_props.end())
180 return _dialog_props[name];
182 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
187 NewGUI::getDialog (const string &name)
189 if(_active_dialogs.find(name) != _active_dialogs.end())
190 return _active_dialogs[name];
192 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
197 NewGUI::setActiveDialog (FGDialog * dialog)
199 _active_dialog = dialog;
203 NewGUI::getActiveDialog ()
205 return _active_dialog;
209 NewGUI::getMenuBar ()
215 NewGUI::getMenuBarVisible () const
217 return _menubar->isVisible();
221 NewGUI::setMenuBarVisible (bool visible)
230 test_extension (const char * path, const char * ext)
232 int pathlen = strlen(path);
233 int extlen = strlen(ext);
235 for (int i = 1; i <= pathlen && i <= extlen; i++) {
236 if (path[pathlen-i] != ext[extlen-i])
243 NewGUI::newDialog (SGPropertyNode* props)
245 const char* cname = props->getStringValue("name");
247 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
251 if(_active_dialogs.find(name) == _active_dialogs.end())
252 _dialog_props[name] = props;
256 NewGUI::readDir (const char * path)
258 ulDir * dir = ulOpenDir(path);
261 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
266 for (ulDirEnt * dirEnt = ulReadDir(dir);
268 dirEnt = ulReadDir(dir)) {
272 ulMakePath(subpath, path, dirEnt->d_name);
274 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
275 SGPropertyNode * props = new SGPropertyNode;
277 readProperties(subpath, props);
278 } catch (const sg_exception &) {
279 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
284 SGPropertyNode *nameprop = props->getNode("name");
286 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
287 << " has no name; skipping.");
291 string name = nameprop->getStringValue();
292 if (_dialog_props[name])
293 delete (SGPropertyNode *)_dialog_props[name];
295 _dialog_props[name] = props;
303 ////////////////////////////////////////////////////////////////////////
305 ////////////////////////////////////////////////////////////////////////
308 NewGUI::setStyle (void)
311 for (it = _colors.begin(); it != _colors.end(); ++it)
315 // set up the traditional colors as default
316 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
317 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
318 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
319 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
320 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
321 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
322 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
324 //puSetDefaultStyle();
326 int which = fgGetInt("/sim/gui/current-style", 0);
327 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
328 SGPropertyNode *n = sim->getChild("style", which);
330 n = sim->getChild("style", 0, true);
332 setupFont(n->getNode("fonts/gui", true));
333 n = n->getNode("colors", true);
335 for (int i = 0; i < n->nChildren(); i++) {
336 SGPropertyNode *child = n->getChild(i);
337 _colors[child->getName()] = new FGColor(child);
340 FGColor *c = _colors["background"];
341 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
346 NewGUI::setupFont (SGPropertyNode *node)
348 _font = globals->get_fontcache()->get(node);
349 puSetDefaultFonts(*_font, *_font);
356 ////////////////////////////////////////////////////////////////////////
358 ////////////////////////////////////////////////////////////////////////
361 FGColor::print() const {
362 std::cerr << "red=" << _red << ", green=" << _green
363 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
367 FGColor::merge(const SGPropertyNode *node)
373 const SGPropertyNode * n;
374 if ((n = node->getNode("red")))
375 _red = n->getFloatValue(), dirty = true;
376 if ((n = node->getNode("green")))
377 _green = n->getFloatValue(), dirty = true;
378 if ((n = node->getNode("blue")))
379 _blue = n->getFloatValue(), dirty = true;
380 if ((n = node->getNode("alpha")))
381 _alpha = n->getFloatValue(), dirty = true;
386 FGColor::merge(const FGColor *color)
389 if (color && color->_red >= 0.0)
390 _red = color->_red, dirty = true;
391 if (color && color->_green >= 0.0)
392 _green = color->_green, dirty = true;
393 if (color && color->_blue >= 0.0)
394 _blue = color->_blue, dirty = true;
395 if (color && color->_alpha >= 0.0)
396 _alpha = color->_alpha, dirty = true;
403 ////////////////////////////////////////////////////////////////////////
404 // FGFontCache class.
405 ////////////////////////////////////////////////////////////////////////
414 : public std::unary_function<const GuiFont, bool>
416 Predicate(const char* name_) : name(name_) {}
417 bool operator() (const GuiFont& f1) const
419 return std::strcmp(f1.name, name) == 0;
425 const GuiFont guifonts[] = {
426 { "default", &FONT_HELVETICA_14 },
427 { "FIXED_8x13", &PUFONT_8_BY_13 },
428 { "FIXED_9x15", &PUFONT_9_BY_15 },
429 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
430 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
431 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
432 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
433 { "HELVETICA_14", &FONT_HELVETICA_14 },
434 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
435 { "SANS_12B", &FONT_SANS_12B }
438 const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])];
441 FGFontCache::FGFontCache() :
446 FGFontCache::~FGFontCache()
448 PuFontMap::iterator it, end = _puFonts.end();
449 for (it = _puFonts.begin(); it != end; ++it)
453 inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1,
454 const FntParams& f2) const
456 int comp = f1.name.compare(f2.name);
461 if (f1.size < f2.size)
463 else if (f1.size > f2.size)
465 return f1.slant < f2.slant;
468 struct FGFontCache::fnt *
469 FGFontCache::getfnt(const char *name, float size, float slant)
471 string fontName(name);
472 FntParams fntParams(fontName, size, slant);
473 PuFontMap::iterator i = _puFonts.find(fntParams);
474 if (i != _puFonts.end())
476 // fntTexFont s are all preloaded into the _texFonts map
477 TexFontMap::iterator texi = _texFonts.find(fontName);
478 fntTexFont* texfont = 0;
480 if (texi != _texFonts.end()) {
481 texfont = texi->second;
483 const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
484 GuiFont::Predicate(name));
485 if (guifont != guifontsEnd) {
486 pufont = guifont->font;
492 } else if (texfont) {
493 f->texfont = texfont;
494 f->pufont = new puFont;
495 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
497 f->pufont = guifonts[0].font;
499 _puFonts[fntParams] = f;
504 FGFontCache::get(const char *name, float size, float slant)
506 return getfnt(name, size, slant)->pufont;
510 FGFontCache::getTexFont(const char *name, float size, float slant)
512 return getfnt(name, size, slant)->texfont;
516 FGFontCache::get(SGPropertyNode *node)
519 return get("Helvetica.txf", 15.0, 0.0);
521 const char *name = node->getStringValue("name", "Helvetica.txf");
522 float size = node->getFloatValue("size", 15.0);
523 float slant = node->getFloatValue("slant", 0.0);
525 return get(name, size, slant);
528 void FGFontCache::init()
531 char *envp = ::getenv("FG_FONTS");
535 _path.set(globals->get_fg_root());
536 _path.append("Fonts");
543 FGFontCache::getfntpath(const char *name)
547 if (name && std::string(name) != "") {
553 path = SGPath(_path);
554 path.append("Helvetica.txf");
559 bool FGFontCache::initializeFonts()
561 static string fontext("txf");
563 ulDir* fontdir = ulOpenDir(_path.c_str());
566 const ulDirEnt *dirEntry;
567 while ((dirEntry = ulReadDir(fontdir)) != 0) {
569 path.append(dirEntry->d_name);
570 if (path.extension() == fontext) {
571 fntTexFont* f = new fntTexFont;
572 if (f->load((char *)path.c_str()))
573 _texFonts[string(dirEntry->d_name)] = f;
582 // end of new_gui.cxx