Debian Codegraph @

src/GUI/new_gui.cxx

raw symbols SCIP
1 // new_gui.cxx: implementation of XML-configurable GUI support.
2
3 /*
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include <config.h>
8
9 #include "new_gui.hxx"
10
11 #include <algorithm>
12 #include <iostream>
13 #include <cstring>
14 #include <sys/types.h>
15
16 #include <simgear/compiler.h>
17 #include <simgear/debug/ErrorReportingCallback.hxx>
18 #include <simgear/misc/sg_dir.hxx>
19 #include <simgear/props/props_io.hxx>
20 #include <simgear/structure/exception.hxx>
21
22 #include <Add-ons/AddonManager.hxx>
23 #include <Main/fg_props.hxx>
24 #include <Main/sentryIntegration.hxx>
25 #include <Scripting/NasalSys.hxx>
26
27 #if defined(SG_UNIX) && !defined(SG_MAC)
28 #include "GL/glx.h"
29 #endif
30
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 // ensure we include this before puAux.h, so that
41 // #define _PU_H_ 1 has been done, and hence we don't
42 // include the un-modified system pu.h
43 #include "FlightGear_pu.h"
44
45 #include <plib/puAux.h>
46
47
48 #include "FGPUIDialog.hxx"
49 #include "FGPUIMenuBar.hxx"
50
51 #include "FGFontCache.hxx"
52 #include "FGColor.hxx"
53
54 #include "Highlight.hxx"
55
56 // ignore the word Navaid here, it's a DataCache
57 #include <Navaids/NavDataCache.hxx>
58
59 using std::map;
60 using std::string;
61
62 extern void puCleanUpJunk(void);
63
64 ////////////////////////////////////////////////////////////////////////
65 // Implementation of NewGUI.
66 ////////////////////////////////////////////////////////////////////////
67
68
69 NewGUI::NewGUI()
70 {
71 }
72
73 NewGUI::~NewGUI ()
74 {
75 for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
76 delete it->second;
77 }
78
79 // Recursively finds all nodes called <leaf> and appends the string values of
80 // these nodes to <out>.
81 //
82 static void findAllLeafValues(SGPropertyNode* node, const std::string& leaf, std::vector<std::string>& out)
83 {
84 string name = node->getNameString();
85 if (name == leaf) {
86 out.push_back(node->getStringValue());
87 }
88 for (int i=0; i<node->nChildren(); ++i) {
89 findAllLeafValues(node->getChild(i), leaf, out);
90 }
91 }
92
93
94 /* Registers menu-dialog associations with Highlight::add_menu_dialog(). */
95 static void scanMenus()
96 {
97 /* We find all nodes called 'dialog-name' in
98 sim/menubar/default/menu[]/item[]. */
99 SGPropertyNode* menubar = globals->get_props()->getNode("sim/menubar/default");
100 assert(menubar);
101 auto highlight = globals->get_subsystem<Highlight>();
102 if (!highlight) {
103 return;
104 }
105 for (int menu_p=0; menu_p<menubar->nChildren(); ++menu_p) {
106 SGPropertyNode* menu = menubar->getChild(menu_p);
107 if (menu->getNameString() != "menu") continue;
108 for (int item_p=0; item_p<menu->nChildren(); ++item_p) {
109 SGPropertyNode* item = menu->getChild(item_p);
110 if (item->getNameString() != "item") continue;
111 std::vector<std::string> dialog_names;
112 findAllLeafValues(item, "dialog-name", dialog_names);
113 for (auto dialog_name: dialog_names) {
114 highlight->addMenuDialog(HighlightMenu(menu->getIndex(), item->getIndex()), dialog_name);
115 }
116 }
117 }
118 }
119
120 void
121 NewGUI::init ()
122 {
123 createMenuBarImplementation();
124 fgTie("/sim/menubar/visibility", this,
125 &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
126
127 fgTie("/sim/menubar/overlap-hide", this,
128 &NewGUI::getMenuBarOverlapHide, &NewGUI::setMenuBarOverlapHide);
129
130 setStyle();
131 SGPath p(globals->get_fg_root(), "gui/dialogs");
132 readDir(p);
133
134 if (fgGetBool("/sim/gui/startup") == false) {
135 SGPath aircraftDialogDir(fgGetString("/sim/aircraft-dir"), "gui/dialogs");
136 if (aircraftDialogDir.exists()) {
137 readDir(aircraftDialogDir);
138 }
139
140 // Read XML dialogs made available by registered add-ons
141 const auto& addonManager = flightgear::addons::AddonManager::instance();
142 for (const auto& addon: addonManager->registeredAddons()) {
143 SGPath addonDialogDir = addon->getBasePath() / "gui/dialogs";
144
145 if (addonDialogDir.exists()) {
146 readDir(addonDialogDir);
147 }
148 }
149 }
150
151 // Fix for http://code.google.com/p/flightgear-bugs/issues/detail?id=947
152 fgGetNode("sim/menubar")->setAttribute(SGPropertyNode::PRESERVE, true);
153 _menubar->init();
154 scanMenus();
155 }
156
157 void
158 NewGUI::shutdown()
159 {
160 _active_dialogs.clear();
161 _active_dialog.clear();
162
163 fgUntie("/sim/menubar/visibility");
164 fgUntie("/sim/menubar/overlap-hide");
165 _menubar.reset();
166 _dialog_props.clear();
167
168 #if defined(HAVE_PUI)
169 puCleanUpJunk();
170 #endif
171 }
172
173 void
174 NewGUI::reinit ()
175 {
176 reset(true);
177 fgSetBool("/sim/signals/reinit-gui", true);
178 }
179
180 void
181 NewGUI::redraw ()
182 {
183 reset(false);
184 }
185
186 void
187 NewGUI::createMenuBarImplementation()
188 {
189 #if defined(SG_MAC)
190 if (fgGetBool("/sim/menubar/native", true)) {
191 _menubar.reset(new FGCocoaMenuBar);
192 }
193 #endif
194 #if defined(SG_WINDOWS)
195 if (fgGetBool("/sim/menubar/native", true)) {
196 // Windows-native menubar disabled for the moment, fall-through
197 // to PUI version
198 // _menubar.reset(new FGWindowsMenuBar);
199 }
200 #endif
201 if (!_menubar) {
202 _menubar.reset(new FGPUIMenuBar);
203 }
204 }
205
206 void
207 NewGUI::reset (bool reload)
208 {
209 DialogDict::iterator iter;
210 string_list openDialogs;
211 // close all open dialogs and remember them ...
212 for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
213 openDialogs.push_back(iter->first);
214
215 for (auto d : openDialogs)
216 closeDialog(d);
217
218 setStyle();
219
220 unbind();
221
222 if (reload) {
223 _dialog_props.clear();
224 _dialog_names.clear();
225 init();
226 } else {
227 createMenuBarImplementation();
228 _menubar->init();
229 }
230
231 bind();
232
233 // open dialogs again
234 for (auto d : openDialogs)
235 showDialog(d);
236 }
237
238 void
239 NewGUI::bind ()
240 {
241 }
242
243 void
244 NewGUI::unbind ()
245 {
246 }
247
248 void NewGUI::postinit()
249 {
250 _menubar->postinit();
251 }
252
253 void
254 NewGUI::update (double delta_time_sec)
255 {
256 SG_UNUSED(delta_time_sec);
257 auto iter = _active_dialogs.begin();
258 for(/**/; iter != _active_dialogs.end(); iter++)
259 iter->second->update();
260 }
261
262 bool
263 NewGUI::showDialog (const string &name)
264 {
265 if (name.empty()) {
266 SG_LOG(SG_GENERAL, SG_ALERT, "showDialog: no dialog name provided");
267 return false;
268 }
269
270 // first, check if it's already shown
271 if (_active_dialogs.find(name) != _active_dialogs.end()){
272 _active_dialogs[name]->bringToFront();
273 return true;
274 }
275
276 // check we know about the dialog by name
277 if (_dialog_names.find(name) == _dialog_names.end()) {
278 simgear::reportFailure(simgear::LoadFailure::NotFound, simgear::ErrorCode::GUIDialog, "Dialog not found:" + name);
279 return false;
280 }
281
282 // log dialog show/close except these noisy ones (screen_window & screen_display)
283 if (name.find("__screen_") != 0) {
284 flightgear::addSentryBreadcrumb("showing GUI dialog:" + name, "info");
285 }
286
287 try {
288 _active_dialogs[name] = new FGPUIDialog(getDialogProperties(name));
289
290 fgSetString("/sim/gui/dialogs/current-dialog", name);
291
292 // setActiveDialog(new FGPUIDialog(getDialogProperties(name)));
293 return true;
294 } catch (sg_exception& e) {
295 simgear::reportFailure(simgear::LoadFailure::Misconfigured, simgear::ErrorCode::GUIDialog, "Dialog failed to show:" + name + ":" + e.getFormattedMessage(), e.getLocation());
296 return false;
297 }
298 }
299
300 bool
301 NewGUI::toggleDialog (const string &name)
302 {
303 if (_active_dialogs.find(name) == _active_dialogs.end()) {
304 showDialog(name);
305 return true;
306 }
307 else {
308 closeDialog(name);
309 return false;
310 }
311 }
312
313 bool
314 NewGUI::closeActiveDialog ()
315 {
316 if (_active_dialog == 0)
317 return false;
318
319 // TODO support a request-close callback here, optionally
320 // many places in code assume this code-path does an
321 // immediate close, but for some UI paths it would be nice to
322 // allow some dialogs the chance to inervene
323
324 // Kill any entries in _active_dialogs... Is there an STL
325 // algorithm to do (delete map entries by value, not key)? I hate
326 // the STL :) -Andy
327 auto iter = _active_dialogs.begin();
328 for(/**/; iter != _active_dialogs.end(); iter++) {
329 if(iter->second == _active_dialog) {
330 _active_dialog->close();
331
332 _active_dialogs.erase(iter);
333 // iter is no longer valid
334 break;
335 }
336 }
337
338 _active_dialog = 0;
339 if (!_active_dialogs.empty()) {
340 fgSetString("/sim/gui/dialogs/current-dialog", _active_dialogs.begin()->second->getName());
341 }
342 return true;
343 }
344
345 bool
346 NewGUI::closeDialog (const string& name)
347 {
348 if(_active_dialogs.find(name) != _active_dialogs.end()) {
349 if (name.find("__screen_") != 0) {
350 flightgear::addSentryBreadcrumb("closing GUI dialog:" + name, "info");
351 }
352
353 if(_active_dialog == _active_dialogs[name])
354 _active_dialog.clear();
355
356 _active_dialogs.erase(name);
357 return true;
358 }
359 return false; // dialog wasn't open...
360 }
361
362 SGPropertyNode_ptr
363 NewGUI::getDialogProperties (const string &name)
364 {
365 if (_dialog_names.find(name) == _dialog_names.end()) {
366 SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
367 return NULL;
368 }
369
370 NameDialogDict::iterator it = _dialog_props.find(name);
371 if (it == _dialog_props.end()) {
372 // load the XML
373 SGPath path = _dialog_names[name];
374 SGPropertyNode_ptr props = new SGPropertyNode;
375 try {
376 readProperties(path, props);
377 } catch (const sg_exception &) {
378 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << path);
379 return NULL;
380 }
381
382 it = _dialog_props.insert(it, std::make_pair(name, props));
383 }
384
385 return it->second;
386 }
387
388 NewGUI::FGDialogRef
389 NewGUI::getDialog(const string& name)
390 {
391 if(_active_dialogs.find(name) != _active_dialogs.end())
392 return _active_dialogs[name];
393
394 SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
395 return 0;
396 }
397
398 void
399 NewGUI::setActiveDialog (FGDialog * dialog)
400 {
401 if (dialog){
402 fgSetString("/sim/gui/dialogs/current-dialog", dialog->getName());
403 }
404 _active_dialog = dialog;
405 }
406
407 NewGUI::FGDialogRef
408 NewGUI::getActiveDialog()
409 {
410 return _active_dialog;
411 }
412
413 FGMenuBar *
414 NewGUI::getMenuBar ()
415 {
416 return _menubar.get();
417 }
418
419 bool
420 NewGUI::getMenuBarVisible () const
421 {
422 return _menubar->isVisible();
423 }
424
425 void
426 NewGUI::setMenuBarVisible (bool visible)
427 {
428 if (visible)
429 _menubar->show();
430 else
431 _menubar->hide();
432 }
433
434 bool
435 NewGUI::getMenuBarOverlapHide() const
436 {
437 return _menubar->getHideIfOverlapsWindow();
438 }
439
440 void
441 NewGUI::setMenuBarOverlapHide(bool hide)
442 {
443 _menubar->setHideIfOverlapsWindow(hide);
444 }
445
446 void
447 NewGUI::newDialog (SGPropertyNode* props)
448 {
449 string name = props->getStringValue("name", "");
450 if(name.empty()) {
451 SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
452 return;
453 }
454
455 if(_active_dialogs.find(name) == _active_dialogs.end()) {
456 _dialog_props[name] = props;
457 // add a dummy path entry, so we believe the dialog exists
458 _dialog_names[name] = SGPath();
459 }
460 }
461
462
463 void
464 NewGUI::readDir (const SGPath& path)
465 {
466 simgear::Dir dir(path);
467 if( !dir.exists() )
468 {
469 SG_LOG(SG_INPUT, SG_INFO, "directory does not exist: " << path);
470 return;
471 }
472
473 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
474 flightgear::NavDataCache::Transaction txn(cache);
475 auto highlight = globals->get_subsystem<Highlight>();
476 for (SGPath xmlPath : dir.children(simgear::Dir::TYPE_FILE, ".xml")) {
477
478 SGPropertyNode_ptr props = new SGPropertyNode;
479 SGPropertyNode *nameprop = nullptr;
480 std::string name;
481
482 /* Always parse the dialog even if cache->isCachedFileModified() says
483 it is cached, so that we can register property-dialog associations with
484 Highlight::add_property_dialog(). */
485 try {
486 readProperties(xmlPath, props);
487 } catch (const sg_exception &) {
488 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << xmlPath);
489 props.reset();
490 }
491 if (props) {
492 nameprop = props->getNode("name");
493 if (nameprop) {
494 name = nameprop->getStringValue();
495 // Look for 'property' nodes within the dialog. (could
496 // also look for "dialog-name" to catch things like
497 // dialog-show.)
498 std::vector<std::string> property_paths;
499 findAllLeafValues(props, "property", property_paths);
500 for (auto property_path: property_paths) {
501 // We could maybe hoist this test for hightlight to avoid reaching
502 // here if it is null, but that's difficult to test, so we do it
503 // here instead.
504 if (highlight) {
505 highlight->addPropertyDialog(property_path, name);
506 }
507 }
508 }
509 }
510
511 if (!cache->isCachedFileModified(xmlPath)) {
512 // cached, easy
513 string name = cache->readStringProperty(xmlPath.utf8Str());
514 _dialog_names[name] = xmlPath;
515 continue;
516 }
517
518 // we need to parse the actual XML
519 if (!props) {
520 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog " << xmlPath);
521 continue;
522 }
523
524 if (!nameprop) {
525 SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmlPath << " has no name; skipping.");
526 continue;
527 }
528
529 _dialog_names[name] = xmlPath;
530 // update cached values
531 if (!cache->isReadOnly()) {
532 cache->stampCacheFile(xmlPath);
533 cache->writeStringProperty(xmlPath.utf8Str(), name);
534 }
535 } // of directory children iteration
536
537 txn.commit();
538 }
539 ////////////////////////////////////////////////////////////////////////
540 // Style handling.
541 ////////////////////////////////////////////////////////////////////////
542
543 void
544 NewGUI::setStyle (void)
545 {
546 _itt_t it;
547 for (it = _colors.begin(); it != _colors.end(); ++it)
548 delete it->second;
549 _colors.clear();
550
551 // set up the traditional colors as default
552 _colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
553 _colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
554 _colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
555 _colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
556 _colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
557 _colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
558 _colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
559
560 //puSetDefaultStyle();
561
562
563 if (0) {
564 // Re-read gui/style/*.xml files so that one can edit them and see the
565 // results without restarting flightgear.
566 SGPath p(globals->get_fg_root(), "gui/styles");
567 SGPropertyNode* sim_gui = globals->get_props()->getNode("sim/gui/", true);
568 simgear::Dir dir(p);
569 int i = 0;
570 for (SGPath xml: dir.children(simgear::Dir::TYPE_FILE, ".xml")) {
571 SGPropertyNode_ptr node = sim_gui->getChild("style", i, true);
572 node->removeAllChildren();
573 SG_LOG(SG_GENERAL, SG_WARN, "reading from " << xml << " into " << node->getPath());
574 readProperties(xml, node);
575 i += 1;
576 }
577 }
578
579 int which = fgGetInt("/sim/gui/current-style", 0);
580 SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
581 SGPropertyNode *n = sim->getChild("style", which);
582 if (!n)
583 n = sim->getChild("style", 0, true);
584
585 SGPropertyNode *selected_style = globals->get_props()->getNode("sim/gui/selected-style", true);
586
587 // n->copy() doesn't delete existing nodes, so need to clear them all
588 // first.
589 selected_style->removeAllChildren();
590 copyProperties(n, selected_style);
591
592 //if (selected_style && n)
593 // n->alias(selected_style);
594
595 setupFont(n->getNode("fonts/gui", true));
596 n = n->getNode("colors", true);
597
598 for (int i = 0; i < n->nChildren(); i++) {
599 SGPropertyNode *child = n->getChild(i);
600 _colors[child->getNameString()] = new FGColor(child);
601 }
602
603 FGColor *c = _colors["background"];
604 #if defined(HAVE_PUI)
605 puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
606 #endif
607 }
608
609
610 void
611 NewGUI::setupFont (SGPropertyNode *node)
612 {
613 #if defined(HAVE_PUI)
614 _font = FGFontCache::instance()->get(node);
615 puSetDefaultFonts(*_font, *_font);
616 #endif
617 return;
618 }
619
620
621 // Register the subsystem.
622 SGSubsystemMgr::Registrant<NewGUI> registrantNewGUI(
623 SGSubsystemMgr::INIT);
624
625 // end of new_gui.cxx