1 // dialog.cxx: implementation of an XML-configurable dialog box.
3 #include <Input/input.hxx>
10 ////////////////////////////////////////////////////////////////////////
12 ////////////////////////////////////////////////////////////////////////
15 * User data for a GUI object.
19 GUIInfo (FGDialog * d);
23 vector <FGBinding *> bindings;
31 action_callback (puObject * object)
33 GUIInfo * info = (GUIInfo *)object->getUserData();
34 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
35 gui->setActiveDialog(info->dialog);
36 int nBindings = info->bindings.size();
37 for (int i = 0; i < nBindings; i++) {
38 info->bindings[i]->fire();
39 if (gui->getActiveDialog() == 0)
42 gui->setActiveDialog(0);
47 ////////////////////////////////////////////////////////////////////////
48 // Static helper functions.
49 ////////////////////////////////////////////////////////////////////////
52 * Copy a property value to a PUI object.
55 copy_to_pui (SGPropertyNode * node, puObject * object)
57 switch (node->getType()) {
58 case SGPropertyNode::BOOL:
59 case SGPropertyNode::INT:
60 case SGPropertyNode::LONG:
61 object->setValue(node->getIntValue());
63 case SGPropertyNode::FLOAT:
64 case SGPropertyNode::DOUBLE:
65 object->setValue(node->getFloatValue());
68 object->setValue(node->getStringValue());
75 copy_from_pui (puObject * object, SGPropertyNode * node)
77 switch (node->getType()) {
78 case SGPropertyNode::BOOL:
79 case SGPropertyNode::INT:
80 case SGPropertyNode::LONG:
81 node->setIntValue(object->getIntegerValue());
83 case SGPropertyNode::FLOAT:
84 case SGPropertyNode::DOUBLE:
85 node->setFloatValue(object->getFloatValue());
88 node->setStringValue(object->getStringValue());
95 ////////////////////////////////////////////////////////////////////////
96 // Implementation of GUIInfo.
97 ////////////////////////////////////////////////////////////////////////
99 GUIInfo::GUIInfo (FGDialog * d)
106 for (int i = 0; i < bindings.size(); i++) {
114 ////////////////////////////////////////////////////////////////////////
115 // Implementation of FGDialog.
116 ////////////////////////////////////////////////////////////////////////
118 FGDialog::FGDialog (SGPropertyNode_ptr props)
124 FGDialog::~FGDialog ()
126 puDeleteObject(_object);
130 // Delete all the arrays we made
131 // and were forced to keep around
132 // because PUI won't do its own
133 // memory management.
134 for (i = 0; i < _char_arrays.size(); i++) {
135 for (int j = 0; _char_arrays[i][j] != 0; j++)
136 free(_char_arrays[i][j]); // added with strdup
137 delete _char_arrays[i];
140 // Delete all the info objects we
141 // were forced to keep around because
142 // PUI cannot delete its own user data.
143 for (i = 0; i < _info.size(); i++) {
144 delete (GUIInfo *)_info[i];
148 // Finally, delete the property links.
149 for (i = 0; i < _propertyObjects.size(); i++) {
150 delete _propertyObjects[i];
151 _propertyObjects[i] = 0;
156 FGDialog::updateValue (const char * objectName)
158 for (int i = 0; i < _propertyObjects.size(); i++) {
159 const string &name = _propertyObjects[i]->name;
160 if (name == objectName)
161 copy_to_pui(_propertyObjects[i]->node,
162 _propertyObjects[i]->object);
167 FGDialog::applyValue (const char * objectName)
169 for (int i = 0; i < _propertyObjects.size(); i++) {
170 if (_propertyObjects[i]->name == objectName)
171 copy_from_pui(_propertyObjects[i]->object,
172 _propertyObjects[i]->node);
177 FGDialog::updateValues ()
179 for (int i = 0; i < _propertyObjects.size(); i++)
180 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
184 FGDialog::applyValues ()
186 for (int i = 0; i < _propertyObjects.size(); i++)
187 copy_from_pui(_propertyObjects[i]->object,
188 _propertyObjects[i]->node);
192 FGDialog::display (SGPropertyNode_ptr props)
195 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
199 _object = makeObject(props, 1024, 768);
204 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
205 << props->getStringValue("name", "[unnamed]")
206 << " does not contain a proper GUI definition");
211 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
213 int width = props->getIntValue("width", parentWidth);
214 int height = props->getIntValue("height", parentHeight);
216 int x = props->getIntValue("x", (parentWidth - width) / 2);
217 int y = props->getIntValue("y", (parentHeight - height) / 2);
219 string type = props->getName();
221 type = props->getStringValue("type");
223 SG_LOG(SG_GENERAL, SG_ALERT, "No type specified for GUI object");
227 if (type == "dialog") {
229 if (props->getBoolValue("modal", false))
230 dialog = new puDialogBox(x, y);
232 dialog = new puPopup(x, y);
233 setupGroup(dialog, props, width, height, true);
235 } else if (type == "group") {
236 puGroup * group = new puGroup(x, y);
237 setupGroup(group, props, width, height, false);
239 } else if (type == "input") {
240 puInput * input = new puInput(x, y, x + width, y + height);
241 setupObject(input, props);
243 } else if (type == "text") {
244 puText * text = new puText(x, y);
245 setupObject(text, props);
247 } else if (type == "checkbox") {
249 b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
250 setupObject(b, props);
252 } else if (type == "button") {
254 const char * legend = props->getStringValue("legend", "[none]");
255 if (props->getBoolValue("one-shot", true))
256 b = new puOneShot(x, y, legend);
258 b = new puButton(x, y, legend);
259 setupObject(b, props);
261 } else if (type == "combo") {
262 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
263 char ** entries = make_char_array(value_nodes.size());
264 for (int i = 0, j = value_nodes.size() - 1;
265 i < value_nodes.size();
267 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
269 new puComboBox(x, y, x + width, y + height, entries,
270 props->getBoolValue("editable", false));
271 setupObject(combo, props);
273 } else if (type == "slider") {
274 bool vertical = props->getBoolValue("vertical", false);
275 puSlider * slider = new puSlider(x, y, (vertical ? height : width));
276 slider->setMinValue(props->getFloatValue("min", 0.0));
277 slider->setMaxValue(props->getFloatValue("max", 1.0));
278 setupObject(slider, props);
280 } else if (type == "dial") {
281 puDial * dial = new puDial(x, y, width);
282 dial->setMinValue(props->getFloatValue("min", 0.0));
283 dial->setMaxValue(props->getFloatValue("max", 1.0));
284 dial->setWrap(props->getBoolValue("wrap", true));
285 setupObject(dial, props);
293 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
295 if (props->hasValue("legend"))
296 object->setLegend(props->getStringValue("legend"));
298 if (props->hasValue("label"))
299 object->setLabel(props->getStringValue("label"));
301 if (props->hasValue("property")) {
302 const char * name = props->getStringValue("name");
305 const char * propname = props->getStringValue("property");
306 SGPropertyNode_ptr node = fgGetNode(propname, true);
307 copy_to_pui(node, object);
309 _propertyObjects.push_back(new PropertyObject(name, object, node));
312 vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
313 if (nodes.size() > 0) {
314 GUIInfo * info = new GUIInfo(this);
316 for (int i = 0; i < nodes.size(); i++)
317 info->bindings.push_back(new FGBinding(nodes[i]));
318 object->setCallback(action_callback);
319 object->setUserData(info);
320 _info.push_back(info);
323 object->makeReturnDefault(props->getBoolValue("default"));
327 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
328 int width, int height, bool makeFrame)
330 setupObject(group, props);
333 new puFrame(0, 0, width, height);
335 int nChildren = props->nChildren();
336 for (int i = 0; i < nChildren; i++)
337 makeObject(props->getChild(i), width, height);
342 FGDialog::make_char_array (int size)
344 char ** list = new char*[size+1];
345 for (int i = 0; i <= size; i++)
347 _char_arrays.push_back(list);
353 ////////////////////////////////////////////////////////////////////////
354 // Implementation of FGDialog::PropertyObject.
355 ////////////////////////////////////////////////////////////////////////
357 FGDialog::PropertyObject::PropertyObject (const char * n,
359 SGPropertyNode_ptr p)