]> git.mxchange.org Git - flightgear.git/blobdiff - src/GUI/menubar.cxx
Fix line endings
[flightgear.git] / src / GUI / menubar.cxx
index 364b3b414e258f1b79bdd6a6ace0f8d1271c950a..e37bb4cafea6912362caef05fde126ff70788e6e 100644 (file)
 // user-configured dialogs and new commands where necessary.
 ////////////////////////////////////////////////////////////////////////
 
-extern void saveFlight (puObject *);
-static bool
-do_save_dialog (const SGPropertyNode * arg)
-{
-    saveFlight(0);
-    return true;
-}
-
-extern void loadFlight (puObject *);
-static bool
-do_load_dialog (const SGPropertyNode * arg)
-{
-    loadFlight(0);
-    return true;
-}
-
 extern void reInit (puObject *);
 static bool
 do_reinit_dialog (const SGPropertyNode * arg)
@@ -72,22 +56,6 @@ do_print_dialog (const SGPropertyNode * arg)
 }
 #endif
 
-extern void PilotOffsetAdjust (puObject *);
-static bool
-do_pilot_offset_dialog (const SGPropertyNode * arg)
-{
-    PilotOffsetAdjust(0);
-    return true;
-}
-
-extern void fgHUDalphaAdjust (puObject *);
-static bool
-do_hud_alpha_dialog (const SGPropertyNode * arg)
-{
-    fgHUDalphaAdjust(0);
-    return true;
-}
-
 extern void prop_pickerView (puObject *);
 static bool
 do_properties_dialog (const SGPropertyNode * arg)
@@ -150,8 +118,6 @@ static struct {
     const char * name;
     SGCommandMgr::command_t command;
 } deprecated_dialogs [] = {
-    { "old-save-dialog", do_save_dialog },
-    { "old-load-dialog", do_load_dialog },
     { "old-reinit-dialog", do_reinit_dialog },
 #if defined(TR_HIRES_SNAP)
     { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
@@ -159,8 +125,6 @@ static struct {
 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
     { "old-print-dialog", do_print_dialog },
 #endif
-    { "old-pilot-offset-dialog", do_pilot_offset_dialog },
-    { "old-hud-alpha-dialog", do_hud_alpha_dialog },
     { "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 },
@@ -270,17 +234,25 @@ FGMenuBar::make_menu (SGPropertyNode * node)
     for (unsigned int i = 0, j = item_nodes.size() - 1;
          i < item_nodes.size();
          i++, j--) {
-        
+
                                 // Set up the PUI entries for this item
         items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
         callbacks[j] = menu_callback;
 
                                 // Load all the bindings for this item
-        vector<SGPropertyNode_ptr> binding_nodes =
-            item_nodes[i]->getChildren("binding");
-
-        for (unsigned int k = 0; k < binding_nodes.size(); k++)
-            _bindings[items[j]].push_back(new FGBinding(binding_nodes[k]));
+        vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
+        SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
+
+        for (unsigned int k = 0; k < bindings.size(); k++) {
+            unsigned int m = 0;
+            SGPropertyNode *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));
+        }
     }
 
     _menuBar->add_submenu(name, items, callbacks);
@@ -319,7 +291,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();
@@ -330,6 +302,8 @@ FGMenuBar::make_menubar(const SGPropertyNode * props)
         make_menu(menu_nodes[i]);
 
     _menuBar->close();
+    make_object_map(props);
+
     if (_visible)
         _menuBar->reveal();
     else
@@ -377,6 +351,92 @@ FGMenuBar::destroy_menubar ()
     SG_LOG(SG_GENERAL, SG_INFO, "Done.");
 }
 
+void
+FGMenuBar::make_object_map(SGPropertyNode * node)
+{
+    unsigned int menu_index = 0;
+    vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
+    for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
+            obj; obj = obj->getNextObject()) {
+
+        // skip puPopupMenus. They are also children of _menuBar,
+        // but we access them via getUserData()  (see below)
+        if (!(obj->getType() & PUCLASS_ONESHOT))
+            continue;
+
+        if (menu_index >= menus.size()) {
+            SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
+                    << node->getPath() << "/menu[" << menu_index << ']');
+            return;
+        }
+
+        SGPropertyNode *menu = menus.at(menu_index);
+        _objects[menu->getPath()] = obj;
+        add_enabled_listener(menu);
+
+        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 there will be; so we collect first
+        vector<puObject *> e;
+        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++) {
+            if (i >= items.size()) {
+                SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
+                        << menu->getPath() << "/item[" << i << ']');
+                break;
+            }
+            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)
+{
+    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 = _objects[path];
+    if (state)
+        object->activate();
+    else
+        object->greyOut();
+
+    return true;
+}
 
 char **
 FGMenuBar::make_char_array (int size)