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