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);
62 for (i = 0; i < dlg.size(); i++)
69 _menubar = new FGMenuBar;
73 // open remembered dialogs again (no nasal generated ones, unfortunately)
74 for (i = 0; i < dlg.size(); i++)
81 fgTie("/sim/menubar/visibility", this,
82 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
88 fgUntie("/sim/menubar/visibility");
92 NewGUI::update (double delta_time_sec)
94 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
95 for(/**/; iter != _active_dialogs.end(); iter++)
96 iter->second->update();
100 NewGUI::showDialog (const string &name)
102 if (_dialog_props.find(name) == _dialog_props.end()) {
103 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
106 if(!_active_dialogs[name])
107 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
113 NewGUI::closeActiveDialog ()
115 if (_active_dialog == 0)
118 // Kill any entries in _active_dialogs... Is there an STL
119 // algorithm to do (delete map entries by value, not key)? I hate
121 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
122 for(/**/; iter != _active_dialogs.end(); iter++) {
123 if(iter->second == _active_dialog) {
124 _active_dialogs.erase(iter);
125 // iter is no longer valid
130 delete _active_dialog;
136 NewGUI::closeDialog (const string& name)
138 if(_active_dialogs.find(name) != _active_dialogs.end()) {
139 if(_active_dialog == _active_dialogs[name])
141 delete _active_dialogs[name];
142 _active_dialogs.erase(name);
145 return false; // dialog wasn't open...
149 NewGUI::getDialog (const string &name)
151 if(_dialog_props.find(name) != _dialog_props.end())
152 return _dialog_props[name];
154 SG_LOG(SG_GENERAL, SG_ALERT, "dialog '" << name << "' missing");
159 NewGUI::setActiveDialog (FGDialog * dialog)
161 _active_dialog = dialog;
165 NewGUI::getActiveDialog ()
167 return _active_dialog;
171 NewGUI::getMenuBar ()
177 NewGUI::getMenuBarVisible () const
179 return _menubar->isVisible();
183 NewGUI::setMenuBarVisible (bool visible)
196 _dialog_props.clear();
198 for (it = _colors.begin(); it != _colors.end(); ++it)
204 test_extension (const char * path, const char * ext)
206 int pathlen = strlen(path);
207 int extlen = strlen(ext);
209 for (int i = 1; i <= pathlen && i <= extlen; i++) {
210 if (path[pathlen-i] != ext[extlen-i])
217 NewGUI::newDialog (SGPropertyNode* props)
219 const char* cname = props->getStringValue("name");
221 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
225 if(_active_dialogs.find(name) == _active_dialogs.end())
226 _dialog_props[name] = props;
230 NewGUI::readDir (const char * path)
232 ulDir * dir = ulOpenDir(path);
235 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
240 for (ulDirEnt * dirEnt = ulReadDir(dir);
242 dirEnt = ulReadDir(dir)) {
246 ulMakePath(subpath, path, dirEnt->d_name);
248 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
249 SGPropertyNode * props = new SGPropertyNode;
251 readProperties(subpath, props);
252 } catch (const sg_exception &) {
253 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
258 SGPropertyNode *nameprop = props->getNode("name");
260 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
261 << " has no name; skipping.");
265 string name = nameprop->getStringValue();
266 if (_dialog_props[name])
267 delete (SGPropertyNode *)_dialog_props[name];
269 _dialog_props[name] = props;
277 ////////////////////////////////////////////////////////////////////////
279 ////////////////////////////////////////////////////////////////////////
282 NewGUI::setStyle (void)
285 for (it = _colors.begin(); it != _colors.end(); ++it)
289 // set up the traditional colors as default
290 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
291 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
292 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
293 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
294 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
295 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
297 //puSetDefaultStyle();
299 int which = fgGetInt("/sim/current-gui", 0);
300 SGPropertyNode *sim = globals->get_props()->getNode("sim");
301 SGPropertyNode *n = sim->getChild("gui", which);
303 n = sim->getChild("gui", 0, true);
305 setupFont(n->getNode("font", true));
306 n = n->getNode("colors", true);
308 for (int i = 0; i < n->nChildren(); i++) {
309 SGPropertyNode *child = n->getChild(i);
310 _colors[child->getName()] = new FGColor(child);
313 FGColor *c = _colors["background"];
314 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
320 static const struct {
324 { "default", &FONT_HELVETICA_14 },
325 { "FIXED_8x13", &PUFONT_8_BY_13 },
326 { "FIXED_9x15", &PUFONT_9_BY_15 },
327 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
328 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
329 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
330 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
331 { "HELVETICA_14", &FONT_HELVETICA_14 },
332 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
333 { "SANS_12B", &FONT_SANS_12B },
338 NewGUI::setupFont (SGPropertyNode *node)
340 string fontname = node->getStringValue("name", "Helvetica.txf");
341 float size = node->getFloatValue("size", 15.0);
342 float slant = node->getFloatValue("slant", 0.0);
345 for (i = 0; guifonts[i].name; i++)
346 if (fontname == guifonts[i].name)
348 if (guifonts[i].name)
349 _font = *guifonts[i].font;
352 char* envp = ::getenv("FG_FONTS");
356 fontpath.set(globals->get_fg_root());
357 fontpath.append("Fonts");
360 SGPath path(fontpath);
361 path.append(fontname);
363 if (_tex_font.load((char *)path.c_str())) {
364 _font.initialize((fntFont *)&_tex_font, size, slant);
366 _font = *guifonts[0].font;
367 fontname = "default";
370 puSetDefaultFonts(_font, _font);
371 node->setStringValue("name", fontname.c_str());
377 ////////////////////////////////////////////////////////////////////////
379 ////////////////////////////////////////////////////////////////////////
382 FGColor::merge(const SGPropertyNode *node)
388 const SGPropertyNode * n;
389 if ((n = node->getNode("red")))
390 _red = n->getFloatValue(), dirty = true;
391 if ((n = node->getNode("green")))
392 _green = n->getFloatValue(), dirty = true;
393 if ((n = node->getNode("blue")))
394 _blue = n->getFloatValue(), dirty = true;
395 if ((n = node->getNode("alpha")))
396 _alpha = n->getFloatValue(), dirty = true;
401 FGColor::merge(const FGColor *color)
404 if (color && color->_red >= 0.0)
405 _red = color->_red, dirty = true;
406 if (color && color->_green >= 0.0)
407 _green = color->_green, dirty = true;
408 if (color && color->_blue >= 0.0)
409 _blue = color->_blue, dirty = true;
410 if (color && color->_alpha >= 0.0)
411 _alpha = color->_alpha, dirty = true;
417 FGFontCache::FGFontCache()
419 char *envp = ::getenv("FG_FONTS");
423 _path.set(globals->get_fg_root());
424 _path.append("Fonts");
427 for (int i=0; guifonts[i].name; i++)
428 _fonts[guifonts[i].name] = guifonts[i].font;
431 FGFontCache::~FGFontCache()
437 FGFontCache::get(const char *name, float size, float slant)
442 if ((it = _fonts.find(name)) == _fonts.end())
448 if (tex_font.load((char *)path.c_str()))
451 font->initialize((fntFont *)&tex_font, size, slant);
456 font = _fonts["default"];
457 // puSetDefaultFonts(font, font);
469 FGFontCache::get(SGPropertyNode *node)
471 const char *name = node->getStringValue("name", "Helvetica.txf");
472 float size = node->getFloatValue("size", 15.0);
473 float slant = node->getFloatValue("slant", 0.0);
475 return get(name, size, slant);
478 // end of new_gui.cxx