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),
38 _dialog_props.clear();
47 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
48 ulMakePath(path2, path1, "dialogs");
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::getDialog (const string &name)
171 if(_dialog_props.find(name) != _dialog_props.end())
172 return _dialog_props[name];
174 SG_LOG(SG_GENERAL, SG_ALERT, "dialog '" << name << "' missing");
179 NewGUI::setActiveDialog (FGDialog * dialog)
181 _active_dialog = dialog;
185 NewGUI::getActiveDialog ()
187 return _active_dialog;
191 NewGUI::getMenuBar ()
197 NewGUI::getMenuBarVisible () const
199 return _menubar->isVisible();
203 NewGUI::setMenuBarVisible (bool visible)
212 test_extension (const char * path, const char * ext)
214 int pathlen = strlen(path);
215 int extlen = strlen(ext);
217 for (int i = 1; i <= pathlen && i <= extlen; i++) {
218 if (path[pathlen-i] != ext[extlen-i])
225 NewGUI::newDialog (SGPropertyNode* props)
227 const char* cname = props->getStringValue("name");
229 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
233 if(_active_dialogs.find(name) == _active_dialogs.end())
234 _dialog_props[name] = props;
238 NewGUI::readDir (const char * path)
240 ulDir * dir = ulOpenDir(path);
243 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
248 for (ulDirEnt * dirEnt = ulReadDir(dir);
250 dirEnt = ulReadDir(dir)) {
254 ulMakePath(subpath, path, dirEnt->d_name);
256 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
257 SGPropertyNode * props = new SGPropertyNode;
259 readProperties(subpath, props);
260 } catch (const sg_exception &) {
261 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
266 SGPropertyNode *nameprop = props->getNode("name");
268 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
269 << " has no name; skipping.");
273 string name = nameprop->getStringValue();
274 if (_dialog_props[name])
275 delete (SGPropertyNode *)_dialog_props[name];
277 _dialog_props[name] = props;
285 ////////////////////////////////////////////////////////////////////////
287 ////////////////////////////////////////////////////////////////////////
290 NewGUI::setStyle (void)
293 for (it = _colors.begin(); it != _colors.end(); ++it)
297 // set up the traditional colors as default
298 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
299 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
300 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
301 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
302 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
303 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
304 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
306 //puSetDefaultStyle();
308 int which = fgGetInt("/sim/current-gui", 0);
309 SGPropertyNode *sim = globals->get_props()->getNode("sim");
310 SGPropertyNode *n = sim->getChild("gui", which);
312 n = sim->getChild("gui", 0, true);
314 setupFont(n->getNode("font", true));
315 n = n->getNode("colors", true);
317 for (int i = 0; i < n->nChildren(); i++) {
318 SGPropertyNode *child = n->getChild(i);
319 _colors[child->getName()] = new FGColor(child);
322 FGColor *c = _colors["background"];
323 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
329 static const struct {
333 { "default", &FONT_HELVETICA_14 },
334 { "FIXED_8x13", &PUFONT_8_BY_13 },
335 { "FIXED_9x15", &PUFONT_9_BY_15 },
336 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
337 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
338 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
339 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
340 { "HELVETICA_14", &FONT_HELVETICA_14 },
341 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
342 { "SANS_12B", &FONT_SANS_12B },
347 NewGUI::setupFont (SGPropertyNode *node)
349 string fontname = node->getStringValue("name", "Helvetica.txf");
350 float size = node->getFloatValue("size", 15.0);
351 float slant = node->getFloatValue("slant", 0.0);
354 for (i = 0; guifonts[i].name; i++)
355 if (fontname == guifonts[i].name)
357 if (guifonts[i].name)
358 _font = *guifonts[i].font;
361 char* envp = ::getenv("FG_FONTS");
365 fontpath.set(globals->get_fg_root());
366 fontpath.append("Fonts");
369 SGPath path(fontpath);
370 path.append(fontname);
372 if (_tex_font.load((char *)path.c_str())) {
373 _font.initialize((fntFont *)&_tex_font, size, slant);
375 _font = *guifonts[0].font;
376 fontname = "default";
379 puSetDefaultFonts(_font, _font);
380 node->setStringValue("name", fontname.c_str());
386 ////////////////////////////////////////////////////////////////////////
388 ////////////////////////////////////////////////////////////////////////
391 FGColor::merge(const SGPropertyNode *node)
397 const SGPropertyNode * n;
398 if ((n = node->getNode("red")))
399 _red = n->getFloatValue(), dirty = true;
400 if ((n = node->getNode("green")))
401 _green = n->getFloatValue(), dirty = true;
402 if ((n = node->getNode("blue")))
403 _blue = n->getFloatValue(), dirty = true;
404 if ((n = node->getNode("alpha")))
405 _alpha = n->getFloatValue(), dirty = true;
410 FGColor::merge(const FGColor *color)
413 if (color && color->_red >= 0.0)
414 _red = color->_red, dirty = true;
415 if (color && color->_green >= 0.0)
416 _green = color->_green, dirty = true;
417 if (color && color->_blue >= 0.0)
418 _blue = color->_blue, dirty = true;
419 if (color && color->_alpha >= 0.0)
420 _alpha = color->_alpha, dirty = true;
426 FGFontCache::FGFontCache()
428 char *envp = ::getenv("FG_FONTS");
432 _path.set(globals->get_fg_root());
433 _path.append("Fonts");
436 for (int i=0; guifonts[i].name; i++)
437 _fonts[guifonts[i].name] = guifonts[i].font;
440 FGFontCache::~FGFontCache()
446 FGFontCache::get(const char *name, float size, float slant)
451 if ((it = _fonts.find(name)) == _fonts.end())
457 if (tex_font.load((char *)path.c_str()))
460 font->initialize((fntFont *)&tex_font, size, slant);
465 font = _fonts["default"];
466 // puSetDefaultFonts(font, font);
478 FGFontCache::get(SGPropertyNode *node)
480 const char *name = node->getStringValue("name", "Helvetica.txf");
481 float size = node->getFloatValue("size", 15.0);
482 float slant = node->getFloatValue("slant", 0.0);
484 return get(name, size, slant);
487 // end of new_gui.cxx