1 // dialog.cxx: implementation of an XML-configurable dialog box.
7 #include <simgear/structure/SGBinding.hxx>
8 #include <Scripting/NasalSys.hxx>
9 #include <Main/fg_os.hxx>
12 #include "new_gui.hxx"
13 #include "AirportList.hxx"
14 #include "property_list.hxx"
16 #include "WaypointList.hxx"
17 #include "MapWidget.hxx"
19 enum format_type { f_INVALID, f_INT, f_LONG, f_FLOAT, f_DOUBLE, f_STRING };
20 static const int FORMAT_BUFSIZE = 255;
21 static const int RESIZE_MARGIN = 7;
25 * Makes sure the format matches '%[ -+#]?\d*(\.\d*)?(l?[df]|s)', with
26 * only one number or string placeholder and otherwise arbitrary prefix
27 * and postfix containing only quoted percent signs (%%).
30 validate_format(const char *f)
44 while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
46 while (*f && isdigit(*f))
50 while (*f && isdigit(*f))
58 type = l ? f_LONG : f_INT;
60 type = l ? f_DOUBLE : f_FLOAT;
80 ////////////////////////////////////////////////////////////////////////
81 // Implementation of GUIInfo.
82 ////////////////////////////////////////////////////////////////////////
85 * User data for a GUI object.
91 void apply_format(SGPropertyNode *);
94 SGPropertyNode_ptr node;
95 vector <SGBinding *> bindings;
97 string label, legend, text, format;
101 GUIInfo::GUIInfo (FGDialog *d) :
110 for (unsigned int i = 0; i < bindings.size(); i++) {
116 void GUIInfo::apply_format(SGPropertyNode *n)
118 char buf[FORMAT_BUFSIZE + 1];
119 if (fmt_type == f_INT)
120 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getIntValue());
121 else if (fmt_type == f_LONG)
122 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getLongValue());
123 else if (fmt_type == f_FLOAT)
124 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getFloatValue());
125 else if (fmt_type == f_DOUBLE)
126 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getDoubleValue());
128 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getStringValue());
130 buf[FORMAT_BUFSIZE] = '\0';
139 int fgPopup::checkKey(int key, int updown)
141 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
144 puObject *input = getActiveInputField(this);
146 return input->checkKey(key, updown);
148 puObject *object = getKeyObject(this, key);
150 return puPopup::checkKey(key, updown);
152 // invokeCallback() isn't enough; we need to simulate a mouse button press
153 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
154 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
155 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
156 object->checkHit(PU_LEFT_BUTTON, PU_UP,
157 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
158 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
162 puObject *fgPopup::getKeyObject(puObject *object, int key)
165 if (object->getType() & PUCLASS_GROUP)
166 for (puObject *obj = ((puGroup *)object)->getFirstChild();
167 obj; obj = obj->getNextObject())
168 if ((ret = getKeyObject(obj, key)))
171 GUIInfo *info = (GUIInfo *)object->getUserData();
172 if (info && info->key == key)
178 puObject *fgPopup::getActiveInputField(puObject *object)
181 if (object->getType() & PUCLASS_GROUP)
182 for (puObject *obj = ((puGroup *)object)->getFirstChild();
183 obj; obj = obj->getNextObject())
184 if ((ret = getActiveInputField(obj)))
187 if (object->getType() & (PUCLASS_INPUT|PUCLASS_LARGEINPUT)
188 && ((puInput *)object)->isAcceptingInput())
197 int fgPopup::checkHit(int button, int updown, int x, int y)
200 if (updown != PU_DRAG && !_dragging)
201 result = puPopup::checkHit(button, updown, x, y);
206 // This is annoying. We would really want a true result from the
207 // superclass to indicate "handled by child object", but all it
208 // tells us is that the pointer is inside the dialog. So do the
209 // intersection test (again) to make sure we don't start a drag
210 // when inside controls.
212 if (updown == PU_DOWN && !_dragging) {
215 int global_drag = fgGetKeyModifiers() & KEYMOD_SHIFT;
216 int global_resize = fgGetKeyModifiers() & KEYMOD_CTRL;
218 int hit = getHitObjects(this, x, y);
219 if (hit & PUCLASS_LIST) // ctrl-click in property browser (toggle bool)
221 if (!global_resize && hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
222 |PUCLASS_LARGEINPUT|PUCLASS_SCROLLBAR))
225 getPosition(&_dlgX, &_dlgY);
226 getSize(&_dlgW, &_dlgH);
227 _start_cursor = fgGetMouseCursor();
232 // check and prepare for resizing
233 static const int cursor[] = {
234 MOUSE_CURSOR_POINTER, MOUSE_CURSOR_LEFTSIDE, MOUSE_CURSOR_RIGHTSIDE, 0,
235 MOUSE_CURSOR_TOPSIDE, MOUSE_CURSOR_TOPLEFT, MOUSE_CURSOR_TOPRIGHT, 0,
236 MOUSE_CURSOR_BOTTOMSIDE, MOUSE_CURSOR_BOTTOMLEFT, MOUSE_CURSOR_BOTTOMRIGHT, 0,
240 if (!global_drag && _resizable) {
241 int hmargin = global_resize ? _dlgW / 3 : RESIZE_MARGIN;
242 int vmargin = global_resize ? _dlgH / 3 : RESIZE_MARGIN;
244 if (y - _dlgY < vmargin)
246 else if (_dlgY + _dlgH - y < vmargin)
249 if (x - _dlgX < hmargin)
251 else if (_dlgX + _dlgW - x < hmargin)
254 if (!_resizing && global_resize)
255 _resizing = BOTTOM|RIGHT;
257 _cursor = cursor[_resizing];
258 if (_resizing && _resizable)
259 fgSetMouseCursor(_cursor);
262 } else if (updown == PU_DRAG && _dragging) {
264 GUIInfo *info = (GUIInfo *)getUserData();
265 if (_resizable && info && info->node) {
268 if (_resizing & LEFT)
270 if (_resizing & RIGHT)
274 if (_resizing & BOTTOM)
278 LayoutWidget wid(info->node);
279 wid.calcPrefSize(&prefw, &prefh);
287 if (_resizing & LEFT)
289 if (_resizing & BOTTOM)
292 wid.layout(x, y, w, h);
295 applySize(static_cast<puObject *>(this));
296 getFirstChild()->setSize(w, h); // dialog background puFrame
299 int posX = x + _dlgX - _startX,
300 posY = y + _dlgY - _startY;
301 setPosition(posX, posY);
303 GUIInfo *info = (GUIInfo *)getUserData();
304 if (info && info->node) {
305 info->node->setIntValue("x", posX);
306 info->node->setIntValue("y", posY);
310 } else if (_dragging) {
311 fgSetMouseCursor(_start_cursor);
317 int fgPopup::getHitObjects(puObject *object, int x, int y)
319 if (!object->isVisible())
323 if (object->getType() & PUCLASS_GROUP)
324 for (puObject *obj = ((puGroup *)object)->getFirstChild();
325 obj; obj = obj->getNextObject())
326 type |= getHitObjects(obj, x, y);
329 object->getAbsolutePosition(&cx, &cy);
330 object->getSize(&cw, &ch);
331 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch)
332 type |= object->getType();
336 void fgPopup::applySize(puObject *object)
338 // compound plib widgets use setUserData() for internal purposes, so refuse
339 // to descend into anything that has other bits set than the following
340 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
341 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
342 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
344 int type = object->getType();
345 if (type & PUCLASS_GROUP && !(type & ~validUserData))
346 for (puObject *obj = ((puGroup *)object)->getFirstChild();
347 obj; obj = obj->getNextObject())
350 GUIInfo *info = (GUIInfo *)object->getUserData();
354 SGPropertyNode *n = info->node;
356 SG_LOG(SG_GENERAL, SG_ALERT, "fgPopup::applySize: no props");
359 int x = n->getIntValue("x");
360 int y = n->getIntValue("y");
361 int w = n->getIntValue("width", 4);
362 int h = n->getIntValue("height", 4);
363 object->setPosition(x, y);
364 object->setSize(w, h);
367 ////////////////////////////////////////////////////////////////////////
369 void FGDialog::ConditionalObject::update(FGDialog* aDlg)
371 if (_name == "visible") {
372 bool wasVis = _pu->isVisible() != 0;
373 bool newVis = test();
375 if (newVis == wasVis) {
379 if (newVis) { // puObject needs a setVisible. Oh well.
384 } else if (_name == "enable") {
385 bool wasEnabled = _pu->isActive() != 0;
386 bool newEnable = test();
388 if (wasEnabled == newEnable) {
399 aDlg->setNeedsLayout();
401 \f////////////////////////////////////////////////////////////////////////
403 ////////////////////////////////////////////////////////////////////////
409 action_callback (puObject *object)
411 GUIInfo *info = (GUIInfo *)object->getUserData();
412 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
413 gui->setActiveDialog(info->dialog);
414 int nBindings = info->bindings.size();
415 for (int i = 0; i < nBindings; i++) {
416 info->bindings[i]->fire();
417 if (gui->getActiveDialog() == 0)
420 gui->setActiveDialog(0);
425 ////////////////////////////////////////////////////////////////////////
426 // Static helper functions.
427 ////////////////////////////////////////////////////////////////////////
430 * Copy a property value to a PUI object.
433 copy_to_pui (SGPropertyNode *node, puObject *object)
435 using namespace simgear;
436 GUIInfo *info = (GUIInfo *)object->getUserData();
438 SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
439 return; // this can't really happen
442 // Treat puText objects specially, so their "values" can be set
444 if (object->getType() & PUCLASS_TEXT) {
445 if (info->fmt_type != f_INVALID)
446 info->apply_format(node);
448 info->text = node->getStringValue();
450 object->setLabel(info->text.c_str());
454 switch (node->getType()) {
458 object->setValue(node->getIntValue());
462 object->setValue(node->getFloatValue());
465 info->text = node->getStringValue();
466 object->setValue(info->text.c_str());
473 copy_from_pui (puObject *object, SGPropertyNode *node)
475 using namespace simgear;
476 // puText objects are immutable, so should not be copied out
477 if (object->getType() & PUCLASS_TEXT)
480 switch (node->getType()) {
484 node->setIntValue(object->getIntegerValue());
488 node->setFloatValue(object->getFloatValue());
491 const char *s = object->getStringValue();
493 node->setStringValue(s);
500 ////////////////////////////////////////////////////////////////////////
501 // Implementation of FGDialog.
502 ////////////////////////////////////////////////////////////////////////
504 FGDialog::FGDialog (SGPropertyNode *props) :
506 _gui((NewGUI *)globals->get_subsystem("gui")),
508 _needsRelayout(false)
510 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
512 SGPropertyNode *nasal = props->getNode("nasal");
514 _nasal_close = nasal->getNode("close");
515 SGPropertyNode *open = nasal->getNode("open");
517 const char *s = open->getStringValue();
518 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
520 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
526 FGDialog::~FGDialog ()
529 _object->getAbsolutePosition(&x, &y);
530 _props->setIntValue("lastx", x);
531 _props->setIntValue("lasty", y);
533 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
536 const char *s = _nasal_close->getStringValue();
537 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
539 nas->deleteModule(_module.c_str());
542 puDeleteObject(_object);
545 // Delete all the info objects we
546 // were forced to keep around because
547 // PUI cannot delete its own user data.
548 for (i = 0; i < _info.size(); i++) {
549 delete (GUIInfo *)_info[i];
552 // Finally, delete the property links.
553 for (i = 0; i < _propertyObjects.size(); i++) {
554 delete _propertyObjects[i];
555 _propertyObjects[i] = 0;
560 FGDialog::updateValues (const char *objectName)
562 if (objectName && !objectName[0])
565 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
566 const string &name = _propertyObjects[i]->name;
567 if (objectName && name != objectName) {
571 puObject *widget = _propertyObjects[i]->object;
572 int widgetType = widget->getType();
573 if (widgetType & PUCLASS_LIST) {
574 GUI_ID* gui_id = dynamic_cast<GUI_ID *>(widget);
575 if (gui_id && (gui_id->id & FGCLASS_LIST)) {
576 fgList *pl = static_cast<fgList*>(widget);
579 copy_to_pui(_propertyObjects[i]->node, widget);
581 } else if (widgetType & PUCLASS_COMBOBOX) {
582 fgComboBox* combo = static_cast<fgComboBox*>(widget);
585 copy_to_pui(_propertyObjects[i]->node, widget);
587 } // of property objects iteration
591 FGDialog::applyValues (const char *objectName)
593 if (objectName && !objectName[0])
596 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
597 const string &name = _propertyObjects[i]->name;
598 if (objectName && name != objectName)
601 copy_from_pui(_propertyObjects[i]->object,
602 _propertyObjects[i]->node);
609 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
610 puObject *obj = _liveObjects[i]->object;
611 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
614 copy_to_pui(_liveObjects[i]->node, obj);
617 for (unsigned int j=0; j < _conditionalObjects.size(); ++j) {
618 _conditionalObjects[j]->update(this);
621 if (_needsRelayout) {
627 FGDialog::display (SGPropertyNode *props)
630 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
634 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
635 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
637 bool userx = props->hasValue("x");
638 bool usery = props->hasValue("y");
639 bool userw = props->hasValue("width");
640 bool userh = props->hasValue("height");
642 // Let the layout widget work in the same property subtree.
643 LayoutWidget wid(props);
645 SGPropertyNode *fontnode = props->getNode("font");
647 FGFontCache *fc = globals->get_fontcache();
648 _font = fc->get(fontnode);
650 _font = _gui->getDefaultFont();
652 wid.setDefaultFont(_font, int(_font->getPointSize()));
655 int px, py, savex, savey;
656 if (!userw || !userh)
657 wid.calcPrefSize(&pw, &ph);
658 pw = props->getIntValue("width", pw);
659 ph = props->getIntValue("height", ph);
660 px = savex = props->getIntValue("x", (screenw - pw) / 2);
661 py = savey = props->getIntValue("y", (screenh - ph) / 2);
663 // Negative x/y coordinates are interpreted as distance from the top/right
664 // corner rather than bottom/left.
666 px = screenw - pw + px;
668 py = screenh - ph + py;
670 // Define "x", "y", "width" and/or "height" in the property tree if they
671 // are not specified in the configuration file.
672 wid.layout(px, py, pw, ph);
674 // Use the dimension and location properties as specified in the
675 // configuration file or from the layout widget.
676 _object = makeObject(props, screenw, screenh);
678 // Remove automatically generated properties, so the layout looks
679 // the same next time around, or restore x and y to preserve negative coords.
681 props->setIntValue("x", savex);
683 props->removeChild("x");
686 props->setIntValue("y", savey);
688 props->removeChild("y");
690 if (!userw) props->removeChild("width");
691 if (!userh) props->removeChild("height");
696 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
697 << props->getStringValue("name", "[unnamed]")
698 << " does not contain a proper GUI definition");
703 FGDialog::makeObject (SGPropertyNode *props, int parentWidth, int parentHeight)
705 if (!props->getBoolValue("enabled", true))
708 bool presetSize = props->hasValue("width") && props->hasValue("height");
709 int width = props->getIntValue("width", parentWidth);
710 int height = props->getIntValue("height", parentHeight);
711 int x = props->getIntValue("x", (parentWidth - width) / 2);
712 int y = props->getIntValue("y", (parentHeight - height) / 2);
713 string type = props->getName();
718 if (type == "dialog") {
720 bool draggable = props->getBoolValue("draggable", true);
721 bool resizable = props->getBoolValue("resizable", false);
722 if (props->getBoolValue("modal", false))
723 obj = new puDialogBox(x, y);
725 obj = new fgPopup(x, y, resizable, draggable);
726 setupGroup(obj, props, width, height, true);
727 setColor(obj, props);
730 } else if (type == "group") {
731 puGroup *obj = new puGroup(x, y);
732 setupGroup(obj, props, width, height, false);
733 setColor(obj, props);
736 } else if (type == "frame") {
737 puGroup *obj = new puGroup(x, y);
738 setupGroup(obj, props, width, height, true);
739 setColor(obj, props);
742 } else if (type == "hrule" || type == "vrule") {
743 puFrame *obj = new puFrame(x, y, x + width, y + height);
744 obj->setBorderThickness(0);
745 setupObject(obj, props);
746 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
749 } else if (type == "list") {
750 int slider_width = props->getIntValue("slider", 20);
751 fgList *obj = new fgList(x, y, x + width, y + height, props, slider_width);
753 obj->setSize(width, height);
754 setupObject(obj, props);
755 setColor(obj, props);
758 } else if (type == "airport-list") {
759 AirportList *obj = new AirportList(x, y, x + width, y + height);
761 obj->setSize(width, height);
762 setupObject(obj, props);
763 setColor(obj, props);
766 } else if (type == "property-list") {
767 PropertyList *obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
769 obj->setSize(width, height);
770 setupObject(obj, props);
771 setColor(obj, props);
774 } else if (type == "input") {
775 puInput *obj = new puInput(x, y, x + width, y + height);
776 setupObject(obj, props);
777 setColor(obj, props, FOREGROUND|LABEL);
780 } else if (type == "text") {
781 puText *obj = new puText(x, y);
782 setupObject(obj, props);
784 // Layed-out objects need their size set, and non-layout ones
785 // get a different placement.
787 obj->setSize(width, height);
789 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
790 setColor(obj, props, LABEL);
793 } else if (type == "checkbox") {
795 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
796 setupObject(obj, props);
797 setColor(obj, props, FOREGROUND|LABEL);
800 } else if (type == "radio") {
802 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
803 setupObject(obj, props);
804 setColor(obj, props, FOREGROUND|LABEL);
807 } else if (type == "button") {
809 const char *legend = props->getStringValue("legend", "[none]");
810 if (props->getBoolValue("one-shot", true))
811 obj = new puOneShot(x, y, legend);
813 obj = new puButton(x, y, legend);
815 obj->setSize(width, height);
816 setupObject(obj, props);
817 setColor(obj, props);
819 } else if (type == "map") {
820 MapWidget* mapWidget = new MapWidget(x, y, x + width, y + height);
821 setupObject(mapWidget, props);
823 } else if (type == "combo") {
824 fgComboBox *obj = new fgComboBox(x, y, x + width, y + height, props,
825 props->getBoolValue("editable", false));
826 setupObject(obj, props);
827 setColor(obj, props, EDITFIELD);
830 } else if (type == "slider") {
831 bool vertical = props->getBoolValue("vertical", false);
832 puSlider *obj = new puSlider(x, y, (vertical ? height : width), vertical);
833 obj->setMinValue(props->getFloatValue("min", 0.0));
834 obj->setMaxValue(props->getFloatValue("max", 1.0));
835 obj->setStepSize(props->getFloatValue("step"));
836 obj->setSliderFraction(props->getFloatValue("fraction"));
837 #if PLIB_VERSION > 185
838 obj->setPageStepSize(props->getFloatValue("pagestep"));
840 setupObject(obj, props);
842 obj->setSize(width, height);
843 setColor(obj, props, FOREGROUND|LABEL);
846 } else if (type == "dial") {
847 puDial *obj = new puDial(x, y, width);
848 obj->setMinValue(props->getFloatValue("min", 0.0));
849 obj->setMaxValue(props->getFloatValue("max", 1.0));
850 obj->setWrap(props->getBoolValue("wrap", true));
851 setupObject(obj, props);
852 setColor(obj, props, FOREGROUND|LABEL);
855 } else if (type == "textbox") {
856 int slider_width = props->getIntValue("slider", 20);
857 int wrap = props->getBoolValue("wrap", true);
858 #if PLIB_VERSION > 185
859 puaLargeInput *obj = new puaLargeInput(x, y,
860 x + width, x + height, 11, slider_width, wrap);
862 puaLargeInput *obj = new puaLargeInput(x, y,
863 x + width, x + height, 2, slider_width, wrap);
866 if (props->getBoolValue("editable"))
872 obj->setSize(width, height);
873 setupObject(obj, props);
874 setColor(obj, props, FOREGROUND|LABEL);
876 int top = props->getIntValue("top-line", 0);
877 obj->setTopLineInWindow(top < 0 ? unsigned(-1) >> 1 : top);
880 } else if (type == "select") {
881 fgSelectBox *obj = new fgSelectBox(x, y, x + width, y + height, props);
882 setupObject(obj, props);
883 setColor(obj, props, EDITFIELD);
885 } else if (type == "waypointlist") {
886 ScrolledWaypointList* obj = new ScrolledWaypointList(x, y, width, height);
887 setupObject(obj, props);
895 FGDialog::setupObject (puObject *object, SGPropertyNode *props)
897 GUIInfo *info = new GUIInfo(this);
898 object->setUserData(info);
899 _info.push_back(info);
900 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
901 object->makeReturnDefault(props->getBoolValue("default"));
904 if (props->hasValue("legend")) {
905 info->legend = props->getStringValue("legend");
906 object->setLegend(info->legend.c_str());
909 if (props->hasValue("label")) {
910 info->label = props->getStringValue("label");
911 object->setLabel(info->label.c_str());
914 if (props->hasValue("border"))
915 object->setBorderThickness( props->getIntValue("border", 2) );
917 if (SGPropertyNode *nft = props->getNode("font", false)) {
918 FGFontCache *fc = globals->get_fontcache();
919 puFont *lfnt = fc->get(nft);
920 object->setLabelFont(*lfnt);
921 object->setLegendFont(*lfnt);
923 object->setLabelFont(*_font);
926 if (props->hasChild("visible")) {
927 ConditionalObject* cnd = new ConditionalObject("visible", object);
928 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("visible")));
929 _conditionalObjects.push_back(cnd);
932 if (props->hasChild("enable")) {
933 ConditionalObject* cnd = new ConditionalObject("enable", object);
934 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("enable")));
935 _conditionalObjects.push_back(cnd);
938 string type = props->getName();
939 if (type == "input" && props->getBoolValue("live"))
940 object->setDownCallback(action_callback);
942 if (type == "text") {
943 const char *format = props->getStringValue("format", 0);
945 info->fmt_type = validate_format(format);
946 if (info->fmt_type != f_INVALID)
947 info->format = format;
949 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
954 if (props->hasValue("property")) {
955 const char *name = props->getStringValue("name");
958 const char *propname = props->getStringValue("property");
959 SGPropertyNode_ptr node = fgGetNode(propname, true);
961 // mapWidget binds to a sub-tree of properties, and
962 // ignroes the puValue mechanism, so special case things here
963 MapWidget* mw = static_cast<MapWidget*>(object);
964 mw->setProperty(node);
966 // normal widget, creating PropertyObject
967 copy_to_pui(node, object);
968 PropertyObject *po = new PropertyObject(name, object, node);
969 _propertyObjects.push_back(po);
970 if (props->getBoolValue("live"))
971 _liveObjects.push_back(po);
977 SGPropertyNode *dest = fgGetNode("/sim/bindings/gui", true);
978 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
979 if (bindings.size() > 0) {
980 info->key = props->getIntValue("keynum", -1);
981 if (props->hasValue("key"))
982 info->key = getKeyCode(props->getStringValue("key", ""));
984 for (unsigned int i = 0; i < bindings.size(); i++) {
986 SGPropertyNode_ptr binding;
987 while (dest->getChild("binding", j))
990 const char *cmd = bindings[i]->getStringValue("command");
991 if (!strcmp(cmd, "nasal"))
992 bindings[i]->setStringValue("module", _module.c_str());
994 binding = dest->getChild("binding", j, true);
995 copyProperties(bindings[i], binding);
996 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
998 object->setCallback(action_callback);
1003 FGDialog::setupGroup(puGroup *group, SGPropertyNode *props,
1004 int width, int height, bool makeFrame)
1006 setupObject(group, props);
1009 puFrame* f = new puFrame(0, 0, width, height);
1013 int nChildren = props->nChildren();
1014 for (int i = 0; i < nChildren; i++)
1015 makeObject(props->getChild(i), width, height);
1020 FGDialog::setColor(puObject *object, SGPropertyNode *props, int which)
1022 string type = props->getName();
1025 if (type == "textbox" && props->getBoolValue("editable"))
1026 type += "-editable";
1028 FGColor c(_gui->getColor("background"));
1029 c.merge(_gui->getColor(type));
1030 c.merge(props->getNode("color"));
1032 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
1040 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
1041 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
1042 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
1043 { LABEL, PUCOL_LABEL, "label", "color-label" },
1044 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
1045 { MISC, PUCOL_MISC, "misc", "color-misc" },
1046 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
1049 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
1051 for (int i = 0; i < numcol; i++) {
1056 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
1057 if (which & pucol[i].mask)
1058 dirty |= c.merge(props->getNode("color"));
1060 if ((pucol[i].mask == LABEL) && !c.isValid())
1061 dirty |= c.merge(_gui->getColor("label"));
1063 dirty |= c.merge(props->getNode(pucol[i].cname));
1065 if (c.isValid() && dirty)
1066 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
1095 {"f10", PU_KEY_F10},
1096 {"f11", PU_KEY_F11},
1097 {"f12", PU_KEY_F12},
1098 {"left", PU_KEY_LEFT},
1100 {"right", PU_KEY_RIGHT},
1101 {"down", PU_KEY_DOWN},
1102 {"pageup", PU_KEY_PAGE_UP},
1103 {"pagedn", PU_KEY_PAGE_DOWN},
1104 {"home", PU_KEY_HOME},
1105 {"end", PU_KEY_END},
1106 {"insert", PU_KEY_INSERT},
1111 FGDialog::getKeyCode(const char *str)
1122 char *buf = new char[strlen(str) + 1];
1124 char *s = buf + strlen(buf);
1125 while (s > str && s[-1] == ' ')
1132 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
1133 s += 5, mod |= CTRL;
1134 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
1135 s += 6, mod |= SHIFT;
1136 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
1143 if (strlen(s) == 1 && isascii(*s)) {
1148 key = toupper(key) - '@';
1150 ; // Alt not propagated to the gui
1153 for (char *t = s; *t; t++)
1155 for (int i = 0; keymap[i].name; i++) {
1156 if (!strcmp(s, keymap[i].name)) {
1157 key = keymap[i].key;
1166 void
\fFGDialog::relayout()
1168 _needsRelayout = false;
1170 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
1171 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
1173 bool userx = _props->hasValue("x");
1174 bool usery = _props->hasValue("y");
1175 bool userw = _props->hasValue("width");
1176 bool userh = _props->hasValue("height");
1178 // Let the layout widget work in the same property subtree.
1179 LayoutWidget wid(_props);
1180 wid.setDefaultFont(_font, int(_font->getPointSize()));
1183 int px, py, savex, savey;
1184 if (!userw || !userh) {
1185 wid.calcPrefSize(&pw, &ph);
1188 pw = _props->getIntValue("width", pw);
1189 ph = _props->getIntValue("height", ph);
1190 px = savex = _props->getIntValue("x", (screenw - pw) / 2);
1191 py = savey = _props->getIntValue("y", (screenh - ph) / 2);
1193 // Negative x/y coordinates are interpreted as distance from the top/right
1194 // corner rather than bottom/left.
1195 if (userx && px < 0)
1196 px = screenw - pw + px;
1197 if (usery && py < 0)
1198 py = screenh - ph + py;
1200 // Define "x", "y", "width" and/or "height" in the property tree if they
1201 // are not specified in the configuration file.
1202 wid.layout(px, py, pw, ph);
1206 // Remove automatically generated properties, so the layout looks
1207 // the same next time around, or restore x and y to preserve negative coords.
1209 _props->setIntValue("x", savex);
1211 _props->removeChild("x");
1214 _props->setIntValue("y", savey);
1216 _props->removeChild("y");
1218 if (!userw) _props->removeChild("width");
1219 if (!userh) _props->removeChild("height");
1223 FGDialog::applySize(puObject *object)
1225 // compound plib widgets use setUserData() for internal purposes, so refuse
1226 // to descend into anything that has other bits set than the following
1227 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
1228 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
1229 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
1231 int type = object->getType();
1232 if ((type & PUCLASS_GROUP) && !(type & ~validUserData)) {
1233 puObject* c = ((puGroup *)object)->getFirstChild();
1234 for (; c != NULL; c = c->getNextObject()) {
1236 } // of child iteration
1237 } // of group object case
1239 GUIInfo *info = (GUIInfo *)object->getUserData();
1243 SGPropertyNode *n = info->node;
1245 SG_LOG(SG_GENERAL, SG_ALERT, "FGDialog::applySize: no props");
1249 int x = n->getIntValue("x");
1250 int y = n->getIntValue("y");
1251 int w = n->getIntValue("width", 4);
1252 int h = n->getIntValue("height", 4);
1253 object->setPosition(x, y);
1254 object->setSize(w, h);
1257 ////////////////////////////////////////////////////////////////////////
1258 // Implementation of FGDialog::PropertyObject.
1259 ////////////////////////////////////////////////////////////////////////
1261 FGDialog::PropertyObject::PropertyObject(const char *n,
1262 puObject *o, SGPropertyNode_ptr p) :
1272 ////////////////////////////////////////////////////////////////////////
1273 // Implementation of fgValueList and derived pui widgets
1274 ////////////////////////////////////////////////////////////////////////
1277 fgValueList::fgValueList(SGPropertyNode *p) :
1284 fgValueList::update()
1290 fgValueList::~fgValueList()
1296 fgValueList::make_list()
1298 SGPropertyNode_ptr values = _props;
1299 const char* vname = "value";
1301 if (_props->hasChild("properties")) {
1302 // dynamic values, read from a property's children
1303 const char* path = _props->getStringValue("properties");
1304 values = fgGetNode(path, true);
1307 if (_props->hasChild("property-name")) {
1308 vname = _props->getStringValue("property-name");
1311 vector<SGPropertyNode_ptr> value_nodes = values->getChildren(vname);
1312 _list = new char *[value_nodes.size() + 1];
1314 for (i = 0; i < value_nodes.size(); i++) {
1315 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
1321 fgValueList::destroy_list()
1323 for (int i = 0; _list[i] != 0; i++)
1334 fgValueList::update();
1335 int top = getTopItem();
1340 void fgComboBox::update()
1346 std::string curValue(getStringValue());
1347 fgValueList::update();
1349 int currentItem = puaComboBox::getCurrentItem();
1352 // look for the previous value, in the new list
1353 for (int i = 0; _list[i] != 0; i++) {
1354 if (_list[i] == curValue) {
1355 // don't set the current item again; this is important to avoid
1356 // recursion here if the combo callback ultimately causes another dialog-update
1357 if (i != currentItem) {
1358 puaComboBox::setCurrentItem(i);
1363 } // of list values iteration
1365 // cound't find current item, default to first
1366 if (currentItem != 0) {
1367 puaComboBox::setCurrentItem(0);
1371 int fgComboBox::checkHit(int b, int up, int x, int y)
1374 int r = puaComboBox::checkHit(b, up, x, y);
1379 void fgComboBox::setSize(int w, int h)
1381 puaComboBox::setSize(w, h);
1385 void fgComboBox::recalc_bbox()
1387 // bug-fix for issue #192
1388 // http://code.google.com/p/flightgear-bugs/issues/detail?id=192
1389 // puaComboBox is including the height of its popupMenu in the height
1390 // computation, which breaks layout computations.
1391 // this implementation skips popup-menus
1396 for (puObject *bo = dlist; bo != NULL; bo = bo -> getNextObject()) {
1397 if (bo->getType() & PUCLASS_POPUPMENU) {
1401 contents.extend (bo -> getBBox()) ;
1404 abox.max[0] = abox.min[0] + contents.max[0] ;
1405 abox.max[1] = abox.min[1] + contents.max[1] ;
1407 puObject::recalc_bbox () ;
1411 // end of dialog.cxx