]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.cxx
339cc92a9016ae1bfa7307c833514206d1d76800
[flightgear.git] / src / GUI / new_gui.cxx
1 // new_gui.cxx: implementation of XML-configurable GUI support.
2
3 #include "new_gui.hxx"
4
5 #include <plib/pu.h>
6 #include <plib/ul.h>
7
8 #include <simgear/compiler.h>
9 #include <simgear/structure/exception.hxx>
10
11 #include <Main/fg_props.hxx>
12
13 #include "menubar.hxx"
14 #include "dialog.hxx"
15
16 extern puFont FONT_HELVETICA_14;
17 extern puFont FONT_SANS_12B;
18
19
20
21 \f
22 ////////////////////////////////////////////////////////////////////////
23 // Implementation of NewGUI.
24 ////////////////////////////////////////////////////////////////////////
25
26
27
28 NewGUI::NewGUI ()
29     : _font(FONT_HELVETICA_14),
30       _menubar(new FGMenuBar),
31       _active_dialog(0)
32 {
33 }
34
35 NewGUI::~NewGUI ()
36 {
37     clear();
38 }
39
40 void
41 NewGUI::init ()
42 {
43     setStyle();
44     char path1[1024];
45     char path2[1024];
46     ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
47     ulMakePath(path2, path1, "dialogs");
48     readDir(path2);
49     _menubar->init();
50 }
51
52 void
53 NewGUI::reinit ()
54 {
55     map<string,FGDialog *>::iterator iter;
56     vector<string> dlg;
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
61     unsigned int i;
62     for (i = 0; i < dlg.size(); i++)
63         closeDialog(dlg[i]);
64
65     unbind();
66     clear();
67     setStyle();
68     delete _menubar;
69     _menubar = new FGMenuBar;
70     init();
71     bind();
72
73     // open remembered dialogs again (no nasal generated ones, unfortunately)
74     for (i = 0; i < dlg.size(); i++)
75         showDialog(dlg[i]);
76 }
77
78 void
79 NewGUI::bind ()
80 {
81     fgTie("/sim/menubar/visibility", this,
82           &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
83 }
84
85 void
86 NewGUI::unbind ()
87 {
88     fgUntie("/sim/menubar/visibility");
89 }
90
91 void
92 NewGUI::update (double delta_time_sec)
93 {
94     map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
95     for(/**/; iter != _active_dialogs.end(); iter++)
96         iter->second->update();
97 }
98
99 bool
100 NewGUI::showDialog (const string &name)
101 {
102     if (_dialog_props.find(name) == _dialog_props.end()) {
103         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
104         return false;
105     } else {
106         if(!_active_dialogs[name])
107             _active_dialogs[name] = new FGDialog(_dialog_props[name]);
108         return true;
109     }
110 }
111
112 bool
113 NewGUI::closeActiveDialog ()
114 {
115     if (_active_dialog == 0)
116         return false;
117
118     // Kill any entries in _active_dialogs...  Is there an STL
119     // algorithm to do (delete map entries by value, not key)?  I hate
120     // the STL :) -Andy
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
126             break;
127         }
128     }
129
130     delete _active_dialog;
131     _active_dialog = 0;
132     return true;
133 }
134
135 bool
136 NewGUI::closeDialog (const string& name)
137 {
138     if(_active_dialogs.find(name) != _active_dialogs.end()) {
139         if(_active_dialog == _active_dialogs[name])
140             _active_dialog = 0;
141         delete _active_dialogs[name];
142         _active_dialogs.erase(name);
143         return true;
144     }
145     return false; // dialog wasn't open...
146 }
147
148 SGPropertyNode_ptr
149 NewGUI::getDialog (const string &name)
150 {
151     if(_dialog_props.find(name) != _dialog_props.end())
152         return _dialog_props[name];
153
154     SG_LOG(SG_GENERAL, SG_ALERT, "dialog '" << name << "' missing");
155     return 0;
156 }
157
158 void
159 NewGUI::setActiveDialog (FGDialog * dialog)
160 {
161     _active_dialog = dialog;
162 }
163
164 FGDialog *
165 NewGUI::getActiveDialog ()
166 {
167     return _active_dialog;
168 }
169
170 FGMenuBar *
171 NewGUI::getMenuBar ()
172 {
173     return _menubar;
174 }
175
176 bool
177 NewGUI::getMenuBarVisible () const
178 {
179     return _menubar->isVisible();
180 }
181
182 void
183 NewGUI::setMenuBarVisible (bool visible)
184 {
185     if (visible)
186         _menubar->show();
187     else
188         _menubar->hide();
189 }
190
191 void
192 NewGUI::clear ()
193 {
194     delete _menubar;
195     _menubar = 0;
196     _dialog_props.clear();
197     _itt_t it;
198     for (it = _colors.begin(); it != _colors.end(); ++it)
199       delete it->second;
200     _colors.clear();
201 }
202
203 static bool
204 test_extension (const char * path, const char * ext)
205 {
206     int pathlen = strlen(path);
207     int extlen = strlen(ext);
208
209     for (int i = 1; i <= pathlen && i <= extlen; i++) {
210         if (path[pathlen-i] != ext[extlen-i])
211             return false;
212     }
213     return true;
214 }
215
216 void
217 NewGUI::newDialog (SGPropertyNode* props)
218 {
219     const char* cname = props->getStringValue("name");
220     if(!cname) {
221         SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
222         return;
223     }
224     string name = cname;
225     if(_active_dialogs.find(name) == _active_dialogs.end())
226         _dialog_props[name] = props;
227 }
228
229 void
230 NewGUI::readDir (const char * path)
231 {
232     ulDir * dir = ulOpenDir(path);
233
234     if (dir == 0) {
235         SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
236                << path);
237         return;
238     }
239
240     for (ulDirEnt * dirEnt = ulReadDir(dir);
241          dirEnt != 0;
242          dirEnt = ulReadDir(dir)) {
243
244         char subpath[1024];
245
246         ulMakePath(subpath, path, dirEnt->d_name);
247
248         if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
249             SGPropertyNode * props = new SGPropertyNode;
250             try {
251                 readProperties(subpath, props);
252             } catch (const sg_exception &) {
253                 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
254                        << subpath);
255                 delete props;
256                 continue;
257             }
258             SGPropertyNode *nameprop = props->getNode("name");
259             if (!nameprop) {
260                 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
261                    << " has no name; skipping.");
262                 delete props;
263                 continue;
264             }
265             string name = nameprop->getStringValue();
266             if (_dialog_props[name])
267                 delete (SGPropertyNode *)_dialog_props[name];
268
269             _dialog_props[name] = props;
270         }
271     }
272     ulCloseDir(dir);
273 }
274
275
276 \f
277 ////////////////////////////////////////////////////////////////////////
278 // Style handling.
279 ////////////////////////////////////////////////////////////////////////
280
281 void
282 NewGUI::setStyle (void)
283 {
284     _itt_t it;
285     for (it = _colors.begin(); it != _colors.end(); ++it)
286       delete it->second;
287     _colors.clear();
288
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);
296
297     //puSetDefaultStyle();
298
299     int which = fgGetInt("/sim/current-gui", 0);
300     SGPropertyNode *sim = globals->get_props()->getNode("sim");
301     SGPropertyNode *n = sim->getChild("gui", which);
302     if (!n)
303         n = sim->getChild("gui", 0, true);
304
305     setupFont(n->getNode("font", true));
306     n = n->getNode("colors", true);
307
308     for (int i = 0; i < n->nChildren(); i++) {
309         SGPropertyNode *child = n->getChild(i);
310         _colors[child->getName()] = new FGColor(child);
311     }
312
313     FGColor *c = _colors["background"];
314     puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
315 }
316
317
318
319
320 static const struct {
321     char *name;
322     puFont *font;
323 } guifonts[] = {
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 },
334     { 0, 0 }
335 };
336
337 void
338 NewGUI::setupFont (SGPropertyNode *node)
339 {
340     string fontname = node->getStringValue("name", "Helvetica.txf");
341     float size = node->getFloatValue("size", 15.0);
342     float slant = node->getFloatValue("slant", 0.0);
343
344     int i;
345     for (i = 0; guifonts[i].name; i++)
346         if (fontname == guifonts[i].name)
347             break;
348     if (guifonts[i].name)
349         _font = *guifonts[i].font;
350     else {
351         SGPath fontpath;
352         char* envp = ::getenv("FG_FONTS");
353         if (envp != NULL) {
354             fontpath.set(envp);
355         } else {
356             fontpath.set(globals->get_fg_root());
357             fontpath.append("Fonts");
358         }
359
360         SGPath path(fontpath);
361         path.append(fontname);
362
363         if (_tex_font.load((char *)path.c_str())) {
364             _font.initialize((fntFont *)&_tex_font, size, slant);
365         } else {
366             _font = *guifonts[0].font;
367             fontname = "default";
368         }
369     }
370     puSetDefaultFonts(_font, _font);
371     node->setStringValue("name", fontname.c_str());
372 }
373
374
375
376 \f
377 ////////////////////////////////////////////////////////////////////////
378 // FGColor class.
379 ////////////////////////////////////////////////////////////////////////
380
381 bool
382 FGColor::merge(const SGPropertyNode *node)
383 {
384     if (!node)
385         return false;
386
387     bool dirty = false;
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;
397     return dirty;
398 }
399
400 bool
401 FGColor::merge(const FGColor *color)
402 {
403     bool dirty = false;
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;
412     return dirty;
413 }
414
415
416 //
417 FGFontCache::FGFontCache()
418 {
419     char *envp = ::getenv("FG_FONTS");
420     if (envp != NULL) {
421         _path.set(envp);
422     } else {
423         _path.set(globals->get_fg_root());
424         _path.append("Fonts");
425     }
426
427     for (int i=0; guifonts[i].name; i++)
428         _fonts[guifonts[i].name] = guifonts[i].font;
429 }
430
431 FGFontCache::~FGFontCache()
432 {
433    _fonts.clear();
434 }
435
436 puFont *
437 FGFontCache::get(const char *name, float size, float slant)
438 {
439     puFont *font;
440     _itt_t it;
441
442     if ((it = _fonts.find(name)) == _fonts.end())
443     {
444         SGPath path(_path);
445         path.append(name);
446
447         fntTexFont tex_font;
448         if (tex_font.load((char *)path.c_str()))
449         {
450             font = new puFont;
451             font->initialize((fntFont *)&tex_font, size, slant);
452             _fonts[name] = font;
453         }
454         else
455         {
456             font = _fonts["default"];
457             // puSetDefaultFonts(font, font);
458         }
459     }
460     else
461     {
462         font = it->second;
463     }
464
465     return font;
466 }
467
468 puFont *
469 FGFontCache::get(SGPropertyNode *node)
470 {
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);
474
475     return get(name, size, slant);
476 }
477
478 // end of new_gui.cxx