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"
16 #include "AirportList.hxx"
19 ////////////////////////////////////////////////////////////////////////
20 // Implementation of GUIInfo.
21 ////////////////////////////////////////////////////////////////////////
24 * User data for a GUI object.
28 GUIInfo (FGDialog * d);
32 vector <FGBinding *> bindings;
36 GUIInfo::GUIInfo (FGDialog * d)
44 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 arrays we made
362 // and were forced to keep around
363 // because PUI won't do its own
364 // memory management.
365 for (i = 0; i < _char_arrays.size(); i++)
366 destroy_char_array(_char_arrays[i]);
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 copy_to_pui(_propertyObjects[i]->node, obj);
399 FGDialog::applyValues (const char * objectName)
401 if (objectName && !objectName[0])
404 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
405 const string &name = _propertyObjects[i]->name;
406 if (objectName && name != objectName)
409 copy_from_pui(_propertyObjects[i]->object,
410 _propertyObjects[i]->node);
417 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
418 puObject *obj = _liveObjects[i]->object;
419 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
422 copy_to_pui(_liveObjects[i]->node, obj);
427 FGDialog::display (SGPropertyNode * props)
430 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
434 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
435 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
437 bool userx = props->hasValue("x");
438 bool usery = props->hasValue("y");
439 bool userw = props->hasValue("width");
440 bool userh = props->hasValue("height");
442 // Let the layout widget work in the same property subtree.
443 LayoutWidget wid(props);
445 SGPropertyNode *fontnode = props->getNode("font");
447 FGFontCache *fc = _gui->get_fontcache();
448 _font = fc->get(fontnode);
450 _font = _gui->getDefaultFont();
452 wid.setDefaultFont(_font, int(_font->getPointSize()));
455 int px, py, savex, savey;
457 wid.calcPrefSize(&pw, &ph);
458 pw = props->getIntValue("width", pw);
459 ph = props->getIntValue("height", ph);
460 px = savex = props->getIntValue("x", (screenw - pw) / 2);
461 py = savey = props->getIntValue("y", (screenh - ph) / 2);
463 // Negative x/y coordinates are interpreted as distance from the top/right
464 // corner rather than bottom/left.
466 px = screenw - pw + px;
468 py = screenh - ph + py;
470 // Define "x", "y", "width" and/or "height" in the property tree if they
471 // are not specified in the configuration file.
472 wid.layout(px, py, pw, ph);
474 // Use the dimension and location properties as specified in the
475 // configuration file or from the layout widget.
476 _object = makeObject(props, screenw, screenh);
478 // Remove automatically generated properties, so the layout looks
479 // the same next time around, or restore x and y to preserve negative coords.
481 props->setIntValue("x", savex);
483 props->removeChild("x");
486 props->setIntValue("y", savey);
488 props->removeChild("y");
490 if(!userw) props->removeChild("width");
491 if(!userh) props->removeChild("height");
496 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
497 << props->getStringValue("name", "[unnamed]")
498 << " does not contain a proper GUI definition");
503 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
505 if (props->getBoolValue("hide"))
508 bool presetSize = props->hasValue("width") && props->hasValue("height");
509 int width = props->getIntValue("width", parentWidth);
510 int height = props->getIntValue("height", parentHeight);
511 int x = props->getIntValue("x", (parentWidth - width) / 2);
512 int y = props->getIntValue("y", (parentHeight - height) / 2);
513 string type = props->getName();
518 if (type == "dialog") {
520 bool draggable = props->getBoolValue("draggable", true);
521 if (props->getBoolValue("modal", false))
522 obj = new puDialogBox(x, y);
524 obj = new fgPopup(x, y, draggable);
525 setupGroup(obj, props, width, height, true);
526 setColor(obj, props);
529 } else if (type == "group") {
530 puGroup * obj = new puGroup(x, y);
531 setupGroup(obj, props, width, height, false);
532 setColor(obj, props);
535 } else if (type == "frame") {
536 puGroup * obj = new puGroup(x, y);
537 setupGroup(obj, props, width, height, true);
538 setColor(obj, props);
541 } else if (type == "hrule" || type == "vrule") {
542 puFrame * obj = new puFrame(x, y, x + width, y + height);
543 obj->setBorderThickness(0);
544 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
547 } else if (type == "list") {
548 char ** entries = value_list(props);
549 int slider_width = props->getIntValue("slider", 20);
550 puList * obj = new puList(x, y, x + width, y + height, entries, 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 == "input") {
566 puInput * obj = new puInput(x, y, x + width, y + height);
567 setupObject(obj, props);
568 setColor(obj, props, FOREGROUND|LABEL);
571 } else if (type == "text") {
572 puText * obj = new puText(x, y);
573 setupObject(obj, props);
575 if (props->getNode("format")) {
576 SGPropertyNode *live = props->getNode("live");
577 if (live && live->getBoolValue())
578 obj->setRenderCallback(format_callback, props);
580 format_callback(obj, x, y, props);
582 // Layed-out objects need their size set, and non-layout ones
583 // get a different placement.
585 obj->setSize(width, height);
587 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
588 setColor(obj, props, LABEL);
591 } else if (type == "checkbox") {
593 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
594 setupObject(obj, props);
595 setColor(obj, props, FOREGROUND|LABEL);
598 } else if (type == "radio") {
600 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
601 setupObject(obj, props);
602 setColor(obj, props, FOREGROUND|LABEL);
605 } else if (type == "button") {
607 const char * legend = props->getStringValue("legend", "[none]");
608 if (props->getBoolValue("one-shot", true))
609 obj = new puOneShot(x, y, legend);
611 obj = new puButton(x, y, legend);
613 obj->setSize(width, height);
614 setupObject(obj, props);
615 setColor(obj, props);
618 } else if (type == "combo") {
619 char ** entries = value_list(props);
620 puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
621 props->getBoolValue("editable", false));
622 setupObject(obj, props);
623 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
624 setColor(obj, props, EDITFIELD);
626 setColor(obj, props, FOREGROUND|LABEL);
630 } else if (type == "slider") {
631 bool vertical = props->getBoolValue("vertical", false);
632 puSlider * obj = new puSlider(x, y, (vertical ? height : width));
633 obj->setMinValue(props->getFloatValue("min", 0.0));
634 obj->setMaxValue(props->getFloatValue("max", 1.0));
635 setupObject(obj, props);
637 obj->setSize(width, height);
638 setColor(obj, props, FOREGROUND|LABEL);
641 } else if (type == "dial") {
642 puDial * obj = new puDial(x, y, width);
643 obj->setMinValue(props->getFloatValue("min", 0.0));
644 obj->setMaxValue(props->getFloatValue("max", 1.0));
645 obj->setWrap(props->getBoolValue("wrap", true));
646 setupObject(obj, props);
647 setColor(obj, props, FOREGROUND|LABEL);
650 } else if (type == "textbox") {
651 int slider_width = props->getIntValue("slider", 20);
652 int wrap = props->getBoolValue("wrap", true);
653 puLargeInput * obj = new puLargeInput(x, y,
654 x+width, x+height, 2, slider_width, wrap);
656 if (props->hasValue("editable")) {
657 if (props->getBoolValue("editable")==false)
663 obj->setSize(width, height);
664 setupObject(obj, props);
665 setColor(obj, props, FOREGROUND|LABEL);
668 } else if (type == "select") {
669 vector<SGPropertyNode_ptr> value_nodes;
670 SGPropertyNode * selection_node =
671 fgGetNode(props->getChild("selection")->getStringValue(), true);
673 for (int q = 0; q < selection_node->nChildren(); q++)
674 value_nodes.push_back(selection_node->getChild(q));
676 char ** entries = make_char_array(value_nodes.size());
677 for (unsigned int i = 0; i < value_nodes.size(); i++)
678 entries[i] = strdup((char *)value_nodes[i]->getName());
681 new puSelectBox(x, y, x + width, y + height, entries);
682 setupObject(obj, props);
683 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
684 setColor(obj, props, EDITFIELD);
686 setColor(obj, props, FOREGROUND|LABEL);
695 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
697 string type = props->getName();
698 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
700 if (props->hasValue("legend"))
701 object->setLegend(props->getStringValue("legend"));
703 if (props->hasValue("label"))
704 object->setLabel(props->getStringValue("label"));
706 if (props->hasValue("border"))
707 object->setBorderThickness( props->getIntValue("border", 2) );
709 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
710 FGFontCache *fc = _gui->get_fontcache();
711 puFont *lfnt = fc->get(nft);
712 object->setLabelFont(*lfnt);
714 object->setLabelFont(*_font);
717 if (props->hasValue("property")) {
718 const char * name = props->getStringValue("name");
721 const char * propname = props->getStringValue("property");
722 SGPropertyNode_ptr node = fgGetNode(propname, true);
723 copy_to_pui(node, object);
724 PropertyObject* po = new PropertyObject(name, object, node);
725 _propertyObjects.push_back(po);
726 if(props->getBoolValue("live"))
727 _liveObjects.push_back(po);
730 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
731 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
732 if (bindings.size() > 0) {
733 GUIInfo * info = new GUIInfo(this);
734 info->key = props->getIntValue("keynum", -1);
735 if (props->hasValue("key"))
736 info->key = getKeyCode(props->getStringValue("key", ""));
738 for (unsigned int i = 0; i < bindings.size(); i++) {
740 SGPropertyNode *binding;
741 while (dest->getChild("binding", j))
744 const char *cmd = bindings[i]->getStringValue("command");
745 if (!strcmp(cmd, "nasal"))
746 bindings[i]->setStringValue("module", _module.c_str());
748 binding = dest->getChild("binding", j, true);
749 copyProperties(bindings[i], binding);
750 info->bindings.push_back(new FGBinding(binding));
752 object->setCallback(action_callback);
754 if (type == "input" && props->getBoolValue("live"))
755 object->setDownCallback(action_callback);
757 object->setUserData(info);
758 _info.push_back(info);
761 object->makeReturnDefault(props->getBoolValue("default"));
765 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
766 int width, int height, bool makeFrame)
768 setupObject(group, props);
771 puFrame* f = new puFrame(0, 0, width, height);
775 int nChildren = props->nChildren();
776 for (int i = 0; i < nChildren; i++)
777 makeObject(props->getChild(i), width, height);
782 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
784 string type = props->getName();
787 if (type == "textbox" && props->getBoolValue("editable"))
790 FGColor *c = new FGColor(_gui->getColor("background"));
791 c->merge(_gui->getColor(type));
792 c->merge(props->getNode("color"));
794 object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
802 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
803 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
804 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
805 { LABEL, PUCOL_LABEL, "label", "color-label" },
806 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
807 { MISC, PUCOL_MISC, "misc", "color-misc" },
808 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
809 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
813 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
815 for (int i = 0; i < numcol; i++) {
820 dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
821 if (which & pucol[i].mask)
822 dirty |= c->merge(props->getNode("color"));
824 if ((pucol[i].mask == LABEL) && !c->isValid())
825 dirty |= c->merge(_gui->getColor("label"));
827 dirty |= c->merge(props->getNode(pucol[i].cname));
829 if (c->isValid() && dirty)
830 object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
862 {"left", PU_KEY_LEFT},
864 {"right", PU_KEY_RIGHT},
865 {"down", PU_KEY_DOWN},
866 {"pageup", PU_KEY_PAGE_UP},
867 {"pagedn", PU_KEY_PAGE_DOWN},
868 {"home", PU_KEY_HOME},
870 {"insert", PU_KEY_INSERT},
875 FGDialog::getKeyCode(const char *str)
886 char *buf = new char[strlen(str) + 1];
888 char *s = buf + strlen(buf);
889 while (s > str && s[-1] == ' ')
896 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
898 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
899 s += 6, mod |= SHIFT;
900 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
907 if (strlen(s) == 1 && isascii(*s)) {
912 key = toupper(key) - 64;
914 ; // Alt not propagated to the gui
916 for (char *t = s; *t; t++)
918 for (int i = 0; keymap[i].name; i++) {
919 if (!strcmp(s, keymap[i].name)) {
930 FGDialog::value_list (const SGPropertyNode *props)
932 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
933 char ** entries = make_char_array(value_nodes.size());
934 for (unsigned int i = 0; i < value_nodes.size(); i++)
935 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
940 FGDialog::make_char_array (int size)
942 char ** list = new char*[size+1];
943 for (int i = 0; i <= size; i++)
945 _char_arrays.push_back(list);
950 FGDialog::destroy_char_array (char ** array)
952 for (int i = 0; array[i] != 0; i++)
954 free(array[i]);// added with strdup
959 ////////////////////////////////////////////////////////////////////////
960 // Implementation of FGDialog::PropertyObject.
961 ////////////////////////////////////////////////////////////////////////
963 FGDialog::PropertyObject::PropertyObject (const char * n,
965 SGPropertyNode_ptr p)