1 // dialog.cxx: implementation of an XML-configurable dialog box.
3 #include <stdlib.h> // atof()
5 #include <Input/input.hxx>
11 #include "AirportList.hxx"
14 ////////////////////////////////////////////////////////////////////////
15 // Implementation of GUIInfo.
16 ////////////////////////////////////////////////////////////////////////
19 * User data for a GUI object.
23 GUIInfo (FGDialog * d);
27 vector <FGBinding *> bindings;
31 GUIInfo::GUIInfo (FGDialog * d)
39 for (unsigned int i = 0; i < bindings.size(); i++) {
49 int fgPopup::checkKey(int key, int updown)
51 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
54 puObject *input = getActiveInputField(this);
56 return input->checkKey(key, updown);
58 puObject *object = getKeyObject(this, key);
60 return puPopup::checkKey(key, updown);
62 // invokeCallback() isn't enough; we need to simulate a mouse button press
63 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
64 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
65 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
66 object->checkHit(PU_LEFT_BUTTON, PU_UP,
67 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
68 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
72 puObject *fgPopup::getKeyObject(puObject *object, int key)
75 if(object->getType() & PUCLASS_GROUP)
76 for (puObject *obj = ((puGroup *)object)->getFirstChild();
77 obj; obj = obj->getNextObject())
78 if ((ret = getKeyObject(obj, key)))
81 GUIInfo *info = (GUIInfo *)object->getUserData();
82 if (info && info->key == key)
88 puObject *fgPopup::getActiveInputField(puObject *object)
91 if(object->getType() & PUCLASS_GROUP)
92 for (puObject *obj = ((puGroup *)object)->getFirstChild();
93 obj; obj = obj->getNextObject())
94 if ((ret = getActiveInputField(obj)))
97 if (object->getType() & PUCLASS_INPUT && ((puInput *)object)->isAcceptingInput())
106 int fgPopup::checkHit(int button, int updown, int x, int y)
108 int result = puPopup::checkHit(button, updown, x, y);
113 // This is annoying. We would really want a true result from the
114 // superclass to indicate "handled by child object", but all it
115 // tells us is that the pointer is inside the dialog. So do the
116 // intersection test (again) to make sure we don't start a drag
117 // when inside controls.
119 if(updown == PU_DOWN && !_dragging) {
123 int hit = getHitObjects(this, x, y);
124 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
128 getPosition(&px, &py);
132 } else if(updown == PU_DRAG && _dragging) {
133 setPosition(x + _dX, y + _dY);
140 int fgPopup::getHitObjects(puObject *object, int x, int y)
143 if(object->getType() & PUCLASS_GROUP)
144 for (puObject *obj = ((puGroup *)object)->getFirstChild();
145 obj; obj = obj->getNextObject())
146 type |= getHitObjects(obj, x, y);
149 object->getAbsolutePosition(&cx, &cy);
150 object->getSize(&cw, &ch);
151 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
152 type |= object->getType();
158 ////////////////////////////////////////////////////////////////////////
160 ////////////////////////////////////////////////////////////////////////
166 action_callback (puObject * object)
168 GUIInfo * info = (GUIInfo *)object->getUserData();
169 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
170 gui->setActiveDialog(info->dialog);
171 int nBindings = info->bindings.size();
172 for (int i = 0; i < nBindings; i++) {
173 info->bindings[i]->fire();
174 if (gui->getActiveDialog() == 0)
177 gui->setActiveDialog(0);
182 format_callback(puObject *obj, int dx, int dy, void *n)
184 SGPropertyNode *node = (SGPropertyNode *)n;
185 const char *format = node->getStringValue("format"), *f = format;
186 bool number, l = false;
187 // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
198 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
200 while (*f && isdigit(*f))
204 while (*f && isdigit(*f))
212 else if (*f == 's') {
229 const char *src = obj->getLabel();
232 float value = atof(src);
233 snprintf(buf, 256, format, value);
235 snprintf(buf, 256, format, src);
240 SGPropertyNode *result = node->getNode("formatted", true);
241 result->setStringValue(buf);
242 obj->setLabel(result->getStringValue());
247 ////////////////////////////////////////////////////////////////////////
248 // Static helper functions.
249 ////////////////////////////////////////////////////////////////////////
252 * Copy a property value to a PUI object.
255 copy_to_pui (SGPropertyNode * node, puObject * object)
257 // Treat puText objects specially, so their "values" can be set
259 if(object->getType() & PUCLASS_TEXT) {
260 object->setLabel(node->getStringValue());
264 switch (node->getType()) {
265 case SGPropertyNode::BOOL:
266 case SGPropertyNode::INT:
267 case SGPropertyNode::LONG:
268 object->setValue(node->getIntValue());
270 case SGPropertyNode::FLOAT:
271 case SGPropertyNode::DOUBLE:
272 object->setValue(node->getFloatValue());
275 object->setValue(node->getStringValue());
282 copy_from_pui (puObject * object, SGPropertyNode * node)
284 // puText objects are immutable, so should not be copied out
285 if(object->getType() & PUCLASS_TEXT)
288 switch (node->getType()) {
289 case SGPropertyNode::BOOL:
290 case SGPropertyNode::INT:
291 case SGPropertyNode::LONG:
292 node->setIntValue(object->getIntegerValue());
294 case SGPropertyNode::FLOAT:
295 case SGPropertyNode::DOUBLE:
296 node->setFloatValue(object->getFloatValue());
299 // Special case to handle lists, as getStringValue cannot be overridden
300 if(object->getType() & PUCLASS_LIST)
302 node->setStringValue(((puList *) object)->getListStringValue());
306 node->setStringValue(object->getStringValue());
314 ////////////////////////////////////////////////////////////////////////
315 // Implementation of FGDialog.
316 ////////////////////////////////////////////////////////////////////////
318 FGDialog::FGDialog (SGPropertyNode * props)
320 _gui((NewGUI *)globals->get_subsystem("gui"))
325 FGDialog::~FGDialog ()
327 puDeleteObject(_object);
331 // Delete all the arrays we made
332 // and were forced to keep around
333 // because PUI won't do its own
334 // memory management.
335 for (i = 0; i < _char_arrays.size(); i++) {
336 for (int j = 0; _char_arrays[i][j] != 0; j++)
337 free(_char_arrays[i][j]); // added with strdup
338 delete[] _char_arrays[i];
341 // Delete all the info objects we
342 // were forced to keep around because
343 // PUI cannot delete its own user data.
344 for (i = 0; i < _info.size(); i++) {
345 delete (GUIInfo *)_info[i];
349 // Finally, delete the property links.
350 for (i = 0; i < _propertyObjects.size(); i++) {
351 delete _propertyObjects[i];
352 _propertyObjects[i] = 0;
357 FGDialog::updateValue (const char * objectName)
359 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
360 const string &name = _propertyObjects[i]->name;
361 if (name == objectName)
362 copy_to_pui(_propertyObjects[i]->node,
363 _propertyObjects[i]->object);
368 FGDialog::applyValue (const char * objectName)
370 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
371 if (_propertyObjects[i]->name == objectName)
372 copy_from_pui(_propertyObjects[i]->object,
373 _propertyObjects[i]->node);
378 FGDialog::updateValues ()
380 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
381 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
385 FGDialog::applyValues ()
387 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
388 copy_from_pui(_propertyObjects[i]->object,
389 _propertyObjects[i]->node);
395 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
396 puObject *obj = _liveObjects[i]->object;
397 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
400 copy_to_pui(_liveObjects[i]->node, obj);
405 FGDialog::display (SGPropertyNode * props)
408 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
412 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
413 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
415 bool userx = props->hasValue("x");
416 bool usery = props->hasValue("y");
417 bool userw = props->hasValue("width");
418 bool userh = props->hasValue("height");
420 // Let the layout widget work in the same property subtree.
421 LayoutWidget wid(props);
423 SGPropertyNode *fontnode = props->getNode("font");
425 FGFontCache *fc = _gui->get_fontcache();
426 _font = fc->get(fontnode);
428 _font = _gui->getDefaultFont();
430 wid.setDefaultFont(_font, int(_font->getPointSize()));
433 int px, py, savex, savey;
435 wid.calcPrefSize(&pw, &ph);
436 pw = props->getIntValue("width", pw);
437 ph = props->getIntValue("height", ph);
438 px = savex = props->getIntValue("x", (screenw - pw) / 2);
439 py = savey = props->getIntValue("y", (screenh - ph) / 2);
441 // Negative x/y coordinates are interpreted as distance from the top/right
442 // corner rather than bottom/left.
444 px = screenw - pw + px;
446 py = screenh - ph + py;
448 // Define "x", "y", "width" and/or "height" in the property tree if they
449 // are not specified in the configuration file.
450 wid.layout(px, py, pw, ph);
452 // Use the dimension and location properties as specified in the
453 // configuration file or from the layout widget.
454 _object = makeObject(props, screenw, screenh);
456 // Remove automatically generated properties, so the layout looks
457 // the same next time around, or restore x and y to preserve negative coords.
459 props->setIntValue("x", savex);
461 props->removeChild("x");
464 props->setIntValue("y", savey);
466 props->removeChild("y");
468 if(!userw) props->removeChild("width");
469 if(!userh) props->removeChild("height");
474 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
475 << props->getStringValue("name", "[unnamed]")
476 << " does not contain a proper GUI definition");
481 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
483 if (props->getBoolValue("hide"))
486 bool presetSize = props->hasValue("width") && props->hasValue("height");
487 int width = props->getIntValue("width", parentWidth);
488 int height = props->getIntValue("height", parentHeight);
489 int x = props->getIntValue("x", (parentWidth - width) / 2);
490 int y = props->getIntValue("y", (parentHeight - height) / 2);
491 string type = props->getName();
496 if (type == "dialog") {
498 bool draggable = props->getBoolValue("draggable", true);
499 if (props->getBoolValue("modal", false))
500 obj = new puDialogBox(x, y);
502 obj = new fgPopup(x, y, draggable);
503 setupGroup(obj, props, width, height, true);
504 setColor(obj, props);
507 } else if (type == "group") {
508 puGroup * obj = new puGroup(x, y);
509 setupGroup(obj, props, width, height, false);
510 setColor(obj, props);
513 } else if (type == "frame") {
514 puGroup * obj = new puGroup(x, y);
515 setupGroup(obj, props, width, height, true);
516 setColor(obj, props);
519 } else if (type == "hrule" || type == "vrule") {
520 puFrame * obj = new puFrame(x, y, x + width, y + height);
521 obj->setBorderThickness(0);
522 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
525 } else if (type == "list") {
526 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
527 char ** entries = make_char_array(value_nodes.size());
528 for (unsigned int i = 0; i < value_nodes.size(); i++)
529 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
531 puList * obj = new puList(x, y, x + width, y + height, entries);
532 setupObject(obj, props);
533 setColor(obj, props);
536 } else if (type == "airport-list") {
537 AirportList * obj = new AirportList(x, y, x + width, y + height);
538 setupObject(obj, props);
539 setColor(obj, props);
542 } else if (type == "input") {
543 puInput * obj = new puInput(x, y, x + width, y + height);
544 setupObject(obj, props);
545 setColor(obj, props, FOREGROUND|LABEL);
548 } else if (type == "text") {
549 puText * obj = new puText(x, y);
550 setupObject(obj, props);
552 if (props->getNode("format")) {
553 SGPropertyNode *live = props->getNode("live");
554 if (live && live->getBoolValue())
555 obj->setRenderCallback(format_callback, props);
557 format_callback(obj, x, y, props);
559 // Layed-out objects need their size set, and non-layout ones
560 // get a different placement.
561 if(presetSize) obj->setSize(width, height);
562 else obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
563 setColor(obj, props, LABEL);
566 } else if (type == "checkbox") {
568 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
569 setupObject(obj, props);
570 setColor(obj, props, FOREGROUND|LABEL);
573 } else if (type == "radio") {
575 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
576 setupObject(obj, props);
577 setColor(obj, props, FOREGROUND|LABEL);
580 } else if (type == "button") {
582 const char * legend = props->getStringValue("legend", "[none]");
583 if (props->getBoolValue("one-shot", true))
584 obj = new puOneShot(x, y, legend);
586 obj = new puButton(x, y, legend);
588 obj->setSize(width, height);
589 setupObject(obj, props);
590 setColor(obj, props);
593 } else if (type == "combo") {
594 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
595 char ** entries = make_char_array(value_nodes.size());
596 for (unsigned int i = 0; i < value_nodes.size(); i++)
597 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
599 puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
600 props->getBoolValue("editable", false));
601 setupObject(obj, props);
602 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
603 setColor(obj, props, EDITFIELD);
605 setColor(obj, props, FOREGROUND|LABEL);
609 } else if (type == "slider") {
610 bool vertical = props->getBoolValue("vertical", false);
611 puSlider * obj = new puSlider(x, y, (vertical ? height : width));
612 obj->setMinValue(props->getFloatValue("min", 0.0));
613 obj->setMaxValue(props->getFloatValue("max", 1.0));
614 setupObject(obj, props);
616 obj->setSize(width, height);
617 setColor(obj, props, FOREGROUND|LABEL);
620 } else if (type == "dial") {
621 puDial * obj = new puDial(x, y, width);
622 obj->setMinValue(props->getFloatValue("min", 0.0));
623 obj->setMaxValue(props->getFloatValue("max", 1.0));
624 obj->setWrap(props->getBoolValue("wrap", true));
625 setupObject(obj, props);
626 setColor(obj, props, FOREGROUND|LABEL);
629 } else if (type == "textbox") {
630 int slider_width = props->getIntValue("slider", parentHeight);
631 int wrap = props->getBoolValue("wrap", true);
632 if (slider_width==0) slider_width=20;
633 puLargeInput * obj = new puLargeInput(x, y,
634 x+width, x+height, 2, slider_width, wrap);
636 if (props->hasValue("editable")) {
637 if (props->getBoolValue("editable")==false)
642 setupObject(obj, props);
643 setColor(obj, props, FOREGROUND|LABEL);
646 } else if (type == "select") {
647 vector<SGPropertyNode_ptr> value_nodes;
648 SGPropertyNode * selection_node =
649 fgGetNode(props->getChild("selection")->getStringValue(), true);
651 for (int q = 0; q < selection_node->nChildren(); q++)
652 value_nodes.push_back(selection_node->getChild(q));
654 char ** entries = make_char_array(value_nodes.size());
655 for (unsigned int i = 0; i < value_nodes.size(); i++)
656 entries[i] = strdup((char *)value_nodes[i]->getName());
659 new puSelectBox(x, y, x + width, y + height, entries);
660 setupObject(obj, props);
661 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
662 setColor(obj, props, EDITFIELD);
664 setColor(obj, props, FOREGROUND|LABEL);
673 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
675 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
677 if (props->hasValue("legend"))
678 object->setLegend(props->getStringValue("legend"));
680 if (props->hasValue("label"))
681 object->setLabel(props->getStringValue("label"));
683 if (props->hasValue("border"))
684 object->setBorderThickness( props->getIntValue("border", 2) );
686 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
687 FGFontCache *fc = _gui->get_fontcache();
688 puFont *lfnt = fc->get(nft);
689 object->setLabelFont(*lfnt);
691 object->setLabelFont(*_font);
694 if (props->hasValue("property")) {
695 const char * name = props->getStringValue("name");
698 const char * propname = props->getStringValue("property");
699 SGPropertyNode_ptr node = fgGetNode(propname, true);
700 copy_to_pui(node, object);
701 PropertyObject* po = new PropertyObject(name, object, node);
702 _propertyObjects.push_back(po);
703 if(props->getBoolValue("live"))
704 _liveObjects.push_back(po);
707 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
708 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
709 if (bindings.size() > 0) {
710 GUIInfo * info = new GUIInfo(this);
711 info->key = props->getIntValue("keynum", -1);
712 if (props->hasValue("key"))
713 info->key = getKeyCode(props->getStringValue("key", ""));
715 for (unsigned int i = 0; i < bindings.size(); i++) {
717 SGPropertyNode *binding;
718 while (dest->getChild("binding", j))
721 binding = dest->getChild("binding", j, true);
722 copyProperties(bindings[i], binding);
723 info->bindings.push_back(new FGBinding(binding));
725 object->setCallback(action_callback);
726 object->setUserData(info);
727 _info.push_back(info);
730 object->makeReturnDefault(props->getBoolValue("default"));
734 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
735 int width, int height, bool makeFrame)
737 setupObject(group, props);
740 puFrame* f = new puFrame(0, 0, width, height);
744 int nChildren = props->nChildren();
745 for (int i = 0; i < nChildren; i++)
746 makeObject(props->getChild(i), width, height);
751 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
753 string type = props->getName();
756 if (type == "textbox" && props->getBoolValue("editable"))
759 FGColor *c = new FGColor(_gui->getColor("background"));
760 c->merge(_gui->getColor(type));
761 c->merge(props->getNode("color"));
763 object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
771 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
772 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
773 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
774 { LABEL, PUCOL_LABEL, "label", "color-label" },
775 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
776 { MISC, PUCOL_MISC, "misc", "color-misc" },
777 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
778 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
782 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
784 for (int i = 0; i < numcol; i++) {
789 dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
790 if (which & pucol[i].mask)
791 dirty |= c->merge(props->getNode("color"));
793 if ((pucol[i].mask == LABEL) && !c->isValid())
794 dirty |= c->merge(_gui->getColor("label"));
796 if (c->isValid() && dirty)
797 object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
828 {"left", PU_KEY_LEFT},
830 {"right", PU_KEY_RIGHT},
831 {"down", PU_KEY_DOWN},
832 {"pageup", PU_KEY_PAGE_UP},
833 {"pagedn", PU_KEY_PAGE_DOWN},
834 {"home", PU_KEY_HOME},
836 {"insert", PU_KEY_INSERT},
841 FGDialog::getKeyCode(const char *str)
852 char *buf = new char[strlen(str) + 1];
854 char *s = buf + strlen(buf);
855 while (s > str && s[-1] == ' ')
862 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
864 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
865 s += 6, mod |= SHIFT;
866 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
873 if (strlen(s) == 1 && isascii(*s)) {
878 key = toupper(key) - 64;
880 ; // Alt not propagated to the gui
882 for (char *t = s; *t; t++)
884 for (int i = 0; keymap[i].name; i++) {
885 if (!strcmp(s, keymap[i].name)) {
896 FGDialog::make_char_array (int size)
898 char ** list = new char*[size+1];
899 for (int i = 0; i <= size; i++)
901 _char_arrays.push_back(list);
907 ////////////////////////////////////////////////////////////////////////
908 // Implementation of FGDialog::PropertyObject.
909 ////////////////////////////////////////////////////////////////////////
911 FGDialog::PropertyObject::PropertyObject (const char * n,
913 SGPropertyNode_ptr p)