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)
152 if(object->getType() & PUCLASS_GROUP)
153 for (puObject *obj = ((puGroup *)object)->getFirstChild();
154 obj; obj = obj->getNextObject())
155 type |= getHitObjects(obj, x, y);
158 object->getAbsolutePosition(&cx, &cy);
159 object->getSize(&cw, &ch);
160 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
161 type |= object->getType();
167 ////////////////////////////////////////////////////////////////////////
169 ////////////////////////////////////////////////////////////////////////
175 action_callback (puObject * object)
177 GUIInfo * info = (GUIInfo *)object->getUserData();
178 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
179 gui->setActiveDialog(info->dialog);
180 int nBindings = info->bindings.size();
181 for (int i = 0; i < nBindings; i++) {
182 info->bindings[i]->fire();
183 if (gui->getActiveDialog() == 0)
186 gui->setActiveDialog(0);
191 format_callback(puObject *obj, int dx, int dy, void *n)
193 SGPropertyNode *node = (SGPropertyNode *)n;
194 const char *format = node->getStringValue("format"), *f = format;
195 bool number, l = false;
196 // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
207 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
209 while (*f && isdigit(*f))
213 while (*f && isdigit(*f))
221 else if (*f == 's') {
238 const char *src = obj->getLabel();
241 float value = atof(src);
242 snprintf(buf, 256, format, value);
244 snprintf(buf, 256, format, src);
249 SGPropertyNode *result = node->getNode("formatted", true);
250 result->setStringValue(buf);
251 obj->setLabel(result->getStringValue());
256 ////////////////////////////////////////////////////////////////////////
257 // Static helper functions.
258 ////////////////////////////////////////////////////////////////////////
261 * Copy a property value to a PUI object.
264 copy_to_pui (SGPropertyNode * node, puObject * object)
266 // Treat puText objects specially, so their "values" can be set
268 if(object->getType() & PUCLASS_TEXT) {
269 object->setLabel(node->getStringValue());
273 switch (node->getType()) {
274 case SGPropertyNode::BOOL:
275 case SGPropertyNode::INT:
276 case SGPropertyNode::LONG:
277 object->setValue(node->getIntValue());
279 case SGPropertyNode::FLOAT:
280 case SGPropertyNode::DOUBLE:
281 object->setValue(node->getFloatValue());
284 object->setValue(node->getStringValue());
291 copy_from_pui (puObject * object, SGPropertyNode * node)
293 // puText objects are immutable, so should not be copied out
294 if(object->getType() & PUCLASS_TEXT)
297 switch (node->getType()) {
298 case SGPropertyNode::BOOL:
299 case SGPropertyNode::INT:
300 case SGPropertyNode::LONG:
301 node->setIntValue(object->getIntegerValue());
303 case SGPropertyNode::FLOAT:
304 case SGPropertyNode::DOUBLE:
305 node->setFloatValue(object->getFloatValue());
308 // Special case to handle lists, as getStringValue cannot be overridden
309 if(object->getType() & PUCLASS_LIST)
311 const char *s = ((puList *) object)->getListStringValue();
313 node->setStringValue(s);
317 node->setStringValue(object->getStringValue());
325 ////////////////////////////////////////////////////////////////////////
326 // Implementation of FGDialog.
327 ////////////////////////////////////////////////////////////////////////
329 FGDialog::FGDialog (SGPropertyNode * props)
331 _gui((NewGUI *)globals->get_subsystem("gui")),
334 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
335 SGPropertyNode *nasal = props->getNode("nasal");
337 _nasal_close = nasal->getNode("close");
338 SGPropertyNode *open = nasal->getNode("open");
340 const char *s = open->getStringValue();
341 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
342 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
348 FGDialog::~FGDialog ()
351 _object->getAbsolutePosition(&x, &y);
352 _props->setIntValue("lastx", x);
353 _props->setIntValue("lasty", y);
355 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
357 const char *s = _nasal_close->getStringValue();
358 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
360 nas->deleteModule(_module.c_str());
362 puDeleteObject(_object);
365 // Delete all the info objects we
366 // were forced to keep around because
367 // PUI cannot delete its own user data.
368 for (i = 0; i < _info.size(); i++) {
369 delete (GUIInfo *)_info[i];
372 // Finally, delete the property links.
373 for (i = 0; i < _propertyObjects.size(); i++) {
374 delete _propertyObjects[i];
375 _propertyObjects[i] = 0;
380 FGDialog::updateValues (const char * objectName)
382 if (objectName && !objectName[0])
385 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
386 const string &name = _propertyObjects[i]->name;
387 if (objectName && name != objectName)
390 puObject *obj = _propertyObjects[i]->object;
391 if ((obj->getType() & PUCLASS_LIST) && (dynamic_cast<GUI_ID *>(obj)->id & FGCLASS_LIST)) {
392 fgList *pl = static_cast<fgList *>(obj);
395 copy_to_pui(_propertyObjects[i]->node, obj);
400 FGDialog::applyValues (const char * objectName)
402 if (objectName && !objectName[0])
405 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
406 const string &name = _propertyObjects[i]->name;
407 if (objectName && name != objectName)
410 copy_from_pui(_propertyObjects[i]->object,
411 _propertyObjects[i]->node);
418 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
419 puObject *obj = _liveObjects[i]->object;
420 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
423 copy_to_pui(_liveObjects[i]->node, obj);
428 FGDialog::display (SGPropertyNode * props)
431 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
435 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
436 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
438 bool userx = props->hasValue("x");
439 bool usery = props->hasValue("y");
440 bool userw = props->hasValue("width");
441 bool userh = props->hasValue("height");
443 // Let the layout widget work in the same property subtree.
444 LayoutWidget wid(props);
446 SGPropertyNode *fontnode = props->getNode("font");
448 FGFontCache *fc = globals->get_fontcache();
449 _font = fc->get(fontnode);
451 _font = _gui->getDefaultFont();
453 wid.setDefaultFont(_font, int(_font->getPointSize()));
456 int px, py, savex, savey;
458 wid.calcPrefSize(&pw, &ph);
459 pw = props->getIntValue("width", pw);
460 ph = props->getIntValue("height", ph);
461 px = savex = props->getIntValue("x", (screenw - pw) / 2);
462 py = savey = props->getIntValue("y", (screenh - ph) / 2);
464 // Negative x/y coordinates are interpreted as distance from the top/right
465 // corner rather than bottom/left.
467 px = screenw - pw + px;
469 py = screenh - ph + py;
471 // Define "x", "y", "width" and/or "height" in the property tree if they
472 // are not specified in the configuration file.
473 wid.layout(px, py, pw, ph);
475 // Use the dimension and location properties as specified in the
476 // configuration file or from the layout widget.
477 _object = makeObject(props, screenw, screenh);
479 // Remove automatically generated properties, so the layout looks
480 // the same next time around, or restore x and y to preserve negative coords.
482 props->setIntValue("x", savex);
484 props->removeChild("x");
487 props->setIntValue("y", savey);
489 props->removeChild("y");
491 if(!userw) props->removeChild("width");
492 if(!userh) props->removeChild("height");
497 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
498 << props->getStringValue("name", "[unnamed]")
499 << " does not contain a proper GUI definition");
504 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
506 if (props->getBoolValue("hide"))
509 bool presetSize = props->hasValue("width") && props->hasValue("height");
510 int width = props->getIntValue("width", parentWidth);
511 int height = props->getIntValue("height", parentHeight);
512 int x = props->getIntValue("x", (parentWidth - width) / 2);
513 int y = props->getIntValue("y", (parentHeight - height) / 2);
514 string type = props->getName();
519 if (type == "dialog") {
521 bool draggable = props->getBoolValue("draggable", true);
522 if (props->getBoolValue("modal", false))
523 obj = new puDialogBox(x, y);
525 obj = new fgPopup(x, y, draggable);
526 setupGroup(obj, props, width, height, true);
527 setColor(obj, props);
530 } else if (type == "group") {
531 puGroup * obj = new puGroup(x, y);
532 setupGroup(obj, props, width, height, false);
533 setColor(obj, props);
536 } else if (type == "frame") {
537 puGroup * obj = new puGroup(x, y);
538 setupGroup(obj, props, width, height, true);
539 setColor(obj, props);
542 } else if (type == "hrule" || type == "vrule") {
543 puFrame * obj = new puFrame(x, y, x + width, y + height);
544 obj->setBorderThickness(0);
545 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
548 } else if (type == "list") {
549 int slider_width = props->getIntValue("slider", 20);
550 fgList * obj = new fgList(x, y, x + width, y + height, props, slider_width);
552 obj->setSize(width, height);
553 setupObject(obj, props);
554 setColor(obj, props);
557 } else if (type == "airport-list") {
558 AirportList * obj = new AirportList(x, y, x + width, y + height);
560 obj->setSize(width, height);
561 setupObject(obj, props);
562 setColor(obj, props);
565 } else if (type == "property-list") {
566 PropertyList * obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
568 obj->setSize(width, height);
569 setupObject(obj, props);
570 setColor(obj, props);
573 } else if (type == "input") {
574 puInput * obj = new puInput(x, y, x + width, y + height);
575 setupObject(obj, props);
576 setColor(obj, props, FOREGROUND|LABEL);
579 } else if (type == "text") {
580 puText * obj = new puText(x, y);
581 setupObject(obj, props);
583 if (props->getNode("format")) {
584 SGPropertyNode *live = props->getNode("live");
585 if (live && live->getBoolValue())
586 obj->setRenderCallback(format_callback, props);
588 format_callback(obj, x, y, props);
590 // Layed-out objects need their size set, and non-layout ones
591 // get a different placement.
593 obj->setSize(width, height);
595 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
596 setColor(obj, props, LABEL);
599 } else if (type == "checkbox") {
601 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
602 setupObject(obj, props);
603 setColor(obj, props, FOREGROUND|LABEL);
606 } else if (type == "radio") {
608 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
609 setupObject(obj, props);
610 setColor(obj, props, FOREGROUND|LABEL);
613 } else if (type == "button") {
615 const char * legend = props->getStringValue("legend", "[none]");
616 if (props->getBoolValue("one-shot", true))
617 obj = new puOneShot(x, y, legend);
619 obj = new puButton(x, y, legend);
621 obj->setSize(width, height);
622 setupObject(obj, props);
623 setColor(obj, props);
626 } else if (type == "combo") {
627 fgComboBox * obj = new fgComboBox(x, y, x + width, y + height, props,
628 props->getBoolValue("editable", false));
629 setupObject(obj, props);
630 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
631 setColor(obj, props, EDITFIELD);
633 setColor(obj, props, FOREGROUND|LABEL);
637 } else if (type == "slider") {
638 bool vertical = props->getBoolValue("vertical", false);
639 puSlider * obj = new puSlider(x, y, (vertical ? height : width));
640 obj->setMinValue(props->getFloatValue("min", 0.0));
641 obj->setMaxValue(props->getFloatValue("max", 1.0));
642 setupObject(obj, props);
644 obj->setSize(width, height);
645 setColor(obj, props, FOREGROUND|LABEL);
648 } else if (type == "dial") {
649 puDial * obj = new puDial(x, y, width);
650 obj->setMinValue(props->getFloatValue("min", 0.0));
651 obj->setMaxValue(props->getFloatValue("max", 1.0));
652 obj->setWrap(props->getBoolValue("wrap", true));
653 setupObject(obj, props);
654 setColor(obj, props, FOREGROUND|LABEL);
657 } else if (type == "textbox") {
658 int slider_width = props->getIntValue("slider", 20);
659 int wrap = props->getBoolValue("wrap", true);
660 puaLargeInput * obj = new puaLargeInput(x, y,
661 x+width, x+height, 2, slider_width, wrap);
663 if (props->hasValue("editable")) {
664 if (props->getBoolValue("editable")==false)
670 obj->setSize(width, height);
671 setupObject(obj, props);
672 setColor(obj, props, FOREGROUND|LABEL);
675 } else if (type == "select") {
676 fgSelectBox * obj = new fgSelectBox(x, y, x + width, y + height, props);
677 setupObject(obj, props);
678 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
679 setColor(obj, props, EDITFIELD);
681 setColor(obj, props, FOREGROUND|LABEL);
690 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
692 string type = props->getName();
693 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
695 if (props->hasValue("legend"))
696 object->setLegend(props->getStringValue("legend"));
698 if (props->hasValue("label"))
699 object->setLabel(props->getStringValue("label"));
701 if (props->hasValue("border"))
702 object->setBorderThickness( props->getIntValue("border", 2) );
704 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
705 FGFontCache *fc = globals->get_fontcache();
706 puFont *lfnt = fc->get(nft);
707 object->setLabelFont(*lfnt);
709 object->setLabelFont(*_font);
712 if (props->hasValue("property")) {
713 const char * name = props->getStringValue("name");
716 const char * propname = props->getStringValue("property");
717 SGPropertyNode_ptr node = fgGetNode(propname, true);
718 copy_to_pui(node, object);
720 PropertyObject* po = new PropertyObject(name, object, node);
721 _propertyObjects.push_back(po);
722 if(props->getBoolValue("live"))
723 _liveObjects.push_back(po);
726 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
727 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
728 if (bindings.size() > 0) {
729 GUIInfo * info = new GUIInfo(this);
730 info->key = props->getIntValue("keynum", -1);
731 if (props->hasValue("key"))
732 info->key = getKeyCode(props->getStringValue("key", ""));
734 for (unsigned int i = 0; i < bindings.size(); i++) {
736 SGPropertyNode *binding;
737 while (dest->getChild("binding", j))
740 const char *cmd = bindings[i]->getStringValue("command");
741 if (!strcmp(cmd, "nasal"))
742 bindings[i]->setStringValue("module", _module.c_str());
744 binding = dest->getChild("binding", j, true);
745 copyProperties(bindings[i], binding);
746 info->bindings.push_back(new FGBinding(binding));
748 object->setCallback(action_callback);
750 if (type == "input" && props->getBoolValue("live"))
751 object->setDownCallback(action_callback);
753 object->setUserData(info);
754 _info.push_back(info);
757 object->makeReturnDefault(props->getBoolValue("default"));
761 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
762 int width, int height, bool makeFrame)
764 setupObject(group, props);
767 puFrame* f = new puFrame(0, 0, width, height);
771 int nChildren = props->nChildren();
772 for (int i = 0; i < nChildren; i++)
773 makeObject(props->getChild(i), width, height);
778 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
780 string type = props->getName();
783 if (type == "textbox" && props->getBoolValue("editable"))
786 FGColor *c = new FGColor(_gui->getColor("background"));
787 c->merge(_gui->getColor(type));
788 c->merge(props->getNode("color"));
790 object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
798 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
799 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
800 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
801 { LABEL, PUCOL_LABEL, "label", "color-label" },
802 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
803 { MISC, PUCOL_MISC, "misc", "color-misc" },
804 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
805 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
809 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
811 for (int i = 0; i < numcol; i++) {
816 dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
817 if (which & pucol[i].mask)
818 dirty |= c->merge(props->getNode("color"));
820 if ((pucol[i].mask == LABEL) && !c->isValid())
821 dirty |= c->merge(_gui->getColor("label"));
823 dirty |= c->merge(props->getNode(pucol[i].cname));
825 if (c->isValid() && dirty)
826 object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
858 {"left", PU_KEY_LEFT},
860 {"right", PU_KEY_RIGHT},
861 {"down", PU_KEY_DOWN},
862 {"pageup", PU_KEY_PAGE_UP},
863 {"pagedn", PU_KEY_PAGE_DOWN},
864 {"home", PU_KEY_HOME},
866 {"insert", PU_KEY_INSERT},
871 FGDialog::getKeyCode(const char *str)
882 char *buf = new char[strlen(str) + 1];
884 char *s = buf + strlen(buf);
885 while (s > str && s[-1] == ' ')
892 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
894 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
895 s += 6, mod |= SHIFT;
896 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
903 if (strlen(s) == 1 && isascii(*s)) {
908 key = toupper(key) - 64;
910 ; // Alt not propagated to the gui
912 for (char *t = s; *t; t++)
914 for (int i = 0; keymap[i].name; i++) {
915 if (!strcmp(s, keymap[i].name)) {
927 ////////////////////////////////////////////////////////////////////////
928 // Implementation of FGDialog::PropertyObject.
929 ////////////////////////////////////////////////////////////////////////
931 FGDialog::PropertyObject::PropertyObject (const char * n,
933 SGPropertyNode_ptr p)
943 ////////////////////////////////////////////////////////////////////////
944 // Implementation of fgValueList and derived pui widgets
945 ////////////////////////////////////////////////////////////////////////
948 fgValueList::fgValueList(SGPropertyNode *p) :
955 fgValueList::update()
961 fgValueList::~fgValueList()
967 fgValueList::make_list()
969 vector<SGPropertyNode_ptr> value_nodes = _props->getChildren("value");
970 _list = new char *[value_nodes.size() + 1];
972 for (i = 0; i < value_nodes.size(); i++)
973 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
978 fgValueList::destroy_list()
980 for (int i = 0; _list[i] != 0; i++)
991 fgValueList::update();