]> git.mxchange.org Git - flightgear.git/blob - src/GUI/menubar.cxx
Some autopilot fixes
[flightgear.git] / src / GUI / menubar.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #include <string.h>
6 #include <iostream>
7 #include <plib/pu.h>
8 #include <simgear/debug/logstream.hxx>
9 #include <simgear/structure/SGBinding.hxx>
10 #include <simgear/props/props_io.hxx>
11
12 #include <Main/globals.hxx>
13
14 #include "new_gui.hxx"
15 #include "menubar.hxx"
16
17 using std::vector;
18 using std::string;
19 using std::map;\f
20 ////////////////////////////////////////////////////////////////////////
21 // FIXME!!
22 //
23 // Deprecated wrappers for old menu commands.
24 //
25 // DO NOT ADD TO THESE.  THEY WILL BE DELETED SOON!
26 //
27 // These are defined in gui_funcs.cxx.  They should be replaced with
28 // user-configured dialogs and new commands where necessary.
29 ////////////////////////////////////////////////////////////////////////
30
31 #if defined(TR_HIRES_SNAP)
32 extern void dumpHiResSnapShot ();
33 static bool
34 do_hires_snapshot_dialog (const SGPropertyNode * arg)
35 {
36     dumpHiResSnapShot();
37     return true;
38 }
39 #endif // TR_HIRES_SNAP
40
41 static struct {
42     const char * name;
43     SGCommandMgr::command_t command;
44 } deprecated_dialogs [] = {
45 #if defined(TR_HIRES_SNAP)
46     { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
47 #endif
48     { 0, 0 }
49 };
50
51 static void
52 add_deprecated_dialogs ()
53 {
54   SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
55   for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
56     SG_LOG(SG_GENERAL, SG_INFO, "  " << deprecated_dialogs[i].name);
57     globals->get_commands()->addCommand(deprecated_dialogs[i].name,
58                                         deprecated_dialogs[i].command);
59   }
60 }
61
62
63 \f
64 ////////////////////////////////////////////////////////////////////////
65 // Static functions.
66 ////////////////////////////////////////////////////////////////////////
67
68
69 static void
70 menu_callback (puObject * object)
71 {
72     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
73     gui->getMenuBar()->fireItem(object);
74 }
75
76
77 \f
78 ////////////////////////////////////////////////////////////////////////
79 // Implementation of FGMenuBar.
80 ////////////////////////////////////////////////////////////////////////
81
82
83 FGMenuBar::FGMenuBar ()
84     : _visible(false),
85       _menuBar(0)
86 {
87 }
88
89 FGMenuBar::~FGMenuBar ()
90 {
91     destroy_menubar();
92 }
93
94 void
95 FGMenuBar::init ()
96 {
97     delete _menuBar;            // FIXME: check if PUI owns the pointer
98     make_menubar();
99                                 // FIXME: temporary commands to get at
100                                 // old, hard-coded dialogs.
101     add_deprecated_dialogs();
102 }
103
104 void
105 FGMenuBar::show ()
106 {
107     if (_menuBar != 0)
108         _menuBar->reveal();
109     _visible = true;
110 }
111
112 void
113 FGMenuBar::hide ()
114 {
115     if (_menuBar != 0)
116         _menuBar->hide();
117     _visible = false;
118 }
119
120 bool
121 FGMenuBar::isVisible () const
122 {
123     return _visible;
124 }
125
126 void
127 FGMenuBar::fireItem (puObject * item)
128 {
129     const char * name = item->getLegend();
130     vector<SGBinding *> &bindings = _bindings[name];
131     int nBindings = bindings.size();
132
133     for (int i = 0; i < nBindings; i++)
134         bindings[i]->fire();
135 }
136
137 void
138 FGMenuBar::make_menu (SGPropertyNode * node)
139 {
140     const char * name = strdup(node->getStringValue("label"));
141     vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
142
143     int array_size = item_nodes.size();
144
145     char ** items = make_char_array(array_size);
146     puCallback * callbacks = make_callback_array(array_size);
147
148     for (unsigned int i = 0, j = item_nodes.size() - 1;
149          i < item_nodes.size();
150          i++, j--) {
151
152                                 // Set up the PUI entries for this item
153         items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
154         callbacks[j] = menu_callback;
155
156                                 // Load all the bindings for this item
157         vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
158         SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
159
160         for (unsigned int k = 0; k < bindings.size(); k++) {
161             unsigned int m = 0;
162             SGPropertyNode_ptr binding;
163             while (dest->getChild("binding", m))
164                 m++;
165
166             binding = dest->getChild("binding", m, true);
167             copyProperties(bindings[k], binding);
168             _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
169         }
170     }
171
172     _menuBar->add_submenu(name, items, callbacks);
173 }
174
175 void
176 FGMenuBar::make_menubar ()
177 {
178     SGPropertyNode *targetpath;
179    
180     targetpath = fgGetNode("/sim/menubar/default",true);
181     // fgLoadProps("gui/menubar.xml", targetpath);
182     
183     /* NOTE: there is no check to see whether there's any usable data at all
184      *
185      * This would also have the advantage of being able to create some kind of
186      * 'fallback' menu - just in case that either menubar.xml is empty OR that
187      * its XML data is not valid, that way we would avoid displaying an
188      * unusable menubar without any functionality - if we decided to add another
189      * char * element to the commands structure in
190      *  $FG_SRC/src/Main/fgcommands.cxx 
191      * we could additionally save each function's (short) description and use
192      * this as label for the fallback PUI menubar item labels - as a workaround
193      * one might simply use the internal fgcommands and put them into the 
194      * fallback menu, so that the user is at least able to re-init the menu
195      * loading - just in case there was some malformed XML in it
196      * (it happend to me ...)
197      */
198     
199     make_menubar(targetpath);
200 }
201
202 /* WARNING: We aren't yet doing any validation of what's found - but since
203  * this isn't done with menubar.xml either, it should not really matter
204  * right now. Although one should later on consider to validate the
205  * contents, whether they are representing a 'legal' menubar structure.
206  */
207 void
208 FGMenuBar::make_menubar(SGPropertyNode * props) 
209 {    
210     // Just in case.
211     destroy_menubar();
212     _menuBar = new puMenuBar;
213
214     vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
215     for (unsigned int i = 0; i < menu_nodes.size(); i++)
216         make_menu(menu_nodes[i]);
217
218     _menuBar->close();
219     make_object_map(props);
220
221     if (_visible)
222         _menuBar->reveal();
223     else
224         _menuBar->hide();
225 }
226
227 void
228 FGMenuBar::destroy_menubar ()
229 {
230     if ( _menuBar == 0 )
231         return;
232
233     hide();
234     puDeleteObject(_menuBar);
235
236     unsigned int i;
237
238                                 // Delete all the character arrays
239                                 // we were forced to keep around for
240                                 // plib.
241     SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays");
242     for (i = 0; i < _char_arrays.size(); i++) {
243         for (int j = 0; _char_arrays[i][j] != 0; j++)
244             free(_char_arrays[i][j]); // added with strdup
245         delete[] _char_arrays[i];
246     }
247
248                                 // Delete all the callback arrays
249                                 // we were forced to keep around for
250                                 // plib.
251     SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays");
252     for (i = 0; i < _callback_arrays.size(); i++)
253         delete[] _callback_arrays[i];
254
255                                 // Delete all those bindings
256     SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings");
257     map<string,vector<SGBinding *> >::iterator it;
258     for (it = _bindings.begin(); it != _bindings.end(); it++) {
259         SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings for " << it->first);
260         for ( i = 0; i < it->second.size(); i++ )
261             delete it->second[i];
262     }
263
264     SG_LOG(SG_GENERAL, SG_BULK, "Done.");
265 }
266
267 void
268 FGMenuBar::make_object_map(SGPropertyNode * node)
269 {
270     unsigned int menu_index = 0;
271     vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
272     for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
273             obj; obj = obj->getNextObject()) {
274
275         // skip puPopupMenus. They are also children of _menuBar,
276         // but we access them via getUserData()  (see below)
277         if (!(obj->getType() & PUCLASS_ONESHOT))
278             continue;
279
280         if (menu_index >= menus.size()) {
281             SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
282                     << node->getPath() << "/menu[" << menu_index << ']');
283             return;
284         }
285
286         SGPropertyNode *menu = menus.at(menu_index);
287         _objects[menu->getPath()] = obj;
288         add_enabled_listener(menu);
289
290         puGroup *popup = (puGroup *)obj->getUserData();
291         if (!popup)
292             continue;
293
294         // the entries are for some reason reversed (last first), and we
295         // don't know yet how many there will be; so we collect first
296         vector<puObject *> e;
297         for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
298             e.push_back(me);
299
300         vector<SGPropertyNode_ptr> items = menu->getChildren("item");
301         for (unsigned int i = 0; i < e.size(); i++) {
302             if (i >= items.size()) {
303                 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
304                         << menu->getPath() << "/item[" << i << ']');
305                 break;
306             }
307             SGPropertyNode *item = items.at(e.size() - i - 1);
308             _objects[item->getPath()] = e[i];
309             add_enabled_listener(item);
310         }
311         menu_index++;
312     }
313 }
314
315 struct EnabledListener : SGPropertyChangeListener {
316     void valueChanged(SGPropertyNode *node) {
317         NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
318         if (!gui)
319             return;
320         FGMenuBar *menubar = gui->getMenuBar();
321         if (menubar)
322             menubar->enable_item(node->getParent(), node->getBoolValue());
323     }
324 };
325
326 void
327 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
328 {
329     if (!node->hasValue("enabled"))
330         node->setBoolValue("enabled", true);
331
332     enable_item(node, node->getBoolValue("enabled"));
333     node->getNode("enabled")->addChangeListener(new EnabledListener());
334 }
335
336 bool
337 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
338 {
339     string path = node->getPath();
340     if (_objects.find(path) == _objects.end()) {
341         SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
342             "non-existent menu item for node `" << path << '\'');
343         return false;
344     }
345     puObject *object = _objects[path];
346     if (state)
347         object->activate();
348     else
349         object->greyOut();
350
351     return true;
352 }
353
354 char **
355 FGMenuBar::make_char_array (int size)
356 {
357     char ** list = new char*[size+1];
358     for (int i = 0; i <= size; i++)
359         list[i] = 0;
360     _char_arrays.push_back(list);
361     return list;
362 }
363
364 puCallback *
365 FGMenuBar::make_callback_array (int size)
366 {
367     puCallback * list = new puCallback[size+1];
368     for (int i = 0; i <= size; i++)
369         list[i] = 0;
370     _callback_arrays.push_back(list);
371     return list;
372 }
373
374 // end of menubar.cxx