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"
18 extern puFont FONT_HELVETICA_14;
19 extern puFont FONT_SANS_12B;
24 ////////////////////////////////////////////////////////////////////////
25 // Implementation of NewGUI.
26 ////////////////////////////////////////////////////////////////////////
31 : _font(FONT_HELVETICA_14),
32 _menubar(new FGMenuBar),
35 // set up the traditional colors as default
36 _colors["background"] = FGColor(0.8f, 0.8f, 0.9f, 0.85f);
37 _colors["foreground"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
38 _colors["highlight"] = FGColor(0.7f, 0.7f, 0.7f, 1.0f);
39 _colors["label"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
40 _colors["legend"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
41 _colors["misc"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
56 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
57 ulMakePath(path2, path1, "dialogs");
65 map<string,FGDialog *>::iterator iter;
67 // close all open dialogs and remember them ...
68 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); iter++) {
69 dlg.push_back(iter->first);
70 closeDialog(iter->first);
76 _menubar = new FGMenuBar;
80 // open remembered dialogs again (no nasal generated ones, unfortunately)
81 // for (unsigned int i = 0; i < dlg.size(); i++)
82 // showDialog(dlg[i]);
88 fgTie("/sim/menubar/visibility", this,
89 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
95 fgUntie("/sim/menubar/visibility");
99 NewGUI::update (double delta_time_sec)
101 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
102 for(/**/; iter != _active_dialogs.end(); iter++)
103 iter->second->update();
107 NewGUI::showDialog (const string &name)
109 if (_dialog_props.find(name) == _dialog_props.end()) {
110 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
113 if(!_active_dialogs[name])
114 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
120 NewGUI::closeActiveDialog ()
122 if (_active_dialog == 0)
125 // Kill any entries in _active_dialogs... Is there an STL
126 // algorithm to do (delete map entries by value, not key)? I hate
128 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
129 for(/**/; iter != _active_dialogs.end(); iter++) {
130 if(iter->second == _active_dialog) {
131 _active_dialogs.erase(iter);
132 // iter is no longer valid
137 delete _active_dialog;
143 NewGUI::closeDialog (const string& name)
145 if(_active_dialogs.find(name) != _active_dialogs.end()) {
146 if(_active_dialog == _active_dialogs[name])
148 delete _active_dialogs[name];
149 _active_dialogs.erase(name);
152 return false; // dialog wasn't open...
156 NewGUI::setActiveDialog (FGDialog * dialog)
158 _active_dialog = dialog;
162 NewGUI::getActiveDialog ()
164 return _active_dialog;
168 NewGUI::getMenuBar ()
174 NewGUI::getMenuBarVisible () const
176 return _menubar->isVisible();
180 NewGUI::setMenuBarVisible (bool visible)
193 _dialog_props.clear();
197 test_extension (const char * path, const char * ext)
199 int pathlen = strlen(path);
200 int extlen = strlen(ext);
202 for (int i = 1; i <= pathlen && i <= extlen; i++) {
203 if (path[pathlen-i] != ext[extlen-i])
210 NewGUI::newDialog (SGPropertyNode* props)
212 const char* cname = props->getStringValue("name");
214 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
218 if(!_active_dialogs[name])
219 _dialog_props[name] = props;
223 NewGUI::readDir (const char * path)
225 ulDir * dir = ulOpenDir(path);
228 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
233 for (ulDirEnt * dirEnt = ulReadDir(dir);
235 dirEnt = ulReadDir(dir)) {
239 ulMakePath(subpath, path, dirEnt->d_name);
241 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
242 SGPropertyNode * props = new SGPropertyNode;
244 readProperties(subpath, props);
245 } catch (const sg_exception &) {
246 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
251 SGPropertyNode *nameprop = props->getNode("name");
253 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
254 << " has no name; skipping.");
258 string name = nameprop->getStringValue();
259 if (_dialog_props[name])
260 delete (SGPropertyNode *)_dialog_props[name];
262 _dialog_props[name] = props;
270 ////////////////////////////////////////////////////////////////////////
272 ////////////////////////////////////////////////////////////////////////
275 NewGUI::setStyle (void)
279 //puSetDefaultStyle();
281 SGPropertyNode *n = fgGetNode("/sim/gui/colors");
285 for (int i = 0; i < n->nChildren(); i++) {
286 SGPropertyNode *child = n->getChild(i);
287 _colors[child->getName()] = FGColor(child);
290 FGColor c = _colors["background"];
291 puSetDefaultColourScheme(c.red(), c.green(), c.blue(), c.alpha());
297 static const struct {
301 "default", &FONT_HELVETICA_14,
302 "FIXED_8x13", &PUFONT_8_BY_13,
303 "FIXED_9x15", &PUFONT_9_BY_15,
304 "TIMES_10", &PUFONT_TIMES_ROMAN_10,
305 "TIMES_24", &PUFONT_TIMES_ROMAN_24,
306 "HELVETICA_10", &PUFONT_HELVETICA_10,
307 "HELVETICA_12", &PUFONT_HELVETICA_12,
308 "HELVETICA_14", &FONT_HELVETICA_14,
309 "HELVETICA_18", &PUFONT_HELVETICA_18,
310 "SANS_12B", &FONT_SANS_12B,
317 SGPropertyNode *node = fgGetNode("/sim/gui/font", true);
318 string fontname = node->getStringValue("name", "Helvetica.txf");
319 float size = node->getFloatValue("size", 15.0);
320 float slant = node->getFloatValue("slant", 0.0);
323 for (i = 0; guifonts[i].name; i++)
324 if (fontname == guifonts[i].name)
326 if (guifonts[i].name)
327 _font = *guifonts[i].font;
330 char* envp = ::getenv("FG_FONTS");
334 fontpath.set(globals->get_fg_root());
335 fontpath.append("Fonts");
338 SGPath path(fontpath);
339 path.append(fontname);
341 if (_tex_font.load((char *)path.c_str())) {
342 _font.initialize((fntFont *)&_tex_font, size, slant);
344 _font = *guifonts[0].font;
345 fontname = "default";
348 puSetDefaultFonts(_font, _font);
349 fgSetString("/sim/gui/font", fontname.c_str());
355 ////////////////////////////////////////////////////////////////////////
357 ////////////////////////////////////////////////////////////////////////
360 FGColor::merge(const SGPropertyNode *node)
365 const SGPropertyNode * n;
366 if ((n = node->getNode("red")))
367 _red = n->getFloatValue();
368 if ((n = node->getNode("green")))
369 _green = n->getFloatValue();
370 if ((n = node->getNode("blue")))
371 _blue = n->getFloatValue();
372 if ((n = node->getNode("alpha")))
373 _alpha = n->getFloatValue();
377 FGColor::merge(const FGColor& color)
379 if (color._red >= 0.0)
381 if (color._green >= 0.0)
382 _green = color._green;
383 if (color._blue >= 0.0)
385 if (color._alpha >= 0.0)
386 _alpha = color._alpha;
389 // end of new_gui.cxx