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),
48 ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
49 ulMakePath(path2, path1, "dialogs");
57 map<string,FGDialog *>::iterator iter;
59 // close all open dialogs and remember them ...
60 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); iter++) {
61 dlg.push_back(iter->first);
62 closeDialog(iter->first);
68 _menubar = new FGMenuBar;
72 // open remembered dialogs again (no nasal generated ones, unfortunately)
73 // for (unsigned int i = 0; i < dlg.size(); i++)
74 // showDialog(dlg[i]);
80 fgTie("/sim/menubar/visibility", this,
81 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
87 fgUntie("/sim/menubar/visibility");
91 NewGUI::update (double delta_time_sec)
93 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
94 for(/**/; iter != _active_dialogs.end(); iter++)
95 iter->second->update();
99 NewGUI::showDialog (const string &name)
101 if (_dialog_props.find(name) == _dialog_props.end()) {
102 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
105 if(!_active_dialogs[name])
106 _active_dialogs[name] = new FGDialog(_dialog_props[name]);
112 NewGUI::closeActiveDialog ()
114 if (_active_dialog == 0)
117 // Kill any entries in _active_dialogs... Is there an STL
118 // algorithm to do (delete map entries by value, not key)? I hate
120 map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
121 for(/**/; iter != _active_dialogs.end(); iter++) {
122 if(iter->second == _active_dialog) {
123 _active_dialogs.erase(iter);
124 // iter is no longer valid
129 delete _active_dialog;
135 NewGUI::closeDialog (const string& name)
137 if(_active_dialogs.find(name) != _active_dialogs.end()) {
138 if(_active_dialog == _active_dialogs[name])
140 delete _active_dialogs[name];
141 _active_dialogs.erase(name);
144 return false; // dialog wasn't open...
148 NewGUI::setActiveDialog (FGDialog * dialog)
150 _active_dialog = dialog;
154 NewGUI::getActiveDialog ()
156 return _active_dialog;
160 NewGUI::getMenuBar ()
166 NewGUI::getMenuBarVisible () const
168 return _menubar->isVisible();
172 NewGUI::setMenuBarVisible (bool visible)
185 _dialog_props.clear();
189 test_extension (const char * path, const char * ext)
191 int pathlen = strlen(path);
192 int extlen = strlen(ext);
194 for (int i = 1; i <= pathlen && i <= extlen; i++) {
195 if (path[pathlen-i] != ext[extlen-i])
202 NewGUI::newDialog (SGPropertyNode* props)
204 const char* cname = props->getStringValue("name");
206 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
210 if(!_active_dialogs[name])
211 _dialog_props[name] = props;
215 NewGUI::readDir (const char * path)
217 ulDir * dir = ulOpenDir(path);
220 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
225 for (ulDirEnt * dirEnt = ulReadDir(dir);
227 dirEnt = ulReadDir(dir)) {
231 ulMakePath(subpath, path, dirEnt->d_name);
233 if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
234 SGPropertyNode * props = new SGPropertyNode;
236 readProperties(subpath, props);
237 } catch (const sg_exception &) {
238 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
243 SGPropertyNode *nameprop = props->getNode("name");
245 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
246 << " has no name; skipping.");
250 string name = nameprop->getStringValue();
251 if (_dialog_props[name])
252 delete (SGPropertyNode *)_dialog_props[name];
254 _dialog_props[name] = props;
262 ////////////////////////////////////////////////////////////////////////
264 ////////////////////////////////////////////////////////////////////////
267 NewGUI::setStyle (void)
271 // set up the traditional colors as default
272 _colors["background"] = FGColor(0.8f, 0.8f, 0.9f, 0.85f);
273 _colors["foreground"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
274 _colors["highlight"] = FGColor(0.7f, 0.7f, 0.7f, 1.0f);
275 _colors["label"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
276 _colors["legend"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
277 _colors["misc"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f);
279 //puSetDefaultStyle();
281 string path = fgGetString("/sim/current-gui", "/sim/gui");
285 setupFont(fgGetNode(p.c_str(), true));
287 p = path + "/colors";
288 SGPropertyNode *n = fgGetNode(p.c_str(), true);
290 for (int i = 0; i < n->nChildren(); i++) {
291 SGPropertyNode *child = n->getChild(i);
292 _colors[child->getName()] = FGColor(child);
295 FGColor c = _colors["background"];
296 puSetDefaultColourScheme(c.red(), c.green(), c.blue(), c.alpha());
302 static const struct {
306 "default", &FONT_HELVETICA_14,
307 "FIXED_8x13", &PUFONT_8_BY_13,
308 "FIXED_9x15", &PUFONT_9_BY_15,
309 "TIMES_10", &PUFONT_TIMES_ROMAN_10,
310 "TIMES_24", &PUFONT_TIMES_ROMAN_24,
311 "HELVETICA_10", &PUFONT_HELVETICA_10,
312 "HELVETICA_12", &PUFONT_HELVETICA_12,
313 "HELVETICA_14", &FONT_HELVETICA_14,
314 "HELVETICA_18", &PUFONT_HELVETICA_18,
315 "SANS_12B", &FONT_SANS_12B,
320 NewGUI::setupFont (SGPropertyNode *node)
322 string fontname = node->getStringValue("name", "Helvetica.txf");
323 float size = node->getFloatValue("size", 15.0);
324 float slant = node->getFloatValue("slant", 0.0);
327 for (i = 0; guifonts[i].name; i++)
328 if (fontname == guifonts[i].name)
330 if (guifonts[i].name)
331 _font = *guifonts[i].font;
334 char* envp = ::getenv("FG_FONTS");
338 fontpath.set(globals->get_fg_root());
339 fontpath.append("Fonts");
342 SGPath path(fontpath);
343 path.append(fontname);
345 if (_tex_font.load((char *)path.c_str())) {
346 _font.initialize((fntFont *)&_tex_font, size, slant);
348 _font = *guifonts[0].font;
349 fontname = "default";
352 puSetDefaultFonts(_font, _font);
353 node->setStringValue("name", fontname.c_str());
359 ////////////////////////////////////////////////////////////////////////
361 ////////////////////////////////////////////////////////////////////////
364 FGColor::merge(const SGPropertyNode *node)
370 const SGPropertyNode * n;
371 if ((n = node->getNode("red")))
372 _red = n->getFloatValue(), dirty = true;
373 if ((n = node->getNode("green")))
374 _green = n->getFloatValue(), dirty = true;
375 if ((n = node->getNode("blue")))
376 _blue = n->getFloatValue(), dirty = true;
377 if ((n = node->getNode("alpha")))
378 _alpha = n->getFloatValue(), dirty = true;
383 FGColor::merge(const FGColor& color)
386 if (color._red >= 0.0)
387 _red = color._red, dirty = true;
388 if (color._green >= 0.0)
389 _green = color._green, dirty = true;
390 if (color._blue >= 0.0)
391 _blue = color._blue, dirty = true;
392 if (color._alpha >= 0.0)
393 _alpha = color._alpha, dirty = true;
397 // end of new_gui.cxx