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