1 // dialog.cxx: implementation of an XML-configurable dialog box.
3 #include <Input/input.hxx>
9 #include "AirportList.hxx"
12 int fgPopup::checkHit(int button, int updown, int x, int y)
14 int result = puPopup::checkHit(button, updown, x, y);
16 // This is annoying. We would really want a true result from the
17 // superclass to indicate "handled by child object", but all it
18 // tells us is that the pointer is inside the dialog. So do the
19 // intersection test (again) to make sure we don't start a drag
20 // when inside controls.
21 if(!result) return result;
22 puObject* child = getFirstChild();
23 if(child) child = child->getNextObject(); // Skip the puFrame
26 child->getAbsolutePosition(&cx, &cy);
27 child->getSize(&cw, &ch);
28 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
30 child = child->getNextObject();
33 // Finally, handle the mouse event
34 if(updown == PU_DOWN) {
36 getPosition(&px, &py);
40 } else if(updown == PU_DRAG && _dragging) {
41 setPosition(x + _dX, y + _dY);
49 ////////////////////////////////////////////////////////////////////////
51 ////////////////////////////////////////////////////////////////////////
54 * User data for a GUI object.
58 GUIInfo (FGDialog * d);
62 vector <FGBinding *> bindings;
70 action_callback (puObject * object)
72 GUIInfo * info = (GUIInfo *)object->getUserData();
73 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
74 gui->setActiveDialog(info->dialog);
75 int nBindings = info->bindings.size();
76 for (int i = 0; i < nBindings; i++) {
77 info->bindings[i]->fire();
78 if (gui->getActiveDialog() == 0)
81 gui->setActiveDialog(0);
86 ////////////////////////////////////////////////////////////////////////
87 // Static helper functions.
88 ////////////////////////////////////////////////////////////////////////
91 * Copy a property value to a PUI object.
94 copy_to_pui (SGPropertyNode * node, puObject * object)
96 // Treat puText objects specially, so their "values" can be set
98 if(object->getType() & PUCLASS_TEXT) {
99 object->setLabel(node->getStringValue());
103 switch (node->getType()) {
104 case SGPropertyNode::BOOL:
105 case SGPropertyNode::INT:
106 case SGPropertyNode::LONG:
107 object->setValue(node->getIntValue());
109 case SGPropertyNode::FLOAT:
110 case SGPropertyNode::DOUBLE:
111 object->setValue(node->getFloatValue());
114 object->setValue(node->getStringValue());
121 copy_from_pui (puObject * object, SGPropertyNode * node)
123 // puText objects are immutable, so should not be copied out
124 if(object->getType() & PUCLASS_TEXT)
127 switch (node->getType()) {
128 case SGPropertyNode::BOOL:
129 case SGPropertyNode::INT:
130 case SGPropertyNode::LONG:
131 node->setIntValue(object->getIntegerValue());
133 case SGPropertyNode::FLOAT:
134 case SGPropertyNode::DOUBLE:
135 node->setFloatValue(object->getFloatValue());
138 node->setStringValue(object->getStringValue());
145 ////////////////////////////////////////////////////////////////////////
146 // Implementation of GUIInfo.
147 ////////////////////////////////////////////////////////////////////////
149 GUIInfo::GUIInfo (FGDialog * d)
156 for (unsigned int i = 0; i < bindings.size(); i++) {
164 ////////////////////////////////////////////////////////////////////////
165 // Implementation of FGDialog.
166 ////////////////////////////////////////////////////////////////////////
168 FGDialog::FGDialog (SGPropertyNode * props)
174 FGDialog::~FGDialog ()
176 puDeleteObject(_object);
180 // Delete all the arrays we made
181 // and were forced to keep around
182 // because PUI won't do its own
183 // memory management.
184 for (i = 0; i < _char_arrays.size(); i++) {
185 for (int j = 0; _char_arrays[i][j] != 0; j++)
186 free(_char_arrays[i][j]); // added with strdup
187 delete[] _char_arrays[i];
190 // Delete all the info objects we
191 // were forced to keep around because
192 // PUI cannot delete its own user data.
193 for (i = 0; i < _info.size(); i++) {
194 delete (GUIInfo *)_info[i];
198 // Finally, delete the property links.
199 for (i = 0; i < _propertyObjects.size(); i++) {
200 delete _propertyObjects[i];
201 _propertyObjects[i] = 0;
206 FGDialog::updateValue (const char * objectName)
208 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
209 const string &name = _propertyObjects[i]->name;
210 if (name == objectName)
211 copy_to_pui(_propertyObjects[i]->node,
212 _propertyObjects[i]->object);
217 FGDialog::applyValue (const char * objectName)
219 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
220 if (_propertyObjects[i]->name == objectName)
221 copy_from_pui(_propertyObjects[i]->object,
222 _propertyObjects[i]->node);
227 FGDialog::updateValues ()
229 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
230 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
234 FGDialog::applyValues ()
236 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
237 copy_from_pui(_propertyObjects[i]->object,
238 _propertyObjects[i]->node);
244 for (unsigned int i = 0; i < _liveObjects.size(); i++)
245 copy_to_pui(_liveObjects[i]->node, _liveObjects[i]->object);
249 FGDialog::display (SGPropertyNode * props)
252 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
256 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
257 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
259 bool userx = props->hasValue("x");
260 bool usery = props->hasValue("y");
261 bool userw = props->hasValue("width");
262 bool userh = props->hasValue("height");
264 LayoutWidget wid(props);
267 wid.calcPrefSize(&pw, &ph);
268 pw = props->getIntValue("width", pw);
269 ph = props->getIntValue("height", ph);
270 int px = props->getIntValue("x", (screenw - pw) / 2);
271 int py = props->getIntValue("y", (screenh - ph) / 2);
272 wid.layout(px, py, pw, ph);
274 _object = makeObject(props, screenw, screenh);
276 // Remove automatically generated properties, so the layout looks
277 // the same next time around.
278 if(!userx) props->removeChild("x");
279 if(!usery) props->removeChild("y");
280 if(!userw) props->removeChild("width");
281 if(!userh) props->removeChild("height");
286 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
287 << props->getStringValue("name", "[unnamed]")
288 << " does not contain a proper GUI definition");
293 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
295 bool presetSize = props->hasValue("width") && props->hasValue("height");
296 int width = props->getIntValue("width", parentWidth);
297 int height = props->getIntValue("height", parentHeight);
298 int x = props->getIntValue("x", (parentWidth - width) / 2);
299 int y = props->getIntValue("y", (parentHeight - height) / 2);
301 string type = props->getName();
305 if (type == "dialog") {
307 if (props->getBoolValue("modal", false))
308 dialog = new puDialogBox(x, y);
310 dialog = new fgPopup(x, y);
311 setupGroup(dialog, props, width, height, true);
313 } else if (type == "group") {
314 puGroup * group = new puGroup(x, y);
315 setupGroup(group, props, width, height, false);
317 } else if (type == "list") {
318 puList * list = new puList(x, y, x + width, y + height);
319 setupObject(list, props);
321 } else if (type == "airport-list") {
322 AirportList * list = new AirportList(x, y, x + width, y + height);
323 setupObject(list, props);
325 } else if (type == "input") {
326 puInput * input = new puInput(x, y, x + width, y + height);
327 setupObject(input, props);
329 } else if (type == "text") {
330 puText * text = new puText(x, y);
331 setupObject(text, props);
332 // Layed-out objects need their size set, and non-layout ones
333 // get a different placement.
334 if(presetSize) text->setSize(width, height);
335 else text->setLabelPlace(PUPLACE_LABEL_DEFAULT);
337 } else if (type == "checkbox") {
339 b = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
340 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
341 setupObject(b, props);
343 } else if (type == "radio") {
345 b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
346 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
347 setupObject(b, props);
349 } else if (type == "button") {
351 const char * legend = props->getStringValue("legend", "[none]");
352 if (props->getBoolValue("one-shot", true))
353 b = new puOneShot(x, y, legend);
355 b = new puButton(x, y, legend);
357 b->setSize(width, height);
358 setupObject(b, props);
360 } else if (type == "combo") {
361 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
362 char ** entries = make_char_array(value_nodes.size());
363 for (unsigned int i = 0, j = value_nodes.size() - 1;
364 i < value_nodes.size();
366 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
368 new puComboBox(x, y, x + width, y + height, entries,
369 props->getBoolValue("editable", false));
370 setupObject(combo, props);
372 } else if (type == "slider") {
373 bool vertical = props->getBoolValue("vertical", false);
374 puSlider * slider = new puSlider(x, y, (vertical ? height : width));
375 slider->setMinValue(props->getFloatValue("min", 0.0));
376 slider->setMaxValue(props->getFloatValue("max", 1.0));
377 setupObject(slider, props);
379 slider->setSize(width, height);
381 } else if (type == "dial") {
382 puDial * dial = new puDial(x, y, width);
383 dial->setMinValue(props->getFloatValue("min", 0.0));
384 dial->setMaxValue(props->getFloatValue("max", 1.0));
385 dial->setWrap(props->getBoolValue("wrap", true));
386 setupObject(dial, props);
388 } else if (type == "textbox") {
389 int slider_width = props->getIntValue("slider", parentHeight);
390 if (slider_width==0) slider_width=20;
391 puLargeInput * puTextBox =
392 new puLargeInput(x, y, x+width, x+height, 2, slider_width);
393 if (props->hasValue("editable"))
395 if (props->getBoolValue("editable")==false)
396 puTextBox->disableInput();
398 puTextBox->enableInput();
400 setupObject(puTextBox,props);
402 } else if (type == "select") {
403 vector<SGPropertyNode_ptr> value_nodes;
404 SGPropertyNode * selection_node =
405 fgGetNode(props->getChild("selection")->getStringValue(), true);
407 for (int q = 0; q < selection_node->nChildren(); q++)
408 value_nodes.push_back(selection_node->getChild(q));
410 char ** entries = make_char_array(value_nodes.size());
411 for (unsigned int i = 0, j = value_nodes.size() - 1;
412 i < value_nodes.size();
414 entries[i] = strdup((char *)value_nodes[i]->getName());
415 puSelectBox * select =
416 new puSelectBox(x, y, x + width, y + height, entries);
417 setupObject(select, props);
425 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
427 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
429 if (props->hasValue("legend"))
430 object->setLegend(props->getStringValue("legend"));
432 if (props->hasValue("label"))
433 object->setLabel(props->getStringValue("label"));
435 if (props->hasValue("property")) {
436 const char * name = props->getStringValue("name");
439 const char * propname = props->getStringValue("property");
440 SGPropertyNode_ptr node = fgGetNode(propname, true);
441 copy_to_pui(node, object);
442 PropertyObject* po = new PropertyObject(name, object, node);
443 _propertyObjects.push_back(po);
444 if(props->getBoolValue("live"))
445 _liveObjects.push_back(po);
448 vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
449 if (nodes.size() > 0) {
450 GUIInfo * info = new GUIInfo(this);
452 for (unsigned int i = 0; i < nodes.size(); i++)
453 info->bindings.push_back(new FGBinding(nodes[i]));
454 object->setCallback(action_callback);
455 object->setUserData(info);
456 _info.push_back(info);
459 object->makeReturnDefault(props->getBoolValue("default"));
463 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
464 int width, int height, bool makeFrame)
466 setupObject(group, props);
469 puFrame* f = new puFrame(0, 0, width, height);
470 f->setColorScheme(0.8, 0.8, 0.9, 0.85);
473 int nChildren = props->nChildren();
474 for (int i = 0; i < nChildren; i++)
475 makeObject(props->getChild(i), width, height);
480 FGDialog::make_char_array (int size)
482 char ** list = new char*[size+1];
483 for (int i = 0; i <= size; i++)
485 _char_arrays.push_back(list);
491 ////////////////////////////////////////////////////////////////////////
492 // Implementation of FGDialog::PropertyObject.
493 ////////////////////////////////////////////////////////////////////////
495 FGDialog::PropertyObject::PropertyObject (const char * n,
497 SGPropertyNode_ptr p)