1 // dialog.cxx: implementation of an XML-configurable dialog box.
7 #include <simgear/structure/SGBinding.hxx>
8 #include <simgear/props/props_io.hxx>
10 #include <Scripting/NasalSys.hxx>
11 #include <Main/fg_os.hxx>
12 #include <Main/globals.hxx>
13 #include <Main/fg_props.hxx>
15 #include "FGPUIDialog.hxx"
16 #include "new_gui.hxx"
17 #include "AirportList.hxx"
18 #include "property_list.hxx"
20 #include "WaypointList.hxx"
21 #include "CanvasWidget.hxx"
22 #include "MapWidget.hxx"
23 #include "FGFontCache.hxx"
24 #include "FGColor.hxx"
26 enum format_type { f_INVALID, f_INT, f_LONG, f_FLOAT, f_DOUBLE, f_STRING };
27 static const int FORMAT_BUFSIZE = 255;
28 static const int RESIZE_MARGIN = 7;
32 * Makes sure the format matches '%[ -+#]?\d*(\.\d*)?(l?[df]|s)', with
33 * only one number or string placeholder and otherwise arbitrary prefix
34 * and postfix containing only quoted percent signs (%%).
37 validate_format(const char *f)
51 while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
53 while (*f && isdigit(*f))
57 while (*f && isdigit(*f))
65 type = l ? f_LONG : f_INT;
67 type = l ? f_DOUBLE : f_FLOAT;
87 ////////////////////////////////////////////////////////////////////////
88 // Implementation of GUIInfo.
89 ////////////////////////////////////////////////////////////////////////
92 * User data for a GUI object.
96 GUIInfo(FGPUIDialog *d);
98 void apply_format(SGPropertyNode *);
101 SGPropertyNode_ptr node;
102 vector <SGBinding *> bindings;
104 string label, legend, text, format;
105 format_type fmt_type;
108 GUIInfo::GUIInfo (FGPUIDialog *d) :
117 for (unsigned int i = 0; i < bindings.size(); i++) {
123 void GUIInfo::apply_format(SGPropertyNode *n)
125 char buf[FORMAT_BUFSIZE + 1];
126 if (fmt_type == f_INT)
127 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getIntValue());
128 else if (fmt_type == f_LONG)
129 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getLongValue());
130 else if (fmt_type == f_FLOAT)
131 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getFloatValue());
132 else if (fmt_type == f_DOUBLE)
133 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getDoubleValue());
135 snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getStringValue());
137 buf[FORMAT_BUFSIZE] = '\0';
146 int fgPopup::checkKey(int key, int updown)
148 if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
151 puObject *input = getActiveInputField(this);
153 return input->checkKey(key, updown);
155 puObject *object = getKeyObject(this, key);
157 return puPopup::checkKey(key, updown);
159 // invokeCallback() isn't enough; we need to simulate a mouse button press
160 object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
161 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
162 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
163 object->checkHit(PU_LEFT_BUTTON, PU_UP,
164 (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
165 (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
169 puObject *fgPopup::getKeyObject(puObject *object, int key)
172 if (object->getType() & PUCLASS_GROUP)
173 for (puObject *obj = ((puGroup *)object)->getFirstChild();
174 obj; obj = obj->getNextObject())
175 if ((ret = getKeyObject(obj, key)))
178 GUIInfo *info = (GUIInfo *)object->getUserData();
179 if (info && info->key == key)
185 puObject *fgPopup::getActiveInputField(puObject *object)
188 if (object->getType() & PUCLASS_GROUP)
189 for (puObject *obj = ((puGroup *)object)->getFirstChild();
190 obj; obj = obj->getNextObject())
191 if ((ret = getActiveInputField(obj)))
194 if (object->getType() & (PUCLASS_INPUT|PUCLASS_LARGEINPUT)
195 && ((puInput *)object)->isAcceptingInput())
204 int fgPopup::checkHit(int button, int updown, int x, int y)
207 if (updown != PU_DRAG && !_dragging)
208 result = puPopup::checkHit(button, updown, x, y);
213 // This is annoying. We would really want a true result from the
214 // superclass to indicate "handled by child object", but all it
215 // tells us is that the pointer is inside the dialog. So do the
216 // intersection test (again) to make sure we don't start a drag
217 // when inside controls.
219 if (updown == PU_DOWN && !_dragging) {
222 int global_drag = fgGetKeyModifiers() & KEYMOD_SHIFT;
223 int global_resize = fgGetKeyModifiers() & KEYMOD_CTRL;
225 int hit = getHitObjects(this, x, y);
226 if (hit & PUCLASS_LIST) // ctrl-click in property browser (toggle bool)
228 if (!global_resize && hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
229 |PUCLASS_LARGEINPUT|PUCLASS_SCROLLBAR))
232 getPosition(&_dlgX, &_dlgY);
233 getSize(&_dlgW, &_dlgH);
234 _start_cursor = fgGetMouseCursor();
239 // check and prepare for resizing
240 static const int cursor[] = {
241 MOUSE_CURSOR_POINTER, MOUSE_CURSOR_LEFTSIDE, MOUSE_CURSOR_RIGHTSIDE, 0,
242 MOUSE_CURSOR_TOPSIDE, MOUSE_CURSOR_TOPLEFT, MOUSE_CURSOR_TOPRIGHT, 0,
243 MOUSE_CURSOR_BOTTOMSIDE, MOUSE_CURSOR_BOTTOMLEFT, MOUSE_CURSOR_BOTTOMRIGHT, 0,
247 if (!global_drag && _resizable) {
248 int hmargin = global_resize ? _dlgW / 3 : RESIZE_MARGIN;
249 int vmargin = global_resize ? _dlgH / 3 : RESIZE_MARGIN;
251 if (y - _dlgY < vmargin)
253 else if (_dlgY + _dlgH - y < vmargin)
256 if (x - _dlgX < hmargin)
258 else if (_dlgX + _dlgW - x < hmargin)
261 if (!_resizing && global_resize)
262 _resizing = BOTTOM|RIGHT;
264 _cursor = cursor[_resizing];
265 if (_resizing && _resizable)
266 fgSetMouseCursor(_cursor);
269 } else if (updown == PU_DRAG && _dragging) {
271 GUIInfo *info = (GUIInfo *)getUserData();
272 if (_resizable && info && info->node) {
275 if (_resizing & LEFT)
277 if (_resizing & RIGHT)
281 if (_resizing & BOTTOM)
285 LayoutWidget wid(info->node);
286 wid.calcPrefSize(&prefw, &prefh);
294 if (_resizing & LEFT)
296 if (_resizing & BOTTOM)
299 wid.layout(x, y, w, h);
302 applySize(static_cast<puObject *>(this));
303 getFirstChild()->setSize(w, h); // dialog background puFrame
306 int posX = x + _dlgX - _startX,
307 posY = y + _dlgY - _startY;
308 setPosition(posX, posY);
310 GUIInfo *info = (GUIInfo *)getUserData();
311 if (info && info->node) {
312 info->node->setIntValue("x", posX);
313 info->node->setIntValue("y", posY);
317 } else if (_dragging) {
318 fgSetMouseCursor(_start_cursor);
324 int fgPopup::getHitObjects(puObject *object, int x, int y)
326 if (!object->isVisible())
330 if (object->getType() & PUCLASS_GROUP)
331 for (puObject *obj = ((puGroup *)object)->getFirstChild();
332 obj; obj = obj->getNextObject())
333 type |= getHitObjects(obj, x, y);
336 object->getAbsolutePosition(&cx, &cy);
337 object->getSize(&cw, &ch);
338 if (x >= cx && x < cx + cw && y >= cy && y < cy + ch)
339 type |= object->getType();
343 void fgPopup::applySize(puObject *object)
345 // compound plib widgets use setUserData() for internal purposes, so refuse
346 // to descend into anything that has other bits set than the following
347 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
348 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
349 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
351 int type = object->getType();
352 if (type & PUCLASS_GROUP && !(type & ~validUserData))
353 for (puObject *obj = ((puGroup *)object)->getFirstChild();
354 obj; obj = obj->getNextObject())
357 GUIInfo *info = (GUIInfo *)object->getUserData();
361 SGPropertyNode *n = info->node;
363 SG_LOG(SG_GENERAL, SG_ALERT, "fgPopup::applySize: no props");
366 int x = n->getIntValue("x");
367 int y = n->getIntValue("y");
368 int w = n->getIntValue("width", 4);
369 int h = n->getIntValue("height", 4);
370 object->setPosition(x, y);
371 object->setSize(w, h);
374 ////////////////////////////////////////////////////////////////////////
376 void FGPUIDialog::ConditionalObject::update(FGPUIDialog* aDlg)
378 if (_name == "visible") {
379 bool wasVis = _pu->isVisible() != 0;
380 bool newVis = test();
382 if (newVis == wasVis) {
386 if (newVis) { // puObject needs a setVisible. Oh well.
391 } else if (_name == "enable") {
392 bool wasEnabled = _pu->isActive() != 0;
393 bool newEnable = test();
395 if (wasEnabled == newEnable) {
406 aDlg->setNeedsLayout();
408 \f////////////////////////////////////////////////////////////////////////
410 ////////////////////////////////////////////////////////////////////////
416 action_callback (puObject *object)
418 GUIInfo *info = (GUIInfo *)object->getUserData();
419 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
420 gui->setActiveDialog(info->dialog);
421 int nBindings = info->bindings.size();
422 for (int i = 0; i < nBindings; i++) {
423 info->bindings[i]->fire();
424 if (gui->getActiveDialog() == 0)
427 gui->setActiveDialog(0);
432 ////////////////////////////////////////////////////////////////////////
433 // Static helper functions.
434 ////////////////////////////////////////////////////////////////////////
437 * Copy a property value to a PUI object.
440 copy_to_pui (SGPropertyNode *node, puObject *object)
442 using namespace simgear;
443 GUIInfo *info = (GUIInfo *)object->getUserData();
445 SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
446 return; // this can't really happen
449 // Treat puText objects specially, so their "values" can be set
451 if (object->getType() & PUCLASS_TEXT) {
452 if (info->fmt_type != f_INVALID)
453 info->apply_format(node);
455 info->text = node->getStringValue();
457 object->setLabel(info->text.c_str());
461 switch (node->getType()) {
465 object->setValue(node->getIntValue());
469 object->setValue(node->getFloatValue());
472 info->text = node->getStringValue();
473 object->setValue(info->text.c_str());
480 copy_from_pui (puObject *object, SGPropertyNode *node)
482 using namespace simgear;
483 // puText objects are immutable, so should not be copied out
484 if (object->getType() & PUCLASS_TEXT)
487 switch (node->getType()) {
491 node->setIntValue(object->getIntegerValue());
495 node->setFloatValue(object->getFloatValue());
498 const char *s = object->getStringValue();
500 node->setStringValue(s);
507 ////////////////////////////////////////////////////////////////////////
508 // Implementation of FGDialog.
509 ////////////////////////////////////////////////////////////////////////
511 FGPUIDialog::FGPUIDialog (SGPropertyNode *props) :
514 _gui((NewGUI *)globals->get_subsystem("gui")),
516 _needsRelayout(false)
518 _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
520 SGPropertyNode *nasal = props->getNode("nasal");
522 _nasal_close = nasal->getNode("close");
523 SGPropertyNode *open = nasal->getNode("open");
525 const char *s = open->getStringValue();
526 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
528 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
534 FGPUIDialog::~FGPUIDialog ()
537 _object->getAbsolutePosition(&x, &y);
538 _props->setIntValue("lastx", x);
539 _props->setIntValue("lasty", y);
541 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
544 const char *s = _nasal_close->getStringValue();
545 nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
547 nas->deleteModule(_module.c_str());
550 puDeleteObject(_object);
553 // Delete all the info objects we
554 // were forced to keep around because
555 // PUI cannot delete its own user data.
556 for (i = 0; i < _info.size(); i++) {
557 delete (GUIInfo *)_info[i];
560 // Finally, delete the property links.
561 for (i = 0; i < _propertyObjects.size(); i++) {
562 delete _propertyObjects[i];
563 _propertyObjects[i] = 0;
568 FGPUIDialog::updateValues (const char *objectName)
570 if (objectName && !objectName[0])
573 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
574 const string &name = _propertyObjects[i]->name;
575 if (objectName && name != objectName) {
579 puObject *widget = _propertyObjects[i]->object;
580 int widgetType = widget->getType();
581 if (widgetType & PUCLASS_LIST) {
582 GUI_ID* gui_id = dynamic_cast<GUI_ID *>(widget);
583 if (gui_id && (gui_id->id & FGCLASS_LIST)) {
584 fgList *pl = static_cast<fgList*>(widget);
587 copy_to_pui(_propertyObjects[i]->node, widget);
589 } else if (widgetType & PUCLASS_COMBOBOX) {
590 fgComboBox* combo = static_cast<fgComboBox*>(widget);
593 copy_to_pui(_propertyObjects[i]->node, widget);
595 } // of property objects iteration
599 FGPUIDialog::applyValues (const char *objectName)
601 if (objectName && !objectName[0])
604 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
605 const string &name = _propertyObjects[i]->name;
606 if (objectName && name != objectName)
609 copy_from_pui(_propertyObjects[i]->object,
610 _propertyObjects[i]->node);
615 FGPUIDialog::update ()
617 for (unsigned int i = 0; i < _liveObjects.size(); i++) {
618 puObject *obj = _liveObjects[i]->object;
619 if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
622 copy_to_pui(_liveObjects[i]->node, obj);
625 for (unsigned int j=0; j < _conditionalObjects.size(); ++j) {
626 _conditionalObjects[j]->update(this);
629 if (_needsRelayout) {
635 FGPUIDialog::display (SGPropertyNode *props)
638 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
642 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
643 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
645 bool userx = props->hasValue("x");
646 bool usery = props->hasValue("y");
647 bool userw = props->hasValue("width");
648 bool userh = props->hasValue("height");
650 // Let the layout widget work in the same property subtree.
651 LayoutWidget wid(props);
653 SGPropertyNode *fontnode = props->getNode("font");
655 FGFontCache *fc = globals->get_fontcache();
656 _font = fc->get(fontnode);
658 _font = _gui->getDefaultFont();
660 wid.setDefaultFont(_font, int(_font->getPointSize()));
663 int px, py, savex, savey;
664 if (!userw || !userh)
665 wid.calcPrefSize(&pw, &ph);
666 pw = props->getIntValue("width", pw);
667 ph = props->getIntValue("height", ph);
668 px = savex = props->getIntValue("x", (screenw - pw) / 2);
669 py = savey = props->getIntValue("y", (screenh - ph) / 2);
671 // Negative x/y coordinates are interpreted as distance from the top/right
672 // corner rather than bottom/left.
674 px = screenw - pw + px;
676 py = screenh - ph + py;
678 // Define "x", "y", "width" and/or "height" in the property tree if they
679 // are not specified in the configuration file.
680 wid.layout(px, py, pw, ph);
682 // Use the dimension and location properties as specified in the
683 // configuration file or from the layout widget.
684 _object = makeObject(props, screenw, screenh);
686 // Remove automatically generated properties, so the layout looks
687 // the same next time around, or restore x and y to preserve negative coords.
689 props->setIntValue("x", savex);
691 props->removeChild("x");
694 props->setIntValue("y", savey);
696 props->removeChild("y");
698 if (!userw) props->removeChild("width");
699 if (!userh) props->removeChild("height");
704 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
705 << props->getStringValue("name", "[unnamed]")
706 << " does not contain a proper GUI definition");
711 FGPUIDialog::makeObject (SGPropertyNode *props, int parentWidth, int parentHeight)
713 if (!props->getBoolValue("enabled", true))
716 bool presetSize = props->hasValue("width") && props->hasValue("height");
717 int width = props->getIntValue("width", parentWidth);
718 int height = props->getIntValue("height", parentHeight);
719 int x = props->getIntValue("x", (parentWidth - width) / 2);
720 int y = props->getIntValue("y", (parentHeight - height) / 2);
721 string type = props->getName();
726 if (type == "dialog") {
728 bool draggable = props->getBoolValue("draggable", true);
729 bool resizable = props->getBoolValue("resizable", false);
730 if (props->getBoolValue("modal", false))
731 obj = new puDialogBox(x, y);
733 obj = new fgPopup(x, y, resizable, draggable);
734 setupGroup(obj, props, width, height, true);
735 setColor(obj, props);
738 } else if (type == "group") {
739 puGroup *obj = new puGroup(x, y);
740 setupGroup(obj, props, width, height, false);
741 setColor(obj, props);
744 } else if (type == "frame") {
745 puGroup *obj = new puGroup(x, y);
746 setupGroup(obj, props, width, height, true);
747 setColor(obj, props);
750 } else if (type == "hrule" || type == "vrule") {
751 puFrame *obj = new puFrame(x, y, x + width, y + height);
752 obj->setBorderThickness(0);
753 setupObject(obj, props);
754 setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
757 } else if (type == "list") {
758 int slider_width = props->getIntValue("slider", 20);
759 fgList *obj = new fgList(x, y, x + width, y + height, props, slider_width);
761 obj->setSize(width, height);
762 setupObject(obj, props);
763 setColor(obj, props);
766 } else if (type == "airport-list") {
767 AirportList *obj = new AirportList(x, y, x + width, y + height);
769 obj->setSize(width, height);
770 setupObject(obj, props);
771 setColor(obj, props);
774 } else if (type == "property-list") {
775 PropertyList *obj = new PropertyList(x, y, x + width, y + height, globals->get_props());
777 obj->setSize(width, height);
778 setupObject(obj, props);
779 setColor(obj, props);
782 } else if (type == "input") {
783 puInput *obj = new puInput(x, y, x + width, y + height);
784 setupObject(obj, props);
785 setColor(obj, props, FOREGROUND|LABEL);
788 } else if (type == "text") {
789 puText *obj = new puText(x, y);
790 setupObject(obj, props);
792 // Layed-out objects need their size set, and non-layout ones
793 // get a different placement.
795 obj->setSize(width, height);
797 obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
798 setColor(obj, props, LABEL);
801 } else if (type == "checkbox") {
803 obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
804 setupObject(obj, props);
805 setColor(obj, props, FOREGROUND|LABEL);
808 } else if (type == "radio") {
810 obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
811 setupObject(obj, props);
812 setColor(obj, props, FOREGROUND|LABEL);
815 } else if (type == "button") {
817 const char *legend = props->getStringValue("legend", "[none]");
818 if (props->getBoolValue("one-shot", true))
819 obj = new puOneShot(x, y, legend);
821 obj = new puButton(x, y, legend);
823 obj->setSize(width, height);
824 setupObject(obj, props);
825 setColor(obj, props);
827 } else if (type == "map") {
828 MapWidget* mapWidget = new MapWidget(x, y, x + width, y + height);
829 setupObject(mapWidget, props);
831 } else if (type == "canvas") {
832 CanvasWidget* canvasWidget = new CanvasWidget( x, y,
833 x + width, y + height,
836 setupObject(canvasWidget, props);
838 } else if (type == "combo") {
839 fgComboBox *obj = new fgComboBox(x, y, x + width, y + height, props,
840 props->getBoolValue("editable", false));
841 setupObject(obj, props);
842 setColor(obj, props, EDITFIELD);
845 } else if (type == "slider") {
846 bool vertical = props->getBoolValue("vertical", false);
847 puSlider *obj = new puSlider(x, y, (vertical ? height : width), vertical);
848 obj->setMinValue(props->getFloatValue("min", 0.0));
849 obj->setMaxValue(props->getFloatValue("max", 1.0));
850 obj->setStepSize(props->getFloatValue("step"));
851 obj->setSliderFraction(props->getFloatValue("fraction"));
852 #if PLIB_VERSION > 185
853 obj->setPageStepSize(props->getFloatValue("pagestep"));
855 setupObject(obj, props);
857 obj->setSize(width, height);
858 setColor(obj, props, FOREGROUND|LABEL);
861 } else if (type == "dial") {
862 puDial *obj = new puDial(x, y, width);
863 obj->setMinValue(props->getFloatValue("min", 0.0));
864 obj->setMaxValue(props->getFloatValue("max", 1.0));
865 obj->setWrap(props->getBoolValue("wrap", true));
866 setupObject(obj, props);
867 setColor(obj, props, FOREGROUND|LABEL);
870 } else if (type == "textbox") {
871 int slider_width = props->getIntValue("slider", 20);
872 int wrap = props->getBoolValue("wrap", true);
873 #if PLIB_VERSION > 185
874 puaLargeInput *obj = new puaLargeInput(x, y,
875 x + width, x + height, 11, slider_width, wrap);
877 puaLargeInput *obj = new puaLargeInput(x, y,
878 x + width, x + height, 2, slider_width, wrap);
881 if (props->getBoolValue("editable"))
887 obj->setSize(width, height);
888 setupObject(obj, props);
889 setColor(obj, props, FOREGROUND|LABEL);
891 int top = props->getIntValue("top-line", 0);
892 obj->setTopLineInWindow(top < 0 ? unsigned(-1) >> 1 : top);
895 } else if (type == "select") {
896 fgSelectBox *obj = new fgSelectBox(x, y, x + width, y + height, props);
897 setupObject(obj, props);
898 setColor(obj, props, EDITFIELD);
900 } else if (type == "waypointlist") {
901 ScrolledWaypointList* obj = new ScrolledWaypointList(x, y, width, height);
902 setupObject(obj, props);
910 FGPUIDialog::setupObject (puObject *object, SGPropertyNode *props)
912 GUIInfo *info = new GUIInfo(this);
913 object->setUserData(info);
914 _info.push_back(info);
915 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
916 object->makeReturnDefault(props->getBoolValue("default"));
919 if (props->hasValue("legend")) {
920 info->legend = props->getStringValue("legend");
921 object->setLegend(info->legend.c_str());
924 if (props->hasValue("label")) {
925 info->label = props->getStringValue("label");
926 object->setLabel(info->label.c_str());
929 if (props->hasValue("border"))
930 object->setBorderThickness( props->getIntValue("border", 2) );
932 if (SGPropertyNode *nft = props->getNode("font", false)) {
933 FGFontCache *fc = globals->get_fontcache();
934 puFont *lfnt = fc->get(nft);
935 object->setLabelFont(*lfnt);
936 object->setLegendFont(*lfnt);
938 object->setLabelFont(*_font);
941 if (props->hasChild("visible")) {
942 ConditionalObject* cnd = new ConditionalObject("visible", object);
943 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("visible")));
944 _conditionalObjects.push_back(cnd);
947 if (props->hasChild("enable")) {
948 ConditionalObject* cnd = new ConditionalObject("enable", object);
949 cnd->setCondition(sgReadCondition(globals->get_props(), props->getChild("enable")));
950 _conditionalObjects.push_back(cnd);
953 string type = props->getName();
954 if (type == "input" && props->getBoolValue("live"))
955 object->setDownCallback(action_callback);
957 if (type == "text") {
958 const char *format = props->getStringValue("format", 0);
960 info->fmt_type = validate_format(format);
961 if (info->fmt_type != f_INVALID)
962 info->format = format;
964 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
969 if (props->hasValue("property")) {
970 const char *name = props->getStringValue("name");
973 const char *propname = props->getStringValue("property");
974 SGPropertyNode_ptr node = fgGetNode(propname, true);
976 // mapWidget binds to a sub-tree of properties, and
977 // ignores the puValue mechanism, so special case things here
978 MapWidget* mw = static_cast<MapWidget*>(object);
979 mw->setProperty(node);
981 // normal widget, creating PropertyObject
982 copy_to_pui(node, object);
983 PropertyObject *po = new PropertyObject(name, object, node);
984 _propertyObjects.push_back(po);
985 if (props->getBoolValue("live"))
986 _liveObjects.push_back(po);
992 SGPropertyNode *dest = fgGetNode("/sim/bindings/gui", true);
993 vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
994 if (bindings.size() > 0) {
995 info->key = props->getIntValue("keynum", -1);
996 if (props->hasValue("key"))
997 info->key = getKeyCode(props->getStringValue("key", ""));
999 for (unsigned int i = 0; i < bindings.size(); i++) {
1001 SGPropertyNode_ptr binding;
1002 while (dest->getChild("binding", j))
1005 const char *cmd = bindings[i]->getStringValue("command");
1006 if (!strcmp(cmd, "nasal"))
1007 bindings[i]->setStringValue("module", _module.c_str());
1009 binding = dest->getChild("binding", j, true);
1010 copyProperties(bindings[i], binding);
1011 info->bindings.push_back(new SGBinding(binding, globals->get_props()));
1013 object->setCallback(action_callback);
1018 FGPUIDialog::setupGroup(puGroup *group, SGPropertyNode *props,
1019 int width, int height, bool makeFrame)
1021 setupObject(group, props);
1024 puFrame* f = new puFrame(0, 0, width, height);
1028 int nChildren = props->nChildren();
1029 for (int i = 0; i < nChildren; i++)
1030 makeObject(props->getChild(i), width, height);
1035 FGPUIDialog::setColor(puObject *object, SGPropertyNode *props, int which)
1037 string type = props->getName();
1040 if (type == "textbox" && props->getBoolValue("editable"))
1041 type += "-editable";
1043 FGColor c(_gui->getColor("background"));
1044 c.merge(_gui->getColor(type));
1045 c.merge(props->getNode("color"));
1047 object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
1055 { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
1056 { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
1057 { HIGHLIGHT, PUCOL_HIGHLIGHT, "highlight", "color-highlight" },
1058 { LABEL, PUCOL_LABEL, "label", "color-label" },
1059 { LEGEND, PUCOL_LEGEND, "legend", "color-legend" },
1060 { MISC, PUCOL_MISC, "misc", "color-misc" },
1061 { EDITFIELD, PUCOL_EDITFIELD, "editfield", "color-editfield" },
1064 const int numcol = sizeof(pucol) / sizeof(pucol[0]);
1066 for (int i = 0; i < numcol; i++) {
1071 dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
1072 if (which & pucol[i].mask)
1073 dirty |= c.merge(props->getNode("color"));
1075 if ((pucol[i].mask == LABEL) && !c.isValid())
1076 dirty |= c.merge(_gui->getColor("label"));
1078 dirty |= c.merge(props->getNode(pucol[i].cname));
1080 if (c.isValid() && dirty)
1081 object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
1110 {"f10", PU_KEY_F10},
1111 {"f11", PU_KEY_F11},
1112 {"f12", PU_KEY_F12},
1113 {"left", PU_KEY_LEFT},
1115 {"right", PU_KEY_RIGHT},
1116 {"down", PU_KEY_DOWN},
1117 {"pageup", PU_KEY_PAGE_UP},
1118 {"pagedn", PU_KEY_PAGE_DOWN},
1119 {"home", PU_KEY_HOME},
1120 {"end", PU_KEY_END},
1121 {"insert", PU_KEY_INSERT},
1126 FGPUIDialog::getKeyCode(const char *str)
1137 char *buf = new char[strlen(str) + 1];
1139 char *s = buf + strlen(buf);
1140 while (s > str && s[-1] == ' ')
1147 if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
1148 s += 5, mod |= CTRL;
1149 else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
1150 s += 6, mod |= SHIFT;
1151 else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
1158 if (strlen(s) == 1 && isascii(*s)) {
1163 key = toupper(key) - '@';
1165 ; // Alt not propagated to the gui
1168 for (char *t = s; *t; t++)
1170 for (int i = 0; keymap[i].name; i++) {
1171 if (!strcmp(s, keymap[i].name)) {
1172 key = keymap[i].key;
1181 void
\fFGPUIDialog::relayout()
1183 _needsRelayout = false;
1185 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
1186 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
1188 bool userx = _props->hasValue("x");
1189 bool usery = _props->hasValue("y");
1190 bool userw = _props->hasValue("width");
1191 bool userh = _props->hasValue("height");
1193 // Let the layout widget work in the same property subtree.
1194 LayoutWidget wid(_props);
1195 wid.setDefaultFont(_font, int(_font->getPointSize()));
1198 int px, py, savex, savey;
1199 if (!userw || !userh) {
1200 wid.calcPrefSize(&pw, &ph);
1203 pw = _props->getIntValue("width", pw);
1204 ph = _props->getIntValue("height", ph);
1205 px = savex = _props->getIntValue("x", (screenw - pw) / 2);
1206 py = savey = _props->getIntValue("y", (screenh - ph) / 2);
1208 // Negative x/y coordinates are interpreted as distance from the top/right
1209 // corner rather than bottom/left.
1210 if (userx && px < 0)
1211 px = screenw - pw + px;
1212 if (usery && py < 0)
1213 py = screenh - ph + py;
1215 // Define "x", "y", "width" and/or "height" in the property tree if they
1216 // are not specified in the configuration file.
1217 wid.layout(px, py, pw, ph);
1221 // Remove automatically generated properties, so the layout looks
1222 // the same next time around, or restore x and y to preserve negative coords.
1224 _props->setIntValue("x", savex);
1226 _props->removeChild("x");
1229 _props->setIntValue("y", savey);
1231 _props->removeChild("y");
1233 if (!userw) _props->removeChild("width");
1234 if (!userh) _props->removeChild("height");
1238 FGPUIDialog::applySize(puObject *object)
1240 // compound plib widgets use setUserData() for internal purposes, so refuse
1241 // to descend into anything that has other bits set than the following
1242 const int validUserData = PUCLASS_VALUE|PUCLASS_OBJECT|PUCLASS_GROUP|PUCLASS_INTERFACE
1243 |PUCLASS_FRAME|PUCLASS_TEXT|PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT
1244 |PUCLASS_ARROW|PUCLASS_DIAL|PUCLASS_POPUP;
1246 int type = object->getType();
1247 if ((type & PUCLASS_GROUP) && !(type & ~validUserData)) {
1248 puObject* c = ((puGroup *)object)->getFirstChild();
1249 for (; c != NULL; c = c->getNextObject()) {
1251 } // of child iteration
1252 } // of group object case
1254 GUIInfo *info = (GUIInfo *)object->getUserData();
1258 SGPropertyNode *n = info->node;
1260 SG_LOG(SG_GENERAL, SG_ALERT, "FGDialog::applySize: no props");
1264 int x = n->getIntValue("x");
1265 int y = n->getIntValue("y");
1266 int w = n->getIntValue("width", 4);
1267 int h = n->getIntValue("height", 4);
1268 object->setPosition(x, y);
1269 object->setSize(w, h);
1272 ////////////////////////////////////////////////////////////////////////
1273 // Implementation of FGDialog::PropertyObject.
1274 ////////////////////////////////////////////////////////////////////////
1276 FGPUIDialog::PropertyObject::PropertyObject(const char *n,
1277 puObject *o, SGPropertyNode_ptr p) :
1287 ////////////////////////////////////////////////////////////////////////
1288 // Implementation of fgValueList and derived pui widgets
1289 ////////////////////////////////////////////////////////////////////////
1292 fgValueList::fgValueList(SGPropertyNode *p) :
1299 fgValueList::update()
1305 fgValueList::~fgValueList()
1311 fgValueList::make_list()
1313 SGPropertyNode_ptr values = _props;
1314 const char* vname = "value";
1316 if (_props->hasChild("properties")) {
1317 // dynamic values, read from a property's children
1318 const char* path = _props->getStringValue("properties");
1319 values = fgGetNode(path, true);
1322 if (_props->hasChild("property-name")) {
1323 vname = _props->getStringValue("property-name");
1326 vector<SGPropertyNode_ptr> value_nodes = values->getChildren(vname);
1327 _list = new char *[value_nodes.size() + 1];
1329 for (i = 0; i < value_nodes.size(); i++) {
1330 _list[i] = strdup((char *)value_nodes[i]->getStringValue());
1336 fgValueList::destroy_list()
1338 for (int i = 0; _list[i] != 0; i++)
1349 fgValueList::update();
1350 int top = getTopItem();
1355 void fgComboBox::update()
1361 std::string curValue(getStringValue());
1362 fgValueList::update();
1364 int currentItem = puaComboBox::getCurrentItem();
1367 // look for the previous value, in the new list
1368 for (int i = 0; _list[i] != 0; i++) {
1369 if (_list[i] == curValue) {
1370 // don't set the current item again; this is important to avoid
1371 // recursion here if the combo callback ultimately causes another dialog-update
1372 if (i != currentItem) {
1373 puaComboBox::setCurrentItem(i);
1378 } // of list values iteration
1380 // cound't find current item, default to first
1381 if (currentItem != 0) {
1382 puaComboBox::setCurrentItem(0);
1386 int fgComboBox::checkHit(int b, int up, int x, int y)
1389 int r = puaComboBox::checkHit(b, up, x, y);
1394 void fgComboBox::setSize(int w, int h)
1396 puaComboBox::setSize(w, h);
1400 void fgComboBox::recalc_bbox()
1402 // bug-fix for issue #192
1403 // http://code.google.com/p/flightgear-bugs/issues/detail?id=192
1404 // puaComboBox is including the height of its popupMenu in the height
1405 // computation, which breaks layout computations.
1406 // this implementation skips popup-menus
1411 for (puObject *bo = dlist; bo != NULL; bo = bo -> getNextObject()) {
1412 if (bo->getType() & PUCLASS_POPUPMENU) {
1416 contents.extend (bo -> getBBox()) ;
1419 abox.max[0] = abox.min[0] + contents.max[0] ;
1420 abox.max[1] = abox.min[1] + contents.max[1] ;
1422 puObject::recalc_bbox () ;
1426 // end of FGPUIDialog.cxx