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