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));
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));
356 nas->deleteModule(_module.c_str());
358 puDeleteObject(_object);
362 // Delete all the arrays we made
363 // and were forced to keep around
364 // because PUI won't do its own
365 // memory management.
366 for (i = 0; i < _char_arrays.size(); i++) {
367 for (int j = 0; _char_arrays[i][j] != 0; j++)
368 free(_char_arrays[i][j]); // added with strdup
369 delete[] _char_arrays[i];
372 // Delete all the info objects we
373 // were forced to keep around because
374 // PUI cannot delete its own user data.
375 for (i = 0; i < _info.size(); i++) {
376 delete (GUIInfo *)_info[i];
380 // Finally, delete the property links.
381 for (i = 0; i < _propertyObjects.size(); i++) {
382 delete _propertyObjects[i];
383 _propertyObjects[i] = 0;
388 FGDialog::updateValue (const char * objectName)
390 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
391 const string &name = _propertyObjects[i]->name;
392 if (name == objectName)
393 copy_to_pui(_propertyObjects[i]->node,
394 _propertyObjects[i]->object);
399 FGDialog::applyValue (const char * objectName)
401 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
402 if (_propertyObjects[i]->name == objectName)
403 copy_from_pui(_propertyObjects[i]->object,
404 _propertyObjects[i]->node);
409 FGDialog::updateValues ()
411 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
412 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
416 FGDialog::applyValues ()
418 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
419 copy_from_pui(_propertyObjects[i]->object,
420 _propertyObjects[i]->node);
426 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
427 puObject *obj = _liveObjects[i]->object;
428 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
431 copy_to_pui(_liveObjects[i]->node, obj);
436 FGDialog::display (SGPropertyNode * props)
439 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
443 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
444 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
446 bool userx = props->hasValue("x");
447 bool usery = props->hasValue("y");
448 bool userw = props->hasValue("width");
449 bool userh = props->hasValue("height");
451 // Let the layout widget work in the same property subtree.
452 LayoutWidget wid(props);
454 SGPropertyNode *fontnode = props->getNode("font");
456 FGFontCache *fc = _gui->get_fontcache();
457 _font = fc->get(fontnode);
459 _font = _gui->getDefaultFont();
461 wid.setDefaultFont(_font, int(_font->getPointSize()));
464 int px, py, savex, savey;
466 wid.calcPrefSize(&pw, &ph);
467 pw = props->getIntValue("width", pw);
468 ph = props->getIntValue("height", ph);
469 px = savex = props->getIntValue("x", (screenw - pw) / 2);
470 py = savey = props->getIntValue("y", (screenh - ph) / 2);
472 // Negative x/y coordinates are interpreted as distance from the top/right
473 // corner rather than bottom/left.
475 px = screenw - pw + px;
477 py = screenh - ph + py;
479 // Define "x", "y", "width" and/or "height" in the property tree if they
480 // are not specified in the configuration file.
481 wid.layout(px, py, pw, ph);
483 // Use the dimension and location properties as specified in the
484 // configuration file or from the layout widget.
485 _object = makeObject(props, screenw, screenh);
487 // Remove automatically generated properties, so the layout looks
488 // the same next time around, or restore x and y to preserve negative coords.
490 props->setIntValue("x", savex);
492 props->removeChild("x");
495 props->setIntValue("y", savey);
497 props->removeChild("y");
499 if(!userw) props->removeChild("width");
500 if(!userh) props->removeChild("height");
505 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
506 << props->getStringValue("name", "[unnamed]")
507 << " does not contain a proper GUI definition");
512 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
514 if (props->getBoolValue("hide"))
517 bool presetSize = props->hasValue("width") && props->hasValue("height");
518 int width = props->getIntValue("width", parentWidth);
519 int height = props->getIntValue("height", parentHeight);
520 int x = props->getIntValue("x", (parentWidth - width) / 2);
521 int y = props->getIntValue("y", (parentHeight - height) / 2);
522 string type = props->getName();
527 if (type == "dialog") {
529 bool draggable = props->getBoolValue("draggable", true);
530 if (props->getBoolValue("modal", false))
531 obj = new puDialogBox(x, y);
533 obj = new fgPopup(x, y, draggable);
534 setupGroup(obj, props, width, height, true);
535 setColor(obj, props);
538 } else if (type == "group") {
539 puGroup * obj = new puGroup(x, y);
540 setupGroup(obj, props, width, height, false);
541 setColor(obj, props);
544 } else if (type == "frame") {
545 puGroup * obj = new puGroup(x, y);
546 setupGroup(obj, props, width, height, true);
547 setColor(obj, props);
550 } else if (type == "hrule" || type == "vrule") {
551 puFrame * obj = new puFrame(x, y, x + width, y + height);
552 obj->setBorderThickness(0);
553 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
556 } else if (type == "list") {
557 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
558 char ** entries = make_char_array(value_nodes.size());
559 for (unsigned int i = 0; i < value_nodes.size(); i++)
560 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
562 puList * obj = new puList(x, y, x + width, y + height, entries);
563 setupObject(obj, props);
564 setColor(obj, props);
567 } else if (type == "airport-list") {
568 AirportList * obj = new AirportList(x, y, x + width, y + 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.
592 if(presetSize) obj->setSize(width, height);
593 else obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
594 setColor(obj, props, LABEL);
597 } else if (type == "checkbox") {
599 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
600 setupObject(obj, props);
601 setColor(obj, props, FOREGROUND|LABEL);
604 } else if (type == "radio") {
606 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
607 setupObject(obj, props);
608 setColor(obj, props, FOREGROUND|LABEL);
611 } else if (type == "button") {
613 const char * legend = props->getStringValue("legend", "[none]");
614 if (props->getBoolValue("one-shot", true))
615 obj = new puOneShot(x, y, legend);
617 obj = new puButton(x, y, legend);
619 obj->setSize(width, height);
620 setupObject(obj, props);
621 setColor(obj, props);
624 } else if (type == "combo") {
625 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
626 char ** entries = make_char_array(value_nodes.size());
627 for (unsigned int i = 0; i < value_nodes.size(); i++)
628 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
630 puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
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", parentHeight);
662 int wrap = props->getBoolValue("wrap", true);
663 if (slider_width==0) slider_width=20;
664 puLargeInput * obj = new puLargeInput(x, y,
665 x+width, x+height, 2, slider_width, wrap);
667 if (props->hasValue("editable")) {
668 if (props->getBoolValue("editable")==false)
673 setupObject(obj, props);
674 setColor(obj, props, FOREGROUND|LABEL);
677 } else if (type == "select") {
678 vector<SGPropertyNode_ptr> value_nodes;
679 SGPropertyNode * selection_node =
680 fgGetNode(props->getChild("selection")->getStringValue(), true);
682 for (int q = 0; q < selection_node->nChildren(); q++)
683 value_nodes.push_back(selection_node->getChild(q));
685 char ** entries = make_char_array(value_nodes.size());
686 for (unsigned int i = 0; i < value_nodes.size(); i++)
687 entries[i] = strdup((char *)value_nodes[i]->getName());
690 new puSelectBox(x, y, x + width, y + height, entries);
691 setupObject(obj, props);
692 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
693 setColor(obj, props, EDITFIELD);
695 setColor(obj, props, FOREGROUND|LABEL);
704 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
706 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
708 if (props->hasValue("legend"))
709 object->setLegend(props->getStringValue("legend"));
711 if (props->hasValue("label"))
712 object->setLabel(props->getStringValue("label"));
714 if (props->hasValue("border"))
715 object->setBorderThickness( props->getIntValue("border", 2) );
717 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
718 FGFontCache *fc = _gui->get_fontcache();
719 puFont *lfnt = fc->get(nft);
720 object->setLabelFont(*lfnt);
722 object->setLabelFont(*_font);
725 if (props->hasValue("property")) {
726 const char * name = props->getStringValue("name");
729 const char * propname = props->getStringValue("property");
730 SGPropertyNode_ptr node = fgGetNode(propname, true);
731 copy_to_pui(node, object);
732 PropertyObject* po = new PropertyObject(name, object, node);
733 _propertyObjects.push_back(po);
734 if(props->getBoolValue("live"))
735 _liveObjects.push_back(po);
738 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
739 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
740 if (bindings.size() > 0) {
741 GUIInfo * info = new GUIInfo(this);
742 info->key = props->getIntValue("keynum", -1);
743 if (props->hasValue("key"))
744 info->key = getKeyCode(props->getStringValue("key", ""));
746 for (unsigned int i = 0; i < bindings.size(); i++) {
748 SGPropertyNode *binding;
749 while (dest->getChild("binding", j))
752 const char *cmd = bindings[i]->getStringValue("command");
753 if (!strcmp(cmd, "nasal"))
754 bindings[i]->setStringValue("module", _module.c_str());
756 binding = dest->getChild("binding", j, true);
757 copyProperties(bindings[i], binding);
758 info->bindings.push_back(new FGBinding(binding));
760 object->setCallback(action_callback);
761 object->setUserData(info);
762 _info.push_back(info);
765 object->makeReturnDefault(props->getBoolValue("default"));
769 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
770 int width, int height, bool makeFrame)
772 setupObject(group, props);
775 puFrame* f = new puFrame(0, 0, width, height);
779 int nChildren = props->nChildren();
780 for (int i = 0; i < nChildren; i++)
781 makeObject(props->getChild(i), width, height);
786 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
788 string type = props->getName();
791 if (type == "textbox" && props->getBoolValue("editable"))
794 FGColor *c = new FGColor(_gui->getColor("background"));
795 c->merge(_gui->getColor(type));
796 c->merge(props->getNode("color"));
798 object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
806 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
807 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
808 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
809 { LABEL, PUCOL_LABEL, "label", "color-label" },
810 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
811 { MISC, PUCOL_MISC, "misc", "color-misc" },
812 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
813 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
817 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
819 for (int i = 0; i < numcol; i++) {
824 dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
825 if (which & pucol[i].mask)
826 dirty |= c->merge(props->getNode("color"));
828 if ((pucol[i].mask == LABEL) && !c->isValid())
829 dirty |= c->merge(_gui->getColor("label"));
831 if (c->isValid() && dirty)
832 object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
864 {"left", PU_KEY_LEFT},
866 {"right", PU_KEY_RIGHT},
867 {"down", PU_KEY_DOWN},
868 {"pageup", PU_KEY_PAGE_UP},
869 {"pagedn", PU_KEY_PAGE_DOWN},
870 {"home", PU_KEY_HOME},
872 {"insert", PU_KEY_INSERT},
877 FGDialog::getKeyCode(const char *str)
888 char *buf = new char[strlen(str) + 1];
890 char *s = buf + strlen(buf);
891 while (s > str && s[-1] == ' ')
898 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
900 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
901 s += 6, mod |= SHIFT;
902 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
909 if (strlen(s) == 1 && isascii(*s)) {
914 key = toupper(key) - 64;
916 ; // Alt not propagated to the gui
918 for (char *t = s; *t; t++)
920 for (int i = 0; keymap[i].name; i++) {
921 if (!strcmp(s, keymap[i].name)) {
932 FGDialog::make_char_array (int size)
934 char ** list = new char*[size+1];
935 for (int i = 0; i <= size; i++)
937 _char_arrays.push_back(list);
943 ////////////////////////////////////////////////////////////////////////
944 // Implementation of FGDialog::PropertyObject.
945 ////////////////////////////////////////////////////////////////////////
947 FGDialog::PropertyObject::PropertyObject (const char * n,
949 SGPropertyNode_ptr p)