]> git.mxchange.org Git - flightgear.git/blobdiff - src/GUI/menubar.cxx
Autopilot: clean up the helpers code (which drives the various /internal/) properties...
[flightgear.git] / src / GUI / menubar.cxx
index 04dc676340b2bd010e980b2b045e027ed314c614..513277dfc0f97f1dbd5b75feff4166996033b95e 100644 (file)
@@ -4,14 +4,11 @@
 
 #include <string.h>
 #include <iostream>
-#include <sstream>
 #include <plib/pu.h>
 #include <simgear/debug/logstream.hxx>
+#include <simgear/structure/SGBinding.hxx>
 
-#include <Autopilot/auto_gui.hxx>
-#include <Input/input.hxx>
 #include <Main/globals.hxx>
-#include <Main/fg_props.hxx>
 
 #include "new_gui.hxx"
 #include "menubar.hxx"
 // user-configured dialogs and new commands where necessary.
 ////////////////////////////////////////////////////////////////////////
 
-extern void reInit (puObject *);
-static bool
-do_reinit_dialog (const SGPropertyNode * arg)
-{
-    reInit(0);
-    return true;
-}
-
 #if defined(TR_HIRES_SNAP)
-extern void dumpHiResSnapShot (puObject *);
+extern void dumpHiResSnapShot ();
 static bool
 do_hires_snapshot_dialog (const SGPropertyNode * arg)
 {
-    dumpHiResSnapShot(0);
+    dumpHiResSnapShot();
     return true;
 }
 #endif // TR_HIRES_SNAP
 
 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
-extern void printScreen (puObject *);
+extern void printScreen ();
 static bool
 do_print_dialog (const SGPropertyNode * arg)
 {
-    printScreen(0);
-    return true;
-}
-#endif
-
-extern void prop_pickerView (puObject *);
-static bool
-do_properties_dialog (const SGPropertyNode * arg)
-{
-    prop_pickerView(0);
-    return true;
-}
-
-extern void AddWayPoint (puObject *);
-static bool
-do_ap_add_waypoint_dialog (const SGPropertyNode * arg)
-{
-    AddWayPoint(0);
-    return true;
-}
-
-extern void PopWayPoint (puObject *);
-static bool
-do_ap_pop_waypoint_dialog (const SGPropertyNode * arg)
-{
-    PopWayPoint(0);
-    return true;
-}
-
-extern void ClearRoute (puObject *);
-static bool
-do_ap_clear_route_dialog (const SGPropertyNode * arg)
-{
-    ClearRoute(0);
-    return true;
-}
-
-#if 0
-extern void fgAPAdjust (puObject *);
-static bool
-do_ap_adjust_dialog (const SGPropertyNode * arg)
-{
-    fgAPAdjust(0);
+    printScreen();
     return true;
 }
 #endif
 
-extern void fgLatLonFormatToggle (puObject *);
-static bool
-do_lat_lon_format_dialog (const SGPropertyNode * arg)
-{
-    fgLatLonFormatToggle(0);
-    return true;
-}
-
-extern void helpCb (puObject *);
+extern void helpCb ();
 static bool
 do_help_dialog (const SGPropertyNode * arg)
 {
-    helpCb(0);
+    helpCb();
     return true;
 }
 
@@ -119,18 +58,12 @@ static struct {
     const char * name;
     SGCommandMgr::command_t command;
 } deprecated_dialogs [] = {
-    { "old-reinit-dialog", do_reinit_dialog },
 #if defined(TR_HIRES_SNAP)
     { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
 #endif
 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
     { "old-print-dialog", do_print_dialog },
 #endif
-    { "old-properties-dialog", do_properties_dialog },
-    { "old-ap-add-waypoint-dialog", do_ap_add_waypoint_dialog },
-    { "old-ap-pop-waypoint-dialog", do_ap_pop_waypoint_dialog },
-    { "old-ap-clear-route-dialog", do_ap_clear_route_dialog },
-    { "old-lat-lon-format-dialog", do_lat_lon_format_dialog },
     { "old-help-dialog", do_help_dialog },
     { 0, 0 }
 };
@@ -214,7 +147,7 @@ void
 FGMenuBar::fireItem (puObject * item)
 {
     const char * name = item->getLegend();
-    vector<FGBinding *> &bindings = _bindings[name];
+    vector<SGBinding *> &bindings = _bindings[name];
     int nBindings = bindings.size();
 
     for (int i = 0; i < nBindings; i++)
@@ -246,13 +179,13 @@ FGMenuBar::make_menu (SGPropertyNode * node)
 
         for (unsigned int k = 0; k < bindings.size(); k++) {
             unsigned int m = 0;
-            SGPropertyNode *binding;
+            SGPropertyNode_ptr binding;
             while (dest->getChild("binding", m))
                 m++;
 
             binding = dest->getChild("binding", m, true);
             copyProperties(bindings[k], binding);
-            _bindings[items[j]].push_back(new FGBinding(binding));
+            _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
         }
     }
 
@@ -292,7 +225,7 @@ FGMenuBar::make_menubar ()
  * contents, whether they are representing a 'legal' menubar structure.
  */
 void
-FGMenuBar::make_menubar(const SGPropertyNode * props) 
+FGMenuBar::make_menubar(SGPropertyNode * props) 
 {    
     // Just in case.
     destroy_menubar();
@@ -303,7 +236,7 @@ FGMenuBar::make_menubar(const SGPropertyNode * props)
         make_menu(menu_nodes[i]);
 
     _menuBar->close();
-    make_map(props);
+    make_object_map(props);
 
     if (_visible)
         _menuBar->reveal();
@@ -341,8 +274,7 @@ FGMenuBar::destroy_menubar ()
 
                                 // Delete all those bindings
     SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings");
-    map<string,vector<FGBinding *> >::iterator it;
-    it = _bindings.begin();
+    map<string,vector<SGBinding *> >::iterator it;
     for (it = _bindings.begin(); it != _bindings.end(); it++) {
         SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings for " << it->first);
         for ( i = 0; i < it->second.size(); i++ )
@@ -352,33 +284,11 @@ FGMenuBar::destroy_menubar ()
     SG_LOG(SG_GENERAL, SG_INFO, "Done.");
 }
 
-struct EnabledListener : SGPropertyChangeListener {
-    void valueChanged(SGPropertyNode* node) {
-        NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
-        if (!gui)
-            return;
-        FGMenuBar *menubar = gui->getMenuBar();
-        if (menubar)
-            menubar->enable_item(node->getParent(), node->getBoolValue());
-    }
-};
-
-void
-FGMenuBar::add_enabled_listener(SGPropertyNode * node)
-{
-    if (!node->hasValue("enabled"))
-        node->setBoolValue("enabled", true);
-
-    enable_item(node, node->getBoolValue("enabled"));
-    node->getNode("enabled")->addChangeListener(new EnabledListener());
-}
-
 void
-FGMenuBar::make_map(const SGPropertyNode * node)
+FGMenuBar::make_object_map(SGPropertyNode * node)
 {
-    string base = node->getPath();
-
-    int menu_index = 0;
+    unsigned int menu_index = 0;
+    vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
     for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
             obj; obj = obj->getNextObject()) {
 
@@ -387,54 +297,72 @@ FGMenuBar::make_map(const SGPropertyNode * node)
         if (!(obj->getType() & PUCLASS_ONESHOT))
             continue;
 
-        std::ostringstream menu;
-        menu << base << "/menu[" << menu_index << "]";
-        SGPropertyNode *prop = fgGetNode(menu.str().c_str());
-        if (!prop) {
-            SG_LOG(SG_GENERAL, SG_WARN, "menu without node: " << menu.str());
-            continue;
+        if (menu_index >= menus.size()) {
+            SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
+                    << node->getPath() << "/menu[" << menu_index << ']');
+            return;
         }
 
-        // push "menu" entry
-        _entries[prop->getPath()] = obj;
-        add_enabled_listener(prop);
+        SGPropertyNode *menu = menus.at(menu_index);
+        _objects[menu->getPath()] = obj;
+        add_enabled_listener(menu);
 
-        // push "item" entries
-        puPopupMenu *popup = (puPopupMenu *)obj->getUserData();
+        puGroup *popup = (puGroup *)obj->getUserData();
         if (!popup)
             continue;
 
         // the entries are for some reason reversed (last first), and we
-        // don't know yet how many will be usable; so we collect first
+        // don't know yet how many there will be; so we collect first
         vector<puObject *> e;
-        for (puObject *me = ((puGroup *)popup)->getFirstChild();
-                me; me = me->getNextObject())
+        for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
             e.push_back(me);
 
+        vector<SGPropertyNode_ptr> items = menu->getChildren("item");
         for (unsigned int i = 0; i < e.size(); i++) {
-            std::ostringstream item;
-            item << menu.str() << "/item[" << (e.size() - i - 1) << "]";
-            prop = fgGetNode(item.str().c_str());
-            if (!prop) {
-                SG_LOG(SG_GENERAL, SG_WARN, "item without node: " << item.str());
-                continue;
+            if (i >= items.size()) {
+                SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
+                        << menu->getPath() << "/item[" << i << ']');
+                break;
             }
-            _entries[prop->getPath()] = e[i];
-            add_enabled_listener(prop);
+            SGPropertyNode *item = items.at(e.size() - i - 1);
+            _objects[item->getPath()] = e[i];
+            add_enabled_listener(item);
         }
         menu_index++;
     }
 }
 
+struct EnabledListener : SGPropertyChangeListener {
+    void valueChanged(SGPropertyNode *node) {
+        NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
+        if (!gui)
+            return;
+        FGMenuBar *menubar = gui->getMenuBar();
+        if (menubar)
+            menubar->enable_item(node->getParent(), node->getBoolValue());
+    }
+};
+
+void
+FGMenuBar::add_enabled_listener(SGPropertyNode * node)
+{
+    if (!node->hasValue("enabled"))
+        node->setBoolValue("enabled", true);
+
+    enable_item(node, node->getBoolValue("enabled"));
+    node->getNode("enabled")->addChangeListener(new EnabledListener());
+}
+
 bool
 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
 {
-    if (!node || _entries.find(node->getPath()) == _entries.end()) {
-        SG_LOG(SG_GENERAL, SG_WARN, "Trying to enable/disable "
-            "non-existent menu item");
+    const char *path = node->getPath();
+    if (_objects.find(path) == _objects.end()) {
+        SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
+            "non-existent menu item for node `" << path << '\'');
         return false;
     }
-    puObject *object = _entries[node->getPath()];
+    puObject *object = _objects[path];
     if (state)
         object->activate();
     else