1 // dialog.cxx: implementation of an XML-configurable dialog box.
7 #include <stdlib.h> // atof()
9 #include <Input/input.hxx>
10 #include <Scripting/NasalSys.hxx>
13 #include "new_gui.hxx"
15 #include "AirportList.hxx"
16 #include "property_list.hxx"
22 ////////////////////////////////////////////////////////////////////////
23 // Implementation of GUIInfo.
24 ////////////////////////////////////////////////////////////////////////
27 * User data for a GUI object.
31 GUIInfo (FGDialog * d);
35 vector <FGBinding *> bindings;
39 GUIInfo::GUIInfo (FGDialog * d)
47 for (unsigned int i = 0; i < bindings.size(); i++) {
58 int fgPopup::checkKey(int key, int updown)
60 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
63 puObject *input = getActiveInputField(this);
65 return input->checkKey(key, updown);
67 puObject *object = getKeyObject(this, key);
69 return puPopup::checkKey(key, updown);
71 // invokeCallback() isn't enough; we need to simulate a mouse button press
72 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
73 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
74 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
75 object->checkHit(PU_LEFT_BUTTON, PU_UP,
76 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
77 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
81 puObject *fgPopup::getKeyObject(puObject *object, int key)
84 if(object->getType() & PUCLASS_GROUP)
85 for (puObject *obj = ((puGroup *)object)->getFirstChild();
86 obj; obj = obj->getNextObject())
87 if ((ret = getKeyObject(obj, key)))
90 GUIInfo *info = (GUIInfo *)object->getUserData();
91 if (info && info->key == key)
97 puObject *fgPopup::getActiveInputField(puObject *object)
100 if(object->getType() & PUCLASS_GROUP)
101 for (puObject *obj = ((puGroup *)object)->getFirstChild();
102 obj; obj = obj->getNextObject())
103 if ((ret = getActiveInputField(obj)))
106 if (object->getType() & PUCLASS_INPUT && ((puInput *)object)->isAcceptingInput())
115 int fgPopup::checkHit(int button, int updown, int x, int y)
117 int result = puPopup::checkHit(button, updown, x, y);
122 // This is annoying. We would really want a true result from the
123 // superclass to indicate "handled by child object", but all it
124 // tells us is that the pointer is inside the dialog. So do the
125 // intersection test (again) to make sure we don't start a drag
126 // when inside controls.
128 if(updown == PU_DOWN && !_dragging) {
132 int hit = getHitObjects(this, x, y);
133 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
137 getPosition(&px, &py);
141 } else if(updown == PU_DRAG && _dragging) {
142 setPosition(x + _dX, y + _dY);
149 int fgPopup::getHitObjects(puObject *object, int x, int y)
151 if (!object->isVisible())
155 if(object->getType() & PUCLASS_GROUP)
156 for (puObject *obj = ((puGroup *)object)->getFirstChild();
157 obj; obj = obj->getNextObject())
158 type |= getHitObjects(obj, x, y);
161 object->getAbsolutePosition(&cx, &cy);
162 object->getSize(&cw, &ch);
163 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
164 type |= object->getType();
170 ////////////////////////////////////////////////////////////////////////
172 ////////////////////////////////////////////////////////////////////////
178 action_callback (puObject * object)
180 GUIInfo * info = (GUIInfo *)object->getUserData();
181 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
182 gui->setActiveDialog(info->dialog);
183 int nBindings = info->bindings.size();
184 for (int i = 0; i < nBindings; i++) {
185 info->bindings[i]->fire();
186 if (gui->getActiveDialog() == 0)
189 gui->setActiveDialog(0);
194 format_callback(puObject *obj, int dx, int dy, void *n)
196 SGPropertyNode *node = (SGPropertyNode *)n;
197 const char *format = node->getStringValue("format"), *f = format;
198 bool number, l = false;
199 // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
210 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
212 while (*f && isdigit(*f))
216 while (*f && isdigit(*f))
224 else if (*f == 's') {
241 const char *src = obj->getLabel();
244 float value = atof(src);
245 snprintf(buf, 256, format, value);
247 snprintf(buf, 256, format, src);
252 SGPropertyNode *result = node->getNode("formatted", true);
253 result->setStringValue(buf);
254 obj->setLabel(result->getStringValue());
259 ////////////////////////////////////////////////////////////////////////
260 // Static helper functions.
261 ////////////////////////////////////////////////////////////////////////
264 * Copy a property value to a PUI object.
267 copy_to_pui (SGPropertyNode * node, puObject * object)
269 // Treat puText objects specially, so their "values" can be set
271 if(object->getType() & PUCLASS_TEXT) {
272 object->setLabel(node->getStringValue());
276 switch (node->getType()) {
277 case SGPropertyNode::BOOL:
278 case SGPropertyNode::INT:
279 case SGPropertyNode::LONG:
280 object->setValue(node->getIntValue());
282 case SGPropertyNode::FLOAT:
283 case SGPropertyNode::DOUBLE:
284 object->setValue(node->getFloatValue());
287 object->setValue(node->getStringValue());
294 copy_from_pui (puObject * object, SGPropertyNode * node)
296 // puText objects are immutable, so should not be copied out
297 if(object->getType() & PUCLASS_TEXT)
300 switch (node->getType()) {
301 case SGPropertyNode::BOOL:
302 case SGPropertyNode::INT:
303 case SGPropertyNode::LONG:
304 node->setIntValue(object->getIntegerValue());
306 case SGPropertyNode::FLOAT:
307 case SGPropertyNode::DOUBLE:
308 node->setFloatValue(object->getFloatValue());
311 // Special case to handle lists, as getStringValue cannot be overridden
312 if(object->getType() & PUCLASS_LIST)
314 const char *s = ((puList *) object)->getListStringValue();
316 node->setStringValue(s);
320 node->setStringValue(object->getStringValue());
328 ////////////////////////////////////////////////////////////////////////
329 // Implementation of FGDialog.
330 ////////////////////////////////////////////////////////////////////////
332 FGDialog::FGDialog (SGPropertyNode * props)
334 _gui((NewGUI *)globals->get_subsystem("gui")),
337 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
338 SGPropertyNode *nasal = props->getNode("nasal");
340 _nasal_close = nasal->getNode("close");
341 SGPropertyNode *open = nasal->getNode("open");
343 const char *s = open->getStringValue();
344 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
345 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
351 FGDialog::~FGDialog ()
354 _object->getAbsolutePosition(&x, &y);
355 _props->setIntValue("lastx", x);
356 _props->setIntValue("lasty", y);
358 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
360 const char *s = _nasal_close->getStringValue();
361 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
363 nas->deleteModule(_module.c_str());
365 puDeleteObject(_object);
368 // Delete all the info objects we
369 // were forced to keep around because
370 // PUI cannot delete its own user data.
371 for (i = 0; i < _info.size(); i++) {
372 delete (GUIInfo *)_info[i];
375 // Finally, delete the property links.
376 for (i = 0; i < _propertyObjects.size(); i++) {
377 delete _propertyObjects[i];
378 _propertyObjects[i] = 0;
383 FGDialog::updateValues (const char * objectName)
385 if (objectName && !objectName[0])
388 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
389 const string &name = _propertyObjects[i]->name;
390 if (objectName && name != objectName)
393 puObject *obj = _propertyObjects[i]->object;
394 if ((obj->getType() & PUCLASS_LIST) && (dynamic_cast<GUI_ID *>(obj)->id & FGCLASS_LIST)) {
395 fgList *pl = static_cast<fgList *>(obj);
398 copy_to_pui(_propertyObjects[i]->node, obj);
403 FGDialog::applyValues (const char * objectName)
405 if (objectName && !objectName[0])
408 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
409 const string &name = _propertyObjects[i]->name;
410 if (objectName && name != objectName)
413 copy_from_pui(_propertyObjects[i]->object,
414 _propertyObjects[i]->node);
421 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
422 puObject *obj = _liveObjects[i]->object;
423 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
426 copy_to_pui(_liveObjects[i]->node, obj);
431 FGDialog::display (SGPropertyNode * props)
434 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
438 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
439 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
441 bool userx = props->hasValue("x");
442 bool usery = props->hasValue("y");
443 bool userw = props->hasValue("width");
444 bool userh = props->hasValue("height");
446 // Let the layout widget work in the same property subtree.
447 LayoutWidget wid(props);
449 SGPropertyNode *fontnode = props->getNode("font");
451 FGFontCache *fc = globals->get_fontcache();
452 _font = fc->get(fontnode);
454 _font = _gui->getDefaultFont();
456 wid.setDefaultFont(_font, int(_font->getPointSize()));
459 int px, py, savex, savey;
461 wid.calcPrefSize(&pw, &ph);
462 pw = props->getIntValue("width", pw);
463 ph = props->getIntValue("height", ph);
464 px = savex = props->getIntValue("x", (screenw - pw) / 2);
465 py = savey = props->getIntValue("y", (screenh - ph) / 2);
467 // Negative x/y coordinates are interpreted as distance from the top/right
468 // corner rather than bottom/left.
470 px = screenw - pw + px;
472 py = screenh - ph + py;
474 // Define "x", "y", "width" and/or "height" in the property tree if they
475 // are not specified in the configuration file.
476 wid.layout(px, py, pw, ph);
478 // Use the dimension and location properties as specified in the
479 // configuration file or from the layout widget.
480 _object = makeObject(props, screenw, screenh);
482 // Remove automatically generated properties, so the layout looks
483 // the same next time around, or restore x and y to preserve negative coords.
485 props->setIntValue("x", savex);
487 props->removeChild("x");
490 props->setIntValue("y", savey);
492 props->removeChild("y");
494 if(!userw) props->removeChild("width");
495 if(!userh) props->removeChild("height");
500 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
501 << props->getStringValue("name", "[unnamed]")
502 << " does not contain a proper GUI definition");
507 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
509 if (props->getBoolValue("hide"))
512 bool presetSize = props->hasValue("width") && props->hasValue("height");
513 int width = props->getIntValue("width", parentWidth);
514 int height = props->getIntValue("height", parentHeight);
515 int x = props->getIntValue("x", (parentWidth - width) / 2);
516 int y = props->getIntValue("y", (parentHeight - height) / 2);
517 string type = props->getName();
522 if (type == "dialog") {
524 bool draggable = props->getBoolValue("draggable", true);
525 if (props->getBoolValue("modal", false))
526 obj = new puDialogBox(x, y);
528 obj = new fgPopup(x, y, draggable);
529 setupGroup(obj, props, width, height, true);
530 setColor(obj, props);
533 } else if (type == "group") {
534 puGroup * obj = new puGroup(x, y);
535 setupGroup(obj, props, width, height, false);
536 setColor(obj, props);
539 } else if (type == "frame") {
540 puGroup * obj = new puGroup(x, y);
541 setupGroup(obj, props, width, height, true);
542 setColor(obj, props);
545 } else if (type == "hrule" || type == "vrule") {
546 puFrame * obj = new puFrame(x, y, x + width, y + height);
547 obj->setBorderThickness(0);
548 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
551 } else if (type == "list") {
552 int slider_width = props->getIntValue("slider", 20);
553 fgList * obj = new fgList(x, y, x + width, y + height, props, slider_width);
555 obj->setSize(width, height);
556 setupObject(obj, props);
557 setColor(obj, props);
560 } else if (type == "airport-list") {
561 AirportList * obj = new AirportList(x, y, x + width, y + height);
563 obj->setSize(width, height);
564 setupObject(obj, props);
565 setColor(obj, props);
568 } else if (type == "property-list") {
569 PropertyList * obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
571 obj->setSize(width, height);
572 setupObject(obj, props);
573 setColor(obj, props);
576 } else if (type == "input") {
577 puInput * obj = new puInput(x, y, x + width, y + height);
578 setupObject(obj, props);
579 setColor(obj, props, FOREGROUND|LABEL);
582 } else if (type == "text") {
583 puText * obj = new puText(x, y);
584 setupObject(obj, props);
586 if (props->getNode("format")) {
587 SGPropertyNode *live = props->getNode("live");
588 if (live && live->getBoolValue())
589 obj->setRenderCallback(format_callback, props);
591 format_callback(obj, x, y, props);
593 // Layed-out objects need their size set, and non-layout ones
594 // get a different placement.
596 obj->setSize(width, height);
598 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
599 setColor(obj, props, LABEL);
602 } else if (type == "checkbox") {
604 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
605 setupObject(obj, props);
606 setColor(obj, props, FOREGROUND|LABEL);
609 } else if (type == "radio") {
611 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
612 setupObject(obj, props);
613 setColor(obj, props, FOREGROUND|LABEL);
616 } else if (type == "button") {
618 const char * legend = props->getStringValue("legend", "[none]");
619 if (props->getBoolValue("one-shot", true))
620 obj = new puOneShot(x, y, legend);
622 obj = new puButton(x, y, legend);
624 obj->setSize(width, height);
625 setupObject(obj, props);
626 setColor(obj, props);
629 } else if (type == "combo") {
630 fgComboBox * obj = new fgComboBox(x, y, x + width, y + height, props,
631 props->getBoolValue("editable", false));
632 setupObject(obj, props);
633 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
634 setColor(obj, props, EDITFIELD);
636 setColor(obj, props, FOREGROUND|LABEL);
640 } else if (type == "slider") {
641 bool vertical = props->getBoolValue("vertical", false);
642 puSlider * obj = new puSlider(x, y, (vertical ? height : width));
643 obj->setMinValue(props->getFloatValue("min", 0.0));
644 obj->setMaxValue(props->getFloatValue("max", 1.0));
645 setupObject(obj, props);
647 obj->setSize(width, height);
648 setColor(obj, props, FOREGROUND|LABEL);
651 } else if (type == "dial") {
652 puDial * obj = new puDial(x, y, width);
653 obj->setMinValue(props->getFloatValue("min", 0.0));
654 obj->setMaxValue(props->getFloatValue("max", 1.0));
655 obj->setWrap(props->getBoolValue("wrap", true));
656 setupObject(obj, props);
657 setColor(obj, props, FOREGROUND|LABEL);
660 } else if (type == "textbox") {
661 int slider_width = props->getIntValue("slider", 20);
662 int wrap = props->getBoolValue("wrap", true);
663 puaLargeInput * obj = new puaLargeInput(x, y,
664 x+width, x+height, 2, slider_width, wrap);
666 if (props->hasValue("editable")) {
667 if (props->getBoolValue("editable")==false)
673 obj->setSize(width, height);
674 setupObject(obj, props);
675 setColor(obj, props, FOREGROUND|LABEL);
678 } else if (type == "select") {
679 fgSelectBox * obj = new fgSelectBox(x, y, x + width, y + height, props);
680 setupObject(obj, props);
681 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
682 setColor(obj, props, EDITFIELD);
684 setColor(obj, props, FOREGROUND|LABEL);
693 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
695 string type = props->getName();
696 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
698 if (props->hasValue("legend"))
699 object->setLegend(props->getStringValue("legend"));
701 if (props->hasValue("label"))
702 object->setLabel(props->getStringValue("label"));
704 if (props->hasValue("border"))
705 object->setBorderThickness( props->getIntValue("border", 2) );
707 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
708 FGFontCache *fc = globals->get_fontcache();
709 puFont *lfnt = fc->get(nft);
710 object->setLabelFont(*lfnt);
712 object->setLabelFont(*_font);
715 if (props->hasValue("property")) {
716 const char * name = props->getStringValue("name");
719 const char * propname = props->getStringValue("property");
720 SGPropertyNode_ptr node = fgGetNode(propname, true);
721 copy_to_pui(node, object);
723 PropertyObject* po = new PropertyObject(name, object, node);
724 _propertyObjects.push_back(po);
725 if(props->getBoolValue("live"))
726 _liveObjects.push_back(po);
729 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
730 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
731 if (bindings.size() > 0) {
732 GUIInfo * info = new GUIInfo(this);
733 info->key = props->getIntValue("keynum", -1);
734 if (props->hasValue("key"))
735 info->key = getKeyCode(props->getStringValue("key", ""));
737 for (unsigned int i = 0; i < bindings.size(); i++) {
739 SGPropertyNode *binding;
740 while (dest->getChild("binding", j))
743 const char *cmd = bindings[i]->getStringValue("command");
744 if (!strcmp(cmd, "nasal"))
745 bindings[i]->setStringValue("module", _module.c_str());
747 binding = dest->getChild("binding", j, true);
748 copyProperties(bindings[i], binding);
749 info->bindings.push_back(new FGBinding(binding));
751 object->setCallback(action_callback);
753 if (type == "input" && props->getBoolValue("live"))
754 object->setDownCallback(action_callback);
756 object->setUserData(info);
757 _info.push_back(info);
760 object->makeReturnDefault(props->getBoolValue("default"));
764 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
765 int width, int height, bool makeFrame)
767 setupObject(group, props);
770 puFrame* f = new puFrame(0, 0, width, height);
774 int nChildren = props->nChildren();
775 for (int i = 0; i < nChildren; i++)
776 makeObject(props->getChild(i), width, height);
781 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
783 string type = props->getName();
786 if (type == "textbox" && props->getBoolValue("editable"))
789 FGColor *c = new FGColor(_gui->getColor("background"));
790 c->merge(_gui->getColor(type));
791 c->merge(props->getNode("color"));
793 object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
801 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
802 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
803 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
804 { LABEL, PUCOL_LABEL, "label", "color-label" },
805 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
806 { MISC, PUCOL_MISC, "misc", "color-misc" },
807 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
808 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
812 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
814 for (int i = 0; i < numcol; i++) {
819 dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
820 if (which & pucol[i].mask)
821 dirty |= c->merge(props->getNode("color"));
823 if ((pucol[i].mask == LABEL) && !c->isValid())
824 dirty |= c->merge(_gui->getColor("label"));
826 dirty |= c->merge(props->getNode(pucol[i].cname));
828 if (c->isValid() && dirty)
829 object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
861 {"left", PU_KEY_LEFT},
863 {"right", PU_KEY_RIGHT},
864 {"down", PU_KEY_DOWN},
865 {"pageup", PU_KEY_PAGE_UP},
866 {"pagedn", PU_KEY_PAGE_DOWN},
867 {"home", PU_KEY_HOME},
869 {"insert", PU_KEY_INSERT},
874 FGDialog::getKeyCode(const char *str)
885 char *buf = new char[strlen(str) + 1];
887 char *s = buf + strlen(buf);
888 while (s > str && s[-1] == ' ')
895 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
897 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
898 s += 6, mod |= SHIFT;
899 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
906 if (strlen(s) == 1 && isascii(*s)) {
911 key = toupper(key) - 64;
913 ; // Alt not propagated to the gui
915 for (char *t = s; *t; t++)
917 for (int i = 0; keymap[i].name; i++) {
918 if (!strcmp(s, keymap[i].name)) {
930 ////////////////////////////////////////////////////////////////////////
931 // Implementation of FGDialog::PropertyObject.
932 ////////////////////////////////////////////////////////////////////////
934 FGDialog::PropertyObject::PropertyObject (const char * n,
936 SGPropertyNode_ptr p)
946 ////////////////////////////////////////////////////////////////////////
947 // Implementation of fgValueList and derived pui widgets
948 ////////////////////////////////////////////////////////////////////////
951 fgValueList::fgValueList(SGPropertyNode *p) :
958 fgValueList::update()
964 fgValueList::~fgValueList()
970 fgValueList::make_list()
972 vector<SGPropertyNode_ptr> value_nodes = _props->getChildren("value");
973 _list = new char *[value_nodes.size() + 1];
975 for (i = 0; i < value_nodes.size(); i++)
976 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
981 fgValueList::destroy_list()
983 for (int i = 0; _list[i] != 0; i++)
994 fgValueList::update();