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