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");
56 fgSetBool("/sim/signals/reinit-gui", true);
66 NewGUI::reset (bool reload)
68 map<string,FGDialog *>::iterator iter;
70 // close all open dialogs and remember them ...
71 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
72 dlg.push_back(iter->first);
75 for (i = 0; i < dlg.size(); i++)
82 _menubar = new FGMenuBar;
85 _dialog_props.clear();
94 for (i = 0; i < dlg.size(); i++)
101 fgTie("/sim/menubar/visibility", this,
102 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
108 fgUntie("/sim/menubar/visibility");
112 NewGUI::update (double delta_time_sec)
114 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
115 for(/**/; iter != _active_dialogs.end(); iter++)
116 iter->second->update();
120 NewGUI::showDialog (const string &name)
122 if (_dialog_props.find(name) == _dialog_props.end()) {
123 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
126 if(!_active_dialogs[name])
127 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
133 NewGUI::closeActiveDialog ()
135 if (_active_dialog == 0)
138 // Kill any entries in _active_dialogs... Is there an STL
139 // algorithm to do (delete map entries by value, not key)? I hate
141 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
142 for(/**/; iter != _active_dialogs.end(); iter++) {
143 if(iter->second == _active_dialog) {
144 _active_dialogs.erase(iter);
145 // iter is no longer valid
150 delete _active_dialog;
156 NewGUI::closeDialog (const string& name)
158 if(_active_dialogs.find(name) != _active_dialogs.end()) {
159 if(_active_dialog == _active_dialogs[name])
161 delete _active_dialogs[name];
162 _active_dialogs.erase(name);
165 return false; // dialog wasn't open...
169 NewGUI::getDialogProperties (const string &name)
171 if(_dialog_props.find(name) != _dialog_props.end())
172 return _dialog_props[name];
174 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
179 NewGUI::getDialog (const string &name)
181 if(_active_dialogs.find(name) != _active_dialogs.end())
182 return _active_dialogs[name];
184 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
189 NewGUI::setActiveDialog (FGDialog * dialog)
191 _active_dialog = dialog;
195 NewGUI::getActiveDialog ()
197 return _active_dialog;
201 NewGUI::getMenuBar ()
207 NewGUI::getMenuBarVisible () const
209 return _menubar->isVisible();
213 NewGUI::setMenuBarVisible (bool visible)
222 test_extension (const char * path, const char * ext)
224 int pathlen = strlen(path);
225 int extlen = strlen(ext);
227 for (int i = 1; i <= pathlen && i <= extlen; i++) {
228 if (path[pathlen-i] != ext[extlen-i])
235 NewGUI::newDialog (SGPropertyNode* props)
237 const char* cname = props->getStringValue("name");
239 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
243 if(_active_dialogs.find(name) == _active_dialogs.end())
244 _dialog_props[name] = props;
248 NewGUI::readDir (const char * path)
250 ulDir * dir = ulOpenDir(path);
253 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
258 for (ulDirEnt * dirEnt = ulReadDir(dir);
260 dirEnt = ulReadDir(dir)) {
264 ulMakePath(subpath, path, dirEnt->d_name);
266 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
267 SGPropertyNode * props = new SGPropertyNode;
269 readProperties(subpath, props);
270 } catch (const sg_exception &) {
271 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
276 SGPropertyNode *nameprop = props->getNode("name");
278 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
279 << " has no name; skipping.");
283 string name = nameprop->getStringValue();
284 if (_dialog_props[name])
285 delete (SGPropertyNode *)_dialog_props[name];
287 _dialog_props[name] = props;
295 ////////////////////////////////////////////////////////////////////////
297 ////////////////////////////////////////////////////////////////////////
300 NewGUI::setStyle (void)
303 for (it = _colors.begin(); it != _colors.end(); ++it)
307 // set up the traditional colors as default
308 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
309 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
310 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
311 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
312 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
313 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
314 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
316 //puSetDefaultStyle();
318 int which = fgGetInt("/sim/gui/current-style", 0);
319 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
320 SGPropertyNode *n = sim->getChild("style", which);
322 n = sim->getChild("style", 0, true);
324 setupFont(n->getNode("fonts/gui", true));
325 n = n->getNode("colors", true);
327 for (int i = 0; i < n->nChildren(); i++) {
328 SGPropertyNode *child = n->getChild(i);
329 _colors[child->getName()] = new FGColor(child);
332 FGColor *c = _colors["background"];
333 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
338 NewGUI::setupFont (SGPropertyNode *node)
340 _font = globals->get_fontcache()->get(node);
341 puSetDefaultFonts(*_font, *_font);
348 ////////////////////////////////////////////////////////////////////////
350 ////////////////////////////////////////////////////////////////////////
353 FGColor::merge(const SGPropertyNode *node)
359 const SGPropertyNode * n;
360 if ((n = node->getNode("red")))
361 _red = n->getFloatValue(), dirty = true;
362 if ((n = node->getNode("green")))
363 _green = n->getFloatValue(), dirty = true;
364 if ((n = node->getNode("blue")))
365 _blue = n->getFloatValue(), dirty = true;
366 if ((n = node->getNode("alpha")))
367 _alpha = n->getFloatValue(), dirty = true;
372 FGColor::merge(const FGColor *color)
375 if (color && color->_red >= 0.0)
376 _red = color->_red, dirty = true;
377 if (color && color->_green >= 0.0)
378 _green = color->_green, dirty = true;
379 if (color && color->_blue >= 0.0)
380 _blue = color->_blue, dirty = true;
381 if (color && color->_alpha >= 0.0)
382 _alpha = color->_alpha, dirty = true;
389 ////////////////////////////////////////////////////////////////////////
390 // FGFontCache class.
391 ////////////////////////////////////////////////////////////////////////
393 static const struct {
397 { "default", &FONT_HELVETICA_14 },
398 { "FIXED_8x13", &PUFONT_8_BY_13 },
399 { "FIXED_9x15", &PUFONT_9_BY_15 },
400 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
401 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
402 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
403 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
404 { "HELVETICA_14", &FONT_HELVETICA_14 },
405 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
406 { "SANS_12B", &FONT_SANS_12B },
411 FGFontCache::FGFontCache() :
416 FGFontCache::~FGFontCache()
418 map<const string, fnt *>::iterator it, end = _fonts.end();
419 for (it = _fonts.begin(); it != end; ++it)
423 struct FGFontCache::fnt *
424 FGFontCache::getfnt(const char *name, float size, float slant)
427 if ((it = _fonts.find(name)) != _fonts.end())
430 SGPath path = getfntpath(name);
433 f->texfont = new fntTexFont;
435 if (f->texfont->load((char *)path.c_str())) {
436 f->pufont = new puFont;
437 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
438 return _fonts[name] = f;
442 return _fonts["default"];
446 FGFontCache::get(const char *name, float size, float slant)
448 return getfnt(name, size, slant)->pufont;
452 FGFontCache::getTexFont(const char *name, float size, float slant)
454 return getfnt(name, size, slant)->texfont;
458 FGFontCache::get(SGPropertyNode *node)
461 return get("Helvetica.txf", 15.0, 0.0);
463 const char *name = node->getStringValue("name", "Helvetica.txf");
464 float size = node->getFloatValue("size", 15.0);
465 float slant = node->getFloatValue("slant", 0.0);
467 return get(name, size, slant);
471 FGFontCache::getfntpath(const char *name)
474 char *envp = ::getenv("FG_FONTS");
478 _path.set(globals->get_fg_root());
479 _path.append("Fonts");
482 for (int i = 0; guifonts[i].name; i++)
483 _fonts[guifonts[i].name] = new fnt(guifonts[i].font);
489 if (name && std::string(name) != "") {
495 path = SGPath(_path);
496 path.append("Helvetica.txf");
501 // end of new_gui.cxx