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 #if defined(SG_UNIX) && !defined(SG_MAC)
29 #include "menubar.hxx"
32 extern puFont FONT_HELVETICA_14;
33 extern puFont FONT_SANS_12B;
38 ////////////////////////////////////////////////////////////////////////
39 // Implementation of NewGUI.
40 ////////////////////////////////////////////////////////////////////////
45 : _menubar(new FGMenuBar),
53 _dialog_props.clear();
54 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
62 SGPath p(globals->get_fg_root(), "gui/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 NewGUI::newDialog (SGPropertyNode* props)
239 const char* cname = props->getStringValue("name");
241 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
245 if(_active_dialogs.find(name) == _active_dialogs.end())
246 _dialog_props[name] = props;
250 NewGUI::readDir (const SGPath& path)
252 simgear::Dir dir(path);
253 simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
255 for (unsigned int i=0; i<xmls.size(); ++i) {
256 SGPropertyNode * props = new SGPropertyNode;
258 readProperties(xmls[i].str(), props);
259 } catch (const sg_exception &) {
260 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
265 SGPropertyNode *nameprop = props->getNode("name");
267 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmls[i].str()
268 << " has no name; skipping.");
272 string name = nameprop->getStringValue();
273 _dialog_props[name] = props;
279 ////////////////////////////////////////////////////////////////////////
281 ////////////////////////////////////////////////////////////////////////
284 NewGUI::setStyle (void)
287 for (it = _colors.begin(); it != _colors.end(); ++it)
291 // set up the traditional colors as default
292 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
293 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
294 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
295 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
296 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
297 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
298 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
300 //puSetDefaultStyle();
302 int which = fgGetInt("/sim/gui/current-style", 0);
303 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
304 SGPropertyNode *n = sim->getChild("style", which);
306 n = sim->getChild("style", 0, true);
308 setupFont(n->getNode("fonts/gui", true));
309 n = n->getNode("colors", true);
311 for (int i = 0; i < n->nChildren(); i++) {
312 SGPropertyNode *child = n->getChild(i);
313 _colors[child->getName()] = new FGColor(child);
316 FGColor *c = _colors["background"];
317 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
322 NewGUI::setupFont (SGPropertyNode *node)
324 _font = globals->get_fontcache()->get(node);
325 puSetDefaultFonts(*_font, *_font);
332 ////////////////////////////////////////////////////////////////////////
334 ////////////////////////////////////////////////////////////////////////
337 FGColor::print() const {
338 std::cerr << "red=" << _red << ", green=" << _green
339 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
343 FGColor::merge(const SGPropertyNode *node)
349 const SGPropertyNode * n;
350 if ((n = node->getNode("red")))
351 _red = n->getFloatValue(), dirty = true;
352 if ((n = node->getNode("green")))
353 _green = n->getFloatValue(), dirty = true;
354 if ((n = node->getNode("blue")))
355 _blue = n->getFloatValue(), dirty = true;
356 if ((n = node->getNode("alpha")))
357 _alpha = n->getFloatValue(), dirty = true;
362 FGColor::merge(const FGColor *color)
365 if (color && color->_red >= 0.0)
366 _red = color->_red, dirty = true;
367 if (color && color->_green >= 0.0)
368 _green = color->_green, dirty = true;
369 if (color && color->_blue >= 0.0)
370 _blue = color->_blue, dirty = true;
371 if (color && color->_alpha >= 0.0)
372 _alpha = color->_alpha, dirty = true;
379 ////////////////////////////////////////////////////////////////////////
380 // FGFontCache class.
381 ////////////////////////////////////////////////////////////////////////
390 : public std::unary_function<const GuiFont, bool>
392 Predicate(const char* name_) : name(name_) {}
393 bool operator() (const GuiFont& f1) const
395 return std::strcmp(f1.name, name) == 0;
401 const GuiFont guifonts[] = {
402 { "default", &FONT_HELVETICA_14 },
403 { "FIXED_8x13", &PUFONT_8_BY_13 },
404 { "FIXED_9x15", &PUFONT_9_BY_15 },
405 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
406 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
407 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
408 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
409 { "HELVETICA_14", &FONT_HELVETICA_14 },
410 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
411 { "SANS_12B", &FONT_SANS_12B }
414 const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])];
417 FGFontCache::FGFontCache() :
422 FGFontCache::~FGFontCache()
424 #if defined(SG_UNIX) && !defined(SG_MAC)
425 // Ugly workaround for a crash on exit with multiple screens configured
426 if (!glXGetCurrentContext())
429 PuFontMap::iterator it, end = _puFonts.end();
430 for (it = _puFonts.begin(); it != end; ++it)
434 inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1,
435 const FntParams& f2) const
437 int comp = f1.name.compare(f2.name);
442 if (f1.size < f2.size)
444 else if (f1.size > f2.size)
446 return f1.slant < f2.slant;
449 struct FGFontCache::fnt *
450 FGFontCache::getfnt(const char *name, float size, float slant)
452 string fontName = boost::to_lower_copy(string(name));
453 FntParams fntParams(fontName, size, slant);
454 PuFontMap::iterator i = _puFonts.find(fntParams);
455 if (i != _puFonts.end()) {
456 // found in the puFonts map, all done
460 // fntTexFont s are all preloaded into the _texFonts map
461 TexFontMap::iterator texi = _texFonts.find(fontName);
462 fntTexFont* texfont = NULL;
463 puFont* pufont = NULL;
464 if (texi != _texFonts.end()) {
465 texfont = texi->second;
467 // check the built-in PUI fonts (in guifonts array)
468 const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
469 GuiFont::Predicate(name));
470 if (guifont != guifontsEnd) {
471 pufont = guifont->font;
478 } else if (texfont) {
479 f->texfont = texfont;
480 f->pufont = new puFont;
481 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
483 f->pufont = guifonts[0].font;
485 _puFonts[fntParams] = f;
490 FGFontCache::get(const char *name, float size, float slant)
492 return getfnt(name, size, slant)->pufont;
496 FGFontCache::getTexFont(const char *name, float size, float slant)
498 return getfnt(name, size, slant)->texfont;
502 FGFontCache::get(SGPropertyNode *node)
505 return get("Helvetica.txf", 15.0, 0.0);
507 const char *name = node->getStringValue("name", "Helvetica.txf");
508 float size = node->getFloatValue("size", 15.0);
509 float slant = node->getFloatValue("slant", 0.0);
511 return get(name, size, slant);
514 void FGFontCache::init()
520 char *envp = ::getenv("FG_FONTS");
524 _path.set(globals->get_fg_root());
525 _path.append("Fonts");
531 FGFontCache::getfntpath(const char *name)
535 if (name && std::string(name) != "") {
541 path = SGPath(_path);
542 path.append("Helvetica.txf");
543 SG_LOG(SG_GENERAL, SG_WARN, "Unknown font name '" << name << "', defaulting to Helvetica");
547 bool FGFontCache::initializeFonts()
549 static string fontext("txf");
551 ulDir* fontdir = ulOpenDir(_path.c_str());
554 const ulDirEnt *dirEntry;
555 while ((dirEntry = ulReadDir(fontdir)) != 0) {
557 path.append(dirEntry->d_name);
558 if (path.extension() == fontext) {
559 fntTexFont* f = new fntTexFont;
560 if (f->load((char *)path.c_str())) {
561 // convert font names in the map to lowercase for matching
562 string fontName = boost::to_lower_copy(string(dirEntry->d_name));
563 _texFonts[fontName] = f;
572 // end of new_gui.cxx