1 // dialog.cxx: implementation of an XML-configurable dialog box.
7 #include <Input/input.hxx>
8 #include <Scripting/NasalSys.hxx>
11 #include "new_gui.hxx"
12 #include "AirportList.hxx"
13 #include "property_list.hxx"
17 enum format_type { f_INVALID, f_INT, f_LONG, f_FLOAT, f_DOUBLE, f_STRING };
18 static const int FORMAT_BUFSIZE = 255;
21 * Makes sure the format matches '%[ -+#]?\d*(\.\d*)?(l?[df]|s)', with
22 * only one number or string placeholder and otherwise arbitrary prefix
23 * and postfix containing only quoted percent signs (%%).
26 validate_format(const char *f)
40 while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
42 while (*f && isdigit(*f))
46 while (*f && isdigit(*f))
54 type = l ? f_LONG : f_INT;
56 type = l ? f_DOUBLE : f_FLOAT;
76 ////////////////////////////////////////////////////////////////////////
77 // Implementation of GUIInfo.
78 ////////////////////////////////////////////////////////////////////////
81 * User data for a GUI object.
85 GUIInfo(FGDialog * d);
87 void apply_format(SGPropertyNode *);
90 vector <SGBinding *> bindings;
92 string label, legend, text, format;
96 GUIInfo::GUIInfo (FGDialog * d) :
105 for (unsigned int i = 0; i < bindings.size(); i++) {
111 void GUIInfo::apply_format(SGPropertyNode *n)
113 char buf[FORMAT_BUFSIZE + 1];
114 if (fmt_type == f_INT)
115 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getIntValue());
116 else if (fmt_type == f_LONG)
117 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getLongValue());
118 else if (fmt_type == f_FLOAT)
119 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getFloatValue());
120 else if (fmt_type == f_DOUBLE)
121 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getDoubleValue());
123 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getStringValue());
125 buf[FORMAT_BUFSIZE] = '\0';
134 int fgPopup::checkKey(int key, int updown)
136 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
139 puObject *input = getActiveInputField(this);
141 return input->checkKey(key, updown);
143 puObject *object = getKeyObject(this, key);
145 return puPopup::checkKey(key, updown);
147 // invokeCallback() isn't enough; we need to simulate a mouse button press
148 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
149 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
150 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
151 object->checkHit(PU_LEFT_BUTTON, PU_UP,
152 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
153 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
157 puObject *fgPopup::getKeyObject(puObject *object, int key)
160 if (object->getType() & PUCLASS_GROUP)
161 for (puObject *obj = ((puGroup *)object)->getFirstChild();
162 obj; obj = obj->getNextObject())
163 if ((ret = getKeyObject(obj, key)))
166 GUIInfo *info = (GUIInfo *)object->getUserData();
167 if (info && info->key == key)
173 puObject *fgPopup::getActiveInputField(puObject *object)
176 if (object->getType() & PUCLASS_GROUP)
177 for (puObject *obj = ((puGroup *)object)->getFirstChild();
178 obj; obj = obj->getNextObject())
179 if ((ret = getActiveInputField(obj)))
182 if (object->getType() & PUCLASS_INPUT && ((puInput *)object)->isAcceptingInput())
191 int fgPopup::checkHit(int button, int updown, int x, int y)
193 int result = puPopup::checkHit(button, updown, x, y);
198 // This is annoying. We would really want a true result from the
199 // superclass to indicate "handled by child object", but all it
200 // tells us is that the pointer is inside the dialog. So do the
201 // intersection test (again) to make sure we don't start a drag
202 // when inside controls.
204 if (updown == PU_DOWN && !_dragging) {
208 int hit = getHitObjects(this, x, y);
209 if (hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
213 getPosition(&px, &py);
217 } else if (updown == PU_DRAG && _dragging) {
218 setPosition(x + _dX, y + _dY);
225 int fgPopup::getHitObjects(puObject *object, int x, int y)
227 if (!object->isVisible())
231 if (object->getType() & PUCLASS_GROUP)
232 for (puObject *obj = ((puGroup *)object)->getFirstChild();
233 obj; obj = obj->getNextObject())
234 type |= getHitObjects(obj, x, y);
237 object->getAbsolutePosition(&cx, &cy);
238 object->getSize(&cw, &ch);
239 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch)
240 type |= object->getType();
246 ////////////////////////////////////////////////////////////////////////
248 ////////////////////////////////////////////////////////////////////////
254 action_callback (puObject * object)
256 GUIInfo * info = (GUIInfo *)object->getUserData();
257 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
258 gui->setActiveDialog(info->dialog);
259 int nBindings = info->bindings.size();
260 for (int i = 0; i < nBindings; i++) {
261 info->bindings[i]->fire();
262 if (gui->getActiveDialog() == 0)
265 gui->setActiveDialog(0);
270 ////////////////////////////////////////////////////////////////////////
271 // Static helper functions.
272 ////////////////////////////////////////////////////////////////////////
275 * Copy a property value to a PUI object.
278 copy_to_pui (SGPropertyNode * node, puObject * object)
280 GUIInfo *info = (GUIInfo *)object->getUserData();
282 SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
283 return; // this can't really happen
286 // Treat puText objects specially, so their "values" can be set
288 if (object->getType() & PUCLASS_TEXT) {
289 if (info->fmt_type != f_INVALID)
290 info->apply_format(node);
292 info->text = node->getStringValue();
294 object->setLabel(info->text.c_str());
298 switch (node->getType()) {
299 case SGPropertyNode::BOOL:
300 case SGPropertyNode::INT:
301 case SGPropertyNode::LONG:
302 object->setValue(node->getIntValue());
304 case SGPropertyNode::FLOAT:
305 case SGPropertyNode::DOUBLE:
306 object->setValue(node->getFloatValue());
309 info->text = node->getStringValue();
310 object->setValue(info->text.c_str());
317 copy_from_pui (puObject * object, SGPropertyNode * node)
319 // puText objects are immutable, so should not be copied out
320 if (object->getType() & PUCLASS_TEXT)
323 switch (node->getType()) {
324 case SGPropertyNode::BOOL:
325 case SGPropertyNode::INT:
326 case SGPropertyNode::LONG:
327 node->setIntValue(object->getIntegerValue());
329 case SGPropertyNode::FLOAT:
330 case SGPropertyNode::DOUBLE:
331 node->setFloatValue(object->getFloatValue());
334 const char *s = object->getStringValue();
336 node->setStringValue(s);
343 ////////////////////////////////////////////////////////////////////////
344 // Implementation of FGDialog.
345 ////////////////////////////////////////////////////////////////////////
347 FGDialog::FGDialog (SGPropertyNode * props)
349 _gui((NewGUI *)globals->get_subsystem("gui")),
352 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
353 SGPropertyNode *nasal = props->getNode("nasal");
355 _nasal_close = nasal->getNode("close");
356 SGPropertyNode *open = nasal->getNode("open");
358 const char *s = open->getStringValue();
359 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
360 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
366 FGDialog::~FGDialog ()
369 _object->getAbsolutePosition(&x, &y);
370 _props->setIntValue("lastx", x);
371 _props->setIntValue("lasty", y);
373 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
375 const char *s = _nasal_close->getStringValue();
376 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
378 nas->deleteModule(_module.c_str());
380 puDeleteObject(_object);
383 // Delete all the info objects we
384 // were forced to keep around because
385 // PUI cannot delete its own user data.
386 for (i = 0; i < _info.size(); i++) {
387 delete (GUIInfo *)_info[i];
390 // Finally, delete the property links.
391 for (i = 0; i < _propertyObjects.size(); i++) {
392 delete _propertyObjects[i];
393 _propertyObjects[i] = 0;
398 FGDialog::updateValues (const char * objectName)
400 if (objectName && !objectName[0])
403 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
404 const string &name = _propertyObjects[i]->name;
405 if (objectName && name != objectName)
408 puObject *obj = _propertyObjects[i]->object;
409 if ((obj->getType() & PUCLASS_LIST) && (dynamic_cast<GUI_ID *>(obj)->id & FGCLASS_LIST)) {
410 fgList *pl = static_cast<fgList *>(obj);
413 copy_to_pui(_propertyObjects[i]->node, obj);
418 FGDialog::applyValues (const char * objectName)
420 if (objectName && !objectName[0])
423 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
424 const string &name = _propertyObjects[i]->name;
425 if (objectName && name != objectName)
428 copy_from_pui(_propertyObjects[i]->object,
429 _propertyObjects[i]->node);
436 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
437 puObject *obj = _liveObjects[i]->object;
438 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
441 copy_to_pui(_liveObjects[i]->node, obj);
446 FGDialog::display (SGPropertyNode * props)
449 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
453 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
454 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
456 bool userx = props->hasValue("x");
457 bool usery = props->hasValue("y");
458 bool userw = props->hasValue("width");
459 bool userh = props->hasValue("height");
461 // Let the layout widget work in the same property subtree.
462 LayoutWidget wid(props);
464 SGPropertyNode *fontnode = props->getNode("font");
466 FGFontCache *fc = globals->get_fontcache();
467 _font = fc->get(fontnode);
469 _font = _gui->getDefaultFont();
471 wid.setDefaultFont(_font, int(_font->getPointSize()));
474 int px, py, savex, savey;
476 wid.calcPrefSize(&pw, &ph);
477 pw = props->getIntValue("width", pw);
478 ph = props->getIntValue("height", ph);
479 px = savex = props->getIntValue("x", (screenw - pw) / 2);
480 py = savey = props->getIntValue("y", (screenh - ph) / 2);
482 // Negative x/y coordinates are interpreted as distance from the top/right
483 // corner rather than bottom/left.
485 px = screenw - pw + px;
487 py = screenh - ph + py;
489 // Define "x", "y", "width" and/or "height" in the property tree if they
490 // are not specified in the configuration file.
491 wid.layout(px, py, pw, ph);
493 // Use the dimension and location properties as specified in the
494 // configuration file or from the layout widget.
495 _object = makeObject(props, screenw, screenh);
497 // Remove automatically generated properties, so the layout looks
498 // the same next time around, or restore x and y to preserve negative coords.
500 props->setIntValue("x", savex);
502 props->removeChild("x");
505 props->setIntValue("y", savey);
507 props->removeChild("y");
509 if(!userw) props->removeChild("width");
510 if(!userh) props->removeChild("height");
515 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
516 << props->getStringValue("name", "[unnamed]")
517 << " does not contain a proper GUI definition");
522 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
524 if (!props->getBoolValue("enabled", true))
527 bool presetSize = props->hasValue("width") && props->hasValue("height");
528 int width = props->getIntValue("width", parentWidth);
529 int height = props->getIntValue("height", parentHeight);
530 int x = props->getIntValue("x", (parentWidth - width) / 2);
531 int y = props->getIntValue("y", (parentHeight - height) / 2);
532 string type = props->getName();
537 if (type == "dialog") {
539 bool draggable = props->getBoolValue("draggable", true);
540 if (props->getBoolValue("modal", false))
541 obj = new puDialogBox(x, y);
543 obj = new fgPopup(x, y, draggable);
544 setupGroup(obj, props, width, height, true);
545 setColor(obj, props);
548 } else if (type == "group") {
549 puGroup * obj = new puGroup(x, y);
550 setupGroup(obj, props, width, height, false);
551 setColor(obj, props);
554 } else if (type == "frame") {
555 puGroup * obj = new puGroup(x, y);
556 setupGroup(obj, props, width, height, true);
557 setColor(obj, props);
560 } else if (type == "hrule" || type == "vrule") {
561 puFrame * obj = new puFrame(x, y, x + width, y + height);
562 obj->setBorderThickness(0);
563 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
566 } else if (type == "list") {
567 int slider_width = props->getIntValue("slider", 20);
568 fgList * obj = new fgList(x, y, x + width, y + height, props, slider_width);
570 obj->setSize(width, height);
571 setupObject(obj, props);
572 setColor(obj, props);
575 } else if (type == "airport-list") {
576 AirportList * obj = new AirportList(x, y, x + width, y + height);
578 obj->setSize(width, height);
579 setupObject(obj, props);
580 setColor(obj, props);
583 } else if (type == "property-list") {
584 PropertyList * obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
586 obj->setSize(width, height);
587 setupObject(obj, props);
588 setColor(obj, props);
591 } else if (type == "input") {
592 puInput * obj = new puInput(x, y, x + width, y + height);
593 setupObject(obj, props);
594 setColor(obj, props, FOREGROUND|LABEL);
597 } else if (type == "text") {
598 puText * obj = new puText(x, y);
599 setupObject(obj, props);
601 // Layed-out objects need their size set, and non-layout ones
602 // get a different placement.
604 obj->setSize(width, height);
606 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
607 setColor(obj, props, LABEL);
610 } else if (type == "checkbox") {
612 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
613 setupObject(obj, props);
614 setColor(obj, props, FOREGROUND|LABEL);
617 } else if (type == "radio") {
619 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
620 setupObject(obj, props);
621 setColor(obj, props, FOREGROUND|LABEL);
624 } else if (type == "button") {
626 const char * legend = props->getStringValue("legend", "[none]");
627 if (props->getBoolValue("one-shot", true))
628 obj = new puOneShot(x, y, legend);
630 obj = new puButton(x, y, legend);
632 obj->setSize(width, height);
633 setupObject(obj, props);
634 setColor(obj, props);
637 } else if (type == "combo") {
638 fgComboBox * obj = new fgComboBox(x, y, x + width, y + height, props,
639 props->getBoolValue("editable", false));
640 setupObject(obj, props);
641 setColor(obj, props, EDITFIELD);
644 } else if (type == "slider") {
645 bool vertical = props->getBoolValue("vertical", false);
646 puSlider * obj = new puSlider(x, y, (vertical ? height : width));
647 obj->setMinValue(props->getFloatValue("min", 0.0));
648 obj->setMaxValue(props->getFloatValue("max", 1.0));
649 setupObject(obj, props);
651 obj->setSize(width, height);
652 setColor(obj, props, FOREGROUND|LABEL);
655 } else if (type == "dial") {
656 puDial * obj = new puDial(x, y, width);
657 obj->setMinValue(props->getFloatValue("min", 0.0));
658 obj->setMaxValue(props->getFloatValue("max", 1.0));
659 obj->setWrap(props->getBoolValue("wrap", true));
660 setupObject(obj, props);
661 setColor(obj, props, FOREGROUND|LABEL);
664 } else if (type == "textbox") {
665 int slider_width = props->getIntValue("slider", 20);
666 int wrap = props->getBoolValue("wrap", true);
667 puaLargeInput * obj = new puaLargeInput(x, y,
668 x+width, x+height, 2, slider_width, wrap);
670 if (props->hasValue("editable")) {
671 if (props->getBoolValue("editable")==false)
677 obj->setSize(width, height);
678 setupObject(obj, props);
679 setColor(obj, props, FOREGROUND|LABEL);
682 } else if (type == "select") {
683 fgSelectBox * obj = new fgSelectBox(x, y, x + width, y + height, props);
684 setupObject(obj, props);
685 setColor(obj, props, EDITFIELD);
693 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
695 GUIInfo *info = new GUIInfo(this);
696 object->setUserData(info);
697 _info.push_back(info);
698 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
699 object->makeReturnDefault(props->getBoolValue("default"));
701 if (props->hasValue("legend")) {
702 info->legend = props->getStringValue("legend");
703 object->setLegend(info->legend.c_str());
706 if (props->hasValue("label")) {
707 info->label = props->getStringValue("label");
708 object->setLabel(info->label.c_str());
711 if (props->hasValue("border"))
712 object->setBorderThickness( props->getIntValue("border", 2) );
714 if (SGPropertyNode *nft = props->getNode("font", false)) {
715 FGFontCache *fc = globals->get_fontcache();
716 puFont *lfnt = fc->get(nft);
717 object->setLabelFont(*lfnt);
718 object->setLegendFont(*lfnt);
720 object->setLabelFont(*_font);
723 if (props->hasValue("property")) {
724 const char * name = props->getStringValue("name");
727 const char * propname = props->getStringValue("property");
728 SGPropertyNode_ptr node = fgGetNode(propname, true);
729 copy_to_pui(node, object);
731 PropertyObject* po = new PropertyObject(name, object, node);
732 _propertyObjects.push_back(po);
733 if (props->getBoolValue("live"))
734 _liveObjects.push_back(po);
737 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
738 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
739 if (bindings.size() > 0) {
740 info->key = props->getIntValue("keynum", -1);
741 if (props->hasValue("key"))
742 info->key = getKeyCode(props->getStringValue("key", ""));
744 for (unsigned int i = 0; i < bindings.size(); i++) {
746 SGPropertyNode_ptr binding;
747 while (dest->getChild("binding", j))
750 const char *cmd = bindings[i]->getStringValue("command");
751 if (!strcmp(cmd, "nasal"))
752 bindings[i]->setStringValue("module", _module.c_str());
754 binding = dest->getChild("binding", j, true);
755 copyProperties(bindings[i], binding);
756 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
758 object->setCallback(action_callback);
761 string type = props->getName();
762 if (type == "input" && props->getBoolValue("live"))
763 object->setDownCallback(action_callback);
765 if (type == "text") {
766 const char *format = props->getStringValue("format", 0);
768 info->fmt_type = validate_format(format);
769 if (info->fmt_type != f_INVALID)
770 info->format = format;
772 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
779 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
780 int width, int height, bool makeFrame)
782 setupObject(group, props);
785 puFrame* f = new puFrame(0, 0, width, height);
789 int nChildren = props->nChildren();
790 for (int i = 0; i < nChildren; i++)
791 makeObject(props->getChild(i), width, height);
796 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
798 string type = props->getName();
801 if (type == "textbox" && props->getBoolValue("editable"))
804 FGColor c(_gui->getColor("background"));
805 c.merge(_gui->getColor(type));
806 c.merge(props->getNode("color"));
808 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
816 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
817 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
818 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
819 { LABEL, PUCOL_LABEL, "label", "color-label" },
820 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
821 { MISC, PUCOL_MISC, "misc", "color-misc" },
822 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
825 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
827 for (int i = 0; i < numcol; i++) {
832 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
833 if (which & pucol[i].mask)
834 dirty |= c.merge(props->getNode("color"));
836 if ((pucol[i].mask == LABEL) && !c.isValid())
837 dirty |= c.merge(_gui->getColor("label"));
839 dirty |= c.merge(props->getNode(pucol[i].cname));
841 if (c.isValid() && dirty)
842 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
874 {"left", PU_KEY_LEFT},
876 {"right", PU_KEY_RIGHT},
877 {"down", PU_KEY_DOWN},
878 {"pageup", PU_KEY_PAGE_UP},
879 {"pagedn", PU_KEY_PAGE_DOWN},
880 {"home", PU_KEY_HOME},
882 {"insert", PU_KEY_INSERT},
887 FGDialog::getKeyCode(const char *str)
898 char *buf = new char[strlen(str) + 1];
900 char *s = buf + strlen(buf);
901 while (s > str && s[-1] == ' ')
908 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
910 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
911 s += 6, mod |= SHIFT;
912 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
919 if (strlen(s) == 1 && isascii(*s)) {
924 key = toupper(key) - 64;
926 ; // Alt not propagated to the gui
928 for (char *t = s; *t; t++)
930 for (int i = 0; keymap[i].name; i++) {
931 if (!strcmp(s, keymap[i].name)) {
943 ////////////////////////////////////////////////////////////////////////
944 // Implementation of FGDialog::PropertyObject.
945 ////////////////////////////////////////////////////////////////////////
947 FGDialog::PropertyObject::PropertyObject (const char * n,
949 SGPropertyNode_ptr p)
959 ////////////////////////////////////////////////////////////////////////
960 // Implementation of fgValueList and derived pui widgets
961 ////////////////////////////////////////////////////////////////////////
964 fgValueList::fgValueList(SGPropertyNode *p) :
971 fgValueList::update()
977 fgValueList::~fgValueList()
983 fgValueList::make_list()
985 vector<SGPropertyNode_ptr> value_nodes = _props->getChildren("value");
986 _list = new char *[value_nodes.size() + 1];
988 for (i = 0; i < value_nodes.size(); i++)
989 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
994 fgValueList::destroy_list()
996 for (int i = 0; _list[i] != 0; i++)
1007 fgValueList::update();
1008 int top = getTopItem();
1013 // end of dialog.cxx