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