1 // new_gui.cxx: implementation of XML-configurable GUI support.
9 #include <simgear/compiler.h>
10 #include <simgear/structure/exception.hxx>
12 #include <Main/fg_props.hxx>
14 #include "menubar.hxx"
16 #include "SafeTexFont.hxx"
18 extern puFont FONT_HELVETICA_14;
19 extern puFont FONT_SANS_12B;
24 ////////////////////////////////////////////////////////////////////////
25 // Implementation of NewGUI.
26 ////////////////////////////////////////////////////////////////////////
31 : _menubar(new FGMenuBar),
39 _dialog_props.clear();
40 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
50 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
51 ulMakePath(path2, path1, "dialogs");
60 fgSetBool("/sim/signals/reinit-gui", true);
70 NewGUI::reset (bool reload)
72 map<string,FGDialog *>::iterator iter;
74 // close all open dialogs and remember them ...
75 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
76 dlg.push_back(iter->first);
79 for (i = 0; i < dlg.size(); i++)
86 _menubar = new FGMenuBar;
89 _dialog_props.clear();
98 for (i = 0; i < dlg.size(); i++)
105 fgTie("/sim/menubar/visibility", this,
106 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
112 fgUntie("/sim/menubar/visibility");
116 NewGUI::update (double delta_time_sec)
118 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
119 for(/**/; iter != _active_dialogs.end(); iter++)
120 iter->second->update();
124 NewGUI::showDialog (const string &name)
126 if (_dialog_props.find(name) == _dialog_props.end()) {
127 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
130 if(!_active_dialogs[name])
131 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
137 NewGUI::closeActiveDialog ()
139 if (_active_dialog == 0)
142 // Kill any entries in _active_dialogs... Is there an STL
143 // algorithm to do (delete map entries by value, not key)? I hate
145 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
146 for(/**/; iter != _active_dialogs.end(); iter++) {
147 if(iter->second == _active_dialog) {
148 _active_dialogs.erase(iter);
149 // iter is no longer valid
154 delete _active_dialog;
160 NewGUI::closeDialog (const string& name)
162 if(_active_dialogs.find(name) != _active_dialogs.end()) {
163 if(_active_dialog == _active_dialogs[name])
165 delete _active_dialogs[name];
166 _active_dialogs.erase(name);
169 return false; // dialog wasn't open...
173 NewGUI::getDialogProperties (const string &name)
175 if(_dialog_props.find(name) != _dialog_props.end())
176 return _dialog_props[name];
178 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
183 NewGUI::getDialog (const string &name)
185 if(_active_dialogs.find(name) != _active_dialogs.end())
186 return _active_dialogs[name];
188 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
193 NewGUI::setActiveDialog (FGDialog * dialog)
195 _active_dialog = dialog;
199 NewGUI::getActiveDialog ()
201 return _active_dialog;
205 NewGUI::getMenuBar ()
211 NewGUI::getMenuBarVisible () const
213 return _menubar->isVisible();
217 NewGUI::setMenuBarVisible (bool visible)
226 test_extension (const char * path, const char * ext)
228 int pathlen = strlen(path);
229 int extlen = strlen(ext);
231 for (int i = 1; i <= pathlen && i <= extlen; i++) {
232 if (path[pathlen-i] != ext[extlen-i])
239 NewGUI::newDialog (SGPropertyNode* props)
241 const char* cname = props->getStringValue("name");
243 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
247 if(_active_dialogs.find(name) == _active_dialogs.end())
248 _dialog_props[name] = props;
252 NewGUI::readDir (const char * path)
254 ulDir * dir = ulOpenDir(path);
257 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
262 for (ulDirEnt * dirEnt = ulReadDir(dir);
264 dirEnt = ulReadDir(dir)) {
268 ulMakePath(subpath, path, dirEnt->d_name);
270 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
271 SGPropertyNode * props = new SGPropertyNode;
273 readProperties(subpath, props);
274 } catch (const sg_exception &) {
275 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
280 SGPropertyNode *nameprop = props->getNode("name");
282 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
283 << " has no name; skipping.");
287 string name = nameprop->getStringValue();
288 if (_dialog_props[name])
289 delete (SGPropertyNode *)_dialog_props[name];
291 _dialog_props[name] = props;
299 ////////////////////////////////////////////////////////////////////////
301 ////////////////////////////////////////////////////////////////////////
304 NewGUI::setStyle (void)
307 for (it = _colors.begin(); it != _colors.end(); ++it)
311 // set up the traditional colors as default
312 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
313 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
314 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
315 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
316 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
317 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
318 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
320 //puSetDefaultStyle();
322 int which = fgGetInt("/sim/gui/current-style", 0);
323 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
324 SGPropertyNode *n = sim->getChild("style", which);
326 n = sim->getChild("style", 0, true);
328 setupFont(n->getNode("fonts/gui", true));
329 n = n->getNode("colors", true);
331 for (int i = 0; i < n->nChildren(); i++) {
332 SGPropertyNode *child = n->getChild(i);
333 _colors[child->getName()] = new FGColor(child);
336 FGColor *c = _colors["background"];
337 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
342 NewGUI::setupFont (SGPropertyNode *node)
344 _font = globals->get_fontcache()->get(node);
345 puSetDefaultFonts(*_font, *_font);
352 ////////////////////////////////////////////////////////////////////////
354 ////////////////////////////////////////////////////////////////////////
357 FGColor::print() const {
358 std::cerr << "red=" << _red << ", green=" << _green
359 << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
363 FGColor::merge(const SGPropertyNode *node)
369 const SGPropertyNode * n;
370 if ((n = node->getNode("red")))
371 _red = n->getFloatValue(), dirty = true;
372 if ((n = node->getNode("green")))
373 _green = n->getFloatValue(), dirty = true;
374 if ((n = node->getNode("blue")))
375 _blue = n->getFloatValue(), dirty = true;
376 if ((n = node->getNode("alpha")))
377 _alpha = n->getFloatValue(), dirty = true;
382 FGColor::merge(const FGColor *color)
385 if (color && color->_red >= 0.0)
386 _red = color->_red, dirty = true;
387 if (color && color->_green >= 0.0)
388 _green = color->_green, dirty = true;
389 if (color && color->_blue >= 0.0)
390 _blue = color->_blue, dirty = true;
391 if (color && color->_alpha >= 0.0)
392 _alpha = color->_alpha, dirty = true;
399 ////////////////////////////////////////////////////////////////////////
400 // FGFontCache class.
401 ////////////////////////////////////////////////////////////////////////
403 static const struct {
407 { "default", &FONT_HELVETICA_14 },
408 { "FIXED_8x13", &PUFONT_8_BY_13 },
409 { "FIXED_9x15", &PUFONT_9_BY_15 },
410 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
411 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
412 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
413 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
414 { "HELVETICA_14", &FONT_HELVETICA_14 },
415 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
416 { "SANS_12B", &FONT_SANS_12B },
421 FGFontCache::FGFontCache() :
426 FGFontCache::~FGFontCache()
428 map<const string, fnt *>::iterator it, end = _fonts.end();
429 for (it = _fonts.begin(); it != end; ++it)
433 struct FGFontCache::fnt *
434 FGFontCache::getfnt(const char *name, float size, float slant)
437 if ((it = _fonts.find(name)) != _fonts.end())
440 SGPath path = getfntpath(name);
443 f->texfont = new flightgear::SafeTexFont;
445 if (f->texfont->load((char *)path.c_str())) {
446 f->pufont = new puFont;
447 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
448 return _fonts[name] = f;
452 return _fonts["default"];
456 FGFontCache::get(const char *name, float size, float slant)
458 return getfnt(name, size, slant)->pufont;
462 FGFontCache::getTexFont(const char *name, float size, float slant)
464 return getfnt(name, size, slant)->texfont;
468 FGFontCache::get(SGPropertyNode *node)
471 return get("Helvetica.txf", 15.0, 0.0);
473 const char *name = node->getStringValue("name", "Helvetica.txf");
474 float size = node->getFloatValue("size", 15.0);
475 float slant = node->getFloatValue("slant", 0.0);
477 return get(name, size, slant);
481 FGFontCache::getfntpath(const char *name)
484 char *envp = ::getenv("FG_FONTS");
488 _path.set(globals->get_fg_root());
489 _path.append("Fonts");
492 for (int i = 0; guifonts[i].name; i++)
493 _fonts[guifonts[i].name] = new fnt(guifonts[i].font);
499 if (name && std::string(name) != "") {
505 path = SGPath(_path);
506 path.append("Helvetica.txf");
511 // end of new_gui.cxx