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); puDeleteObject(_object);
unsigned int i; unsigned int i;
// Delete all the arrays we made // Delete all the arrays we made
// and were forced to keep around // and were forced to keep around
// because PUI won't do its own // because PUI won't do its own
// memory management. // memory management.
for (i = 0; i < _char_arrays.size(); i++) { for (i = 0; i < _char_arrays.size(); i++)
for (int j = 0; _char_arrays[i][j] != 0; j++) destroy_char_array(_char_arrays[i]);
free(_char_arrays[i][j]); // added with strdup
delete[] _char_arrays[i];
}
// Delete all the info objects we // Delete all the info objects we
// were forced to keep around because // were forced to keep around because
@ -376,7 +372,6 @@ FGDialog::~FGDialog ()
delete (GUIInfo *)_info[i]; delete (GUIInfo *)_info[i];
_info[i] = 0; _info[i] = 0;
} }
// Finally, delete the property links. // Finally, delete the property links.
for (i = 0; i < _propertyObjects.size(); i++) { for (i = 0; i < _propertyObjects.size(); i++) {
delete _propertyObjects[i]; delete _propertyObjects[i];
@ -385,39 +380,29 @@ FGDialog::~FGDialog ()
} }
void void
FGDialog::updateValue (const char * objectName) FGDialog::updateValues (const char * objectName)
{ {
for (unsigned int i = 0; i < _propertyObjects.size(); i++) { for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
const string &name = _propertyObjects[i]->name; const string &name = _propertyObjects[i]->name;
if (name == objectName) if (name.size() && name != objectName)
copy_to_pui(_propertyObjects[i]->node, continue;
_propertyObjects[i]->object);
puObject *obj = _propertyObjects[i]->object;
copy_to_pui(_propertyObjects[i]->node, obj);
} }
} }
void void
FGDialog::applyValue (const char * objectName) FGDialog::applyValues (const char * objectName)
{ {
for (unsigned int i = 0; i < _propertyObjects.size(); i++) { for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
if (_propertyObjects[i]->name == objectName) const string &name = _propertyObjects[i]->name;
copy_from_pui(_propertyObjects[i]->object, if (name.size() && name != objectName)
_propertyObjects[i]->node); 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, copy_from_pui(_propertyObjects[i]->object,
_propertyObjects[i]->node); _propertyObjects[i]->node);
}
} }
void void
@ -554,11 +539,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
return obj; return obj;
} else if (type == "list") { } else if (type == "list") {
vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value"); char ** entries = value_list(props);
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());
int slider_width = props->getIntValue("slider", 20); int slider_width = props->getIntValue("slider", 20);
puList * obj = new puList(x, y, x + width, y + height, entries, slider_width); puList * obj = new puList(x, y, x + width, y + height, entries, slider_width);
if (presetSize) if (presetSize)
@ -629,11 +610,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
return obj; return obj;
} else if (type == "combo") { } else if (type == "combo") {
vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value"); char ** entries = value_list(props);
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());
puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries, puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
props->getBoolValue("editable", false)); props->getBoolValue("editable", false));
setupObject(obj, props); setupObject(obj, props);
@ -943,6 +920,16 @@ FGDialog::getKeyCode(const char *str)
return key; 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 ** char **
FGDialog::make_char_array (int size) FGDialog::make_char_array (int size)
{ {
@ -953,6 +940,14 @@ FGDialog::make_char_array (int size)
return list; 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 * This method copies values from the FlightGear property tree to
* the GUI object(s). * the GUI object(s).
@ -62,11 +63,12 @@ public:
* @param objectName The name of the GUI object(s) to update. * @param objectName The name of the GUI object(s) to update.
* Use the empty name for all unnamed objects. * 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 * This method copies values from the GUI object(s) to the
* FlightGear property tree. * FlightGear property tree.
@ -74,25 +76,7 @@ public:
* @param objectName The name of the GUI object(s) to update. * @param objectName The name of the GUI object(s) to update.
* Use the empty name for all unnamed objects. * Use the empty name for all unnamed objects.
*/ */
virtual void applyValue (const char * objectName); virtual void applyValues (const char * objectName = 0);
/**
* 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 ();
/** /**
@ -172,8 +156,11 @@ private:
// PUI doesn't copy arrays, so we have to allocate string arrays // PUI doesn't copy arrays, so we have to allocate string arrays
// and then keep pointers so that we can delete them when the // 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 ** make_char_array (int size);
char ** value_list(const SGPropertyNode * prop);
void destroy_char_array (char **array);
vector<char **> _char_arrays; vector<char **> _char_arrays;
}; };

View file

@ -1104,11 +1104,7 @@ do_dialog_update (const SGPropertyNode * arg)
dialog = gui->getActiveDialog(); dialog = gui->getActiveDialog();
if (dialog != 0) { if (dialog != 0) {
if (arg->hasValue("object-name")) { dialog->updateValues(arg->getStringValue("object-name"));
dialog->updateValue(arg->getStringValue("object-name"));
} else {
dialog->updateValues();
}
return true; return true;
} else { } else {
return false; return false;
@ -1132,12 +1128,7 @@ do_dialog_apply (const SGPropertyNode * arg)
dialog = gui->getActiveDialog(); dialog = gui->getActiveDialog();
if (dialog != 0) { if (dialog != 0) {
if (arg->hasValue("object-name")) { dialog->applyValues(arg->getStringValue("object-name"));
const char * name = arg->getStringValue("object-name");
dialog->applyValue(name);
} else {
dialog->applyValues();
}
return true; return true;
} else { } else {
return false; return false;