8 #include <simgear/debug/logstream.hxx>
10 #include <Autopilot/auto_gui.hxx>
11 #include <Input/input.hxx>
12 #include <Main/globals.hxx>
14 #include "new_gui.hxx"
15 #include "menubar.hxx"
19 ////////////////////////////////////////////////////////////////////////
22 // Deprecated wrappers for old menu commands.
24 // DO NOT ADD TO THESE. THEY WILL BE DELETED SOON!
26 // These are defined in gui_funcs.cxx. They should be replaced with
27 // user-configured dialogs and new commands where necessary.
28 ////////////////////////////////////////////////////////////////////////
30 extern void reInit (puObject *);
32 do_reinit_dialog (const SGPropertyNode * arg)
38 #if defined(TR_HIRES_SNAP)
39 extern void dumpHiResSnapShot (puObject *);
41 do_hires_snapshot_dialog (const SGPropertyNode * arg)
46 #endif // TR_HIRES_SNAP
48 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
49 extern void printScreen (puObject *);
51 do_print_dialog (const SGPropertyNode * arg)
58 extern void prop_pickerView (puObject *);
60 do_properties_dialog (const SGPropertyNode * arg)
66 extern void AddWayPoint (puObject *);
68 do_ap_add_waypoint_dialog (const SGPropertyNode * arg)
74 extern void PopWayPoint (puObject *);
76 do_ap_pop_waypoint_dialog (const SGPropertyNode * arg)
82 extern void ClearRoute (puObject *);
84 do_ap_clear_route_dialog (const SGPropertyNode * arg)
91 extern void fgAPAdjust (puObject *);
93 do_ap_adjust_dialog (const SGPropertyNode * arg)
100 extern void fgLatLonFormatToggle (puObject *);
102 do_lat_lon_format_dialog (const SGPropertyNode * arg)
104 fgLatLonFormatToggle(0);
108 extern void helpCb (puObject *);
110 do_help_dialog (const SGPropertyNode * arg)
118 SGCommandMgr::command_t command;
119 } deprecated_dialogs [] = {
120 { "old-reinit-dialog", do_reinit_dialog },
121 #if defined(TR_HIRES_SNAP)
122 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
124 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
125 { "old-print-dialog", do_print_dialog },
127 { "old-properties-dialog", do_properties_dialog },
128 { "old-ap-add-waypoint-dialog", do_ap_add_waypoint_dialog },
129 { "old-ap-pop-waypoint-dialog", do_ap_pop_waypoint_dialog },
130 { "old-ap-clear-route-dialog", do_ap_clear_route_dialog },
131 { "old-lat-lon-format-dialog", do_lat_lon_format_dialog },
132 { "old-help-dialog", do_help_dialog },
137 add_deprecated_dialogs ()
139 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
140 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
141 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
142 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
143 deprecated_dialogs[i].command);
149 ////////////////////////////////////////////////////////////////////////
151 ////////////////////////////////////////////////////////////////////////
155 menu_callback (puObject * object)
157 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
158 gui->getMenuBar()->fireItem(object);
163 ////////////////////////////////////////////////////////////////////////
164 // Implementation of FGMenuBar.
165 ////////////////////////////////////////////////////////////////////////
168 FGMenuBar::FGMenuBar ()
174 FGMenuBar::~FGMenuBar ()
182 delete _menuBar; // FIXME: check if PUI owns the pointer
184 // FIXME: temporary commands to get at
185 // old, hard-coded dialogs.
186 add_deprecated_dialogs();
206 FGMenuBar::isVisible () const
212 FGMenuBar::fireItem (puObject * item)
214 const char * name = item->getLegend();
215 vector<FGBinding *> &bindings = _bindings[name];
216 int nBindings = bindings.size();
218 for (int i = 0; i < nBindings; i++)
223 FGMenuBar::make_menu (SGPropertyNode * node)
225 const char * name = strdup(node->getStringValue("label"));
226 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
228 int array_size = item_nodes.size();
230 char ** items = make_char_array(array_size);
231 puCallback * callbacks = make_callback_array(array_size);
233 for (unsigned int i = 0, j = item_nodes.size() - 1;
234 i < item_nodes.size();
237 // Set up the PUI entries for this item
238 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
239 callbacks[j] = menu_callback;
241 // Load all the bindings for this item
242 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
243 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
245 for (unsigned int k = 0; k < bindings.size(); k++) {
247 SGPropertyNode *binding;
248 while (dest->getChild("binding", m))
251 binding = dest->getChild("binding", m, true);
252 copyProperties(bindings[k], binding);
253 _bindings[items[j]].push_back(new FGBinding(binding));
257 _menuBar->add_submenu(name, items, callbacks);
261 FGMenuBar::make_menubar ()
263 SGPropertyNode *targetpath;
265 targetpath = fgGetNode("/sim/menubar/default",true);
266 // fgLoadProps("gui/menubar.xml", targetpath);
268 /* NOTE: there is no check to see whether there's any usable data at all
270 * This would also have the advantage of being able to create some kind of
271 * 'fallback' menu - just in case that either menubar.xml is empty OR that
272 * its XML data is not valid, that way we would avoid displaying an
273 * unusable menubar without any functionality - if we decided to add another
274 * char * element to the commands structure in
275 * $FG_SRC/src/Main/fgcommands.cxx
276 * we could additionally save each function's (short) description and use
277 * this as label for the fallback PUI menubar item labels - as a workaround
278 * one might simply use the internal fgcommands and put them into the
279 * fallback menu, so that the user is at least able to re-init the menu
280 * loading - just in case there was some malformed XML in it
281 * (it happend to me ...)
284 make_menubar(targetpath);
287 /* WARNING: We aren't yet doing any validation of what's found - but since
288 * this isn't done with menubar.xml either, it should not really matter
289 * right now. Although one should later on consider to validate the
290 * contents, whether they are representing a 'legal' menubar structure.
293 FGMenuBar::make_menubar(SGPropertyNode * props)
297 _menuBar = new puMenuBar;
299 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
300 for (unsigned int i = 0; i < menu_nodes.size(); i++)
301 make_menu(menu_nodes[i]);
304 make_object_map(props);
313 FGMenuBar::destroy_menubar ()
319 puDeleteObject(_menuBar);
323 // Delete all the character arrays
324 // we were forced to keep around for
326 SG_LOG(SG_GENERAL, SG_INFO, "Deleting char arrays");
327 for (i = 0; i < _char_arrays.size(); i++) {
328 for (int j = 0; _char_arrays[i][j] != 0; j++)
329 free(_char_arrays[i][j]); // added with strdup
330 delete[] _char_arrays[i];
333 // Delete all the callback arrays
334 // we were forced to keep around for
336 SG_LOG(SG_GENERAL, SG_INFO, "Deleting callback arrays");
337 for (i = 0; i < _callback_arrays.size(); i++)
338 delete[] _callback_arrays[i];
340 // Delete all those bindings
341 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings");
342 map<string,vector<FGBinding *> >::iterator it;
343 it = _bindings.begin();
344 for (it = _bindings.begin(); it != _bindings.end(); it++) {
345 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings for " << it->first);
346 for ( i = 0; i < it->second.size(); i++ )
347 delete it->second[i];
350 SG_LOG(SG_GENERAL, SG_INFO, "Done.");
354 FGMenuBar::make_object_map(SGPropertyNode * node)
356 unsigned int menu_index = 0;
357 vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
358 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
359 obj; obj = obj->getNextObject()) {
361 // skip puPopupMenus. They are also children of _menuBar,
362 // but we access them via getUserData() (see below)
363 if (!(obj->getType() & PUCLASS_ONESHOT))
366 if (menu_index >= menus.size()) {
367 SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
368 << node->getPath() << "/menu[" << menu_index << ']');
372 SGPropertyNode *menu = menus.at(menu_index);
373 _objects[menu->getPath()] = obj;
374 add_enabled_listener(menu);
376 puGroup *popup = (puGroup *)obj->getUserData();
380 // the entries are for some reason reversed (last first), and we
381 // don't know yet how many there will be; so we collect first
382 vector<puObject *> e;
383 for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
386 vector<SGPropertyNode_ptr> items = menu->getChildren("item");
387 for (unsigned int i = 0; i < e.size(); i++) {
388 if (i >= items.size()) {
389 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
390 << menu->getPath() << "/item[" << i << ']');
393 SGPropertyNode *item = items.at(e.size() - i - 1);
394 _objects[item->getPath()] = e[i];
395 add_enabled_listener(item);
401 struct EnabledListener : SGPropertyChangeListener {
402 void valueChanged(SGPropertyNode *node) {
403 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
406 FGMenuBar *menubar = gui->getMenuBar();
408 menubar->enable_item(node->getParent(), node->getBoolValue());
413 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
415 if (!node->hasValue("enabled"))
416 node->setBoolValue("enabled", true);
418 enable_item(node, node->getBoolValue("enabled"));
419 node->getNode("enabled")->addChangeListener(new EnabledListener());
423 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
425 const char *path = node->getPath();
426 if (_objects.find(path) == _objects.end()) {
427 SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
428 "non-existent menu item for node `" << path << '\'');
431 puObject *object = _objects[path];
441 FGMenuBar::make_char_array (int size)
443 char ** list = new char*[size+1];
444 for (int i = 0; i <= size; i++)
446 _char_arrays.push_back(list);
451 FGMenuBar::make_callback_array (int size)
453 puCallback * list = new puCallback[size+1];
454 for (int i = 0; i <= size; i++)
456 _callback_arrays.push_back(list);
460 // end of menubar.cxx