]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.cxx
Tie the bool property /sim/menubar/visibility to hide and reveal the
[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/misc/exception.hxx>
9 #include <Main/fg_props.hxx>
10
11 #include "menubar.hxx"
12 #include "dialog.hxx"
13
14
15 \f
16 ////////////////////////////////////////////////////////////////////////
17 // Implementation of NewGUI.
18 ////////////////////////////////////////////////////////////////////////
19
20
21 NewGUI::NewGUI ()
22     : _menubar(new FGMenuBar),
23       _current_widget(0)
24 {
25 }
26
27 NewGUI::~NewGUI ()
28 {
29     delete _menubar;
30 }
31
32 void
33 NewGUI::init ()
34 {
35     char path1[1024];
36     char path2[1024];
37     ulMakePath(path1, getenv("FG_ROOT"), "gui");
38     ulMakePath(path2, path1, "dialogs");
39     readDir(path2);
40 #if !defined(FG_OLD_MENUBAR)
41     _menubar->init();
42 #endif
43 }
44
45 void
46 NewGUI::bind ()
47 {
48     fgTie("/sim/menubar/visibility", this,
49           &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
50 }
51
52 void
53 NewGUI::unbind ()
54 {
55     fgUntie("/sim/menubar/visibility");
56 }
57
58 void
59 NewGUI::update (double delta_time_sec)
60 {
61     // NO OP
62 }
63
64 void
65 NewGUI::display (const string &name)
66 {
67     if (_widgets.find(name) == _widgets.end())
68         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
69     else
70         new FGDialog(_widgets[name]);
71 }
72
73 void
74 NewGUI::setCurrentWidget (FGDialog * widget)
75 {
76     _current_widget = widget;
77 }
78
79 FGDialog *
80 NewGUI::getCurrentWidget ()
81 {
82     return _current_widget;
83 }
84
85 FGMenuBar *
86 NewGUI::getMenuBar ()
87 {
88     return _menubar;
89 }
90
91 bool
92 NewGUI::getMenuBarVisible () const
93 {
94     return _menubar->isVisible();
95 }
96
97 void
98 NewGUI::setMenuBarVisible (bool visible)
99 {
100     if (visible)
101         _menubar->show();
102     else
103         _menubar->hide();
104 }
105
106 void
107 NewGUI::readDir (const char * path)
108 {
109     ulDir * dir = ulOpenDir(path);
110
111     if (dir == 0) {
112         SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
113                << path);
114         return;
115     }
116
117     ulDirEnt * dirEnt = ulReadDir(dir);
118     while (dirEnt != 0) {
119         char subpath[1024];
120
121         ulMakePath(subpath, path, dirEnt->d_name);
122
123         if (dirEnt->d_isdir && dirEnt->d_name[0] != '.') {
124             readDir(subpath);
125         } else {
126             SGPropertyNode_ptr props = new SGPropertyNode;
127             try {
128                 readProperties(subpath, props);
129             } catch (const sg_exception &ex) {
130                 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing GUI file "
131                        << subpath);
132             }
133             if (!props->hasValue("name")) {
134                 SG_LOG(SG_INPUT, SG_WARN, "GUI file " << subpath
135                    << " has no name; skipping.");
136             } else {
137                 string name = props->getStringValue("name");
138                 SG_LOG(SG_INPUT, SG_BULK, "Saving GUI node " << name);
139                 _widgets[name] = props;
140             }
141         }
142         dirEnt = ulReadDir(dir);
143     }
144     ulCloseDir(dir);
145 }
146
147 // end of new_gui.cxx