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 : _font(FONT_HELVETICA_14),
30 _menubar(new FGMenuBar),
46 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
47 ulMakePath(path2, path1, "dialogs");
55 map<string,FGDialog *>::iterator iter;
57 // close all open dialogs and remember them ...
58 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); iter++) {
59 dlg.push_back(iter->first);
60 closeDialog(iter->first);
66 _menubar = new FGMenuBar;
70 // open remembered dialogs again (no nasal generated ones, unfortunately)
71 // for (unsigned int i = 0; i < dlg.size(); i++)
72 // showDialog(dlg[i]);
78 fgTie("/sim/menubar/visibility", this,
79 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
85 fgUntie("/sim/menubar/visibility");
89 NewGUI::update (double delta_time_sec)
91 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
92 for(/**/; iter != _active_dialogs.end(); iter++)
93 iter->second->update();
97 NewGUI::showDialog (const string &name)
99 if (_dialog_props.find(name) == _dialog_props.end()) {
100 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
103 if(!_active_dialogs[name])
104 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
110 NewGUI::closeActiveDialog ()
112 if (_active_dialog == 0)
115 // Kill any entries in _active_dialogs... Is there an STL
116 // algorithm to do (delete map entries by value, not key)? I hate
118 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
119 for(/**/; iter != _active_dialogs.end(); iter++) {
120 if(iter->second == _active_dialog) {
121 _active_dialogs.erase(iter);
122 // iter is no longer valid
127 delete _active_dialog;
133 NewGUI::closeDialog (const string& name)
135 if(_active_dialogs.find(name) != _active_dialogs.end()) {
136 if(_active_dialog == _active_dialogs[name])
138 delete _active_dialogs[name];
139 _active_dialogs.erase(name);
142 return false; // dialog wasn't open...
146 NewGUI::getDialog (const string &name)
148 if(_dialog_props.find(name) != _dialog_props.end())
149 return _dialog_props[name];
155 NewGUI::setActiveDialog (FGDialog * dialog)
157 _active_dialog = dialog;
161 NewGUI::getActiveDialog ()
163 return _active_dialog;
167 NewGUI::getMenuBar ()
173 NewGUI::getMenuBarVisible () const
175 return _menubar->isVisible();
179 NewGUI::setMenuBarVisible (bool visible)
192 _dialog_props.clear();
194 for (it = _colors.begin(); it != _colors.end(); ++it)
200 test_extension (const char * path, const char * ext)
202 int pathlen = strlen(path);
203 int extlen = strlen(ext);
205 for (int i = 1; i <= pathlen && i <= extlen; i++) {
206 if (path[pathlen-i] != ext[extlen-i])
213 NewGUI::newDialog (SGPropertyNode* props)
215 const char* cname = props->getStringValue("name");
217 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
221 if(!_active_dialogs[name])
222 _dialog_props[name] = props;
226 NewGUI::readDir (const char * path)
228 ulDir * dir = ulOpenDir(path);
231 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
236 for (ulDirEnt * dirEnt = ulReadDir(dir);
238 dirEnt = ulReadDir(dir)) {
242 ulMakePath(subpath, path, dirEnt->d_name);
244 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
245 SGPropertyNode * props = new SGPropertyNode;
247 readProperties(subpath, props);
248 } catch (const sg_exception &) {
249 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
254 SGPropertyNode *nameprop = props->getNode("name");
256 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
257 << " has no name; skipping.");
261 string name = nameprop->getStringValue();
262 if (_dialog_props[name])
263 delete (SGPropertyNode *)_dialog_props[name];
265 _dialog_props[name] = props;
273 ////////////////////////////////////////////////////////////////////////
275 ////////////////////////////////////////////////////////////////////////
278 NewGUI::setStyle (void)
281 for (it = _colors.begin(); it != _colors.end(); ++it)
285 // set up the traditional colors as default
286 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
287 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
288 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
289 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
290 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
291 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
293 //puSetDefaultStyle();
295 int which = fgGetInt("/sim/current-gui", 0);
296 SGPropertyNode *sim = globals->get_props()->getNode("sim");
297 SGPropertyNode *n = sim->getChild("gui", which);
299 n = sim->getChild("gui", 0, true);
301 setupFont(n->getNode("font", true));
302 n = n->getNode("colors", true);
304 for (int i = 0; i < n->nChildren(); i++) {
305 SGPropertyNode *child = n->getChild(i);
306 _colors[child->getName()] = new FGColor(child);
309 FGColor *c = _colors["background"];
310 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
316 static const struct {
320 { "default", &FONT_HELVETICA_14 },
321 { "FIXED_8x13", &PUFONT_8_BY_13 },
322 { "FIXED_9x15", &PUFONT_9_BY_15 },
323 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
324 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
325 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
326 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
327 { "HELVETICA_14", &FONT_HELVETICA_14 },
328 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
329 { "SANS_12B", &FONT_SANS_12B },
334 NewGUI::setupFont (SGPropertyNode *node)
336 string fontname = node->getStringValue("name", "Helvetica.txf");
337 float size = node->getFloatValue("size", 15.0);
338 float slant = node->getFloatValue("slant", 0.0);
341 for (i = 0; guifonts[i].name; i++)
342 if (fontname == guifonts[i].name)
344 if (guifonts[i].name)
345 _font = *guifonts[i].font;
348 char* envp = ::getenv("FG_FONTS");
352 fontpath.set(globals->get_fg_root());
353 fontpath.append("Fonts");
356 SGPath path(fontpath);
357 path.append(fontname);
359 if (_tex_font.load((char *)path.c_str())) {
360 _font.initialize((fntFont *)&_tex_font, size, slant);
362 _font = *guifonts[0].font;
363 fontname = "default";
366 puSetDefaultFonts(_font, _font);
367 node->setStringValue("name", fontname.c_str());
373 ////////////////////////////////////////////////////////////////////////
375 ////////////////////////////////////////////////////////////////////////
378 FGColor::merge(const SGPropertyNode *node)
384 const SGPropertyNode * n;
385 if ((n = node->getNode("red")))
386 _red = n->getFloatValue(), dirty = true;
387 if ((n = node->getNode("green")))
388 _green = n->getFloatValue(), dirty = true;
389 if ((n = node->getNode("blue")))
390 _blue = n->getFloatValue(), dirty = true;
391 if ((n = node->getNode("alpha")))
392 _alpha = n->getFloatValue(), dirty = true;
397 FGColor::merge(const FGColor *color)
400 if (color && color->_red >= 0.0)
401 _red = color->_red, dirty = true;
402 if (color && color->_green >= 0.0)
403 _green = color->_green, dirty = true;
404 if (color && color->_blue >= 0.0)
405 _blue = color->_blue, dirty = true;
406 if (color && color->_alpha >= 0.0)
407 _alpha = color->_alpha, dirty = true;
413 FGFontCache::FGFontCache()
415 char *envp = ::getenv("FG_FONTS");
419 _path.set(globals->get_fg_root());
420 _path.append("Fonts");
423 for (int i=0; guifonts[i].name; i++)
424 _fonts[guifonts[i].name] = guifonts[i].font;
427 FGFontCache::~FGFontCache()
433 FGFontCache::get(const char *name, float size, float slant)
438 if ((it = _fonts.find(name)) == _fonts.end())
444 if (tex_font.load((char *)path.c_str()))
447 font->initialize((fntFont *)&tex_font, size, slant);
452 font = _fonts["default"];
453 // puSetDefaultFonts(font, font);
465 FGFontCache::get(SGPropertyNode *node)
467 const char *name = node->getStringValue("name", "Helvetica.txf");
468 float size = node->getFloatValue("size", 15.0);
469 float slant = node->getFloatValue("slant", 0.0);
471 return get(name, size, slant);
474 // end of new_gui.cxx