]> git.mxchange.org Git - flightgear.git/blobdiff - src/GUI/dialog.cxx
Moved some of the low level scene graph construction code over to simgear.
[flightgear.git] / src / GUI / dialog.cxx
index 2a5bd4c3b66d556f3f92d35712651a30b40a1edd..5370e2a22c3f2dd406bc0b320336b237fd3d288a 100644 (file)
 // Callbacks.
 ////////////////////////////////////////////////////////////////////////
 
+/**
+ * User data for a GUI object.
+ */
+struct GUIInfo
+{
+    GUIInfo (FGDialog * d);
+    virtual ~GUIInfo ();
+
+    FGDialog * dialog;
+    vector <FGBinding *> bindings;
+};
+
+
 /**
  * Action callback.
  */
@@ -19,14 +32,14 @@ action_callback (puObject * object)
 {
     GUIInfo * info = (GUIInfo *)object->getUserData();
     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
-    gui->setCurrentWidget(info->widget);
+    gui->setActiveDialog(info->dialog);
     int nBindings = info->bindings.size();
     for (int i = 0; i < nBindings; i++) {
         info->bindings[i]->fire();
-        if (gui->getCurrentWidget() == 0)
+        if (gui->getActiveDialog() == 0)
             break;
     }
-    gui->setCurrentWidget(0);
+    gui->setActiveDialog(0);
 }
 
 
@@ -83,14 +96,14 @@ copy_from_pui (puObject * object, SGPropertyNode * node)
 // Implementation of GUIInfo.
 ////////////////////////////////////////////////////////////////////////
 
-GUIInfo::GUIInfo (FGDialog * w)
-    : widget(w)
+GUIInfo::GUIInfo (FGDialog * d)
+    : dialog(d)
 {
 }
 
 GUIInfo::~GUIInfo ()
 {
-    for (int i = 0; i < bindings.size(); i++) {
+    for (unsigned int i = 0; i < bindings.size(); i++) {
         delete bindings[i];
         bindings[i] = 0;
     }
@@ -102,7 +115,7 @@ GUIInfo::~GUIInfo ()
 // Implementation of FGDialog.
 ////////////////////////////////////////////////////////////////////////
 
-FGDialog::FGDialog (SGPropertyNode_ptr props)
+FGDialog::FGDialog (SGPropertyNode * props)
     : _object(0)
 {
     display(props);
@@ -110,14 +123,29 @@ FGDialog::FGDialog (SGPropertyNode_ptr props)
 
 FGDialog::~FGDialog ()
 {
-    delete _object;
+    puDeleteObject(_object);
+
+    unsigned int i;
+
+                                // Delete all the arrays we made
+                                // and were forced to keep around
+                                // because PUI won't do its own
+                                // memory management.
+    for (i = 0; i < _char_arrays.size(); i++) {
+        for (int j = 0; _char_arrays[i][j] != 0; j++)
+            free(_char_arrays[i][j]); // added with strdup
+        delete _char_arrays[i];
+    }
 
-    int i;
+                                // Delete all the info objects we
+                                // were forced to keep around because
+                                // PUI cannot delete its own user data.
     for (i = 0; i < _info.size(); i++) {
-        delete _info[i];
+        delete (GUIInfo *)_info[i];
         _info[i] = 0;
     }
 
+                                // Finally, delete the property links.
     for (i = 0; i < _propertyObjects.size(); i++) {
         delete _propertyObjects[i];
         _propertyObjects[i] = 0;
@@ -127,7 +155,7 @@ FGDialog::~FGDialog ()
 void
 FGDialog::updateValue (const char * objectName)
 {
-    for (int i = 0; i < _propertyObjects.size(); i++) {
+    for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
         const string &name = _propertyObjects[i]->name;
         if (name == objectName)
             copy_to_pui(_propertyObjects[i]->node,
@@ -138,7 +166,7 @@ FGDialog::updateValue (const char * objectName)
 void
 FGDialog::applyValue (const char * objectName)
 {
-    for (int i = 0; i < _propertyObjects.size(); i++) {
+    for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
         if (_propertyObjects[i]->name == objectName)
             copy_from_pui(_propertyObjects[i]->object,
                           _propertyObjects[i]->node);
@@ -148,27 +176,29 @@ FGDialog::applyValue (const char * objectName)
 void
 FGDialog::updateValues ()
 {
-    for (int i = 0; i < _propertyObjects.size(); i++)
+    for (unsigned int i = 0; i < _propertyObjects.size(); i++)
         copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
 }
 
 void
 FGDialog::applyValues ()
 {
-    for (int i = 0; i < _propertyObjects.size(); i++)
+    for (unsigned int i = 0; i < _propertyObjects.size(); i++)
         copy_from_pui(_propertyObjects[i]->object,
                       _propertyObjects[i]->node);
 }
 
 void
-FGDialog::display (SGPropertyNode_ptr props)
+FGDialog::display (SGPropertyNode * props)
 {
     if (_object != 0) {
         SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
         return;
     }
 
-    _object = makeObject(props, 1024, 768);
+    _object = makeObject(props,
+                      globals->get_props()->getIntValue("/sim/startup/xsize"),
+                      globals->get_props()->getIntValue("/sim/startup/ysize"));
 
     if (_object != 0) {
         _object->reveal();
@@ -190,11 +220,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
 
     string type = props->getName();
     if (type == "")
-        type = props->getStringValue("type");
-    if (type == "") {
-        SG_LOG(SG_GENERAL, SG_ALERT, "No type specified for GUI object");
-        return 0;
-    }
+        type = "dialog";
 
     if (type == "dialog") {
         puPopup * dialog;
@@ -232,18 +258,47 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         return b;
     } else if (type == "combo") {
         vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
-        char ** entries = new char*[value_nodes.size()+1];
-        for (int i = 0, j = value_nodes.size() - 1;
+        char ** entries = make_char_array(value_nodes.size());
+        for (unsigned int i = 0, j = value_nodes.size() - 1;
              i < value_nodes.size();
              i++, j--)
-            entries[i] = (char *)value_nodes[i]->getStringValue();
-        entries[value_nodes.size()] = 0;
+            entries[i] = strdup((char *)value_nodes[i]->getStringValue());
         puComboBox * combo =
             new puComboBox(x, y, x + width, y + height, entries,
                            props->getBoolValue("editable", false));
-//         delete entries;
         setupObject(combo, props);
         return combo;
+    } else if (type == "slider") {
+        bool vertical = props->getBoolValue("vertical", false);
+        puSlider * slider = new puSlider(x, y, (vertical ? height : width));
+        slider->setMinValue(props->getFloatValue("min", 0.0));
+        slider->setMaxValue(props->getFloatValue("max", 1.0));
+        setupObject(slider, props);
+        return slider;
+    } else if (type == "dial") {
+        puDial * dial = new puDial(x, y, width);
+        dial->setMinValue(props->getFloatValue("min", 0.0));
+        dial->setMaxValue(props->getFloatValue("max", 1.0));
+        dial->setWrap(props->getBoolValue("wrap", true));
+        setupObject(dial, props);
+        return dial;
+    } else if (type == "select") {
+        vector<SGPropertyNode_ptr> value_nodes;
+        SGPropertyNode * selection_node =
+                fgGetNode(props->getChild("selection")->getStringValue(), true);
+
+        for (int q = 0; q < selection_node->nChildren(); q++)
+            value_nodes.push_back(selection_node->getChild(q));
+
+        char ** entries = make_char_array(value_nodes.size());
+        for (unsigned int i = 0, j = value_nodes.size() - 1;
+             i < value_nodes.size();
+             i++, j--)
+            entries[i] = strdup((char *)value_nodes[i]->getName());
+        puSelectBox * select =
+            new puSelectBox(x, y, x + width, y + height, entries);
+        setupObject(select, props);
+        return select;
     } else {
         return 0;
     }
@@ -273,7 +328,7 @@ FGDialog::setupObject (puObject * object, SGPropertyNode * props)
     if (nodes.size() > 0) {
         GUIInfo * info = new GUIInfo(this);
 
-        for (int i = 0; i < nodes.size(); i++)
+        for (unsigned int i = 0; i < nodes.size(); i++)
             info->bindings.push_back(new FGBinding(nodes[i]));
         object->setCallback(action_callback);
         object->setUserData(info);
@@ -298,6 +353,16 @@ FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
     group->close();
 }
 
+char **
+FGDialog::make_char_array (int size)
+{
+    char ** list = new char*[size+1];
+    for (int i = 0; i <= size; i++)
+        list[i] = 0;
+    _char_arrays.push_back(list);
+    return list;
+}
+
 
 \f
 ////////////////////////////////////////////////////////////////////////