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 "CanvasWidget.hxx"
22 #include "MapWidget.hxx"
23 #include "FGFontCache.hxx"
24 #include "FGColor.hxx"
26 enum format_type { f_INVALID, f_INT, f_LONG, f_FLOAT, f_DOUBLE, f_STRING };
27 static const int FORMAT_BUFSIZE = 255;
28 static const int RESIZE_MARGIN = 7;
32 * Makes sure the format matches '%[ -+#]?\d*(\.\d*)?(l?[df]|s)', with
33 * only one number or string placeholder and otherwise arbitrary prefix
34 * and postfix containing only quoted percent signs (%%).
37 validate_format(const char *f)
51 while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
53 while (*f && isdigit(*f))
57 while (*f && isdigit(*f))
65 type = l ? f_LONG : f_INT;
67 type = l ? f_DOUBLE : f_FLOAT;
87 ////////////////////////////////////////////////////////////////////////
88 // Implementation of GUIInfo.
89 ////////////////////////////////////////////////////////////////////////
92 * User data for a GUI object.
96 GUIInfo(FGPUIDialog *d);
98 void apply_format(SGPropertyNode *);
101 SGPropertyNode_ptr node;
102 vector <SGBinding *> bindings;
104 string label, legend, text, format;
105 format_type fmt_type;
108 GUIInfo::GUIInfo (FGPUIDialog *d) :
117 for (unsigned int i = 0; i < bindings.size(); i++) {
123 void GUIInfo::apply_format(SGPropertyNode *n)
125 char buf[FORMAT_BUFSIZE + 1];
126 if (fmt_type == f_INT)
127 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getIntValue());
128 else if (fmt_type == f_LONG)
129 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getLongValue());
130 else if (fmt_type == f_FLOAT)
131 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getFloatValue());
132 else if (fmt_type == f_DOUBLE)
133 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getDoubleValue());
135 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getStringValue());
137 buf[FORMAT_BUFSIZE] = '\0';
146 int fgPopup::checkKey(int key, int updown)
148 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
151 puObject *input = getActiveInputField(this);
153 return input->checkKey(key, updown);
155 puObject *object = getKeyObject(this, key);
157 return puPopup::checkKey(key, updown);
159 // invokeCallback() isn't enough; we need to simulate a mouse button press
160 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
161 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
162 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
163 object->checkHit(PU_LEFT_BUTTON, PU_UP,
164 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
165 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
169 puObject *fgPopup::getKeyObject(puObject *object, int key)
172 if (object->getType() & PUCLASS_GROUP)
173 for (puObject *obj = ((puGroup *)object)->getFirstChild();
174 obj; obj = obj->getNextObject())
175 if ((ret = getKeyObject(obj, key)))
178 GUIInfo *info = (GUIInfo *)object->getUserData();
179 if (info && info->key == key)
185 puObject *fgPopup::getActiveInputField(puObject *object)
188 if (object->getType() & PUCLASS_GROUP)
189 for (puObject *obj = ((puGroup *)object)->getFirstChild();
190 obj; obj = obj->getNextObject())
191 if ((ret = getActiveInputField(obj)))
194 if (object->getType() & (PUCLASS_INPUT|PUCLASS_LARGEINPUT)
195 && ((puInput *)object)->isAcceptingInput())
204 int fgPopup::checkHit(int button, int updown, int x, int y)
207 if (updown != PU_DRAG && !_dragging)
208 result = puPopup::checkHit(button, updown, x, y);
213 // This is annoying. We would really want a true result from the
214 // superclass to indicate "handled by child object", but all it
215 // tells us is that the pointer is inside the dialog. So do the
216 // intersection test (again) to make sure we don't start a drag
217 // when inside controls.
219 if (updown == PU_DOWN && !_dragging) {
222 int global_drag = fgGetKeyModifiers() & KEYMOD_SHIFT;
223 int global_resize = fgGetKeyModifiers() & KEYMOD_CTRL;
225 int hit = getHitObjects(this, x, y);
226 if (hit & PUCLASS_LIST) // ctrl-click in property browser (toggle bool)
228 if (!global_resize && hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
229 |PUCLASS_LARGEINPUT|PUCLASS_SCROLLBAR))
232 getPosition(&_dlgX, &_dlgY);
233 getSize(&_dlgW, &_dlgH);
234 _start_cursor = fgGetMouseCursor();
239 // check and prepare for resizing
240 static const int cursor[] = {
241 MOUSE_CURSOR_POINTER, MOUSE_CURSOR_LEFTSIDE, MOUSE_CURSOR_RIGHTSIDE, 0,
242 MOUSE_CURSOR_TOPSIDE, MOUSE_CURSOR_TOPLEFT, MOUSE_CURSOR_TOPRIGHT, 0,
243 MOUSE_CURSOR_BOTTOMSIDE, MOUSE_CURSOR_BOTTOMLEFT, MOUSE_CURSOR_BOTTOMRIGHT, 0,
247 if (!global_drag && _resizable) {
248 int hmargin = global_resize ? _dlgW / 3 : RESIZE_MARGIN;
249 int vmargin = global_resize ? _dlgH / 3 : RESIZE_MARGIN;
251 if (y - _dlgY < vmargin)
253 else if (_dlgY + _dlgH - y < vmargin)
256 if (x - _dlgX < hmargin)
258 else if (_dlgX + _dlgW - x < hmargin)
261 if (!_resizing && global_resize)
262 _resizing = BOTTOM|RIGHT;
264 _cursor = cursor[_resizing];
265 if (_resizing && _resizable)
266 fgSetMouseCursor(_cursor);
269 } else if (updown == PU_DRAG && _dragging) {
271 GUIInfo *info = (GUIInfo *)getUserData();
272 if (_resizable && info && info->node) {
275 if (_resizing & LEFT)
277 if (_resizing & RIGHT)
281 if (_resizing & BOTTOM)
285 LayoutWidget wid(info->node);
286 wid.calcPrefSize(&prefw, &prefh);
294 if (_resizing & LEFT)
296 if (_resizing & BOTTOM)
299 wid.layout(x, y, w, h);
302 applySize(static_cast<puObject *>(this));
303 getFirstChild()->setSize(w, h); // dialog background puFrame
306 int posX = x + _dlgX - _startX,
307 posY = y + _dlgY - _startY;
308 setPosition(posX, posY);
310 GUIInfo *info = (GUIInfo *)getUserData();
311 if (info && info->node) {
312 info->node->setIntValue("x", posX);
313 info->node->setIntValue("y", posY);
317 } else if (_dragging) {
318 fgSetMouseCursor(_start_cursor);
324 int fgPopup::getHitObjects(puObject *object, int x, int y)
326 if (!object->isVisible())
330 if (object->getType() & PUCLASS_GROUP)
331 for (puObject *obj = ((puGroup *)object)->getFirstChild();
332 obj; obj = obj->getNextObject())
333 type |= getHitObjects(obj, x, y);
336 object->getAbsolutePosition(&cx, &cy);
337 object->getSize(&cw, &ch);
338 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch)
339 type |= object->getType();
343 void fgPopup::applySize(puObject *object)
345 // compound plib widgets use setUserData() for internal purposes, so refuse
346 // to descend into anything that has other bits set than the following
347 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
348 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
349 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
351 int type = object->getType();
352 if (type & PUCLASS_GROUP && !(type & ~validUserData))
353 for (puObject *obj = ((puGroup *)object)->getFirstChild();
354 obj; obj = obj->getNextObject())
357 GUIInfo *info = (GUIInfo *)object->getUserData();
361 SGPropertyNode *n = info->node;
363 SG_LOG(SG_GENERAL, SG_ALERT, "fgPopup::applySize: no props");
366 int x = n->getIntValue("x");
367 int y = n->getIntValue("y");
368 int w = n->getIntValue("width", 4);
369 int h = n->getIntValue("height", 4);
370 object->setPosition(x, y);
371 object->setSize(w, h);
374 ////////////////////////////////////////////////////////////////////////
376 void FGPUIDialog::ConditionalObject::update(FGPUIDialog* aDlg)
378 if (_name == "visible") {
379 bool wasVis = _pu->isVisible() != 0;
380 bool newVis = test();
382 if (newVis == wasVis) {
386 if (newVis) { // puObject needs a setVisible. Oh well.
391 } else if (_name == "enable") {
392 bool wasEnabled = _pu->isActive() != 0;
393 bool newEnable = test();
395 if (wasEnabled == newEnable) {
406 aDlg->setNeedsLayout();
408 \f////////////////////////////////////////////////////////////////////////
410 ////////////////////////////////////////////////////////////////////////
416 action_callback (puObject *object)
418 GUIInfo *info = (GUIInfo *)object->getUserData();
419 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
420 gui->setActiveDialog(info->dialog);
421 int nBindings = info->bindings.size();
422 for (int i = 0; i < nBindings; i++) {
423 info->bindings[i]->fire();
424 if (gui->getActiveDialog() == 0)
427 gui->setActiveDialog(0);
432 ////////////////////////////////////////////////////////////////////////
433 // Static helper functions.
434 ////////////////////////////////////////////////////////////////////////
437 * Copy a property value to a PUI object.
440 copy_to_pui (SGPropertyNode *node, puObject *object)
442 using namespace simgear;
443 GUIInfo *info = (GUIInfo *)object->getUserData();
445 SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
446 return; // this can't really happen
449 // Treat puText objects specially, so their "values" can be set
451 if (object->getType() & PUCLASS_TEXT) {
452 if (info->fmt_type != f_INVALID)
453 info->apply_format(node);
455 info->text = node->getStringValue();
457 object->setLabel(info->text.c_str());
461 switch (node->getType()) {
465 object->setValue(node->getIntValue());
469 object->setValue(node->getFloatValue());
472 info->text = node->getStringValue();
473 object->setValue(info->text.c_str());
480 copy_from_pui (puObject *object, SGPropertyNode *node)
482 using namespace simgear;
483 // puText objects are immutable, so should not be copied out
484 if (object->getType() & PUCLASS_TEXT)
487 switch (node->getType()) {
491 node->setIntValue(object->getIntegerValue());
494 node->setFloatValue(object->getFloatValue());
498 // puObject only provides float, not double, which causes precision/rounding issues
499 // with some numerical values (try "114.2").
500 // Work around: obtain string value, and manually convert with proper double precision.
501 const char *s = object->getStringValue();
502 node->setDoubleValue(atof(s));
506 const char *s = object->getStringValue();
508 node->setStringValue(s);
515 ////////////////////////////////////////////////////////////////////////
516 // Implementation of FGDialog.
517 ////////////////////////////////////////////////////////////////////////
519 FGPUIDialog::FGPUIDialog (SGPropertyNode *props) :
522 _gui((NewGUI *)globals->get_subsystem("gui")),
524 _needsRelayout(false)
526 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
528 SGPropertyNode *nasal = props->getNode("nasal");
530 _nasal_close = nasal->getNode("close");
531 SGPropertyNode *open = nasal->getNode("open");
533 const char *s = open->getStringValue();
534 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
536 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
542 FGPUIDialog::~FGPUIDialog ()
545 _object->getAbsolutePosition(&x, &y);
546 _props->setIntValue("lastx", x);
547 _props->setIntValue("lasty", y);
549 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
552 const char *s = _nasal_close->getStringValue();
553 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
555 nas->deleteModule(_module.c_str());
558 puDeleteObject(_object);
561 // Delete all the info objects we
562 // were forced to keep around because
563 // PUI cannot delete its own user data.
564 for (i = 0; i < _info.size(); i++) {
565 delete (GUIInfo *)_info[i];
568 // Finally, delete the property links.
569 for (i = 0; i < _propertyObjects.size(); i++) {
570 delete _propertyObjects[i];
571 _propertyObjects[i] = 0;
576 FGPUIDialog::updateValues (const char *objectName)
578 if (objectName && !objectName[0])
581 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
582 const string &name = _propertyObjects[i]->name;
583 if (objectName && name != objectName) {
587 puObject *widget = _propertyObjects[i]->object;
588 int widgetType = widget->getType();
589 if (widgetType & PUCLASS_LIST) {
590 GUI_ID* gui_id = dynamic_cast<GUI_ID *>(widget);
591 if (gui_id && (gui_id->id & FGCLASS_LIST)) {
592 fgList *pl = static_cast<fgList*>(widget);
595 copy_to_pui(_propertyObjects[i]->node, widget);
597 } else if (widgetType & PUCLASS_COMBOBOX) {
598 fgComboBox* combo = static_cast<fgComboBox*>(widget);
601 copy_to_pui(_propertyObjects[i]->node, widget);
603 } // of property objects iteration
607 FGPUIDialog::applyValues (const char *objectName)
609 if (objectName && !objectName[0])
612 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
613 const string &name = _propertyObjects[i]->name;
614 if (objectName && name != objectName)
617 copy_from_pui(_propertyObjects[i]->object,
618 _propertyObjects[i]->node);
623 FGPUIDialog::update ()
625 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
626 puObject *obj = _liveObjects[i]->object;
627 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
630 copy_to_pui(_liveObjects[i]->node, obj);
633 for (unsigned int j=0; j < _conditionalObjects.size(); ++j) {
634 _conditionalObjects[j]->update(this);
637 if (_needsRelayout) {
643 FGPUIDialog::display (SGPropertyNode *props)
646 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
650 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
651 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
653 bool userx = props->hasValue("x");
654 bool usery = props->hasValue("y");
655 bool userw = props->hasValue("width");
656 bool userh = props->hasValue("height");
658 // Let the layout widget work in the same property subtree.
659 LayoutWidget wid(props);
661 SGPropertyNode *fontnode = props->getNode("font");
663 FGFontCache *fc = globals->get_fontcache();
664 _font = fc->get(fontnode);
666 _font = _gui->getDefaultFont();
668 wid.setDefaultFont(_font, int(_font->getPointSize()));
671 int px, py, savex, savey;
672 if (!userw || !userh)
673 wid.calcPrefSize(&pw, &ph);
674 pw = props->getIntValue("width", pw);
675 ph = props->getIntValue("height", ph);
676 px = savex = props->getIntValue("x", (screenw - pw) / 2);
677 py = savey = props->getIntValue("y", (screenh - ph) / 2);
679 // Negative x/y coordinates are interpreted as distance from the top/right
680 // corner rather than bottom/left.
682 px = screenw - pw + px;
684 py = screenh - ph + py;
686 // Define "x", "y", "width" and/or "height" in the property tree if they
687 // are not specified in the configuration file.
688 wid.layout(px, py, pw, ph);
690 // Use the dimension and location properties as specified in the
691 // configuration file or from the layout widget.
692 _object = makeObject(props, screenw, screenh);
694 // Remove automatically generated properties, so the layout looks
695 // the same next time around, or restore x and y to preserve negative coords.
697 props->setIntValue("x", savex);
699 props->removeChild("x");
702 props->setIntValue("y", savey);
704 props->removeChild("y");
706 if (!userw) props->removeChild("width");
707 if (!userh) props->removeChild("height");
712 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
713 << props->getStringValue("name", "[unnamed]")
714 << " does not contain a proper GUI definition");
719 FGPUIDialog::makeObject (SGPropertyNode *props, int parentWidth, int parentHeight)
721 if (!props->getBoolValue("enabled", true))
724 bool presetSize = props->hasValue("width") && props->hasValue("height");
725 int width = props->getIntValue("width", parentWidth);
726 int height = props->getIntValue("height", parentHeight);
727 int x = props->getIntValue("x", (parentWidth - width) / 2);
728 int y = props->getIntValue("y", (parentHeight - height) / 2);
729 string type = props->getName();
734 if (type == "dialog") {
736 bool draggable = props->getBoolValue("draggable", true);
737 bool resizable = props->getBoolValue("resizable", false);
738 if (props->getBoolValue("modal", false))
739 obj = new puDialogBox(x, y);
741 obj = new fgPopup(x, y, resizable, draggable);
742 setupGroup(obj, props, width, height, true);
743 setColor(obj, props);
746 } else if (type == "group") {
747 puGroup *obj = new puGroup(x, y);
748 setupGroup(obj, props, width, height, false);
749 setColor(obj, props);
752 } else if (type == "frame") {
753 puGroup *obj = new puGroup(x, y);
754 setupGroup(obj, props, width, height, true);
755 setColor(obj, props);
758 } else if (type == "hrule" || type == "vrule") {
759 puFrame *obj = new puFrame(x, y, x + width, y + height);
760 obj->setBorderThickness(0);
761 setupObject(obj, props);
762 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
765 } else if (type == "list") {
766 int slider_width = props->getIntValue("slider", 20);
767 fgList *obj = new fgList(x, y, x + width, y + height, props, slider_width);
769 obj->setSize(width, height);
770 setupObject(obj, props);
771 setColor(obj, props);
774 } else if (type == "airport-list") {
775 AirportList *obj = new AirportList(x, y, x + width, y + height);
777 obj->setSize(width, height);
778 setupObject(obj, props);
779 setColor(obj, props);
782 } else if (type == "property-list") {
783 PropertyList *obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
785 obj->setSize(width, height);
786 setupObject(obj, props);
787 setColor(obj, props);
790 } else if (type == "input") {
791 puInput *obj = new puInput(x, y, x + width, y + height);
792 setupObject(obj, props);
793 setColor(obj, props, FOREGROUND|LABEL);
796 } else if (type == "text") {
797 puText *obj = new puText(x, y);
798 setupObject(obj, props);
800 // Layed-out objects need their size set, and non-layout ones
801 // get a different placement.
803 obj->setSize(width, height);
805 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
806 setColor(obj, props, LABEL);
809 } else if (type == "checkbox") {
811 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
812 setupObject(obj, props);
813 setColor(obj, props, FOREGROUND|LABEL);
816 } else if (type == "radio") {
818 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
819 setupObject(obj, props);
820 setColor(obj, props, FOREGROUND|LABEL);
823 } else if (type == "button") {
825 const char *legend = props->getStringValue("legend", "[none]");
826 if (props->getBoolValue("one-shot", true))
827 obj = new puOneShot(x, y, legend);
829 obj = new puButton(x, y, legend);
831 obj->setSize(width, height);
832 setupObject(obj, props);
833 setColor(obj, props);
835 } else if (type == "map") {
836 MapWidget* mapWidget = new MapWidget(x, y, x + width, y + height);
837 setupObject(mapWidget, props);
839 } else if (type == "canvas") {
840 CanvasWidget* canvasWidget = new CanvasWidget( x, y,
841 x + width, y + height,
844 setupObject(canvasWidget, props);
846 } else if (type == "combo") {
847 fgComboBox *obj = new fgComboBox(x, y, x + width, y + height, props,
848 props->getBoolValue("editable", false));
849 setupObject(obj, props);
850 setColor(obj, props, EDITFIELD);
853 } else if (type == "slider") {
854 bool vertical = props->getBoolValue("vertical", false);
855 puSlider *obj = new puSlider(x, y, (vertical ? height : width), vertical);
856 obj->setMinValue(props->getFloatValue("min", 0.0));
857 obj->setMaxValue(props->getFloatValue("max", 1.0));
858 obj->setStepSize(props->getFloatValue("step"));
859 obj->setSliderFraction(props->getFloatValue("fraction"));
860 #if PLIB_VERSION > 185
861 obj->setPageStepSize(props->getFloatValue("pagestep"));
863 setupObject(obj, props);
865 obj->setSize(width, height);
866 setColor(obj, props, FOREGROUND|LABEL);
869 } else if (type == "dial") {
870 puDial *obj = new puDial(x, y, width);
871 obj->setMinValue(props->getFloatValue("min", 0.0));
872 obj->setMaxValue(props->getFloatValue("max", 1.0));
873 obj->setWrap(props->getBoolValue("wrap", true));
874 setupObject(obj, props);
875 setColor(obj, props, FOREGROUND|LABEL);
878 } else if (type == "textbox") {
879 int slider_width = props->getIntValue("slider", 20);
880 int wrap = props->getBoolValue("wrap", true);
881 #if PLIB_VERSION > 185
882 puaLargeInput *obj = new puaLargeInput(x, y,
883 x + width, x + height, 11, slider_width, wrap);
885 puaLargeInput *obj = new puaLargeInput(x, y,
886 x + width, x + height, 2, slider_width, wrap);
889 if (props->getBoolValue("editable"))
895 obj->setSize(width, height);
896 setupObject(obj, props);
897 setColor(obj, props, FOREGROUND|LABEL);
899 int top = props->getIntValue("top-line", 0);
900 obj->setTopLineInWindow(top < 0 ? unsigned(-1) >> 1 : top);
903 } else if (type == "select") {
904 fgSelectBox *obj = new fgSelectBox(x, y, x + width, y + height, props);
905 setupObject(obj, props);
906 setColor(obj, props, EDITFIELD);
908 } else if (type == "waypointlist") {
909 ScrolledWaypointList* obj = new ScrolledWaypointList(x, y, width, height);
910 setupObject(obj, props);
918 FGPUIDialog::setupObject (puObject *object, SGPropertyNode *props)
920 GUIInfo *info = new GUIInfo(this);
921 object->setUserData(info);
922 _info.push_back(info);
923 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
924 object->makeReturnDefault(props->getBoolValue("default"));
927 if (props->hasValue("legend")) {
928 info->legend = props->getStringValue("legend");
929 object->setLegend(info->legend.c_str());
932 if (props->hasValue("label")) {
933 info->label = props->getStringValue("label");
934 object->setLabel(info->label.c_str());
937 if (props->hasValue("border"))
938 object->setBorderThickness( props->getIntValue("border", 2) );
940 if (SGPropertyNode *nft = props->getNode("font", false)) {
941 FGFontCache *fc = globals->get_fontcache();
942 puFont *lfnt = fc->get(nft);
943 object->setLabelFont(*lfnt);
944 object->setLegendFont(*lfnt);
946 object->setLabelFont(*_font);
949 if (props->hasChild("visible")) {
950 ConditionalObject* cnd = new ConditionalObject("visible", object);
951 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("visible")));
952 _conditionalObjects.push_back(cnd);
955 if (props->hasChild("enable")) {
956 ConditionalObject* cnd = new ConditionalObject("enable", object);
957 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("enable")));
958 _conditionalObjects.push_back(cnd);
961 string type = props->getName();
962 if (type == "input" && props->getBoolValue("live"))
963 object->setDownCallback(action_callback);
965 if (type == "text") {
966 const char *format = props->getStringValue("format", 0);
968 info->fmt_type = validate_format(format);
969 if (info->fmt_type != f_INVALID)
970 info->format = format;
972 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
977 if (props->hasValue("property")) {
978 const char *name = props->getStringValue("name");
981 const char *propname = props->getStringValue("property");
982 SGPropertyNode_ptr node = fgGetNode(propname, true);
984 // mapWidget binds to a sub-tree of properties, and
985 // ignores the puValue mechanism, so special case things here
986 MapWidget* mw = static_cast<MapWidget*>(object);
987 mw->setProperty(node);
989 // normal widget, creating PropertyObject
990 copy_to_pui(node, object);
991 PropertyObject *po = new PropertyObject(name, object, node);
992 _propertyObjects.push_back(po);
993 if (props->getBoolValue("live"))
994 _liveObjects.push_back(po);
1000 SGPropertyNode *dest = fgGetNode("/sim/bindings/gui", true);
1001 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
1002 if (bindings.size() > 0) {
1003 info->key = props->getIntValue("keynum", -1);
1004 if (props->hasValue("key"))
1005 info->key = getKeyCode(props->getStringValue("key", ""));
1007 for (unsigned int i = 0; i < bindings.size(); i++) {
1009 SGPropertyNode_ptr binding;
1010 while (dest->getChild("binding", j))
1013 const char *cmd = bindings[i]->getStringValue("command");
1014 if (!strcmp(cmd, "nasal"))
1015 bindings[i]->setStringValue("module", _module.c_str());
1017 binding = dest->getChild("binding", j, true);
1018 copyProperties(bindings[i], binding);
1019 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
1021 object->setCallback(action_callback);
1026 FGPUIDialog::setupGroup(puGroup *group, SGPropertyNode *props,
1027 int width, int height, bool makeFrame)
1029 setupObject(group, props);
1032 puFrame* f = new puFrame(0, 0, width, height);
1036 int nChildren = props->nChildren();
1037 for (int i = 0; i < nChildren; i++)
1038 makeObject(props->getChild(i), width, height);
1043 FGPUIDialog::setColor(puObject *object, SGPropertyNode *props, int which)
1045 string type = props->getName();
1048 if (type == "textbox" && props->getBoolValue("editable"))
1049 type += "-editable";
1051 FGColor c(_gui->getColor("background"));
1052 c.merge(_gui->getColor(type));
1053 c.merge(props->getNode("color"));
1055 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
1063 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
1064 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
1065 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
1066 { LABEL, PUCOL_LABEL, "label", "color-label" },
1067 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
1068 { MISC, PUCOL_MISC, "misc", "color-misc" },
1069 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
1072 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
1074 for (int i = 0; i < numcol; i++) {
1079 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
1080 if (which & pucol[i].mask)
1081 dirty |= c.merge(props->getNode("color"));
1083 if ((pucol[i].mask == LABEL) && !c.isValid())
1084 dirty |= c.merge(_gui->getColor("label"));
1086 dirty |= c.merge(props->getNode(pucol[i].cname));
1088 if (c.isValid() && dirty)
1089 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
1118 {"f10", PU_KEY_F10},
1119 {"f11", PU_KEY_F11},
1120 {"f12", PU_KEY_F12},
1121 {"left", PU_KEY_LEFT},
1123 {"right", PU_KEY_RIGHT},
1124 {"down", PU_KEY_DOWN},
1125 {"pageup", PU_KEY_PAGE_UP},
1126 {"pagedn", PU_KEY_PAGE_DOWN},
1127 {"home", PU_KEY_HOME},
1128 {"end", PU_KEY_END},
1129 {"insert", PU_KEY_INSERT},
1134 FGPUIDialog::getKeyCode(const char *str)
1145 char *buf = new char[strlen(str) + 1];
1147 char *s = buf + strlen(buf);
1148 while (s > str && s[-1] == ' ')
1155 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
1156 s += 5, mod |= CTRL;
1157 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
1158 s += 6, mod |= SHIFT;
1159 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
1166 if (strlen(s) == 1 && isascii(*s)) {
1171 key = toupper(key) - '@';
1173 ; // Alt not propagated to the gui
1176 for (char *t = s; *t; t++)
1178 for (int i = 0; keymap[i].name; i++) {
1179 if (!strcmp(s, keymap[i].name)) {
1180 key = keymap[i].key;
1189 void
\fFGPUIDialog::relayout()
1191 _needsRelayout = false;
1193 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
1194 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
1196 bool userx = _props->hasValue("x");
1197 bool usery = _props->hasValue("y");
1198 bool userw = _props->hasValue("width");
1199 bool userh = _props->hasValue("height");
1201 // Let the layout widget work in the same property subtree.
1202 LayoutWidget wid(_props);
1203 wid.setDefaultFont(_font, int(_font->getPointSize()));
1206 int px, py, savex, savey;
1207 if (!userw || !userh) {
1208 wid.calcPrefSize(&pw, &ph);
1211 pw = _props->getIntValue("width", pw);
1212 ph = _props->getIntValue("height", ph);
1213 px = savex = _props->getIntValue("x", (screenw - pw) / 2);
1214 py = savey = _props->getIntValue("y", (screenh - ph) / 2);
1216 // Negative x/y coordinates are interpreted as distance from the top/right
1217 // corner rather than bottom/left.
1218 if (userx && px < 0)
1219 px = screenw - pw + px;
1220 if (usery && py < 0)
1221 py = screenh - ph + py;
1223 // Define "x", "y", "width" and/or "height" in the property tree if they
1224 // are not specified in the configuration file.
1225 wid.layout(px, py, pw, ph);
1229 // Remove automatically generated properties, so the layout looks
1230 // the same next time around, or restore x and y to preserve negative coords.
1232 _props->setIntValue("x", savex);
1234 _props->removeChild("x");
1237 _props->setIntValue("y", savey);
1239 _props->removeChild("y");
1241 if (!userw) _props->removeChild("width");
1242 if (!userh) _props->removeChild("height");
1246 FGPUIDialog::applySize(puObject *object)
1248 // compound plib widgets use setUserData() for internal purposes, so refuse
1249 // to descend into anything that has other bits set than the following
1250 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
1251 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
1252 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
1254 int type = object->getType();
1255 if ((type & PUCLASS_GROUP) && !(type & ~validUserData)) {
1256 puObject* c = ((puGroup *)object)->getFirstChild();
1257 for (; c != NULL; c = c->getNextObject()) {
1259 } // of child iteration
1260 } // of group object case
1262 GUIInfo *info = (GUIInfo *)object->getUserData();
1266 SGPropertyNode *n = info->node;
1268 SG_LOG(SG_GENERAL, SG_ALERT, "FGDialog::applySize: no props");
1272 int x = n->getIntValue("x");
1273 int y = n->getIntValue("y");
1274 int w = n->getIntValue("width", 4);
1275 int h = n->getIntValue("height", 4);
1276 object->setPosition(x, y);
1277 object->setSize(w, h);
1280 ////////////////////////////////////////////////////////////////////////
1281 // Implementation of FGDialog::PropertyObject.
1282 ////////////////////////////////////////////////////////////////////////
1284 FGPUIDialog::PropertyObject::PropertyObject(const char *n,
1285 puObject *o, SGPropertyNode_ptr p) :
1295 ////////////////////////////////////////////////////////////////////////
1296 // Implementation of fgValueList and derived pui widgets
1297 ////////////////////////////////////////////////////////////////////////
1300 fgValueList::fgValueList(SGPropertyNode *p) :
1307 fgValueList::update()
1313 fgValueList::~fgValueList()
1319 fgValueList::make_list()
1321 SGPropertyNode_ptr values = _props;
1322 const char* vname = "value";
1324 if (_props->hasChild("properties")) {
1325 // dynamic values, read from a property's children
1326 const char* path = _props->getStringValue("properties");
1327 values = fgGetNode(path, true);
1330 if (_props->hasChild("property-name")) {
1331 vname = _props->getStringValue("property-name");
1334 vector<SGPropertyNode_ptr> value_nodes = values->getChildren(vname);
1335 _list = new char *[value_nodes.size() + 1];
1337 for (i = 0; i < value_nodes.size(); i++) {
1338 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
1344 fgValueList::destroy_list()
1346 for (int i = 0; _list[i] != 0; i++)
1357 fgValueList::update();
1358 int top = getTopItem();
1363 void fgComboBox::update()
1369 std::string curValue(getStringValue());
1370 fgValueList::update();
1372 int currentItem = puaComboBox::getCurrentItem();
1375 // look for the previous value, in the new list
1376 for (int i = 0; _list[i] != 0; i++) {
1377 if (_list[i] == curValue) {
1378 // don't set the current item again; this is important to avoid
1379 // recursion here if the combo callback ultimately causes another dialog-update
1380 if (i != currentItem) {
1381 puaComboBox::setCurrentItem(i);
1386 } // of list values iteration
1388 // cound't find current item, default to first
1389 if (currentItem != 0) {
1390 puaComboBox::setCurrentItem(0);
1394 int fgComboBox::checkHit(int b, int up, int x, int y)
1397 int r = puaComboBox::checkHit(b, up, x, y);
1402 void fgComboBox::setSize(int w, int h)
1404 puaComboBox::setSize(w, h);
1408 void fgComboBox::recalc_bbox()
1410 // bug-fix for issue #192
1411 // http://code.google.com/p/flightgear-bugs/issues/detail?id=192
1412 // puaComboBox is including the height of its popupMenu in the height
1413 // computation, which breaks layout computations.
1414 // this implementation skips popup-menus
1419 for (puObject *bo = dlist; bo != NULL; bo = bo -> getNextObject()) {
1420 if (bo->getType() & PUCLASS_POPUPMENU) {
1424 contents.extend (bo -> getBBox()) ;
1427 abox.max[0] = abox.min[0] + contents.max[0] ;
1428 abox.max[1] = abox.min[1] + contents.max[1] ;
1430 puObject::recalc_bbox () ;
1434 // end of FGPUIDialog.cxx