1
0
Fork 0

- 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 <value> children
This commit is contained in:
mfranz 2006-04-28 11:49:11 +00:00
parent eb19c2650d
commit 29ff85142b
3 changed files with 45 additions and 72 deletions

View 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;
puObject *obj = _propertyObjects[i]->object;
copy_to_pui(_propertyObjects[i]->node, obj);
}
}
void
FGDialog::applyValue (const char * objectName)
FGDialog::applyValues (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);
}
}
const string &name = _propertyObjects[i]->name;
if (name.size() && name != objectName)
continue;
void
FGDialog::updateValues ()
{
for (unsigned int i = 0; i < _propertyObjects.size(); i++)
copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
}
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;
}
////////////////////////////////////////////////////////////////////////

View 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;
};

View 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;