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();
46 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
47 ulMakePath(path2, path1, "dialogs");
65 NewGUI::reset (bool reload)
67 map<string,FGDialog *>::iterator iter;
69 // close all open dialogs and remember them ...
70 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
71 dlg.push_back(iter->first);
74 for (i = 0; i < dlg.size(); i++)
81 _menubar = new FGMenuBar;
84 _dialog_props.clear();
93 for (i = 0; i < dlg.size(); i++)
100 fgTie("/sim/menubar/visibility", this,
101 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
107 fgUntie("/sim/menubar/visibility");
111 NewGUI::update (double delta_time_sec)
113 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
114 for(/**/; iter != _active_dialogs.end(); iter++)
115 iter->second->update();
119 NewGUI::showDialog (const string &name)
121 if (_dialog_props.find(name) == _dialog_props.end()) {
122 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
125 if(!_active_dialogs[name])
126 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
132 NewGUI::closeActiveDialog ()
134 if (_active_dialog == 0)
137 // Kill any entries in _active_dialogs... Is there an STL
138 // algorithm to do (delete map entries by value, not key)? I hate
140 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
141 for(/**/; iter != _active_dialogs.end(); iter++) {
142 if(iter->second == _active_dialog) {
143 _active_dialogs.erase(iter);
144 // iter is no longer valid
149 delete _active_dialog;
155 NewGUI::closeDialog (const string& name)
157 if(_active_dialogs.find(name) != _active_dialogs.end()) {
158 if(_active_dialog == _active_dialogs[name])
160 delete _active_dialogs[name];
161 _active_dialogs.erase(name);
164 return false; // dialog wasn't open...
168 NewGUI::getDialogProperties (const string &name)
170 if(_dialog_props.find(name) != _dialog_props.end())
171 return _dialog_props[name];
173 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
178 NewGUI::getDialog (const string &name)
180 if(_active_dialogs.find(name) != _active_dialogs.end())
181 return _active_dialogs[name];
183 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
188 NewGUI::setActiveDialog (FGDialog * dialog)
190 _active_dialog = dialog;
194 NewGUI::getActiveDialog ()
196 return _active_dialog;
200 NewGUI::getMenuBar ()
206 NewGUI::getMenuBarVisible () const
208 return _menubar->isVisible();
212 NewGUI::setMenuBarVisible (bool visible)
221 test_extension (const char * path, const char * ext)
223 int pathlen = strlen(path);
224 int extlen = strlen(ext);
226 for (int i = 1; i <= pathlen && i <= extlen; i++) {
227 if (path[pathlen-i] != ext[extlen-i])
234 NewGUI::newDialog (SGPropertyNode* props)
236 const char* cname = props->getStringValue("name");
238 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
242 if(_active_dialogs.find(name) == _active_dialogs.end())
243 _dialog_props[name] = props;
247 NewGUI::readDir (const char * path)
249 ulDir * dir = ulOpenDir(path);
252 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
257 for (ulDirEnt * dirEnt = ulReadDir(dir);
259 dirEnt = ulReadDir(dir)) {
263 ulMakePath(subpath, path, dirEnt->d_name);
265 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
266 SGPropertyNode * props = new SGPropertyNode;
268 readProperties(subpath, props);
269 } catch (const sg_exception &) {
270 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
275 SGPropertyNode *nameprop = props->getNode("name");
277 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
278 << " has no name; skipping.");
282 string name = nameprop->getStringValue();
283 if (_dialog_props[name])
284 delete (SGPropertyNode *)_dialog_props[name];
286 _dialog_props[name] = props;
294 ////////////////////////////////////////////////////////////////////////
296 ////////////////////////////////////////////////////////////////////////
299 NewGUI::setStyle (void)
302 for (it = _colors.begin(); it != _colors.end(); ++it)
306 // set up the traditional colors as default
307 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
308 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
309 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
310 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
311 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
312 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
313 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
315 //puSetDefaultStyle();
317 int which = fgGetInt("/sim/gui/current-style", 0);
318 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
319 SGPropertyNode *n = sim->getChild("style", which);
321 n = sim->getChild("style", 0, true);
323 setupFont(n->getNode("fonts/gui", true));
324 n = n->getNode("colors", true);
326 for (int i = 0; i < n->nChildren(); i++) {
327 SGPropertyNode *child = n->getChild(i);
328 _colors[child->getName()] = new FGColor(child);
331 FGColor *c = _colors["background"];
332 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
337 NewGUI::setupFont (SGPropertyNode *node)
339 _font = globals->get_fontcache()->get(node);
340 puSetDefaultFonts(*_font, *_font);
347 ////////////////////////////////////////////////////////////////////////
349 ////////////////////////////////////////////////////////////////////////
352 FGColor::merge(const SGPropertyNode *node)
358 const SGPropertyNode * n;
359 if ((n = node->getNode("red")))
360 _red = n->getFloatValue(), dirty = true;
361 if ((n = node->getNode("green")))
362 _green = n->getFloatValue(), dirty = true;
363 if ((n = node->getNode("blue")))
364 _blue = n->getFloatValue(), dirty = true;
365 if ((n = node->getNode("alpha")))
366 _alpha = n->getFloatValue(), dirty = true;
371 FGColor::merge(const FGColor *color)
374 if (color && color->_red >= 0.0)
375 _red = color->_red, dirty = true;
376 if (color && color->_green >= 0.0)
377 _green = color->_green, dirty = true;
378 if (color && color->_blue >= 0.0)
379 _blue = color->_blue, dirty = true;
380 if (color && color->_alpha >= 0.0)
381 _alpha = color->_alpha, dirty = true;
388 ////////////////////////////////////////////////////////////////////////
389 // FGFontCache class.
390 ////////////////////////////////////////////////////////////////////////
392 static const struct {
396 { "default", &FONT_HELVETICA_14 },
397 { "FIXED_8x13", &PUFONT_8_BY_13 },
398 { "FIXED_9x15", &PUFONT_9_BY_15 },
399 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
400 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
401 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
402 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
403 { "HELVETICA_14", &FONT_HELVETICA_14 },
404 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
405 { "SANS_12B", &FONT_SANS_12B },
410 FGFontCache::FGFontCache() :
415 FGFontCache::~FGFontCache()
417 map<const string, fnt *>::iterator it, end = _fonts.end();
418 for (it = _fonts.begin(); it != end; ++it)
422 struct FGFontCache::fnt *
423 FGFontCache::getfnt(const char *name, float size, float slant)
426 char *envp = ::getenv("FG_FONTS");
430 _path.set(globals->get_fg_root());
431 _path.append("Fonts");
434 for (int i = 0; guifonts[i].name; i++)
435 _fonts[guifonts[i].name] = new fnt(guifonts[i].font);
441 if ((it = _fonts.find(name)) != _fonts.end())
448 f->texfont = new fntTexFont;
450 if (f->texfont->load((char *)path.c_str())) {
451 f->pufont = new puFont;
452 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
453 return _fonts[name] = f;
457 return _fonts["default"];
461 FGFontCache::get(const char *name, float size, float slant)
463 return getfnt(name, size, slant)->pufont;
467 FGFontCache::getTexFont(const char *name, float size, float slant)
469 return getfnt(name, size, slant)->texfont;
473 FGFontCache::get(SGPropertyNode *node)
476 return get("Helvetica.txf", 15.0, 0.0);
478 const char *name = node->getStringValue("name", "Helvetica.txf");
479 float size = node->getFloatValue("size", 15.0);
480 float slant = node->getFloatValue("slant", 0.0);
482 return get(name, size, slant);
485 // end of new_gui.cxx