]> git.mxchange.org Git - flightgear.git/commitdiff
- merge FGDialog::{update,apply}Value{,s} ... there's really no need to have
authormfranz <mfranz>
Fri, 28 Apr 2006 11:49:11 +0000 (11:49 +0000)
committermfranz <mfranz>
Fri, 28 Apr 2006 11:49:11 +0000 (11:49 +0000)
  two almost identical functions for these methods. It only forces to repeat
  the redundancy for every small change to either.
- abstract out generation and destruction of plib string arrays
- abstract out generation of lists from <value> children

src/GUI/dialog.cxx
src/GUI/dialog.hxx
src/Main/fg_commands.cxx

index a40bcd2f2bb32c459107143a3b1a3cb31c24c3a8..4c55fe8d9335d652ca5167a60ad92aafa9d44494 100644 (file)
@@ -358,16 +358,12 @@ FGDialog::~FGDialog ()
     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];
-    }
+    for (i = 0; i < _char_arrays.size(); i++)
+        destroy_char_array(_char_arrays[i]);
 
                                 // Delete all the info objects we
                                 // were forced to keep around because
@@ -376,7 +372,6 @@ FGDialog::~FGDialog ()
         delete (GUIInfo *)_info[i];
         _info[i] = 0;
     }
-
                                 // Finally, delete the property links.
     for (i = 0; i < _propertyObjects.size(); i++) {
         delete _propertyObjects[i];
@@ -385,39 +380,29 @@ FGDialog::~FGDialog ()
 }
 
 void
-FGDialog::updateValue (const char * objectName)
+FGDialog::updateValues (const char * objectName)
 {
     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
         const string &name = _propertyObjects[i]->name;
-        if (name == objectName)
-            copy_to_pui(_propertyObjects[i]->node,
-                        _propertyObjects[i]->object);
-    }
-}
+        if (name.size() && name != objectName)
+            continue;
 
-void
-FGDialog::applyValue (const char * objectName)
-{
-    for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
-        if (_propertyObjects[i]->name == objectName)
-            copy_from_pui(_propertyObjects[i]->object,
-                          _propertyObjects[i]->node);
+        puObject *obj = _propertyObjects[i]->object;
+        copy_to_pui(_propertyObjects[i]->node, obj);
     }
 }
 
 void
-FGDialog::updateValues ()
+FGDialog::applyValues (const char * objectName)
 {
-    for (unsigned int i = 0; i < _propertyObjects.size(); i++)
-        copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
-}
+    for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
+        const string &name = _propertyObjects[i]->name;
+        if (name.size() && name != objectName)
+            continue;
 
-void
-FGDialog::applyValues ()
-{
-    for (unsigned int i = 0; i < _propertyObjects.size(); i++)
         copy_from_pui(_propertyObjects[i]->object,
                       _propertyObjects[i]->node);
+    }
 }
 
 void
@@ -554,11 +539,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         return obj;
 
     } else if (type == "list") {
-        vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
-        char ** entries = make_char_array(value_nodes.size());
-        for (unsigned int i = 0; i < value_nodes.size(); i++)
-            entries[i] = strdup((char *)value_nodes[i]->getStringValue());
-
+        char ** entries = value_list(props);
         int slider_width = props->getIntValue("slider", 20);
         puList * obj = new puList(x, y, x + width, y + height, entries, slider_width);
         if (presetSize)
@@ -629,11 +610,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         return obj;
 
     } else if (type == "combo") {
-        vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
-        char ** entries = make_char_array(value_nodes.size());
-        for (unsigned int i = 0; i < value_nodes.size(); i++)
-            entries[i] = strdup((char *)value_nodes[i]->getStringValue());
-
+        char ** entries = value_list(props);
         puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
                            props->getBoolValue("editable", false));
         setupObject(obj, props);
@@ -943,6 +920,16 @@ FGDialog::getKeyCode(const char *str)
     return key;
 }
 
+char **
+FGDialog::value_list (const SGPropertyNode *props)
+{
+    vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
+    char ** entries = make_char_array(value_nodes.size());
+    for (unsigned int i = 0; i < value_nodes.size(); i++)
+        entries[i] = strdup((char *)value_nodes[i]->getStringValue());
+    return entries;
+}
+
 char **
 FGDialog::make_char_array (int size)
 {
@@ -953,6 +940,14 @@ FGDialog::make_char_array (int size)
     return list;
 }
 
+void
+FGDialog::destroy_char_array (char ** array)
+{
+    for (int i = 0; array[i] != 0; i++)
+        if (array[i])
+            free(array[i]);// added with strdup
+    delete[] array;
+}
 
 \f
 ////////////////////////////////////////////////////////////////////////
index 0bad5ad18c60c778045dc7b12bd99d8b463d0600..270d8aefb9b72b8c4750e2a862c506bbd07647dd 100644 (file)
@@ -54,7 +54,8 @@ public:
 
 
     /**
-     * Update the values of all GUI objects with a specific name.
+     * Update the values of all GUI objects with a specific name,
+     * or all if name is 0 (default).
      *
      * This method copies values from the FlightGear property tree to
      * the GUI object(s).
@@ -62,11 +63,12 @@ public:
      * @param objectName The name of the GUI object(s) to update.
      *        Use the empty name for all unnamed objects.
      */
-    virtual void updateValue (const char * objectName);
+    virtual void updateValues (const char * objectName = 0);
 
 
     /**
-     * Apply the values of all GUI objects with a specific name.
+     * Apply the values of all GUI objects with a specific name,
+     * or all if name is 0 (default)
      *
      * This method copies values from the GUI object(s) to the
      * FlightGear property tree.
@@ -74,25 +76,7 @@ public:
      * @param objectName The name of the GUI object(s) to update.
      *        Use the empty name for all unnamed objects.
      */
-    virtual void applyValue (const char * objectName);
-
-
-    /**
-     * Update the values of all GUI objects.
-     *
-     * This method copies values from the FlightGear property tree to
-     * the GUI objects.
-     */
-    virtual void updateValues ();
-
-
-    /**
-     * Apply the values of all GUI objects.
-     *
-     * This method copies from the GUI objects to the FlightGear
-     * property tree properties.
-     */
-    virtual void applyValues ();
+    virtual void applyValues (const char * objectName = 0);
 
 
     /**
@@ -172,8 +156,11 @@ private:
 
     // PUI doesn't copy arrays, so we have to allocate string arrays
     // and then keep pointers so that we can delete them when the
-    // dialog closes.
+    // dialog closes. value_list() builds such a list from "value"
+    // children.
     char ** make_char_array (int size);
+    char ** value_list(const SGPropertyNode * prop);
+    void destroy_char_array (char **array);
     vector<char **> _char_arrays;
 };
 
index 710f929a0f9c80576acc59bcb5d621b75a26712b..5a4b58762717dbc4b468fd04b6d4e2d27d6b17da 100644 (file)
@@ -1104,11 +1104,7 @@ do_dialog_update (const SGPropertyNode * arg)
         dialog = gui->getActiveDialog();
 
     if (dialog != 0) {
-        if (arg->hasValue("object-name")) {
-            dialog->updateValue(arg->getStringValue("object-name"));
-        } else {
-            dialog->updateValues();
-        }
+        dialog->updateValues(arg->getStringValue("object-name"));
         return true;
     } else {
         return false;
@@ -1132,12 +1128,7 @@ do_dialog_apply (const SGPropertyNode * arg)
         dialog = gui->getActiveDialog();
 
     if (dialog != 0) {
-        if (arg->hasValue("object-name")) {
-            const char * name = arg->getStringValue("object-name");
-            dialog->applyValue(name);
-        } else {
-            dialog->applyValues();
-        }
+        dialog->applyValues(arg->getStringValue("object-name"));
         return true;
     } else {
         return false;