]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.cxx
Support for multiple data dirs.
[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 #include <boost/foreach.hpp>
23
24 #include <Main/fg_props.hxx>
25
26 #if defined(SG_UNIX) && !defined(SG_MAC) 
27 #include "GL/glx.h"
28 #endif
29
30 #include "FGPUIMenuBar.hxx"
31
32 #if defined(SG_MAC)
33 #include "FGCocoaMenuBar.hxx"
34 #endif
35
36 #include "FGPUIDialog.hxx"
37 #include "FGFontCache.hxx"
38 #include "FGColor.hxx"
39
40 // ignore the word Navaid here, it's a DataCache
41 #include <Navaids/NavDataCache.hxx>
42
43 using std::map;
44 using std::string;
45
46 ////////////////////////////////////////////////////////////////////////
47 // Implementation of NewGUI.
48 ////////////////////////////////////////////////////////////////////////
49
50
51
52 NewGUI::NewGUI () :
53   _active_dialog(0)
54 {
55 #if defined(SG_MAC)
56     if (fgGetBool("/sim/menubar/native", true)) {
57         _menubar.reset(new FGCocoaMenuBar);
58         return;
59     }
60 #endif
61   _menubar.reset(new FGPUIMenuBar);
62 }
63
64 NewGUI::~NewGUI ()
65 {
66     _dialog_props.clear();
67     for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
68         delete it->second;
69 }
70
71 void
72 NewGUI::init ()
73 {
74     setStyle();
75     SGPath p(globals->get_fg_root(), "gui/dialogs");
76     readDir(p);
77     const std::string aircraft_dir(fgGetString("/sim/aircraft-dir"));
78     readDir( SGPath(aircraft_dir, "gui/dialogs") );
79     
80     // Fix for http://code.google.com/p/flightgear-bugs/issues/detail?id=947
81     fgGetNode("sim/menubar")->setAttribute(SGPropertyNode::PRESERVE, true);
82     _menubar->init();
83 }
84
85 void
86 NewGUI::reinit ()
87 {
88     reset(true);
89     fgSetBool("/sim/signals/reinit-gui", true);
90 }
91
92 void
93 NewGUI::redraw ()
94 {
95     reset(false);
96 }
97
98 void
99 NewGUI::reset (bool reload)
100 {
101     map<string,FGDialog *>::iterator iter;
102     string_list openDialogs;
103     // close all open dialogs and remember them ...
104     for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
105         openDialogs.push_back(iter->first);
106
107     BOOST_FOREACH(string d, openDialogs)
108         closeDialog(d);
109
110     setStyle();
111
112     unbind();
113 #if !defined(SG_MAC)
114     _menubar.reset(new FGPUIMenuBar);
115 #endif
116
117     if (reload) {
118         _dialog_props.clear();
119         _dialog_names.clear();
120         init();
121     } else {
122         _menubar->init();
123     }
124
125     bind();
126
127     // open dialogs again
128     BOOST_FOREACH(string d, openDialogs)
129         showDialog(d);
130 }
131
132 void
133 NewGUI::bind ()
134 {
135     fgTie("/sim/menubar/visibility", this,
136           &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
137 }
138
139 void
140 NewGUI::unbind ()
141 {
142     fgUntie("/sim/menubar/visibility");
143 }
144
145 void
146 NewGUI::update (double delta_time_sec)
147 {
148     SG_UNUSED(delta_time_sec);
149     map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
150     for(/**/; iter != _active_dialogs.end(); iter++)
151         iter->second->update();
152 }
153
154 bool
155 NewGUI::showDialog (const string &name)
156 {
157     // first, check if it's already shown
158     if (_active_dialogs.find(name) != _active_dialogs.end())
159       return true;
160   
161     // check we know about the dialog by name
162     if (_dialog_names.find(name) == _dialog_names.end()) {
163         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
164         return false;
165     }
166     
167     _active_dialogs[name] = new FGPUIDialog(getDialogProperties(name));
168     return true;
169 }
170
171 bool
172 NewGUI::closeActiveDialog ()
173 {
174     if (_active_dialog == 0)
175         return false;
176
177     // Kill any entries in _active_dialogs...  Is there an STL
178     // algorithm to do (delete map entries by value, not key)?  I hate
179     // the STL :) -Andy
180     map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
181     for(/**/; iter != _active_dialogs.end(); iter++) {
182         if(iter->second == _active_dialog) {
183             _active_dialogs.erase(iter);
184             // iter is no longer valid
185             break;
186         }
187     }
188
189     delete _active_dialog;
190     _active_dialog = 0;
191     return true;
192 }
193
194 bool
195 NewGUI::closeDialog (const string& name)
196 {
197     if(_active_dialogs.find(name) != _active_dialogs.end()) {
198         if(_active_dialog == _active_dialogs[name])
199             _active_dialog = 0;
200         delete _active_dialogs[name];
201         _active_dialogs.erase(name);
202         return true;
203     }
204     return false; // dialog wasn't open...
205 }
206
207 SGPropertyNode_ptr
208 NewGUI::getDialogProperties (const string &name)
209 {
210     if (_dialog_names.find(name) == _dialog_names.end()) {
211       SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
212       return NULL;
213     }
214   
215     NameDialogDict::iterator it = _dialog_props.find(name);
216     if (it == _dialog_props.end()) {
217       // load the XML
218       SGPath path = _dialog_names[name];
219       SGPropertyNode_ptr props = new SGPropertyNode;
220       try {
221         readProperties(path.str(), props);
222       } catch (const sg_exception &) {
223         SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << path);
224         return NULL;
225       }
226       
227       it = _dialog_props.insert(it, std::make_pair(name, props));
228     }
229
230     return it->second;
231 }
232
233 FGDialog *
234 NewGUI::getDialog (const string &name)
235 {
236     if(_active_dialogs.find(name) != _active_dialogs.end())
237         return _active_dialogs[name];
238
239     SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
240     return 0;
241 }
242
243 void
244 NewGUI::setActiveDialog (FGDialog * dialog)
245 {
246     _active_dialog = dialog;
247 }
248
249 FGDialog *
250 NewGUI::getActiveDialog ()
251 {
252     return _active_dialog;
253 }
254
255 FGMenuBar *
256 NewGUI::getMenuBar ()
257 {
258     return _menubar.get();
259 }
260
261 bool
262 NewGUI::getMenuBarVisible () const
263 {
264     return _menubar->isVisible();
265 }
266
267 void
268 NewGUI::setMenuBarVisible (bool visible)
269 {
270     if (visible)
271         _menubar->show();
272     else
273         _menubar->hide();
274 }
275
276 void
277 NewGUI::newDialog (SGPropertyNode* props)
278 {
279     const char* cname = props->getStringValue("name");
280     if(!cname) {
281         SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
282         return;
283     }
284     string name = cname;
285   
286     if(_active_dialogs.find(name) == _active_dialogs.end()) {
287         _dialog_props[name] = props;
288     // add a dummy path entry, so we believe the dialog exists
289         _dialog_names[name] = SGPath();
290     }
291 }
292
293 void
294 NewGUI::readDir (const SGPath& path)
295 {
296     simgear::Dir dir(path);
297     if( !dir.exists() )
298     {
299       SG_LOG(SG_INPUT, SG_INFO, "directory does not exist: " << path.str());
300       return;
301     }
302
303     flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
304     flightgear::NavDataCache::Transaction txn(cache);
305     simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
306     
307     BOOST_FOREACH(SGPath xmlPath, xmls) {
308       if (!cache->isCachedFileModified(xmlPath)) {
309         // cached, easy
310         string name = cache->readStringProperty(xmlPath.str());
311         _dialog_names[name] = xmlPath;
312         continue;
313       }
314       
315     // we need to parse the actual XML
316       SGPropertyNode_ptr props = new SGPropertyNode;
317       try {
318         readProperties(xmlPath.str(), props);
319       } catch (const sg_exception &) {
320         SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << xmlPath);
321         continue;
322       }
323       
324       SGPropertyNode *nameprop = props->getNode("name");
325       if (!nameprop) {
326         SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmlPath << " has no name; skipping.");
327         continue;
328       }
329       
330       string name = nameprop->getStringValue();
331       _dialog_names[name] = xmlPath;
332     // update cached values
333       cache->stampCacheFile(xmlPath);
334       cache->writeStringProperty(xmlPath.str(), name);
335     } // of directory children iteration
336   
337     txn.commit();
338 }\f
339 ////////////////////////////////////////////////////////////////////////
340 // Style handling.
341 ////////////////////////////////////////////////////////////////////////
342
343 void
344 NewGUI::setStyle (void)
345 {
346     _itt_t it;
347     for (it = _colors.begin(); it != _colors.end(); ++it)
348       delete it->second;
349     _colors.clear();
350
351     // set up the traditional colors as default
352     _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
353     _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
354     _colors["highlight"]  = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
355     _colors["label"]      = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
356     _colors["legend"]     = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
357     _colors["misc"]       = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
358     _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
359
360     //puSetDefaultStyle();
361
362     int which = fgGetInt("/sim/gui/current-style", 0);
363     SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
364     SGPropertyNode *n = sim->getChild("style", which);
365     if (!n)
366         n = sim->getChild("style", 0, true);
367
368     setupFont(n->getNode("fonts/gui", true));
369     n = n->getNode("colors", true);
370
371     for (int i = 0; i < n->nChildren(); i++) {
372         SGPropertyNode *child = n->getChild(i);
373         _colors[child->getName()] = new FGColor(child);
374     }
375
376     FGColor *c = _colors["background"];
377     puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
378 }
379
380
381 void
382 NewGUI::setupFont (SGPropertyNode *node)
383 {
384     _font = globals->get_fontcache()->get(node);
385     puSetDefaultFonts(*_font, *_font);
386     return;
387 }
388
389 // end of new_gui.cxx