1 // dialog.cxx: implementation of an XML-configurable dialog box.
3 #include <stdlib.h> // atof()
5 #include <Input/input.hxx>
11 #include "AirportList.hxx"
14 int fgPopup::checkHit(int button, int updown, int x, int y)
16 int result = puPopup::checkHit(button, updown, x, y);
18 // This is annoying. We would really want a true result from the
19 // superclass to indicate "handled by child object", but all it
20 // tells us is that the pointer is inside the dialog. So do the
21 // intersection test (again) to make sure we don't start a drag
22 // when inside controls.
24 if(updown == PU_DOWN && !_dragging) {
28 int hit = getHitObjects(this, x, y);
29 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
33 getPosition(&px, &py);
37 } else if(updown == PU_DRAG && _dragging) {
38 setPosition(x + _dX, y + _dY);
45 int fgPopup::getHitObjects(puObject *object, int x, int y)
48 if(object->getType() & PUCLASS_GROUP)
49 for (puObject *obj = ((puGroup *)object)->getFirstChild();
50 obj; obj = obj->getNextObject())
51 type |= getHitObjects(obj, x, y);
54 object->getAbsolutePosition(&cx, &cy);
55 object->getSize(&cw, &ch);
56 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
57 type |= object->getType();
63 ////////////////////////////////////////////////////////////////////////
65 ////////////////////////////////////////////////////////////////////////
68 * User data for a GUI object.
72 GUIInfo (FGDialog * d);
76 vector <FGBinding *> bindings;
84 action_callback (puObject * object)
86 GUIInfo * info = (GUIInfo *)object->getUserData();
87 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
88 gui->setActiveDialog(info->dialog);
89 int nBindings = info->bindings.size();
90 for (int i = 0; i < nBindings; i++) {
91 info->bindings[i]->fire();
92 if (gui->getActiveDialog() == 0)
95 gui->setActiveDialog(0);
100 format_callback(puObject *obj, int dx, int dy, void *n)
102 SGPropertyNode *node = (SGPropertyNode *)n;
103 const char *format = node->getStringValue("format"), *f = format;
105 // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
116 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
118 while (*f && isdigit(*f))
122 while (*f && isdigit(*f))
145 const char *src = obj->getLabel();
148 float value = atof(src);
150 return; // not a number
151 snprintf(buf, 256, format, value);
153 snprintf(buf, 256, format, src);
158 SGPropertyNode *result = node->getNode("formatted", true);
159 result->setStringValue(buf);
160 obj->setLabel(result->getStringValue());
165 ////////////////////////////////////////////////////////////////////////
166 // Static helper functions.
167 ////////////////////////////////////////////////////////////////////////
170 * Copy a property value to a PUI object.
173 copy_to_pui (SGPropertyNode * node, puObject * object)
175 // Treat puText objects specially, so their "values" can be set
177 if(object->getType() & PUCLASS_TEXT) {
178 object->setLabel(node->getStringValue());
182 switch (node->getType()) {
183 case SGPropertyNode::BOOL:
184 case SGPropertyNode::INT:
185 case SGPropertyNode::LONG:
186 object->setValue(node->getIntValue());
188 case SGPropertyNode::FLOAT:
189 case SGPropertyNode::DOUBLE:
190 object->setValue(node->getFloatValue());
193 object->setValue(node->getStringValue());
200 copy_from_pui (puObject * object, SGPropertyNode * node)
202 // puText objects are immutable, so should not be copied out
203 if(object->getType() & PUCLASS_TEXT)
206 switch (node->getType()) {
207 case SGPropertyNode::BOOL:
208 case SGPropertyNode::INT:
209 case SGPropertyNode::LONG:
210 node->setIntValue(object->getIntegerValue());
212 case SGPropertyNode::FLOAT:
213 case SGPropertyNode::DOUBLE:
214 node->setFloatValue(object->getFloatValue());
217 // Special case to handle lists, as getStringValue cannot be overridden
218 if(object->getType() & PUCLASS_LIST)
220 node->setStringValue(((puList *) object)->getListStringValue());
224 node->setStringValue(object->getStringValue());
232 ////////////////////////////////////////////////////////////////////////
233 // Implementation of GUIInfo.
234 ////////////////////////////////////////////////////////////////////////
236 GUIInfo::GUIInfo (FGDialog * d)
243 for (unsigned int i = 0; i < bindings.size(); i++) {
251 ////////////////////////////////////////////////////////////////////////
252 // Implementation of FGDialog.
253 ////////////////////////////////////////////////////////////////////////
255 FGDialog::FGDialog (SGPropertyNode * props)
261 FGDialog::~FGDialog ()
263 puDeleteObject(_object);
267 // Delete all the arrays we made
268 // and were forced to keep around
269 // because PUI won't do its own
270 // memory management.
271 for (i = 0; i < _char_arrays.size(); i++) {
272 for (int j = 0; _char_arrays[i][j] != 0; j++)
273 free(_char_arrays[i][j]); // added with strdup
274 delete[] _char_arrays[i];
277 // Delete all the info objects we
278 // were forced to keep around because
279 // PUI cannot delete its own user data.
280 for (i = 0; i < _info.size(); i++) {
281 delete (GUIInfo *)_info[i];
285 // Finally, delete the property links.
286 for (i = 0; i < _propertyObjects.size(); i++) {
287 delete _propertyObjects[i];
288 _propertyObjects[i] = 0;
293 FGDialog::updateValue (const char * objectName)
295 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
296 const string &name = _propertyObjects[i]->name;
297 if (name == objectName)
298 copy_to_pui(_propertyObjects[i]->node,
299 _propertyObjects[i]->object);
304 FGDialog::applyValue (const char * objectName)
306 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
307 if (_propertyObjects[i]->name == objectName)
308 copy_from_pui(_propertyObjects[i]->object,
309 _propertyObjects[i]->node);
314 FGDialog::updateValues ()
316 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
317 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
321 FGDialog::applyValues ()
323 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
324 copy_from_pui(_propertyObjects[i]->object,
325 _propertyObjects[i]->node);
331 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
332 puObject *obj = _liveObjects[i]->object;
333 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
336 copy_to_pui(_liveObjects[i]->node, obj);
341 FGDialog::display (SGPropertyNode * props)
344 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
348 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
349 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
351 bool userx = props->hasValue("x");
352 bool usery = props->hasValue("y");
353 bool userw = props->hasValue("width");
354 bool userh = props->hasValue("height");
356 LayoutWidget wid(props);
359 wid.calcPrefSize(&pw, &ph);
360 pw = props->getIntValue("width", pw);
361 ph = props->getIntValue("height", ph);
362 int px = props->getIntValue("x", (screenw - pw) / 2);
363 int py = props->getIntValue("y", (screenh - ph) / 2);
364 wid.layout(px, py, pw, ph);
366 _object = makeObject(props, screenw, screenh);
368 // Remove automatically generated properties, so the layout looks
369 // the same next time around.
370 if(!userx) props->removeChild("x");
371 if(!usery) props->removeChild("y");
372 if(!userw) props->removeChild("width");
373 if(!userh) props->removeChild("height");
378 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
379 << props->getStringValue("name", "[unnamed]")
380 << " does not contain a proper GUI definition");
385 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
387 bool presetSize = props->hasValue("width") && props->hasValue("height");
388 int width = props->getIntValue("width", parentWidth);
389 int height = props->getIntValue("height", parentHeight);
390 int x = props->getIntValue("x", (parentWidth - width) / 2);
391 int y = props->getIntValue("y", (parentHeight - height) / 2);
393 string type = props->getName();
397 if (type == "dialog") {
399 if (props->getBoolValue("modal", false))
400 dialog = new puDialogBox(x, y);
402 dialog = new fgPopup(x, y);
403 setupGroup(dialog, props, width, height, true);
405 } else if (type == "group") {
406 puGroup * group = new puGroup(x, y);
407 setupGroup(group, props, width, height, false);
409 } else if (type == "list") {
410 puList * list = new puList(x, y, x + width, y + height);
411 setupObject(list, props);
413 } else if (type == "airport-list") {
414 AirportList * list = new AirportList(x, y, x + width, y + height);
415 setupObject(list, props);
417 } else if (type == "input") {
418 puInput * input = new puInput(x, y, x + width, y + height);
419 setupObject(input, props);
421 } else if (type == "text") {
422 puText * text = new puText(x, y);
423 setupObject(text, props);
425 if (props->getNode("format"))
426 text->setRenderCallback(format_callback, props);
427 // Layed-out objects need their size set, and non-layout ones
428 // get a different placement.
429 if(presetSize) text->setSize(width, height);
430 else text->setLabelPlace(PUPLACE_LABEL_DEFAULT);
432 } else if (type == "checkbox") {
434 b = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
435 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
436 setupObject(b, props);
438 } else if (type == "radio") {
440 b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
441 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
442 setupObject(b, props);
444 } else if (type == "button") {
446 const char * legend = props->getStringValue("legend", "[none]");
447 if (props->getBoolValue("one-shot", true))
448 b = new puOneShot(x, y, legend);
450 b = new puButton(x, y, legend);
452 b->setSize(width, height);
453 setupObject(b, props);
455 } else if (type == "combo") {
456 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
457 char ** entries = make_char_array(value_nodes.size());
458 for (unsigned int i = 0, j = value_nodes.size() - 1;
459 i < value_nodes.size();
461 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
463 new puComboBox(x, y, x + width, y + height, entries,
464 props->getBoolValue("editable", false));
465 setupObject(combo, props);
467 } else if (type == "slider") {
468 bool vertical = props->getBoolValue("vertical", false);
469 puSlider * slider = new puSlider(x, y, (vertical ? height : width));
470 slider->setMinValue(props->getFloatValue("min", 0.0));
471 slider->setMaxValue(props->getFloatValue("max", 1.0));
472 setupObject(slider, props);
474 slider->setSize(width, height);
476 } else if (type == "dial") {
477 puDial * dial = new puDial(x, y, width);
478 dial->setMinValue(props->getFloatValue("min", 0.0));
479 dial->setMaxValue(props->getFloatValue("max", 1.0));
480 dial->setWrap(props->getBoolValue("wrap", true));
481 setupObject(dial, props);
483 } else if (type == "textbox") {
484 int slider_width = props->getIntValue("slider", parentHeight);
485 if (slider_width==0) slider_width=20;
486 puLargeInput * puTextBox =
487 new puLargeInput(x, y, x+width, x+height, 2, slider_width);
488 if (props->hasValue("editable"))
490 if (props->getBoolValue("editable")==false)
491 puTextBox->disableInput();
493 puTextBox->enableInput();
495 setupObject(puTextBox,props);
497 } else if (type == "select") {
498 vector<SGPropertyNode_ptr> value_nodes;
499 SGPropertyNode * selection_node =
500 fgGetNode(props->getChild("selection")->getStringValue(), true);
502 for (int q = 0; q < selection_node->nChildren(); q++)
503 value_nodes.push_back(selection_node->getChild(q));
505 char ** entries = make_char_array(value_nodes.size());
506 for (unsigned int i = 0, j = value_nodes.size() - 1;
507 i < value_nodes.size();
509 entries[i] = strdup((char *)value_nodes[i]->getName());
510 puSelectBox * select =
511 new puSelectBox(x, y, x + width, y + height, entries);
512 setupObject(select, props);
520 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
522 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
524 if (props->hasValue("legend"))
525 object->setLegend(props->getStringValue("legend"));
527 if (props->hasValue("label"))
528 object->setLabel(props->getStringValue("label"));
530 if (props->hasValue("property")) {
531 const char * name = props->getStringValue("name");
534 const char * propname = props->getStringValue("property");
535 SGPropertyNode_ptr node = fgGetNode(propname, true);
536 copy_to_pui(node, object);
537 PropertyObject* po = new PropertyObject(name, object, node);
538 _propertyObjects.push_back(po);
539 if(props->getBoolValue("live"))
540 _liveObjects.push_back(po);
543 vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
544 if (nodes.size() > 0) {
545 GUIInfo * info = new GUIInfo(this);
547 for (unsigned int i = 0; i < nodes.size(); i++)
548 info->bindings.push_back(new FGBinding(nodes[i]));
549 object->setCallback(action_callback);
550 object->setUserData(info);
551 _info.push_back(info);
554 object->makeReturnDefault(props->getBoolValue("default"));
558 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
559 int width, int height, bool makeFrame)
561 setupObject(group, props);
564 puFrame* f = new puFrame(0, 0, width, height);
565 f->setColorScheme(0.8, 0.8, 0.9, 0.85);
568 int nChildren = props->nChildren();
569 for (int i = 0; i < nChildren; i++)
570 makeObject(props->getChild(i), width, height);
575 FGDialog::make_char_array (int size)
577 char ** list = new char*[size+1];
578 for (int i = 0; i <= size; i++)
580 _char_arrays.push_back(list);
586 ////////////////////////////////////////////////////////////////////////
587 // Implementation of FGDialog::PropertyObject.
588 ////////////////////////////////////////////////////////////////////////
590 FGDialog::PropertyObject::PropertyObject (const char * n,
592 SGPropertyNode_ptr p)