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 // invokeCallback() isn't enough; we need to simulate a mouse button press
63 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
64 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
65 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
66 object->checkHit(PU_LEFT_BUTTON, PU_UP,
67 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
68 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
72 puObject *fgPopup::getKeyObject(puObject *object, int key)
75 if(object->getType() & PUCLASS_GROUP)
76 for (puObject *obj = ((puGroup *)object)->getFirstChild();
77 obj; obj = obj->getNextObject())
78 if ((ret = getKeyObject(obj, key)))
81 GUIInfo *info = (GUIInfo *)object->getUserData();
82 if (info && info->key == key)
88 puObject *fgPopup::getActiveInputField(puObject *object)
91 if(object->getType() & PUCLASS_GROUP)
92 for (puObject *obj = ((puGroup *)object)->getFirstChild();
93 obj; obj = obj->getNextObject())
94 if ((ret = getActiveInputField(obj)))
97 if (object->getType() & PUCLASS_INPUT && ((puInput *)object)->isAcceptingInput())
106 int fgPopup::checkHit(int button, int updown, int x, int y)
108 int result = puPopup::checkHit(button, updown, x, y);
113 // This is annoying. We would really want a true result from the
114 // superclass to indicate "handled by child object", but all it
115 // tells us is that the pointer is inside the dialog. So do the
116 // intersection test (again) to make sure we don't start a drag
117 // when inside controls.
119 if(updown == PU_DOWN && !_dragging) {
123 int hit = getHitObjects(this, x, y);
124 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
128 getPosition(&px, &py);
132 } else if(updown == PU_DRAG && _dragging) {
133 setPosition(x + _dX, y + _dY);
140 int fgPopup::getHitObjects(puObject *object, int x, int y)
143 if(object->getType() & PUCLASS_GROUP)
144 for (puObject *obj = ((puGroup *)object)->getFirstChild();
145 obj; obj = obj->getNextObject())
146 type |= getHitObjects(obj, x, y);
149 object->getAbsolutePosition(&cx, &cy);
150 object->getSize(&cw, &ch);
151 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
152 type |= object->getType();
158 ////////////////////////////////////////////////////////////////////////
160 ////////////////////////////////////////////////////////////////////////
166 action_callback (puObject * object)
168 GUIInfo * info = (GUIInfo *)object->getUserData();
169 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
170 gui->setActiveDialog(info->dialog);
171 int nBindings = info->bindings.size();
172 for (int i = 0; i < nBindings; i++) {
173 info->bindings[i]->fire();
174 if (gui->getActiveDialog() == 0)
177 gui->setActiveDialog(0);
182 format_callback(puObject *obj, int dx, int dy, void *n)
184 SGPropertyNode *node = (SGPropertyNode *)n;
185 const char *format = node->getStringValue("format"), *f = format;
186 bool number, l = false;
187 // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
198 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
200 while (*f && isdigit(*f))
204 while (*f && isdigit(*f))
212 else if (*f == 's') {
229 const char *src = obj->getLabel();
232 float value = atof(src);
233 snprintf(buf, 256, format, value);
235 snprintf(buf, 256, format, src);
240 SGPropertyNode *result = node->getNode("formatted", true);
241 result->setStringValue(buf);
242 obj->setLabel(result->getStringValue());
247 ////////////////////////////////////////////////////////////////////////
248 // Static helper functions.
249 ////////////////////////////////////////////////////////////////////////
252 * Copy a property value to a PUI object.
255 copy_to_pui (SGPropertyNode * node, puObject * object)
257 // Treat puText objects specially, so their "values" can be set
259 if(object->getType() & PUCLASS_TEXT) {
260 object->setLabel(node->getStringValue());
264 switch (node->getType()) {
265 case SGPropertyNode::BOOL:
266 case SGPropertyNode::INT:
267 case SGPropertyNode::LONG:
268 object->setValue(node->getIntValue());
270 case SGPropertyNode::FLOAT:
271 case SGPropertyNode::DOUBLE:
272 object->setValue(node->getFloatValue());
275 object->setValue(node->getStringValue());
282 copy_from_pui (puObject * object, SGPropertyNode * node)
284 // puText objects are immutable, so should not be copied out
285 if(object->getType() & PUCLASS_TEXT)
288 switch (node->getType()) {
289 case SGPropertyNode::BOOL:
290 case SGPropertyNode::INT:
291 case SGPropertyNode::LONG:
292 node->setIntValue(object->getIntegerValue());
294 case SGPropertyNode::FLOAT:
295 case SGPropertyNode::DOUBLE:
296 node->setFloatValue(object->getFloatValue());
299 // Special case to handle lists, as getStringValue cannot be overridden
300 if(object->getType() & PUCLASS_LIST)
302 node->setStringValue(((puList *) object)->getListStringValue());
306 node->setStringValue(object->getStringValue());
314 ////////////////////////////////////////////////////////////////////////
315 // Implementation of FGDialog.
316 ////////////////////////////////////////////////////////////////////////
318 FGDialog::FGDialog (SGPropertyNode * props)
320 _gui((NewGUI *)globals->get_subsystem("gui"))
322 char* envp = ::getenv( "FG_FONTS" );
323 if ( envp != NULL ) {
324 _font_path.set( envp );
326 _font_path.set( globals->get_fg_root() );
327 _font_path.append( "Fonts" );
333 FGDialog::~FGDialog ()
335 puDeleteObject(_object);
339 // Delete all the arrays we made
340 // and were forced to keep around
341 // because PUI won't do its own
342 // memory management.
343 for (i = 0; i < _char_arrays.size(); i++) {
344 for (int j = 0; _char_arrays[i][j] != 0; j++)
345 free(_char_arrays[i][j]); // added with strdup
346 delete[] _char_arrays[i];
349 // Delete all the info objects we
350 // were forced to keep around because
351 // PUI cannot delete its own user data.
352 for (i = 0; i < _info.size(); i++) {
353 delete (GUIInfo *)_info[i];
357 // Finally, delete the property links.
358 for (i = 0; i < _propertyObjects.size(); i++) {
359 delete _propertyObjects[i];
360 _propertyObjects[i] = 0;
365 FGDialog::updateValue (const char * objectName)
367 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
368 const string &name = _propertyObjects[i]->name;
369 if (name == objectName)
370 copy_to_pui(_propertyObjects[i]->node,
371 _propertyObjects[i]->object);
376 FGDialog::applyValue (const char * objectName)
378 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
379 if (_propertyObjects[i]->name == objectName)
380 copy_from_pui(_propertyObjects[i]->object,
381 _propertyObjects[i]->node);
386 FGDialog::updateValues ()
388 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
389 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
393 FGDialog::applyValues ()
395 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
396 copy_from_pui(_propertyObjects[i]->object,
397 _propertyObjects[i]->node);
403 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
404 puObject *obj = _liveObjects[i]->object;
405 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
408 copy_to_pui(_liveObjects[i]->node, obj);
413 FGDialog::display (SGPropertyNode * props)
416 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
420 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
421 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
423 bool userx = props->hasValue("x");
424 bool usery = props->hasValue("y");
425 bool userw = props->hasValue("width");
426 bool userh = props->hasValue("height");
428 // Let the layout widget work in the same property subtree.
429 LayoutWidget wid(props);
431 puFont *fnt = _gui->getDefaultFont();
432 wid.setDefaultFont(fnt, int(fnt->getPointSize()));
436 wid.calcPrefSize(&pw, &ph);
437 pw = props->getIntValue("width", pw);
438 ph = props->getIntValue("height", ph);
439 int px = props->getIntValue("x", (screenw - pw) / 2);
440 int py = props->getIntValue("y", (screenh - ph) / 2);
442 // Define "x", "y", "width" and/or "height" in the property tree if they
443 // are not specified in the configuration file.
444 wid.layout(px, py, pw, ph);
446 // Use the dimension and location properties as specified in the
447 // configuration file or from the layout widget.
448 _object = makeObject(props, screenw, screenh);
450 // Remove automatically generated properties, so the layout looks
451 // the same next time around.
452 if(!userx) props->removeChild("x");
453 if(!usery) props->removeChild("y");
454 if(!userw) props->removeChild("width");
455 if(!userh) props->removeChild("height");
460 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
461 << props->getStringValue("name", "[unnamed]")
462 << " does not contain a proper GUI definition");
467 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
469 if (props->getBoolValue("hide"))
472 bool presetSize = props->hasValue("width") && props->hasValue("height");
473 int width = props->getIntValue("width", parentWidth);
474 int height = props->getIntValue("height", parentHeight);
475 int x = props->getIntValue("x", (parentWidth - width) / 2);
476 int y = props->getIntValue("y", (parentHeight - height) / 2);
477 string type = props->getName();
482 if (type == "dialog") {
484 bool draggable = props->getBoolValue("draggable", true);
485 if (props->getBoolValue("modal", false))
486 obj = new puDialogBox(x, y);
488 obj = new fgPopup(x, y, draggable);
489 setupGroup(obj, props, width, height, true);
490 setColor(obj, props);
493 } else if (type == "group") {
494 puGroup * obj = new puGroup(x, y);
495 setupGroup(obj, props, width, height, false);
496 setColor(obj, props);
499 } else if (type == "frame") {
500 puGroup * obj = new puGroup(x, y);
501 setupGroup(obj, props, width, height, true);
502 setColor(obj, props);
505 } else if (type == "hrule" || type == "vrule") {
506 puFrame * obj = new puFrame(x, y, x + width, y + height);
507 obj->setBorderThickness(0);
508 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
511 } else if (type == "list") {
512 puList * obj = new puList(x, y, x + width, y + height);
513 setupObject(obj, props);
514 setColor(obj, props);
517 } else if (type == "airport-list") {
518 AirportList * obj = new AirportList(x, y, x + width, y + height);
519 setupObject(obj, props);
520 setColor(obj, props);
523 } else if (type == "input") {
524 puInput * obj = new puInput(x, y, x + width, y + height);
525 setupObject(obj, props);
526 setColor(obj, props, FOREGROUND|LABEL);
529 } else if (type == "text") {
530 puText * obj = new puText(x, y);
531 setupObject(obj, props);
533 if (props->getNode("format")) {
534 SGPropertyNode *live = props->getNode("live");
535 if (live && live->getBoolValue())
536 obj->setRenderCallback(format_callback, props);
538 format_callback(obj, x, y, props);
540 // Layed-out objects need their size set, and non-layout ones
541 // get a different placement.
542 if(presetSize) obj->setSize(width, height);
543 else obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
544 setColor(obj, props, LABEL);
547 } else if (type == "checkbox") {
549 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
550 setupObject(obj, props);
551 setColor(obj, props, FOREGROUND|LABEL);
554 } else if (type == "radio") {
556 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
557 setupObject(obj, props);
558 setColor(obj, props, FOREGROUND|LABEL);
561 } else if (type == "button") {
563 const char * legend = props->getStringValue("legend", "[none]");
564 if (props->getBoolValue("one-shot", true))
565 obj = new puOneShot(x, y, legend);
567 obj = new puButton(x, y, legend);
569 obj->setSize(width, height);
570 setupObject(obj, props);
571 setColor(obj, props);
574 } else if (type == "combo") {
575 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
576 char ** entries = make_char_array(value_nodes.size());
577 for (unsigned int i = 0, j = value_nodes.size() - 1;
578 i < value_nodes.size();
580 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
581 puaComboBox * obj = new puaComboBox(x, y, x + width, y + height, entries,
582 props->getBoolValue("editable", false));
583 setupObject(obj, props);
584 setColor(obj, props, FOREGROUND|LABEL);
587 } else if (type == "slider") {
588 bool vertical = props->getBoolValue("vertical", false);
589 puSlider * obj = new puSlider(x, y, (vertical ? height : width));
590 obj->setMinValue(props->getFloatValue("min", 0.0));
591 obj->setMaxValue(props->getFloatValue("max", 1.0));
592 setupObject(obj, props);
594 obj->setSize(width, height);
595 setColor(obj, props, FOREGROUND|LABEL);
598 } else if (type == "dial") {
599 puDial * obj = new puDial(x, y, width);
600 obj->setMinValue(props->getFloatValue("min", 0.0));
601 obj->setMaxValue(props->getFloatValue("max", 1.0));
602 obj->setWrap(props->getBoolValue("wrap", true));
603 setupObject(obj, props);
604 setColor(obj, props, FOREGROUND|LABEL);
607 } else if (type == "textbox") {
608 int slider_width = props->getIntValue("slider", parentHeight);
609 int wrap = props->getBoolValue("wrap", true);
610 if (slider_width==0) slider_width=20;
611 puaLargeInput * obj = new puaLargeInput(x, y,
612 x+width, x+height, 2, slider_width, wrap);
614 if (props->hasValue("editable")) {
615 if (props->getBoolValue("editable")==false)
620 setupObject(obj, props);
621 setColor(obj, props, FOREGROUND|LABEL);
624 } else if (type == "select") {
625 vector<SGPropertyNode_ptr> value_nodes;
626 SGPropertyNode * selection_node =
627 fgGetNode(props->getChild("selection")->getStringValue(), true);
629 for (int q = 0; q < selection_node->nChildren(); q++)
630 value_nodes.push_back(selection_node->getChild(q));
632 char ** entries = make_char_array(value_nodes.size());
633 for (unsigned int i = 0, j = value_nodes.size() - 1;
634 i < value_nodes.size();
636 entries[i] = strdup((char *)value_nodes[i]->getName());
638 new puaSelectBox(x, y, x + width, y + height, entries);
639 setupObject(obj, props);
640 setColor(obj, props, FOREGROUND|LABEL);
648 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
650 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
652 if (props->hasValue("legend"))
653 object->setLegend(props->getStringValue("legend"));
655 if (props->hasValue("label"))
656 object->setLabel(props->getStringValue("label"));
658 if (props->hasValue("border"))
659 object->setBorderThickness( props->getIntValue("border", 2) );
661 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
662 SGPath path( _font_path );
663 const char *name = nft->getStringValue("name", "default");
664 float size = nft->getFloatValue("size", 13.0);
665 float slant = nft->getFloatValue("slant", 0.0);
667 path.concat( ".txf" );
669 fntFont *font = new fntTexFont;
670 font->load( (char *)path.c_str() );
672 puFont lfnt(font, size, slant);
673 object->setLabelFont( lfnt );
676 if (props->hasValue("property")) {
677 const char * name = props->getStringValue("name");
680 const char * propname = props->getStringValue("property");
681 SGPropertyNode_ptr node = fgGetNode(propname, true);
682 copy_to_pui(node, object);
683 PropertyObject* po = new PropertyObject(name, object, node);
684 _propertyObjects.push_back(po);
685 if(props->getBoolValue("live"))
686 _liveObjects.push_back(po);
689 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
690 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
691 if (bindings.size() > 0) {
692 GUIInfo * info = new GUIInfo(this);
693 info->key = props->getIntValue("keynum", -1);
694 if (props->hasValue("key"))
695 info->key = getKeyCode(props->getStringValue("key", ""));
697 for (unsigned int i = 0; i < bindings.size(); i++) {
699 SGPropertyNode *binding;
700 while (dest->getChild("binding", j))
703 binding = dest->getChild("binding", j, true);
704 copyProperties(bindings[i], binding);
705 info->bindings.push_back(new FGBinding(binding));
707 object->setCallback(action_callback);
708 object->setUserData(info);
709 _info.push_back(info);
712 object->makeReturnDefault(props->getBoolValue("default"));
716 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
717 int width, int height, bool makeFrame)
719 setupObject(group, props);
722 puFrame* f = new puFrame(0, 0, width, height);
726 int nChildren = props->nChildren();
727 for (int i = 0; i < nChildren; i++)
728 makeObject(props->getChild(i), width, height);
733 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
735 string type = props->getName();
739 FGColor *c = new FGColor(_gui->getColor("background"));
740 c->merge(_gui->getColor(type));
741 c->merge(props->getNode("color"));
743 object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
751 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
752 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
753 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
754 { LABEL, PUCOL_LABEL, "label", "color-label" },
755 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
756 { MISC, PUCOL_MISC, "misc", "color-misc" }
759 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
761 for (int i = 0; i < numcol; i++) {
766 dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
767 if (which & pucol[i].mask)
768 dirty |= c->merge(props->getNode("color"));
770 if ((pucol[i].mask == LABEL) && !c->isValid())
771 dirty |= c->merge(_gui->getColor("label"));
773 if (c->isValid() && dirty)
774 object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
805 {"left", PU_KEY_LEFT},
807 {"right", PU_KEY_RIGHT},
808 {"down", PU_KEY_DOWN},
809 {"pageup", PU_KEY_PAGE_UP},
810 {"pagedn", PU_KEY_PAGE_DOWN},
811 {"home", PU_KEY_HOME},
813 {"insert", PU_KEY_INSERT},
818 FGDialog::getKeyCode(const char *str)
829 char *buf = new char[strlen(str) + 1];
831 char *s = buf + strlen(buf);
832 while (s > str && s[-1] == ' ')
839 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
841 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
842 s += 6, mod |= SHIFT;
843 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
850 if (strlen(s) == 1 && isascii(*s)) {
855 key = toupper(key) - 64;
857 ; // Alt not propagated to the gui
859 for (char *t = s; *t; t++)
861 for (int i = 0; keymap[i].name; i++) {
862 if (!strcmp(s, keymap[i].name)) {
873 FGDialog::make_char_array (int size)
875 char ** list = new char*[size+1];
876 for (int i = 0; i <= size; i++)
878 _char_arrays.push_back(list);
884 ////////////////////////////////////////////////////////////////////////
885 // Implementation of FGDialog::PropertyObject.
886 ////////////////////////////////////////////////////////////////////////
888 FGDialog::PropertyObject::PropertyObject (const char * n,
890 SGPropertyNode_ptr p)