]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.cxx
create /sim/gui if it doesn't exist
[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::getDialog (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_ALERT, "dialog '" << name << "' missing");
175     return 0;
176 }
177
178 void
179 NewGUI::setActiveDialog (FGDialog * dialog)
180 {
181     _active_dialog = dialog;
182 }
183
184 FGDialog *
185 NewGUI::getActiveDialog ()
186 {
187     return _active_dialog;
188 }
189
190 FGMenuBar *
191 NewGUI::getMenuBar ()
192 {
193     return _menubar;
194 }
195
196 bool
197 NewGUI::getMenuBarVisible () const
198 {
199     return _menubar->isVisible();
200 }
201
202 void
203 NewGUI::setMenuBarVisible (bool visible)
204 {
205     if (visible)
206         _menubar->show();
207     else
208         _menubar->hide();
209 }
210
211 static bool
212 test_extension (const char * path, const char * ext)
213 {
214     int pathlen = strlen(path);
215     int extlen = strlen(ext);
216
217     for (int i = 1; i <= pathlen && i <= extlen; i++) {
218         if (path[pathlen-i] != ext[extlen-i])
219             return false;
220     }
221     return true;
222 }
223
224 void
225 NewGUI::newDialog (SGPropertyNode* props)
226 {
227     const char* cname = props->getStringValue("name");
228     if(!cname) {
229         SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
230         return;
231     }
232     string name = cname;
233     if(_active_dialogs.find(name) == _active_dialogs.end())
234         _dialog_props[name] = props;
235 }
236
237 void
238 NewGUI::readDir (const char * path)
239 {
240     ulDir * dir = ulOpenDir(path);
241
242     if (dir == 0) {
243         SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
244                << path);
245         return;
246     }
247
248     for (ulDirEnt * dirEnt = ulReadDir(dir);
249          dirEnt != 0;
250          dirEnt = ulReadDir(dir)) {
251
252         char subpath[1024];
253
254         ulMakePath(subpath, path, dirEnt->d_name);
255
256         if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
257             SGPropertyNode * props = new SGPropertyNode;
258             try {
259                 readProperties(subpath, props);
260             } catch (const sg_exception &) {
261                 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
262                        << subpath);
263                 delete props;
264                 continue;
265             }
266             SGPropertyNode *nameprop = props->getNode("name");
267             if (!nameprop) {
268                 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
269                    << " has no name; skipping.");
270                 delete props;
271                 continue;
272             }
273             string name = nameprop->getStringValue();
274             if (_dialog_props[name])
275                 delete (SGPropertyNode *)_dialog_props[name];
276
277             _dialog_props[name] = props;
278         }
279     }
280     ulCloseDir(dir);
281 }
282
283
284 \f
285 ////////////////////////////////////////////////////////////////////////
286 // Style handling.
287 ////////////////////////////////////////////////////////////////////////
288
289 void
290 NewGUI::setStyle (void)
291 {
292     _itt_t it;
293     for (it = _colors.begin(); it != _colors.end(); ++it)
294       delete it->second;
295     _colors.clear();
296
297     // set up the traditional colors as default
298     _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
299     _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
300     _colors["highlight"]  = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
301     _colors["label"]      = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
302     _colors["legend"]     = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
303     _colors["misc"]       = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
304     _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
305
306     //puSetDefaultStyle();
307
308     int which = fgGetInt("/sim/gui/current-style", 0);
309     SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
310     SGPropertyNode *n = sim->getChild("style", which);
311     if (!n)
312         n = sim->getChild("style", 0, true);
313
314     setupFont(n->getNode("fonts/gui", true));
315     n = n->getNode("colors", true);
316
317     for (int i = 0; i < n->nChildren(); i++) {
318         SGPropertyNode *child = n->getChild(i);
319         _colors[child->getName()] = new FGColor(child);
320     }
321
322     FGColor *c = _colors["background"];
323     puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
324 }
325
326
327
328
329 static const struct {
330     char *name;
331     puFont *font;
332 } guifonts[] = {
333     { "default",      &FONT_HELVETICA_14 },
334     { "FIXED_8x13",   &PUFONT_8_BY_13 },
335     { "FIXED_9x15",   &PUFONT_9_BY_15 },
336     { "TIMES_10",     &PUFONT_TIMES_ROMAN_10 },
337     { "TIMES_24",     &PUFONT_TIMES_ROMAN_24 },
338     { "HELVETICA_10", &PUFONT_HELVETICA_10 },
339     { "HELVETICA_12", &PUFONT_HELVETICA_12 },
340     { "HELVETICA_14", &FONT_HELVETICA_14 },
341     { "HELVETICA_18", &PUFONT_HELVETICA_18 },
342     { "SANS_12B",     &FONT_SANS_12B },
343     { 0, 0 }
344 };
345
346 void
347 NewGUI::setupFont (SGPropertyNode *node)
348 {
349     _font = _fontcache->get(node);
350     puSetDefaultFonts(*_font, *_font);
351     return;
352 }
353
354
355
356 \f
357 ////////////////////////////////////////////////////////////////////////
358 // FGColor class.
359 ////////////////////////////////////////////////////////////////////////
360
361 bool
362 FGColor::merge(const SGPropertyNode *node)
363 {
364     if (!node)
365         return false;
366
367     bool dirty = false;
368     const SGPropertyNode * n;
369     if ((n = node->getNode("red")))
370         _red = n->getFloatValue(), dirty = true;
371     if ((n = node->getNode("green")))
372         _green = n->getFloatValue(), dirty = true;
373     if ((n = node->getNode("blue")))
374         _blue = n->getFloatValue(), dirty = true;
375     if ((n = node->getNode("alpha")))
376         _alpha = n->getFloatValue(), dirty = true;
377     return dirty;
378 }
379
380 bool
381 FGColor::merge(const FGColor *color)
382 {
383     bool dirty = false;
384     if (color && color->_red >= 0.0)
385         _red = color->_red, dirty = true;
386     if (color && color->_green >= 0.0)
387         _green = color->_green, dirty = true;
388     if (color && color->_blue >= 0.0)
389         _blue = color->_blue, dirty = true;
390     if (color && color->_alpha >= 0.0)
391         _alpha = color->_alpha, dirty = true;
392     return dirty;
393 }
394
395
396 //
397 FGFontCache::FGFontCache()
398 {
399     char *envp = ::getenv("FG_FONTS");
400     if (envp != NULL) {
401         _path.set(envp);
402     } else {
403         _path.set(globals->get_fg_root());
404         _path.append("Fonts");
405     }
406
407     for (int i=0; guifonts[i].name; i++)
408         _fonts[guifonts[i].name] = new fnt(guifonts[i].font);
409 }
410
411 FGFontCache::~FGFontCache()
412 {
413    _fonts.clear();
414 }
415
416 puFont *
417 FGFontCache::get(const char *name, float size, float slant)
418 {
419     _itt_t it;
420
421     if ((it = _fonts.find(name)) != _fonts.end())
422         return it->second->pufont;
423
424     SGPath path(_path);
425     path.append(name);
426
427     fnt *f = new fnt();
428     f->texfont = new fntTexFont;
429
430     if (f->texfont->load((char *)path.c_str())) {
431         f->pufont = new puFont;
432         f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
433         _fonts[name] = f;
434         return f->pufont;
435     }
436
437     delete f;
438     return _fonts["default"]->pufont;
439 }
440
441 puFont *
442 FGFontCache::get(SGPropertyNode *node)
443 {
444     const char *name = node->getStringValue("name", "Helvetica.txf");
445     float size = node->getFloatValue("size", 15.0);
446     float slant = node->getFloatValue("slant", 0.0);
447
448     return get(name, size, slant);
449 }
450
451 // end of new_gui.cxx