]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.cxx
Rename some methods to be more intuitive, add a close method for 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       _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::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 bool
65 NewGUI::showDialog (const string &name)
66 {
67     if (_dialog_props.find(name) == _dialog_props.end()) {
68         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
69         return false;
70     } else {
71         new FGDialog(_dialog_props[name]); // it will be deleted by a callback
72         return true;
73     }
74 }
75
76 bool
77 NewGUI::closeActiveDialog ()
78 {
79     if (_active_dialog == 0) {
80         return false;
81     } else {
82         delete _active_dialog;
83         _active_dialog = 0;
84         return true;
85     }
86 }
87
88 void
89 NewGUI::setActiveDialog (FGDialog * dialog)
90 {
91     _active_dialog = dialog;
92 }
93
94 FGDialog *
95 NewGUI::getActiveDialog ()
96 {
97     return _active_dialog;
98 }
99
100 FGMenuBar *
101 NewGUI::getMenuBar ()
102 {
103     return _menubar;
104 }
105
106 bool
107 NewGUI::getMenuBarVisible () const
108 {
109     return _menubar->isVisible();
110 }
111
112 void
113 NewGUI::setMenuBarVisible (bool visible)
114 {
115     if (visible)
116         _menubar->show();
117     else
118         _menubar->hide();
119 }
120
121 void
122 NewGUI::readDir (const char * path)
123 {
124     ulDir * dir = ulOpenDir(path);
125
126     if (dir == 0) {
127         SG_LOG(SG_GENERAL, SG_ALERT, "Failed to read GUI files from "
128                << path);
129         return;
130     }
131
132     ulDirEnt * dirEnt = ulReadDir(dir);
133     while (dirEnt != 0) {
134         char subpath[1024];
135
136         ulMakePath(subpath, path, dirEnt->d_name);
137
138         if (dirEnt->d_isdir && dirEnt->d_name[0] != '.') {
139             readDir(subpath);
140         } else {
141             SGPropertyNode_ptr props = new SGPropertyNode;
142             try {
143                 readProperties(subpath, props);
144             } catch (const sg_exception &ex) {
145                 SG_LOG(SG_INPUT, SG_ALERT, "Error parsing GUI file "
146                        << subpath);
147             }
148             if (!props->hasValue("name")) {
149                 SG_LOG(SG_INPUT, SG_WARN, "GUI file " << subpath
150                    << " has no name; skipping.");
151             } else {
152                 string name = props->getStringValue("name");
153                 SG_LOG(SG_INPUT, SG_BULK, "Saving GUI node " << name);
154                 _dialog_props[name] = props;
155             }
156         }
157         dirEnt = ulReadDir(dir);
158     }
159     ulCloseDir(dir);
160 }
161
162 // end of new_gui.cxx