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"
15 #include "SafeTexFont.hxx"
17 extern puFont FONT_HELVETICA_14;
18 extern puFont FONT_SANS_12B;
23 ////////////////////////////////////////////////////////////////////////
24 // Implementation of NewGUI.
25 ////////////////////////////////////////////////////////////////////////
30 : _menubar(new FGMenuBar),
38 _dialog_props.clear();
39 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
49 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
50 ulMakePath(path2, path1, "dialogs");
59 fgSetBool("/sim/signals/reinit-gui", true);
69 NewGUI::reset (bool reload)
71 map<string,FGDialog *>::iterator iter;
73 // close all open dialogs and remember them ...
74 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
75 dlg.push_back(iter->first);
78 for (i = 0; i < dlg.size(); i++)
85 _menubar = new FGMenuBar;
88 _dialog_props.clear();
97 for (i = 0; i < dlg.size(); i++)
104 fgTie("/sim/menubar/visibility", this,
105 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
111 fgUntie("/sim/menubar/visibility");
115 NewGUI::update (double delta_time_sec)
117 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
118 for(/**/; iter != _active_dialogs.end(); iter++)
119 iter->second->update();
123 NewGUI::showDialog (const string &name)
125 if (_dialog_props.find(name) == _dialog_props.end()) {
126 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
129 if(!_active_dialogs[name])
130 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
136 NewGUI::closeActiveDialog ()
138 if (_active_dialog == 0)
141 // Kill any entries in _active_dialogs... Is there an STL
142 // algorithm to do (delete map entries by value, not key)? I hate
144 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
145 for(/**/; iter != _active_dialogs.end(); iter++) {
146 if(iter->second == _active_dialog) {
147 _active_dialogs.erase(iter);
148 // iter is no longer valid
153 delete _active_dialog;
159 NewGUI::closeDialog (const string& name)
161 if(_active_dialogs.find(name) != _active_dialogs.end()) {
162 if(_active_dialog == _active_dialogs[name])
164 delete _active_dialogs[name];
165 _active_dialogs.erase(name);
168 return false; // dialog wasn't open...
172 NewGUI::getDialogProperties (const string &name)
174 if(_dialog_props.find(name) != _dialog_props.end())
175 return _dialog_props[name];
177 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
182 NewGUI::getDialog (const string &name)
184 if(_active_dialogs.find(name) != _active_dialogs.end())
185 return _active_dialogs[name];
187 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
192 NewGUI::setActiveDialog (FGDialog * dialog)
194 _active_dialog = dialog;
198 NewGUI::getActiveDialog ()
200 return _active_dialog;
204 NewGUI::getMenuBar ()
210 NewGUI::getMenuBarVisible () const
212 return _menubar->isVisible();
216 NewGUI::setMenuBarVisible (bool visible)
225 test_extension (const char * path, const char * ext)
227 int pathlen = strlen(path);
228 int extlen = strlen(ext);
230 for (int i = 1; i <= pathlen && i <= extlen; i++) {
231 if (path[pathlen-i] != ext[extlen-i])
238 NewGUI::newDialog (SGPropertyNode* props)
240 const char* cname = props->getStringValue("name");
242 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
246 if(_active_dialogs.find(name) == _active_dialogs.end())
247 _dialog_props[name] = props;
251 NewGUI::readDir (const char * path)
253 ulDir * dir = ulOpenDir(path);
256 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
261 for (ulDirEnt * dirEnt = ulReadDir(dir);
263 dirEnt = ulReadDir(dir)) {
267 ulMakePath(subpath, path, dirEnt->d_name);
269 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
270 SGPropertyNode * props = new SGPropertyNode;
272 readProperties(subpath, props);
273 } catch (const sg_exception &) {
274 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
279 SGPropertyNode *nameprop = props->getNode("name");
281 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
282 << " has no name; skipping.");
286 string name = nameprop->getStringValue();
287 if (_dialog_props[name])
288 delete (SGPropertyNode *)_dialog_props[name];
290 _dialog_props[name] = props;
298 ////////////////////////////////////////////////////////////////////////
300 ////////////////////////////////////////////////////////////////////////
303 NewGUI::setStyle (void)
306 for (it = _colors.begin(); it != _colors.end(); ++it)
310 // set up the traditional colors as default
311 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
312 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
313 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
314 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
315 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
316 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
317 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
319 //puSetDefaultStyle();
321 int which = fgGetInt("/sim/gui/current-style", 0);
322 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
323 SGPropertyNode *n = sim->getChild("style", which);
325 n = sim->getChild("style", 0, true);
327 setupFont(n->getNode("fonts/gui", true));
328 n = n->getNode("colors", true);
330 for (int i = 0; i < n->nChildren(); i++) {
331 SGPropertyNode *child = n->getChild(i);
332 _colors[child->getName()] = new FGColor(child);
335 FGColor *c = _colors["background"];
336 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
341 NewGUI::setupFont (SGPropertyNode *node)
343 _font = globals->get_fontcache()->get(node);
344 puSetDefaultFonts(*_font, *_font);
351 ////////////////////////////////////////////////////////////////////////
353 ////////////////////////////////////////////////////////////////////////
356 FGColor::merge(const SGPropertyNode *node)
362 const SGPropertyNode * n;
363 if ((n = node->getNode("red")))
364 _red = n->getFloatValue(), dirty = true;
365 if ((n = node->getNode("green")))
366 _green = n->getFloatValue(), dirty = true;
367 if ((n = node->getNode("blue")))
368 _blue = n->getFloatValue(), dirty = true;
369 if ((n = node->getNode("alpha")))
370 _alpha = n->getFloatValue(), dirty = true;
375 FGColor::merge(const FGColor *color)
378 if (color && color->_red >= 0.0)
379 _red = color->_red, dirty = true;
380 if (color && color->_green >= 0.0)
381 _green = color->_green, dirty = true;
382 if (color && color->_blue >= 0.0)
383 _blue = color->_blue, dirty = true;
384 if (color && color->_alpha >= 0.0)
385 _alpha = color->_alpha, dirty = true;
392 ////////////////////////////////////////////////////////////////////////
393 // FGFontCache class.
394 ////////////////////////////////////////////////////////////////////////
396 static const struct {
400 { "default", &FONT_HELVETICA_14 },
401 { "FIXED_8x13", &PUFONT_8_BY_13 },
402 { "FIXED_9x15", &PUFONT_9_BY_15 },
403 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
404 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
405 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
406 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
407 { "HELVETICA_14", &FONT_HELVETICA_14 },
408 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
409 { "SANS_12B", &FONT_SANS_12B },
414 FGFontCache::FGFontCache() :
419 FGFontCache::~FGFontCache()
421 map<const string, fnt *>::iterator it, end = _fonts.end();
422 for (it = _fonts.begin(); it != end; ++it)
426 struct FGFontCache::fnt *
427 FGFontCache::getfnt(const char *name, float size, float slant)
430 if ((it = _fonts.find(name)) != _fonts.end())
433 SGPath path = getfntpath(name);
436 f->texfont = new flightgear::SafeTexFont;
438 if (f->texfont->load((char *)path.c_str())) {
439 f->pufont = new puFont;
440 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
441 return _fonts[name] = f;
445 return _fonts["default"];
449 FGFontCache::get(const char *name, float size, float slant)
451 return getfnt(name, size, slant)->pufont;
455 FGFontCache::getTexFont(const char *name, float size, float slant)
457 return getfnt(name, size, slant)->texfont;
461 FGFontCache::get(SGPropertyNode *node)
464 return get("Helvetica.txf", 15.0, 0.0);
466 const char *name = node->getStringValue("name", "Helvetica.txf");
467 float size = node->getFloatValue("size", 15.0);
468 float slant = node->getFloatValue("slant", 0.0);
470 return get(name, size, slant);
474 FGFontCache::getfntpath(const char *name)
477 char *envp = ::getenv("FG_FONTS");
481 _path.set(globals->get_fg_root());
482 _path.append("Fonts");
485 for (int i = 0; guifonts[i].name; i++)
486 _fonts[guifonts[i].name] = new fnt(guifonts[i].font);
492 if (name && std::string(name) != "") {
498 path = SGPath(_path);
499 path.append("Helvetica.txf");
504 // end of new_gui.cxx