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);
21 // This is annoying. We would really want a true result from the
22 // superclass to indicate "handled by child object", but all it
23 // tells us is that the pointer is inside the dialog. So do the
24 // intersection test (again) to make sure we don't start a drag
25 // when inside controls.
27 if(updown == PU_DOWN && !_dragging) {
31 int hit = getHitObjects(this, x, y);
32 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
36 getPosition(&px, &py);
40 } else if(updown == PU_DRAG && _dragging) {
41 setPosition(x + _dX, y + _dY);
48 int fgPopup::getHitObjects(puObject *object, int x, int y)
51 if(object->getType() & PUCLASS_GROUP)
52 for (puObject *obj = ((puGroup *)object)->getFirstChild();
53 obj; obj = obj->getNextObject())
54 type |= getHitObjects(obj, x, y);
57 object->getAbsolutePosition(&cx, &cy);
58 object->getSize(&cw, &ch);
59 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
60 type |= object->getType();
66 ////////////////////////////////////////////////////////////////////////
68 ////////////////////////////////////////////////////////////////////////
71 * User data for a GUI object.
75 GUIInfo (FGDialog * d);
79 vector <FGBinding *> bindings;
87 action_callback (puObject * object)
89 GUIInfo * info = (GUIInfo *)object->getUserData();
90 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
91 gui->setActiveDialog(info->dialog);
92 int nBindings = info->bindings.size();
93 for (int i = 0; i < nBindings; i++) {
94 info->bindings[i]->fire();
95 if (gui->getActiveDialog() == 0)
98 gui->setActiveDialog(0);
103 format_callback(puObject *obj, int dx, int dy, void *n)
105 SGPropertyNode *node = (SGPropertyNode *)n;
106 const char *format = node->getStringValue("format"), *f = format;
107 bool number, l = false;
108 // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
119 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
121 while (*f && isdigit(*f))
125 while (*f && isdigit(*f))
133 else if (*f == 's') {
150 const char *src = obj->getLabel();
153 float value = atof(src);
154 snprintf(buf, 256, format, value);
156 snprintf(buf, 256, format, src);
161 SGPropertyNode *result = node->getNode("formatted", true);
162 result->setStringValue(buf);
163 obj->setLabel(result->getStringValue());
168 ////////////////////////////////////////////////////////////////////////
169 // Static helper functions.
170 ////////////////////////////////////////////////////////////////////////
173 * Copy a property value to a PUI object.
176 copy_to_pui (SGPropertyNode * node, puObject * object)
178 // Treat puText objects specially, so their "values" can be set
180 if(object->getType() & PUCLASS_TEXT) {
181 object->setLabel(node->getStringValue());
185 switch (node->getType()) {
186 case SGPropertyNode::BOOL:
187 case SGPropertyNode::INT:
188 case SGPropertyNode::LONG:
189 object->setValue(node->getIntValue());
191 case SGPropertyNode::FLOAT:
192 case SGPropertyNode::DOUBLE:
193 object->setValue(node->getFloatValue());
196 object->setValue(node->getStringValue());
203 copy_from_pui (puObject * object, SGPropertyNode * node)
205 // puText objects are immutable, so should not be copied out
206 if(object->getType() & PUCLASS_TEXT)
209 switch (node->getType()) {
210 case SGPropertyNode::BOOL:
211 case SGPropertyNode::INT:
212 case SGPropertyNode::LONG:
213 node->setIntValue(object->getIntegerValue());
215 case SGPropertyNode::FLOAT:
216 case SGPropertyNode::DOUBLE:
217 node->setFloatValue(object->getFloatValue());
220 // Special case to handle lists, as getStringValue cannot be overridden
221 if(object->getType() & PUCLASS_LIST)
223 node->setStringValue(((puList *) object)->getListStringValue());
227 node->setStringValue(object->getStringValue());
235 ////////////////////////////////////////////////////////////////////////
236 // Implementation of GUIInfo.
237 ////////////////////////////////////////////////////////////////////////
239 GUIInfo::GUIInfo (FGDialog * d)
246 for (unsigned int i = 0; i < bindings.size(); i++) {
254 ////////////////////////////////////////////////////////////////////////
255 // Implementation of FGDialog.
256 ////////////////////////////////////////////////////////////////////////
258 FGDialog::FGDialog (SGPropertyNode * props)
261 char* envp = ::getenv( "FG_FONTS" );
262 if ( envp != NULL ) {
263 _font_path.set( envp );
265 _font_path.set( globals->get_fg_root() );
266 _font_path.append( "Fonts" );
272 FGDialog::~FGDialog ()
274 puDeleteObject(_object);
278 // Delete all the arrays we made
279 // and were forced to keep around
280 // because PUI won't do its own
281 // memory management.
282 for (i = 0; i < _char_arrays.size(); i++) {
283 for (int j = 0; _char_arrays[i][j] != 0; j++)
284 free(_char_arrays[i][j]); // added with strdup
285 delete[] _char_arrays[i];
288 // Delete all the info objects we
289 // were forced to keep around because
290 // PUI cannot delete its own user data.
291 for (i = 0; i < _info.size(); i++) {
292 delete (GUIInfo *)_info[i];
296 // Finally, delete the property links.
297 for (i = 0; i < _propertyObjects.size(); i++) {
298 delete _propertyObjects[i];
299 _propertyObjects[i] = 0;
304 FGDialog::updateValue (const char * objectName)
306 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
307 const string &name = _propertyObjects[i]->name;
308 if (name == objectName)
309 copy_to_pui(_propertyObjects[i]->node,
310 _propertyObjects[i]->object);
315 FGDialog::applyValue (const char * objectName)
317 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
318 if (_propertyObjects[i]->name == objectName)
319 copy_from_pui(_propertyObjects[i]->object,
320 _propertyObjects[i]->node);
325 FGDialog::updateValues ()
327 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
328 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
332 FGDialog::applyValues ()
334 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
335 copy_from_pui(_propertyObjects[i]->object,
336 _propertyObjects[i]->node);
342 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
343 puObject *obj = _liveObjects[i]->object;
344 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
347 copy_to_pui(_liveObjects[i]->node, obj);
352 FGDialog::display (SGPropertyNode * props)
355 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
359 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
360 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
362 bool userx = props->hasValue("x");
363 bool usery = props->hasValue("y");
364 bool userw = props->hasValue("width");
365 bool userh = props->hasValue("height");
367 // Let the layout widget work in the same property subtree.
368 LayoutWidget wid(props);
372 wid.calcPrefSize(&pw, &ph);
373 pw = props->getIntValue("width", pw);
374 ph = props->getIntValue("height", ph);
375 int px = props->getIntValue("x", (screenw - pw) / 2);
376 int py = props->getIntValue("y", (screenh - ph) / 2);
378 // Define "x", "y", "width" and/or "height" in the property tree if they
379 // are not specified in the configuration file.
380 wid.layout(px, py, pw, ph);
382 // Use the dimension and location properties as specified in the
383 // configuration file or from the layout widget.
384 _object = makeObject(props, screenw, screenh);
386 // Remove automatically generated properties, so the layout looks
387 // the same next time around.
388 if(!userx) props->removeChild("x");
389 if(!usery) props->removeChild("y");
390 if(!userw) props->removeChild("width");
391 if(!userh) props->removeChild("height");
396 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
397 << props->getStringValue("name", "[unnamed]")
398 << " does not contain a proper GUI definition");
403 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
405 bool presetSize = props->hasValue("width") && props->hasValue("height");
406 int width = props->getIntValue("width", parentWidth);
407 int height = props->getIntValue("height", parentHeight);
408 int x = props->getIntValue("x", (parentWidth - width) / 2);
409 int y = props->getIntValue("y", (parentHeight - height) / 2);
410 string type = props->getName();
412 const sgVec4 background = {0.8, 0.8, 0.9, 0.85};
413 const sgVec4 foreground = {0, 0, 0, 1};
416 if (type == "hrule" || type == "vrule")
417 sgCopyVec4(color, foreground);
419 sgCopyVec4(color, background);
421 SGPropertyNode *ncs = props->getNode("color", false);
423 color[0] = ncs->getFloatValue("red", color[0]);
424 color[1] = ncs->getFloatValue("green", color[1]);
425 color[2] = ncs->getFloatValue("blue", color[2]);
426 color[3] = ncs->getFloatValue("alpha", color[3]);
432 if (type == "dialog") {
434 bool draggable = props->getBoolValue("draggable", true);
435 if (props->getBoolValue("modal", false))
436 dialog = new puDialogBox(x, y);
438 dialog = new fgPopup(x, y, draggable);
439 setupGroup(dialog, props, width, height, color, true);
441 } else if (type == "group") {
442 puGroup * group = new puGroup(x, y);
443 setupGroup(group, props, width, height, color, false);
445 } else if (type == "frame") {
446 puGroup * group = new puGroup(x, y);
447 setupGroup(group, props, width, height, color, true);
449 } else if (type == "hrule" || type == "vrule") {
450 puFrame * rule = new puFrame(x, y, x + width, y + height);
451 rule->setBorderThickness(0);
452 rule->setColorScheme(color[0], color[1], color[2], color[3]);
454 } else if (type == "list") {
455 puList * list = new puList(x, y, x + width, y + height);
456 setupObject(list, props);
458 } else if (type == "airport-list") {
459 AirportList * list = new AirportList(x, y, x + width, y + height);
460 setupObject(list, props);
462 } else if (type == "input") {
463 puInput * input = new puInput(x, y, x + width, y + height);
464 setupObject(input, props);
466 } else if (type == "text") {
467 puText * text = new puText(x, y);
468 setupObject(text, props);
470 if (props->getNode("format")) {
471 SGPropertyNode *live = props->getNode("live");
472 if (live && live->getBoolValue())
473 text->setRenderCallback(format_callback, props);
475 format_callback(text, x, y, props);
477 // Layed-out objects need their size set, and non-layout ones
478 // get a different placement.
479 if(presetSize) text->setSize(width, height);
480 else text->setLabelPlace(PUPLACE_LABEL_DEFAULT);
482 } else if (type == "checkbox") {
484 b = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
485 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
486 setupObject(b, props);
488 } else if (type == "radio") {
490 b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
491 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
492 setupObject(b, props);
494 } else if (type == "button") {
496 const char * legend = props->getStringValue("legend", "[none]");
497 if (props->getBoolValue("one-shot", true))
498 b = new puOneShot(x, y, legend);
500 b = new puButton(x, y, legend);
502 b->setSize(width, height);
503 setupObject(b, props);
505 } else if (type == "combo") {
506 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
507 char ** entries = make_char_array(value_nodes.size());
508 for (unsigned int i = 0, j = value_nodes.size() - 1;
509 i < value_nodes.size();
511 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
513 new puComboBox(x, y, x + width, y + height, entries,
514 props->getBoolValue("editable", false));
515 setupObject(combo, props);
517 } else if (type == "slider") {
518 bool vertical = props->getBoolValue("vertical", false);
519 puSlider * slider = new puSlider(x, y, (vertical ? height : width));
520 slider->setMinValue(props->getFloatValue("min", 0.0));
521 slider->setMaxValue(props->getFloatValue("max", 1.0));
522 setupObject(slider, props);
524 slider->setSize(width, height);
526 } else if (type == "dial") {
527 puDial * dial = new puDial(x, y, width);
528 dial->setMinValue(props->getFloatValue("min", 0.0));
529 dial->setMaxValue(props->getFloatValue("max", 1.0));
530 dial->setWrap(props->getBoolValue("wrap", true));
531 setupObject(dial, props);
533 } else if (type == "textbox") {
534 int slider_width = props->getIntValue("slider", parentHeight);
535 int wrap = props->getBoolValue("wrap", true);
536 if (slider_width==0) slider_width=20;
537 puLargeInput * puTextBox =
538 new puLargeInput(x, y, x+width, x+height, 2, slider_width, wrap);
539 if (props->hasValue("editable"))
541 if (props->getBoolValue("editable")==false)
542 puTextBox->disableInput();
544 puTextBox->enableInput();
546 setupObject(puTextBox,props);
548 } else if (type == "select") {
549 vector<SGPropertyNode_ptr> value_nodes;
550 SGPropertyNode * selection_node =
551 fgGetNode(props->getChild("selection")->getStringValue(), true);
553 for (int q = 0; q < selection_node->nChildren(); q++)
554 value_nodes.push_back(selection_node->getChild(q));
556 char ** entries = make_char_array(value_nodes.size());
557 for (unsigned int i = 0, j = value_nodes.size() - 1;
558 i < value_nodes.size();
560 entries[i] = strdup((char *)value_nodes[i]->getName());
561 puSelectBox * select =
562 new puSelectBox(x, y, x + width, y + height, entries);
563 setupObject(select, props);
571 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
573 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
575 if (props->hasValue("legend"))
576 object->setLegend(props->getStringValue("legend"));
578 if (props->hasValue("label"))
579 object->setLabel(props->getStringValue("label"));
581 if (props->hasValue("border"))
582 object->setBorderThickness( props->getIntValue("border", 2) );
584 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
585 SGPath path( _font_path );
586 const char *name = nft->getStringValue("name", "default");
587 float size = nft->getFloatValue("size", 13.0);
588 float slant = nft->getFloatValue("slant", 0.0);
590 path.concat( ".txf" );
592 fntFont *font = new fntTexFont;
593 font->load( (char *)path.c_str() );
595 puFont lfnt(font, size, slant);
596 object->setLabelFont( lfnt );
599 if ( SGPropertyNode *ncs = props->getNode("color", false) ) {
601 color[0] = ncs->getFloatValue("red", 0.0);
602 color[1] = ncs->getFloatValue("green", 0.0);
603 color[2] = ncs->getFloatValue("blue", 0.0);
604 color[3] = ncs->getFloatValue("alpha", 1.0);
605 object->setColor(PUCOL_LABEL, color[0], color[1], color[2], color[3]);
608 if (props->hasValue("property")) {
609 const char * name = props->getStringValue("name");
612 const char * propname = props->getStringValue("property");
613 SGPropertyNode_ptr node = fgGetNode(propname, true);
614 copy_to_pui(node, object);
615 PropertyObject* po = new PropertyObject(name, object, node);
616 _propertyObjects.push_back(po);
617 if(props->getBoolValue("live"))
618 _liveObjects.push_back(po);
621 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
622 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
623 if (bindings.size() > 0) {
624 GUIInfo * info = new GUIInfo(this);
626 for (unsigned int i = 0; i < bindings.size(); i++) {
628 SGPropertyNode *binding;
629 while (dest->getChild("binding", j))
632 binding = dest->getChild("binding", j, true);
633 copyProperties(bindings[i], binding);
634 info->bindings.push_back(new FGBinding(binding));
636 object->setCallback(action_callback);
637 object->setUserData(info);
638 _info.push_back(info);
641 object->makeReturnDefault(props->getBoolValue("default"));
645 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
646 int width, int height, sgVec4 color, bool makeFrame)
648 setupObject(group, props);
651 puFrame* f = new puFrame(0, 0, width, height);
652 f->setColorScheme(color[0], color[1], color[2], color[3]);
655 int nChildren = props->nChildren();
656 for (int i = 0; i < nChildren; i++)
657 makeObject(props->getChild(i), width, height);
662 FGDialog::make_char_array (int size)
664 char ** list = new char*[size+1];
665 for (int i = 0; i <= size; i++)
667 _char_arrays.push_back(list);
673 ////////////////////////////////////////////////////////////////////////
674 // Implementation of FGDialog::PropertyObject.
675 ////////////////////////////////////////////////////////////////////////
677 FGDialog::PropertyObject::PropertyObject (const char * n,
679 SGPropertyNode_ptr p)