1 // dialog.cxx: implementation of an XML-configurable dialog box.
7 #include <simgear/structure/SGBinding.hxx>
8 #include <Scripting/NasalSys.hxx>
9 #include <Main/fg_os.hxx>
12 #include "new_gui.hxx"
13 #include "AirportList.hxx"
14 #include "property_list.hxx"
18 enum format_type { f_INVALID, f_INT, f_LONG, f_FLOAT, f_DOUBLE, f_STRING };
19 static const int FORMAT_BUFSIZE = 255;
20 static const int RESIZE_MARGIN = 7;
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 while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
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.
90 void apply_format(SGPropertyNode *);
93 SGPropertyNode_ptr node;
94 vector <SGBinding *> bindings;
96 string label, legend, text, format;
100 GUIInfo::GUIInfo (FGDialog *d) :
109 for (unsigned int i = 0; i < bindings.size(); i++) {
115 void GUIInfo::apply_format(SGPropertyNode *n)
117 char buf[FORMAT_BUFSIZE + 1];
118 if (fmt_type == f_INT)
119 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getIntValue());
120 else if (fmt_type == f_LONG)
121 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getLongValue());
122 else if (fmt_type == f_FLOAT)
123 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getFloatValue());
124 else if (fmt_type == f_DOUBLE)
125 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getDoubleValue());
127 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getStringValue());
129 buf[FORMAT_BUFSIZE] = '\0';
138 int fgPopup::checkKey(int key, int updown)
140 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
143 puObject *input = getActiveInputField(this);
145 return input->checkKey(key, updown);
147 puObject *object = getKeyObject(this, key);
149 return puPopup::checkKey(key, updown);
151 // invokeCallback() isn't enough; we need to simulate a mouse button press
152 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
153 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
154 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
155 object->checkHit(PU_LEFT_BUTTON, PU_UP,
156 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
157 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
161 puObject *fgPopup::getKeyObject(puObject *object, int key)
164 if (object->getType() & PUCLASS_GROUP)
165 for (puObject *obj = ((puGroup *)object)->getFirstChild();
166 obj; obj = obj->getNextObject())
167 if ((ret = getKeyObject(obj, key)))
170 GUIInfo *info = (GUIInfo *)object->getUserData();
171 if (info && info->key == key)
177 puObject *fgPopup::getActiveInputField(puObject *object)
180 if (object->getType() & PUCLASS_GROUP)
181 for (puObject *obj = ((puGroup *)object)->getFirstChild();
182 obj; obj = obj->getNextObject())
183 if ((ret = getActiveInputField(obj)))
186 if (object->getType() & (PUCLASS_INPUT|PUCLASS_LARGEINPUT)
187 && ((puInput *)object)->isAcceptingInput())
196 int fgPopup::checkHit(int button, int updown, int x, int y)
199 if (updown != PU_DRAG && !_dragging)
200 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) {
214 int global_drag = fgGetKeyModifiers() & KEYMOD_SHIFT;
215 int global_resize = fgGetKeyModifiers() & KEYMOD_CTRL;
217 int hit = getHitObjects(this, x, y);
218 if (hit & PUCLASS_LIST) // ctrl-click in property browser (toggle bool)
220 if (!global_resize && hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
221 |PUCLASS_LARGEINPUT|PUCLASS_SCROLLBAR))
224 getPosition(&_dlgX, &_dlgY);
225 getSize(&_dlgW, &_dlgH);
226 _start_cursor = fgGetMouseCursor();
231 // check and prepare for resizing
232 static const int cursor[] = {
233 MOUSE_CURSOR_POINTER, MOUSE_CURSOR_LEFTSIDE, MOUSE_CURSOR_RIGHTSIDE, 0,
234 MOUSE_CURSOR_TOPSIDE, MOUSE_CURSOR_TOPLEFT, MOUSE_CURSOR_TOPRIGHT, 0,
235 MOUSE_CURSOR_BOTTOMSIDE, MOUSE_CURSOR_BOTTOMLEFT, MOUSE_CURSOR_BOTTOMRIGHT, 0,
239 if (!global_drag && _resizable) {
240 int hmargin = global_resize ? _dlgW / 3 : RESIZE_MARGIN;
241 int vmargin = global_resize ? _dlgH / 3 : RESIZE_MARGIN;
243 if (y - _dlgY < vmargin)
245 else if (_dlgY + _dlgH - y < vmargin)
248 if (x - _dlgX < hmargin)
250 else if (_dlgX + _dlgW - x < hmargin)
253 if (!_resizing && global_resize)
254 _resizing = BOTTOM|RIGHT;
256 _cursor = cursor[_resizing];
257 if (_resizing && _resizable)
258 fgSetMouseCursor(_cursor);
261 } else if (updown == PU_DRAG && _dragging) {
263 GUIInfo *info = (GUIInfo *)getUserData();
264 if (_resizable && info && info->node) {
267 if (_resizing & LEFT)
269 if (_resizing & RIGHT)
273 if (_resizing & BOTTOM)
277 LayoutWidget wid(info->node);
278 wid.calcPrefSize(&prefw, &prefh);
286 if (_resizing & LEFT)
288 if (_resizing & BOTTOM)
291 wid.layout(x, y, w, h);
294 applySize(static_cast<puObject *>(this));
295 getFirstChild()->setSize(w, h); // dialog background puFrame
298 int posX = x + _dlgX - _startX,
299 posY = y + _dlgY - _startY;
300 setPosition(posX, posY);
302 GUIInfo *info = (GUIInfo *)getUserData();
303 if (info && info->node) {
304 info->node->setIntValue("x", posX);
305 info->node->setIntValue("y", posY);
309 } else if (_dragging) {
310 fgSetMouseCursor(_start_cursor);
316 int fgPopup::getHitObjects(puObject *object, int x, int y)
318 if (!object->isVisible())
322 if (object->getType() & PUCLASS_GROUP)
323 for (puObject *obj = ((puGroup *)object)->getFirstChild();
324 obj; obj = obj->getNextObject())
325 type |= getHitObjects(obj, x, y);
328 object->getAbsolutePosition(&cx, &cy);
329 object->getSize(&cw, &ch);
330 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch)
331 type |= object->getType();
335 void fgPopup::applySize(puObject *object)
337 // compound plib widgets use setUserData() for internal purposes, so refuse
338 // to descend into anything that has other bits set than the following
339 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
340 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
341 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
343 int type = object->getType();
344 if (type & PUCLASS_GROUP && !(type & ~validUserData))
345 for (puObject *obj = ((puGroup *)object)->getFirstChild();
346 obj; obj = obj->getNextObject())
349 GUIInfo *info = (GUIInfo *)object->getUserData();
353 SGPropertyNode *n = info->node;
355 SG_LOG(SG_GENERAL, SG_ALERT, "fgPopup::applySize: no props");
358 int x = n->getIntValue("x");
359 int y = n->getIntValue("y");
360 int w = n->getIntValue("width", 4);
361 int h = n->getIntValue("height", 4);
362 object->setPosition(x, y);
363 object->setSize(w, h);
366 ////////////////////////////////////////////////////////////////////////
368 void FGDialog::ConditionalObject::update(FGDialog* aDlg)
370 if (_name == "visible") {
371 bool wasVis = _pu->isVisible();
372 bool newVis = test();
374 if (newVis == wasVis) {
378 if (newVis) { // puObject needs a setVisible. Oh well.
383 } else if (_name == "enable") {
384 bool wasEnabled = _pu->isActive();
385 bool newEnable = test();
387 if (wasEnabled == newEnable) {
398 aDlg->setNeedsLayout();
400 \f////////////////////////////////////////////////////////////////////////
402 ////////////////////////////////////////////////////////////////////////
408 action_callback (puObject *object)
410 GUIInfo *info = (GUIInfo *)object->getUserData();
411 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
412 gui->setActiveDialog(info->dialog);
413 int nBindings = info->bindings.size();
414 for (int i = 0; i < nBindings; i++) {
415 info->bindings[i]->fire();
416 if (gui->getActiveDialog() == 0)
419 gui->setActiveDialog(0);
424 ////////////////////////////////////////////////////////////////////////
425 // Static helper functions.
426 ////////////////////////////////////////////////////////////////////////
429 * Copy a property value to a PUI object.
432 copy_to_pui (SGPropertyNode *node, puObject *object)
434 using namespace simgear;
435 GUIInfo *info = (GUIInfo *)object->getUserData();
437 SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
438 return; // this can't really happen
441 // Treat puText objects specially, so their "values" can be set
443 if (object->getType() & PUCLASS_TEXT) {
444 if (info->fmt_type != f_INVALID)
445 info->apply_format(node);
447 info->text = node->getStringValue();
449 object->setLabel(info->text.c_str());
453 switch (node->getType()) {
457 object->setValue(node->getIntValue());
461 object->setValue(node->getFloatValue());
464 info->text = node->getStringValue();
465 object->setValue(info->text.c_str());
472 copy_from_pui (puObject *object, SGPropertyNode *node)
474 using namespace simgear;
475 // puText objects are immutable, so should not be copied out
476 if (object->getType() & PUCLASS_TEXT)
479 switch (node->getType()) {
483 node->setIntValue(object->getIntegerValue());
487 node->setFloatValue(object->getFloatValue());
490 const char *s = object->getStringValue();
492 node->setStringValue(s);
499 ////////////////////////////////////////////////////////////////////////
500 // Implementation of FGDialog.
501 ////////////////////////////////////////////////////////////////////////
503 FGDialog::FGDialog (SGPropertyNode *props) :
505 _gui((NewGUI *)globals->get_subsystem("gui")),
507 _needsRelayout(false)
509 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
511 SGPropertyNode *nasal = props->getNode("nasal");
513 _nasal_close = nasal->getNode("close");
514 SGPropertyNode *open = nasal->getNode("open");
516 const char *s = open->getStringValue();
517 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
519 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
525 FGDialog::~FGDialog ()
528 _object->getAbsolutePosition(&x, &y);
529 _props->setIntValue("lastx", x);
530 _props->setIntValue("lasty", y);
532 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
535 const char *s = _nasal_close->getStringValue();
536 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
538 nas->deleteModule(_module.c_str());
541 puDeleteObject(_object);
544 // Delete all the info objects we
545 // were forced to keep around because
546 // PUI cannot delete its own user data.
547 for (i = 0; i < _info.size(); i++) {
548 delete (GUIInfo *)_info[i];
551 // Finally, delete the property links.
552 for (i = 0; i < _propertyObjects.size(); i++) {
553 delete _propertyObjects[i];
554 _propertyObjects[i] = 0;
559 FGDialog::updateValues (const char *objectName)
561 if (objectName && !objectName[0])
564 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
565 const string &name = _propertyObjects[i]->name;
566 if (objectName && name != objectName) {
570 puObject *widget = _propertyObjects[i]->object;
571 int widgetType = widget->getType();
572 if ((widgetType & PUCLASS_LIST) && (dynamic_cast<GUI_ID *>(widget)->id & FGCLASS_LIST)) {
573 fgList *pl = static_cast<fgList*>(widget);
575 } else if (widgetType & PUCLASS_COMBOBOX) {
576 fgComboBox* combo = static_cast<fgComboBox*>(widget);
579 copy_to_pui(_propertyObjects[i]->node, widget);
581 } // of property objects iteration
585 FGDialog::applyValues (const char *objectName)
587 if (objectName && !objectName[0])
590 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
591 const string &name = _propertyObjects[i]->name;
592 if (objectName && name != objectName)
595 copy_from_pui(_propertyObjects[i]->object,
596 _propertyObjects[i]->node);
603 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
604 puObject *obj = _liveObjects[i]->object;
605 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
608 copy_to_pui(_liveObjects[i]->node, obj);
611 for (unsigned int j=0; j < _conditionalObjects.size(); ++j) {
612 _conditionalObjects[j]->update(this);
615 if (_needsRelayout) {
621 FGDialog::display (SGPropertyNode *props)
624 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
628 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
629 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
631 bool userx = props->hasValue("x");
632 bool usery = props->hasValue("y");
633 bool userw = props->hasValue("width");
634 bool userh = props->hasValue("height");
636 // Let the layout widget work in the same property subtree.
637 LayoutWidget wid(props);
639 SGPropertyNode *fontnode = props->getNode("font");
641 FGFontCache *fc = globals->get_fontcache();
642 _font = fc->get(fontnode);
644 _font = _gui->getDefaultFont();
646 wid.setDefaultFont(_font, int(_font->getPointSize()));
649 int px, py, savex, savey;
650 if (!userw || !userh)
651 wid.calcPrefSize(&pw, &ph);
652 pw = props->getIntValue("width", pw);
653 ph = props->getIntValue("height", ph);
654 px = savex = props->getIntValue("x", (screenw - pw) / 2);
655 py = savey = props->getIntValue("y", (screenh - ph) / 2);
657 // Negative x/y coordinates are interpreted as distance from the top/right
658 // corner rather than bottom/left.
660 px = screenw - pw + px;
662 py = screenh - ph + py;
664 // Define "x", "y", "width" and/or "height" in the property tree if they
665 // are not specified in the configuration file.
666 wid.layout(px, py, pw, ph);
668 // Use the dimension and location properties as specified in the
669 // configuration file or from the layout widget.
670 _object = makeObject(props, screenw, screenh);
672 // Remove automatically generated properties, so the layout looks
673 // the same next time around, or restore x and y to preserve negative coords.
675 props->setIntValue("x", savex);
677 props->removeChild("x");
680 props->setIntValue("y", savey);
682 props->removeChild("y");
684 if (!userw) props->removeChild("width");
685 if (!userh) props->removeChild("height");
690 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
691 << props->getStringValue("name", "[unnamed]")
692 << " does not contain a proper GUI definition");
697 FGDialog::makeObject (SGPropertyNode *props, int parentWidth, int parentHeight)
699 if (!props->getBoolValue("enabled", true))
702 bool presetSize = props->hasValue("width") && props->hasValue("height");
703 int width = props->getIntValue("width", parentWidth);
704 int height = props->getIntValue("height", parentHeight);
705 int x = props->getIntValue("x", (parentWidth - width) / 2);
706 int y = props->getIntValue("y", (parentHeight - height) / 2);
707 string type = props->getName();
712 if (type == "dialog") {
714 bool draggable = props->getBoolValue("draggable", true);
715 bool resizable = props->getBoolValue("resizable", false);
716 if (props->getBoolValue("modal", false))
717 obj = new puDialogBox(x, y);
719 obj = new fgPopup(x, y, resizable, draggable);
720 setupGroup(obj, props, width, height, true);
721 setColor(obj, props);
724 } else if (type == "group") {
725 puGroup *obj = new puGroup(x, y);
726 setupGroup(obj, props, width, height, false);
727 setColor(obj, props);
730 } else if (type == "frame") {
731 puGroup *obj = new puGroup(x, y);
732 setupGroup(obj, props, width, height, true);
733 setColor(obj, props);
736 } else if (type == "hrule" || type == "vrule") {
737 puFrame *obj = new puFrame(x, y, x + width, y + height);
738 obj->setBorderThickness(0);
739 setupObject(obj, props);
740 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
743 } else if (type == "list") {
744 int slider_width = props->getIntValue("slider", 20);
745 fgList *obj = new fgList(x, y, x + width, y + height, props, slider_width);
747 obj->setSize(width, height);
748 setupObject(obj, props);
749 setColor(obj, props);
752 } else if (type == "airport-list") {
753 AirportList *obj = new AirportList(x, y, x + width, y + height);
755 obj->setSize(width, height);
756 setupObject(obj, props);
757 setColor(obj, props);
760 } else if (type == "property-list") {
761 PropertyList *obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
763 obj->setSize(width, height);
764 setupObject(obj, props);
765 setColor(obj, props);
768 } else if (type == "input") {
769 puInput *obj = new puInput(x, y, x + width, y + height);
770 setupObject(obj, props);
771 setColor(obj, props, FOREGROUND|LABEL);
774 } else if (type == "text") {
775 puText *obj = new puText(x, y);
776 setupObject(obj, props);
778 // Layed-out objects need their size set, and non-layout ones
779 // get a different placement.
781 obj->setSize(width, height);
783 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
784 setColor(obj, props, LABEL);
787 } else if (type == "checkbox") {
789 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
790 setupObject(obj, props);
791 setColor(obj, props, FOREGROUND|LABEL);
794 } else if (type == "radio") {
796 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
797 setupObject(obj, props);
798 setColor(obj, props, FOREGROUND|LABEL);
801 } else if (type == "button") {
803 const char *legend = props->getStringValue("legend", "[none]");
804 if (props->getBoolValue("one-shot", true))
805 obj = new puOneShot(x, y, legend);
807 obj = new puButton(x, y, legend);
809 obj->setSize(width, height);
810 setupObject(obj, props);
811 setColor(obj, props);
814 } else if (type == "combo") {
815 fgComboBox *obj = new fgComboBox(x, y, x + width, y + height, props,
816 props->getBoolValue("editable", false));
817 setupObject(obj, props);
818 setColor(obj, props, EDITFIELD);
821 } else if (type == "slider") {
822 bool vertical = props->getBoolValue("vertical", false);
823 puSlider *obj = new puSlider(x, y, (vertical ? height : width), vertical);
824 obj->setMinValue(props->getFloatValue("min", 0.0));
825 obj->setMaxValue(props->getFloatValue("max", 1.0));
826 obj->setStepSize(props->getFloatValue("step"));
827 obj->setSliderFraction(props->getFloatValue("fraction"));
828 #if PLIB_VERSION > 185
829 obj->setPageStepSize(props->getFloatValue("pagestep"));
831 setupObject(obj, props);
833 obj->setSize(width, height);
834 setColor(obj, props, FOREGROUND|LABEL);
837 } else if (type == "dial") {
838 puDial *obj = new puDial(x, y, width);
839 obj->setMinValue(props->getFloatValue("min", 0.0));
840 obj->setMaxValue(props->getFloatValue("max", 1.0));
841 obj->setWrap(props->getBoolValue("wrap", true));
842 setupObject(obj, props);
843 setColor(obj, props, FOREGROUND|LABEL);
846 } else if (type == "textbox") {
847 int slider_width = props->getIntValue("slider", 20);
848 int wrap = props->getBoolValue("wrap", true);
849 #if PLIB_VERSION > 185
850 puaLargeInput *obj = new puaLargeInput(x, y,
851 x + width, x + height, 11, slider_width, wrap);
853 puaLargeInput *obj = new puaLargeInput(x, y,
854 x + width, x + height, 2, slider_width, wrap);
857 if (props->getBoolValue("editable"))
863 obj->setSize(width, height);
864 setupObject(obj, props);
865 setColor(obj, props, FOREGROUND|LABEL);
867 int top = props->getIntValue("top-line", 0);
868 obj->setTopLineInWindow(top < 0 ? unsigned(-1) >> 1 : top);
871 } else if (type == "select") {
872 fgSelectBox *obj = new fgSelectBox(x, y, x + width, y + height, props);
873 setupObject(obj, props);
874 setColor(obj, props, EDITFIELD);
882 FGDialog::setupObject (puObject *object, SGPropertyNode *props)
884 GUIInfo *info = new GUIInfo(this);
885 object->setUserData(info);
886 _info.push_back(info);
887 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
888 object->makeReturnDefault(props->getBoolValue("default"));
891 if (props->hasValue("legend")) {
892 info->legend = props->getStringValue("legend");
893 object->setLegend(info->legend.c_str());
896 if (props->hasValue("label")) {
897 info->label = props->getStringValue("label");
898 object->setLabel(info->label.c_str());
901 if (props->hasValue("border"))
902 object->setBorderThickness( props->getIntValue("border", 2) );
904 if (SGPropertyNode *nft = props->getNode("font", false)) {
905 FGFontCache *fc = globals->get_fontcache();
906 puFont *lfnt = fc->get(nft);
907 object->setLabelFont(*lfnt);
908 object->setLegendFont(*lfnt);
910 object->setLabelFont(*_font);
913 if (props->hasChild("visible")) {
914 ConditionalObject* cnd = new ConditionalObject("visible", object);
915 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("visible")));
916 _conditionalObjects.push_back(cnd);
919 if (props->hasChild("enable")) {
920 ConditionalObject* cnd = new ConditionalObject("enable", object);
921 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("enable")));
922 _conditionalObjects.push_back(cnd);
925 string type = props->getName();
926 if (type == "input" && props->getBoolValue("live"))
927 object->setDownCallback(action_callback);
929 if (type == "text") {
930 const char *format = props->getStringValue("format", 0);
932 info->fmt_type = validate_format(format);
933 if (info->fmt_type != f_INVALID)
934 info->format = format;
936 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
941 if (props->hasValue("property")) {
942 const char *name = props->getStringValue("name");
945 const char *propname = props->getStringValue("property");
946 SGPropertyNode_ptr node = fgGetNode(propname, true);
947 copy_to_pui(node, object);
949 PropertyObject *po = new PropertyObject(name, object, node);
950 _propertyObjects.push_back(po);
951 if (props->getBoolValue("live"))
952 _liveObjects.push_back(po);
955 SGPropertyNode *dest = fgGetNode("/sim/bindings/gui", true);
956 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
957 if (bindings.size() > 0) {
958 info->key = props->getIntValue("keynum", -1);
959 if (props->hasValue("key"))
960 info->key = getKeyCode(props->getStringValue("key", ""));
962 for (unsigned int i = 0; i < bindings.size(); i++) {
964 SGPropertyNode_ptr binding;
965 while (dest->getChild("binding", j))
968 const char *cmd = bindings[i]->getStringValue("command");
969 if (!strcmp(cmd, "nasal"))
970 bindings[i]->setStringValue("module", _module.c_str());
972 binding = dest->getChild("binding", j, true);
973 copyProperties(bindings[i], binding);
974 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
976 object->setCallback(action_callback);
981 FGDialog::setupGroup(puGroup *group, SGPropertyNode *props,
982 int width, int height, bool makeFrame)
984 setupObject(group, props);
987 puFrame* f = new puFrame(0, 0, width, height);
991 int nChildren = props->nChildren();
992 for (int i = 0; i < nChildren; i++)
993 makeObject(props->getChild(i), width, height);
998 FGDialog::setColor(puObject *object, SGPropertyNode *props, int which)
1000 string type = props->getName();
1003 if (type == "textbox" && props->getBoolValue("editable"))
1004 type += "-editable";
1006 FGColor c(_gui->getColor("background"));
1007 c.merge(_gui->getColor(type));
1008 c.merge(props->getNode("color"));
1010 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
1018 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
1019 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
1020 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
1021 { LABEL, PUCOL_LABEL, "label", "color-label" },
1022 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
1023 { MISC, PUCOL_MISC, "misc", "color-misc" },
1024 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
1027 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
1029 for (int i = 0; i < numcol; i++) {
1034 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
1035 if (which & pucol[i].mask)
1036 dirty |= c.merge(props->getNode("color"));
1038 if ((pucol[i].mask == LABEL) && !c.isValid())
1039 dirty |= c.merge(_gui->getColor("label"));
1041 dirty |= c.merge(props->getNode(pucol[i].cname));
1043 if (c.isValid() && dirty)
1044 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
1073 {"f10", PU_KEY_F10},
1074 {"f11", PU_KEY_F11},
1075 {"f12", PU_KEY_F12},
1076 {"left", PU_KEY_LEFT},
1078 {"right", PU_KEY_RIGHT},
1079 {"down", PU_KEY_DOWN},
1080 {"pageup", PU_KEY_PAGE_UP},
1081 {"pagedn", PU_KEY_PAGE_DOWN},
1082 {"home", PU_KEY_HOME},
1083 {"end", PU_KEY_END},
1084 {"insert", PU_KEY_INSERT},
1089 FGDialog::getKeyCode(const char *str)
1100 char *buf = new char[strlen(str) + 1];
1102 char *s = buf + strlen(buf);
1103 while (s > str && s[-1] == ' ')
1110 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
1111 s += 5, mod |= CTRL;
1112 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
1113 s += 6, mod |= SHIFT;
1114 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
1121 if (strlen(s) == 1 && isascii(*s)) {
1126 key = toupper(key) - '@';
1128 ; // Alt not propagated to the gui
1131 for (char *t = s; *t; t++)
1133 for (int i = 0; keymap[i].name; i++) {
1134 if (!strcmp(s, keymap[i].name)) {
1135 key = keymap[i].key;
1144 void
\fFGDialog::relayout()
1146 _needsRelayout = false;
1148 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
1149 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
1151 bool userx = _props->hasValue("x");
1152 bool usery = _props->hasValue("y");
1153 bool userw = _props->hasValue("width");
1154 bool userh = _props->hasValue("height");
1156 // Let the layout widget work in the same property subtree.
1157 LayoutWidget wid(_props);
1158 wid.setDefaultFont(_font, int(_font->getPointSize()));
1161 int px, py, savex, savey;
1162 if (!userw || !userh) {
1163 wid.calcPrefSize(&pw, &ph);
1166 pw = _props->getIntValue("width", pw);
1167 ph = _props->getIntValue("height", ph);
1168 px = savex = _props->getIntValue("x", (screenw - pw) / 2);
1169 py = savey = _props->getIntValue("y", (screenh - ph) / 2);
1171 // Negative x/y coordinates are interpreted as distance from the top/right
1172 // corner rather than bottom/left.
1173 if (userx && px < 0)
1174 px = screenw - pw + px;
1175 if (usery && py < 0)
1176 py = screenh - ph + py;
1178 // Define "x", "y", "width" and/or "height" in the property tree if they
1179 // are not specified in the configuration file.
1180 wid.layout(px, py, pw, ph);
1184 // Remove automatically generated properties, so the layout looks
1185 // the same next time around, or restore x and y to preserve negative coords.
1187 _props->setIntValue("x", savex);
1189 _props->removeChild("x");
1192 _props->setIntValue("y", savey);
1194 _props->removeChild("y");
1196 if (!userw) _props->removeChild("width");
1197 if (!userh) _props->removeChild("height");
1201 FGDialog::applySize(puObject *object)
1203 // compound plib widgets use setUserData() for internal purposes, so refuse
1204 // to descend into anything that has other bits set than the following
1205 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
1206 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
1207 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
1209 int type = object->getType();
1210 if ((type & PUCLASS_GROUP) && !(type & ~validUserData)) {
1211 puObject* c = ((puGroup *)object)->getFirstChild();
1212 for (; c != NULL; c = c->getNextObject()) {
1214 } // of child iteration
1215 } // of group object case
1217 GUIInfo *info = (GUIInfo *)object->getUserData();
1221 SGPropertyNode *n = info->node;
1223 SG_LOG(SG_GENERAL, SG_ALERT, "FGDialog::applySize: no props");
1227 int x = n->getIntValue("x");
1228 int y = n->getIntValue("y");
1229 int w = n->getIntValue("width", 4);
1230 int h = n->getIntValue("height", 4);
1231 object->setPosition(x, y);
1232 object->setSize(w, h);
1235 ////////////////////////////////////////////////////////////////////////
1236 // Implementation of FGDialog::PropertyObject.
1237 ////////////////////////////////////////////////////////////////////////
1239 FGDialog::PropertyObject::PropertyObject(const char *n,
1240 puObject *o, SGPropertyNode_ptr p) :
1250 ////////////////////////////////////////////////////////////////////////
1251 // Implementation of fgValueList and derived pui widgets
1252 ////////////////////////////////////////////////////////////////////////
1255 fgValueList::fgValueList(SGPropertyNode *p) :
1262 fgValueList::update()
1268 fgValueList::~fgValueList()
1274 fgValueList::make_list()
1276 SGPropertyNode_ptr values = _props;
1277 const char* vname = "value";
1279 if (_props->hasChild("properties")) {
1280 // dynamic values, read from a property's children
1281 const char* path = _props->getStringValue("properties");
1282 values = fgGetNode(path, true);
1285 if (_props->hasChild("property-name")) {
1286 vname = _props->getStringValue("property-name");
1289 vector<SGPropertyNode_ptr> value_nodes = values->getChildren(vname);
1290 _list = new char *[value_nodes.size() + 1];
1292 for (i = 0; i < value_nodes.size(); i++) {
1293 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
1299 fgValueList::destroy_list()
1301 for (int i = 0; _list[i] != 0; i++)
1312 fgValueList::update();
1313 int top = getTopItem();
1318 void fgComboBox::update()
1320 fgValueList::update();
1324 // end of dialog.cxx