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)
260 _gui((NewGUI *)globals->get_subsystem("gui"))
262 char* envp = ::getenv( "FG_FONTS" );
263 if ( envp != NULL ) {
264 _font_path.set( envp );
266 _font_path.set( globals->get_fg_root() );
267 _font_path.append( "Fonts" );
273 FGDialog::~FGDialog ()
275 puDeleteObject(_object);
279 // Delete all the arrays we made
280 // and were forced to keep around
281 // because PUI won't do its own
282 // memory management.
283 for (i = 0; i < _char_arrays.size(); i++) {
284 for (int j = 0; _char_arrays[i][j] != 0; j++)
285 free(_char_arrays[i][j]); // added with strdup
286 delete[] _char_arrays[i];
289 // Delete all the info objects we
290 // were forced to keep around because
291 // PUI cannot delete its own user data.
292 for (i = 0; i < _info.size(); i++) {
293 delete (GUIInfo *)_info[i];
297 // Finally, delete the property links.
298 for (i = 0; i < _propertyObjects.size(); i++) {
299 delete _propertyObjects[i];
300 _propertyObjects[i] = 0;
305 FGDialog::updateValue (const char * objectName)
307 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
308 const string &name = _propertyObjects[i]->name;
309 if (name == objectName)
310 copy_to_pui(_propertyObjects[i]->node,
311 _propertyObjects[i]->object);
316 FGDialog::applyValue (const char * objectName)
318 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
319 if (_propertyObjects[i]->name == objectName)
320 copy_from_pui(_propertyObjects[i]->object,
321 _propertyObjects[i]->node);
326 FGDialog::updateValues ()
328 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
329 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
333 FGDialog::applyValues ()
335 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
336 copy_from_pui(_propertyObjects[i]->object,
337 _propertyObjects[i]->node);
343 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
344 puObject *obj = _liveObjects[i]->object;
345 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
348 copy_to_pui(_liveObjects[i]->node, obj);
353 FGDialog::display (SGPropertyNode * props)
356 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
360 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
361 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
363 bool userx = props->hasValue("x");
364 bool usery = props->hasValue("y");
365 bool userw = props->hasValue("width");
366 bool userh = props->hasValue("height");
368 // Expand some elements. Has to be done before the layouter sees them.
369 // preprocess(props);
371 // Let the layout widget work in the same property subtree.
372 LayoutWidget wid(props);
376 wid.calcPrefSize(&pw, &ph);
377 pw = props->getIntValue("width", pw);
378 ph = props->getIntValue("height", ph);
379 int px = props->getIntValue("x", (screenw - pw) / 2);
380 int py = props->getIntValue("y", (screenh - ph) / 2);
382 // Define "x", "y", "width" and/or "height" in the property tree if they
383 // are not specified in the configuration file.
384 wid.layout(px, py, pw, ph);
386 // Use the dimension and location properties as specified in the
387 // configuration file or from the layout widget.
388 _object = makeObject(props, screenw, screenh);
390 // Remove automatically generated properties, so the layout looks
391 // the same next time around.
392 if(!userx) props->removeChild("x");
393 if(!usery) props->removeChild("y");
394 if(!userw) props->removeChild("width");
395 if(!userh) props->removeChild("height");
400 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
401 << props->getStringValue("name", "[unnamed]")
402 << " does not contain a proper GUI definition");
407 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
409 bool presetSize = props->hasValue("width") && props->hasValue("height");
410 int width = props->getIntValue("width", parentWidth);
411 int height = props->getIntValue("height", parentHeight);
412 int x = props->getIntValue("x", (parentWidth - width) / 2);
413 int y = props->getIntValue("y", (parentHeight - height) / 2);
414 string type = props->getName();
419 if (type == "dialog") {
421 bool draggable = props->getBoolValue("draggable", true);
422 if (props->getBoolValue("modal", false))
423 obj = new puDialogBox(x, y);
425 obj = new fgPopup(x, y, draggable);
426 setupGroup(obj, props, width, height, true);
427 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
430 } else if (type == "group") {
431 puGroup * obj = new puGroup(x, y);
432 setupGroup(obj, props, width, height, false);
433 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
436 } else if (type == "frame") {
437 puGroup * obj = new puGroup(x, y);
438 setupGroup(obj, props, width, height, true);
439 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
442 } else if (type == "hrule" || type == "vrule") {
443 puFrame * obj = new puFrame(x, y, x + width, y + height);
444 obj->setBorderThickness(0);
445 setColor(obj, props, FOREGROUND);
448 } else if (type == "list") {
449 puList * obj = new puList(x, y, x + width, y + height);
450 setupObject(obj, props);
451 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
454 } else if (type == "airport-list") {
455 AirportList * obj = new AirportList(x, y, x + width, y + height);
456 setupObject(obj, props);
457 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
460 } else if (type == "input") {
461 puInput * obj = new puInput(x, y, x + width, y + height);
462 setupObject(obj, props);
463 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
466 } else if (type == "text") {
467 puText * obj = new puText(x, y);
468 setupObject(obj, props);
470 if (props->getNode("format")) {
471 SGPropertyNode *live = props->getNode("live");
472 if (live && live->getBoolValue())
473 obj->setRenderCallback(format_callback, props);
475 format_callback(obj, x, y, props);
477 // Layed-out objects need their size set, and non-layout ones
478 // get a different placement.
479 if(presetSize) obj->setSize(width, height);
480 else obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
481 setColor(obj, props, LABEL);
484 } else if (type == "checkbox") {
486 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
487 setupObject(obj, props);
488 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
491 } else if (type == "radio") {
493 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
494 setupObject(obj, props);
495 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
498 } else if (type == "button") {
500 const char * legend = props->getStringValue("legend", "[none]");
501 if (props->getBoolValue("one-shot", true))
502 obj = new puOneShot(x, y, legend);
504 obj = new puButton(x, y, legend);
506 obj->setSize(width, height);
507 setupObject(obj, props);
508 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
511 } else if (type == "combo") {
512 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
513 char ** entries = make_char_array(value_nodes.size());
514 for (unsigned int i = 0, j = value_nodes.size() - 1;
515 i < value_nodes.size();
517 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
518 puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
519 props->getBoolValue("editable", false));
520 setupObject(obj, props);
521 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
524 } else if (type == "slider") {
525 bool vertical = props->getBoolValue("vertical", false);
526 puSlider * obj = new puSlider(x, y, (vertical ? height : width));
527 obj->setMinValue(props->getFloatValue("min", 0.0));
528 obj->setMaxValue(props->getFloatValue("max", 1.0));
529 setupObject(obj, props);
531 obj->setSize(width, height);
532 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
535 } else if (type == "dial") {
536 puDial * obj = new puDial(x, y, width);
537 obj->setMinValue(props->getFloatValue("min", 0.0));
538 obj->setMaxValue(props->getFloatValue("max", 1.0));
539 obj->setWrap(props->getBoolValue("wrap", true));
540 setupObject(obj, props);
541 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
544 } else if (type == "textbox") {
545 int slider_width = props->getIntValue("slider", parentHeight);
546 int wrap = props->getBoolValue("wrap", true);
547 if (slider_width==0) slider_width=20;
548 puLargeInput * obj = new puLargeInput(x, y,
549 x+width, x+height, 2, slider_width, wrap);
551 if (props->hasValue("editable")) {
552 if (props->getBoolValue("editable")==false)
557 setupObject(obj, props);
558 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
561 } else if (type == "select") {
562 vector<SGPropertyNode_ptr> value_nodes;
563 SGPropertyNode * selection_node =
564 fgGetNode(props->getChild("selection")->getStringValue(), true);
566 for (int q = 0; q < selection_node->nChildren(); q++)
567 value_nodes.push_back(selection_node->getChild(q));
569 char ** entries = make_char_array(value_nodes.size());
570 for (unsigned int i = 0, j = value_nodes.size() - 1;
571 i < value_nodes.size();
573 entries[i] = strdup((char *)value_nodes[i]->getName());
575 new puSelectBox(x, y, x + width, y + height, entries);
576 setupObject(obj, props);
577 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
585 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
587 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
589 if (props->hasValue("legend"))
590 object->setLegend(props->getStringValue("legend"));
592 if (props->hasValue("label"))
593 object->setLabel(props->getStringValue("label"));
595 if (props->hasValue("border"))
596 object->setBorderThickness( props->getIntValue("border", 2) );
598 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
599 SGPath path( _font_path );
600 const char *name = nft->getStringValue("name", "default");
601 float size = nft->getFloatValue("size", 13.0);
602 float slant = nft->getFloatValue("slant", 0.0);
604 path.concat( ".txf" );
606 fntFont *font = new fntTexFont;
607 font->load( (char *)path.c_str() );
609 puFont lfnt(font, size, slant);
610 object->setLabelFont( lfnt );
613 if (props->hasValue("property")) {
614 const char * name = props->getStringValue("name");
617 const char * propname = props->getStringValue("property");
618 SGPropertyNode_ptr node = fgGetNode(propname, true);
619 copy_to_pui(node, object);
620 PropertyObject* po = new PropertyObject(name, object, node);
621 _propertyObjects.push_back(po);
622 if(props->getBoolValue("live"))
623 _liveObjects.push_back(po);
626 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
627 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
628 if (bindings.size() > 0) {
629 GUIInfo * info = new GUIInfo(this);
631 for (unsigned int i = 0; i < bindings.size(); i++) {
633 SGPropertyNode *binding;
634 while (dest->getChild("binding", j))
637 binding = dest->getChild("binding", j, true);
638 copyProperties(bindings[i], binding);
639 info->bindings.push_back(new FGBinding(binding));
641 object->setCallback(action_callback);
642 object->setUserData(info);
643 _info.push_back(info);
646 object->makeReturnDefault(props->getBoolValue("default"));
650 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
651 int width, int height, bool makeFrame)
653 setupObject(group, props);
656 puFrame* f = new puFrame(0, 0, width, height);
657 setColor(f, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
660 int nChildren = props->nChildren();
661 for (int i = 0; i < nChildren; i++)
662 makeObject(props->getChild(i), width, height);
667 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
669 string type = props->getName();
670 // first set whole color scheme (see below for a description)
671 FGColor c = _gui->getColor("background");
672 c.merge(_gui->getColor(type));
673 c.merge(props->getNode("color"));
674 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
676 // now look for the 6 plib color qualities
677 const int numcol = 6;
683 BACKGROUND, PUCOL_BACKGROUND, "background",
684 FOREGROUND, PUCOL_FOREGROUND, "foreground",
685 HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight",
686 LABEL, PUCOL_LABEL, "label",
687 LEGEND, PUCOL_LEGEND, "legend",
688 MISC, PUCOL_MISC, "misc",
691 for (int i = 0; i < numcol; i++) {
692 // merge in object color quality components (e.g. "button-background")
693 FGColor c(_gui->getColor(type));
694 c.merge(_gui->getColor(type + '-' + pucol[i].name));
696 // merge in explicit color components from the XML structure (<color>)
697 if (which & pucol[i].mask)
698 c.merge(props->getNode("color"));
699 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
705 FGDialog::preprocess (SGPropertyNode * prop)
707 for (int i = 0; i < prop->nChildren(); i++) {
708 SGPropertyNode *child = prop->getChild(i);
709 const char *name = child->getName();
711 if (!strcmp(name, "title")) {
712 prop->removeChild("title", child->getIndex(), false);
713 SGPropertyNode *tit = fgGetNode("/sim/gui/title");
715 fprintf(stderr, "no tits here!\n");
718 copyProperties(tit, prop->getChild(child->getIndex()));
719 writeProperties(cerr, prop, true);
721 preprocess(prop->getChild(i));
726 FGDialog::make_char_array (int size)
728 char ** list = new char*[size+1];
729 for (int i = 0; i <= size; i++)
731 _char_arrays.push_back(list);
737 ////////////////////////////////////////////////////////////////////////
738 // Implementation of FGDialog::PropertyObject.
739 ////////////////////////////////////////////////////////////////////////
741 FGDialog::PropertyObject::PropertyObject (const char * n,
743 SGPropertyNode_ptr p)