]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.cxx
Removed old menubar option.
[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       _active_dialog(0)
24 {
25 }
26
27 NewGUI::~NewGUI ()
28 {
29     clear();
30 }
31
32 void
33 NewGUI::init ()
34 {
35     char path1[1024];
36     char path2[1024];
37     ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
38     ulMakePath(path2, path1, "dialogs");
39     readDir(path2);
40     _menubar->init();
41 }
42
43 void
44 NewGUI::reinit ()
45 {
46     unbind();
47     clear();
48     _menubar = new FGMenuBar;
49     init();
50     bind();
51 }
52
53 void
54 NewGUI::bind ()
55 {
56     fgTie("/sim/menubar/visibility", this,
57           &NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
58 }
59
60 void
61 NewGUI::unbind ()
62 {
63     fgUntie("/sim/menubar/visibility");
64 }
65
66 void
67 NewGUI::update (double delta_time_sec)
68 {
69     // NO OP
70 }
71
72 bool
73 NewGUI::showDialog (const string &name)
74 {
75     if (_dialog_props.find(name) == _dialog_props.end()) {
76         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
77         return false;
78     } else {
79         new FGDialog(_dialog_props[name]); // it will be deleted by a callback
80         return true;
81     }
82 }
83
84 bool
85 NewGUI::closeActiveDialog ()
86 {
87     if (_active_dialog == 0) {
88         return false;
89     } else {
90         delete _active_dialog;
91         _active_dialog = 0;
92         return true;
93     }
94 }
95
96 void
97 NewGUI::setActiveDialog (FGDialog * dialog)
98 {
99     _active_dialog = dialog;
100 }
101
102 FGDialog *
103 NewGUI::getActiveDialog ()
104 {
105     return _active_dialog;
106 }
107
108 FGMenuBar *
109 NewGUI::getMenuBar ()
110 {
111     return _menubar;
112 }
113
114 bool
115 NewGUI::getMenuBarVisible () const
116 {
117     return _menubar->isVisible();
118 }
119
120 void
121 NewGUI::setMenuBarVisible (bool visible)
122 {
123     if (visible)
124         _menubar->show();
125     else
126         _menubar->hide();
127 }
128
129 void
130 NewGUI::clear ()
131 {
132     delete _menubar;
133     _menubar = 0;
134
135     map<string,SGPropertyNode *>::iterator it;
136     for (it = _dialog_props.begin(); it != _dialog_props.end(); it++)
137         delete it->second;
138     _dialog_props.clear();
139 }
140
141 static bool
142 test_extension (const char * path, const char * ext)
143 {
144     int pathlen = strlen(path);
145     int extlen = strlen(ext);
146     
147     for (int i = 1; i <= pathlen && i <= extlen; i++) {
148         if (path[pathlen-i] != ext[extlen-i])
149             return false;
150     }
151     return true;
152 }
153
154 void
155 NewGUI::readDir (const char * path)
156 {
157     ulDir * dir = ulOpenDir(path);
158
159     if (dir == 0) {
160         SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
161                << path);
162         return;
163     }
164
165     for (ulDirEnt * dirEnt = ulReadDir(dir);
166          dirEnt != 0;
167          dirEnt = ulReadDir(dir)) {
168
169         char subpath[1024];
170
171         ulMakePath(subpath, path, dirEnt->d_name);
172
173         if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
174             SGPropertyNode * props = new SGPropertyNode;
175             try {
176                 readProperties(subpath, props);
177             } catch (const sg_exception &ex) {
178                 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
179                        << subpath);
180                 delete props;
181                 continue;
182             }
183             if (!props->hasValue("name")) {
184                 SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
185                    << " has no name; skipping.");
186                 delete props;
187                 continue;
188             }
189             string name = props->getStringValue("name");
190             SG_LOG(SG_INPUT, SG_BULK, "Saving dialog " << name);
191             if (_dialog_props[name] != 0)
192                 delete _dialog_props[name];
193             _dialog_props[name] = props;
194         }
195     }
196     ulCloseDir(dir);
197 }
198
199 // end of new_gui.cxx