]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.cxx
Modified Files:
[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     : _menubar(new FGMenuBar),
30       _active_dialog(0)
31 {
32 }
33
34 NewGUI::~NewGUI ()
35 {
36     delete _menubar;
37     _dialog_props.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     reset(true);
56 }
57
58 void
59 NewGUI::redraw ()
60 {
61     reset(false);
62 }
63
64 void
65 NewGUI::reset (bool reload)
66 {
67     map<string,FGDialog *>::iterator iter;
68     vector<string> dlg;
69     // close all open dialogs and remember them ...
70     for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
71         dlg.push_back(iter->first);
72
73     unsigned int i;
74     for (i = 0; i < dlg.size(); i++)
75         closeDialog(dlg[i]);
76
77     setStyle();
78
79     unbind();
80     delete _menubar;
81     _menubar = new FGMenuBar;
82
83     if (reload) {
84         _dialog_props.clear();
85         init();
86     } else {
87         _menubar->init();
88     }
89
90     bind();
91
92     // open dialogs again
93     for (i = 0; i < dlg.size(); i++)
94         showDialog(dlg[i]);
95 }
96
97 void
98 NewGUI::bind ()
99 {
100     fgTie("/sim/menubar/visibility", this,
101           &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
102 }
103
104 void
105 NewGUI::unbind ()
106 {
107     fgUntie("/sim/menubar/visibility");
108 }
109
110 void
111 NewGUI::update (double delta_time_sec)
112 {
113     map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
114     for(/**/; iter != _active_dialogs.end(); iter++)
115         iter->second->update();
116 }
117
118 bool
119 NewGUI::showDialog (const string &name)
120 {
121     if (_dialog_props.find(name) == _dialog_props.end()) {
122         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
123         return false;
124     } else {
125         if(!_active_dialogs[name])
126             _active_dialogs[name] = new FGDialog(_dialog_props[name]);
127         return true;
128     }
129 }
130
131 bool
132 NewGUI::closeActiveDialog ()
133 {
134     if (_active_dialog == 0)
135         return false;
136
137     // Kill any entries in _active_dialogs...  Is there an STL
138     // algorithm to do (delete map entries by value, not key)?  I hate
139     // the STL :) -Andy
140     map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
141     for(/**/; iter != _active_dialogs.end(); iter++) {
142         if(iter->second == _active_dialog) {
143             _active_dialogs.erase(iter);
144             // iter is no longer valid
145             break;
146         }
147     }
148
149     delete _active_dialog;
150     _active_dialog = 0;
151     return true;
152 }
153
154 bool
155 NewGUI::closeDialog (const string& name)
156 {
157     if(_active_dialogs.find(name) != _active_dialogs.end()) {
158         if(_active_dialog == _active_dialogs[name])
159             _active_dialog = 0;
160         delete _active_dialogs[name];
161         _active_dialogs.erase(name);
162         return true;
163     }
164     return false; // dialog wasn't open...
165 }
166
167 SGPropertyNode_ptr
168 NewGUI::getDialogProperties (const string &name)
169 {
170     if(_dialog_props.find(name) != _dialog_props.end())
171         return _dialog_props[name];
172
173     SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
174     return 0;
175 }
176
177 FGDialog *
178 NewGUI::getDialog (const string &name)
179 {
180     if(_active_dialogs.find(name) != _active_dialogs.end())
181         return _active_dialogs[name];
182
183     SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
184     return 0;
185 }
186
187 void
188 NewGUI::setActiveDialog (FGDialog * dialog)
189 {
190     _active_dialog = dialog;
191 }
192
193 FGDialog *
194 NewGUI::getActiveDialog ()
195 {
196     return _active_dialog;
197 }
198
199 FGMenuBar *
200 NewGUI::getMenuBar ()
201 {
202     return _menubar;
203 }
204
205 bool
206 NewGUI::getMenuBarVisible () const
207 {
208     return _menubar->isVisible();
209 }
210
211 void
212 NewGUI::setMenuBarVisible (bool visible)
213 {
214     if (visible)
215         _menubar->show();
216     else
217         _menubar->hide();
218 }
219
220 static bool
221 test_extension (const char * path, const char * ext)
222 {
223     int pathlen = strlen(path);
224     int extlen = strlen(ext);
225
226     for (int i = 1; i <= pathlen && i <= extlen; i++) {
227         if (path[pathlen-i] != ext[extlen-i])
228             return false;
229     }
230     return true;
231 }
232
233 void
234 NewGUI::newDialog (SGPropertyNode* props)
235 {
236     const char* cname = props->getStringValue("name");
237     if(!cname) {
238         SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
239         return;
240     }
241     string name = cname;
242     if(_active_dialogs.find(name) == _active_dialogs.end())
243         _dialog_props[name] = props;
244 }
245
246 void
247 NewGUI::readDir (const char * path)
248 {
249     ulDir * dir = ulOpenDir(path);
250
251     if (dir == 0) {
252         SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
253                << path);
254         return;
255     }
256
257     for (ulDirEnt * dirEnt = ulReadDir(dir);
258          dirEnt != 0;
259          dirEnt = ulReadDir(dir)) {
260
261         char subpath[1024];
262
263         ulMakePath(subpath, path, dirEnt->d_name);
264
265         if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
266             SGPropertyNode * props = new SGPropertyNode;
267             try {
268                 readProperties(subpath, props);
269             } catch (const sg_exception &) {
270                 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
271                        << subpath);
272                 delete props;
273                 continue;
274             }
275             SGPropertyNode *nameprop = props->getNode("name");
276             if (!nameprop) {
277                 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
278                    << " has no name; skipping.");
279                 delete props;
280                 continue;
281             }
282             string name = nameprop->getStringValue();
283             if (_dialog_props[name])
284                 delete (SGPropertyNode *)_dialog_props[name];
285
286             _dialog_props[name] = props;
287         }
288     }
289     ulCloseDir(dir);
290 }
291
292
293 \f
294 ////////////////////////////////////////////////////////////////////////
295 // Style handling.
296 ////////////////////////////////////////////////////////////////////////
297
298 void
299 NewGUI::setStyle (void)
300 {
301     _itt_t it;
302     for (it = _colors.begin(); it != _colors.end(); ++it)
303       delete it->second;
304     _colors.clear();
305
306     // set up the traditional colors as default
307     _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
308     _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
309     _colors["highlight"]  = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
310     _colors["label"]      = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
311     _colors["legend"]     = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
312     _colors["misc"]       = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
313     _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
314
315     //puSetDefaultStyle();
316
317     int which = fgGetInt("/sim/gui/current-style", 0);
318     SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
319     SGPropertyNode *n = sim->getChild("style", which);
320     if (!n)
321         n = sim->getChild("style", 0, true);
322
323     setupFont(n->getNode("fonts/gui", true));
324     n = n->getNode("colors", true);
325
326     for (int i = 0; i < n->nChildren(); i++) {
327         SGPropertyNode *child = n->getChild(i);
328         _colors[child->getName()] = new FGColor(child);
329     }
330
331     FGColor *c = _colors["background"];
332     puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
333 }
334
335
336 void
337 NewGUI::setupFont (SGPropertyNode *node)
338 {
339     _font = globals->get_fontcache()->get(node);
340     puSetDefaultFonts(*_font, *_font);
341     return;
342 }
343
344
345
346 \f
347 ////////////////////////////////////////////////////////////////////////
348 // FGColor class.
349 ////////////////////////////////////////////////////////////////////////
350
351 bool
352 FGColor::merge(const SGPropertyNode *node)
353 {
354     if (!node)
355         return false;
356
357     bool dirty = false;
358     const SGPropertyNode * n;
359     if ((n = node->getNode("red")))
360         _red = n->getFloatValue(), dirty = true;
361     if ((n = node->getNode("green")))
362         _green = n->getFloatValue(), dirty = true;
363     if ((n = node->getNode("blue")))
364         _blue = n->getFloatValue(), dirty = true;
365     if ((n = node->getNode("alpha")))
366         _alpha = n->getFloatValue(), dirty = true;
367     return dirty;
368 }
369
370 bool
371 FGColor::merge(const FGColor *color)
372 {
373     bool dirty = false;
374     if (color && color->_red >= 0.0)
375         _red = color->_red, dirty = true;
376     if (color && color->_green >= 0.0)
377         _green = color->_green, dirty = true;
378     if (color && color->_blue >= 0.0)
379         _blue = color->_blue, dirty = true;
380     if (color && color->_alpha >= 0.0)
381         _alpha = color->_alpha, dirty = true;
382     return dirty;
383 }
384
385
386
387 \f
388 ////////////////////////////////////////////////////////////////////////
389 // FGFontCache class.
390 ////////////////////////////////////////////////////////////////////////
391
392 static const struct {
393     char *name;
394     puFont *font;
395 } guifonts[] = {
396     { "default",      &FONT_HELVETICA_14 },
397     { "FIXED_8x13",   &PUFONT_8_BY_13 },
398     { "FIXED_9x15",   &PUFONT_9_BY_15 },
399     { "TIMES_10",     &PUFONT_TIMES_ROMAN_10 },
400     { "TIMES_24",     &PUFONT_TIMES_ROMAN_24 },
401     { "HELVETICA_10", &PUFONT_HELVETICA_10 },
402     { "HELVETICA_12", &PUFONT_HELVETICA_12 },
403     { "HELVETICA_14", &FONT_HELVETICA_14 },
404     { "HELVETICA_18", &PUFONT_HELVETICA_18 },
405     { "SANS_12B",     &FONT_SANS_12B },
406     { 0, 0 }
407 };
408
409
410 FGFontCache::FGFontCache() :
411     _initialized(false)
412 {
413 }
414
415 FGFontCache::~FGFontCache()
416 {
417    map<const string, fnt *>::iterator it, end = _fonts.end();
418    for (it = _fonts.begin(); it != end; ++it)
419        delete it->second;
420 }
421
422 struct FGFontCache::fnt *
423 FGFontCache::getfnt(const char *name, float size, float slant)
424 {
425     if (!_initialized) {
426         char *envp = ::getenv("FG_FONTS");
427         if (envp != NULL) {
428             _path.set(envp);
429         } else {
430             _path.set(globals->get_fg_root());
431             _path.append("Fonts");
432         }
433
434         for (int i = 0; guifonts[i].name; i++)
435             _fonts[guifonts[i].name] = new fnt(guifonts[i].font);
436
437         _initialized = true;
438     }
439
440     _itt_t it;
441     if ((it = _fonts.find(name)) != _fonts.end())
442         return it->second;
443
444     SGPath path(_path);
445     path.append(name);
446
447     fnt *f = new fnt();
448     f->texfont = new fntTexFont;
449
450     if (f->texfont->load((char *)path.c_str())) {
451         f->pufont = new puFont;
452         f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
453         return _fonts[name] = f;
454     }
455
456     delete f;
457     return _fonts["default"];
458 }
459
460 puFont *
461 FGFontCache::get(const char *name, float size, float slant)
462 {
463     return getfnt(name, size, slant)->pufont;
464 }
465
466 fntTexFont *
467 FGFontCache::getTexFont(const char *name, float size, float slant)
468 {
469     return getfnt(name, size, slant)->texfont;
470 }
471
472 puFont *
473 FGFontCache::get(SGPropertyNode *node)
474 {
475     if (!node)
476         return get("Helvetica.txf", 15.0, 0.0);
477
478     const char *name = node->getStringValue("name", "Helvetica.txf");
479     float size = node->getFloatValue("size", 15.0);
480     float slant = node->getFloatValue("slant", 0.0);
481
482     return get(name, size, slant);
483 }
484
485 // end of new_gui.cxx