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"
16 #include "WaypointList.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) {
573 GUI_ID* gui_id = dynamic_cast<GUI_ID *>(widget);
574 if (gui_id && (gui_id->id & FGCLASS_LIST)) {
575 fgList *pl = static_cast<fgList*>(widget);
578 copy_to_pui(_propertyObjects[i]->node, widget);
580 } else if (widgetType & PUCLASS_COMBOBOX) {
581 fgComboBox* combo = static_cast<fgComboBox*>(widget);
584 copy_to_pui(_propertyObjects[i]->node, widget);
586 } // of property objects iteration
590 FGDialog::applyValues (const char *objectName)
592 if (objectName && !objectName[0])
595 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
596 const string &name = _propertyObjects[i]->name;
597 if (objectName && name != objectName)
600 copy_from_pui(_propertyObjects[i]->object,
601 _propertyObjects[i]->node);
608 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
609 puObject *obj = _liveObjects[i]->object;
610 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
613 copy_to_pui(_liveObjects[i]->node, obj);
616 for (unsigned int j=0; j < _conditionalObjects.size(); ++j) {
617 _conditionalObjects[j]->update(this);
620 if (_needsRelayout) {
626 FGDialog::display (SGPropertyNode *props)
629 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
633 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
634 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
636 bool userx = props->hasValue("x");
637 bool usery = props->hasValue("y");
638 bool userw = props->hasValue("width");
639 bool userh = props->hasValue("height");
641 // Let the layout widget work in the same property subtree.
642 LayoutWidget wid(props);
644 SGPropertyNode *fontnode = props->getNode("font");
646 FGFontCache *fc = globals->get_fontcache();
647 _font = fc->get(fontnode);
649 _font = _gui->getDefaultFont();
651 wid.setDefaultFont(_font, int(_font->getPointSize()));
654 int px, py, savex, savey;
655 if (!userw || !userh)
656 wid.calcPrefSize(&pw, &ph);
657 pw = props->getIntValue("width", pw);
658 ph = props->getIntValue("height", ph);
659 px = savex = props->getIntValue("x", (screenw - pw) / 2);
660 py = savey = props->getIntValue("y", (screenh - ph) / 2);
662 // Negative x/y coordinates are interpreted as distance from the top/right
663 // corner rather than bottom/left.
665 px = screenw - pw + px;
667 py = screenh - ph + py;
669 // Define "x", "y", "width" and/or "height" in the property tree if they
670 // are not specified in the configuration file.
671 wid.layout(px, py, pw, ph);
673 // Use the dimension and location properties as specified in the
674 // configuration file or from the layout widget.
675 _object = makeObject(props, screenw, screenh);
677 // Remove automatically generated properties, so the layout looks
678 // the same next time around, or restore x and y to preserve negative coords.
680 props->setIntValue("x", savex);
682 props->removeChild("x");
685 props->setIntValue("y", savey);
687 props->removeChild("y");
689 if (!userw) props->removeChild("width");
690 if (!userh) props->removeChild("height");
695 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
696 << props->getStringValue("name", "[unnamed]")
697 << " does not contain a proper GUI definition");
702 FGDialog::makeObject (SGPropertyNode *props, int parentWidth, int parentHeight)
704 if (!props->getBoolValue("enabled", true))
707 bool presetSize = props->hasValue("width") && props->hasValue("height");
708 int width = props->getIntValue("width", parentWidth);
709 int height = props->getIntValue("height", parentHeight);
710 int x = props->getIntValue("x", (parentWidth - width) / 2);
711 int y = props->getIntValue("y", (parentHeight - height) / 2);
712 string type = props->getName();
717 if (type == "dialog") {
719 bool draggable = props->getBoolValue("draggable", true);
720 bool resizable = props->getBoolValue("resizable", false);
721 if (props->getBoolValue("modal", false))
722 obj = new puDialogBox(x, y);
724 obj = new fgPopup(x, y, resizable, draggable);
725 setupGroup(obj, props, width, height, true);
726 setColor(obj, props);
729 } else if (type == "group") {
730 puGroup *obj = new puGroup(x, y);
731 setupGroup(obj, props, width, height, false);
732 setColor(obj, props);
735 } else if (type == "frame") {
736 puGroup *obj = new puGroup(x, y);
737 setupGroup(obj, props, width, height, true);
738 setColor(obj, props);
741 } else if (type == "hrule" || type == "vrule") {
742 puFrame *obj = new puFrame(x, y, x + width, y + height);
743 obj->setBorderThickness(0);
744 setupObject(obj, props);
745 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
748 } else if (type == "list") {
749 int slider_width = props->getIntValue("slider", 20);
750 fgList *obj = new fgList(x, y, x + width, y + height, props, slider_width);
752 obj->setSize(width, height);
753 setupObject(obj, props);
754 setColor(obj, props);
757 } else if (type == "airport-list") {
758 AirportList *obj = new AirportList(x, y, x + width, y + height);
760 obj->setSize(width, height);
761 setupObject(obj, props);
762 setColor(obj, props);
765 } else if (type == "property-list") {
766 PropertyList *obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
768 obj->setSize(width, height);
769 setupObject(obj, props);
770 setColor(obj, props);
773 } else if (type == "input") {
774 puInput *obj = new puInput(x, y, x + width, y + height);
775 setupObject(obj, props);
776 setColor(obj, props, FOREGROUND|LABEL);
779 } else if (type == "text") {
780 puText *obj = new puText(x, y);
781 setupObject(obj, props);
783 // Layed-out objects need their size set, and non-layout ones
784 // get a different placement.
786 obj->setSize(width, height);
788 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
789 setColor(obj, props, LABEL);
792 } else if (type == "checkbox") {
794 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
795 setupObject(obj, props);
796 setColor(obj, props, FOREGROUND|LABEL);
799 } else if (type == "radio") {
801 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
802 setupObject(obj, props);
803 setColor(obj, props, FOREGROUND|LABEL);
806 } else if (type == "button") {
808 const char *legend = props->getStringValue("legend", "[none]");
809 if (props->getBoolValue("one-shot", true))
810 obj = new puOneShot(x, y, legend);
812 obj = new puButton(x, y, legend);
814 obj->setSize(width, height);
815 setupObject(obj, props);
816 setColor(obj, props);
819 } else if (type == "combo") {
820 fgComboBox *obj = new fgComboBox(x, y, x + width, y + height, props,
821 props->getBoolValue("editable", false));
822 setupObject(obj, props);
823 setColor(obj, props, EDITFIELD);
826 } else if (type == "slider") {
827 bool vertical = props->getBoolValue("vertical", false);
828 puSlider *obj = new puSlider(x, y, (vertical ? height : width), vertical);
829 obj->setMinValue(props->getFloatValue("min", 0.0));
830 obj->setMaxValue(props->getFloatValue("max", 1.0));
831 obj->setStepSize(props->getFloatValue("step"));
832 obj->setSliderFraction(props->getFloatValue("fraction"));
833 #if PLIB_VERSION > 185
834 obj->setPageStepSize(props->getFloatValue("pagestep"));
836 setupObject(obj, props);
838 obj->setSize(width, height);
839 setColor(obj, props, FOREGROUND|LABEL);
842 } else if (type == "dial") {
843 puDial *obj = new puDial(x, y, width);
844 obj->setMinValue(props->getFloatValue("min", 0.0));
845 obj->setMaxValue(props->getFloatValue("max", 1.0));
846 obj->setWrap(props->getBoolValue("wrap", true));
847 setupObject(obj, props);
848 setColor(obj, props, FOREGROUND|LABEL);
851 } else if (type == "textbox") {
852 int slider_width = props->getIntValue("slider", 20);
853 int wrap = props->getBoolValue("wrap", true);
854 #if PLIB_VERSION > 185
855 puaLargeInput *obj = new puaLargeInput(x, y,
856 x + width, x + height, 11, slider_width, wrap);
858 puaLargeInput *obj = new puaLargeInput(x, y,
859 x + width, x + height, 2, slider_width, wrap);
862 if (props->getBoolValue("editable"))
868 obj->setSize(width, height);
869 setupObject(obj, props);
870 setColor(obj, props, FOREGROUND|LABEL);
872 int top = props->getIntValue("top-line", 0);
873 obj->setTopLineInWindow(top < 0 ? unsigned(-1) >> 1 : top);
876 } else if (type == "select") {
877 fgSelectBox *obj = new fgSelectBox(x, y, x + width, y + height, props);
878 setupObject(obj, props);
879 setColor(obj, props, EDITFIELD);
881 } else if (type == "waypointlist") {
882 ScrolledWaypointList* obj = new ScrolledWaypointList(x, y, width, height);
883 setupObject(obj, props);
891 FGDialog::setupObject (puObject *object, SGPropertyNode *props)
893 GUIInfo *info = new GUIInfo(this);
894 object->setUserData(info);
895 _info.push_back(info);
896 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
897 object->makeReturnDefault(props->getBoolValue("default"));
900 if (props->hasValue("legend")) {
901 info->legend = props->getStringValue("legend");
902 object->setLegend(info->legend.c_str());
905 if (props->hasValue("label")) {
906 info->label = props->getStringValue("label");
907 object->setLabel(info->label.c_str());
910 if (props->hasValue("border"))
911 object->setBorderThickness( props->getIntValue("border", 2) );
913 if (SGPropertyNode *nft = props->getNode("font", false)) {
914 FGFontCache *fc = globals->get_fontcache();
915 puFont *lfnt = fc->get(nft);
916 object->setLabelFont(*lfnt);
917 object->setLegendFont(*lfnt);
919 object->setLabelFont(*_font);
922 if (props->hasChild("visible")) {
923 ConditionalObject* cnd = new ConditionalObject("visible", object);
924 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("visible")));
925 _conditionalObjects.push_back(cnd);
928 if (props->hasChild("enable")) {
929 ConditionalObject* cnd = new ConditionalObject("enable", object);
930 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("enable")));
931 _conditionalObjects.push_back(cnd);
934 string type = props->getName();
935 if (type == "input" && props->getBoolValue("live"))
936 object->setDownCallback(action_callback);
938 if (type == "text") {
939 const char *format = props->getStringValue("format", 0);
941 info->fmt_type = validate_format(format);
942 if (info->fmt_type != f_INVALID)
943 info->format = format;
945 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
950 if (props->hasValue("property")) {
951 const char *name = props->getStringValue("name");
954 const char *propname = props->getStringValue("property");
955 SGPropertyNode_ptr node = fgGetNode(propname, true);
956 copy_to_pui(node, object);
958 PropertyObject *po = new PropertyObject(name, object, node);
959 _propertyObjects.push_back(po);
960 if (props->getBoolValue("live"))
961 _liveObjects.push_back(po);
964 SGPropertyNode *dest = fgGetNode("/sim/bindings/gui", true);
965 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
966 if (bindings.size() > 0) {
967 info->key = props->getIntValue("keynum", -1);
968 if (props->hasValue("key"))
969 info->key = getKeyCode(props->getStringValue("key", ""));
971 for (unsigned int i = 0; i < bindings.size(); i++) {
973 SGPropertyNode_ptr binding;
974 while (dest->getChild("binding", j))
977 const char *cmd = bindings[i]->getStringValue("command");
978 if (!strcmp(cmd, "nasal"))
979 bindings[i]->setStringValue("module", _module.c_str());
981 binding = dest->getChild("binding", j, true);
982 copyProperties(bindings[i], binding);
983 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
985 object->setCallback(action_callback);
990 FGDialog::setupGroup(puGroup *group, SGPropertyNode *props,
991 int width, int height, bool makeFrame)
993 setupObject(group, props);
996 puFrame* f = new puFrame(0, 0, width, height);
1000 int nChildren = props->nChildren();
1001 for (int i = 0; i < nChildren; i++)
1002 makeObject(props->getChild(i), width, height);
1007 FGDialog::setColor(puObject *object, SGPropertyNode *props, int which)
1009 string type = props->getName();
1012 if (type == "textbox" && props->getBoolValue("editable"))
1013 type += "-editable";
1015 FGColor c(_gui->getColor("background"));
1016 c.merge(_gui->getColor(type));
1017 c.merge(props->getNode("color"));
1019 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
1027 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
1028 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
1029 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
1030 { LABEL, PUCOL_LABEL, "label", "color-label" },
1031 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
1032 { MISC, PUCOL_MISC, "misc", "color-misc" },
1033 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
1036 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
1038 for (int i = 0; i < numcol; i++) {
1043 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
1044 if (which & pucol[i].mask)
1045 dirty |= c.merge(props->getNode("color"));
1047 if ((pucol[i].mask == LABEL) && !c.isValid())
1048 dirty |= c.merge(_gui->getColor("label"));
1050 dirty |= c.merge(props->getNode(pucol[i].cname));
1052 if (c.isValid() && dirty)
1053 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
1082 {"f10", PU_KEY_F10},
1083 {"f11", PU_KEY_F11},
1084 {"f12", PU_KEY_F12},
1085 {"left", PU_KEY_LEFT},
1087 {"right", PU_KEY_RIGHT},
1088 {"down", PU_KEY_DOWN},
1089 {"pageup", PU_KEY_PAGE_UP},
1090 {"pagedn", PU_KEY_PAGE_DOWN},
1091 {"home", PU_KEY_HOME},
1092 {"end", PU_KEY_END},
1093 {"insert", PU_KEY_INSERT},
1098 FGDialog::getKeyCode(const char *str)
1109 char *buf = new char[strlen(str) + 1];
1111 char *s = buf + strlen(buf);
1112 while (s > str && s[-1] == ' ')
1119 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
1120 s += 5, mod |= CTRL;
1121 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
1122 s += 6, mod |= SHIFT;
1123 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
1130 if (strlen(s) == 1 && isascii(*s)) {
1135 key = toupper(key) - '@';
1137 ; // Alt not propagated to the gui
1140 for (char *t = s; *t; t++)
1142 for (int i = 0; keymap[i].name; i++) {
1143 if (!strcmp(s, keymap[i].name)) {
1144 key = keymap[i].key;
1153 void
\fFGDialog::relayout()
1155 _needsRelayout = false;
1157 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
1158 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
1160 bool userx = _props->hasValue("x");
1161 bool usery = _props->hasValue("y");
1162 bool userw = _props->hasValue("width");
1163 bool userh = _props->hasValue("height");
1165 // Let the layout widget work in the same property subtree.
1166 LayoutWidget wid(_props);
1167 wid.setDefaultFont(_font, int(_font->getPointSize()));
1170 int px, py, savex, savey;
1171 if (!userw || !userh) {
1172 wid.calcPrefSize(&pw, &ph);
1175 pw = _props->getIntValue("width", pw);
1176 ph = _props->getIntValue("height", ph);
1177 px = savex = _props->getIntValue("x", (screenw - pw) / 2);
1178 py = savey = _props->getIntValue("y", (screenh - ph) / 2);
1180 // Negative x/y coordinates are interpreted as distance from the top/right
1181 // corner rather than bottom/left.
1182 if (userx && px < 0)
1183 px = screenw - pw + px;
1184 if (usery && py < 0)
1185 py = screenh - ph + py;
1187 // Define "x", "y", "width" and/or "height" in the property tree if they
1188 // are not specified in the configuration file.
1189 wid.layout(px, py, pw, ph);
1193 // Remove automatically generated properties, so the layout looks
1194 // the same next time around, or restore x and y to preserve negative coords.
1196 _props->setIntValue("x", savex);
1198 _props->removeChild("x");
1201 _props->setIntValue("y", savey);
1203 _props->removeChild("y");
1205 if (!userw) _props->removeChild("width");
1206 if (!userh) _props->removeChild("height");
1210 FGDialog::applySize(puObject *object)
1212 // compound plib widgets use setUserData() for internal purposes, so refuse
1213 // to descend into anything that has other bits set than the following
1214 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
1215 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
1216 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
1218 int type = object->getType();
1219 if ((type & PUCLASS_GROUP) && !(type & ~validUserData)) {
1220 puObject* c = ((puGroup *)object)->getFirstChild();
1221 for (; c != NULL; c = c->getNextObject()) {
1223 } // of child iteration
1224 } // of group object case
1226 GUIInfo *info = (GUIInfo *)object->getUserData();
1230 SGPropertyNode *n = info->node;
1232 SG_LOG(SG_GENERAL, SG_ALERT, "FGDialog::applySize: no props");
1236 int x = n->getIntValue("x");
1237 int y = n->getIntValue("y");
1238 int w = n->getIntValue("width", 4);
1239 int h = n->getIntValue("height", 4);
1240 object->setPosition(x, y);
1241 object->setSize(w, h);
1244 ////////////////////////////////////////////////////////////////////////
1245 // Implementation of FGDialog::PropertyObject.
1246 ////////////////////////////////////////////////////////////////////////
1248 FGDialog::PropertyObject::PropertyObject(const char *n,
1249 puObject *o, SGPropertyNode_ptr p) :
1259 ////////////////////////////////////////////////////////////////////////
1260 // Implementation of fgValueList and derived pui widgets
1261 ////////////////////////////////////////////////////////////////////////
1264 fgValueList::fgValueList(SGPropertyNode *p) :
1271 fgValueList::update()
1277 fgValueList::~fgValueList()
1283 fgValueList::make_list()
1285 SGPropertyNode_ptr values = _props;
1286 const char* vname = "value";
1288 if (_props->hasChild("properties")) {
1289 // dynamic values, read from a property's children
1290 const char* path = _props->getStringValue("properties");
1291 values = fgGetNode(path, true);
1294 if (_props->hasChild("property-name")) {
1295 vname = _props->getStringValue("property-name");
1298 vector<SGPropertyNode_ptr> value_nodes = values->getChildren(vname);
1299 _list = new char *[value_nodes.size() + 1];
1301 for (i = 0; i < value_nodes.size(); i++) {
1302 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
1308 fgValueList::destroy_list()
1310 for (int i = 0; _list[i] != 0; i++)
1321 fgValueList::update();
1322 int top = getTopItem();
1327 void fgComboBox::update()
1329 fgValueList::update();
1333 // end of dialog.cxx