9 #include <simgear/debug/logstream.hxx>
11 #include <Autopilot/auto_gui.hxx>
12 #include <Input/input.hxx>
13 #include <Main/globals.hxx>
14 #include <Main/fg_props.hxx>
16 #include "new_gui.hxx"
17 #include "menubar.hxx"
21 ////////////////////////////////////////////////////////////////////////
24 // Deprecated wrappers for old menu commands.
26 // DO NOT ADD TO THESE. THEY WILL BE DELETED SOON!
28 // These are defined in gui_funcs.cxx. They should be replaced with
29 // user-configured dialogs and new commands where necessary.
30 ////////////////////////////////////////////////////////////////////////
32 extern void reInit (puObject *);
34 do_reinit_dialog (const SGPropertyNode * arg)
40 #if defined(TR_HIRES_SNAP)
41 extern void dumpHiResSnapShot (puObject *);
43 do_hires_snapshot_dialog (const SGPropertyNode * arg)
48 #endif // TR_HIRES_SNAP
50 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
51 extern void printScreen (puObject *);
53 do_print_dialog (const SGPropertyNode * arg)
60 extern void prop_pickerView (puObject *);
62 do_properties_dialog (const SGPropertyNode * arg)
68 extern void AddWayPoint (puObject *);
70 do_ap_add_waypoint_dialog (const SGPropertyNode * arg)
76 extern void PopWayPoint (puObject *);
78 do_ap_pop_waypoint_dialog (const SGPropertyNode * arg)
84 extern void ClearRoute (puObject *);
86 do_ap_clear_route_dialog (const SGPropertyNode * arg)
93 extern void fgAPAdjust (puObject *);
95 do_ap_adjust_dialog (const SGPropertyNode * arg)
102 extern void fgLatLonFormatToggle (puObject *);
104 do_lat_lon_format_dialog (const SGPropertyNode * arg)
106 fgLatLonFormatToggle(0);
110 extern void helpCb (puObject *);
112 do_help_dialog (const SGPropertyNode * arg)
120 SGCommandMgr::command_t command;
121 } deprecated_dialogs [] = {
122 { "old-reinit-dialog", do_reinit_dialog },
123 #if defined(TR_HIRES_SNAP)
124 { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
126 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
127 { "old-print-dialog", do_print_dialog },
129 { "old-properties-dialog", do_properties_dialog },
130 { "old-ap-add-waypoint-dialog", do_ap_add_waypoint_dialog },
131 { "old-ap-pop-waypoint-dialog", do_ap_pop_waypoint_dialog },
132 { "old-ap-clear-route-dialog", do_ap_clear_route_dialog },
133 { "old-lat-lon-format-dialog", do_lat_lon_format_dialog },
134 { "old-help-dialog", do_help_dialog },
139 add_deprecated_dialogs ()
141 SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
142 for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
143 SG_LOG(SG_GENERAL, SG_INFO, " " << deprecated_dialogs[i].name);
144 globals->get_commands()->addCommand(deprecated_dialogs[i].name,
145 deprecated_dialogs[i].command);
151 ////////////////////////////////////////////////////////////////////////
153 ////////////////////////////////////////////////////////////////////////
157 menu_callback (puObject * object)
159 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
160 gui->getMenuBar()->fireItem(object);
165 ////////////////////////////////////////////////////////////////////////
166 // Implementation of FGMenuBar.
167 ////////////////////////////////////////////////////////////////////////
170 FGMenuBar::FGMenuBar ()
176 FGMenuBar::~FGMenuBar ()
184 delete _menuBar; // FIXME: check if PUI owns the pointer
186 // FIXME: temporary commands to get at
187 // old, hard-coded dialogs.
188 add_deprecated_dialogs();
208 FGMenuBar::isVisible () const
214 FGMenuBar::fireItem (puObject * item)
216 const char * name = item->getLegend();
217 vector<FGBinding *> &bindings = _bindings[name];
218 int nBindings = bindings.size();
220 for (int i = 0; i < nBindings; i++)
225 FGMenuBar::make_menu (SGPropertyNode * node)
227 const char * name = strdup(node->getStringValue("label"));
228 vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
230 int array_size = item_nodes.size();
232 char ** items = make_char_array(array_size);
233 puCallback * callbacks = make_callback_array(array_size);
235 for (unsigned int i = 0, j = item_nodes.size() - 1;
236 i < item_nodes.size();
239 // Set up the PUI entries for this item
240 items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
241 callbacks[j] = menu_callback;
243 // Load all the bindings for this item
244 vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
245 SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
247 for (unsigned int k = 0; k < bindings.size(); k++) {
249 SGPropertyNode *binding;
250 while (dest->getChild("binding", m))
253 binding = dest->getChild("binding", m, true);
254 copyProperties(bindings[k], binding);
255 _bindings[items[j]].push_back(new FGBinding(binding));
259 _menuBar->add_submenu(name, items, callbacks);
263 FGMenuBar::make_menubar ()
265 SGPropertyNode *targetpath;
267 targetpath = fgGetNode("/sim/menubar/default",true);
268 // fgLoadProps("gui/menubar.xml", targetpath);
270 /* NOTE: there is no check to see whether there's any usable data at all
272 * This would also have the advantage of being able to create some kind of
273 * 'fallback' menu - just in case that either menubar.xml is empty OR that
274 * its XML data is not valid, that way we would avoid displaying an
275 * unusable menubar without any functionality - if we decided to add another
276 * char * element to the commands structure in
277 * $FG_SRC/src/Main/fgcommands.cxx
278 * we could additionally save each function's (short) description and use
279 * this as label for the fallback PUI menubar item labels - as a workaround
280 * one might simply use the internal fgcommands and put them into the
281 * fallback menu, so that the user is at least able to re-init the menu
282 * loading - just in case there was some malformed XML in it
283 * (it happend to me ...)
286 make_menubar(targetpath);
289 /* WARNING: We aren't yet doing any validation of what's found - but since
290 * this isn't done with menubar.xml either, it should not really matter
291 * right now. Although one should later on consider to validate the
292 * contents, whether they are representing a 'legal' menubar structure.
295 FGMenuBar::make_menubar(const SGPropertyNode * props)
299 _menuBar = new puMenuBar;
301 vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
302 for (unsigned int i = 0; i < menu_nodes.size(); i++)
303 make_menu(menu_nodes[i]);
315 FGMenuBar::destroy_menubar ()
321 puDeleteObject(_menuBar);
325 // Delete all the character arrays
326 // we were forced to keep around for
328 SG_LOG(SG_GENERAL, SG_INFO, "Deleting char arrays");
329 for (i = 0; i < _char_arrays.size(); i++) {
330 for (int j = 0; _char_arrays[i][j] != 0; j++)
331 free(_char_arrays[i][j]); // added with strdup
332 delete[] _char_arrays[i];
335 // Delete all the callback arrays
336 // we were forced to keep around for
338 SG_LOG(SG_GENERAL, SG_INFO, "Deleting callback arrays");
339 for (i = 0; i < _callback_arrays.size(); i++)
340 delete[] _callback_arrays[i];
342 // Delete all those bindings
343 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings");
344 map<string,vector<FGBinding *> >::iterator it;
345 it = _bindings.begin();
346 for (it = _bindings.begin(); it != _bindings.end(); it++) {
347 SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings for " << it->first);
348 for ( i = 0; i < it->second.size(); i++ )
349 delete it->second[i];
352 SG_LOG(SG_GENERAL, SG_INFO, "Done.");
355 struct EnabledListener : SGPropertyChangeListener {
356 void valueChanged(SGPropertyNode* node) {
357 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
360 FGMenuBar *menubar = gui->getMenuBar();
362 menubar->enable_item(node->getParent(), node->getBoolValue());
367 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
369 if (!node->hasValue("enabled"))
370 node->setBoolValue("enabled", true);
372 enable_item(node, node->getBoolValue("enabled"));
373 node->getNode("enabled")->addChangeListener(new EnabledListener());
377 FGMenuBar::make_map(const SGPropertyNode * node)
379 string base = node->getPath();
382 for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
383 obj; obj = obj->getNextObject()) {
385 // skip puPopupMenus. They are also children of _menuBar,
386 // but we access them via getUserData() (see below)
387 if (!(obj->getType() & PUCLASS_ONESHOT))
390 if (!obj->getLegend())
393 std::ostringstream menu;
394 menu << base << "/menu[" << menu_index << "]";
395 SGPropertyNode *prop = fgGetNode(menu.str().c_str());
397 SG_LOG(SG_GENERAL, SG_WARN, "menu without node: " << menu.str());
402 _entries[prop->getPath()] = obj;
403 add_enabled_listener(prop);
405 // push "item" entries
406 puPopupMenu *popup = (puPopupMenu *)obj->getUserData();
410 // the entries are for some reason reversed (last first), and we
411 // don't know yet how many will be usable; so we collect first
412 vector<puObject *> e;
413 for (puObject *me = ((puGroup *)popup)->getFirstChild();
414 me; me = me->getNextObject()) {
416 if (!me->getLegend())
422 for (unsigned int i = 0; i < e.size(); i++) {
423 std::ostringstream item;
424 item << menu.str() << "/item[" << (e.size() - i - 1) << "]";
425 prop = fgGetNode(item.str().c_str());
427 SG_LOG(SG_GENERAL, SG_WARN, "item without node: " << item.str());
430 _entries[prop->getPath()] = e[i];
431 add_enabled_listener(prop);
438 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
440 if (!node || _entries.find(node->getPath()) == _entries.end()) {
441 SG_LOG(SG_GENERAL, SG_WARN, "Trying to enable/disable "
442 "non-existent menu item");
445 puObject *object = _entries[node->getPath()];
455 FGMenuBar::make_char_array (int size)
457 char ** list = new char*[size+1];
458 for (int i = 0; i <= size; i++)
460 _char_arrays.push_back(list);
465 FGMenuBar::make_callback_array (int size)
467 puCallback * list = new puCallback[size+1];
468 for (int i = 0; i <= size; i++)
470 _callback_arrays.push_back(list);
474 // end of menubar.cxx