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