1 // dialog.cxx: implementation of an XML-configurable dialog box.
7 #include <stdlib.h> // atof()
9 #include <Input/input.hxx>
12 #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++) {
53 int fgPopup::checkKey(int key, int updown)
55 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
58 puObject *input = getActiveInputField(this);
60 return input->checkKey(key, updown);
62 puObject *object = getKeyObject(this, key);
64 return puPopup::checkKey(key, updown);
66 // invokeCallback() isn't enough; we need to simulate a mouse button press
67 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
68 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
69 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
70 object->checkHit(PU_LEFT_BUTTON, PU_UP,
71 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
72 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
76 puObject *fgPopup::getKeyObject(puObject *object, int key)
79 if(object->getType() & PUCLASS_GROUP)
80 for (puObject *obj = ((puGroup *)object)->getFirstChild();
81 obj; obj = obj->getNextObject())
82 if ((ret = getKeyObject(obj, key)))
85 GUIInfo *info = (GUIInfo *)object->getUserData();
86 if (info && info->key == key)
92 puObject *fgPopup::getActiveInputField(puObject *object)
95 if(object->getType() & PUCLASS_GROUP)
96 for (puObject *obj = ((puGroup *)object)->getFirstChild();
97 obj; obj = obj->getNextObject())
98 if ((ret = getActiveInputField(obj)))
101 if (object->getType() & PUCLASS_INPUT && ((puInput *)object)->isAcceptingInput())
110 int fgPopup::checkHit(int button, int updown, int x, int y)
112 int result = puPopup::checkHit(button, updown, x, y);
117 // This is annoying. We would really want a true result from the
118 // superclass to indicate "handled by child object", but all it
119 // tells us is that the pointer is inside the dialog. So do the
120 // intersection test (again) to make sure we don't start a drag
121 // when inside controls.
123 if(updown == PU_DOWN && !_dragging) {
127 int hit = getHitObjects(this, x, y);
128 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
132 getPosition(&px, &py);
136 } else if(updown == PU_DRAG && _dragging) {
137 setPosition(x + _dX, y + _dY);
144 int fgPopup::getHitObjects(puObject *object, int x, int y)
147 if(object->getType() & PUCLASS_GROUP)
148 for (puObject *obj = ((puGroup *)object)->getFirstChild();
149 obj; obj = obj->getNextObject())
150 type |= getHitObjects(obj, x, y);
153 object->getAbsolutePosition(&cx, &cy);
154 object->getSize(&cw, &ch);
155 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
156 type |= object->getType();
162 ////////////////////////////////////////////////////////////////////////
164 ////////////////////////////////////////////////////////////////////////
170 action_callback (puObject * object)
172 GUIInfo * info = (GUIInfo *)object->getUserData();
173 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
174 gui->setActiveDialog(info->dialog);
175 int nBindings = info->bindings.size();
176 for (int i = 0; i < nBindings; i++) {
177 info->bindings[i]->fire();
178 if (gui->getActiveDialog() == 0)
181 gui->setActiveDialog(0);
186 format_callback(puObject *obj, int dx, int dy, void *n)
188 SGPropertyNode *node = (SGPropertyNode *)n;
189 const char *format = node->getStringValue("format"), *f = format;
190 bool number, l = false;
191 // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
202 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
204 while (*f && isdigit(*f))
208 while (*f && isdigit(*f))
216 else if (*f == 's') {
233 const char *src = obj->getLabel();
236 float value = atof(src);
237 snprintf(buf, 256, format, value);
239 snprintf(buf, 256, format, src);
244 SGPropertyNode *result = node->getNode("formatted", true);
245 result->setStringValue(buf);
246 obj->setLabel(result->getStringValue());
251 ////////////////////////////////////////////////////////////////////////
252 // Static helper functions.
253 ////////////////////////////////////////////////////////////////////////
256 * Copy a property value to a PUI object.
259 copy_to_pui (SGPropertyNode * node, puObject * object)
261 // Treat puText objects specially, so their "values" can be set
263 if(object->getType() & PUCLASS_TEXT) {
264 object->setLabel(node->getStringValue());
268 switch (node->getType()) {
269 case SGPropertyNode::BOOL:
270 case SGPropertyNode::INT:
271 case SGPropertyNode::LONG:
272 object->setValue(node->getIntValue());
274 case SGPropertyNode::FLOAT:
275 case SGPropertyNode::DOUBLE:
276 object->setValue(node->getFloatValue());
279 object->setValue(node->getStringValue());
286 copy_from_pui (puObject * object, SGPropertyNode * node)
288 // puText objects are immutable, so should not be copied out
289 if(object->getType() & PUCLASS_TEXT)
292 switch (node->getType()) {
293 case SGPropertyNode::BOOL:
294 case SGPropertyNode::INT:
295 case SGPropertyNode::LONG:
296 node->setIntValue(object->getIntegerValue());
298 case SGPropertyNode::FLOAT:
299 case SGPropertyNode::DOUBLE:
300 node->setFloatValue(object->getFloatValue());
303 // Special case to handle lists, as getStringValue cannot be overridden
304 if(object->getType() & PUCLASS_LIST)
306 node->setStringValue(((puList *) object)->getListStringValue());
310 node->setStringValue(object->getStringValue());
318 ////////////////////////////////////////////////////////////////////////
319 // Implementation of FGDialog.
320 ////////////////////////////////////////////////////////////////////////
322 FGDialog::FGDialog (SGPropertyNode * props)
324 _gui((NewGUI *)globals->get_subsystem("gui")),
330 FGDialog::~FGDialog ()
333 _object->getAbsolutePosition(&x, &y);
334 _props->setIntValue("lastx", x);
335 _props->setIntValue("lasty", y);
336 puDeleteObject(_object);
340 // Delete all the arrays we made
341 // and were forced to keep around
342 // because PUI won't do its own
343 // memory management.
344 for (i = 0; i < _char_arrays.size(); i++) {
345 for (int j = 0; _char_arrays[i][j] != 0; j++)
346 free(_char_arrays[i][j]); // added with strdup
347 delete[] _char_arrays[i];
350 // Delete all the info objects we
351 // were forced to keep around because
352 // PUI cannot delete its own user data.
353 for (i = 0; i < _info.size(); i++) {
354 delete (GUIInfo *)_info[i];
358 // Finally, delete the property links.
359 for (i = 0; i < _propertyObjects.size(); i++) {
360 delete _propertyObjects[i];
361 _propertyObjects[i] = 0;
366 FGDialog::updateValue (const char * objectName)
368 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
369 const string &name = _propertyObjects[i]->name;
370 if (name == objectName)
371 copy_to_pui(_propertyObjects[i]->node,
372 _propertyObjects[i]->object);
377 FGDialog::applyValue (const char * objectName)
379 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
380 if (_propertyObjects[i]->name == objectName)
381 copy_from_pui(_propertyObjects[i]->object,
382 _propertyObjects[i]->node);
387 FGDialog::updateValues ()
389 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
390 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
394 FGDialog::applyValues ()
396 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
397 copy_from_pui(_propertyObjects[i]->object,
398 _propertyObjects[i]->node);
404 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
405 puObject *obj = _liveObjects[i]->object;
406 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
409 copy_to_pui(_liveObjects[i]->node, obj);
414 FGDialog::display (SGPropertyNode * props)
417 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
421 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
422 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
424 bool userx = props->hasValue("x");
425 bool usery = props->hasValue("y");
426 bool userw = props->hasValue("width");
427 bool userh = props->hasValue("height");
429 // Let the layout widget work in the same property subtree.
430 LayoutWidget wid(props);
432 SGPropertyNode *fontnode = props->getNode("font");
434 FGFontCache *fc = _gui->get_fontcache();
435 _font = fc->get(fontnode);
437 _font = _gui->getDefaultFont();
439 wid.setDefaultFont(_font, int(_font->getPointSize()));
442 int px, py, savex, savey;
444 wid.calcPrefSize(&pw, &ph);
445 pw = props->getIntValue("width", pw);
446 ph = props->getIntValue("height", ph);
447 px = savex = props->getIntValue("x", (screenw - pw) / 2);
448 py = savey = props->getIntValue("y", (screenh - ph) / 2);
450 // Negative x/y coordinates are interpreted as distance from the top/right
451 // corner rather than bottom/left.
453 px = screenw - pw + px;
455 py = screenh - ph + py;
457 // Define "x", "y", "width" and/or "height" in the property tree if they
458 // are not specified in the configuration file.
459 wid.layout(px, py, pw, ph);
461 // Use the dimension and location properties as specified in the
462 // configuration file or from the layout widget.
463 _object = makeObject(props, screenw, screenh);
465 // Remove automatically generated properties, so the layout looks
466 // the same next time around, or restore x and y to preserve negative coords.
468 props->setIntValue("x", savex);
470 props->removeChild("x");
473 props->setIntValue("y", savey);
475 props->removeChild("y");
477 if(!userw) props->removeChild("width");
478 if(!userh) props->removeChild("height");
483 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
484 << props->getStringValue("name", "[unnamed]")
485 << " does not contain a proper GUI definition");
490 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
492 if (props->getBoolValue("hide"))
495 bool presetSize = props->hasValue("width") && props->hasValue("height");
496 int width = props->getIntValue("width", parentWidth);
497 int height = props->getIntValue("height", parentHeight);
498 int x = props->getIntValue("x", (parentWidth - width) / 2);
499 int y = props->getIntValue("y", (parentHeight - height) / 2);
500 string type = props->getName();
505 if (type == "dialog") {
507 bool draggable = props->getBoolValue("draggable", true);
508 if (props->getBoolValue("modal", false))
509 obj = new puDialogBox(x, y);
511 obj = new fgPopup(x, y, draggable);
512 setupGroup(obj, props, width, height, true);
513 setColor(obj, props);
516 } else if (type == "group") {
517 puGroup * obj = new puGroup(x, y);
518 setupGroup(obj, props, width, height, false);
519 setColor(obj, props);
522 } else if (type == "frame") {
523 puGroup * obj = new puGroup(x, y);
524 setupGroup(obj, props, width, height, true);
525 setColor(obj, props);
528 } else if (type == "hrule" || type == "vrule") {
529 puFrame * obj = new puFrame(x, y, x + width, y + height);
530 obj->setBorderThickness(0);
531 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
534 } else if (type == "list") {
535 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
536 char ** entries = make_char_array(value_nodes.size());
537 for (unsigned int i = 0; i < value_nodes.size(); i++)
538 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
540 puList * obj = new puList(x, y, x + width, y + height, entries);
541 setupObject(obj, props);
542 setColor(obj, props);
545 } else if (type == "airport-list") {
546 AirportList * obj = new AirportList(x, y, x + width, y + height);
547 setupObject(obj, props);
548 setColor(obj, props);
551 } else if (type == "input") {
552 puInput * obj = new puInput(x, y, x + width, y + height);
553 setupObject(obj, props);
554 setColor(obj, props, FOREGROUND|LABEL);
557 } else if (type == "text") {
558 puText * obj = new puText(x, y);
559 setupObject(obj, props);
561 if (props->getNode("format")) {
562 SGPropertyNode *live = props->getNode("live");
563 if (live && live->getBoolValue())
564 obj->setRenderCallback(format_callback, props);
566 format_callback(obj, x, y, props);
568 // Layed-out objects need their size set, and non-layout ones
569 // get a different placement.
570 if(presetSize) obj->setSize(width, height);
571 else obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
572 setColor(obj, props, LABEL);
575 } else if (type == "checkbox") {
577 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
578 setupObject(obj, props);
579 setColor(obj, props, FOREGROUND|LABEL);
582 } else if (type == "radio") {
584 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
585 setupObject(obj, props);
586 setColor(obj, props, FOREGROUND|LABEL);
589 } else if (type == "button") {
591 const char * legend = props->getStringValue("legend", "[none]");
592 if (props->getBoolValue("one-shot", true))
593 obj = new puOneShot(x, y, legend);
595 obj = new puButton(x, y, legend);
597 obj->setSize(width, height);
598 setupObject(obj, props);
599 setColor(obj, props);
602 } else if (type == "combo") {
603 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
604 char ** entries = make_char_array(value_nodes.size());
605 for (unsigned int i = 0; i < value_nodes.size(); i++)
606 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
608 puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
609 props->getBoolValue("editable", false));
610 setupObject(obj, props);
611 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
612 setColor(obj, props, EDITFIELD);
614 setColor(obj, props, FOREGROUND|LABEL);
618 } else if (type == "slider") {
619 bool vertical = props->getBoolValue("vertical", false);
620 puSlider * obj = new puSlider(x, y, (vertical ? height : width));
621 obj->setMinValue(props->getFloatValue("min", 0.0));
622 obj->setMaxValue(props->getFloatValue("max", 1.0));
623 setupObject(obj, props);
625 obj->setSize(width, height);
626 setColor(obj, props, FOREGROUND|LABEL);
629 } else if (type == "dial") {
630 puDial * obj = new puDial(x, y, width);
631 obj->setMinValue(props->getFloatValue("min", 0.0));
632 obj->setMaxValue(props->getFloatValue("max", 1.0));
633 obj->setWrap(props->getBoolValue("wrap", true));
634 setupObject(obj, props);
635 setColor(obj, props, FOREGROUND|LABEL);
638 } else if (type == "textbox") {
639 int slider_width = props->getIntValue("slider", parentHeight);
640 int wrap = props->getBoolValue("wrap", true);
641 if (slider_width==0) slider_width=20;
642 puLargeInput * obj = new puLargeInput(x, y,
643 x+width, x+height, 2, slider_width, wrap);
645 if (props->hasValue("editable")) {
646 if (props->getBoolValue("editable")==false)
651 setupObject(obj, props);
652 setColor(obj, props, FOREGROUND|LABEL);
655 } else if (type == "select") {
656 vector<SGPropertyNode_ptr> value_nodes;
657 SGPropertyNode * selection_node =
658 fgGetNode(props->getChild("selection")->getStringValue(), true);
660 for (int q = 0; q < selection_node->nChildren(); q++)
661 value_nodes.push_back(selection_node->getChild(q));
663 char ** entries = make_char_array(value_nodes.size());
664 for (unsigned int i = 0; i < value_nodes.size(); i++)
665 entries[i] = strdup((char *)value_nodes[i]->getName());
668 new puSelectBox(x, y, x + width, y + height, entries);
669 setupObject(obj, props);
670 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
671 setColor(obj, props, EDITFIELD);
673 setColor(obj, props, FOREGROUND|LABEL);
682 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
684 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
686 if (props->hasValue("legend"))
687 object->setLegend(props->getStringValue("legend"));
689 if (props->hasValue("label"))
690 object->setLabel(props->getStringValue("label"));
692 if (props->hasValue("border"))
693 object->setBorderThickness( props->getIntValue("border", 2) );
695 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
696 FGFontCache *fc = _gui->get_fontcache();
697 puFont *lfnt = fc->get(nft);
698 object->setLabelFont(*lfnt);
700 object->setLabelFont(*_font);
703 if (props->hasValue("property")) {
704 const char * name = props->getStringValue("name");
707 const char * propname = props->getStringValue("property");
708 SGPropertyNode_ptr node = fgGetNode(propname, true);
709 copy_to_pui(node, object);
710 PropertyObject* po = new PropertyObject(name, object, node);
711 _propertyObjects.push_back(po);
712 if(props->getBoolValue("live"))
713 _liveObjects.push_back(po);
716 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
717 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
718 if (bindings.size() > 0) {
719 GUIInfo * info = new GUIInfo(this);
720 info->key = props->getIntValue("keynum", -1);
721 if (props->hasValue("key"))
722 info->key = getKeyCode(props->getStringValue("key", ""));
724 for (unsigned int i = 0; i < bindings.size(); i++) {
726 SGPropertyNode *binding;
727 while (dest->getChild("binding", j))
730 binding = dest->getChild("binding", j, true);
731 copyProperties(bindings[i], binding);
732 info->bindings.push_back(new FGBinding(binding));
734 object->setCallback(action_callback);
735 object->setUserData(info);
736 _info.push_back(info);
739 object->makeReturnDefault(props->getBoolValue("default"));
743 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
744 int width, int height, bool makeFrame)
746 setupObject(group, props);
749 puFrame* f = new puFrame(0, 0, width, height);
753 int nChildren = props->nChildren();
754 for (int i = 0; i < nChildren; i++)
755 makeObject(props->getChild(i), width, height);
760 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
762 string type = props->getName();
765 if (type == "textbox" && props->getBoolValue("editable"))
768 FGColor *c = new FGColor(_gui->getColor("background"));
769 c->merge(_gui->getColor(type));
770 c->merge(props->getNode("color"));
772 object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
780 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
781 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
782 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
783 { LABEL, PUCOL_LABEL, "label", "color-label" },
784 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
785 { MISC, PUCOL_MISC, "misc", "color-misc" },
786 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
787 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
791 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
793 for (int i = 0; i < numcol; i++) {
798 dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
799 if (which & pucol[i].mask)
800 dirty |= c->merge(props->getNode("color"));
802 if ((pucol[i].mask == LABEL) && !c->isValid())
803 dirty |= c->merge(_gui->getColor("label"));
805 if (c->isValid() && dirty)
806 object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
838 {"left", PU_KEY_LEFT},
840 {"right", PU_KEY_RIGHT},
841 {"down", PU_KEY_DOWN},
842 {"pageup", PU_KEY_PAGE_UP},
843 {"pagedn", PU_KEY_PAGE_DOWN},
844 {"home", PU_KEY_HOME},
846 {"insert", PU_KEY_INSERT},
851 FGDialog::getKeyCode(const char *str)
862 char *buf = new char[strlen(str) + 1];
864 char *s = buf + strlen(buf);
865 while (s > str && s[-1] == ' ')
872 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
874 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
875 s += 6, mod |= SHIFT;
876 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
883 if (strlen(s) == 1 && isascii(*s)) {
888 key = toupper(key) - 64;
890 ; // Alt not propagated to the gui
892 for (char *t = s; *t; t++)
894 for (int i = 0; keymap[i].name; i++) {
895 if (!strcmp(s, keymap[i].name)) {
906 FGDialog::make_char_array (int size)
908 char ** list = new char*[size+1];
909 for (int i = 0; i <= size; i++)
911 _char_arrays.push_back(list);
917 ////////////////////////////////////////////////////////////////////////
918 // Implementation of FGDialog::PropertyObject.
919 ////////////////////////////////////////////////////////////////////////
921 FGDialog::PropertyObject::PropertyObject (const char * n,
923 SGPropertyNode_ptr p)