]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.cxx
Added two missing files from JSBSim.org that were missing in the last sync.
[flightgear.git] / src / GUI / new_gui.cxx
1 // new_gui.cxx: implementation of XML-configurable GUI support.
2 #include <algorithm>
3 #include <iostream>
4 #include <cstring>
5 #include <sys/types.h>
6 #include <plib/ul.h>
7
8 #include "new_gui.hxx"
9
10 #include <plib/pu.h>
11 #include <plib/ul.h>
12
13 #include <simgear/compiler.h>
14 #include <simgear/structure/exception.hxx>
15 #include <simgear/props/props_io.hxx>
16
17 #include <Main/fg_props.hxx>
18
19 #include "menubar.hxx"
20 #include "dialog.hxx"
21
22 extern puFont FONT_HELVETICA_14;
23 extern puFont FONT_SANS_12B;
24
25
26
27 \f
28 ////////////////////////////////////////////////////////////////////////
29 // Implementation of NewGUI.
30 ////////////////////////////////////////////////////////////////////////
31
32
33
34 NewGUI::NewGUI ()
35     : _menubar(new FGMenuBar),
36       _active_dialog(0)
37 {
38 }
39
40 NewGUI::~NewGUI ()
41 {
42     delete _menubar;
43     _dialog_props.clear();
44     for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
45         delete it->second;
46 }
47
48 void
49 NewGUI::init ()
50 {
51     setStyle();
52     char path1[1024];
53     char path2[1024];
54     ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
55     ulMakePath(path2, path1, "dialogs");
56     readDir(path2);
57     _menubar->init();
58 }
59
60 void
61 NewGUI::reinit ()
62 {
63     reset(true);
64     fgSetBool("/sim/signals/reinit-gui", true);
65 }
66
67 void
68 NewGUI::redraw ()
69 {
70     reset(false);
71 }
72
73 void
74 NewGUI::reset (bool reload)
75 {
76     map<string,FGDialog *>::iterator iter;
77     vector<string> dlg;
78     // close all open dialogs and remember them ...
79     for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
80         dlg.push_back(iter->first);
81
82     unsigned int i;
83     for (i = 0; i < dlg.size(); i++)
84         closeDialog(dlg[i]);
85
86     setStyle();
87
88     unbind();
89     delete _menubar;
90     _menubar = new FGMenuBar;
91
92     if (reload) {
93         _dialog_props.clear();
94         init();
95     } else {
96         _menubar->init();
97     }
98
99     bind();
100
101     // open dialogs again
102     for (i = 0; i < dlg.size(); i++)
103         showDialog(dlg[i]);
104 }
105
106 void
107 NewGUI::bind ()
108 {
109     fgTie("/sim/menubar/visibility", this,
110           &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
111 }
112
113 void
114 NewGUI::unbind ()
115 {
116     fgUntie("/sim/menubar/visibility");
117 }
118
119 void
120 NewGUI::update (double delta_time_sec)
121 {
122     map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
123     for(/**/; iter != _active_dialogs.end(); iter++)
124         iter->second->update();
125 }
126
127 bool
128 NewGUI::showDialog (const string &name)
129 {
130     if (_dialog_props.find(name) == _dialog_props.end()) {
131         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
132         return false;
133     } else {
134         if(!_active_dialogs[name])
135             _active_dialogs[name] = new FGDialog(_dialog_props[name]);
136         return true;
137     }
138 }
139
140 bool
141 NewGUI::closeActiveDialog ()
142 {
143     if (_active_dialog == 0)
144         return false;
145
146     // Kill any entries in _active_dialogs...  Is there an STL
147     // algorithm to do (delete map entries by value, not key)?  I hate
148     // the STL :) -Andy
149     map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
150     for(/**/; iter != _active_dialogs.end(); iter++) {
151         if(iter->second == _active_dialog) {
152             _active_dialogs.erase(iter);
153             // iter is no longer valid
154             break;
155         }
156     }
157
158     delete _active_dialog;
159     _active_dialog = 0;
160     return true;
161 }
162
163 bool
164 NewGUI::closeDialog (const string& name)
165 {
166     if(_active_dialogs.find(name) != _active_dialogs.end()) {
167         if(_active_dialog == _active_dialogs[name])
168             _active_dialog = 0;
169         delete _active_dialogs[name];
170         _active_dialogs.erase(name);
171         return true;
172     }
173     return false; // dialog wasn't open...
174 }
175
176 SGPropertyNode_ptr
177 NewGUI::getDialogProperties (const string &name)
178 {
179     if(_dialog_props.find(name) != _dialog_props.end())
180         return _dialog_props[name];
181
182     SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
183     return 0;
184 }
185
186 FGDialog *
187 NewGUI::getDialog (const string &name)
188 {
189     if(_active_dialogs.find(name) != _active_dialogs.end())
190         return _active_dialogs[name];
191
192     SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
193     return 0;
194 }
195
196 void
197 NewGUI::setActiveDialog (FGDialog * dialog)
198 {
199     _active_dialog = dialog;
200 }
201
202 FGDialog *
203 NewGUI::getActiveDialog ()
204 {
205     return _active_dialog;
206 }
207
208 FGMenuBar *
209 NewGUI::getMenuBar ()
210 {
211     return _menubar;
212 }
213
214 bool
215 NewGUI::getMenuBarVisible () const
216 {
217     return _menubar->isVisible();
218 }
219
220 void
221 NewGUI::setMenuBarVisible (bool visible)
222 {
223     if (visible)
224         _menubar->show();
225     else
226         _menubar->hide();
227 }
228
229 static bool
230 test_extension (const char * path, const char * ext)
231 {
232     int pathlen = strlen(path);
233     int extlen = strlen(ext);
234
235     for (int i = 1; i <= pathlen && i <= extlen; i++) {
236         if (path[pathlen-i] != ext[extlen-i])
237             return false;
238     }
239     return true;
240 }
241
242 void
243 NewGUI::newDialog (SGPropertyNode* props)
244 {
245     const char* cname = props->getStringValue("name");
246     if(!cname) {
247         SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
248         return;
249     }
250     string name = cname;
251     if(_active_dialogs.find(name) == _active_dialogs.end())
252         _dialog_props[name] = props;
253 }
254
255 void
256 NewGUI::readDir (const char * path)
257 {
258     ulDir * dir = ulOpenDir(path);
259
260     if (dir == 0) {
261         SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
262                << path);
263         return;
264     }
265
266     for (ulDirEnt * dirEnt = ulReadDir(dir);
267          dirEnt != 0;
268          dirEnt = ulReadDir(dir)) {
269
270         char subpath[1024];
271
272         ulMakePath(subpath, path, dirEnt->d_name);
273
274         if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
275             SGPropertyNode * props = new SGPropertyNode;
276             try {
277                 readProperties(subpath, props);
278             } catch (const sg_exception &) {
279                 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
280                        << subpath);
281                 delete props;
282                 continue;
283             }
284             SGPropertyNode *nameprop = props->getNode("name");
285             if (!nameprop) {
286                 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
287                    << " has no name; skipping.");
288                 delete props;
289                 continue;
290             }
291             string name = nameprop->getStringValue();
292             if (_dialog_props[name])
293                 delete (SGPropertyNode *)_dialog_props[name];
294
295             _dialog_props[name] = props;
296         }
297     }
298     ulCloseDir(dir);
299 }
300
301
302 \f
303 ////////////////////////////////////////////////////////////////////////
304 // Style handling.
305 ////////////////////////////////////////////////////////////////////////
306
307 void
308 NewGUI::setStyle (void)
309 {
310     _itt_t it;
311     for (it = _colors.begin(); it != _colors.end(); ++it)
312       delete it->second;
313     _colors.clear();
314
315     // set up the traditional colors as default
316     _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
317     _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
318     _colors["highlight"]  = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
319     _colors["label"]      = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
320     _colors["legend"]     = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
321     _colors["misc"]       = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
322     _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
323
324     //puSetDefaultStyle();
325
326     int which = fgGetInt("/sim/gui/current-style", 0);
327     SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
328     SGPropertyNode *n = sim->getChild("style", which);
329     if (!n)
330         n = sim->getChild("style", 0, true);
331
332     setupFont(n->getNode("fonts/gui", true));
333     n = n->getNode("colors", true);
334
335     for (int i = 0; i < n->nChildren(); i++) {
336         SGPropertyNode *child = n->getChild(i);
337         _colors[child->getName()] = new FGColor(child);
338     }
339
340     FGColor *c = _colors["background"];
341     puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
342 }
343
344
345 void
346 NewGUI::setupFont (SGPropertyNode *node)
347 {
348     _font = globals->get_fontcache()->get(node);
349     puSetDefaultFonts(*_font, *_font);
350     return;
351 }
352
353
354
355 \f
356 ////////////////////////////////////////////////////////////////////////
357 // FGColor class.
358 ////////////////////////////////////////////////////////////////////////
359
360 void
361 FGColor::print() const {
362     std::cerr << "red=" << _red << ", green=" << _green
363               << ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
364 }
365
366 bool
367 FGColor::merge(const SGPropertyNode *node)
368 {
369     if (!node)
370         return false;
371
372     bool dirty = false;
373     const SGPropertyNode * n;
374     if ((n = node->getNode("red")))
375         _red = n->getFloatValue(), dirty = true;
376     if ((n = node->getNode("green")))
377         _green = n->getFloatValue(), dirty = true;
378     if ((n = node->getNode("blue")))
379         _blue = n->getFloatValue(), dirty = true;
380     if ((n = node->getNode("alpha")))
381         _alpha = n->getFloatValue(), dirty = true;
382     return dirty;
383 }
384
385 bool
386 FGColor::merge(const FGColor *color)
387 {
388     bool dirty = false;
389     if (color && color->_red >= 0.0)
390         _red = color->_red, dirty = true;
391     if (color && color->_green >= 0.0)
392         _green = color->_green, dirty = true;
393     if (color && color->_blue >= 0.0)
394         _blue = color->_blue, dirty = true;
395     if (color && color->_alpha >= 0.0)
396         _alpha = color->_alpha, dirty = true;
397     return dirty;
398 }
399
400
401
402 \f
403 ////////////////////////////////////////////////////////////////////////
404 // FGFontCache class.
405 ////////////////////////////////////////////////////////////////////////
406
407 namespace
408 {
409 struct GuiFont
410 {
411     const char *name;
412     puFont *font;
413     struct Predicate
414         : public std::unary_function<const GuiFont, bool>
415     {
416         Predicate(const char* name_) : name(name_) {}
417         bool operator() (const GuiFont& f1) const
418         {
419             return std::strcmp(f1.name, name) == 0;
420         }
421         const char* name;
422     };
423 };
424
425 const GuiFont guifonts[] = {
426     { "default",      &FONT_HELVETICA_14 },
427     { "FIXED_8x13",   &PUFONT_8_BY_13 },
428     { "FIXED_9x15",   &PUFONT_9_BY_15 },
429     { "TIMES_10",     &PUFONT_TIMES_ROMAN_10 },
430     { "TIMES_24",     &PUFONT_TIMES_ROMAN_24 },
431     { "HELVETICA_10", &PUFONT_HELVETICA_10 },
432     { "HELVETICA_12", &PUFONT_HELVETICA_12 },
433     { "HELVETICA_14", &FONT_HELVETICA_14 },
434     { "HELVETICA_18", &PUFONT_HELVETICA_18 },
435     { "SANS_12B",     &FONT_SANS_12B }
436 };
437
438 const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])];
439 }
440
441 FGFontCache::FGFontCache() :
442     _initialized(false)
443 {
444 }
445
446 FGFontCache::~FGFontCache()
447 {
448    PuFontMap::iterator it, end = _puFonts.end();
449    for (it = _puFonts.begin(); it != end; ++it)
450        delete it->second;
451 }
452
453 inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1,
454                                                    const FntParams& f2) const
455 {
456     int comp = f1.name.compare(f2.name);
457     if (comp < 0)
458         return true;
459     else if (comp > 0)
460         return false;
461     if (f1.size < f2.size)
462         return true;
463     else if (f1.size > f2.size)
464         return false;
465     return f1.slant < f2.slant;
466 }
467
468 struct FGFontCache::fnt *
469 FGFontCache::getfnt(const char *name, float size, float slant)
470 {
471     string fontName(name);
472     FntParams fntParams(fontName, size, slant);
473     PuFontMap::iterator i = _puFonts.find(fntParams);
474     if (i != _puFonts.end())
475         return i->second;
476     // fntTexFont s are all preloaded into the _texFonts map
477     TexFontMap::iterator texi = _texFonts.find(fontName);
478     fntTexFont* texfont = 0;
479     puFont* pufont = 0;
480     if (texi != _texFonts.end()) {
481         texfont = texi->second;
482     } else {
483         const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
484                                               GuiFont::Predicate(name));
485         if (guifont != guifontsEnd) {
486             pufont = guifont->font;
487         }
488     }
489     fnt* f = new fnt;
490     if (pufont) {
491         f->pufont = pufont;
492     } else if (texfont) {
493         f->texfont = texfont;
494         f->pufont = new puFont;
495         f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
496     } else {
497         f->pufont = guifonts[0].font;
498     }
499     _puFonts[fntParams] = f;
500     return f;
501 }
502
503 puFont *
504 FGFontCache::get(const char *name, float size, float slant)
505 {
506     return getfnt(name, size, slant)->pufont;
507 }
508
509 fntTexFont *
510 FGFontCache::getTexFont(const char *name, float size, float slant)
511 {
512     return getfnt(name, size, slant)->texfont;
513 }
514
515 puFont *
516 FGFontCache::get(SGPropertyNode *node)
517 {
518     if (!node)
519         return get("Helvetica.txf", 15.0, 0.0);
520
521     const char *name = node->getStringValue("name", "Helvetica.txf");
522     float size = node->getFloatValue("size", 15.0);
523     float slant = node->getFloatValue("slant", 0.0);
524
525     return get(name, size, slant);
526 }
527
528 void FGFontCache::init()
529 {
530     if (!_initialized) {
531         char *envp = ::getenv("FG_FONTS");
532         if (envp != NULL) {
533             _path.set(envp);
534         } else {
535             _path.set(globals->get_fg_root());
536             _path.append("Fonts");
537         }
538         _initialized = true;
539     }
540 }
541
542 SGPath
543 FGFontCache::getfntpath(const char *name)
544 {
545     init();
546     SGPath path(_path);
547     if (name && std::string(name) != "") {
548         path.append(name);
549         if (path.exists())
550             return path;
551     }
552
553     path = SGPath(_path);
554     path.append("Helvetica.txf");
555     
556     return path;
557 }
558
559 bool FGFontCache::initializeFonts()
560 {
561     static string fontext("txf");
562     init();
563     ulDir* fontdir = ulOpenDir(_path.c_str());
564     if (!fontdir)
565         return false;
566     const ulDirEnt *dirEntry;
567     while ((dirEntry = ulReadDir(fontdir)) != 0) {
568         SGPath path(_path);
569         path.append(dirEntry->d_name);
570         if (path.extension() == fontext) {
571             fntTexFont* f = new fntTexFont;
572             if (f->load((char *)path.c_str()))
573                 _texFonts[string(dirEntry->d_name)] = f;
574             else
575                 delete f;
576         }
577     }
578     ulCloseDir(fontdir);
579     return true;
580 }
581
582 // end of new_gui.cxx