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