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