1 // dialog.cxx: implementation of an XML-configurable dialog box.
7 #include <simgear/structure/SGBinding.hxx>
8 #include <simgear/props/props_io.hxx>
10 #include <Scripting/NasalSys.hxx>
11 #include <Main/fg_os.hxx>
12 #include <Main/globals.hxx>
13 #include <Main/fg_props.hxx>
15 #include "FGPUIDialog.hxx"
16 #include "new_gui.hxx"
17 #include "AirportList.hxx"
18 #include "property_list.hxx"
20 #include "WaypointList.hxx"
21 #include "MapWidget.hxx"
22 #include "FGFontCache.hxx"
23 #include "FGColor.hxx"
25 enum format_type { f_INVALID, f_INT, f_LONG, f_FLOAT, f_DOUBLE, f_STRING };
26 static const int FORMAT_BUFSIZE = 255;
27 static const int RESIZE_MARGIN = 7;
31 * Makes sure the format matches '%[ -+#]?\d*(\.\d*)?(l?[df]|s)', with
32 * only one number or string placeholder and otherwise arbitrary prefix
33 * and postfix containing only quoted percent signs (%%).
36 validate_format(const char *f)
50 while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
52 while (*f && isdigit(*f))
56 while (*f && isdigit(*f))
64 type = l ? f_LONG : f_INT;
66 type = l ? f_DOUBLE : f_FLOAT;
86 ////////////////////////////////////////////////////////////////////////
87 // Implementation of GUIInfo.
88 ////////////////////////////////////////////////////////////////////////
91 * User data for a GUI object.
95 GUIInfo(FGPUIDialog *d);
97 void apply_format(SGPropertyNode *);
100 SGPropertyNode_ptr node;
101 vector <SGBinding *> bindings;
103 string label, legend, text, format;
104 format_type fmt_type;
107 GUIInfo::GUIInfo (FGPUIDialog *d) :
116 for (unsigned int i = 0; i < bindings.size(); i++) {
122 void GUIInfo::apply_format(SGPropertyNode *n)
124 char buf[FORMAT_BUFSIZE + 1];
125 if (fmt_type == f_INT)
126 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getIntValue());
127 else if (fmt_type == f_LONG)
128 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getLongValue());
129 else if (fmt_type == f_FLOAT)
130 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getFloatValue());
131 else if (fmt_type == f_DOUBLE)
132 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getDoubleValue());
134 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getStringValue());
136 buf[FORMAT_BUFSIZE] = '\0';
145 int fgPopup::checkKey(int key, int updown)
147 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
150 puObject *input = getActiveInputField(this);
152 return input->checkKey(key, updown);
154 puObject *object = getKeyObject(this, key);
156 return puPopup::checkKey(key, updown);
158 // invokeCallback() isn't enough; we need to simulate a mouse button press
159 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
160 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
161 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
162 object->checkHit(PU_LEFT_BUTTON, PU_UP,
163 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
164 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
168 puObject *fgPopup::getKeyObject(puObject *object, int key)
171 if (object->getType() & PUCLASS_GROUP)
172 for (puObject *obj = ((puGroup *)object)->getFirstChild();
173 obj; obj = obj->getNextObject())
174 if ((ret = getKeyObject(obj, key)))
177 GUIInfo *info = (GUIInfo *)object->getUserData();
178 if (info && info->key == key)
184 puObject *fgPopup::getActiveInputField(puObject *object)
187 if (object->getType() & PUCLASS_GROUP)
188 for (puObject *obj = ((puGroup *)object)->getFirstChild();
189 obj; obj = obj->getNextObject())
190 if ((ret = getActiveInputField(obj)))
193 if (object->getType() & (PUCLASS_INPUT|PUCLASS_LARGEINPUT)
194 && ((puInput *)object)->isAcceptingInput())
203 int fgPopup::checkHit(int button, int updown, int x, int y)
206 if (updown != PU_DRAG && !_dragging)
207 result = puPopup::checkHit(button, updown, x, y);
212 // This is annoying. We would really want a true result from the
213 // superclass to indicate "handled by child object", but all it
214 // tells us is that the pointer is inside the dialog. So do the
215 // intersection test (again) to make sure we don't start a drag
216 // when inside controls.
218 if (updown == PU_DOWN && !_dragging) {
221 int global_drag = fgGetKeyModifiers() & KEYMOD_SHIFT;
222 int global_resize = fgGetKeyModifiers() & KEYMOD_CTRL;
224 int hit = getHitObjects(this, x, y);
225 if (hit & PUCLASS_LIST) // ctrl-click in property browser (toggle bool)
227 if (!global_resize && hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
228 |PUCLASS_LARGEINPUT|PUCLASS_SCROLLBAR))
231 getPosition(&_dlgX, &_dlgY);
232 getSize(&_dlgW, &_dlgH);
233 _start_cursor = fgGetMouseCursor();
238 // check and prepare for resizing
239 static const int cursor[] = {
240 MOUSE_CURSOR_POINTER, MOUSE_CURSOR_LEFTSIDE, MOUSE_CURSOR_RIGHTSIDE, 0,
241 MOUSE_CURSOR_TOPSIDE, MOUSE_CURSOR_TOPLEFT, MOUSE_CURSOR_TOPRIGHT, 0,
242 MOUSE_CURSOR_BOTTOMSIDE, MOUSE_CURSOR_BOTTOMLEFT, MOUSE_CURSOR_BOTTOMRIGHT, 0,
246 if (!global_drag && _resizable) {
247 int hmargin = global_resize ? _dlgW / 3 : RESIZE_MARGIN;
248 int vmargin = global_resize ? _dlgH / 3 : RESIZE_MARGIN;
250 if (y - _dlgY < vmargin)
252 else if (_dlgY + _dlgH - y < vmargin)
255 if (x - _dlgX < hmargin)
257 else if (_dlgX + _dlgW - x < hmargin)
260 if (!_resizing && global_resize)
261 _resizing = BOTTOM|RIGHT;
263 _cursor = cursor[_resizing];
264 if (_resizing && _resizable)
265 fgSetMouseCursor(_cursor);
268 } else if (updown == PU_DRAG && _dragging) {
270 GUIInfo *info = (GUIInfo *)getUserData();
271 if (_resizable && info && info->node) {
274 if (_resizing & LEFT)
276 if (_resizing & RIGHT)
280 if (_resizing & BOTTOM)
284 LayoutWidget wid(info->node);
285 wid.calcPrefSize(&prefw, &prefh);
293 if (_resizing & LEFT)
295 if (_resizing & BOTTOM)
298 wid.layout(x, y, w, h);
301 applySize(static_cast<puObject *>(this));
302 getFirstChild()->setSize(w, h); // dialog background puFrame
305 int posX = x + _dlgX - _startX,
306 posY = y + _dlgY - _startY;
307 setPosition(posX, posY);
309 GUIInfo *info = (GUIInfo *)getUserData();
310 if (info && info->node) {
311 info->node->setIntValue("x", posX);
312 info->node->setIntValue("y", posY);
316 } else if (_dragging) {
317 fgSetMouseCursor(_start_cursor);
323 int fgPopup::getHitObjects(puObject *object, int x, int y)
325 if (!object->isVisible())
329 if (object->getType() & PUCLASS_GROUP)
330 for (puObject *obj = ((puGroup *)object)->getFirstChild();
331 obj; obj = obj->getNextObject())
332 type |= getHitObjects(obj, x, y);
335 object->getAbsolutePosition(&cx, &cy);
336 object->getSize(&cw, &ch);
337 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch)
338 type |= object->getType();
342 void fgPopup::applySize(puObject *object)
344 // compound plib widgets use setUserData() for internal purposes, so refuse
345 // to descend into anything that has other bits set than the following
346 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
347 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
348 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
350 int type = object->getType();
351 if (type & PUCLASS_GROUP && !(type & ~validUserData))
352 for (puObject *obj = ((puGroup *)object)->getFirstChild();
353 obj; obj = obj->getNextObject())
356 GUIInfo *info = (GUIInfo *)object->getUserData();
360 SGPropertyNode *n = info->node;
362 SG_LOG(SG_GENERAL, SG_ALERT, "fgPopup::applySize: no props");
365 int x = n->getIntValue("x");
366 int y = n->getIntValue("y");
367 int w = n->getIntValue("width", 4);
368 int h = n->getIntValue("height", 4);
369 object->setPosition(x, y);
370 object->setSize(w, h);
373 ////////////////////////////////////////////////////////////////////////
375 void FGPUIDialog::ConditionalObject::update(FGPUIDialog* aDlg)
377 if (_name == "visible") {
378 bool wasVis = _pu->isVisible() != 0;
379 bool newVis = test();
381 if (newVis == wasVis) {
385 if (newVis) { // puObject needs a setVisible. Oh well.
390 } else if (_name == "enable") {
391 bool wasEnabled = _pu->isActive() != 0;
392 bool newEnable = test();
394 if (wasEnabled == newEnable) {
405 aDlg->setNeedsLayout();
407 \f////////////////////////////////////////////////////////////////////////
409 ////////////////////////////////////////////////////////////////////////
415 action_callback (puObject *object)
417 GUIInfo *info = (GUIInfo *)object->getUserData();
418 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
419 gui->setActiveDialog(info->dialog);
420 int nBindings = info->bindings.size();
421 for (int i = 0; i < nBindings; i++) {
422 info->bindings[i]->fire();
423 if (gui->getActiveDialog() == 0)
426 gui->setActiveDialog(0);
431 ////////////////////////////////////////////////////////////////////////
432 // Static helper functions.
433 ////////////////////////////////////////////////////////////////////////
436 * Copy a property value to a PUI object.
439 copy_to_pui (SGPropertyNode *node, puObject *object)
441 using namespace simgear;
442 GUIInfo *info = (GUIInfo *)object->getUserData();
444 SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
445 return; // this can't really happen
448 // Treat puText objects specially, so their "values" can be set
450 if (object->getType() & PUCLASS_TEXT) {
451 if (info->fmt_type != f_INVALID)
452 info->apply_format(node);
454 info->text = node->getStringValue();
456 object->setLabel(info->text.c_str());
460 switch (node->getType()) {
464 object->setValue(node->getIntValue());
468 object->setValue(node->getFloatValue());
471 info->text = node->getStringValue();
472 object->setValue(info->text.c_str());
479 copy_from_pui (puObject *object, SGPropertyNode *node)
481 using namespace simgear;
482 // puText objects are immutable, so should not be copied out
483 if (object->getType() & PUCLASS_TEXT)
486 switch (node->getType()) {
490 node->setIntValue(object->getIntegerValue());
494 node->setFloatValue(object->getFloatValue());
497 const char *s = object->getStringValue();
499 node->setStringValue(s);
506 ////////////////////////////////////////////////////////////////////////
507 // Implementation of FGDialog.
508 ////////////////////////////////////////////////////////////////////////
510 FGPUIDialog::FGPUIDialog (SGPropertyNode *props) :
513 _gui((NewGUI *)globals->get_subsystem("gui")),
515 _needsRelayout(false)
517 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
519 SGPropertyNode *nasal = props->getNode("nasal");
521 _nasal_close = nasal->getNode("close");
522 SGPropertyNode *open = nasal->getNode("open");
524 const char *s = open->getStringValue();
525 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
527 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
533 FGPUIDialog::~FGPUIDialog ()
536 _object->getAbsolutePosition(&x, &y);
537 _props->setIntValue("lastx", x);
538 _props->setIntValue("lasty", y);
540 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
543 const char *s = _nasal_close->getStringValue();
544 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
546 nas->deleteModule(_module.c_str());
549 puDeleteObject(_object);
552 // Delete all the info objects we
553 // were forced to keep around because
554 // PUI cannot delete its own user data.
555 for (i = 0; i < _info.size(); i++) {
556 delete (GUIInfo *)_info[i];
559 // Finally, delete the property links.
560 for (i = 0; i < _propertyObjects.size(); i++) {
561 delete _propertyObjects[i];
562 _propertyObjects[i] = 0;
567 FGPUIDialog::updateValues (const char *objectName)
569 if (objectName && !objectName[0])
572 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
573 const string &name = _propertyObjects[i]->name;
574 if (objectName && name != objectName) {
578 puObject *widget = _propertyObjects[i]->object;
579 int widgetType = widget->getType();
580 if (widgetType & PUCLASS_LIST) {
581 GUI_ID* gui_id = dynamic_cast<GUI_ID *>(widget);
582 if (gui_id && (gui_id->id & FGCLASS_LIST)) {
583 fgList *pl = static_cast<fgList*>(widget);
586 copy_to_pui(_propertyObjects[i]->node, widget);
588 } else if (widgetType & PUCLASS_COMBOBOX) {
589 fgComboBox* combo = static_cast<fgComboBox*>(widget);
592 copy_to_pui(_propertyObjects[i]->node, widget);
594 } // of property objects iteration
598 FGPUIDialog::applyValues (const char *objectName)
600 if (objectName && !objectName[0])
603 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
604 const string &name = _propertyObjects[i]->name;
605 if (objectName && name != objectName)
608 copy_from_pui(_propertyObjects[i]->object,
609 _propertyObjects[i]->node);
614 FGPUIDialog::update ()
616 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
617 puObject *obj = _liveObjects[i]->object;
618 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
621 copy_to_pui(_liveObjects[i]->node, obj);
624 for (unsigned int j=0; j < _conditionalObjects.size(); ++j) {
625 _conditionalObjects[j]->update(this);
628 if (_needsRelayout) {
634 FGPUIDialog::display (SGPropertyNode *props)
637 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
641 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
642 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
644 bool userx = props->hasValue("x");
645 bool usery = props->hasValue("y");
646 bool userw = props->hasValue("width");
647 bool userh = props->hasValue("height");
649 // Let the layout widget work in the same property subtree.
650 LayoutWidget wid(props);
652 SGPropertyNode *fontnode = props->getNode("font");
654 FGFontCache *fc = globals->get_fontcache();
655 _font = fc->get(fontnode);
657 _font = _gui->getDefaultFont();
659 wid.setDefaultFont(_font, int(_font->getPointSize()));
662 int px, py, savex, savey;
663 if (!userw || !userh)
664 wid.calcPrefSize(&pw, &ph);
665 pw = props->getIntValue("width", pw);
666 ph = props->getIntValue("height", ph);
667 px = savex = props->getIntValue("x", (screenw - pw) / 2);
668 py = savey = props->getIntValue("y", (screenh - ph) / 2);
670 // Negative x/y coordinates are interpreted as distance from the top/right
671 // corner rather than bottom/left.
673 px = screenw - pw + px;
675 py = screenh - ph + py;
677 // Define "x", "y", "width" and/or "height" in the property tree if they
678 // are not specified in the configuration file.
679 wid.layout(px, py, pw, ph);
681 // Use the dimension and location properties as specified in the
682 // configuration file or from the layout widget.
683 _object = makeObject(props, screenw, screenh);
685 // Remove automatically generated properties, so the layout looks
686 // the same next time around, or restore x and y to preserve negative coords.
688 props->setIntValue("x", savex);
690 props->removeChild("x");
693 props->setIntValue("y", savey);
695 props->removeChild("y");
697 if (!userw) props->removeChild("width");
698 if (!userh) props->removeChild("height");
703 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
704 << props->getStringValue("name", "[unnamed]")
705 << " does not contain a proper GUI definition");
710 FGPUIDialog::makeObject (SGPropertyNode *props, int parentWidth, int parentHeight)
712 if (!props->getBoolValue("enabled", true))
715 bool presetSize = props->hasValue("width") && props->hasValue("height");
716 int width = props->getIntValue("width", parentWidth);
717 int height = props->getIntValue("height", parentHeight);
718 int x = props->getIntValue("x", (parentWidth - width) / 2);
719 int y = props->getIntValue("y", (parentHeight - height) / 2);
720 string type = props->getName();
725 if (type == "dialog") {
727 bool draggable = props->getBoolValue("draggable", true);
728 bool resizable = props->getBoolValue("resizable", false);
729 if (props->getBoolValue("modal", false))
730 obj = new puDialogBox(x, y);
732 obj = new fgPopup(x, y, resizable, draggable);
733 setupGroup(obj, props, width, height, true);
734 setColor(obj, props);
737 } else if (type == "group") {
738 puGroup *obj = new puGroup(x, y);
739 setupGroup(obj, props, width, height, false);
740 setColor(obj, props);
743 } else if (type == "frame") {
744 puGroup *obj = new puGroup(x, y);
745 setupGroup(obj, props, width, height, true);
746 setColor(obj, props);
749 } else if (type == "hrule" || type == "vrule") {
750 puFrame *obj = new puFrame(x, y, x + width, y + height);
751 obj->setBorderThickness(0);
752 setupObject(obj, props);
753 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
756 } else if (type == "list") {
757 int slider_width = props->getIntValue("slider", 20);
758 fgList *obj = new fgList(x, y, x + width, y + height, props, slider_width);
760 obj->setSize(width, height);
761 setupObject(obj, props);
762 setColor(obj, props);
765 } else if (type == "airport-list") {
766 AirportList *obj = new AirportList(x, y, x + width, y + height);
768 obj->setSize(width, height);
769 setupObject(obj, props);
770 setColor(obj, props);
773 } else if (type == "property-list") {
774 PropertyList *obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
776 obj->setSize(width, height);
777 setupObject(obj, props);
778 setColor(obj, props);
781 } else if (type == "input") {
782 puInput *obj = new puInput(x, y, x + width, y + height);
783 setupObject(obj, props);
784 setColor(obj, props, FOREGROUND|LABEL);
787 } else if (type == "text") {
788 puText *obj = new puText(x, y);
789 setupObject(obj, props);
791 // Layed-out objects need their size set, and non-layout ones
792 // get a different placement.
794 obj->setSize(width, height);
796 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
797 setColor(obj, props, LABEL);
800 } else if (type == "checkbox") {
802 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
803 setupObject(obj, props);
804 setColor(obj, props, FOREGROUND|LABEL);
807 } else if (type == "radio") {
809 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
810 setupObject(obj, props);
811 setColor(obj, props, FOREGROUND|LABEL);
814 } else if (type == "button") {
816 const char *legend = props->getStringValue("legend", "[none]");
817 if (props->getBoolValue("one-shot", true))
818 obj = new puOneShot(x, y, legend);
820 obj = new puButton(x, y, legend);
822 obj->setSize(width, height);
823 setupObject(obj, props);
824 setColor(obj, props);
826 } else if (type == "map") {
827 MapWidget* mapWidget = new MapWidget(x, y, x + width, y + height);
828 setupObject(mapWidget, props);
830 } else if (type == "combo") {
831 fgComboBox *obj = new fgComboBox(x, y, x + width, y + height, props,
832 props->getBoolValue("editable", false));
833 setupObject(obj, props);
834 setColor(obj, props, EDITFIELD);
837 } else if (type == "slider") {
838 bool vertical = props->getBoolValue("vertical", false);
839 puSlider *obj = new puSlider(x, y, (vertical ? height : width), vertical);
840 obj->setMinValue(props->getFloatValue("min", 0.0));
841 obj->setMaxValue(props->getFloatValue("max", 1.0));
842 obj->setStepSize(props->getFloatValue("step"));
843 obj->setSliderFraction(props->getFloatValue("fraction"));
844 #if PLIB_VERSION > 185
845 obj->setPageStepSize(props->getFloatValue("pagestep"));
847 setupObject(obj, props);
849 obj->setSize(width, height);
850 setColor(obj, props, FOREGROUND|LABEL);
853 } else if (type == "dial") {
854 puDial *obj = new puDial(x, y, width);
855 obj->setMinValue(props->getFloatValue("min", 0.0));
856 obj->setMaxValue(props->getFloatValue("max", 1.0));
857 obj->setWrap(props->getBoolValue("wrap", true));
858 setupObject(obj, props);
859 setColor(obj, props, FOREGROUND|LABEL);
862 } else if (type == "textbox") {
863 int slider_width = props->getIntValue("slider", 20);
864 int wrap = props->getBoolValue("wrap", true);
865 #if PLIB_VERSION > 185
866 puaLargeInput *obj = new puaLargeInput(x, y,
867 x + width, x + height, 11, slider_width, wrap);
869 puaLargeInput *obj = new puaLargeInput(x, y,
870 x + width, x + height, 2, slider_width, wrap);
873 if (props->getBoolValue("editable"))
879 obj->setSize(width, height);
880 setupObject(obj, props);
881 setColor(obj, props, FOREGROUND|LABEL);
883 int top = props->getIntValue("top-line", 0);
884 obj->setTopLineInWindow(top < 0 ? unsigned(-1) >> 1 : top);
887 } else if (type == "select") {
888 fgSelectBox *obj = new fgSelectBox(x, y, x + width, y + height, props);
889 setupObject(obj, props);
890 setColor(obj, props, EDITFIELD);
892 } else if (type == "waypointlist") {
893 ScrolledWaypointList* obj = new ScrolledWaypointList(x, y, width, height);
894 setupObject(obj, props);
902 FGPUIDialog::setupObject (puObject *object, SGPropertyNode *props)
904 GUIInfo *info = new GUIInfo(this);
905 object->setUserData(info);
906 _info.push_back(info);
907 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
908 object->makeReturnDefault(props->getBoolValue("default"));
911 if (props->hasValue("legend")) {
912 info->legend = props->getStringValue("legend");
913 object->setLegend(info->legend.c_str());
916 if (props->hasValue("label")) {
917 info->label = props->getStringValue("label");
918 object->setLabel(info->label.c_str());
921 if (props->hasValue("border"))
922 object->setBorderThickness( props->getIntValue("border", 2) );
924 if (SGPropertyNode *nft = props->getNode("font", false)) {
925 FGFontCache *fc = globals->get_fontcache();
926 puFont *lfnt = fc->get(nft);
927 object->setLabelFont(*lfnt);
928 object->setLegendFont(*lfnt);
930 object->setLabelFont(*_font);
933 if (props->hasChild("visible")) {
934 ConditionalObject* cnd = new ConditionalObject("visible", object);
935 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("visible")));
936 _conditionalObjects.push_back(cnd);
939 if (props->hasChild("enable")) {
940 ConditionalObject* cnd = new ConditionalObject("enable", object);
941 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("enable")));
942 _conditionalObjects.push_back(cnd);
945 string type = props->getName();
946 if (type == "input" && props->getBoolValue("live"))
947 object->setDownCallback(action_callback);
949 if (type == "text") {
950 const char *format = props->getStringValue("format", 0);
952 info->fmt_type = validate_format(format);
953 if (info->fmt_type != f_INVALID)
954 info->format = format;
956 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
961 if (props->hasValue("property")) {
962 const char *name = props->getStringValue("name");
965 const char *propname = props->getStringValue("property");
966 SGPropertyNode_ptr node = fgGetNode(propname, true);
968 // mapWidget binds to a sub-tree of properties, and
969 // ignores the puValue mechanism, so special case things here
970 MapWidget* mw = static_cast<MapWidget*>(object);
971 mw->setProperty(node);
973 // normal widget, creating PropertyObject
974 copy_to_pui(node, object);
975 PropertyObject *po = new PropertyObject(name, object, node);
976 _propertyObjects.push_back(po);
977 if (props->getBoolValue("live"))
978 _liveObjects.push_back(po);
984 SGPropertyNode *dest = fgGetNode("/sim/bindings/gui", true);
985 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
986 if (bindings.size() > 0) {
987 info->key = props->getIntValue("keynum", -1);
988 if (props->hasValue("key"))
989 info->key = getKeyCode(props->getStringValue("key", ""));
991 for (unsigned int i = 0; i < bindings.size(); i++) {
993 SGPropertyNode_ptr binding;
994 while (dest->getChild("binding", j))
997 const char *cmd = bindings[i]->getStringValue("command");
998 if (!strcmp(cmd, "nasal"))
999 bindings[i]->setStringValue("module", _module.c_str());
1001 binding = dest->getChild("binding", j, true);
1002 copyProperties(bindings[i], binding);
1003 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
1005 object->setCallback(action_callback);
1010 FGPUIDialog::setupGroup(puGroup *group, SGPropertyNode *props,
1011 int width, int height, bool makeFrame)
1013 setupObject(group, props);
1016 puFrame* f = new puFrame(0, 0, width, height);
1020 int nChildren = props->nChildren();
1021 for (int i = 0; i < nChildren; i++)
1022 makeObject(props->getChild(i), width, height);
1027 FGPUIDialog::setColor(puObject *object, SGPropertyNode *props, int which)
1029 string type = props->getName();
1032 if (type == "textbox" && props->getBoolValue("editable"))
1033 type += "-editable";
1035 FGColor c(_gui->getColor("background"));
1036 c.merge(_gui->getColor(type));
1037 c.merge(props->getNode("color"));
1039 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
1047 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
1048 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
1049 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
1050 { LABEL, PUCOL_LABEL, "label", "color-label" },
1051 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
1052 { MISC, PUCOL_MISC, "misc", "color-misc" },
1053 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
1056 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
1058 for (int i = 0; i < numcol; i++) {
1063 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
1064 if (which & pucol[i].mask)
1065 dirty |= c.merge(props->getNode("color"));
1067 if ((pucol[i].mask == LABEL) && !c.isValid())
1068 dirty |= c.merge(_gui->getColor("label"));
1070 dirty |= c.merge(props->getNode(pucol[i].cname));
1072 if (c.isValid() && dirty)
1073 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
1102 {"f10", PU_KEY_F10},
1103 {"f11", PU_KEY_F11},
1104 {"f12", PU_KEY_F12},
1105 {"left", PU_KEY_LEFT},
1107 {"right", PU_KEY_RIGHT},
1108 {"down", PU_KEY_DOWN},
1109 {"pageup", PU_KEY_PAGE_UP},
1110 {"pagedn", PU_KEY_PAGE_DOWN},
1111 {"home", PU_KEY_HOME},
1112 {"end", PU_KEY_END},
1113 {"insert", PU_KEY_INSERT},
1118 FGPUIDialog::getKeyCode(const char *str)
1129 char *buf = new char[strlen(str) + 1];
1131 char *s = buf + strlen(buf);
1132 while (s > str && s[-1] == ' ')
1139 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
1140 s += 5, mod |= CTRL;
1141 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
1142 s += 6, mod |= SHIFT;
1143 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
1150 if (strlen(s) == 1 && isascii(*s)) {
1155 key = toupper(key) - '@';
1157 ; // Alt not propagated to the gui
1160 for (char *t = s; *t; t++)
1162 for (int i = 0; keymap[i].name; i++) {
1163 if (!strcmp(s, keymap[i].name)) {
1164 key = keymap[i].key;
1173 void
\fFGPUIDialog::relayout()
1175 _needsRelayout = false;
1177 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
1178 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
1180 bool userx = _props->hasValue("x");
1181 bool usery = _props->hasValue("y");
1182 bool userw = _props->hasValue("width");
1183 bool userh = _props->hasValue("height");
1185 // Let the layout widget work in the same property subtree.
1186 LayoutWidget wid(_props);
1187 wid.setDefaultFont(_font, int(_font->getPointSize()));
1190 int px, py, savex, savey;
1191 if (!userw || !userh) {
1192 wid.calcPrefSize(&pw, &ph);
1195 pw = _props->getIntValue("width", pw);
1196 ph = _props->getIntValue("height", ph);
1197 px = savex = _props->getIntValue("x", (screenw - pw) / 2);
1198 py = savey = _props->getIntValue("y", (screenh - ph) / 2);
1200 // Negative x/y coordinates are interpreted as distance from the top/right
1201 // corner rather than bottom/left.
1202 if (userx && px < 0)
1203 px = screenw - pw + px;
1204 if (usery && py < 0)
1205 py = screenh - ph + py;
1207 // Define "x", "y", "width" and/or "height" in the property tree if they
1208 // are not specified in the configuration file.
1209 wid.layout(px, py, pw, ph);
1213 // Remove automatically generated properties, so the layout looks
1214 // the same next time around, or restore x and y to preserve negative coords.
1216 _props->setIntValue("x", savex);
1218 _props->removeChild("x");
1221 _props->setIntValue("y", savey);
1223 _props->removeChild("y");
1225 if (!userw) _props->removeChild("width");
1226 if (!userh) _props->removeChild("height");
1230 FGPUIDialog::applySize(puObject *object)
1232 // compound plib widgets use setUserData() for internal purposes, so refuse
1233 // to descend into anything that has other bits set than the following
1234 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
1235 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
1236 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
1238 int type = object->getType();
1239 if ((type & PUCLASS_GROUP) && !(type & ~validUserData)) {
1240 puObject* c = ((puGroup *)object)->getFirstChild();
1241 for (; c != NULL; c = c->getNextObject()) {
1243 } // of child iteration
1244 } // of group object case
1246 GUIInfo *info = (GUIInfo *)object->getUserData();
1250 SGPropertyNode *n = info->node;
1252 SG_LOG(SG_GENERAL, SG_ALERT, "FGDialog::applySize: no props");
1256 int x = n->getIntValue("x");
1257 int y = n->getIntValue("y");
1258 int w = n->getIntValue("width", 4);
1259 int h = n->getIntValue("height", 4);
1260 object->setPosition(x, y);
1261 object->setSize(w, h);
1264 ////////////////////////////////////////////////////////////////////////
1265 // Implementation of FGDialog::PropertyObject.
1266 ////////////////////////////////////////////////////////////////////////
1268 FGPUIDialog::PropertyObject::PropertyObject(const char *n,
1269 puObject *o, SGPropertyNode_ptr p) :
1279 ////////////////////////////////////////////////////////////////////////
1280 // Implementation of fgValueList and derived pui widgets
1281 ////////////////////////////////////////////////////////////////////////
1284 fgValueList::fgValueList(SGPropertyNode *p) :
1291 fgValueList::update()
1297 fgValueList::~fgValueList()
1303 fgValueList::make_list()
1305 SGPropertyNode_ptr values = _props;
1306 const char* vname = "value";
1308 if (_props->hasChild("properties")) {
1309 // dynamic values, read from a property's children
1310 const char* path = _props->getStringValue("properties");
1311 values = fgGetNode(path, true);
1314 if (_props->hasChild("property-name")) {
1315 vname = _props->getStringValue("property-name");
1318 vector<SGPropertyNode_ptr> value_nodes = values->getChildren(vname);
1319 _list = new char *[value_nodes.size() + 1];
1321 for (i = 0; i < value_nodes.size(); i++) {
1322 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
1328 fgValueList::destroy_list()
1330 for (int i = 0; _list[i] != 0; i++)
1341 fgValueList::update();
1342 int top = getTopItem();
1347 void fgComboBox::update()
1353 std::string curValue(getStringValue());
1354 fgValueList::update();
1356 int currentItem = puaComboBox::getCurrentItem();
1359 // look for the previous value, in the new list
1360 for (int i = 0; _list[i] != 0; i++) {
1361 if (_list[i] == curValue) {
1362 // don't set the current item again; this is important to avoid
1363 // recursion here if the combo callback ultimately causes another dialog-update
1364 if (i != currentItem) {
1365 puaComboBox::setCurrentItem(i);
1370 } // of list values iteration
1372 // cound't find current item, default to first
1373 if (currentItem != 0) {
1374 puaComboBox::setCurrentItem(0);
1378 int fgComboBox::checkHit(int b, int up, int x, int y)
1381 int r = puaComboBox::checkHit(b, up, x, y);
1386 void fgComboBox::setSize(int w, int h)
1388 puaComboBox::setSize(w, h);
1392 void fgComboBox::recalc_bbox()
1394 // bug-fix for issue #192
1395 // http://code.google.com/p/flightgear-bugs/issues/detail?id=192
1396 // puaComboBox is including the height of its popupMenu in the height
1397 // computation, which breaks layout computations.
1398 // this implementation skips popup-menus
1403 for (puObject *bo = dlist; bo != NULL; bo = bo -> getNextObject()) {
1404 if (bo->getType() & PUCLASS_POPUPMENU) {
1408 contents.extend (bo -> getBBox()) ;
1411 abox.max[0] = abox.min[0] + contents.max[0] ;
1412 abox.max[1] = abox.min[1] + contents.max[1] ;
1414 puObject::recalc_bbox () ;
1418 // end of FGPUIDialog.cxx