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