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