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"
18 ////////////////////////////////////////////////////////////////////////
19 // Implementation of GUIInfo.
20 ////////////////////////////////////////////////////////////////////////
23 * User data for a GUI object.
27 GUIInfo (FGDialog * d);
31 vector <FGBinding *> bindings;
35 GUIInfo::GUIInfo (FGDialog * d)
43 for (unsigned int i = 0; i < bindings.size(); i++) {
54 int fgPopup::checkKey(int key, int updown)
56 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
59 puObject *input = getActiveInputField(this);
61 return input->checkKey(key, updown);
63 puObject *object = getKeyObject(this, key);
65 return puPopup::checkKey(key, updown);
67 // invokeCallback() isn't enough; we need to simulate a mouse button press
68 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
69 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
70 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
71 object->checkHit(PU_LEFT_BUTTON, PU_UP,
72 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
73 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
77 puObject *fgPopup::getKeyObject(puObject *object, int key)
80 if(object->getType() & PUCLASS_GROUP)
81 for (puObject *obj = ((puGroup *)object)->getFirstChild();
82 obj; obj = obj->getNextObject())
83 if ((ret = getKeyObject(obj, key)))
86 GUIInfo *info = (GUIInfo *)object->getUserData();
87 if (info && info->key == key)
93 puObject *fgPopup::getActiveInputField(puObject *object)
96 if(object->getType() & PUCLASS_GROUP)
97 for (puObject *obj = ((puGroup *)object)->getFirstChild();
98 obj; obj = obj->getNextObject())
99 if ((ret = getActiveInputField(obj)))
102 if (object->getType() & PUCLASS_INPUT && ((puInput *)object)->isAcceptingInput())
111 int fgPopup::checkHit(int button, int updown, int x, int y)
113 int result = puPopup::checkHit(button, updown, x, y);
118 // This is annoying. We would really want a true result from the
119 // superclass to indicate "handled by child object", but all it
120 // tells us is that the pointer is inside the dialog. So do the
121 // intersection test (again) to make sure we don't start a drag
122 // when inside controls.
124 if(updown == PU_DOWN && !_dragging) {
128 int hit = getHitObjects(this, x, y);
129 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
133 getPosition(&px, &py);
137 } else if(updown == PU_DRAG && _dragging) {
138 setPosition(x + _dX, y + _dY);
145 int fgPopup::getHitObjects(puObject *object, int x, int y)
148 if(object->getType() & PUCLASS_GROUP)
149 for (puObject *obj = ((puGroup *)object)->getFirstChild();
150 obj; obj = obj->getNextObject())
151 type |= getHitObjects(obj, x, y);
154 object->getAbsolutePosition(&cx, &cy);
155 object->getSize(&cw, &ch);
156 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
157 type |= object->getType();
163 ////////////////////////////////////////////////////////////////////////
165 ////////////////////////////////////////////////////////////////////////
171 action_callback (puObject * object)
173 GUIInfo * info = (GUIInfo *)object->getUserData();
174 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
175 gui->setActiveDialog(info->dialog);
176 int nBindings = info->bindings.size();
177 for (int i = 0; i < nBindings; i++) {
178 info->bindings[i]->fire();
179 if (gui->getActiveDialog() == 0)
182 gui->setActiveDialog(0);
187 format_callback(puObject *obj, int dx, int dy, void *n)
189 SGPropertyNode *node = (SGPropertyNode *)n;
190 const char *format = node->getStringValue("format"), *f = format;
191 bool number, l = false;
192 // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
203 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
205 while (*f && isdigit(*f))
209 while (*f && isdigit(*f))
217 else if (*f == 's') {
234 const char *src = obj->getLabel();
237 float value = atof(src);
238 snprintf(buf, 256, format, value);
240 snprintf(buf, 256, format, src);
245 SGPropertyNode *result = node->getNode("formatted", true);
246 result->setStringValue(buf);
247 obj->setLabel(result->getStringValue());
252 ////////////////////////////////////////////////////////////////////////
253 // Static helper functions.
254 ////////////////////////////////////////////////////////////////////////
257 * Copy a property value to a PUI object.
260 copy_to_pui (SGPropertyNode * node, puObject * object)
262 // Treat puText objects specially, so their "values" can be set
264 if(object->getType() & PUCLASS_TEXT) {
265 object->setLabel(node->getStringValue());
269 switch (node->getType()) {
270 case SGPropertyNode::BOOL:
271 case SGPropertyNode::INT:
272 case SGPropertyNode::LONG:
273 object->setValue(node->getIntValue());
275 case SGPropertyNode::FLOAT:
276 case SGPropertyNode::DOUBLE:
277 object->setValue(node->getFloatValue());
280 object->setValue(node->getStringValue());
287 copy_from_pui (puObject * object, SGPropertyNode * node)
289 // puText objects are immutable, so should not be copied out
290 if(object->getType() & PUCLASS_TEXT)
293 switch (node->getType()) {
294 case SGPropertyNode::BOOL:
295 case SGPropertyNode::INT:
296 case SGPropertyNode::LONG:
297 node->setIntValue(object->getIntegerValue());
299 case SGPropertyNode::FLOAT:
300 case SGPropertyNode::DOUBLE:
301 node->setFloatValue(object->getFloatValue());
304 // Special case to handle lists, as getStringValue cannot be overridden
305 if(object->getType() & PUCLASS_LIST)
307 const char *s = ((puList *) object)->getListStringValue();
309 node->setStringValue(s);
313 node->setStringValue(object->getStringValue());
321 ////////////////////////////////////////////////////////////////////////
322 // Implementation of FGDialog.
323 ////////////////////////////////////////////////////////////////////////
325 FGDialog::FGDialog (SGPropertyNode * props)
327 _gui((NewGUI *)globals->get_subsystem("gui")),
330 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
331 SGPropertyNode *nasal = props->getNode("nasal");
333 _nasal_close = nasal->getNode("close");
334 SGPropertyNode *open = nasal->getNode("open");
336 const char *s = open->getStringValue();
337 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
338 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
344 FGDialog::~FGDialog ()
347 _object->getAbsolutePosition(&x, &y);
348 _props->setIntValue("lastx", x);
349 _props->setIntValue("lasty", y);
351 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
353 const char *s = _nasal_close->getStringValue();
354 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
356 nas->deleteModule(_module.c_str());
358 puDeleteObject(_object);
361 // Delete all the info objects we
362 // were forced to keep around because
363 // PUI cannot delete its own user data.
364 for (i = 0; i < _info.size(); i++) {
365 delete (GUIInfo *)_info[i];
368 // Finally, delete the property links.
369 for (i = 0; i < _propertyObjects.size(); i++) {
370 delete _propertyObjects[i];
371 _propertyObjects[i] = 0;
376 FGDialog::updateValues (const char * objectName)
378 if (objectName && !objectName[0])
381 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
382 const string &name = _propertyObjects[i]->name;
383 if (objectName && name != objectName)
386 puObject *obj = _propertyObjects[i]->object;
387 if (obj->getType() & PUCLASS_LIST) {
388 fgList *pl = static_cast<fgList *>(obj);
391 copy_to_pui(_propertyObjects[i]->node, obj);
396 FGDialog::applyValues (const char * objectName)
398 if (objectName && !objectName[0])
401 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
402 const string &name = _propertyObjects[i]->name;
403 if (objectName && name != objectName)
406 copy_from_pui(_propertyObjects[i]->object,
407 _propertyObjects[i]->node);
414 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
415 puObject *obj = _liveObjects[i]->object;
416 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
419 copy_to_pui(_liveObjects[i]->node, obj);
424 FGDialog::display (SGPropertyNode * props)
427 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
431 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
432 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
434 bool userx = props->hasValue("x");
435 bool usery = props->hasValue("y");
436 bool userw = props->hasValue("width");
437 bool userh = props->hasValue("height");
439 // Let the layout widget work in the same property subtree.
440 LayoutWidget wid(props);
442 SGPropertyNode *fontnode = props->getNode("font");
444 FGFontCache *fc = _gui->get_fontcache();
445 _font = fc->get(fontnode);
447 _font = _gui->getDefaultFont();
449 wid.setDefaultFont(_font, int(_font->getPointSize()));
452 int px, py, savex, savey;
454 wid.calcPrefSize(&pw, &ph);
455 pw = props->getIntValue("width", pw);
456 ph = props->getIntValue("height", ph);
457 px = savex = props->getIntValue("x", (screenw - pw) / 2);
458 py = savey = props->getIntValue("y", (screenh - ph) / 2);
460 // Negative x/y coordinates are interpreted as distance from the top/right
461 // corner rather than bottom/left.
463 px = screenw - pw + px;
465 py = screenh - ph + py;
467 // Define "x", "y", "width" and/or "height" in the property tree if they
468 // are not specified in the configuration file.
469 wid.layout(px, py, pw, ph);
471 // Use the dimension and location properties as specified in the
472 // configuration file or from the layout widget.
473 _object = makeObject(props, screenw, screenh);
475 // Remove automatically generated properties, so the layout looks
476 // the same next time around, or restore x and y to preserve negative coords.
478 props->setIntValue("x", savex);
480 props->removeChild("x");
483 props->setIntValue("y", savey);
485 props->removeChild("y");
487 if(!userw) props->removeChild("width");
488 if(!userh) props->removeChild("height");
493 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
494 << props->getStringValue("name", "[unnamed]")
495 << " does not contain a proper GUI definition");
500 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
502 if (props->getBoolValue("hide"))
505 bool presetSize = props->hasValue("width") && props->hasValue("height");
506 int width = props->getIntValue("width", parentWidth);
507 int height = props->getIntValue("height", parentHeight);
508 int x = props->getIntValue("x", (parentWidth - width) / 2);
509 int y = props->getIntValue("y", (parentHeight - height) / 2);
510 string type = props->getName();
515 if (type == "dialog") {
517 bool draggable = props->getBoolValue("draggable", true);
518 if (props->getBoolValue("modal", false))
519 obj = new puDialogBox(x, y);
521 obj = new fgPopup(x, y, draggable);
522 setupGroup(obj, props, width, height, true);
523 setColor(obj, props);
526 } else if (type == "group") {
527 puGroup * obj = new puGroup(x, y);
528 setupGroup(obj, props, width, height, false);
529 setColor(obj, props);
532 } else if (type == "frame") {
533 puGroup * obj = new puGroup(x, y);
534 setupGroup(obj, props, width, height, true);
535 setColor(obj, props);
538 } else if (type == "hrule" || type == "vrule") {
539 puFrame * obj = new puFrame(x, y, x + width, y + height);
540 obj->setBorderThickness(0);
541 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
544 } else if (type == "list") {
545 int slider_width = props->getIntValue("slider", 20);
546 fgList * obj = new fgList(x, y, x + width, y + height, props, slider_width);
548 obj->setSize(width, height);
549 setupObject(obj, props);
550 setColor(obj, props);
553 } else if (type == "airport-list") {
554 AirportList * obj = new AirportList(x, y, x + width, y + height);
556 obj->setSize(width, height);
557 setupObject(obj, props);
558 setColor(obj, props);
561 } else if (type == "input") {
562 puInput * obj = new puInput(x, y, x + width, y + height);
563 setupObject(obj, props);
564 setColor(obj, props, FOREGROUND|LABEL);
567 } else if (type == "text") {
568 puText * obj = new puText(x, y);
569 setupObject(obj, props);
571 if (props->getNode("format")) {
572 SGPropertyNode *live = props->getNode("live");
573 if (live && live->getBoolValue())
574 obj->setRenderCallback(format_callback, props);
576 format_callback(obj, x, y, props);
578 // Layed-out objects need their size set, and non-layout ones
579 // get a different placement.
581 obj->setSize(width, height);
583 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
584 setColor(obj, props, LABEL);
587 } else if (type == "checkbox") {
589 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
590 setupObject(obj, props);
591 setColor(obj, props, FOREGROUND|LABEL);
594 } else if (type == "radio") {
596 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
597 setupObject(obj, props);
598 setColor(obj, props, FOREGROUND|LABEL);
601 } else if (type == "button") {
603 const char * legend = props->getStringValue("legend", "[none]");
604 if (props->getBoolValue("one-shot", true))
605 obj = new puOneShot(x, y, legend);
607 obj = new puButton(x, y, legend);
609 obj->setSize(width, height);
610 setupObject(obj, props);
611 setColor(obj, props);
614 } else if (type == "combo") {
615 fgComboBox * obj = new fgComboBox(x, y, x + width, y + height, props,
616 props->getBoolValue("editable", false));
617 setupObject(obj, props);
618 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
619 setColor(obj, props, EDITFIELD);
621 setColor(obj, props, FOREGROUND|LABEL);
625 } else if (type == "slider") {
626 bool vertical = props->getBoolValue("vertical", false);
627 puSlider * obj = new puSlider(x, y, (vertical ? height : width));
628 obj->setMinValue(props->getFloatValue("min", 0.0));
629 obj->setMaxValue(props->getFloatValue("max", 1.0));
630 setupObject(obj, props);
632 obj->setSize(width, height);
633 setColor(obj, props, FOREGROUND|LABEL);
636 } else if (type == "dial") {
637 puDial * obj = new puDial(x, y, width);
638 obj->setMinValue(props->getFloatValue("min", 0.0));
639 obj->setMaxValue(props->getFloatValue("max", 1.0));
640 obj->setWrap(props->getBoolValue("wrap", true));
641 setupObject(obj, props);
642 setColor(obj, props, FOREGROUND|LABEL);
645 } else if (type == "textbox") {
646 int slider_width = props->getIntValue("slider", 20);
647 int wrap = props->getBoolValue("wrap", true);
648 puaLargeInput * obj = new puaLargeInput(x, y,
649 x+width, x+height, 2, slider_width, wrap);
651 if (props->hasValue("editable")) {
652 if (props->getBoolValue("editable")==false)
658 obj->setSize(width, height);
659 setupObject(obj, props);
660 setColor(obj, props, FOREGROUND|LABEL);
663 } else if (type == "select") {
664 fgSelectBox * obj = new fgSelectBox(x, y, x + width, y + height, props);
665 setupObject(obj, props);
666 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
667 setColor(obj, props, EDITFIELD);
669 setColor(obj, props, FOREGROUND|LABEL);
678 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
680 string type = props->getName();
681 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
683 if (props->hasValue("legend"))
684 object->setLegend(props->getStringValue("legend"));
686 if (props->hasValue("label"))
687 object->setLabel(props->getStringValue("label"));
689 if (props->hasValue("border"))
690 object->setBorderThickness( props->getIntValue("border", 2) );
692 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
693 FGFontCache *fc = _gui->get_fontcache();
694 puFont *lfnt = fc->get(nft);
695 object->setLabelFont(*lfnt);
697 object->setLabelFont(*_font);
700 if (props->hasValue("property")) {
701 const char * name = props->getStringValue("name");
704 const char * propname = props->getStringValue("property");
705 SGPropertyNode_ptr node = fgGetNode(propname, true);
706 copy_to_pui(node, object);
708 PropertyObject* po = new PropertyObject(name, object, node);
709 _propertyObjects.push_back(po);
710 if(props->getBoolValue("live"))
711 _liveObjects.push_back(po);
714 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
715 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
716 if (bindings.size() > 0) {
717 GUIInfo * info = new GUIInfo(this);
718 info->key = props->getIntValue("keynum", -1);
719 if (props->hasValue("key"))
720 info->key = getKeyCode(props->getStringValue("key", ""));
722 for (unsigned int i = 0; i < bindings.size(); i++) {
724 SGPropertyNode *binding;
725 while (dest->getChild("binding", j))
728 const char *cmd = bindings[i]->getStringValue("command");
729 if (!strcmp(cmd, "nasal"))
730 bindings[i]->setStringValue("module", _module.c_str());
732 binding = dest->getChild("binding", j, true);
733 copyProperties(bindings[i], binding);
734 info->bindings.push_back(new FGBinding(binding));
736 object->setCallback(action_callback);
738 if (type == "input" && props->getBoolValue("live"))
739 object->setDownCallback(action_callback);
741 object->setUserData(info);
742 _info.push_back(info);
745 object->makeReturnDefault(props->getBoolValue("default"));
749 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
750 int width, int height, bool makeFrame)
752 setupObject(group, props);
755 puFrame* f = new puFrame(0, 0, width, height);
759 int nChildren = props->nChildren();
760 for (int i = 0; i < nChildren; i++)
761 makeObject(props->getChild(i), width, height);
766 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
768 string type = props->getName();
771 if (type == "textbox" && props->getBoolValue("editable"))
774 FGColor *c = new FGColor(_gui->getColor("background"));
775 c->merge(_gui->getColor(type));
776 c->merge(props->getNode("color"));
778 object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
786 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
787 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
788 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
789 { LABEL, PUCOL_LABEL, "label", "color-label" },
790 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
791 { MISC, PUCOL_MISC, "misc", "color-misc" },
792 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
793 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
797 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
799 for (int i = 0; i < numcol; i++) {
804 dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
805 if (which & pucol[i].mask)
806 dirty |= c->merge(props->getNode("color"));
808 if ((pucol[i].mask == LABEL) && !c->isValid())
809 dirty |= c->merge(_gui->getColor("label"));
811 dirty |= c->merge(props->getNode(pucol[i].cname));
813 if (c->isValid() && dirty)
814 object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
846 {"left", PU_KEY_LEFT},
848 {"right", PU_KEY_RIGHT},
849 {"down", PU_KEY_DOWN},
850 {"pageup", PU_KEY_PAGE_UP},
851 {"pagedn", PU_KEY_PAGE_DOWN},
852 {"home", PU_KEY_HOME},
854 {"insert", PU_KEY_INSERT},
859 FGDialog::getKeyCode(const char *str)
870 char *buf = new char[strlen(str) + 1];
872 char *s = buf + strlen(buf);
873 while (s > str && s[-1] == ' ')
880 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
882 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
883 s += 6, mod |= SHIFT;
884 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
891 if (strlen(s) == 1 && isascii(*s)) {
896 key = toupper(key) - 64;
898 ; // Alt not propagated to the gui
900 for (char *t = s; *t; t++)
902 for (int i = 0; keymap[i].name; i++) {
903 if (!strcmp(s, keymap[i].name)) {
915 ////////////////////////////////////////////////////////////////////////
916 // Implementation of FGDialog::PropertyObject.
917 ////////////////////////////////////////////////////////////////////////
919 FGDialog::PropertyObject::PropertyObject (const char * n,
921 SGPropertyNode_ptr p)
931 ////////////////////////////////////////////////////////////////////////
932 // Implementation of fgList and derived pui widgets
933 ////////////////////////////////////////////////////////////////////////
936 fgValueList::fgValueList(SGPropertyNode *p) :
943 fgValueList::update()
949 fgValueList::~fgValueList()
955 fgValueList::make_list()
957 vector<SGPropertyNode_ptr> value_nodes = _props->getChildren("value");
958 _list = new char *[value_nodes.size() + 1];
960 for (i = 0; i < value_nodes.size(); i++)
961 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
966 fgValueList::destroy_list()
968 for (int i = 0; _list[i] != 0; i++)
979 fgValueList::update();