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 ////////////////////////////////////////////////////////////////////////
15 // Implementation of GUIInfo.
16 ////////////////////////////////////////////////////////////////////////
19 * User data for a GUI object.
23 GUIInfo (FGDialog * d);
27 vector <FGBinding *> bindings;
31 GUIInfo::GUIInfo (FGDialog * d)
39 for (unsigned int i = 0; i < bindings.size(); i++) {
49 int fgPopup::checkKey(int key, int updown)
51 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
54 puObject *input = getActiveInputField(this);
56 return input->checkKey(key, updown);
58 puObject *object = getKeyObject(this, key);
60 return puPopup::checkKey(key, updown);
62 object->invokeCallback() ;
66 puObject *fgPopup::getKeyObject(puObject *object, int key)
69 if(object->getType() & PUCLASS_GROUP)
70 for (puObject *obj = ((puGroup *)object)->getFirstChild();
71 obj; obj = obj->getNextObject())
72 if ((ret = getKeyObject(obj, key)))
75 GUIInfo *info = (GUIInfo *)object->getUserData();
76 if (info && info->key == key)
82 puObject *fgPopup::getActiveInputField(puObject *object)
85 if(object->getType() & PUCLASS_GROUP)
86 for (puObject *obj = ((puGroup *)object)->getFirstChild();
87 obj; obj = obj->getNextObject())
88 if ((ret = getActiveInputField(obj)))
91 if (object->getType() & PUCLASS_INPUT && ((puInput *)object)->isAcceptingInput())
100 int fgPopup::checkHit(int button, int updown, int x, int y)
102 int result = puPopup::checkHit(button, updown, x, y);
107 // This is annoying. We would really want a true result from the
108 // superclass to indicate "handled by child object", but all it
109 // tells us is that the pointer is inside the dialog. So do the
110 // intersection test (again) to make sure we don't start a drag
111 // when inside controls.
113 if(updown == PU_DOWN && !_dragging) {
117 int hit = getHitObjects(this, x, y);
118 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
122 getPosition(&px, &py);
126 } else if(updown == PU_DRAG && _dragging) {
127 setPosition(x + _dX, y + _dY);
134 int fgPopup::getHitObjects(puObject *object, int x, int y)
137 if(object->getType() & PUCLASS_GROUP)
138 for (puObject *obj = ((puGroup *)object)->getFirstChild();
139 obj; obj = obj->getNextObject())
140 type |= getHitObjects(obj, x, y);
143 object->getAbsolutePosition(&cx, &cy);
144 object->getSize(&cw, &ch);
145 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
146 type |= object->getType();
152 ////////////////////////////////////////////////////////////////////////
154 ////////////////////////////////////////////////////////////////////////
160 action_callback (puObject * object)
162 GUIInfo * info = (GUIInfo *)object->getUserData();
163 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
164 gui->setActiveDialog(info->dialog);
165 int nBindings = info->bindings.size();
166 for (int i = 0; i < nBindings; i++) {
167 info->bindings[i]->fire();
168 if (gui->getActiveDialog() == 0)
171 gui->setActiveDialog(0);
176 format_callback(puObject *obj, int dx, int dy, void *n)
178 SGPropertyNode *node = (SGPropertyNode *)n;
179 const char *format = node->getStringValue("format"), *f = format;
180 bool number, l = false;
181 // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
192 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
194 while (*f && isdigit(*f))
198 while (*f && isdigit(*f))
206 else if (*f == 's') {
223 const char *src = obj->getLabel();
226 float value = atof(src);
227 snprintf(buf, 256, format, value);
229 snprintf(buf, 256, format, src);
234 SGPropertyNode *result = node->getNode("formatted", true);
235 result->setStringValue(buf);
236 obj->setLabel(result->getStringValue());
241 ////////////////////////////////////////////////////////////////////////
242 // Static helper functions.
243 ////////////////////////////////////////////////////////////////////////
246 * Copy a property value to a PUI object.
249 copy_to_pui (SGPropertyNode * node, puObject * object)
251 // Treat puText objects specially, so their "values" can be set
253 if(object->getType() & PUCLASS_TEXT) {
254 object->setLabel(node->getStringValue());
258 switch (node->getType()) {
259 case SGPropertyNode::BOOL:
260 case SGPropertyNode::INT:
261 case SGPropertyNode::LONG:
262 object->setValue(node->getIntValue());
264 case SGPropertyNode::FLOAT:
265 case SGPropertyNode::DOUBLE:
266 object->setValue(node->getFloatValue());
269 object->setValue(node->getStringValue());
276 copy_from_pui (puObject * object, SGPropertyNode * node)
278 // puText objects are immutable, so should not be copied out
279 if(object->getType() & PUCLASS_TEXT)
282 switch (node->getType()) {
283 case SGPropertyNode::BOOL:
284 case SGPropertyNode::INT:
285 case SGPropertyNode::LONG:
286 node->setIntValue(object->getIntegerValue());
288 case SGPropertyNode::FLOAT:
289 case SGPropertyNode::DOUBLE:
290 node->setFloatValue(object->getFloatValue());
293 // Special case to handle lists, as getStringValue cannot be overridden
294 if(object->getType() & PUCLASS_LIST)
296 node->setStringValue(((puList *) object)->getListStringValue());
300 node->setStringValue(object->getStringValue());
308 ////////////////////////////////////////////////////////////////////////
309 // Implementation of FGDialog.
310 ////////////////////////////////////////////////////////////////////////
312 FGDialog::FGDialog (SGPropertyNode * props)
314 _gui((NewGUI *)globals->get_subsystem("gui"))
316 char* envp = ::getenv( "FG_FONTS" );
317 if ( envp != NULL ) {
318 _font_path.set( envp );
320 _font_path.set( globals->get_fg_root() );
321 _font_path.append( "Fonts" );
327 FGDialog::~FGDialog ()
329 puDeleteObject(_object);
333 // Delete all the arrays we made
334 // and were forced to keep around
335 // because PUI won't do its own
336 // memory management.
337 for (i = 0; i < _char_arrays.size(); i++) {
338 for (int j = 0; _char_arrays[i][j] != 0; j++)
339 free(_char_arrays[i][j]); // added with strdup
340 delete[] _char_arrays[i];
343 // Delete all the info objects we
344 // were forced to keep around because
345 // PUI cannot delete its own user data.
346 for (i = 0; i < _info.size(); i++) {
347 delete (GUIInfo *)_info[i];
351 // Finally, delete the property links.
352 for (i = 0; i < _propertyObjects.size(); i++) {
353 delete _propertyObjects[i];
354 _propertyObjects[i] = 0;
359 FGDialog::updateValue (const char * objectName)
361 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
362 const string &name = _propertyObjects[i]->name;
363 if (name == objectName)
364 copy_to_pui(_propertyObjects[i]->node,
365 _propertyObjects[i]->object);
370 FGDialog::applyValue (const char * objectName)
372 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
373 if (_propertyObjects[i]->name == objectName)
374 copy_from_pui(_propertyObjects[i]->object,
375 _propertyObjects[i]->node);
380 FGDialog::updateValues ()
382 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
383 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
387 FGDialog::applyValues ()
389 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
390 copy_from_pui(_propertyObjects[i]->object,
391 _propertyObjects[i]->node);
397 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
398 puObject *obj = _liveObjects[i]->object;
399 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
402 copy_to_pui(_liveObjects[i]->node, obj);
407 FGDialog::display (SGPropertyNode * props)
410 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
414 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
415 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
417 bool userx = props->hasValue("x");
418 bool usery = props->hasValue("y");
419 bool userw = props->hasValue("width");
420 bool userh = props->hasValue("height");
422 // Let the layout widget work in the same property subtree.
423 LayoutWidget wid(props);
425 puFont *fnt = _gui->getDefaultFont();
426 wid.setDefaultFont(fnt, int(fnt->getPointSize()));
430 wid.calcPrefSize(&pw, &ph);
431 pw = props->getIntValue("width", pw);
432 ph = props->getIntValue("height", ph);
433 int px = props->getIntValue("x", (screenw - pw) / 2);
434 int py = props->getIntValue("y", (screenh - ph) / 2);
436 // Define "x", "y", "width" and/or "height" in the property tree if they
437 // are not specified in the configuration file.
438 wid.layout(px, py, pw, ph);
440 // Use the dimension and location properties as specified in the
441 // configuration file or from the layout widget.
442 _object = makeObject(props, screenw, screenh);
444 // Remove automatically generated properties, so the layout looks
445 // the same next time around.
446 if(!userx) props->removeChild("x");
447 if(!usery) props->removeChild("y");
448 if(!userw) props->removeChild("width");
449 if(!userh) props->removeChild("height");
454 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
455 << props->getStringValue("name", "[unnamed]")
456 << " does not contain a proper GUI definition");
461 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
463 if (props->getBoolValue("hide"))
466 bool presetSize = props->hasValue("width") && props->hasValue("height");
467 int width = props->getIntValue("width", parentWidth);
468 int height = props->getIntValue("height", parentHeight);
469 int x = props->getIntValue("x", (parentWidth - width) / 2);
470 int y = props->getIntValue("y", (parentHeight - height) / 2);
471 string type = props->getName();
476 if (type == "dialog") {
478 bool draggable = props->getBoolValue("draggable", true);
479 if (props->getBoolValue("modal", false))
480 obj = new puDialogBox(x, y);
482 obj = new fgPopup(x, y, draggable);
483 setupGroup(obj, props, width, height, true);
484 setColor(obj, props);
487 } else if (type == "group") {
488 puGroup * obj = new puGroup(x, y);
489 setupGroup(obj, props, width, height, false);
490 setColor(obj, props);
493 } else if (type == "frame") {
494 puGroup * obj = new puGroup(x, y);
495 setupGroup(obj, props, width, height, true);
496 setColor(obj, props);
499 } else if (type == "hrule" || type == "vrule") {
500 puFrame * obj = new puFrame(x, y, x + width, y + height);
501 obj->setBorderThickness(0);
502 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
505 } else if (type == "list") {
506 puList * obj = new puList(x, y, x + width, y + height);
507 setupObject(obj, props);
508 setColor(obj, props);
511 } else if (type == "airport-list") {
512 AirportList * obj = new AirportList(x, y, x + width, y + height);
513 setupObject(obj, props);
514 setColor(obj, props);
517 } else if (type == "input") {
518 puInput * obj = new puInput(x, y, x + width, y + height);
519 setupObject(obj, props);
520 setColor(obj, props, FOREGROUND|LABEL);
523 } else if (type == "text") {
524 puText * obj = new puText(x, y);
525 setupObject(obj, props);
527 if (props->getNode("format")) {
528 SGPropertyNode *live = props->getNode("live");
529 if (live && live->getBoolValue())
530 obj->setRenderCallback(format_callback, props);
532 format_callback(obj, x, y, props);
534 // Layed-out objects need their size set, and non-layout ones
535 // get a different placement.
536 if(presetSize) obj->setSize(width, height);
537 else obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
538 setColor(obj, props, LABEL);
541 } else if (type == "checkbox") {
543 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
544 setupObject(obj, props);
545 setColor(obj, props, FOREGROUND|LABEL);
548 } else if (type == "radio") {
550 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
551 setupObject(obj, props);
552 setColor(obj, props, FOREGROUND|LABEL);
555 } else if (type == "button") {
557 const char * legend = props->getStringValue("legend", "[none]");
558 if (props->getBoolValue("one-shot", true))
559 obj = new puOneShot(x, y, legend);
561 obj = new puButton(x, y, legend);
563 obj->setSize(width, height);
564 setupObject(obj, props);
565 setColor(obj, props);
568 } else if (type == "combo") {
569 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
570 char ** entries = make_char_array(value_nodes.size());
571 for (unsigned int i = 0, j = value_nodes.size() - 1;
572 i < value_nodes.size();
574 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
575 puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
576 props->getBoolValue("editable", false));
577 setupObject(obj, props);
578 setColor(obj, props, FOREGROUND|LABEL);
581 } else if (type == "slider") {
582 bool vertical = props->getBoolValue("vertical", false);
583 puSlider * obj = new puSlider(x, y, (vertical ? height : width));
584 obj->setMinValue(props->getFloatValue("min", 0.0));
585 obj->setMaxValue(props->getFloatValue("max", 1.0));
586 setupObject(obj, props);
588 obj->setSize(width, height);
589 setColor(obj, props, FOREGROUND|LABEL);
592 } else if (type == "dial") {
593 puDial * obj = new puDial(x, y, width);
594 obj->setMinValue(props->getFloatValue("min", 0.0));
595 obj->setMaxValue(props->getFloatValue("max", 1.0));
596 obj->setWrap(props->getBoolValue("wrap", true));
597 setupObject(obj, props);
598 setColor(obj, props, FOREGROUND|LABEL);
601 } else if (type == "textbox") {
602 int slider_width = props->getIntValue("slider", parentHeight);
603 int wrap = props->getBoolValue("wrap", true);
604 if (slider_width==0) slider_width=20;
605 puLargeInput * obj = new puLargeInput(x, y,
606 x+width, x+height, 2, slider_width, wrap);
608 if (props->hasValue("editable")) {
609 if (props->getBoolValue("editable")==false)
614 setupObject(obj, props);
615 setColor(obj, props, FOREGROUND|LABEL);
618 } else if (type == "select") {
619 vector<SGPropertyNode_ptr> value_nodes;
620 SGPropertyNode * selection_node =
621 fgGetNode(props->getChild("selection")->getStringValue(), true);
623 for (int q = 0; q < selection_node->nChildren(); q++)
624 value_nodes.push_back(selection_node->getChild(q));
626 char ** entries = make_char_array(value_nodes.size());
627 for (unsigned int i = 0, j = value_nodes.size() - 1;
628 i < value_nodes.size();
630 entries[i] = strdup((char *)value_nodes[i]->getName());
632 new puSelectBox(x, y, x + width, y + height, entries);
633 setupObject(obj, props);
634 setColor(obj, props, FOREGROUND|LABEL);
642 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
644 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
646 if (props->hasValue("legend"))
647 object->setLegend(props->getStringValue("legend"));
649 if (props->hasValue("label"))
650 object->setLabel(props->getStringValue("label"));
652 if (props->hasValue("border"))
653 object->setBorderThickness( props->getIntValue("border", 2) );
655 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
656 SGPath path( _font_path );
657 const char *name = nft->getStringValue("name", "default");
658 float size = nft->getFloatValue("size", 13.0);
659 float slant = nft->getFloatValue("slant", 0.0);
661 path.concat( ".txf" );
663 fntFont *font = new fntTexFont;
664 font->load( (char *)path.c_str() );
666 puFont lfnt(font, size, slant);
667 object->setLabelFont( lfnt );
670 if (props->hasValue("property")) {
671 const char * name = props->getStringValue("name");
674 const char * propname = props->getStringValue("property");
675 SGPropertyNode_ptr node = fgGetNode(propname, true);
676 copy_to_pui(node, object);
677 PropertyObject* po = new PropertyObject(name, object, node);
678 _propertyObjects.push_back(po);
679 if(props->getBoolValue("live"))
680 _liveObjects.push_back(po);
683 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
684 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
685 if (bindings.size() > 0) {
686 GUIInfo * info = new GUIInfo(this);
687 info->key = props->getIntValue("keynum", -1);
689 for (unsigned int i = 0; i < bindings.size(); i++) {
691 SGPropertyNode *binding;
692 while (dest->getChild("binding", j))
695 binding = dest->getChild("binding", j, true);
696 copyProperties(bindings[i], binding);
697 info->bindings.push_back(new FGBinding(binding));
699 object->setCallback(action_callback);
700 object->setUserData(info);
701 _info.push_back(info);
704 object->makeReturnDefault(props->getBoolValue("default"));
708 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
709 int width, int height, bool makeFrame)
711 setupObject(group, props);
714 puFrame* f = new puFrame(0, 0, width, height);
718 int nChildren = props->nChildren();
719 for (int i = 0; i < nChildren; i++)
720 makeObject(props->getChild(i), width, height);
725 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
727 string type = props->getName();
731 FGColor *c = new FGColor(_gui->getColor("background"));
732 c->merge(_gui->getColor(type));
733 c->merge(props->getNode("color"));
735 object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
743 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
744 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
745 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
746 { LABEL, PUCOL_LABEL, "label", "color-label" },
747 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
748 { MISC, PUCOL_MISC, "misc", "color-misc" }
751 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
753 for (int i = 0; i < numcol; i++) {
758 dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
759 if (which & pucol[i].mask)
760 dirty |= c->merge(props->getNode("color"));
762 if ((pucol[i].mask == LABEL) && !c->isValid())
763 dirty |= c->merge(_gui->getColor("label"));
765 if (c->isValid() && dirty)
766 object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
771 FGDialog::make_char_array (int size)
773 char ** list = new char*[size+1];
774 for (int i = 0; i <= size; i++)
776 _char_arrays.push_back(list);
782 ////////////////////////////////////////////////////////////////////////
783 // Implementation of FGDialog::PropertyObject.
784 ////////////////////////////////////////////////////////////////////////
786 FGDialog::PropertyObject::PropertyObject (const char * n,
788 SGPropertyNode_ptr p)