From 29ff85142bfc83c03f01c82f29f63ff78b98ec70 Mon Sep 17 00:00:00 2001 From: mfranz Date: Fri, 28 Apr 2006 11:49:11 +0000 Subject: [PATCH] - merge FGDialog::{update,apply}Value{,s} ... there's really no need to have 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 children --- src/GUI/dialog.cxx | 71 +++++++++++++++++++--------------------- src/GUI/dialog.hxx | 33 ++++++------------- src/Main/fg_commands.cxx | 13 ++------ 3 files changed, 45 insertions(+), 72 deletions(-) diff --git a/src/GUI/dialog.cxx b/src/GUI/dialog.cxx index a40bcd2f2..4c55fe8d9 100644 --- a/src/GUI/dialog.cxx +++ b/src/GUI/dialog.cxx @@ -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 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 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 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; +} //////////////////////////////////////////////////////////////////////// diff --git a/src/GUI/dialog.hxx b/src/GUI/dialog.hxx index 0bad5ad18..270d8aefb 100644 --- a/src/GUI/dialog.hxx +++ b/src/GUI/dialog.hxx @@ -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_arrays; }; diff --git a/src/Main/fg_commands.cxx b/src/Main/fg_commands.cxx index 710f929a0..5a4b58762 100644 --- a/src/Main/fg_commands.cxx +++ b/src/Main/fg_commands.cxx @@ -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; -- 2.39.5