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"
30 enum format_type { f_INVALID, f_INT, f_LONG, f_FLOAT, f_DOUBLE, f_STRING };
31 static const int FORMAT_BUFSIZE = 255;
32 static const int RESIZE_MARGIN = 7;
36 * Makes sure the format matches '%[ -+#]?\d*(\.\d*)?(l?[df]|s)', with
37 * only one number or string placeholder and otherwise arbitrary prefix
38 * and postfix containing only quoted percent signs (%%).
41 validate_format(const char *f)
55 while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
57 while (*f && isdigit(*f))
61 while (*f && isdigit(*f))
69 type = l ? f_LONG : f_INT;
71 type = l ? f_DOUBLE : f_FLOAT;
90 ////////////////////////////////////////////////////////////////////////
93 // Custom subclass of puPopup to implement "draggable" windows in the
94 // interface. Note that this is a subclass of puPopup, not
95 // puDialogBox. Sadly, PUI (mis)uses subclassing to implement a
96 // boolean property: modality. That means that we can't implement
97 // dragging of both puPopup windows and puDialogBoxes with the same
98 // code. Rather than duplicate code, I've chosen to implement only
99 // "non-modal dragability" here. Modal dialog boxes (like the exit
100 // confirmation) are not draggable.
102 class fgPopup : public puPopup {
104 fgPopup(int x, int y, bool r = true, bool d = true) :
105 puPopup(x, y), _draggable(d), _resizable(r), _dragging(false)
107 int checkHit(int b, int up, int x, int y);
108 int checkKey(int key, int updown);
109 int getHitObjects(puObject *, int x, int y);
110 bool checkHitCanvas(puObject *, int x, int y);
111 puObject *getKeyObject(puObject *, int key);
112 puObject *getActiveInputField(puObject *);
113 void applySize(puObject *);
115 enum { LEFT = 1, RIGHT = 2, TOP = 4, BOTTOM = 8 };
122 int _dlgX, _dlgY, _dlgW, _dlgH;
123 int _startX, _startY;
129 fgValueList(SGPropertyNode *p);
130 virtual ~fgValueList();
131 virtual void update();
139 SGPropertyNode_ptr _props;
143 class fgList : public fgValueList, public puaList, public GUI_ID {
145 fgList(int x1, int y1, int x2, int y2, SGPropertyNode *p, int sw) :
146 fgValueList(p), puaList(x1, y1, x2, y2, _list, sw), GUI_ID(FGCLASS_LIST) {}
150 class fgComboBox : public fgValueList, public puaComboBox {
152 fgComboBox(int x1, int y1, int x2, int y2, SGPropertyNode *p, bool editable) :
154 puaComboBox(x1, y1, x2, y2, _list, editable),
160 virtual void setSize(int w, int h);
162 virtual int checkHit(int b, int up, int x, int y);
164 virtual void recalc_bbox();
169 class LogList : public puaList, public FGPUIDialog::ActiveWidget, public GUI_ID {
171 LogList(int x1, int y1, int x2, int y2, int sw) :
172 puaList(x1, y1, x2, y2, sw),
173 GUI_ID(FGCLASS_LOGLIST)
179 void setBuffer(simgear::BufferedLogCallback* buf);
181 virtual void update();
183 std::vector<unsigned char*> m_items;
184 simgear::BufferedLogCallback* m_buffer;
185 unsigned int m_stamp;
188 class fgSelectBox : public fgValueList, public puaSelectBox {
190 fgSelectBox(int x1, int y1, int x2, int y2, SGPropertyNode *p) :
191 fgValueList(p), puaSelectBox(x1, y1, x2, y2, _list) {}
194 ////////////////////////////////////////////////////////////////////////
195 // Implementation of GUIInfo.
196 ////////////////////////////////////////////////////////////////////////
199 * User data for a GUI object.
203 GUIInfo(FGPUIDialog *d);
205 void apply_format(SGPropertyNode *);
208 SGPropertyNode_ptr node;
209 std::vector <SGBinding *> bindings;
211 string label, legend, text, format;
212 format_type fmt_type;
215 GUIInfo::GUIInfo (FGPUIDialog *d) :
224 for (unsigned int i = 0; i < bindings.size(); i++) {
230 void GUIInfo::apply_format(SGPropertyNode *n)
232 char buf[FORMAT_BUFSIZE + 1];
233 if (fmt_type == f_INT)
234 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getIntValue());
235 else if (fmt_type == f_LONG)
236 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getLongValue());
237 else if (fmt_type == f_FLOAT)
238 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getFloatValue());
239 else if (fmt_type == f_DOUBLE)
240 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getDoubleValue());
242 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getStringValue());
244 buf[FORMAT_BUFSIZE] = '\0';
252 int fgPopup::checkKey(int key, int updown)
254 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
257 puObject *input = getActiveInputField(this);
259 return input->checkKey(key, updown);
261 puObject *object = getKeyObject(this, key);
263 return puPopup::checkKey(key, updown);
265 // invokeCallback() isn't enough; we need to simulate a mouse button press
266 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
267 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
268 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
269 object->checkHit(PU_LEFT_BUTTON, PU_UP,
270 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
271 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
275 puObject *fgPopup::getKeyObject(puObject *object, int key)
278 if (object->getType() & PUCLASS_GROUP)
279 for (puObject *obj = ((puGroup *)object)->getFirstChild();
280 obj; obj = obj->getNextObject())
281 if ((ret = getKeyObject(obj, key)))
284 GUIInfo *info = (GUIInfo *)object->getUserData();
285 if (info && info->key == key)
291 puObject *fgPopup::getActiveInputField(puObject *object)
294 if (object->getType() & PUCLASS_GROUP)
295 for (puObject *obj = ((puGroup *)object)->getFirstChild();
296 obj; obj = obj->getNextObject())
297 if ((ret = getActiveInputField(obj)))
300 if (object->getType() & (PUCLASS_INPUT|PUCLASS_LARGEINPUT)
301 && ((puInput *)object)->isAcceptingInput())
310 int fgPopup::checkHit(int button, int updown, int x, int y)
313 if (updown != PU_DRAG && !_dragging)
314 result = puPopup::checkHit(button, updown, x, y);
319 // This is annoying. We would really want a true result from the
320 // superclass to indicate "handled by child object", but all it
321 // tells us is that the pointer is inside the dialog. So do the
322 // intersection test (again) to make sure we don't start a drag
323 // when inside controls.
325 if (updown == PU_DOWN && !_dragging) {
328 int global_drag = fgGetKeyModifiers() & KEYMOD_SHIFT;
329 int global_resize = fgGetKeyModifiers() & KEYMOD_CTRL;
331 int hit = getHitObjects(this, x, y);
332 if (hit & PUCLASS_LIST) // ctrl-click in property browser (toggle bool)
335 && ( (hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
336 |PUCLASS_LARGEINPUT|PUCLASS_SCROLLBAR))
337 // The canvas should handle drag events on its own so exit
338 // here if mouse is over a CanvasWidget
339 || (!global_drag && checkHitCanvas(this, x, y))
345 getPosition(&_dlgX, &_dlgY);
346 getSize(&_dlgW, &_dlgH);
347 _start_cursor = fgGetMouseCursor();
352 // check and prepare for resizing
353 static const int cursor[] = {
354 MOUSE_CURSOR_POINTER, MOUSE_CURSOR_LEFTSIDE, MOUSE_CURSOR_RIGHTSIDE, 0,
355 MOUSE_CURSOR_TOPSIDE, MOUSE_CURSOR_TOPLEFT, MOUSE_CURSOR_TOPRIGHT, 0,
356 MOUSE_CURSOR_BOTTOMSIDE, MOUSE_CURSOR_BOTTOMLEFT, MOUSE_CURSOR_BOTTOMRIGHT, 0,
360 if (!global_drag && _resizable) {
361 int hmargin = global_resize ? _dlgW / 3 : RESIZE_MARGIN;
362 int vmargin = global_resize ? _dlgH / 3 : RESIZE_MARGIN;
364 if (y - _dlgY < vmargin)
366 else if (_dlgY + _dlgH - y < vmargin)
369 if (x - _dlgX < hmargin)
371 else if (_dlgX + _dlgW - x < hmargin)
374 if (!_resizing && global_resize)
375 _resizing = BOTTOM|RIGHT;
377 _cursor = cursor[_resizing];
378 if (_resizing && _resizable)
379 fgSetMouseCursor(_cursor);
382 } else if (updown == PU_DRAG && _dragging) {
384 GUIInfo *info = (GUIInfo *)getUserData();
385 if (_resizable && info && info->node) {
388 if (_resizing & LEFT)
390 if (_resizing & RIGHT)
394 if (_resizing & BOTTOM)
398 LayoutWidget wid(info->node);
399 wid.calcPrefSize(&prefw, &prefh);
407 if (_resizing & LEFT)
409 if (_resizing & BOTTOM)
412 wid.layout(x, y, w, h);
415 applySize(static_cast<puObject *>(this));
416 getFirstChild()->setSize(w, h); // dialog background puFrame
419 int posX = x + _dlgX - _startX,
420 posY = y + _dlgY - _startY;
421 setPosition(posX, posY);
423 GUIInfo *info = (GUIInfo *)getUserData();
424 if (info && info->node) {
425 info->node->setIntValue("x", posX);
426 info->node->setIntValue("y", posY);
430 } else if (_dragging) {
431 fgSetMouseCursor(_start_cursor);
437 int fgPopup::getHitObjects(puObject *object, int x, int y)
439 if (!object->isVisible())
443 if (object->getType() & PUCLASS_GROUP)
444 for (puObject *obj = ((puGroup *)object)->getFirstChild();
445 obj; obj = obj->getNextObject())
446 type |= getHitObjects(obj, x, y);
449 object->getAbsolutePosition(&cx, &cy);
450 object->getSize(&cw, &ch);
451 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch)
452 type |= object->getType();
456 bool fgPopup::checkHitCanvas(puObject* object, int x, int y)
458 if( !object->isVisible() )
461 if( object->getType() & PUCLASS_GROUP )
463 for( puObject* obj = ((puGroup*)object)->getFirstChild();
465 obj = obj->getNextObject() )
467 if( checkHitCanvas(obj, x, y) )
473 object->getAbsolutePosition(&cx, &cy);
474 object->getSize(&cw, &ch);
475 if( x >= cx && x < cx + cw
476 && y >= cy && y < cy + ch
477 && dynamic_cast<CanvasWidget*>(object) )
482 void fgPopup::applySize(puObject *object)
484 // compound plib widgets use setUserData() for internal purposes, so refuse
485 // to descend into anything that has other bits set than the following
486 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
487 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
488 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
490 int type = object->getType();
491 if (type & PUCLASS_GROUP && !(type & ~validUserData))
492 for (puObject *obj = ((puGroup *)object)->getFirstChild();
493 obj; obj = obj->getNextObject())
496 GUIInfo *info = (GUIInfo *)object->getUserData();
500 SGPropertyNode *n = info->node;
502 SG_LOG(SG_GENERAL, SG_ALERT, "fgPopup::applySize: no props");
505 int x = n->getIntValue("x");
506 int y = n->getIntValue("y");
507 int w = n->getIntValue("width", 4);
508 int h = n->getIntValue("height", 4);
509 object->setPosition(x, y);
510 object->setSize(w, h);
513 ////////////////////////////////////////////////////////////////////////
515 void FGPUIDialog::ConditionalObject::update(FGPUIDialog* aDlg)
517 if (_name == "visible") {
518 bool wasVis = _pu->isVisible() != 0;
519 bool newVis = test();
521 if (newVis == wasVis) {
525 if (newVis) { // puObject needs a setVisible. Oh well.
530 } else if (_name == "enable") {
531 bool wasEnabled = _pu->isActive() != 0;
532 bool newEnable = test();
534 if (wasEnabled == newEnable) {
545 aDlg->setNeedsLayout();
548 ////////////////////////////////////////////////////////////////////////
550 ////////////////////////////////////////////////////////////////////////
556 action_callback (puObject *object)
558 GUIInfo *info = (GUIInfo *)object->getUserData();
559 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
560 gui->setActiveDialog(info->dialog);
561 int nBindings = info->bindings.size();
562 for (int i = 0; i < nBindings; i++) {
563 info->bindings[i]->fire();
564 if (gui->getActiveDialog() == 0)
567 gui->setActiveDialog(0);
571 ////////////////////////////////////////////////////////////////////////
572 // Static helper functions.
573 ////////////////////////////////////////////////////////////////////////
576 * Copy a property value to a PUI object.
579 copy_to_pui (SGPropertyNode *node, puObject *object)
581 using namespace simgear;
582 GUIInfo *info = (GUIInfo *)object->getUserData();
584 SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
585 return; // this can't really happen
588 // Treat puText objects specially, so their "values" can be set
590 if (object->getType() & PUCLASS_TEXT) {
591 if (info->fmt_type != f_INVALID)
592 info->apply_format(node);
594 info->text = node->getStringValue();
596 object->setLabel(info->text.c_str());
600 switch (node->getType()) {
604 object->setValue(node->getIntValue());
608 object->setValue(node->getFloatValue());
611 info->text = node->getStringValue();
612 object->setValue(info->text.c_str());
619 copy_from_pui (puObject *object, SGPropertyNode *node)
621 using namespace simgear;
622 // puText objects are immutable, so should not be copied out
623 if (object->getType() & PUCLASS_TEXT)
626 switch (node->getType()) {
630 node->setIntValue(object->getIntegerValue());
633 node->setFloatValue(object->getFloatValue());
637 // puObject only provides float, not double, which causes precision/rounding issues
638 // with some numerical values (try "114.2").
639 // Work around: obtain string value, and manually convert with proper double precision.
640 const char *s = object->getStringValue();
641 node->setDoubleValue(atof(s));
645 const char *s = object->getStringValue();
647 node->setStringValue(s);
653 ////////////////////////////////////////////////////////////////////////
654 // Implementation of FGDialog.
655 ////////////////////////////////////////////////////////////////////////
657 FGPUIDialog::FGPUIDialog (SGPropertyNode *props) :
660 _gui((NewGUI *)globals->get_subsystem("gui")),
662 _needsRelayout(false)
664 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
666 SGPropertyNode *nasal = props->getNode("nasal");
668 _nasal_close = nasal->getNode("close");
669 SGPropertyNode *open = nasal->getNode("open");
671 const char *s = open->getStringValue();
672 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
674 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
680 FGPUIDialog::~FGPUIDialog ()
683 _object->getAbsolutePosition(&x, &y);
684 _props->setIntValue("lastx", x);
685 _props->setIntValue("lasty", y);
687 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
690 const char *s = _nasal_close->getStringValue();
691 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
693 nas->deleteModule(_module.c_str());
696 puDeleteObject(_object);
699 // Delete all the info objects we
700 // were forced to keep around because
701 // PUI cannot delete its own user data.
702 for (i = 0; i < _info.size(); i++) {
703 delete (GUIInfo *)_info[i];
706 // Finally, delete the property links.
707 for (i = 0; i < _propertyObjects.size(); i++) {
708 delete _propertyObjects[i];
709 _propertyObjects[i] = 0;
714 FGPUIDialog::updateValues (const char *objectName)
716 if (objectName && !objectName[0])
719 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
720 const string &name = _propertyObjects[i]->name;
721 if (objectName && name != objectName) {
725 puObject *widget = _propertyObjects[i]->object;
726 int widgetType = widget->getType();
727 if (widgetType & PUCLASS_LIST) {
728 GUI_ID* gui_id = dynamic_cast<GUI_ID *>(widget);
729 if (gui_id && (gui_id->id & FGCLASS_LIST)) {
730 fgList *pl = static_cast<fgList*>(widget);
733 copy_to_pui(_propertyObjects[i]->node, widget);
735 } else if (widgetType & PUCLASS_COMBOBOX) {
736 fgComboBox* combo = static_cast<fgComboBox*>(widget);
739 copy_to_pui(_propertyObjects[i]->node, widget);
741 } // of property objects iteration
745 FGPUIDialog::applyValues (const char *objectName)
747 if (objectName && !objectName[0])
750 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
751 const string &name = _propertyObjects[i]->name;
752 if (objectName && name != objectName)
755 copy_from_pui(_propertyObjects[i]->object,
756 _propertyObjects[i]->node);
761 FGPUIDialog::update ()
763 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
764 puObject *obj = _liveObjects[i]->object;
765 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
768 copy_to_pui(_liveObjects[i]->node, obj);
771 for (unsigned int j=0; j < _conditionalObjects.size(); ++j) {
772 _conditionalObjects[j]->update(this);
775 for (unsigned int i = 0; i < _activeWidgets.size(); i++) {
776 _activeWidgets[i]->update();
779 if (_needsRelayout) {
785 FGPUIDialog::display (SGPropertyNode *props)
788 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
792 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
793 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
795 bool userx = props->hasValue("x");
796 bool usery = props->hasValue("y");
797 bool userw = props->hasValue("width");
798 bool userh = props->hasValue("height");
800 // Let the layout widget work in the same property subtree.
801 LayoutWidget wid(props);
803 SGPropertyNode *fontnode = props->getNode("font");
805 FGFontCache *fc = globals->get_fontcache();
806 _font = fc->get(fontnode);
808 _font = _gui->getDefaultFont();
810 wid.setDefaultFont(_font, int(_font->getPointSize()));
813 int px, py, savex, savey;
814 if (!userw || !userh)
815 wid.calcPrefSize(&pw, &ph);
816 pw = props->getIntValue("width", pw);
817 ph = props->getIntValue("height", ph);
818 px = savex = props->getIntValue("x", (screenw - pw) / 2);
819 py = savey = props->getIntValue("y", (screenh - ph) / 2);
821 // Negative x/y coordinates are interpreted as distance from the top/right
822 // corner rather than bottom/left.
824 px = screenw - pw + px;
826 py = screenh - ph + py;
828 // Define "x", "y", "width" and/or "height" in the property tree if they
829 // are not specified in the configuration file.
830 wid.layout(px, py, pw, ph);
832 // Use the dimension and location properties as specified in the
833 // configuration file or from the layout widget.
834 _object = makeObject(props, screenw, screenh);
836 // Remove automatically generated properties, so the layout looks
837 // the same next time around, or restore x and y to preserve negative coords.
839 props->setIntValue("x", savex);
841 props->removeChild("x");
844 props->setIntValue("y", savey);
846 props->removeChild("y");
848 if (!userw) props->removeChild("width");
849 if (!userh) props->removeChild("height");
854 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
855 << props->getStringValue("name", "[unnamed]")
856 << " does not contain a proper GUI definition");
861 FGPUIDialog::makeObject (SGPropertyNode *props, int parentWidth, int parentHeight)
863 if (!props->getBoolValue("enabled", true))
866 bool presetSize = props->hasValue("width") && props->hasValue("height");
867 int width = props->getIntValue("width", parentWidth);
868 int height = props->getIntValue("height", parentHeight);
869 int x = props->getIntValue("x", (parentWidth - width) / 2);
870 int y = props->getIntValue("y", (parentHeight - height) / 2);
871 string type = props->getName();
876 if (type == "dialog") {
878 bool draggable = props->getBoolValue("draggable", true);
879 bool resizable = props->getBoolValue("resizable", false);
880 if (props->getBoolValue("modal", false))
881 obj = new puDialogBox(x, y);
883 obj = new fgPopup(x, y, resizable, draggable);
884 setupGroup(obj, props, width, height, true);
885 setColor(obj, props);
888 } else if (type == "group") {
889 puGroup *obj = new puGroup(x, y);
890 setupGroup(obj, props, width, height, false);
891 setColor(obj, props);
894 } else if (type == "frame") {
895 puGroup *obj = new puGroup(x, y);
896 setupGroup(obj, props, width, height, true);
897 setColor(obj, props);
900 } else if (type == "hrule" || type == "vrule") {
901 puFrame *obj = new puFrame(x, y, x + width, y + height);
902 obj->setBorderThickness(0);
903 setupObject(obj, props);
904 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
907 } else if (type == "list") {
908 int slider_width = props->getIntValue("slider", 20);
909 fgList *obj = new fgList(x, y, x + width, y + height, props, slider_width);
911 obj->setSize(width, height);
912 setupObject(obj, props);
913 setColor(obj, props);
916 } else if (type == "airport-list") {
917 AirportList *obj = new AirportList(x, y, x + width, y + height);
919 obj->setSize(width, height);
920 setupObject(obj, props);
921 setColor(obj, props);
924 } else if (type == "property-list") {
925 PropertyList *obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
927 obj->setSize(width, height);
928 setupObject(obj, props);
929 setColor(obj, props);
932 } else if (type == "input") {
933 puInput *obj = new puInput(x, y, x + width, y + height);
934 setupObject(obj, props);
935 setColor(obj, props, FOREGROUND|LABEL);
938 } else if (type == "text") {
939 puText *obj = new puText(x, y);
940 setupObject(obj, props);
942 // Layed-out objects need their size set, and non-layout ones
943 // get a different placement.
945 obj->setSize(width, height);
947 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
948 setColor(obj, props, LABEL);
951 } else if (type == "checkbox") {
953 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
954 setupObject(obj, props);
955 setColor(obj, props, FOREGROUND|LABEL);
958 } else if (type == "radio") {
960 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
961 setupObject(obj, props);
962 setColor(obj, props, FOREGROUND|LABEL);
965 } else if (type == "button") {
967 const char *legend = props->getStringValue("legend", "[none]");
968 if (props->getBoolValue("one-shot", true))
969 obj = new puOneShot(x, y, legend);
971 obj = new puButton(x, y, legend);
973 obj->setSize(width, height);
974 setupObject(obj, props);
975 setColor(obj, props);
977 } else if (type == "map") {
978 MapWidget* mapWidget = new MapWidget(x, y, x + width, y + height);
979 setupObject(mapWidget, props);
981 } else if (type == "canvas") {
982 CanvasWidget* canvasWidget = new CanvasWidget( x, y,
983 x + width, y + height,
986 setupObject(canvasWidget, props);
988 } else if (type == "combo") {
989 fgComboBox *obj = new fgComboBox(x, y, x + width, y + height, props,
990 props->getBoolValue("editable", false));
991 setupObject(obj, props);
992 setColor(obj, props, EDITFIELD);
995 } else if (type == "slider") {
996 bool vertical = props->getBoolValue("vertical", false);
997 puSlider *obj = new puSlider(x, y, (vertical ? height : width), vertical);
998 obj->setMinValue(props->getFloatValue("min", 0.0));
999 obj->setMaxValue(props->getFloatValue("max", 1.0));
1000 obj->setStepSize(props->getFloatValue("step"));
1001 obj->setSliderFraction(props->getFloatValue("fraction"));
1002 #if PLIB_VERSION > 185
1003 obj->setPageStepSize(props->getFloatValue("pagestep"));
1005 setupObject(obj, props);
1007 obj->setSize(width, height);
1008 setColor(obj, props, FOREGROUND|LABEL);
1011 } else if (type == "dial") {
1012 puDial *obj = new puDial(x, y, width);
1013 obj->setMinValue(props->getFloatValue("min", 0.0));
1014 obj->setMaxValue(props->getFloatValue("max", 1.0));
1015 obj->setWrap(props->getBoolValue("wrap", true));
1016 setupObject(obj, props);
1017 setColor(obj, props, FOREGROUND|LABEL);
1020 } else if (type == "textbox") {
1021 int slider_width = props->getIntValue("slider", 20);
1022 int wrap = props->getBoolValue("wrap", true);
1023 #if PLIB_VERSION > 185
1024 puaLargeInput *obj = new puaLargeInput(x, y,
1025 x + width, x + height, 11, slider_width, wrap);
1027 puaLargeInput *obj = new puaLargeInput(x, y,
1028 x + width, x + height, 2, slider_width, wrap);
1031 if (props->getBoolValue("editable"))
1034 obj->disableInput();
1037 obj->setSize(width, height);
1038 setupObject(obj, props);
1039 setColor(obj, props, FOREGROUND|LABEL);
1041 int top = props->getIntValue("top-line", 0);
1042 obj->setTopLineInWindow(top < 0 ? unsigned(-1) >> 1 : top);
1045 } else if (type == "select") {
1046 fgSelectBox *obj = new fgSelectBox(x, y, x + width, y + height, props);
1047 setupObject(obj, props);
1048 setColor(obj, props, EDITFIELD);
1050 } else if (type == "waypointlist") {
1051 ScrolledWaypointList* obj = new ScrolledWaypointList(x, y, width, height);
1052 setupObject(obj, props);
1055 } else if (type == "loglist") {
1056 LogList* obj = new LogList(x, y, width, height, 20);
1057 string logClass = props->getStringValue("logclass");
1058 if (logClass == "terrasync") {
1059 simgear::SGTerraSync* tsync = (simgear::SGTerraSync*) globals->get_subsystem("terrasync");
1061 obj->setBuffer(tsync->log());
1064 FGNasalSys* nasal = (FGNasalSys*) globals->get_subsystem("nasal");
1065 obj->setBuffer(nasal->log());
1068 setupObject(obj, props);
1069 _activeWidgets.push_back(obj);
1077 FGPUIDialog::setupObject (puObject *object, SGPropertyNode *props)
1079 GUIInfo *info = new GUIInfo(this);
1080 object->setUserData(info);
1081 _info.push_back(info);
1082 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
1083 object->makeReturnDefault(props->getBoolValue("default"));
1086 if (props->hasValue("legend")) {
1087 info->legend = props->getStringValue("legend");
1088 object->setLegend(info->legend.c_str());
1091 if (props->hasValue("label")) {
1092 info->label = props->getStringValue("label");
1093 object->setLabel(info->label.c_str());
1096 if (props->hasValue("border"))
1097 object->setBorderThickness( props->getIntValue("border", 2) );
1099 if (SGPropertyNode *nft = props->getNode("font", false)) {
1100 FGFontCache *fc = globals->get_fontcache();
1101 puFont *lfnt = fc->get(nft);
1102 object->setLabelFont(*lfnt);
1103 object->setLegendFont(*lfnt);
1105 object->setLabelFont(*_font);
1108 if (props->hasChild("visible")) {
1109 ConditionalObject* cnd = new ConditionalObject("visible", object);
1110 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("visible")));
1111 _conditionalObjects.push_back(cnd);
1114 if (props->hasChild("enable")) {
1115 ConditionalObject* cnd = new ConditionalObject("enable", object);
1116 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("enable")));
1117 _conditionalObjects.push_back(cnd);
1120 string type = props->getName();
1121 if (type == "input" && props->getBoolValue("live"))
1122 object->setDownCallback(action_callback);
1124 if (type == "text") {
1125 const char *format = props->getStringValue("format", 0);
1127 info->fmt_type = validate_format(format);
1128 if (info->fmt_type != f_INVALID)
1129 info->format = format;
1131 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
1136 if (props->hasValue("property")) {
1137 const char *name = props->getStringValue("name");
1140 const char *propname = props->getStringValue("property");
1141 SGPropertyNode_ptr node = fgGetNode(propname, true);
1142 if (type == "map") {
1143 // mapWidget binds to a sub-tree of properties, and
1144 // ignores the puValue mechanism, so special case things here
1145 MapWidget* mw = static_cast<MapWidget*>(object);
1146 mw->setProperty(node);
1148 // normal widget, creating PropertyObject
1149 copy_to_pui(node, object);
1150 PropertyObject *po = new PropertyObject(name, object, node);
1151 _propertyObjects.push_back(po);
1152 if (props->getBoolValue("live"))
1153 _liveObjects.push_back(po);
1159 SGPropertyNode *dest = fgGetNode("/sim/bindings/gui", true);
1160 std::vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
1161 if (bindings.size() > 0) {
1162 info->key = props->getIntValue("keynum", -1);
1163 if (props->hasValue("key"))
1164 info->key = getKeyCode(props->getStringValue("key", ""));
1166 for (unsigned int i = 0; i < bindings.size(); i++) {
1168 SGPropertyNode_ptr binding;
1169 while (dest->getChild("binding", j))
1172 const char *cmd = bindings[i]->getStringValue("command");
1173 if (!strcmp(cmd, "nasal"))
1174 bindings[i]->setStringValue("module", _module.c_str());
1176 binding = dest->getChild("binding", j, true);
1177 copyProperties(bindings[i], binding);
1178 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
1180 object->setCallback(action_callback);
1185 FGPUIDialog::setupGroup(puGroup *group, SGPropertyNode *props,
1186 int width, int height, bool makeFrame)
1188 setupObject(group, props);
1191 puFrame* f = new puFrame(0, 0, width, height);
1195 int nChildren = props->nChildren();
1196 for (int i = 0; i < nChildren; i++)
1197 makeObject(props->getChild(i), width, height);
1202 FGPUIDialog::setColor(puObject *object, SGPropertyNode *props, int which)
1204 string type = props->getName();
1207 if (type == "textbox" && props->getBoolValue("editable"))
1208 type += "-editable";
1210 FGColor c(_gui->getColor("background"));
1211 c.merge(_gui->getColor(type));
1212 c.merge(props->getNode("color"));
1214 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
1222 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
1223 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
1224 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
1225 { LABEL, PUCOL_LABEL, "label", "color-label" },
1226 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
1227 { MISC, PUCOL_MISC, "misc", "color-misc" },
1228 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
1231 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
1233 for (int i = 0; i < numcol; i++) {
1238 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
1239 if (which & pucol[i].mask)
1240 dirty |= c.merge(props->getNode("color"));
1242 if ((pucol[i].mask == LABEL) && !c.isValid())
1243 dirty |= c.merge(_gui->getColor("label"));
1245 dirty |= c.merge(props->getNode(pucol[i].cname));
1247 if (c.isValid() && dirty)
1248 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
1277 {"f10", PU_KEY_F10},
1278 {"f11", PU_KEY_F11},
1279 {"f12", PU_KEY_F12},
1280 {"left", PU_KEY_LEFT},
1282 {"right", PU_KEY_RIGHT},
1283 {"down", PU_KEY_DOWN},
1284 {"pageup", PU_KEY_PAGE_UP},
1285 {"pagedn", PU_KEY_PAGE_DOWN},
1286 {"home", PU_KEY_HOME},
1287 {"end", PU_KEY_END},
1288 {"insert", PU_KEY_INSERT},
1293 FGPUIDialog::getKeyCode(const char *str)
1304 char *buf = new char[strlen(str) + 1];
1306 char *s = buf + strlen(buf);
1307 while (s > str && s[-1] == ' ')
1314 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
1315 s += 5, mod |= CTRL;
1316 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
1317 s += 6, mod |= SHIFT;
1318 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
1325 if (strlen(s) == 1 && isascii(*s)) {
1330 key = toupper(key) - '@';
1332 ; // Alt not propagated to the gui
1335 for (char *t = s; *t; t++)
1337 for (int i = 0; keymap[i].name; i++) {
1338 if (!strcmp(s, keymap[i].name)) {
1339 key = keymap[i].key;
1348 void FGPUIDialog::relayout()
1350 _needsRelayout = false;
1352 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
1353 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
1355 bool userx = _props->hasValue("x");
1356 bool usery = _props->hasValue("y");
1357 bool userw = _props->hasValue("width");
1358 bool userh = _props->hasValue("height");
1360 // Let the layout widget work in the same property subtree.
1361 LayoutWidget wid(_props);
1362 wid.setDefaultFont(_font, int(_font->getPointSize()));
1365 int px, py, savex, savey;
1366 if (!userw || !userh) {
1367 wid.calcPrefSize(&pw, &ph);
1370 pw = _props->getIntValue("width", pw);
1371 ph = _props->getIntValue("height", ph);
1372 px = savex = _props->getIntValue("x", (screenw - pw) / 2);
1373 py = savey = _props->getIntValue("y", (screenh - ph) / 2);
1375 // Negative x/y coordinates are interpreted as distance from the top/right
1376 // corner rather than bottom/left.
1377 if (userx && px < 0)
1378 px = screenw - pw + px;
1379 if (usery && py < 0)
1380 py = screenh - ph + py;
1382 // Define "x", "y", "width" and/or "height" in the property tree if they
1383 // are not specified in the configuration file.
1384 wid.layout(px, py, pw, ph);
1388 // Remove automatically generated properties, so the layout looks
1389 // the same next time around, or restore x and y to preserve negative coords.
1391 _props->setIntValue("x", savex);
1393 _props->removeChild("x");
1396 _props->setIntValue("y", savey);
1398 _props->removeChild("y");
1400 if (!userw) _props->removeChild("width");
1401 if (!userh) _props->removeChild("height");
1405 FGPUIDialog::applySize(puObject *object)
1407 // compound plib widgets use setUserData() for internal purposes, so refuse
1408 // to descend into anything that has other bits set than the following
1409 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
1410 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
1411 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
1413 int type = object->getType();
1414 if ((type & PUCLASS_GROUP) && !(type & ~validUserData)) {
1415 puObject* c = ((puGroup *)object)->getFirstChild();
1416 for (; c != NULL; c = c->getNextObject()) {
1418 } // of child iteration
1419 } // of group object case
1421 GUIInfo *info = (GUIInfo *)object->getUserData();
1425 SGPropertyNode *n = info->node;
1427 SG_LOG(SG_GENERAL, SG_ALERT, "FGDialog::applySize: no props");
1431 int x = n->getIntValue("x");
1432 int y = n->getIntValue("y");
1433 int w = n->getIntValue("width", 4);
1434 int h = n->getIntValue("height", 4);
1435 object->setPosition(x, y);
1436 object->setSize(w, h);
1439 ////////////////////////////////////////////////////////////////////////
1440 // Implementation of FGDialog::PropertyObject.
1441 ////////////////////////////////////////////////////////////////////////
1443 FGPUIDialog::PropertyObject::PropertyObject(const char *n,
1444 puObject *o, SGPropertyNode_ptr p) :
1453 ////////////////////////////////////////////////////////////////////////
1454 // Implementation of fgValueList and derived pui widgets
1455 ////////////////////////////////////////////////////////////////////////
1458 fgValueList::fgValueList(SGPropertyNode *p) :
1465 fgValueList::update()
1471 fgValueList::~fgValueList()
1477 fgValueList::make_list()
1479 SGPropertyNode_ptr values = _props;
1480 const char* vname = "value";
1482 if (_props->hasChild("properties")) {
1483 // dynamic values, read from a property's children
1484 const char* path = _props->getStringValue("properties");
1485 values = fgGetNode(path, true);
1488 if (_props->hasChild("property-name")) {
1489 vname = _props->getStringValue("property-name");
1492 std::vector<SGPropertyNode_ptr> value_nodes = values->getChildren(vname);
1493 _list = new char *[value_nodes.size() + 1];
1495 for (i = 0; i < value_nodes.size(); i++) {
1496 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
1502 fgValueList::destroy_list()
1504 for (int i = 0; _list[i] != 0; i++)
1515 fgValueList::update();
1516 int top = getTopItem();
1521 ////////////////////////////////////////////////////////////////////////
1522 // Implementation of fgLogList
1523 ////////////////////////////////////////////////////////////////////////
1525 void LogList::update()
1527 if (!m_buffer) return;
1529 if (m_stamp != m_buffer->stamp()) {
1530 m_stamp = m_buffer->threadsafeCopy(m_items);
1531 m_items.push_back(NULL); // terminator value
1532 newList((char**) m_items.data());
1533 setTopItem(m_items.size() - 1); // scroll to bottom of list
1537 void LogList::setBuffer(simgear::BufferedLogCallback* buf)
1540 m_stamp = m_buffer->stamp() - 1; // force an update
1544 ////////////////////////////////////////////////////////////////////////
1545 // Implementation of fgComboBox
1546 ////////////////////////////////////////////////////////////////////////
1549 void fgComboBox::update()
1555 std::string curValue(getStringValue());
1556 fgValueList::update();
1558 int currentItem = puaComboBox::getCurrentItem();
1561 // look for the previous value, in the new list
1562 for (int i = 0; _list[i] != 0; i++) {
1563 if (_list[i] == curValue) {
1564 // don't set the current item again; this is important to avoid
1565 // recursion here if the combo callback ultimately causes another dialog-update
1566 if (i != currentItem) {
1567 puaComboBox::setCurrentItem(i);
1572 } // of list values iteration
1574 // cound't find current item, default to first
1575 if (currentItem != 0) {
1576 puaComboBox::setCurrentItem(0);
1580 int fgComboBox::checkHit(int b, int up, int x, int y)
1583 int r = puaComboBox::checkHit(b, up, x, y);
1588 void fgComboBox::setSize(int w, int h)
1590 puaComboBox::setSize(w, h);
1594 void fgComboBox::recalc_bbox()
1596 // bug-fix for issue #192
1597 // http://code.google.com/p/flightgear-bugs/issues/detail?id=192
1598 // puaComboBox is including the height of its popupMenu in the height
1599 // computation, which breaks layout computations.
1600 // this implementation skips popup-menus
1605 for (puObject *bo = dlist; bo != NULL; bo = bo -> getNextObject()) {
1606 if (bo->getType() & PUCLASS_POPUPMENU) {
1610 contents.extend (bo -> getBBox()) ;
1613 abox.max[0] = abox.min[0] + contents.max[0] ;
1614 abox.max[1] = abox.min[1] + contents.max[1] ;
1616 puObject::recalc_bbox () ;
1620 // end of FGPUIDialog.cxx