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"
16 #include "property_list.hxx"
20 enum format_type { f_INVALID, f_INT, f_LONG, f_FLOAT, f_DOUBLE, f_STRING };
21 static const int FORMAT_BUFSIZE = 255;
24 * Makes sure the format matches '%[ -+#]?\d*(\.\d*)?(l?[df]|s)', with
25 * only one number or string placeholder and otherwise arbitrary prefix
26 * and postfix containing only quoted percent signs (%%).
29 validate_format(const char *f)
43 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
45 while (*f && isdigit(*f))
49 while (*f && isdigit(*f))
57 type = l ? f_LONG : f_INT;
59 type = l ? f_DOUBLE : f_FLOAT;
79 ////////////////////////////////////////////////////////////////////////
80 // Implementation of GUIInfo.
81 ////////////////////////////////////////////////////////////////////////
84 * User data for a GUI object.
88 GUIInfo(FGDialog * d);
90 char *format(SGPropertyNode *);
93 vector <SGBinding *> bindings;
100 GUIInfo::GUIInfo (FGDialog * d)
111 for (unsigned int i = 0; i < bindings.size(); i++) {
116 delete [] fmt_string;
119 char *GUIInfo::format(SGPropertyNode *n)
121 if (fmt_type == f_INT)
122 snprintf(text, FORMAT_BUFSIZE, fmt_string, n->getIntValue());
123 else if (fmt_type == f_LONG)
124 snprintf(text, FORMAT_BUFSIZE, fmt_string, n->getLongValue());
125 else if (fmt_type == f_FLOAT)
126 snprintf(text, FORMAT_BUFSIZE, fmt_string, n->getFloatValue());
127 else if (fmt_type == f_DOUBLE)
128 snprintf(text, FORMAT_BUFSIZE, fmt_string, n->getDoubleValue());
130 snprintf(text, FORMAT_BUFSIZE, fmt_string, n->getStringValue());
132 text[FORMAT_BUFSIZE] = '\0';
141 int fgPopup::checkKey(int key, int updown)
143 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
146 puObject *input = getActiveInputField(this);
148 return input->checkKey(key, updown);
150 puObject *object = getKeyObject(this, key);
152 return puPopup::checkKey(key, updown);
154 // invokeCallback() isn't enough; we need to simulate a mouse button press
155 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
156 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
157 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
158 object->checkHit(PU_LEFT_BUTTON, PU_UP,
159 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
160 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
164 puObject *fgPopup::getKeyObject(puObject *object, int key)
167 if(object->getType() & PUCLASS_GROUP)
168 for (puObject *obj = ((puGroup *)object)->getFirstChild();
169 obj; obj = obj->getNextObject())
170 if ((ret = getKeyObject(obj, key)))
173 GUIInfo *info = (GUIInfo *)object->getUserData();
174 if (info && info->key == key)
180 puObject *fgPopup::getActiveInputField(puObject *object)
183 if(object->getType() & PUCLASS_GROUP)
184 for (puObject *obj = ((puGroup *)object)->getFirstChild();
185 obj; obj = obj->getNextObject())
186 if ((ret = getActiveInputField(obj)))
189 if (object->getType() & PUCLASS_INPUT && ((puInput *)object)->isAcceptingInput())
198 int fgPopup::checkHit(int button, int updown, int x, int y)
200 int result = puPopup::checkHit(button, updown, x, y);
205 // This is annoying. We would really want a true result from the
206 // superclass to indicate "handled by child object", but all it
207 // tells us is that the pointer is inside the dialog. So do the
208 // intersection test (again) to make sure we don't start a drag
209 // when inside controls.
211 if(updown == PU_DOWN && !_dragging) {
215 int hit = getHitObjects(this, x, y);
216 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
220 getPosition(&px, &py);
224 } else if(updown == PU_DRAG && _dragging) {
225 setPosition(x + _dX, y + _dY);
232 int fgPopup::getHitObjects(puObject *object, int x, int y)
234 if (!object->isVisible())
238 if(object->getType() & PUCLASS_GROUP)
239 for (puObject *obj = ((puGroup *)object)->getFirstChild();
240 obj; obj = obj->getNextObject())
241 type |= getHitObjects(obj, x, y);
244 object->getAbsolutePosition(&cx, &cy);
245 object->getSize(&cw, &ch);
246 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
247 type |= object->getType();
253 ////////////////////////////////////////////////////////////////////////
255 ////////////////////////////////////////////////////////////////////////
261 action_callback (puObject * object)
263 GUIInfo * info = (GUIInfo *)object->getUserData();
264 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
265 gui->setActiveDialog(info->dialog);
266 int nBindings = info->bindings.size();
267 for (int i = 0; i < nBindings; i++) {
268 info->bindings[i]->fire();
269 if (gui->getActiveDialog() == 0)
272 gui->setActiveDialog(0);
277 ////////////////////////////////////////////////////////////////////////
278 // Static helper functions.
279 ////////////////////////////////////////////////////////////////////////
282 * Copy a property value to a PUI object.
285 copy_to_pui (SGPropertyNode * node, puObject * object)
287 // Treat puText objects specially, so their "values" can be set
289 if (object->getType() & PUCLASS_TEXT) {
290 GUIInfo *info = (GUIInfo *)object->getUserData();
291 if (info && info->fmt_string) {
292 object->setLabel(info->format(node));
294 object->setLabel(node->getStringValue());
299 switch (node->getType()) {
300 case SGPropertyNode::BOOL:
301 case SGPropertyNode::INT:
302 case SGPropertyNode::LONG:
303 object->setValue(node->getIntValue());
305 case SGPropertyNode::FLOAT:
306 case SGPropertyNode::DOUBLE:
307 object->setValue(node->getFloatValue());
310 object->setValue(node->getStringValue());
317 copy_from_pui (puObject * object, SGPropertyNode * node)
319 // puText objects are immutable, so should not be copied out
320 if (object->getType() & PUCLASS_TEXT)
323 switch (node->getType()) {
324 case SGPropertyNode::BOOL:
325 case SGPropertyNode::INT:
326 case SGPropertyNode::LONG:
327 node->setIntValue(object->getIntegerValue());
329 case SGPropertyNode::FLOAT:
330 case SGPropertyNode::DOUBLE:
331 node->setFloatValue(object->getFloatValue());
334 // Special case to handle lists, as getStringValue cannot be overridden
335 if(object->getType() & PUCLASS_LIST)
337 const char *s = ((puList *) object)->getListStringValue();
339 node->setStringValue(s);
343 node->setStringValue(object->getStringValue());
351 ////////////////////////////////////////////////////////////////////////
352 // Implementation of FGDialog.
353 ////////////////////////////////////////////////////////////////////////
355 FGDialog::FGDialog (SGPropertyNode * props)
357 _gui((NewGUI *)globals->get_subsystem("gui")),
360 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
361 SGPropertyNode *nasal = props->getNode("nasal");
363 _nasal_close = nasal->getNode("close");
364 SGPropertyNode *open = nasal->getNode("open");
366 const char *s = open->getStringValue();
367 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
368 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
374 FGDialog::~FGDialog ()
377 _object->getAbsolutePosition(&x, &y);
378 _props->setIntValue("lastx", x);
379 _props->setIntValue("lasty", y);
381 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
383 const char *s = _nasal_close->getStringValue();
384 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
386 nas->deleteModule(_module.c_str());
388 puDeleteObject(_object);
391 // Delete all the info objects we
392 // were forced to keep around because
393 // PUI cannot delete its own user data.
394 for (i = 0; i < _info.size(); i++) {
395 delete (GUIInfo *)_info[i];
398 // Finally, delete the property links.
399 for (i = 0; i < _propertyObjects.size(); i++) {
400 delete _propertyObjects[i];
401 _propertyObjects[i] = 0;
406 FGDialog::updateValues (const char * objectName)
408 if (objectName && !objectName[0])
411 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
412 const string &name = _propertyObjects[i]->name;
413 if (objectName && name != objectName)
416 puObject *obj = _propertyObjects[i]->object;
417 if ((obj->getType() & PUCLASS_LIST) && (dynamic_cast<GUI_ID *>(obj)->id & FGCLASS_LIST)) {
418 fgList *pl = static_cast<fgList *>(obj);
421 copy_to_pui(_propertyObjects[i]->node, obj);
426 FGDialog::applyValues (const char * objectName)
428 if (objectName && !objectName[0])
431 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
432 const string &name = _propertyObjects[i]->name;
433 if (objectName && name != objectName)
436 copy_from_pui(_propertyObjects[i]->object,
437 _propertyObjects[i]->node);
444 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
445 puObject *obj = _liveObjects[i]->object;
446 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
449 copy_to_pui(_liveObjects[i]->node, obj);
454 FGDialog::display (SGPropertyNode * props)
457 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
461 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
462 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
464 bool userx = props->hasValue("x");
465 bool usery = props->hasValue("y");
466 bool userw = props->hasValue("width");
467 bool userh = props->hasValue("height");
469 // Let the layout widget work in the same property subtree.
470 LayoutWidget wid(props);
472 SGPropertyNode *fontnode = props->getNode("font");
474 FGFontCache *fc = globals->get_fontcache();
475 _font = fc->get(fontnode);
477 _font = _gui->getDefaultFont();
479 wid.setDefaultFont(_font, int(_font->getPointSize()));
482 int px, py, savex, savey;
484 wid.calcPrefSize(&pw, &ph);
485 pw = props->getIntValue("width", pw);
486 ph = props->getIntValue("height", ph);
487 px = savex = props->getIntValue("x", (screenw - pw) / 2);
488 py = savey = props->getIntValue("y", (screenh - ph) / 2);
490 // Negative x/y coordinates are interpreted as distance from the top/right
491 // corner rather than bottom/left.
493 px = screenw - pw + px;
495 py = screenh - ph + py;
497 // Define "x", "y", "width" and/or "height" in the property tree if they
498 // are not specified in the configuration file.
499 wid.layout(px, py, pw, ph);
501 // Use the dimension and location properties as specified in the
502 // configuration file or from the layout widget.
503 _object = makeObject(props, screenw, screenh);
505 // Remove automatically generated properties, so the layout looks
506 // the same next time around, or restore x and y to preserve negative coords.
508 props->setIntValue("x", savex);
510 props->removeChild("x");
513 props->setIntValue("y", savey);
515 props->removeChild("y");
517 if(!userw) props->removeChild("width");
518 if(!userh) props->removeChild("height");
523 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
524 << props->getStringValue("name", "[unnamed]")
525 << " does not contain a proper GUI definition");
530 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
532 if (props->getBoolValue("hide"))
535 bool presetSize = props->hasValue("width") && props->hasValue("height");
536 int width = props->getIntValue("width", parentWidth);
537 int height = props->getIntValue("height", parentHeight);
538 int x = props->getIntValue("x", (parentWidth - width) / 2);
539 int y = props->getIntValue("y", (parentHeight - height) / 2);
540 string type = props->getName();
545 if (type == "dialog") {
547 bool draggable = props->getBoolValue("draggable", true);
548 if (props->getBoolValue("modal", false))
549 obj = new puDialogBox(x, y);
551 obj = new fgPopup(x, y, draggable);
552 setupGroup(obj, props, width, height, true);
553 setColor(obj, props);
556 } else if (type == "group") {
557 puGroup * obj = new puGroup(x, y);
558 setupGroup(obj, props, width, height, false);
559 setColor(obj, props);
562 } else if (type == "frame") {
563 puGroup * obj = new puGroup(x, y);
564 setupGroup(obj, props, width, height, true);
565 setColor(obj, props);
568 } else if (type == "hrule" || type == "vrule") {
569 puFrame * obj = new puFrame(x, y, x + width, y + height);
570 obj->setBorderThickness(0);
571 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
574 } else if (type == "list") {
575 int slider_width = props->getIntValue("slider", 20);
576 fgList * obj = new fgList(x, y, x + width, y + height, props, slider_width);
578 obj->setSize(width, height);
579 setupObject(obj, props);
580 setColor(obj, props);
583 } else if (type == "airport-list") {
584 AirportList * obj = new AirportList(x, y, x + width, y + height);
586 obj->setSize(width, height);
587 setupObject(obj, props);
588 setColor(obj, props);
591 } else if (type == "property-list") {
592 PropertyList * obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
594 obj->setSize(width, height);
595 setupObject(obj, props);
596 setColor(obj, props);
599 } else if (type == "input") {
600 puInput * obj = new puInput(x, y, x + width, y + height);
601 setupObject(obj, props);
602 setColor(obj, props, FOREGROUND|LABEL);
605 } else if (type == "text") {
606 puText * obj = new puText(x, y);
607 setupObject(obj, props);
609 // Layed-out objects need their size set, and non-layout ones
610 // get a different placement.
612 obj->setSize(width, height);
614 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
615 setColor(obj, props, LABEL);
618 } else if (type == "checkbox") {
620 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
621 setupObject(obj, props);
622 setColor(obj, props, FOREGROUND|LABEL);
625 } else if (type == "radio") {
627 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
628 setupObject(obj, props);
629 setColor(obj, props, FOREGROUND|LABEL);
632 } else if (type == "button") {
634 const char * legend = props->getStringValue("legend", "[none]");
635 if (props->getBoolValue("one-shot", true))
636 obj = new puOneShot(x, y, legend);
638 obj = new puButton(x, y, legend);
640 obj->setSize(width, height);
641 setupObject(obj, props);
642 setColor(obj, props);
645 } else if (type == "combo") {
646 fgComboBox * obj = new fgComboBox(x, y, x + width, y + height, props,
647 props->getBoolValue("editable", false));
648 setupObject(obj, props);
649 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
650 setColor(obj, props, EDITFIELD);
652 setColor(obj, props, FOREGROUND|LABEL);
656 } else if (type == "slider") {
657 bool vertical = props->getBoolValue("vertical", false);
658 puSlider * obj = new puSlider(x, y, (vertical ? height : width));
659 obj->setMinValue(props->getFloatValue("min", 0.0));
660 obj->setMaxValue(props->getFloatValue("max", 1.0));
661 setupObject(obj, props);
663 obj->setSize(width, height);
664 setColor(obj, props, FOREGROUND|LABEL);
667 } else if (type == "dial") {
668 puDial * obj = new puDial(x, y, width);
669 obj->setMinValue(props->getFloatValue("min", 0.0));
670 obj->setMaxValue(props->getFloatValue("max", 1.0));
671 obj->setWrap(props->getBoolValue("wrap", true));
672 setupObject(obj, props);
673 setColor(obj, props, FOREGROUND|LABEL);
676 } else if (type == "textbox") {
677 int slider_width = props->getIntValue("slider", 20);
678 int wrap = props->getBoolValue("wrap", true);
679 puaLargeInput * obj = new puaLargeInput(x, y,
680 x+width, x+height, 2, slider_width, wrap);
682 if (props->hasValue("editable")) {
683 if (props->getBoolValue("editable")==false)
689 obj->setSize(width, height);
690 setupObject(obj, props);
691 setColor(obj, props, FOREGROUND|LABEL);
694 } else if (type == "select") {
695 fgSelectBox * obj = new fgSelectBox(x, y, x + width, y + height, props);
696 setupObject(obj, props);
697 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
698 setColor(obj, props, EDITFIELD);
700 setColor(obj, props, FOREGROUND|LABEL);
709 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
711 string type = props->getName();
712 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
714 if (props->hasValue("legend"))
715 object->setLegend(props->getStringValue("legend"));
717 if (props->hasValue("label"))
718 object->setLabel(props->getStringValue("label"));
720 if (props->hasValue("border"))
721 object->setBorderThickness( props->getIntValue("border", 2) );
723 if ( SGPropertyNode *nft = props->getNode("font", false) ) {
724 FGFontCache *fc = globals->get_fontcache();
725 puFont *lfnt = fc->get(nft);
726 object->setLabelFont(*lfnt);
728 object->setLabelFont(*_font);
731 if (props->hasValue("property")) {
732 const char * name = props->getStringValue("name");
735 const char * propname = props->getStringValue("property");
736 SGPropertyNode_ptr node = fgGetNode(propname, true);
737 copy_to_pui(node, object);
739 PropertyObject* po = new PropertyObject(name, object, node);
740 _propertyObjects.push_back(po);
741 if(props->getBoolValue("live"))
742 _liveObjects.push_back(po);
745 SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
746 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
747 if (type == "text" || bindings.size() > 0) {
748 GUIInfo * info = new GUIInfo(this);
749 info->key = props->getIntValue("keynum", -1);
750 if (props->hasValue("key"))
751 info->key = getKeyCode(props->getStringValue("key", ""));
753 for (unsigned int i = 0; i < bindings.size(); i++) {
755 SGPropertyNode *binding;
756 while (dest->getChild("binding", j))
759 const char *cmd = bindings[i]->getStringValue("command");
760 if (!strcmp(cmd, "nasal"))
761 bindings[i]->setStringValue("module", _module.c_str());
763 binding = dest->getChild("binding", j, true);
764 copyProperties(bindings[i], binding);
765 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
767 object->setCallback(action_callback);
769 if (type == "input" && props->getBoolValue("live"))
770 object->setDownCallback(action_callback);
772 if (type == "text") {
773 const char *format = props->getStringValue("format", 0);
775 info->fmt_type = validate_format(props->getStringValue("format", 0));
776 if (info->fmt_type != f_INVALID) {
777 info->text = new char[FORMAT_BUFSIZE + 1];
778 info->fmt_string = new char[strlen(format) + 1];
779 strcpy(info->fmt_string, format);
784 object->setUserData(info);
785 _info.push_back(info);
788 object->makeReturnDefault(props->getBoolValue("default"));
792 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
793 int width, int height, bool makeFrame)
795 setupObject(group, props);
798 puFrame* f = new puFrame(0, 0, width, height);
802 int nChildren = props->nChildren();
803 for (int i = 0; i < nChildren; i++)
804 makeObject(props->getChild(i), width, height);
809 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
811 string type = props->getName();
814 if (type == "textbox" && props->getBoolValue("editable"))
817 FGColor *c = new FGColor(_gui->getColor("background"));
818 c->merge(_gui->getColor(type));
819 c->merge(props->getNode("color"));
821 object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
829 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
830 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
831 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
832 { LABEL, PUCOL_LABEL, "label", "color-label" },
833 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
834 { MISC, PUCOL_MISC, "misc", "color-misc" },
835 #ifdef PUCOL_EDITFIELD // plib > 0.8.4
836 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
840 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
842 for (int i = 0; i < numcol; i++) {
847 dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
848 if (which & pucol[i].mask)
849 dirty |= c->merge(props->getNode("color"));
851 if ((pucol[i].mask == LABEL) && !c->isValid())
852 dirty |= c->merge(_gui->getColor("label"));
854 dirty |= c->merge(props->getNode(pucol[i].cname));
856 if (c->isValid() && dirty)
857 object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
889 {"left", PU_KEY_LEFT},
891 {"right", PU_KEY_RIGHT},
892 {"down", PU_KEY_DOWN},
893 {"pageup", PU_KEY_PAGE_UP},
894 {"pagedn", PU_KEY_PAGE_DOWN},
895 {"home", PU_KEY_HOME},
897 {"insert", PU_KEY_INSERT},
902 FGDialog::getKeyCode(const char *str)
913 char *buf = new char[strlen(str) + 1];
915 char *s = buf + strlen(buf);
916 while (s > str && s[-1] == ' ')
923 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
925 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
926 s += 6, mod |= SHIFT;
927 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
934 if (strlen(s) == 1 && isascii(*s)) {
939 key = toupper(key) - 64;
941 ; // Alt not propagated to the gui
943 for (char *t = s; *t; t++)
945 for (int i = 0; keymap[i].name; i++) {
946 if (!strcmp(s, keymap[i].name)) {
958 ////////////////////////////////////////////////////////////////////////
959 // Implementation of FGDialog::PropertyObject.
960 ////////////////////////////////////////////////////////////////////////
962 FGDialog::PropertyObject::PropertyObject (const char * n,
964 SGPropertyNode_ptr p)
974 ////////////////////////////////////////////////////////////////////////
975 // Implementation of fgValueList and derived pui widgets
976 ////////////////////////////////////////////////////////////////////////
979 fgValueList::fgValueList(SGPropertyNode *p) :
986 fgValueList::update()
992 fgValueList::~fgValueList()
998 fgValueList::make_list()
1000 vector<SGPropertyNode_ptr> value_nodes = _props->getChildren("value");
1001 _list = new char *[value_nodes.size() + 1];
1003 for (i = 0; i < value_nodes.size(); i++)
1004 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
1009 fgValueList::destroy_list()
1011 for (int i = 0; _list[i] != 0; i++)
1022 fgValueList::update();
1026 // end of dialog.cxx