1 // new_gui.cxx: implementation of XML-configurable GUI support.
8 #include <simgear/compiler.h>
9 #include <simgear/structure/exception.hxx>
11 #include <Main/fg_props.hxx>
13 #include "menubar.hxx"
16 extern puFont FONT_HELVETICA_14;
17 extern puFont FONT_SANS_12B;
22 ////////////////////////////////////////////////////////////////////////
23 // Implementation of NewGUI.
24 ////////////////////////////////////////////////////////////////////////
29 : _menubar(new FGMenuBar),
37 _dialog_props.clear();
38 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
48 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
49 ulMakePath(path2, path1, "dialogs");
58 fgSetBool("/sim/signals/reinit-gui", true);
68 NewGUI::reset (bool reload)
70 map<string,FGDialog *>::iterator iter;
72 // close all open dialogs and remember them ...
73 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
74 dlg.push_back(iter->first);
77 for (i = 0; i < dlg.size(); i++)
84 _menubar = new FGMenuBar;
87 _dialog_props.clear();
96 for (i = 0; i < dlg.size(); i++)
103 fgTie("/sim/menubar/visibility", this,
104 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
110 fgUntie("/sim/menubar/visibility");
114 NewGUI::update (double delta_time_sec)
116 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
117 for(/**/; iter != _active_dialogs.end(); iter++)
118 iter->second->update();
122 NewGUI::showDialog (const string &name)
124 if (_dialog_props.find(name) == _dialog_props.end()) {
125 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
128 if(!_active_dialogs[name])
129 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
135 NewGUI::closeActiveDialog ()
137 if (_active_dialog == 0)
140 // Kill any entries in _active_dialogs... Is there an STL
141 // algorithm to do (delete map entries by value, not key)? I hate
143 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
144 for(/**/; iter != _active_dialogs.end(); iter++) {
145 if(iter->second == _active_dialog) {
146 _active_dialogs.erase(iter);
147 // iter is no longer valid
152 delete _active_dialog;
158 NewGUI::closeDialog (const string& name)
160 if(_active_dialogs.find(name) != _active_dialogs.end()) {
161 if(_active_dialog == _active_dialogs[name])
163 delete _active_dialogs[name];
164 _active_dialogs.erase(name);
167 return false; // dialog wasn't open...
171 NewGUI::getDialogProperties (const string &name)
173 if(_dialog_props.find(name) != _dialog_props.end())
174 return _dialog_props[name];
176 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
181 NewGUI::getDialog (const string &name)
183 if(_active_dialogs.find(name) != _active_dialogs.end())
184 return _active_dialogs[name];
186 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
191 NewGUI::setActiveDialog (FGDialog * dialog)
193 _active_dialog = dialog;
197 NewGUI::getActiveDialog ()
199 return _active_dialog;
203 NewGUI::getMenuBar ()
209 NewGUI::getMenuBarVisible () const
211 return _menubar->isVisible();
215 NewGUI::setMenuBarVisible (bool visible)
224 test_extension (const char * path, const char * ext)
226 int pathlen = strlen(path);
227 int extlen = strlen(ext);
229 for (int i = 1; i <= pathlen && i <= extlen; i++) {
230 if (path[pathlen-i] != ext[extlen-i])
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 char * path)
252 ulDir * dir = ulOpenDir(path);
255 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
260 for (ulDirEnt * dirEnt = ulReadDir(dir);
262 dirEnt = ulReadDir(dir)) {
266 ulMakePath(subpath, path, dirEnt->d_name);
268 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
269 SGPropertyNode * props = new SGPropertyNode;
271 readProperties(subpath, props);
272 } catch (const sg_exception &) {
273 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
278 SGPropertyNode *nameprop = props->getNode("name");
280 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
281 << " has no name; skipping.");
285 string name = nameprop->getStringValue();
286 if (_dialog_props[name])
287 delete (SGPropertyNode *)_dialog_props[name];
289 _dialog_props[name] = props;
297 ////////////////////////////////////////////////////////////////////////
299 ////////////////////////////////////////////////////////////////////////
302 NewGUI::setStyle (void)
305 for (it = _colors.begin(); it != _colors.end(); ++it)
309 // set up the traditional colors as default
310 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
311 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
312 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
313 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
314 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
315 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
316 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
318 //puSetDefaultStyle();
320 int which = fgGetInt("/sim/gui/current-style", 0);
321 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
322 SGPropertyNode *n = sim->getChild("style", which);
324 n = sim->getChild("style", 0, true);
326 setupFont(n->getNode("fonts/gui", true));
327 n = n->getNode("colors", true);
329 for (int i = 0; i < n->nChildren(); i++) {
330 SGPropertyNode *child = n->getChild(i);
331 _colors[child->getName()] = new FGColor(child);
334 FGColor *c = _colors["background"];
335 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
340 NewGUI::setupFont (SGPropertyNode *node)
342 _font = globals->get_fontcache()->get(node);
343 puSetDefaultFonts(*_font, *_font);
350 ////////////////////////////////////////////////////////////////////////
352 ////////////////////////////////////////////////////////////////////////
355 FGColor::merge(const SGPropertyNode *node)
361 const SGPropertyNode * n;
362 if ((n = node->getNode("red")))
363 _red = n->getFloatValue(), dirty = true;
364 if ((n = node->getNode("green")))
365 _green = n->getFloatValue(), dirty = true;
366 if ((n = node->getNode("blue")))
367 _blue = n->getFloatValue(), dirty = true;
368 if ((n = node->getNode("alpha")))
369 _alpha = n->getFloatValue(), dirty = true;
374 FGColor::merge(const FGColor *color)
377 if (color && color->_red >= 0.0)
378 _red = color->_red, dirty = true;
379 if (color && color->_green >= 0.0)
380 _green = color->_green, dirty = true;
381 if (color && color->_blue >= 0.0)
382 _blue = color->_blue, dirty = true;
383 if (color && color->_alpha >= 0.0)
384 _alpha = color->_alpha, dirty = true;
391 ////////////////////////////////////////////////////////////////////////
392 // FGFontCache class.
393 ////////////////////////////////////////////////////////////////////////
395 static const struct {
399 { "default", &FONT_HELVETICA_14 },
400 { "FIXED_8x13", &PUFONT_8_BY_13 },
401 { "FIXED_9x15", &PUFONT_9_BY_15 },
402 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
403 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
404 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
405 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
406 { "HELVETICA_14", &FONT_HELVETICA_14 },
407 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
408 { "SANS_12B", &FONT_SANS_12B },
413 FGFontCache::FGFontCache() :
418 FGFontCache::~FGFontCache()
420 map<const string, fnt *>::iterator it, end = _fonts.end();
421 for (it = _fonts.begin(); it != end; ++it)
425 struct FGFontCache::fnt *
426 FGFontCache::getfnt(const char *name, float size, float slant)
429 if ((it = _fonts.find(name)) != _fonts.end())
432 SGPath path = getfntpath(name);
435 f->texfont = new fntTexFont;
437 if (f->texfont->load((char *)path.c_str())) {
438 f->pufont = new puFont;
439 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
440 return _fonts[name] = f;
444 return _fonts["default"];
448 FGFontCache::get(const char *name, float size, float slant)
450 return getfnt(name, size, slant)->pufont;
454 FGFontCache::getTexFont(const char *name, float size, float slant)
456 return getfnt(name, size, slant)->texfont;
460 FGFontCache::get(SGPropertyNode *node)
463 return get("Helvetica.txf", 15.0, 0.0);
465 const char *name = node->getStringValue("name", "Helvetica.txf");
466 float size = node->getFloatValue("size", 15.0);
467 float slant = node->getFloatValue("slant", 0.0);
469 return get(name, size, slant);
473 FGFontCache::getfntpath(const char *name)
476 char *envp = ::getenv("FG_FONTS");
480 _path.set(globals->get_fg_root());
481 _path.append("Fonts");
484 for (int i = 0; guifonts[i].name; i++)
485 _fonts[guifonts[i].name] = new fnt(guifonts[i].font);
491 if (name && std::string(name) != "") {
497 path = SGPath(_path);
498 path.append("Helvetica.txf");
503 // end of new_gui.cxx