1 // dialog.cxx: implementation of an XML-configurable dialog box.
7 #include <simgear/structure/SGBinding.hxx>
8 #include <simgear/props/props_io.hxx>
9 #include <simgear/debug/BufferedLogCallback.hxx>
10 #include <simgear/scene/tsync/terrasync.hxx>
12 #include <Scripting/NasalSys.hxx>
13 #include <Main/fg_os.hxx>
14 #include <Main/globals.hxx>
15 #include <Main/fg_props.hxx>
17 #include "FGPUIDialog.hxx"
18 #include "new_gui.hxx"
19 #include "AirportList.hxx"
20 #include "property_list.hxx"
22 #include "WaypointList.hxx"
23 #include "CanvasWidget.hxx"
24 #include "MapWidget.hxx"
25 #include "FGFontCache.hxx"
26 #include "FGColor.hxx"
28 enum format_type { f_INVALID, f_INT, f_LONG, f_FLOAT, f_DOUBLE, f_STRING };
29 static const int FORMAT_BUFSIZE = 255;
30 static const int RESIZE_MARGIN = 7;
34 * Makes sure the format matches '%[ -+#]?\d*(\.\d*)?(l?[df]|s)', with
35 * only one number or string placeholder and otherwise arbitrary prefix
36 * and postfix containing only quoted percent signs (%%).
39 validate_format(const char *f)
53 while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
55 while (*f && isdigit(*f))
59 while (*f && isdigit(*f))
67 type = l ? f_LONG : f_INT;
69 type = l ? f_DOUBLE : f_FLOAT;
88 ////////////////////////////////////////////////////////////////////////
91 // Custom subclass of puPopup to implement "draggable" windows in the
92 // interface. Note that this is a subclass of puPopup, not
93 // puDialogBox. Sadly, PUI (mis)uses subclassing to implement a
94 // boolean property: modality. That means that we can't implement
95 // dragging of both puPopup windows and puDialogBoxes with the same
96 // code. Rather than duplicate code, I've chosen to implement only
97 // "non-modal dragability" here. Modal dialog boxes (like the exit
98 // confirmation) are not draggable.
100 class fgPopup : public puPopup {
102 fgPopup(int x, int y, bool r = true, bool d = true) :
103 puPopup(x, y), _draggable(d), _resizable(r), _dragging(false)
105 int checkHit(int b, int up, int x, int y);
106 int checkKey(int key, int updown);
107 int getHitObjects(puObject *, int x, int y);
108 bool checkHitCanvas(puObject *, int x, int y);
109 puObject *getKeyObject(puObject *, int key);
110 puObject *getActiveInputField(puObject *);
111 void applySize(puObject *);
113 enum { LEFT = 1, RIGHT = 2, TOP = 4, BOTTOM = 8 };
120 int _dlgX, _dlgY, _dlgW, _dlgH;
121 int _startX, _startY;
127 fgValueList(SGPropertyNode *p);
128 virtual ~fgValueList();
129 virtual void update();
137 SGPropertyNode_ptr _props;
141 class fgList : public fgValueList, public puaList, public GUI_ID {
143 fgList(int x1, int y1, int x2, int y2, SGPropertyNode *p, int sw) :
144 fgValueList(p), puaList(x1, y1, x2, y2, _list, sw), GUI_ID(FGCLASS_LIST) {}
148 class fgComboBox : public fgValueList, public puaComboBox {
150 fgComboBox(int x1, int y1, int x2, int y2, SGPropertyNode *p, bool editable) :
152 puaComboBox(x1, y1, x2, y2, _list, editable),
158 virtual void setSize(int w, int h);
160 virtual int checkHit(int b, int up, int x, int y);
162 virtual void recalc_bbox();
167 class LogList : public puaList, public FGPUIDialog::ActiveWidget, public GUI_ID {
169 LogList(int x1, int y1, int x2, int y2, int sw) :
170 puaList(x1, y1, x2, y2, sw),
171 GUI_ID(FGCLASS_LOGLIST)
177 void setBuffer(simgear::BufferedLogCallback* buf);
179 virtual void update();
181 std::vector<unsigned char*> m_items;
182 simgear::BufferedLogCallback* m_buffer;
183 unsigned int m_stamp;
186 class fgSelectBox : public fgValueList, public puaSelectBox {
188 fgSelectBox(int x1, int y1, int x2, int y2, SGPropertyNode *p) :
189 fgValueList(p), puaSelectBox(x1, y1, x2, y2, _list) {}
192 ////////////////////////////////////////////////////////////////////////
193 // Implementation of GUIInfo.
194 ////////////////////////////////////////////////////////////////////////
197 * User data for a GUI object.
201 GUIInfo(FGPUIDialog *d);
203 void apply_format(SGPropertyNode *);
206 SGPropertyNode_ptr node;
207 vector <SGBinding *> bindings;
209 string label, legend, text, format;
210 format_type fmt_type;
213 GUIInfo::GUIInfo (FGPUIDialog *d) :
222 for (unsigned int i = 0; i < bindings.size(); i++) {
228 void GUIInfo::apply_format(SGPropertyNode *n)
230 char buf[FORMAT_BUFSIZE + 1];
231 if (fmt_type == f_INT)
232 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getIntValue());
233 else if (fmt_type == f_LONG)
234 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getLongValue());
235 else if (fmt_type == f_FLOAT)
236 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getFloatValue());
237 else if (fmt_type == f_DOUBLE)
238 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getDoubleValue());
240 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getStringValue());
242 buf[FORMAT_BUFSIZE] = '\0';
250 int fgPopup::checkKey(int key, int updown)
252 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
255 puObject *input = getActiveInputField(this);
257 return input->checkKey(key, updown);
259 puObject *object = getKeyObject(this, key);
261 return puPopup::checkKey(key, updown);
263 // invokeCallback() isn't enough; we need to simulate a mouse button press
264 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
265 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
266 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
267 object->checkHit(PU_LEFT_BUTTON, PU_UP,
268 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
269 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
273 puObject *fgPopup::getKeyObject(puObject *object, int key)
276 if (object->getType() & PUCLASS_GROUP)
277 for (puObject *obj = ((puGroup *)object)->getFirstChild();
278 obj; obj = obj->getNextObject())
279 if ((ret = getKeyObject(obj, key)))
282 GUIInfo *info = (GUIInfo *)object->getUserData();
283 if (info && info->key == key)
289 puObject *fgPopup::getActiveInputField(puObject *object)
292 if (object->getType() & PUCLASS_GROUP)
293 for (puObject *obj = ((puGroup *)object)->getFirstChild();
294 obj; obj = obj->getNextObject())
295 if ((ret = getActiveInputField(obj)))
298 if (object->getType() & (PUCLASS_INPUT|PUCLASS_LARGEINPUT)
299 && ((puInput *)object)->isAcceptingInput())
308 int fgPopup::checkHit(int button, int updown, int x, int y)
311 if (updown != PU_DRAG && !_dragging)
312 result = puPopup::checkHit(button, updown, x, y);
317 // This is annoying. We would really want a true result from the
318 // superclass to indicate "handled by child object", but all it
319 // tells us is that the pointer is inside the dialog. So do the
320 // intersection test (again) to make sure we don't start a drag
321 // when inside controls.
323 if (updown == PU_DOWN && !_dragging) {
326 int global_drag = fgGetKeyModifiers() & KEYMOD_SHIFT;
327 int global_resize = fgGetKeyModifiers() & KEYMOD_CTRL;
329 int hit = getHitObjects(this, x, y);
330 if (hit & PUCLASS_LIST) // ctrl-click in property browser (toggle bool)
333 && ( (hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
334 |PUCLASS_LARGEINPUT|PUCLASS_SCROLLBAR))
335 // The canvas should handle drag events on its own so exit
336 // here if mouse is over a CanvasWidget
337 || (!global_drag && checkHitCanvas(this, x, y))
343 getPosition(&_dlgX, &_dlgY);
344 getSize(&_dlgW, &_dlgH);
345 _start_cursor = fgGetMouseCursor();
350 // check and prepare for resizing
351 static const int cursor[] = {
352 MOUSE_CURSOR_POINTER, MOUSE_CURSOR_LEFTSIDE, MOUSE_CURSOR_RIGHTSIDE, 0,
353 MOUSE_CURSOR_TOPSIDE, MOUSE_CURSOR_TOPLEFT, MOUSE_CURSOR_TOPRIGHT, 0,
354 MOUSE_CURSOR_BOTTOMSIDE, MOUSE_CURSOR_BOTTOMLEFT, MOUSE_CURSOR_BOTTOMRIGHT, 0,
358 if (!global_drag && _resizable) {
359 int hmargin = global_resize ? _dlgW / 3 : RESIZE_MARGIN;
360 int vmargin = global_resize ? _dlgH / 3 : RESIZE_MARGIN;
362 if (y - _dlgY < vmargin)
364 else if (_dlgY + _dlgH - y < vmargin)
367 if (x - _dlgX < hmargin)
369 else if (_dlgX + _dlgW - x < hmargin)
372 if (!_resizing && global_resize)
373 _resizing = BOTTOM|RIGHT;
375 _cursor = cursor[_resizing];
376 if (_resizing && _resizable)
377 fgSetMouseCursor(_cursor);
380 } else if (updown == PU_DRAG && _dragging) {
382 GUIInfo *info = (GUIInfo *)getUserData();
383 if (_resizable && info && info->node) {
386 if (_resizing & LEFT)
388 if (_resizing & RIGHT)
392 if (_resizing & BOTTOM)
396 LayoutWidget wid(info->node);
397 wid.calcPrefSize(&prefw, &prefh);
405 if (_resizing & LEFT)
407 if (_resizing & BOTTOM)
410 wid.layout(x, y, w, h);
413 applySize(static_cast<puObject *>(this));
414 getFirstChild()->setSize(w, h); // dialog background puFrame
417 int posX = x + _dlgX - _startX,
418 posY = y + _dlgY - _startY;
419 setPosition(posX, posY);
421 GUIInfo *info = (GUIInfo *)getUserData();
422 if (info && info->node) {
423 info->node->setIntValue("x", posX);
424 info->node->setIntValue("y", posY);
428 } else if (_dragging) {
429 fgSetMouseCursor(_start_cursor);
435 int fgPopup::getHitObjects(puObject *object, int x, int y)
437 if (!object->isVisible())
441 if (object->getType() & PUCLASS_GROUP)
442 for (puObject *obj = ((puGroup *)object)->getFirstChild();
443 obj; obj = obj->getNextObject())
444 type |= getHitObjects(obj, x, y);
447 object->getAbsolutePosition(&cx, &cy);
448 object->getSize(&cw, &ch);
449 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch)
450 type |= object->getType();
454 bool fgPopup::checkHitCanvas(puObject* object, int x, int y)
456 if( !object->isVisible() )
459 if( object->getType() & PUCLASS_GROUP )
461 for( puObject* obj = ((puGroup*)object)->getFirstChild();
463 obj = obj->getNextObject() )
465 if( checkHitCanvas(obj, x, y) )
471 object->getAbsolutePosition(&cx, &cy);
472 object->getSize(&cw, &ch);
473 if( x >= cx && x < cx + cw
474 && y >= cy && y < cy + ch
475 && dynamic_cast<CanvasWidget*>(object) )
480 void fgPopup::applySize(puObject *object)
482 // compound plib widgets use setUserData() for internal purposes, so refuse
483 // to descend into anything that has other bits set than the following
484 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
485 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
486 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
488 int type = object->getType();
489 if (type & PUCLASS_GROUP && !(type & ~validUserData))
490 for (puObject *obj = ((puGroup *)object)->getFirstChild();
491 obj; obj = obj->getNextObject())
494 GUIInfo *info = (GUIInfo *)object->getUserData();
498 SGPropertyNode *n = info->node;
500 SG_LOG(SG_GENERAL, SG_ALERT, "fgPopup::applySize: no props");
503 int x = n->getIntValue("x");
504 int y = n->getIntValue("y");
505 int w = n->getIntValue("width", 4);
506 int h = n->getIntValue("height", 4);
507 object->setPosition(x, y);
508 object->setSize(w, h);
511 ////////////////////////////////////////////////////////////////////////
513 void FGPUIDialog::ConditionalObject::update(FGPUIDialog* aDlg)
515 if (_name == "visible") {
516 bool wasVis = _pu->isVisible() != 0;
517 bool newVis = test();
519 if (newVis == wasVis) {
523 if (newVis) { // puObject needs a setVisible. Oh well.
528 } else if (_name == "enable") {
529 bool wasEnabled = _pu->isActive() != 0;
530 bool newEnable = test();
532 if (wasEnabled == newEnable) {
543 aDlg->setNeedsLayout();
546 ////////////////////////////////////////////////////////////////////////
548 ////////////////////////////////////////////////////////////////////////
554 action_callback (puObject *object)
556 GUIInfo *info = (GUIInfo *)object->getUserData();
557 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
558 gui->setActiveDialog(info->dialog);
559 int nBindings = info->bindings.size();
560 for (int i = 0; i < nBindings; i++) {
561 info->bindings[i]->fire();
562 if (gui->getActiveDialog() == 0)
565 gui->setActiveDialog(0);
569 ////////////////////////////////////////////////////////////////////////
570 // Static helper functions.
571 ////////////////////////////////////////////////////////////////////////
574 * Copy a property value to a PUI object.
577 copy_to_pui (SGPropertyNode *node, puObject *object)
579 using namespace simgear;
580 GUIInfo *info = (GUIInfo *)object->getUserData();
582 SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
583 return; // this can't really happen
586 // Treat puText objects specially, so their "values" can be set
588 if (object->getType() & PUCLASS_TEXT) {
589 if (info->fmt_type != f_INVALID)
590 info->apply_format(node);
592 info->text = node->getStringValue();
594 object->setLabel(info->text.c_str());
598 switch (node->getType()) {
602 object->setValue(node->getIntValue());
606 object->setValue(node->getFloatValue());
609 info->text = node->getStringValue();
610 object->setValue(info->text.c_str());
617 copy_from_pui (puObject *object, SGPropertyNode *node)
619 using namespace simgear;
620 // puText objects are immutable, so should not be copied out
621 if (object->getType() & PUCLASS_TEXT)
624 switch (node->getType()) {
628 node->setIntValue(object->getIntegerValue());
631 node->setFloatValue(object->getFloatValue());
635 // puObject only provides float, not double, which causes precision/rounding issues
636 // with some numerical values (try "114.2").
637 // Work around: obtain string value, and manually convert with proper double precision.
638 const char *s = object->getStringValue();
639 node->setDoubleValue(atof(s));
643 const char *s = object->getStringValue();
645 node->setStringValue(s);
651 ////////////////////////////////////////////////////////////////////////
652 // Implementation of FGDialog.
653 ////////////////////////////////////////////////////////////////////////
655 FGPUIDialog::FGPUIDialog (SGPropertyNode *props) :
658 _gui((NewGUI *)globals->get_subsystem("gui")),
660 _needsRelayout(false)
662 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
664 SGPropertyNode *nasal = props->getNode("nasal");
666 _nasal_close = nasal->getNode("close");
667 SGPropertyNode *open = nasal->getNode("open");
669 const char *s = open->getStringValue();
670 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
672 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
678 FGPUIDialog::~FGPUIDialog ()
681 _object->getAbsolutePosition(&x, &y);
682 _props->setIntValue("lastx", x);
683 _props->setIntValue("lasty", y);
685 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
688 const char *s = _nasal_close->getStringValue();
689 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
691 nas->deleteModule(_module.c_str());
694 puDeleteObject(_object);
697 // Delete all the info objects we
698 // were forced to keep around because
699 // PUI cannot delete its own user data.
700 for (i = 0; i < _info.size(); i++) {
701 delete (GUIInfo *)_info[i];
704 // Finally, delete the property links.
705 for (i = 0; i < _propertyObjects.size(); i++) {
706 delete _propertyObjects[i];
707 _propertyObjects[i] = 0;
712 FGPUIDialog::updateValues (const char *objectName)
714 if (objectName && !objectName[0])
717 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
718 const string &name = _propertyObjects[i]->name;
719 if (objectName && name != objectName) {
723 puObject *widget = _propertyObjects[i]->object;
724 int widgetType = widget->getType();
725 if (widgetType & PUCLASS_LIST) {
726 GUI_ID* gui_id = dynamic_cast<GUI_ID *>(widget);
727 if (gui_id && (gui_id->id & FGCLASS_LIST)) {
728 fgList *pl = static_cast<fgList*>(widget);
731 copy_to_pui(_propertyObjects[i]->node, widget);
733 } else if (widgetType & PUCLASS_COMBOBOX) {
734 fgComboBox* combo = static_cast<fgComboBox*>(widget);
737 copy_to_pui(_propertyObjects[i]->node, widget);
739 } // of property objects iteration
743 FGPUIDialog::applyValues (const char *objectName)
745 if (objectName && !objectName[0])
748 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
749 const string &name = _propertyObjects[i]->name;
750 if (objectName && name != objectName)
753 copy_from_pui(_propertyObjects[i]->object,
754 _propertyObjects[i]->node);
759 FGPUIDialog::update ()
761 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
762 puObject *obj = _liveObjects[i]->object;
763 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
766 copy_to_pui(_liveObjects[i]->node, obj);
769 for (unsigned int j=0; j < _conditionalObjects.size(); ++j) {
770 _conditionalObjects[j]->update(this);
773 for (unsigned int i = 0; i < _activeWidgets.size(); i++) {
774 _activeWidgets[i]->update();
777 if (_needsRelayout) {
783 FGPUIDialog::display (SGPropertyNode *props)
786 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
790 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
791 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
793 bool userx = props->hasValue("x");
794 bool usery = props->hasValue("y");
795 bool userw = props->hasValue("width");
796 bool userh = props->hasValue("height");
798 // Let the layout widget work in the same property subtree.
799 LayoutWidget wid(props);
801 SGPropertyNode *fontnode = props->getNode("font");
803 FGFontCache *fc = globals->get_fontcache();
804 _font = fc->get(fontnode);
806 _font = _gui->getDefaultFont();
808 wid.setDefaultFont(_font, int(_font->getPointSize()));
811 int px, py, savex, savey;
812 if (!userw || !userh)
813 wid.calcPrefSize(&pw, &ph);
814 pw = props->getIntValue("width", pw);
815 ph = props->getIntValue("height", ph);
816 px = savex = props->getIntValue("x", (screenw - pw) / 2);
817 py = savey = props->getIntValue("y", (screenh - ph) / 2);
819 // Negative x/y coordinates are interpreted as distance from the top/right
820 // corner rather than bottom/left.
822 px = screenw - pw + px;
824 py = screenh - ph + py;
826 // Define "x", "y", "width" and/or "height" in the property tree if they
827 // are not specified in the configuration file.
828 wid.layout(px, py, pw, ph);
830 // Use the dimension and location properties as specified in the
831 // configuration file or from the layout widget.
832 _object = makeObject(props, screenw, screenh);
834 // Remove automatically generated properties, so the layout looks
835 // the same next time around, or restore x and y to preserve negative coords.
837 props->setIntValue("x", savex);
839 props->removeChild("x");
842 props->setIntValue("y", savey);
844 props->removeChild("y");
846 if (!userw) props->removeChild("width");
847 if (!userh) props->removeChild("height");
852 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
853 << props->getStringValue("name", "[unnamed]")
854 << " does not contain a proper GUI definition");
859 FGPUIDialog::makeObject (SGPropertyNode *props, int parentWidth, int parentHeight)
861 if (!props->getBoolValue("enabled", true))
864 bool presetSize = props->hasValue("width") && props->hasValue("height");
865 int width = props->getIntValue("width", parentWidth);
866 int height = props->getIntValue("height", parentHeight);
867 int x = props->getIntValue("x", (parentWidth - width) / 2);
868 int y = props->getIntValue("y", (parentHeight - height) / 2);
869 string type = props->getName();
874 if (type == "dialog") {
876 bool draggable = props->getBoolValue("draggable", true);
877 bool resizable = props->getBoolValue("resizable", false);
878 if (props->getBoolValue("modal", false))
879 obj = new puDialogBox(x, y);
881 obj = new fgPopup(x, y, resizable, draggable);
882 setupGroup(obj, props, width, height, true);
883 setColor(obj, props);
886 } else if (type == "group") {
887 puGroup *obj = new puGroup(x, y);
888 setupGroup(obj, props, width, height, false);
889 setColor(obj, props);
892 } else if (type == "frame") {
893 puGroup *obj = new puGroup(x, y);
894 setupGroup(obj, props, width, height, true);
895 setColor(obj, props);
898 } else if (type == "hrule" || type == "vrule") {
899 puFrame *obj = new puFrame(x, y, x + width, y + height);
900 obj->setBorderThickness(0);
901 setupObject(obj, props);
902 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
905 } else if (type == "list") {
906 int slider_width = props->getIntValue("slider", 20);
907 fgList *obj = new fgList(x, y, x + width, y + height, props, slider_width);
909 obj->setSize(width, height);
910 setupObject(obj, props);
911 setColor(obj, props);
914 } else if (type == "airport-list") {
915 AirportList *obj = new AirportList(x, y, x + width, y + height);
917 obj->setSize(width, height);
918 setupObject(obj, props);
919 setColor(obj, props);
922 } else if (type == "property-list") {
923 PropertyList *obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
925 obj->setSize(width, height);
926 setupObject(obj, props);
927 setColor(obj, props);
930 } else if (type == "input") {
931 puInput *obj = new puInput(x, y, x + width, y + height);
932 setupObject(obj, props);
933 setColor(obj, props, FOREGROUND|LABEL);
936 } else if (type == "text") {
937 puText *obj = new puText(x, y);
938 setupObject(obj, props);
940 // Layed-out objects need their size set, and non-layout ones
941 // get a different placement.
943 obj->setSize(width, height);
945 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
946 setColor(obj, props, LABEL);
949 } else if (type == "checkbox") {
951 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
952 setupObject(obj, props);
953 setColor(obj, props, FOREGROUND|LABEL);
956 } else if (type == "radio") {
958 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
959 setupObject(obj, props);
960 setColor(obj, props, FOREGROUND|LABEL);
963 } else if (type == "button") {
965 const char *legend = props->getStringValue("legend", "[none]");
966 if (props->getBoolValue("one-shot", true))
967 obj = new puOneShot(x, y, legend);
969 obj = new puButton(x, y, legend);
971 obj->setSize(width, height);
972 setupObject(obj, props);
973 setColor(obj, props);
975 } else if (type == "map") {
976 MapWidget* mapWidget = new MapWidget(x, y, x + width, y + height);
977 setupObject(mapWidget, props);
979 } else if (type == "canvas") {
980 CanvasWidget* canvasWidget = new CanvasWidget( x, y,
981 x + width, y + height,
984 setupObject(canvasWidget, props);
986 } else if (type == "combo") {
987 fgComboBox *obj = new fgComboBox(x, y, x + width, y + height, props,
988 props->getBoolValue("editable", false));
989 setupObject(obj, props);
990 setColor(obj, props, EDITFIELD);
993 } else if (type == "slider") {
994 bool vertical = props->getBoolValue("vertical", false);
995 puSlider *obj = new puSlider(x, y, (vertical ? height : width), vertical);
996 obj->setMinValue(props->getFloatValue("min", 0.0));
997 obj->setMaxValue(props->getFloatValue("max", 1.0));
998 obj->setStepSize(props->getFloatValue("step"));
999 obj->setSliderFraction(props->getFloatValue("fraction"));
1000 #if PLIB_VERSION > 185
1001 obj->setPageStepSize(props->getFloatValue("pagestep"));
1003 setupObject(obj, props);
1005 obj->setSize(width, height);
1006 setColor(obj, props, FOREGROUND|LABEL);
1009 } else if (type == "dial") {
1010 puDial *obj = new puDial(x, y, width);
1011 obj->setMinValue(props->getFloatValue("min", 0.0));
1012 obj->setMaxValue(props->getFloatValue("max", 1.0));
1013 obj->setWrap(props->getBoolValue("wrap", true));
1014 setupObject(obj, props);
1015 setColor(obj, props, FOREGROUND|LABEL);
1018 } else if (type == "textbox") {
1019 int slider_width = props->getIntValue("slider", 20);
1020 int wrap = props->getBoolValue("wrap", true);
1021 #if PLIB_VERSION > 185
1022 puaLargeInput *obj = new puaLargeInput(x, y,
1023 x + width, x + height, 11, slider_width, wrap);
1025 puaLargeInput *obj = new puaLargeInput(x, y,
1026 x + width, x + height, 2, slider_width, wrap);
1029 if (props->getBoolValue("editable"))
1032 obj->disableInput();
1035 obj->setSize(width, height);
1036 setupObject(obj, props);
1037 setColor(obj, props, FOREGROUND|LABEL);
1039 int top = props->getIntValue("top-line", 0);
1040 obj->setTopLineInWindow(top < 0 ? unsigned(-1) >> 1 : top);
1043 } else if (type == "select") {
1044 fgSelectBox *obj = new fgSelectBox(x, y, x + width, y + height, props);
1045 setupObject(obj, props);
1046 setColor(obj, props, EDITFIELD);
1048 } else if (type == "waypointlist") {
1049 ScrolledWaypointList* obj = new ScrolledWaypointList(x, y, width, height);
1050 setupObject(obj, props);
1053 } else if (type == "loglist") {
1054 LogList* obj = new LogList(x, y, width, height, 20);
1055 string logClass = props->getStringValue("logclass");
1056 if (logClass == "terrasync") {
1057 simgear::SGTerraSync* tsync = (simgear::SGTerraSync*) globals->get_subsystem("terrasync");
1058 obj->setBuffer(tsync->log());
1060 FGNasalSys* nasal = (FGNasalSys*) globals->get_subsystem("nasal");
1061 obj->setBuffer(nasal->log());
1064 setupObject(obj, props);
1065 _activeWidgets.push_back(obj);
1073 FGPUIDialog::setupObject (puObject *object, SGPropertyNode *props)
1075 GUIInfo *info = new GUIInfo(this);
1076 object->setUserData(info);
1077 _info.push_back(info);
1078 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
1079 object->makeReturnDefault(props->getBoolValue("default"));
1082 if (props->hasValue("legend")) {
1083 info->legend = props->getStringValue("legend");
1084 object->setLegend(info->legend.c_str());
1087 if (props->hasValue("label")) {
1088 info->label = props->getStringValue("label");
1089 object->setLabel(info->label.c_str());
1092 if (props->hasValue("border"))
1093 object->setBorderThickness( props->getIntValue("border", 2) );
1095 if (SGPropertyNode *nft = props->getNode("font", false)) {
1096 FGFontCache *fc = globals->get_fontcache();
1097 puFont *lfnt = fc->get(nft);
1098 object->setLabelFont(*lfnt);
1099 object->setLegendFont(*lfnt);
1101 object->setLabelFont(*_font);
1104 if (props->hasChild("visible")) {
1105 ConditionalObject* cnd = new ConditionalObject("visible", object);
1106 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("visible")));
1107 _conditionalObjects.push_back(cnd);
1110 if (props->hasChild("enable")) {
1111 ConditionalObject* cnd = new ConditionalObject("enable", object);
1112 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("enable")));
1113 _conditionalObjects.push_back(cnd);
1116 string type = props->getName();
1117 if (type == "input" && props->getBoolValue("live"))
1118 object->setDownCallback(action_callback);
1120 if (type == "text") {
1121 const char *format = props->getStringValue("format", 0);
1123 info->fmt_type = validate_format(format);
1124 if (info->fmt_type != f_INVALID)
1125 info->format = format;
1127 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
1132 if (props->hasValue("property")) {
1133 const char *name = props->getStringValue("name");
1136 const char *propname = props->getStringValue("property");
1137 SGPropertyNode_ptr node = fgGetNode(propname, true);
1138 if (type == "map") {
1139 // mapWidget binds to a sub-tree of properties, and
1140 // ignores the puValue mechanism, so special case things here
1141 MapWidget* mw = static_cast<MapWidget*>(object);
1142 mw->setProperty(node);
1144 // normal widget, creating PropertyObject
1145 copy_to_pui(node, object);
1146 PropertyObject *po = new PropertyObject(name, object, node);
1147 _propertyObjects.push_back(po);
1148 if (props->getBoolValue("live"))
1149 _liveObjects.push_back(po);
1155 SGPropertyNode *dest = fgGetNode("/sim/bindings/gui", true);
1156 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
1157 if (bindings.size() > 0) {
1158 info->key = props->getIntValue("keynum", -1);
1159 if (props->hasValue("key"))
1160 info->key = getKeyCode(props->getStringValue("key", ""));
1162 for (unsigned int i = 0; i < bindings.size(); i++) {
1164 SGPropertyNode_ptr binding;
1165 while (dest->getChild("binding", j))
1168 const char *cmd = bindings[i]->getStringValue("command");
1169 if (!strcmp(cmd, "nasal"))
1170 bindings[i]->setStringValue("module", _module.c_str());
1172 binding = dest->getChild("binding", j, true);
1173 copyProperties(bindings[i], binding);
1174 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
1176 object->setCallback(action_callback);
1181 FGPUIDialog::setupGroup(puGroup *group, SGPropertyNode *props,
1182 int width, int height, bool makeFrame)
1184 setupObject(group, props);
1187 puFrame* f = new puFrame(0, 0, width, height);
1191 int nChildren = props->nChildren();
1192 for (int i = 0; i < nChildren; i++)
1193 makeObject(props->getChild(i), width, height);
1198 FGPUIDialog::setColor(puObject *object, SGPropertyNode *props, int which)
1200 string type = props->getName();
1203 if (type == "textbox" && props->getBoolValue("editable"))
1204 type += "-editable";
1206 FGColor c(_gui->getColor("background"));
1207 c.merge(_gui->getColor(type));
1208 c.merge(props->getNode("color"));
1210 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
1218 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
1219 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
1220 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
1221 { LABEL, PUCOL_LABEL, "label", "color-label" },
1222 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
1223 { MISC, PUCOL_MISC, "misc", "color-misc" },
1224 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
1227 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
1229 for (int i = 0; i < numcol; i++) {
1234 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
1235 if (which & pucol[i].mask)
1236 dirty |= c.merge(props->getNode("color"));
1238 if ((pucol[i].mask == LABEL) && !c.isValid())
1239 dirty |= c.merge(_gui->getColor("label"));
1241 dirty |= c.merge(props->getNode(pucol[i].cname));
1243 if (c.isValid() && dirty)
1244 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
1273 {"f10", PU_KEY_F10},
1274 {"f11", PU_KEY_F11},
1275 {"f12", PU_KEY_F12},
1276 {"left", PU_KEY_LEFT},
1278 {"right", PU_KEY_RIGHT},
1279 {"down", PU_KEY_DOWN},
1280 {"pageup", PU_KEY_PAGE_UP},
1281 {"pagedn", PU_KEY_PAGE_DOWN},
1282 {"home", PU_KEY_HOME},
1283 {"end", PU_KEY_END},
1284 {"insert", PU_KEY_INSERT},
1289 FGPUIDialog::getKeyCode(const char *str)
1300 char *buf = new char[strlen(str) + 1];
1302 char *s = buf + strlen(buf);
1303 while (s > str && s[-1] == ' ')
1310 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
1311 s += 5, mod |= CTRL;
1312 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
1313 s += 6, mod |= SHIFT;
1314 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
1321 if (strlen(s) == 1 && isascii(*s)) {
1326 key = toupper(key) - '@';
1328 ; // Alt not propagated to the gui
1331 for (char *t = s; *t; t++)
1333 for (int i = 0; keymap[i].name; i++) {
1334 if (!strcmp(s, keymap[i].name)) {
1335 key = keymap[i].key;
1344 void FGPUIDialog::relayout()
1346 _needsRelayout = false;
1348 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
1349 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
1351 bool userx = _props->hasValue("x");
1352 bool usery = _props->hasValue("y");
1353 bool userw = _props->hasValue("width");
1354 bool userh = _props->hasValue("height");
1356 // Let the layout widget work in the same property subtree.
1357 LayoutWidget wid(_props);
1358 wid.setDefaultFont(_font, int(_font->getPointSize()));
1361 int px, py, savex, savey;
1362 if (!userw || !userh) {
1363 wid.calcPrefSize(&pw, &ph);
1366 pw = _props->getIntValue("width", pw);
1367 ph = _props->getIntValue("height", ph);
1368 px = savex = _props->getIntValue("x", (screenw - pw) / 2);
1369 py = savey = _props->getIntValue("y", (screenh - ph) / 2);
1371 // Negative x/y coordinates are interpreted as distance from the top/right
1372 // corner rather than bottom/left.
1373 if (userx && px < 0)
1374 px = screenw - pw + px;
1375 if (usery && py < 0)
1376 py = screenh - ph + py;
1378 // Define "x", "y", "width" and/or "height" in the property tree if they
1379 // are not specified in the configuration file.
1380 wid.layout(px, py, pw, ph);
1384 // Remove automatically generated properties, so the layout looks
1385 // the same next time around, or restore x and y to preserve negative coords.
1387 _props->setIntValue("x", savex);
1389 _props->removeChild("x");
1392 _props->setIntValue("y", savey);
1394 _props->removeChild("y");
1396 if (!userw) _props->removeChild("width");
1397 if (!userh) _props->removeChild("height");
1401 FGPUIDialog::applySize(puObject *object)
1403 // compound plib widgets use setUserData() for internal purposes, so refuse
1404 // to descend into anything that has other bits set than the following
1405 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
1406 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
1407 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
1409 int type = object->getType();
1410 if ((type & PUCLASS_GROUP) && !(type & ~validUserData)) {
1411 puObject* c = ((puGroup *)object)->getFirstChild();
1412 for (; c != NULL; c = c->getNextObject()) {
1414 } // of child iteration
1415 } // of group object case
1417 GUIInfo *info = (GUIInfo *)object->getUserData();
1421 SGPropertyNode *n = info->node;
1423 SG_LOG(SG_GENERAL, SG_ALERT, "FGDialog::applySize: no props");
1427 int x = n->getIntValue("x");
1428 int y = n->getIntValue("y");
1429 int w = n->getIntValue("width", 4);
1430 int h = n->getIntValue("height", 4);
1431 object->setPosition(x, y);
1432 object->setSize(w, h);
1435 ////////////////////////////////////////////////////////////////////////
1436 // Implementation of FGDialog::PropertyObject.
1437 ////////////////////////////////////////////////////////////////////////
1439 FGPUIDialog::PropertyObject::PropertyObject(const char *n,
1440 puObject *o, SGPropertyNode_ptr p) :
1449 ////////////////////////////////////////////////////////////////////////
1450 // Implementation of fgValueList and derived pui widgets
1451 ////////////////////////////////////////////////////////////////////////
1454 fgValueList::fgValueList(SGPropertyNode *p) :
1461 fgValueList::update()
1467 fgValueList::~fgValueList()
1473 fgValueList::make_list()
1475 SGPropertyNode_ptr values = _props;
1476 const char* vname = "value";
1478 if (_props->hasChild("properties")) {
1479 // dynamic values, read from a property's children
1480 const char* path = _props->getStringValue("properties");
1481 values = fgGetNode(path, true);
1484 if (_props->hasChild("property-name")) {
1485 vname = _props->getStringValue("property-name");
1488 vector<SGPropertyNode_ptr> value_nodes = values->getChildren(vname);
1489 _list = new char *[value_nodes.size() + 1];
1491 for (i = 0; i < value_nodes.size(); i++) {
1492 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
1498 fgValueList::destroy_list()
1500 for (int i = 0; _list[i] != 0; i++)
1511 fgValueList::update();
1512 int top = getTopItem();
1517 ////////////////////////////////////////////////////////////////////////
1518 // Implementation of fgLogList
1519 ////////////////////////////////////////////////////////////////////////
1521 void LogList::update()
1523 if (!m_buffer) return;
1525 if (m_stamp != m_buffer->stamp()) {
1526 m_stamp = m_buffer->threadsafeCopy(m_items);
1527 m_items.push_back(NULL); // terminator value
1528 newList((char**) m_items.data());
1529 setTopItem(m_items.size() - 1); // scroll to bottom of list
1533 void LogList::setBuffer(simgear::BufferedLogCallback* buf)
1536 m_stamp = m_buffer->stamp() - 1; // force an update
1540 ////////////////////////////////////////////////////////////////////////
1541 // Implementation of fgComboBox
1542 ////////////////////////////////////////////////////////////////////////
1545 void fgComboBox::update()
1551 std::string curValue(getStringValue());
1552 fgValueList::update();
1554 int currentItem = puaComboBox::getCurrentItem();
1557 // look for the previous value, in the new list
1558 for (int i = 0; _list[i] != 0; i++) {
1559 if (_list[i] == curValue) {
1560 // don't set the current item again; this is important to avoid
1561 // recursion here if the combo callback ultimately causes another dialog-update
1562 if (i != currentItem) {
1563 puaComboBox::setCurrentItem(i);
1568 } // of list values iteration
1570 // cound't find current item, default to first
1571 if (currentItem != 0) {
1572 puaComboBox::setCurrentItem(0);
1576 int fgComboBox::checkHit(int b, int up, int x, int y)
1579 int r = puaComboBox::checkHit(b, up, x, y);
1584 void fgComboBox::setSize(int w, int h)
1586 puaComboBox::setSize(w, h);
1590 void fgComboBox::recalc_bbox()
1592 // bug-fix for issue #192
1593 // http://code.google.com/p/flightgear-bugs/issues/detail?id=192
1594 // puaComboBox is including the height of its popupMenu in the height
1595 // computation, which breaks layout computations.
1596 // this implementation skips popup-menus
1601 for (puObject *bo = dlist; bo != NULL; bo = bo -> getNextObject()) {
1602 if (bo->getType() & PUCLASS_POPUPMENU) {
1606 contents.extend (bo -> getBBox()) ;
1609 abox.max[0] = abox.min[0] + contents.max[0] ;
1610 abox.max[1] = abox.min[1] + contents.max[1] ;
1612 puObject::recalc_bbox () ;
1616 // end of FGPUIDialog.cxx