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 setPosition(x + _dlgX - _startX, y + _dlgY - _startY);
301 } else if (_dragging) {
302 fgSetMouseCursor(_start_cursor);
308 int fgPopup::getHitObjects(puObject *object, int x, int y)
310 if (!object->isVisible())
314 if (object->getType() & PUCLASS_GROUP)
315 for (puObject *obj = ((puGroup *)object)->getFirstChild();
316 obj; obj = obj->getNextObject())
317 type |= getHitObjects(obj, x, y);
320 object->getAbsolutePosition(&cx, &cy);
321 object->getSize(&cw, &ch);
322 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch)
323 type |= object->getType();
327 void fgPopup::applySize(puObject *object)
329 // compound plib widgets use setUserData() for internal purposes, so refuse
330 // to descend into anything that has other bits set than the following
331 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
332 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
333 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
335 int type = object->getType();
336 if (type & PUCLASS_GROUP && !(type & ~validUserData))
337 for (puObject *obj = ((puGroup *)object)->getFirstChild();
338 obj; obj = obj->getNextObject())
341 GUIInfo *info = (GUIInfo *)object->getUserData();
345 SGPropertyNode *n = info->node;
347 SG_LOG(SG_GENERAL, SG_ALERT, "fgPopup::applySize: no props");
350 int x = n->getIntValue("x");
351 int y = n->getIntValue("y");
352 int w = n->getIntValue("width", 4);
353 int h = n->getIntValue("height", 4);
354 object->setPosition(x, y);
355 object->setSize(w, h);
358 ////////////////////////////////////////////////////////////////////////
360 void FGDialog::ConditionalObject::update(FGDialog* aDlg)
362 if (_name == "visible") {
363 bool wasVis = _pu->isVisible();
364 bool newVis = test();
366 if (newVis == wasVis) {
370 if (newVis) { // puObject needs a setVisible. Oh well.
375 } else if (_name == "enable") {
376 bool wasEnabled = _pu->isActive();
377 bool newEnable = test();
379 if (wasEnabled == newEnable) {
390 aDlg->setNeedsLayout();
392 \f////////////////////////////////////////////////////////////////////////
394 ////////////////////////////////////////////////////////////////////////
400 action_callback (puObject *object)
402 GUIInfo *info = (GUIInfo *)object->getUserData();
403 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
404 gui->setActiveDialog(info->dialog);
405 int nBindings = info->bindings.size();
406 for (int i = 0; i < nBindings; i++) {
407 info->bindings[i]->fire();
408 if (gui->getActiveDialog() == 0)
411 gui->setActiveDialog(0);
416 ////////////////////////////////////////////////////////////////////////
417 // Static helper functions.
418 ////////////////////////////////////////////////////////////////////////
421 * Copy a property value to a PUI object.
424 copy_to_pui (SGPropertyNode *node, puObject *object)
426 using namespace simgear;
427 GUIInfo *info = (GUIInfo *)object->getUserData();
429 SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
430 return; // this can't really happen
433 // Treat puText objects specially, so their "values" can be set
435 if (object->getType() & PUCLASS_TEXT) {
436 if (info->fmt_type != f_INVALID)
437 info->apply_format(node);
439 info->text = node->getStringValue();
441 object->setLabel(info->text.c_str());
445 switch (node->getType()) {
449 object->setValue(node->getIntValue());
453 object->setValue(node->getFloatValue());
456 info->text = node->getStringValue();
457 object->setValue(info->text.c_str());
464 copy_from_pui (puObject *object, SGPropertyNode *node)
466 using namespace simgear;
467 // puText objects are immutable, so should not be copied out
468 if (object->getType() & PUCLASS_TEXT)
471 switch (node->getType()) {
475 node->setIntValue(object->getIntegerValue());
479 node->setFloatValue(object->getFloatValue());
482 const char *s = object->getStringValue();
484 node->setStringValue(s);
491 ////////////////////////////////////////////////////////////////////////
492 // Implementation of FGDialog.
493 ////////////////////////////////////////////////////////////////////////
495 FGDialog::FGDialog (SGPropertyNode *props) :
497 _gui((NewGUI *)globals->get_subsystem("gui")),
499 _needsRelayout(false)
501 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
503 SGPropertyNode *nasal = props->getNode("nasal");
505 _nasal_close = nasal->getNode("close");
506 SGPropertyNode *open = nasal->getNode("open");
508 const char *s = open->getStringValue();
509 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
511 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
517 FGDialog::~FGDialog ()
520 _object->getAbsolutePosition(&x, &y);
521 _props->setIntValue("lastx", x);
522 _props->setIntValue("lasty", y);
524 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
527 const char *s = _nasal_close->getStringValue();
528 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
530 nas->deleteModule(_module.c_str());
533 puDeleteObject(_object);
536 // Delete all the info objects we
537 // were forced to keep around because
538 // PUI cannot delete its own user data.
539 for (i = 0; i < _info.size(); i++) {
540 delete (GUIInfo *)_info[i];
543 // Finally, delete the property links.
544 for (i = 0; i < _propertyObjects.size(); i++) {
545 delete _propertyObjects[i];
546 _propertyObjects[i] = 0;
551 FGDialog::updateValues (const char *objectName)
553 if (objectName && !objectName[0])
556 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
557 const string &name = _propertyObjects[i]->name;
558 if (objectName && name != objectName) {
562 puObject *widget = _propertyObjects[i]->object;
563 int widgetType = widget->getType();
564 if ((widgetType & PUCLASS_LIST) && (dynamic_cast<GUI_ID *>(widget)->id & FGCLASS_LIST)) {
565 fgList *pl = static_cast<fgList*>(widget);
567 } else if (widgetType & PUCLASS_COMBOBOX) {
568 fgComboBox* combo = static_cast<fgComboBox*>(widget);
571 copy_to_pui(_propertyObjects[i]->node, widget);
573 } // of property objects iteration
577 FGDialog::applyValues (const char *objectName)
579 if (objectName && !objectName[0])
582 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
583 const string &name = _propertyObjects[i]->name;
584 if (objectName && name != objectName)
587 copy_from_pui(_propertyObjects[i]->object,
588 _propertyObjects[i]->node);
595 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
596 puObject *obj = _liveObjects[i]->object;
597 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
600 copy_to_pui(_liveObjects[i]->node, obj);
603 for (unsigned int j=0; j < _conditionalObjects.size(); ++j) {
604 _conditionalObjects[j]->update(this);
607 if (_needsRelayout) {
613 FGDialog::display (SGPropertyNode *props)
616 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
620 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
621 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
623 bool userx = props->hasValue("x");
624 bool usery = props->hasValue("y");
625 bool userw = props->hasValue("width");
626 bool userh = props->hasValue("height");
628 // Let the layout widget work in the same property subtree.
629 LayoutWidget wid(props);
631 SGPropertyNode *fontnode = props->getNode("font");
633 FGFontCache *fc = globals->get_fontcache();
634 _font = fc->get(fontnode);
636 _font = _gui->getDefaultFont();
638 wid.setDefaultFont(_font, int(_font->getPointSize()));
641 int px, py, savex, savey;
642 if (!userw || !userh)
643 wid.calcPrefSize(&pw, &ph);
644 pw = props->getIntValue("width", pw);
645 ph = props->getIntValue("height", ph);
646 px = savex = props->getIntValue("x", (screenw - pw) / 2);
647 py = savey = props->getIntValue("y", (screenh - ph) / 2);
649 // Negative x/y coordinates are interpreted as distance from the top/right
650 // corner rather than bottom/left.
652 px = screenw - pw + px;
654 py = screenh - ph + py;
656 // Define "x", "y", "width" and/or "height" in the property tree if they
657 // are not specified in the configuration file.
658 wid.layout(px, py, pw, ph);
660 // Use the dimension and location properties as specified in the
661 // configuration file or from the layout widget.
662 _object = makeObject(props, screenw, screenh);
664 // Remove automatically generated properties, so the layout looks
665 // the same next time around, or restore x and y to preserve negative coords.
667 props->setIntValue("x", savex);
669 props->removeChild("x");
672 props->setIntValue("y", savey);
674 props->removeChild("y");
676 if (!userw) props->removeChild("width");
677 if (!userh) props->removeChild("height");
682 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
683 << props->getStringValue("name", "[unnamed]")
684 << " does not contain a proper GUI definition");
689 FGDialog::makeObject (SGPropertyNode *props, int parentWidth, int parentHeight)
691 if (!props->getBoolValue("enabled", true))
694 bool presetSize = props->hasValue("width") && props->hasValue("height");
695 int width = props->getIntValue("width", parentWidth);
696 int height = props->getIntValue("height", parentHeight);
697 int x = props->getIntValue("x", (parentWidth - width) / 2);
698 int y = props->getIntValue("y", (parentHeight - height) / 2);
699 string type = props->getName();
704 if (type == "dialog") {
706 bool draggable = props->getBoolValue("draggable", true);
707 bool resizable = props->getBoolValue("resizable", false);
708 if (props->getBoolValue("modal", false))
709 obj = new puDialogBox(x, y);
711 obj = new fgPopup(x, y, resizable, draggable);
712 setupGroup(obj, props, width, height, true);
713 setColor(obj, props);
716 } else if (type == "group") {
717 puGroup *obj = new puGroup(x, y);
718 setupGroup(obj, props, width, height, false);
719 setColor(obj, props);
722 } else if (type == "frame") {
723 puGroup *obj = new puGroup(x, y);
724 setupGroup(obj, props, width, height, true);
725 setColor(obj, props);
728 } else if (type == "hrule" || type == "vrule") {
729 puFrame *obj = new puFrame(x, y, x + width, y + height);
730 obj->setBorderThickness(0);
731 setupObject(obj, props);
732 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
735 } else if (type == "list") {
736 int slider_width = props->getIntValue("slider", 20);
737 fgList *obj = new fgList(x, y, x + width, y + height, props, slider_width);
739 obj->setSize(width, height);
740 setupObject(obj, props);
741 setColor(obj, props);
744 } else if (type == "airport-list") {
745 AirportList *obj = new AirportList(x, y, x + width, y + height);
747 obj->setSize(width, height);
748 setupObject(obj, props);
749 setColor(obj, props);
752 } else if (type == "property-list") {
753 PropertyList *obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
755 obj->setSize(width, height);
756 setupObject(obj, props);
757 setColor(obj, props);
760 } else if (type == "input") {
761 puInput *obj = new puInput(x, y, x + width, y + height);
762 setupObject(obj, props);
763 setColor(obj, props, FOREGROUND|LABEL);
766 } else if (type == "text") {
767 puText *obj = new puText(x, y);
768 setupObject(obj, props);
770 // Layed-out objects need their size set, and non-layout ones
771 // get a different placement.
773 obj->setSize(width, height);
775 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
776 setColor(obj, props, LABEL);
779 } else if (type == "checkbox") {
781 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
782 setupObject(obj, props);
783 setColor(obj, props, FOREGROUND|LABEL);
786 } else if (type == "radio") {
788 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
789 setupObject(obj, props);
790 setColor(obj, props, FOREGROUND|LABEL);
793 } else if (type == "button") {
795 const char *legend = props->getStringValue("legend", "[none]");
796 if (props->getBoolValue("one-shot", true))
797 obj = new puOneShot(x, y, legend);
799 obj = new puButton(x, y, legend);
801 obj->setSize(width, height);
802 setupObject(obj, props);
803 setColor(obj, props);
806 } else if (type == "combo") {
807 fgComboBox *obj = new fgComboBox(x, y, x + width, y + height, props,
808 props->getBoolValue("editable", false));
809 setupObject(obj, props);
810 setColor(obj, props, EDITFIELD);
813 } else if (type == "slider") {
814 bool vertical = props->getBoolValue("vertical", false);
815 puSlider *obj = new puSlider(x, y, (vertical ? height : width), vertical);
816 obj->setMinValue(props->getFloatValue("min", 0.0));
817 obj->setMaxValue(props->getFloatValue("max", 1.0));
818 obj->setStepSize(props->getFloatValue("step"));
819 obj->setSliderFraction(props->getFloatValue("fraction"));
820 #if PLIB_VERSION > 185
821 obj->setPageStepSize(props->getFloatValue("pagestep"));
823 setupObject(obj, props);
825 obj->setSize(width, height);
826 setColor(obj, props, FOREGROUND|LABEL);
829 } else if (type == "dial") {
830 puDial *obj = new puDial(x, y, width);
831 obj->setMinValue(props->getFloatValue("min", 0.0));
832 obj->setMaxValue(props->getFloatValue("max", 1.0));
833 obj->setWrap(props->getBoolValue("wrap", true));
834 setupObject(obj, props);
835 setColor(obj, props, FOREGROUND|LABEL);
838 } else if (type == "textbox") {
839 int slider_width = props->getIntValue("slider", 20);
840 int wrap = props->getBoolValue("wrap", true);
841 #if PLIB_VERSION > 185
842 puaLargeInput *obj = new puaLargeInput(x, y,
843 x + width, x + height, 11, slider_width, wrap);
845 puaLargeInput *obj = new puaLargeInput(x, y,
846 x + width, x + height, 2, slider_width, wrap);
849 if (props->getBoolValue("editable"))
855 obj->setSize(width, height);
856 setupObject(obj, props);
857 setColor(obj, props, FOREGROUND|LABEL);
859 int top = props->getIntValue("top-line", 0);
860 obj->setTopLineInWindow(top < 0 ? unsigned(-1) >> 1 : top);
863 } else if (type == "select") {
864 fgSelectBox *obj = new fgSelectBox(x, y, x + width, y + height, props);
865 setupObject(obj, props);
866 setColor(obj, props, EDITFIELD);
874 FGDialog::setupObject (puObject *object, SGPropertyNode *props)
876 GUIInfo *info = new GUIInfo(this);
877 object->setUserData(info);
878 _info.push_back(info);
879 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
880 object->makeReturnDefault(props->getBoolValue("default"));
883 if (props->hasValue("legend")) {
884 info->legend = props->getStringValue("legend");
885 object->setLegend(info->legend.c_str());
888 if (props->hasValue("label")) {
889 info->label = props->getStringValue("label");
890 object->setLabel(info->label.c_str());
893 if (props->hasValue("border"))
894 object->setBorderThickness( props->getIntValue("border", 2) );
896 if (SGPropertyNode *nft = props->getNode("font", false)) {
897 FGFontCache *fc = globals->get_fontcache();
898 puFont *lfnt = fc->get(nft);
899 object->setLabelFont(*lfnt);
900 object->setLegendFont(*lfnt);
902 object->setLabelFont(*_font);
905 if (props->hasChild("visible")) {
906 ConditionalObject* cnd = new ConditionalObject("visible", object);
907 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("visible")));
908 _conditionalObjects.push_back(cnd);
911 if (props->hasChild("enable")) {
912 ConditionalObject* cnd = new ConditionalObject("enable", object);
913 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("enable")));
914 _conditionalObjects.push_back(cnd);
917 string type = props->getName();
918 if (type == "input" && props->getBoolValue("live"))
919 object->setDownCallback(action_callback);
921 if (type == "text") {
922 const char *format = props->getStringValue("format", 0);
924 info->fmt_type = validate_format(format);
925 if (info->fmt_type != f_INVALID)
926 info->format = format;
928 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
933 if (props->hasValue("property")) {
934 const char *name = props->getStringValue("name");
937 const char *propname = props->getStringValue("property");
938 SGPropertyNode_ptr node = fgGetNode(propname, true);
939 copy_to_pui(node, object);
941 PropertyObject *po = new PropertyObject(name, object, node);
942 _propertyObjects.push_back(po);
943 if (props->getBoolValue("live"))
944 _liveObjects.push_back(po);
947 SGPropertyNode *dest = fgGetNode("/sim/bindings/gui", true);
948 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
949 if (bindings.size() > 0) {
950 info->key = props->getIntValue("keynum", -1);
951 if (props->hasValue("key"))
952 info->key = getKeyCode(props->getStringValue("key", ""));
954 for (unsigned int i = 0; i < bindings.size(); i++) {
956 SGPropertyNode_ptr binding;
957 while (dest->getChild("binding", j))
960 const char *cmd = bindings[i]->getStringValue("command");
961 if (!strcmp(cmd, "nasal"))
962 bindings[i]->setStringValue("module", _module.c_str());
964 binding = dest->getChild("binding", j, true);
965 copyProperties(bindings[i], binding);
966 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
968 object->setCallback(action_callback);
973 FGDialog::setupGroup(puGroup *group, SGPropertyNode *props,
974 int width, int height, bool makeFrame)
976 setupObject(group, props);
979 puFrame* f = new puFrame(0, 0, width, height);
983 int nChildren = props->nChildren();
984 for (int i = 0; i < nChildren; i++)
985 makeObject(props->getChild(i), width, height);
990 FGDialog::setColor(puObject *object, SGPropertyNode *props, int which)
992 string type = props->getName();
995 if (type == "textbox" && props->getBoolValue("editable"))
998 FGColor c(_gui->getColor("background"));
999 c.merge(_gui->getColor(type));
1000 c.merge(props->getNode("color"));
1002 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
1010 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
1011 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
1012 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
1013 { LABEL, PUCOL_LABEL, "label", "color-label" },
1014 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
1015 { MISC, PUCOL_MISC, "misc", "color-misc" },
1016 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
1019 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
1021 for (int i = 0; i < numcol; i++) {
1026 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
1027 if (which & pucol[i].mask)
1028 dirty |= c.merge(props->getNode("color"));
1030 if ((pucol[i].mask == LABEL) && !c.isValid())
1031 dirty |= c.merge(_gui->getColor("label"));
1033 dirty |= c.merge(props->getNode(pucol[i].cname));
1035 if (c.isValid() && dirty)
1036 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
1065 {"f10", PU_KEY_F10},
1066 {"f11", PU_KEY_F11},
1067 {"f12", PU_KEY_F12},
1068 {"left", PU_KEY_LEFT},
1070 {"right", PU_KEY_RIGHT},
1071 {"down", PU_KEY_DOWN},
1072 {"pageup", PU_KEY_PAGE_UP},
1073 {"pagedn", PU_KEY_PAGE_DOWN},
1074 {"home", PU_KEY_HOME},
1075 {"end", PU_KEY_END},
1076 {"insert", PU_KEY_INSERT},
1081 FGDialog::getKeyCode(const char *str)
1092 char *buf = new char[strlen(str) + 1];
1094 char *s = buf + strlen(buf);
1095 while (s > str && s[-1] == ' ')
1102 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
1103 s += 5, mod |= CTRL;
1104 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
1105 s += 6, mod |= SHIFT;
1106 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
1113 if (strlen(s) == 1 && isascii(*s)) {
1118 key = toupper(key) - '@';
1120 ; // Alt not propagated to the gui
1123 for (char *t = s; *t; t++)
1125 for (int i = 0; keymap[i].name; i++) {
1126 if (!strcmp(s, keymap[i].name)) {
1127 key = keymap[i].key;
1136 void
\fFGDialog::relayout()
1138 _needsRelayout = false;
1140 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
1141 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
1143 bool userx = _props->hasValue("x");
1144 bool usery = _props->hasValue("y");
1145 bool userw = _props->hasValue("width");
1146 bool userh = _props->hasValue("height");
1148 // Let the layout widget work in the same property subtree.
1149 LayoutWidget wid(_props);
1150 wid.setDefaultFont(_font, int(_font->getPointSize()));
1153 int px, py, savex, savey;
1154 if (!userw || !userh) {
1155 wid.calcPrefSize(&pw, &ph);
1158 pw = _props->getIntValue("width", pw);
1159 ph = _props->getIntValue("height", ph);
1160 px = savex = _props->getIntValue("x", (screenw - pw) / 2);
1161 py = savey = _props->getIntValue("y", (screenh - ph) / 2);
1163 // Negative x/y coordinates are interpreted as distance from the top/right
1164 // corner rather than bottom/left.
1165 if (userx && px < 0)
1166 px = screenw - pw + px;
1167 if (usery && py < 0)
1168 py = screenh - ph + py;
1170 // Define "x", "y", "width" and/or "height" in the property tree if they
1171 // are not specified in the configuration file.
1172 wid.layout(px, py, pw, ph);
1176 // Remove automatically generated properties, so the layout looks
1177 // the same next time around, or restore x and y to preserve negative coords.
1179 _props->setIntValue("x", savex);
1181 _props->removeChild("x");
1184 _props->setIntValue("y", savey);
1186 _props->removeChild("y");
1188 if (!userw) _props->removeChild("width");
1189 if (!userh) _props->removeChild("height");
1193 FGDialog::applySize(puObject *object)
1195 // compound plib widgets use setUserData() for internal purposes, so refuse
1196 // to descend into anything that has other bits set than the following
1197 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
1198 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
1199 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
1201 int type = object->getType();
1202 if ((type & PUCLASS_GROUP) && !(type & ~validUserData)) {
1203 puObject* c = ((puGroup *)object)->getFirstChild();
1204 for (; c != NULL; c = c->getNextObject()) {
1206 } // of child iteration
1207 } // of group object case
1209 GUIInfo *info = (GUIInfo *)object->getUserData();
1213 SGPropertyNode *n = info->node;
1215 SG_LOG(SG_GENERAL, SG_ALERT, "FGDialog::applySize: no props");
1219 int x = n->getIntValue("x");
1220 int y = n->getIntValue("y");
1221 int w = n->getIntValue("width", 4);
1222 int h = n->getIntValue("height", 4);
1223 object->setPosition(x, y);
1224 object->setSize(w, h);
1227 ////////////////////////////////////////////////////////////////////////
1228 // Implementation of FGDialog::PropertyObject.
1229 ////////////////////////////////////////////////////////////////////////
1231 FGDialog::PropertyObject::PropertyObject(const char *n,
1232 puObject *o, SGPropertyNode_ptr p) :
1242 ////////////////////////////////////////////////////////////////////////
1243 // Implementation of fgValueList and derived pui widgets
1244 ////////////////////////////////////////////////////////////////////////
1247 fgValueList::fgValueList(SGPropertyNode *p) :
1254 fgValueList::update()
1260 fgValueList::~fgValueList()
1266 fgValueList::make_list()
1268 SGPropertyNode_ptr values = _props;
1269 const char* vname = "value";
1271 if (_props->hasChild("properties")) {
1272 // dynamic values, read from a property's children
1273 const char* path = _props->getStringValue("properties");
1274 values = fgGetNode(path, true);
1277 if (_props->hasChild("property-name")) {
1278 vname = _props->getStringValue("property-name");
1281 vector<SGPropertyNode_ptr> value_nodes = values->getChildren(vname);
1282 _list = new char *[value_nodes.size() + 1];
1284 for (i = 0; i < value_nodes.size(); i++) {
1285 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
1291 fgValueList::destroy_list()
1293 for (int i = 0; _list[i] != 0; i++)
1304 fgValueList::update();
1305 int top = getTopItem();
1310 void fgComboBox::update()
1312 fgValueList::update();
1316 // end of dialog.cxx